"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. |
| 3 | * |
| 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 |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 6 | * |
| 7 | * Copyright 2006 Rob Landley <rob@landley.net> |
| 8 | * |
| 9 | * 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] | 10 | */ |
| 11 | |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 12 | #include <fcntl.h> |
Rob Landley | fa6b5e6 | 2006-05-23 00:28:06 +0000 | [diff] [blame] | 13 | #include <signal.h> |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 15 | #include "busybox.h" |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 16 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 17 | int nohup_main(int argc, char *argv[]) |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 18 | { |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 19 | int temp, nullfd; |
| 20 | char *nohupout = "nohup.out", *home = NULL; |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 21 | |
Rob Landley | 027ea1a | 2006-05-24 17:58:00 +0000 | [diff] [blame] | 22 | // I have no idea why the standard cares about this. |
| 23 | |
| 24 | bb_default_error_retval = 127; |
| 25 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 26 | if (argc<2) bb_show_usage(); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 27 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 28 | nullfd = bb_xopen(bb_dev_null, O_WRONLY|O_APPEND); |
| 29 | // If stdin is a tty, detach from it. |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 30 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 31 | if (isatty(0)) dup2(nullfd, 0); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 32 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 33 | // Redirect stdout to nohup.out, either in "." or in "$HOME". |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 34 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 35 | if (isatty(1)) { |
| 36 | close(1); |
| 37 | if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) { |
| 38 | home = getenv("HOME"); |
| 39 | if (home) { |
| 40 | home = concat_path_file(home, nohupout); |
| 41 | bb_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] | 42 | } |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 43 | } |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 44 | } else dup2(nullfd, 1); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 45 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 46 | // If we have a tty on strderr, announce filename and redirect to stdout. |
| 47 | // Else redirect to /dev/null. |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 48 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 49 | temp = isatty(2); |
| 50 | if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout); |
| 51 | dup2(temp ? 1 : nullfd, 2); |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 52 | close(nullfd); |
Rob Landley | fa6b5e6 | 2006-05-23 00:28:06 +0000 | [diff] [blame] | 53 | signal (SIGHUP, SIG_IGN); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 54 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 55 | // Exec our new program. |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 56 | |
Rob Landley | c020f5f | 2006-05-21 18:28:13 +0000 | [diff] [blame] | 57 | execvp(argv[1],argv+1); |
| 58 | if (ENABLE_FEATURE_CLEAN_UP) free(home); |
| 59 | bb_error_msg_and_die("exec %s",argv[1]); |
Bernhard Reutner-Fischer | 9d7010c | 2005-09-21 18:25:05 +0000 | [diff] [blame] | 60 | } |