nohup.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /* vi: set sw=4 ts=4: */
  2. /* nohup -- run a command immune to hangups, with output to a non-tty
  3. Copyright (C) 2003, 2004, 2005 Free Software Foundation, Inc.
  4. Licensed under the GPL v2, see the file LICENSE in this tarball.
  5. */
  6. /* Written by Jim Meyering */
  7. /* initial busybox port by Bernhard Fischer */
  8. #include <stdio_ext.h> /* __fpending */
  9. #include <stdio.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <sys/types.h>
  13. #include <signal.h>
  14. #include <errno.h>
  15. #include "busybox.h"
  16. #define EXIT_CANNOT_INVOKE (126)
  17. #define NOHUP_FAILURE (127)
  18. #define EXIT_ENOENT NOHUP_FAILURE
  19. #if defined F_GETFD && defined F_SETFD
  20. static inline int set_cloexec_flag (int desc)
  21. {
  22. int flags = fcntl (desc, F_GETFD, 0);
  23. if (0 <= flags) {
  24. if (flags == (flags |= FD_CLOEXEC) ||
  25. fcntl (desc, F_SETFD, flags) != -1) {
  26. return 0;
  27. }
  28. }
  29. return -1;
  30. }
  31. #else
  32. #define set_cloexec_flag(desc) (0)
  33. #endif
  34. static int fd_reopen (int desired_fd, char const *file, int flags)
  35. {
  36. int fd;
  37. close (desired_fd);
  38. fd = open (file, flags | O_WRONLY, S_IRUSR | S_IWUSR);
  39. if (fd == desired_fd || fd < 0)
  40. return fd;
  41. else {
  42. int fd2 = fcntl (fd, F_DUPFD, desired_fd);
  43. int saved_errno = errno;
  44. close (fd);
  45. errno = saved_errno;
  46. return fd2;
  47. }
  48. }
  49. /* Close standard output, exiting with status 'exit_failure' on failure.
  50. If a program writes *anything* to stdout, that program should close
  51. stdout and make sure that it succeeds before exiting. Otherwise,
  52. suppose that you go to the extreme of checking the return status
  53. of every function that does an explicit write to stdout. The last
  54. printf can succeed in writing to the internal stream buffer, and yet
  55. the fclose(stdout) could still fail (due e.g., to a disk full error)
  56. when it tries to write out that buffered data. Thus, you would be
  57. left with an incomplete output file and the offending program would
  58. exit successfully. Even calling fflush is not always sufficient,
  59. since some file systems (NFS and CODA) buffer written/flushed data
  60. until an actual close call.
  61. Besides, it's wasteful to check the return value from every call
  62. that writes to stdout -- just let the internal stream state record
  63. the failure. That's what the ferror test is checking below.
  64. It's important to detect such failures and exit nonzero because many
  65. tools (most notably `make' and other build-management systems) depend
  66. on being able to detect failure in other tools via their exit status. */
  67. static void close_stdout (void)
  68. {
  69. int prev_fail = ferror (stdout);
  70. int none_pending = (0 == __fpending (stdout));
  71. int fclose_fail = fclose (stdout);
  72. if (prev_fail || fclose_fail) {
  73. /* If ferror returned zero, no data remains to be flushed, and we'd
  74. otherwise fail with EBADF due to a failed fclose, then assume that
  75. it's ok to ignore the fclose failure. That can happen when a
  76. program like cp is invoked like this `cp a b >&-' (i.e., with
  77. stdout closed) and doesn't generate any output (hence no previous
  78. error and nothing to be flushed). */
  79. if ((fclose_fail ? errno : 0) == EBADF && !prev_fail && none_pending)
  80. return;
  81. bb_perror_msg_and_die(bb_msg_write_error);
  82. }
  83. }
  84. int nohup_main (int argc, char **argv)
  85. {
  86. int saved_stderr_fd;
  87. if (argc < 2)
  88. bb_show_usage();
  89. bb_default_error_retval = NOHUP_FAILURE;
  90. atexit (close_stdout);
  91. /* If standard input is a tty, replace it with /dev/null.
  92. Note that it is deliberately opened for *writing*,
  93. to ensure any read evokes an error. */
  94. if (isatty (STDIN_FILENO))
  95. fd_reopen (STDIN_FILENO, bb_dev_null, 0);
  96. /* If standard output is a tty, redirect it (appending) to a file.
  97. First try nohup.out, then $HOME/nohup.out. */
  98. if (isatty (STDOUT_FILENO)) {
  99. char *in_home = NULL;
  100. char const *file = "nohup.out";
  101. int fd = fd_reopen (STDOUT_FILENO, file, O_CREAT | O_APPEND);
  102. if (fd < 0) {
  103. if ((in_home = getenv ("HOME")) != NULL) {
  104. in_home = concat_path_file(in_home, file);
  105. fd = fd_reopen (STDOUT_FILENO, in_home, O_CREAT | O_APPEND);
  106. }
  107. if (fd < 0) {
  108. bb_perror_msg("failed to open '%s'", file);
  109. if (in_home)
  110. bb_perror_msg("failed to open '%s'",in_home);
  111. return (NOHUP_FAILURE);
  112. }
  113. file = in_home;
  114. }
  115. umask (~(S_IRUSR | S_IWUSR));
  116. bb_error_msg("appending output to '%s'", file);
  117. if (ENABLE_FEATURE_CLEAN_UP)
  118. free (in_home);
  119. }
  120. /* If standard error is a tty, redirect it to stdout. */
  121. if (isatty (STDERR_FILENO)) {
  122. /* Save a copy of stderr before redirecting, so we can use the original
  123. if execve fails. It's no big deal if this dup fails. It might
  124. not change anything, and at worst, it'll lead to suppression of
  125. the post-failed-execve diagnostic. */
  126. saved_stderr_fd = dup (STDERR_FILENO);
  127. if (0 <= saved_stderr_fd && set_cloexec_flag (saved_stderr_fd) == -1)
  128. bb_perror_msg_and_die("failed to set the copy"
  129. "of stderr to close on exec");
  130. if (dup2 (STDOUT_FILENO, STDERR_FILENO) < 0) {
  131. if (errno != EBADF)
  132. bb_perror_msg_and_die("failed to redirect standard error");
  133. close (STDERR_FILENO);
  134. }
  135. } else
  136. saved_stderr_fd = STDERR_FILENO;
  137. signal (SIGHUP, SIG_IGN);
  138. {
  139. char **cmd = argv + 1;
  140. execvp (*cmd, cmd);
  141. /* The execve failed. Output a diagnostic to stderr only if:
  142. - stderr was initially redirected to a non-tty, or
  143. - stderr was initially directed to a tty, and we
  144. can dup2 it to point back to that same tty.
  145. In other words, output the diagnostic if possible, but only if
  146. it will go to the original stderr. */
  147. if (dup2 (saved_stderr_fd, STDERR_FILENO) == STDERR_FILENO)
  148. bb_perror_msg("cannot run command '%s'",*cmd);
  149. return (errno == ENOENT ? EXIT_ENOENT : EXIT_CANNOT_INVOKE);
  150. }
  151. }