1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.proc.c,v 3.109 2009/06/25 21:15:37 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.109 2009/06/25 21:15:37 christos 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 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__)
193 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), &ru
);
195 pid
= wait3(&w
.w_status
,
196 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), &ru
);
197 # endif /* BSDWAIT */
199 # else /* !BSDTIMES */
201 (void) get_process_stats(&tv
, PS_SELF
, 0, &cpst1
);
202 pid
= waitpid(-1, &w
,
203 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
204 (void) get_process_stats(&tv
, PS_SELF
, 0, &cpst2
);
205 pr_stat_sub(&cpst2
, &cpst1
, &ru
);
206 # else /* !_SEQUENT_ */
208 /* we have a wait3, but no rusage stuff */
209 pid
= wait3(&w
.w_status
,
210 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), 0);
212 pid
= waitpid(-1, &w
,
213 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
215 # endif /* !_SEQUENT_ */
216 # endif /* !BSDTIMES */
217 # else /* !BSDJOBS */
220 /* both a wait3 and rusage */
222 pid
= wait3(&w
.w_status
, WNOHANG
, 0);
224 pid
= wait3(&w
.w_status
, WNOHANG
, &ru
);
226 # else /* !BSDTIMES */
227 # ifdef ODT /* For Sco Unix 3.2.0 or ODT 1.0 */
229 pid
= waitpid(-1, &w
,
230 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
232 # if defined(aiws) || defined(uts)
234 pid
= wait3(&w
.w_status
,
235 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), 0);
236 # endif /* aiws || uts */
239 /* no wait3, therefore no rusage */
240 /* on Sys V, this may hang. I hope it's not going to be a problem */
244 * XXX: for greater than 3 we should use waitpid().
245 * but then again, SVR4 falls into the POSIX/BSDJOBS category.
247 pid
= wait(&w
.w_status
);
248 # endif /* BSDWAIT */
249 # endif /* !HAVEwait3 */
250 # endif /* !BSDTIMES */
251 # endif /* !BSDJOBS */
252 #else /* WINNT_NATIVE */
253 pid
= waitpid(-1, &w
,
254 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
255 #endif /* WINNT_NATIVE */
257 jobdebug_xprintf(("parent %d pid %d, retval %x termsig %x retcode %x\n",
258 (int)getpid(), (int)pid
, w
, WTERMSIG(w
),
262 if ((pid
== 0) || (pid
== -1)) {
263 handle_pending_signals();
264 jobdebug_xprintf(("errno == %d\n", errno
));
269 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
)
270 if (pid
== pp
->p_procid
)
272 #if !defined(BSDJOBS) && !defined(WINNT_NATIVE)
273 /* this should never have happened */
274 stderror(ERR_SYNC
, pid
);
276 #else /* BSDJOBS || WINNT_NATIVE */
278 #endif /* !BSDJOBS && !WINNT_NATIVE */
280 pp
->p_flags
&= ~(PRUNNING
| PSTOPPED
| PREPORTED
);
282 pp
->p_flags
|= PSTOPPED
;
283 pp
->p_reason
= WSTOPSIG(w
);
286 if (pp
->p_flags
& (PTIME
| PPTIME
) || adrof(STRtime
))
289 (void) get_process_stats(&pp
->p_etime
, PS_SELF
, NULL
, NULL
);
290 # else /* !_SEQUENT_ */
291 pp
->p_etime
= times(&proctimes
);
292 # endif /* !_SEQUENT_ */
294 (void) gettimeofday(&pp
->p_etime
, NULL
);
295 #endif /* BSDTIMES */
298 #if defined(BSDTIMES) || defined(_SEQUENT_)
300 #else /* !BSDTIMES && !_SEQUENT_ */
301 (void) times(&proctimes
);
302 pp
->p_utime
= proctimes
.tms_cutime
- shtimes
.tms_cutime
;
303 pp
->p_stime
= proctimes
.tms_cstime
- shtimes
.tms_cstime
;
305 #endif /* !BSDTIMES && !_SEQUENT_ */
306 if (WIFSIGNALED(w
)) {
307 if (WTERMSIG(w
) == SIGINT
)
308 pp
->p_flags
|= PINTERRUPTED
;
310 pp
->p_flags
|= PSIGNALED
;
312 pp
->p_flags
|= PDUMPED
;
313 pp
->p_reason
= WTERMSIG(w
);
316 pp
->p_reason
= WEXITSTATUS(w
);
317 if (pp
->p_reason
!= 0)
318 pp
->p_flags
|= PAEXITED
;
320 pp
->p_flags
|= PNEXITED
;
326 if ((fp
->p_flags
& (PPTIME
| PRUNNING
| PSTOPPED
)) == 0 &&
327 !child
&& adrof(STRtime
) &&
329 fp
->p_rusage
.ru_utime
.tv_sec
+ fp
->p_rusage
.ru_stime
.tv_sec
330 #else /* !BSDTIMES */
332 fp
->p_rusage
.ps_utime
.tv_sec
+ fp
->p_rusage
.ps_stime
.tv_sec
333 # else /* !_SEQUENT_ */
335 (fp
->p_utime
+ fp
->p_stime
) / HZ
337 (fp
->p_utime
+ fp
->p_stime
) / clk_tck
339 # endif /* !_SEQUENT_ */
340 #endif /* !BSDTIMES */
341 >= atoi(short2str(varval(STRtime
))))
342 fp
->p_flags
|= PTIME
;
343 jobflags
|= fp
->p_flags
;
344 } while ((fp
= fp
->p_friends
) != pp
);
345 pp
->p_flags
&= ~PFOREGND
;
346 if (pp
== pp
->p_friends
&& (pp
->p_flags
& PPTIME
)) {
347 pp
->p_flags
&= ~PPTIME
;
348 pp
->p_flags
|= PTIME
;
350 if ((jobflags
& (PRUNNING
| PREPORTED
)) == 0) {
353 if (fp
->p_flags
& PSTOPPED
)
354 fp
->p_flags
|= PREPORTED
;
355 } while ((fp
= fp
->p_friends
) != pp
);
356 while (fp
->p_procid
!= fp
->p_jobid
)
358 if (jobflags
& PSTOPPED
) {
359 if (pcurrent
&& pcurrent
!= fp
)
360 pprevious
= pcurrent
;
365 if (jobflags
& PFOREGND
) {
366 if (!(jobflags
& (PSIGNALED
| PSTOPPED
| PPTIME
) ||
368 jobflags
& PAEXITED
||
370 !eq(dcwd
->di_name
, fp
->p_cwd
->di_name
))) {
371 /* PWP: print a newline after ^C */
372 if (jobflags
& PINTERRUPTED
) {
373 xputchar('\r' | QUOTE
);
377 else if ((jobflags
& (PTIME
|PSTOPPED
)) == PTIME
)
383 if (jobflags
& PNOTIFY
|| adrof(STRnotify
)) {
384 xputchar('\r' | QUOTE
);
386 (void) pprint(pp
, NUMBER
| NAME
| REASON
);
387 if ((jobflags
& PSTOPPED
) == 0)
394 * don't really want to do that, because it
395 * will erase our message in case of multi-line
405 fp
->p_flags
|= PNEEDNOTE
;
410 #if defined(BSDJOBS) || defined(HAVEwait3) ||defined(WINNT_NATIVE)
412 #endif /* BSDJOBS || HAVEwait3 */
424 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
) {
425 if (pp
->p_flags
& PNEEDNOTE
) {
427 cleanup_push(&pchild_disabled
, disabled_cleanup
);
428 pp
->p_flags
&= ~PNEEDNOTE
;
429 flags
= pprint(pp
, NUMBER
| NAME
| REASON
);
430 if ((flags
& (PRUNNING
| PSTOPPED
)) == 0)
432 cleanup_until(&pchild_disabled
);
439 pfree(struct process
*pp
)
441 xfree(pp
->p_command
);
442 if (pp
->p_cwd
&& --pp
->p_cwd
->di_count
== 0)
443 if (pp
->p_cwd
->di_next
== 0)
450 * pwait - wait for current job to terminate, maintaining integrity
451 * of current and previous job indicators.
456 struct process
*fp
, *pp
;
459 * Here's where dead procs get flushed.
461 for (pp
= (fp
= &proclist
)->p_next
; pp
!= NULL
; pp
= (fp
= pp
)->p_next
)
462 if (pp
->p_procid
== 0) {
463 fp
->p_next
= pp
->p_next
;
472 * pjwait - wait for a job to finish or become stopped
473 * It is assumed to be in the foreground state (PFOREGND)
476 pjwait(struct process
*pp
)
479 int jobflags
, reason
;
480 sigset_t oset
, set
, pause_mask
;
483 while (pp
->p_procid
!= pp
->p_jobid
)
488 if ((fp
->p_flags
& (PFOREGND
| PRUNNING
)) == PRUNNING
)
489 xprintf("%s", CGETS(17, 1, "BUG: waiting for background job!\n"));
490 } while ((fp
= fp
->p_friends
) != pp
);
492 * Now keep pausing as long as we are not interrupted (SIGINT), and the
493 * target process, or any of its friends, are running
497 sigaddset(&set
, SIGINT
);
498 sigaddset(&set
, SIGCHLD
);
499 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
500 cleanup_push(&oset
, sigprocmask_cleanup
);
502 sigdelset(&pause_mask
, SIGCHLD
);
504 handle_pending_signals();
507 jobflags
|= fp
->p_flags
;
508 while ((fp
= (fp
->p_friends
)) != pp
);
509 if ((jobflags
& PRUNNING
) == 0)
511 jobdebug_xprintf(("%d starting to sigsuspend for SIGCHLD on %d\n",
512 getpid(), fp
->p_procid
));
513 sigsuspend(&pause_mask
);
515 cleanup_until(&oset
);
516 jobdebug_xprintf(("%d returned from sigsuspend loop\n", getpid()));
518 if (tpgrp
> 0) /* get tty back */
519 (void) tcsetpgrp(FSHTTY
, tpgrp
);
521 if ((jobflags
& (PSIGNALED
| PSTOPPED
| PTIME
)) ||
522 fp
->p_cwd
== NULL
|| !eq(dcwd
->di_name
, fp
->p_cwd
->di_name
)) {
523 if (jobflags
& PSTOPPED
) {
525 if (adrof(STRlistjobs
)) {
528 jobcommand
[0] = STRjobs
;
529 if (eq(varval(STRlistjobs
), STRlong
))
530 jobcommand
[1] = STRml
;
532 jobcommand
[1] = NULL
;
533 jobcommand
[2] = NULL
;
535 dojobs(jobcommand
, NULL
);
536 (void) pprint(pp
, SHELLDIR
);
539 (void) pprint(pp
, AREASON
| SHELLDIR
);
542 (void) pprint(pp
, AREASON
| SHELLDIR
);
544 if ((jobflags
& (PINTERRUPTED
| PSTOPPED
)) && setintr
&&
545 (!gointr
|| !eq(gointr
, STRminus
))) {
546 if ((jobflags
& PSTOPPED
) == 0)
555 reason
= fp
->p_flags
& (PSIGNALED
| PINTERRUPTED
) ?
556 fp
->p_reason
| META
: fp
->p_reason
;
557 } while ((fp
= fp
->p_friends
) != pp
);
559 * Don't report on backquoted jobs, cause it will mess up
562 if ((reason
!= 0) && (adrof(STRprintexitvalue
)) &&
563 (pp
->p_flags
& PBACKQ
) == 0)
564 xprintf(CGETS(17, 2, "Exit %d\n"), reason
);
565 reason_str
= putn(reason
);
566 cleanup_push(reason_str
, xfree
);
567 setv(STRstatus
, reason_str
, VAR_READWRITE
);
568 cleanup_ignore(reason_str
);
569 cleanup_until(reason_str
);
570 if (reason
&& exiterr
)
576 * dowait - wait for all processes to finish
581 dowait(Char
**v
, struct command
*c
)
589 sigprocmask(SIG_BLOCK
, NULL
, &pause_mask
);
590 sigdelset(&pause_mask
, SIGCHLD
);
592 sigdelset(&pause_mask
, SIGINT
);
594 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
595 if (pp
->p_procid
&& /* pp->p_procid == pp->p_jobid && */
596 pp
->p_flags
& PRUNNING
) {
597 handle_pending_signals();
598 sigsuspend(&pause_mask
);
599 handle_pending_signals();
606 * pflushall - flush all jobs from list (e.g. at fork())
613 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
)
619 * pflush - flag all process structures in the same job as the
620 * the argument process for deletion. The actual free of the
621 * space is not done here since pflush is called at interrupt level.
624 pflush(struct process
*pp
)
629 if (pp
->p_procid
== 0) {
630 xprintf("%s", CGETS(17, 3, "BUG: process flushed twice"));
633 while (pp
->p_procid
!= pp
->p_jobid
)
641 np
->p_index
= np
->p_procid
= 0;
642 np
->p_flags
&= ~PNEEDNOTE
;
643 } while ((np
= np
->p_friends
) != pp
);
644 if (idx
== pmaxindex
) {
645 for (np
= proclist
.p_next
, idx
= 0; np
; np
= np
->p_next
)
646 if (np
->p_index
> idx
)
653 * pclrcurr - make sure the given job is not the current or previous job;
654 * pp MUST be the job leader
657 pclrcurr(struct process
*pp
)
659 if (pp
== pcurrent
) {
660 if (pprevious
!= NULL
) {
661 pcurrent
= pprevious
;
662 pprevious
= pgetcurr(pp
);
665 pcurrent
= pgetcurr(pp
);
666 pprevious
= pgetcurr(pp
);
669 else if (pp
== pprevious
)
670 pprevious
= pgetcurr(pp
);
673 /* +4 here is 1 for '\0', 1 ea for << >& >> */
675 static size_t cmdmax
;
676 static size_t cmdlen
;
678 #define CMD_INIT 1024
682 morecommand(size_t s
)
688 ncmdstr
= xrealloc(cmdstr
, cmdmax
* sizeof(*cmdstr
));
689 d
= ncmdstr
- cmdstr
;
695 * unparse - Export padd() functionality
698 unparse(struct command
*t
)
701 morecommand(CMD_INIT
);
706 return Strsave(cmdstr
);
711 * palloc - allocate a process structure and fill it up.
712 * an important assumption is made that the process is running.
715 palloc(pid_t pid
, struct command
*t
)
720 pp
= xcalloc(1, sizeof(struct process
));
722 pp
->p_flags
= ((t
->t_dflg
& F_AMPERSAND
) ? 0 : PFOREGND
) | PRUNNING
;
723 if (t
->t_dflg
& F_TIME
)
724 pp
->p_flags
|= PPTIME
;
725 if (t
->t_dflg
& F_BACKQ
)
726 pp
->p_flags
|= PBACKQ
;
727 if (t
->t_dflg
& F_HUP
)
730 morecommand(CMD_INIT
);
735 if (t
->t_dflg
& F_PIPEOUT
) {
737 if (t
->t_dflg
& F_STDERR
)
738 pp
->p_flags
|= PDIAG
;
740 pp
->p_command
= Strsave(cmdstr
);
744 /* careful here with interrupt level */
746 pp
->p_index
= pcurrjob
->p_index
;
747 pp
->p_friends
= pcurrjob
;
748 pp
->p_jobid
= pcurrjob
->p_procid
;
749 for (fp
= pcurrjob
; fp
->p_friends
!= pcurrjob
; fp
= fp
->p_friends
)
759 if (pmaxindex
< BIGINDEX
)
760 pp
->p_index
= ++pmaxindex
;
765 for (np
= proclist
.p_next
; np
; np
= np
->p_next
)
766 if (np
->p_index
== i
)
775 if (pcurrent
== NULL
)
777 else if (pprevious
== NULL
)
780 pp
->p_next
= proclist
.p_next
;
781 proclist
.p_next
= pp
;
783 (void) gettimeofday(&pp
->p_btime
, NULL
);
784 #else /* !BSDTIMES */
786 (void) get_process_stats(&pp
->p_btime
, PS_SELF
, NULL
, NULL
);
787 # else /* !_SEQUENT_ */
791 pp
->p_btime
= times(&tmptimes
);
793 # endif /* !_SEQUENT_ */
794 #endif /* !BSDTIMES */
798 padd(struct command
*t
)
813 for (argp
= t
->t_dcom
; *argp
; argp
++) {
847 if ((t
->t_dflg
& F_PIPEIN
) == 0 && t
->t_dlef
) {
848 pads((t
->t_dflg
& F_READ
) ? STRspLarrow2sp
: STRspLarrowsp
);
851 if ((t
->t_dflg
& F_PIPEOUT
) == 0 && t
->t_drit
) {
852 pads((t
->t_dflg
& F_APPEND
) ? STRspRarrow2
: STRspRarrow
);
853 if (t
->t_dflg
& F_STDERR
)
866 * Avoid the Quoted Space alias hack! Reported by:
867 * sam@john-bigboote.ICS.UCI.EDU (Sam Horrocks)
869 if (cp
[0] == STRQNULL
[0])
874 len
= cmdlen
+ i
+ CMD_INCR
;
877 (void) Strcpy(cmdp
, cp
);
883 * psavejob - temporarily save the current job on a one level stack
884 * so another job can be created. Used for { } in exp6
885 * and `` in globbing.
895 psavejob_cleanup(void *dummy
)
903 * pendjob - indicate that a job (set of commands) has been completed
904 * or is about to begin.
909 struct process
*pp
, *tp
;
911 if (pcurrjob
&& (pcurrjob
->p_flags
& (PFOREGND
| PSTOPPED
)) == 0) {
913 while (pp
->p_procid
!= pp
->p_jobid
)
915 xprintf("[%d]", pp
->p_index
);
918 xprintf(" %d", pp
->p_procid
);
923 pholdjob
= pcurrjob
= 0;
927 * pprint - print a job
931 * Hacks have been added for SVR4 to deal with pipe's being spawned in
934 * David Dawes (dawes@physics.su.oz.au) Oct 1991
938 pprint(struct process
*pp
, int flag
)
942 int jobflags
, pstatus
, pcond
;
946 struct process
*pipehead
= NULL
, *pipetail
= NULL
, *pmarker
= NULL
;
948 #endif /* BACKPIPE */
950 while (pp
->p_procid
!= pp
->p_jobid
)
952 if (pp
== pp
->p_friends
&& (pp
->p_flags
& PPTIME
)) {
953 pp
->p_flags
&= ~PPTIME
;
954 pp
->p_flags
|= PTIME
;
957 status
= reason
= -1;
962 * The pipeline is reversed, so locate the real head of the pipeline
963 * if pp is at the tail of a pipe (and not already in a pipeline)
965 if ((pp
->p_friends
->p_flags
& PPOU
) && !inpipe
&& (flag
& NAME
)) {
970 while (pp
->p_friends
->p_flags
& PPOU
);
974 * pmarker is used to hold the place of the proc being processed, so
975 * we can search for the next one downstream later.
978 pcond
= (tp
!= pp
|| (inpipe
&& tp
== pp
));
979 #else /* !BACKPIPE */
981 #endif /* BACKPIPE */
983 jobflags
|= pp
->p_flags
;
984 pstatus
= (int) (pp
->p_flags
& PALLSTATES
);
985 if (pcond
&& linp
!= linbuf
&& !(flag
& FANCY
) &&
986 ((pstatus
== status
&& pp
->p_reason
== reason
) ||
990 if (pcond
&& linp
!= linbuf
)
994 pcond
= ((pp
== tp
&& !inpipe
) ||
995 (inpipe
&& pipetail
== tp
&& pp
== pipehead
));
998 #endif /* BACKPIPE */
1000 xprintf("[%d]%s %c ", pp
->p_index
,
1001 pp
->p_index
< 10 ? " " : "",
1002 pp
== pcurrent
? '+' :
1003 (pp
== pprevious
? '-' : ' '));
1008 xprintf("%5d ", pp
->p_procid
);
1010 xprintf("%11s ", sitename(pp
->p_procid
));
1013 if (flag
& (REASON
| AREASON
)) {
1018 if (pstatus
== status
) {
1019 if (pp
->p_reason
== reason
) {
1020 xprintf(format
, "");
1024 reason
= (int) pp
->p_reason
;
1028 reason
= (int) pp
->p_reason
;
1033 xprintf(format
, CGETS(17, 4, "Running "));
1040 * tell what happened to the background job
1041 * From: Michael Schroeder
1042 * <mlschroe@immd4.informatik.uni-erlangen.de>
1045 || ((flag
& AREASON
)
1047 && (reason
!= SIGPIPE
1048 || (pp
->p_flags
& PPOU
) == 0))) {
1053 ptr
= (char *)(intptr_t)mesg
[pp
->p_reason
& 0177].pname
;
1055 ptr
= xasprintf("%s %d", CGETS(17, 5, "Signal"),
1056 pp
->p_reason
& 0177);
1057 cleanup_push(ptr
, xfree
);
1060 xprintf(format
, ptr
);
1070 if (flag
& REASON
) {
1072 xprintf(CGETS(17, 6, "Exit %-25d"), pp
->p_reason
);
1074 xprintf(format
, CGETS(17, 7, "Done"));
1079 xprintf(CGETS(17, 8, "BUG: status=%-9o"),
1086 xprintf("%S", pp
->p_command
);
1087 if (pp
->p_flags
& PPOU
)
1089 if (pp
->p_flags
& PDIAG
)
1092 if (flag
& (REASON
| AREASON
) && pp
->p_flags
& PDUMPED
)
1093 xprintf("%s", CGETS(17, 9, " (core dumped)"));
1094 if (tp
== pp
->p_friends
) {
1095 if (flag
& AMPERSAND
)
1097 if (flag
& JOBDIR
&&
1098 !eq(tp
->p_cwd
->di_name
, dcwd
->di_name
)) {
1099 xprintf("%s", CGETS(17, 10, " (wd: "));
1100 dtildepr(tp
->p_cwd
->di_name
);
1104 if (pp
->p_flags
& PPTIME
&& !(status
& (PSTOPPED
| PRUNNING
))) {
1107 #if defined(BSDTIMES) || defined(_SEQUENT_)
1108 prusage(&zru
, &pp
->p_rusage
, &pp
->p_etime
,
1110 #else /* !BSDTIMES && !SEQUENT */
1111 lru
.tms_utime
= pp
->p_utime
;
1112 lru
.tms_stime
= pp
->p_stime
;
1115 prusage(&zru
, &lru
, pp
->p_etime
,
1117 #endif /* !BSDTIMES && !SEQUENT */
1121 pcond
= ((tp
== pp
->p_friends
&& !inpipe
) ||
1122 (inpipe
&& pipehead
->p_friends
== tp
&& pp
== pipetail
));
1123 #else /* !BACKPIPE */
1124 pcond
= (tp
== pp
->p_friends
);
1125 #endif /* BACKPIPE */
1129 if (flag
& SHELLDIR
&& !eq(tp
->p_cwd
->di_name
, dcwd
->di_name
)) {
1130 xprintf("%s", CGETS(17, 11, "(wd now: "));
1131 dtildepr(dcwd
->di_name
);
1138 * if pmaker == pipetail, we are finished that pipeline, and
1139 * can now skip to past the head
1141 if (pmarker
== pipetail
) {
1147 * set pp to one before the one we want next, so the while below
1148 * increments to the correct spot.
1152 while (pp
->p_friends
->p_friends
!= pmarker
);
1153 pmarker
= pp
->p_friends
;
1156 pcond
= ((pp
= pp
->p_friends
) != tp
|| inpipe
);
1157 #else /* !BACKPIPE */
1158 pcond
= ((pp
= pp
->p_friends
) != tp
);
1159 #endif /* BACKPIPE */
1162 if (jobflags
& PTIME
&& (jobflags
& (PSTOPPED
| PRUNNING
)) == 0) {
1163 if (jobflags
& NUMBER
)
1171 * All 4.3 BSD derived implementations are buggy and I've had enough.
1172 * The following implementation produces similar code and works in all
1173 * cases. The 4.3BSD one works only for <, >, !=
1176 # define timercmp(tvp, uvp, cmp) \
1177 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1178 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1179 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1182 ptprint(struct process
*tp
)
1185 struct timeval tetime
, diff
;
1186 static struct timeval ztime
;
1187 struct sysrusage 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 /* !BSDTIMES */
1201 timeval_t tetime
, diff
;
1202 static timeval_t ztime
;
1203 struct process_stats ru
;
1204 struct process
*pp
= tp
;
1209 ruadd(&ru
, &pp
->p_rusage
);
1210 tvsub(&diff
, &pp
->p_etime
, &pp
->p_btime
);
1211 if (timercmp(&diff
, &tetime
, >))
1213 } while ((pp
= pp
->p_friends
) != tp
);
1214 prusage(&zru
, &ru
, &tetime
, &ztime
);
1215 # else /* !_SEQUENT_ */
1217 static time_t ztime
= 0;
1218 static time_t zu_time
= 0;
1219 static time_t zs_time
= 0;
1220 time_t tetime
, diff
;
1221 time_t u_time
, s_time
;
1224 static clock_t ztime
= 0;
1225 static clock_t zu_time
= 0;
1226 static clock_t zs_time
= 0;
1227 clock_t tetime
, diff
;
1228 clock_t u_time
, s_time
;
1231 struct tms zts
, rts
;
1232 struct process
*pp
= tp
;
1238 u_time
+= pp
->p_utime
;
1239 s_time
+= pp
->p_stime
;
1240 diff
= pp
->p_etime
- pp
->p_btime
;
1243 } while ((pp
= pp
->p_friends
) != tp
);
1244 zts
.tms_utime
= zu_time
;
1245 zts
.tms_stime
= zs_time
;
1248 rts
.tms_utime
= u_time
;
1249 rts
.tms_stime
= s_time
;
1252 prusage(&zts
, &rts
, tetime
, ztime
);
1253 # endif /* !_SEQUENT_ */
1254 #endif /* !BSDTIMES */
1258 * dojobs - print all jobs
1262 dojobs(Char
**v
, struct command
*c
)
1265 int flag
= NUMBER
| NAME
| REASON
;
1272 if (v
[1] || !eq(*v
, STRml
))
1274 flag
|= FANCY
| JOBDIR
;
1276 for (i
= 1; i
<= pmaxindex
; i
++)
1277 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1278 if (pp
->p_index
== i
&& pp
->p_procid
== pp
->p_jobid
) {
1279 pp
->p_flags
&= ~PNEEDNOTE
;
1280 if (!(pprint(pp
, flag
) & (PRUNNING
| PSTOPPED
)))
1287 * dofg - builtin - put the job into the foreground
1291 dofg(Char
**v
, struct command
*c
)
1300 if (!pstart(pp
, 1)) {
1302 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1306 } while (*v
&& *++v
);
1310 * %... - builtin - put the job into the foreground
1314 dofg1(Char
**v
, struct command
*c
)
1321 if (!pstart(pp
, 1)) {
1323 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1330 * dobg - builtin - put the job into the background
1334 dobg(Char
**v
, struct command
*c
)
1343 if (!pstart(pp
, 0)) {
1345 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1347 } while (*v
&& *++v
);
1351 * %... & - builtin - put the job into the background
1355 dobg1(Char
**v
, struct command
*c
)
1361 if (!pstart(pp
, 0)) {
1363 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1368 * dostop - builtin - stop the job
1372 dostop(Char
**v
, struct command
*c
)
1376 pkill(++v
, SIGSTOP
);
1377 #endif /* BSDJOBS */
1381 * dokill - builtin - superset of kill (1)
1385 dokill(Char
**v
, struct command
*c
)
1387 int signum
, len
= 0;
1393 if (v
[0] && v
[0][0] == '-') {
1394 if (v
[0][1] == 'l') {
1395 for (signum
= 0; signum
<= nsig
; signum
++) {
1396 if ((name
= mesg
[signum
].iname
) != NULL
) {
1397 len
+= strlen(name
) + 1;
1398 if (len
>= TermH
- 1) {
1400 len
= strlen(name
) + 1;
1402 xprintf("%s ", name
);
1409 if (v
[0][1] == 's') {
1414 stderror(ERR_NAME
| ERR_TOOFEW
);
1417 if (Isdigit(*sigptr
)) {
1419 signum
= strtoul(short2str(sigptr
), &ep
, 0);
1420 if (*ep
|| signum
< 0 || signum
> (MAXSIG
-1))
1421 stderror(ERR_NAME
| ERR_BADSIG
);
1424 for (signum
= 0; signum
<= nsig
; signum
++)
1425 if (mesg
[signum
].iname
&&
1426 eq(sigptr
, str2short(mesg
[signum
].iname
)))
1428 setname(short2str(sigptr
));
1429 stderror(ERR_NAME
| ERR_UNKSIG
);
1440 pkill(Char
**v
, int signum
)
1442 struct process
*pp
, *np
;
1443 int jobflags
= 0, err1
= 0;
1445 Char
*cp
, **vp
, **globbed
;
1447 /* Avoid globbing %?x patterns */
1448 for (vp
= v
; vp
&& *vp
; vp
++)
1452 v
= glob_all_or_error(v
);
1454 cleanup_push(globbed
, blk_cleanup
);
1457 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1460 cleanup_push(&pintr_disabled
, disabled_cleanup
);
1463 while (v
&& (cp
= *v
)) {
1465 np
= pp
= pfind(cp
);
1467 jobflags
|= np
->p_flags
;
1468 while ((np
= np
->p_friends
) != pp
);
1476 if ((jobflags
& PRUNNING
) == 0) {
1478 xprintf(CGETS(17, 12, "%S: Already suspended\n"), cp
);
1479 # else /* !SUSPENDED */
1480 xprintf(CGETS(17, 13, "%S: Already stopped\n"), cp
);
1481 # endif /* !SUSPENDED */
1487 * suspend a process, kill -CONT %, then type jobs; the shell
1488 * says it is suspended, but it is running; thanks jaap..
1491 if (!pstart(pp
, 0)) {
1493 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
,
1500 #endif /* BSDJOBS */
1501 if (killpg(pp
->p_jobid
, signum
) < 0) {
1502 xprintf("%S: %s\n", cp
, strerror(errno
));
1506 if (signum
== SIGTERM
|| signum
== SIGHUP
)
1507 (void) killpg(pp
->p_jobid
, SIGCONT
);
1508 #endif /* BSDJOBS */
1510 else if (!(Isdigit(*cp
) || *cp
== '-'))
1511 stderror(ERR_NAME
| ERR_JOBARGS
);
1514 #ifndef WINNT_NATIVE
1515 pid
= strtol(short2str(cp
), &ep
, 10);
1517 pid
= strtoul(short2str(cp
), &ep
, 0);
1518 #endif /* WINNT_NATIVE */
1520 stderror(ERR_NAME
| ERR_JOBARGS
);
1521 else if (kill(pid
, signum
) < 0) {
1522 xprintf("%d: %s\n", pid
, strerror(errno
));
1527 if (signum
== SIGTERM
|| signum
== SIGHUP
)
1528 (void) kill(pid
, SIGCONT
);
1529 #endif /* BSDJOBS */
1534 cleanup_until(&pchild_disabled
);
1536 stderror(ERR_SILENT
);
1540 * pstart - start the job in foreground/background
1543 pstart(struct process
*pp
, int foregnd
)
1547 /* We don't use jobflags in this function right now (see below) */
1548 /* long jobflags = 0; */
1551 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1554 /* We don't use jobflags in this function right now (see below) */
1555 /* jobflags |= np->p_flags; */
1556 if (np
->p_flags
& (PRUNNING
| PSTOPPED
)) {
1557 np
->p_flags
|= PRUNNING
;
1558 np
->p_flags
&= ~PSTOPPED
;
1560 np
->p_flags
|= PFOREGND
;
1562 np
->p_flags
&= ~PFOREGND
;
1564 } while ((np
= np
->p_friends
) != pp
);
1567 (void) pprint(pp
, foregnd
? NAME
| JOBDIR
: NUMBER
| NAME
| AMPERSAND
);
1569 /* GrP run jobcmd hook if foregrounding */
1571 job_cmd(pp
->p_command
);
1576 rv
= tcsetpgrp(FSHTTY
, pp
->p_jobid
);
1579 * 1. child process of csh (shell script) receives SIGTTIN/SIGTTOU
1580 * 2. parent process (csh) receives SIGCHLD
1581 * 3. The "csh" signal handling function pchild() is invoked
1582 * with a SIGCHLD signal.
1583 * 4. pchild() calls wait3(WNOHANG) which returns 0.
1584 * The child process is NOT ready to be waited for at this time.
1585 * pchild() returns without picking-up the correct status
1586 * for the child process which generated the SIGCHLD.
1587 * 5. CONSEQUENCE : csh is UNaware that the process is stopped
1588 * 6. THIS LINE HAS BEEN COMMENTED OUT : if (jobflags&PSTOPPED)
1589 * (beto@aixwiz.austin.ibm.com - aug/03/91)
1590 * 7. I removed the line completely and added extra checks for
1591 * pstart, so that if a job gets attached to and dies inside
1592 * a debugger it does not confuse the shell. [christos]
1593 * 8. on the nec sx-4 there seems to be a problem, which requires
1594 * a syscall(151, getpid(), getpid()) in osinit. Don't ask me
1595 * what this is doing. [schott@rzg.mpg.de]
1599 rv
= killpg(pp
->p_jobid
, SIGCONT
);
1600 #endif /* BSDJOBS */
1601 cleanup_until(&pchild_disabled
);
1606 panystop(int neednl
)
1611 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1612 if (pp
->p_flags
& PSTOPPED
)
1613 stderror(ERR_STOPPED
, neednl
? "\n" : "");
1619 struct process
*pp
, *np
;
1621 if (cp
== 0 || cp
[1] == 0 || eq(cp
, STRcent2
) || eq(cp
, STRcentplus
)) {
1622 if (pcurrent
== NULL
)
1623 stderror(ERR_NAME
| ERR_JOBCUR
);
1626 if (eq(cp
, STRcentminus
) || eq(cp
, STRcenthash
)) {
1627 if (pprevious
== NULL
)
1628 stderror(ERR_NAME
| ERR_JOBPREV
);
1631 if (Isdigit(cp
[1])) {
1632 int idx
= atoi(short2str(cp
+ 1));
1634 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1635 if (pp
->p_index
== idx
&& pp
->p_procid
== pp
->p_jobid
)
1637 stderror(ERR_NAME
| ERR_NOSUCHJOB
);
1640 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1641 if (pp
->p_procid
== pp
->p_jobid
) {
1645 for (dp
= pp
->p_command
; *dp
; dp
++) {
1648 if (prefix(cp
+ 2, dp
))
1652 else if (prefix(cp
+ 1, pp
->p_command
)) {
1655 stderror(ERR_NAME
| ERR_AMBIG
);
1661 stderror(ERR_NAME
| (cp
[1] == '?' ? ERR_JOBPAT
: ERR_NOSUCHJOB
));
1668 * pgetcurr - find most recent job that is not pp, preferably stopped
1670 static struct process
*
1671 pgetcurr(struct process
*pp
)
1674 struct process
*xp
= NULL
;
1676 for (np
= proclist
.p_next
; np
; np
= np
->p_next
)
1677 if (np
!= pcurrent
&& np
!= pp
&& np
->p_procid
&&
1678 np
->p_procid
== np
->p_jobid
) {
1679 if (np
->p_flags
& PSTOPPED
)
1688 * donotify - flag the job so as to report termination asynchronously
1692 donotify(Char
**v
, struct command
*c
)
1698 pp
->p_flags
|= PNOTIFY
;
1703 synch_handler(int sno
)
1707 #endif /* SIGSYNCH */
1710 * Do the fork and whatever should be done in the child side that
1711 * should not be done if we are not forking at all (like for simple builtin's)
1712 * Also do everything that needs any signals fiddled with in the parent side
1714 * Wanttty tells whether process and/or tty pgrps are to be manipulated:
1715 * -1: leave tty alone; inherit pgrp from parent
1716 * 0: already have tty; manipulate process pgrps only
1717 * 1: want to claim tty; manipulate process and tty pgrps
1718 * It is usually just the value of tpgrp.
1722 pfork(struct command
*t
, int wanttty
)
1728 struct sigaction osa
, nsa
;
1729 #endif /* SIGSYNCH */
1732 * A child will be uninterruptible only under very special conditions.
1733 * Remember that the semantics of '&' is implemented by disconnecting the
1734 * process from the tty so signals do not need to ignored just for '&'.
1735 * Thus signals are set to default action for children unless: we have had
1736 * an "onintr -" (then specifically ignored) we are not playing with
1737 * signals (inherit action)
1740 ignint
= (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
))
1741 || (gointr
&& eq(gointr
, STRminus
));
1744 * Check for maximum nesting of 16 processes to avoid Forking loops
1747 stderror(ERR_NESTING
, 16);
1749 nsa
.sa_handler
= synch_handler
;
1750 sigfillset(&nsa
.sa_mask
);
1751 nsa
.sa_flags
= SA_RESTART
;
1752 if (sigaction(SIGSYNCH
, &nsa
, &osa
))
1753 stderror(ERR_SYSTEM
, "pfork: sigaction set", strerror(errno
));
1754 #endif /* SIGSYNCH */
1756 * Hold pchild() until we have the process installed in our table.
1760 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1762 while ((pid
= fork()) == -1)
1764 (void) sleep(FORKSLEEP
);
1766 stderror(ERR_NOPROC
);
1768 (void)cleanup_push_mark(); /* Never to be popped */
1769 pchild_disabled
= 0;
1771 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: getpid();
1774 #if !defined(BSDTIMES) && !defined(_SEQUENT_)
1776 #endif /* !defined(BSDTIMES) && !defined(_SEQUENT_) */
1779 setintr
= 0; /* until I think otherwise */
1781 * Children just get blown away on SIGINT, SIGQUIT unless "onintr
1784 (void) signal(SIGINT
, ignint
? SIG_IGN
: SIG_DFL
);
1785 (void) signal(SIGQUIT
, ignint
? SIG_IGN
: SIG_DFL
);
1788 /* make stoppable */
1789 (void) signal(SIGTSTP
, SIG_DFL
);
1790 (void) signal(SIGTTIN
, SIG_DFL
);
1791 (void) signal(SIGTTOU
, SIG_DFL
);
1793 #endif /* BSDJOBS */
1794 sigaction(SIGTERM
, &parterm
, NULL
);
1796 else if (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
)) {
1797 (void) signal(SIGINT
, SIG_IGN
);
1798 (void) signal(SIGQUIT
, SIG_IGN
);
1801 signal(SIGIO
, SIG_IGN
); /* ignore SIGIO in child too */
1804 pgetty(wanttty
, pgrp
);
1806 * Nohup and nice apply only to NODE_COMMAND's but it would be nice
1807 * (?!?) if you could say "nohup (foo;bar)" Then the parser would have
1808 * to know about nice/nohup/time
1810 if (t
->t_dflg
& F_NOHUP
)
1811 (void) signal(SIGHUP
, SIG_IGN
);
1812 if (t
->t_dflg
& F_NICE
) {
1813 int nval
= SIGN_EXTEND_CHAR(t
->t_nice
);
1814 #ifdef HAVE_SETPRIORITY
1815 if (setpriority(PRIO_PROCESS
, 0, nval
) == -1 && errno
)
1816 stderror(ERR_SYSTEM
, "setpriority", strerror(errno
));
1817 #else /* !HAVE_SETPRIORITY */
1819 #endif /* !HAVE_SETPRIORITY */
1822 if (t
->t_dflg
& F_VER
) {
1823 tsetenv(STRSYSTYPE
, t
->t_systype
? STRbsd43
: STRsys53
);
1828 /* rfw 8/89 now parent can continue */
1829 if (kill(getppid(), SIGSYNCH
))
1830 stderror(ERR_SYSTEM
, "pfork child: kill", strerror(errno
));
1831 #endif /* SIGSYNCH */
1838 * `Walking' process group fix from Beto Appleton.
1839 * (beto@aixwiz.austin.ibm.com)
1840 * If setpgid fails at this point that means that
1841 * our process leader has died. We flush the current
1842 * job and become the process leader ourselves.
1843 * The parent will figure that out later.
1845 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: pid
;
1846 if (setpgid(pid
, pgrp
) == -1 && errno
== EPERM
) {
1849 * We don't care if this causes an error here;
1850 * then we are already in the right process group
1852 (void) setpgid(pid
, pgrp
= pid
);
1855 #endif /* POSIXJOBS */
1859 sigset_t pause_mask
;
1862 * rfw 8/89 Wait for child to own terminal. Solves half of ugly
1863 * synchronization problem. With this change, we know that the only
1864 * reason setpgrp to a previous process in a pipeline can fail is that
1865 * the previous process has already exited. Without this hack, he may
1866 * either have exited or not yet started to run. Two uglies become
1869 sigprocmask(SIG_BLOCK
, NULL
, &pause
);
1870 sigdelset(&pause_mask
, SIGCHLD
);
1871 sigdelset(&pause_mask
, SIGSYNCH
);
1872 sigsuspend(&pause_mask
);
1873 handle_pending_signals();
1874 if (sigaction(SIGSYNCH
, &osa
, NULL
))
1875 stderror(ERR_SYSTEM
, "pfork parent: sigaction restore",
1878 #endif /* SIGSYNCH */
1881 cleanup_until(&pchild_disabled
);
1890 stderror(ERR_JOBCONTROL
);
1892 stderror(ERR_JOBCTRLSUB
);
1897 setttypgrp(int pgrp
)
1900 * If we are piping out a builtin, eg. 'echo | more' things can go
1901 * out of sequence, i.e. the more can run before the echo. This
1902 * can happen even if we have vfork, since the echo will be forked
1903 * with the regular fork. In this case, we need to set the tty
1904 * pgrp ourselves. If that happens, then the process will be still
1905 * alive. And the tty process group will already be set.
1906 * This should fix the famous sequent problem as a side effect:
1907 * The controlling terminal is lost if all processes in the
1908 * terminal process group are zombies. In this case tcgetpgrp()
1909 * returns 0. If this happens we must set the terminal process
1912 if (tcgetpgrp(FSHTTY
) != pgrp
) {
1914 struct sigaction old
;
1917 * tcsetpgrp will set SIGTTOU to all the the processes in
1918 * the background according to POSIX... We ignore this here.
1920 sigaction(SIGTTOU
, NULL
, &old
);
1921 signal(SIGTTOU
, SIG_IGN
);
1923 (void) tcsetpgrp(FSHTTY
, pgrp
);
1925 sigaction(SIGTTOU
, &old
, NULL
);
1933 * if we don't have vfork(), things can still go in the wrong order
1934 * resulting in the famous 'Stopped (tty output)'. But some systems
1935 * don't permit the setpgid() call, (these are more recent secure
1936 * systems such as ibm's aix), when they do. Then we'd rather print
1937 * an error message than hang the shell!
1938 * I am open to suggestions how to fix that.
1941 pgetty(int wanttty
, pid_t pgrp
)
1946 # endif /* POSIXJOBS */
1948 jobdebug_xprintf(("wanttty %d pid %d opgrp%d pgrp %d tpgrp %d\n",
1949 wanttty
, (int)getpid(), (int)pgrp
, (int)mygetpgrp(),
1950 (int)tcgetpgrp(FSHTTY
)));
1953 * christos: I am blocking the tty signals till I've set things
1958 sigaddset(&set
, SIGTSTP
);
1959 sigaddset(&set
, SIGTTIN
);
1960 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
1961 cleanup_push(&oset
, sigprocmask_cleanup
);
1963 # endif /* POSIXJOBS */
1968 # endif /* !POSIXJOBS */
1971 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
1972 * Don't check for tpgrp >= 0 so even non-interactive shells give
1973 * background jobs process groups Same for the comparison in the other part
1977 if (setpgid(0, pgrp
) == -1) {
1979 /* Walking process group fix; see above */
1980 if (setpgid(0, pgrp
= getpid()) == -1) {
1981 # endif /* POSIXJOBS */
1982 stderror(ERR_SYSTEM
, "setpgid child:\n", strerror(errno
));
1986 wanttty
= pgrp
; /* Now we really want the tty, since we became the
1987 * the process group leader
1989 # endif /* POSIXJOBS */
1996 cleanup_until(&oset
);
1998 # endif /* POSIXJOBS */
2000 jobdebug_xprintf(("wanttty %d pid %d pgrp %d tpgrp %d\n",
2001 wanttty
, getpid(), mygetpgrp(), tcgetpgrp(FSHTTY
)));
2004 tpgrp
= 0; /* gave tty away */
2005 #endif /* BSDJOBS */