5 |
* http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html |
* http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html |
6 |
* |
* |
7 |
* Copyright 2006 Rob Landley <rob@landley.net> |
* Copyright 2006 Rob Landley <rob@landley.net> |
8 |
* Copyright 2006 Bernhard Fischer |
* Copyright 2006 Bernhard Reutner-Fischer |
9 |
* |
* |
10 |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
* Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
11 |
*/ |
*/ |
12 |
|
|
13 |
#include "busybox.h" |
#include "libbb.h" |
14 |
|
|
15 |
|
/* Compat info: nohup (GNU coreutils 6.8) does this: |
16 |
|
# nohup true |
17 |
|
nohup: ignoring input and appending output to `nohup.out' |
18 |
|
# nohup true 1>/dev/null |
19 |
|
nohup: ignoring input and redirecting stderr to stdout |
20 |
|
# nohup true 2>zz |
21 |
|
# cat zz |
22 |
|
nohup: ignoring input and appending output to `nohup.out' |
23 |
|
# nohup true 2>zz 1>/dev/null |
24 |
|
# cat zz |
25 |
|
nohup: ignoring input |
26 |
|
# nohup true </dev/null 1>/dev/null |
27 |
|
nohup: redirecting stderr to stdout |
28 |
|
# nohup true </dev/null 2>zz 1>/dev/null |
29 |
|
# cat zz |
30 |
|
(nothing) |
31 |
|
# |
32 |
|
*/ |
33 |
|
|
34 |
|
int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
35 |
int nohup_main(int argc, char **argv) |
int nohup_main(int argc, char **argv) |
36 |
{ |
{ |
37 |
int temp, nullfd; |
const char *nohupout; |
38 |
char *nohupout, *home = NULL; |
char *home; |
39 |
|
|
40 |
xfunc_error_retval = 127; |
xfunc_error_retval = 127; |
41 |
|
|
42 |
if (argc<2) bb_show_usage(); |
if (argc < 2) bb_show_usage(); |
43 |
|
|
|
nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND); |
|
44 |
/* If stdin is a tty, detach from it. */ |
/* If stdin is a tty, detach from it. */ |
45 |
if (isatty(STDIN_FILENO)) dup2(nullfd, STDIN_FILENO); |
if (isatty(STDIN_FILENO)) { |
46 |
|
/* bb_error_msg("ignoring input"); */ |
47 |
|
close(STDIN_FILENO); |
48 |
|
xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */ |
49 |
|
} |
50 |
|
|
51 |
nohupout = "nohup.out"; |
nohupout = "nohup.out"; |
52 |
/* Redirect stdout to nohup.out, either in "." or in "$HOME". */ |
/* Redirect stdout to nohup.out, either in "." or in "$HOME". */ |
57 |
if (home) { |
if (home) { |
58 |
nohupout = concat_path_file(home, nohupout); |
nohupout = concat_path_file(home, nohupout); |
59 |
xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR); |
xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR); |
60 |
|
} else { |
61 |
|
xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */ |
62 |
} |
} |
63 |
} |
} |
64 |
} else dup2(nullfd, STDOUT_FILENO); |
bb_error_msg("appending output to %s", nohupout); |
65 |
|
} |
66 |
|
|
67 |
|
/* If we have a tty on stderr, redirect to stdout. */ |
68 |
|
if (isatty(STDERR_FILENO)) { |
69 |
|
/* if (stdout_wasnt_a_tty) |
70 |
|
bb_error_msg("redirecting stderr to stdout"); */ |
71 |
|
dup2(STDOUT_FILENO, STDERR_FILENO); |
72 |
|
} |
73 |
|
|
74 |
|
signal(SIGHUP, SIG_IGN); |
75 |
|
|
76 |
/* If we have a tty on strderr, announce filename and redirect to stdout. |
BB_EXECVP(argv[1], argv+1); |
77 |
* Else redirect to /dev/null. |
bb_simple_perror_msg_and_die(argv[1]); |
|
*/ |
|
|
temp = isatty(STDERR_FILENO); |
|
|
if (temp) bb_error_msg("appending to %s", nohupout); |
|
|
dup2(temp ? STDOUT_FILENO : nullfd, STDERR_FILENO); |
|
|
close(nullfd); |
|
|
signal (SIGHUP, SIG_IGN); |
|
|
|
|
|
execvp(argv[1],argv+1); |
|
|
if (00 && ENABLE_FEATURE_CLEAN_UP && home) free(nohupout); |
|
|
bb_perror_msg_and_die("%s", argv[1]); |
|
78 |
} |
} |