1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.proc.c,v 3.127 2015/02/22 21:40:14 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.127 2015/02/22 21:40:14 christos Exp $")
44 #endif /* WINNT_NATIVE */
50 #if defined(_BSD) || (defined(IRIS4D) && __STDC__) || defined(__lucid) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__)
51 # if !defined(__ANDROID__)
54 #endif /* _BSD || (IRIS4D && __STDC__) || __lucid || glibc */
56 # define WTERMSIG(w) (((union wait *) &(w))->w_termsig)
59 # endif /* !BSDWAIT */
60 #endif /* !WTERMSIG */
62 # define WEXITSTATUS(w) (((union wait *) &(w))->w_retcode)
63 #endif /* !WEXITSTATUS */
65 # define WSTOPSIG(w) (((union wait *) &(w))->w_stopsig)
66 #endif /* !WSTOPSIG */
70 # define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
76 # define WCOREDUMP(w) (((union wait *) &(w))->w_coredump)
78 # define WCOREDUMP(w) ((w) & 0200)
79 # endif /* !BSDWAIT */
80 #endif /* !WCOREDUMP */
83 # define jobdebug_xprintf(x) (void)0
84 # define jobdebug_flush() (void)0
86 # define jobdebug_xprintf(s) xprintf s
87 # define jobdebug_flush() flush()
91 * C Shell - functions that manage processes, handling hanging, termination
94 #define BIGINDEX 9 /* largest desirable job index */
98 /* use 'cvxrusage' to get parallel statistics */
99 static struct cvxrusage zru = {{0L, 0L}, {0L, 0L}, 0L, 0L, 0L, 0L,
100 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
101 {0L, 0L}, 0LL, 0LL, 0LL, 0LL, 0L, 0L, 0L,
102 0LL, 0LL, {0L, 0L, 0L, 0L, 0L}};
104 static struct rusage zru;
106 #else /* !BSDTIMES */
108 static struct process_stats zru = {{0L, 0L}, {0L, 0L}, 0, 0, 0, 0, 0, 0, 0,
109 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
110 # else /* !_SEQUENT_ */
112 static struct tms zru = {0, 0, 0, 0}, lru = {0, 0, 0, 0};
114 static struct tms zru = {0L, 0L, 0L, 0L}, lru = {0L, 0L, 0L, 0L};
116 # endif /* !_SEQUENT_ */
117 #endif /* !BSDTIMES */
120 static int timesdone; /* shtimes buffer full ? */
121 #endif /* BSDTIMES */
123 #ifndef RUSAGE_CHILDREN
124 # define RUSAGE_CHILDREN -1
125 #endif /* RUSAGE_CHILDREN */
127 static void pflushall (void);
128 static void pflush (struct process *);
129 static void pfree (struct process *);
130 static void pclrcurr (struct process *);
131 static void morecommand (size_t);
132 static void padd (struct command *);
133 static int pprint (struct process *, int);
134 static void ptprint (struct process *);
135 static void pads (Char *);
136 static void pkill (Char **, int);
137 static struct process *pgetcurr (struct process *);
138 static void okpcntl (void);
139 static void setttypgrp (int);
142 * pchild - call queued by the SIGCHLD signal
143 * indicating that at least one child has terminated or stopped
144 * thus at least one wait system call will definitely return a
145 * childs status. Top level routines (like pwait) must be sure
146 * to mask interrupts when playing with the proclist data structures!
158 #endif /* !BSDWAIT */
162 #else /* !BSDTIMES */
164 struct process_stats ru;
165 struct process_stats cpst1, cpst2;
167 # else /* !_SEQUENT_ */
168 struct tms proctimes;
172 (void) times(&shtimes);
174 # endif /* !_SEQUENT_ */
175 #endif /* !BSDTIMES */
177 jobdebug_xprintf(("pchild()\n"));
180 jobdebug_xprintf(("Waiting...\n"));
182 errno = 0; /* reset, just in case */
188 /* use 'cvxwait' to get parallel statistics */
190 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
192 /* both a wait3 and rusage */
193 # if !defined(BSDWAIT) || defined(NeXT) || defined(MACH) || defined(__linux__) || defined(__GNU__) || defined(__GLIBC__) || (defined(IRIS4D) && SYSVREL <= 3) || defined(__lucid) || defined(__osf__)
195 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
197 pid = wait3(&w.w_status,
198 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
199 # endif /* BSDWAIT */
201 # else /* !BSDTIMES */
203 (void) get_process_stats(&tv, PS_SELF, 0, &cpst1);
204 pid = waitpid(-1, &w,
205 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
206 (void) get_process_stats(&tv, PS_SELF, 0, &cpst2);
207 pr_stat_sub(&cpst2, &cpst1, &ru);
208 # else /* !_SEQUENT_ */
210 /* we have a wait3, but no rusage stuff */
211 pid = wait3(&w.w_status,
212 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), 0);
214 pid = waitpid(-1, &w,
215 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
217 # endif /* !_SEQUENT_ */
218 # endif /* !BSDTIMES */
219 # else /* !BSDJOBS */
222 /* both a wait3 and rusage */
224 pid = wait3(&w.w_status, WNOHANG, 0);
227 pid = wait3(&w, WNOHANG, &ru);
229 pid = wait3(&w.w_status, WNOHANG, &ru);
230 # endif /* BSDWAIT */
232 # else /* !BSDTIMES */
233 # ifdef ODT /* For Sco Unix 3.2.0 or ODT 1.0 */
235 pid = waitpid(-1, &w,
236 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
238 # if defined(aiws) || defined(uts)
240 pid = wait3(&w.w_status,
241 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), 0);
242 # endif /* aiws || uts */
245 /* no wait3, therefore no rusage */
246 /* on Sys V, this may hang. I hope it's not going to be a problem */
250 * XXX: for greater than 3 we should use waitpid().
251 * but then again, SVR4 falls into the POSIX/BSDJOBS category.
253 pid = wait(&w.w_status);
254 # endif /* BSDWAIT */
255 # endif /* !HAVEwait3 */
256 # endif /* !BSDTIMES */
257 # endif /* !BSDJOBS */
258 #else /* WINNT_NATIVE */
259 pid = waitpid(-1, &w,
260 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
261 #endif /* WINNT_NATIVE */
263 jobdebug_xprintf(("parent %d pid %d, retval %x termsig %x retcode %x\n",
264 (int)getpid(), (int)pid, w, WTERMSIG(w),
268 if ((pid == 0) || (pid == -1)) {
269 (void)handle_pending_signals();
270 jobdebug_xprintf(("errno == %d\n", errno));
275 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
276 if (pid == pp->p_procid)
278 #if !defined(BSDJOBS) && !defined(WINNT_NATIVE)
279 /* this should never have happened */
280 stderror(ERR_SYNC, pid);
282 #else /* BSDJOBS || WINNT_NATIVE */
284 #endif /* !BSDJOBS && !WINNT_NATIVE */
286 pp->p_flags &= ~(PRUNNING | PSTOPPED | PREPORTED);
288 pp->p_flags |= PSTOPPED;
289 pp->p_reason = WSTOPSIG(w);
292 if (pp->p_flags & (PTIME | PPTIME) || adrof(STRtime))
295 (void) get_process_stats(&pp->p_etime, PS_SELF, NULL, NULL);
296 # else /* !_SEQUENT_ */
297 pp->p_etime = times(&proctimes);
298 # endif /* !_SEQUENT_ */
300 (void) gettimeofday(&pp->p_etime, NULL);
301 #endif /* BSDTIMES */
304 #if defined(BSDTIMES) || defined(_SEQUENT_)
306 #else /* !BSDTIMES && !_SEQUENT_ */
307 (void) times(&proctimes);
308 pp->p_utime = proctimes.tms_cutime - shtimes.tms_cutime;
309 pp->p_stime = proctimes.tms_cstime - shtimes.tms_cstime;
311 #endif /* !BSDTIMES && !_SEQUENT_ */
312 if (WIFSIGNALED(w)) {
313 if (WTERMSIG(w) == SIGINT)
314 pp->p_flags |= PINTERRUPTED;
316 pp->p_flags |= PSIGNALED;
318 pp->p_flags |= PDUMPED;
319 pp->p_reason = WTERMSIG(w);
322 pp->p_reason = WEXITSTATUS(w);
323 if (pp->p_reason != 0)
324 pp->p_flags |= PAEXITED;
326 pp->p_flags |= PNEXITED;
332 if ((fp->p_flags & (PPTIME | PRUNNING | PSTOPPED)) == 0 &&
333 !child && adrof(STRtime) &&
335 fp->p_rusage.ru_utime.tv_sec + fp->p_rusage.ru_stime.tv_sec
336 #else /* !BSDTIMES */
338 fp->p_rusage.ps_utime.tv_sec + fp->p_rusage.ps_stime.tv_sec
339 # else /* !_SEQUENT_ */
341 (fp->p_utime + fp->p_stime) / HZ
343 (fp->p_utime + fp->p_stime) / clk_tck
345 # endif /* !_SEQUENT_ */
346 #endif /* !BSDTIMES */
347 >= atoi(short2str(varval(STRtime))))
348 fp->p_flags |= PTIME;
349 jobflags |= fp->p_flags;
350 } while ((fp = fp->p_friends) != pp);
351 pp->p_flags &= ~PFOREGND;
352 if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
353 pp->p_flags &= ~PPTIME;
354 pp->p_flags |= PTIME;
356 if ((jobflags & (PRUNNING | PREPORTED)) == 0) {
359 if (fp->p_flags & PSTOPPED)
360 fp->p_flags |= PREPORTED;
361 } while ((fp = fp->p_friends) != pp);
362 while (fp->p_procid != fp->p_jobid)
364 if (jobflags & PSTOPPED) {
365 if (pcurrent && pcurrent != fp)
366 pprevious = pcurrent;
371 if (jobflags & PFOREGND) {
372 if (!(jobflags & (PSIGNALED | PSTOPPED | PPTIME) ||
374 jobflags & PAEXITED ||
377 !eq(dcwd->di_name, fp->p_cwd->di_name))) {
378 /* PWP: print a newline after ^C */
379 if (jobflags & PINTERRUPTED) {
380 xputchar('\r' | QUOTE);
384 else if ((jobflags & (PTIME|PSTOPPED)) == PTIME)
390 if (jobflags & PNOTIFY || adrof(STRnotify)) {
391 xputchar('\r' | QUOTE);
393 (void) pprint(pp, NUMBER | NAME | REASON);
394 if ((jobflags & PSTOPPED) == 0)
401 * don't really want to do that, because it
402 * will erase our message in case of multi-line
412 fp->p_flags |= PNEEDNOTE;
417 #if defined(BSDJOBS) || defined(HAVEwait3) ||defined(WINNT_NATIVE)
419 #endif /* BSDJOBS || HAVEwait3 */
431 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next) {
432 if (pp->p_flags & PNEEDNOTE) {
434 cleanup_push(&pchild_disabled, disabled_cleanup);
435 pp->p_flags &= ~PNEEDNOTE;
436 flags = pprint(pp, NUMBER | NAME | REASON);
437 if ((flags & (PRUNNING | PSTOPPED)) == 0)
439 cleanup_until(&pchild_disabled);
446 pfree(struct process *pp)
448 xfree(pp->p_command);
449 if (pp->p_cwd && --pp->p_cwd->di_count == 0)
450 if (pp->p_cwd->di_next == 0)
457 * pwait - wait for current job to terminate, maintaining integrity
458 * of current and previous job indicators.
463 struct process *fp, *pp;
466 * Here's where dead procs get flushed.
468 for (pp = (fp = &proclist)->p_next; pp != NULL; pp = (fp = pp)->p_next)
469 if (pp->p_procid == 0) {
470 fp->p_next = pp->p_next;
479 * pjwait - wait for a job to finish or become stopped
480 * It is assumed to be in the foreground state (PFOREGND)
483 pjwait(struct process *pp)
486 int jobflags, reason;
487 sigset_t oset, set, pause_mask;
490 while (pp->p_procid != pp->p_jobid)
495 if ((fp->p_flags & (PFOREGND | PRUNNING)) == PRUNNING)
496 xprintf("%s", CGETS(17, 1, "BUG: waiting for background job!\n"));
497 } while ((fp = fp->p_friends) != pp);
499 * Now keep pausing as long as we are not interrupted (SIGINT), and the
500 * target process, or any of its friends, are running
504 sigaddset(&set, SIGINT);
505 sigaddset(&set, SIGCHLD);
506 (void)sigprocmask(SIG_BLOCK, &set, &oset);
507 cleanup_push(&oset, sigprocmask_cleanup);
509 sigdelset(&pause_mask, SIGCHLD);
510 sigaddset(&pause_mask, SIGINT);
512 (void)handle_pending_signals();
515 jobflags |= fp->p_flags;
516 while ((fp = (fp->p_friends)) != pp);
517 if ((jobflags & PRUNNING) == 0)
519 jobdebug_xprintf(("%d starting to sigsuspend for SIGCHLD on %d\n",
520 getpid(), fp->p_procid));
521 sigsuspend(&pause_mask);
523 cleanup_until(&oset);
524 jobdebug_xprintf(("%d returned from sigsuspend loop\n", getpid()));
526 if (tpgrp > 0) /* get tty back */
527 (void) tcsetpgrp(FSHTTY, tpgrp);
529 if ((jobflags & (PSIGNALED | PSTOPPED | PTIME)) ||
530 fp->p_cwd == NULL || !eq(dcwd->di_name, fp->p_cwd->di_name)) {
531 if (jobflags & PSTOPPED) {
533 if (adrof(STRlistjobs)) {
536 jobcommand[0] = STRjobs;
537 if (eq(varval(STRlistjobs), STRlong))
538 jobcommand[1] = STRml;
540 jobcommand[1] = NULL;
541 jobcommand[2] = NULL;
543 dojobs(jobcommand, NULL);
544 (void) pprint(pp, SHELLDIR);
547 (void) pprint(pp, AREASON | SHELLDIR);
550 (void) pprint(pp, AREASON | SHELLDIR);
552 if ((jobflags & (PINTERRUPTED | PSTOPPED)) && setintr &&
553 (!gointr || !eq(gointr, STRminus))) {
554 if ((jobflags & PSTOPPED) == 0)
562 /* In case of pipelines only the result of the last
563 * command should be taken in account */
564 if (!anyerror && !(fp->p_flags & PBRACE)
565 && ((fp->p_flags & PPOU) || (fp->p_flags & PBACKQ)))
568 reason = fp->p_flags & (PSIGNALED | PINTERRUPTED) ?
569 fp->p_reason | META : fp->p_reason;
570 } while ((fp = fp->p_friends) != pp);
572 * Don't report on backquoted jobs, cause it will mess up
575 if ((reason != 0) && (adrof(STRprintexitvalue)) &&
576 (pp->p_flags & PBACKQ) == 0)
577 xprintf(CGETS(17, 2, "Exit %d\n"), reason);
578 reason_str = putn((tcsh_number_t)reason);
579 cleanup_push(reason_str, xfree);
580 setv(STRstatus, reason_str, VAR_READWRITE);
581 cleanup_ignore(reason_str);
582 cleanup_until(reason_str);
583 if (reason && exiterr)
589 * dowait - wait for all processes to finish
594 dowait(Char **v, struct command *c)
598 /* the current block mask to be able to restore */
601 /* block mask for critical section: OLD_MASK U {SIGCHLD} */
604 /* ignore those during blocking sigsuspend:
605 OLD_MASK / {SIGCHLD, possibly(SIGINT)} */
608 int opintr_disabled, gotsig;
614 sigprocmask(SIG_BLOCK, NULL, &pause_mask);
615 sigdelset(&pause_mask, SIGCHLD);
617 sigdelset(&pause_mask, SIGINT);
619 /* critical section, block also SIGCHLD */
620 sigprocmask(SIG_BLOCK, NULL, &block_mask);
621 sigaddset(&block_mask, SIGCHLD);
622 sigprocmask(SIG_BLOCK, &block_mask, &old_mask);
624 /* detect older SIGCHLDs and remove PRUNNING flag from proclist */
625 (void)handle_pending_signals();
628 for (pp = proclist.p_next; pp; pp = pp->p_next)
629 if (pp->p_procid && /* pp->p_procid == pp->p_jobid && */
630 pp->p_flags & PRUNNING) {
631 /* wait for (or pick up alredy blocked) SIGCHLD */
632 sigsuspend(&pause_mask);
634 /* make the 'wait' interuptable by CTRL-C */
635 opintr_disabled = pintr_disabled;
637 gotsig = handle_pending_signals();
638 pintr_disabled = opintr_disabled;
645 sigprocmask(SIG_SETMASK, &old_mask, NULL);
649 * pflushall - flush all jobs from list (e.g. at fork())
656 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
662 * pflush - flag all process structures in the same job as the
663 * the argument process for deletion. The actual free of the
664 * space is not done here since pflush is called at interrupt level.
667 pflush(struct process *pp)
672 if (pp->p_procid == 0) {
673 xprintf("%s", CGETS(17, 3, "BUG: process flushed twice"));
676 while (pp->p_procid != pp->p_jobid)
684 np->p_index = np->p_procid = 0;
685 np->p_flags &= ~PNEEDNOTE;
686 } while ((np = np->p_friends) != pp);
687 if (idx == pmaxindex) {
688 for (np = proclist.p_next, idx = 0; np; np = np->p_next)
689 if (np->p_index > idx)
696 * pclrcurr - make sure the given job is not the current or previous job;
697 * pp MUST be the job leader
700 pclrcurr(struct process *pp)
702 if (pp == pcurrent) {
703 if (pprevious != NULL) {
704 pcurrent = pprevious;
705 pprevious = pgetcurr(pp);
708 pcurrent = pgetcurr(pp);
709 pprevious = pgetcurr(pp);
712 else if (pp == pprevious)
713 pprevious = pgetcurr(pp);
716 /* +4 here is 1 for '\0', 1 ea for << >& >> */
718 static size_t cmdmax;
719 static size_t cmdlen;
721 #define CMD_INIT 1024
725 morecommand(size_t s)
731 ncmdstr = xrealloc(cmdstr, cmdmax * sizeof(*cmdstr));
732 d = ncmdstr - cmdstr;
738 * unparse - Export padd() functionality
741 unparse(struct command *t)
744 morecommand(CMD_INIT);
749 return Strsave(cmdstr);
754 * palloc - allocate a process structure and fill it up.
755 * an important assumption is made that the process is running.
758 palloc(pid_t pid, struct command *t)
763 pp = xcalloc(1, sizeof(struct process));
765 pp->p_parentid = shpgrp;
766 pp->p_flags = ((t->t_dflg & F_AMPERSAND) ? 0 : PFOREGND) | PRUNNING;
767 if (t->t_dflg & F_TIME)
768 pp->p_flags |= PPTIME;
769 if (t->t_dflg & F_BACKQ)
770 pp->p_flags |= PBACKQ;
771 if (t->t_dflg & F_HUP)
773 if (t->t_dcom && t->t_dcom[0] && (*t->t_dcom[0] == '{'))
774 pp->p_flags |= PBRACE;
776 morecommand(CMD_INIT);
781 if (t->t_dflg & F_PIPEOUT) {
783 if (t->t_dflg & F_STDERR)
784 pp->p_flags |= PDIAG;
786 pp->p_command = Strsave(cmdstr);
790 /* careful here with interrupt level */
792 pp->p_index = pcurrjob->p_index;
793 pp->p_friends = pcurrjob;
794 pp->p_jobid = pcurrjob->p_procid;
795 for (fp = pcurrjob; fp->p_friends != pcurrjob; fp = fp->p_friends)
805 if (pmaxindex < BIGINDEX)
806 pp->p_index = ++pmaxindex;
811 for (np = proclist.p_next; np; np = np->p_next)
812 if (np->p_index == i)
821 if (pcurrent == NULL)
823 else if (pprevious == NULL)
826 pp->p_next = proclist.p_next;
827 proclist.p_next = pp;
829 (void) gettimeofday(&pp->p_btime, NULL);
830 #else /* !BSDTIMES */
832 (void) get_process_stats(&pp->p_btime, PS_SELF, NULL, NULL);
833 # else /* !_SEQUENT_ */
837 pp->p_btime = times(&tmptimes);
839 # endif /* !_SEQUENT_ */
840 #endif /* !BSDTIMES */
844 padd(struct command *t)
859 for (argp = t->t_dcom; *argp; argp++) {
893 if ((t->t_dflg & F_PIPEIN) == 0 && t->t_dlef) {
894 pads((t->t_dflg & F_READ) ? STRspLarrow2sp : STRspLarrowsp);
897 if ((t->t_dflg & F_PIPEOUT) == 0 && t->t_drit) {
898 pads((t->t_dflg & F_APPEND) ? STRspRarrow2 : STRspRarrow);
899 if (t->t_dflg & F_STDERR)
912 * Avoid the Quoted Space alias hack! Reported by:
913 * sam@john-bigboote.ICS.UCI.EDU (Sam Horrocks)
915 if (cp[0] == STRQNULL[0])
920 len = cmdlen + i + CMD_INCR;
923 (void) Strcpy(cmdp, cp);
929 * psavejob - temporarily save the current job on a one level stack
930 * so another job can be created. Used for { } in exp6
931 * and `` in globbing.
941 psavejob_cleanup(void *dummy)
949 * pendjob - indicate that a job (set of commands) has been completed
950 * or is about to begin.
955 struct process *pp, *tp;
957 if (pcurrjob && (pcurrjob->p_flags & (PFOREGND | PSTOPPED)) == 0) {
960 while (pp->p_procid != pp->p_jobid)
962 xprintf("[%d]", pp->p_index);
965 xprintf(" %d", pp->p_procid);
970 pholdjob = pcurrjob = 0;
974 * pprint - print a job
978 * Hacks have been added for SVR4 to deal with pipe's being spawned in
981 * David Dawes (dawes@physics.su.oz.au) Oct 1991
985 pprint(struct process *pp, int flag)
989 int jobflags, pstatus, pcond;
993 struct process *pipehead = NULL, *pipetail = NULL, *pmarker = NULL;
995 #endif /* BACKPIPE */
997 while (pp->p_procid != pp->p_jobid)
999 if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
1000 pp->p_flags &= ~PPTIME;
1001 pp->p_flags |= PTIME;
1004 status = reason = -1;
1006 haderr = 1; /* Print statuc to stderr */
1010 * The pipeline is reversed, so locate the real head of the pipeline
1011 * if pp is at the tail of a pipe (and not already in a pipeline)
1013 if ((pp->p_friends->p_flags & PPOU) && !inpipe && (flag & NAME)) {
1018 while (pp->p_friends->p_flags & PPOU);
1022 * pmarker is used to hold the place of the proc being processed, so
1023 * we can search for the next one downstream later.
1026 pcond = (tp != pp || (inpipe && tp == pp));
1027 #else /* !BACKPIPE */
1029 #endif /* BACKPIPE */
1031 jobflags |= pp->p_flags;
1032 pstatus = (int) (pp->p_flags & PALLSTATES);
1033 if (pcond && linp != linbuf && !(flag & FANCY) &&
1034 ((pstatus == status && pp->p_reason == reason) ||
1038 if (pcond && linp != linbuf)
1040 if (flag & NUMBER) {
1042 pcond = ((pp == tp && !inpipe) ||
1043 (inpipe && pipetail == tp && pp == pipehead));
1044 #else /* BACKPIPE */
1046 #endif /* BACKPIPE */
1048 xprintf("[%d]%s %c ", pp->p_index,
1049 pp->p_index < 10 ? " " : "",
1050 pp == pcurrent ? '+' :
1051 (pp == pprevious ? '-' : ' '));
1056 xprintf("%5d ", pp->p_procid);
1058 xprintf("%11s ", sitename(pp->p_procid));
1061 if (flag & (REASON | AREASON)) {
1066 if (pstatus == status) {
1067 if (pp->p_reason == reason) {
1068 xprintf(format, "");
1072 reason = (int) pp->p_reason;
1076 reason = (int) pp->p_reason;
1081 xprintf(format, CGETS(17, 4, "Running "));
1088 * tell what happened to the background job
1089 * From: Michael Schroeder
1090 * <mlschroe@immd4.informatik.uni-erlangen.de>
1093 || ((flag & AREASON)
1095 && (reason != SIGPIPE
1096 || (pp->p_flags & PPOU) == 0))) {
1101 ptr = (char *)(intptr_t)mesg[pp->p_reason & 0177].pname;
1103 ptr = xasprintf("%s %d", CGETS(17, 5, "Signal"),
1104 pp->p_reason & 0177);
1105 cleanup_push(ptr, xfree);
1108 xprintf(format, ptr);
1118 if (flag & REASON) {
1120 xprintf(CGETS(17, 6, "Exit %-25d"), pp->p_reason);
1122 xprintf(format, CGETS(17, 7, "Done"));
1127 xprintf(CGETS(17, 8, "BUG: status=%-9o"),
1134 xprintf("%S", pp->p_command);
1135 if (pp->p_flags & PPOU)
1137 if (pp->p_flags & PDIAG)
1140 if (flag & (REASON | AREASON) && pp->p_flags & PDUMPED)
1141 xprintf("%s", CGETS(17, 9, " (core dumped)"));
1142 if (tp == pp->p_friends) {
1143 if (flag & AMPERSAND)
1145 if (flag & JOBDIR &&
1146 !eq(tp->p_cwd->di_name, dcwd->di_name)) {
1147 xprintf("%s", CGETS(17, 10, " (wd: "));
1148 dtildepr(tp->p_cwd->di_name);
1152 if (pp->p_flags & PPTIME && !(status & (PSTOPPED | PRUNNING))) {
1155 #if defined(BSDTIMES) || defined(_SEQUENT_)
1156 prusage(&zru, &pp->p_rusage, &pp->p_etime,
1158 #else /* !BSDTIMES && !SEQUENT */
1159 lru.tms_utime = pp->p_utime;
1160 lru.tms_stime = pp->p_stime;
1163 prusage(&zru, &lru, pp->p_etime,
1165 #endif /* !BSDTIMES && !SEQUENT */
1169 pcond = ((tp == pp->p_friends && !inpipe) ||
1170 (inpipe && pipehead->p_friends == tp && pp == pipetail));
1171 #else /* !BACKPIPE */
1172 pcond = (tp == pp->p_friends);
1173 #endif /* BACKPIPE */
1177 if (flag & SHELLDIR && !eq(tp->p_cwd->di_name, dcwd->di_name)) {
1178 xprintf("%s", CGETS(17, 11, "(wd now: "));
1179 dtildepr(dcwd->di_name);
1186 * if pmaker == pipetail, we are finished that pipeline, and
1187 * can now skip to past the head
1189 if (pmarker == pipetail) {
1195 * set pp to one before the one we want next, so the while below
1196 * increments to the correct spot.
1200 while (pp->p_friends->p_friends != pmarker);
1201 pmarker = pp->p_friends;
1204 pcond = ((pp = pp->p_friends) != tp || inpipe);
1205 #else /* !BACKPIPE */
1206 pcond = ((pp = pp->p_friends) != tp);
1207 #endif /* BACKPIPE */
1210 if (jobflags & PTIME && (jobflags & (PSTOPPED | PRUNNING)) == 0) {
1211 if (jobflags & NUMBER)
1220 * All 4.3 BSD derived implementations are buggy and I've had enough.
1221 * The following implementation produces similar code and works in all
1222 * cases. The 4.3BSD one works only for <, >, !=
1225 # define timercmp(tvp, uvp, cmp) \
1226 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1227 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1228 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1231 ptprint(struct process *tp)
1234 struct timeval tetime, diff;
1235 static struct timeval ztime;
1236 struct sysrusage ru;
1237 struct process *pp = tp;
1242 ruadd(&ru, &pp->p_rusage);
1243 tvsub(&diff, &pp->p_etime, &pp->p_btime);
1244 if (timercmp(&diff, &tetime, >))
1246 } while ((pp = pp->p_friends) != tp);
1247 prusage(&zru, &ru, &tetime, &ztime);
1248 #else /* !BSDTIMES */
1250 timeval_t tetime, diff;
1251 static timeval_t ztime;
1252 struct process_stats ru;
1253 struct process *pp = tp;
1258 ruadd(&ru, &pp->p_rusage);
1259 tvsub(&diff, &pp->p_etime, &pp->p_btime);
1260 if (timercmp(&diff, &tetime, >))
1262 } while ((pp = pp->p_friends) != tp);
1263 prusage(&zru, &ru, &tetime, &ztime);
1264 # else /* !_SEQUENT_ */
1266 static time_t ztime = 0;
1267 static time_t zu_time = 0;
1268 static time_t zs_time = 0;
1269 time_t tetime, diff;
1270 time_t u_time, s_time;
1273 static clock_t ztime = 0;
1274 static clock_t zu_time = 0;
1275 static clock_t zs_time = 0;
1276 clock_t tetime, diff;
1277 clock_t u_time, s_time;
1280 struct tms zts, rts;
1281 struct process *pp = tp;
1287 u_time += pp->p_utime;
1288 s_time += pp->p_stime;
1289 diff = pp->p_etime - pp->p_btime;
1292 } while ((pp = pp->p_friends) != tp);
1293 zts.tms_utime = zu_time;
1294 zts.tms_stime = zs_time;
1297 rts.tms_utime = u_time;
1298 rts.tms_stime = s_time;
1301 prusage(&zts, &rts, tetime, ztime);
1302 # endif /* !_SEQUENT_ */
1303 #endif /* !BSDTIMES */
1307 * dojobs - print all jobs
1311 dojobs(Char **v, struct command *c)
1314 int flag = NUMBER | NAME | REASON;
1321 if (v[1] || !eq(*v, STRml))
1323 flag |= FANCY | JOBDIR;
1325 for (i = 1; i <= pmaxindex; i++)
1326 for (pp = proclist.p_next; pp; pp = pp->p_next)
1327 if (pp->p_index == i && pp->p_procid == pp->p_jobid) {
1328 pp->p_flags &= ~PNEEDNOTE;
1329 if (!(pprint(pp, flag) & (PRUNNING | PSTOPPED)))
1336 * dofg - builtin - put the job into the foreground
1340 dofg(Char **v, struct command *c)
1349 if (!pstart(pp, 1)) {
1351 stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1355 } while (*v && *++v);
1359 * %... - builtin - put the job into the foreground
1363 dofg1(Char **v, struct command *c)
1370 if (!pstart(pp, 1)) {
1372 stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1379 * dobg - builtin - put the job into the background
1383 dobg(Char **v, struct command *c)
1392 if (!pstart(pp, 0)) {
1394 stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1396 } while (*v && *++v);
1400 * %... & - builtin - put the job into the background
1404 dobg1(Char **v, struct command *c)
1410 if (!pstart(pp, 0)) {
1412 stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1417 * dostop - builtin - stop the job
1421 dostop(Char **v, struct command *c)
1425 pkill(++v, SIGSTOP);
1426 #endif /* BSDJOBS */
1430 * dokill - builtin - superset of kill (1)
1434 dokill(Char **v, struct command *c)
1436 int signum, len = 0;
1442 if (v[0] && v[0][0] == '-') {
1443 if (v[0][1] == 'l') {
1444 for (signum = 0; signum <= nsig; signum++) {
1445 if ((name = mesg[signum].iname) != NULL) {
1446 len += strlen(name) + 1;
1447 if (len >= TermH - 1) {
1449 len = strlen(name) + 1;
1451 xprintf("%s ", name);
1458 if (v[0][1] == 's') {
1463 stderror(ERR_NAME | ERR_TOOFEW);
1466 if (Isdigit(*sigptr)) {
1468 signum = strtoul(short2str(sigptr), &ep, 0);
1469 if (*ep || signum < 0 || signum > (MAXSIG-1))
1470 stderror(ERR_NAME | ERR_BADSIG);
1473 for (signum = 0; signum <= nsig; signum++)
1474 if (mesg[signum].iname &&
1475 eq(sigptr, str2short(mesg[signum].iname)))
1477 setname(short2str(sigptr));
1478 stderror(ERR_NAME | ERR_UNKSIG);
1489 pkill(Char **v, int signum)
1491 struct process *pp, *np;
1492 int jobflags = 0, err1 = 0;
1494 Char *cp, **vp, **globbed;
1496 /* Avoid globbing %?x patterns */
1497 for (vp = v; vp && *vp; vp++)
1501 v = glob_all_or_error(v);
1503 cleanup_push(globbed, blk_cleanup);
1506 cleanup_push(&pchild_disabled, disabled_cleanup);
1509 cleanup_push(&pintr_disabled, disabled_cleanup);
1512 while (v && (cp = *v)) {
1514 np = pp = pfind(cp);
1516 jobflags |= np->p_flags;
1517 while ((np = np->p_friends) != pp);
1525 if ((jobflags & PRUNNING) == 0) {
1527 xprintf(CGETS(17, 12, "%S: Already suspended\n"), cp);
1528 # else /* !SUSPENDED */
1529 xprintf(CGETS(17, 13, "%S: Already stopped\n"), cp);
1530 # endif /* !SUSPENDED */
1536 * suspend a process, kill -CONT %, then type jobs; the shell
1537 * says it is suspended, but it is running; thanks jaap..
1540 if (!pstart(pp, 0)) {
1542 stderror(ERR_NAME|ERR_BADJOB, pp->p_command,
1549 #endif /* BSDJOBS */
1550 if (killpg(pp->p_jobid, signum) < 0) {
1551 xprintf("%S: %s\n", cp, strerror(errno));
1555 if (signum == SIGTERM || signum == SIGHUP)
1556 (void) killpg(pp->p_jobid, SIGCONT);
1557 #endif /* BSDJOBS */
1559 else if (!(Isdigit(*cp) || *cp == '-'))
1560 stderror(ERR_NAME | ERR_JOBARGS);
1563 #ifndef WINNT_NATIVE
1564 pid = strtol(short2str(cp), &ep, 10);
1566 pid = strtoul(short2str(cp), &ep, 0);
1567 #endif /* WINNT_NATIVE */
1569 stderror(ERR_NAME | ERR_JOBARGS);
1570 else if (kill(pid, signum) < 0) {
1571 xprintf("%d: %s\n", pid, strerror(errno));
1576 if (signum == SIGTERM || signum == SIGHUP)
1577 (void) kill(pid, SIGCONT);
1578 #endif /* BSDJOBS */
1583 cleanup_until(&pchild_disabled);
1585 stderror(ERR_SILENT);
1589 * pstart - start the job in foreground/background
1592 pstart(struct process *pp, int foregnd)
1596 /* We don't use jobflags in this function right now (see below) */
1597 /* long jobflags = 0; */
1600 cleanup_push(&pchild_disabled, disabled_cleanup);
1603 /* We don't use jobflags in this function right now (see below) */
1604 /* jobflags |= np->p_flags; */
1605 if (np->p_flags & (PRUNNING | PSTOPPED)) {
1606 np->p_flags |= PRUNNING;
1607 np->p_flags &= ~PSTOPPED;
1609 np->p_flags |= PFOREGND;
1611 np->p_flags &= ~PFOREGND;
1613 } while ((np = np->p_friends) != pp);
1616 (void) pprint(pp, foregnd ? NAME | JOBDIR : NUMBER | NAME | AMPERSAND);
1618 /* GrP run jobcmd hook if foregrounding */
1620 job_cmd(pp->p_command);
1625 rv = tcsetpgrp(FSHTTY, pp->p_jobid);
1628 * 1. child process of csh (shell script) receives SIGTTIN/SIGTTOU
1629 * 2. parent process (csh) receives SIGCHLD
1630 * 3. The "csh" signal handling function pchild() is invoked
1631 * with a SIGCHLD signal.
1632 * 4. pchild() calls wait3(WNOHANG) which returns 0.
1633 * The child process is NOT ready to be waited for at this time.
1634 * pchild() returns without picking-up the correct status
1635 * for the child process which generated the SIGCHLD.
1636 * 5. CONSEQUENCE : csh is UNaware that the process is stopped
1637 * 6. THIS LINE HAS BEEN COMMENTED OUT : if (jobflags&PSTOPPED)
1638 * (beto@aixwiz.austin.ibm.com - aug/03/91)
1639 * 7. I removed the line completely and added extra checks for
1640 * pstart, so that if a job gets attached to and dies inside
1641 * a debugger it does not confuse the shell. [christos]
1642 * 8. on the nec sx-4 there seems to be a problem, which requires
1643 * a syscall(151, getpid(), getpid()) in osinit. Don't ask me
1644 * what this is doing. [schott@rzg.mpg.de]
1648 rv = killpg(pp->p_jobid, SIGCONT);
1649 #endif /* BSDJOBS */
1650 cleanup_until(&pchild_disabled);
1655 panystop(int neednl)
1660 for (pp = proclist.p_next; pp; pp = pp->p_next)
1661 if (pp->p_flags & PSTOPPED)
1662 stderror(ERR_STOPPED, neednl ? "\n" : "");
1668 struct process *pp, *np;
1670 if (cp == 0 || cp[1] == 0 || eq(cp, STRcent2) || eq(cp, STRcentplus)) {
1671 if (pcurrent == NULL)
1672 stderror(ERR_NAME | ERR_JOBCUR);
1675 if (eq(cp, STRcentminus) || eq(cp, STRcenthash)) {
1676 if (pprevious == NULL)
1677 stderror(ERR_NAME | ERR_JOBPREV);
1680 if (Isdigit(cp[1])) {
1681 int idx = atoi(short2str(cp + 1));
1683 for (pp = proclist.p_next; pp; pp = pp->p_next)
1684 if (pp->p_index == idx && pp->p_procid == pp->p_jobid)
1686 stderror(ERR_NAME | ERR_NOSUCHJOB);
1689 for (pp = proclist.p_next; pp; pp = pp->p_next)
1690 if (pp->p_procid == pp->p_jobid) {
1694 for (dp = pp->p_command; *dp; dp++) {
1697 if (prefix(cp + 2, dp))
1701 else if (prefix(cp + 1, pp->p_command)) {
1704 stderror(ERR_NAME | ERR_AMBIG);
1710 stderror(ERR_NAME | (cp[1] == '?' ? ERR_JOBPAT : ERR_NOSUCHJOB));
1717 * pgetcurr - find most recent job that is not pp, preferably stopped
1719 static struct process *
1720 pgetcurr(struct process *pp)
1723 struct process *xp = NULL;
1725 for (np = proclist.p_next; np; np = np->p_next)
1726 if (np != pcurrent && np != pp && np->p_procid &&
1727 np->p_procid == np->p_jobid) {
1728 if (np->p_flags & PSTOPPED)
1737 * donotify - flag the job so as to report termination asynchronously
1741 donotify(Char **v, struct command *c)
1747 pp->p_flags |= PNOTIFY;
1752 synch_handler(int sno)
1756 #endif /* SIGSYNCH */
1759 * Do the fork and whatever should be done in the child side that
1760 * should not be done if we are not forking at all (like for simple builtin's)
1761 * Also do everything that needs any signals fiddled with in the parent side
1763 * Wanttty tells whether process and/or tty pgrps are to be manipulated:
1764 * -1: leave tty alone; inherit pgrp from parent
1765 * 0: already have tty; manipulate process pgrps only
1766 * 1: want to claim tty; manipulate process and tty pgrps
1767 * It is usually just the value of tpgrp.
1771 pfork(struct command *t, int wanttty)
1777 struct sigaction osa, nsa;
1778 #endif /* SIGSYNCH */
1781 * A child will be uninterruptible only under very special conditions.
1782 * Remember that the semantics of '&' is implemented by disconnecting the
1783 * process from the tty so signals do not need to ignored just for '&'.
1784 * Thus signals are set to default action for children unless: we have had
1785 * an "onintr -" (then specifically ignored) we are not playing with
1786 * signals (inherit action)
1789 ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
1790 || (gointr && eq(gointr, STRminus));
1793 * Check for maximum nesting of 16 processes to avoid Forking loops
1796 stderror(ERR_NESTING, 16);
1798 nsa.sa_handler = synch_handler;
1799 sigfillset(&nsa.sa_mask);
1800 nsa.sa_flags = SA_RESTART;
1801 if (sigaction(SIGSYNCH, &nsa, &osa))
1802 stderror(ERR_SYSTEM, "pfork: sigaction set", strerror(errno));
1803 #endif /* SIGSYNCH */
1805 * Hold pchild() until we have the process installed in our table.
1809 cleanup_push(&pchild_disabled, disabled_cleanup);
1811 while ((pid = fork()) == -1)
1813 (void) sleep(FORKSLEEP);
1815 stderror(ERR_NOPROC);
1817 (void)cleanup_push_mark(); /* Never to be popped */
1818 pchild_disabled = 0;
1820 pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
1823 #if !defined(BSDTIMES) && !defined(_SEQUENT_)
1825 #endif /* !defined(BSDTIMES) && !defined(_SEQUENT_) */
1828 setintr = 0; /* until I think otherwise */
1830 * Children just get blown away on SIGINT, SIGQUIT unless "onintr
1833 (void) signal(SIGINT, ignint ? SIG_IGN : SIG_DFL);
1834 (void) signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL);
1837 /* make stoppable */
1838 (void) signal(SIGTSTP, SIG_DFL);
1839 (void) signal(SIGTTIN, SIG_DFL);
1840 (void) signal(SIGTTOU, SIG_DFL);
1842 #endif /* BSDJOBS */
1843 sigaction(SIGTERM, &parterm, NULL);
1845 else if (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT)) {
1846 (void) signal(SIGINT, SIG_IGN);
1847 (void) signal(SIGQUIT, SIG_IGN);
1850 signal(SIGIO, SIG_IGN); /* ignore SIGIO in child too */
1853 pgetty(wanttty, pgrp);
1855 * Nohup and nice apply only to NODE_COMMAND's but it would be nice
1856 * (?!?) if you could say "nohup (foo;bar)" Then the parser would have
1857 * to know about nice/nohup/time
1859 if (t->t_dflg & F_NOHUP)
1860 (void) signal(SIGHUP, SIG_IGN);
1861 if (t->t_dflg & F_NICE) {
1862 int nval = SIGN_EXTEND_CHAR(t->t_nice);
1863 #if defined(HAVE_SETPRIORITY) && defined(PRIO_PROCESS)
1864 if (setpriority(PRIO_PROCESS, 0, nval) == -1 && errno)
1865 stderror(ERR_SYSTEM, "setpriority", strerror(errno));
1866 #else /* !HAVE_SETPRIORITY || !PRIO_PROCESS */
1868 #endif /* HAVE_SETPRIORITY && PRIO_PROCESS */
1871 if (t->t_dflg & F_VER) {
1872 tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
1877 /* rfw 8/89 now parent can continue */
1878 if (kill(getppid(), SIGSYNCH))
1879 stderror(ERR_SYSTEM, "pfork child: kill", strerror(errno));
1880 #endif /* SIGSYNCH */
1887 * `Walking' process group fix from Beto Appleton.
1888 * (beto@aixwiz.austin.ibm.com)
1889 * If setpgid fails at this point that means that
1890 * our process leader has died. We flush the current
1891 * job and become the process leader ourselves.
1892 * The parent will figure that out later.
1894 pgrp = pcurrjob ? pcurrjob->p_jobid : pid;
1895 if (setpgid(pid, pgrp) == -1 && errno == EPERM) {
1898 * We don't care if this causes an error here;
1899 * then we are already in the right process group
1901 (void) setpgid(pid, pgrp = pid);
1904 #endif /* POSIXJOBS */
1908 sigset_t pause_mask;
1911 * rfw 8/89 Wait for child to own terminal. Solves half of ugly
1912 * synchronization problem. With this change, we know that the only
1913 * reason setpgrp to a previous process in a pipeline can fail is that
1914 * the previous process has already exited. Without this hack, he may
1915 * either have exited or not yet started to run. Two uglies become
1918 sigprocmask(SIG_BLOCK, NULL, &pause);
1919 sigdelset(&pause_mask, SIGCHLD);
1920 sigdelset(&pause_mask, SIGSYNCH);
1921 sigsuspend(&pause_mask);
1922 (void)handle_pending_signals();
1923 if (sigaction(SIGSYNCH, &osa, NULL))
1924 stderror(ERR_SYSTEM, "pfork parent: sigaction restore",
1927 #endif /* SIGSYNCH */
1930 cleanup_until(&pchild_disabled);
1939 stderror(ERR_JOBCONTROL);
1941 stderror(ERR_JOBCTRLSUB);
1946 setttypgrp(int pgrp)
1949 * If we are piping out a builtin, eg. 'echo | more' things can go
1950 * out of sequence, i.e. the more can run before the echo. This
1951 * can happen even if we have vfork, since the echo will be forked
1952 * with the regular fork. In this case, we need to set the tty
1953 * pgrp ourselves. If that happens, then the process will be still
1954 * alive. And the tty process group will already be set.
1955 * This should fix the famous sequent problem as a side effect:
1956 * The controlling terminal is lost if all processes in the
1957 * terminal process group are zombies. In this case tcgetpgrp()
1958 * returns 0. If this happens we must set the terminal process
1961 if (tcgetpgrp(FSHTTY) != pgrp) {
1963 struct sigaction old;
1966 * tcsetpgrp will set SIGTTOU to all the the processes in
1967 * the background according to POSIX... We ignore this here.
1969 sigaction(SIGTTOU, NULL, &old);
1970 signal(SIGTTOU, SIG_IGN);
1972 (void) tcsetpgrp(FSHTTY, pgrp);
1974 sigaction(SIGTTOU, &old, NULL);
1982 * if we don't have vfork(), things can still go in the wrong order
1983 * resulting in the famous 'Stopped (tty output)'. But some systems
1984 * don't permit the setpgid() call, (these are more recent secure
1985 * systems such as ibm's aix), when they do. Then we'd rather print
1986 * an error message than hang the shell!
1987 * I am open to suggestions how to fix that.
1990 pgetty(int wanttty, pid_t pgrp)
1995 # endif /* POSIXJOBS */
1997 jobdebug_xprintf(("wanttty %d pid %d opgrp%d pgrp %d tpgrp %d\n",
1998 wanttty, (int)getpid(), (int)pgrp, (int)mygetpgrp(),
1999 (int)tcgetpgrp(FSHTTY)));
2002 * christos: I am blocking the tty signals till I've set things
2007 sigaddset(&set, SIGTSTP);
2008 sigaddset(&set, SIGTTIN);
2009 (void)sigprocmask(SIG_BLOCK, &set, &oset);
2010 cleanup_push(&oset, sigprocmask_cleanup);
2012 # endif /* POSIXJOBS */
2017 # endif /* !POSIXJOBS */
2020 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
2021 * Don't check for tpgrp >= 0 so even non-interactive shells give
2022 * background jobs process groups Same for the comparison in the other part
2026 if (setpgid(0, pgrp) == -1) {
2028 /* Walking process group fix; see above */
2029 if (setpgid(0, pgrp = getpid()) == -1) {
2030 # endif /* POSIXJOBS */
2031 stderror(ERR_SYSTEM, "setpgid child:\n", strerror(errno));
2035 wanttty = pgrp; /* Now we really want the tty, since we became the
2036 * the process group leader
2038 # endif /* POSIXJOBS */
2045 cleanup_until(&oset);
2047 # endif /* POSIXJOBS */
2049 jobdebug_xprintf(("wanttty %d pid %d pgrp %d tpgrp %d\n",
2050 wanttty, getpid(), mygetpgrp(), tcgetpgrp(FSHTTY)));
2053 tpgrp = 0; /* gave tty away */
2054 #endif /* BSDJOBS */