Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 1 | /* See LICENSE file for copyright and license details. */ |
FRIGN | b7c199e | 2015-03-05 00:57:14 +0100 | [diff] [blame] | 2 | #include <sys/stat.h> |
| 3 | |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 4 | #include <errno.h> |
| 5 | #include <fcntl.h> |
| 6 | #include <signal.h> |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 7 | #include <unistd.h> |
FRIGN | eee98ed | 2014-11-13 18:29:30 +0100 | [diff] [blame] | 8 | |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 9 | #include "util.h" |
| 10 | |
Christoph Lohmann | 4d38f60 | 2013-06-14 20:20:47 +0200 | [diff] [blame] | 11 | static void |
| 12 | usage(void) |
| 13 | { |
FRIGN | 7b6d918 | 2015-01-28 23:55:57 +0100 | [diff] [blame] | 14 | eprintf("usage: %s cmd [arg ...]\n", argv0); |
Christoph Lohmann | 4d38f60 | 2013-06-14 20:20:47 +0200 | [diff] [blame] | 15 | } |
| 16 | |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 17 | int |
| 18 | main(int argc, char *argv[]) |
| 19 | { |
FRIGN | a6ee96a | 2015-03-04 22:39:12 +0100 | [diff] [blame] | 20 | int fd, savederrno; |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 21 | |
FRIGN | 9016d28 | 2015-04-24 23:54:17 +0200 | [diff] [blame] | 22 | argv0 = argv[0], argc--, argv++; |
Christoph Lohmann | 4d38f60 | 2013-06-14 20:20:47 +0200 | [diff] [blame] | 23 | |
FRIGN | a6ee96a | 2015-03-04 22:39:12 +0100 | [diff] [blame] | 24 | if (!argc) |
Christoph Lohmann | 4d38f60 | 2013-06-14 20:20:47 +0200 | [diff] [blame] | 25 | usage(); |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 26 | |
FRIGN | eee98ed | 2014-11-13 18:29:30 +0100 | [diff] [blame] | 27 | if (signal(SIGHUP, SIG_IGN) == SIG_ERR) |
FRIGN | a6ee96a | 2015-03-04 22:39:12 +0100 | [diff] [blame] | 28 | enprintf(127, "signal HUP:"); |
Christoph Lohmann | 4d38f60 | 2013-06-14 20:20:47 +0200 | [diff] [blame] | 29 | |
FRIGN | eee98ed | 2014-11-13 18:29:30 +0100 | [diff] [blame] | 30 | if (isatty(STDOUT_FILENO)) { |
FRIGN | a6ee96a | 2015-03-04 22:39:12 +0100 | [diff] [blame] | 31 | if ((fd = open("nohup.out", O_APPEND | O_CREAT, S_IRUSR | S_IWUSR)) < 0) |
| 32 | enprintf(127, "open nohup.out:"); |
FRIGN | 1436518 | 2014-11-19 20:59:37 +0100 | [diff] [blame] | 33 | if (dup2(fd, STDOUT_FILENO) < 0) |
FRIGN | a6ee96a | 2015-03-04 22:39:12 +0100 | [diff] [blame] | 34 | enprintf(127, "dup2:"); |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 35 | close(fd); |
| 36 | } |
FRIGN | a6ee96a | 2015-03-04 22:39:12 +0100 | [diff] [blame] | 37 | if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) < 0) |
| 38 | enprintf(127, "dup2:"); |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 39 | |
FRIGN | a6ee96a | 2015-03-04 22:39:12 +0100 | [diff] [blame] | 40 | execvp(argv[0], argv); |
| 41 | savederrno = errno; |
FRIGN | 6f207da | 2015-03-09 01:04:34 +0100 | [diff] [blame] | 42 | weprintf("execvp %s:", argv[0]); |
FRIGN | a6ee96a | 2015-03-04 22:39:12 +0100 | [diff] [blame] | 43 | |
FRIGN | 6f207da | 2015-03-09 01:04:34 +0100 | [diff] [blame] | 44 | _exit(126 + (savederrno == ENOENT)); |
Connor Lane Smith | f24772d | 2011-06-18 06:41:28 +0100 | [diff] [blame] | 45 | } |