xref: /third_party/toybox/toys/posix/nohup.c (revision 0f66f451)
10f66f451Sopenharmony_ci/* nohup.c - run commandline with SIGHUP blocked.
20f66f451Sopenharmony_ci *
30f66f451Sopenharmony_ci * Copyright 2011 Rob Landley <rob@landley.net>
40f66f451Sopenharmony_ci *
50f66f451Sopenharmony_ci * See http://opengroup.org/onlinepubs/9699919799/utilities/nohup.html
60f66f451Sopenharmony_ci
70f66f451Sopenharmony_ciUSE_NOHUP(NEWTOY(nohup, "<1^", TOYFLAG_USR|TOYFLAG_BIN|TOYFLAG_ARGFAIL(125)))
80f66f451Sopenharmony_ci
90f66f451Sopenharmony_ciconfig NOHUP
100f66f451Sopenharmony_ci  bool "nohup"
110f66f451Sopenharmony_ci  default y
120f66f451Sopenharmony_ci  help
130f66f451Sopenharmony_ci    usage: nohup COMMAND...
140f66f451Sopenharmony_ci
150f66f451Sopenharmony_ci    Run a command that survives the end of its terminal.
160f66f451Sopenharmony_ci
170f66f451Sopenharmony_ci    Redirect tty on stdin to /dev/null, tty on stdout to "nohup.out".
180f66f451Sopenharmony_ci*/
190f66f451Sopenharmony_ci
200f66f451Sopenharmony_ci#include "toys.h"
210f66f451Sopenharmony_ci
220f66f451Sopenharmony_civoid nohup_main(void)
230f66f451Sopenharmony_ci{
240f66f451Sopenharmony_ci  toys.exitval = 125;
250f66f451Sopenharmony_ci  xsignal(SIGHUP, SIG_IGN);
260f66f451Sopenharmony_ci  if (isatty(1)) {
270f66f451Sopenharmony_ci    close(1);
280f66f451Sopenharmony_ci    if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY,
290f66f451Sopenharmony_ci        S_IRUSR|S_IWUSR ))
300f66f451Sopenharmony_ci    {
310f66f451Sopenharmony_ci      char *temp = getenv("HOME");
320f66f451Sopenharmony_ci
330f66f451Sopenharmony_ci      temp = xmprintf("%s/%s", temp ? temp : "", "nohup.out");
340f66f451Sopenharmony_ci      xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, 0600);
350f66f451Sopenharmony_ci      free(temp);
360f66f451Sopenharmony_ci    }
370f66f451Sopenharmony_ci  }
380f66f451Sopenharmony_ci  if (isatty(0)) {
390f66f451Sopenharmony_ci    close(0);
400f66f451Sopenharmony_ci    xopen_stdio("/dev/null", O_RDONLY);
410f66f451Sopenharmony_ci  }
420f66f451Sopenharmony_ci  toys.exitval = 0;
430f66f451Sopenharmony_ci  xexec(toys.optargs);
440f66f451Sopenharmony_ci}
45