xref: /third_party/toybox/toys/posix/nohup.c (revision 0f66f451)
1/* nohup.c - run commandline with SIGHUP blocked.
2 *
3 * Copyright 2011 Rob Landley <rob@landley.net>
4 *
5 * See http://opengroup.org/onlinepubs/9699919799/utilities/nohup.html
6
7USE_NOHUP(NEWTOY(nohup, "<1^", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(125)))
8
9config 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
22void nohup_main(void)
23{
24  toys.exitval = 125;
25  xsignal(SIGHUP, SIG_IGN);
26  if (isatty(1)) {
27    close(1);
28    if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY,
29        S_IRUSR|S_IWUSR ))
30    {
31      char *temp = getenv("HOME");
32
33      temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out");
34      xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600);
35      free(temp);
36    }
37  }
38  if (isatty(0)) {
39    close(0);
40    xopen_stdio("/dev/null", O_RDONLY);
41  }
42  toys.exitval = 0;
43  xexec(toys.optargs);
44}
45