sbase/nohup.c

48 lines
895 B
C
Raw Normal View History

2011-06-18 05:41:28 +00:00
/* See LICENSE file for copyright and license details. */
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
2011-06-18 05:41:28 +00:00
#include "util.h"
2013-06-14 18:20:47 +00:00
static void
usage(void)
{
eprintf("usage: %s cmd [arg ...]\n", argv0);
2013-06-14 18:20:47 +00:00
}
2011-06-18 05:41:28 +00:00
int
main(int argc, char *argv[])
{
int fd, savederrno;
2011-06-18 05:41:28 +00:00
2013-06-14 18:20:47 +00:00
ARGBEGIN {
default:
usage();
} ARGEND;
if (!argc)
2013-06-14 18:20:47 +00:00
usage();
2011-06-18 05:41:28 +00:00
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
enprintf(127, "signal HUP:");
2013-06-14 18:20:47 +00:00
if (isatty(STDOUT_FILENO)) {
if ((fd = open("nohup.out", O_APPEND | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
enprintf(127, "open nohup.out:");
2014-11-19 19:59:37 +00:00
if (dup2(fd, STDOUT_FILENO) < 0)
enprintf(127, "dup2:");
2011-06-18 05:41:28 +00:00
close(fd);
}
if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
enprintf(127, "dup2:");
execvp(argv[0], argv);
savederrno = errno;
weprintf("exec %s:", argv[0]);
_exit(126 + (savederrno == ENOENT));
return 0; /* not reached */
2011-06-18 05:41:28 +00:00
}