1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.proc.c,v 3.104 2006/09/27 16:59:04 mitr 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.104 2006/09/27 16:59:04 mitr Exp $")
44 #endif /* WINNT_NATIVE */
50 #if defined(_BSD) || (defined(IRIS4D) && __STDC__) || defined(__lucid) || defined(linux) || defined(__GNU__) || defined(__GLIBC__)
52 #endif /* _BSD || (IRIS4D && __STDC__) || __lucid || glibc */
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 padd (struct command
*);
130 static int pprint (struct process
*, int);
131 static void ptprint (struct process
*);
132 static void pads (Char
*);
133 static void pkill (Char
**, int);
134 static struct process
*pgetcurr (struct process
*);
135 static void okpcntl (void);
136 static void setttypgrp (int);
139 * pchild - call queued by the SIGCHLD signal
140 * indicating that at least one child has terminated or stopped
141 * thus at least one wait system call will definitely return a
142 * childs status. Top level routines (like pwait) must be sure
143 * to mask interrupts when playing with the proclist data structures!
155 #endif /* !BSDWAIT */
159 #else /* !BSDTIMES */
161 struct process_stats ru
;
162 struct process_stats cpst1
, cpst2
;
164 # else /* !_SEQUENT_ */
165 struct tms proctimes
;
169 (void) times(&shtimes
);
171 # endif /* !_SEQUENT_ */
172 #endif /* !BSDTIMES */
174 jobdebug_xprintf(("pchild()\n"));
177 jobdebug_xprintf(("Waiting...\n"));
179 errno
= 0; /* reset, just in case */
185 /* use 'cvxwait' to get parallel statistics */
187 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), &ru
);
189 /* both a wait3 and rusage */
190 # if !defined(BSDWAIT) || defined(NeXT) || defined(MACH) || defined(linux) || defined(__GNU__) || defined(__GLIBC__) || (defined(IRIS4D) && SYSVREL <= 3) || defined(__lucid) || defined(__osf__)
192 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), &ru
);
194 pid
= wait3(&w
.w_status
,
195 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), &ru
);
196 # endif /* BSDWAIT */
198 # else /* !BSDTIMES */
200 (void) get_process_stats(&tv
, PS_SELF
, 0, &cpst1
);
201 pid
= waitpid(-1, &w
,
202 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
203 (void) get_process_stats(&tv
, PS_SELF
, 0, &cpst2
);
204 pr_stat_sub(&cpst2
, &cpst1
, &ru
);
205 # else /* !_SEQUENT_ */
207 /* we have a wait3, but no rusage stuff */
208 pid
= wait3(&w
.w_status
,
209 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), 0);
211 pid
= waitpid(-1, &w
,
212 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
214 # endif /* !_SEQUENT_ */
215 # endif /* !BSDTIMES */
216 # else /* !BSDJOBS */
219 /* both a wait3 and rusage */
221 pid
= wait3(&w
.w_status
, WNOHANG
, 0);
223 pid
= wait3(&w
.w_status
, WNOHANG
, &ru
);
225 # else /* !BSDTIMES */
226 # ifdef ODT /* For Sco Unix 3.2.0 or ODT 1.0 */
228 pid
= waitpid(-1, &w
,
229 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
231 # if defined(aiws) || defined(uts)
233 pid
= wait3(&w
.w_status
,
234 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), 0);
235 # endif /* aiws || uts */
238 /* no wait3, therefore no rusage */
239 /* on Sys V, this may hang. I hope it's not going to be a problem */
243 * XXX: for greater than 3 we should use waitpid().
244 * but then again, SVR4 falls into the POSIX/BSDJOBS category.
246 pid
= wait(&w
.w_status
);
247 # endif /* BSDWAIT */
248 # endif /* !HAVEwait3 */
249 # endif /* !BSDTIMES */
250 # endif /* !BSDJOBS */
251 #else /* WINNT_NATIVE */
252 pid
= waitpid(-1, &w
,
253 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
254 #endif /* WINNT_NATIVE */
256 jobdebug_xprintf(("parent %d pid %d, retval %x termsig %x retcode %x\n",
257 (int)getpid(), (int)pid
, w
, WTERMSIG(w
),
261 if ((pid
== 0) || (pid
== -1)) {
262 handle_pending_signals();
263 jobdebug_xprintf(("errno == %d\n", errno
));
268 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
)
269 if (pid
== pp
->p_procid
)
271 #if !defined(BSDJOBS) && !defined(WINNT_NATIVE)
272 /* this should never have happened */
273 stderror(ERR_SYNC
, pid
);
275 #else /* BSDJOBS || WINNT_NATIVE */
277 #endif /* !BSDJOBS && !WINNT_NATIVE */
279 pp
->p_flags
&= ~(PRUNNING
| PSTOPPED
| PREPORTED
);
281 pp
->p_flags
|= PSTOPPED
;
282 pp
->p_reason
= WSTOPSIG(w
);
285 if (pp
->p_flags
& (PTIME
| PPTIME
) || adrof(STRtime
))
288 (void) get_process_stats(&pp
->p_etime
, PS_SELF
, NULL
, NULL
);
289 # else /* !_SEQUENT_ */
290 pp
->p_etime
= times(&proctimes
);
291 # endif /* !_SEQUENT_ */
293 (void) gettimeofday(&pp
->p_etime
, NULL
);
294 #endif /* BSDTIMES */
297 #if defined(BSDTIMES) || defined(_SEQUENT_)
299 #else /* !BSDTIMES && !_SEQUENT_ */
300 (void) times(&proctimes
);
301 pp
->p_utime
= proctimes
.tms_cutime
- shtimes
.tms_cutime
;
302 pp
->p_stime
= proctimes
.tms_cstime
- shtimes
.tms_cstime
;
304 #endif /* !BSDTIMES && !_SEQUENT_ */
305 if (WIFSIGNALED(w
)) {
306 if (WTERMSIG(w
) == SIGINT
)
307 pp
->p_flags
|= PINTERRUPTED
;
309 pp
->p_flags
|= PSIGNALED
;
311 pp
->p_flags
|= PDUMPED
;
312 pp
->p_reason
= WTERMSIG(w
);
315 pp
->p_reason
= WEXITSTATUS(w
);
316 if (pp
->p_reason
!= 0)
317 pp
->p_flags
|= PAEXITED
;
319 pp
->p_flags
|= PNEXITED
;
325 if ((fp
->p_flags
& (PPTIME
| PRUNNING
| PSTOPPED
)) == 0 &&
326 !child
&& adrof(STRtime
) &&
328 fp
->p_rusage
.ru_utime
.tv_sec
+ fp
->p_rusage
.ru_stime
.tv_sec
329 #else /* !BSDTIMES */
331 fp
->p_rusage
.ps_utime
.tv_sec
+ fp
->p_rusage
.ps_stime
.tv_sec
332 # else /* !_SEQUENT_ */
334 (fp
->p_utime
+ fp
->p_stime
) / HZ
336 (fp
->p_utime
+ fp
->p_stime
) / clk_tck
338 # endif /* !_SEQUENT_ */
339 #endif /* !BSDTIMES */
340 >= atoi(short2str(varval(STRtime
))))
341 fp
->p_flags
|= PTIME
;
342 jobflags
|= fp
->p_flags
;
343 } while ((fp
= fp
->p_friends
) != pp
);
344 pp
->p_flags
&= ~PFOREGND
;
345 if (pp
== pp
->p_friends
&& (pp
->p_flags
& PPTIME
)) {
346 pp
->p_flags
&= ~PPTIME
;
347 pp
->p_flags
|= PTIME
;
349 if ((jobflags
& (PRUNNING
| PREPORTED
)) == 0) {
352 if (fp
->p_flags
& PSTOPPED
)
353 fp
->p_flags
|= PREPORTED
;
354 } while ((fp
= fp
->p_friends
) != pp
);
355 while (fp
->p_procid
!= fp
->p_jobid
)
357 if (jobflags
& PSTOPPED
) {
358 if (pcurrent
&& pcurrent
!= fp
)
359 pprevious
= pcurrent
;
364 if (jobflags
& PFOREGND
) {
365 if (!(jobflags
& (PSIGNALED
| PSTOPPED
| PPTIME
) ||
367 jobflags
& PAEXITED
||
369 !eq(dcwd
->di_name
, fp
->p_cwd
->di_name
))) {
370 /* PWP: print a newline after ^C */
371 if (jobflags
& PINTERRUPTED
) {
372 xputchar('\r' | QUOTE
);
376 else if ((jobflags
& (PTIME
|PSTOPPED
)) == PTIME
)
382 if (jobflags
& PNOTIFY
|| adrof(STRnotify
)) {
383 xputchar('\r' | QUOTE
);
385 (void) pprint(pp
, NUMBER
| NAME
| REASON
);
386 if ((jobflags
& PSTOPPED
) == 0)
393 * don't really want to do that, because it
394 * will erase our message in case of multi-line
404 fp
->p_flags
|= PNEEDNOTE
;
409 #if defined(BSDJOBS) || defined(HAVEwait3) ||defined(WINNT_NATIVE)
411 #endif /* BSDJOBS || HAVEwait3 */
423 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
) {
424 if (pp
->p_flags
& PNEEDNOTE
) {
426 cleanup_push(&pchild_disabled
, disabled_cleanup
);
427 pp
->p_flags
&= ~PNEEDNOTE
;
428 flags
= pprint(pp
, NUMBER
| NAME
| REASON
);
429 if ((flags
& (PRUNNING
| PSTOPPED
)) == 0)
431 cleanup_until(&pchild_disabled
);
438 pfree(struct process
*pp
)
440 xfree(pp
->p_command
);
441 if (pp
->p_cwd
&& --pp
->p_cwd
->di_count
== 0)
442 if (pp
->p_cwd
->di_next
== 0)
449 * pwait - wait for current job to terminate, maintaining integrity
450 * of current and previous job indicators.
455 struct process
*fp
, *pp
;
458 * Here's where dead procs get flushed.
460 for (pp
= (fp
= &proclist
)->p_next
; pp
!= NULL
; pp
= (fp
= pp
)->p_next
)
461 if (pp
->p_procid
== 0) {
462 fp
->p_next
= pp
->p_next
;
471 * pjwait - wait for a job to finish or become stopped
472 * It is assumed to be in the foreground state (PFOREGND)
475 pjwait(struct process
*pp
)
478 int jobflags
, reason
;
479 sigset_t oset
, set
, pause_mask
;
482 while (pp
->p_procid
!= pp
->p_jobid
)
487 if ((fp
->p_flags
& (PFOREGND
| PRUNNING
)) == PRUNNING
)
488 xprintf(CGETS(17, 1, "BUG: waiting for background job!\n"));
489 } while ((fp
= fp
->p_friends
) != pp
);
491 * Now keep pausing as long as we are not interrupted (SIGINT), and the
492 * target process, or any of its friends, are running
496 sigaddset(&set
, SIGINT
);
497 sigaddset(&set
, SIGCHLD
);
498 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
499 cleanup_push(&oset
, sigprocmask_cleanup
);
501 sigdelset(&pause_mask
, SIGCHLD
);
503 handle_pending_signals();
506 jobflags
|= fp
->p_flags
;
507 while ((fp
= (fp
->p_friends
)) != pp
);
508 if ((jobflags
& PRUNNING
) == 0)
510 jobdebug_xprintf(("%d starting to sigsuspend for SIGCHLD on %d\n",
511 getpid(), fp
->p_procid
));
512 sigsuspend(&pause_mask
);
514 cleanup_until(&oset
);
515 jobdebug_xprintf(("%d returned from sigsuspend loop\n", getpid()));
517 if (tpgrp
> 0) /* get tty back */
518 (void) tcsetpgrp(FSHTTY
, tpgrp
);
520 if ((jobflags
& (PSIGNALED
| PSTOPPED
| PTIME
)) ||
521 !eq(dcwd
->di_name
, fp
->p_cwd
->di_name
)) {
522 if (jobflags
& PSTOPPED
) {
524 if (adrof(STRlistjobs
)) {
527 jobcommand
[0] = STRjobs
;
528 if (eq(varval(STRlistjobs
), STRlong
))
529 jobcommand
[1] = STRml
;
531 jobcommand
[1] = NULL
;
532 jobcommand
[2] = NULL
;
534 dojobs(jobcommand
, NULL
);
535 (void) pprint(pp
, SHELLDIR
);
538 (void) pprint(pp
, AREASON
| SHELLDIR
);
541 (void) pprint(pp
, AREASON
| SHELLDIR
);
543 if ((jobflags
& (PINTERRUPTED
| PSTOPPED
)) && setintr
&&
544 (!gointr
|| !eq(gointr
, STRminus
))) {
545 if ((jobflags
& PSTOPPED
) == 0)
554 reason
= fp
->p_flags
& (PSIGNALED
| PINTERRUPTED
) ?
555 fp
->p_reason
| META
: fp
->p_reason
;
556 } while ((fp
= fp
->p_friends
) != pp
);
558 * Don't report on backquoted jobs, cause it will mess up
561 if ((reason
!= 0) && (adrof(STRprintexitvalue
)) &&
562 (pp
->p_flags
& PBACKQ
) == 0)
563 xprintf(CGETS(17, 2, "Exit %d\n"), reason
);
564 reason_str
= putn(reason
);
565 cleanup_push(reason_str
, xfree
);
566 setv(STRstatus
, reason_str
, VAR_READWRITE
);
567 cleanup_ignore(reason_str
);
568 cleanup_until(reason_str
);
569 if (reason
&& exiterr
)
575 * dowait - wait for all processes to finish
580 dowait(Char
**v
, struct command
*c
)
588 sigprocmask(SIG_BLOCK
, NULL
, &pause_mask
);
589 sigdelset(&pause_mask
, SIGCHLD
);
591 sigdelset(&pause_mask
, SIGINT
);
593 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
594 if (pp
->p_procid
&& /* pp->p_procid == pp->p_jobid && */
595 pp
->p_flags
& PRUNNING
) {
596 handle_pending_signals();
597 sigsuspend(&pause_mask
);
598 handle_pending_signals();
605 * pflushall - flush all jobs from list (e.g. at fork())
612 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
)
618 * pflush - flag all process structures in the same job as the
619 * the argument process for deletion. The actual free of the
620 * space is not done here since pflush is called at interrupt level.
623 pflush(struct process
*pp
)
628 if (pp
->p_procid
== 0) {
629 xprintf(CGETS(17, 3, "BUG: process flushed twice"));
632 while (pp
->p_procid
!= pp
->p_jobid
)
640 np
->p_index
= np
->p_procid
= 0;
641 np
->p_flags
&= ~PNEEDNOTE
;
642 } while ((np
= np
->p_friends
) != pp
);
643 if (idx
== pmaxindex
) {
644 for (np
= proclist
.p_next
, idx
= 0; np
; np
= np
->p_next
)
645 if (np
->p_index
> idx
)
652 * pclrcurr - make sure the given job is not the current or previous job;
653 * pp MUST be the job leader
656 pclrcurr(struct process
*pp
)
658 if (pp
== pcurrent
) {
659 if (pprevious
!= NULL
) {
660 pcurrent
= pprevious
;
661 pprevious
= pgetcurr(pp
);
664 pcurrent
= pgetcurr(pp
);
665 pprevious
= pgetcurr(pp
);
668 else if (pp
== pprevious
)
669 pprevious
= pgetcurr(pp
);
672 /* +4 here is 1 for '\0', 1 ea for << >& >> */
673 static Char command
[PMAXLEN
+ 4];
674 static size_t cmdlen
;
678 * unparse - Export padd() functionality
681 unparse(struct command
*t
)
687 return Strsave(command
);
692 * palloc - allocate a process structure and fill it up.
693 * an important assumption is made that the process is running.
696 palloc(pid_t pid
, struct command
*t
)
701 pp
= xcalloc(1, sizeof(struct process
));
703 pp
->p_flags
= ((t
->t_dflg
& F_AMPERSAND
) ? 0 : PFOREGND
) | PRUNNING
;
704 if (t
->t_dflg
& F_TIME
)
705 pp
->p_flags
|= PPTIME
;
706 if (t
->t_dflg
& F_BACKQ
)
707 pp
->p_flags
|= PBACKQ
;
708 if (t
->t_dflg
& F_HUP
)
714 if (t
->t_dflg
& F_PIPEOUT
) {
716 if (t
->t_dflg
& F_STDERR
)
717 pp
->p_flags
|= PDIAG
;
719 pp
->p_command
= Strsave(command
);
723 /* careful here with interrupt level */
725 pp
->p_index
= pcurrjob
->p_index
;
726 pp
->p_friends
= pcurrjob
;
727 pp
->p_jobid
= pcurrjob
->p_procid
;
728 for (fp
= pcurrjob
; fp
->p_friends
!= pcurrjob
; fp
= fp
->p_friends
)
738 if (pmaxindex
< BIGINDEX
)
739 pp
->p_index
= ++pmaxindex
;
744 for (np
= proclist
.p_next
; np
; np
= np
->p_next
)
745 if (np
->p_index
== i
)
754 if (pcurrent
== NULL
)
756 else if (pprevious
== NULL
)
759 pp
->p_next
= proclist
.p_next
;
760 proclist
.p_next
= pp
;
762 (void) gettimeofday(&pp
->p_btime
, NULL
);
763 #else /* !BSDTIMES */
765 (void) get_process_stats(&pp
->p_btime
, PS_SELF
, NULL
, NULL
);
766 # else /* !_SEQUENT_ */
770 pp
->p_btime
= times(&tmptimes
);
772 # endif /* !_SEQUENT_ */
773 #endif /* !BSDTIMES */
777 padd(struct command
*t
)
792 for (argp
= t
->t_dcom
; *argp
; argp
++) {
826 if ((t
->t_dflg
& F_PIPEIN
) == 0 && t
->t_dlef
) {
827 pads((t
->t_dflg
& F_READ
) ? STRspLarrow2sp
: STRspLarrowsp
);
830 if ((t
->t_dflg
& F_PIPEOUT
) == 0 && t
->t_drit
) {
831 pads((t
->t_dflg
& F_APPEND
) ? STRspRarrow2
: STRspRarrow
);
832 if (t
->t_dflg
& F_STDERR
)
845 * Avoid the Quoted Space alias hack! Reported by:
846 * sam@john-bigboote.ICS.UCI.EDU (Sam Horrocks)
848 if (cp
[0] == STRQNULL
[0])
853 if (cmdlen
>= PMAXLEN
)
855 if (cmdlen
+ i
>= PMAXLEN
) {
856 (void) Strcpy(cmdp
, STRsp3dots
);
861 (void) Strcpy(cmdp
, cp
);
867 * psavejob - temporarily save the current job on a one level stack
868 * so another job can be created. Used for { } in exp6
869 * and `` in globbing.
879 psavejob_cleanup(void *dummy
)
887 * pendjob - indicate that a job (set of commands) has been completed
888 * or is about to begin.
893 struct process
*pp
, *tp
;
895 if (pcurrjob
&& (pcurrjob
->p_flags
& (PFOREGND
| PSTOPPED
)) == 0) {
897 while (pp
->p_procid
!= pp
->p_jobid
)
899 xprintf("[%d]", pp
->p_index
);
902 xprintf(" %d", pp
->p_procid
);
907 pholdjob
= pcurrjob
= 0;
911 * pprint - print a job
915 * Hacks have been added for SVR4 to deal with pipe's being spawned in
918 * David Dawes (dawes@physics.su.oz.au) Oct 1991
922 pprint(struct process
*pp
, int flag
)
926 int jobflags
, pstatus
, pcond
;
930 struct process
*pipehead
= NULL
, *pipetail
= NULL
, *pmarker
= NULL
;
932 #endif /* BACKPIPE */
934 while (pp
->p_procid
!= pp
->p_jobid
)
936 if (pp
== pp
->p_friends
&& (pp
->p_flags
& PPTIME
)) {
937 pp
->p_flags
&= ~PPTIME
;
938 pp
->p_flags
|= PTIME
;
941 status
= reason
= -1;
946 * The pipeline is reversed, so locate the real head of the pipeline
947 * if pp is at the tail of a pipe (and not already in a pipeline)
949 if ((pp
->p_friends
->p_flags
& PPOU
) && !inpipe
&& (flag
& NAME
)) {
954 while (pp
->p_friends
->p_flags
& PPOU
);
958 * pmarker is used to hold the place of the proc being processed, so
959 * we can search for the next one downstream later.
962 pcond
= (tp
!= pp
|| (inpipe
&& tp
== pp
));
963 #else /* !BACKPIPE */
965 #endif /* BACKPIPE */
967 jobflags
|= pp
->p_flags
;
968 pstatus
= (int) (pp
->p_flags
& PALLSTATES
);
969 if (pcond
&& linp
!= linbuf
&& !(flag
& FANCY
) &&
970 ((pstatus
== status
&& pp
->p_reason
== reason
) ||
974 if (pcond
&& linp
!= linbuf
)
978 pcond
= ((pp
== tp
&& !inpipe
) ||
979 (inpipe
&& pipetail
== tp
&& pp
== pipehead
));
982 #endif /* BACKPIPE */
984 xprintf("[%d]%s %c ", pp
->p_index
,
985 pp
->p_index
< 10 ? " " : "",
986 pp
== pcurrent
? '+' :
987 (pp
== pprevious
? '-' : ' '));
992 xprintf("%5d ", pp
->p_procid
);
994 xprintf("%11s ", sitename(pp
->p_procid
));
997 if (flag
& (REASON
| AREASON
)) {
1002 if (pstatus
== status
) {
1003 if (pp
->p_reason
== reason
) {
1004 xprintf(format
, "");
1008 reason
= (int) pp
->p_reason
;
1012 reason
= (int) pp
->p_reason
;
1017 xprintf(format
, CGETS(17, 4, "Running "));
1024 * tell what happened to the background job
1025 * From: Michael Schroeder
1026 * <mlschroe@immd4.informatik.uni-erlangen.de>
1029 || ((flag
& AREASON
)
1031 && (reason
!= SIGPIPE
1032 || (pp
->p_flags
& PPOU
) == 0))) {
1037 ptr
= (char *)(intptr_t)mesg
[pp
->p_reason
& 0177].pname
;
1039 ptr
= xasprintf("%s %d", CGETS(17, 5, "Signal"),
1040 pp
->p_reason
& 0177);
1041 cleanup_push(ptr
, xfree
);
1044 xprintf(format
, ptr
);
1054 if (flag
& REASON
) {
1056 xprintf(CGETS(17, 6, "Exit %-25d"), pp
->p_reason
);
1058 xprintf(format
, CGETS(17, 7, "Done"));
1063 xprintf(CGETS(17, 8, "BUG: status=%-9o"),
1070 xprintf("%S", pp
->p_command
);
1071 if (pp
->p_flags
& PPOU
)
1073 if (pp
->p_flags
& PDIAG
)
1076 if (flag
& (REASON
| AREASON
) && pp
->p_flags
& PDUMPED
)
1077 xprintf(CGETS(17, 9, " (core dumped)"));
1078 if (tp
== pp
->p_friends
) {
1079 if (flag
& AMPERSAND
)
1081 if (flag
& JOBDIR
&&
1082 !eq(tp
->p_cwd
->di_name
, dcwd
->di_name
)) {
1083 xprintf(CGETS(17, 10, " (wd: "));
1084 dtildepr(tp
->p_cwd
->di_name
);
1088 if (pp
->p_flags
& PPTIME
&& !(status
& (PSTOPPED
| PRUNNING
))) {
1091 #if defined(BSDTIMES) || defined(_SEQUENT_)
1092 prusage(&zru
, &pp
->p_rusage
, &pp
->p_etime
,
1094 #else /* !BSDTIMES && !SEQUENT */
1095 lru
.tms_utime
= pp
->p_utime
;
1096 lru
.tms_stime
= pp
->p_stime
;
1099 prusage(&zru
, &lru
, pp
->p_etime
,
1101 #endif /* !BSDTIMES && !SEQUENT */
1105 pcond
= ((tp
== pp
->p_friends
&& !inpipe
) ||
1106 (inpipe
&& pipehead
->p_friends
== tp
&& pp
== pipetail
));
1107 #else /* !BACKPIPE */
1108 pcond
= (tp
== pp
->p_friends
);
1109 #endif /* BACKPIPE */
1113 if (flag
& SHELLDIR
&& !eq(tp
->p_cwd
->di_name
, dcwd
->di_name
)) {
1114 xprintf(CGETS(17, 11, "(wd now: "));
1115 dtildepr(dcwd
->di_name
);
1122 * if pmaker == pipetail, we are finished that pipeline, and
1123 * can now skip to past the head
1125 if (pmarker
== pipetail
) {
1131 * set pp to one before the one we want next, so the while below
1132 * increments to the correct spot.
1136 while (pp
->p_friends
->p_friends
!= pmarker
);
1137 pmarker
= pp
->p_friends
;
1140 pcond
= ((pp
= pp
->p_friends
) != tp
|| inpipe
);
1141 #else /* !BACKPIPE */
1142 pcond
= ((pp
= pp
->p_friends
) != tp
);
1143 #endif /* BACKPIPE */
1146 if (jobflags
& PTIME
&& (jobflags
& (PSTOPPED
| PRUNNING
)) == 0) {
1147 if (jobflags
& NUMBER
)
1155 * All 4.3 BSD derived implementations are buggy and I've had enough.
1156 * The following implementation produces similar code and works in all
1157 * cases. The 4.3BSD one works only for <, >, !=
1160 # define timercmp(tvp, uvp, cmp) \
1161 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1162 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1163 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1166 ptprint(struct process
*tp
)
1169 struct timeval tetime
, diff
;
1170 static struct timeval ztime
;
1171 struct sysrusage ru
;
1172 struct process
*pp
= tp
;
1177 ruadd(&ru
, &pp
->p_rusage
);
1178 tvsub(&diff
, &pp
->p_etime
, &pp
->p_btime
);
1179 if (timercmp(&diff
, &tetime
, >))
1181 } while ((pp
= pp
->p_friends
) != tp
);
1182 prusage(&zru
, &ru
, &tetime
, &ztime
);
1183 #else /* !BSDTIMES */
1185 timeval_t tetime
, diff
;
1186 static timeval_t ztime
;
1187 struct process_stats ru
;
1188 struct process
*pp
= tp
;
1193 ruadd(&ru
, &pp
->p_rusage
);
1194 tvsub(&diff
, &pp
->p_etime
, &pp
->p_btime
);
1195 if (timercmp(&diff
, &tetime
, >))
1197 } while ((pp
= pp
->p_friends
) != tp
);
1198 prusage(&zru
, &ru
, &tetime
, &ztime
);
1199 # else /* !_SEQUENT_ */
1201 static time_t ztime
= 0;
1202 static time_t zu_time
= 0;
1203 static time_t zs_time
= 0;
1204 time_t tetime
, diff
;
1205 time_t u_time
, s_time
;
1208 static clock_t ztime
= 0;
1209 static clock_t zu_time
= 0;
1210 static clock_t zs_time
= 0;
1211 clock_t tetime
, diff
;
1212 clock_t u_time
, s_time
;
1215 struct tms zts
, rts
;
1216 struct process
*pp
= tp
;
1222 u_time
+= pp
->p_utime
;
1223 s_time
+= pp
->p_stime
;
1224 diff
= pp
->p_etime
- pp
->p_btime
;
1227 } while ((pp
= pp
->p_friends
) != tp
);
1228 zts
.tms_utime
= zu_time
;
1229 zts
.tms_stime
= zs_time
;
1232 rts
.tms_utime
= u_time
;
1233 rts
.tms_stime
= s_time
;
1236 prusage(&zts
, &rts
, tetime
, ztime
);
1237 # endif /* !_SEQUENT_ */
1238 #endif /* !BSDTIMES */
1242 * dojobs - print all jobs
1246 dojobs(Char
**v
, struct command
*c
)
1249 int flag
= NUMBER
| NAME
| REASON
;
1256 if (v
[1] || !eq(*v
, STRml
))
1258 flag
|= FANCY
| JOBDIR
;
1260 for (i
= 1; i
<= pmaxindex
; i
++)
1261 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1262 if (pp
->p_index
== i
&& pp
->p_procid
== pp
->p_jobid
) {
1263 pp
->p_flags
&= ~PNEEDNOTE
;
1264 if (!(pprint(pp
, flag
) & (PRUNNING
| PSTOPPED
)))
1271 * dofg - builtin - put the job into the foreground
1275 dofg(Char
**v
, struct command
*c
)
1284 if (!pstart(pp
, 1)) {
1286 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1290 } while (*v
&& *++v
);
1294 * %... - builtin - put the job into the foreground
1298 dofg1(Char
**v
, struct command
*c
)
1305 if (!pstart(pp
, 1)) {
1307 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1314 * dobg - builtin - put the job into the background
1318 dobg(Char
**v
, struct command
*c
)
1327 if (!pstart(pp
, 0)) {
1329 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1331 } while (*v
&& *++v
);
1335 * %... & - builtin - put the job into the background
1339 dobg1(Char
**v
, struct command
*c
)
1345 if (!pstart(pp
, 0)) {
1347 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1352 * dostop - builtin - stop the job
1356 dostop(Char
**v
, struct command
*c
)
1360 pkill(++v
, SIGSTOP
);
1361 #endif /* BSDJOBS */
1365 * dokill - builtin - superset of kill (1)
1369 dokill(Char
**v
, struct command
*c
)
1371 int signum
, len
= 0;
1377 if (v
[0] && v
[0][0] == '-') {
1378 if (v
[0][1] == 'l') {
1379 for (signum
= 0; signum
<= nsig
; signum
++) {
1380 if ((name
= mesg
[signum
].iname
) != NULL
) {
1381 len
+= strlen(name
) + 1;
1382 if (len
>= TermH
- 1) {
1384 len
= strlen(name
) + 1;
1386 xprintf("%s ", name
);
1393 if (v
[0][1] == 's') {
1398 stderror(ERR_NAME
| ERR_TOOFEW
);
1401 if (Isdigit(*sigptr
)) {
1403 signum
= strtoul(short2str(sigptr
), &ep
, 0);
1404 if (*ep
|| signum
< 0 || signum
> (MAXSIG
-1))
1405 stderror(ERR_NAME
| ERR_BADSIG
);
1408 for (signum
= 0; signum
<= nsig
; signum
++)
1409 if (mesg
[signum
].iname
&&
1410 eq(sigptr
, str2short(mesg
[signum
].iname
)))
1412 setname(short2str(sigptr
));
1413 stderror(ERR_NAME
| ERR_UNKSIG
);
1424 pkill(Char
**v
, int signum
)
1426 struct process
*pp
, *np
;
1427 int jobflags
= 0, err1
= 0;
1429 Char
*cp
, **vp
, **globbed
;
1432 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1435 cleanup_push(&pintr_disabled
, disabled_cleanup
);
1438 /* Avoid globbing %?x patterns */
1439 for (vp
= v
; vp
&& *vp
; vp
++)
1443 v
= glob_all_or_error(v
);
1445 cleanup_push(globbed
, blk_cleanup
);
1448 while (v
&& (cp
= *v
)) {
1450 np
= pp
= pfind(cp
);
1452 jobflags
|= np
->p_flags
;
1453 while ((np
= np
->p_friends
) != pp
);
1461 if ((jobflags
& PRUNNING
) == 0) {
1463 xprintf(CGETS(17, 12, "%S: Already suspended\n"), cp
);
1464 # else /* !SUSPENDED */
1465 xprintf(CGETS(17, 13, "%S: Already stopped\n"), cp
);
1466 # endif /* !SUSPENDED */
1472 * suspend a process, kill -CONT %, then type jobs; the shell
1473 * says it is suspended, but it is running; thanks jaap..
1476 if (!pstart(pp
, 0)) {
1478 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
,
1485 #endif /* BSDJOBS */
1486 if (killpg(pp
->p_jobid
, signum
) < 0) {
1487 xprintf("%S: %s\n", cp
, strerror(errno
));
1491 if (signum
== SIGTERM
|| signum
== SIGHUP
)
1492 (void) killpg(pp
->p_jobid
, SIGCONT
);
1493 #endif /* BSDJOBS */
1495 else if (!(Isdigit(*cp
) || *cp
== '-'))
1496 stderror(ERR_NAME
| ERR_JOBARGS
);
1499 #ifndef WINNT_NATIVE
1500 pid
= strtol(short2str(cp
), &ep
, 10);
1502 pid
= strtoul(short2str(cp
), &ep
, 0);
1503 #endif /* WINNT_NATIVE */
1505 stderror(ERR_NAME
| ERR_JOBARGS
);
1506 else if (kill(pid
, signum
) < 0) {
1507 xprintf("%d: %s\n", pid
, strerror(errno
));
1512 if (signum
== SIGTERM
|| signum
== SIGHUP
)
1513 (void) kill(pid
, SIGCONT
);
1514 #endif /* BSDJOBS */
1519 cleanup_until(&pchild_disabled
);
1521 stderror(ERR_SILENT
);
1525 * pstart - start the job in foreground/background
1528 pstart(struct process
*pp
, int foregnd
)
1532 /* We don't use jobflags in this function right now (see below) */
1533 /* long jobflags = 0; */
1536 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1539 /* We don't use jobflags in this function right now (see below) */
1540 /* jobflags |= np->p_flags; */
1541 if (np
->p_flags
& (PRUNNING
| PSTOPPED
)) {
1542 np
->p_flags
|= PRUNNING
;
1543 np
->p_flags
&= ~PSTOPPED
;
1545 np
->p_flags
|= PFOREGND
;
1547 np
->p_flags
&= ~PFOREGND
;
1549 } while ((np
= np
->p_friends
) != pp
);
1552 (void) pprint(pp
, foregnd
? NAME
| JOBDIR
: NUMBER
| NAME
| AMPERSAND
);
1554 /* GrP run jobcmd hook if foregrounding */
1556 job_cmd(pp
->p_command
);
1561 rv
= tcsetpgrp(FSHTTY
, pp
->p_jobid
);
1564 * 1. child process of csh (shell script) receives SIGTTIN/SIGTTOU
1565 * 2. parent process (csh) receives SIGCHLD
1566 * 3. The "csh" signal handling function pchild() is invoked
1567 * with a SIGCHLD signal.
1568 * 4. pchild() calls wait3(WNOHANG) which returns 0.
1569 * The child process is NOT ready to be waited for at this time.
1570 * pchild() returns without picking-up the correct status
1571 * for the child process which generated the SIGCHLD.
1572 * 5. CONSEQUENCE : csh is UNaware that the process is stopped
1573 * 6. THIS LINE HAS BEEN COMMENTED OUT : if (jobflags&PSTOPPED)
1574 * (beto@aixwiz.austin.ibm.com - aug/03/91)
1575 * 7. I removed the line completely and added extra checks for
1576 * pstart, so that if a job gets attached to and dies inside
1577 * a debugger it does not confuse the shell. [christos]
1578 * 8. on the nec sx-4 there seems to be a problem, which requires
1579 * a syscall(151, getpid(), getpid()) in osinit. Don't ask me
1580 * what this is doing. [schott@rzg.mpg.de]
1584 rv
= killpg(pp
->p_jobid
, SIGCONT
);
1585 #endif /* BSDJOBS */
1586 cleanup_until(&pchild_disabled
);
1591 panystop(int neednl
)
1596 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1597 if (pp
->p_flags
& PSTOPPED
)
1598 stderror(ERR_STOPPED
, neednl
? "\n" : "");
1604 struct process
*pp
, *np
;
1606 if (cp
== 0 || cp
[1] == 0 || eq(cp
, STRcent2
) || eq(cp
, STRcentplus
)) {
1607 if (pcurrent
== NULL
)
1608 stderror(ERR_NAME
| ERR_JOBCUR
);
1611 if (eq(cp
, STRcentminus
) || eq(cp
, STRcenthash
)) {
1612 if (pprevious
== NULL
)
1613 stderror(ERR_NAME
| ERR_JOBPREV
);
1616 if (Isdigit(cp
[1])) {
1617 int idx
= atoi(short2str(cp
+ 1));
1619 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1620 if (pp
->p_index
== idx
&& pp
->p_procid
== pp
->p_jobid
)
1622 stderror(ERR_NAME
| ERR_NOSUCHJOB
);
1625 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1626 if (pp
->p_procid
== pp
->p_jobid
) {
1630 for (dp
= pp
->p_command
; *dp
; dp
++) {
1633 if (prefix(cp
+ 2, dp
))
1637 else if (prefix(cp
+ 1, pp
->p_command
)) {
1640 stderror(ERR_NAME
| ERR_AMBIG
);
1646 stderror(ERR_NAME
| (cp
[1] == '?' ? ERR_JOBPAT
: ERR_NOSUCHJOB
));
1653 * pgetcurr - find most recent job that is not pp, preferably stopped
1655 static struct process
*
1656 pgetcurr(struct process
*pp
)
1659 struct process
*xp
= NULL
;
1661 for (np
= proclist
.p_next
; np
; np
= np
->p_next
)
1662 if (np
!= pcurrent
&& np
!= pp
&& np
->p_procid
&&
1663 np
->p_procid
== np
->p_jobid
) {
1664 if (np
->p_flags
& PSTOPPED
)
1673 * donotify - flag the job so as to report termination asynchronously
1677 donotify(Char
**v
, struct command
*c
)
1683 pp
->p_flags
|= PNOTIFY
;
1688 synch_handler(int sno
)
1692 #endif /* SIGSYNCH */
1695 * Do the fork and whatever should be done in the child side that
1696 * should not be done if we are not forking at all (like for simple builtin's)
1697 * Also do everything that needs any signals fiddled with in the parent side
1699 * Wanttty tells whether process and/or tty pgrps are to be manipulated:
1700 * -1: leave tty alone; inherit pgrp from parent
1701 * 0: already have tty; manipulate process pgrps only
1702 * 1: want to claim tty; manipulate process and tty pgrps
1703 * It is usually just the value of tpgrp.
1707 pfork(struct command
*t
, int wanttty
)
1713 struct sigaction osa
, nsa
;
1714 #endif /* SIGSYNCH */
1717 * A child will be uninterruptible only under very special conditions.
1718 * Remember that the semantics of '&' is implemented by disconnecting the
1719 * process from the tty so signals do not need to ignored just for '&'.
1720 * Thus signals are set to default action for children unless: we have had
1721 * an "onintr -" (then specifically ignored) we are not playing with
1722 * signals (inherit action)
1725 ignint
= (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
))
1726 || (gointr
&& eq(gointr
, STRminus
));
1729 * Check for maximum nesting of 16 processes to avoid Forking loops
1732 stderror(ERR_NESTING
, 16);
1734 nsa
.sa_handler
= synch_handler
;
1735 sigfillset(&nsa
.sa_mask
);
1736 nsa
.sa_flags
= SA_RESTART
;
1737 if (sigaction(SIGSYNCH
, &nsa
, &osa
))
1738 stderror(ERR_SYSTEM
, "pfork: sigaction set", strerror(errno
));
1739 #endif /* SIGSYNCH */
1741 * Hold pchild() until we have the process installed in our table.
1745 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1747 while ((pid
= fork()) == -1)
1749 (void) sleep(FORKSLEEP
);
1751 stderror(ERR_NOPROC
);
1753 (void)cleanup_push_mark(); /* Never to be popped */
1754 pchild_disabled
= 0;
1756 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: getpid();
1759 #if !defined(BSDTIMES) && !defined(_SEQUENT_)
1761 #endif /* !defined(BSDTIMES) && !defined(_SEQUENT_) */
1764 setintr
= 0; /* until I think otherwise */
1766 * Children just get blown away on SIGINT, SIGQUIT unless "onintr
1769 (void) signal(SIGINT
, ignint
? SIG_IGN
: SIG_DFL
);
1770 (void) signal(SIGQUIT
, ignint
? SIG_IGN
: SIG_DFL
);
1773 /* make stoppable */
1774 (void) signal(SIGTSTP
, SIG_DFL
);
1775 (void) signal(SIGTTIN
, SIG_DFL
);
1776 (void) signal(SIGTTOU
, SIG_DFL
);
1778 #endif /* BSDJOBS */
1779 sigaction(SIGTERM
, &parterm
, NULL
);
1781 else if (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
)) {
1782 (void) signal(SIGINT
, SIG_IGN
);
1783 (void) signal(SIGQUIT
, SIG_IGN
);
1786 signal(SIGIO
, SIG_IGN
); /* ignore SIGIO in child too */
1789 pgetty(wanttty
, pgrp
);
1791 * Nohup and nice apply only to NODE_COMMAND's but it would be nice
1792 * (?!?) if you could say "nohup (foo;bar)" Then the parser would have
1793 * to know about nice/nohup/time
1795 if (t
->t_dflg
& F_NOHUP
)
1796 (void) signal(SIGHUP
, SIG_IGN
);
1797 if (t
->t_dflg
& F_NICE
) {
1798 int nval
= SIGN_EXTEND_CHAR(t
->t_nice
);
1799 #ifdef HAVE_SETPRIORITY
1800 if (setpriority(PRIO_PROCESS
, 0, nval
) == -1 && errno
)
1801 stderror(ERR_SYSTEM
, "setpriority", strerror(errno
));
1802 #else /* !HAVE_SETPRIORITY */
1804 #endif /* !HAVE_SETPRIORITY */
1807 if (t
->t_dflg
& F_VER
) {
1808 tsetenv(STRSYSTYPE
, t
->t_systype
? STRbsd43
: STRsys53
);
1813 /* rfw 8/89 now parent can continue */
1814 if (kill(getppid(), SIGSYNCH
))
1815 stderror(ERR_SYSTEM
, "pfork child: kill", strerror(errno
));
1816 #endif /* SIGSYNCH */
1823 * `Walking' process group fix from Beto Appleton.
1824 * (beto@aixwiz.austin.ibm.com)
1825 * If setpgid fails at this point that means that
1826 * our process leader has died. We flush the current
1827 * job and become the process leader ourselves.
1828 * The parent will figure that out later.
1830 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: pid
;
1831 if (setpgid(pid
, pgrp
) == -1 && errno
== EPERM
) {
1834 * We don't care if this causes an error here;
1835 * then we are already in the right process group
1837 (void) setpgid(pid
, pgrp
= pid
);
1840 #endif /* POSIXJOBS */
1844 sigset_t pause_mask
;
1847 * rfw 8/89 Wait for child to own terminal. Solves half of ugly
1848 * synchronization problem. With this change, we know that the only
1849 * reason setpgrp to a previous process in a pipeline can fail is that
1850 * the previous process has already exited. Without this hack, he may
1851 * either have exited or not yet started to run. Two uglies become
1854 sigprocmask(SIG_BLOCK
, NULL
, &pause
);
1855 sigdelset(&pause_mask
, SIGCHLD
);
1856 sigdelset(&pause_mask
, SIGSYNCH
);
1857 sigsuspend(&pause_mask
);
1858 handle_pending_signals();
1859 if (sigaction(SIGSYNCH
, &osa
, NULL
))
1860 stderror(ERR_SYSTEM
, "pfork parent: sigaction restore",
1863 #endif /* SIGSYNCH */
1866 cleanup_until(&pchild_disabled
);
1875 stderror(ERR_JOBCONTROL
);
1877 stderror(ERR_JOBCTRLSUB
);
1882 setttypgrp(int pgrp
)
1885 * If we are piping out a builtin, eg. 'echo | more' things can go
1886 * out of sequence, i.e. the more can run before the echo. This
1887 * can happen even if we have vfork, since the echo will be forked
1888 * with the regular fork. In this case, we need to set the tty
1889 * pgrp ourselves. If that happens, then the process will be still
1890 * alive. And the tty process group will already be set.
1891 * This should fix the famous sequent problem as a side effect:
1892 * The controlling terminal is lost if all processes in the
1893 * terminal process group are zombies. In this case tcgetpgrp()
1894 * returns 0. If this happens we must set the terminal process
1897 if (tcgetpgrp(FSHTTY
) != pgrp
) {
1899 struct sigaction old
;
1902 * tcsetpgrp will set SIGTTOU to all the the processes in
1903 * the background according to POSIX... We ignore this here.
1905 sigaction(SIGTTOU
, NULL
, &old
);
1906 signal(SIGTTOU
, SIG_IGN
);
1908 (void) tcsetpgrp(FSHTTY
, pgrp
);
1910 sigaction(SIGTTOU
, &old
, NULL
);
1918 * if we don't have vfork(), things can still go in the wrong order
1919 * resulting in the famous 'Stopped (tty output)'. But some systems
1920 * don't permit the setpgid() call, (these are more recent secure
1921 * systems such as ibm's aix), when they do. Then we'd rather print
1922 * an error message than hang the shell!
1923 * I am open to suggestions how to fix that.
1926 pgetty(int wanttty
, pid_t pgrp
)
1931 # endif /* POSIXJOBS */
1933 jobdebug_xprintf(("wanttty %d pid %d opgrp%d pgrp %d tpgrp %d\n",
1934 wanttty
, (int)getpid(), (int)pgrp
, (int)mygetpgrp(),
1935 (int)tcgetpgrp(FSHTTY
)));
1938 * christos: I am blocking the tty signals till I've set things
1943 sigaddset(&set
, SIGTSTP
);
1944 sigaddset(&set
, SIGTTIN
);
1945 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
1946 cleanup_push(&oset
, sigprocmask_cleanup
);
1948 # endif /* POSIXJOBS */
1953 # endif /* !POSIXJOBS */
1956 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
1957 * Don't check for tpgrp >= 0 so even non-interactive shells give
1958 * background jobs process groups Same for the comparison in the other part
1962 if (setpgid(0, pgrp
) == -1) {
1964 /* Walking process group fix; see above */
1965 if (setpgid(0, pgrp
= getpid()) == -1) {
1966 # endif /* POSIXJOBS */
1967 stderror(ERR_SYSTEM
, "setpgid child:\n", strerror(errno
));
1971 wanttty
= pgrp
; /* Now we really want the tty, since we became the
1972 * the process group leader
1974 # endif /* POSIXJOBS */
1981 cleanup_until(&oset
);
1983 # endif /* POSIXJOBS */
1985 jobdebug_xprintf(("wanttty %d pid %d pgrp %d tpgrp %d\n",
1986 wanttty
, getpid(), mygetpgrp(), tcgetpgrp(FSHTTY
)));
1989 tpgrp
= 0; /* gave tty away */
1990 #endif /* BSDJOBS */