1/* See LICENSE file for copyright and license details. */
2
3#include <sys/stat.h>
4
5#include <errno.h>
6#include <fcntl.h>
7#include <signal.h>
8#include <unistd.h>
9
10#include "util.h"
11#include "wexec.h"
12
13static void
14usage(void)
15{
16 eprintf("usage: %s cmd [arg ...]\n", argv0);
17}
18
19// ?man nohup: run command immune to hangups
20// ?man arguments: cmd [arg ...]
21// ?man run a command that persists after logging out
22int
23main(int argc, char *argv[])
24{
25 int fd, savederrno;
26
27 ARGBEGIN
28 {
29 default:
30 usage();
31 }
32 ARGEND
33
34 if (!argc)
35 usage();
36
37 if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
38 enprintf(127, "signal HUP:");
39
40 if (isatty(STDOUT_FILENO)) {
41 if ((fd = open("nohup.out", O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
42 enprintf(127, "open nohup.out:");
43 if (dup2(fd, STDOUT_FILENO) < 0)
44 enprintf(127, "dup2:");
45 close(fd);
46 }
47 if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
48 enprintf(127, "dup2:");
49
50 wexecvp_self(argv[0], argv);
51 savederrno = errno;
52 weprintf("wexecvp %s:", argv[0]);
53
54 _exit(126 + (savederrno == ENOENT));
55}