]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 1989, 1993 | |
3 | * The Regents of the University of California. All rights reserved. | |
4 | * Portions copyright (c) 2007 Apple Inc. All rights reserved. | |
5 | * | |
6 | * Redistribution and use in source and binary forms, with or without | |
7 | * modification, are permitted provided that the following conditions | |
8 | * are met: | |
9 | * 1. Redistributions of source code must retain the above copyright | |
10 | * notice, this list of conditions and the following disclaimer. | |
11 | * 2. Redistributions in binary form must reproduce the above copyright | |
12 | * notice, this list of conditions and the following disclaimer in the | |
13 | * documentation and/or other materials provided with the distribution. | |
14 | * 3. Neither the name of the University nor the names of its contributors | |
15 | * may be used to endorse or promote products derived from this software | |
16 | * without specific prior written permission. | |
17 | * | |
18 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND | |
19 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE | |
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
24 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
25 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
26 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
27 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
28 | * SUCH DAMAGE. | |
29 | */ | |
30 | ||
31 | #if 0 | |
32 | #ifndef lint | |
33 | static const char copyright[] = | |
34 | "@(#) Copyright (c) 1989, 1993\n\ | |
35 | The Regents of the University of California. All rights reserved.\n"; | |
36 | #endif /* not lint */ | |
37 | ||
38 | #ifndef lint | |
39 | static char sccsid[] = "@(#)nohup.c 8.1 (Berkeley) 6/6/93"; | |
40 | #endif /* not lint */ | |
41 | #endif | |
42 | #include <sys/cdefs.h> | |
43 | #ifndef __APPLE__ | |
44 | __FBSDID("$FreeBSD: src/usr.bin/nohup/nohup.c,v 1.10 2003/05/03 19:44:46 obrien Exp $"); | |
45 | #endif | |
46 | ||
47 | #include <sys/param.h> | |
48 | #include <sys/stat.h> | |
49 | ||
50 | #include <err.h> | |
51 | #include <errno.h> | |
52 | #include <fcntl.h> | |
53 | #include <signal.h> | |
54 | #include <stdio.h> | |
55 | #include <stdlib.h> | |
56 | #include <string.h> | |
57 | #include <unistd.h> | |
58 | ||
59 | #ifdef __APPLE__ | |
60 | #include <vproc.h> | |
61 | #include <vproc_priv.h> | |
62 | #endif | |
63 | ||
64 | static void dofile(void); | |
65 | static void usage(void); | |
66 | ||
67 | #define FILENAME "nohup.out" | |
68 | /* | |
69 | * POSIX mandates that we exit with: | |
70 | * 126 - If the utility was found, but failed to execute. | |
71 | * 127 - If any other error occurred. | |
72 | */ | |
73 | #define EXIT_NOEXEC 126 | |
74 | #define EXIT_NOTFOUND 127 | |
75 | #define EXIT_MISC 127 | |
76 | ||
77 | int | |
78 | main(int argc, char *argv[]) | |
79 | { | |
80 | int exit_status; | |
81 | ||
82 | while (getopt(argc, argv, "") != -1) | |
83 | usage(); | |
84 | argc -= optind; | |
85 | argv += optind; | |
86 | if (argc < 1) | |
87 | usage(); | |
88 | ||
89 | if (isatty(STDOUT_FILENO)) | |
90 | dofile(); | |
91 | if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1) | |
92 | /* may have just closed stderr */ | |
93 | err(EXIT_MISC, "%s", argv[0]); | |
94 | ||
95 | (void)signal(SIGHUP, SIG_IGN); | |
96 | ||
97 | #ifdef __APPLE__ | |
98 | if (_vprocmgr_detach_from_console(0) != NULL) | |
99 | err(EXIT_MISC, "can't detach from console"); | |
100 | #endif | |
101 | execvp(*argv, argv); | |
102 | exit_status = (errno == ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC; | |
103 | err(exit_status, "%s", argv[0]); | |
104 | } | |
105 | ||
106 | static void | |
107 | dofile(void) | |
108 | { | |
109 | int fd; | |
110 | char path[MAXPATHLEN]; | |
111 | const char *p; | |
112 | ||
113 | /* | |
114 | * POSIX mandates if the standard output is a terminal, the standard | |
115 | * output is appended to nohup.out in the working directory. Failing | |
116 | * that, it will be appended to nohup.out in the directory obtained | |
117 | * from the HOME environment variable. If file creation is required, | |
118 | * the mode_t is set to S_IRUSR | S_IWUSR. | |
119 | */ | |
120 | p = FILENAME; | |
121 | fd = open(p, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR); | |
122 | if (fd != -1) | |
123 | goto dupit; | |
124 | if ((p = getenv("HOME")) != NULL && *p != '\0' && | |
125 | (size_t)snprintf(path, sizeof(path), "%s/%s", p, FILENAME) < | |
126 | sizeof(path)) { | |
127 | fd = open(p = path, O_RDWR | O_CREAT | O_APPEND, | |
128 | S_IRUSR | S_IWUSR); | |
129 | if (fd != -1) | |
130 | goto dupit; | |
131 | } | |
132 | errx(EXIT_MISC, "can't open a nohup.out file"); | |
133 | ||
134 | dupit: | |
135 | #ifdef __APPLE__ | |
136 | (void)lseek(fd, 0L, SEEK_END); | |
137 | #endif | |
138 | if (dup2(fd, STDOUT_FILENO) == -1) | |
139 | err(EXIT_MISC, NULL); | |
140 | (void)fprintf(stderr, "appending output to %s\n", p); | |
141 | } | |
142 | ||
143 | static void | |
144 | usage(void) | |
145 | { | |
146 | (void)fprintf(stderr, "usage: nohup [--] utility [arguments]\n"); | |
147 | exit(EXIT_MISC); | |
148 | } |