xref: /freebsd/contrib/tcsh/sh.proc.c (revision 6560ac57ce879857203bc456cdc3849808dc0700)
1c80476e4SDavid E. O'Brien /*
2c80476e4SDavid E. O'Brien  * sh.proc.c: Job manipulations
3c80476e4SDavid E. O'Brien  */
4c80476e4SDavid E. O'Brien /*-
5c80476e4SDavid E. O'Brien  * Copyright (c) 1980, 1991 The Regents of the University of California.
6c80476e4SDavid E. O'Brien  * All rights reserved.
7c80476e4SDavid E. O'Brien  *
8c80476e4SDavid E. O'Brien  * Redistribution and use in source and binary forms, with or without
9c80476e4SDavid E. O'Brien  * modification, are permitted provided that the following conditions
10c80476e4SDavid E. O'Brien  * are met:
11c80476e4SDavid E. O'Brien  * 1. Redistributions of source code must retain the above copyright
12c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer.
13c80476e4SDavid E. O'Brien  * 2. Redistributions in binary form must reproduce the above copyright
14c80476e4SDavid E. O'Brien  *    notice, this list of conditions and the following disclaimer in the
15c80476e4SDavid E. O'Brien  *    documentation and/or other materials provided with the distribution.
1629301572SMark Peek  * 3. Neither the name of the University nor the names of its contributors
17c80476e4SDavid E. O'Brien  *    may be used to endorse or promote products derived from this software
18c80476e4SDavid E. O'Brien  *    without specific prior written permission.
19c80476e4SDavid E. O'Brien  *
20c80476e4SDavid E. O'Brien  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21c80476e4SDavid E. O'Brien  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22c80476e4SDavid E. O'Brien  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23c80476e4SDavid E. O'Brien  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24c80476e4SDavid E. O'Brien  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25c80476e4SDavid E. O'Brien  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26c80476e4SDavid E. O'Brien  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27c80476e4SDavid E. O'Brien  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28c80476e4SDavid E. O'Brien  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29c80476e4SDavid E. O'Brien  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30c80476e4SDavid E. O'Brien  * SUCH DAMAGE.
31c80476e4SDavid E. O'Brien  */
32c80476e4SDavid E. O'Brien #include "sh.h"
33c80476e4SDavid E. O'Brien #include "ed.h"
34c80476e4SDavid E. O'Brien #include "tc.h"
35c80476e4SDavid E. O'Brien #include "tc.wait.h"
36c80476e4SDavid E. O'Brien 
373b6eaa7bSAndrey A. Chernov #ifdef WINNT_NATIVE
38c80476e4SDavid E. O'Brien #undef POSIX
39c80476e4SDavid E. O'Brien #define POSIX
403b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
41c80476e4SDavid E. O'Brien #ifdef aiws
42c80476e4SDavid E. O'Brien # undef HZ
43c80476e4SDavid E. O'Brien # define HZ 16
44c80476e4SDavid E. O'Brien #endif /* aiws */
45c80476e4SDavid E. O'Brien 
46*19d2e3deSDmitry Chagin #if defined(_BSD) || (defined(IRIS4D) && __STDC__) || defined(__lucid)
47c80476e4SDavid E. O'Brien # define BSDWAIT
48*19d2e3deSDmitry Chagin #endif /* _BSD || (IRIS4D && __STDC__) || __lucid */
49c80476e4SDavid E. O'Brien #ifndef WTERMSIG
50c80476e4SDavid E. O'Brien # define WTERMSIG(w)	(((union wait *) &(w))->w_termsig)
51c80476e4SDavid E. O'Brien # ifndef BSDWAIT
52c80476e4SDavid E. O'Brien #  define BSDWAIT
53c80476e4SDavid E. O'Brien # endif /* !BSDWAIT */
54c80476e4SDavid E. O'Brien #endif /* !WTERMSIG */
55c80476e4SDavid E. O'Brien #ifndef WEXITSTATUS
56c80476e4SDavid E. O'Brien # define WEXITSTATUS(w)	(((union wait *) &(w))->w_retcode)
57c80476e4SDavid E. O'Brien #endif /* !WEXITSTATUS */
58c80476e4SDavid E. O'Brien #ifndef WSTOPSIG
59c80476e4SDavid E. O'Brien # define WSTOPSIG(w)	(((union wait *) &(w))->w_stopsig)
60c80476e4SDavid E. O'Brien #endif /* !WSTOPSIG */
61c80476e4SDavid E. O'Brien 
62c80476e4SDavid E. O'Brien #ifdef __osf__
63c80476e4SDavid E. O'Brien # ifndef WCOREDUMP
64c80476e4SDavid E. O'Brien #  define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
65c80476e4SDavid E. O'Brien # endif
66c80476e4SDavid E. O'Brien #endif
67c80476e4SDavid E. O'Brien 
68c80476e4SDavid E. O'Brien #ifndef WCOREDUMP
69c80476e4SDavid E. O'Brien # ifdef BSDWAIT
70c80476e4SDavid E. O'Brien #  define WCOREDUMP(w)	(((union wait *) &(w))->w_coredump)
71c80476e4SDavid E. O'Brien # else /* !BSDWAIT */
72c80476e4SDavid E. O'Brien #  define WCOREDUMP(w)	((w) & 0200)
73c80476e4SDavid E. O'Brien # endif /* !BSDWAIT */
74c80476e4SDavid E. O'Brien #endif /* !WCOREDUMP */
75c80476e4SDavid E. O'Brien 
7645e5710bSMark Peek #ifndef JOBDEBUG
7745e5710bSMark Peek # define jobdebug_xprintf(x)	(void)0
7845e5710bSMark Peek # define jobdebug_flush()	(void)0
7945e5710bSMark Peek #else
8045e5710bSMark Peek # define jobdebug_xprintf(s)	xprintf s
8145e5710bSMark Peek # define jobdebug_flush()	flush()
8245e5710bSMark Peek #endif
8345e5710bSMark Peek 
84c80476e4SDavid E. O'Brien /*
85c80476e4SDavid E. O'Brien  * C Shell - functions that manage processes, handling hanging, termination
86c80476e4SDavid E. O'Brien  */
87c80476e4SDavid E. O'Brien 
88c80476e4SDavid E. O'Brien #define BIGINDEX	9	/* largest desirable job index */
89c80476e4SDavid E. O'Brien 
90c80476e4SDavid E. O'Brien #ifdef BSDTIMES
91c80476e4SDavid E. O'Brien # ifdef convex
92c80476e4SDavid E. O'Brien /* use 'cvxrusage' to get parallel statistics */
93c80476e4SDavid E. O'Brien static struct cvxrusage zru = {{0L, 0L}, {0L, 0L}, 0L, 0L, 0L, 0L,
94c80476e4SDavid E. O'Brien 				0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
95c80476e4SDavid E. O'Brien 				{0L, 0L}, 0LL, 0LL, 0LL, 0LL, 0L, 0L, 0L,
96c80476e4SDavid E. O'Brien 				0LL, 0LL, {0L, 0L, 0L, 0L, 0L}};
97c80476e4SDavid E. O'Brien # else
98c80476e4SDavid E. O'Brien static struct rusage zru;
99c80476e4SDavid E. O'Brien # endif /* convex */
100c80476e4SDavid E. O'Brien #else /* !BSDTIMES */
101c80476e4SDavid E. O'Brien # ifdef _SEQUENT_
102c80476e4SDavid E. O'Brien static struct process_stats zru = {{0L, 0L}, {0L, 0L}, 0, 0, 0, 0, 0, 0, 0,
103c80476e4SDavid E. O'Brien 				   0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
104c80476e4SDavid E. O'Brien # else /* !_SEQUENT_ */
105c80476e4SDavid E. O'Brien #  ifdef _SX
106c80476e4SDavid E. O'Brien static struct tms zru = {0, 0, 0, 0}, lru = {0, 0, 0, 0};
107c80476e4SDavid E. O'Brien #  else	/* !_SX */
108c80476e4SDavid E. O'Brien static struct tms zru = {0L, 0L, 0L, 0L}, lru = {0L, 0L, 0L, 0L};
109c80476e4SDavid E. O'Brien #  endif	/* !_SX */
110c80476e4SDavid E. O'Brien # endif	/* !_SEQUENT_ */
111c80476e4SDavid E. O'Brien #endif /* !BSDTIMES */
112c80476e4SDavid E. O'Brien 
11345e5710bSMark Peek #ifndef BSDTIMES
11445e5710bSMark Peek static int timesdone;	/* shtimes buffer full ? */
11545e5710bSMark Peek #endif /* BSDTIMES */
11645e5710bSMark Peek 
117c80476e4SDavid E. O'Brien #ifndef RUSAGE_CHILDREN
118c80476e4SDavid E. O'Brien # define	RUSAGE_CHILDREN	-1
119c80476e4SDavid E. O'Brien #endif /* RUSAGE_CHILDREN */
120c80476e4SDavid E. O'Brien 
12145e5710bSMark Peek static	void		 pflushall	(void);
12245e5710bSMark Peek static	void		 pflush		(struct process *);
12345e5710bSMark Peek static	void		 pfree		(struct process *);
12445e5710bSMark Peek static	void		 pclrcurr	(struct process *);
125a15e6f9aSMark Peek static	void		 morecommand	(size_t);
12645e5710bSMark Peek static	void		 padd		(struct command *);
12745e5710bSMark Peek static	int		 pprint		(struct process *, int);
12845e5710bSMark Peek static	void		 ptprint	(struct process *);
12945e5710bSMark Peek static	void		 pads		(Char *);
13045e5710bSMark Peek static	void		 pkill		(Char **, int);
13145e5710bSMark Peek static	struct process	*pgetcurr	(struct process *);
13245e5710bSMark Peek static	void		 okpcntl	(void);
13345e5710bSMark Peek static	void		 setttypgrp	(int);
134c80476e4SDavid E. O'Brien 
135c80476e4SDavid E. O'Brien /*
13645e5710bSMark Peek  * pchild - call queued by the SIGCHLD signal
137c80476e4SDavid E. O'Brien  *	indicating that at least one child has terminated or stopped
138c80476e4SDavid E. O'Brien  *	thus at least one wait system call will definitely return a
139c80476e4SDavid E. O'Brien  *	childs status.  Top level routines (like pwait) must be sure
140c80476e4SDavid E. O'Brien  *	to mask interrupts when playing with the proclist data structures!
141c80476e4SDavid E. O'Brien  */
14245e5710bSMark Peek void
pchild(void)14345e5710bSMark Peek pchild(void)
144c80476e4SDavid E. O'Brien {
14523338178SMark Peek     struct process *pp;
14623338178SMark Peek     struct process *fp;
14745e5710bSMark Peek     pid_t pid;
148c80476e4SDavid E. O'Brien #ifdef BSDWAIT
149c80476e4SDavid E. O'Brien     union wait w;
150c80476e4SDavid E. O'Brien #else /* !BSDWAIT */
151c80476e4SDavid E. O'Brien     int     w;
152c80476e4SDavid E. O'Brien #endif /* !BSDWAIT */
153c80476e4SDavid E. O'Brien     int     jobflags;
154c80476e4SDavid E. O'Brien #ifdef BSDTIMES
155c80476e4SDavid E. O'Brien     struct sysrusage ru;
156c80476e4SDavid E. O'Brien #else /* !BSDTIMES */
157c80476e4SDavid E. O'Brien # ifdef _SEQUENT_
158c80476e4SDavid E. O'Brien     struct process_stats ru;
159c80476e4SDavid E. O'Brien     struct process_stats cpst1, cpst2;
160c80476e4SDavid E. O'Brien     timeval_t tv;
161c80476e4SDavid E. O'Brien # else /* !_SEQUENT_ */
162c80476e4SDavid E. O'Brien     struct tms proctimes;
163c80476e4SDavid E. O'Brien 
164c80476e4SDavid E. O'Brien     if (!timesdone) {
165c80476e4SDavid E. O'Brien 	timesdone++;
166c80476e4SDavid E. O'Brien 	(void) times(&shtimes);
167c80476e4SDavid E. O'Brien     }
168c80476e4SDavid E. O'Brien # endif	/* !_SEQUENT_ */
169c80476e4SDavid E. O'Brien #endif /* !BSDTIMES */
170c80476e4SDavid E. O'Brien 
17145e5710bSMark Peek     jobdebug_xprintf(("pchild()\n"));
172c80476e4SDavid E. O'Brien 
173c80476e4SDavid E. O'Brien loop:
17445e5710bSMark Peek     jobdebug_xprintf(("Waiting...\n"));
17545e5710bSMark Peek     jobdebug_flush();
176c80476e4SDavid E. O'Brien     errno = 0;			/* reset, just in case */
17745e5710bSMark Peek 
1783b6eaa7bSAndrey A. Chernov #ifndef WINNT_NATIVE
179c80476e4SDavid E. O'Brien # ifdef BSDJOBS
180c80476e4SDavid E. O'Brien #  ifdef BSDTIMES
181c80476e4SDavid E. O'Brien #   ifdef convex
182c80476e4SDavid E. O'Brien     /* use 'cvxwait' to get parallel statistics */
183c80476e4SDavid E. O'Brien     pid = cvxwait(&w,
184c80476e4SDavid E. O'Brien         (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
185c80476e4SDavid E. O'Brien #   else
186c80476e4SDavid E. O'Brien     /* both a wait3 and rusage */
1879ccc37e3SMark Peek #    if !defined(BSDWAIT) || defined(NeXT) || defined(MACH) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || (defined(IRIS4D) && SYSVREL <= 3) || defined(__lucid) || defined(__osf__)
188*19d2e3deSDmitry Chagin #ifdef __ANDROID__ /* no wait3, only wait4 */
189*19d2e3deSDmitry Chagin     pid = wait4(-1, &w,
190*19d2e3deSDmitry Chagin        (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
191*19d2e3deSDmitry Chagin #else
192c80476e4SDavid E. O'Brien     pid = wait3(&w,
193c80476e4SDavid E. O'Brien        (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
194*19d2e3deSDmitry Chagin #endif /* __ANDROID__ */
195c80476e4SDavid E. O'Brien #    else /* BSDWAIT */
196c80476e4SDavid E. O'Brien     pid = wait3(&w.w_status,
197c80476e4SDavid E. O'Brien        (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
198c80476e4SDavid E. O'Brien #    endif /* BSDWAIT */
199c80476e4SDavid E. O'Brien #   endif /* convex */
200c80476e4SDavid E. O'Brien #  else /* !BSDTIMES */
201c80476e4SDavid E. O'Brien #   ifdef _SEQUENT_
202c80476e4SDavid E. O'Brien     (void) get_process_stats(&tv, PS_SELF, 0, &cpst1);
203c80476e4SDavid E. O'Brien     pid = waitpid(-1, &w,
204c80476e4SDavid E. O'Brien 	    (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
205c80476e4SDavid E. O'Brien     (void) get_process_stats(&tv, PS_SELF, 0, &cpst2);
206c80476e4SDavid E. O'Brien     pr_stat_sub(&cpst2, &cpst1, &ru);
207c80476e4SDavid E. O'Brien #   else	/* !_SEQUENT_ */
208c80476e4SDavid E. O'Brien #    ifndef POSIX
209c80476e4SDavid E. O'Brien     /* we have a wait3, but no rusage stuff */
210c80476e4SDavid E. O'Brien     pid = wait3(&w.w_status,
211c80476e4SDavid E. O'Brien 	 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), 0);
212c80476e4SDavid E. O'Brien #    else /* POSIX */
213c80476e4SDavid E. O'Brien     pid = waitpid(-1, &w,
214c80476e4SDavid E. O'Brien 	    (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
215c80476e4SDavid E. O'Brien #    endif /* POSIX */
216c80476e4SDavid E. O'Brien #   endif /* !_SEQUENT_ */
217c80476e4SDavid E. O'Brien #  endif	/* !BSDTIMES */
218c80476e4SDavid E. O'Brien # else /* !BSDJOBS */
219c80476e4SDavid E. O'Brien #  ifdef BSDTIMES
220c80476e4SDavid E. O'Brien #   define HAVEwait3
221c80476e4SDavid E. O'Brien     /* both a wait3 and rusage */
222c80476e4SDavid E. O'Brien #   ifdef hpux
223c80476e4SDavid E. O'Brien     pid = wait3(&w.w_status, WNOHANG, 0);
224c80476e4SDavid E. O'Brien #   else	/* !hpux */
2259ccc37e3SMark Peek #     ifndef BSDWAIT
2269ccc37e3SMark Peek     pid = wait3(&w, WNOHANG, &ru);
2279ccc37e3SMark Peek #     else
228c80476e4SDavid E. O'Brien     pid = wait3(&w.w_status, WNOHANG, &ru);
2299ccc37e3SMark Peek #     endif /* BSDWAIT */
230c80476e4SDavid E. O'Brien #   endif /* !hpux */
231c80476e4SDavid E. O'Brien #  else /* !BSDTIMES */
232c80476e4SDavid E. O'Brien #   ifdef ODT  /* For Sco Unix 3.2.0 or ODT 1.0 */
233c80476e4SDavid E. O'Brien #    define HAVEwait3
234c80476e4SDavid E. O'Brien     pid = waitpid(-1, &w,
235c80476e4SDavid E. O'Brien  	    (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
236c80476e4SDavid E. O'Brien #   endif /* ODT */
237c80476e4SDavid E. O'Brien #   if defined(aiws) || defined(uts)
238c80476e4SDavid E. O'Brien #    define HAVEwait3
239c80476e4SDavid E. O'Brien     pid = wait3(&w.w_status,
240c80476e4SDavid E. O'Brien 	(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), 0);
241c80476e4SDavid E. O'Brien #   endif /* aiws || uts */
242c80476e4SDavid E. O'Brien #   ifndef HAVEwait3
24345e5710bSMark Peek #    ifndef BSDWAIT
244c80476e4SDavid E. O'Brien      /* no wait3, therefore no rusage */
245c80476e4SDavid E. O'Brien      /* on Sys V, this may hang.  I hope it's not going to be a problem */
246c80476e4SDavid E. O'Brien     pid = wait(&w);
24745e5710bSMark Peek #    else /* BSDWAIT */
248c80476e4SDavid E. O'Brien      /*
249c80476e4SDavid E. O'Brien       * XXX: for greater than 3 we should use waitpid().
250c80476e4SDavid E. O'Brien       * but then again, SVR4 falls into the POSIX/BSDJOBS category.
251c80476e4SDavid E. O'Brien       */
252c80476e4SDavid E. O'Brien     pid = wait(&w.w_status);
25345e5710bSMark Peek #    endif /* BSDWAIT */
254c80476e4SDavid E. O'Brien #   endif /* !HAVEwait3 */
255c80476e4SDavid E. O'Brien #  endif	/* !BSDTIMES */
256c80476e4SDavid E. O'Brien # endif /* !BSDJOBS */
2573b6eaa7bSAndrey A. Chernov #else /* WINNT_NATIVE */
258c80476e4SDavid E. O'Brien     pid = waitpid(-1, &w,
259c80476e4SDavid E. O'Brien 	    (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
2603b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
261c80476e4SDavid E. O'Brien 
26245e5710bSMark Peek     jobdebug_xprintf(("parent %d pid %d, retval %x termsig %x retcode %x\n",
26345e5710bSMark Peek 		      (int)getpid(), (int)pid, w, WTERMSIG(w),
26445e5710bSMark Peek 		      WEXITSTATUS(w)));
26545e5710bSMark Peek     jobdebug_flush();
266c80476e4SDavid E. O'Brien 
267c80476e4SDavid E. O'Brien     if ((pid == 0) || (pid == -1)) {
2689ccc37e3SMark Peek 	(void)handle_pending_signals();
26945e5710bSMark Peek 	jobdebug_xprintf(("errno == %d\n", errno));
27045e5710bSMark Peek 	if (errno == EINTR)
271c80476e4SDavid E. O'Brien 	    goto loop;
27223338178SMark Peek 	goto end;
273c80476e4SDavid E. O'Brien     }
274c80476e4SDavid E. O'Brien     for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
275c80476e4SDavid E. O'Brien 	if (pid == pp->p_procid)
276c80476e4SDavid E. O'Brien 	    goto found;
2773b6eaa7bSAndrey A. Chernov #if !defined(BSDJOBS) && !defined(WINNT_NATIVE)
278c80476e4SDavid E. O'Brien     /* this should never have happened */
279c80476e4SDavid E. O'Brien     stderror(ERR_SYNC, pid);
280c80476e4SDavid E. O'Brien     xexit(0);
2813b6eaa7bSAndrey A. Chernov #else /* BSDJOBS || WINNT_NATIVE */
282c80476e4SDavid E. O'Brien     goto loop;
2833b6eaa7bSAndrey A. Chernov #endif /* !BSDJOBS && !WINNT_NATIVE */
284c80476e4SDavid E. O'Brien found:
285c80476e4SDavid E. O'Brien     pp->p_flags &= ~(PRUNNING | PSTOPPED | PREPORTED);
286c80476e4SDavid E. O'Brien     if (WIFSTOPPED(w)) {
287c80476e4SDavid E. O'Brien 	pp->p_flags |= PSTOPPED;
288c80476e4SDavid E. O'Brien 	pp->p_reason = WSTOPSIG(w);
289c80476e4SDavid E. O'Brien     }
290c80476e4SDavid E. O'Brien     else {
291c80476e4SDavid E. O'Brien 	if (pp->p_flags & (PTIME | PPTIME) || adrof(STRtime))
292c80476e4SDavid E. O'Brien #ifndef BSDTIMES
293c80476e4SDavid E. O'Brien # ifdef _SEQUENT_
294c80476e4SDavid E. O'Brien 	    (void) get_process_stats(&pp->p_etime, PS_SELF, NULL, NULL);
295c80476e4SDavid E. O'Brien # else	/* !_SEQUENT_ */
296c80476e4SDavid E. O'Brien 	    pp->p_etime = times(&proctimes);
297c80476e4SDavid E. O'Brien # endif	/* !_SEQUENT_ */
298c80476e4SDavid E. O'Brien #else /* BSDTIMES */
299c80476e4SDavid E. O'Brien 	    (void) gettimeofday(&pp->p_etime, NULL);
300c80476e4SDavid E. O'Brien #endif /* BSDTIMES */
301c80476e4SDavid E. O'Brien 
302c80476e4SDavid E. O'Brien 
303c80476e4SDavid E. O'Brien #if defined(BSDTIMES) || defined(_SEQUENT_)
304c80476e4SDavid E. O'Brien 	pp->p_rusage = ru;
305c80476e4SDavid E. O'Brien #else /* !BSDTIMES && !_SEQUENT_ */
306c80476e4SDavid E. O'Brien 	(void) times(&proctimes);
307c80476e4SDavid E. O'Brien 	pp->p_utime = proctimes.tms_cutime - shtimes.tms_cutime;
308c80476e4SDavid E. O'Brien 	pp->p_stime = proctimes.tms_cstime - shtimes.tms_cstime;
309c80476e4SDavid E. O'Brien 	shtimes = proctimes;
310c80476e4SDavid E. O'Brien #endif /* !BSDTIMES && !_SEQUENT_ */
311c80476e4SDavid E. O'Brien 	if (WIFSIGNALED(w)) {
312c80476e4SDavid E. O'Brien 	    if (WTERMSIG(w) == SIGINT)
313c80476e4SDavid E. O'Brien 		pp->p_flags |= PINTERRUPTED;
314c80476e4SDavid E. O'Brien 	    else
315c80476e4SDavid E. O'Brien 		pp->p_flags |= PSIGNALED;
316c80476e4SDavid E. O'Brien 	    if (WCOREDUMP(w))
317c80476e4SDavid E. O'Brien 		pp->p_flags |= PDUMPED;
318c80476e4SDavid E. O'Brien 	    pp->p_reason = WTERMSIG(w);
319c80476e4SDavid E. O'Brien 	}
320c80476e4SDavid E. O'Brien 	else {
321c80476e4SDavid E. O'Brien 	    pp->p_reason = WEXITSTATUS(w);
322c80476e4SDavid E. O'Brien 	    if (pp->p_reason != 0)
323c80476e4SDavid E. O'Brien 		pp->p_flags |= PAEXITED;
324c80476e4SDavid E. O'Brien 	    else
325c80476e4SDavid E. O'Brien 		pp->p_flags |= PNEXITED;
326c80476e4SDavid E. O'Brien 	}
327c80476e4SDavid E. O'Brien     }
328c80476e4SDavid E. O'Brien     jobflags = 0;
329c80476e4SDavid E. O'Brien     fp = pp;
330c80476e4SDavid E. O'Brien     do {
331c80476e4SDavid E. O'Brien 	if ((fp->p_flags & (PPTIME | PRUNNING | PSTOPPED)) == 0 &&
332c80476e4SDavid E. O'Brien 	    !child && adrof(STRtime) &&
333c80476e4SDavid E. O'Brien #ifdef BSDTIMES
334c80476e4SDavid E. O'Brien 	    fp->p_rusage.ru_utime.tv_sec + fp->p_rusage.ru_stime.tv_sec
335c80476e4SDavid E. O'Brien #else /* !BSDTIMES */
336c80476e4SDavid E. O'Brien # ifdef _SEQUENT_
337c80476e4SDavid E. O'Brien 	    fp->p_rusage.ps_utime.tv_sec + fp->p_rusage.ps_stime.tv_sec
338c80476e4SDavid E. O'Brien # else /* !_SEQUENT_ */
339c80476e4SDavid E. O'Brien #  ifndef POSIX
340c80476e4SDavid E. O'Brien 	    (fp->p_utime + fp->p_stime) / HZ
341c80476e4SDavid E. O'Brien #  else /* POSIX */
342c80476e4SDavid E. O'Brien 	    (fp->p_utime + fp->p_stime) / clk_tck
343c80476e4SDavid E. O'Brien #  endif /* POSIX */
344c80476e4SDavid E. O'Brien # endif /* !_SEQUENT_ */
345c80476e4SDavid E. O'Brien #endif /* !BSDTIMES */
346c80476e4SDavid E. O'Brien 	    >= atoi(short2str(varval(STRtime))))
347c80476e4SDavid E. O'Brien 	    fp->p_flags |= PTIME;
348c80476e4SDavid E. O'Brien 	jobflags |= fp->p_flags;
349c80476e4SDavid E. O'Brien     } while ((fp = fp->p_friends) != pp);
350c80476e4SDavid E. O'Brien     pp->p_flags &= ~PFOREGND;
351c80476e4SDavid E. O'Brien     if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
352c80476e4SDavid E. O'Brien 	pp->p_flags &= ~PPTIME;
353c80476e4SDavid E. O'Brien 	pp->p_flags |= PTIME;
354c80476e4SDavid E. O'Brien     }
355c80476e4SDavid E. O'Brien     if ((jobflags & (PRUNNING | PREPORTED)) == 0) {
356c80476e4SDavid E. O'Brien 	fp = pp;
357c80476e4SDavid E. O'Brien 	do {
358c80476e4SDavid E. O'Brien 	    if (fp->p_flags & PSTOPPED)
359c80476e4SDavid E. O'Brien 		fp->p_flags |= PREPORTED;
360c80476e4SDavid E. O'Brien 	} while ((fp = fp->p_friends) != pp);
361c80476e4SDavid E. O'Brien 	while (fp->p_procid != fp->p_jobid)
362c80476e4SDavid E. O'Brien 	    fp = fp->p_friends;
363c80476e4SDavid E. O'Brien 	if (jobflags & PSTOPPED) {
364c80476e4SDavid E. O'Brien 	    if (pcurrent && pcurrent != fp)
365c80476e4SDavid E. O'Brien 		pprevious = pcurrent;
366c80476e4SDavid E. O'Brien 	    pcurrent = fp;
367c80476e4SDavid E. O'Brien 	}
368c80476e4SDavid E. O'Brien 	else
369c80476e4SDavid E. O'Brien 	    pclrcurr(fp);
370c80476e4SDavid E. O'Brien 	if (jobflags & PFOREGND) {
371c80476e4SDavid E. O'Brien 	    if (!(jobflags & (PSIGNALED | PSTOPPED | PPTIME) ||
372c80476e4SDavid E. O'Brien #ifdef notdef
373c80476e4SDavid E. O'Brien 		jobflags & PAEXITED ||
374c80476e4SDavid E. O'Brien #endif /* notdef */
3759ccc37e3SMark Peek 		fp->p_cwd == NULL ||
376c80476e4SDavid E. O'Brien 		!eq(dcwd->di_name, fp->p_cwd->di_name))) {
377c80476e4SDavid E. O'Brien 	    /* PWP: print a newline after ^C */
378c80476e4SDavid E. O'Brien 		if (jobflags & PINTERRUPTED) {
37945e5710bSMark Peek 		    xputchar('\r' | QUOTE);
38045e5710bSMark Peek 		    xputchar('\n');
381c80476e4SDavid E. O'Brien 		}
382c80476e4SDavid E. O'Brien #ifdef notdef
383c80476e4SDavid E. O'Brien 		else if ((jobflags & (PTIME|PSTOPPED)) == PTIME)
384c80476e4SDavid E. O'Brien 		    ptprint(fp);
385c80476e4SDavid E. O'Brien #endif /* notdef */
386c80476e4SDavid E. O'Brien 	    }
387c80476e4SDavid E. O'Brien 	}
388c80476e4SDavid E. O'Brien 	else {
389c80476e4SDavid E. O'Brien 	    if (jobflags & PNOTIFY || adrof(STRnotify)) {
39045e5710bSMark Peek 	        xputchar('\r' | QUOTE);
39145e5710bSMark Peek 		xputchar('\n');
392c80476e4SDavid E. O'Brien 		(void) pprint(pp, NUMBER | NAME | REASON);
393c80476e4SDavid E. O'Brien 		if ((jobflags & PSTOPPED) == 0)
394c80476e4SDavid E. O'Brien 		    pflush(pp);
395c80476e4SDavid E. O'Brien 		if (GettingInput) {
396c80476e4SDavid E. O'Brien 		    errno = 0;
397c80476e4SDavid E. O'Brien 		    (void) Rawmode();
398c80476e4SDavid E. O'Brien #ifdef notdef
399c80476e4SDavid E. O'Brien 		    /*
400c80476e4SDavid E. O'Brien 		     * don't really want to do that, because it
401c80476e4SDavid E. O'Brien 		     * will erase our message in case of multi-line
402c80476e4SDavid E. O'Brien 		     * input
403c80476e4SDavid E. O'Brien 		     */
404c80476e4SDavid E. O'Brien 		    ClearLines();
405c80476e4SDavid E. O'Brien #endif /* notdef */
406c80476e4SDavid E. O'Brien 		    ClearDisp();
407c80476e4SDavid E. O'Brien 		    Refresh();
408c80476e4SDavid E. O'Brien 		}
409c80476e4SDavid E. O'Brien 	    }
410c80476e4SDavid E. O'Brien 	    else {
411c80476e4SDavid E. O'Brien 		fp->p_flags |= PNEEDNOTE;
41245e5710bSMark Peek 		neednote = 1;
413c80476e4SDavid E. O'Brien 	    }
414c80476e4SDavid E. O'Brien 	}
415c80476e4SDavid E. O'Brien     }
41645e5710bSMark Peek #if defined(BSDJOBS) || defined(HAVEwait3) ||defined(WINNT_NATIVE)
417c80476e4SDavid E. O'Brien     goto loop;
418c80476e4SDavid E. O'Brien #endif /* BSDJOBS || HAVEwait3 */
41923338178SMark Peek  end:
42023338178SMark Peek     ;
421c80476e4SDavid E. O'Brien }
422c80476e4SDavid E. O'Brien 
423c80476e4SDavid E. O'Brien void
pnote(void)42445e5710bSMark Peek pnote(void)
425c80476e4SDavid E. O'Brien {
42623338178SMark Peek     struct process *pp;
427c80476e4SDavid E. O'Brien     int     flags;
428c80476e4SDavid E. O'Brien 
429c80476e4SDavid E. O'Brien     neednote = 0;
430c80476e4SDavid E. O'Brien     for (pp = proclist.p_next; pp != NULL; pp = pp->p_next) {
431c80476e4SDavid E. O'Brien 	if (pp->p_flags & PNEEDNOTE) {
43245e5710bSMark Peek 	    pchild_disabled++;
43345e5710bSMark Peek 	    cleanup_push(&pchild_disabled, disabled_cleanup);
434c80476e4SDavid E. O'Brien 	    pp->p_flags &= ~PNEEDNOTE;
435c80476e4SDavid E. O'Brien 	    flags = pprint(pp, NUMBER | NAME | REASON);
436c80476e4SDavid E. O'Brien 	    if ((flags & (PRUNNING | PSTOPPED)) == 0)
437c80476e4SDavid E. O'Brien 		pflush(pp);
43845e5710bSMark Peek 	    cleanup_until(&pchild_disabled);
439c80476e4SDavid E. O'Brien 	}
440c80476e4SDavid E. O'Brien     }
441c80476e4SDavid E. O'Brien }
442c80476e4SDavid E. O'Brien 
443c80476e4SDavid E. O'Brien 
444c80476e4SDavid E. O'Brien static void
pfree(struct process * pp)44545e5710bSMark Peek pfree(struct process *pp)
446c80476e4SDavid E. O'Brien {
44745e5710bSMark Peek     xfree(pp->p_command);
448c80476e4SDavid E. O'Brien     if (pp->p_cwd && --pp->p_cwd->di_count == 0)
449c80476e4SDavid E. O'Brien 	if (pp->p_cwd->di_next == 0)
450c80476e4SDavid E. O'Brien 	    dfree(pp->p_cwd);
45145e5710bSMark Peek     xfree(pp);
452c80476e4SDavid E. O'Brien }
453c80476e4SDavid E. O'Brien 
454c80476e4SDavid E. O'Brien 
455c80476e4SDavid E. O'Brien /*
456c80476e4SDavid E. O'Brien  * pwait - wait for current job to terminate, maintaining integrity
457c80476e4SDavid E. O'Brien  *	of current and previous job indicators.
458c80476e4SDavid E. O'Brien  */
459c80476e4SDavid E. O'Brien void
pwait(void)46045e5710bSMark Peek pwait(void)
461c80476e4SDavid E. O'Brien {
46223338178SMark Peek     struct process *fp, *pp;
463c80476e4SDavid E. O'Brien 
464c80476e4SDavid E. O'Brien     /*
465c80476e4SDavid E. O'Brien      * Here's where dead procs get flushed.
466c80476e4SDavid E. O'Brien      */
467c80476e4SDavid E. O'Brien     for (pp = (fp = &proclist)->p_next; pp != NULL; pp = (fp = pp)->p_next)
468c80476e4SDavid E. O'Brien 	if (pp->p_procid == 0) {
469c80476e4SDavid E. O'Brien 	    fp->p_next = pp->p_next;
470c80476e4SDavid E. O'Brien 	    pfree(pp);
471c80476e4SDavid E. O'Brien 	    pp = fp;
472c80476e4SDavid E. O'Brien 	}
473c80476e4SDavid E. O'Brien     pjwait(pcurrjob);
474c80476e4SDavid E. O'Brien }
475c80476e4SDavid E. O'Brien 
476c80476e4SDavid E. O'Brien 
477c80476e4SDavid E. O'Brien /*
478c80476e4SDavid E. O'Brien  * pjwait - wait for a job to finish or become stopped
479c80476e4SDavid E. O'Brien  *	It is assumed to be in the foreground state (PFOREGND)
480c80476e4SDavid E. O'Brien  */
481c80476e4SDavid E. O'Brien void
pjwait(struct process * pp)48245e5710bSMark Peek pjwait(struct process *pp)
483c80476e4SDavid E. O'Brien {
48423338178SMark Peek     struct process *fp;
485c80476e4SDavid E. O'Brien     int     jobflags, reason;
48645e5710bSMark Peek     sigset_t oset, set, pause_mask;
48745e5710bSMark Peek     Char *reason_str;
48845e5710bSMark Peek 
489c80476e4SDavid E. O'Brien     while (pp->p_procid != pp->p_jobid)
490c80476e4SDavid E. O'Brien 	pp = pp->p_friends;
491c80476e4SDavid E. O'Brien     fp = pp;
492c80476e4SDavid E. O'Brien 
493c80476e4SDavid E. O'Brien     do {
494c80476e4SDavid E. O'Brien 	if ((fp->p_flags & (PFOREGND | PRUNNING)) == PRUNNING)
495a15e6f9aSMark Peek 	  xprintf("%s", CGETS(17, 1, "BUG: waiting for background job!\n"));
496c80476e4SDavid E. O'Brien     } while ((fp = fp->p_friends) != pp);
497c80476e4SDavid E. O'Brien     /*
498c80476e4SDavid E. O'Brien      * Now keep pausing as long as we are not interrupted (SIGINT), and the
499c80476e4SDavid E. O'Brien      * target process, or any of its friends, are running
500c80476e4SDavid E. O'Brien      */
501c80476e4SDavid E. O'Brien     fp = pp;
50245e5710bSMark Peek     sigemptyset(&set);
50345e5710bSMark Peek     sigaddset(&set, SIGINT);
50445e5710bSMark Peek     sigaddset(&set, SIGCHLD);
50545e5710bSMark Peek     (void)sigprocmask(SIG_BLOCK, &set, &oset);
50645e5710bSMark Peek     cleanup_push(&oset, sigprocmask_cleanup);
50745e5710bSMark Peek     pause_mask = oset;
50845e5710bSMark Peek     sigdelset(&pause_mask, SIGCHLD);
509*19d2e3deSDmitry Chagin     sigaddset(&pause_mask, SIGINT);
510c80476e4SDavid E. O'Brien     for (;;) {
5119ccc37e3SMark Peek 	(void)handle_pending_signals();
512c80476e4SDavid E. O'Brien 	jobflags = 0;
513c80476e4SDavid E. O'Brien 	do
514c80476e4SDavid E. O'Brien 	    jobflags |= fp->p_flags;
515c80476e4SDavid E. O'Brien 	while ((fp = (fp->p_friends)) != pp);
516c80476e4SDavid E. O'Brien 	if ((jobflags & PRUNNING) == 0)
517c80476e4SDavid E. O'Brien 	    break;
51845e5710bSMark Peek 	jobdebug_xprintf(("%d starting to sigsuspend for SIGCHLD on %d\n",
51945e5710bSMark Peek 			  getpid(), fp->p_procid));
52045e5710bSMark Peek 	sigsuspend(&pause_mask);
521c80476e4SDavid E. O'Brien     }
52245e5710bSMark Peek     cleanup_until(&oset);
52345e5710bSMark Peek     jobdebug_xprintf(("%d returned from sigsuspend loop\n", getpid()));
524c80476e4SDavid E. O'Brien #ifdef BSDJOBS
525c80476e4SDavid E. O'Brien     if (tpgrp > 0)		/* get tty back */
526c80476e4SDavid E. O'Brien 	(void) tcsetpgrp(FSHTTY, tpgrp);
527c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
528c80476e4SDavid E. O'Brien     if ((jobflags & (PSIGNALED | PSTOPPED | PTIME)) ||
529a15e6f9aSMark Peek 	fp->p_cwd == NULL || !eq(dcwd->di_name, fp->p_cwd->di_name)) {
530c80476e4SDavid E. O'Brien 	if (jobflags & PSTOPPED) {
531c80476e4SDavid E. O'Brien 	    xputchar('\n');
532c80476e4SDavid E. O'Brien 	    if (adrof(STRlistjobs)) {
533c80476e4SDavid E. O'Brien 		Char   *jobcommand[3];
534c80476e4SDavid E. O'Brien 
535c80476e4SDavid E. O'Brien 		jobcommand[0] = STRjobs;
536c80476e4SDavid E. O'Brien 		if (eq(varval(STRlistjobs), STRlong))
537c80476e4SDavid E. O'Brien 		    jobcommand[1] = STRml;
538c80476e4SDavid E. O'Brien 		else
539c80476e4SDavid E. O'Brien 		    jobcommand[1] = NULL;
540c80476e4SDavid E. O'Brien 		jobcommand[2] = NULL;
541c80476e4SDavid E. O'Brien 
542c80476e4SDavid E. O'Brien 		dojobs(jobcommand, NULL);
543c80476e4SDavid E. O'Brien 		(void) pprint(pp, SHELLDIR);
544c80476e4SDavid E. O'Brien 	    }
545c80476e4SDavid E. O'Brien 	    else
546c80476e4SDavid E. O'Brien 		(void) pprint(pp, AREASON | SHELLDIR);
547c80476e4SDavid E. O'Brien 	}
548c80476e4SDavid E. O'Brien 	else
549c80476e4SDavid E. O'Brien 	    (void) pprint(pp, AREASON | SHELLDIR);
550c80476e4SDavid E. O'Brien     }
551c80476e4SDavid E. O'Brien     if ((jobflags & (PINTERRUPTED | PSTOPPED)) && setintr &&
552c80476e4SDavid E. O'Brien 	(!gointr || !eq(gointr, STRminus))) {
553c80476e4SDavid E. O'Brien 	if ((jobflags & PSTOPPED) == 0)
554c80476e4SDavid E. O'Brien 	    pflush(pp);
555c80476e4SDavid E. O'Brien 	pintr1(0);
556c80476e4SDavid E. O'Brien 	/* NOTREACHED */
557c80476e4SDavid E. O'Brien     }
558c80476e4SDavid E. O'Brien     reason = 0;
559c80476e4SDavid E. O'Brien     fp = pp;
560c80476e4SDavid E. O'Brien     do {
5619ccc37e3SMark Peek 	/* In case of pipelines only the result of the last
5629ccc37e3SMark Peek 	 * command should be taken in account */
5639ccc37e3SMark Peek 	if (!anyerror && !(fp->p_flags & PBRACE)
5649ccc37e3SMark Peek 		&& ((fp->p_flags & PPOU) || (fp->p_flags & PBACKQ)))
5659ccc37e3SMark Peek 	    continue;
566c80476e4SDavid E. O'Brien 	if (fp->p_reason)
567c80476e4SDavid E. O'Brien 	    reason = fp->p_flags & (PSIGNALED | PINTERRUPTED) ?
568c80476e4SDavid E. O'Brien 		fp->p_reason | META : fp->p_reason;
569c80476e4SDavid E. O'Brien     } while ((fp = fp->p_friends) != pp);
570c80476e4SDavid E. O'Brien     /*
571c80476e4SDavid E. O'Brien      * Don't report on backquoted jobs, cause it will mess up
572c80476e4SDavid E. O'Brien      * their output.
573c80476e4SDavid E. O'Brien      */
574c80476e4SDavid E. O'Brien     if ((reason != 0) && (adrof(STRprintexitvalue)) &&
575c80476e4SDavid E. O'Brien 	(pp->p_flags & PBACKQ) == 0)
576c80476e4SDavid E. O'Brien 	xprintf(CGETS(17, 2, "Exit %d\n"), reason);
5779ccc37e3SMark Peek     reason_str = putn((tcsh_number_t)reason);
57845e5710bSMark Peek     cleanup_push(reason_str, xfree);
57945e5710bSMark Peek     setv(STRstatus, reason_str, VAR_READWRITE);
58045e5710bSMark Peek     cleanup_ignore(reason_str);
58145e5710bSMark Peek     cleanup_until(reason_str);
582c80476e4SDavid E. O'Brien     if (reason && exiterr)
583c80476e4SDavid E. O'Brien 	exitstat();
584c80476e4SDavid E. O'Brien     pflush(pp);
585c80476e4SDavid E. O'Brien }
586c80476e4SDavid E. O'Brien 
587c80476e4SDavid E. O'Brien /*
588c80476e4SDavid E. O'Brien  * dowait - wait for all processes to finish
589c80476e4SDavid E. O'Brien  */
590c80476e4SDavid E. O'Brien 
591c80476e4SDavid E. O'Brien /*ARGSUSED*/
592c80476e4SDavid E. O'Brien void
dowait(Char ** v,struct command * c)59345e5710bSMark Peek dowait(Char **v, struct command *c)
594c80476e4SDavid E. O'Brien {
59523338178SMark Peek     struct process *pp;
596*19d2e3deSDmitry Chagin 
597*19d2e3deSDmitry Chagin     /* the current block mask to be able to restore */
598*19d2e3deSDmitry Chagin     sigset_t old_mask;
599*19d2e3deSDmitry Chagin 
600*19d2e3deSDmitry Chagin     /* block mask for critical section: OLD_MASK U {SIGCHLD} */
601*19d2e3deSDmitry Chagin     sigset_t block_mask;
602*19d2e3deSDmitry Chagin 
603*19d2e3deSDmitry Chagin     /* ignore those during blocking sigsuspend:
604*19d2e3deSDmitry Chagin        OLD_MASK / {SIGCHLD, possibly(SIGINT)} */
60545e5710bSMark Peek     sigset_t pause_mask;
606*19d2e3deSDmitry Chagin 
6079ccc37e3SMark Peek     int opintr_disabled, gotsig;
608c80476e4SDavid E. O'Brien 
609c80476e4SDavid E. O'Brien     USE(c);
610c80476e4SDavid E. O'Brien     USE(v);
611c80476e4SDavid E. O'Brien     pjobs++;
612*19d2e3deSDmitry Chagin 
61345e5710bSMark Peek     sigprocmask(SIG_BLOCK, NULL, &pause_mask);
61445e5710bSMark Peek     sigdelset(&pause_mask, SIGCHLD);
615c80476e4SDavid E. O'Brien     if (setintr)
61645e5710bSMark Peek 	sigdelset(&pause_mask, SIGINT);
617*19d2e3deSDmitry Chagin 
618*19d2e3deSDmitry Chagin     /* critical section, block also SIGCHLD */
619*19d2e3deSDmitry Chagin     sigprocmask(SIG_BLOCK, NULL, &block_mask);
620*19d2e3deSDmitry Chagin     sigaddset(&block_mask, SIGCHLD);
621*19d2e3deSDmitry Chagin     sigprocmask(SIG_BLOCK, &block_mask, &old_mask);
622*19d2e3deSDmitry Chagin 
623*19d2e3deSDmitry Chagin     /* detect older SIGCHLDs and remove PRUNNING flag from proclist */
624*19d2e3deSDmitry Chagin     (void)handle_pending_signals();
625*19d2e3deSDmitry Chagin 
626c80476e4SDavid E. O'Brien loop:
627c80476e4SDavid E. O'Brien     for (pp = proclist.p_next; pp; pp = pp->p_next)
628c80476e4SDavid E. O'Brien 	if (pp->p_procid &&	/* pp->p_procid == pp->p_jobid && */
629c80476e4SDavid E. O'Brien 	    pp->p_flags & PRUNNING) {
630*19d2e3deSDmitry Chagin 	    /* wait for (or pick up alredy blocked) SIGCHLD */
63145e5710bSMark Peek 	    sigsuspend(&pause_mask);
632*19d2e3deSDmitry Chagin 
633*19d2e3deSDmitry Chagin 	    /* make the 'wait' interuptable by CTRL-C */
6349ccc37e3SMark Peek 	    opintr_disabled = pintr_disabled;
6359ccc37e3SMark Peek 	    pintr_disabled = 0;
6369ccc37e3SMark Peek 	    gotsig = handle_pending_signals();
6379ccc37e3SMark Peek 	    pintr_disabled = opintr_disabled;
6389ccc37e3SMark Peek 	    if (gotsig)
6399ccc37e3SMark Peek 		break;
640c80476e4SDavid E. O'Brien 	    goto loop;
641c80476e4SDavid E. O'Brien 	}
642c80476e4SDavid E. O'Brien     pjobs = 0;
643*19d2e3deSDmitry Chagin 
644*19d2e3deSDmitry Chagin     sigprocmask(SIG_SETMASK, &old_mask, NULL);
645c80476e4SDavid E. O'Brien }
646c80476e4SDavid E. O'Brien 
647c80476e4SDavid E. O'Brien /*
648c80476e4SDavid E. O'Brien  * pflushall - flush all jobs from list (e.g. at fork())
649c80476e4SDavid E. O'Brien  */
650c80476e4SDavid E. O'Brien static void
pflushall(void)65145e5710bSMark Peek pflushall(void)
652c80476e4SDavid E. O'Brien {
65323338178SMark Peek     struct process *pp;
654c80476e4SDavid E. O'Brien 
655c80476e4SDavid E. O'Brien     for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
656c80476e4SDavid E. O'Brien 	if (pp->p_procid)
657c80476e4SDavid E. O'Brien 	    pflush(pp);
658c80476e4SDavid E. O'Brien }
659c80476e4SDavid E. O'Brien 
660c80476e4SDavid E. O'Brien /*
661c80476e4SDavid E. O'Brien  * pflush - flag all process structures in the same job as the
662c80476e4SDavid E. O'Brien  *	the argument process for deletion.  The actual free of the
663c80476e4SDavid E. O'Brien  *	space is not done here since pflush is called at interrupt level.
664c80476e4SDavid E. O'Brien  */
665c80476e4SDavid E. O'Brien static void
pflush(struct process * pp)66645e5710bSMark Peek pflush(struct process *pp)
667c80476e4SDavid E. O'Brien {
66823338178SMark Peek     struct process *np;
66923338178SMark Peek     int idx;
670c80476e4SDavid E. O'Brien 
671c80476e4SDavid E. O'Brien     if (pp->p_procid == 0) {
672a15e6f9aSMark Peek 	xprintf("%s", CGETS(17, 3, "BUG: process flushed twice"));
673c80476e4SDavid E. O'Brien 	return;
674c80476e4SDavid E. O'Brien     }
675c80476e4SDavid E. O'Brien     while (pp->p_procid != pp->p_jobid)
676c80476e4SDavid E. O'Brien 	pp = pp->p_friends;
677c80476e4SDavid E. O'Brien     pclrcurr(pp);
678c80476e4SDavid E. O'Brien     if (pp == pcurrjob)
679c80476e4SDavid E. O'Brien 	pcurrjob = 0;
680c80476e4SDavid E. O'Brien     idx = pp->p_index;
681c80476e4SDavid E. O'Brien     np = pp;
682c80476e4SDavid E. O'Brien     do {
683c80476e4SDavid E. O'Brien 	np->p_index = np->p_procid = 0;
684c80476e4SDavid E. O'Brien 	np->p_flags &= ~PNEEDNOTE;
685c80476e4SDavid E. O'Brien     } while ((np = np->p_friends) != pp);
686c80476e4SDavid E. O'Brien     if (idx == pmaxindex) {
687c80476e4SDavid E. O'Brien 	for (np = proclist.p_next, idx = 0; np; np = np->p_next)
688c80476e4SDavid E. O'Brien 	    if (np->p_index > idx)
689c80476e4SDavid E. O'Brien 		idx = np->p_index;
690c80476e4SDavid E. O'Brien 	pmaxindex = idx;
691c80476e4SDavid E. O'Brien     }
692c80476e4SDavid E. O'Brien }
693c80476e4SDavid E. O'Brien 
694c80476e4SDavid E. O'Brien /*
695c80476e4SDavid E. O'Brien  * pclrcurr - make sure the given job is not the current or previous job;
696c80476e4SDavid E. O'Brien  *	pp MUST be the job leader
697c80476e4SDavid E. O'Brien  */
698c80476e4SDavid E. O'Brien static void
pclrcurr(struct process * pp)69945e5710bSMark Peek pclrcurr(struct process *pp)
700c80476e4SDavid E. O'Brien {
701c80476e4SDavid E. O'Brien     if (pp == pcurrent) {
702c80476e4SDavid E. O'Brien 	if (pprevious != NULL) {
703c80476e4SDavid E. O'Brien 	    pcurrent = pprevious;
704c80476e4SDavid E. O'Brien 	    pprevious = pgetcurr(pp);
705c80476e4SDavid E. O'Brien 	}
706c80476e4SDavid E. O'Brien 	else {
707c80476e4SDavid E. O'Brien 	    pcurrent = pgetcurr(pp);
708c80476e4SDavid E. O'Brien 	    pprevious = pgetcurr(pp);
709c80476e4SDavid E. O'Brien 	}
710c80476e4SDavid E. O'Brien     }
711c80476e4SDavid E. O'Brien     else if (pp == pprevious)
712c80476e4SDavid E. O'Brien 	pprevious = pgetcurr(pp);
713c80476e4SDavid E. O'Brien }
714c80476e4SDavid E. O'Brien 
715c80476e4SDavid E. O'Brien /* +4 here is 1 for '\0', 1 ea for << >& >> */
716a15e6f9aSMark Peek static Char *cmdstr;
717a15e6f9aSMark Peek static size_t cmdmax;
71845e5710bSMark Peek static size_t cmdlen;
719c80476e4SDavid E. O'Brien static Char *cmdp;
720a15e6f9aSMark Peek #define CMD_INIT 1024
721a15e6f9aSMark Peek #define CMD_INCR 64
722a15e6f9aSMark Peek 
723a15e6f9aSMark Peek static void
morecommand(size_t s)724a15e6f9aSMark Peek morecommand(size_t s)
725a15e6f9aSMark Peek {
726a15e6f9aSMark Peek     Char *ncmdstr;
727a15e6f9aSMark Peek     ptrdiff_t d;
728a15e6f9aSMark Peek 
729a15e6f9aSMark Peek     cmdmax += s;
730a15e6f9aSMark Peek     ncmdstr = xrealloc(cmdstr, cmdmax * sizeof(*cmdstr));
731a15e6f9aSMark Peek     d = ncmdstr - cmdstr;
732a15e6f9aSMark Peek     cmdstr = ncmdstr;
733a15e6f9aSMark Peek     cmdp += d;
734a15e6f9aSMark Peek }
735c80476e4SDavid E. O'Brien 
7366767bd61SMark Peek /* GrP
7376767bd61SMark Peek  * unparse - Export padd() functionality
7386767bd61SMark Peek  */
7396767bd61SMark Peek Char *
unparse(struct command * t)74045e5710bSMark Peek unparse(struct command *t)
7416767bd61SMark Peek {
742a15e6f9aSMark Peek     if (cmdmax == 0)
743a15e6f9aSMark Peek 	morecommand(CMD_INIT);
744a15e6f9aSMark Peek     cmdp = cmdstr;
7456767bd61SMark Peek     cmdlen = 0;
7466767bd61SMark Peek     padd(t);
7476767bd61SMark Peek     *cmdp++ = '\0';
748a15e6f9aSMark Peek     return Strsave(cmdstr);
7496767bd61SMark Peek }
7506767bd61SMark Peek 
7516767bd61SMark Peek 
752c80476e4SDavid E. O'Brien /*
753c80476e4SDavid E. O'Brien  * palloc - allocate a process structure and fill it up.
754c80476e4SDavid E. O'Brien  *	an important assumption is made that the process is running.
755c80476e4SDavid E. O'Brien  */
756c80476e4SDavid E. O'Brien void
palloc(pid_t pid,struct command * t)75745e5710bSMark Peek palloc(pid_t pid, struct command *t)
758c80476e4SDavid E. O'Brien {
75923338178SMark Peek     struct process *pp;
760c80476e4SDavid E. O'Brien     int     i;
761c80476e4SDavid E. O'Brien 
76245e5710bSMark Peek     pp = xcalloc(1, sizeof(struct process));
763c80476e4SDavid E. O'Brien     pp->p_procid = pid;
7649ccc37e3SMark Peek     pp->p_parentid = shpgrp;
765c80476e4SDavid E. O'Brien     pp->p_flags = ((t->t_dflg & F_AMPERSAND) ? 0 : PFOREGND) | PRUNNING;
766c80476e4SDavid E. O'Brien     if (t->t_dflg & F_TIME)
767c80476e4SDavid E. O'Brien 	pp->p_flags |= PPTIME;
768c80476e4SDavid E. O'Brien     if (t->t_dflg & F_BACKQ)
769c80476e4SDavid E. O'Brien 	pp->p_flags |= PBACKQ;
770c80476e4SDavid E. O'Brien     if (t->t_dflg & F_HUP)
771c80476e4SDavid E. O'Brien 	pp->p_flags |= PHUP;
7729ccc37e3SMark Peek     if (t->t_dcom && t->t_dcom[0] && (*t->t_dcom[0] == '{'))
7739ccc37e3SMark Peek 	pp->p_flags |= PBRACE;
774a15e6f9aSMark Peek     if (cmdmax == 0)
775a15e6f9aSMark Peek 	morecommand(CMD_INIT);
776a15e6f9aSMark Peek     cmdp = cmdstr;
777c80476e4SDavid E. O'Brien     cmdlen = 0;
778c80476e4SDavid E. O'Brien     padd(t);
779c80476e4SDavid E. O'Brien     *cmdp++ = 0;
780c80476e4SDavid E. O'Brien     if (t->t_dflg & F_PIPEOUT) {
781c80476e4SDavid E. O'Brien 	pp->p_flags |= PPOU;
782c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_STDERR)
783c80476e4SDavid E. O'Brien 	    pp->p_flags |= PDIAG;
784c80476e4SDavid E. O'Brien     }
785a15e6f9aSMark Peek     pp->p_command = Strsave(cmdstr);
786c80476e4SDavid E. O'Brien     if (pcurrjob) {
787c80476e4SDavid E. O'Brien 	struct process *fp;
788c80476e4SDavid E. O'Brien 
789c80476e4SDavid E. O'Brien 	/* careful here with interrupt level */
790c80476e4SDavid E. O'Brien 	pp->p_cwd = 0;
791c80476e4SDavid E. O'Brien 	pp->p_index = pcurrjob->p_index;
792c80476e4SDavid E. O'Brien 	pp->p_friends = pcurrjob;
793c80476e4SDavid E. O'Brien 	pp->p_jobid = pcurrjob->p_procid;
794c80476e4SDavid E. O'Brien 	for (fp = pcurrjob; fp->p_friends != pcurrjob; fp = fp->p_friends)
795c80476e4SDavid E. O'Brien 	    continue;
796c80476e4SDavid E. O'Brien 	fp->p_friends = pp;
797c80476e4SDavid E. O'Brien     }
798c80476e4SDavid E. O'Brien     else {
799c80476e4SDavid E. O'Brien 	pcurrjob = pp;
800c80476e4SDavid E. O'Brien 	pp->p_jobid = pid;
801c80476e4SDavid E. O'Brien 	pp->p_friends = pp;
802c80476e4SDavid E. O'Brien 	pp->p_cwd = dcwd;
803c80476e4SDavid E. O'Brien 	dcwd->di_count++;
804c80476e4SDavid E. O'Brien 	if (pmaxindex < BIGINDEX)
805c80476e4SDavid E. O'Brien 	    pp->p_index = ++pmaxindex;
806c80476e4SDavid E. O'Brien 	else {
807c80476e4SDavid E. O'Brien 	    struct process *np;
808c80476e4SDavid E. O'Brien 
809c80476e4SDavid E. O'Brien 	    for (i = 1;; i++) {
810c80476e4SDavid E. O'Brien 		for (np = proclist.p_next; np; np = np->p_next)
811c80476e4SDavid E. O'Brien 		    if (np->p_index == i)
812c80476e4SDavid E. O'Brien 			goto tryagain;
813c80476e4SDavid E. O'Brien 		pp->p_index = i;
814c80476e4SDavid E. O'Brien 		if (i > pmaxindex)
815c80476e4SDavid E. O'Brien 		    pmaxindex = i;
816c80476e4SDavid E. O'Brien 		break;
817c80476e4SDavid E. O'Brien 	tryagain:;
818c80476e4SDavid E. O'Brien 	    }
819c80476e4SDavid E. O'Brien 	}
820c80476e4SDavid E. O'Brien 	if (pcurrent == NULL)
821c80476e4SDavid E. O'Brien 	    pcurrent = pp;
822c80476e4SDavid E. O'Brien 	else if (pprevious == NULL)
823c80476e4SDavid E. O'Brien 	    pprevious = pp;
824c80476e4SDavid E. O'Brien     }
825c80476e4SDavid E. O'Brien     pp->p_next = proclist.p_next;
826c80476e4SDavid E. O'Brien     proclist.p_next = pp;
827c80476e4SDavid E. O'Brien #ifdef BSDTIMES
828c80476e4SDavid E. O'Brien     (void) gettimeofday(&pp->p_btime, NULL);
829c80476e4SDavid E. O'Brien #else /* !BSDTIMES */
830c80476e4SDavid E. O'Brien # ifdef _SEQUENT_
831c80476e4SDavid E. O'Brien     (void) get_process_stats(&pp->p_btime, PS_SELF, NULL, NULL);
832c80476e4SDavid E. O'Brien # else /* !_SEQUENT_ */
833c80476e4SDavid E. O'Brien     {
834c80476e4SDavid E. O'Brien 	struct tms tmptimes;
835c80476e4SDavid E. O'Brien 
836c80476e4SDavid E. O'Brien 	pp->p_btime = times(&tmptimes);
837c80476e4SDavid E. O'Brien     }
838c80476e4SDavid E. O'Brien # endif /* !_SEQUENT_ */
839c80476e4SDavid E. O'Brien #endif /* !BSDTIMES */
840c80476e4SDavid E. O'Brien }
841c80476e4SDavid E. O'Brien 
842c80476e4SDavid E. O'Brien static void
padd(struct command * t)84345e5710bSMark Peek padd(struct command *t)
844c80476e4SDavid E. O'Brien {
845c80476e4SDavid E. O'Brien     Char  **argp;
846c80476e4SDavid E. O'Brien 
847c80476e4SDavid E. O'Brien     if (t == 0)
848c80476e4SDavid E. O'Brien 	return;
849c80476e4SDavid E. O'Brien     switch (t->t_dtyp) {
850c80476e4SDavid E. O'Brien 
851c80476e4SDavid E. O'Brien     case NODE_PAREN:
852c80476e4SDavid E. O'Brien 	pads(STRLparensp);
853c80476e4SDavid E. O'Brien 	padd(t->t_dspr);
854c80476e4SDavid E. O'Brien 	pads(STRspRparen);
855c80476e4SDavid E. O'Brien 	break;
856c80476e4SDavid E. O'Brien 
857c80476e4SDavid E. O'Brien     case NODE_COMMAND:
858c80476e4SDavid E. O'Brien 	for (argp = t->t_dcom; *argp; argp++) {
859c80476e4SDavid E. O'Brien 	    pads(*argp);
860c80476e4SDavid E. O'Brien 	    if (argp[1])
861c80476e4SDavid E. O'Brien 		pads(STRspace);
862c80476e4SDavid E. O'Brien 	}
863c80476e4SDavid E. O'Brien 	break;
864c80476e4SDavid E. O'Brien 
865c80476e4SDavid E. O'Brien     case NODE_OR:
866c80476e4SDavid E. O'Brien     case NODE_AND:
867c80476e4SDavid E. O'Brien     case NODE_PIPE:
868c80476e4SDavid E. O'Brien     case NODE_LIST:
869c80476e4SDavid E. O'Brien 	padd(t->t_dcar);
870c80476e4SDavid E. O'Brien 	switch (t->t_dtyp) {
871c80476e4SDavid E. O'Brien 	case NODE_OR:
872c80476e4SDavid E. O'Brien 	    pads(STRspor2sp);
873c80476e4SDavid E. O'Brien 	    break;
874c80476e4SDavid E. O'Brien 	case NODE_AND:
875c80476e4SDavid E. O'Brien 	    pads(STRspand2sp);
876c80476e4SDavid E. O'Brien 	    break;
877c80476e4SDavid E. O'Brien 	case NODE_PIPE:
878c80476e4SDavid E. O'Brien 	    pads(STRsporsp);
879c80476e4SDavid E. O'Brien 	    break;
880c80476e4SDavid E. O'Brien 	case NODE_LIST:
881c80476e4SDavid E. O'Brien 	    pads(STRsemisp);
882c80476e4SDavid E. O'Brien 	    break;
883c80476e4SDavid E. O'Brien 	default:
884c80476e4SDavid E. O'Brien 	    break;
885c80476e4SDavid E. O'Brien 	}
886c80476e4SDavid E. O'Brien 	padd(t->t_dcdr);
887c80476e4SDavid E. O'Brien 	return;
888c80476e4SDavid E. O'Brien 
889c80476e4SDavid E. O'Brien     default:
890c80476e4SDavid E. O'Brien 	break;
891c80476e4SDavid E. O'Brien     }
892c80476e4SDavid E. O'Brien     if ((t->t_dflg & F_PIPEIN) == 0 && t->t_dlef) {
893c80476e4SDavid E. O'Brien 	pads((t->t_dflg & F_READ) ? STRspLarrow2sp : STRspLarrowsp);
894c80476e4SDavid E. O'Brien 	pads(t->t_dlef);
895c80476e4SDavid E. O'Brien     }
896c80476e4SDavid E. O'Brien     if ((t->t_dflg & F_PIPEOUT) == 0 && t->t_drit) {
897c80476e4SDavid E. O'Brien 	pads((t->t_dflg & F_APPEND) ? STRspRarrow2 : STRspRarrow);
898c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_STDERR)
899c80476e4SDavid E. O'Brien 	    pads(STRand);
900c80476e4SDavid E. O'Brien 	pads(STRspace);
901c80476e4SDavid E. O'Brien 	pads(t->t_drit);
902c80476e4SDavid E. O'Brien     }
903c80476e4SDavid E. O'Brien }
904c80476e4SDavid E. O'Brien 
905c80476e4SDavid E. O'Brien static void
pads(Char * cp)90645e5710bSMark Peek pads(Char *cp)
907c80476e4SDavid E. O'Brien {
908a15e6f9aSMark Peek     size_t i, len;
909c80476e4SDavid E. O'Brien 
910c80476e4SDavid E. O'Brien     /*
911c80476e4SDavid E. O'Brien      * Avoid the Quoted Space alias hack! Reported by:
912c80476e4SDavid E. O'Brien      * sam@john-bigboote.ICS.UCI.EDU (Sam Horrocks)
913c80476e4SDavid E. O'Brien      */
914c80476e4SDavid E. O'Brien     if (cp[0] == STRQNULL[0])
915c80476e4SDavid E. O'Brien 	cp++;
916c80476e4SDavid E. O'Brien 
91745e5710bSMark Peek     i = Strlen(cp);
918c80476e4SDavid E. O'Brien 
919a15e6f9aSMark Peek     len = cmdlen + i + CMD_INCR;
920a15e6f9aSMark Peek     if (len >= cmdmax)
921a15e6f9aSMark Peek 	morecommand(len);
922c80476e4SDavid E. O'Brien     (void) Strcpy(cmdp, cp);
923c80476e4SDavid E. O'Brien     cmdp += i;
924c80476e4SDavid E. O'Brien     cmdlen += i;
925c80476e4SDavid E. O'Brien }
926c80476e4SDavid E. O'Brien 
927c80476e4SDavid E. O'Brien /*
928c80476e4SDavid E. O'Brien  * psavejob - temporarily save the current job on a one level stack
929c80476e4SDavid E. O'Brien  *	so another job can be created.  Used for { } in exp6
930c80476e4SDavid E. O'Brien  *	and `` in globbing.
931c80476e4SDavid E. O'Brien  */
932c80476e4SDavid E. O'Brien void
psavejob(void)93345e5710bSMark Peek psavejob(void)
934c80476e4SDavid E. O'Brien {
935c80476e4SDavid E. O'Brien     pholdjob = pcurrjob;
936c80476e4SDavid E. O'Brien     pcurrjob = NULL;
937c80476e4SDavid E. O'Brien }
938c80476e4SDavid E. O'Brien 
939c80476e4SDavid E. O'Brien void
psavejob_cleanup(void * dummy)94045e5710bSMark Peek psavejob_cleanup(void *dummy)
941c80476e4SDavid E. O'Brien {
94245e5710bSMark Peek     USE(dummy);
943c80476e4SDavid E. O'Brien     pcurrjob = pholdjob;
944c80476e4SDavid E. O'Brien     pholdjob = NULL;
945c80476e4SDavid E. O'Brien }
946c80476e4SDavid E. O'Brien 
947c80476e4SDavid E. O'Brien /*
948c80476e4SDavid E. O'Brien  * pendjob - indicate that a job (set of commands) has been completed
949c80476e4SDavid E. O'Brien  *	or is about to begin.
950c80476e4SDavid E. O'Brien  */
951c80476e4SDavid E. O'Brien void
pendjob(void)95245e5710bSMark Peek pendjob(void)
953c80476e4SDavid E. O'Brien {
95423338178SMark Peek     struct process *pp, *tp;
955c80476e4SDavid E. O'Brien 
956c80476e4SDavid E. O'Brien     if (pcurrjob && (pcurrjob->p_flags & (PFOREGND | PSTOPPED)) == 0) {
957c80476e4SDavid E. O'Brien 	pp = pcurrjob;
9589ccc37e3SMark Peek 	pcurrjob = NULL;
959c80476e4SDavid E. O'Brien 	while (pp->p_procid != pp->p_jobid)
960c80476e4SDavid E. O'Brien 	    pp = pp->p_friends;
961c80476e4SDavid E. O'Brien 	xprintf("[%d]", pp->p_index);
962c80476e4SDavid E. O'Brien 	tp = pp;
963c80476e4SDavid E. O'Brien 	do {
964c80476e4SDavid E. O'Brien 	    xprintf(" %d", pp->p_procid);
965c80476e4SDavid E. O'Brien 	    pp = pp->p_friends;
966c80476e4SDavid E. O'Brien 	} while (pp != tp);
967c80476e4SDavid E. O'Brien 	xputchar('\n');
968c80476e4SDavid E. O'Brien     }
969c80476e4SDavid E. O'Brien     pholdjob = pcurrjob = 0;
970c80476e4SDavid E. O'Brien }
971c80476e4SDavid E. O'Brien 
972c80476e4SDavid E. O'Brien /*
973c80476e4SDavid E. O'Brien  * pprint - print a job
974c80476e4SDavid E. O'Brien  */
975c80476e4SDavid E. O'Brien 
976c80476e4SDavid E. O'Brien /*
977c80476e4SDavid E. O'Brien  * Hacks have been added for SVR4 to deal with pipe's being spawned in
978c80476e4SDavid E. O'Brien  * reverse order
979c80476e4SDavid E. O'Brien  *
980c80476e4SDavid E. O'Brien  * David Dawes (dawes@physics.su.oz.au) Oct 1991
981c80476e4SDavid E. O'Brien  */
982c80476e4SDavid E. O'Brien 
983c80476e4SDavid E. O'Brien static int
pprint(struct process * pp,int flag)98445e5710bSMark Peek pprint(struct process *pp, int flag)
985c80476e4SDavid E. O'Brien {
986c80476e4SDavid E. O'Brien     int status, reason;
987c80476e4SDavid E. O'Brien     struct process *tp;
988c80476e4SDavid E. O'Brien     int     jobflags, pstatus, pcond;
98923338178SMark Peek     const char *format;
990*19d2e3deSDmitry Chagin     int ohaderr;
991c80476e4SDavid E. O'Brien 
992c80476e4SDavid E. O'Brien #ifdef BACKPIPE
993c80476e4SDavid E. O'Brien     struct process *pipehead = NULL, *pipetail = NULL, *pmarker = NULL;
994c80476e4SDavid E. O'Brien     int inpipe = 0;
995c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
996c80476e4SDavid E. O'Brien 
997c80476e4SDavid E. O'Brien     while (pp->p_procid != pp->p_jobid)
998c80476e4SDavid E. O'Brien 	pp = pp->p_friends;
999c80476e4SDavid E. O'Brien     if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
1000c80476e4SDavid E. O'Brien 	pp->p_flags &= ~PPTIME;
1001c80476e4SDavid E. O'Brien 	pp->p_flags |= PTIME;
1002c80476e4SDavid E. O'Brien     }
1003c80476e4SDavid E. O'Brien     tp = pp;
1004c80476e4SDavid E. O'Brien     status = reason = -1;
1005c80476e4SDavid E. O'Brien     jobflags = 0;
1006*19d2e3deSDmitry Chagin     ohaderr = haderr;
1007*19d2e3deSDmitry Chagin     /* Print status to stderr, except for jobs built-in */
1008*19d2e3deSDmitry Chagin     haderr = !(flag & JOBLIST);
1009c80476e4SDavid E. O'Brien     do {
1010c80476e4SDavid E. O'Brien #ifdef BACKPIPE
1011c80476e4SDavid E. O'Brien 	/*
1012c80476e4SDavid E. O'Brien 	 * The pipeline is reversed, so locate the real head of the pipeline
1013c80476e4SDavid E. O'Brien 	 * if pp is at the tail of a pipe (and not already in a pipeline)
1014c80476e4SDavid E. O'Brien 	 */
1015c80476e4SDavid E. O'Brien 	if ((pp->p_friends->p_flags & PPOU) && !inpipe && (flag & NAME)) {
1016c80476e4SDavid E. O'Brien 	    inpipe = 1;
1017c80476e4SDavid E. O'Brien 	    pipetail = pp;
1018c80476e4SDavid E. O'Brien 	    do
1019c80476e4SDavid E. O'Brien 		pp = pp->p_friends;
1020c80476e4SDavid E. O'Brien 	    while (pp->p_friends->p_flags & PPOU);
1021c80476e4SDavid E. O'Brien 	    pipehead = pp;
1022c80476e4SDavid E. O'Brien 	    pmarker = pp;
1023c80476e4SDavid E. O'Brien 	/*
1024c80476e4SDavid E. O'Brien 	 * pmarker is used to hold the place of the proc being processed, so
1025c80476e4SDavid E. O'Brien 	 * we can search for the next one downstream later.
1026c80476e4SDavid E. O'Brien 	 */
1027c80476e4SDavid E. O'Brien 	}
102845e5710bSMark Peek 	pcond = (tp != pp || (inpipe && tp == pp));
1029c80476e4SDavid E. O'Brien #else /* !BACKPIPE */
103045e5710bSMark Peek 	pcond = (tp != pp);
1031c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
1032c80476e4SDavid E. O'Brien 
1033c80476e4SDavid E. O'Brien 	jobflags |= pp->p_flags;
1034c80476e4SDavid E. O'Brien 	pstatus = (int) (pp->p_flags & PALLSTATES);
1035c80476e4SDavid E. O'Brien 	if (pcond && linp != linbuf && !(flag & FANCY) &&
1036c80476e4SDavid E. O'Brien 	    ((pstatus == status && pp->p_reason == reason) ||
1037c80476e4SDavid E. O'Brien 	     !(flag & REASON)))
1038c80476e4SDavid E. O'Brien 	    xputchar(' ');
1039c80476e4SDavid E. O'Brien 	else {
1040c80476e4SDavid E. O'Brien 	    if (pcond && linp != linbuf)
1041c80476e4SDavid E. O'Brien 		xputchar('\n');
1042c80476e4SDavid E. O'Brien 	    if (flag & NUMBER) {
1043c80476e4SDavid E. O'Brien #ifdef BACKPIPE
1044c80476e4SDavid E. O'Brien 		pcond = ((pp == tp && !inpipe) ||
1045c80476e4SDavid E. O'Brien 			 (inpipe && pipetail == tp && pp == pipehead));
1046c80476e4SDavid E. O'Brien #else /* BACKPIPE */
1047c80476e4SDavid E. O'Brien 		pcond = (pp == tp);
1048c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
1049c80476e4SDavid E. O'Brien 		if (pcond)
1050c80476e4SDavid E. O'Brien 		    xprintf("[%d]%s %c ", pp->p_index,
1051c80476e4SDavid E. O'Brien 			    pp->p_index < 10 ? " " : "",
1052c80476e4SDavid E. O'Brien 			    pp == pcurrent ? '+' :
1053c80476e4SDavid E. O'Brien 			    (pp == pprevious ? '-' : ' '));
1054c80476e4SDavid E. O'Brien 		else
1055c80476e4SDavid E. O'Brien 		    xprintf("       ");
1056c80476e4SDavid E. O'Brien 	    }
1057c80476e4SDavid E. O'Brien 	    if (flag & FANCY) {
1058c80476e4SDavid E. O'Brien 		xprintf("%5d ", pp->p_procid);
1059c80476e4SDavid E. O'Brien #ifdef TCF
1060c80476e4SDavid E. O'Brien 		xprintf("%11s ", sitename(pp->p_procid));
1061c80476e4SDavid E. O'Brien #endif /* TCF */
1062c80476e4SDavid E. O'Brien 	    }
1063c80476e4SDavid E. O'Brien 	    if (flag & (REASON | AREASON)) {
1064c80476e4SDavid E. O'Brien 		if (flag & NAME)
1065c80476e4SDavid E. O'Brien 		    format = "%-30s";
1066c80476e4SDavid E. O'Brien 		else
1067c80476e4SDavid E. O'Brien 		    format = "%s";
1068c80476e4SDavid E. O'Brien 		if (pstatus == status) {
1069c80476e4SDavid E. O'Brien 		    if (pp->p_reason == reason) {
1070c80476e4SDavid E. O'Brien 			xprintf(format, "");
1071c80476e4SDavid E. O'Brien 			goto prcomd;
1072c80476e4SDavid E. O'Brien 		    }
1073c80476e4SDavid E. O'Brien 		    else
1074c80476e4SDavid E. O'Brien 			reason = (int) pp->p_reason;
1075c80476e4SDavid E. O'Brien 		}
1076c80476e4SDavid E. O'Brien 		else {
1077c80476e4SDavid E. O'Brien 		    status = pstatus;
1078c80476e4SDavid E. O'Brien 		    reason = (int) pp->p_reason;
1079c80476e4SDavid E. O'Brien 		}
1080c80476e4SDavid E. O'Brien 		switch (status) {
1081c80476e4SDavid E. O'Brien 
1082c80476e4SDavid E. O'Brien 		case PRUNNING:
1083c80476e4SDavid E. O'Brien 		    xprintf(format, CGETS(17, 4, "Running "));
1084c80476e4SDavid E. O'Brien 		    break;
1085c80476e4SDavid E. O'Brien 
1086c80476e4SDavid E. O'Brien 		case PINTERRUPTED:
1087c80476e4SDavid E. O'Brien 		case PSTOPPED:
1088c80476e4SDavid E. O'Brien 		case PSIGNALED:
1089c80476e4SDavid E. O'Brien 		    /*
1090c80476e4SDavid E. O'Brien 		     * tell what happened to the background job
1091c80476e4SDavid E. O'Brien 		     * From: Michael Schroeder
1092c80476e4SDavid E. O'Brien 		     * <mlschroe@immd4.informatik.uni-erlangen.de>
1093c80476e4SDavid E. O'Brien 		     */
1094c80476e4SDavid E. O'Brien 		    if ((flag & REASON)
1095c80476e4SDavid E. O'Brien 			|| ((flag & AREASON)
1096c80476e4SDavid E. O'Brien 			    && reason != SIGINT
1097c80476e4SDavid E. O'Brien 			    && (reason != SIGPIPE
1098c80476e4SDavid E. O'Brien 				|| (pp->p_flags & PPOU) == 0))) {
109945e5710bSMark Peek 			char *ptr;
110045e5710bSMark Peek 			int free_ptr;
1101c80476e4SDavid E. O'Brien 
110245e5710bSMark Peek 			free_ptr = 0;
110345e5710bSMark Peek 			ptr = (char *)(intptr_t)mesg[pp->p_reason & 0177].pname;
110445e5710bSMark Peek 			if (ptr == NULL) {
110545e5710bSMark Peek 			    ptr = xasprintf("%s %d", CGETS(17, 5, "Signal"),
110645e5710bSMark Peek 					    pp->p_reason & 0177);
110745e5710bSMark Peek 			    cleanup_push(ptr, xfree);
110845e5710bSMark Peek 			    free_ptr = 1;
110923338178SMark Peek 			}
1110c80476e4SDavid E. O'Brien 			xprintf(format, ptr);
111145e5710bSMark Peek 			if (free_ptr != 0)
111245e5710bSMark Peek 			    cleanup_until(ptr);
1113c80476e4SDavid E. O'Brien 		    }
1114c80476e4SDavid E. O'Brien 		    else
1115c80476e4SDavid E. O'Brien 			reason = -1;
1116c80476e4SDavid E. O'Brien 		    break;
1117c80476e4SDavid E. O'Brien 
1118c80476e4SDavid E. O'Brien 		case PNEXITED:
1119c80476e4SDavid E. O'Brien 		case PAEXITED:
1120c80476e4SDavid E. O'Brien 		    if (flag & REASON) {
1121c80476e4SDavid E. O'Brien 			if (pp->p_reason)
1122c80476e4SDavid E. O'Brien 			    xprintf(CGETS(17, 6, "Exit %-25d"), pp->p_reason);
1123c80476e4SDavid E. O'Brien 			else
1124c80476e4SDavid E. O'Brien 			    xprintf(format, CGETS(17, 7, "Done"));
1125c80476e4SDavid E. O'Brien 		    }
1126c80476e4SDavid E. O'Brien 		    break;
1127c80476e4SDavid E. O'Brien 
1128c80476e4SDavid E. O'Brien 		default:
1129c80476e4SDavid E. O'Brien 		    xprintf(CGETS(17, 8, "BUG: status=%-9o"),
1130c80476e4SDavid E. O'Brien 			    status);
1131c80476e4SDavid E. O'Brien 		}
1132c80476e4SDavid E. O'Brien 	    }
1133c80476e4SDavid E. O'Brien 	}
1134c80476e4SDavid E. O'Brien prcomd:
1135c80476e4SDavid E. O'Brien 	if (flag & NAME) {
1136c80476e4SDavid E. O'Brien 	    xprintf("%S", pp->p_command);
1137c80476e4SDavid E. O'Brien 	    if (pp->p_flags & PPOU)
1138c80476e4SDavid E. O'Brien 		xprintf(" |");
1139c80476e4SDavid E. O'Brien 	    if (pp->p_flags & PDIAG)
1140c80476e4SDavid E. O'Brien 		xprintf("&");
1141c80476e4SDavid E. O'Brien 	}
1142c80476e4SDavid E. O'Brien 	if (flag & (REASON | AREASON) && pp->p_flags & PDUMPED)
1143a15e6f9aSMark Peek 	    xprintf("%s", CGETS(17, 9, " (core dumped)"));
1144c80476e4SDavid E. O'Brien 	if (tp == pp->p_friends) {
1145c80476e4SDavid E. O'Brien 	    if (flag & AMPERSAND)
1146c80476e4SDavid E. O'Brien 		xprintf(" &");
1147c80476e4SDavid E. O'Brien 	    if (flag & JOBDIR &&
1148c80476e4SDavid E. O'Brien 		!eq(tp->p_cwd->di_name, dcwd->di_name)) {
1149a15e6f9aSMark Peek 		xprintf("%s", CGETS(17, 10, " (wd: "));
1150c80476e4SDavid E. O'Brien 		dtildepr(tp->p_cwd->di_name);
1151c80476e4SDavid E. O'Brien 		xprintf(")");
1152c80476e4SDavid E. O'Brien 	    }
1153c80476e4SDavid E. O'Brien 	}
1154c80476e4SDavid E. O'Brien 	if (pp->p_flags & PPTIME && !(status & (PSTOPPED | PRUNNING))) {
1155c80476e4SDavid E. O'Brien 	    if (linp != linbuf)
1156c80476e4SDavid E. O'Brien 		xprintf("\n\t");
1157c80476e4SDavid E. O'Brien #if defined(BSDTIMES) || defined(_SEQUENT_)
1158c80476e4SDavid E. O'Brien 	    prusage(&zru, &pp->p_rusage, &pp->p_etime,
1159c80476e4SDavid E. O'Brien 		    &pp->p_btime);
1160c80476e4SDavid E. O'Brien #else /* !BSDTIMES && !SEQUENT */
1161c80476e4SDavid E. O'Brien 	    lru.tms_utime = pp->p_utime;
1162c80476e4SDavid E. O'Brien 	    lru.tms_stime = pp->p_stime;
1163c80476e4SDavid E. O'Brien 	    lru.tms_cutime = 0;
1164c80476e4SDavid E. O'Brien 	    lru.tms_cstime = 0;
1165c80476e4SDavid E. O'Brien 	    prusage(&zru, &lru, pp->p_etime,
1166c80476e4SDavid E. O'Brien 		    pp->p_btime);
1167c80476e4SDavid E. O'Brien #endif /* !BSDTIMES && !SEQUENT */
1168c80476e4SDavid E. O'Brien 
1169c80476e4SDavid E. O'Brien 	}
1170c80476e4SDavid E. O'Brien #ifdef BACKPIPE
1171c80476e4SDavid E. O'Brien 	pcond = ((tp == pp->p_friends && !inpipe) ||
1172c80476e4SDavid E. O'Brien 		 (inpipe && pipehead->p_friends == tp && pp == pipetail));
1173c80476e4SDavid E. O'Brien #else  /* !BACKPIPE */
1174c80476e4SDavid E. O'Brien 	pcond = (tp == pp->p_friends);
1175c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
1176c80476e4SDavid E. O'Brien 	if (pcond) {
1177c80476e4SDavid E. O'Brien 	    if (linp != linbuf)
1178c80476e4SDavid E. O'Brien 		xputchar('\n');
1179c80476e4SDavid E. O'Brien 	    if (flag & SHELLDIR && !eq(tp->p_cwd->di_name, dcwd->di_name)) {
1180a15e6f9aSMark Peek 		xprintf("%s", CGETS(17, 11, "(wd now: "));
1181c80476e4SDavid E. O'Brien 		dtildepr(dcwd->di_name);
1182c80476e4SDavid E. O'Brien 		xprintf(")\n");
1183c80476e4SDavid E. O'Brien 	    }
1184c80476e4SDavid E. O'Brien 	}
1185c80476e4SDavid E. O'Brien #ifdef BACKPIPE
1186c80476e4SDavid E. O'Brien 	if (inpipe) {
1187c80476e4SDavid E. O'Brien 	    /*
1188c80476e4SDavid E. O'Brien 	     * if pmaker == pipetail, we are finished that pipeline, and
1189c80476e4SDavid E. O'Brien 	     * can now skip to past the head
1190c80476e4SDavid E. O'Brien 	     */
1191c80476e4SDavid E. O'Brien 	    if (pmarker == pipetail) {
1192c80476e4SDavid E. O'Brien 		inpipe = 0;
1193c80476e4SDavid E. O'Brien 		pp = pipehead;
1194c80476e4SDavid E. O'Brien 	    }
1195c80476e4SDavid E. O'Brien 	    else {
1196c80476e4SDavid E. O'Brien 	    /*
1197c80476e4SDavid E. O'Brien 	     * set pp to one before the one we want next, so the while below
1198c80476e4SDavid E. O'Brien 	     * increments to the correct spot.
1199c80476e4SDavid E. O'Brien 	     */
1200c80476e4SDavid E. O'Brien 		do
1201c80476e4SDavid E. O'Brien 		    pp = pp->p_friends;
1202c80476e4SDavid E. O'Brien 	    	while (pp->p_friends->p_friends != pmarker);
1203c80476e4SDavid E. O'Brien 	    	pmarker = pp->p_friends;
1204c80476e4SDavid E. O'Brien 	    }
1205c80476e4SDavid E. O'Brien 	}
1206c80476e4SDavid E. O'Brien 	pcond = ((pp = pp->p_friends) != tp || inpipe);
1207c80476e4SDavid E. O'Brien #else /* !BACKPIPE */
1208c80476e4SDavid E. O'Brien 	pcond = ((pp = pp->p_friends) != tp);
1209c80476e4SDavid E. O'Brien #endif /* BACKPIPE */
1210c80476e4SDavid E. O'Brien     } while (pcond);
1211c80476e4SDavid E. O'Brien 
1212c80476e4SDavid E. O'Brien     if (jobflags & PTIME && (jobflags & (PSTOPPED | PRUNNING)) == 0) {
1213c80476e4SDavid E. O'Brien 	if (jobflags & NUMBER)
1214c80476e4SDavid E. O'Brien 	    xprintf("       ");
1215c80476e4SDavid E. O'Brien 	ptprint(tp);
1216c80476e4SDavid E. O'Brien     }
1217*19d2e3deSDmitry Chagin     haderr = ohaderr;
1218c80476e4SDavid E. O'Brien     return (jobflags);
1219c80476e4SDavid E. O'Brien }
1220c80476e4SDavid E. O'Brien 
1221c80476e4SDavid E. O'Brien /*
1222c80476e4SDavid E. O'Brien  * All 4.3 BSD derived implementations are buggy and I've had enough.
1223c80476e4SDavid E. O'Brien  * The following implementation produces similar code and works in all
1224c80476e4SDavid E. O'Brien  * cases. The 4.3BSD one works only for <, >, !=
1225c80476e4SDavid E. O'Brien  */
1226c80476e4SDavid E. O'Brien # undef timercmp
1227c80476e4SDavid E. O'Brien #  define timercmp(tvp, uvp, cmp) \
1228c80476e4SDavid E. O'Brien       (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1229c80476e4SDavid E. O'Brien 	   ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1230c80476e4SDavid E. O'Brien 	   ((tvp)->tv_sec  cmp (uvp)->tv_sec))
1231c80476e4SDavid E. O'Brien 
1232c80476e4SDavid E. O'Brien static void
ptprint(struct process * tp)123345e5710bSMark Peek ptprint(struct process *tp)
1234c80476e4SDavid E. O'Brien {
1235c80476e4SDavid E. O'Brien #ifdef BSDTIMES
1236c80476e4SDavid E. O'Brien     struct timeval tetime, diff;
1237c80476e4SDavid E. O'Brien     static struct timeval ztime;
1238c80476e4SDavid E. O'Brien     struct sysrusage ru;
123923338178SMark Peek     struct process *pp = tp;
1240c80476e4SDavid E. O'Brien 
1241c80476e4SDavid E. O'Brien     ru = zru;
1242c80476e4SDavid E. O'Brien     tetime = ztime;
1243c80476e4SDavid E. O'Brien     do {
1244c80476e4SDavid E. O'Brien 	ruadd(&ru, &pp->p_rusage);
1245c80476e4SDavid E. O'Brien 	tvsub(&diff, &pp->p_etime, &pp->p_btime);
1246c80476e4SDavid E. O'Brien 	if (timercmp(&diff, &tetime, >))
1247c80476e4SDavid E. O'Brien 	    tetime = diff;
1248c80476e4SDavid E. O'Brien     } while ((pp = pp->p_friends) != tp);
1249c80476e4SDavid E. O'Brien     prusage(&zru, &ru, &tetime, &ztime);
1250c80476e4SDavid E. O'Brien #else /* !BSDTIMES */
1251c80476e4SDavid E. O'Brien # ifdef _SEQUENT_
1252c80476e4SDavid E. O'Brien     timeval_t tetime, diff;
1253c80476e4SDavid E. O'Brien     static timeval_t ztime;
1254c80476e4SDavid E. O'Brien     struct process_stats ru;
125523338178SMark Peek     struct process *pp = tp;
1256c80476e4SDavid E. O'Brien 
1257c80476e4SDavid E. O'Brien     ru = zru;
1258c80476e4SDavid E. O'Brien     tetime = ztime;
1259c80476e4SDavid E. O'Brien     do {
1260c80476e4SDavid E. O'Brien 	ruadd(&ru, &pp->p_rusage);
1261c80476e4SDavid E. O'Brien 	tvsub(&diff, &pp->p_etime, &pp->p_btime);
1262c80476e4SDavid E. O'Brien 	if (timercmp(&diff, &tetime, >))
1263c80476e4SDavid E. O'Brien 	    tetime = diff;
1264c80476e4SDavid E. O'Brien     } while ((pp = pp->p_friends) != tp);
1265c80476e4SDavid E. O'Brien     prusage(&zru, &ru, &tetime, &ztime);
1266c80476e4SDavid E. O'Brien # else /* !_SEQUENT_ */
1267c80476e4SDavid E. O'Brien #  ifndef POSIX
1268c80476e4SDavid E. O'Brien     static time_t ztime = 0;
1269c80476e4SDavid E. O'Brien     static time_t zu_time = 0;
1270c80476e4SDavid E. O'Brien     static time_t zs_time = 0;
1271c80476e4SDavid E. O'Brien     time_t  tetime, diff;
1272c80476e4SDavid E. O'Brien     time_t  u_time, s_time;
1273c80476e4SDavid E. O'Brien 
1274c80476e4SDavid E. O'Brien #  else	/* POSIX */
1275c80476e4SDavid E. O'Brien     static clock_t ztime = 0;
1276c80476e4SDavid E. O'Brien     static clock_t zu_time = 0;
1277c80476e4SDavid E. O'Brien     static clock_t zs_time = 0;
1278c80476e4SDavid E. O'Brien     clock_t tetime, diff;
1279c80476e4SDavid E. O'Brien     clock_t u_time, s_time;
1280c80476e4SDavid E. O'Brien 
1281c80476e4SDavid E. O'Brien #  endif /* POSIX */
1282c80476e4SDavid E. O'Brien     struct tms zts, rts;
128323338178SMark Peek     struct process *pp = tp;
1284c80476e4SDavid E. O'Brien 
1285c80476e4SDavid E. O'Brien     u_time = zu_time;
1286c80476e4SDavid E. O'Brien     s_time = zs_time;
1287c80476e4SDavid E. O'Brien     tetime = ztime;
1288c80476e4SDavid E. O'Brien     do {
1289c80476e4SDavid E. O'Brien 	u_time += pp->p_utime;
1290c80476e4SDavid E. O'Brien 	s_time += pp->p_stime;
1291c80476e4SDavid E. O'Brien 	diff = pp->p_etime - pp->p_btime;
1292c80476e4SDavid E. O'Brien 	if (diff > tetime)
1293c80476e4SDavid E. O'Brien 	    tetime = diff;
1294c80476e4SDavid E. O'Brien     } while ((pp = pp->p_friends) != tp);
1295c80476e4SDavid E. O'Brien     zts.tms_utime = zu_time;
1296c80476e4SDavid E. O'Brien     zts.tms_stime = zs_time;
1297c80476e4SDavid E. O'Brien     zts.tms_cutime = 0;
1298c80476e4SDavid E. O'Brien     zts.tms_cstime = 0;
1299c80476e4SDavid E. O'Brien     rts.tms_utime = u_time;
1300c80476e4SDavid E. O'Brien     rts.tms_stime = s_time;
1301c80476e4SDavid E. O'Brien     rts.tms_cutime = 0;
1302c80476e4SDavid E. O'Brien     rts.tms_cstime = 0;
1303c80476e4SDavid E. O'Brien     prusage(&zts, &rts, tetime, ztime);
1304c80476e4SDavid E. O'Brien # endif /* !_SEQUENT_ */
1305c80476e4SDavid E. O'Brien #endif	/* !BSDTIMES */
1306c80476e4SDavid E. O'Brien }
1307c80476e4SDavid E. O'Brien 
1308c80476e4SDavid E. O'Brien /*
1309c80476e4SDavid E. O'Brien  * dojobs - print all jobs
1310c80476e4SDavid E. O'Brien  */
1311c80476e4SDavid E. O'Brien /*ARGSUSED*/
1312c80476e4SDavid E. O'Brien void
dojobs(Char ** v,struct command * c)131345e5710bSMark Peek dojobs(Char **v, struct command *c)
1314c80476e4SDavid E. O'Brien {
131523338178SMark Peek     struct process *pp;
1316*19d2e3deSDmitry Chagin     int flag = NUMBER | NAME | REASON | JOBLIST;
1317c80476e4SDavid E. O'Brien     int     i;
1318c80476e4SDavid E. O'Brien 
1319c80476e4SDavid E. O'Brien     USE(c);
1320c80476e4SDavid E. O'Brien     if (chkstop)
1321c80476e4SDavid E. O'Brien 	chkstop = 2;
1322c80476e4SDavid E. O'Brien     if (*++v) {
1323c80476e4SDavid E. O'Brien 	if (v[1] || !eq(*v, STRml))
1324c80476e4SDavid E. O'Brien 	    stderror(ERR_JOBS);
1325c80476e4SDavid E. O'Brien 	flag |= FANCY | JOBDIR;
1326c80476e4SDavid E. O'Brien     }
1327c80476e4SDavid E. O'Brien     for (i = 1; i <= pmaxindex; i++)
1328c80476e4SDavid E. O'Brien 	for (pp = proclist.p_next; pp; pp = pp->p_next)
1329c80476e4SDavid E. O'Brien 	    if (pp->p_index == i && pp->p_procid == pp->p_jobid) {
1330c80476e4SDavid E. O'Brien 		pp->p_flags &= ~PNEEDNOTE;
1331c80476e4SDavid E. O'Brien 		if (!(pprint(pp, flag) & (PRUNNING | PSTOPPED)))
1332c80476e4SDavid E. O'Brien 		    pflush(pp);
1333c80476e4SDavid E. O'Brien 		break;
1334c80476e4SDavid E. O'Brien 	    }
1335c80476e4SDavid E. O'Brien }
1336c80476e4SDavid E. O'Brien 
1337c80476e4SDavid E. O'Brien /*
1338c80476e4SDavid E. O'Brien  * dofg - builtin - put the job into the foreground
1339c80476e4SDavid E. O'Brien  */
1340c80476e4SDavid E. O'Brien /*ARGSUSED*/
1341c80476e4SDavid E. O'Brien void
dofg(Char ** v,struct command * c)134245e5710bSMark Peek dofg(Char **v, struct command *c)
1343c80476e4SDavid E. O'Brien {
134423338178SMark Peek     struct process *pp;
1345c80476e4SDavid E. O'Brien 
1346c80476e4SDavid E. O'Brien     USE(c);
1347c80476e4SDavid E. O'Brien     okpcntl();
1348c80476e4SDavid E. O'Brien     ++v;
1349c80476e4SDavid E. O'Brien     do {
1350c80476e4SDavid E. O'Brien 	pp = pfind(*v);
1351c80476e4SDavid E. O'Brien 	if (!pstart(pp, 1)) {
1352c80476e4SDavid E. O'Brien 	    pp->p_procid = 0;
1353c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1354c80476e4SDavid E. O'Brien 	    continue;
1355c80476e4SDavid E. O'Brien 	}
1356c80476e4SDavid E. O'Brien 	pjwait(pp);
1357c80476e4SDavid E. O'Brien     } while (*v && *++v);
1358c80476e4SDavid E. O'Brien }
1359c80476e4SDavid E. O'Brien 
1360c80476e4SDavid E. O'Brien /*
1361c80476e4SDavid E. O'Brien  * %... - builtin - put the job into the foreground
1362c80476e4SDavid E. O'Brien  */
1363c80476e4SDavid E. O'Brien /*ARGSUSED*/
1364c80476e4SDavid E. O'Brien void
dofg1(Char ** v,struct command * c)136545e5710bSMark Peek dofg1(Char **v, struct command *c)
1366c80476e4SDavid E. O'Brien {
136723338178SMark Peek     struct process *pp;
1368c80476e4SDavid E. O'Brien 
1369c80476e4SDavid E. O'Brien     USE(c);
1370c80476e4SDavid E. O'Brien     okpcntl();
1371c80476e4SDavid E. O'Brien     pp = pfind(v[0]);
1372c80476e4SDavid E. O'Brien     if (!pstart(pp, 1)) {
1373c80476e4SDavid E. O'Brien 	pp->p_procid = 0;
1374c80476e4SDavid E. O'Brien 	stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1375c80476e4SDavid E. O'Brien 	return;
1376c80476e4SDavid E. O'Brien     }
1377c80476e4SDavid E. O'Brien     pjwait(pp);
1378c80476e4SDavid E. O'Brien }
1379c80476e4SDavid E. O'Brien 
1380c80476e4SDavid E. O'Brien /*
1381c80476e4SDavid E. O'Brien  * dobg - builtin - put the job into the background
1382c80476e4SDavid E. O'Brien  */
1383c80476e4SDavid E. O'Brien /*ARGSUSED*/
1384c80476e4SDavid E. O'Brien void
dobg(Char ** v,struct command * c)138545e5710bSMark Peek dobg(Char **v, struct command *c)
1386c80476e4SDavid E. O'Brien {
138723338178SMark Peek     struct process *pp;
1388c80476e4SDavid E. O'Brien 
1389c80476e4SDavid E. O'Brien     USE(c);
1390c80476e4SDavid E. O'Brien     okpcntl();
1391c80476e4SDavid E. O'Brien     ++v;
1392c80476e4SDavid E. O'Brien     do {
1393c80476e4SDavid E. O'Brien 	pp = pfind(*v);
1394c80476e4SDavid E. O'Brien 	if (!pstart(pp, 0)) {
1395c80476e4SDavid E. O'Brien 	    pp->p_procid = 0;
1396c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1397c80476e4SDavid E. O'Brien 	}
1398c80476e4SDavid E. O'Brien     } while (*v && *++v);
1399c80476e4SDavid E. O'Brien }
1400c80476e4SDavid E. O'Brien 
1401c80476e4SDavid E. O'Brien /*
1402c80476e4SDavid E. O'Brien  * %... & - builtin - put the job into the background
1403c80476e4SDavid E. O'Brien  */
1404c80476e4SDavid E. O'Brien /*ARGSUSED*/
1405c80476e4SDavid E. O'Brien void
dobg1(Char ** v,struct command * c)140645e5710bSMark Peek dobg1(Char **v, struct command *c)
1407c80476e4SDavid E. O'Brien {
140823338178SMark Peek     struct process *pp;
1409c80476e4SDavid E. O'Brien 
1410c80476e4SDavid E. O'Brien     USE(c);
1411c80476e4SDavid E. O'Brien     pp = pfind(v[0]);
1412c80476e4SDavid E. O'Brien     if (!pstart(pp, 0)) {
1413c80476e4SDavid E. O'Brien 	pp->p_procid = 0;
1414c80476e4SDavid E. O'Brien 	stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1415c80476e4SDavid E. O'Brien     }
1416c80476e4SDavid E. O'Brien }
1417c80476e4SDavid E. O'Brien 
1418c80476e4SDavid E. O'Brien /*
1419c80476e4SDavid E. O'Brien  * dostop - builtin - stop the job
1420c80476e4SDavid E. O'Brien  */
1421c80476e4SDavid E. O'Brien /*ARGSUSED*/
1422c80476e4SDavid E. O'Brien void
dostop(Char ** v,struct command * c)142345e5710bSMark Peek dostop(Char **v, struct command *c)
1424c80476e4SDavid E. O'Brien {
1425c80476e4SDavid E. O'Brien     USE(c);
1426c80476e4SDavid E. O'Brien #ifdef BSDJOBS
1427c80476e4SDavid E. O'Brien     pkill(++v, SIGSTOP);
1428c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
1429c80476e4SDavid E. O'Brien }
1430c80476e4SDavid E. O'Brien 
1431c80476e4SDavid E. O'Brien /*
1432c80476e4SDavid E. O'Brien  * dokill - builtin - superset of kill (1)
1433c80476e4SDavid E. O'Brien  */
1434c80476e4SDavid E. O'Brien /*ARGSUSED*/
1435c80476e4SDavid E. O'Brien void
dokill(Char ** v,struct command * c)143645e5710bSMark Peek dokill(Char **v, struct command *c)
1437c80476e4SDavid E. O'Brien {
143823338178SMark Peek     int signum, len = 0;
143923338178SMark Peek     const char *name;
14406767bd61SMark Peek     Char *sigptr;
1441c80476e4SDavid E. O'Brien 
1442c80476e4SDavid E. O'Brien     USE(c);
1443c80476e4SDavid E. O'Brien     v++;
1444c80476e4SDavid E. O'Brien     if (v[0] && v[0][0] == '-') {
1445c80476e4SDavid E. O'Brien 	if (v[0][1] == 'l') {
1446c80476e4SDavid E. O'Brien 	    for (signum = 0; signum <= nsig; signum++) {
1447c80476e4SDavid E. O'Brien 		if ((name = mesg[signum].iname) != NULL) {
1448c80476e4SDavid E. O'Brien 		    len += strlen(name) + 1;
144945e5710bSMark Peek 		    if (len >= TermH - 1) {
1450c80476e4SDavid E. O'Brien 			xputchar('\n');
1451c80476e4SDavid E. O'Brien 			len = strlen(name) + 1;
1452c80476e4SDavid E. O'Brien 		    }
1453c80476e4SDavid E. O'Brien 		    xprintf("%s ", name);
1454c80476e4SDavid E. O'Brien 		}
1455c80476e4SDavid E. O'Brien 	    }
1456c80476e4SDavid E. O'Brien 	    xputchar('\n');
1457c80476e4SDavid E. O'Brien 	    return;
1458c80476e4SDavid E. O'Brien 	}
14596767bd61SMark Peek  	sigptr = &v[0][1];
14606767bd61SMark Peek  	if (v[0][1] == 's') {
14616767bd61SMark Peek  	    if (v[1]) {
14626767bd61SMark Peek  		v++;
14636767bd61SMark Peek  		sigptr = &v[0][0];
14646767bd61SMark Peek  	    } else {
14656767bd61SMark Peek  		stderror(ERR_NAME | ERR_TOOFEW);
14666767bd61SMark Peek  	    }
14676767bd61SMark Peek  	}
14686767bd61SMark Peek  	if (Isdigit(*sigptr)) {
1469b2d5d167SMark Peek 	    char *ep;
1470b2d5d167SMark Peek  	    signum = strtoul(short2str(sigptr), &ep, 0);
1471b2d5d167SMark Peek 	    if (*ep || signum < 0 || signum > (MAXSIG-1))
1472c80476e4SDavid E. O'Brien 		stderror(ERR_NAME | ERR_BADSIG);
1473c80476e4SDavid E. O'Brien 	}
1474c80476e4SDavid E. O'Brien 	else {
1475c80476e4SDavid E. O'Brien 	    for (signum = 0; signum <= nsig; signum++)
1476c80476e4SDavid E. O'Brien 		if (mesg[signum].iname &&
14776767bd61SMark Peek  		    eq(sigptr, str2short(mesg[signum].iname)))
1478c80476e4SDavid E. O'Brien 		    goto gotsig;
14796767bd61SMark Peek  	    setname(short2str(sigptr));
1480c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME | ERR_UNKSIG);
1481c80476e4SDavid E. O'Brien 	}
1482c80476e4SDavid E. O'Brien gotsig:
1483c80476e4SDavid E. O'Brien 	v++;
1484c80476e4SDavid E. O'Brien     }
1485c80476e4SDavid E. O'Brien     else
1486c80476e4SDavid E. O'Brien 	signum = SIGTERM;
1487c80476e4SDavid E. O'Brien     pkill(v, signum);
1488c80476e4SDavid E. O'Brien }
1489c80476e4SDavid E. O'Brien 
1490c80476e4SDavid E. O'Brien static void
pkill(Char ** v,int signum)149145e5710bSMark Peek pkill(Char **v, int signum)
1492c80476e4SDavid E. O'Brien {
149323338178SMark Peek     struct process *pp, *np;
1494c80476e4SDavid E. O'Brien     int jobflags = 0, err1 = 0;
1495c80476e4SDavid E. O'Brien     pid_t     pid;
149645e5710bSMark Peek     Char *cp, **vp, **globbed;
1497c80476e4SDavid E. O'Brien 
1498c80476e4SDavid E. O'Brien     /* Avoid globbing %?x patterns */
1499c80476e4SDavid E. O'Brien     for (vp = v; vp && *vp; vp++)
1500c80476e4SDavid E. O'Brien 	if (**vp == '%')
1501c80476e4SDavid E. O'Brien 	    (void) quote(*vp);
1502c80476e4SDavid E. O'Brien 
150345e5710bSMark Peek     v = glob_all_or_error(v);
150445e5710bSMark Peek     globbed = v;
150545e5710bSMark Peek     cleanup_push(globbed, blk_cleanup);
1506c80476e4SDavid E. O'Brien 
15075c3ede85SMark Peek     pchild_disabled++;
15085c3ede85SMark Peek     cleanup_push(&pchild_disabled, disabled_cleanup);
15095c3ede85SMark Peek     if (setintr) {
15105c3ede85SMark Peek 	pintr_disabled++;
15115c3ede85SMark Peek 	cleanup_push(&pintr_disabled, disabled_cleanup);
15125c3ede85SMark Peek     }
1513c80476e4SDavid E. O'Brien 
1514c80476e4SDavid E. O'Brien     while (v && (cp = *v)) {
1515c80476e4SDavid E. O'Brien 	if (*cp == '%') {
1516c80476e4SDavid E. O'Brien 	    np = pp = pfind(cp);
1517c80476e4SDavid E. O'Brien 	    do
1518c80476e4SDavid E. O'Brien 		jobflags |= np->p_flags;
1519c80476e4SDavid E. O'Brien 	    while ((np = np->p_friends) != pp);
1520c80476e4SDavid E. O'Brien #ifdef BSDJOBS
1521c80476e4SDavid E. O'Brien 	    switch (signum) {
1522c80476e4SDavid E. O'Brien 
1523c80476e4SDavid E. O'Brien 	    case SIGSTOP:
1524c80476e4SDavid E. O'Brien 	    case SIGTSTP:
1525c80476e4SDavid E. O'Brien 	    case SIGTTIN:
1526c80476e4SDavid E. O'Brien 	    case SIGTTOU:
1527c80476e4SDavid E. O'Brien 		if ((jobflags & PRUNNING) == 0) {
1528c80476e4SDavid E. O'Brien # ifdef SUSPENDED
1529c80476e4SDavid E. O'Brien 		    xprintf(CGETS(17, 12, "%S: Already suspended\n"), cp);
1530c80476e4SDavid E. O'Brien # else /* !SUSPENDED */
1531c80476e4SDavid E. O'Brien 		    xprintf(CGETS(17, 13, "%S: Already stopped\n"), cp);
1532c80476e4SDavid E. O'Brien # endif /* !SUSPENDED */
1533c80476e4SDavid E. O'Brien 		    err1++;
1534c80476e4SDavid E. O'Brien 		    goto cont;
1535c80476e4SDavid E. O'Brien 		}
1536c80476e4SDavid E. O'Brien 		break;
1537c80476e4SDavid E. O'Brien 		/*
1538c80476e4SDavid E. O'Brien 		 * suspend a process, kill -CONT %, then type jobs; the shell
1539c80476e4SDavid E. O'Brien 		 * says it is suspended, but it is running; thanks jaap..
1540c80476e4SDavid E. O'Brien 		 */
1541c80476e4SDavid E. O'Brien 	    case SIGCONT:
1542c80476e4SDavid E. O'Brien 		if (!pstart(pp, 0)) {
1543c80476e4SDavid E. O'Brien 		    pp->p_procid = 0;
1544c80476e4SDavid E. O'Brien 		    stderror(ERR_NAME|ERR_BADJOB, pp->p_command,
1545c80476e4SDavid E. O'Brien 			     strerror(errno));
1546c80476e4SDavid E. O'Brien 		}
1547c80476e4SDavid E. O'Brien 		goto cont;
1548c80476e4SDavid E. O'Brien 	    default:
1549c80476e4SDavid E. O'Brien 		break;
1550c80476e4SDavid E. O'Brien 	    }
1551c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
1552c80476e4SDavid E. O'Brien 	    if (killpg(pp->p_jobid, signum) < 0) {
1553c80476e4SDavid E. O'Brien 		xprintf("%S: %s\n", cp, strerror(errno));
1554c80476e4SDavid E. O'Brien 		err1++;
1555c80476e4SDavid E. O'Brien 	    }
1556c80476e4SDavid E. O'Brien #ifdef BSDJOBS
1557c80476e4SDavid E. O'Brien 	    if (signum == SIGTERM || signum == SIGHUP)
1558c80476e4SDavid E. O'Brien 		(void) killpg(pp->p_jobid, SIGCONT);
1559c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
1560c80476e4SDavid E. O'Brien 	}
1561c80476e4SDavid E. O'Brien 	else if (!(Isdigit(*cp) || *cp == '-'))
1562c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME | ERR_JOBARGS);
1563c80476e4SDavid E. O'Brien 	else {
1564b2d5d167SMark Peek 	    char *ep;
15653b6eaa7bSAndrey A. Chernov #ifndef WINNT_NATIVE
1566b2d5d167SMark Peek 	    pid = strtol(short2str(cp), &ep, 10);
1567c80476e4SDavid E. O'Brien #else
1568b2d5d167SMark Peek 	    pid = strtoul(short2str(cp), &ep, 0);
15693b6eaa7bSAndrey A. Chernov #endif /* WINNT_NATIVE */
1570b2d5d167SMark Peek 	    if (*ep)
1571b2d5d167SMark Peek 		stderror(ERR_NAME | ERR_JOBARGS);
1572b2d5d167SMark Peek 	    else if (kill(pid, signum) < 0) {
1573c80476e4SDavid E. O'Brien 		xprintf("%d: %s\n", pid, strerror(errno));
1574c80476e4SDavid E. O'Brien 		err1++;
1575c80476e4SDavid E. O'Brien 		goto cont;
1576c80476e4SDavid E. O'Brien 	    }
1577c80476e4SDavid E. O'Brien #ifdef BSDJOBS
1578c80476e4SDavid E. O'Brien 	    if (signum == SIGTERM || signum == SIGHUP)
1579c80476e4SDavid E. O'Brien 		(void) kill(pid, SIGCONT);
1580c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
1581c80476e4SDavid E. O'Brien 	}
1582c80476e4SDavid E. O'Brien cont:
1583c80476e4SDavid E. O'Brien 	v++;
1584c80476e4SDavid E. O'Brien     }
158545e5710bSMark Peek     cleanup_until(&pchild_disabled);
1586c80476e4SDavid E. O'Brien     if (err1)
1587c80476e4SDavid E. O'Brien 	stderror(ERR_SILENT);
1588c80476e4SDavid E. O'Brien }
1589c80476e4SDavid E. O'Brien 
1590c80476e4SDavid E. O'Brien /*
1591c80476e4SDavid E. O'Brien  * pstart - start the job in foreground/background
1592c80476e4SDavid E. O'Brien  */
1593c80476e4SDavid E. O'Brien int
pstart(struct process * pp,int foregnd)159445e5710bSMark Peek pstart(struct process *pp, int foregnd)
1595c80476e4SDavid E. O'Brien {
1596c80476e4SDavid E. O'Brien     int rv = 0;
159723338178SMark Peek     struct process *np;
1598c80476e4SDavid E. O'Brien     /* We don't use jobflags in this function right now (see below) */
1599c80476e4SDavid E. O'Brien     /* long    jobflags = 0; */
1600c80476e4SDavid E. O'Brien 
160145e5710bSMark Peek     pchild_disabled++;
160245e5710bSMark Peek     cleanup_push(&pchild_disabled, disabled_cleanup);
1603c80476e4SDavid E. O'Brien     np = pp;
1604c80476e4SDavid E. O'Brien     do {
1605c80476e4SDavid E. O'Brien 	/* We don't use jobflags in this function right now (see below) */
1606c80476e4SDavid E. O'Brien 	/* jobflags |= np->p_flags; */
1607c80476e4SDavid E. O'Brien 	if (np->p_flags & (PRUNNING | PSTOPPED)) {
1608c80476e4SDavid E. O'Brien 	    np->p_flags |= PRUNNING;
1609c80476e4SDavid E. O'Brien 	    np->p_flags &= ~PSTOPPED;
1610c80476e4SDavid E. O'Brien 	    if (foregnd)
1611c80476e4SDavid E. O'Brien 		np->p_flags |= PFOREGND;
1612c80476e4SDavid E. O'Brien 	    else
1613c80476e4SDavid E. O'Brien 		np->p_flags &= ~PFOREGND;
1614c80476e4SDavid E. O'Brien 	}
1615c80476e4SDavid E. O'Brien     } while ((np = np->p_friends) != pp);
1616c80476e4SDavid E. O'Brien     if (!foregnd)
1617c80476e4SDavid E. O'Brien 	pclrcurr(pp);
1618c80476e4SDavid E. O'Brien     (void) pprint(pp, foregnd ? NAME | JOBDIR : NUMBER | NAME | AMPERSAND);
16196767bd61SMark Peek 
16206767bd61SMark Peek     /* GrP run jobcmd hook if foregrounding */
16216767bd61SMark Peek     if (foregnd) {
16226767bd61SMark Peek 	job_cmd(pp->p_command);
16236767bd61SMark Peek     }
16246767bd61SMark Peek 
1625c80476e4SDavid E. O'Brien #ifdef BSDJOBS
1626c80476e4SDavid E. O'Brien     if (foregnd) {
1627c80476e4SDavid E. O'Brien 	rv = tcsetpgrp(FSHTTY, pp->p_jobid);
1628c80476e4SDavid E. O'Brien     }
1629c80476e4SDavid E. O'Brien     /*
1630c80476e4SDavid E. O'Brien      * 1. child process of csh (shell script) receives SIGTTIN/SIGTTOU
1631c80476e4SDavid E. O'Brien      * 2. parent process (csh) receives SIGCHLD
1632c80476e4SDavid E. O'Brien      * 3. The "csh" signal handling function pchild() is invoked
1633c80476e4SDavid E. O'Brien      *    with a SIGCHLD signal.
1634c80476e4SDavid E. O'Brien      * 4. pchild() calls wait3(WNOHANG) which returns 0.
1635c80476e4SDavid E. O'Brien      *    The child process is NOT ready to be waited for at this time.
1636c80476e4SDavid E. O'Brien      *    pchild() returns without picking-up the correct status
163745e5710bSMark Peek      *    for the child process which generated the SIGCHLD.
1638c80476e4SDavid E. O'Brien      * 5. CONSEQUENCE : csh is UNaware that the process is stopped
1639c80476e4SDavid E. O'Brien      * 6. THIS LINE HAS BEEN COMMENTED OUT : if (jobflags&PSTOPPED)
1640c80476e4SDavid E. O'Brien      * 	  (beto@aixwiz.austin.ibm.com - aug/03/91)
1641c80476e4SDavid E. O'Brien      * 7. I removed the line completely and added extra checks for
1642c80476e4SDavid E. O'Brien      *    pstart, so that if a job gets attached to and dies inside
1643c80476e4SDavid E. O'Brien      *    a debugger it does not confuse the shell. [christos]
1644c80476e4SDavid E. O'Brien      * 8. on the nec sx-4 there seems to be a problem, which requires
1645c80476e4SDavid E. O'Brien      *    a syscall(151, getpid(), getpid()) in osinit. Don't ask me
1646c80476e4SDavid E. O'Brien      *    what this is doing. [schott@rzg.mpg.de]
1647c80476e4SDavid E. O'Brien      */
1648c80476e4SDavid E. O'Brien 
1649c80476e4SDavid E. O'Brien     if (rv != -1)
1650c80476e4SDavid E. O'Brien 	rv = killpg(pp->p_jobid, SIGCONT);
1651c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
165245e5710bSMark Peek     cleanup_until(&pchild_disabled);
1653c80476e4SDavid E. O'Brien     return rv != -1;
1654c80476e4SDavid E. O'Brien }
1655c80476e4SDavid E. O'Brien 
1656c80476e4SDavid E. O'Brien void
panystop(int neednl)165745e5710bSMark Peek panystop(int neednl)
1658c80476e4SDavid E. O'Brien {
165923338178SMark Peek     struct process *pp;
1660c80476e4SDavid E. O'Brien 
1661c80476e4SDavid E. O'Brien     chkstop = 2;
1662c80476e4SDavid E. O'Brien     for (pp = proclist.p_next; pp; pp = pp->p_next)
1663c80476e4SDavid E. O'Brien 	if (pp->p_flags & PSTOPPED)
1664c80476e4SDavid E. O'Brien 	    stderror(ERR_STOPPED, neednl ? "\n" : "");
1665c80476e4SDavid E. O'Brien }
1666c80476e4SDavid E. O'Brien 
1667c80476e4SDavid E. O'Brien struct process *
pfind(Char * cp)166845e5710bSMark Peek pfind(Char *cp)
1669c80476e4SDavid E. O'Brien {
167023338178SMark Peek     struct process *pp, *np;
1671c80476e4SDavid E. O'Brien 
1672c80476e4SDavid E. O'Brien     if (cp == 0 || cp[1] == 0 || eq(cp, STRcent2) || eq(cp, STRcentplus)) {
1673c80476e4SDavid E. O'Brien 	if (pcurrent == NULL)
1674c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME | ERR_JOBCUR);
1675c80476e4SDavid E. O'Brien 	return (pcurrent);
1676c80476e4SDavid E. O'Brien     }
1677c80476e4SDavid E. O'Brien     if (eq(cp, STRcentminus) || eq(cp, STRcenthash)) {
1678c80476e4SDavid E. O'Brien 	if (pprevious == NULL)
1679c80476e4SDavid E. O'Brien 	    stderror(ERR_NAME | ERR_JOBPREV);
1680c80476e4SDavid E. O'Brien 	return (pprevious);
1681c80476e4SDavid E. O'Brien     }
1682c80476e4SDavid E. O'Brien     if (Isdigit(cp[1])) {
1683c80476e4SDavid E. O'Brien 	int     idx = atoi(short2str(cp + 1));
1684c80476e4SDavid E. O'Brien 
1685c80476e4SDavid E. O'Brien 	for (pp = proclist.p_next; pp; pp = pp->p_next)
1686c80476e4SDavid E. O'Brien 	    if (pp->p_index == idx && pp->p_procid == pp->p_jobid)
1687c80476e4SDavid E. O'Brien 		return (pp);
1688c80476e4SDavid E. O'Brien 	stderror(ERR_NAME | ERR_NOSUCHJOB);
1689c80476e4SDavid E. O'Brien     }
1690c80476e4SDavid E. O'Brien     np = NULL;
1691c80476e4SDavid E. O'Brien     for (pp = proclist.p_next; pp; pp = pp->p_next)
1692c80476e4SDavid E. O'Brien 	if (pp->p_procid == pp->p_jobid) {
1693c80476e4SDavid E. O'Brien 	    if (cp[1] == '?') {
169423338178SMark Peek 		Char *dp;
1695c80476e4SDavid E. O'Brien 
1696c80476e4SDavid E. O'Brien 		for (dp = pp->p_command; *dp; dp++) {
1697c80476e4SDavid E. O'Brien 		    if (*dp != cp[2])
1698c80476e4SDavid E. O'Brien 			continue;
1699c80476e4SDavid E. O'Brien 		    if (prefix(cp + 2, dp))
1700c80476e4SDavid E. O'Brien 			goto match;
1701c80476e4SDavid E. O'Brien 		}
1702c80476e4SDavid E. O'Brien 	    }
1703c80476e4SDavid E. O'Brien 	    else if (prefix(cp + 1, pp->p_command)) {
1704c80476e4SDavid E. O'Brien 	match:
1705c80476e4SDavid E. O'Brien 		if (np)
1706c80476e4SDavid E. O'Brien 		    stderror(ERR_NAME | ERR_AMBIG);
1707c80476e4SDavid E. O'Brien 		np = pp;
1708c80476e4SDavid E. O'Brien 	    }
1709c80476e4SDavid E. O'Brien 	}
1710c80476e4SDavid E. O'Brien     if (np)
1711c80476e4SDavid E. O'Brien 	return (np);
1712c80476e4SDavid E. O'Brien     stderror(ERR_NAME | (cp[1] == '?' ? ERR_JOBPAT : ERR_NOSUCHJOB));
1713c80476e4SDavid E. O'Brien     /* NOTREACHED */
1714c80476e4SDavid E. O'Brien     return (0);
1715c80476e4SDavid E. O'Brien }
1716c80476e4SDavid E. O'Brien 
1717c80476e4SDavid E. O'Brien 
1718c80476e4SDavid E. O'Brien /*
1719c80476e4SDavid E. O'Brien  * pgetcurr - find most recent job that is not pp, preferably stopped
1720c80476e4SDavid E. O'Brien  */
1721c80476e4SDavid E. O'Brien static struct process *
pgetcurr(struct process * pp)172245e5710bSMark Peek pgetcurr(struct process *pp)
1723c80476e4SDavid E. O'Brien {
172423338178SMark Peek     struct process *np;
172523338178SMark Peek     struct process *xp = NULL;
1726c80476e4SDavid E. O'Brien 
1727c80476e4SDavid E. O'Brien     for (np = proclist.p_next; np; np = np->p_next)
1728c80476e4SDavid E. O'Brien 	if (np != pcurrent && np != pp && np->p_procid &&
1729c80476e4SDavid E. O'Brien 	    np->p_procid == np->p_jobid) {
1730c80476e4SDavid E. O'Brien 	    if (np->p_flags & PSTOPPED)
1731c80476e4SDavid E. O'Brien 		return (np);
1732c80476e4SDavid E. O'Brien 	    if (xp == NULL)
1733c80476e4SDavid E. O'Brien 		xp = np;
1734c80476e4SDavid E. O'Brien 	}
1735c80476e4SDavid E. O'Brien     return (xp);
1736c80476e4SDavid E. O'Brien }
1737c80476e4SDavid E. O'Brien 
1738c80476e4SDavid E. O'Brien /*
1739c80476e4SDavid E. O'Brien  * donotify - flag the job so as to report termination asynchronously
1740c80476e4SDavid E. O'Brien  */
1741c80476e4SDavid E. O'Brien /*ARGSUSED*/
1742c80476e4SDavid E. O'Brien void
donotify(Char ** v,struct command * c)174345e5710bSMark Peek donotify(Char **v, struct command *c)
1744c80476e4SDavid E. O'Brien {
174523338178SMark Peek     struct process *pp;
1746c80476e4SDavid E. O'Brien 
1747c80476e4SDavid E. O'Brien     USE(c);
1748c80476e4SDavid E. O'Brien     pp = pfind(*++v);
1749c80476e4SDavid E. O'Brien     pp->p_flags |= PNOTIFY;
1750c80476e4SDavid E. O'Brien }
1751c80476e4SDavid E. O'Brien 
175245e5710bSMark Peek #ifdef SIGSYNCH
175345e5710bSMark Peek static void
synch_handler(int sno)175445e5710bSMark Peek synch_handler(int sno)
175545e5710bSMark Peek {
175645e5710bSMark Peek     USE(sno);
175745e5710bSMark Peek }
175845e5710bSMark Peek #endif /* SIGSYNCH */
175945e5710bSMark Peek 
1760c80476e4SDavid E. O'Brien /*
1761c80476e4SDavid E. O'Brien  * Do the fork and whatever should be done in the child side that
1762c80476e4SDavid E. O'Brien  * should not be done if we are not forking at all (like for simple builtin's)
1763c80476e4SDavid E. O'Brien  * Also do everything that needs any signals fiddled with in the parent side
1764c80476e4SDavid E. O'Brien  *
1765c80476e4SDavid E. O'Brien  * Wanttty tells whether process and/or tty pgrps are to be manipulated:
1766c80476e4SDavid E. O'Brien  *	-1:	leave tty alone; inherit pgrp from parent
1767c80476e4SDavid E. O'Brien  *	 0:	already have tty; manipulate process pgrps only
1768c80476e4SDavid E. O'Brien  *	 1:	want to claim tty; manipulate process and tty pgrps
1769c80476e4SDavid E. O'Brien  * It is usually just the value of tpgrp.
1770c80476e4SDavid E. O'Brien  */
1771c80476e4SDavid E. O'Brien 
177245e5710bSMark Peek pid_t
pfork(struct command * t,int wanttty)177345e5710bSMark Peek pfork(struct command *t, int wanttty)
1774c80476e4SDavid E. O'Brien {
177545e5710bSMark Peek     pid_t pid;
177623338178SMark Peek     int    ignint = 0;
177745e5710bSMark Peek     pid_t pgrp;
1778c80476e4SDavid E. O'Brien #ifdef SIGSYNCH
177945e5710bSMark Peek     struct sigaction osa, nsa;
1780c80476e4SDavid E. O'Brien #endif /* SIGSYNCH */
1781c80476e4SDavid E. O'Brien 
1782c80476e4SDavid E. O'Brien     /*
1783c80476e4SDavid E. O'Brien      * A child will be uninterruptible only under very special conditions.
1784c80476e4SDavid E. O'Brien      * Remember that the semantics of '&' is implemented by disconnecting the
1785c80476e4SDavid E. O'Brien      * process from the tty so signals do not need to ignored just for '&'.
1786c80476e4SDavid E. O'Brien      * Thus signals are set to default action for children unless: we have had
1787c80476e4SDavid E. O'Brien      * an "onintr -" (then specifically ignored) we are not playing with
1788c80476e4SDavid E. O'Brien      * signals (inherit action)
1789c80476e4SDavid E. O'Brien      */
1790c80476e4SDavid E. O'Brien     if (setintr)
1791c80476e4SDavid E. O'Brien 	ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
1792c80476e4SDavid E. O'Brien 	    || (gointr && eq(gointr, STRminus));
1793c80476e4SDavid E. O'Brien 
1794c80476e4SDavid E. O'Brien     /*
1795c80476e4SDavid E. O'Brien      * Check for maximum nesting of 16 processes to avoid Forking loops
1796c80476e4SDavid E. O'Brien      */
1797c80476e4SDavid E. O'Brien     if (child == 16)
1798c80476e4SDavid E. O'Brien 	stderror(ERR_NESTING, 16);
1799c80476e4SDavid E. O'Brien #ifdef SIGSYNCH
180045e5710bSMark Peek     nsa.sa_handler = synch_handler;
180145e5710bSMark Peek     sigfillset(&nsa.sa_mask);
180245e5710bSMark Peek     nsa.sa_flags = SA_RESTART;
180345e5710bSMark Peek     if (sigaction(SIGSYNCH, &nsa, &osa))
180445e5710bSMark Peek 	stderror(ERR_SYSTEM, "pfork: sigaction set", strerror(errno));
1805c80476e4SDavid E. O'Brien #endif /* SIGSYNCH */
1806c80476e4SDavid E. O'Brien     /*
180745e5710bSMark Peek      * Hold pchild() until we have the process installed in our table.
1808c80476e4SDavid E. O'Brien      */
1809c80476e4SDavid E. O'Brien     if (wanttty < 0) {
181045e5710bSMark Peek 	pchild_disabled++;
181145e5710bSMark Peek 	cleanup_push(&pchild_disabled, disabled_cleanup);
1812c80476e4SDavid E. O'Brien     }
1813c80476e4SDavid E. O'Brien     while ((pid = fork()) == -1)
1814c80476e4SDavid E. O'Brien 	if (setintr == 0)
1815c80476e4SDavid E. O'Brien 	    (void) sleep(FORKSLEEP);
181645e5710bSMark Peek 	else
1817c80476e4SDavid E. O'Brien 	    stderror(ERR_NOPROC);
1818c80476e4SDavid E. O'Brien     if (pid == 0) {
181945e5710bSMark Peek 	(void)cleanup_push_mark(); /* Never to be popped */
182045e5710bSMark Peek 	pchild_disabled = 0;
1821c80476e4SDavid E. O'Brien 	settimes();
1822c80476e4SDavid E. O'Brien 	pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
1823c80476e4SDavid E. O'Brien 	pflushall();
1824c80476e4SDavid E. O'Brien 	pcurrjob = NULL;
1825c80476e4SDavid E. O'Brien #if !defined(BSDTIMES) && !defined(_SEQUENT_)
1826c80476e4SDavid E. O'Brien 	timesdone = 0;
1827c80476e4SDavid E. O'Brien #endif /* !defined(BSDTIMES) && !defined(_SEQUENT_) */
1828c80476e4SDavid E. O'Brien 	child++;
1829c80476e4SDavid E. O'Brien 	if (setintr) {
1830c80476e4SDavid E. O'Brien 	    setintr = 0;	/* until I think otherwise */
1831c80476e4SDavid E. O'Brien 	    /*
1832c80476e4SDavid E. O'Brien 	     * Children just get blown away on SIGINT, SIGQUIT unless "onintr
1833c80476e4SDavid E. O'Brien 	     * -" seen.
1834c80476e4SDavid E. O'Brien 	     */
1835c80476e4SDavid E. O'Brien 	    (void) signal(SIGINT, ignint ? SIG_IGN : SIG_DFL);
1836c80476e4SDavid E. O'Brien 	    (void) signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL);
1837c80476e4SDavid E. O'Brien #ifdef BSDJOBS
1838c80476e4SDavid E. O'Brien 	    if (wanttty >= 0) {
1839c80476e4SDavid E. O'Brien 		/* make stoppable */
1840c80476e4SDavid E. O'Brien 		(void) signal(SIGTSTP, SIG_DFL);
1841c80476e4SDavid E. O'Brien 		(void) signal(SIGTTIN, SIG_DFL);
1842c80476e4SDavid E. O'Brien 		(void) signal(SIGTTOU, SIG_DFL);
1843c80476e4SDavid E. O'Brien 	    }
1844c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
184545e5710bSMark Peek 	    sigaction(SIGTERM, &parterm, NULL);
1846c80476e4SDavid E. O'Brien 	}
1847c80476e4SDavid E. O'Brien 	else if (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT)) {
1848c80476e4SDavid E. O'Brien 	    (void) signal(SIGINT, SIG_IGN);
1849c80476e4SDavid E. O'Brien 	    (void) signal(SIGQUIT, SIG_IGN);
1850c80476e4SDavid E. O'Brien 	}
1851c80476e4SDavid E. O'Brien #ifdef OREO
185245e5710bSMark Peek 	signal(SIGIO, SIG_IGN);	/* ignore SIGIO in child too */
1853c80476e4SDavid E. O'Brien #endif /* OREO */
1854c80476e4SDavid E. O'Brien 
1855c80476e4SDavid E. O'Brien 	pgetty(wanttty, pgrp);
1856c80476e4SDavid E. O'Brien 	/*
1857c80476e4SDavid E. O'Brien 	 * Nohup and nice apply only to NODE_COMMAND's but it would be nice
1858c80476e4SDavid E. O'Brien 	 * (?!?) if you could say "nohup (foo;bar)" Then the parser would have
1859c80476e4SDavid E. O'Brien 	 * to know about nice/nohup/time
1860c80476e4SDavid E. O'Brien 	 */
1861c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_NOHUP)
1862c80476e4SDavid E. O'Brien 	    (void) signal(SIGHUP, SIG_IGN);
1863c80476e4SDavid E. O'Brien 	if (t->t_dflg & F_NICE) {
1864c80476e4SDavid E. O'Brien 	    int nval = SIGN_EXTEND_CHAR(t->t_nice);
1865*19d2e3deSDmitry Chagin #if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
18666767bd61SMark Peek 	    if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
18676767bd61SMark Peek 		stderror(ERR_SYSTEM, "setpriority", strerror(errno));
1868*19d2e3deSDmitry Chagin #else /* !HAVE_SETPRIORITY || !PRIO_PROCESS */
1869c80476e4SDavid E. O'Brien 	    (void) nice(nval);
1870*19d2e3deSDmitry Chagin #endif /* HAVE_SETPRIORITY  && PRIO_PROCESS */
1871c80476e4SDavid E. O'Brien 	}
1872c80476e4SDavid E. O'Brien #ifdef F_VER
1873c80476e4SDavid E. O'Brien         if (t->t_dflg & F_VER) {
1874c80476e4SDavid E. O'Brien 	    tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
1875c80476e4SDavid E. O'Brien 	    dohash(NULL, NULL);
1876c80476e4SDavid E. O'Brien 	}
1877c80476e4SDavid E. O'Brien #endif /* F_VER */
1878c80476e4SDavid E. O'Brien #ifdef SIGSYNCH
1879c80476e4SDavid E. O'Brien 	/* rfw 8/89 now parent can continue */
1880c80476e4SDavid E. O'Brien 	if (kill(getppid(), SIGSYNCH))
1881c80476e4SDavid E. O'Brien 	    stderror(ERR_SYSTEM, "pfork child: kill", strerror(errno));
1882c80476e4SDavid E. O'Brien #endif /* SIGSYNCH */
1883c80476e4SDavid E. O'Brien 
1884c80476e4SDavid E. O'Brien     }
1885c80476e4SDavid E. O'Brien     else {
1886c80476e4SDavid E. O'Brien #ifdef POSIXJOBS
1887c80476e4SDavid E. O'Brien         if (wanttty >= 0) {
1888c80476e4SDavid E. O'Brien 	    /*
1889c80476e4SDavid E. O'Brien 	     * `Walking' process group fix from Beto Appleton.
1890c80476e4SDavid E. O'Brien 	     * (beto@aixwiz.austin.ibm.com)
1891c80476e4SDavid E. O'Brien 	     * If setpgid fails at this point that means that
1892c80476e4SDavid E. O'Brien 	     * our process leader has died. We flush the current
1893c80476e4SDavid E. O'Brien 	     * job and become the process leader ourselves.
1894c80476e4SDavid E. O'Brien 	     * The parent will figure that out later.
1895c80476e4SDavid E. O'Brien 	     */
1896c80476e4SDavid E. O'Brien 	    pgrp = pcurrjob ? pcurrjob->p_jobid : pid;
1897c80476e4SDavid E. O'Brien 	    if (setpgid(pid, pgrp) == -1 && errno == EPERM) {
1898c80476e4SDavid E. O'Brien 		pcurrjob = NULL;
1899c80476e4SDavid E. O'Brien 		/*
1900c80476e4SDavid E. O'Brien 		 * We don't care if this causes an error here;
1901c80476e4SDavid E. O'Brien 		 * then we are already in the right process group
1902c80476e4SDavid E. O'Brien 		 */
1903c80476e4SDavid E. O'Brien 		(void) setpgid(pid, pgrp = pid);
1904c80476e4SDavid E. O'Brien 	    }
1905c80476e4SDavid E. O'Brien 	}
1906c80476e4SDavid E. O'Brien #endif /* POSIXJOBS */
1907c80476e4SDavid E. O'Brien 	palloc(pid, t);
1908c80476e4SDavid E. O'Brien #ifdef SIGSYNCH
190945e5710bSMark Peek 	{
191045e5710bSMark Peek 	    sigset_t pause_mask;
191145e5710bSMark Peek 
1912c80476e4SDavid E. O'Brien 	/*
1913c80476e4SDavid E. O'Brien 	 * rfw 8/89 Wait for child to own terminal.  Solves half of ugly
1914c80476e4SDavid E. O'Brien 	 * synchronization problem.  With this change, we know that the only
1915c80476e4SDavid E. O'Brien 	 * reason setpgrp to a previous process in a pipeline can fail is that
1916c80476e4SDavid E. O'Brien 	 * the previous process has already exited. Without this hack, he may
1917c80476e4SDavid E. O'Brien 	 * either have exited or not yet started to run.  Two uglies become
1918c80476e4SDavid E. O'Brien 	 * one.
1919c80476e4SDavid E. O'Brien 	 */
192045e5710bSMark Peek 	    sigprocmask(SIG_BLOCK, NULL, &pause);
192145e5710bSMark Peek 	    sigdelset(&pause_mask, SIGCHLD);
192245e5710bSMark Peek 	    sigdelset(&pause_mask, SIGSYNCH);
192345e5710bSMark Peek 	    sigsuspend(&pause_mask);
19249ccc37e3SMark Peek 	    (void)handle_pending_signals();
192545e5710bSMark Peek 	    if (sigaction(SIGSYNCH, &osa, NULL))
192645e5710bSMark Peek 		stderror(ERR_SYSTEM, "pfork parent: sigaction restore",
1927c80476e4SDavid E. O'Brien 			 strerror(errno));
192845e5710bSMark Peek 	}
1929c80476e4SDavid E. O'Brien #endif /* SIGSYNCH */
1930c80476e4SDavid E. O'Brien 
193145e5710bSMark Peek 	if (wanttty < 0)
193245e5710bSMark Peek 	    cleanup_until(&pchild_disabled);
1933c80476e4SDavid E. O'Brien     }
1934c80476e4SDavid E. O'Brien     return (pid);
1935c80476e4SDavid E. O'Brien }
1936c80476e4SDavid E. O'Brien 
1937c80476e4SDavid E. O'Brien static void
okpcntl(void)193845e5710bSMark Peek okpcntl(void)
1939c80476e4SDavid E. O'Brien {
1940c80476e4SDavid E. O'Brien     if (tpgrp == -1)
1941c80476e4SDavid E. O'Brien 	stderror(ERR_JOBCONTROL);
1942c80476e4SDavid E. O'Brien     if (tpgrp == 0)
1943c80476e4SDavid E. O'Brien 	stderror(ERR_JOBCTRLSUB);
1944c80476e4SDavid E. O'Brien }
1945c80476e4SDavid E. O'Brien 
1946c80476e4SDavid E. O'Brien 
1947c80476e4SDavid E. O'Brien static void
setttypgrp(int pgrp)194845e5710bSMark Peek setttypgrp(int pgrp)
1949c80476e4SDavid E. O'Brien {
1950c80476e4SDavid E. O'Brien     /*
1951c80476e4SDavid E. O'Brien      * If we are piping out a builtin, eg. 'echo | more' things can go
1952c80476e4SDavid E. O'Brien      * out of sequence, i.e. the more can run before the echo. This
1953c80476e4SDavid E. O'Brien      * can happen even if we have vfork, since the echo will be forked
1954c80476e4SDavid E. O'Brien      * with the regular fork. In this case, we need to set the tty
1955c80476e4SDavid E. O'Brien      * pgrp ourselves. If that happens, then the process will be still
1956c80476e4SDavid E. O'Brien      * alive. And the tty process group will already be set.
1957c80476e4SDavid E. O'Brien      * This should fix the famous sequent problem as a side effect:
1958c80476e4SDavid E. O'Brien      *    The controlling terminal is lost if all processes in the
1959c80476e4SDavid E. O'Brien      *    terminal process group are zombies. In this case tcgetpgrp()
1960c80476e4SDavid E. O'Brien      *    returns 0. If this happens we must set the terminal process
1961c80476e4SDavid E. O'Brien      *    group again.
1962c80476e4SDavid E. O'Brien      */
1963c80476e4SDavid E. O'Brien     if (tcgetpgrp(FSHTTY) != pgrp) {
1964c80476e4SDavid E. O'Brien #ifdef POSIXJOBS
196545e5710bSMark Peek 	struct sigaction old;
196645e5710bSMark Peek 
1967c80476e4SDavid E. O'Brien         /*
1968c80476e4SDavid E. O'Brien 	 * tcsetpgrp will set SIGTTOU to all the the processes in
1969c80476e4SDavid E. O'Brien 	 * the background according to POSIX... We ignore this here.
1970c80476e4SDavid E. O'Brien 	 */
197145e5710bSMark Peek 	sigaction(SIGTTOU, NULL, &old);
197245e5710bSMark Peek 	signal(SIGTTOU, SIG_IGN);
1973c80476e4SDavid E. O'Brien #endif
1974c80476e4SDavid E. O'Brien 	(void) tcsetpgrp(FSHTTY, pgrp);
1975c80476e4SDavid E. O'Brien # ifdef POSIXJOBS
197645e5710bSMark Peek 	sigaction(SIGTTOU, &old, NULL);
1977c80476e4SDavid E. O'Brien # endif
1978c80476e4SDavid E. O'Brien 
1979c80476e4SDavid E. O'Brien     }
1980c80476e4SDavid E. O'Brien }
1981c80476e4SDavid E. O'Brien 
1982c80476e4SDavid E. O'Brien 
1983c80476e4SDavid E. O'Brien /*
1984c80476e4SDavid E. O'Brien  * if we don't have vfork(), things can still go in the wrong order
1985c80476e4SDavid E. O'Brien  * resulting in the famous 'Stopped (tty output)'. But some systems
1986c80476e4SDavid E. O'Brien  * don't permit the setpgid() call, (these are more recent secure
1987c80476e4SDavid E. O'Brien  * systems such as ibm's aix), when they do. Then we'd rather print
1988c80476e4SDavid E. O'Brien  * an error message than hang the shell!
1989c80476e4SDavid E. O'Brien  * I am open to suggestions how to fix that.
1990c80476e4SDavid E. O'Brien  */
1991c80476e4SDavid E. O'Brien void
pgetty(int wanttty,pid_t pgrp)199245e5710bSMark Peek pgetty(int wanttty, pid_t pgrp)
1993c80476e4SDavid E. O'Brien {
1994c80476e4SDavid E. O'Brien #ifdef BSDJOBS
199545e5710bSMark Peek # ifdef POSIXJOBS
199645e5710bSMark Peek     sigset_t oset, set;
199745e5710bSMark Peek # endif /* POSIXJOBS */
1998c80476e4SDavid E. O'Brien 
199945e5710bSMark Peek     jobdebug_xprintf(("wanttty %d pid %d opgrp%d pgrp %d tpgrp %d\n",
200045e5710bSMark Peek 		      wanttty, (int)getpid(), (int)pgrp, (int)mygetpgrp(),
200145e5710bSMark Peek 		      (int)tcgetpgrp(FSHTTY)));
2002c80476e4SDavid E. O'Brien # ifdef POSIXJOBS
2003c80476e4SDavid E. O'Brien     /*
2004c80476e4SDavid E. O'Brien      * christos: I am blocking the tty signals till I've set things
2005c80476e4SDavid E. O'Brien      * correctly....
2006c80476e4SDavid E. O'Brien      */
200745e5710bSMark Peek     if (wanttty > 0) {
200845e5710bSMark Peek 	sigemptyset(&set);
200945e5710bSMark Peek 	sigaddset(&set, SIGTSTP);
201045e5710bSMark Peek 	sigaddset(&set, SIGTTIN);
201145e5710bSMark Peek 	(void)sigprocmask(SIG_BLOCK, &set, &oset);
201245e5710bSMark Peek 	cleanup_push(&oset, sigprocmask_cleanup);
2013c80476e4SDavid E. O'Brien     }
2014c80476e4SDavid E. O'Brien # endif /* POSIXJOBS */
2015c80476e4SDavid E. O'Brien 
2016c80476e4SDavid E. O'Brien # ifndef POSIXJOBS
2017c80476e4SDavid E. O'Brien     if (wanttty > 0)
2018c80476e4SDavid E. O'Brien 	setttypgrp(pgrp);
2019c80476e4SDavid E. O'Brien # endif /* !POSIXJOBS */
2020c80476e4SDavid E. O'Brien 
2021c80476e4SDavid E. O'Brien     /*
2022c80476e4SDavid E. O'Brien      * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
2023c80476e4SDavid E. O'Brien      * Don't check for tpgrp >= 0 so even non-interactive shells give
2024c80476e4SDavid E. O'Brien      * background jobs process groups Same for the comparison in the other part
2025c80476e4SDavid E. O'Brien      * of the #ifdef
2026c80476e4SDavid E. O'Brien      */
2027c80476e4SDavid E. O'Brien     if (wanttty >= 0) {
2028c80476e4SDavid E. O'Brien 	if (setpgid(0, pgrp) == -1) {
2029c80476e4SDavid E. O'Brien # ifdef POSIXJOBS
2030c80476e4SDavid E. O'Brien 	    /* Walking process group fix; see above */
2031c80476e4SDavid E. O'Brien 	    if (setpgid(0, pgrp = getpid()) == -1) {
2032c80476e4SDavid E. O'Brien # endif /* POSIXJOBS */
2033c80476e4SDavid E. O'Brien 		stderror(ERR_SYSTEM, "setpgid child:\n", strerror(errno));
2034c80476e4SDavid E. O'Brien 		xexit(0);
2035c80476e4SDavid E. O'Brien # ifdef POSIXJOBS
2036c80476e4SDavid E. O'Brien 	    }
2037c80476e4SDavid E. O'Brien 	    wanttty = pgrp;  /* Now we really want the tty, since we became the
2038c80476e4SDavid E. O'Brien 			      * the process group leader
2039c80476e4SDavid E. O'Brien 			      */
2040c80476e4SDavid E. O'Brien # endif /* POSIXJOBS */
2041c80476e4SDavid E. O'Brien 	}
2042c80476e4SDavid E. O'Brien     }
2043c80476e4SDavid E. O'Brien 
2044c80476e4SDavid E. O'Brien # ifdef POSIXJOBS
204545e5710bSMark Peek     if (wanttty > 0) {
2046c80476e4SDavid E. O'Brien 	setttypgrp(pgrp);
204745e5710bSMark Peek 	cleanup_until(&oset);
204845e5710bSMark Peek     }
2049c80476e4SDavid E. O'Brien # endif /* POSIXJOBS */
2050c80476e4SDavid E. O'Brien 
205145e5710bSMark Peek     jobdebug_xprintf(("wanttty %d pid %d pgrp %d tpgrp %d\n",
205245e5710bSMark Peek 		      wanttty, getpid(), mygetpgrp(), tcgetpgrp(FSHTTY)));
2053c80476e4SDavid E. O'Brien 
2054c80476e4SDavid E. O'Brien     if (tpgrp > 0)
2055c80476e4SDavid E. O'Brien 	tpgrp = 0;		/* gave tty away */
2056c80476e4SDavid E. O'Brien #endif /* BSDJOBS */
2057c80476e4SDavid E. O'Brien }
2058