xref: /illumos-gate/usr/src/cmd/sh/jobs.c (revision 24da5b34)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
539e7390aSna  * Common Development and Distribution License (the "License").
639e7390aSna  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
2139e7390aSna 
227c478bd9Sstevel@tonic-gate /*
23*24da5b34Srie  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24965005c8Schin  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
2639e7390aSna 
27965005c8Schin /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
28965005c8Schin /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
317c478bd9Sstevel@tonic-gate /*
327c478bd9Sstevel@tonic-gate  * Job control for UNIX Shell
337c478bd9Sstevel@tonic-gate  */
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include	<sys/termio.h>
367c478bd9Sstevel@tonic-gate #include	<sys/types.h>
377c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
387c478bd9Sstevel@tonic-gate #include	<sys/param.h>
397c478bd9Sstevel@tonic-gate #include	<fcntl.h>
407c478bd9Sstevel@tonic-gate #include	<errno.h>
417c478bd9Sstevel@tonic-gate #include	"defs.h"
427c478bd9Sstevel@tonic-gate 
437c478bd9Sstevel@tonic-gate /*
447c478bd9Sstevel@tonic-gate  * one of these for each active job
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate struct job
487c478bd9Sstevel@tonic-gate {
497c478bd9Sstevel@tonic-gate 	struct job *j_nxtp;	/* next job in job ID order */
507c478bd9Sstevel@tonic-gate 	struct job *j_curp;	/* next job in job currency order */
517c478bd9Sstevel@tonic-gate 	struct termios j_stty;	/* termio save area when job stops */
52*24da5b34Srie 	pid_t	j_pid;		/* job leader's process ID */
53*24da5b34Srie 	pid_t	j_pgid;		/* job's process group ID */
54*24da5b34Srie 	pid_t	j_tgid;		/* job's foreground process group ID */
55*24da5b34Srie 	uint_t	j_jid;		/* job ID */
56*24da5b34Srie 	ushort_t j_xval;	/* exit code, or exit or stop signal */
57*24da5b34Srie 	ushort_t j_flag;	/* various status flags defined below */
58*24da5b34Srie 	char	*j_pwd;		/* job's working directory */
59*24da5b34Srie 	char	*j_cmd;		/* cmd used to invoke this job */
607c478bd9Sstevel@tonic-gate };
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /* defines for j_flag */
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #define	J_DUMPED	0001	/* job has core dumped */
657c478bd9Sstevel@tonic-gate #define	J_NOTIFY	0002	/* job has changed status */
667c478bd9Sstevel@tonic-gate #define	J_SAVETTY	0004	/* job was stopped in foreground, and its */
677c478bd9Sstevel@tonic-gate 				/*   termio settings were saved */
687c478bd9Sstevel@tonic-gate #define	J_STOPPED	0010	/* job has been stopped */
697c478bd9Sstevel@tonic-gate #define	J_SIGNALED	0020	/* job has received signal; j_xval has it */
707c478bd9Sstevel@tonic-gate #define	J_DONE		0040	/* job has finished */
717c478bd9Sstevel@tonic-gate #define	J_RUNNING	0100	/* job is currently running */
727c478bd9Sstevel@tonic-gate #define	J_FOREGND	0200	/* job was put in foreground by shell */
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate /* options to the printjob() function defined below */
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #define	PR_CUR		00001	/* print job currency ('+', '-', or ' ') */
777c478bd9Sstevel@tonic-gate #define	PR_JID		00002	/* print job ID				 */
787c478bd9Sstevel@tonic-gate #define	PR_PGID		00004	/* print job's process group ID		 */
797c478bd9Sstevel@tonic-gate #define	PR_STAT		00010	/* print status obtained from wait	 */
807c478bd9Sstevel@tonic-gate #define	PR_CMD		00020	/* print cmd that invoked job		 */
817c478bd9Sstevel@tonic-gate #define	PR_AMP		00040	/* print a '&' if in the background	 */
827c478bd9Sstevel@tonic-gate #define	PR_PWD		00100	/* print jobs present working directory	 */
837c478bd9Sstevel@tonic-gate 
847c478bd9Sstevel@tonic-gate #define	PR_DFL		(PR_CUR|PR_JID|PR_STAT|PR_CMD) /* default options */
857c478bd9Sstevel@tonic-gate #define	PR_LONG		(PR_DFL|PR_PGID|PR_PWD)	/* long options */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static struct termios 	mystty;	 /* default termio settings		 */
887c478bd9Sstevel@tonic-gate static int 		eofflg,
897c478bd9Sstevel@tonic-gate 			jobcnt,	 /* number of active jobs		 */
907c478bd9Sstevel@tonic-gate 			jobdone, /* number of active but finished jobs	 */
917c478bd9Sstevel@tonic-gate 			jobnote; /* jobs requiring notification		 */
927c478bd9Sstevel@tonic-gate static pid_t		svpgid,	 /* saved process group ID		 */
937c478bd9Sstevel@tonic-gate 			svtgid;	 /* saved foreground process group ID	 */
947c478bd9Sstevel@tonic-gate static struct job 	*jobcur, /* active jobs listed in currency order */
957c478bd9Sstevel@tonic-gate 			**nextjob,
967c478bd9Sstevel@tonic-gate 			*thisjob,
977c478bd9Sstevel@tonic-gate 			*joblst; /* active jobs listed in job ID order	 */
987c478bd9Sstevel@tonic-gate 
99965005c8Schin static void printjob(struct job *, int);
100965005c8Schin 
1017c478bd9Sstevel@tonic-gate static struct job *
102965005c8Schin pgid2job(pid_t pgid)
1037c478bd9Sstevel@tonic-gate {
104965005c8Schin 	struct job *jp;
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate 	for (jp = joblst; jp != 0 && jp->j_pid != pgid; jp = jp->j_nxtp)
1077c478bd9Sstevel@tonic-gate 		continue;
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate 	return (jp);
1107c478bd9Sstevel@tonic-gate }
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate static struct job *
113965005c8Schin str2job(char *cmd, char *job, int mustbejob)
1147c478bd9Sstevel@tonic-gate {
115965005c8Schin 	struct job *jp, *njp;
116965005c8Schin 	int i;
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate 	if (*job != '%')
1197c478bd9Sstevel@tonic-gate 		jp = pgid2job(stoi(job));
1207c478bd9Sstevel@tonic-gate 	else if (*++job == 0 || *job == '+' || *job == '%' || *job == '-') {
1217c478bd9Sstevel@tonic-gate 		jp = jobcur;
1227c478bd9Sstevel@tonic-gate 		if (*job == '-' && jp)
1237c478bd9Sstevel@tonic-gate 			jp = jp->j_curp;
1247c478bd9Sstevel@tonic-gate 	} else if (*job >= '0' && *job <= '9') {
1257c478bd9Sstevel@tonic-gate 		i = stoi(job);
1267c478bd9Sstevel@tonic-gate 		for (jp = joblst; jp && jp->j_jid != i; jp = jp->j_nxtp)
1277c478bd9Sstevel@tonic-gate 			continue;
1287c478bd9Sstevel@tonic-gate 	} else if (*job == '?') {
129965005c8Schin 		int j;
130965005c8Schin 		char *p;
1317c478bd9Sstevel@tonic-gate 		i = strlen(++job);
1327c478bd9Sstevel@tonic-gate 		jp = 0;
1337c478bd9Sstevel@tonic-gate 		for (njp = jobcur; njp; njp = njp->j_curp) {
1347c478bd9Sstevel@tonic-gate 			if (njp->j_jid == 0)
1357c478bd9Sstevel@tonic-gate 				continue;
1367c478bd9Sstevel@tonic-gate 			for (p = njp->j_cmd, j = strlen(p); j >= i; p++, j--) {
1377c478bd9Sstevel@tonic-gate 				if (strncmp(job, p, i) == 0) {
1387c478bd9Sstevel@tonic-gate 					if (jp != 0)
139965005c8Schin 						failed((unsigned char *)cmd,
140965005c8Schin 						    ambiguous);
1417c478bd9Sstevel@tonic-gate 					jp = njp;
1427c478bd9Sstevel@tonic-gate 					break;
1437c478bd9Sstevel@tonic-gate 				}
1447c478bd9Sstevel@tonic-gate 			}
1457c478bd9Sstevel@tonic-gate 		}
1467c478bd9Sstevel@tonic-gate 	} else {
1477c478bd9Sstevel@tonic-gate 		i = strlen(job);
1487c478bd9Sstevel@tonic-gate 		jp = 0;
1497c478bd9Sstevel@tonic-gate 		for (njp = jobcur; njp; njp = njp->j_curp) {
1507c478bd9Sstevel@tonic-gate 			if (njp->j_jid == 0)
1517c478bd9Sstevel@tonic-gate 				continue;
1527c478bd9Sstevel@tonic-gate 			if (strncmp(job, njp->j_cmd, i) == 0) {
1537c478bd9Sstevel@tonic-gate 				if (jp != 0)
154965005c8Schin 					failed((unsigned char *)cmd, ambiguous);
1557c478bd9Sstevel@tonic-gate 				jp = njp;
1567c478bd9Sstevel@tonic-gate 			}
1577c478bd9Sstevel@tonic-gate 		}
1587c478bd9Sstevel@tonic-gate 	}
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate 	if (mustbejob && (jp == 0 || jp->j_jid == 0))
161965005c8Schin 		failed((unsigned char *)cmd, nosuchjob);
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	return (jp);
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate 
1667c478bd9Sstevel@tonic-gate static void
167965005c8Schin freejob(struct job *jp)
1687c478bd9Sstevel@tonic-gate {
169965005c8Schin 	struct job **njp;
170965005c8Schin 	struct job **cjp;
1717c478bd9Sstevel@tonic-gate 
1727c478bd9Sstevel@tonic-gate 	for (njp = &joblst; *njp != jp; njp = &(*njp)->j_nxtp)
1737c478bd9Sstevel@tonic-gate 		continue;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	for (cjp = &jobcur; *cjp != jp; cjp = &(*cjp)->j_curp)
1767c478bd9Sstevel@tonic-gate 		continue;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate 	*njp = jp->j_nxtp;
1797c478bd9Sstevel@tonic-gate 	*cjp = jp->j_curp;
1807c478bd9Sstevel@tonic-gate 	free(jp);
1817c478bd9Sstevel@tonic-gate 	jobcnt--;
1827c478bd9Sstevel@tonic-gate 	jobdone--;
1837c478bd9Sstevel@tonic-gate }
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate /*
1867c478bd9Sstevel@tonic-gate  * Collect the foreground job.
1877c478bd9Sstevel@tonic-gate  * Used in the case where the subshell wants
1887c478bd9Sstevel@tonic-gate  * to exit, but needs to wait until the fg job
1897c478bd9Sstevel@tonic-gate  * is done.
1907c478bd9Sstevel@tonic-gate  */
191965005c8Schin void
192965005c8Schin collect_fg_job(void)
1937c478bd9Sstevel@tonic-gate {
194965005c8Schin 	struct job *jp;
195965005c8Schin 	pid_t pid;
1967c478bd9Sstevel@tonic-gate 	int stat;
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate 	for (jp = joblst; jp; jp = jp->j_nxtp)
1997c478bd9Sstevel@tonic-gate 		if (jp->j_flag & J_FOREGND)
2007c478bd9Sstevel@tonic-gate 			break;
2017c478bd9Sstevel@tonic-gate 
2027c478bd9Sstevel@tonic-gate 	if (!jp)
2037c478bd9Sstevel@tonic-gate 		/* no foreground job */
2047c478bd9Sstevel@tonic-gate 		return;
2057c478bd9Sstevel@tonic-gate 
2067c478bd9Sstevel@tonic-gate 	/*
2077c478bd9Sstevel@tonic-gate 	 * Wait on fg job until wait succeeds
2087c478bd9Sstevel@tonic-gate 	 * or it fails due to no waitable children.
2097c478bd9Sstevel@tonic-gate 	 */
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 	while (1) {
2127c478bd9Sstevel@tonic-gate 		errno = 0;
2137c478bd9Sstevel@tonic-gate 		pid = waitpid(jp->j_pid, &stat, 0);
2147c478bd9Sstevel@tonic-gate 		if (pid == jp->j_pid || (pid == -1 && errno == ECHILD))
2157c478bd9Sstevel@tonic-gate 			break;
2167c478bd9Sstevel@tonic-gate 	}
2177c478bd9Sstevel@tonic-gate }
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate /*
2207c478bd9Sstevel@tonic-gate  * analyze the status of a job
2217c478bd9Sstevel@tonic-gate  */
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate static int
224965005c8Schin statjob(struct job *jp, int stat, int fg, int rc)
2257c478bd9Sstevel@tonic-gate {
2267c478bd9Sstevel@tonic-gate 	pid_t tgid;
2277c478bd9Sstevel@tonic-gate 	int done = 0;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	if (WIFCONTINUED(stat)) {
2307c478bd9Sstevel@tonic-gate 		if (jp->j_flag & J_STOPPED) {
2317c478bd9Sstevel@tonic-gate 			jp->j_flag &= ~(J_STOPPED|J_SIGNALED|J_SAVETTY);
2327c478bd9Sstevel@tonic-gate 			jp->j_flag |= J_RUNNING;
2337c478bd9Sstevel@tonic-gate 			if (!fg && jp->j_jid) {
2347c478bd9Sstevel@tonic-gate 				jp->j_flag |= J_NOTIFY;
2357c478bd9Sstevel@tonic-gate 				jobnote++;
2367c478bd9Sstevel@tonic-gate 			}
2377c478bd9Sstevel@tonic-gate 		}
2387c478bd9Sstevel@tonic-gate 	} else if (WIFSTOPPED(stat)) {
2397c478bd9Sstevel@tonic-gate 		jp->j_xval = WSTOPSIG(stat);
2407c478bd9Sstevel@tonic-gate 		jp->j_flag &= ~J_RUNNING;
2417c478bd9Sstevel@tonic-gate 		jp->j_flag |= (J_SIGNALED|J_STOPPED);
2427c478bd9Sstevel@tonic-gate 		jp->j_pgid = getpgid(jp->j_pid);
2437c478bd9Sstevel@tonic-gate 		jp->j_tgid = jp->j_pgid;
2447c478bd9Sstevel@tonic-gate 		if (fg) {
2457c478bd9Sstevel@tonic-gate 			if (tgid = settgid(mypgid, jp->j_pgid))
2467c478bd9Sstevel@tonic-gate 				jp->j_tgid = tgid;
2477c478bd9Sstevel@tonic-gate 			else {
2487c478bd9Sstevel@tonic-gate 				jp->j_flag |= J_SAVETTY;
2497c478bd9Sstevel@tonic-gate 				tcgetattr(0, &jp->j_stty);
2507c478bd9Sstevel@tonic-gate 				(void) tcsetattr(0, TCSANOW, &mystty);
2517c478bd9Sstevel@tonic-gate 			}
2527c478bd9Sstevel@tonic-gate 		}
2537c478bd9Sstevel@tonic-gate 		if (jp->j_jid) {
2547c478bd9Sstevel@tonic-gate 			jp->j_flag |= J_NOTIFY;
2557c478bd9Sstevel@tonic-gate 			jobnote++;
2567c478bd9Sstevel@tonic-gate 		}
2577c478bd9Sstevel@tonic-gate 	} else {
2587c478bd9Sstevel@tonic-gate 		jp->j_flag &= ~J_RUNNING;
2597c478bd9Sstevel@tonic-gate 		jp->j_flag |= J_DONE;
2607c478bd9Sstevel@tonic-gate 		done++;
2617c478bd9Sstevel@tonic-gate 		jobdone++;
2627c478bd9Sstevel@tonic-gate 		if (WIFSIGNALED(stat)) {
2637c478bd9Sstevel@tonic-gate 			jp->j_xval = WTERMSIG(stat);
2647c478bd9Sstevel@tonic-gate 			jp->j_flag |= J_SIGNALED;
2657c478bd9Sstevel@tonic-gate 			if (WCOREDUMP(stat))
2667c478bd9Sstevel@tonic-gate 				jp->j_flag |= J_DUMPED;
2677c478bd9Sstevel@tonic-gate 			if (!fg || jp->j_xval != SIGINT) {
2687c478bd9Sstevel@tonic-gate 				jp->j_flag |= J_NOTIFY;
2697c478bd9Sstevel@tonic-gate 				jobnote++;
2707c478bd9Sstevel@tonic-gate 			}
2717c478bd9Sstevel@tonic-gate 		} else { /* WIFEXITED */
2727c478bd9Sstevel@tonic-gate 			jp->j_xval = WEXITSTATUS(stat);
2737c478bd9Sstevel@tonic-gate 			jp->j_flag &= ~J_SIGNALED;
2747c478bd9Sstevel@tonic-gate 			if (!fg && jp->j_jid) {
2757c478bd9Sstevel@tonic-gate 				jp->j_flag |= J_NOTIFY;
2767c478bd9Sstevel@tonic-gate 				jobnote++;
2777c478bd9Sstevel@tonic-gate 			}
2787c478bd9Sstevel@tonic-gate 		}
2797c478bd9Sstevel@tonic-gate 		if (fg) {
2807c478bd9Sstevel@tonic-gate 			if (!settgid(mypgid, jp->j_pgid) ||
2817c478bd9Sstevel@tonic-gate 			    !settgid(mypgid, getpgid(jp->j_pid)))
2827c478bd9Sstevel@tonic-gate 				tcgetattr(0, &mystty);
2837c478bd9Sstevel@tonic-gate 		}
2847c478bd9Sstevel@tonic-gate 	}
2857c478bd9Sstevel@tonic-gate 	if (rc) {
2867c478bd9Sstevel@tonic-gate 		exitval = jp->j_xval;
2877c478bd9Sstevel@tonic-gate 		if (jp->j_flag & J_SIGNALED)
2887c478bd9Sstevel@tonic-gate 			exitval |= SIGFLG;
2897c478bd9Sstevel@tonic-gate 		exitset();
2907c478bd9Sstevel@tonic-gate 	}
2917c478bd9Sstevel@tonic-gate 	if (done && !(jp->j_flag & J_NOTIFY))
2927c478bd9Sstevel@tonic-gate 		freejob(jp);
2937c478bd9Sstevel@tonic-gate 	return (done);
2947c478bd9Sstevel@tonic-gate }
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate /*
2977c478bd9Sstevel@tonic-gate  * collect the status of jobs that have recently exited or stopped -
2987c478bd9Sstevel@tonic-gate  * if wnohang == WNOHANG, wait until error, or all jobs are accounted for;
2997c478bd9Sstevel@tonic-gate  *
3007c478bd9Sstevel@tonic-gate  * called after each command is executed, with wnohang == 0, and as part
3017c478bd9Sstevel@tonic-gate  * of "wait" builtin with wnohang == WNOHANG
3027c478bd9Sstevel@tonic-gate  *
3037c478bd9Sstevel@tonic-gate  * We do not need to call chktrap here if waitpid(2) is called with
3047c478bd9Sstevel@tonic-gate  * wnohang == 0, because that only happens from syswait() which is called
3057c478bd9Sstevel@tonic-gate  * from builtin() where chktrap() is already called.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate 
3087c478bd9Sstevel@tonic-gate static void
3097c478bd9Sstevel@tonic-gate collectjobs(wnohang)
3107c478bd9Sstevel@tonic-gate {
3117c478bd9Sstevel@tonic-gate 	pid_t pid;
312965005c8Schin 	struct job *jp;
3137c478bd9Sstevel@tonic-gate 	int stat, n;
3147c478bd9Sstevel@tonic-gate 	int wflags;
3157c478bd9Sstevel@tonic-gate 
3167c478bd9Sstevel@tonic-gate 	if ((flags & (monitorflg|jcflg|jcoff)) == (monitorflg|jcflg))
3177c478bd9Sstevel@tonic-gate 		wflags = WUNTRACED|WCONTINUED;
3187c478bd9Sstevel@tonic-gate 	else
3197c478bd9Sstevel@tonic-gate 		wflags = 0;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	for (n = jobcnt - jobdone; n > 0; n--) {
3227c478bd9Sstevel@tonic-gate 		if ((pid = waitpid(-1, &stat, wnohang|wflags)) <= 0)
3237c478bd9Sstevel@tonic-gate 			break;
3247c478bd9Sstevel@tonic-gate 		if (jp = pgid2job(pid))
3257c478bd9Sstevel@tonic-gate 			(void) statjob(jp, stat, 0, 0);
3267c478bd9Sstevel@tonic-gate 	}
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate }
3297c478bd9Sstevel@tonic-gate 
3307c478bd9Sstevel@tonic-gate void
3317c478bd9Sstevel@tonic-gate freejobs()
3327c478bd9Sstevel@tonic-gate {
333965005c8Schin 	struct job *jp;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	collectjobs(WNOHANG);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if (jobnote) {
338965005c8Schin 		int savefd = setb(2);
3397c478bd9Sstevel@tonic-gate 		for (jp = joblst; jp; jp = jp->j_nxtp) {
3407c478bd9Sstevel@tonic-gate 			if (jp->j_flag & J_NOTIFY) {
3417c478bd9Sstevel@tonic-gate 				if (jp->j_jid)
3427c478bd9Sstevel@tonic-gate 					printjob(jp, PR_DFL);
3437c478bd9Sstevel@tonic-gate 				else if (jp->j_flag & J_FOREGND)
3447c478bd9Sstevel@tonic-gate 					printjob(jp, PR_STAT);
3457c478bd9Sstevel@tonic-gate 				else
3467c478bd9Sstevel@tonic-gate 					printjob(jp, PR_STAT|PR_PGID);
3477c478bd9Sstevel@tonic-gate 			}
3487c478bd9Sstevel@tonic-gate 		}
3497c478bd9Sstevel@tonic-gate 		(void) setb(savefd);
3507c478bd9Sstevel@tonic-gate 	}
3517c478bd9Sstevel@tonic-gate 
3527c478bd9Sstevel@tonic-gate 	if (jobdone) {
3537c478bd9Sstevel@tonic-gate 		for (jp = joblst; jp; jp = jp->j_nxtp) {
3547c478bd9Sstevel@tonic-gate 			if (jp->j_flag & J_DONE)
3557c478bd9Sstevel@tonic-gate 				freejob(jp);
3567c478bd9Sstevel@tonic-gate 		}
3577c478bd9Sstevel@tonic-gate 	}
3587c478bd9Sstevel@tonic-gate }
3597c478bd9Sstevel@tonic-gate 
3607c478bd9Sstevel@tonic-gate static void
361965005c8Schin waitjob(struct job *jp)
3627c478bd9Sstevel@tonic-gate {
3637c478bd9Sstevel@tonic-gate 	int stat;
3647c478bd9Sstevel@tonic-gate 	int done;
3657c478bd9Sstevel@tonic-gate 	pid_t pid = jp->j_pid;
3667c478bd9Sstevel@tonic-gate 	int wflags;
3677c478bd9Sstevel@tonic-gate 	int ret = 0;
3687c478bd9Sstevel@tonic-gate 	int	err = 0;
3697c478bd9Sstevel@tonic-gate 
3707c478bd9Sstevel@tonic-gate 	if ((flags & (monitorflg|jcflg|jcoff)) == (monitorflg|jcflg))
3717c478bd9Sstevel@tonic-gate 		wflags = WUNTRACED;
3727c478bd9Sstevel@tonic-gate 	else
3737c478bd9Sstevel@tonic-gate 		wflags = 0;
3747c478bd9Sstevel@tonic-gate 	do {
3757c478bd9Sstevel@tonic-gate 		errno = 0;
3767c478bd9Sstevel@tonic-gate 		ret = waitpid(pid, &stat, wflags|WNOWAIT);
3777c478bd9Sstevel@tonic-gate 		err = errno;
3787c478bd9Sstevel@tonic-gate 		if (ret == -1 && err == ECHILD) {
3797c478bd9Sstevel@tonic-gate 			stat = 0;
3807c478bd9Sstevel@tonic-gate 			break;
3817c478bd9Sstevel@tonic-gate 		}
3827c478bd9Sstevel@tonic-gate 	} while (ret != pid);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	done = statjob(jp, stat, 1, 1);
3857c478bd9Sstevel@tonic-gate 	waitpid(pid, 0, wflags);
3867c478bd9Sstevel@tonic-gate 	if (done && exitval && (flags & errflg))
3877c478bd9Sstevel@tonic-gate 		exitsh(exitval);
3887c478bd9Sstevel@tonic-gate 	flags |= eflag;
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * modify the foreground process group to *new* only if the
3937c478bd9Sstevel@tonic-gate  * current foreground process group is equal to *expected*
3947c478bd9Sstevel@tonic-gate  */
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate int
3977c478bd9Sstevel@tonic-gate settgid(new, expected)
3987c478bd9Sstevel@tonic-gate pid_t new, expected;
3997c478bd9Sstevel@tonic-gate {
400965005c8Schin 	pid_t current = tcgetpgrp(0);
4017c478bd9Sstevel@tonic-gate 
4027c478bd9Sstevel@tonic-gate 	if (current != expected)
4037c478bd9Sstevel@tonic-gate 		return (current);
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate 	if (new != current)
4067c478bd9Sstevel@tonic-gate 		tcsetpgrp(0, new);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	return (0);
4097c478bd9Sstevel@tonic-gate }
4107c478bd9Sstevel@tonic-gate 
4117c478bd9Sstevel@tonic-gate static void
412965005c8Schin restartjob(struct job *jp, int fg)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	if (jp != jobcur) {
415965005c8Schin 		struct job *t;
4167c478bd9Sstevel@tonic-gate 		for (t = jobcur; t->j_curp != jp; t = t->j_curp);
4177c478bd9Sstevel@tonic-gate 		t->j_curp = jp->j_curp;
4187c478bd9Sstevel@tonic-gate 		jp->j_curp = jobcur;
4197c478bd9Sstevel@tonic-gate 		jobcur = jp;
4207c478bd9Sstevel@tonic-gate 	}
4217c478bd9Sstevel@tonic-gate 	if (fg) {
4227c478bd9Sstevel@tonic-gate 		if (jp->j_flag & J_SAVETTY) {
4237c478bd9Sstevel@tonic-gate 			jp->j_stty.c_lflag &= ~TOSTOP;
4247c478bd9Sstevel@tonic-gate 			jp->j_stty.c_lflag |= (mystty.c_lflag&TOSTOP);
4257c478bd9Sstevel@tonic-gate 			jp->j_stty.c_cc[VSUSP] = mystty.c_cc[VSUSP];
4267c478bd9Sstevel@tonic-gate 			jp->j_stty.c_cc[VDSUSP] = mystty.c_cc[VDSUSP];
4277c478bd9Sstevel@tonic-gate 			(void) tcsetattr(0, TCSADRAIN, &jp->j_stty);
4287c478bd9Sstevel@tonic-gate 		}
4297c478bd9Sstevel@tonic-gate 		(void) settgid(jp->j_tgid, mypgid);
4307c478bd9Sstevel@tonic-gate 	}
4317c478bd9Sstevel@tonic-gate 	(void) kill(-(jp->j_pgid), SIGCONT);
4327c478bd9Sstevel@tonic-gate 	if (jp->j_tgid != jp->j_pgid)
4337c478bd9Sstevel@tonic-gate 		(void) kill(-(jp->j_tgid), SIGCONT);
4347c478bd9Sstevel@tonic-gate 	jp->j_flag &= ~(J_STOPPED|J_SIGNALED|J_SAVETTY);
4357c478bd9Sstevel@tonic-gate 	jp->j_flag |= J_RUNNING;
4367c478bd9Sstevel@tonic-gate 	if (fg)  {
4377c478bd9Sstevel@tonic-gate 		jp->j_flag |= J_FOREGND;
4387c478bd9Sstevel@tonic-gate 		printjob(jp, PR_JID|PR_CMD);
4397c478bd9Sstevel@tonic-gate 		waitjob(jp);
4407c478bd9Sstevel@tonic-gate 	} else {
4417c478bd9Sstevel@tonic-gate 		jp->j_flag &= ~J_FOREGND;
4427c478bd9Sstevel@tonic-gate 		printjob(jp, PR_JID|PR_CMD|PR_AMP);
4437c478bd9Sstevel@tonic-gate 	}
4447c478bd9Sstevel@tonic-gate }
4457c478bd9Sstevel@tonic-gate 
446965005c8Schin static void
447965005c8Schin printjob(struct job *jp, int propts)
4487c478bd9Sstevel@tonic-gate {
4497c478bd9Sstevel@tonic-gate 	int sp = 0;
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	if (jp->j_flag & J_NOTIFY) {
4527c478bd9Sstevel@tonic-gate 		jobnote--;
4537c478bd9Sstevel@tonic-gate 		jp->j_flag &= ~J_NOTIFY;
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 
4567c478bd9Sstevel@tonic-gate 	if (propts & PR_JID) {
4577c478bd9Sstevel@tonic-gate 		prc_buff('[');
4587c478bd9Sstevel@tonic-gate 		prn_buff(jp->j_jid);
4597c478bd9Sstevel@tonic-gate 		prc_buff(']');
4607c478bd9Sstevel@tonic-gate 		sp = 1;
4617c478bd9Sstevel@tonic-gate 	}
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate 	if (propts & PR_CUR) {
4647c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
4657c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
4667c478bd9Sstevel@tonic-gate 		sp = 1;
4677c478bd9Sstevel@tonic-gate 		if (jobcur == jp)
4687c478bd9Sstevel@tonic-gate 			prc_buff('+');
4697c478bd9Sstevel@tonic-gate 		else if (jobcur != 0 && jobcur->j_curp == jp)
4707c478bd9Sstevel@tonic-gate 			prc_buff('-');
4717c478bd9Sstevel@tonic-gate 		else
4727c478bd9Sstevel@tonic-gate 			sp++;
4737c478bd9Sstevel@tonic-gate 	}
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	if (propts & PR_PGID) {
4767c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
4777c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
4787c478bd9Sstevel@tonic-gate 		prn_buff(jp->j_pid);
4797c478bd9Sstevel@tonic-gate 		sp = 1;
4807c478bd9Sstevel@tonic-gate 	}
4817c478bd9Sstevel@tonic-gate 
4827c478bd9Sstevel@tonic-gate 	if (propts & PR_STAT) {
48339e7390aSna 		char	*gmsg;
4847c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
4857c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
4867c478bd9Sstevel@tonic-gate 		sp = 28;
4877c478bd9Sstevel@tonic-gate 		if (jp->j_flag & J_SIGNALED) {
4887c478bd9Sstevel@tonic-gate 			char *sigstr, *strsignal();
4897c478bd9Sstevel@tonic-gate 			if ((sigstr = strsignal(jp->j_xval)) != NULL) {
4907c478bd9Sstevel@tonic-gate 				sp -= strlen(sigstr);
4917c478bd9Sstevel@tonic-gate 				prs_buff(sigstr);
4927c478bd9Sstevel@tonic-gate 			} else {
4937c478bd9Sstevel@tonic-gate 				itos(jp->j_xval);
49439e7390aSna 				gmsg = gettext(signalnum);
49539e7390aSna 				sp -= strlen(numbuf) + strlen(gmsg);
49639e7390aSna 				prs_buff((unsigned char *)gmsg);
4977c478bd9Sstevel@tonic-gate 				prs_buff(numbuf);
4987c478bd9Sstevel@tonic-gate 			}
4997c478bd9Sstevel@tonic-gate 			if (jp->j_flag & J_DUMPED) {
50039e7390aSna 				gmsg = gettext(coredump);
50139e7390aSna 				sp -= strlen(gmsg);
50239e7390aSna 				prs_buff((unsigned char *)gmsg);
5037c478bd9Sstevel@tonic-gate 			}
5047c478bd9Sstevel@tonic-gate 		} else if (jp->j_flag & J_DONE) {
5057c478bd9Sstevel@tonic-gate 			itos(jp->j_xval);
50639e7390aSna 			gmsg = gettext(exited);
50739e7390aSna 			sp -= strlen(gmsg) + strlen(numbuf) + 2;
50839e7390aSna 			prs_buff((unsigned char *)gmsg);
5097c478bd9Sstevel@tonic-gate 			prc_buff('(');
5107c478bd9Sstevel@tonic-gate 			itos(jp->j_xval);
5117c478bd9Sstevel@tonic-gate 			prs_buff(numbuf);
5127c478bd9Sstevel@tonic-gate 			prc_buff(')');
5137c478bd9Sstevel@tonic-gate 		} else {
51439e7390aSna 			gmsg = gettext(running);
51539e7390aSna 			sp -= strlen(gmsg);
51639e7390aSna 			prs_buff((unsigned char *)gmsg);
5177c478bd9Sstevel@tonic-gate 		}
5187c478bd9Sstevel@tonic-gate 		if (sp < 1)
5197c478bd9Sstevel@tonic-gate 			sp = 1;
5207c478bd9Sstevel@tonic-gate 	}
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate 	if (propts & PR_CMD) {
5237c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
5247c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
5257c478bd9Sstevel@tonic-gate 		prs_buff(jp->j_cmd);
5267c478bd9Sstevel@tonic-gate 		sp = 1;
5277c478bd9Sstevel@tonic-gate 	}
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 	if (propts & PR_AMP) {
5307c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
5317c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
5327c478bd9Sstevel@tonic-gate 		prc_buff('&');
5337c478bd9Sstevel@tonic-gate 		sp = 1;
5347c478bd9Sstevel@tonic-gate 	}
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	if (propts & PR_PWD) {
5377c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
5387c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
5397c478bd9Sstevel@tonic-gate 		prs_buff("(wd: ");
5407c478bd9Sstevel@tonic-gate 		prs_buff(jp->j_pwd);
5417c478bd9Sstevel@tonic-gate 		prc_buff(')');
5427c478bd9Sstevel@tonic-gate 	}
5437c478bd9Sstevel@tonic-gate 
5447c478bd9Sstevel@tonic-gate 	prc_buff(NL);
5457c478bd9Sstevel@tonic-gate 	flushb();
5467c478bd9Sstevel@tonic-gate 
5477c478bd9Sstevel@tonic-gate }
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate  * called to initialize job control for each new input file to the shell,
5527c478bd9Sstevel@tonic-gate  * and after the "exec" builtin
5537c478bd9Sstevel@tonic-gate  */
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate void
5567c478bd9Sstevel@tonic-gate startjobs()
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	svpgid = mypgid;
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 	if (tcgetattr(0, &mystty) == -1 || (svtgid = tcgetpgrp(0)) == -1) {
5617c478bd9Sstevel@tonic-gate 		flags &= ~jcflg;
5627c478bd9Sstevel@tonic-gate 		return;
5637c478bd9Sstevel@tonic-gate 	}
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	flags |= jcflg;
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	handle(SIGTTOU, SIG_IGN);
5687c478bd9Sstevel@tonic-gate 	handle(SIGTSTP, SIG_DFL);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	if (mysid != mypgid) {
5717c478bd9Sstevel@tonic-gate 		setpgid(0, 0);
5727c478bd9Sstevel@tonic-gate 		mypgid = mypid;
5737c478bd9Sstevel@tonic-gate 		(void) settgid(mypgid, svpgid);
5747c478bd9Sstevel@tonic-gate 	}
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate }
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate int
5797c478bd9Sstevel@tonic-gate endjobs(check_if)
5807c478bd9Sstevel@tonic-gate int check_if;
5817c478bd9Sstevel@tonic-gate {
5827c478bd9Sstevel@tonic-gate 	if ((flags & (jcoff|jcflg)) != jcflg)
5837c478bd9Sstevel@tonic-gate 		return (1);
5847c478bd9Sstevel@tonic-gate 
5857c478bd9Sstevel@tonic-gate 	if (check_if && jobcnt && eofflg++ == 0) {
586965005c8Schin 		struct job *jp;
5877c478bd9Sstevel@tonic-gate 		if (check_if & JOB_STOPPED) {
5887c478bd9Sstevel@tonic-gate 			for (jp = joblst; jp; jp = jp->j_nxtp) {
5897c478bd9Sstevel@tonic-gate 				if (jp->j_jid && (jp->j_flag & J_STOPPED)) {
59039e7390aSna 					prs(_gettext(jobsstopped));
5917c478bd9Sstevel@tonic-gate 					prc(NL);
5927c478bd9Sstevel@tonic-gate 					return (0);
5937c478bd9Sstevel@tonic-gate 				}
5947c478bd9Sstevel@tonic-gate 			}
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 		if (check_if & JOB_RUNNING) {
5977c478bd9Sstevel@tonic-gate 			for (jp = joblst; jp; jp = jp->j_nxtp) {
5987c478bd9Sstevel@tonic-gate 				if (jp->j_jid && (jp->j_flag & J_RUNNING)) {
59939e7390aSna 					prs(_gettext(jobsrunning));
6007c478bd9Sstevel@tonic-gate 					prc(NL);
6017c478bd9Sstevel@tonic-gate 					return (0);
6027c478bd9Sstevel@tonic-gate 				}
6037c478bd9Sstevel@tonic-gate 			}
6047c478bd9Sstevel@tonic-gate 		}
6057c478bd9Sstevel@tonic-gate 	}
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 	if (svpgid != mypgid) {
6087c478bd9Sstevel@tonic-gate 		(void) settgid(svtgid, mypgid);
6097c478bd9Sstevel@tonic-gate 		setpgid(0, svpgid);
6107c478bd9Sstevel@tonic-gate 	}
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate 	return (1);
6137c478bd9Sstevel@tonic-gate }
6147c478bd9Sstevel@tonic-gate 
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * called by the shell to reserve a job slot for a job about to be spawned
6187c478bd9Sstevel@tonic-gate  */
6197c478bd9Sstevel@tonic-gate 
6207c478bd9Sstevel@tonic-gate void
6217c478bd9Sstevel@tonic-gate deallocjob()
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	free(thisjob);
6247c478bd9Sstevel@tonic-gate 	jobcnt--;
6257c478bd9Sstevel@tonic-gate }
6267c478bd9Sstevel@tonic-gate 
627965005c8Schin void
628*24da5b34Srie allocjob(char *cmd, uchar_t *cwd, int monitor)
6297c478bd9Sstevel@tonic-gate {
630965005c8Schin 	struct job *jp, **jpp;
631965005c8Schin 	int jid, cmdlen, cwdlen;
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	cmdlen = strlen(cmd) + 1;
6347c478bd9Sstevel@tonic-gate 	if (cmd[cmdlen-2] == '&') {
6357c478bd9Sstevel@tonic-gate 		cmd[cmdlen-3] = 0;
6367c478bd9Sstevel@tonic-gate 		cmdlen -= 2;
6377c478bd9Sstevel@tonic-gate 	}
6387c478bd9Sstevel@tonic-gate 	cwdlen = strlen(cwd) + 1;
6397c478bd9Sstevel@tonic-gate 	jp = (struct job *) alloc(sizeof (struct job) + cmdlen + cwdlen);
6407c478bd9Sstevel@tonic-gate 	if (jp == 0)
6417c478bd9Sstevel@tonic-gate 		error(nostack);
6427c478bd9Sstevel@tonic-gate 	jobcnt++;
6437c478bd9Sstevel@tonic-gate 	jp->j_cmd = ((char *)jp) + sizeof (struct job);
6447c478bd9Sstevel@tonic-gate 	strcpy(jp->j_cmd, cmd);
6457c478bd9Sstevel@tonic-gate 	jp->j_pwd = jp->j_cmd + cmdlen;
6467c478bd9Sstevel@tonic-gate 	strcpy(jp->j_pwd, cwd);
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 	jpp = &joblst;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	if (monitor) {
6517c478bd9Sstevel@tonic-gate 		for (; *jpp; jpp = &(*jpp)->j_nxtp)
6527c478bd9Sstevel@tonic-gate 			if ((*jpp)->j_jid != 0)
6537c478bd9Sstevel@tonic-gate 				break;
6547c478bd9Sstevel@tonic-gate 		for (jid = 1; *jpp; jpp = &(*jpp)->j_nxtp, jid++)
6557c478bd9Sstevel@tonic-gate 			if ((*jpp)->j_jid != jid)
6567c478bd9Sstevel@tonic-gate 				break;
6577c478bd9Sstevel@tonic-gate 	} else
6587c478bd9Sstevel@tonic-gate 		jid = 0;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	jp->j_jid = jid;
6617c478bd9Sstevel@tonic-gate 	nextjob = jpp;
6627c478bd9Sstevel@tonic-gate 	thisjob = jp;
6637c478bd9Sstevel@tonic-gate }
6647c478bd9Sstevel@tonic-gate 
665965005c8Schin void
666965005c8Schin clearjobs(void)
6677c478bd9Sstevel@tonic-gate {
668965005c8Schin 	struct job *jp, *sjp;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	for (jp = joblst; jp; jp = sjp) {
6717c478bd9Sstevel@tonic-gate 		sjp = jp->j_nxtp;
6727c478bd9Sstevel@tonic-gate 		free(jp);
6737c478bd9Sstevel@tonic-gate 	}
6747c478bd9Sstevel@tonic-gate 	joblst = NULL;
6757c478bd9Sstevel@tonic-gate 	jobcnt = 0;
6767c478bd9Sstevel@tonic-gate 	jobnote = 0;
6777c478bd9Sstevel@tonic-gate 	jobdone = 0;
6787c478bd9Sstevel@tonic-gate 
6797c478bd9Sstevel@tonic-gate }
6807c478bd9Sstevel@tonic-gate 
681965005c8Schin void
682965005c8Schin makejob(int monitor, int fg)
6837c478bd9Sstevel@tonic-gate {
6847c478bd9Sstevel@tonic-gate 	if (monitor) {
6857c478bd9Sstevel@tonic-gate 		mypgid = mypid;
6867c478bd9Sstevel@tonic-gate 		setpgid(0, 0);
6877c478bd9Sstevel@tonic-gate 		if (fg)
6887c478bd9Sstevel@tonic-gate 			tcsetpgrp(0, mypid);
6897c478bd9Sstevel@tonic-gate 		handle(SIGTTOU, SIG_DFL);
6907c478bd9Sstevel@tonic-gate 		handle(SIGTSTP, SIG_DFL);
6917c478bd9Sstevel@tonic-gate 	} else if (!fg) {
6927c478bd9Sstevel@tonic-gate #ifdef NICE
6937c478bd9Sstevel@tonic-gate 		nice(NICE);
6947c478bd9Sstevel@tonic-gate #endif
6957c478bd9Sstevel@tonic-gate 		handle(SIGTTIN, SIG_IGN);
6967c478bd9Sstevel@tonic-gate 		handle(SIGINT,  SIG_IGN);
6977c478bd9Sstevel@tonic-gate 		handle(SIGQUIT, SIG_IGN);
6987c478bd9Sstevel@tonic-gate 		if (!ioset)
6997c478bd9Sstevel@tonic-gate 			renamef(chkopen(devnull, 0), 0);
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate }
7027c478bd9Sstevel@tonic-gate 
7037c478bd9Sstevel@tonic-gate /*
7047c478bd9Sstevel@tonic-gate  * called by the shell after job has been spawned, to fill in the
7057c478bd9Sstevel@tonic-gate  * job slot, and wait for the job if in the foreground
7067c478bd9Sstevel@tonic-gate  */
7077c478bd9Sstevel@tonic-gate 
7087c478bd9Sstevel@tonic-gate void
7097c478bd9Sstevel@tonic-gate postjob(pid, fg)
7107c478bd9Sstevel@tonic-gate pid_t pid;
7117c478bd9Sstevel@tonic-gate int fg;
7127c478bd9Sstevel@tonic-gate {
7137c478bd9Sstevel@tonic-gate 
714965005c8Schin 	int propts;
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	thisjob->j_nxtp = *nextjob;
7177c478bd9Sstevel@tonic-gate 	*nextjob = thisjob;
7187c478bd9Sstevel@tonic-gate 	thisjob->j_curp = jobcur;
7197c478bd9Sstevel@tonic-gate 	jobcur = thisjob;
7207c478bd9Sstevel@tonic-gate 
7217c478bd9Sstevel@tonic-gate 	if (thisjob->j_jid) {
7227c478bd9Sstevel@tonic-gate 		thisjob->j_pgid = pid;
7237c478bd9Sstevel@tonic-gate 		propts = PR_JID|PR_PGID;
7247c478bd9Sstevel@tonic-gate 	} else {
7257c478bd9Sstevel@tonic-gate 		thisjob->j_pgid = mypgid;
7267c478bd9Sstevel@tonic-gate 		propts = PR_PGID;
7277c478bd9Sstevel@tonic-gate 	}
7287c478bd9Sstevel@tonic-gate 
7297c478bd9Sstevel@tonic-gate 	thisjob->j_flag = J_RUNNING;
7307c478bd9Sstevel@tonic-gate 	thisjob->j_tgid = thisjob->j_pgid;
7317c478bd9Sstevel@tonic-gate 	thisjob->j_pid = pid;
7327c478bd9Sstevel@tonic-gate 	eofflg = 0;
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	if (fg) {
7357c478bd9Sstevel@tonic-gate 		thisjob->j_flag |= J_FOREGND;
7367c478bd9Sstevel@tonic-gate 		waitjob(thisjob);
7377c478bd9Sstevel@tonic-gate 	} else  {
7387c478bd9Sstevel@tonic-gate 		if (flags & ttyflg)
7397c478bd9Sstevel@tonic-gate 			printjob(thisjob, propts);
7407c478bd9Sstevel@tonic-gate 		assnum(&pcsadr, (long)pid);
7417c478bd9Sstevel@tonic-gate 	}
7427c478bd9Sstevel@tonic-gate }
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate /*
7457c478bd9Sstevel@tonic-gate  * the builtin "jobs" command
7467c478bd9Sstevel@tonic-gate  */
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate void
7497c478bd9Sstevel@tonic-gate sysjobs(argc, argv)
7507c478bd9Sstevel@tonic-gate int argc;
7517c478bd9Sstevel@tonic-gate char *argv[];
7527c478bd9Sstevel@tonic-gate {
753965005c8Schin 	char *cmd = *argv;
754965005c8Schin 	struct job *jp;
755965005c8Schin 	int propts, c;
7567c478bd9Sstevel@tonic-gate 	extern int opterr, i;
7577c478bd9Sstevel@tonic-gate 	int savoptind = optind;
7587c478bd9Sstevel@tonic-gate 	int loptind = -1;
7597c478bd9Sstevel@tonic-gate 	int savopterr = opterr;
7607c478bd9Sstevel@tonic-gate 	int savsp = _sp;
7617c478bd9Sstevel@tonic-gate 	char *savoptarg = optarg;
7627c478bd9Sstevel@tonic-gate 	optind = 1;
7637c478bd9Sstevel@tonic-gate 	opterr = 0;
7647c478bd9Sstevel@tonic-gate 	_sp = 1;
7657c478bd9Sstevel@tonic-gate 	propts = 0;
7667c478bd9Sstevel@tonic-gate 
7677c478bd9Sstevel@tonic-gate 	if ((flags & jcflg) == 0)
768965005c8Schin 		failed((unsigned char *)cmd, nojc);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "lpx")) != -1) {
7717c478bd9Sstevel@tonic-gate 		if (propts) {
77239e7390aSna 			gfailure(usage, jobsuse);
7737c478bd9Sstevel@tonic-gate 			goto err;
7747c478bd9Sstevel@tonic-gate 		}
7757c478bd9Sstevel@tonic-gate 		switch (c) {
7767c478bd9Sstevel@tonic-gate 			case 'x':
7777c478bd9Sstevel@tonic-gate 				propts = -1;
7787c478bd9Sstevel@tonic-gate 				break;
7797c478bd9Sstevel@tonic-gate 			case 'p':
7807c478bd9Sstevel@tonic-gate 				propts = PR_PGID;
7817c478bd9Sstevel@tonic-gate 				break;
7827c478bd9Sstevel@tonic-gate 			case 'l':
7837c478bd9Sstevel@tonic-gate 				propts = PR_LONG;
7847c478bd9Sstevel@tonic-gate 				break;
7857c478bd9Sstevel@tonic-gate 			case '?':
78639e7390aSna 				gfailure(usage, jobsuse);
7877c478bd9Sstevel@tonic-gate 				goto err;
7887c478bd9Sstevel@tonic-gate 		}
7897c478bd9Sstevel@tonic-gate 	}
7907c478bd9Sstevel@tonic-gate 
7917c478bd9Sstevel@tonic-gate 	loptind = optind;
7927c478bd9Sstevel@tonic-gate err:
7937c478bd9Sstevel@tonic-gate 	optind = savoptind;
7947c478bd9Sstevel@tonic-gate 	optarg = savoptarg;
7957c478bd9Sstevel@tonic-gate 	opterr = savopterr;
7967c478bd9Sstevel@tonic-gate 	_sp = savsp;
7977c478bd9Sstevel@tonic-gate 	if (loptind == -1)
7987c478bd9Sstevel@tonic-gate 		return;
7997c478bd9Sstevel@tonic-gate 
8007c478bd9Sstevel@tonic-gate 	if (propts == -1) {
801965005c8Schin 		unsigned char *bp;
802965005c8Schin 		char *cp;
8037c478bd9Sstevel@tonic-gate 		unsigned char *savebp;
8047c478bd9Sstevel@tonic-gate 		for (savebp = bp = locstak(); loptind < argc; loptind++) {
8057c478bd9Sstevel@tonic-gate 			cp = argv[loptind];
8067c478bd9Sstevel@tonic-gate 			if (*cp == '%') {
8077c478bd9Sstevel@tonic-gate 				jp = str2job(cmd, cp, 1);
8087c478bd9Sstevel@tonic-gate 				itos(jp->j_pid);
8097c478bd9Sstevel@tonic-gate 				cp = (char *)numbuf;
8107c478bd9Sstevel@tonic-gate 			}
8117c478bd9Sstevel@tonic-gate 			while (*cp) {
8127c478bd9Sstevel@tonic-gate 				if (bp >= brkend)
8137c478bd9Sstevel@tonic-gate 					growstak(bp);
8147c478bd9Sstevel@tonic-gate 				*bp++ = *cp++;
8157c478bd9Sstevel@tonic-gate 			}
8167c478bd9Sstevel@tonic-gate 			if (bp >= brkend)
8177c478bd9Sstevel@tonic-gate 				growstak(bp);
8187c478bd9Sstevel@tonic-gate 			*bp++ = SPACE;
8197c478bd9Sstevel@tonic-gate 		}
8207c478bd9Sstevel@tonic-gate 		endstak(bp);
8217c478bd9Sstevel@tonic-gate 		execexp(savebp, 0);
8227c478bd9Sstevel@tonic-gate 		return;
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	collectjobs(WNOHANG);
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	if (propts == 0)
8287c478bd9Sstevel@tonic-gate 		propts = PR_DFL;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	if (loptind == argc) {
8317c478bd9Sstevel@tonic-gate 		for (jp = joblst; jp; jp = jp->j_nxtp) {
8327c478bd9Sstevel@tonic-gate 			if (jp->j_jid)
8337c478bd9Sstevel@tonic-gate 				printjob(jp, propts);
8347c478bd9Sstevel@tonic-gate 		}
8357c478bd9Sstevel@tonic-gate 	} else do
8367c478bd9Sstevel@tonic-gate 		printjob(str2job(cmd, argv[loptind++], 1), propts);
8377c478bd9Sstevel@tonic-gate 	while (loptind < argc);
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate }
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate /*
8427c478bd9Sstevel@tonic-gate  * the builtin "fg" and "bg" commands
8437c478bd9Sstevel@tonic-gate  */
844965005c8Schin void
845965005c8Schin sysfgbg(int argc, char *argv[])
8467c478bd9Sstevel@tonic-gate {
847965005c8Schin 	char *cmd = *argv;
848965005c8Schin 	int fg;
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate 	if ((flags & jcflg) == 0)
851965005c8Schin 		failed((unsigned char *)cmd, nojc);
8527c478bd9Sstevel@tonic-gate 
8537c478bd9Sstevel@tonic-gate 	fg = eq("fg", cmd);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	if (*++argv == 0) {
8567c478bd9Sstevel@tonic-gate 		struct job *jp;
8577c478bd9Sstevel@tonic-gate 		for (jp = jobcur; ; jp = jp->j_curp) {
8587c478bd9Sstevel@tonic-gate 			if (jp == 0)
859965005c8Schin 				failed((unsigned char *)cmd, nocurjob);
8607c478bd9Sstevel@tonic-gate 			if (jp->j_jid)
8617c478bd9Sstevel@tonic-gate 				break;
8627c478bd9Sstevel@tonic-gate 		}
8637c478bd9Sstevel@tonic-gate 		restartjob(jp, fg);
8647c478bd9Sstevel@tonic-gate 	}
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate 	else do
8677c478bd9Sstevel@tonic-gate 		restartjob(str2job(cmd, *argv, 1), fg);
8687c478bd9Sstevel@tonic-gate 	while (*++argv);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate }
8717c478bd9Sstevel@tonic-gate 
8727c478bd9Sstevel@tonic-gate /*
8737c478bd9Sstevel@tonic-gate  * the builtin "wait" commands
8747c478bd9Sstevel@tonic-gate  */
8757c478bd9Sstevel@tonic-gate 
8767c478bd9Sstevel@tonic-gate void
8777c478bd9Sstevel@tonic-gate syswait(argc, argv)
8787c478bd9Sstevel@tonic-gate int argc;
8797c478bd9Sstevel@tonic-gate char *argv[];
8807c478bd9Sstevel@tonic-gate {
881965005c8Schin 	char *cmd = *argv;
882965005c8Schin 	struct job *jp;
8837c478bd9Sstevel@tonic-gate 	int stat;
8847c478bd9Sstevel@tonic-gate 	int wflags;
8857c478bd9Sstevel@tonic-gate 
8867c478bd9Sstevel@tonic-gate 	if ((flags & (monitorflg|jcflg|jcoff)) == (monitorflg|jcflg))
8877c478bd9Sstevel@tonic-gate 		wflags = WUNTRACED;
8887c478bd9Sstevel@tonic-gate 	else
8897c478bd9Sstevel@tonic-gate 		wflags = 0;
8907c478bd9Sstevel@tonic-gate 
8917c478bd9Sstevel@tonic-gate 	if (argc == 1)
8927c478bd9Sstevel@tonic-gate 		collectjobs(0);
8937c478bd9Sstevel@tonic-gate 	else while (--argc) {
8947c478bd9Sstevel@tonic-gate 		if ((jp = str2job(cmd, *++argv, 0)) == 0)
8957c478bd9Sstevel@tonic-gate 			continue;
8967c478bd9Sstevel@tonic-gate 		if (!(jp->j_flag & J_RUNNING))
8977c478bd9Sstevel@tonic-gate 			continue;
8987c478bd9Sstevel@tonic-gate 		if (waitpid(jp->j_pid, &stat, wflags) <= 0)
8997c478bd9Sstevel@tonic-gate 			break;
9007c478bd9Sstevel@tonic-gate 		(void) statjob(jp, stat, 0, 1);
9017c478bd9Sstevel@tonic-gate 	}
9027c478bd9Sstevel@tonic-gate }
9037c478bd9Sstevel@tonic-gate 
904965005c8Schin static void
905965005c8Schin sigv(char *cmd, int sig, char *args)
9067c478bd9Sstevel@tonic-gate {
9077c478bd9Sstevel@tonic-gate 	int pgrp = 0;
9087c478bd9Sstevel@tonic-gate 	int stopme = 0;
9097c478bd9Sstevel@tonic-gate 	pid_t id;
9107c478bd9Sstevel@tonic-gate 
9117c478bd9Sstevel@tonic-gate 	if (*args == '%') {
912965005c8Schin 		struct job *jp;
9137c478bd9Sstevel@tonic-gate 		jp = str2job(cmd, args, 1);
9147c478bd9Sstevel@tonic-gate 		id = jp->j_pgid;
9157c478bd9Sstevel@tonic-gate 		pgrp++;
9167c478bd9Sstevel@tonic-gate 	} else {
9177c478bd9Sstevel@tonic-gate 		if (*args == '-') {
9187c478bd9Sstevel@tonic-gate 			pgrp++;
9197c478bd9Sstevel@tonic-gate 			args++;
9207c478bd9Sstevel@tonic-gate 		}
9217c478bd9Sstevel@tonic-gate 		id = 0;
9227c478bd9Sstevel@tonic-gate 		do {
9237c478bd9Sstevel@tonic-gate 			if (*args < '0' || *args > '9') {
9247c478bd9Sstevel@tonic-gate 				failure(cmd, badid);
9257c478bd9Sstevel@tonic-gate 				return;
9267c478bd9Sstevel@tonic-gate 			}
9277c478bd9Sstevel@tonic-gate 			id = (id * 10) + (*args - '0');
9287c478bd9Sstevel@tonic-gate 		} while (*++args);
9297c478bd9Sstevel@tonic-gate 		if (id == 0) {
9307c478bd9Sstevel@tonic-gate 			id = mypgid;
9317c478bd9Sstevel@tonic-gate 			pgrp++;
9327c478bd9Sstevel@tonic-gate 		}
9337c478bd9Sstevel@tonic-gate 	}
9347c478bd9Sstevel@tonic-gate 
9357c478bd9Sstevel@tonic-gate 	if (sig == SIGSTOP) {
9367c478bd9Sstevel@tonic-gate 		if (id == mysid || id == mypid && mypgid == mysid) {
9377c478bd9Sstevel@tonic-gate 			failure(cmd, loginsh);
9387c478bd9Sstevel@tonic-gate 			return;
9397c478bd9Sstevel@tonic-gate 		}
9407c478bd9Sstevel@tonic-gate 		if (id == mypgid && mypgid != svpgid) {
9417c478bd9Sstevel@tonic-gate 			(void) settgid(svtgid, mypgid);
9427c478bd9Sstevel@tonic-gate 			setpgid(0, svpgid);
9437c478bd9Sstevel@tonic-gate 			stopme++;
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	if (pgrp)
9487c478bd9Sstevel@tonic-gate 		id = -id;
9497c478bd9Sstevel@tonic-gate 
9507c478bd9Sstevel@tonic-gate 	if (kill(id, sig) < 0) {
9517c478bd9Sstevel@tonic-gate 
9527c478bd9Sstevel@tonic-gate 		switch (errno) {
9537c478bd9Sstevel@tonic-gate 			case EPERM:
9547c478bd9Sstevel@tonic-gate 				failure(cmd, eacces);
9557c478bd9Sstevel@tonic-gate 				break;
9567c478bd9Sstevel@tonic-gate 
9577c478bd9Sstevel@tonic-gate 			case EINVAL:
9587c478bd9Sstevel@tonic-gate 				failure(cmd, badsig);
9597c478bd9Sstevel@tonic-gate 				break;
9607c478bd9Sstevel@tonic-gate 
9617c478bd9Sstevel@tonic-gate 			default:
9627c478bd9Sstevel@tonic-gate 				if (pgrp)
9637c478bd9Sstevel@tonic-gate 					failure(cmd, nosuchpgid);
9647c478bd9Sstevel@tonic-gate 				else
9657c478bd9Sstevel@tonic-gate 					failure(cmd, nosuchpid);
9667c478bd9Sstevel@tonic-gate 				break;
9677c478bd9Sstevel@tonic-gate 		}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	} else if (sig == SIGTERM && pgrp)
9707c478bd9Sstevel@tonic-gate 		(void) kill(id, SIGCONT);
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 	if (stopme) {
9737c478bd9Sstevel@tonic-gate 		setpgid(0, mypgid);
9747c478bd9Sstevel@tonic-gate 		(void) settgid(mypgid, svpgid);
9757c478bd9Sstevel@tonic-gate 	}
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate }
9787c478bd9Sstevel@tonic-gate 
979965005c8Schin void
980965005c8Schin sysstop(int argc, char *argv[])
9817c478bd9Sstevel@tonic-gate {
9827c478bd9Sstevel@tonic-gate 	char *cmd = *argv;
98339e7390aSna 	if (argc <= 1) {
98439e7390aSna 		gfailure(usage, stopuse);
98539e7390aSna 		return;
98639e7390aSna 	}
9877c478bd9Sstevel@tonic-gate 	while (*++argv)
9887c478bd9Sstevel@tonic-gate 		sigv(cmd, SIGSTOP, *argv);
9897c478bd9Sstevel@tonic-gate }
9907c478bd9Sstevel@tonic-gate 
991965005c8Schin void
992965005c8Schin syskill(int argc, char *argv[])
9937c478bd9Sstevel@tonic-gate {
9947c478bd9Sstevel@tonic-gate 	char *cmd = *argv;
9957c478bd9Sstevel@tonic-gate 	int sig = SIGTERM;
9967c478bd9Sstevel@tonic-gate 
9977c478bd9Sstevel@tonic-gate 	if (argc == 1) {
99839e7390aSna 		gfailure(usage, killuse);
9997c478bd9Sstevel@tonic-gate 		return;
10007c478bd9Sstevel@tonic-gate 	}
10017c478bd9Sstevel@tonic-gate 
10027c478bd9Sstevel@tonic-gate 	if (argv[1][0] == '-') {
10037c478bd9Sstevel@tonic-gate 
10047c478bd9Sstevel@tonic-gate 		if (argc == 2) {
10057c478bd9Sstevel@tonic-gate 
1006965005c8Schin 			int i;
1007965005c8Schin 			int cnt = 0;
1008965005c8Schin 			char sep = 0;
10097c478bd9Sstevel@tonic-gate 			char buf[12];
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 			if (!eq(argv[1], "-l")) {
101239e7390aSna 				gfailure(usage, killuse);
10137c478bd9Sstevel@tonic-gate 				return;
10147c478bd9Sstevel@tonic-gate 			}
10157c478bd9Sstevel@tonic-gate 
10167c478bd9Sstevel@tonic-gate 			for (i = 1; i < MAXTRAP; i++) {
10177c478bd9Sstevel@tonic-gate 				if (sig2str(i, buf) < 0)
10187c478bd9Sstevel@tonic-gate 					continue;
10197c478bd9Sstevel@tonic-gate 				if (sep)
10207c478bd9Sstevel@tonic-gate 					prc_buff(sep);
10217c478bd9Sstevel@tonic-gate 				prs_buff(buf);
10227c478bd9Sstevel@tonic-gate 				if ((flags & ttyflg) && (++cnt % 10))
10237c478bd9Sstevel@tonic-gate 					sep = TAB;
10247c478bd9Sstevel@tonic-gate 				else
10257c478bd9Sstevel@tonic-gate 					sep = NL;
10267c478bd9Sstevel@tonic-gate 			}
10277c478bd9Sstevel@tonic-gate 			prc_buff(NL);
10287c478bd9Sstevel@tonic-gate 			return;
10297c478bd9Sstevel@tonic-gate 		}
10307c478bd9Sstevel@tonic-gate 
10317c478bd9Sstevel@tonic-gate 		if (str2sig(&argv[1][1], &sig)) {
10327c478bd9Sstevel@tonic-gate 			failure(cmd, badsig);
10337c478bd9Sstevel@tonic-gate 			return;
10347c478bd9Sstevel@tonic-gate 		}
10357c478bd9Sstevel@tonic-gate 		argv++;
10367c478bd9Sstevel@tonic-gate 	}
10377c478bd9Sstevel@tonic-gate 
10387c478bd9Sstevel@tonic-gate 	while (*++argv)
10397c478bd9Sstevel@tonic-gate 		sigv(cmd, sig, *argv);
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate }
10427c478bd9Sstevel@tonic-gate 
1043965005c8Schin void
1044965005c8Schin syssusp(int argc, char *argv[])
10457c478bd9Sstevel@tonic-gate {
10467c478bd9Sstevel@tonic-gate 	if (argc != 1)
1047965005c8Schin 		failed((unsigned char *)argv[0], badopt);
10487c478bd9Sstevel@tonic-gate 	sigv(argv[0], SIGSTOP, "0");
10497c478bd9Sstevel@tonic-gate }
1050