3
0

nohup.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 tarball for details.
  11. */
  12. #include "libbb.h"
  13. /* Compat info: nohup (GNU coreutils 6.8) does this:
  14. # nohup true
  15. nohup: ignoring input and appending output to `nohup.out'
  16. # nohup true 1>/dev/null
  17. nohup: ignoring input and redirecting stderr to stdout
  18. # nohup true 2>zz
  19. # cat zz
  20. nohup: ignoring input and appending output to `nohup.out'
  21. # nohup true 2>zz 1>/dev/null
  22. # cat zz
  23. nohup: ignoring input
  24. # nohup true </dev/null 1>/dev/null
  25. nohup: redirecting stderr to stdout
  26. # nohup true </dev/null 2>zz 1>/dev/null
  27. # cat zz
  28. (nothing)
  29. #
  30. */
  31. int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  32. int nohup_main(int argc UNUSED_PARAM, char **argv)
  33. {
  34. const char *nohupout;
  35. char *home;
  36. xfunc_error_retval = 127;
  37. if (!argv[1]) {
  38. bb_show_usage();
  39. }
  40. /* If stdin is a tty, detach from it. */
  41. if (isatty(STDIN_FILENO)) {
  42. /* bb_error_msg("ignoring input"); */
  43. close(STDIN_FILENO);
  44. xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */
  45. }
  46. nohupout = "nohup.out";
  47. /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
  48. if (isatty(STDOUT_FILENO)) {
  49. close(STDOUT_FILENO);
  50. if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
  51. home = getenv("HOME");
  52. if (home) {
  53. nohupout = concat_path_file(home, nohupout);
  54. xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
  55. } else {
  56. xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */
  57. }
  58. }
  59. bb_error_msg("appending output to %s", nohupout);
  60. }
  61. /* If we have a tty on stderr, redirect to stdout. */
  62. if (isatty(STDERR_FILENO)) {
  63. /* if (stdout_wasnt_a_tty)
  64. bb_error_msg("redirecting stderr to stdout"); */
  65. dup2(STDOUT_FILENO, STDERR_FILENO);
  66. }
  67. signal(SIGHUP, SIG_IGN);
  68. BB_EXECVP(argv[1], argv+1);
  69. bb_simple_perror_msg_and_die(argv[1]);
  70. }