"Robert P. J. Day" | 63fc1a9 | 2006-07-02 19:47:05 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 2 | /* nohup - invoke a utility immune to hangups. |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 3 | * |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 4 | * Busybox version based on nohup specification at |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 5 | * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 6 | * |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 7 | * Copyright 2006 Rob Landley <rob@landley.net> |
Bernhard Reutner-Fischer | 6c4dade | 2008-09-25 12:13:34 +0000 | [diff] [blame] | 8 | * Copyright 2006 Bernhard Reutner-Fischer |
Denis Vlasenko | 9213a9e | 2006-09-17 16:28:10 +0000 | [diff] [blame] | 9 | * |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 13 | #include "libbb.h" |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 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 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 34 | int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Denys Vlasenko | e992bae | 2009-11-28 15:18:53 +0100 | [diff] [blame] | 35 | int nohup_main(int argc UNUSED_PARAM, char **argv) |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 36 | { |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 37 | const char *nohupout; |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 38 | char *home; |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 39 | |
Denis Vlasenko | 4092082 | 2006-10-03 20:28:06 +0000 | [diff] [blame] | 40 | xfunc_error_retval = 127; |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 41 | |
Denys Vlasenko | 2ec91ae | 2010-01-04 14:15:38 +0100 | [diff] [blame] | 42 | if (!argv[1]) { |
| 43 | bb_show_usage(); |
| 44 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 45 | |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 46 | /* If stdin is a tty, detach from it. */ |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 47 | if (isatty(STDIN_FILENO)) { |
| 48 | /* bb_error_msg("ignoring input"); */ |
| 49 | close(STDIN_FILENO); |
| 50 | xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */ |
| 51 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 52 | |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 53 | nohupout = "nohup.out"; |
| 54 | /* Redirect stdout to nohup.out, either in "." or in "$HOME". */ |
| 55 | if (isatty(STDOUT_FILENO)) { |
| 56 | close(STDOUT_FILENO); |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 57 | if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) { |
| 58 | home = getenv("HOME"); |
| 59 | if (home) { |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 60 | nohupout = concat_path_file(home, nohupout); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 61 | xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR); |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 62 | } else { |
| 63 | xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */ |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 64 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 65 | } |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 66 | bb_error_msg("appending output to %s", nohupout); |
| 67 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 68 | |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 69 | /* If we have a tty on stderr, redirect to stdout. */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 70 | if (isatty(STDERR_FILENO)) { |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 71 | /* if (stdout_wasnt_a_tty) |
| 72 | bb_error_msg("redirecting stderr to stdout"); */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 73 | dup2(STDOUT_FILENO, STDERR_FILENO); |
Denis Vlasenko | f8157ca | 2008-02-04 00:30:06 +0000 | [diff] [blame] | 74 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 75 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 76 | signal(SIGHUP, SIG_IGN); |
| 77 | |
Denys Vlasenko | 41ddd9f | 2010-06-25 01:46:53 +0200 | [diff] [blame] | 78 | argv++; |
Pascal Bellard | 21e8e8d | 2010-07-04 00:57:03 +0200 | [diff] [blame] | 79 | BB_EXECVP_or_die(argv); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 80 | } |