Changeset 1770 in MondoRescue for branches/stable/mindi-busybox/coreutils/nohup.c
- Timestamp:
- Nov 6, 2007, 11:01:53 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/stable/mindi-busybox/coreutils/nohup.c
r821 r1770 1 /* vi: setts=4: */1 /* vi: set sw=4 ts=4: */ 2 2 /* nohup - invoke a utility immune to hangups. 3 * 3 * 4 4 * Busybox version based on nohup specification at 5 5 * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html 6 * 6 * 7 7 * Copyright 2006 Rob Landley <rob@landley.net> 8 * 8 * Copyright 2006 Bernhard Fischer 9 * 9 10 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. 10 11 */ 11 12 12 #include <fcntl.h> 13 #include <signal.h> 14 #include <unistd.h> 15 #include "busybox.h" 13 #include "libbb.h" 16 14 17 int nohup_main(int argc, char *argv[]) 15 int nohup_main(int argc, char **argv); 16 int nohup_main(int argc, char **argv) 18 17 { 19 int temp, nullfd; 20 char *nohupout = "nohup.out", *home = NULL; 18 int nullfd; 19 const char *nohupout; 20 char *home = NULL; 21 21 22 // I have no idea why the standard cares about this.22 xfunc_error_retval = 127; 23 23 24 bb_default_error_retval = 127;24 if (argc < 2) bb_show_usage(); 25 25 26 if (argc<2) bb_show_usage(); 26 nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND); 27 /* If stdin is a tty, detach from it. */ 28 if (isatty(STDIN_FILENO)) 29 dup2(nullfd, STDIN_FILENO); 27 30 28 nullfd = bb_xopen(bb_dev_null, O_WRONLY|O_APPEND); 29 // If stdin is a tty, detach from it. 30 31 if (isatty(0)) dup2(nullfd, 0); 32 33 // Redirect stdout to nohup.out, either in "." or in "$HOME". 34 35 if (isatty(1)) { 36 close(1); 31 nohupout = "nohup.out"; 32 /* Redirect stdout to nohup.out, either in "." or in "$HOME". */ 33 if (isatty(STDOUT_FILENO)) { 34 close(STDOUT_FILENO); 37 35 if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) { 38 36 home = getenv("HOME"); 39 37 if (home) { 40 home= concat_path_file(home, nohupout);41 bb_xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);38 nohupout = concat_path_file(home, nohupout); 39 xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR); 42 40 } 43 41 } 44 } else dup2(nullfd, 1);42 } else dup2(nullfd, STDOUT_FILENO); 45 43 46 // If we have a tty on strderr, announce filename and redirect to stdout. 47 // Else redirect to /dev/null. 44 /* If we have a tty on stderr, announce filename and redirect to stdout. 45 * Else redirect to /dev/null. 46 */ 47 if (isatty(STDERR_FILENO)) { 48 bb_error_msg("appending to %s", nohupout); 49 dup2(STDOUT_FILENO, STDERR_FILENO); 50 } else dup2(nullfd, STDERR_FILENO); 48 51 49 temp = isatty(2); 50 if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout); 51 dup2(temp ? 1 : nullfd, 2); 52 close(nullfd); 53 signal (SIGHUP, SIG_IGN); 52 if (nullfd > 2) 53 close(nullfd); 54 signal(SIGHUP, SIG_IGN); 54 55 55 // Exec our new program. 56 57 execvp(argv[1],argv+1); 58 if (ENABLE_FEATURE_CLEAN_UP) free(home); 59 bb_error_msg_and_die("exec %s",argv[1]); 56 BB_EXECVP(argv[1], argv+1); 57 if (ENABLE_FEATURE_CLEAN_UP && home) 58 free((char*)nohupout); 59 bb_perror_msg_and_die("%s", argv[1]); 60 60 }
Note:
See TracChangeset
for help on using the changeset viewer.