xref: /illumos-gate/usr/src/cmd/nohup/nohup.c (revision 2a8bcb4e)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28*7c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
29*7c478bd9Sstevel@tonic-gate 
30*7c478bd9Sstevel@tonic-gate #include <stdio.h>
31*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
32*7c478bd9Sstevel@tonic-gate #include <nl_types.h>
33*7c478bd9Sstevel@tonic-gate #include <locale.h>
34*7c478bd9Sstevel@tonic-gate #include <signal.h>
35*7c478bd9Sstevel@tonic-gate #include <string.h>
36*7c478bd9Sstevel@tonic-gate #include <limits.h>
37*7c478bd9Sstevel@tonic-gate #include <errno.h>
38*7c478bd9Sstevel@tonic-gate #include <unistd.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/stat.h>
41*7c478bd9Sstevel@tonic-gate #include <libproc.h>
42*7c478bd9Sstevel@tonic-gate #include <dirent.h>
43*7c478bd9Sstevel@tonic-gate #include <ctype.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/time.h>
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate #define	NOHUP_PERM	(S_IRUSR | S_IWUSR)
47*7c478bd9Sstevel@tonic-gate 
48*7c478bd9Sstevel@tonic-gate #define	NOHUP_NOEXEC	126
49*7c478bd9Sstevel@tonic-gate #define	NOHUP_ERROR	127
50*7c478bd9Sstevel@tonic-gate 
51*7c478bd9Sstevel@tonic-gate #ifdef XPG4
52*7c478bd9Sstevel@tonic-gate #define	OPTSTR	""
53*7c478bd9Sstevel@tonic-gate #else
54*7c478bd9Sstevel@tonic-gate #define	OPTSTR	"pFag"
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate static int pnohup(int, char **);
57*7c478bd9Sstevel@tonic-gate 
58*7c478bd9Sstevel@tonic-gate static struct ps_prochandle *g_proc;
59*7c478bd9Sstevel@tonic-gate static int g_wrfd;
60*7c478bd9Sstevel@tonic-gate static int g_rdfd;
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate static int g_dirty;
63*7c478bd9Sstevel@tonic-gate static volatile int g_interrupt = 0;
64*7c478bd9Sstevel@tonic-gate #endif
65*7c478bd9Sstevel@tonic-gate 
66*7c478bd9Sstevel@tonic-gate static int opt_p = 0;
67*7c478bd9Sstevel@tonic-gate static int opt_g = 0;
68*7c478bd9Sstevel@tonic-gate static int opt_a = 0;
69*7c478bd9Sstevel@tonic-gate static int opt_F = 0;
70*7c478bd9Sstevel@tonic-gate 
71*7c478bd9Sstevel@tonic-gate static char *pname;
72*7c478bd9Sstevel@tonic-gate 
73*7c478bd9Sstevel@tonic-gate static char nout[PATH_MAX] = "nohup.out";
74*7c478bd9Sstevel@tonic-gate 
75*7c478bd9Sstevel@tonic-gate static int
open_file(void)76*7c478bd9Sstevel@tonic-gate open_file(void)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	char *home;
79*7c478bd9Sstevel@tonic-gate 	int fd;
80*7c478bd9Sstevel@tonic-gate 	int flags = O_CREAT | O_WRONLY | O_APPEND;
81*7c478bd9Sstevel@tonic-gate 
82*7c478bd9Sstevel@tonic-gate 	if ((fd = open(nout, flags, NOHUP_PERM)) < 0) {
83*7c478bd9Sstevel@tonic-gate 		if ((home = getenv("HOME")) == NULL)
84*7c478bd9Sstevel@tonic-gate 			return (-1);
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 		if ((snprintf(nout, sizeof (nout),
87*7c478bd9Sstevel@tonic-gate 		    "%s/nohup.out", home) >= sizeof (nout)) ||
88*7c478bd9Sstevel@tonic-gate 		    (fd = open(nout, flags, NOHUP_PERM)) < 0) {
89*7c478bd9Sstevel@tonic-gate 			return (-1);
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 
92*7c478bd9Sstevel@tonic-gate 	}
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Sending output to %s\n"), nout);
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 	return (fd);
97*7c478bd9Sstevel@tonic-gate }
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate int
main(int argc,char ** argv)100*7c478bd9Sstevel@tonic-gate main(int argc, char **argv)
101*7c478bd9Sstevel@tonic-gate {
102*7c478bd9Sstevel@tonic-gate 	int fd = -1;
103*7c478bd9Sstevel@tonic-gate 	int opt;
104*7c478bd9Sstevel@tonic-gate 	int err;
105*7c478bd9Sstevel@tonic-gate 
106*7c478bd9Sstevel@tonic-gate 	if ((pname = strrchr(argv[0], '/')) == NULL)
107*7c478bd9Sstevel@tonic-gate 		pname = argv[0];
108*7c478bd9Sstevel@tonic-gate 	else
109*7c478bd9Sstevel@tonic-gate 		argv[0] = ++pname;		/* for getopt */
110*7c478bd9Sstevel@tonic-gate 
111*7c478bd9Sstevel@tonic-gate 	(void) setlocale(LC_ALL, "");
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate #ifndef TEXT_DOMAIN
114*7c478bd9Sstevel@tonic-gate #define	TEXT_DOMAIN "SYS_TEST"
115*7c478bd9Sstevel@tonic-gate #endif
116*7c478bd9Sstevel@tonic-gate 
117*7c478bd9Sstevel@tonic-gate 	(void) textdomain(TEXT_DOMAIN);
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	while ((opt = getopt(argc, argv, OPTSTR)) != EOF) {
120*7c478bd9Sstevel@tonic-gate 		switch (opt) {
121*7c478bd9Sstevel@tonic-gate 		case 'p':
122*7c478bd9Sstevel@tonic-gate 			opt_p = 1;
123*7c478bd9Sstevel@tonic-gate 			break;
124*7c478bd9Sstevel@tonic-gate 		case 'F':
125*7c478bd9Sstevel@tonic-gate 			opt_F = 1;
126*7c478bd9Sstevel@tonic-gate 			break;
127*7c478bd9Sstevel@tonic-gate 		case 'a':
128*7c478bd9Sstevel@tonic-gate 			opt_a = 1;
129*7c478bd9Sstevel@tonic-gate 			break;
130*7c478bd9Sstevel@tonic-gate 		case 'g':
131*7c478bd9Sstevel@tonic-gate 			opt_g = 1;
132*7c478bd9Sstevel@tonic-gate 			break;
133*7c478bd9Sstevel@tonic-gate 		default:
134*7c478bd9Sstevel@tonic-gate 			goto usage;
135*7c478bd9Sstevel@tonic-gate 		}
136*7c478bd9Sstevel@tonic-gate 	}
137*7c478bd9Sstevel@tonic-gate 
138*7c478bd9Sstevel@tonic-gate 	argc -= optind;
139*7c478bd9Sstevel@tonic-gate 	argv += optind;
140*7c478bd9Sstevel@tonic-gate 
141*7c478bd9Sstevel@tonic-gate 	if (argc == 0)
142*7c478bd9Sstevel@tonic-gate 		goto usage;			/* need at least one argument */
143*7c478bd9Sstevel@tonic-gate 
144*7c478bd9Sstevel@tonic-gate #ifndef XPG4
145*7c478bd9Sstevel@tonic-gate 	if (opt_p && opt_g)
146*7c478bd9Sstevel@tonic-gate 		goto usage;
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (opt_p || opt_g)
149*7c478bd9Sstevel@tonic-gate 		return (pnohup(argc, argv));
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	if (opt_a || opt_F)
152*7c478bd9Sstevel@tonic-gate 		goto usage;			/* only valid with -p or -g */
153*7c478bd9Sstevel@tonic-gate #endif
154*7c478bd9Sstevel@tonic-gate 
155*7c478bd9Sstevel@tonic-gate 	argv[argc] = NULL;
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGHUP, SIG_IGN);		/* POSIX.2 only SIGHUP */
158*7c478bd9Sstevel@tonic-gate #ifndef XPG4
159*7c478bd9Sstevel@tonic-gate 	(void) signal(SIGQUIT, SIG_IGN);	/* Solaris compatibility */
160*7c478bd9Sstevel@tonic-gate #endif
161*7c478bd9Sstevel@tonic-gate 
162*7c478bd9Sstevel@tonic-gate 	if (isatty(STDOUT_FILENO)) {
163*7c478bd9Sstevel@tonic-gate 		if ((fd = open_file()) < 0)
164*7c478bd9Sstevel@tonic-gate 			goto err;
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDOUT_FILENO);
167*7c478bd9Sstevel@tonic-gate 	}
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	if (isatty(STDERR_FILENO)) {
170*7c478bd9Sstevel@tonic-gate 		if (fd < 0 && (fd = open_file()) < 0)
171*7c478bd9Sstevel@tonic-gate 			goto err;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 		(void) dup2(fd, STDERR_FILENO);
174*7c478bd9Sstevel@tonic-gate 	}
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	if (fd >= 0)
177*7c478bd9Sstevel@tonic-gate 		(void) close(fd);
178*7c478bd9Sstevel@tonic-gate 
179*7c478bd9Sstevel@tonic-gate 	(void) execvp(argv[0], argv);
180*7c478bd9Sstevel@tonic-gate 	err = errno;
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	(void) freopen("/dev/tty", "w", stderr);
183*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("nohup: %s: %s\n"), argv[0],
184*7c478bd9Sstevel@tonic-gate 	    strerror(err));
185*7c478bd9Sstevel@tonic-gate 
186*7c478bd9Sstevel@tonic-gate 	return (err == ENOENT ? NOHUP_ERROR : NOHUP_NOEXEC);
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate err:
189*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("nohup: cannot open/create "
190*7c478bd9Sstevel@tonic-gate 	    "nohup.out: %s\n"), strerror(errno));
191*7c478bd9Sstevel@tonic-gate 	return (NOHUP_ERROR);
192*7c478bd9Sstevel@tonic-gate 
193*7c478bd9Sstevel@tonic-gate usage:
194*7c478bd9Sstevel@tonic-gate #ifdef XPG4
195*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr,
196*7c478bd9Sstevel@tonic-gate 	    gettext("usage: nohup command [argument ...]\n"));
197*7c478bd9Sstevel@tonic-gate #else
198*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("usage:\n"
199*7c478bd9Sstevel@tonic-gate 	    "\tnohup command [argument ...]\n"
200*7c478bd9Sstevel@tonic-gate 	    "\tnohup -p [-Fa] pid [pid ...]\n"
201*7c478bd9Sstevel@tonic-gate 	    "\tnohup -g [-Fa] pgid [pgid ...]\n"));
202*7c478bd9Sstevel@tonic-gate #endif
203*7c478bd9Sstevel@tonic-gate 	return (NOHUP_ERROR);
204*7c478bd9Sstevel@tonic-gate }
205*7c478bd9Sstevel@tonic-gate 
206*7c478bd9Sstevel@tonic-gate #ifndef XPG4
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate /*
209*7c478bd9Sstevel@tonic-gate  * File descriptor iteration interface.
210*7c478bd9Sstevel@tonic-gate  */
211*7c478bd9Sstevel@tonic-gate typedef int proc_fd_iter_f(void *, int);
212*7c478bd9Sstevel@tonic-gate 
213*7c478bd9Sstevel@tonic-gate static int
Pfd_iter(struct ps_prochandle * P,proc_fd_iter_f * cb,void * data)214*7c478bd9Sstevel@tonic-gate Pfd_iter(struct ps_prochandle *P, proc_fd_iter_f *cb, void *data)
215*7c478bd9Sstevel@tonic-gate {
216*7c478bd9Sstevel@tonic-gate 	char file[64];
217*7c478bd9Sstevel@tonic-gate 	dirent_t *dentp;
218*7c478bd9Sstevel@tonic-gate 	DIR *dirp;
219*7c478bd9Sstevel@tonic-gate 	int ret = 0;
220*7c478bd9Sstevel@tonic-gate 
221*7c478bd9Sstevel@tonic-gate 	if (Pstate(P) == PS_DEAD)
222*7c478bd9Sstevel@tonic-gate 		return (-1);
223*7c478bd9Sstevel@tonic-gate 
224*7c478bd9Sstevel@tonic-gate 	(void) sprintf(file, "/proc/%d/fd", (int)Pstatus(P)->pr_pid);
225*7c478bd9Sstevel@tonic-gate 	if ((dirp = opendir(file)) == NULL)
226*7c478bd9Sstevel@tonic-gate 		return (-1);
227*7c478bd9Sstevel@tonic-gate 
228*7c478bd9Sstevel@tonic-gate 	while ((dentp = readdir(dirp)) != NULL) {
229*7c478bd9Sstevel@tonic-gate 		if (dentp->d_name[0] == '.')
230*7c478bd9Sstevel@tonic-gate 			continue;
231*7c478bd9Sstevel@tonic-gate 
232*7c478bd9Sstevel@tonic-gate 		if ((ret = cb(data, atoi(dentp->d_name))) != 0)
233*7c478bd9Sstevel@tonic-gate 			break;
234*7c478bd9Sstevel@tonic-gate 	}
235*7c478bd9Sstevel@tonic-gate 
236*7c478bd9Sstevel@tonic-gate 	(void) closedir(dirp);
237*7c478bd9Sstevel@tonic-gate 
238*7c478bd9Sstevel@tonic-gate 	return (ret);
239*7c478bd9Sstevel@tonic-gate }
240*7c478bd9Sstevel@tonic-gate 
241*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
242*7c478bd9Sstevel@tonic-gate static int
fd_cb(void * data,int fd)243*7c478bd9Sstevel@tonic-gate fd_cb(void *data, int fd)
244*7c478bd9Sstevel@tonic-gate {
245*7c478bd9Sstevel@tonic-gate 	struct stat64 sbuf;
246*7c478bd9Sstevel@tonic-gate 	int flags;
247*7c478bd9Sstevel@tonic-gate 	int *fdp;
248*7c478bd9Sstevel@tonic-gate 	int oflags;
249*7c478bd9Sstevel@tonic-gate 	char *file;
250*7c478bd9Sstevel@tonic-gate 	int tmpfd;
251*7c478bd9Sstevel@tonic-gate 
252*7c478bd9Sstevel@tonic-gate 	/*
253*7c478bd9Sstevel@tonic-gate 	 * See if this fd refers to the controlling tty.
254*7c478bd9Sstevel@tonic-gate 	 */
255*7c478bd9Sstevel@tonic-gate 	if (pr_fstat64(g_proc, fd, &sbuf) == -1 ||
256*7c478bd9Sstevel@tonic-gate 	    sbuf.st_rdev != Ppsinfo(g_proc)->pr_ttydev)
257*7c478bd9Sstevel@tonic-gate 		return (0);
258*7c478bd9Sstevel@tonic-gate 
259*7c478bd9Sstevel@tonic-gate 	/*
260*7c478bd9Sstevel@tonic-gate 	 * tty's opened for input are usually O_RDWR so that the program
261*7c478bd9Sstevel@tonic-gate 	 * can change terminal settings. We assume that if there's a
262*7c478bd9Sstevel@tonic-gate 	 * controlling tty in the STDIN_FILENO file descriptor that is
263*7c478bd9Sstevel@tonic-gate 	 * effectively used only for input. If standard in gets dup'ed to
264*7c478bd9Sstevel@tonic-gate 	 * other file descriptors, then we're out of luck unless the
265*7c478bd9Sstevel@tonic-gate 	 * program is nice enough to fcntl it to be O_RDONLY. We close the
266*7c478bd9Sstevel@tonic-gate 	 * file descriptor before we call open to handle the case that
267*7c478bd9Sstevel@tonic-gate 	 * there are no available file descriptors left in the victim. If
268*7c478bd9Sstevel@tonic-gate 	 * our call to pr_open fails, we try to reopen the controlling tty.
269*7c478bd9Sstevel@tonic-gate 	 */
270*7c478bd9Sstevel@tonic-gate 	flags = pr_fcntl(g_proc, fd, F_GETFL, NULL);
271*7c478bd9Sstevel@tonic-gate 	if ((flags & O_ACCMODE) == O_RDONLY || fd == STDIN_FILENO) {
272*7c478bd9Sstevel@tonic-gate 		fdp = &g_rdfd;
273*7c478bd9Sstevel@tonic-gate 		oflags = O_RDONLY;
274*7c478bd9Sstevel@tonic-gate 		file = "/dev/null";
275*7c478bd9Sstevel@tonic-gate 	} else {
276*7c478bd9Sstevel@tonic-gate 		fdp = &g_wrfd;
277*7c478bd9Sstevel@tonic-gate 		oflags = O_RDWR | O_APPEND;
278*7c478bd9Sstevel@tonic-gate 		file = &nout[0];
279*7c478bd9Sstevel@tonic-gate 	}
280*7c478bd9Sstevel@tonic-gate 
281*7c478bd9Sstevel@tonic-gate 	if (*fdp < 0) {
282*7c478bd9Sstevel@tonic-gate 		(void) pr_close(g_proc, fd);
283*7c478bd9Sstevel@tonic-gate 
284*7c478bd9Sstevel@tonic-gate 		tmpfd = pr_open(g_proc, file, oflags, 0);
285*7c478bd9Sstevel@tonic-gate 
286*7c478bd9Sstevel@tonic-gate 		if (tmpfd < 0) {
287*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr,
288*7c478bd9Sstevel@tonic-gate 			    gettext("nohup: process %d cannot open %s: %s\n"),
289*7c478bd9Sstevel@tonic-gate 			    Pstatus(g_proc)->pr_pid, file, strerror(errno));
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 			goto err;
292*7c478bd9Sstevel@tonic-gate 		}
293*7c478bd9Sstevel@tonic-gate 
294*7c478bd9Sstevel@tonic-gate 		if (tmpfd != fd) {
295*7c478bd9Sstevel@tonic-gate 			(void) pr_fcntl(g_proc, tmpfd, F_DUP2FD,
296*7c478bd9Sstevel@tonic-gate 				(void *)(uintptr_t)fd);
297*7c478bd9Sstevel@tonic-gate 			(void) pr_close(g_proc, tmpfd);
298*7c478bd9Sstevel@tonic-gate 		}
299*7c478bd9Sstevel@tonic-gate 
300*7c478bd9Sstevel@tonic-gate 		*fdp = fd;
301*7c478bd9Sstevel@tonic-gate 	} else {
302*7c478bd9Sstevel@tonic-gate 		(void) pr_fcntl(g_proc, *fdp, F_DUP2FD, (void *)(uintptr_t)fd);
303*7c478bd9Sstevel@tonic-gate 	}
304*7c478bd9Sstevel@tonic-gate 
305*7c478bd9Sstevel@tonic-gate 	return (0);
306*7c478bd9Sstevel@tonic-gate 
307*7c478bd9Sstevel@tonic-gate err:
308*7c478bd9Sstevel@tonic-gate 	/*
309*7c478bd9Sstevel@tonic-gate 	 * The victim couldn't open nohup.out so we'll have it try to reopen
310*7c478bd9Sstevel@tonic-gate 	 * its terminal. If this fails, we are left with little recourse.
311*7c478bd9Sstevel@tonic-gate 	 */
312*7c478bd9Sstevel@tonic-gate 	tmpfd = pr_open(g_proc, "/dev/tty", O_RDWR, 0);
313*7c478bd9Sstevel@tonic-gate 
314*7c478bd9Sstevel@tonic-gate 	if (tmpfd != fd && tmpfd >= 0) {
315*7c478bd9Sstevel@tonic-gate 		(void) pr_fcntl(g_proc, tmpfd, F_DUP2FD, (void *)(uintptr_t)fd);
316*7c478bd9Sstevel@tonic-gate 		(void) pr_close(g_proc, tmpfd);
317*7c478bd9Sstevel@tonic-gate 	}
318*7c478bd9Sstevel@tonic-gate 
319*7c478bd9Sstevel@tonic-gate 	return (1);
320*7c478bd9Sstevel@tonic-gate }
321*7c478bd9Sstevel@tonic-gate 
322*7c478bd9Sstevel@tonic-gate static int
lwp_restartable(short syscall)323*7c478bd9Sstevel@tonic-gate lwp_restartable(short syscall)
324*7c478bd9Sstevel@tonic-gate {
325*7c478bd9Sstevel@tonic-gate 	switch (syscall) {
326*7c478bd9Sstevel@tonic-gate 	case SYS_read:
327*7c478bd9Sstevel@tonic-gate 	case SYS_readv:
328*7c478bd9Sstevel@tonic-gate 	case SYS_pread:
329*7c478bd9Sstevel@tonic-gate 	case SYS_pread64:
330*7c478bd9Sstevel@tonic-gate 	case SYS_write:
331*7c478bd9Sstevel@tonic-gate 	case SYS_writev:
332*7c478bd9Sstevel@tonic-gate 	case SYS_pwrite:
333*7c478bd9Sstevel@tonic-gate 	case SYS_pwrite64:
334*7c478bd9Sstevel@tonic-gate 	case SYS_ioctl:
335*7c478bd9Sstevel@tonic-gate 	case SYS_fcntl:
336*7c478bd9Sstevel@tonic-gate 	case SYS_getmsg:
337*7c478bd9Sstevel@tonic-gate 	case SYS_getpmsg:
338*7c478bd9Sstevel@tonic-gate 	case SYS_putmsg:
339*7c478bd9Sstevel@tonic-gate 	case SYS_putpmsg:
340*7c478bd9Sstevel@tonic-gate 	case SYS_recv:
341*7c478bd9Sstevel@tonic-gate 	case SYS_recvmsg:
342*7c478bd9Sstevel@tonic-gate 	case SYS_recvfrom:
343*7c478bd9Sstevel@tonic-gate 	case SYS_send:
344*7c478bd9Sstevel@tonic-gate 	case SYS_sendmsg:
345*7c478bd9Sstevel@tonic-gate 	case SYS_sendto:
346*7c478bd9Sstevel@tonic-gate 		return (1);
347*7c478bd9Sstevel@tonic-gate 	}
348*7c478bd9Sstevel@tonic-gate 
349*7c478bd9Sstevel@tonic-gate 	return (0);
350*7c478bd9Sstevel@tonic-gate }
351*7c478bd9Sstevel@tonic-gate 
352*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
353*7c478bd9Sstevel@tonic-gate static int
lwp_abort(void * data,const lwpstatus_t * lsp)354*7c478bd9Sstevel@tonic-gate lwp_abort(void *data, const lwpstatus_t *lsp)
355*7c478bd9Sstevel@tonic-gate {
356*7c478bd9Sstevel@tonic-gate 	struct ps_lwphandle *L;
357*7c478bd9Sstevel@tonic-gate 	int err;
358*7c478bd9Sstevel@tonic-gate 
359*7c478bd9Sstevel@tonic-gate 	/*
360*7c478bd9Sstevel@tonic-gate 	 * Continue if this lwp isn't asleep in a restartable syscall.
361*7c478bd9Sstevel@tonic-gate 	 */
362*7c478bd9Sstevel@tonic-gate 	if (!(lsp->pr_flags & PR_ASLEEP) || !lwp_restartable(lsp->pr_syscall))
363*7c478bd9Sstevel@tonic-gate 		return (0);
364*7c478bd9Sstevel@tonic-gate 
365*7c478bd9Sstevel@tonic-gate 	L = Lgrab(g_proc, lsp->pr_lwpid, &err);
366*7c478bd9Sstevel@tonic-gate 	(void) Lsetrun(L, 0, PRSABORT);
367*7c478bd9Sstevel@tonic-gate 	Lfree(L);
368*7c478bd9Sstevel@tonic-gate 
369*7c478bd9Sstevel@tonic-gate 	/*
370*7c478bd9Sstevel@tonic-gate 	 * Indicate that we have aborted a syscall.
371*7c478bd9Sstevel@tonic-gate 	 */
372*7c478bd9Sstevel@tonic-gate 	g_dirty = 1;
373*7c478bd9Sstevel@tonic-gate 
374*7c478bd9Sstevel@tonic-gate 	return (0);
375*7c478bd9Sstevel@tonic-gate }
376*7c478bd9Sstevel@tonic-gate 
377*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
378*7c478bd9Sstevel@tonic-gate static int
lwp_restart(void * data,const lwpstatus_t * lsp)379*7c478bd9Sstevel@tonic-gate lwp_restart(void *data, const lwpstatus_t *lsp)
380*7c478bd9Sstevel@tonic-gate {
381*7c478bd9Sstevel@tonic-gate 	struct ps_lwphandle *L;
382*7c478bd9Sstevel@tonic-gate 	int err;
383*7c478bd9Sstevel@tonic-gate 
384*7c478bd9Sstevel@tonic-gate 	/*
385*7c478bd9Sstevel@tonic-gate 	 * If any lwp is still sleeping in a restartable syscall, it means
386*7c478bd9Sstevel@tonic-gate 	 * the lwp is wedged and we've screwed up.
387*7c478bd9Sstevel@tonic-gate 	 */
388*7c478bd9Sstevel@tonic-gate 	if (lsp->pr_flags & PR_ASLEEP) {
389*7c478bd9Sstevel@tonic-gate 		if (!lwp_restartable(lsp->pr_syscall))
390*7c478bd9Sstevel@tonic-gate 			return (0);
391*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: LWP %d failed "
392*7c478bd9Sstevel@tonic-gate 		    "to abort syscall (%d) in process %d\n"),
393*7c478bd9Sstevel@tonic-gate 		    lsp->pr_lwpid, lsp->pr_syscall, Pstatus(g_proc)->pr_pid);
394*7c478bd9Sstevel@tonic-gate 		return (1);
395*7c478bd9Sstevel@tonic-gate 	}
396*7c478bd9Sstevel@tonic-gate 
397*7c478bd9Sstevel@tonic-gate 	if (lsp->pr_why == PR_SYSEXIT && lsp->pr_errno == EINTR) {
398*7c478bd9Sstevel@tonic-gate 		L = Lgrab(g_proc, lsp->pr_lwpid, &err);
399*7c478bd9Sstevel@tonic-gate 		(void) Lputareg(L, R_R0, ERESTART);
400*7c478bd9Sstevel@tonic-gate 		Lsync(L);
401*7c478bd9Sstevel@tonic-gate 		Lfree(L);
402*7c478bd9Sstevel@tonic-gate 	}
403*7c478bd9Sstevel@tonic-gate 
404*7c478bd9Sstevel@tonic-gate 	return (0);
405*7c478bd9Sstevel@tonic-gate }
406*7c478bd9Sstevel@tonic-gate 
407*7c478bd9Sstevel@tonic-gate static int
do_pnohup(struct ps_prochandle * P)408*7c478bd9Sstevel@tonic-gate do_pnohup(struct ps_prochandle *P)
409*7c478bd9Sstevel@tonic-gate {
410*7c478bd9Sstevel@tonic-gate 	int sig = 0;
411*7c478bd9Sstevel@tonic-gate 	struct sigaction sa;
412*7c478bd9Sstevel@tonic-gate 	const pstatus_t *psp;
413*7c478bd9Sstevel@tonic-gate 
414*7c478bd9Sstevel@tonic-gate 	psp = Pstatus(P);
415*7c478bd9Sstevel@tonic-gate 
416*7c478bd9Sstevel@tonic-gate 	/*
417*7c478bd9Sstevel@tonic-gate 	 * Make sure there's a pending procfs stop directive.
418*7c478bd9Sstevel@tonic-gate 	 */
419*7c478bd9Sstevel@tonic-gate 	(void) Pdstop(P);
420*7c478bd9Sstevel@tonic-gate 
421*7c478bd9Sstevel@tonic-gate 	if (Pcreate_agent(P) != 0) {
422*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: cannot control "
423*7c478bd9Sstevel@tonic-gate 		    "process %d\n"), psp->pr_pid);
424*7c478bd9Sstevel@tonic-gate 		goto err_no_agent;
425*7c478bd9Sstevel@tonic-gate 	}
426*7c478bd9Sstevel@tonic-gate 
427*7c478bd9Sstevel@tonic-gate 	/*
428*7c478bd9Sstevel@tonic-gate 	 * Set the disposition of SIGHUP and SIGQUIT to SIG_IGN. If either
429*7c478bd9Sstevel@tonic-gate 	 * signal is handled by the victim, only adjust the disposition if
430*7c478bd9Sstevel@tonic-gate 	 * the -a flag is set.
431*7c478bd9Sstevel@tonic-gate 	 */
432*7c478bd9Sstevel@tonic-gate 	if (!opt_a && pr_sigaction(P, SIGHUP, NULL, &sa) != 0) {
433*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: cannot read "
434*7c478bd9Sstevel@tonic-gate 		    "disposition of SIGHUP for %d\n"), psp->pr_pid);
435*7c478bd9Sstevel@tonic-gate 		goto no_sigs;
436*7c478bd9Sstevel@tonic-gate 	}
437*7c478bd9Sstevel@tonic-gate 
438*7c478bd9Sstevel@tonic-gate 	if (!opt_a && sa.sa_handler != SIG_DFL && sa.sa_handler != SIG_IGN) {
439*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: SIGHUP already handled "
440*7c478bd9Sstevel@tonic-gate 		    "by %d; use -a to force process to ignore\n"), psp->pr_pid);
441*7c478bd9Sstevel@tonic-gate 		goto no_sigs;
442*7c478bd9Sstevel@tonic-gate 	}
443*7c478bd9Sstevel@tonic-gate 
444*7c478bd9Sstevel@tonic-gate 	if (!opt_a && pr_sigaction(P, SIGQUIT, NULL, &sa) != 0) {
445*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: cannot read "
446*7c478bd9Sstevel@tonic-gate 		    "disposition of SIGQUIT for %d\n"), psp->pr_pid);
447*7c478bd9Sstevel@tonic-gate 		goto no_sigs;
448*7c478bd9Sstevel@tonic-gate 	}
449*7c478bd9Sstevel@tonic-gate 
450*7c478bd9Sstevel@tonic-gate 	if (!opt_a && sa.sa_handler != SIG_DFL && sa.sa_handler != SIG_IGN) {
451*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: SIGQUIT already handled "
452*7c478bd9Sstevel@tonic-gate 		    "by %d; use -a to force process to ignore\n"), psp->pr_pid);
453*7c478bd9Sstevel@tonic-gate 		goto no_sigs;
454*7c478bd9Sstevel@tonic-gate 	}
455*7c478bd9Sstevel@tonic-gate 
456*7c478bd9Sstevel@tonic-gate 	sa.sa_handler = SIG_IGN;
457*7c478bd9Sstevel@tonic-gate 
458*7c478bd9Sstevel@tonic-gate 	if (pr_sigaction(P, SIGHUP, &sa, NULL) != 0) {
459*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: cannot set "
460*7c478bd9Sstevel@tonic-gate 		    "disposition of SIGHUP for %d\n"), psp->pr_pid);
461*7c478bd9Sstevel@tonic-gate 		goto no_sigs;
462*7c478bd9Sstevel@tonic-gate 	}
463*7c478bd9Sstevel@tonic-gate 
464*7c478bd9Sstevel@tonic-gate 	if (pr_sigaction(P, SIGQUIT, &sa, NULL) != 0) {
465*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: cannot set "
466*7c478bd9Sstevel@tonic-gate 		    "disposition of SIGQUIT for %d\n"), psp->pr_pid);
467*7c478bd9Sstevel@tonic-gate 		goto no_sigs;
468*7c478bd9Sstevel@tonic-gate 	}
469*7c478bd9Sstevel@tonic-gate 
470*7c478bd9Sstevel@tonic-gate no_sigs:
471*7c478bd9Sstevel@tonic-gate 	Pdestroy_agent(P);
472*7c478bd9Sstevel@tonic-gate 
473*7c478bd9Sstevel@tonic-gate 	/*
474*7c478bd9Sstevel@tonic-gate 	 * We need to close and reassign some file descriptors, but we
475*7c478bd9Sstevel@tonic-gate 	 * need to be careful about how we do it. If we send in the agent
476*7c478bd9Sstevel@tonic-gate 	 * to close some fd and there's an lwp asleep in the kernel due to
477*7c478bd9Sstevel@tonic-gate 	 * a syscall using that fd, then we have a problem. The normal
478*7c478bd9Sstevel@tonic-gate 	 * sequence of events is the close syscall wakes up any threads
479*7c478bd9Sstevel@tonic-gate 	 * that have the fd in question active (see kthread.t_activefd)
480*7c478bd9Sstevel@tonic-gate 	 * and then waits for those threads to wake up and release the
481*7c478bd9Sstevel@tonic-gate 	 * file descriptors (they then continue to user-land to return
482*7c478bd9Sstevel@tonic-gate 	 * EBADF from the syscall). However, recall that if the agent lwp
483*7c478bd9Sstevel@tonic-gate 	 * is present in a process, no other lwps can run, so if the agent
484*7c478bd9Sstevel@tonic-gate 	 * lwp itself is making the call to close(2) (or something else
485*7c478bd9Sstevel@tonic-gate 	 * like dup2 that involves a call to closeandsetf()) then we're in
486*7c478bd9Sstevel@tonic-gate 	 * pretty bad shape. The solution is to abort and restart any lwp
487*7c478bd9Sstevel@tonic-gate 	 * asleep in a syscall on the off chance that it may be using one
488*7c478bd9Sstevel@tonic-gate 	 * of the file descriptors that we want to manipulate.
489*7c478bd9Sstevel@tonic-gate 	 */
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	/*
492*7c478bd9Sstevel@tonic-gate 	 * We may need to chase some lwps out of the kernel briefly, so we
493*7c478bd9Sstevel@tonic-gate 	 * send SIGCONT to the process if it was previously stopped due to
494*7c478bd9Sstevel@tonic-gate 	 * a job control signal, and save the current signal to repost it
495*7c478bd9Sstevel@tonic-gate 	 * when we detatch from the victim. A process that is stopped due
496*7c478bd9Sstevel@tonic-gate 	 * to job control will start running as soon as we send SIGCONT
497*7c478bd9Sstevel@tonic-gate 	 * since there is no procfs stop command pending; we use Pdstop to
498*7c478bd9Sstevel@tonic-gate 	 * post a procfs stop request (above).
499*7c478bd9Sstevel@tonic-gate 	 */
500*7c478bd9Sstevel@tonic-gate 	if ((psp->pr_lwp.pr_flags & PR_STOPPED) &&
501*7c478bd9Sstevel@tonic-gate 	    psp->pr_lwp.pr_why == PR_JOBCONTROL) {
502*7c478bd9Sstevel@tonic-gate 		sig = psp->pr_lwp.pr_what;
503*7c478bd9Sstevel@tonic-gate 		(void) kill(psp->pr_pid, SIGCONT);
504*7c478bd9Sstevel@tonic-gate 		(void) Pwait(P, 0);
505*7c478bd9Sstevel@tonic-gate 	}
506*7c478bd9Sstevel@tonic-gate 
507*7c478bd9Sstevel@tonic-gate 	(void) Psysexit(P, 0, 1);
508*7c478bd9Sstevel@tonic-gate 
509*7c478bd9Sstevel@tonic-gate 	/*
510*7c478bd9Sstevel@tonic-gate 	 * Abort each syscall; set g_dirty if any lwp was asleep.
511*7c478bd9Sstevel@tonic-gate 	 */
512*7c478bd9Sstevel@tonic-gate 	g_dirty = 0;
513*7c478bd9Sstevel@tonic-gate 	g_proc = P;
514*7c478bd9Sstevel@tonic-gate 	(void) Plwp_iter(P, lwp_abort, NULL);
515*7c478bd9Sstevel@tonic-gate 
516*7c478bd9Sstevel@tonic-gate 	if (g_dirty) {
517*7c478bd9Sstevel@tonic-gate 		/*
518*7c478bd9Sstevel@tonic-gate 		 * Block until each lwp that was asleep in a syscall has
519*7c478bd9Sstevel@tonic-gate 		 * wandered back up to user-land.
520*7c478bd9Sstevel@tonic-gate 		 */
521*7c478bd9Sstevel@tonic-gate 		(void) Pwait(P, 0);
522*7c478bd9Sstevel@tonic-gate 
523*7c478bd9Sstevel@tonic-gate 		/*
524*7c478bd9Sstevel@tonic-gate 		 * Make sure that each lwp has successfully aborted its
525*7c478bd9Sstevel@tonic-gate 		 * syscall and that the syscall gets restarted when we
526*7c478bd9Sstevel@tonic-gate 		 * detach later.
527*7c478bd9Sstevel@tonic-gate 		 */
528*7c478bd9Sstevel@tonic-gate 		if (Plwp_iter(P, lwp_restart, NULL) != 0)
529*7c478bd9Sstevel@tonic-gate 			goto err_no_agent;
530*7c478bd9Sstevel@tonic-gate 	}
531*7c478bd9Sstevel@tonic-gate 
532*7c478bd9Sstevel@tonic-gate 	(void) Psysexit(P, 0, 0);
533*7c478bd9Sstevel@tonic-gate 
534*7c478bd9Sstevel@tonic-gate 	if (Pcreate_agent(P) != 0) {
535*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: cannot control "
536*7c478bd9Sstevel@tonic-gate 		    "process %d\n"), psp->pr_pid);
537*7c478bd9Sstevel@tonic-gate 		goto err_no_agent;
538*7c478bd9Sstevel@tonic-gate 	}
539*7c478bd9Sstevel@tonic-gate 
540*7c478bd9Sstevel@tonic-gate 	/*
541*7c478bd9Sstevel@tonic-gate 	 * See if the victim has access to the nohup.out file we created.
542*7c478bd9Sstevel@tonic-gate 	 * If the user does something that would invalidate the result
543*7c478bd9Sstevel@tonic-gate 	 * of this call from here until the call to pr_open, the process
544*7c478bd9Sstevel@tonic-gate 	 * may be left in an inconsistent state -- we assume that the user
545*7c478bd9Sstevel@tonic-gate 	 * is not intentionally trying to shoot himself in the foot.
546*7c478bd9Sstevel@tonic-gate 	 */
547*7c478bd9Sstevel@tonic-gate 	if (pr_access(P, nout, R_OK | W_OK) != 0) {
548*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: process %d can not "
549*7c478bd9Sstevel@tonic-gate 		    "access %s: %s\n"), psp->pr_pid, nout, strerror(errno));
550*7c478bd9Sstevel@tonic-gate 		goto err_agent;
551*7c478bd9Sstevel@tonic-gate 	}
552*7c478bd9Sstevel@tonic-gate 
553*7c478bd9Sstevel@tonic-gate 	/*
554*7c478bd9Sstevel@tonic-gate 	 * Redirect output to the controlling tty to nohup.out and tty
555*7c478bd9Sstevel@tonic-gate 	 * input to read from /dev/null.
556*7c478bd9Sstevel@tonic-gate 	 */
557*7c478bd9Sstevel@tonic-gate 
558*7c478bd9Sstevel@tonic-gate 	g_wrfd = -1;
559*7c478bd9Sstevel@tonic-gate 	g_rdfd = -1;
560*7c478bd9Sstevel@tonic-gate 
561*7c478bd9Sstevel@tonic-gate 	(void) Pfd_iter(P, fd_cb, NULL);
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate 	Pdestroy_agent(P);
564*7c478bd9Sstevel@tonic-gate 	if (sig != 0)
565*7c478bd9Sstevel@tonic-gate 		(void) kill(psp->pr_pid, sig);
566*7c478bd9Sstevel@tonic-gate 
567*7c478bd9Sstevel@tonic-gate 	return (0);
568*7c478bd9Sstevel@tonic-gate 
569*7c478bd9Sstevel@tonic-gate err_agent:
570*7c478bd9Sstevel@tonic-gate 	Pdestroy_agent(P);
571*7c478bd9Sstevel@tonic-gate err_no_agent:
572*7c478bd9Sstevel@tonic-gate 	if (sig != 0)
573*7c478bd9Sstevel@tonic-gate 		(void) kill(psp->pr_pid, sig);
574*7c478bd9Sstevel@tonic-gate 	return (-1);
575*7c478bd9Sstevel@tonic-gate }
576*7c478bd9Sstevel@tonic-gate 
577*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
578*7c478bd9Sstevel@tonic-gate static void
intr(int sig)579*7c478bd9Sstevel@tonic-gate intr(int sig)
580*7c478bd9Sstevel@tonic-gate {
581*7c478bd9Sstevel@tonic-gate 	g_interrupt = 1;
582*7c478bd9Sstevel@tonic-gate }
583*7c478bd9Sstevel@tonic-gate 
584*7c478bd9Sstevel@tonic-gate static int
pnohup(int argc,char ** argv)585*7c478bd9Sstevel@tonic-gate pnohup(int argc, char **argv)
586*7c478bd9Sstevel@tonic-gate {
587*7c478bd9Sstevel@tonic-gate 	struct ps_prochandle *P;
588*7c478bd9Sstevel@tonic-gate 	int i, j;
589*7c478bd9Sstevel@tonic-gate 	int flag = 0;
590*7c478bd9Sstevel@tonic-gate 	int gcode;
591*7c478bd9Sstevel@tonic-gate 	int nh_fd = -1;
592*7c478bd9Sstevel@tonic-gate 	char *fname;
593*7c478bd9Sstevel@tonic-gate 	char *home;
594*7c478bd9Sstevel@tonic-gate 	int nerrs = 0;
595*7c478bd9Sstevel@tonic-gate 
596*7c478bd9Sstevel@tonic-gate 	/*
597*7c478bd9Sstevel@tonic-gate 	 * Catch signals from the terminal.
598*7c478bd9Sstevel@tonic-gate 	 */
599*7c478bd9Sstevel@tonic-gate 	if (sigset(SIGHUP, SIG_IGN) == SIG_DFL)
600*7c478bd9Sstevel@tonic-gate 		(void) sigset(SIGHUP, intr);
601*7c478bd9Sstevel@tonic-gate 	if (sigset(SIGINT, SIG_IGN) == SIG_DFL)
602*7c478bd9Sstevel@tonic-gate 		(void) sigset(SIGINT, intr);
603*7c478bd9Sstevel@tonic-gate 	if (sigset(SIGQUIT, SIG_IGN) == SIG_DFL)
604*7c478bd9Sstevel@tonic-gate 		(void) sigset(SIGQUIT, intr);
605*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGPIPE, intr);
606*7c478bd9Sstevel@tonic-gate 	(void) sigset(SIGTERM, intr);
607*7c478bd9Sstevel@tonic-gate 
608*7c478bd9Sstevel@tonic-gate 	if (opt_F)
609*7c478bd9Sstevel@tonic-gate 		flag |= PGRAB_FORCE;
610*7c478bd9Sstevel@tonic-gate 
611*7c478bd9Sstevel@tonic-gate 	/*
612*7c478bd9Sstevel@tonic-gate 	 * Set nout to be the full path name of nohup.out and fname to be
613*7c478bd9Sstevel@tonic-gate 	 * the simplified path name:
614*7c478bd9Sstevel@tonic-gate 	 * nout = /cwd/nohup.out	fname = nohup.out
615*7c478bd9Sstevel@tonic-gate 	 * nout = $HOME/nohup.out	fname = $HOME/nohup.out
616*7c478bd9Sstevel@tonic-gate 	 */
617*7c478bd9Sstevel@tonic-gate 	if (getcwd(nout, sizeof (nout) - strlen("/nohup.out") - 1) != NULL) {
618*7c478bd9Sstevel@tonic-gate 		fname = &nout[strlen(nout)];
619*7c478bd9Sstevel@tonic-gate 		(void) strcpy(fname, "/nohup.out");
620*7c478bd9Sstevel@tonic-gate 		fname++;
621*7c478bd9Sstevel@tonic-gate 
622*7c478bd9Sstevel@tonic-gate 		nh_fd = open(nout, O_WRONLY | O_CREAT, NOHUP_PERM);
623*7c478bd9Sstevel@tonic-gate 	}
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	if (nh_fd == -1 && (home = getenv("HOME")) != NULL) {
626*7c478bd9Sstevel@tonic-gate 		if (snprintf(nout, sizeof (nout),
627*7c478bd9Sstevel@tonic-gate 		    "%s/nohup.out", home) < sizeof (nout)) {
628*7c478bd9Sstevel@tonic-gate 			nh_fd = open(nout, O_WRONLY | O_CREAT, NOHUP_PERM);
629*7c478bd9Sstevel@tonic-gate 			fname = &nout[0];
630*7c478bd9Sstevel@tonic-gate 		}
631*7c478bd9Sstevel@tonic-gate 	}
632*7c478bd9Sstevel@tonic-gate 
633*7c478bd9Sstevel@tonic-gate 	if (nh_fd == -1) {
634*7c478bd9Sstevel@tonic-gate 		(void) fprintf(stderr, gettext("nohup: cannot open/create "
635*7c478bd9Sstevel@tonic-gate 		    "nohup.out: %s\n"), strerror(errno));
636*7c478bd9Sstevel@tonic-gate 
637*7c478bd9Sstevel@tonic-gate 		return (NOHUP_ERROR);
638*7c478bd9Sstevel@tonic-gate 	}
639*7c478bd9Sstevel@tonic-gate 
640*7c478bd9Sstevel@tonic-gate 	if (opt_g) {
641*7c478bd9Sstevel@tonic-gate 		pid_t *pgids;
642*7c478bd9Sstevel@tonic-gate 		int npgids;
643*7c478bd9Sstevel@tonic-gate 		int success;
644*7c478bd9Sstevel@tonic-gate 
645*7c478bd9Sstevel@tonic-gate 		/*
646*7c478bd9Sstevel@tonic-gate 		 * Make nohup its own process group leader so that we
647*7c478bd9Sstevel@tonic-gate 		 * don't accidently send SIGSTOP to this process.
648*7c478bd9Sstevel@tonic-gate 		 */
649*7c478bd9Sstevel@tonic-gate 		(void) setpgid(0, 0);
650*7c478bd9Sstevel@tonic-gate 
651*7c478bd9Sstevel@tonic-gate 		/*
652*7c478bd9Sstevel@tonic-gate 		 * If a list of process group ids is specified, we want to
653*7c478bd9Sstevel@tonic-gate 		 * first SIGSTOP the whole process group so that we can be
654*7c478bd9Sstevel@tonic-gate 		 * sure not to miss any processes that belong to the group
655*7c478bd9Sstevel@tonic-gate 		 * (it's harder to hit a moving target). We then iterate
656*7c478bd9Sstevel@tonic-gate 		 * over all the processes on the system looking for
657*7c478bd9Sstevel@tonic-gate 		 * members of the given process group to apply the
658*7c478bd9Sstevel@tonic-gate 		 * do_pnohup function to. If the process was stopped due
659*7c478bd9Sstevel@tonic-gate 		 * to our SIGSTOP, we send the process SIGCONT; if the
660*7c478bd9Sstevel@tonic-gate 		 * process was already stopped, we leave it alone.
661*7c478bd9Sstevel@tonic-gate 		 */
662*7c478bd9Sstevel@tonic-gate 		pgids = calloc(argc, sizeof (pid_t));
663*7c478bd9Sstevel@tonic-gate 		pgids[0] = getpid();
664*7c478bd9Sstevel@tonic-gate 		npgids = 1;
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc; i++) {
667*7c478bd9Sstevel@tonic-gate 			dirent_t *dent;
668*7c478bd9Sstevel@tonic-gate 			DIR *dirp;
669*7c478bd9Sstevel@tonic-gate 			psinfo_t psinfo;
670*7c478bd9Sstevel@tonic-gate 			const pstatus_t *psp;
671*7c478bd9Sstevel@tonic-gate 			pid_t pgid;
672*7c478bd9Sstevel@tonic-gate 			char *end;
673*7c478bd9Sstevel@tonic-gate 			hrtime_t kill_time, stop_time;
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate 			if (isdigit(*argv[i])) {
676*7c478bd9Sstevel@tonic-gate 				pgid = strtol(argv[i], &end, 10);
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 				/*
679*7c478bd9Sstevel@tonic-gate 				 * kill(2) with pid = 0 or -1 has a special
680*7c478bd9Sstevel@tonic-gate 				 * meaning, so don't let pgid be 0 or 1.
681*7c478bd9Sstevel@tonic-gate 				 */
682*7c478bd9Sstevel@tonic-gate 				if (*end == '\0' && pgid > 1)
683*7c478bd9Sstevel@tonic-gate 					goto pgid_ok;
684*7c478bd9Sstevel@tonic-gate 			}
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 			(void) fprintf(stderr, gettext("nohup: "
687*7c478bd9Sstevel@tonic-gate 			    "bad process group %s\n"), argv[i]);
688*7c478bd9Sstevel@tonic-gate 			nerrs++;
689*7c478bd9Sstevel@tonic-gate 			continue;
690*7c478bd9Sstevel@tonic-gate 
691*7c478bd9Sstevel@tonic-gate pgid_ok:
692*7c478bd9Sstevel@tonic-gate 			/*
693*7c478bd9Sstevel@tonic-gate 			 * We don't want to nohup a process group twice.
694*7c478bd9Sstevel@tonic-gate 			 */
695*7c478bd9Sstevel@tonic-gate 			for (j = 0; j < npgids; j++) {
696*7c478bd9Sstevel@tonic-gate 				if (pgids[j] == pgid)
697*7c478bd9Sstevel@tonic-gate 					break;
698*7c478bd9Sstevel@tonic-gate 			}
699*7c478bd9Sstevel@tonic-gate 
700*7c478bd9Sstevel@tonic-gate 			if (j != npgids)
701*7c478bd9Sstevel@tonic-gate 				continue;
702*7c478bd9Sstevel@tonic-gate 
703*7c478bd9Sstevel@tonic-gate 			pgids[npgids++] = pgid;
704*7c478bd9Sstevel@tonic-gate 
705*7c478bd9Sstevel@tonic-gate 			/*
706*7c478bd9Sstevel@tonic-gate 			 * Have the kernel stop all members of the process
707*7c478bd9Sstevel@tonic-gate 			 * group; record the time we stopped the process
708*7c478bd9Sstevel@tonic-gate 			 * group so that we can tell if a member stopped
709*7c478bd9Sstevel@tonic-gate 			 * because of this call to kill(2) or if it was
710*7c478bd9Sstevel@tonic-gate 			 * already stopped when we got here. If the user
711*7c478bd9Sstevel@tonic-gate 			 * job control stops the victim between the call
712*7c478bd9Sstevel@tonic-gate 			 * to gethrtime(2) and kill(2), we may send
713*7c478bd9Sstevel@tonic-gate 			 * SIGCONT when we really shouldn't -- we assume
714*7c478bd9Sstevel@tonic-gate 			 * that the user is not trying to shoot himself in
715*7c478bd9Sstevel@tonic-gate 			 * the foot.
716*7c478bd9Sstevel@tonic-gate 			 */
717*7c478bd9Sstevel@tonic-gate 			kill_time = gethrtime();
718*7c478bd9Sstevel@tonic-gate 			if (kill(-pgid, SIGSTOP) == -1) {
719*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr, gettext("nohup: cannot "
720*7c478bd9Sstevel@tonic-gate 				    "stop process group %d: %s\n"), pgid,
721*7c478bd9Sstevel@tonic-gate 				    errno != ESRCH ? strerror(errno) :
722*7c478bd9Sstevel@tonic-gate 				    gettext("No such process group"));
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate 				nerrs++;
725*7c478bd9Sstevel@tonic-gate 				continue;
726*7c478bd9Sstevel@tonic-gate 			}
727*7c478bd9Sstevel@tonic-gate 
728*7c478bd9Sstevel@tonic-gate 			dirp = opendir("/proc");
729*7c478bd9Sstevel@tonic-gate 			success = 0;
730*7c478bd9Sstevel@tonic-gate 			while ((dent = readdir(dirp)) != NULL && !g_interrupt) {
731*7c478bd9Sstevel@tonic-gate 				if (dent->d_name[0] == '.')
732*7c478bd9Sstevel@tonic-gate 					continue;
733*7c478bd9Sstevel@tonic-gate 
734*7c478bd9Sstevel@tonic-gate 				if (proc_arg_psinfo(dent->d_name,
735*7c478bd9Sstevel@tonic-gate 				    PR_ARG_PIDS, &psinfo, &gcode) == -1)
736*7c478bd9Sstevel@tonic-gate 					continue;
737*7c478bd9Sstevel@tonic-gate 
738*7c478bd9Sstevel@tonic-gate 				if (psinfo.pr_pgid != pgid)
739*7c478bd9Sstevel@tonic-gate 					continue;
740*7c478bd9Sstevel@tonic-gate 
741*7c478bd9Sstevel@tonic-gate 				/*
742*7c478bd9Sstevel@tonic-gate 				 * Ignore zombies.
743*7c478bd9Sstevel@tonic-gate 				 */
744*7c478bd9Sstevel@tonic-gate 				if (psinfo.pr_nlwp == 0)
745*7c478bd9Sstevel@tonic-gate 					continue;
746*7c478bd9Sstevel@tonic-gate 
747*7c478bd9Sstevel@tonic-gate 				if ((P = proc_arg_grab(dent->d_name,
748*7c478bd9Sstevel@tonic-gate 				    PR_ARG_PIDS, flag, &gcode)) == NULL) {
749*7c478bd9Sstevel@tonic-gate 					(void) fprintf(stderr, gettext("nohup: "
750*7c478bd9Sstevel@tonic-gate 					    "cannot examine %s: %s\n"),
751*7c478bd9Sstevel@tonic-gate 					    dent->d_name, Pgrab_error(gcode));
752*7c478bd9Sstevel@tonic-gate 
753*7c478bd9Sstevel@tonic-gate 					(void) kill(psinfo.pr_pid, SIGCONT);
754*7c478bd9Sstevel@tonic-gate 					continue;
755*7c478bd9Sstevel@tonic-gate 				}
756*7c478bd9Sstevel@tonic-gate 
757*7c478bd9Sstevel@tonic-gate 				/*
758*7c478bd9Sstevel@tonic-gate 				 * This implicitly restarts any process that
759*7c478bd9Sstevel@tonic-gate 				 * was stopped via job control any time after
760*7c478bd9Sstevel@tonic-gate 				 * the call to kill(2). This is the desired
761*7c478bd9Sstevel@tonic-gate 				 * behavior since nohup is busy trying to
762*7c478bd9Sstevel@tonic-gate 				 * disassociate a process from its controlling
763*7c478bd9Sstevel@tonic-gate 				 * terminal.
764*7c478bd9Sstevel@tonic-gate 				 */
765*7c478bd9Sstevel@tonic-gate 				psp = Pstatus(P);
766*7c478bd9Sstevel@tonic-gate 				if (psp->pr_lwp.pr_why == PR_JOBCONTROL) {
767*7c478bd9Sstevel@tonic-gate 					stop_time =
768*7c478bd9Sstevel@tonic-gate 					    psp->pr_lwp.pr_tstamp.tv_sec;
769*7c478bd9Sstevel@tonic-gate 					stop_time *= (hrtime_t)NANOSEC;
770*7c478bd9Sstevel@tonic-gate 					stop_time +=
771*7c478bd9Sstevel@tonic-gate 					    psp->pr_lwp.pr_tstamp.tv_nsec;
772*7c478bd9Sstevel@tonic-gate 				} else {
773*7c478bd9Sstevel@tonic-gate 					stop_time = 0;
774*7c478bd9Sstevel@tonic-gate 				}
775*7c478bd9Sstevel@tonic-gate 
776*7c478bd9Sstevel@tonic-gate 				if (do_pnohup(P) == 0)
777*7c478bd9Sstevel@tonic-gate 					success = 1;
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 				/*
780*7c478bd9Sstevel@tonic-gate 				 * If the process was stopped because of
781*7c478bd9Sstevel@tonic-gate 				 * our call to kill(2) (i.e. if it stopped
782*7c478bd9Sstevel@tonic-gate 				 * some time after kill_time) then restart
783*7c478bd9Sstevel@tonic-gate 				 * the process.
784*7c478bd9Sstevel@tonic-gate 				 */
785*7c478bd9Sstevel@tonic-gate 				if (kill_time <= stop_time)
786*7c478bd9Sstevel@tonic-gate 					(void) kill(psinfo.pr_pid, SIGCONT);
787*7c478bd9Sstevel@tonic-gate 
788*7c478bd9Sstevel@tonic-gate 				Prelease(P, 0);
789*7c478bd9Sstevel@tonic-gate 			}
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate 			/*
792*7c478bd9Sstevel@tonic-gate 			 * If we didn't successfully nohup any member of the
793*7c478bd9Sstevel@tonic-gate 			 * process group.
794*7c478bd9Sstevel@tonic-gate 			 */
795*7c478bd9Sstevel@tonic-gate 			if (!success)
796*7c478bd9Sstevel@tonic-gate 				nerrs++;
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 			(void) closedir(dirp);
799*7c478bd9Sstevel@tonic-gate 		}
800*7c478bd9Sstevel@tonic-gate 	} else {
801*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < argc && !g_interrupt; i++) {
802*7c478bd9Sstevel@tonic-gate 			if ((P = proc_arg_grab(argv[i], PR_ARG_PIDS, flag,
803*7c478bd9Sstevel@tonic-gate 			    &gcode)) == NULL) {
804*7c478bd9Sstevel@tonic-gate 				(void) fprintf(stderr,
805*7c478bd9Sstevel@tonic-gate 				    gettext("nohup: cannot examine %s: %s\n"),
806*7c478bd9Sstevel@tonic-gate 				    argv[i], Pgrab_error(gcode));
807*7c478bd9Sstevel@tonic-gate 
808*7c478bd9Sstevel@tonic-gate 				nerrs++;
809*7c478bd9Sstevel@tonic-gate 				continue;
810*7c478bd9Sstevel@tonic-gate 			}
811*7c478bd9Sstevel@tonic-gate 
812*7c478bd9Sstevel@tonic-gate 			if (do_pnohup(P) != 0)
813*7c478bd9Sstevel@tonic-gate 				nerrs++;
814*7c478bd9Sstevel@tonic-gate 
815*7c478bd9Sstevel@tonic-gate 			Prelease(P, 0);
816*7c478bd9Sstevel@tonic-gate 		}
817*7c478bd9Sstevel@tonic-gate 	}
818*7c478bd9Sstevel@tonic-gate 
819*7c478bd9Sstevel@tonic-gate 	(void) close(nh_fd);
820*7c478bd9Sstevel@tonic-gate 
821*7c478bd9Sstevel@tonic-gate 	if (argc == nerrs)
822*7c478bd9Sstevel@tonic-gate 		return (NOHUP_ERROR);
823*7c478bd9Sstevel@tonic-gate 
824*7c478bd9Sstevel@tonic-gate 	(void) fprintf(stderr, gettext("Sending output to %s\n"), fname);
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 	return (0);
827*7c478bd9Sstevel@tonic-gate }
828*7c478bd9Sstevel@tonic-gate 
829*7c478bd9Sstevel@tonic-gate #endif /* !XPG4 */
830