]> git.saurik.com Git - apple/shell_cmds.git/blame - nohup/nohup.c
shell_cmds-179.tar.gz
[apple/shell_cmds.git] / nohup / nohup.c
CommitLineData
44bd5ea7 1/*
e1a085ba
A
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.
44bd5ea7
A
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.
e1a085ba 14 * 3. Neither the name of the University nor the names of its contributors
44bd5ea7
A
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
e1a085ba 31#if 0
44bd5ea7 32#ifndef lint
e1a085ba
A
33static const char copyright[] =
34"@(#) Copyright (c) 1989, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
44bd5ea7
A
36#endif /* not lint */
37
38#ifndef lint
e1a085ba 39static char sccsid[] = "@(#)nohup.c 8.1 (Berkeley) 6/6/93";
44bd5ea7 40#endif /* not lint */
e1a085ba
A
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
44bd5ea7
A
46
47#include <sys/param.h>
44bd5ea7 48#include <sys/stat.h>
e1a085ba
A
49
50#include <err.h>
51#include <errno.h>
44bd5ea7 52#include <fcntl.h>
44bd5ea7
A
53#include <signal.h>
54#include <stdio.h>
55#include <stdlib.h>
56#include <string.h>
e1a085ba
A
57#include <unistd.h>
58
59#ifdef __APPLE__
f14763b6 60#include <TargetConditionals.h>
e1a085ba
A
61#include <vproc.h>
62#include <vproc_priv.h>
63#endif
44bd5ea7 64
e1a085ba
A
65static void dofile(void);
66static void usage(void);
44bd5ea7 67
e1a085ba
A
68#define FILENAME "nohup.out"
69/*
70 * POSIX mandates that we exit with:
71 * 126 - If the utility was found, but failed to execute.
72 * 127 - If any other error occurred.
73 */
74#define EXIT_NOEXEC 126
75#define EXIT_NOTFOUND 127
76#define EXIT_MISC 127
44bd5ea7
A
77
78int
e1a085ba 79main(int argc, char *argv[])
44bd5ea7
A
80{
81 int exit_status;
82
e1a085ba
A
83 while (getopt(argc, argv, "") != -1)
84 usage();
85 argc -= optind;
86 argv += optind;
87 if (argc < 1)
44bd5ea7
A
88 usage();
89
90 if (isatty(STDOUT_FILENO))
91 dofile();
e1a085ba 92 if (isatty(STDERR_FILENO) && dup2(STDOUT_FILENO, STDERR_FILENO) == -1)
44bd5ea7 93 /* may have just closed stderr */
e1a085ba 94 err(EXIT_MISC, "%s", argv[0]);
44bd5ea7 95
44bd5ea7
A
96 (void)signal(SIGHUP, SIG_IGN);
97
f14763b6 98#if defined(__APPLE__) && !TARGET_OS_EMBEDDED
ddb4a88b
A
99 if (_vprocmgr_detach_from_console(0) != NULL)
100 err(EXIT_MISC, "can't detach from console");
e1a085ba
A
101#endif
102 execvp(*argv, argv);
44bd5ea7 103 exit_status = (errno == ENOENT) ? EXIT_NOTFOUND : EXIT_NOEXEC;
e1a085ba 104 err(exit_status, "%s", argv[0]);
44bd5ea7
A
105}
106
107static void
e1a085ba 108dofile(void)
44bd5ea7
A
109{
110 int fd;
e1a085ba
A
111 char path[MAXPATHLEN];
112 const char *p;
113
114 /*
115 * POSIX mandates if the standard output is a terminal, the standard
116 * output is appended to nohup.out in the working directory. Failing
117 * that, it will be appended to nohup.out in the directory obtained
118 * from the HOME environment variable. If file creation is required,
119 * the mode_t is set to S_IRUSR | S_IWUSR.
120 */
44bd5ea7 121 p = FILENAME;
e1a085ba
A
122 fd = open(p, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR);
123 if (fd != -1)
44bd5ea7 124 goto dupit;
e1a085ba
A
125 if ((p = getenv("HOME")) != NULL && *p != '\0' &&
126 (size_t)snprintf(path, sizeof(path), "%s/%s", p, FILENAME) <
127 sizeof(path)) {
128 fd = open(p = path, O_RDWR | O_CREAT | O_APPEND,
129 S_IRUSR | S_IWUSR);
130 if (fd != -1)
44bd5ea7
A
131 goto dupit;
132 }
e1a085ba 133 errx(EXIT_MISC, "can't open a nohup.out file");
44bd5ea7 134
e1a085ba
A
135dupit:
136#ifdef __APPLE__
137 (void)lseek(fd, 0L, SEEK_END);
138#endif
139 if (dup2(fd, STDOUT_FILENO) == -1)
140 err(EXIT_MISC, NULL);
141 (void)fprintf(stderr, "appending output to %s\n", p);
44bd5ea7
A
142}
143
144static void
e1a085ba 145usage(void)
44bd5ea7 146{
e1a085ba 147 (void)fprintf(stderr, "usage: nohup [--] utility [arguments]\n");
44bd5ea7
A
148 exit(EXIT_MISC);
149}