1 /* $Header: /p/tcsh/cvsroot/tcsh/sh.proc.c,v 3.121 2012/01/25 15:34:41 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.121 2012/01/25 15:34:41 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
);
511 (void)handle_pending_signals();
514 jobflags
|= fp
->p_flags
;
515 while ((fp
= (fp
->p_friends
)) != pp
);
516 if ((jobflags
& PRUNNING
) == 0)
518 jobdebug_xprintf(("%d starting to sigsuspend for SIGCHLD on %d\n",
519 getpid(), fp
->p_procid
));
520 sigsuspend(&pause_mask
);
522 cleanup_until(&oset
);
523 jobdebug_xprintf(("%d returned from sigsuspend loop\n", getpid()));
525 if (tpgrp
> 0) /* get tty back */
526 (void) tcsetpgrp(FSHTTY
, tpgrp
);
528 if ((jobflags
& (PSIGNALED
| PSTOPPED
| PTIME
)) ||
529 fp
->p_cwd
== NULL
|| !eq(dcwd
->di_name
, fp
->p_cwd
->di_name
)) {
530 if (jobflags
& PSTOPPED
) {
532 if (adrof(STRlistjobs
)) {
535 jobcommand
[0] = STRjobs
;
536 if (eq(varval(STRlistjobs
), STRlong
))
537 jobcommand
[1] = STRml
;
539 jobcommand
[1] = NULL
;
540 jobcommand
[2] = NULL
;
542 dojobs(jobcommand
, NULL
);
543 (void) pprint(pp
, SHELLDIR
);
546 (void) pprint(pp
, AREASON
| SHELLDIR
);
549 (void) pprint(pp
, AREASON
| SHELLDIR
);
551 if ((jobflags
& (PINTERRUPTED
| PSTOPPED
)) && setintr
&&
552 (!gointr
|| !eq(gointr
, STRminus
))) {
553 if ((jobflags
& PSTOPPED
) == 0)
561 /* In case of pipelines only the result of the last
562 * command should be taken in account */
563 if (!anyerror
&& !(fp
->p_flags
& PBRACE
)
564 && ((fp
->p_flags
& PPOU
) || (fp
->p_flags
& PBACKQ
)))
567 reason
= fp
->p_flags
& (PSIGNALED
| PINTERRUPTED
) ?
568 fp
->p_reason
| META
: fp
->p_reason
;
569 } while ((fp
= fp
->p_friends
) != pp
);
571 * Don't report on backquoted jobs, cause it will mess up
574 if ((reason
!= 0) && (adrof(STRprintexitvalue
)) &&
575 (pp
->p_flags
& PBACKQ
) == 0)
576 xprintf(CGETS(17, 2, "Exit %d\n"), reason
);
577 reason_str
= putn((tcsh_number_t
)reason
);
578 cleanup_push(reason_str
, xfree
);
579 setv(STRstatus
, reason_str
, VAR_READWRITE
);
580 cleanup_ignore(reason_str
);
581 cleanup_until(reason_str
);
582 if (reason
&& exiterr
)
588 * dowait - wait for all processes to finish
593 dowait(Char
**v
, struct command
*c
)
597 int opintr_disabled
, gotsig
;
602 sigprocmask(SIG_BLOCK
, NULL
, &pause_mask
);
603 sigdelset(&pause_mask
, SIGCHLD
);
605 sigdelset(&pause_mask
, SIGINT
);
607 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
608 if (pp
->p_procid
&& /* pp->p_procid == pp->p_jobid && */
609 pp
->p_flags
& PRUNNING
) {
610 (void)handle_pending_signals();
611 sigsuspend(&pause_mask
);
612 opintr_disabled
= pintr_disabled
;
614 gotsig
= handle_pending_signals();
615 pintr_disabled
= opintr_disabled
;
624 * pflushall - flush all jobs from list (e.g. at fork())
631 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
)
637 * pflush - flag all process structures in the same job as the
638 * the argument process for deletion. The actual free of the
639 * space is not done here since pflush is called at interrupt level.
642 pflush(struct process
*pp
)
647 if (pp
->p_procid
== 0) {
648 xprintf("%s", CGETS(17, 3, "BUG: process flushed twice"));
651 while (pp
->p_procid
!= pp
->p_jobid
)
659 np
->p_index
= np
->p_procid
= 0;
660 np
->p_flags
&= ~PNEEDNOTE
;
661 } while ((np
= np
->p_friends
) != pp
);
662 if (idx
== pmaxindex
) {
663 for (np
= proclist
.p_next
, idx
= 0; np
; np
= np
->p_next
)
664 if (np
->p_index
> idx
)
671 * pclrcurr - make sure the given job is not the current or previous job;
672 * pp MUST be the job leader
675 pclrcurr(struct process
*pp
)
677 if (pp
== pcurrent
) {
678 if (pprevious
!= NULL
) {
679 pcurrent
= pprevious
;
680 pprevious
= pgetcurr(pp
);
683 pcurrent
= pgetcurr(pp
);
684 pprevious
= pgetcurr(pp
);
687 else if (pp
== pprevious
)
688 pprevious
= pgetcurr(pp
);
691 /* +4 here is 1 for '\0', 1 ea for << >& >> */
693 static size_t cmdmax
;
694 static size_t cmdlen
;
696 #define CMD_INIT 1024
700 morecommand(size_t s
)
706 ncmdstr
= xrealloc(cmdstr
, cmdmax
* sizeof(*cmdstr
));
707 d
= ncmdstr
- cmdstr
;
713 * unparse - Export padd() functionality
716 unparse(struct command
*t
)
719 morecommand(CMD_INIT
);
724 return Strsave(cmdstr
);
729 * palloc - allocate a process structure and fill it up.
730 * an important assumption is made that the process is running.
733 palloc(pid_t pid
, struct command
*t
)
738 pp
= xcalloc(1, sizeof(struct process
));
740 pp
->p_parentid
= shpgrp
;
741 pp
->p_flags
= ((t
->t_dflg
& F_AMPERSAND
) ? 0 : PFOREGND
) | PRUNNING
;
742 if (t
->t_dflg
& F_TIME
)
743 pp
->p_flags
|= PPTIME
;
744 if (t
->t_dflg
& F_BACKQ
)
745 pp
->p_flags
|= PBACKQ
;
746 if (t
->t_dflg
& F_HUP
)
748 if (t
->t_dcom
&& t
->t_dcom
[0] && (*t
->t_dcom
[0] == '{'))
749 pp
->p_flags
|= PBRACE
;
751 morecommand(CMD_INIT
);
756 if (t
->t_dflg
& F_PIPEOUT
) {
758 if (t
->t_dflg
& F_STDERR
)
759 pp
->p_flags
|= PDIAG
;
761 pp
->p_command
= Strsave(cmdstr
);
765 /* careful here with interrupt level */
767 pp
->p_index
= pcurrjob
->p_index
;
768 pp
->p_friends
= pcurrjob
;
769 pp
->p_jobid
= pcurrjob
->p_procid
;
770 for (fp
= pcurrjob
; fp
->p_friends
!= pcurrjob
; fp
= fp
->p_friends
)
780 if (pmaxindex
< BIGINDEX
)
781 pp
->p_index
= ++pmaxindex
;
786 for (np
= proclist
.p_next
; np
; np
= np
->p_next
)
787 if (np
->p_index
== i
)
796 if (pcurrent
== NULL
)
798 else if (pprevious
== NULL
)
801 pp
->p_next
= proclist
.p_next
;
802 proclist
.p_next
= pp
;
804 (void) gettimeofday(&pp
->p_btime
, NULL
);
805 #else /* !BSDTIMES */
807 (void) get_process_stats(&pp
->p_btime
, PS_SELF
, NULL
, NULL
);
808 # else /* !_SEQUENT_ */
812 pp
->p_btime
= times(&tmptimes
);
814 # endif /* !_SEQUENT_ */
815 #endif /* !BSDTIMES */
819 padd(struct command
*t
)
834 for (argp
= t
->t_dcom
; *argp
; argp
++) {
868 if ((t
->t_dflg
& F_PIPEIN
) == 0 && t
->t_dlef
) {
869 pads((t
->t_dflg
& F_READ
) ? STRspLarrow2sp
: STRspLarrowsp
);
872 if ((t
->t_dflg
& F_PIPEOUT
) == 0 && t
->t_drit
) {
873 pads((t
->t_dflg
& F_APPEND
) ? STRspRarrow2
: STRspRarrow
);
874 if (t
->t_dflg
& F_STDERR
)
887 * Avoid the Quoted Space alias hack! Reported by:
888 * sam@john-bigboote.ICS.UCI.EDU (Sam Horrocks)
890 if (cp
[0] == STRQNULL
[0])
895 len
= cmdlen
+ i
+ CMD_INCR
;
898 (void) Strcpy(cmdp
, cp
);
904 * psavejob - temporarily save the current job on a one level stack
905 * so another job can be created. Used for { } in exp6
906 * and `` in globbing.
916 psavejob_cleanup(void *dummy
)
924 * pendjob - indicate that a job (set of commands) has been completed
925 * or is about to begin.
930 struct process
*pp
, *tp
;
932 if (pcurrjob
&& (pcurrjob
->p_flags
& (PFOREGND
| PSTOPPED
)) == 0) {
935 while (pp
->p_procid
!= pp
->p_jobid
)
937 xprintf("[%d]", pp
->p_index
);
940 xprintf(" %d", pp
->p_procid
);
945 pholdjob
= pcurrjob
= 0;
949 * pprint - print a job
953 * Hacks have been added for SVR4 to deal with pipe's being spawned in
956 * David Dawes (dawes@physics.su.oz.au) Oct 1991
960 pprint(struct process
*pp
, int flag
)
964 int jobflags
, pstatus
, pcond
;
968 struct process
*pipehead
= NULL
, *pipetail
= NULL
, *pmarker
= NULL
;
970 #endif /* BACKPIPE */
972 while (pp
->p_procid
!= pp
->p_jobid
)
974 if (pp
== pp
->p_friends
&& (pp
->p_flags
& PPTIME
)) {
975 pp
->p_flags
&= ~PPTIME
;
976 pp
->p_flags
|= PTIME
;
979 status
= reason
= -1;
984 * The pipeline is reversed, so locate the real head of the pipeline
985 * if pp is at the tail of a pipe (and not already in a pipeline)
987 if ((pp
->p_friends
->p_flags
& PPOU
) && !inpipe
&& (flag
& NAME
)) {
992 while (pp
->p_friends
->p_flags
& PPOU
);
996 * pmarker is used to hold the place of the proc being processed, so
997 * we can search for the next one downstream later.
1000 pcond
= (tp
!= pp
|| (inpipe
&& tp
== pp
));
1001 #else /* !BACKPIPE */
1003 #endif /* BACKPIPE */
1005 jobflags
|= pp
->p_flags
;
1006 pstatus
= (int) (pp
->p_flags
& PALLSTATES
);
1007 if (pcond
&& linp
!= linbuf
&& !(flag
& FANCY
) &&
1008 ((pstatus
== status
&& pp
->p_reason
== reason
) ||
1012 if (pcond
&& linp
!= linbuf
)
1014 if (flag
& NUMBER
) {
1016 pcond
= ((pp
== tp
&& !inpipe
) ||
1017 (inpipe
&& pipetail
== tp
&& pp
== pipehead
));
1018 #else /* BACKPIPE */
1020 #endif /* BACKPIPE */
1022 xprintf("[%d]%s %c ", pp
->p_index
,
1023 pp
->p_index
< 10 ? " " : "",
1024 pp
== pcurrent
? '+' :
1025 (pp
== pprevious
? '-' : ' '));
1030 xprintf("%5d ", pp
->p_procid
);
1032 xprintf("%11s ", sitename(pp
->p_procid
));
1035 if (flag
& (REASON
| AREASON
)) {
1040 if (pstatus
== status
) {
1041 if (pp
->p_reason
== reason
) {
1042 xprintf(format
, "");
1046 reason
= (int) pp
->p_reason
;
1050 reason
= (int) pp
->p_reason
;
1055 xprintf(format
, CGETS(17, 4, "Running "));
1062 * tell what happened to the background job
1063 * From: Michael Schroeder
1064 * <mlschroe@immd4.informatik.uni-erlangen.de>
1067 || ((flag
& AREASON
)
1069 && (reason
!= SIGPIPE
1070 || (pp
->p_flags
& PPOU
) == 0))) {
1075 ptr
= (char *)(intptr_t)mesg
[pp
->p_reason
& 0177].pname
;
1077 ptr
= xasprintf("%s %d", CGETS(17, 5, "Signal"),
1078 pp
->p_reason
& 0177);
1079 cleanup_push(ptr
, xfree
);
1082 xprintf(format
, ptr
);
1092 if (flag
& REASON
) {
1094 xprintf(CGETS(17, 6, "Exit %-25d"), pp
->p_reason
);
1096 xprintf(format
, CGETS(17, 7, "Done"));
1101 xprintf(CGETS(17, 8, "BUG: status=%-9o"),
1108 xprintf("%S", pp
->p_command
);
1109 if (pp
->p_flags
& PPOU
)
1111 if (pp
->p_flags
& PDIAG
)
1114 if (flag
& (REASON
| AREASON
) && pp
->p_flags
& PDUMPED
)
1115 xprintf("%s", CGETS(17, 9, " (core dumped)"));
1116 if (tp
== pp
->p_friends
) {
1117 if (flag
& AMPERSAND
)
1119 if (flag
& JOBDIR
&&
1120 !eq(tp
->p_cwd
->di_name
, dcwd
->di_name
)) {
1121 xprintf("%s", CGETS(17, 10, " (wd: "));
1122 dtildepr(tp
->p_cwd
->di_name
);
1126 if (pp
->p_flags
& PPTIME
&& !(status
& (PSTOPPED
| PRUNNING
))) {
1129 #if defined(BSDTIMES) || defined(_SEQUENT_)
1130 prusage(&zru
, &pp
->p_rusage
, &pp
->p_etime
,
1132 #else /* !BSDTIMES && !SEQUENT */
1133 lru
.tms_utime
= pp
->p_utime
;
1134 lru
.tms_stime
= pp
->p_stime
;
1137 prusage(&zru
, &lru
, pp
->p_etime
,
1139 #endif /* !BSDTIMES && !SEQUENT */
1143 pcond
= ((tp
== pp
->p_friends
&& !inpipe
) ||
1144 (inpipe
&& pipehead
->p_friends
== tp
&& pp
== pipetail
));
1145 #else /* !BACKPIPE */
1146 pcond
= (tp
== pp
->p_friends
);
1147 #endif /* BACKPIPE */
1151 if (flag
& SHELLDIR
&& !eq(tp
->p_cwd
->di_name
, dcwd
->di_name
)) {
1152 xprintf("%s", CGETS(17, 11, "(wd now: "));
1153 dtildepr(dcwd
->di_name
);
1160 * if pmaker == pipetail, we are finished that pipeline, and
1161 * can now skip to past the head
1163 if (pmarker
== pipetail
) {
1169 * set pp to one before the one we want next, so the while below
1170 * increments to the correct spot.
1174 while (pp
->p_friends
->p_friends
!= pmarker
);
1175 pmarker
= pp
->p_friends
;
1178 pcond
= ((pp
= pp
->p_friends
) != tp
|| inpipe
);
1179 #else /* !BACKPIPE */
1180 pcond
= ((pp
= pp
->p_friends
) != tp
);
1181 #endif /* BACKPIPE */
1184 if (jobflags
& PTIME
&& (jobflags
& (PSTOPPED
| PRUNNING
)) == 0) {
1185 if (jobflags
& NUMBER
)
1193 * All 4.3 BSD derived implementations are buggy and I've had enough.
1194 * The following implementation produces similar code and works in all
1195 * cases. The 4.3BSD one works only for <, >, !=
1198 # define timercmp(tvp, uvp, cmp) \
1199 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1200 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1201 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1204 ptprint(struct process
*tp
)
1207 struct timeval tetime
, diff
;
1208 static struct timeval ztime
;
1209 struct sysrusage ru
;
1210 struct process
*pp
= tp
;
1215 ruadd(&ru
, &pp
->p_rusage
);
1216 tvsub(&diff
, &pp
->p_etime
, &pp
->p_btime
);
1217 if (timercmp(&diff
, &tetime
, >))
1219 } while ((pp
= pp
->p_friends
) != tp
);
1220 prusage(&zru
, &ru
, &tetime
, &ztime
);
1221 #else /* !BSDTIMES */
1223 timeval_t tetime
, diff
;
1224 static timeval_t ztime
;
1225 struct process_stats ru
;
1226 struct process
*pp
= tp
;
1231 ruadd(&ru
, &pp
->p_rusage
);
1232 tvsub(&diff
, &pp
->p_etime
, &pp
->p_btime
);
1233 if (timercmp(&diff
, &tetime
, >))
1235 } while ((pp
= pp
->p_friends
) != tp
);
1236 prusage(&zru
, &ru
, &tetime
, &ztime
);
1237 # else /* !_SEQUENT_ */
1239 static time_t ztime
= 0;
1240 static time_t zu_time
= 0;
1241 static time_t zs_time
= 0;
1242 time_t tetime
, diff
;
1243 time_t u_time
, s_time
;
1246 static clock_t ztime
= 0;
1247 static clock_t zu_time
= 0;
1248 static clock_t zs_time
= 0;
1249 clock_t tetime
, diff
;
1250 clock_t u_time
, s_time
;
1253 struct tms zts
, rts
;
1254 struct process
*pp
= tp
;
1260 u_time
+= pp
->p_utime
;
1261 s_time
+= pp
->p_stime
;
1262 diff
= pp
->p_etime
- pp
->p_btime
;
1265 } while ((pp
= pp
->p_friends
) != tp
);
1266 zts
.tms_utime
= zu_time
;
1267 zts
.tms_stime
= zs_time
;
1270 rts
.tms_utime
= u_time
;
1271 rts
.tms_stime
= s_time
;
1274 prusage(&zts
, &rts
, tetime
, ztime
);
1275 # endif /* !_SEQUENT_ */
1276 #endif /* !BSDTIMES */
1280 * dojobs - print all jobs
1284 dojobs(Char
**v
, struct command
*c
)
1287 int flag
= NUMBER
| NAME
| REASON
;
1294 if (v
[1] || !eq(*v
, STRml
))
1296 flag
|= FANCY
| JOBDIR
;
1298 for (i
= 1; i
<= pmaxindex
; i
++)
1299 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1300 if (pp
->p_index
== i
&& pp
->p_procid
== pp
->p_jobid
) {
1301 pp
->p_flags
&= ~PNEEDNOTE
;
1302 if (!(pprint(pp
, flag
) & (PRUNNING
| PSTOPPED
)))
1309 * dofg - builtin - put the job into the foreground
1313 dofg(Char
**v
, struct command
*c
)
1322 if (!pstart(pp
, 1)) {
1324 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1328 } while (*v
&& *++v
);
1332 * %... - builtin - put the job into the foreground
1336 dofg1(Char
**v
, struct command
*c
)
1343 if (!pstart(pp
, 1)) {
1345 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1352 * dobg - builtin - put the job into the background
1356 dobg(Char
**v
, struct command
*c
)
1365 if (!pstart(pp
, 0)) {
1367 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1369 } while (*v
&& *++v
);
1373 * %... & - builtin - put the job into the background
1377 dobg1(Char
**v
, struct command
*c
)
1383 if (!pstart(pp
, 0)) {
1385 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1390 * dostop - builtin - stop the job
1394 dostop(Char
**v
, struct command
*c
)
1398 pkill(++v
, SIGSTOP
);
1399 #endif /* BSDJOBS */
1403 * dokill - builtin - superset of kill (1)
1407 dokill(Char
**v
, struct command
*c
)
1409 int signum
, len
= 0;
1415 if (v
[0] && v
[0][0] == '-') {
1416 if (v
[0][1] == 'l') {
1417 for (signum
= 0; signum
<= nsig
; signum
++) {
1418 if ((name
= mesg
[signum
].iname
) != NULL
) {
1419 len
+= strlen(name
) + 1;
1420 if (len
>= TermH
- 1) {
1422 len
= strlen(name
) + 1;
1424 xprintf("%s ", name
);
1431 if (v
[0][1] == 's') {
1436 stderror(ERR_NAME
| ERR_TOOFEW
);
1439 if (Isdigit(*sigptr
)) {
1441 signum
= strtoul(short2str(sigptr
), &ep
, 0);
1442 if (*ep
|| signum
< 0 || signum
> (MAXSIG
-1))
1443 stderror(ERR_NAME
| ERR_BADSIG
);
1446 for (signum
= 0; signum
<= nsig
; signum
++)
1447 if (mesg
[signum
].iname
&&
1448 eq(sigptr
, str2short(mesg
[signum
].iname
)))
1450 setname(short2str(sigptr
));
1451 stderror(ERR_NAME
| ERR_UNKSIG
);
1462 pkill(Char
**v
, int signum
)
1464 struct process
*pp
, *np
;
1465 int jobflags
= 0, err1
= 0;
1467 Char
*cp
, **vp
, **globbed
;
1469 /* Avoid globbing %?x patterns */
1470 for (vp
= v
; vp
&& *vp
; vp
++)
1474 v
= glob_all_or_error(v
);
1476 cleanup_push(globbed
, blk_cleanup
);
1479 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1482 cleanup_push(&pintr_disabled
, disabled_cleanup
);
1485 while (v
&& (cp
= *v
)) {
1487 np
= pp
= pfind(cp
);
1489 jobflags
|= np
->p_flags
;
1490 while ((np
= np
->p_friends
) != pp
);
1498 if ((jobflags
& PRUNNING
) == 0) {
1500 xprintf(CGETS(17, 12, "%S: Already suspended\n"), cp
);
1501 # else /* !SUSPENDED */
1502 xprintf(CGETS(17, 13, "%S: Already stopped\n"), cp
);
1503 # endif /* !SUSPENDED */
1509 * suspend a process, kill -CONT %, then type jobs; the shell
1510 * says it is suspended, but it is running; thanks jaap..
1513 if (!pstart(pp
, 0)) {
1515 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
,
1522 #endif /* BSDJOBS */
1523 if (killpg(pp
->p_jobid
, signum
) < 0) {
1524 xprintf("%S: %s\n", cp
, strerror(errno
));
1528 if (signum
== SIGTERM
|| signum
== SIGHUP
)
1529 (void) killpg(pp
->p_jobid
, SIGCONT
);
1530 #endif /* BSDJOBS */
1532 else if (!(Isdigit(*cp
) || *cp
== '-'))
1533 stderror(ERR_NAME
| ERR_JOBARGS
);
1536 #ifndef WINNT_NATIVE
1537 pid
= strtol(short2str(cp
), &ep
, 10);
1539 pid
= strtoul(short2str(cp
), &ep
, 0);
1540 #endif /* WINNT_NATIVE */
1542 stderror(ERR_NAME
| ERR_JOBARGS
);
1543 else if (kill(pid
, signum
) < 0) {
1544 xprintf("%d: %s\n", pid
, strerror(errno
));
1549 if (signum
== SIGTERM
|| signum
== SIGHUP
)
1550 (void) kill(pid
, SIGCONT
);
1551 #endif /* BSDJOBS */
1556 cleanup_until(&pchild_disabled
);
1558 stderror(ERR_SILENT
);
1562 * pstart - start the job in foreground/background
1565 pstart(struct process
*pp
, int foregnd
)
1569 /* We don't use jobflags in this function right now (see below) */
1570 /* long jobflags = 0; */
1573 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1576 /* We don't use jobflags in this function right now (see below) */
1577 /* jobflags |= np->p_flags; */
1578 if (np
->p_flags
& (PRUNNING
| PSTOPPED
)) {
1579 np
->p_flags
|= PRUNNING
;
1580 np
->p_flags
&= ~PSTOPPED
;
1582 np
->p_flags
|= PFOREGND
;
1584 np
->p_flags
&= ~PFOREGND
;
1586 } while ((np
= np
->p_friends
) != pp
);
1589 (void) pprint(pp
, foregnd
? NAME
| JOBDIR
: NUMBER
| NAME
| AMPERSAND
);
1591 /* GrP run jobcmd hook if foregrounding */
1593 job_cmd(pp
->p_command
);
1598 rv
= tcsetpgrp(FSHTTY
, pp
->p_jobid
);
1601 * 1. child process of csh (shell script) receives SIGTTIN/SIGTTOU
1602 * 2. parent process (csh) receives SIGCHLD
1603 * 3. The "csh" signal handling function pchild() is invoked
1604 * with a SIGCHLD signal.
1605 * 4. pchild() calls wait3(WNOHANG) which returns 0.
1606 * The child process is NOT ready to be waited for at this time.
1607 * pchild() returns without picking-up the correct status
1608 * for the child process which generated the SIGCHLD.
1609 * 5. CONSEQUENCE : csh is UNaware that the process is stopped
1610 * 6. THIS LINE HAS BEEN COMMENTED OUT : if (jobflags&PSTOPPED)
1611 * (beto@aixwiz.austin.ibm.com - aug/03/91)
1612 * 7. I removed the line completely and added extra checks for
1613 * pstart, so that if a job gets attached to and dies inside
1614 * a debugger it does not confuse the shell. [christos]
1615 * 8. on the nec sx-4 there seems to be a problem, which requires
1616 * a syscall(151, getpid(), getpid()) in osinit. Don't ask me
1617 * what this is doing. [schott@rzg.mpg.de]
1621 rv
= killpg(pp
->p_jobid
, SIGCONT
);
1622 #endif /* BSDJOBS */
1623 cleanup_until(&pchild_disabled
);
1628 panystop(int neednl
)
1633 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1634 if (pp
->p_flags
& PSTOPPED
)
1635 stderror(ERR_STOPPED
, neednl
? "\n" : "");
1641 struct process
*pp
, *np
;
1643 if (cp
== 0 || cp
[1] == 0 || eq(cp
, STRcent2
) || eq(cp
, STRcentplus
)) {
1644 if (pcurrent
== NULL
)
1645 stderror(ERR_NAME
| ERR_JOBCUR
);
1648 if (eq(cp
, STRcentminus
) || eq(cp
, STRcenthash
)) {
1649 if (pprevious
== NULL
)
1650 stderror(ERR_NAME
| ERR_JOBPREV
);
1653 if (Isdigit(cp
[1])) {
1654 int idx
= atoi(short2str(cp
+ 1));
1656 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1657 if (pp
->p_index
== idx
&& pp
->p_procid
== pp
->p_jobid
)
1659 stderror(ERR_NAME
| ERR_NOSUCHJOB
);
1662 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1663 if (pp
->p_procid
== pp
->p_jobid
) {
1667 for (dp
= pp
->p_command
; *dp
; dp
++) {
1670 if (prefix(cp
+ 2, dp
))
1674 else if (prefix(cp
+ 1, pp
->p_command
)) {
1677 stderror(ERR_NAME
| ERR_AMBIG
);
1683 stderror(ERR_NAME
| (cp
[1] == '?' ? ERR_JOBPAT
: ERR_NOSUCHJOB
));
1690 * pgetcurr - find most recent job that is not pp, preferably stopped
1692 static struct process
*
1693 pgetcurr(struct process
*pp
)
1696 struct process
*xp
= NULL
;
1698 for (np
= proclist
.p_next
; np
; np
= np
->p_next
)
1699 if (np
!= pcurrent
&& np
!= pp
&& np
->p_procid
&&
1700 np
->p_procid
== np
->p_jobid
) {
1701 if (np
->p_flags
& PSTOPPED
)
1710 * donotify - flag the job so as to report termination asynchronously
1714 donotify(Char
**v
, struct command
*c
)
1720 pp
->p_flags
|= PNOTIFY
;
1725 synch_handler(int sno
)
1729 #endif /* SIGSYNCH */
1732 * Do the fork and whatever should be done in the child side that
1733 * should not be done if we are not forking at all (like for simple builtin's)
1734 * Also do everything that needs any signals fiddled with in the parent side
1736 * Wanttty tells whether process and/or tty pgrps are to be manipulated:
1737 * -1: leave tty alone; inherit pgrp from parent
1738 * 0: already have tty; manipulate process pgrps only
1739 * 1: want to claim tty; manipulate process and tty pgrps
1740 * It is usually just the value of tpgrp.
1744 pfork(struct command
*t
, int wanttty
)
1750 struct sigaction osa
, nsa
;
1751 #endif /* SIGSYNCH */
1754 * A child will be uninterruptible only under very special conditions.
1755 * Remember that the semantics of '&' is implemented by disconnecting the
1756 * process from the tty so signals do not need to ignored just for '&'.
1757 * Thus signals are set to default action for children unless: we have had
1758 * an "onintr -" (then specifically ignored) we are not playing with
1759 * signals (inherit action)
1762 ignint
= (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
))
1763 || (gointr
&& eq(gointr
, STRminus
));
1766 * Check for maximum nesting of 16 processes to avoid Forking loops
1769 stderror(ERR_NESTING
, 16);
1771 nsa
.sa_handler
= synch_handler
;
1772 sigfillset(&nsa
.sa_mask
);
1773 nsa
.sa_flags
= SA_RESTART
;
1774 if (sigaction(SIGSYNCH
, &nsa
, &osa
))
1775 stderror(ERR_SYSTEM
, "pfork: sigaction set", strerror(errno
));
1776 #endif /* SIGSYNCH */
1778 * Hold pchild() until we have the process installed in our table.
1782 cleanup_push(&pchild_disabled
, disabled_cleanup
);
1784 while ((pid
= fork()) == -1)
1786 (void) sleep(FORKSLEEP
);
1788 stderror(ERR_NOPROC
);
1790 (void)cleanup_push_mark(); /* Never to be popped */
1791 pchild_disabled
= 0;
1793 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: getpid();
1796 #if !defined(BSDTIMES) && !defined(_SEQUENT_)
1798 #endif /* !defined(BSDTIMES) && !defined(_SEQUENT_) */
1801 setintr
= 0; /* until I think otherwise */
1803 * Children just get blown away on SIGINT, SIGQUIT unless "onintr
1806 (void) signal(SIGINT
, ignint
? SIG_IGN
: SIG_DFL
);
1807 (void) signal(SIGQUIT
, ignint
? SIG_IGN
: SIG_DFL
);
1810 /* make stoppable */
1811 (void) signal(SIGTSTP
, SIG_DFL
);
1812 (void) signal(SIGTTIN
, SIG_DFL
);
1813 (void) signal(SIGTTOU
, SIG_DFL
);
1815 #endif /* BSDJOBS */
1816 sigaction(SIGTERM
, &parterm
, NULL
);
1818 else if (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
)) {
1819 (void) signal(SIGINT
, SIG_IGN
);
1820 (void) signal(SIGQUIT
, SIG_IGN
);
1823 signal(SIGIO
, SIG_IGN
); /* ignore SIGIO in child too */
1826 pgetty(wanttty
, pgrp
);
1828 * Nohup and nice apply only to NODE_COMMAND's but it would be nice
1829 * (?!?) if you could say "nohup (foo;bar)" Then the parser would have
1830 * to know about nice/nohup/time
1832 if (t
->t_dflg
& F_NOHUP
)
1833 (void) signal(SIGHUP
, SIG_IGN
);
1834 if (t
->t_dflg
& F_NICE
) {
1835 int nval
= SIGN_EXTEND_CHAR(t
->t_nice
);
1836 #ifdef HAVE_SETPRIORITY
1837 if (setpriority(PRIO_PROCESS
, 0, nval
) == -1 && errno
)
1838 stderror(ERR_SYSTEM
, "setpriority", strerror(errno
));
1839 #else /* !HAVE_SETPRIORITY */
1841 #endif /* !HAVE_SETPRIORITY */
1844 if (t
->t_dflg
& F_VER
) {
1845 tsetenv(STRSYSTYPE
, t
->t_systype
? STRbsd43
: STRsys53
);
1850 /* rfw 8/89 now parent can continue */
1851 if (kill(getppid(), SIGSYNCH
))
1852 stderror(ERR_SYSTEM
, "pfork child: kill", strerror(errno
));
1853 #endif /* SIGSYNCH */
1860 * `Walking' process group fix from Beto Appleton.
1861 * (beto@aixwiz.austin.ibm.com)
1862 * If setpgid fails at this point that means that
1863 * our process leader has died. We flush the current
1864 * job and become the process leader ourselves.
1865 * The parent will figure that out later.
1867 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: pid
;
1868 if (setpgid(pid
, pgrp
) == -1 && errno
== EPERM
) {
1871 * We don't care if this causes an error here;
1872 * then we are already in the right process group
1874 (void) setpgid(pid
, pgrp
= pid
);
1877 #endif /* POSIXJOBS */
1881 sigset_t pause_mask
;
1884 * rfw 8/89 Wait for child to own terminal. Solves half of ugly
1885 * synchronization problem. With this change, we know that the only
1886 * reason setpgrp to a previous process in a pipeline can fail is that
1887 * the previous process has already exited. Without this hack, he may
1888 * either have exited or not yet started to run. Two uglies become
1891 sigprocmask(SIG_BLOCK
, NULL
, &pause
);
1892 sigdelset(&pause_mask
, SIGCHLD
);
1893 sigdelset(&pause_mask
, SIGSYNCH
);
1894 sigsuspend(&pause_mask
);
1895 (void)handle_pending_signals();
1896 if (sigaction(SIGSYNCH
, &osa
, NULL
))
1897 stderror(ERR_SYSTEM
, "pfork parent: sigaction restore",
1900 #endif /* SIGSYNCH */
1903 cleanup_until(&pchild_disabled
);
1912 stderror(ERR_JOBCONTROL
);
1914 stderror(ERR_JOBCTRLSUB
);
1919 setttypgrp(int pgrp
)
1922 * If we are piping out a builtin, eg. 'echo | more' things can go
1923 * out of sequence, i.e. the more can run before the echo. This
1924 * can happen even if we have vfork, since the echo will be forked
1925 * with the regular fork. In this case, we need to set the tty
1926 * pgrp ourselves. If that happens, then the process will be still
1927 * alive. And the tty process group will already be set.
1928 * This should fix the famous sequent problem as a side effect:
1929 * The controlling terminal is lost if all processes in the
1930 * terminal process group are zombies. In this case tcgetpgrp()
1931 * returns 0. If this happens we must set the terminal process
1934 if (tcgetpgrp(FSHTTY
) != pgrp
) {
1936 struct sigaction old
;
1939 * tcsetpgrp will set SIGTTOU to all the the processes in
1940 * the background according to POSIX... We ignore this here.
1942 sigaction(SIGTTOU
, NULL
, &old
);
1943 signal(SIGTTOU
, SIG_IGN
);
1945 (void) tcsetpgrp(FSHTTY
, pgrp
);
1947 sigaction(SIGTTOU
, &old
, NULL
);
1955 * if we don't have vfork(), things can still go in the wrong order
1956 * resulting in the famous 'Stopped (tty output)'. But some systems
1957 * don't permit the setpgid() call, (these are more recent secure
1958 * systems such as ibm's aix), when they do. Then we'd rather print
1959 * an error message than hang the shell!
1960 * I am open to suggestions how to fix that.
1963 pgetty(int wanttty
, pid_t pgrp
)
1968 # endif /* POSIXJOBS */
1970 jobdebug_xprintf(("wanttty %d pid %d opgrp%d pgrp %d tpgrp %d\n",
1971 wanttty
, (int)getpid(), (int)pgrp
, (int)mygetpgrp(),
1972 (int)tcgetpgrp(FSHTTY
)));
1975 * christos: I am blocking the tty signals till I've set things
1980 sigaddset(&set
, SIGTSTP
);
1981 sigaddset(&set
, SIGTTIN
);
1982 (void)sigprocmask(SIG_BLOCK
, &set
, &oset
);
1983 cleanup_push(&oset
, sigprocmask_cleanup
);
1985 # endif /* POSIXJOBS */
1990 # endif /* !POSIXJOBS */
1993 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
1994 * Don't check for tpgrp >= 0 so even non-interactive shells give
1995 * background jobs process groups Same for the comparison in the other part
1999 if (setpgid(0, pgrp
) == -1) {
2001 /* Walking process group fix; see above */
2002 if (setpgid(0, pgrp
= getpid()) == -1) {
2003 # endif /* POSIXJOBS */
2004 stderror(ERR_SYSTEM
, "setpgid child:\n", strerror(errno
));
2008 wanttty
= pgrp
; /* Now we really want the tty, since we became the
2009 * the process group leader
2011 # endif /* POSIXJOBS */
2018 cleanup_until(&oset
);
2020 # endif /* POSIXJOBS */
2022 jobdebug_xprintf(("wanttty %d pid %d pgrp %d tpgrp %d\n",
2023 wanttty
, getpid(), mygetpgrp(), tcgetpgrp(FSHTTY
)));
2026 tpgrp
= 0; /* gave tty away */
2027 #endif /* BSDJOBS */