"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Denys Vlasenko | a02a4e9 | 2017-10-05 15:19:25 +0200 | [diff] [blame] | 2 | /* |
| 3 | * nohup - invoke a utility immune to hangups. |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 4 | * |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 5 | * Busybox version based on nohup specification at |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 6 | * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 7 | * |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 8 | * Copyright 2006 Rob Landley <rob@landley.net> |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 9 | * Copyright 2006 Bernhard Reutner-Fischer |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 10 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 11 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 12 | */ |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 13 | //config:config NOHUP |
Denys Vlasenko | 4eed2c6 | 2017-07-18 22:01:24 +0200 | [diff] [blame] | 14 | //config: bool "nohup (2 kb)" |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 15 | //config: default y |
| 16 | //config: help |
Denys Vlasenko | 72089cf | 2017-07-21 09:50:55 +0200 | [diff] [blame] | 17 | //config: run a command immune to hangups, with output to a non-tty. |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 18 | |
Denys Vlasenko | 5c527dc | 2017-08-04 19:55:01 +0200 | [diff] [blame] | 19 | //applet:IF_NOHUP(APPLET_NOEXEC(nohup, nohup, BB_DIR_USR_BIN, BB_SUID_DROP, nohup)) |
Denys Vlasenko | af3f420 | 2016-11-23 14:46:56 +0100 | [diff] [blame] | 20 | |
| 21 | //kbuild:lib-$(CONFIG_NOHUP) += nohup.o |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 22 | |
Pere Orga | 3442538 | 2011-03-31 14:43:25 +0200 | [diff] [blame] | 23 | //usage:#define nohup_trivial_usage |
| 24 | //usage: "PROG ARGS" |
| 25 | //usage:#define nohup_full_usage "\n\n" |
| 26 | //usage: "Run PROG immune to hangups, with output to a non-tty" |
| 27 | //usage: |
| 28 | //usage:#define nohup_example_usage |
| 29 | //usage: "$ nohup make &" |
| 30 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 31 | #include "libbb.h" |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 32 | |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 33 | /* Compat info: nohup (GNU coreutils 6.8) does this: |
| 34 | # nohup true |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 35 | nohup: ignoring input and appending output to 'nohup.out' |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 36 | # nohup true 1>/dev/null |
| 37 | nohup: ignoring input and redirecting stderr to stdout |
| 38 | # nohup true 2>zz |
| 39 | # cat zz |
Denys Vlasenko | 95f7953 | 2017-08-02 14:26:33 +0200 | [diff] [blame] | 40 | nohup: ignoring input and appending output to 'nohup.out' |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 41 | # nohup true 2>zz 1>/dev/null |
| 42 | # cat zz |
| 43 | nohup: ignoring input |
| 44 | # nohup true </dev/null 1>/dev/null |
| 45 | nohup: redirecting stderr to stdout |
| 46 | # nohup true </dev/null 2>zz 1>/dev/null |
| 47 | # cat zz |
| 48 | (nothing) |
| 49 | # |
| 50 | */ |
| 51 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 52 | int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 53 | int nohup_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 54 | { |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 55 | const char *nohupout; |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 56 | char *home; |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 57 | |
Denis Vlasenko | 4092082 | 2006-10-03 20:28:06 +0000 | [diff] [blame] | 58 | xfunc_error_retval = 127; |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 59 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 60 | if (!argv[1]) { |
| 61 | bb_show_usage(); |
| 62 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 63 | |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 64 | /* If stdin is a tty, detach from it. */ |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 65 | if (isatty(STDIN_FILENO)) { |
| 66 | /* bb_error_msg("ignoring input"); */ |
| 67 | close(STDIN_FILENO); |
| 68 | xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */ |
| 69 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 70 | |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 71 | nohupout = "nohup.out"; |
| 72 | /* Redirect stdout to nohup.out, either in "." or in "$HOME". */ |
| 73 | if (isatty(STDOUT_FILENO)) { |
| 74 | close(STDOUT_FILENO); |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 75 | if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) { |
| 76 | home = getenv("HOME"); |
| 77 | if (home) { |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 78 | nohupout = concat_path_file(home, nohupout); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 79 | xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR); |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 80 | } else { |
| 81 | xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */ |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 82 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 83 | } |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 84 | bb_error_msg("appending output to %s", nohupout); |
| 85 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 86 | |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 87 | /* If we have a tty on stderr, redirect to stdout. */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 88 | if (isatty(STDERR_FILENO)) { |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 89 | /* if (stdout_wasnt_a_tty) |
| 90 | bb_error_msg("redirecting stderr to stdout"); */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 91 | dup2(STDOUT_FILENO, STDERR_FILENO); |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 92 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 93 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 94 | signal(SIGHUP, SIG_IGN); |
| 95 | |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 96 | argv++; |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 97 | BB_EXECVP_or_die(argv); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 98 | } |