Commit | Line | Data |
---|---|---|
15637ed4 RG |
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[] = "@(#)nohup.c 5.4 (Berkeley) 6/1/90"; | |
42 | #endif /* not lint */ | |
43 | ||
44 | #include <sys/param.h> | |
45 | #include <sys/signal.h> | |
46 | #include <sys/file.h> | |
57971bbc C |
47 | #include <sys/stat.h> |
48 | #include <fcntl.h> | |
15637ed4 RG |
49 | #include <unistd.h> |
50 | #include <stdio.h> | |
57971bbc C |
51 | #include <stdlib.h> |
52 | #include <string.h> | |
53 | #include <errno.h> | |
15637ed4 | 54 | |
57971bbc C |
55 | static void dofile(); |
56 | static void usage(); | |
15637ed4 | 57 | |
57971bbc C |
58 | /* nohup shall exit with one of the following values: |
59 | 126 - The utility was found but could not be invoked. | |
60 | 127 - An error occured in the nohup utility, or the utility could | |
61 | not be found. */ | |
62 | #define EXIT_NOEXEC 126 | |
63 | #define EXIT_NOTFOUND 127 | |
64 | #define EXIT_MISC 127 | |
65 | ||
66 | int | |
15637ed4 RG |
67 | main(argc, argv) |
68 | int argc; | |
69 | char **argv; | |
70 | { | |
57971bbc | 71 | int exit_status; |
15637ed4 RG |
72 | |
73 | if (argc < 2) | |
74 | usage(); | |
75 | ||
76 | if (isatty(STDOUT_FILENO)) | |
77 | dofile(); | |
78 | if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) { | |
79 | /* may have just closed stderr */ | |
80 | (void)fprintf(stdin, "nohup: %s\n", strerror(errno)); | |
57971bbc | 81 | exit(EXIT_MISC); |
15637ed4 RG |
82 | } |
83 | ||
57971bbc C |
84 | /* The nohup utility shall take the standard action for all signals |
85 | except that SIGHUP shall be ignored. */ | |
15637ed4 | 86 | (void)signal(SIGHUP, SIG_IGN); |
15637ed4 RG |
87 | |
88 | execvp(argv[1], &argv[1]); | |
57971bbc C |
89 | exit_status = (errno = ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC; |
90 | (void)fprintf(stderr, "nohup: %s: %s\n", argv[1], strerror(errno)); | |
91 | exit(exit_status); | |
15637ed4 RG |
92 | } |
93 | ||
57971bbc | 94 | static void |
15637ed4 RG |
95 | dofile() |
96 | { | |
97 | int fd; | |
98 | char *p, path[MAXPATHLEN]; | |
15637ed4 | 99 | |
57971bbc C |
100 | /* If the standard output is a terminal, all output written to |
101 | its standard output shall be appended to the end of the file | |
102 | nohup.out in the current directory. If nohup.out cannot be | |
103 | created or opened for appending, the output shall be appended | |
104 | to the end of the file nohup.out in the directory specified | |
105 | by the HOME environment variable. | |
106 | ||
107 | If a file is created, the file's permission bits shall be | |
108 | set to S_IRUSR | S_IWUSR. */ | |
15637ed4 | 109 | #define FILENAME "nohup.out" |
6ea9622d C |
110 | p = FILENAME; |
111 | if ((fd = open(p, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0) | |
15637ed4 | 112 | goto dupit; |
57971bbc | 113 | if ((p = getenv("HOME")) != NULL) { |
15637ed4 RG |
114 | (void)strcpy(path, p); |
115 | (void)strcat(path, "/"); | |
116 | (void)strcat(path, FILENAME); | |
57971bbc | 117 | if ((fd = open(p = path, O_RDWR|O_CREAT|O_APPEND, S_IRUSR|S_IWUSR)) >= 0) |
15637ed4 RG |
118 | goto dupit; |
119 | } | |
120 | (void)fprintf(stderr, "nohup: can't open a nohup.out file.\n"); | |
57971bbc | 121 | exit(EXIT_MISC); |
15637ed4 RG |
122 | |
123 | dupit: (void)lseek(fd, 0L, SEEK_END); | |
124 | if (dup2(fd, STDOUT_FILENO) == -1) { | |
125 | (void)fprintf(stderr, "nohup: %s\n", strerror(errno)); | |
57971bbc | 126 | exit(EXIT_MISC); |
15637ed4 RG |
127 | } |
128 | (void)fprintf(stderr, "sending output to %s\n", p); | |
129 | } | |
130 | ||
57971bbc | 131 | static void |
15637ed4 RG |
132 | usage() |
133 | { | |
134 | (void)fprintf(stderr, "usage: nohup command\n"); | |
57971bbc | 135 | exit(EXIT_MISC); |
15637ed4 | 136 | } |