annotate toys/nohup.c @ 389:5e9c1d73c84a

Implement nohup.
author Rob Landley <rob@landley.net>
date Sun, 20 Nov 2011 21:13:47 -0600
parents
children 82d70cff7357
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
389
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
1 /* vi: set sw=4 ts=4:
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
2 *
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
3 * nohup.c - run commandline with SIGHUP blocked.
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
4 *
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
5 * Copyright 2011 Rob Landley <rob@landley.net>
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
6 *
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
7 * See http://opengroup.org/onlinepubs/9699919799/utilities/nohup.html
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
8
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
9 USE_NOHUP(NEWTOY(nohup, "<1", TOYFLAG_USR|TOYFLAG_BIN))
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
10
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
11 config NOHUP
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
12 bool "nohup"
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
13 default y
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
14 help
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
15 usage: nohup COMMAND [ARGS...]
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
16
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
17 Run a command that survives the end of its terminal.
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
18 If stdin is a tty, redirect from /dev/null
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
19 If stdout is a tty, redirect to file "nohup.out"
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
20 */
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
21
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
22 #include "toys.h"
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
23
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
24 void nohup_main(void)
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
25 {
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
26 signal(SIGHUP, SIG_IGN);
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
27 if (ttyname(1)) {
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
28 close(1);
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
29 if (-1 == open("nohup.out", O_CREAT|O_APPEND|O_WRONLY,
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
30 S_IRUSR|S_IWUSR ))
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
31 {
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
32 char *temp = getenv("HOME");
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
33 temp = xmsprintf("%s/%s", temp ? temp : "", "nohup.out");
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
34 xcreate(temp, O_CREAT|O_APPEND|O_WRONLY, S_IRUSR|S_IWUSR);
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
35 }
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
36 }
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
37 if (ttyname(0)) {
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
38 close(0);
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
39 open("/dev/null", O_RDONLY);
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
40 }
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
41 xexec(toys.optargs);
5e9c1d73c84a Implement nohup.
Rob Landley <rob@landley.net>
parents:
diff changeset
42 }