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 <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2011-06-18 05:41:28 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
|
|
|
enum { Error = 127, Found = 126 };
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
eprintf("usage: %s command [argument...]\n", argv0);
|
|
|
|
}
|
|
|
|
|
2011-06-18 05:41:28 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
} ARGEND;
|
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (argc == 0)
|
2013-06-14 18:20:47 +00:00
|
|
|
usage();
|
2011-06-18 05:41:28 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
|
2011-06-24 07:33:08 +00:00
|
|
|
enprintf(Error, "signal HUP:");
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2014-11-13 17:29:30 +00:00
|
|
|
if (isatty(STDOUT_FILENO)) {
|
|
|
|
if ((fd = open("nohup.out", O_APPEND|O_CREAT,
|
|
|
|
S_IRUSR|S_IWUSR)) == -1) {
|
2011-06-18 05:41:28 +00:00
|
|
|
enprintf(Error, "open nohup.out:");
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (dup2(fd, STDOUT_FILENO) == -1)
|
2011-06-18 05:41:28 +00:00
|
|
|
enprintf(Error, "dup2:");
|
|
|
|
close(fd);
|
|
|
|
}
|
2014-11-13 17:29:30 +00:00
|
|
|
if (isatty(STDERR_FILENO))
|
|
|
|
if (dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
|
2011-06-18 05:41:28 +00:00
|
|
|
enprintf(Error, "dup2:");
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
execvp(argv[0], &argv[0]);
|
|
|
|
enprintf(errno == ENOENT ? Error : Found, "exec %s:", argv[0]);
|
2014-11-13 17:06:18 +00:00
|
|
|
_exit(Error);
|
2014-11-17 10:51:36 +00:00
|
|
|
/* unreachable */
|
|
|
|
return 0;
|
2011-06-18 05:41:28 +00:00
|
|
|
}
|