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