"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 | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 8 | * Copyright 2006 Bernhard 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 | |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 13 | #include "busybox.h" |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 14 | |
Denis Vlasenko | 06af216 | 2007-02-03 17:28:39 +0000 | [diff] [blame] | 15 | int nohup_main(int argc, char **argv); |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 16 | int nohup_main(int argc, char **argv) |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 17 | { |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 18 | int nullfd; |
| 19 | const char *nohupout; |
| 20 | char *home = NULL; |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 21 | |
Denis Vlasenko | 4092082 | 2006-10-03 20:28:06 +0000 | [diff] [blame] | 22 | xfunc_error_retval = 127; |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 23 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 24 | if (argc < 2) bb_show_usage(); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 25 | |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 26 | nullfd = xopen(bb_dev_null, O_WRONLY|O_APPEND); |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 27 | /* If stdin is a tty, detach from it. */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 28 | if (isatty(STDIN_FILENO)) |
| 29 | dup2(nullfd, STDIN_FILENO); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 30 | |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 31 | nohupout = "nohup.out"; |
| 32 | /* Redirect stdout to nohup.out, either in "." or in "$HOME". */ |
| 33 | if (isatty(STDOUT_FILENO)) { |
| 34 | close(STDOUT_FILENO); |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 35 | if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) { |
| 36 | home = getenv("HOME"); |
| 37 | if (home) { |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 38 | nohupout = concat_path_file(home, nohupout); |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 39 | xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 40 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 41 | } |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 42 | } else dup2(nullfd, STDOUT_FILENO); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 43 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 44 | /* If we have a tty on stderr, announce filename and redirect to stdout. |
Bernhard Reutner-Fischer | 3503ff7 | 2006-09-21 22:10:24 +0000 | [diff] [blame] | 45 | * Else redirect to /dev/null. |
| 46 | */ |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 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); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 51 | |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 52 | if (nullfd > 2) |
| 53 | close(nullfd); |
| 54 | signal(SIGHUP, SIG_IGN); |
| 55 | |
Denis Vlasenko | 1d76f43 | 2007-02-06 01:20:12 +0000 | [diff] [blame] | 56 | BB_EXECVP(argv[1], argv+1); |
Denis Vlasenko | e06bed3 | 2007-01-27 22:21:12 +0000 | [diff] [blame] | 57 | if (ENABLE_FEATURE_CLEAN_UP && home) |
| 58 | free((char*)nohupout); |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 59 | bb_perror_msg_and_die("%s", argv[1]); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 60 | } |