2011-06-18 01:41:28 -04:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
2015-03-04 18:57:14 -05:00
|
|
|
#include <sys/stat.h>
|
|
|
|
|
2011-06-18 01:41:28 -04:00
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <unistd.h>
|
2014-11-13 12:29:30 -05:00
|
|
|
|
2011-06-18 01:41:28 -04:00
|
|
|
#include "util.h"
|
|
|
|
|
2013-06-14 14:20:47 -04:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-01-28 17:55:57 -05:00
|
|
|
eprintf("usage: %s cmd [arg ...]\n", argv0);
|
2013-06-14 14:20:47 -04:00
|
|
|
}
|
|
|
|
|
2011-06-18 01:41:28 -04:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2015-03-04 16:39:12 -05:00
|
|
|
int fd, savederrno;
|
2011-06-18 01:41:28 -04:00
|
|
|
|
2017-08-05 17:50:39 -04:00
|
|
|
argv0 = *argv, argv0 ? (argc--, argv++) : (void *)0;
|
2013-06-14 14:20:47 -04:00
|
|
|
|
2015-03-04 16:39:12 -05:00
|
|
|
if (!argc)
|
2013-06-14 14:20:47 -04:00
|
|
|
usage();
|
2011-06-18 01:41:28 -04:00
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (signal(SIGHUP, SIG_IGN) == SIG_ERR)
|
2015-03-04 16:39:12 -05:00
|
|
|
enprintf(127, "signal HUP:");
|
2013-06-14 14:20:47 -04:00
|
|
|
|
2014-11-13 12:29:30 -05:00
|
|
|
if (isatty(STDOUT_FILENO)) {
|
2015-03-04 16:39:12 -05:00
|
|
|
if ((fd = open("nohup.out", O_APPEND | O_CREAT, S_IRUSR | S_IWUSR)) < 0)
|
|
|
|
enprintf(127, "open nohup.out:");
|
2014-11-19 14:59:37 -05:00
|
|
|
if (dup2(fd, STDOUT_FILENO) < 0)
|
2015-03-04 16:39:12 -05:00
|
|
|
enprintf(127, "dup2:");
|
2011-06-18 01:41:28 -04:00
|
|
|
close(fd);
|
|
|
|
}
|
2015-03-04 16:39:12 -05:00
|
|
|
if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0)
|
|
|
|
enprintf(127, "dup2:");
|
|
|
|
|
|
|
|
execvp(argv[0], argv);
|
|
|
|
savederrno = errno;
|
2015-03-08 20:04:34 -04:00
|
|
|
weprintf("execvp %s:", argv[0]);
|
2015-03-04 16:39:12 -05:00
|
|
|
|
2015-03-08 20:04:34 -04:00
|
|
|
_exit(126 + (savederrno == ENOENT));
|
2011-06-18 01:41:28 -04:00
|
|
|
}
|