xref: /illumos-gate/usr/src/cmd/sh/jobs.c (revision 05845d98)
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*05845d98SArindam Sarkar  * Copyright 2009 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 /*
317c478bd9Sstevel@tonic-gate  * Job control for UNIX Shell
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	<sys/termio.h>
357c478bd9Sstevel@tonic-gate #include	<sys/types.h>
367c478bd9Sstevel@tonic-gate #include	<sys/wait.h>
377c478bd9Sstevel@tonic-gate #include	<sys/param.h>
387c478bd9Sstevel@tonic-gate #include	<fcntl.h>
397c478bd9Sstevel@tonic-gate #include	<errno.h>
40*05845d98SArindam Sarkar #include	<signal.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 */
5224da5b34Srie 	pid_t	j_pid;		/* job leader's process ID */
5324da5b34Srie 	pid_t	j_pgid;		/* job's process group ID */
5424da5b34Srie 	pid_t	j_tgid;		/* job's foreground process group ID */
5524da5b34Srie 	uint_t	j_jid;		/* job ID */
5624da5b34Srie 	ushort_t j_xval;	/* exit code, or exit or stop signal */
5724da5b34Srie 	ushort_t j_flag;	/* various status flags defined below */
5824da5b34Srie 	char	*j_pwd;		/* job's working directory */
5924da5b34Srie 	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 *
pgid2job(pid_t pgid)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 *
str2job(char * cmd,char * job,int mustbejob)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
freejob(struct job * jp)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
collect_fg_job(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
statjob(struct job * jp,int stat,int fg,int rc)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
collectjobs(wnohang)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
freejobs()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
waitjob(struct job * jp)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
settgid(new,expected)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
restartjob(struct job * jp,int fg)412965005c8Schin restartjob(struct job *jp, int fg)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	if (jp != jobcur) {
415965005c8Schin 		struct job *t;
416*05845d98SArindam Sarkar 
417*05845d98SArindam Sarkar 		for (t = jobcur; t->j_curp != jp; t = t->j_curp)
418*05845d98SArindam Sarkar 			;
4197c478bd9Sstevel@tonic-gate 		t->j_curp = jp->j_curp;
4207c478bd9Sstevel@tonic-gate 		jp->j_curp = jobcur;
4217c478bd9Sstevel@tonic-gate 		jobcur = jp;
4227c478bd9Sstevel@tonic-gate 	}
4237c478bd9Sstevel@tonic-gate 	if (fg) {
4247c478bd9Sstevel@tonic-gate 		if (jp->j_flag & J_SAVETTY) {
4257c478bd9Sstevel@tonic-gate 			jp->j_stty.c_lflag &= ~TOSTOP;
4267c478bd9Sstevel@tonic-gate 			jp->j_stty.c_lflag |= (mystty.c_lflag&TOSTOP);
4277c478bd9Sstevel@tonic-gate 			jp->j_stty.c_cc[VSUSP] = mystty.c_cc[VSUSP];
4287c478bd9Sstevel@tonic-gate 			jp->j_stty.c_cc[VDSUSP] = mystty.c_cc[VDSUSP];
4297c478bd9Sstevel@tonic-gate 			(void) tcsetattr(0, TCSADRAIN, &jp->j_stty);
4307c478bd9Sstevel@tonic-gate 		}
4317c478bd9Sstevel@tonic-gate 		(void) settgid(jp->j_tgid, mypgid);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	(void) kill(-(jp->j_pgid), SIGCONT);
4347c478bd9Sstevel@tonic-gate 	if (jp->j_tgid != jp->j_pgid)
4357c478bd9Sstevel@tonic-gate 		(void) kill(-(jp->j_tgid), SIGCONT);
4367c478bd9Sstevel@tonic-gate 	jp->j_flag &= ~(J_STOPPED|J_SIGNALED|J_SAVETTY);
4377c478bd9Sstevel@tonic-gate 	jp->j_flag |= J_RUNNING;
4387c478bd9Sstevel@tonic-gate 	if (fg)  {
4397c478bd9Sstevel@tonic-gate 		jp->j_flag |= J_FOREGND;
4407c478bd9Sstevel@tonic-gate 		printjob(jp, PR_JID|PR_CMD);
4417c478bd9Sstevel@tonic-gate 		waitjob(jp);
4427c478bd9Sstevel@tonic-gate 	} else {
4437c478bd9Sstevel@tonic-gate 		jp->j_flag &= ~J_FOREGND;
4447c478bd9Sstevel@tonic-gate 		printjob(jp, PR_JID|PR_CMD|PR_AMP);
4457c478bd9Sstevel@tonic-gate 	}
4467c478bd9Sstevel@tonic-gate }
4477c478bd9Sstevel@tonic-gate 
448965005c8Schin static void
printjob(struct job * jp,int propts)449965005c8Schin printjob(struct job *jp, int propts)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	int sp = 0;
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 	if (jp->j_flag & J_NOTIFY) {
4547c478bd9Sstevel@tonic-gate 		jobnote--;
4557c478bd9Sstevel@tonic-gate 		jp->j_flag &= ~J_NOTIFY;
4567c478bd9Sstevel@tonic-gate 	}
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	if (propts & PR_JID) {
4597c478bd9Sstevel@tonic-gate 		prc_buff('[');
4607c478bd9Sstevel@tonic-gate 		prn_buff(jp->j_jid);
4617c478bd9Sstevel@tonic-gate 		prc_buff(']');
4627c478bd9Sstevel@tonic-gate 		sp = 1;
4637c478bd9Sstevel@tonic-gate 	}
4647c478bd9Sstevel@tonic-gate 
4657c478bd9Sstevel@tonic-gate 	if (propts & PR_CUR) {
4667c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
4677c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
4687c478bd9Sstevel@tonic-gate 		sp = 1;
4697c478bd9Sstevel@tonic-gate 		if (jobcur == jp)
4707c478bd9Sstevel@tonic-gate 			prc_buff('+');
4717c478bd9Sstevel@tonic-gate 		else if (jobcur != 0 && jobcur->j_curp == jp)
4727c478bd9Sstevel@tonic-gate 			prc_buff('-');
4737c478bd9Sstevel@tonic-gate 		else
4747c478bd9Sstevel@tonic-gate 			sp++;
4757c478bd9Sstevel@tonic-gate 	}
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 	if (propts & PR_PGID) {
4787c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
4797c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
4807c478bd9Sstevel@tonic-gate 		prn_buff(jp->j_pid);
4817c478bd9Sstevel@tonic-gate 		sp = 1;
4827c478bd9Sstevel@tonic-gate 	}
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	if (propts & PR_STAT) {
48539e7390aSna 		char	*gmsg;
4867c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
4877c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
4887c478bd9Sstevel@tonic-gate 		sp = 28;
4897c478bd9Sstevel@tonic-gate 		if (jp->j_flag & J_SIGNALED) {
4907c478bd9Sstevel@tonic-gate 			char *sigstr, *strsignal();
4917c478bd9Sstevel@tonic-gate 			if ((sigstr = strsignal(jp->j_xval)) != NULL) {
4927c478bd9Sstevel@tonic-gate 				sp -= strlen(sigstr);
4937c478bd9Sstevel@tonic-gate 				prs_buff(sigstr);
4947c478bd9Sstevel@tonic-gate 			} else {
4957c478bd9Sstevel@tonic-gate 				itos(jp->j_xval);
49639e7390aSna 				gmsg = gettext(signalnum);
49739e7390aSna 				sp -= strlen(numbuf) + strlen(gmsg);
49839e7390aSna 				prs_buff((unsigned char *)gmsg);
4997c478bd9Sstevel@tonic-gate 				prs_buff(numbuf);
5007c478bd9Sstevel@tonic-gate 			}
5017c478bd9Sstevel@tonic-gate 			if (jp->j_flag & J_DUMPED) {
50239e7390aSna 				gmsg = gettext(coredump);
50339e7390aSna 				sp -= strlen(gmsg);
50439e7390aSna 				prs_buff((unsigned char *)gmsg);
5057c478bd9Sstevel@tonic-gate 			}
5067c478bd9Sstevel@tonic-gate 		} else if (jp->j_flag & J_DONE) {
5077c478bd9Sstevel@tonic-gate 			itos(jp->j_xval);
50839e7390aSna 			gmsg = gettext(exited);
50939e7390aSna 			sp -= strlen(gmsg) + strlen(numbuf) + 2;
51039e7390aSna 			prs_buff((unsigned char *)gmsg);
5117c478bd9Sstevel@tonic-gate 			prc_buff('(');
5127c478bd9Sstevel@tonic-gate 			itos(jp->j_xval);
5137c478bd9Sstevel@tonic-gate 			prs_buff(numbuf);
5147c478bd9Sstevel@tonic-gate 			prc_buff(')');
5157c478bd9Sstevel@tonic-gate 		} else {
51639e7390aSna 			gmsg = gettext(running);
51739e7390aSna 			sp -= strlen(gmsg);
51839e7390aSna 			prs_buff((unsigned char *)gmsg);
5197c478bd9Sstevel@tonic-gate 		}
5207c478bd9Sstevel@tonic-gate 		if (sp < 1)
5217c478bd9Sstevel@tonic-gate 			sp = 1;
5227c478bd9Sstevel@tonic-gate 	}
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 	if (propts & PR_CMD) {
5257c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
5267c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
5277c478bd9Sstevel@tonic-gate 		prs_buff(jp->j_cmd);
5287c478bd9Sstevel@tonic-gate 		sp = 1;
5297c478bd9Sstevel@tonic-gate 	}
5307c478bd9Sstevel@tonic-gate 
5317c478bd9Sstevel@tonic-gate 	if (propts & PR_AMP) {
5327c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
5337c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
5347c478bd9Sstevel@tonic-gate 		prc_buff('&');
5357c478bd9Sstevel@tonic-gate 		sp = 1;
5367c478bd9Sstevel@tonic-gate 	}
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 	if (propts & PR_PWD) {
5397c478bd9Sstevel@tonic-gate 		while (sp-- > 0)
5407c478bd9Sstevel@tonic-gate 			prc_buff(SPACE);
5417c478bd9Sstevel@tonic-gate 		prs_buff("(wd: ");
5427c478bd9Sstevel@tonic-gate 		prs_buff(jp->j_pwd);
5437c478bd9Sstevel@tonic-gate 		prc_buff(')');
5447c478bd9Sstevel@tonic-gate 	}
5457c478bd9Sstevel@tonic-gate 
5467c478bd9Sstevel@tonic-gate 	prc_buff(NL);
5477c478bd9Sstevel@tonic-gate 	flushb();
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate }
5507c478bd9Sstevel@tonic-gate 
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate /*
5537c478bd9Sstevel@tonic-gate  * called to initialize job control for each new input file to the shell,
5547c478bd9Sstevel@tonic-gate  * and after the "exec" builtin
5557c478bd9Sstevel@tonic-gate  */
5567c478bd9Sstevel@tonic-gate 
5577c478bd9Sstevel@tonic-gate void
startjobs()5587c478bd9Sstevel@tonic-gate startjobs()
5597c478bd9Sstevel@tonic-gate {
5607c478bd9Sstevel@tonic-gate 	svpgid = mypgid;
5617c478bd9Sstevel@tonic-gate 
5627c478bd9Sstevel@tonic-gate 	if (tcgetattr(0, &mystty) == -1 || (svtgid = tcgetpgrp(0)) == -1) {
5637c478bd9Sstevel@tonic-gate 		flags &= ~jcflg;
5647c478bd9Sstevel@tonic-gate 		return;
5657c478bd9Sstevel@tonic-gate 	}
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	flags |= jcflg;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	handle(SIGTTOU, SIG_IGN);
5707c478bd9Sstevel@tonic-gate 	handle(SIGTSTP, SIG_DFL);
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate 	if (mysid != mypgid) {
5737c478bd9Sstevel@tonic-gate 		setpgid(0, 0);
5747c478bd9Sstevel@tonic-gate 		mypgid = mypid;
5757c478bd9Sstevel@tonic-gate 		(void) settgid(mypgid, svpgid);
5767c478bd9Sstevel@tonic-gate 	}
5777c478bd9Sstevel@tonic-gate 
5787c478bd9Sstevel@tonic-gate }
5797c478bd9Sstevel@tonic-gate 
5807c478bd9Sstevel@tonic-gate int
endjobs(check_if)5817c478bd9Sstevel@tonic-gate endjobs(check_if)
5827c478bd9Sstevel@tonic-gate int check_if;
5837c478bd9Sstevel@tonic-gate {
5847c478bd9Sstevel@tonic-gate 	if ((flags & (jcoff|jcflg)) != jcflg)
5857c478bd9Sstevel@tonic-gate 		return (1);
5867c478bd9Sstevel@tonic-gate 
5877c478bd9Sstevel@tonic-gate 	if (check_if && jobcnt && eofflg++ == 0) {
588965005c8Schin 		struct job *jp;
5897c478bd9Sstevel@tonic-gate 		if (check_if & JOB_STOPPED) {
5907c478bd9Sstevel@tonic-gate 			for (jp = joblst; jp; jp = jp->j_nxtp) {
5917c478bd9Sstevel@tonic-gate 				if (jp->j_jid && (jp->j_flag & J_STOPPED)) {
59239e7390aSna 					prs(_gettext(jobsstopped));
5937c478bd9Sstevel@tonic-gate 					prc(NL);
5947c478bd9Sstevel@tonic-gate 					return (0);
5957c478bd9Sstevel@tonic-gate 				}
5967c478bd9Sstevel@tonic-gate 			}
5977c478bd9Sstevel@tonic-gate 		}
5987c478bd9Sstevel@tonic-gate 		if (check_if & JOB_RUNNING) {
5997c478bd9Sstevel@tonic-gate 			for (jp = joblst; jp; jp = jp->j_nxtp) {
6007c478bd9Sstevel@tonic-gate 				if (jp->j_jid && (jp->j_flag & J_RUNNING)) {
60139e7390aSna 					prs(_gettext(jobsrunning));
6027c478bd9Sstevel@tonic-gate 					prc(NL);
6037c478bd9Sstevel@tonic-gate 					return (0);
6047c478bd9Sstevel@tonic-gate 				}
6057c478bd9Sstevel@tonic-gate 			}
6067c478bd9Sstevel@tonic-gate 		}
6077c478bd9Sstevel@tonic-gate 	}
6087c478bd9Sstevel@tonic-gate 
6097c478bd9Sstevel@tonic-gate 	if (svpgid != mypgid) {
6107c478bd9Sstevel@tonic-gate 		(void) settgid(svtgid, mypgid);
6117c478bd9Sstevel@tonic-gate 		setpgid(0, svpgid);
6127c478bd9Sstevel@tonic-gate 	}
6137c478bd9Sstevel@tonic-gate 
6147c478bd9Sstevel@tonic-gate 	return (1);
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate 
6187c478bd9Sstevel@tonic-gate /*
6197c478bd9Sstevel@tonic-gate  * called by the shell to reserve a job slot for a job about to be spawned
6207c478bd9Sstevel@tonic-gate  */
6217c478bd9Sstevel@tonic-gate 
6227c478bd9Sstevel@tonic-gate void
deallocjob()6237c478bd9Sstevel@tonic-gate deallocjob()
6247c478bd9Sstevel@tonic-gate {
6257c478bd9Sstevel@tonic-gate 	free(thisjob);
6267c478bd9Sstevel@tonic-gate 	jobcnt--;
6277c478bd9Sstevel@tonic-gate }
6287c478bd9Sstevel@tonic-gate 
629965005c8Schin void
allocjob(char * cmd,uchar_t * cwd,int monitor)63024da5b34Srie allocjob(char *cmd, uchar_t *cwd, int monitor)
6317c478bd9Sstevel@tonic-gate {
632965005c8Schin 	struct job *jp, **jpp;
633965005c8Schin 	int jid, cmdlen, cwdlen;
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	cmdlen = strlen(cmd) + 1;
6367c478bd9Sstevel@tonic-gate 	if (cmd[cmdlen-2] == '&') {
6377c478bd9Sstevel@tonic-gate 		cmd[cmdlen-3] = 0;
6387c478bd9Sstevel@tonic-gate 		cmdlen -= 2;
6397c478bd9Sstevel@tonic-gate 	}
6407c478bd9Sstevel@tonic-gate 	cwdlen = strlen(cwd) + 1;
6417c478bd9Sstevel@tonic-gate 	jp = (struct job *) alloc(sizeof (struct job) + cmdlen + cwdlen);
6427c478bd9Sstevel@tonic-gate 	if (jp == 0)
6437c478bd9Sstevel@tonic-gate 		error(nostack);
6447c478bd9Sstevel@tonic-gate 	jobcnt++;
6457c478bd9Sstevel@tonic-gate 	jp->j_cmd = ((char *)jp) + sizeof (struct job);
6467c478bd9Sstevel@tonic-gate 	strcpy(jp->j_cmd, cmd);
6477c478bd9Sstevel@tonic-gate 	jp->j_pwd = jp->j_cmd + cmdlen;
6487c478bd9Sstevel@tonic-gate 	strcpy(jp->j_pwd, cwd);
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	jpp = &joblst;
6517c478bd9Sstevel@tonic-gate 
6527c478bd9Sstevel@tonic-gate 	if (monitor) {
6537c478bd9Sstevel@tonic-gate 		for (; *jpp; jpp = &(*jpp)->j_nxtp)
6547c478bd9Sstevel@tonic-gate 			if ((*jpp)->j_jid != 0)
6557c478bd9Sstevel@tonic-gate 				break;
6567c478bd9Sstevel@tonic-gate 		for (jid = 1; *jpp; jpp = &(*jpp)->j_nxtp, jid++)
6577c478bd9Sstevel@tonic-gate 			if ((*jpp)->j_jid != jid)
6587c478bd9Sstevel@tonic-gate 				break;
6597c478bd9Sstevel@tonic-gate 	} else
6607c478bd9Sstevel@tonic-gate 		jid = 0;
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	jp->j_jid = jid;
6637c478bd9Sstevel@tonic-gate 	nextjob = jpp;
6647c478bd9Sstevel@tonic-gate 	thisjob = jp;
6657c478bd9Sstevel@tonic-gate }
6667c478bd9Sstevel@tonic-gate 
667965005c8Schin void
clearjobs(void)668965005c8Schin clearjobs(void)
6697c478bd9Sstevel@tonic-gate {
670965005c8Schin 	struct job *jp, *sjp;
6717c478bd9Sstevel@tonic-gate 
6727c478bd9Sstevel@tonic-gate 	for (jp = joblst; jp; jp = sjp) {
6737c478bd9Sstevel@tonic-gate 		sjp = jp->j_nxtp;
6747c478bd9Sstevel@tonic-gate 		free(jp);
6757c478bd9Sstevel@tonic-gate 	}
6767c478bd9Sstevel@tonic-gate 	joblst = NULL;
6777c478bd9Sstevel@tonic-gate 	jobcnt = 0;
6787c478bd9Sstevel@tonic-gate 	jobnote = 0;
6797c478bd9Sstevel@tonic-gate 	jobdone = 0;
6807c478bd9Sstevel@tonic-gate 
6817c478bd9Sstevel@tonic-gate }
6827c478bd9Sstevel@tonic-gate 
683965005c8Schin void
makejob(int monitor,int fg)684965005c8Schin makejob(int monitor, int fg)
6857c478bd9Sstevel@tonic-gate {
6867c478bd9Sstevel@tonic-gate 	if (monitor) {
6877c478bd9Sstevel@tonic-gate 		mypgid = mypid;
6887c478bd9Sstevel@tonic-gate 		setpgid(0, 0);
6897c478bd9Sstevel@tonic-gate 		if (fg)
6907c478bd9Sstevel@tonic-gate 			tcsetpgrp(0, mypid);
6917c478bd9Sstevel@tonic-gate 		handle(SIGTTOU, SIG_DFL);
6927c478bd9Sstevel@tonic-gate 		handle(SIGTSTP, SIG_DFL);
6937c478bd9Sstevel@tonic-gate 	} else if (!fg) {
6947c478bd9Sstevel@tonic-gate #ifdef NICE
6957c478bd9Sstevel@tonic-gate 		nice(NICE);
6967c478bd9Sstevel@tonic-gate #endif
6977c478bd9Sstevel@tonic-gate 		handle(SIGTTIN, SIG_IGN);
6987c478bd9Sstevel@tonic-gate 		handle(SIGINT,  SIG_IGN);
6997c478bd9Sstevel@tonic-gate 		handle(SIGQUIT, SIG_IGN);
7007c478bd9Sstevel@tonic-gate 		if (!ioset)
7017c478bd9Sstevel@tonic-gate 			renamef(chkopen(devnull, 0), 0);
7027c478bd9Sstevel@tonic-gate 	}
7037c478bd9Sstevel@tonic-gate }
7047c478bd9Sstevel@tonic-gate 
7057c478bd9Sstevel@tonic-gate /*
7067c478bd9Sstevel@tonic-gate  * called by the shell after job has been spawned, to fill in the
7077c478bd9Sstevel@tonic-gate  * job slot, and wait for the job if in the foreground
7087c478bd9Sstevel@tonic-gate  */
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate void
postjob(pid,fg)7117c478bd9Sstevel@tonic-gate postjob(pid, fg)
7127c478bd9Sstevel@tonic-gate pid_t pid;
7137c478bd9Sstevel@tonic-gate int fg;
7147c478bd9Sstevel@tonic-gate {
7157c478bd9Sstevel@tonic-gate 
716965005c8Schin 	int propts;
7177c478bd9Sstevel@tonic-gate 
7187c478bd9Sstevel@tonic-gate 	thisjob->j_nxtp = *nextjob;
7197c478bd9Sstevel@tonic-gate 	*nextjob = thisjob;
7207c478bd9Sstevel@tonic-gate 	thisjob->j_curp = jobcur;
7217c478bd9Sstevel@tonic-gate 	jobcur = thisjob;
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 	if (thisjob->j_jid) {
7247c478bd9Sstevel@tonic-gate 		thisjob->j_pgid = pid;
7257c478bd9Sstevel@tonic-gate 		propts = PR_JID|PR_PGID;
7267c478bd9Sstevel@tonic-gate 	} else {
7277c478bd9Sstevel@tonic-gate 		thisjob->j_pgid = mypgid;
7287c478bd9Sstevel@tonic-gate 		propts = PR_PGID;
7297c478bd9Sstevel@tonic-gate 	}
7307c478bd9Sstevel@tonic-gate 
7317c478bd9Sstevel@tonic-gate 	thisjob->j_flag = J_RUNNING;
7327c478bd9Sstevel@tonic-gate 	thisjob->j_tgid = thisjob->j_pgid;
7337c478bd9Sstevel@tonic-gate 	thisjob->j_pid = pid;
7347c478bd9Sstevel@tonic-gate 	eofflg = 0;
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 	if (fg) {
7377c478bd9Sstevel@tonic-gate 		thisjob->j_flag |= J_FOREGND;
7387c478bd9Sstevel@tonic-gate 		waitjob(thisjob);
7397c478bd9Sstevel@tonic-gate 	} else  {
7407c478bd9Sstevel@tonic-gate 		if (flags & ttyflg)
7417c478bd9Sstevel@tonic-gate 			printjob(thisjob, propts);
7427c478bd9Sstevel@tonic-gate 		assnum(&pcsadr, (long)pid);
7437c478bd9Sstevel@tonic-gate 	}
7447c478bd9Sstevel@tonic-gate }
7457c478bd9Sstevel@tonic-gate 
7467c478bd9Sstevel@tonic-gate /*
7477c478bd9Sstevel@tonic-gate  * the builtin "jobs" command
7487c478bd9Sstevel@tonic-gate  */
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate void
sysjobs(argc,argv)7517c478bd9Sstevel@tonic-gate sysjobs(argc, argv)
7527c478bd9Sstevel@tonic-gate int argc;
7537c478bd9Sstevel@tonic-gate char *argv[];
7547c478bd9Sstevel@tonic-gate {
755965005c8Schin 	char *cmd = *argv;
756965005c8Schin 	struct job *jp;
757965005c8Schin 	int propts, c;
7587c478bd9Sstevel@tonic-gate 	extern int opterr, i;
7597c478bd9Sstevel@tonic-gate 	int savoptind = optind;
7607c478bd9Sstevel@tonic-gate 	int loptind = -1;
7617c478bd9Sstevel@tonic-gate 	int savopterr = opterr;
7627c478bd9Sstevel@tonic-gate 	int savsp = _sp;
7637c478bd9Sstevel@tonic-gate 	char *savoptarg = optarg;
7647c478bd9Sstevel@tonic-gate 	optind = 1;
7657c478bd9Sstevel@tonic-gate 	opterr = 0;
7667c478bd9Sstevel@tonic-gate 	_sp = 1;
7677c478bd9Sstevel@tonic-gate 	propts = 0;
7687c478bd9Sstevel@tonic-gate 
7697c478bd9Sstevel@tonic-gate 	if ((flags & jcflg) == 0)
770965005c8Schin 		failed((unsigned char *)cmd, nojc);
7717c478bd9Sstevel@tonic-gate 
7727c478bd9Sstevel@tonic-gate 	while ((c = getopt(argc, argv, "lpx")) != -1) {
7737c478bd9Sstevel@tonic-gate 		if (propts) {
77439e7390aSna 			gfailure(usage, jobsuse);
7757c478bd9Sstevel@tonic-gate 			goto err;
7767c478bd9Sstevel@tonic-gate 		}
7777c478bd9Sstevel@tonic-gate 		switch (c) {
7787c478bd9Sstevel@tonic-gate 			case 'x':
7797c478bd9Sstevel@tonic-gate 				propts = -1;
7807c478bd9Sstevel@tonic-gate 				break;
7817c478bd9Sstevel@tonic-gate 			case 'p':
7827c478bd9Sstevel@tonic-gate 				propts = PR_PGID;
7837c478bd9Sstevel@tonic-gate 				break;
7847c478bd9Sstevel@tonic-gate 			case 'l':
7857c478bd9Sstevel@tonic-gate 				propts = PR_LONG;
7867c478bd9Sstevel@tonic-gate 				break;
7877c478bd9Sstevel@tonic-gate 			case '?':
78839e7390aSna 				gfailure(usage, jobsuse);
7897c478bd9Sstevel@tonic-gate 				goto err;
7907c478bd9Sstevel@tonic-gate 		}
7917c478bd9Sstevel@tonic-gate 	}
7927c478bd9Sstevel@tonic-gate 
7937c478bd9Sstevel@tonic-gate 	loptind = optind;
7947c478bd9Sstevel@tonic-gate err:
7957c478bd9Sstevel@tonic-gate 	optind = savoptind;
7967c478bd9Sstevel@tonic-gate 	optarg = savoptarg;
7977c478bd9Sstevel@tonic-gate 	opterr = savopterr;
7987c478bd9Sstevel@tonic-gate 	_sp = savsp;
7997c478bd9Sstevel@tonic-gate 	if (loptind == -1)
8007c478bd9Sstevel@tonic-gate 		return;
8017c478bd9Sstevel@tonic-gate 
8027c478bd9Sstevel@tonic-gate 	if (propts == -1) {
803965005c8Schin 		unsigned char *bp;
804965005c8Schin 		char *cp;
8057c478bd9Sstevel@tonic-gate 		unsigned char *savebp;
8067c478bd9Sstevel@tonic-gate 		for (savebp = bp = locstak(); loptind < argc; loptind++) {
8077c478bd9Sstevel@tonic-gate 			cp = argv[loptind];
8087c478bd9Sstevel@tonic-gate 			if (*cp == '%') {
8097c478bd9Sstevel@tonic-gate 				jp = str2job(cmd, cp, 1);
8107c478bd9Sstevel@tonic-gate 				itos(jp->j_pid);
8117c478bd9Sstevel@tonic-gate 				cp = (char *)numbuf;
8127c478bd9Sstevel@tonic-gate 			}
8137c478bd9Sstevel@tonic-gate 			while (*cp) {
8147c478bd9Sstevel@tonic-gate 				if (bp >= brkend)
8157c478bd9Sstevel@tonic-gate 					growstak(bp);
8167c478bd9Sstevel@tonic-gate 				*bp++ = *cp++;
8177c478bd9Sstevel@tonic-gate 			}
8187c478bd9Sstevel@tonic-gate 			if (bp >= brkend)
8197c478bd9Sstevel@tonic-gate 				growstak(bp);
8207c478bd9Sstevel@tonic-gate 			*bp++ = SPACE;
8217c478bd9Sstevel@tonic-gate 		}
8227c478bd9Sstevel@tonic-gate 		endstak(bp);
8237c478bd9Sstevel@tonic-gate 		execexp(savebp, 0);
8247c478bd9Sstevel@tonic-gate 		return;
8257c478bd9Sstevel@tonic-gate 	}
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate 	collectjobs(WNOHANG);
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate 	if (propts == 0)
8307c478bd9Sstevel@tonic-gate 		propts = PR_DFL;
8317c478bd9Sstevel@tonic-gate 
8327c478bd9Sstevel@tonic-gate 	if (loptind == argc) {
8337c478bd9Sstevel@tonic-gate 		for (jp = joblst; jp; jp = jp->j_nxtp) {
8347c478bd9Sstevel@tonic-gate 			if (jp->j_jid)
8357c478bd9Sstevel@tonic-gate 				printjob(jp, propts);
8367c478bd9Sstevel@tonic-gate 		}
8377c478bd9Sstevel@tonic-gate 	} else do
8387c478bd9Sstevel@tonic-gate 		printjob(str2job(cmd, argv[loptind++], 1), propts);
8397c478bd9Sstevel@tonic-gate 	while (loptind < argc);
8407c478bd9Sstevel@tonic-gate 
8417c478bd9Sstevel@tonic-gate }
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate /*
8447c478bd9Sstevel@tonic-gate  * the builtin "fg" and "bg" commands
8457c478bd9Sstevel@tonic-gate  */
846965005c8Schin void
sysfgbg(int argc,char * argv[])847965005c8Schin sysfgbg(int argc, char *argv[])
8487c478bd9Sstevel@tonic-gate {
849965005c8Schin 	char *cmd = *argv;
850965005c8Schin 	int fg;
8517c478bd9Sstevel@tonic-gate 
8527c478bd9Sstevel@tonic-gate 	if ((flags & jcflg) == 0)
853965005c8Schin 		failed((unsigned char *)cmd, nojc);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 	fg = eq("fg", cmd);
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate 	if (*++argv == 0) {
8587c478bd9Sstevel@tonic-gate 		struct job *jp;
8597c478bd9Sstevel@tonic-gate 		for (jp = jobcur; ; jp = jp->j_curp) {
8607c478bd9Sstevel@tonic-gate 			if (jp == 0)
861965005c8Schin 				failed((unsigned char *)cmd, nocurjob);
8627c478bd9Sstevel@tonic-gate 			if (jp->j_jid)
8637c478bd9Sstevel@tonic-gate 				break;
8647c478bd9Sstevel@tonic-gate 		}
8657c478bd9Sstevel@tonic-gate 		restartjob(jp, fg);
866*05845d98SArindam Sarkar 	} else {
867*05845d98SArindam Sarkar 		do {
868*05845d98SArindam Sarkar 			restartjob(str2job(cmd, *argv, 1), fg);
869*05845d98SArindam Sarkar 		} while (*++argv);
8707c478bd9Sstevel@tonic-gate 	}
8717c478bd9Sstevel@tonic-gate }
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate /*
8747c478bd9Sstevel@tonic-gate  * the builtin "wait" commands
8757c478bd9Sstevel@tonic-gate  */
8767c478bd9Sstevel@tonic-gate 
8777c478bd9Sstevel@tonic-gate void
syswait(argc,argv)8787c478bd9Sstevel@tonic-gate syswait(argc, argv)
8797c478bd9Sstevel@tonic-gate int argc;
8807c478bd9Sstevel@tonic-gate char *argv[];
8817c478bd9Sstevel@tonic-gate {
882965005c8Schin 	char *cmd = *argv;
883965005c8Schin 	struct job *jp;
8847c478bd9Sstevel@tonic-gate 	int stat;
8857c478bd9Sstevel@tonic-gate 	int wflags;
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 	if ((flags & (monitorflg|jcflg|jcoff)) == (monitorflg|jcflg))
8887c478bd9Sstevel@tonic-gate 		wflags = WUNTRACED;
8897c478bd9Sstevel@tonic-gate 	else
8907c478bd9Sstevel@tonic-gate 		wflags = 0;
8917c478bd9Sstevel@tonic-gate 
8927c478bd9Sstevel@tonic-gate 	if (argc == 1)
8937c478bd9Sstevel@tonic-gate 		collectjobs(0);
8947c478bd9Sstevel@tonic-gate 	else while (--argc) {
8957c478bd9Sstevel@tonic-gate 		if ((jp = str2job(cmd, *++argv, 0)) == 0)
8967c478bd9Sstevel@tonic-gate 			continue;
8977c478bd9Sstevel@tonic-gate 		if (!(jp->j_flag & J_RUNNING))
8987c478bd9Sstevel@tonic-gate 			continue;
8997c478bd9Sstevel@tonic-gate 		if (waitpid(jp->j_pid, &stat, wflags) <= 0)
9007c478bd9Sstevel@tonic-gate 			break;
9017c478bd9Sstevel@tonic-gate 		(void) statjob(jp, stat, 0, 1);
9027c478bd9Sstevel@tonic-gate 	}
9037c478bd9Sstevel@tonic-gate }
9047c478bd9Sstevel@tonic-gate 
905965005c8Schin static void
sigv(char * cmd,int sig,char * args)906965005c8Schin sigv(char *cmd, int sig, char *args)
9077c478bd9Sstevel@tonic-gate {
9087c478bd9Sstevel@tonic-gate 	int pgrp = 0;
9097c478bd9Sstevel@tonic-gate 	int stopme = 0;
9107c478bd9Sstevel@tonic-gate 	pid_t id;
9117c478bd9Sstevel@tonic-gate 
9127c478bd9Sstevel@tonic-gate 	if (*args == '%') {
913965005c8Schin 		struct job *jp;
9147c478bd9Sstevel@tonic-gate 		jp = str2job(cmd, args, 1);
9157c478bd9Sstevel@tonic-gate 		id = jp->j_pgid;
9167c478bd9Sstevel@tonic-gate 		pgrp++;
9177c478bd9Sstevel@tonic-gate 	} else {
9187c478bd9Sstevel@tonic-gate 		if (*args == '-') {
9197c478bd9Sstevel@tonic-gate 			pgrp++;
9207c478bd9Sstevel@tonic-gate 			args++;
9217c478bd9Sstevel@tonic-gate 		}
9227c478bd9Sstevel@tonic-gate 		id = 0;
9237c478bd9Sstevel@tonic-gate 		do {
9247c478bd9Sstevel@tonic-gate 			if (*args < '0' || *args > '9') {
9257c478bd9Sstevel@tonic-gate 				failure(cmd, badid);
9267c478bd9Sstevel@tonic-gate 				return;
9277c478bd9Sstevel@tonic-gate 			}
9287c478bd9Sstevel@tonic-gate 			id = (id * 10) + (*args - '0');
9297c478bd9Sstevel@tonic-gate 		} while (*++args);
9307c478bd9Sstevel@tonic-gate 		if (id == 0) {
9317c478bd9Sstevel@tonic-gate 			id = mypgid;
9327c478bd9Sstevel@tonic-gate 			pgrp++;
9337c478bd9Sstevel@tonic-gate 		}
9347c478bd9Sstevel@tonic-gate 	}
9357c478bd9Sstevel@tonic-gate 
9367c478bd9Sstevel@tonic-gate 	if (sig == SIGSTOP) {
9377c478bd9Sstevel@tonic-gate 		if (id == mysid || id == mypid && mypgid == mysid) {
9387c478bd9Sstevel@tonic-gate 			failure(cmd, loginsh);
9397c478bd9Sstevel@tonic-gate 			return;
9407c478bd9Sstevel@tonic-gate 		}
9417c478bd9Sstevel@tonic-gate 		if (id == mypgid && mypgid != svpgid) {
9427c478bd9Sstevel@tonic-gate 			(void) settgid(svtgid, mypgid);
9437c478bd9Sstevel@tonic-gate 			setpgid(0, svpgid);
9447c478bd9Sstevel@tonic-gate 			stopme++;
9457c478bd9Sstevel@tonic-gate 		}
9467c478bd9Sstevel@tonic-gate 	}
9477c478bd9Sstevel@tonic-gate 
9487c478bd9Sstevel@tonic-gate 	if (pgrp)
9497c478bd9Sstevel@tonic-gate 		id = -id;
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate 	if (kill(id, sig) < 0) {
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate 		switch (errno) {
9547c478bd9Sstevel@tonic-gate 			case EPERM:
9557c478bd9Sstevel@tonic-gate 				failure(cmd, eacces);
9567c478bd9Sstevel@tonic-gate 				break;
9577c478bd9Sstevel@tonic-gate 
9587c478bd9Sstevel@tonic-gate 			case EINVAL:
9597c478bd9Sstevel@tonic-gate 				failure(cmd, badsig);
9607c478bd9Sstevel@tonic-gate 				break;
9617c478bd9Sstevel@tonic-gate 
9627c478bd9Sstevel@tonic-gate 			default:
9637c478bd9Sstevel@tonic-gate 				if (pgrp)
9647c478bd9Sstevel@tonic-gate 					failure(cmd, nosuchpgid);
9657c478bd9Sstevel@tonic-gate 				else
9667c478bd9Sstevel@tonic-gate 					failure(cmd, nosuchpid);
9677c478bd9Sstevel@tonic-gate 				break;
9687c478bd9Sstevel@tonic-gate 		}
9697c478bd9Sstevel@tonic-gate 
9707c478bd9Sstevel@tonic-gate 	} else if (sig == SIGTERM && pgrp)
9717c478bd9Sstevel@tonic-gate 		(void) kill(id, SIGCONT);
9727c478bd9Sstevel@tonic-gate 
9737c478bd9Sstevel@tonic-gate 	if (stopme) {
9747c478bd9Sstevel@tonic-gate 		setpgid(0, mypgid);
9757c478bd9Sstevel@tonic-gate 		(void) settgid(mypgid, svpgid);
9767c478bd9Sstevel@tonic-gate 	}
9777c478bd9Sstevel@tonic-gate 
9787c478bd9Sstevel@tonic-gate }
9797c478bd9Sstevel@tonic-gate 
980965005c8Schin void
sysstop(int argc,char * argv[])981965005c8Schin sysstop(int argc, char *argv[])
9827c478bd9Sstevel@tonic-gate {
9837c478bd9Sstevel@tonic-gate 	char *cmd = *argv;
98439e7390aSna 	if (argc <= 1) {
98539e7390aSna 		gfailure(usage, stopuse);
98639e7390aSna 		return;
98739e7390aSna 	}
9887c478bd9Sstevel@tonic-gate 	while (*++argv)
9897c478bd9Sstevel@tonic-gate 		sigv(cmd, SIGSTOP, *argv);
9907c478bd9Sstevel@tonic-gate }
9917c478bd9Sstevel@tonic-gate 
992965005c8Schin void
syskill(int argc,char * argv[])993965005c8Schin syskill(int argc, char *argv[])
9947c478bd9Sstevel@tonic-gate {
9957c478bd9Sstevel@tonic-gate 	char *cmd = *argv;
9967c478bd9Sstevel@tonic-gate 	int sig = SIGTERM;
9977c478bd9Sstevel@tonic-gate 
9987c478bd9Sstevel@tonic-gate 	if (argc == 1) {
99939e7390aSna 		gfailure(usage, killuse);
10007c478bd9Sstevel@tonic-gate 		return;
10017c478bd9Sstevel@tonic-gate 	}
10027c478bd9Sstevel@tonic-gate 
10037c478bd9Sstevel@tonic-gate 	if (argv[1][0] == '-') {
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 		if (argc == 2) {
10067c478bd9Sstevel@tonic-gate 
1007965005c8Schin 			int i;
1008965005c8Schin 			int cnt = 0;
1009965005c8Schin 			char sep = 0;
10107c478bd9Sstevel@tonic-gate 			char buf[12];
10117c478bd9Sstevel@tonic-gate 
10127c478bd9Sstevel@tonic-gate 			if (!eq(argv[1], "-l")) {
101339e7390aSna 				gfailure(usage, killuse);
10147c478bd9Sstevel@tonic-gate 				return;
10157c478bd9Sstevel@tonic-gate 			}
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate 			for (i = 1; i < MAXTRAP; i++) {
10187c478bd9Sstevel@tonic-gate 				if (sig2str(i, buf) < 0)
10197c478bd9Sstevel@tonic-gate 					continue;
10207c478bd9Sstevel@tonic-gate 				if (sep)
10217c478bd9Sstevel@tonic-gate 					prc_buff(sep);
10227c478bd9Sstevel@tonic-gate 				prs_buff(buf);
10237c478bd9Sstevel@tonic-gate 				if ((flags & ttyflg) && (++cnt % 10))
10247c478bd9Sstevel@tonic-gate 					sep = TAB;
10257c478bd9Sstevel@tonic-gate 				else
10267c478bd9Sstevel@tonic-gate 					sep = NL;
10277c478bd9Sstevel@tonic-gate 			}
10287c478bd9Sstevel@tonic-gate 			prc_buff(NL);
10297c478bd9Sstevel@tonic-gate 			return;
10307c478bd9Sstevel@tonic-gate 		}
10317c478bd9Sstevel@tonic-gate 
10327c478bd9Sstevel@tonic-gate 		if (str2sig(&argv[1][1], &sig)) {
10337c478bd9Sstevel@tonic-gate 			failure(cmd, badsig);
10347c478bd9Sstevel@tonic-gate 			return;
10357c478bd9Sstevel@tonic-gate 		}
10367c478bd9Sstevel@tonic-gate 		argv++;
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 
10397c478bd9Sstevel@tonic-gate 	while (*++argv)
10407c478bd9Sstevel@tonic-gate 		sigv(cmd, sig, *argv);
10417c478bd9Sstevel@tonic-gate 
10427c478bd9Sstevel@tonic-gate }
10437c478bd9Sstevel@tonic-gate 
1044965005c8Schin void
syssusp(int argc,char * argv[])1045965005c8Schin syssusp(int argc, char *argv[])
10467c478bd9Sstevel@tonic-gate {
10477c478bd9Sstevel@tonic-gate 	if (argc != 1)
1048965005c8Schin 		failed((unsigned char *)argv[0], badopt);
10497c478bd9Sstevel@tonic-gate 	sigv(argv[0], SIGSTOP, "0");
10507c478bd9Sstevel@tonic-gate }
1051*05845d98SArindam Sarkar 
1052*05845d98SArindam Sarkar void
hupforegnd(void)1053*05845d98SArindam Sarkar hupforegnd(void)
1054*05845d98SArindam Sarkar {
1055*05845d98SArindam Sarkar 	struct job *jp;
1056*05845d98SArindam Sarkar 
1057*05845d98SArindam Sarkar 	(void) sighold(SIGCHLD);
1058*05845d98SArindam Sarkar 	for (jp = joblst; jp != NULL; jp = jp->j_nxtp) {
1059*05845d98SArindam Sarkar 		if (jp->j_flag & J_FOREGND) {
1060*05845d98SArindam Sarkar 			(void) kill(jp->j_pid, SIGHUP);
1061*05845d98SArindam Sarkar 			break;
1062*05845d98SArindam Sarkar 		}
1063*05845d98SArindam Sarkar 	}
1064*05845d98SArindam Sarkar 	(void) sigrelse(SIGCHLD);
1065*05845d98SArindam Sarkar }
1066