1 /* $Header: /src/pub/tcsh/sh.proc.c,v 3.76 2002/03/08 17:36:46 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("$Id: sh.proc.c,v 3.76 2002/03/08 17:36:46 christos Exp $")
44 #endif /* WINNT_NATIVE */
50 #if defined(_BSD) || (defined(IRIS4D) && __STDC__) || defined(__lucid) || defined(linux)
52 #endif /* _BSD || (IRIS4D && __STDC__) || __lucid || linux */
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 * C Shell - functions that manage processes, handling hanging, termination
84 #define BIGINDEX 9 /* largest desirable job index */
88 /* use 'cvxrusage' to get parallel statistics */
89 static struct cvxrusage zru
= {{0L, 0L}, {0L, 0L}, 0L, 0L, 0L, 0L,
90 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
91 {0L, 0L}, 0LL, 0LL, 0LL, 0LL, 0L, 0L, 0L,
92 0LL, 0LL, {0L, 0L, 0L, 0L, 0L}};
94 # if defined(SUNOS4) || defined(hp9000) || (defined(__alpha) && defined(__osf__))
95 static struct rusage zru
= {{0L, 0L}, {0L, 0L}, 0L, 0L, 0L, 0L,
96 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L};
98 # else /* !SUNOS4 && !hp9000 && !(__alpha && __osf__) */
101 * Initialization of this structure under RTU 4.1A & RTU 5.0 is problematic
102 * because the first two elements are unions of a time_t and a struct timeval.
103 * So we'll just have to trust the loader to do the "right thing", DAS DEC-90.
105 static struct rusage zru
;
106 # else /* masscomp */
107 static struct rusage zru
= {{0L, 0L}, {0L, 0L}, 0, 0, 0, 0, 0, 0, 0,
109 # endif /* masscomp */
110 # endif /* SUNOS4 || hp9000 || (__alpha && __osf__) */
112 #else /* !BSDTIMES */
114 static struct process_stats zru
= {{0L, 0L}, {0L, 0L}, 0, 0, 0, 0, 0, 0, 0,
115 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
116 # else /* !_SEQUENT_ */
118 static struct tms zru
= {0, 0, 0, 0}, lru
= {0, 0, 0, 0};
120 static struct tms zru
= {0L, 0L, 0L, 0L}, lru
= {0L, 0L, 0L, 0L};
122 # endif /* !_SEQUENT_ */
123 #endif /* !BSDTIMES */
125 #ifndef RUSAGE_CHILDREN
126 # define RUSAGE_CHILDREN -1
127 #endif /* RUSAGE_CHILDREN */
129 static void pflushall
__P((void));
130 static void pflush
__P((struct process
*));
131 static void pfree
__P((struct process
*));
132 static void pclrcurr
__P((struct process
*));
133 static void padd
__P((struct command
*));
134 static int pprint
__P((struct process
*, int));
135 static void ptprint
__P((struct process
*));
136 static void pads
__P((Char
*));
137 static void pkill
__P((Char
**, int));
138 static struct process
*pgetcurr
__P((struct process
*));
139 static void okpcntl
__P((void));
140 static void setttypgrp
__P((int));
143 * pchild - called at interrupt level by the SIGCHLD signal
144 * indicating that at least one child has terminated or stopped
145 * thus at least one wait system call will definitely return a
146 * childs status. Top level routines (like pwait) must be sure
147 * to mask interrupts when playing with the proclist data structures!
154 register struct process
*pp
;
155 register struct process
*fp
;
157 #if defined(BSDJOBS) || (!defined(BSDTIMES) && (defined(ODT) || defined(aiws) || defined(uts)))
159 #endif /* BSDJOBS || (!BSDTIMES && (ODT || aiws || uts)) */
164 #endif /* !BSDWAIT */
168 #else /* !BSDTIMES */
170 struct process_stats ru
;
171 struct process_stats cpst1
, cpst2
;
173 # else /* !_SEQUENT_ */
174 struct tms proctimes
;
179 (void) times(&shtimes
);
181 # endif /* !_SEQUENT_ */
182 #endif /* !BSDTIMES */
185 xprintf("pchild()\n");
186 #endif /* JOBDEBUG */
188 /* Christos on where the signal(SIGCHLD, pchild) shoud be:
190 * I think that it should go *after* the wait, unlike most signal handlers.
192 * In release two (for which I have manuals), it says that wait will remove
193 * the first child from the queue of dead children.
194 * All the rest of the children that die while in the signal handler of the
195 * SIGC(H)LD, will be placed in the queue. If signal is called to re-establish
196 * the signal handler, and there are items in the queue, the process will
197 * receive another SIGC(H)LD before signal returns. BTW this is from the
198 * manual page on comp-sim... Maybe it is not applicable to the hp's, but
199 * I read on the news in comp.unix.wizards or comp.unix.questions yesterday
200 * that another person was claiming the the signal() call should be after
205 errno
= 0; /* reset, just in case */
207 xprintf("Waiting...\n");
209 #endif /* JOBDEBUG */
214 /* use 'cvxwait' to get parallel statistics */
216 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), &ru
);
218 /* both a wait3 and rusage */
219 # if !defined(BSDWAIT) || defined(NeXT) || defined(MACH) || defined(linux) || (defined(IRIS4D) && (__STDC__ || defined(FUNCPROTO)) && SYSVREL <= 3) || defined(__lucid) || defined(__osf__)
221 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), &ru
);
223 pid
= wait3(&w
.w_status
,
224 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), &ru
);
225 # endif /* BSDWAIT */
227 # else /* !BSDTIMES */
229 (void) get_process_stats(&tv
, PS_SELF
, 0, &cpst1
);
230 pid
= waitpid(-1, &w
,
231 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
232 (void) get_process_stats(&tv
, PS_SELF
, 0, &cpst2
);
233 pr_stat_sub(&cpst2
, &cpst1
, &ru
);
234 # else /* !_SEQUENT_ */
236 /* we have a wait3, but no rusage stuff */
237 pid
= wait3(&w
.w_status
,
238 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), 0);
240 pid
= waitpid(-1, &w
,
241 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
243 # endif /* !_SEQUENT_ */
244 # endif /* !BSDTIMES */
245 # else /* !BSDJOBS */
248 /* both a wait3 and rusage */
250 pid
= wait3(&w
.w_status
, WNOHANG
, 0);
252 pid
= wait3(&w
.w_status
, WNOHANG
, &ru
);
254 # else /* !BSDTIMES */
255 # ifdef ODT /* For Sco Unix 3.2.0 or ODT 1.0 */
257 pid
= waitpid(-1, &w
,
258 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
260 # if defined(aiws) || defined(uts)
262 pid
= wait3(&w
.w_status
,
263 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
), 0);
264 # endif /* aiws || uts */
267 /* no wait3, therefore no rusage */
268 /* on Sys V, this may hang. I hope it's not going to be a problem */
272 pid
= ourwait(&w
.w_status
);
274 # else /* !UNRELSIGS */
276 * XXX: for greater than 3 we should use waitpid().
277 * but then again, SVR4 falls into the POSIX/BSDJOBS category.
279 pid
= wait(&w
.w_status
);
280 # endif /* !UNRELSIGS */
281 # endif /* !HAVEwait3 */
282 # endif /* !BSDTIMES */
284 (void) sigset(SIGCHLD
, pchild
);
285 # endif /* !BSDSIGS */
286 # endif /* !BSDJOBS */
287 #else /* WINNT_NATIVE */
290 pid
= waitpid(-1, &w
,
291 (setintr
&& (intty
|| insource
) ? WNOHANG
| WUNTRACED
: WNOHANG
));
293 #endif /* WINNT_NATIVE */
296 xprintf("parent %d pid %d, retval %x termsig %x retcode %x\n",
297 getpid(), pid
, w
, WTERMSIG(w
), WEXITSTATUS(w
));
299 #endif /* JOBDEBUG */
301 if ((pid
== 0) || (pid
== -1)) {
303 xprintf("errno == %d\n", errno
);
304 #endif /* JOBDEBUG */
305 if (errno
== EINTR
) {
309 pnoprocesses
= pid
== -1;
314 #endif /* !SIGVOID */
316 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
)
317 if (pid
== pp
->p_procid
)
319 #if !defined(BSDJOBS) && !defined(WINNT_NATIVE)
320 /* this should never have happened */
321 stderror(ERR_SYNC
, pid
);
323 #else /* BSDJOBS || WINNT_NATIVE */
325 #endif /* !BSDJOBS && !WINNT_NATIVE */
327 pp
->p_flags
&= ~(PRUNNING
| PSTOPPED
| PREPORTED
);
329 pp
->p_flags
|= PSTOPPED
;
330 pp
->p_reason
= WSTOPSIG(w
);
333 if (pp
->p_flags
& (PTIME
| PPTIME
) || adrof(STRtime
))
336 (void) get_process_stats(&pp
->p_etime
, PS_SELF
, NULL
, NULL
);
337 # else /* !_SEQUENT_ */
339 pp
->p_etime
= times(&proctimes
);
340 # else /* COHERENT */
341 pp
->p_etime
= HZ
* time(NULL
);
343 # endif /* COHERENT */
344 # endif /* !_SEQUENT_ */
346 (void) gettimeofday(&pp
->p_etime
, NULL
);
347 #endif /* BSDTIMES */
350 #if defined(BSDTIMES) || defined(_SEQUENT_)
352 #else /* !BSDTIMES && !_SEQUENT_ */
353 (void) times(&proctimes
);
354 pp
->p_utime
= proctimes
.tms_cutime
- shtimes
.tms_cutime
;
355 pp
->p_stime
= proctimes
.tms_cstime
- shtimes
.tms_cstime
;
357 #endif /* !BSDTIMES && !_SEQUENT_ */
358 if (WIFSIGNALED(w
)) {
359 if (WTERMSIG(w
) == SIGINT
)
360 pp
->p_flags
|= PINTERRUPTED
;
362 pp
->p_flags
|= PSIGNALED
;
364 pp
->p_flags
|= PDUMPED
;
365 pp
->p_reason
= WTERMSIG(w
);
368 pp
->p_reason
= WEXITSTATUS(w
);
369 if (pp
->p_reason
!= 0)
370 pp
->p_flags
|= PAEXITED
;
372 pp
->p_flags
|= PNEXITED
;
378 if ((fp
->p_flags
& (PPTIME
| PRUNNING
| PSTOPPED
)) == 0 &&
379 !child
&& adrof(STRtime
) &&
381 fp
->p_rusage
.ru_utime
.tv_sec
+ fp
->p_rusage
.ru_stime
.tv_sec
382 #else /* !BSDTIMES */
384 fp
->p_rusage
.ps_utime
.tv_sec
+ fp
->p_rusage
.ps_stime
.tv_sec
385 # else /* !_SEQUENT_ */
387 (fp
->p_utime
+ fp
->p_stime
) / HZ
389 (fp
->p_utime
+ fp
->p_stime
) / clk_tck
391 # endif /* !_SEQUENT_ */
392 #endif /* !BSDTIMES */
393 >= atoi(short2str(varval(STRtime
))))
394 fp
->p_flags
|= PTIME
;
395 jobflags
|= fp
->p_flags
;
396 } while ((fp
= fp
->p_friends
) != pp
);
397 pp
->p_flags
&= ~PFOREGND
;
398 if (pp
== pp
->p_friends
&& (pp
->p_flags
& PPTIME
)) {
399 pp
->p_flags
&= ~PPTIME
;
400 pp
->p_flags
|= PTIME
;
402 if ((jobflags
& (PRUNNING
| PREPORTED
)) == 0) {
405 if (fp
->p_flags
& PSTOPPED
)
406 fp
->p_flags
|= PREPORTED
;
407 } while ((fp
= fp
->p_friends
) != pp
);
408 while (fp
->p_procid
!= fp
->p_jobid
)
410 if (jobflags
& PSTOPPED
) {
411 if (pcurrent
&& pcurrent
!= fp
)
412 pprevious
= pcurrent
;
417 if (jobflags
& PFOREGND
) {
418 if (!(jobflags
& (PSIGNALED
| PSTOPPED
| PPTIME
) ||
420 jobflags
& PAEXITED
||
422 !eq(dcwd
->di_name
, fp
->p_cwd
->di_name
))) {
423 /* PWP: print a newline after ^C */
424 if (jobflags
& PINTERRUPTED
) {
426 xputchar('\r' | QUOTE
), xputchar('\n');
427 #else /* !SHORT_STRINGS */
428 xprintf("\215\n"); /* \215 is a quoted ^M */
429 #endif /* !SHORT_STRINGS */
432 else if ((jobflags
& (PTIME
|PSTOPPED
)) == PTIME
)
438 if (jobflags
& PNOTIFY
|| adrof(STRnotify
)) {
440 xputchar('\r' | QUOTE
), xputchar('\n');
441 #else /* !SHORT_STRINGS */
442 xprintf("\215\n"); /* \215 is a quoted ^M */
443 #endif /* !SHORT_STRINGS */
444 (void) pprint(pp
, NUMBER
| NAME
| REASON
);
445 if ((jobflags
& PSTOPPED
) == 0)
448 extern Char GettingInput
;
455 * don't really want to do that, because it
456 * will erase our message in case of multi-line
467 fp
->p_flags
|= PNEEDNOTE
;
472 #if defined(BSDJOBS) || defined(HAVEwait3)
474 #endif /* BSDJOBS || HAVEwait3 */
480 register struct process
*pp
;
487 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
) {
488 if (pp
->p_flags
& PNEEDNOTE
) {
490 omask
= sigblock(sigmask(SIGCHLD
));
492 (void) sighold(SIGCHLD
);
493 #endif /* !BSDSIGS */
494 pp
->p_flags
&= ~PNEEDNOTE
;
495 flags
= pprint(pp
, NUMBER
| NAME
| REASON
);
496 if ((flags
& (PRUNNING
| PSTOPPED
)) == 0)
499 (void) sigsetmask(omask
);
501 (void) sigrelse(SIGCHLD
);
502 #endif /* !BSDSIGS */
512 xfree((ptr_t
) pp
->p_command
);
513 if (pp
->p_cwd
&& --pp
->p_cwd
->di_count
== 0)
514 if (pp
->p_cwd
->di_next
== 0)
521 * pwait - wait for current job to terminate, maintaining integrity
522 * of current and previous job indicators.
527 register struct process
*fp
, *pp
;
533 * Here's where dead procs get flushed.
536 omask
= sigblock(sigmask(SIGCHLD
));
538 (void) sighold(SIGCHLD
);
539 #endif /* !BSDSIGS */
540 for (pp
= (fp
= &proclist
)->p_next
; pp
!= NULL
; pp
= (fp
= pp
)->p_next
)
541 if (pp
->p_procid
== 0) {
542 fp
->p_next
= pp
->p_next
;
547 (void) sigsetmask(omask
);
549 (void) sigrelse(SIGCHLD
);
554 #endif /* !BSDSIGS */
560 * pjwait - wait for a job to finish or become stopped
561 * It is assumed to be in the foreground state (PFOREGND)
565 register struct process
*pp
;
567 register struct process
*fp
;
568 int jobflags
, reason
;
573 signalfun_t inthandler
;
574 #endif /* UNRELSIGS */
575 while (pp
->p_procid
!= pp
->p_jobid
)
580 if ((fp
->p_flags
& (PFOREGND
| PRUNNING
)) == PRUNNING
)
581 xprintf(CGETS(17, 1, "BUG: waiting for background job!\n"));
582 } while ((fp
= fp
->p_friends
) != pp
);
584 * Now keep pausing as long as we are not interrupted (SIGINT), and the
585 * target process, or any of its friends, are running
589 omask
= sigblock(sigmask(SIGCHLD
));
593 inthandler
= signal(SIGINT
, SIG_IGN
);
594 #endif /* UNRELSIGS */
597 (void) sighold(SIGCHLD
);
598 #endif /* !BSDSIGS */
601 jobflags
|= fp
->p_flags
;
602 while ((fp
= (fp
->p_friends
)) != pp
);
603 if ((jobflags
& PRUNNING
) == 0)
606 xprintf("%d starting to sigpause for SIGCHLD on %d\n",
607 getpid(), fp
->p_procid
);
608 #endif /* JOBDEBUG */
610 /* (void) sigpause(sigblock((sigmask_t) 0) &~ sigmask(SIGCHLD)); */
611 (void) sigpause(omask
& ~sigmask(SIGCHLD
));
613 (void) sigpause(SIGCHLD
);
614 #endif /* !BSDSIGS */
617 xprintf("%d returned from sigpause loop\n", getpid());
618 #endif /* JOBDEBUG */
620 (void) sigsetmask(omask
);
622 (void) sigrelse(SIGCHLD
);
623 #endif /* !BSDSIGS */
626 (void) signal(SIGINT
, inthandler
);
627 #endif /* UNRELSIGS */
629 if (tpgrp
> 0) /* get tty back */
630 (void) tcsetpgrp(FSHTTY
, tpgrp
);
632 if ((jobflags
& (PSIGNALED
| PSTOPPED
| PTIME
)) ||
633 !eq(dcwd
->di_name
, fp
->p_cwd
->di_name
)) {
634 if (jobflags
& PSTOPPED
) {
636 if (adrof(STRlistjobs
)) {
639 jobcommand
[0] = STRjobs
;
640 if (eq(varval(STRlistjobs
), STRlong
))
641 jobcommand
[1] = STRml
;
643 jobcommand
[1] = NULL
;
644 jobcommand
[2] = NULL
;
646 dojobs(jobcommand
, NULL
);
647 (void) pprint(pp
, SHELLDIR
);
650 (void) pprint(pp
, AREASON
| SHELLDIR
);
653 (void) pprint(pp
, AREASON
| SHELLDIR
);
655 if ((jobflags
& (PINTERRUPTED
| PSTOPPED
)) && setintr
&&
656 (!gointr
|| !eq(gointr
, STRminus
))) {
657 if ((jobflags
& PSTOPPED
) == 0)
666 reason
= fp
->p_flags
& (PSIGNALED
| PINTERRUPTED
) ?
667 fp
->p_reason
| META
: fp
->p_reason
;
668 } while ((fp
= fp
->p_friends
) != pp
);
670 * Don't report on backquoted jobs, cause it will mess up
673 if ((reason
!= 0) && (adrof(STRprintexitvalue
)) &&
674 (pp
->p_flags
& PBACKQ
) == 0)
675 xprintf(CGETS(17, 2, "Exit %d\n"), reason
);
676 set(STRstatus
, putn(reason
), VAR_READWRITE
);
677 if (reason
&& exiterr
)
683 * dowait - wait for all processes to finish
692 register struct process
*pp
;
701 omask
= sigblock(sigmask(SIGCHLD
));
705 (void) sigrelse(SIGINT
);
707 (void) sighold(SIGCHLD
);
708 #endif /* !BSDSIGS */
709 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
710 if (pp
->p_procid
&& /* pp->p_procid == pp->p_jobid && */
711 pp
->p_flags
& PRUNNING
) {
713 (void) sigpause((sigmask_t
) 0);
715 (void) sigpause(SIGCHLD
);
716 #endif /* !BSDSIGS */
720 (void) sigsetmask(omask
);
722 (void) sigrelse(SIGCHLD
);
723 #endif /* !BSDSIGS */
728 * pflushall - flush all jobs from list (e.g. at fork())
733 register struct process
*pp
;
735 for (pp
= proclist
.p_next
; pp
!= NULL
; pp
= pp
->p_next
)
741 * pflush - flag all process structures in the same job as the
742 * the argument process for deletion. The actual free of the
743 * space is not done here since pflush is called at interrupt level.
747 register struct process
*pp
;
749 register struct process
*np
;
752 if (pp
->p_procid
== 0) {
753 xprintf(CGETS(17, 3, "BUG: process flushed twice"));
756 while (pp
->p_procid
!= pp
->p_jobid
)
764 np
->p_index
= np
->p_procid
= 0;
765 np
->p_flags
&= ~PNEEDNOTE
;
766 } while ((np
= np
->p_friends
) != pp
);
767 if (idx
== pmaxindex
) {
768 for (np
= proclist
.p_next
, idx
= 0; np
; np
= np
->p_next
)
769 if (np
->p_index
> idx
)
776 * pclrcurr - make sure the given job is not the current or previous job;
777 * pp MUST be the job leader
781 register struct process
*pp
;
783 if (pp
== pcurrent
) {
784 if (pprevious
!= NULL
) {
785 pcurrent
= pprevious
;
786 pprevious
= pgetcurr(pp
);
789 pcurrent
= pgetcurr(pp
);
790 pprevious
= pgetcurr(pp
);
793 else if (pp
== pprevious
)
794 pprevious
= pgetcurr(pp
);
797 /* +4 here is 1 for '\0', 1 ea for << >& >> */
798 static Char command
[PMAXLEN
+ 4];
803 * unparse - Export padd() functionality
807 register struct command
*t
;
813 return Strsave(command
);
818 * palloc - allocate a process structure and fill it up.
819 * an important assumption is made that the process is running.
824 register struct command
*t
;
826 register struct process
*pp
;
829 pp
= (struct process
*) xcalloc(1, (size_t) sizeof(struct process
));
831 pp
->p_flags
= ((t
->t_dflg
& F_AMPERSAND
) ? 0 : PFOREGND
) | PRUNNING
;
832 if (t
->t_dflg
& F_TIME
)
833 pp
->p_flags
|= PPTIME
;
834 if (t
->t_dflg
& F_BACKQ
)
835 pp
->p_flags
|= PBACKQ
;
836 if (t
->t_dflg
& F_HUP
)
842 if (t
->t_dflg
& F_PIPEOUT
) {
844 if (t
->t_dflg
& F_STDERR
)
845 pp
->p_flags
|= PDIAG
;
847 pp
->p_command
= Strsave(command
);
851 /* careful here with interrupt level */
853 pp
->p_index
= pcurrjob
->p_index
;
854 pp
->p_friends
= pcurrjob
;
855 pp
->p_jobid
= pcurrjob
->p_procid
;
856 for (fp
= pcurrjob
; fp
->p_friends
!= pcurrjob
; fp
= fp
->p_friends
)
866 if (pmaxindex
< BIGINDEX
)
867 pp
->p_index
= ++pmaxindex
;
872 for (np
= proclist
.p_next
; np
; np
= np
->p_next
)
873 if (np
->p_index
== i
)
882 if (pcurrent
== NULL
)
884 else if (pprevious
== NULL
)
887 pp
->p_next
= proclist
.p_next
;
888 proclist
.p_next
= pp
;
890 (void) gettimeofday(&pp
->p_btime
, NULL
);
891 #else /* !BSDTIMES */
893 (void) get_process_stats(&pp
->p_btime
, PS_SELF
, NULL
, NULL
);
894 # else /* !_SEQUENT_ */
899 pp
->p_btime
= times(&tmptimes
);
900 # else /* !COHERENT */
901 pp
->p_btime
= HZ
* time(NULL
);
903 # endif /* !COHERENT */
905 # endif /* !_SEQUENT_ */
906 #endif /* !BSDTIMES */
911 register struct command
*t
;
926 for (argp
= t
->t_dcom
; *argp
; argp
++) {
960 if ((t
->t_dflg
& F_PIPEIN
) == 0 && t
->t_dlef
) {
961 pads((t
->t_dflg
& F_READ
) ? STRspLarrow2sp
: STRspLarrowsp
);
964 if ((t
->t_dflg
& F_PIPEOUT
) == 0 && t
->t_drit
) {
965 pads((t
->t_dflg
& F_APPEND
) ? STRspRarrow2
: STRspRarrow
);
966 if (t
->t_dflg
& F_STDERR
)
980 * Avoid the Quoted Space alias hack! Reported by:
981 * sam@john-bigboote.ICS.UCI.EDU (Sam Horrocks)
983 if (cp
[0] == STRQNULL
[0])
986 i
= (int) Strlen(cp
);
988 if (cmdlen
>= PMAXLEN
)
990 if (cmdlen
+ i
>= PMAXLEN
) {
991 (void) Strcpy(cmdp
, STRsp3dots
);
996 (void) Strcpy(cmdp
, cp
);
1002 * psavejob - temporarily save the current job on a one level stack
1003 * so another job can be created. Used for { } in exp6
1004 * and `` in globbing.
1009 pholdjob
= pcurrjob
;
1014 * prestjob - opposite of psavejob. This may be missed if we are interrupted
1015 * somewhere, but pendjob cleans up anyway.
1020 pcurrjob
= pholdjob
;
1025 * pendjob - indicate that a job (set of commands) has been completed
1026 * or is about to begin.
1031 register struct process
*pp
, *tp
;
1033 if (pcurrjob
&& (pcurrjob
->p_flags
& (PFOREGND
| PSTOPPED
)) == 0) {
1035 while (pp
->p_procid
!= pp
->p_jobid
)
1037 xprintf("[%d]", pp
->p_index
);
1040 xprintf(" %d", pp
->p_procid
);
1045 pholdjob
= pcurrjob
= 0;
1049 * pprint - print a job
1053 * Hacks have been added for SVR4 to deal with pipe's being spawned in
1056 * David Dawes (dawes@physics.su.oz.au) Oct 1991
1061 register struct process
*pp
;
1066 extern char *linp
, linbuf
[];
1067 int jobflags
, pstatus
, pcond
;
1071 struct process
*pipehead
= NULL
, *pipetail
= NULL
, *pmarker
= NULL
;
1073 #endif /* BACKPIPE */
1075 while (pp
->p_procid
!= pp
->p_jobid
)
1077 if (pp
== pp
->p_friends
&& (pp
->p_flags
& PPTIME
)) {
1078 pp
->p_flags
&= ~PPTIME
;
1079 pp
->p_flags
|= PTIME
;
1082 status
= reason
= -1;
1087 * The pipeline is reversed, so locate the real head of the pipeline
1088 * if pp is at the tail of a pipe (and not already in a pipeline)
1090 if ((pp
->p_friends
->p_flags
& PPOU
) && !inpipe
&& (flag
& NAME
)) {
1095 while (pp
->p_friends
->p_flags
& PPOU
);
1099 * pmarker is used to hold the place of the proc being processed, so
1100 * we can search for the next one downstream later.
1103 pcond
= (int) (tp
!= pp
|| (inpipe
&& tp
== pp
));
1104 #else /* !BACKPIPE */
1105 pcond
= (int) (tp
!= pp
);
1106 #endif /* BACKPIPE */
1108 jobflags
|= pp
->p_flags
;
1109 pstatus
= (int) (pp
->p_flags
& PALLSTATES
);
1110 if (pcond
&& linp
!= linbuf
&& !(flag
& FANCY
) &&
1111 ((pstatus
== status
&& pp
->p_reason
== reason
) ||
1115 if (pcond
&& linp
!= linbuf
)
1117 if (flag
& NUMBER
) {
1119 pcond
= ((pp
== tp
&& !inpipe
) ||
1120 (inpipe
&& pipetail
== tp
&& pp
== pipehead
));
1121 #else /* BACKPIPE */
1123 #endif /* BACKPIPE */
1125 xprintf("[%d]%s %c ", pp
->p_index
,
1126 pp
->p_index
< 10 ? " " : "",
1127 pp
== pcurrent
? '+' :
1128 (pp
== pprevious
? '-' : ' '));
1134 extern char *sitename();
1137 xprintf("%5d ", pp
->p_procid
);
1139 xprintf("%11s ", sitename(pp
->p_procid
));
1142 if (flag
& (REASON
| AREASON
)) {
1147 if (pstatus
== status
) {
1148 if (pp
->p_reason
== reason
) {
1149 xprintf(format
, "");
1153 reason
= (int) pp
->p_reason
;
1157 reason
= (int) pp
->p_reason
;
1162 xprintf(format
, CGETS(17, 4, "Running "));
1169 * tell what happened to the background job
1170 * From: Michael Schroeder
1171 * <mlschroe@immd4.informatik.uni-erlangen.de>
1174 || ((flag
& AREASON
)
1176 && (reason
!= SIGPIPE
1177 || (pp
->p_flags
& PPOU
) == 0))) {
1181 if ((ptr
= mesg
[pp
->p_reason
& ASCII
].pname
) == NULL
)
1182 xsnprintf(ptr
= buf
, sizeof(buf
), "%s %d",
1183 CGETS(17, 5, "Signal"), pp
->p_reason
& ASCII
);
1184 xprintf(format
, ptr
);
1192 if (flag
& REASON
) {
1194 xprintf(CGETS(17, 6, "Exit %-25d"), pp
->p_reason
);
1196 xprintf(format
, CGETS(17, 7, "Done"));
1201 xprintf(CGETS(17, 8, "BUG: status=%-9o"),
1208 xprintf("%S", pp
->p_command
);
1209 if (pp
->p_flags
& PPOU
)
1211 if (pp
->p_flags
& PDIAG
)
1214 if (flag
& (REASON
| AREASON
) && pp
->p_flags
& PDUMPED
)
1215 xprintf(CGETS(17, 9, " (core dumped)"));
1216 if (tp
== pp
->p_friends
) {
1217 if (flag
& AMPERSAND
)
1219 if (flag
& JOBDIR
&&
1220 !eq(tp
->p_cwd
->di_name
, dcwd
->di_name
)) {
1221 xprintf(CGETS(17, 10, " (wd: "));
1222 dtildepr(tp
->p_cwd
->di_name
);
1226 if (pp
->p_flags
& PPTIME
&& !(status
& (PSTOPPED
| PRUNNING
))) {
1229 #if defined(BSDTIMES) || defined(_SEQUENT_)
1230 prusage(&zru
, &pp
->p_rusage
, &pp
->p_etime
,
1232 #else /* !BSDTIMES && !SEQUENT */
1233 lru
.tms_utime
= pp
->p_utime
;
1234 lru
.tms_stime
= pp
->p_stime
;
1237 prusage(&zru
, &lru
, pp
->p_etime
,
1239 #endif /* !BSDTIMES && !SEQUENT */
1243 pcond
= ((tp
== pp
->p_friends
&& !inpipe
) ||
1244 (inpipe
&& pipehead
->p_friends
== tp
&& pp
== pipetail
));
1245 #else /* !BACKPIPE */
1246 pcond
= (tp
== pp
->p_friends
);
1247 #endif /* BACKPIPE */
1251 if (flag
& SHELLDIR
&& !eq(tp
->p_cwd
->di_name
, dcwd
->di_name
)) {
1252 xprintf(CGETS(17, 11, "(wd now: "));
1253 dtildepr(dcwd
->di_name
);
1260 * if pmaker == pipetail, we are finished that pipeline, and
1261 * can now skip to past the head
1263 if (pmarker
== pipetail
) {
1269 * set pp to one before the one we want next, so the while below
1270 * increments to the correct spot.
1274 while (pp
->p_friends
->p_friends
!= pmarker
);
1275 pmarker
= pp
->p_friends
;
1278 pcond
= ((pp
= pp
->p_friends
) != tp
|| inpipe
);
1279 #else /* !BACKPIPE */
1280 pcond
= ((pp
= pp
->p_friends
) != tp
);
1281 #endif /* BACKPIPE */
1284 if (jobflags
& PTIME
&& (jobflags
& (PSTOPPED
| PRUNNING
)) == 0) {
1285 if (jobflags
& NUMBER
)
1293 * All 4.3 BSD derived implementations are buggy and I've had enough.
1294 * The following implementation produces similar code and works in all
1295 * cases. The 4.3BSD one works only for <, >, !=
1298 # define timercmp(tvp, uvp, cmp) \
1299 (((tvp)->tv_sec == (uvp)->tv_sec) ? \
1300 ((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1301 ((tvp)->tv_sec cmp (uvp)->tv_sec))
1305 register struct process
*tp
;
1308 struct timeval tetime
, diff
;
1309 static struct timeval ztime
;
1310 struct sysrusage ru
;
1311 register struct process
*pp
= tp
;
1316 ruadd(&ru
, &pp
->p_rusage
);
1317 tvsub(&diff
, &pp
->p_etime
, &pp
->p_btime
);
1318 if (timercmp(&diff
, &tetime
, >))
1320 } while ((pp
= pp
->p_friends
) != tp
);
1321 prusage(&zru
, &ru
, &tetime
, &ztime
);
1322 #else /* !BSDTIMES */
1324 timeval_t tetime
, diff
;
1325 static timeval_t ztime
;
1326 struct process_stats ru
;
1327 register struct process
*pp
= tp
;
1332 ruadd(&ru
, &pp
->p_rusage
);
1333 tvsub(&diff
, &pp
->p_etime
, &pp
->p_btime
);
1334 if (timercmp(&diff
, &tetime
, >))
1336 } while ((pp
= pp
->p_friends
) != tp
);
1337 prusage(&zru
, &ru
, &tetime
, &ztime
);
1338 # else /* !_SEQUENT_ */
1340 static time_t ztime
= 0;
1341 static time_t zu_time
= 0;
1342 static time_t zs_time
= 0;
1343 time_t tetime
, diff
;
1344 time_t u_time
, s_time
;
1347 static clock_t ztime
= 0;
1348 static clock_t zu_time
= 0;
1349 static clock_t zs_time
= 0;
1350 clock_t tetime
, diff
;
1351 clock_t u_time
, s_time
;
1354 struct tms zts
, rts
;
1355 register struct process
*pp
= tp
;
1361 u_time
+= pp
->p_utime
;
1362 s_time
+= pp
->p_stime
;
1363 diff
= pp
->p_etime
- pp
->p_btime
;
1366 } while ((pp
= pp
->p_friends
) != tp
);
1367 zts
.tms_utime
= zu_time
;
1368 zts
.tms_stime
= zs_time
;
1371 rts
.tms_utime
= u_time
;
1372 rts
.tms_stime
= s_time
;
1375 prusage(&zts
, &rts
, tetime
, ztime
);
1376 # endif /* !_SEQUENT_ */
1377 #endif /* !BSDTIMES */
1381 * dojobs - print all jobs
1389 register struct process
*pp
;
1390 register int flag
= NUMBER
| NAME
| REASON
;
1397 if (v
[1] || !eq(*v
, STRml
))
1399 flag
|= FANCY
| JOBDIR
;
1401 for (i
= 1; i
<= pmaxindex
; i
++)
1402 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1403 if (pp
->p_index
== i
&& pp
->p_procid
== pp
->p_jobid
) {
1404 pp
->p_flags
&= ~PNEEDNOTE
;
1405 if (!(pprint(pp
, flag
) & (PRUNNING
| PSTOPPED
)))
1412 * dofg - builtin - put the job into the foreground
1420 register struct process
*pp
;
1427 if (!pstart(pp
, 1)) {
1429 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1437 #endif /* !BSDSIGS */
1439 } while (*v
&& *++v
);
1443 * %... - builtin - put the job into the foreground
1451 register struct process
*pp
;
1456 if (!pstart(pp
, 1)) {
1458 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1466 #endif /* !BSDSIGS */
1471 * dobg - builtin - put the job into the background
1479 register struct process
*pp
;
1486 if (!pstart(pp
, 0)) {
1488 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1490 } while (*v
&& *++v
);
1494 * %... & - builtin - put the job into the background
1502 register struct process
*pp
;
1506 if (!pstart(pp
, 0)) {
1508 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
, strerror(errno
));
1513 * dostop - builtin - stop the job
1523 pkill(++v
, SIGSTOP
);
1524 #endif /* BSDJOBS */
1528 * dokill - builtin - superset of kill (1)
1536 register int signum
, len
= 0;
1537 register char *name
;
1544 if (v
[0] && v
[0][0] == '-') {
1545 if (v
[0][1] == 'l') {
1546 for (signum
= 0; signum
<= nsig
; signum
++) {
1547 if ((name
= mesg
[signum
].iname
) != NULL
) {
1548 len
+= strlen(name
) + 1;
1549 if (len
>= T_Cols
- 1) {
1551 len
= strlen(name
) + 1;
1553 xprintf("%s ", name
);
1560 if (v
[0][1] == 's') {
1565 stderror(ERR_NAME
| ERR_TOOFEW
);
1568 if (Isdigit(*sigptr
)) {
1569 signum
= atoi(short2str(sigptr
));
1570 if (signum
< 0 || signum
> (MAXSIG
-1))
1571 stderror(ERR_NAME
| ERR_BADSIG
);
1574 for (signum
= 0; signum
<= nsig
; signum
++)
1575 if (mesg
[signum
].iname
&&
1576 eq(sigptr
, str2short(mesg
[signum
].iname
)))
1578 setname(short2str(sigptr
));
1579 stderror(ERR_NAME
| ERR_UNKSIG
);
1594 register struct process
*pp
, *np
;
1595 int jobflags
= 0, err1
= 0;
1599 #endif /* BSDSIGS */
1603 omask
= sigmask(SIGCHLD
);
1605 omask
|= sigmask(SIGINT
);
1606 omask
= sigblock(omask
) & ~omask
;
1607 #else /* !BSDSIGS */
1609 (void) sighold(SIGINT
);
1610 (void) sighold(SIGCHLD
);
1611 #endif /* !BSDSIGS */
1613 /* Avoid globbing %?x patterns */
1614 for (vp
= v
; vp
&& *vp
; vp
++)
1618 gflag
= 0, tglob(v
);
1622 stderror(ERR_NAME
| ERR_NOMATCH
);
1625 v
= gargv
= saveblk(v
);
1630 while (v
&& (cp
= *v
)) {
1632 np
= pp
= pfind(cp
);
1634 jobflags
|= np
->p_flags
;
1635 while ((np
= np
->p_friends
) != pp
);
1643 if ((jobflags
& PRUNNING
) == 0) {
1645 xprintf(CGETS(17, 12, "%S: Already suspended\n"), cp
);
1646 # else /* !SUSPENDED */
1647 xprintf(CGETS(17, 13, "%S: Already stopped\n"), cp
);
1648 # endif /* !SUSPENDED */
1654 * suspend a process, kill -CONT %, then type jobs; the shell
1655 * says it is suspended, but it is running; thanks jaap..
1658 if (!pstart(pp
, 0)) {
1660 stderror(ERR_NAME
|ERR_BADJOB
, pp
->p_command
,
1667 #endif /* BSDJOBS */
1668 if (killpg(pp
->p_jobid
, signum
) < 0) {
1669 xprintf("%S: %s\n", cp
, strerror(errno
));
1673 if (signum
== SIGTERM
|| signum
== SIGHUP
)
1674 (void) killpg(pp
->p_jobid
, SIGCONT
);
1675 #endif /* BSDJOBS */
1677 else if (!(Isdigit(*cp
) || *cp
== '-'))
1678 stderror(ERR_NAME
| ERR_JOBARGS
);
1680 #ifndef WINNT_NATIVE
1681 pid
= atoi(short2str(cp
));
1683 pid
= strtoul(short2str(cp
),NULL
,0);
1684 #endif /* WINNT_NATIVE */
1685 if (kill(pid
, signum
) < 0) {
1686 xprintf("%d: %s\n", pid
, strerror(errno
));
1691 if (signum
== SIGTERM
|| signum
== SIGHUP
)
1692 (void) kill(pid
, SIGCONT
);
1693 #endif /* BSDJOBS */
1699 blkfree(gargv
), gargv
= 0;
1701 (void) sigsetmask(omask
);
1702 #else /* !BSDSIGS */
1703 (void) sigrelse(SIGCHLD
);
1705 (void) sigrelse(SIGINT
);
1706 #endif /* !BSDSIGS */
1708 stderror(ERR_SILENT
);
1712 * pstart - start the job in foreground/background
1716 register struct process
*pp
;
1720 register struct process
*np
;
1723 #endif /* BSDSIGS */
1724 /* We don't use jobflags in this function right now (see below) */
1725 /* long jobflags = 0; */
1728 omask
= sigblock(sigmask(SIGCHLD
));
1729 #else /* !BSDSIGS */
1730 (void) sighold(SIGCHLD
);
1734 /* We don't use jobflags in this function right now (see below) */
1735 /* jobflags |= np->p_flags; */
1736 if (np
->p_flags
& (PRUNNING
| PSTOPPED
)) {
1737 np
->p_flags
|= PRUNNING
;
1738 np
->p_flags
&= ~PSTOPPED
;
1740 np
->p_flags
|= PFOREGND
;
1742 np
->p_flags
&= ~PFOREGND
;
1744 } while ((np
= np
->p_friends
) != pp
);
1747 (void) pprint(pp
, foregnd
? NAME
| JOBDIR
: NUMBER
| NAME
| AMPERSAND
);
1749 /* GrP run jobcmd hook if foregrounding */
1751 job_cmd(pp
->p_command
);
1756 rv
= tcsetpgrp(FSHTTY
, pp
->p_jobid
);
1759 * 1. child process of csh (shell script) receives SIGTTIN/SIGTTOU
1760 * 2. parent process (csh) receives SIGCHLD
1761 * 3. The "csh" signal handling function pchild() is invoked
1762 * with a SIGCHLD signal.
1763 * 4. pchild() calls wait3(WNOHANG) which returns 0.
1764 * The child process is NOT ready to be waited for at this time.
1765 * pchild() returns without picking-up the correct status
1766 * for the child process which generated the SIGCHILD.
1767 * 5. CONSEQUENCE : csh is UNaware that the process is stopped
1768 * 6. THIS LINE HAS BEEN COMMENTED OUT : if (jobflags&PSTOPPED)
1769 * (beto@aixwiz.austin.ibm.com - aug/03/91)
1770 * 7. I removed the line completely and added extra checks for
1771 * pstart, so that if a job gets attached to and dies inside
1772 * a debugger it does not confuse the shell. [christos]
1773 * 8. on the nec sx-4 there seems to be a problem, which requires
1774 * a syscall(151, getpid(), getpid()) in osinit. Don't ask me
1775 * what this is doing. [schott@rzg.mpg.de]
1779 rv
= killpg(pp
->p_jobid
, SIGCONT
);
1780 #endif /* BSDJOBS */
1782 (void) sigsetmask(omask
);
1783 #else /* !BSDSIGS */
1784 (void) sigrelse(SIGCHLD
);
1785 #endif /* !BSDSIGS */
1793 register struct process
*pp
;
1796 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1797 if (pp
->p_flags
& PSTOPPED
)
1798 stderror(ERR_STOPPED
, neednl
? "\n" : "");
1805 register struct process
*pp
, *np
;
1807 if (cp
== 0 || cp
[1] == 0 || eq(cp
, STRcent2
) || eq(cp
, STRcentplus
)) {
1808 if (pcurrent
== NULL
)
1809 stderror(ERR_NAME
| ERR_JOBCUR
);
1812 if (eq(cp
, STRcentminus
) || eq(cp
, STRcenthash
)) {
1813 if (pprevious
== NULL
)
1814 stderror(ERR_NAME
| ERR_JOBPREV
);
1817 if (Isdigit(cp
[1])) {
1818 int idx
= atoi(short2str(cp
+ 1));
1820 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1821 if (pp
->p_index
== idx
&& pp
->p_procid
== pp
->p_jobid
)
1823 stderror(ERR_NAME
| ERR_NOSUCHJOB
);
1826 for (pp
= proclist
.p_next
; pp
; pp
= pp
->p_next
)
1827 if (pp
->p_procid
== pp
->p_jobid
) {
1831 for (dp
= pp
->p_command
; *dp
; dp
++) {
1834 if (prefix(cp
+ 2, dp
))
1838 else if (prefix(cp
+ 1, pp
->p_command
)) {
1841 stderror(ERR_NAME
| ERR_AMBIG
);
1847 stderror(ERR_NAME
| (cp
[1] == '?' ? ERR_JOBPAT
: ERR_NOSUCHJOB
));
1854 * pgetcurr - find most recent job that is not pp, preferably stopped
1856 static struct process
*
1858 register struct process
*pp
;
1860 register struct process
*np
;
1861 register struct process
*xp
= NULL
;
1863 for (np
= proclist
.p_next
; np
; np
= np
->p_next
)
1864 if (np
!= pcurrent
&& np
!= pp
&& np
->p_procid
&&
1865 np
->p_procid
== np
->p_jobid
) {
1866 if (np
->p_flags
& PSTOPPED
)
1875 * donotify - flag the job so as to report termination asynchronously
1883 register struct process
*pp
;
1887 pp
->p_flags
|= PNOTIFY
;
1891 * Do the fork and whatever should be done in the child side that
1892 * should not be done if we are not forking at all (like for simple builtin's)
1893 * Also do everything that needs any signals fiddled with in the parent side
1895 * Wanttty tells whether process and/or tty pgrps are to be manipulated:
1896 * -1: leave tty alone; inherit pgrp from parent
1897 * 0: already have tty; manipulate process pgrps only
1898 * 1: want to claim tty; manipulate process and tty pgrps
1899 * It is usually just the value of tpgrp.
1904 struct command
*t
; /* command we are forking for */
1911 sigmask_t omask
= 0;
1912 #endif /* BSDSIGS */
1915 static sigvec_t nsv
= {synch_handler
, (sigset_t
) ~0, 0};
1916 #endif /* SIGSYNCH */
1919 * A child will be uninterruptible only under very special conditions.
1920 * Remember that the semantics of '&' is implemented by disconnecting the
1921 * process from the tty so signals do not need to ignored just for '&'.
1922 * Thus signals are set to default action for children unless: we have had
1923 * an "onintr -" (then specifically ignored) we are not playing with
1924 * signals (inherit action)
1927 ignint
= (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
))
1928 || (gointr
&& eq(gointr
, STRminus
));
1931 ignint
|= gointr
&& eq(gointr
, STRminus
);
1932 #endif /* COHERENT */
1935 * Check for maximum nesting of 16 processes to avoid Forking loops
1938 stderror(ERR_NESTING
, 16);
1940 if (mysigvec(SIGSYNCH
, &nsv
, &osv
))
1941 stderror(ERR_SYSTEM
, "pfork: sigvec set", strerror(errno
));
1942 #endif /* SIGSYNCH */
1944 * Hold SIGCHLD until we have the process installed in our table.
1948 omask
= sigblock(sigmask(SIGCHLD
));
1949 #else /* !BSDSIGS */
1950 (void) sighold(SIGCHLD
);
1951 #endif /* !BSDSIGS */
1953 while ((pid
= fork()) == -1)
1955 (void) sleep(FORKSLEEP
);
1959 (void) sigsetmask(omask
);
1960 #else /* !BSDSIGS */
1961 (void) sigrelse(SIGCHLD
);
1962 (void) sigrelse(SIGINT
);
1963 #endif /* !BSDSIGS */
1964 stderror(ERR_NOPROC
);
1968 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: getpid();
1971 #if !defined(BSDTIMES) && !defined(_SEQUENT_)
1973 #endif /* !defined(BSDTIMES) && !defined(_SEQUENT_) */
1976 setintr
= 0; /* until I think otherwise */
1979 (void) sigrelse(SIGCHLD
);
1980 #endif /* !BSDSIGS */
1982 * Children just get blown away on SIGINT, SIGQUIT unless "onintr
1985 (void) signal(SIGINT
, ignint
? SIG_IGN
: SIG_DFL
);
1986 (void) signal(SIGQUIT
, ignint
? SIG_IGN
: SIG_DFL
);
1989 /* make stoppable */
1990 (void) signal(SIGTSTP
, SIG_DFL
);
1991 (void) signal(SIGTTIN
, SIG_DFL
);
1992 (void) signal(SIGTTOU
, SIG_DFL
);
1994 #endif /* BSDJOBS */
1995 (void) signal(SIGTERM
, parterm
);
1997 else if (tpgrp
== -1 && (t
->t_dflg
& F_NOINTERRUPT
)) {
1998 (void) signal(SIGINT
, SIG_IGN
);
1999 (void) signal(SIGQUIT
, SIG_IGN
);
2002 sigignore(SIGIO
); /* ignore SIGIO in child too */
2005 pgetty(wanttty
, pgrp
);
2007 * Nohup and nice apply only to NODE_COMMAND's but it would be nice
2008 * (?!?) if you could say "nohup (foo;bar)" Then the parser would have
2009 * to know about nice/nohup/time
2011 if (t
->t_dflg
& F_NOHUP
)
2012 (void) signal(SIGHUP
, SIG_IGN
);
2013 if (t
->t_dflg
& F_NICE
) {
2014 int nval
= SIGN_EXTEND_CHAR(t
->t_nice
);
2016 if (setpriority(PRIO_PROCESS
, 0, nval
) == -1 && errno
)
2017 stderror(ERR_SYSTEM
, "setpriority", strerror(errno
));
2018 #else /* !BSDNICE */
2020 #endif /* !BSDNICE */
2023 if (t
->t_dflg
& F_VER
) {
2024 tsetenv(STRSYSTYPE
, t
->t_systype
? STRbsd43
: STRsys53
);
2029 /* rfw 8/89 now parent can continue */
2030 if (kill(getppid(), SIGSYNCH
))
2031 stderror(ERR_SYSTEM
, "pfork child: kill", strerror(errno
));
2032 #endif /* SIGSYNCH */
2039 * `Walking' process group fix from Beto Appleton.
2040 * (beto@aixwiz.austin.ibm.com)
2041 * If setpgid fails at this point that means that
2042 * our process leader has died. We flush the current
2043 * job and become the process leader ourselves.
2044 * The parent will figure that out later.
2046 pgrp
= pcurrjob
? pcurrjob
->p_jobid
: pid
;
2047 if (setpgid(pid
, pgrp
) == -1 && errno
== EPERM
) {
2050 * We don't care if this causes an error here;
2051 * then we are already in the right process group
2053 (void) setpgid(pid
, pgrp
= pid
);
2056 #endif /* POSIXJOBS */
2060 * rfw 8/89 Wait for child to own terminal. Solves half of ugly
2061 * synchronization problem. With this change, we know that the only
2062 * reason setpgrp to a previous process in a pipeline can fail is that
2063 * the previous process has already exited. Without this hack, he may
2064 * either have exited or not yet started to run. Two uglies become
2067 (void) sigpause(omask
& ~SYNCHMASK
);
2068 if (mysigvec(SIGSYNCH
, &osv
, NULL
))
2069 stderror(ERR_SYSTEM
, "pfork parent: sigvec restore",
2071 #endif /* SIGSYNCH */
2075 (void) sigsetmask(omask
);
2076 #else /* !BSDSIGS */
2077 (void) sigrelse(SIGCHLD
);
2078 #endif /* !BSDSIGS */
2088 stderror(ERR_JOBCONTROL
);
2090 stderror(ERR_JOBCTRLSUB
);
2099 * If we are piping out a builtin, eg. 'echo | more' things can go
2100 * out of sequence, i.e. the more can run before the echo. This
2101 * can happen even if we have vfork, since the echo will be forked
2102 * with the regular fork. In this case, we need to set the tty
2103 * pgrp ourselves. If that happens, then the process will be still
2104 * alive. And the tty process group will already be set.
2105 * This should fix the famous sequent problem as a side effect:
2106 * The controlling terminal is lost if all processes in the
2107 * terminal process group are zombies. In this case tcgetpgrp()
2108 * returns 0. If this happens we must set the terminal process
2111 if (tcgetpgrp(FSHTTY
) != pgrp
) {
2114 * tcsetpgrp will set SIGTTOU to all the the processes in
2115 * the background according to POSIX... We ignore this here.
2117 signalfun_t old
= sigset(SIGTTOU
, SIG_IGN
);
2119 (void) tcsetpgrp(FSHTTY
, pgrp
);
2121 (void) sigset(SIGTTOU
, old
);
2129 * if we don't have vfork(), things can still go in the wrong order
2130 * resulting in the famous 'Stopped (tty output)'. But some systems
2131 * don't permit the setpgid() call, (these are more recent secure
2132 * systems such as ibm's aix), when they do. Then we'd rather print
2133 * an error message than hang the shell!
2134 * I am open to suggestions how to fix that.
2137 pgetty(wanttty
, pgrp
)
2141 # if defined(BSDSIGS) && defined(POSIXJOBS)
2142 sigmask_t omask
= 0;
2143 # endif /* BSDSIGS && POSIXJOBS */
2146 xprintf("wanttty %d pid %d opgrp%d pgrp %d tpgrp %d\n",
2147 wanttty
, getpid(), pgrp
, mygetpgrp(), tcgetpgrp(FSHTTY
));
2148 # endif /* JOBDEBUG */
2151 * christos: I am blocking the tty signals till I've set things
2156 omask
= sigblock(sigmask(SIGTSTP
)|sigmask(SIGTTIN
));
2157 # else /* !BSDSIGS */
2159 (void) sighold(SIGTSTP
);
2160 (void) sighold(SIGTTIN
);
2162 # endif /* !BSDSIGS */
2163 # endif /* POSIXJOBS */
2168 # endif /* !POSIXJOBS */
2171 * From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
2172 * Don't check for tpgrp >= 0 so even non-interactive shells give
2173 * background jobs process groups Same for the comparison in the other part
2177 if (setpgid(0, pgrp
) == -1) {
2179 /* Walking process group fix; see above */
2180 if (setpgid(0, pgrp
= getpid()) == -1) {
2181 # endif /* POSIXJOBS */
2182 stderror(ERR_SYSTEM
, "setpgid child:\n", strerror(errno
));
2186 wanttty
= pgrp
; /* Now we really want the tty, since we became the
2187 * the process group leader
2189 # endif /* POSIXJOBS */
2197 (void) sigsetmask(omask
);
2198 # else /* BSDSIGS */
2199 (void) sigrelse(SIGTSTP
);
2200 (void) sigrelse(SIGTTIN
);
2201 # endif /* !BSDSIGS */
2202 # endif /* POSIXJOBS */
2205 xprintf("wanttty %d pid %d pgrp %d tpgrp %d\n",
2206 wanttty
, getpid(), mygetpgrp(), tcgetpgrp(FSHTTY
));
2207 # endif /* JOBDEBUG */
2210 tpgrp
= 0; /* gave tty away */
2211 #endif /* BSDJOBS */