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))
8
9config NOHUP
10  bool "nohup"
11  default y
12  help
13    usage: nohup COMMAND [ARGS...]
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  xsignal(SIGHUP, SIG_IGN);
25  if (isatty(1)) {
26    close(1);
27    if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY,
28        S_IRUSR|S_IWUSR ))
29    {
30      char *temp = getenv("HOME");
31
32      temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out");
33      xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600);
34      free(temp);
35    }
36  }
37  if (isatty(0)) {
38    close(0);
39    open("/dev/null", O_RDONLY);
40  }
41  xexec(toys.optargs);
42}
43