1 | /* vi:set ts=4: */
|
---|
2 | /* nohup - invoke a utility immune to hangups.
|
---|
3 | *
|
---|
4 | * Busybox version based on nohup specification at
|
---|
5 | * http://www.opengroup.org/onlinepubs/007904975/utilities/nohup.html
|
---|
6 | *
|
---|
7 | * Copyright 2006 Rob Landley <rob@landley.net>
|
---|
8 | *
|
---|
9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
|
---|
10 | */
|
---|
11 |
|
---|
12 | #include <fcntl.h>
|
---|
13 | #include <signal.h>
|
---|
14 | #include <unistd.h>
|
---|
15 | #include "busybox.h"
|
---|
16 |
|
---|
17 | int nohup_main(int argc, char *argv[])
|
---|
18 | {
|
---|
19 | int temp, nullfd;
|
---|
20 | char *nohupout = "nohup.out", *home = NULL;
|
---|
21 |
|
---|
22 | // I have no idea why the standard cares about this.
|
---|
23 |
|
---|
24 | bb_default_error_retval = 127;
|
---|
25 |
|
---|
26 | if (argc<2) bb_show_usage();
|
---|
27 |
|
---|
28 | nullfd = bb_xopen(bb_dev_null, O_WRONLY|O_APPEND);
|
---|
29 | // If stdin is a tty, detach from it.
|
---|
30 |
|
---|
31 | if (isatty(0)) dup2(nullfd, 0);
|
---|
32 |
|
---|
33 | // Redirect stdout to nohup.out, either in "." or in "$HOME".
|
---|
34 |
|
---|
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);
|
---|
42 | }
|
---|
43 | }
|
---|
44 | } else dup2(nullfd, 1);
|
---|
45 |
|
---|
46 | // If we have a tty on strderr, announce filename and redirect to stdout.
|
---|
47 | // Else redirect to /dev/null.
|
---|
48 |
|
---|
49 | temp = isatty(2);
|
---|
50 | if (temp) fdprintf(2,"Writing to %s\n", home ? home : nohupout);
|
---|
51 | dup2(temp ? 1 : nullfd, 2);
|
---|
52 | close(nullfd);
|
---|
53 | signal (SIGHUP, SIG_IGN);
|
---|
54 |
|
---|
55 | // Exec our new program.
|
---|
56 |
|
---|
57 | execvp(argv[1],argv+1);
|
---|
58 | if (ENABLE_FEATURE_CLEAN_UP) free(home);
|
---|
59 | bb_error_msg_and_die("exec %s",argv[1]);
|
---|
60 | }
|
---|