[1765] | 1 | /* vi: set sw=4 ts=4: */
|
---|
[821] | 2 | /* nohup - invoke a utility immune to hangups.
|
---|
[1765] | 3 | *
|
---|
[821] | 4 | * Busybox version based on nohup specification at
|
---|
| 5 | * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
|
---|
[1765] | 6 | *
|
---|
[821] | 7 | * Copyright 2006 Rob Landley <rob@landley.net>
|
---|
[2725] | 8 | * Copyright 2006 Bernhard Reutner-Fischer
|
---|
[1765] | 9 | *
|
---|
[2725] | 10 | * Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
---|
[821] | 11 | */
|
---|
| 12 |
|
---|
[3232] | 13 | //usage:#define nohup_trivial_usage
|
---|
| 14 | //usage: "PROG ARGS"
|
---|
| 15 | //usage:#define nohup_full_usage "\n\n"
|
---|
| 16 | //usage: "Run PROG immune to hangups, with output to a non-tty"
|
---|
| 17 | //usage:
|
---|
| 18 | //usage:#define nohup_example_usage
|
---|
| 19 | //usage: "$ nohup make &"
|
---|
| 20 |
|
---|
[1765] | 21 | #include "libbb.h"
|
---|
[821] | 22 |
|
---|
[2725] | 23 | /* Compat info: nohup (GNU coreutils 6.8) does this:
|
---|
| 24 | # nohup true
|
---|
| 25 | nohup: ignoring input and appending output to `nohup.out'
|
---|
| 26 | # nohup true 1>/dev/null
|
---|
| 27 | nohup: ignoring input and redirecting stderr to stdout
|
---|
| 28 | # nohup true 2>zz
|
---|
| 29 | # cat zz
|
---|
| 30 | nohup: ignoring input and appending output to `nohup.out'
|
---|
| 31 | # nohup true 2>zz 1>/dev/null
|
---|
| 32 | # cat zz
|
---|
| 33 | nohup: ignoring input
|
---|
| 34 | # nohup true </dev/null 1>/dev/null
|
---|
| 35 | nohup: redirecting stderr to stdout
|
---|
| 36 | # nohup true </dev/null 2>zz 1>/dev/null
|
---|
| 37 | # cat zz
|
---|
| 38 | (nothing)
|
---|
| 39 | #
|
---|
| 40 | */
|
---|
| 41 |
|
---|
| 42 | int nohup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
---|
| 43 | int nohup_main(int argc UNUSED_PARAM, char **argv)
|
---|
[821] | 44 | {
|
---|
[1765] | 45 | const char *nohupout;
|
---|
[2725] | 46 | char *home;
|
---|
[821] | 47 |
|
---|
[1765] | 48 | xfunc_error_retval = 127;
|
---|
[821] | 49 |
|
---|
[2725] | 50 | if (!argv[1]) {
|
---|
| 51 | bb_show_usage();
|
---|
| 52 | }
|
---|
[821] | 53 |
|
---|
[1765] | 54 | /* If stdin is a tty, detach from it. */
|
---|
[2725] | 55 | if (isatty(STDIN_FILENO)) {
|
---|
| 56 | /* bb_error_msg("ignoring input"); */
|
---|
| 57 | close(STDIN_FILENO);
|
---|
| 58 | xopen(bb_dev_null, O_RDONLY); /* will be fd 0 (STDIN_FILENO) */
|
---|
| 59 | }
|
---|
[821] | 60 |
|
---|
[1765] | 61 | nohupout = "nohup.out";
|
---|
| 62 | /* Redirect stdout to nohup.out, either in "." or in "$HOME". */
|
---|
| 63 | if (isatty(STDOUT_FILENO)) {
|
---|
| 64 | close(STDOUT_FILENO);
|
---|
[821] | 65 | if (open(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR) < 0) {
|
---|
| 66 | home = getenv("HOME");
|
---|
| 67 | if (home) {
|
---|
[1765] | 68 | nohupout = concat_path_file(home, nohupout);
|
---|
| 69 | xopen3(nohupout, O_CREAT|O_WRONLY|O_APPEND, S_IRUSR|S_IWUSR);
|
---|
[2725] | 70 | } else {
|
---|
| 71 | xopen(bb_dev_null, O_RDONLY); /* will be fd 1 */
|
---|
[821] | 72 | }
|
---|
| 73 | }
|
---|
[2725] | 74 | bb_error_msg("appending output to %s", nohupout);
|
---|
| 75 | }
|
---|
[821] | 76 |
|
---|
[2725] | 77 | /* If we have a tty on stderr, redirect to stdout. */
|
---|
[1765] | 78 | if (isatty(STDERR_FILENO)) {
|
---|
[2725] | 79 | /* if (stdout_wasnt_a_tty)
|
---|
| 80 | bb_error_msg("redirecting stderr to stdout"); */
|
---|
[1765] | 81 | dup2(STDOUT_FILENO, STDERR_FILENO);
|
---|
[2725] | 82 | }
|
---|
[821] | 83 |
|
---|
[1765] | 84 | signal(SIGHUP, SIG_IGN);
|
---|
[821] | 85 |
|
---|
[2725] | 86 | argv++;
|
---|
| 87 | BB_EXECVP_or_die(argv);
|
---|
[821] | 88 | }
|
---|