1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.proc.c,v 3.134 2016/09/23 19:17:28 christos Exp $ */
3 * sh.proc.c: Job manipulations
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$tcsh: sh.proc.c,v 3.134 2016/09/23 19:17:28 christos Exp $")
44 #endif /* WINNT_NATIVE */
50 #if defined(_BSD) || (defined(IRIS4D) && __STDC__) || defined(__lucid)
52 #endif /* _BSD || (IRIS4D && __STDC__) || __lucid */
54 # define WTERMSIG(w) (((union wait *) &(w))->w_termsig)
57 # endif /* !BSDWAIT */
58 #endif /* !WTERMSIG */
60 # define WEXITSTATUS(w) (((union wait *) &(w))->w_retcode)
61 #endif /* !WEXITSTATUS */
63 # define WSTOPSIG(w) (((union wait *) &(w))->w_stopsig)
64 #endif /* !WSTOPSIG */
68 # define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
74 # define WCOREDUMP(w) (((union wait *) &(w))->w_coredump)
76 # define WCOREDUMP(w) ((w) & 0200)
77 # endif /* !BSDWAIT */
78 #endif /* !WCOREDUMP */
81 # define jobdebug_xprintf(x) (void)0
82 # define jobdebug_flush() (void)0
84 # define jobdebug_xprintf(s) xprintf s
85 # define jobdebug_flush() flush()
89 * C Shell - functions that manage processes, handling hanging, termination
92 #define BIGINDEX 9 /* largest desirable job index */
96 /* use 'cvxrusage' to get parallel statistics */
97 static struct cvxrusage zru = {{0L, 0L}, {0L, 0L}, 0L, 0L, 0L, 0L,
98 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
99 {0L, 0L}, 0LL, 0LL, 0LL, 0LL, 0L, 0L, 0L,
100 0LL, 0LL, {0L, 0L, 0L, 0L, 0L}};
102 static struct rusage zru;
104 #else /* !BSDTIMES */
106 static struct process_stats zru = {{0L, 0L}, {0L, 0L}, 0, 0, 0, 0, 0, 0, 0,
107 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
108 # else /* !_SEQUENT_ */
110 static struct tms zru = {0, 0, 0, 0}, lru = {0, 0, 0, 0};
112 static struct tms zru = {0L, 0L, 0L, 0L}, lru = {0L, 0L, 0L, 0L};
114 # endif /* !_SEQUENT_ */
115 #endif /* !BSDTIMES */
118 static int timesdone; /* shtimes buffer full ? */
119 #endif /* BSDTIMES */
121 #ifndef RUSAGE_CHILDREN
122 # define RUSAGE_CHILDREN -1
123 #endif /* RUSAGE_CHILDREN */
125 static void pflushall (void);
126 static void pflush (struct process *);
127 static void pfree (struct process *);
128 static void pclrcurr (struct process *);
129 static void morecommand (size_t);
130 static void padd (struct command *);
131 static int pprint (struct process *, int);
132 static void ptprint (struct process *);
133 static void pads (Char *);
134 static void pkill (Char **, int);
135 static struct process *pgetcurr (struct process *);
136 static void okpcntl (void);
137 static void setttypgrp (int);
140 * pchild - call queued by the SIGCHLD signal
141 * indicating that at least one child has terminated or stopped
142 * thus at least one wait system call will definitely return a
143 * childs status. Top level routines (like pwait) must be sure
144 * to mask interrupts when playing with the proclist data structures!
156 #endif /* !BSDWAIT */
160 #else /* !BSDTIMES */
162 struct process_stats ru;
163 struct process_stats cpst1, cpst2;
165 # else /* !_SEQUENT_ */
166 struct tms proctimes;
170 (void) times(&shtimes);
172 # endif /* !_SEQUENT_ */
173 #endif /* !BSDTIMES */
175 jobdebug_xprintf(("pchild()\n"));
178 jobdebug_xprintf(("Waiting...\n"));
180 errno = 0; /* reset, just in case */
186 /* use 'cvxwait' to get parallel statistics */
188 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
190 /* both a wait3 and rusage */
191 # if !defined(BSDWAIT) || defined(NeXT) || defined(MACH) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || (defined(IRIS4D) && SYSVREL <= 3) || defined(__lucid) || defined(__osf__)
192 #ifdef __ANDROID__ /* no wait3, only wait4 */
194 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
197 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
198 #endif /* __ANDROID__ */
200 pid = wait3(&w.w_status,
201 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
202 # endif /* BSDWAIT */
204 # else /* !BSDTIMES */
206 (void) get_process_stats(&tv, PS_SELF, 0, &cpst1);
207 pid = waitpid(-1, &w,
208 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
209 (void) get_process_stats(&tv, PS_SELF, 0, &cpst2);
210 pr_stat_sub(&cpst2, &cpst1, &ru);
211 # else /* !_SEQUENT_ */
213 /* we have a wait3, but no rusage stuff */
214 pid = wait3(&w.w_status,
215 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), 0);
217 pid = waitpid(-1, &w,
218 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
220 # endif /* !_SEQUENT_ */
221 # endif /* !BSDTIMES */
222 # else /* !BSDJOBS */
225 /* both a wait3 and rusage */
227 pid = wait3(&w.w_status, WNOHANG, 0);
230 pid = wait3(&w, WNOHANG, &ru);
232 pid = wait3(&w.w_status, WNOHANG, &ru);
233 # endif /* BSDWAIT */
235 # else /* !BSDTIMES */
236 # ifdef ODT /* For Sco Unix 3.2.0 or ODT 1.0 */
238 pid = waitpid(-1, &w,
239 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
241 # if defined(aiws) || defined(uts)
243 pid = wait3(&w.w_status,
244 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), 0);
245 # endif /* aiws || uts */
248 /* no wait3, therefore no rusage */
249 /* on Sys V, this may hang. I hope it's not going to be a problem */
253 * XXX: for greater than 3 we should use waitpid().
254 * but then again, SVR4 falls into the POSIX/BSDJOBS category.
256 pid = wait(&w.w_status);
257 # endif /* BSDWAIT */
258 # endif /* !HAVEwait3 */
259 # endif /* !BSDTIMES */
260 # endif /* !BSDJOBS */
261 #else /* WINNT_NATIVE */
262 pid = waitpid(-1, &w,
263 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
264 #endif /* WINNT_NATIVE */
266 jobdebug_xprintf(("parent %d pid %d, retval %x termsig %x retcode %x\n",
267 (int)getpid(), (int)pid, w, WTERMSIG(w),
271 if ((pid == 0) || (pid == -1)) {
272 (void)handle_pending_signals();
273 jobdebug_xprintf(("errno == %d\n", errno));
278 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
279 if (pid == pp->p_procid)
281 #if !defined(BSDJOBS) && !defined(WINNT_NATIVE)
282 /* this should never have happened */
283 stderror(ERR_SYNC, pid);
285 #else /* BSDJOBS || WINNT_NATIVE */
287 #endif /* !BSDJOBS && !WINNT_NATIVE */
289 pp->p_flags &= ~(PRUNNING | PSTOPPED | PREPORTED);
291 pp->p_flags |= PSTOPPED;
292 pp->p_reason = WSTOPSIG(w);
295 if (pp->p_flags & (PTIME | PPTIME) || adrof(STRtime))
298 (void) get_process_stats(&pp->p_etime, PS_SELF, NULL, NULL);
299 # else /* !_SEQUENT_ */
300 pp->p_etime = times(&proctimes);
301 # endif /* !_SEQUENT_ */
303 (void) gettimeofday(&pp->p_etime, NULL);
304 #endif /* BSDTIMES */
307 #if defined(BSDTIMES) || defined(_SEQUENT_)
309 #else /* !BSDTIMES && !_SEQUENT_ */
310 (void) times(&proctimes);
311 pp->p_utime = proctimes.tms_cutime - shtimes.tms_cutime;
312 pp->p_stime = proctimes.tms_cstime - shtimes.tms_cstime;
314 #endif /* !BSDTIMES && !_SEQUENT_ */
315 if (WIFSIGNALED(w)) {
316 if (WTERMSIG(w) == SIGINT)
317 pp->p_flags |= PINTERRUPTED;
319 pp->p_flags |= PSIGNALED;
321 pp->p_flags |= PDUMPED;
322 pp->p_reason = WTERMSIG(w);
325 pp->p_reason = WEXITSTATUS(w);
326 if (pp->p_reason != 0)
327 pp->p_flags |= PAEXITED;
329 pp->p_flags |= PNEXITED;
335 if ((fp->p_flags & (PPTIME | PRUNNING | PSTOPPED)) == 0 &&
336 !child && adrof(STRtime) &&
338 fp->p_rusage.ru_utime.tv_sec + fp->p_rusage.ru_stime.tv_sec
339 #else /* !BSDTIMES */
341 fp->p_rusage.ps_utime.tv_sec + fp->p_rusage.ps_stime.tv_sec
342 # else /* !_SEQUENT_ */
344 (fp->p_utime + fp->p_stime) / HZ
346 (fp->p_utime + fp->p_stime) / clk_tck
348 # endif /* !_SEQUENT_ */
349 #endif /* !BSDTIMES */
350 >= atoi(short2str(varval(STRtime))))
351 fp->p_flags |= PTIME;
352 jobflags |= fp->p_flags;
353 } while ((fp = fp->p_friends) != pp);
354 pp->p_flags &= ~PFOREGND;
355 if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
356 pp->p_flags &= ~PPTIME;
357 pp->p_flags |= PTIME;
359 if ((jobflags & (PRUNNING | PREPORTED)) == 0) {
362 if (fp->p_flags & PSTOPPED)
363 fp->p_flags |= PREPORTED;
364 } while ((fp = fp->p_friends) != pp);
365 while (fp->p_procid != fp->p_jobid)
367 if (jobflags & PSTOPPED) {
368 if (pcurrent && pcurrent != fp)
369 pprevious = pcurrent;
374 if (jobflags & PFOREGND) {
375 if (!(jobflags & (PSIGNALED | PSTOPPED | PPTIME) ||
377 jobflags & PAEXITED ||
380 !eq(dcwd->di_name, fp->p_cwd->di_name))) {
381 /* PWP: print a newline after ^C */
382 if (jobflags & PINTERRUPTED) {
383 xputchar('\r' | QUOTE);
387 else if ((jobflags & (PTIME|PSTOPPED)) == PTIME)
393 if (jobflags & PNOTIFY || adrof(STRnotify)) {
394 xputchar('\r' | QUOTE);
396 (void) pprint(pp, NUMBER | NAME | REASON);
397 if ((jobflags & PSTOPPED) == 0)
404 * don't really want to do that, because it
405 * will erase our message in case of multi-line
415 fp->p_flags |= PNEEDNOTE;
420 #if defined(BSDJOBS) || defined(HAVEwait3) ||defined(WINNT_NATIVE)
422 #endif /* BSDJOBS || HAVEwait3 */
434 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next) {
435 if (pp->p_flags & PNEEDNOTE) {
437 cleanup_push(&pchild_disabled, disabled_cleanup);
438 pp->p_flags &= ~PNEEDNOTE;
439 flags = pprint(pp, NUMBER | NAME | REASON);
440 if ((flags & (PRUNNING | PSTOPPED)) == 0)
442 cleanup_until(&pchild_disabled);
449 pfree(struct process *pp)
451 xfree(pp->p_command);
452 if (pp->p_cwd && --pp->p_cwd->di_count == 0)
453 if (pp->p_cwd->di_next == 0)
460 * pwait - wait for current job to terminate, maintaining integrity
461 * of current and previous job indicators.
466 struct process *fp, *pp;
469 * Here's where dead procs get flushed.
471 for (pp = (fp = &proclist)->p_next; pp != NULL; pp = (fp = pp)->p_next)
472 if (pp->p_procid == 0) {
473 fp->p_next = pp->p_next;
482 * pjwait - wait for a job to finish or become stopped
483 * It is assumed to be in the foreground state (PFOREGND)
486 pjwait(struct process *pp)
489 int jobflags, reason;
490 sigset_t oset, set, pause_mask;
493 while (pp->p_procid != pp->p_jobid)
498 if ((fp->p_flags & (PFOREGND | PRUNNING)) == PRUNNING)
499 xprintf("%s", CGETS(17, 1, "BUG: waiting for background job!\n"));
500 } while ((fp = fp->p_friends) != pp);
502 * Now keep pausing as long as we are not interrupted (SIGINT), and the
503 * target process, or any of its friends, are running
507 sigaddset(&set, SIGINT);
508 sigaddset(&set, SIGCHLD);
509 (void)sigprocmask(SIG_BLOCK, &set, &oset);
510 cleanup_push(&oset, sigprocmask_cleanup);
512 sigdelset(&pause_mask, SIGCHLD);
513 sigaddset(&pause_mask, SIGINT);
515 (void)handle_pending_signals();
518 jobflags |= fp->p_flags;
519 while ((fp = (fp->p_friends)) != pp);
520 if ((jobflags & PRUNNING) == 0)
522 jobdebug_xprintf(("%d starting to sigsuspend for SIGCHLD on %d\n",
523 getpid(), fp->p_procid));
524 sigsuspend(&pause_mask);
526 cleanup_until(&oset);
527 jobdebug_xprintf(("%d returned from sigsuspend loop\n", getpid()));
529 if (tpgrp > 0) /* get tty back */
530 (void) tcsetpgrp(FSHTTY, tpgrp);
532 if ((jobflags & (PSIGNALED | PSTOPPED | PTIME)) ||
533 fp->p_cwd == NULL || !eq(dcwd->di_name, fp->p_cwd->di_name)) {
534 if (jobflags & PSTOPPED) {
536 if (adrof(STRlistjobs)) {
539 jobcommand[0] = STRjobs;
540 if (eq(varval(STRlistjobs), STRlong))
541 jobcommand[1] = STRml;
543 jobcommand[1] = NULL;
544 jobcommand[2] = NULL;
546 dojobs(jobcommand, NULL);
547 (void) pprint(pp, SHELLDIR);
550 (void) pprint(pp, AREASON | SHELLDIR);
553 (void) pprint(pp, AREASON | SHELLDIR);
555 if ((jobflags & (PINTERRUPTED | PSTOPPED)) && setintr &&
556 (!gointr || !eq(gointr, STRminus))) {
557 if ((jobflags & PSTOPPED) == 0)
565 /* In case of pipelines only the result of the last
566 * command should be taken in account */
567 if (!anyerror && !(fp->p_flags & PBRACE)
568 && ((fp->p_flags & PPOU) || (fp->p_flags & PBACKQ)))
571 reason = fp->p_flags & (PSIGNALED | PINTERRUPTED) ?
572 fp->p_reason | META : fp->p_reason;
573 } while ((fp = fp->p_friends) != pp);
575 * Don't report on backquoted jobs, cause it will mess up
578 if ((reason != 0) && (adrof(STRprintexitvalue)) &&
579 (pp->p_flags & PBACKQ) == 0)
580 xprintf(CGETS(17, 2, "Exit %d\n"), reason);
581 reason_str = putn((tcsh_number_t)reason);
582 cleanup_push(reason_str, xfree);
583 setv(STRstatus, reason_str, VAR_READWRITE);
584 cleanup_ignore(reason_str);
585 cleanup_until(reason_str);
586 if (reason && exiterr)
592 * dowait - wait for all processes to finish
597 dowait(Char **v, struct command *c)
601 /* the current block mask to be able to restore */
604 /* block mask for critical section: OLD_MASK U {SIGCHLD} */
607 /* ignore those during blocking sigsuspend:
608 OLD_MASK / {SIGCHLD, possibly(SIGINT)} */
611 int opintr_disabled, gotsig;
617 sigprocmask(SIG_BLOCK, NULL, &pause_mask);
618 sigdelset(&pause_mask, SIGCHLD);
620 sigdelset(&pause_mask, SIGINT);
622 /* critical section, block also SIGCHLD */
623 sigprocmask(SIG_BLOCK, NULL, &block_mask);
624 sigaddset(&block_mask, SIGCHLD);
625 sigprocmask(SIG_BLOCK, &block_mask, &old_mask);
627 /* detect older SIGCHLDs and remove PRUNNING flag from proclist */
628 (void)handle_pending_signals();
631 for (pp = proclist.p_next; pp; pp = pp->p_next)
632 if (pp->p_procid && /* pp->p_procid == pp->p_jobid && */
633 pp->p_flags & PRUNNING) {
634 /* wait for (or pick up alredy blocked) SIGCHLD */
635 sigsuspend(&pause_mask);
637 /* make the 'wait' interuptable by CTRL-C */
638 opintr_disabled = pintr_disabled;
640 gotsig = handle_pending_signals();
641 pintr_disabled = opintr_disabled;
648 sigprocmask(SIG_SETMASK, &old_mask, NULL);
652 * pflushall - flush all jobs from list (e.g. at fork())
659 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
665 * pflush - flag all process structures in the same job as the
666 * the argument process for deletion. The actual free of the
667 * space is not done here since pflush is called at interrupt level.
670 pflush(struct process *pp)
675 if (pp->p_procid == 0) {
676 xprintf("%s", CGETS(17, 3, "BUG: process flushed twice"));
679 while (pp->p_procid != pp->p_jobid)
687 np->p_index = np->p_procid = 0;
688 np->p_flags &= ~PNEEDNOTE;
689 } while ((np = np->p_friends) != pp);
690 if (idx == pmaxindex) {
691 for (np = proclist.p_next, idx = 0; np; np = np->p_next)
692 if (np->p_index > idx)
699 * pclrcurr - make sure the given job is not the current or previous job;
700 * pp MUST be the job leader
703 pclrcurr(struct process *pp)
705 if (pp == pcurrent) {
706 if (pprevious != NULL) {
707 pcurrent = pprevious;
708 pprevious = pgetcurr(pp);
711 pcurrent = pgetcurr(pp);
712 pprevious = pgetcurr(pp);
715 else if (pp == pprevious)
716 pprevious = pgetcurr(pp);
719 /* +4 here is 1 for '\0', 1 ea for << >& >> */
721 static size_t cmdmax;
722 static size_t cmdlen;
724 #define CMD_INIT 1024
728 morecommand(size_t s)
734 ncmdstr = xrealloc(cmdstr, cmdmax * sizeof(*cmdstr));
735 d = ncmdstr - cmdstr;
741 * unparse - Export padd() functionality
744 unparse(struct command *t)
747 morecommand(CMD_INIT);
752 return Strsave(cmdstr);
757 * palloc - allocate a process structure and fill it up.
758 * an important assumption is made that the process is running.
761 palloc(pid_t pid, struct command *t)
766 pp = xcalloc(1, sizeof(struct process));
768 pp->p_parentid = shpgrp;
769 pp->p_flags = ((t->t_dflg & F_AMPERSAND) ? 0 : PFOREGND) | PRUNNING;
770 if (t->t_dflg & F_TIME)
771 pp->p_flags |= PPTIME;
772 if (t->t_dflg & F_BACKQ)
773 pp->p_flags |= PBACKQ;
774 if (t->t_dflg & F_HUP)
776 if (t->t_dcom && t->t_dcom[0] && (*t->t_dcom[0] == '{'))
777 pp->p_flags |= PBRACE;
779 morecommand(CMD_INIT);
784 if (t->t_dflg & F_PIPEOUT) {
786 if (t->t_dflg & F_STDERR)
787 pp->p_flags |= PDIAG;
789 pp->p_command = Strsave(cmdstr);
793 /* careful here with interrupt level */
795 pp->p_index = pcurrjob->p_index;
796 pp->p_friends = pcurrjob;
797 pp->p_jobid = pcurrjob->p_procid;
798 for (fp = pcurrjob; fp->p_friends != pcurrjob; fp = fp->p_friends)
808 if (pmaxindex < BIGINDEX)
809 pp->p_index = ++pmaxindex;
814 for (np = proclist.p_next; np; np = np->p_next)
815 if (np->p_index == i)
824 if (pcurrent == NULL)
826 else if (pprevious == NULL)
829 pp->p_next = proclist.p_next;
830 proclist.p_next = pp;
832 (void) gettimeofday(&pp->p_btime, NULL);
833 #else /* !BSDTIMES */
835 (void) get_process_stats(&pp->p_btime, PS_SELF, NULL, NULL);
836 # else /* !_SEQUENT_ */
840 pp->p_btime = times(&tmptimes);
842 # endif /* !_SEQUENT_ */
843 #endif /* !BSDTIMES */
847 padd(struct command *t)
862 for (argp = t->t_dcom; *argp; argp++) {
896 if ((t->t_dflg & F_PIPEIN) == 0 && t->t_dlef) {
897 pads((t->t_dflg & F_READ) ? STRspLarrow2sp : STRspLarrowsp);
900 if ((t->t_dflg & F_PIPEOUT) == 0 && t->t_drit) {
901 pads((t->t_dflg & F_APPEND) ? STRspRarrow2 : STRspRarrow);
902 if (t->t_dflg & F_STDERR)
915 * Avoid the Quoted Space alias hack! Reported by:
916 * sam@john-bigboote.ICS.UCI.EDU (Sam Horrocks)
918 if (cp[0] == STRQNULL[0])
923 len = cmdlen + i + CMD_INCR;
926 (void) Strcpy(cmdp, cp);
932 * psavejob - temporarily save the current job on a one level stack
933 * so another job can be created. Used for { } in exp6
934 * and `` in globbing.
944 psavejob_cleanup(void *dummy)
952 * pendjob - indicate that a job (set of commands) has been completed
953 * or is about to begin.
958 struct process *pp, *tp;
960 if (pcurrjob && (pcurrjob->p_flags & (PFOREGND | PSTOPPED)) == 0) {
963 while (pp->p_procid != pp->p_jobid)
965 xprintf("[%d]", pp->p_index);
968 xprintf(" %d", pp->p_procid);
973 pholdjob = pcurrjob = 0;
977 * pprint - print a job
981 * Hacks have been added for SVR4 to deal with pipe's being spawned in
984 * David Dawes (dawes@physics.su.oz.au) Oct 1991
988 pprint(struct process *pp, int flag)
992 int jobflags, pstatus, pcond;
997 struct process *pipehead = NULL, *pipetail = NULL, *pmarker = NULL;
999 #endif /* BACKPIPE */
1001 while (pp->p_procid != pp->p_jobid)
1003 if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
1004 pp->p_flags &= ~PPTIME;
1005 pp->p_flags |= PTIME;
1008 status = reason = -1;
1011 /* Print status to stderr, except for jobs built-in */
1012 haderr = !(flag & JOBLIST);
1016 * The pipeline is reversed, so locate the real head of the pipeline
1017 * if pp is at the tail of a pipe (and not already in a pipeline)
1019 if ((pp->p_friends->p_flags & PPOU) && !inpipe && (flag & NAME)) {
1024 while (pp->p_friends->p_flags & PPOU);
1028 * pmarker is used to hold the place of the proc being processed, so
1029 * we can search for the next one downstream later.
1032 pcond = (tp != pp || (inpipe && tp == pp));
1033 #else /* !BACKPIPE */
1035 #endif /* BACKPIPE */
1037 jobflags |= pp->p_flags;
1038 pstatus = (int) (pp->p_flags & PALLSTATES);
1039 if (pcond && linp != linbuf && !(flag & FANCY) &&
1040 ((pstatus == status && pp->p_reason == reason) ||
1044 if (pcond && linp != linbuf)
1046 if (flag & NUMBER) {
1048 pcond = ((pp == tp && !inpipe) ||
1049 (inpipe && pipetail == tp && pp == pipehead));
1050 #else /* BACKPIPE */
1052 #endif /* BACKPIPE */
1054 xprintf("[%d]%s %c ", pp->p_index,
1055 pp->p_index < 10 ? " " : "",
1056 pp == pcurrent ? '+' :
1057 (pp == pprevious ? '-' : ' '));
1062 xprintf("%5d ", pp->p_procid);
1064 xprintf("%11s ", sitename(pp->p_procid));
1067 if (flag & (REASON | AREASON)) {
1072 if (pstatus == status) {
1073 if (pp->p_reason == reason) {
1074 xprintf(format, "");
1078 reason = (int) pp->p_reason;
1082 reason = (int) pp->p_reason;
1087 xprintf(format, CGETS(17, 4, "Running "));
1094 * tell what happened to the background job
1095 * From: Michael Schroeder
1096 * <mlschroe@immd4.informatik.uni-erlangen.de>
1099 || ((flag & AREASON)
1101 && (reason != SIGPIPE
1102 || (pp->p_flags & PPOU) == 0))) {
1107 ptr = (char *)(intptr_t)mesg[pp->p_reason & 0177].pname;
1109 ptr = xasprintf("%s %d", CGETS(17, 5, "Signal"),
1110 pp->p_reason & 0177);
1111 cleanup_push(ptr, xfree);
1114 xprintf(format, ptr);
1124 if (flag & REASON) {
1126 xprintf(CGETS(17, 6, "Exit %-25d"), pp->p_reason);
1128 xprintf(format, CGETS(17, 7, "Done"));
1133 xprintf(CGETS(17, 8, "BUG: status=%-9o"),
1140 xprintf("%S", pp->p_command);
1141 if (pp->p_flags & PPOU)
1143 if (pp->p_flags & PDIAG)
1146 if (flag & (REASON | AREASON) && pp->p_flags & PDUMPED)
1147 xprintf("%s", CGETS(17, 9, " (core dumped)"));
1148 if (tp == pp->p_friends) {
1149 if (flag & AMPERSAND)
1151 if (flag & JOBDIR &&
1152 !eq(tp->p_cwd->di_name, dcwd->di_name)) {
1153 xprintf("%s", CGETS(17, 10, " (wd: "));
1154 dtildepr(tp->p_cwd->di_name);
1158 if (pp->p_flags & PPTIME && !(status & (PSTOPPED | PRUNNING))) {
1161 #if defined(BSDTIMES) || defined(_SEQUENT_)
1162 prusage(&zru, &pp->p_rusage, &pp->p_etime,
1164 #else /* !BSDTIMES && !SEQUENT */
1165 lru.tms_utime = pp->p_utime;
1166 lru.tms_stime = pp->p_stime;
1169 prusage(&zru, &lru, pp->p_etime,
1171 #endif /* !BSDTIMES && !SEQUENT */
1175 pcond = ((tp == pp->p_friends && !inpipe) ||
1176 (inpipe && pipehead->p_friends == tp && pp == pipetail));
1177 #else /* !BACKPIPE */
1178 pcond = (tp == pp->p_friends);
1179 #endif /* BACKPIPE */
1183 if (flag & SHELLDIR && !eq(tp->p_cwd->di_name, dcwd->di_name)) {
1184 xprintf("%s", CGETS(17, 11, "(wd now: "));
1185 dtildepr(dcwd->di_name);
1192 * if pmaker == pipetail, we are finished that pipeline, and
1193 * can now skip to past the head
1195 if (pmarker == pipetail) {
1201 * set pp to one before the one we want next, so the while below
1202 * increments to the correct spot.
1206 while (pp->p_friends->p_friends != pmarker);
1207 pmarker = pp->p_friends;
1210 pcond = ((pp = pp->p_friends) != tp || inpipe);
1211 #else /* !BACKPIPE */
1212 pcond = ((pp = pp->p_friends) != tp);
1213 #endif /* BACKPIPE */
1216 if (jobflags & PTIME && (jobflags & (PSTOPPED | PRUNNING)) == 0) {
1217 if (jobflags & NUMBER)
1226 * All 4.3 BSD derived implementations are buggy and I've had enough.
1227 * The following implementation produces similar code and works in all
1228 * cases. The 4.3BSD one works only for <, >, !=
1231 # define timercmp(tvp, uvp, cmp) \
1232 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1233 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1234 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1237 ptprint(struct process *tp)
1240 struct timeval tetime, diff;
1241 static struct timeval ztime;
1242 struct sysrusage ru;
1243 struct process *pp = tp;
1248 ruadd(&ru, &pp->p_rusage);
1249 tvsub(&diff, &pp->p_etime, &pp->p_btime);
1250 if (timercmp(&diff, &tetime, >))
1252 } while ((pp = pp->p_friends) != tp);
1253 prusage(&zru, &ru, &tetime, &ztime);
1254 #else /* !BSDTIMES */
1256 timeval_t tetime, diff;
1257 static timeval_t ztime;
1258 struct process_stats ru;
1259 struct process *pp = tp;
1264 ruadd(&ru, &pp->p_rusage);
1265 tvsub(&diff, &pp->p_etime, &pp->p_btime);
1266 if (timercmp(&diff, &tetime, >))
1268 } while ((pp = pp->p_friends) != tp);
1269 prusage(&zru, &ru, &tetime, &ztime);
1270 # else /* !_SEQUENT_ */
1272 static time_t ztime = 0;
1273 static time_t zu_time = 0;
1274 static time_t zs_time = 0;
1275 time_t tetime, diff;
1276 time_t u_time, s_time;
1279 static clock_t ztime = 0;
1280 static clock_t zu_time = 0;
1281 static clock_t zs_time = 0;
1282 clock_t tetime, diff;
1283 clock_t u_time, s_time;
1286 struct tms zts, rts;
1287 struct process *pp = tp;
1293 u_time += pp->p_utime;
1294 s_time += pp->p_stime;
1295 diff = pp->p_etime - pp->p_btime;
1298 } while ((pp = pp->p_friends) != tp);
1299 zts.tms_utime = zu_time;
1300 zts.tms_stime = zs_time;
1303 rts.tms_utime = u_time;
1304 rts.tms_stime = s_time;
1307 prusage(&zts, &rts, tetime, ztime);
1308 # endif /* !_SEQUENT_ */
1309 #endif /* !BSDTIMES */
1313 * dojobs - print all jobs
1317 dojobs(Char **v, struct command *c)
1320 int flag = NUMBER | NAME | REASON | JOBLIST;
1327 if (v[1] || !eq(*v, STRml))
1329 flag |= FANCY | JOBDIR;
1331 for (i = 1; i <= pmaxindex; i++)
1332 for (pp = proclist.p_next; pp; pp = pp->p_next)
1333 if (pp->p_index == i && pp->p_procid == pp->p_jobid) {
1334 pp->p_flags &= ~PNEEDNOTE;
1335 if (!(pprint(pp, flag) & (PRUNNING | PSTOPPED)))
1342 * dofg - builtin - put the job into the foreground
1346 dofg(Char **v, struct command *c)
1355 if (!pstart(pp, 1)) {
1357 stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1361 } while (*v && *++v);
1365 * %... - builtin - put the job into the foreground
1369 dofg1(Char **v, struct command *c)
1376 if (!pstart(pp, 1)) {
1378 stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1385 * dobg - builtin - put the job into the background
1389 dobg(Char **v, struct command *c)
1398 if (!pstart(pp, 0)) {
1400 stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1402 } while (*v && *++v);
1406 * %... & - builtin - put the job into the background
1410 dobg1(Char **v, struct command *c)
1416 if (!pstart(pp, 0)) {
1418 stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1423 * dostop - builtin - stop the job
1427 dostop(Char **v, struct command *c)
1431 pkill(++v, SIGSTOP);
1432 #endif /* BSDJOBS */
1436 * dokill - builtin - superset of kill (1)
1440 dokill(Char **v, struct command *c)
1442 int signum, len = 0;
1448 if (v[0] && v[0][0] == '-') {
1449 if (v[0][1] == 'l') {
1450 for (signum = 0; signum <= nsig; signum++) {
1451 if ((name = mesg[signum].iname) != NULL) {
1452 len += strlen(name) + 1;
1453 if (len >= TermH - 1) {
1455 len = strlen(name) + 1;
1457 xprintf("%s ", name);
1464 if (v[0][1] == 's') {
1469 stderror(ERR_NAME | ERR_TOOFEW);
1472 if (Isdigit(*sigptr)) {
1474 signum = strtoul(short2str(sigptr), &ep, 0);
1475 if (*ep || signum < 0 || signum > (MAXSIG-1))
1476 stderror(ERR_NAME | ERR_BADSIG);
1479 for (signum = 0; signum <= nsig; signum++)
1480 if (mesg[signum].iname &&
1481 eq(sigptr, str2short(mesg[signum].iname)))
1483 setname(short2str(sigptr));
1484 stderror(ERR_NAME | ERR_UNKSIG);
1495 pkill(Char **v, int signum)
1497 struct process *pp, *np;
1498 int jobflags = 0, err1 = 0;
1500 Char *cp, **vp, **globbed;
1502 /* Avoid globbing %?x patterns */
1503 for (vp = v; vp && *vp; vp++)
1507 v = glob_all_or_error(v);
1509 cleanup_push(globbed, blk_cleanup);
1512 cleanup_push(&pchild_disabled, disabled_cleanup);
1515 cleanup_push(&pintr_disabled, disabled_cleanup);
1518 while (v && (cp = *v)) {
1520 np = pp = pfind(cp);
1522 jobflags |= np->p_flags;
1523 while ((np = np->p_friends) != pp);
1531 if ((jobflags & PRUNNING) == 0) {
1533 xprintf(CGETS(17, 12, "%S: Already suspended\n"), cp);
1534 # else /* !SUSPENDED */
1535 xprintf(CGETS(17, 13, "%S: Already stopped\n"), cp);
1536 # endif /* !SUSPENDED */
1542 * suspend a process, kill -CONT %, then type jobs; the shell
1543 * says it is suspended, but it is running; thanks jaap..
1546 if (!pstart(pp, 0)) {
1548 stderror(ERR_NAME|ERR_BADJOB, pp->p_command,
1555 #endif /* BSDJOBS */
1556 if (killpg(pp->p_jobid, signum) < 0) {
1557 xprintf("%S: %s\n", cp, strerror(errno));
1561 if (signum == SIGTERM || signum == SIGHUP)
1562 (void) killpg(pp->p_jobid, SIGCONT);
1563 #endif /* BSDJOBS */
1565 else if (!(Isdigit(*cp) || *cp == '-'))
1566 stderror(ERR_NAME | ERR_JOBARGS);
1569 #ifndef WINNT_NATIVE
1570 pid = strtol(short2str(cp), &ep, 10);
1572 pid = strtoul(short2str(cp), &ep, 0);
1573 #endif /* WINNT_NATIVE */
1575 stderror(ERR_NAME | ERR_JOBARGS);
1576 else if (kill(pid, signum) < 0) {
1577 xprintf("%d: %s\n", pid, strerror(errno));
1582 if (signum == SIGTERM || signum == SIGHUP)
1583 (void) kill(pid, SIGCONT);
1584 #endif /* BSDJOBS */
1589 cleanup_until(&pchild_disabled);
1591 stderror(ERR_SILENT);
1595 * pstart - start the job in foreground/background
1598 pstart(struct process *pp, int foregnd)
1602 /* We don't use jobflags in this function right now (see below) */
1603 /* long jobflags = 0; */
1606 cleanup_push(&pchild_disabled, disabled_cleanup);
1609 /* We don't use jobflags in this function right now (see below) */
1610 /* jobflags |= np->p_flags; */
1611 if (np->p_flags & (PRUNNING | PSTOPPED)) {
1612 np->p_flags |= PRUNNING;
1613 np->p_flags &= ~PSTOPPED;
1615 np->p_flags |= PFOREGND;
1617 np->p_flags &= ~PFOREGND;
1619 } while ((np = np->p_friends) != pp);
1622 (void) pprint(pp, foregnd ? NAME | JOBDIR : NUMBER | NAME | AMPERSAND);
1624 /* GrP run jobcmd hook if foregrounding */
1626 job_cmd(pp->p_command);
1631 rv = tcsetpgrp(FSHTTY, pp->p_jobid);
1634 * 1. child process of csh (shell script) receives SIGTTIN/SIGTTOU
1635 * 2. parent process (csh) receives SIGCHLD
1636 * 3. The "csh" signal handling function pchild() is invoked
1637 * with a SIGCHLD signal.
1638 * 4. pchild() calls wait3(WNOHANG) which returns 0.
1639 * The child process is NOT ready to be waited for at this time.
1640 * pchild() returns without picking-up the correct status
1641 * for the child process which generated the SIGCHLD.
1642 * 5. CONSEQUENCE : csh is UNaware that the process is stopped
1643 * 6. THIS LINE HAS BEEN COMMENTED OUT : if (jobflags&PSTOPPED)
1644 * (beto@aixwiz.austin.ibm.com - aug/03/91)
1645 * 7. I removed the line completely and added extra checks for
1646 * pstart, so that if a job gets attached to and dies inside
1647 * a debugger it does not confuse the shell. [christos]
1648 * 8. on the nec sx-4 there seems to be a problem, which requires
1649 * a syscall(151, getpid(), getpid()) in osinit. Don't ask me
1650 * what this is doing. [schott@rzg.mpg.de]
1654 rv = killpg(pp->p_jobid, SIGCONT);
1655 #endif /* BSDJOBS */
1656 cleanup_until(&pchild_disabled);
1661 panystop(int neednl)
1666 for (pp = proclist.p_next; pp; pp = pp->p_next)
1667 if (pp->p_flags & PSTOPPED)
1668 stderror(ERR_STOPPED, neednl ? "\n" : "");
1674 struct process *pp, *np;
1676 if (cp == 0 || cp[1] == 0 || eq(cp, STRcent2) || eq(cp, STRcentplus)) {
1677 if (pcurrent == NULL)
1678 stderror(ERR_NAME | ERR_JOBCUR);
1681 if (eq(cp, STRcentminus) || eq(cp, STRcenthash)) {
1682 if (pprevious == NULL)
1683 stderror(ERR_NAME | ERR_JOBPREV);
1686 if (Isdigit(cp[1])) {
1687 int idx = atoi(short2str(cp + 1));
1689 for (pp = proclist.p_next; pp; pp = pp->p_next)
1690 if (pp->p_index == idx && pp->p_procid == pp->p_jobid)
1692 stderror(ERR_NAME | ERR_NOSUCHJOB);
1695 for (pp = proclist.p_next; pp; pp = pp->p_next)
1696 if (pp->p_procid == pp->p_jobid) {
1700 for (dp = pp->p_command; *dp; dp++) {
1703 if (prefix(cp + 2, dp))
1707 else if (prefix(cp + 1, pp->p_command)) {
1710 stderror(ERR_NAME | ERR_AMBIG);
1716 stderror(ERR_NAME | (cp[1] == '?' ? ERR_JOBPAT : ERR_NOSUCHJOB));
1723 * pgetcurr - find most recent job that is not pp, preferably stopped
1725 static struct process *
1726 pgetcurr(struct process *pp)
1729 struct process *xp = NULL;
1731 for (np = proclist.p_next; np; np = np->p_next)
1732 if (np != pcurrent && np != pp && np->p_procid &&
1733 np->p_procid == np->p_jobid) {
1734 if (np->p_flags & PSTOPPED)
1743 * donotify - flag the job so as to report termination asynchronously
1747 donotify(Char **v, struct command *c)
1753 pp->p_flags |= PNOTIFY;
1758 synch_handler(int sno)
1762 #endif /* SIGSYNCH */
1765 * Do the fork and whatever should be done in the child side that
1766 * should not be done if we are not forking at all (like for simple builtin's)
1767 * Also do everything that needs any signals fiddled with in the parent side
1769 * Wanttty tells whether process and/or tty pgrps are to be manipulated:
1770 * -1: leave tty alone; inherit pgrp from parent
1771 * 0: already have tty; manipulate process pgrps only
1772 * 1: want to claim tty; manipulate process and tty pgrps
1773 * It is usually just the value of tpgrp.
1777 pfork(struct command *t, int wanttty)
1783 struct sigaction osa, nsa;
1784 #endif /* SIGSYNCH */
1787 * A child will be uninterruptible only under very special conditions.
1788 * Remember that the semantics of '&' is implemented by disconnecting the
1789 * process from the tty so signals do not need to ignored just for '&'.
1790 * Thus signals are set to default action for children unless: we have had
1791 * an "onintr -" (then specifically ignored) we are not playing with
1792 * signals (inherit action)
1795 ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
1796 || (gointr && eq(gointr, STRminus));
1799 * Check for maximum nesting of 16 processes to avoid Forking loops
1802 stderror(ERR_NESTING, 16);
1804 nsa.sa_handler = synch_handler;
1805 sigfillset(&nsa.sa_mask);
1806 nsa.sa_flags = SA_RESTART;
1807 if (sigaction(SIGSYNCH, &nsa, &osa))
1808 stderror(ERR_SYSTEM, "pfork: sigaction set", strerror(errno));
1809 #endif /* SIGSYNCH */
1811 * Hold pchild() until we have the process installed in our table.
1815 cleanup_push(&pchild_disabled, disabled_cleanup);
1817 while ((pid = fork()) == -1)
1819 (void) sleep(FORKSLEEP);
1821 stderror(ERR_NOPROC);
1823 (void)cleanup_push_mark(); /* Never to be popped */
1824 pchild_disabled = 0;
1826 pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
1829 #if !defined(BSDTIMES) && !defined(_SEQUENT_)
1831 #endif /* !defined(BSDTIMES) && !defined(_SEQUENT_) */
1834 setintr = 0; /* until I think otherwise */
1836 * Children just get blown away on SIGINT, SIGQUIT unless "onintr
1839 (void) signal(SIGINT, ignint ? SIG_IGN : SIG_DFL);
1840 (void) signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL);
1843 /* make stoppable */
1844 (void) signal(SIGTSTP, SIG_DFL);
1845 (void) signal(SIGTTIN, SIG_DFL);
1846 (void) signal(SIGTTOU, SIG_DFL);
1848 #endif /* BSDJOBS */
1849 sigaction(SIGTERM, &parterm, NULL);
1851 else if (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT)) {
1852 (void) signal(SIGINT, SIG_IGN);
1853 (void) signal(SIGQUIT, SIG_IGN);
1856 signal(SIGIO, SIG_IGN); /* ignore SIGIO in child too */
1859 pgetty(wanttty, pgrp);
1861 * Nohup and nice apply only to NODE_COMMAND's but it would be nice
1862 * (?!?) if you could say "nohup (foo;bar)" Then the parser would have
1863 * to know about nice/nohup/time
1865 if (t->t_dflg & F_NOHUP)
1866 (void) signal(SIGHUP, SIG_IGN);
1867 if (t->t_dflg & F_NICE) {
1868 int nval = SIGN_EXTEND_CHAR(t->t_nice);
1869 #if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
1870 if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
1871 stderror(ERR_SYSTEM, "setpriority", strerror(errno));
1872 #else /* !HAVE_SETPRIORITY || !PRIO_PROCESS */
1874 #endif /* HAVE_SETPRIORITY && PRIO_PROCESS */
1877 if (t->t_dflg & F_VER) {
1878 tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
1883 /* rfw 8/89 now parent can continue */
1884 if (kill(getppid(), SIGSYNCH))
1885 stderror(ERR_SYSTEM, "pfork child: kill", strerror(errno));
1886 #endif /* SIGSYNCH */
1893 * `Walking' process group fix from Beto Appleton.
1894 * (beto@aixwiz.austin.ibm.com)
1895 * If setpgid fails at this point that means that
1896 * our process leader has died. We flush the current
1897 * job and become the process leader ourselves.
1898 * The parent will figure that out later.
1900 pgrp = pcurrjob ? pcurrjob->p_jobid : pid;
1901 if (setpgid(pid, pgrp) == -1 && errno == EPERM) {
1904 * We don't care if this causes an error here;
1905 * then we are already in the right process group
1907 (void) setpgid(pid, pgrp = pid);
1910 #endif /* POSIXJOBS */
1914 sigset_t pause_mask;
1917 * rfw 8/89 Wait for child to own terminal. Solves half of ugly
1918 * synchronization problem. With this change, we know that the only
1919 * reason setpgrp to a previous process in a pipeline can fail is that
1920 * the previous process has already exited. Without this hack, he may
1921 * either have exited or not yet started to run. Two uglies become
1924 sigprocmask(SIG_BLOCK, NULL, &pause);
1925 sigdelset(&pause_mask, SIGCHLD);
1926 sigdelset(&pause_mask, SIGSYNCH);
1927 sigsuspend(&pause_mask);
1928 (void)handle_pending_signals();
1929 if (sigaction(SIGSYNCH, &osa, NULL))
1930 stderror(ERR_SYSTEM, "pfork parent: sigaction restore",
1933 #endif /* SIGSYNCH */
1936 cleanup_until(&pchild_disabled);
1945 stderror(ERR_JOBCONTROL);
1947 stderror(ERR_JOBCTRLSUB);
1952 setttypgrp(int pgrp)
1955 * If we are piping out a builtin, eg. 'echo | more' things can go
1956 * out of sequence, i.e. the more can run before the echo. This
1957 * can happen even if we have vfork, since the echo will be forked
1958 * with the regular fork. In this case, we need to set the tty
1959 * pgrp ourselves. If that happens, then the process will be still
1960 * alive. And the tty process group will already be set.
1961 * This should fix the famous sequent problem as a side effect:
1962 * The controlling terminal is lost if all processes in the
1963 * terminal process group are zombies. In this case tcgetpgrp()
1964 * returns 0. If this happens we must set the terminal process
1967 if (tcgetpgrp(FSHTTY) != pgrp) {
1969 struct sigaction old;
1972 * tcsetpgrp will set SIGTTOU to all the the processes in
1973 * the background according to POSIX... We ignore this here.
1975 sigaction(SIGTTOU, NULL, &old);
1976 signal(SIGTTOU, SIG_IGN);
1978 (void) tcsetpgrp(FSHTTY, pgrp);
1980 sigaction(SIGTTOU, &old, NULL);
1988 * if we don't have vfork(), things can still go in the wrong order
1989 * resulting in the famous 'Stopped (tty output)'. But some systems
1990 * don't permit the setpgid() call, (these are more recent secure
1991 * systems such as ibm's aix), when they do. Then we'd rather print
1992 * an error message than hang the shell!
1993 * I am open to suggestions how to fix that.
1996 pgetty(int wanttty, pid_t pgrp)
2001 # endif /* POSIXJOBS */
2003 jobdebug_xprintf(("wanttty %d pid %d opgrp%d pgrp %d tpgrp %d\n",
2004 wanttty, (int)getpid(), (int)pgrp, (int)mygetpgrp(),
2005 (int)tcgetpgrp(FSHTTY)));
2008 * christos: I am blocking the tty signals till I've set things
2013 sigaddset(&set, SIGTSTP);
2014 sigaddset(&set, SIGTTIN);
2015 (void)sigprocmask(SIG_BLOCK, &set, &oset);
2016 cleanup_push(&oset, sigprocmask_cleanup);
2018 # endif /* POSIXJOBS */
2023 # endif /* !POSIXJOBS */
2026 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
2027 * Don't check for tpgrp >= 0 so even non-interactive shells give
2028 * background jobs process groups Same for the comparison in the other part
2032 if (setpgid(0, pgrp) == -1) {
2034 /* Walking process group fix; see above */
2035 if (setpgid(0, pgrp = getpid()) == -1) {
2036 # endif /* POSIXJOBS */
2037 stderror(ERR_SYSTEM, "setpgid child:\n", strerror(errno));
2041 wanttty = pgrp; /* Now we really want the tty, since we became the
2042 * the process group leader
2044 # endif /* POSIXJOBS */
2051 cleanup_until(&oset);
2053 # endif /* POSIXJOBS */
2055 jobdebug_xprintf(("wanttty %d pid %d pgrp %d tpgrp %d\n",
2056 wanttty, getpid(), mygetpgrp(), tcgetpgrp(FSHTTY)));
2059 tpgrp = 0; /* gave tty away */
2060 #endif /* BSDJOBS */