xref: /aosp_15_r20/external/toybox/toys/posix/nohup.c (revision cf5a6c84e2b8763fc1a7db14496fd4742913b199)
1 /* nohup.c - run commandline with SIGHUP blocked.
2  *
3  * Copyright 2011 Rob Landley <[email protected]>
4  *
5  * See http://opengroup.org/onlinepubs/9699919799/utilities/nohup.html
6 
7 USE_NOHUP(NEWTOY(nohup, "<1^", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(125)))
8 
9 config NOHUP
10   bool "nohup"
11   default y
12   help
13     usage: nohup COMMAND...
14 
15     Run a command that survives the end of its terminal.
16 
17     Redirect tty on stdin to /dev/null, tty on stdout to "nohup.out".
18 */
19 
20 #include "toys.h"
21 
nohup_main(void)22 void nohup_main(void)
23 {
24   xsignal(SIGHUP, SIG_IGN);
25   if (isatty(1)) {
26     close(1);
27     if (open("nohup.out", O_CREAT|O_APPEND|O_WRONLY, 0600) == -1) {
28       char *temp = getenv("HOME");
29 
30       xcreate(temp ? temp = xmprintf("%s/nohup.out", temp) : "nohup.out",
31         O_CREAT|O_APPEND|O_WRONLY, 0600);
32       free(temp);
33     }
34   }
35   if (isatty(0)) {
36     close(0);
37     xopen_stdio("/dev/null", O_RDONLY);
38   }
39 
40   xexec(toys.optargs);
41 }
42