blob: d8489686dc6bd48d4988d80fd99a777c94d6ad73
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 | //config:config NOHUP |
13 | //config: bool "nohup" |
14 | //config: default y |
15 | //config: help |
16 | //config: run a command immune to hangups, with output to a non-tty. |
17 | |
18 | //applet:IF_NOHUP(APPLET(nohup, BB_DIR_USR_BIN, BB_SUID_DROP)) |
19 | |
20 | //kbuild:lib-$(CONFIG_NOHUP) += nohup.o |
21 | |
22 | //usage:#define nohup_trivial_usage |
23 | //usage: "PROG ARGS" |
24 | //usage:#define nohup_full_usage "\n\n" |
25 | //usage: "Run PROG immune to hangups, with output to a non-tty" |
26 | //usage: |
27 | //usage:#define nohup_example_usage |
28 | //usage: "$ nohup make &" |
29 | |
30 | #include "libbb.h" |
31 | |
32 | /* Compat info: nohup (GNU coreutils 6.8) does this: |
33 | # nohup true |
34 | nohup: ignoring input and appending output to `nohup.out' |
35 | # nohup true 1>/dev/null |
36 | nohup: ignoring input and redirecting stderr to stdout |
37 | # nohup true 2>zz |
38 | # cat zz |
39 | nohup: ignoring input and appending output to `nohup.out' |
40 | # nohup true 2>zz 1>/dev/null |
41 | # cat zz |
42 | nohup: ignoring input |
43 | # nohup true </dev/null 1>/dev/null |
44 | nohup: redirecting stderr to stdout |
45 | # nohup true </dev/null 2>zz 1>/dev/null |
46 | # cat zz |
47 | (nothing) |
48 | # |
49 | */ |
50 | |
51 | int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
52 | int nohup_main(int argc UNUSED_PARAM, char **argv) |
53 | { |
54 | const char *nohupout; |
55 | char *home; |
56 | |
57 | xfunc_error_retval = 127; |
58 | |
59 | if (!argv[1]) { |
60 | bb_show_usage(); |
61 | } |
62 | |
63 | /* If stdin is a tty, detach from it. */ |
64 | if (isatty(STDIN_FILENO)) { |
65 | /* bb_error_msg("ignoring input"); */ |
66 | close(STDIN_FILENO); |
67 | xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */ |
68 | } |
69 | |
70 | nohupout = "nohup.out"; |
71 | /* Redirect stdout to nohup.out, either in "." or in "$HOME". */ |
72 | if (isatty(STDOUT_FILENO)) { |
73 | close(STDOUT_FILENO); |
74 | if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) { |
75 | home = getenv("HOME"); |
76 | if (home) { |
77 | nohupout = concat_path_file(home, nohupout); |
78 | xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR); |
79 | } else { |
80 | xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */ |
81 | } |
82 | } |
83 | bb_error_msg("appending output to %s", nohupout); |
84 | } |
85 | |
86 | /* If we have a tty on stderr, redirect to stdout. */ |
87 | if (isatty(STDERR_FILENO)) { |
88 | /* if (stdout_wasnt_a_tty) |
89 | bb_error_msg("redirecting stderr to stdout"); */ |
90 | dup2(STDOUT_FILENO, STDERR_FILENO); |
91 | } |
92 | |
93 | signal(SIGHUP, SIG_IGN); |
94 | |
95 | argv++; |
96 | BB_EXECVP_or_die(argv); |
97 | } |
98 |