3
0

nohup.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* vi: set sw=4 ts=4: */
  2. /* nohup - invoke a utility immune to hangups.
  3. *
  4. * Busybox version based on nohup specification at
  5. * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
  6. *
  7. * Copyright 2006 Rob Landley <rob@landley.net>
  8. * Copyright 2006 Bernhard Reutner-Fischer
  9. *
  10. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  11. */
  12. //usage:#define nohup_trivial_usage
  13. //usage: "PROG ARGS"
  14. //usage:#define nohup_full_usage "\n\n"
  15. //usage: "Run PROG immune to hangups, with output to a non-tty"
  16. //usage:
  17. //usage:#define nohup_example_usage
  18. //usage: "$ nohup make &"
  19. #include "libbb.h"
  20. /* Compat info: nohup (GNU coreutils 6.8) does this:
  21. # nohup true
  22. nohup: ignoring input and appending output to `nohup.out'
  23. # nohup true 1>/dev/null
  24. nohup: ignoring input and redirecting stderr to stdout
  25. # nohup true 2>zz
  26. # cat zz
  27. nohup: ignoring input and appending output to `nohup.out'
  28. # nohup true 2>zz 1>/dev/null
  29. # cat zz
  30. nohup: ignoring input
  31. # nohup true </dev/null 1>/dev/null
  32. nohup: redirecting stderr to stdout
  33. # nohup true </dev/null 2>zz 1>/dev/null
  34. # cat zz
  35. (nothing)
  36. #
  37. */
  38. int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  39. int nohup_main(int argc UNUSED_PARAM, char **argv)
  40. {
  41. const char *nohupout;
  42. char *home;
  43. xfunc_error_retval = 127;
  44. if (!argv[1]) {
  45. bb_show_usage();
  46. }
  47. /* If stdin is a tty, detach from it. */
  48. if (isatty(STDIN_FILENO)) {
  49. /* bb_error_msg("ignoring input"); */
  50. close(STDIN_FILENO);
  51. xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */
  52. }
  53. nohupout = "nohup.out";
  54. /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
  55. if (isatty(STDOUT_FILENO)) {
  56. close(STDOUT_FILENO);
  57. if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
  58. home = getenv("HOME");
  59. if (home) {
  60. nohupout = concat_path_file(home, nohupout);
  61. xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
  62. } else {
  63. xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */
  64. }
  65. }
  66. bb_error_msg("appending output to %s", nohupout);
  67. }
  68. /* If we have a tty on stderr, redirect to stdout. */
  69. if (isatty(STDERR_FILENO)) {
  70. /* if (stdout_wasnt_a_tty)
  71. bb_error_msg("redirecting stderr to stdout"); */
  72. dup2(STDOUT_FILENO, STDERR_FILENO);
  73. }
  74. signal(SIGHUP, SIG_IGN);
  75. argv++;
  76. BB_EXECVP_or_die(argv);
  77. }