nohup.c revision 1.5 1 /*
2 * Copyright (c) 1989 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)nohup.c 5.4 (Berkeley) 6/1/90";*/
42 static char rcsid[] = "$Id: nohup.c,v 1.5 1995/06/27 00:20:24 jtc Exp $";
43 #endif /* not lint */
44
45 #include <sys/param.h>
46 #include <sys/file.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include <signal.h>
51 #include <stdio.h>
52 #include <stdlib.h>
53 #include <string.h>
54 #include <errno.h>
55
56 static void dofile();
57 static void usage();
58
59 /* nohup shall exit with one of the following values:
60 126 - The utility was found but could not be invoked.
61 127 - An error occured in the nohup utility, or the utility could
62 not be found. */
63 #define EXIT_NOEXEC 126
64 #define EXIT_NOTFOUND 127
65 #define EXIT_MISC 127
66
67 int
68 main(argc, argv)
69 int argc;
70 char **argv;
71 {
72 int exit_status;
73
74 if (argc < 2)
75 usage();
76
77 if (isatty(STDOUT_FILENO))
78 dofile();
79 if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) {
80 /* may have just closed stderr */
81 (void)fprintf(stdin, "nohup: %s\n", strerror(errno));
82 exit(EXIT_MISC);
83 }
84
85 /* The nohup utility shall take the standard action for all signals
86 except that SIGHUP shall be ignored. */
87 (void)signal(SIGHUP, SIG_IGN);
88
89 execvp(argv[1], &argv[1]);
90 exit_status = (errno = ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC;
91 (void)fprintf(stderr, "nohup: %s: %s\n", argv[1], strerror(errno));
92 exit(exit_status);
93 }
94
95 static void
96 dofile()
97 {
98 int fd;
99 char *p, path[MAXPATHLEN];
100
101 /* If the standard output is a terminal, all output written to
102 its standard output shall be appended to the end of the file
103 nohup.out in the current directory. If nohup.out cannot be
104 created or opened for appending, the output shall be appended
105 to the end of the file nohup.out in the directory specified
106 by the HOME environment variable.
107
108 If a file is created, the file's permission bits shall be
109 set to S_IRUSR | S_IWUSR. */
110 #define FILENAME "nohup.out"
111 p = FILENAME;
112 if ((fd = open(p, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0)
113 goto dupit;
114 if ((p = getenv("HOME")) != NULL) {
115 (void)strcpy(path, p);
116 (void)strcat(path, "/");
117 (void)strcat(path, FILENAME);
118 if ((fd = open(p = path, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0)
119 goto dupit;
120 }
121 (void)fprintf(stderr, "nohup: can't open a nohup.out file.\n");
122 exit(EXIT_MISC);
123
124 dupit: (void)lseek(fd, 0L, SEEK_END);
125 if (dup2(fd, STDOUT_FILENO) == -1) {
126 (void)fprintf(stderr, "nohup: %s\n", strerror(errno));
127 exit(EXIT_MISC);
128 }
129 (void)fprintf(stderr, "sending output to %s\n", p);
130 }
131
132 static void
133 usage()
134 {
135 (void)fprintf(stderr, "usage: nohup command\n");
136 exit(EXIT_MISC);
137 }
138