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. All advertising materials mentioning features or use of this software
18
* must display the following acknowledgement:
19
* This product includes software developed by the University of
20
* California, Berkeley and its contributors.
21
* 4. Neither the name of the University nor the names of its contributors
22
* may be used to endorse or promote products derived from this software
23
* without specific prior written permission.
25
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
26
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
30
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
31
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
32
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
33
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
34
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
54
#if defined(_BSD) || (defined(IRIS4D) && __STDC__) || defined(__lucid) || defined(linux)
56
#endif /* _BSD || (IRIS4D && __STDC__) || __lucid || linux */
58
# define WTERMSIG(w) (((union wait *) &(w))->w_termsig)
61
# endif /* !BSDWAIT */
62
#endif /* !WTERMSIG */
64
# define WEXITSTATUS(w) (((union wait *) &(w))->w_retcode)
65
#endif /* !WEXITSTATUS */
67
# define WSTOPSIG(w) (((union wait *) &(w))->w_stopsig)
68
#endif /* !WSTOPSIG */
72
# define WCOREDUMP(x) (_W_INT(x) & WCOREFLAG)
78
# define WCOREDUMP(w) (((union wait *) &(w))->w_coredump)
80
# define WCOREDUMP(w) ((w) & 0200)
81
# endif /* !BSDWAIT */
82
#endif /* !WCOREDUMP */
85
* C Shell - functions that manage processes, handling hanging, termination
88
#define BIGINDEX 9 /* largest desirable job index */
92
/* use 'cvxrusage' to get parallel statistics */
93
static struct cvxrusage zru = {{0L, 0L}, {0L, 0L}, 0L, 0L, 0L, 0L,
94
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,
95
{0L, 0L}, 0LL, 0LL, 0LL, 0LL, 0L, 0L, 0L,
96
0LL, 0LL, {0L, 0L, 0L, 0L, 0L}};
98
# if defined(SUNOS4) || defined(hp9000) || (defined(__alpha) && defined(__osf__))
99
static struct rusage zru = {{0L, 0L}, {0L, 0L}, 0L, 0L, 0L, 0L,
100
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L};
102
# else /* !SUNOS4 && !hp9000 && !(__alpha && __osf__) */
105
* Initialization of this structure under RTU 4.1A & RTU 5.0 is problematic
106
* because the first two elements are unions of a time_t and a struct timeval.
107
* So we'll just have to trust the loader to do the "right thing", DAS DEC-90.
109
static struct rusage zru;
110
# else /* masscomp */
111
static struct rusage zru = {{0L, 0L}, {0L, 0L}, 0, 0, 0, 0, 0, 0, 0,
113
# endif /* masscomp */
114
# endif /* SUNOS4 || hp9000 || (__alpha && __osf__) */
116
#else /* !BSDTIMES */
118
static struct process_stats zru = {{0L, 0L}, {0L, 0L}, 0, 0, 0, 0, 0, 0, 0,
119
0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
120
# else /* !_SEQUENT_ */
122
static struct tms zru = {0, 0, 0, 0}, lru = {0, 0, 0, 0};
124
static struct tms zru = {0L, 0L, 0L, 0L}, lru = {0L, 0L, 0L, 0L};
126
# endif /* !_SEQUENT_ */
127
#endif /* !BSDTIMES */
129
#ifndef RUSAGE_CHILDREN
130
# define RUSAGE_CHILDREN -1
131
#endif /* RUSAGE_CHILDREN */
133
static void pflushall __P((void));
134
static void pflush __P((struct process *));
135
static void pfree __P((struct process *));
136
static void pclrcurr __P((struct process *));
137
static void padd __P((struct command *));
138
static int pprint __P((struct process *, int));
139
static void ptprint __P((struct process *));
140
static void pads __P((Char *));
141
static void pkill __P((Char **, int));
142
static struct process *pgetcurr __P((struct process *));
143
static void okpcntl __P((void));
144
static void setttypgrp __P((int));
147
* pchild - called at interrupt level by the SIGCHLD signal
148
* indicating that at least one child has terminated or stopped
149
* thus at least one wait system call will definitely return a
150
* childs status. Top level routines (like pwait) must be sure
151
* to mask interrupts when playing with the proclist data structures!
158
register struct process *pp;
159
register struct process *fp;
161
#if defined(BSDJOBS) || (!defined(BSDTIMES) && (defined(INTERIX) || defined(ODT) || defined(aiws) || defined(uts)))
163
#endif /* BSDJOBS || (!BSDTIMES && (ODT || aiws || uts)) */
168
#endif /* !BSDWAIT */
172
#else /* !BSDTIMES */
174
struct process_stats ru;
175
struct process_stats cpst1, cpst2;
177
# else /* !_SEQUENT_ */
178
struct tms proctimes;
183
(void) times(&shtimes);
185
# endif /* !_SEQUENT_ */
186
#endif /* !BSDTIMES */
189
xprintf("pchild()\n");
190
#endif /* JOBDEBUG */
192
/* Christos on where the signal(SIGCHLD, pchild) shoud be:
194
* I think that it should go *after* the wait, unlike most signal handlers.
196
* In release two (for which I have manuals), it says that wait will remove
197
* the first child from the queue of dead children.
198
* All the rest of the children that die while in the signal handler of the
199
* SIGC(H)LD, will be placed in the queue. If signal is called to re-establish
200
* the signal handler, and there are items in the queue, the process will
201
* receive another SIGC(H)LD before signal returns. BTW this is from the
202
* manual page on comp-sim... Maybe it is not applicable to the hp's, but
203
* I read on the news in comp.unix.wizards or comp.unix.questions yesterday
204
* that another person was claiming the the signal() call should be after
209
errno = 0; /* reset, just in case */
211
xprintf("Waiting...\n");
213
#endif /* JOBDEBUG */
218
/* use 'cvxwait' to get parallel statistics */
220
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
222
/* both a wait3 and rusage */
223
# if !defined(BSDWAIT) || defined(NeXT) || defined(MACH) || defined(linux) || (defined(IRIS4D) && (__STDC__ || defined(FUNCPROTO)) && SYSVREL <= 3) || defined(__lucid) || defined(__osf__)
225
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
227
pid = wait3(&w.w_status,
228
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
229
# endif /* BSDWAIT */
231
# else /* !BSDTIMES */
233
(void) get_process_stats(&tv, PS_SELF, 0, &cpst1);
234
pid = waitpid(-1, &w,
235
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
236
(void) get_process_stats(&tv, PS_SELF, 0, &cpst2);
237
pr_stat_sub(&cpst2, &cpst1, &ru);
238
# else /* !_SEQUENT_ */
240
/* we have a wait3, but no rusage stuff */
241
pid = wait3(&w.w_status,
242
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), 0);
244
pid = waitpid(-1, &w,
245
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
247
# endif /* !_SEQUENT_ */
248
# endif /* !BSDTIMES */
249
# else /* !BSDJOBS */
252
/* both a wait3 and rusage */
254
pid = wait3(&w.w_status, WNOHANG, 0);
256
pid = wait3(&w.w_status, WNOHANG, &ru);
258
# else /* !BSDTIMES */
259
# if defined(INTERIX) || defined(ODT) /* For Sco Unix 3.2.0 or ODT 1.0 */
261
pid = waitpid(-1, &w,
262
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
264
# if defined(aiws) || defined(uts)
266
pid = wait3(&w.w_status,
267
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), 0);
268
# endif /* aiws || uts */
271
/* no wait3, therefore no rusage */
272
/* on Sys V, this may hang. I hope it's not going to be a problem */
276
pid = ourwait(&w.w_status);
278
# else /* !UNRELSIGS */
280
* XXX: for greater than 3 we should use waitpid().
281
* but then again, SVR4 falls into the POSIX/BSDJOBS category.
283
pid = wait(&w.w_status);
284
# endif /* !UNRELSIGS */
285
# endif /* !HAVEwait3 */
286
# endif /* !BSDTIMES */
288
(void) sigset(SIGCHLD, pchild);
289
# endif /* !BSDSIGS */
290
# endif /* !BSDJOBS */
294
pid = waitpid(-1, &w,
295
(setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG));
300
xprintf("parent %d pid %d, retval %x termsig %x retcode %x\n",
301
getpid(), pid, w, WTERMSIG(w), WEXITSTATUS(w));
303
#endif /* JOBDEBUG */
305
if ((pid == 0) || (pid == -1)) {
307
xprintf("errno == %d\n", errno);
308
#endif /* JOBDEBUG */
309
if (errno == EINTR) {
313
pnoprocesses = pid == -1;
318
#endif /* !SIGVOID */
320
for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
321
if (pid == pp->p_procid)
323
#if !defined(BSDJOBS) && !defined(WINNT)
324
/* this should never have happened */
325
stderror(ERR_SYNC, pid);
327
#else /* BSDJOBS || WINNT */
329
#endif /* !BSDJOBS && !WINNT */
331
pp->p_flags &= ~(PRUNNING | PSTOPPED | PREPORTED);
333
pp->p_flags |= PSTOPPED;
334
pp->p_reason = WSTOPSIG(w);
337
if (pp->p_flags & (PTIME | PPTIME) || adrof(STRtime))
340
(void) get_process_stats(&pp->p_etime, PS_SELF, NULL, NULL);
341
# else /* !_SEQUENT_ */
343
pp->p_etime = times(&proctimes);
344
# else /* COHERENT */
345
pp->p_etime = HZ * time(NULL);
347
# endif /* COHERENT */
348
# endif /* !_SEQUENT_ */
350
(void) gettimeofday(&pp->p_etime, NULL);
351
#endif /* BSDTIMES */
354
#if defined(BSDTIMES) || defined(_SEQUENT_)
356
#else /* !BSDTIMES && !_SEQUENT_ */
357
(void) times(&proctimes);
358
pp->p_utime = proctimes.tms_cutime - shtimes.tms_cutime;
359
pp->p_stime = proctimes.tms_cstime - shtimes.tms_cstime;
361
#endif /* !BSDTIMES && !_SEQUENT_ */
362
if (WIFSIGNALED(w)) {
363
if (WTERMSIG(w) == SIGINT)
364
pp->p_flags |= PINTERRUPTED;
366
pp->p_flags |= PSIGNALED;
368
pp->p_flags |= PDUMPED;
369
pp->p_reason = WTERMSIG(w);
372
pp->p_reason = WEXITSTATUS(w);
373
if (pp->p_reason != 0)
374
pp->p_flags |= PAEXITED;
376
pp->p_flags |= PNEXITED;
382
if ((fp->p_flags & (PPTIME | PRUNNING | PSTOPPED)) == 0 &&
383
!child && adrof(STRtime) &&
385
fp->p_rusage.ru_utime.tv_sec + fp->p_rusage.ru_stime.tv_sec
386
#else /* !BSDTIMES */
388
fp->p_rusage.ps_utime.tv_sec + fp->p_rusage.ps_stime.tv_sec
389
# else /* !_SEQUENT_ */
391
(fp->p_utime + fp->p_stime) / HZ
393
(fp->p_utime + fp->p_stime) / clk_tck
395
# endif /* !_SEQUENT_ */
396
#endif /* !BSDTIMES */
397
>= atoi(short2str(varval(STRtime))))
398
fp->p_flags |= PTIME;
399
jobflags |= fp->p_flags;
400
} while ((fp = fp->p_friends) != pp);
401
pp->p_flags &= ~PFOREGND;
402
if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
403
pp->p_flags &= ~PPTIME;
404
pp->p_flags |= PTIME;
406
if ((jobflags & (PRUNNING | PREPORTED)) == 0) {
409
if (fp->p_flags & PSTOPPED)
410
fp->p_flags |= PREPORTED;
411
} while ((fp = fp->p_friends) != pp);
412
while (fp->p_procid != fp->p_jobid)
414
if (jobflags & PSTOPPED) {
415
if (pcurrent && pcurrent != fp)
416
pprevious = pcurrent;
421
if (jobflags & PFOREGND) {
422
if (!(jobflags & (PSIGNALED | PSTOPPED | PPTIME) ||
424
jobflags & PAEXITED ||
426
!eq(dcwd->di_name, fp->p_cwd->di_name))) {
427
/* PWP: print a newline after ^C */
428
if (jobflags & PINTERRUPTED) {
430
xputchar('\r' | QUOTE), xputchar('\n');
431
#else /* !SHORT_STRINGS */
432
xprintf("\215\n"); /* \215 is a quoted ^M */
433
#endif /* !SHORT_STRINGS */
436
else if ((jobflags & (PTIME|PSTOPPED)) == PTIME)
442
if (jobflags & PNOTIFY || adrof(STRnotify)) {
444
xputchar('\r' | QUOTE), xputchar('\n');
445
#else /* !SHORT_STRINGS */
446
xprintf("\215\n"); /* \215 is a quoted ^M */
447
#endif /* !SHORT_STRINGS */
448
(void) pprint(pp, NUMBER | NAME | REASON);
449
if ((jobflags & PSTOPPED) == 0)
452
extern Char GettingInput;
459
* don't really want to do that, because it
460
* will erase our message in case of multi-line
471
fp->p_flags |= PNEEDNOTE;
476
#if defined(BSDJOBS) || defined(HAVEwait3)
478
#endif /* BSDJOBS || HAVEwait3 */
484
register struct process *pp;
491
for (pp = proclist.p_next; pp != NULL; pp = pp->p_next) {
492
if (pp->p_flags & PNEEDNOTE) {
494
omask = sigblock(sigmask(SIGCHLD));
496
(void) sighold(SIGCHLD);
497
#endif /* !BSDSIGS */
498
pp->p_flags &= ~PNEEDNOTE;
499
flags = pprint(pp, NUMBER | NAME | REASON);
500
if ((flags & (PRUNNING | PSTOPPED)) == 0)
503
(void) sigsetmask(omask);
505
(void) sigrelse(SIGCHLD);
506
#endif /* !BSDSIGS */
516
xfree((ptr_t) pp->p_command);
517
if (pp->p_cwd && --pp->p_cwd->di_count == 0)
518
if (pp->p_cwd->di_next == 0)
525
* pwait - wait for current job to terminate, maintaining integrity
526
* of current and previous job indicators.
531
register struct process *fp, *pp;
537
* Here's where dead procs get flushed.
540
omask = sigblock(sigmask(SIGCHLD));
542
(void) sighold(SIGCHLD);
543
#endif /* !BSDSIGS */
544
for (pp = (fp = &proclist)->p_next; pp != NULL; pp = (fp = pp)->p_next)
545
if (pp->p_procid == 0) {
546
fp->p_next = pp->p_next;
551
(void) sigsetmask(omask);
553
(void) sigrelse(SIGCHLD);
558
#endif /* !BSDSIGS */
564
* pjwait - wait for a job to finish or become stopped
565
* It is assumed to be in the foreground state (PFOREGND)
569
register struct process *pp;
571
register struct process *fp;
572
int jobflags, reason;
577
signalfun_t inthandler;
578
#endif /* UNRELSIGS */
579
while (pp->p_procid != pp->p_jobid)
584
if ((fp->p_flags & (PFOREGND | PRUNNING)) == PRUNNING)
585
xprintf(CGETS(17, 1, "BUG: waiting for background job!\n"));
586
} while ((fp = fp->p_friends) != pp);
588
* Now keep pausing as long as we are not interrupted (SIGINT), and the
589
* target process, or any of its friends, are running
593
omask = sigblock(sigmask(SIGCHLD));
597
inthandler = signal(SIGINT, SIG_IGN);
598
#endif /* UNRELSIGS */
601
(void) sighold(SIGCHLD);
602
#endif /* !BSDSIGS */
605
jobflags |= fp->p_flags;
606
while ((fp = (fp->p_friends)) != pp);
607
if ((jobflags & PRUNNING) == 0)
610
xprintf("%d starting to sigpause for SIGCHLD on %d\n",
611
getpid(), fp->p_procid);
612
#endif /* JOBDEBUG */
614
/* (void) sigpause(sigblock((sigmask_t) 0) &~ sigmask(SIGCHLD)); */
615
(void) sigpause(omask & ~sigmask(SIGCHLD));
617
(void) sigpause(SIGCHLD);
618
#endif /* !BSDSIGS */
621
xprintf("%d returned from sigpause loop\n", getpid());
622
#endif /* JOBDEBUG */
624
(void) sigsetmask(omask);
626
(void) sigrelse(SIGCHLD);
627
#endif /* !BSDSIGS */
630
(void) signal(SIGINT, inthandler);
631
#endif /* UNRELSIGS */
633
if (tpgrp > 0) /* get tty back */
634
(void) tcsetpgrp(FSHTTY, tpgrp);
636
if ((jobflags & (PSIGNALED | PSTOPPED | PTIME)) ||
637
!eq(dcwd->di_name, fp->p_cwd->di_name)) {
638
if (jobflags & PSTOPPED) {
640
if (adrof(STRlistjobs)) {
643
jobcommand[0] = STRjobs;
644
if (eq(varval(STRlistjobs), STRlong))
645
jobcommand[1] = STRml;
647
jobcommand[1] = NULL;
648
jobcommand[2] = NULL;
650
dojobs(jobcommand, NULL);
651
(void) pprint(pp, SHELLDIR);
654
(void) pprint(pp, AREASON | SHELLDIR);
657
(void) pprint(pp, AREASON | SHELLDIR);
659
if ((jobflags & (PINTERRUPTED | PSTOPPED)) && setintr &&
660
(!gointr || !eq(gointr, STRminus))) {
661
if ((jobflags & PSTOPPED) == 0)
670
reason = fp->p_flags & (PSIGNALED | PINTERRUPTED) ?
671
fp->p_reason | META : fp->p_reason;
672
} while ((fp = fp->p_friends) != pp);
674
* Don't report on backquoted jobs, cause it will mess up
677
if ((reason != 0) && (adrof(STRprintexitvalue)) &&
678
(pp->p_flags & PBACKQ) == 0)
679
xprintf(CGETS(17, 2, "Exit %d\n"), reason);
680
set(STRstatus, putn(reason), VAR_READWRITE);
681
if (reason && exiterr)
687
* dowait - wait for all processes to finish
696
register struct process *pp;
705
omask = sigblock(sigmask(SIGCHLD));
709
(void) sigrelse(SIGINT);
711
(void) sighold(SIGCHLD);
712
#endif /* !BSDSIGS */
713
for (pp = proclist.p_next; pp; pp = pp->p_next)
714
if (pp->p_procid && /* pp->p_procid == pp->p_jobid && */
715
pp->p_flags & PRUNNING) {
717
(void) sigpause((sigmask_t) 0);
719
(void) sigpause(SIGCHLD);
720
#endif /* !BSDSIGS */
724
(void) sigsetmask(omask);
726
(void) sigrelse(SIGCHLD);
727
#endif /* !BSDSIGS */
732
* pflushall - flush all jobs from list (e.g. at fork())
737
register struct process *pp;
739
for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
745
* pflush - flag all process structures in the same job as the
746
* the argument process for deletion. The actual free of the
747
* space is not done here since pflush is called at interrupt level.
751
register struct process *pp;
753
register struct process *np;
756
if (pp->p_procid == 0) {
757
xprintf(CGETS(17, 3, "BUG: process flushed twice"));
760
while (pp->p_procid != pp->p_jobid)
768
np->p_index = np->p_procid = 0;
769
np->p_flags &= ~PNEEDNOTE;
770
} while ((np = np->p_friends) != pp);
771
if (idx == pmaxindex) {
772
for (np = proclist.p_next, idx = 0; np; np = np->p_next)
773
if (np->p_index > idx)
780
* pclrcurr - make sure the given job is not the current or previous job;
781
* pp MUST be the job leader
785
register struct process *pp;
787
if (pp == pcurrent) {
788
if (pprevious != NULL) {
789
pcurrent = pprevious;
790
pprevious = pgetcurr(pp);
793
pcurrent = pgetcurr(pp);
794
pprevious = pgetcurr(pp);
797
else if (pp == pprevious)
798
pprevious = pgetcurr(pp);
801
/* +4 here is 1 for '\0', 1 ea for << >& >> */
802
static Char command[PMAXLEN + 4];
807
* palloc - allocate a process structure and fill it up.
808
* an important assumption is made that the process is running.
813
register struct command *t;
815
register struct process *pp;
818
pp = (struct process *) xcalloc(1, (size_t) sizeof(struct process));
820
pp->p_flags = ((t->t_dflg & F_AMPERSAND) ? 0 : PFOREGND) | PRUNNING;
821
if (t->t_dflg & F_TIME)
822
pp->p_flags |= PPTIME;
823
if (t->t_dflg & F_BACKQ)
824
pp->p_flags |= PBACKQ;
825
if (t->t_dflg & F_HUP)
831
if (t->t_dflg & F_PIPEOUT) {
833
if (t->t_dflg & F_STDERR)
834
pp->p_flags |= PDIAG;
836
pp->p_command = Strsave(command);
840
/* careful here with interrupt level */
842
pp->p_index = pcurrjob->p_index;
843
pp->p_friends = pcurrjob;
844
pp->p_jobid = pcurrjob->p_procid;
845
for (fp = pcurrjob; fp->p_friends != pcurrjob; fp = fp->p_friends)
855
if (pmaxindex < BIGINDEX)
856
pp->p_index = ++pmaxindex;
861
for (np = proclist.p_next; np; np = np->p_next)
862
if (np->p_index == i)
871
if (pcurrent == NULL)
873
else if (pprevious == NULL)
876
pp->p_next = proclist.p_next;
877
proclist.p_next = pp;
879
(void) gettimeofday(&pp->p_btime, NULL);
880
#else /* !BSDTIMES */
882
(void) get_process_stats(&pp->p_btime, PS_SELF, NULL, NULL);
883
# else /* !_SEQUENT_ */
888
pp->p_btime = times(&tmptimes);
889
# else /* !COHERENT */
890
pp->p_btime = HZ * time(NULL);
892
# endif /* !COHERENT */
894
# endif /* !_SEQUENT_ */
895
#endif /* !BSDTIMES */
900
register struct command *t;
915
for (argp = t->t_dcom; *argp; argp++) {
949
if ((t->t_dflg & F_PIPEIN) == 0 && t->t_dlef) {
950
pads((t->t_dflg & F_READ) ? STRspLarrow2sp : STRspLarrowsp);
953
if ((t->t_dflg & F_PIPEOUT) == 0 && t->t_drit) {
954
pads((t->t_dflg & F_APPEND) ? STRspRarrow2 : STRspRarrow);
955
if (t->t_dflg & F_STDERR)
969
* Avoid the Quoted Space alias hack! Reported by:
970
* sam@john-bigboote.ICS.UCI.EDU (Sam Horrocks)
972
if (cp[0] == STRQNULL[0])
975
i = (int) Strlen(cp);
977
if (cmdlen >= PMAXLEN)
979
if (cmdlen + i >= PMAXLEN) {
980
(void) Strcpy(cmdp, STRsp3dots);
985
(void) Strcpy(cmdp, cp);
991
* psavejob - temporarily save the current job on a one level stack
992
* so another job can be created. Used for { } in exp6
993
* and `` in globbing.
1003
* prestjob - opposite of psavejob. This may be missed if we are interrupted
1004
* somewhere, but pendjob cleans up anyway.
1009
pcurrjob = pholdjob;
1014
* pendjob - indicate that a job (set of commands) has been completed
1015
* or is about to begin.
1020
register struct process *pp, *tp;
1022
if (pcurrjob && (pcurrjob->p_flags & (PFOREGND | PSTOPPED)) == 0) {
1024
while (pp->p_procid != pp->p_jobid)
1026
xprintf("[%d]", pp->p_index);
1029
xprintf(" %d", pp->p_procid);
1034
pholdjob = pcurrjob = 0;
1038
* pprint - print a job
1042
* Hacks have been added for SVR4 to deal with pipe's being spawned in
1045
* David Dawes (dawes@physics.su.oz.au) Oct 1991
1050
register struct process *pp;
1055
extern char *linp, linbuf[];
1056
int jobflags, pstatus, pcond;
1060
struct process *pipehead = NULL, *pipetail = NULL, *pmarker = NULL;
1062
#endif /* BACKPIPE */
1064
while (pp->p_procid != pp->p_jobid)
1066
if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
1067
pp->p_flags &= ~PPTIME;
1068
pp->p_flags |= PTIME;
1071
status = reason = -1;
1076
* The pipeline is reversed, so locate the real head of the pipeline
1077
* if pp is at the tail of a pipe (and not already in a pipeline)
1079
if ((pp->p_friends->p_flags & PPOU) && !inpipe && (flag & NAME)) {
1084
while (pp->p_friends->p_flags & PPOU);
1088
* pmarker is used to hold the place of the proc being processed, so
1089
* we can search for the next one downstream later.
1092
pcond = (int) (tp != pp || (inpipe && tp == pp));
1093
#else /* !BACKPIPE */
1094
pcond = (int) (tp != pp);
1095
#endif /* BACKPIPE */
1097
jobflags |= pp->p_flags;
1098
pstatus = (int) (pp->p_flags & PALLSTATES);
1099
if (pcond && linp != linbuf && !(flag & FANCY) &&
1100
((pstatus == status && pp->p_reason == reason) ||
1104
if (pcond && linp != linbuf)
1106
if (flag & NUMBER) {
1108
pcond = ((pp == tp && !inpipe) ||
1109
(inpipe && pipetail == tp && pp == pipehead));
1110
#else /* BACKPIPE */
1112
#endif /* BACKPIPE */
1114
xprintf("[%d]%s %c ", pp->p_index,
1115
pp->p_index < 10 ? " " : "",
1116
pp == pcurrent ? '+' :
1117
(pp == pprevious ? '-' : ' '));
1123
extern char *sitename();
1126
xprintf("%5d ", pp->p_procid);
1128
xprintf("%11s ", sitename(pp->p_procid));
1131
if (flag & (REASON | AREASON)) {
1136
if (pstatus == status) {
1137
if (pp->p_reason == reason) {
1138
xprintf(format, "");
1142
reason = (int) pp->p_reason;
1146
reason = (int) pp->p_reason;
1151
xprintf(format, CGETS(17, 4, "Running "));
1158
* tell what happened to the background job
1159
* From: Michael Schroeder
1160
* <mlschroe@immd4.informatik.uni-erlangen.de>
1163
|| ((flag & AREASON)
1165
&& (reason != SIGPIPE
1166
|| (pp->p_flags & PPOU) == 0))) {
1170
if ((ptr = mesg[pp->p_reason & ASCII].pname) == NULL)
1171
xsnprintf(ptr = buf, sizeof(buf), "%s %d",
1172
CGETS(17, 5, "Signal"), pp->p_reason & ASCII);
1173
xprintf(format, ptr);
1181
if (flag & REASON) {
1183
xprintf(CGETS(17, 6, "Exit %-25d"), pp->p_reason);
1185
xprintf(format, CGETS(17, 7, "Done"));
1190
xprintf(CGETS(17, 8, "BUG: status=%-9o"),
1197
xprintf("%S", pp->p_command);
1198
if (pp->p_flags & PPOU)
1200
if (pp->p_flags & PDIAG)
1203
if (flag & (REASON | AREASON) && pp->p_flags & PDUMPED)
1204
xprintf(CGETS(17, 9, " (core dumped)"));
1205
if (tp == pp->p_friends) {
1206
if (flag & AMPERSAND)
1208
if (flag & JOBDIR &&
1209
!eq(tp->p_cwd->di_name, dcwd->di_name)) {
1210
xprintf(CGETS(17, 10, " (wd: "));
1211
dtildepr(tp->p_cwd->di_name);
1215
if (pp->p_flags & PPTIME && !(status & (PSTOPPED | PRUNNING))) {
1218
#if defined(BSDTIMES) || defined(_SEQUENT_)
1219
prusage(&zru, &pp->p_rusage, &pp->p_etime,
1221
#else /* !BSDTIMES && !SEQUENT */
1222
lru.tms_utime = pp->p_utime;
1223
lru.tms_stime = pp->p_stime;
1226
prusage(&zru, &lru, pp->p_etime,
1228
#endif /* !BSDTIMES && !SEQUENT */
1232
pcond = ((tp == pp->p_friends && !inpipe) ||
1233
(inpipe && pipehead->p_friends == tp && pp == pipetail));
1234
#else /* !BACKPIPE */
1235
pcond = (tp == pp->p_friends);
1236
#endif /* BACKPIPE */
1240
if (flag & SHELLDIR && !eq(tp->p_cwd->di_name, dcwd->di_name)) {
1241
xprintf(CGETS(17, 11, "(wd now: "));
1242
dtildepr(dcwd->di_name);
1249
* if pmaker == pipetail, we are finished that pipeline, and
1250
* can now skip to past the head
1252
if (pmarker == pipetail) {
1258
* set pp to one before the one we want next, so the while below
1259
* increments to the correct spot.
1263
while (pp->p_friends->p_friends != pmarker);
1264
pmarker = pp->p_friends;
1267
pcond = ((pp = pp->p_friends) != tp || inpipe);
1268
#else /* !BACKPIPE */
1269
pcond = ((pp = pp->p_friends) != tp);
1270
#endif /* BACKPIPE */
1273
if (jobflags & PTIME && (jobflags & (PSTOPPED | PRUNNING)) == 0) {
1274
if (jobflags & NUMBER)
1282
* All 4.3 BSD derived implementations are buggy and I've had enough.
1283
* The following implementation produces similar code and works in all
1284
* cases. The 4.3BSD one works only for <, >, !=
1287
# define timercmp(tvp, uvp, cmp) \
1288
(((tvp)->tv_sec == (uvp)->tv_sec) ? \
1289
((tvp)->tv_usec cmp (uvp)->tv_usec) : \
1290
((tvp)->tv_sec cmp (uvp)->tv_sec))
1294
register struct process *tp;
1297
struct timeval tetime, diff;
1298
static struct timeval ztime;
1299
struct sysrusage ru;
1300
register struct process *pp = tp;
1305
ruadd(&ru, &pp->p_rusage);
1306
tvsub(&diff, &pp->p_etime, &pp->p_btime);
1307
if (timercmp(&diff, &tetime, >))
1309
} while ((pp = pp->p_friends) != tp);
1310
prusage(&zru, &ru, &tetime, &ztime);
1311
#else /* !BSDTIMES */
1313
timeval_t tetime, diff;
1314
static timeval_t ztime;
1315
struct process_stats ru;
1316
register struct process *pp = tp;
1321
ruadd(&ru, &pp->p_rusage);
1322
tvsub(&diff, &pp->p_etime, &pp->p_btime);
1323
if (timercmp(&diff, &tetime, >))
1325
} while ((pp = pp->p_friends) != tp);
1326
prusage(&zru, &ru, &tetime, &ztime);
1327
# else /* !_SEQUENT_ */
1329
static time_t ztime = 0;
1330
static time_t zu_time = 0;
1331
static time_t zs_time = 0;
1332
time_t tetime, diff;
1333
time_t u_time, s_time;
1336
static clock_t ztime = 0;
1337
static clock_t zu_time = 0;
1338
static clock_t zs_time = 0;
1339
clock_t tetime, diff;
1340
clock_t u_time, s_time;
1343
struct tms zts, rts;
1344
register struct process *pp = tp;
1350
u_time += pp->p_utime;
1351
s_time += pp->p_stime;
1352
diff = pp->p_etime - pp->p_btime;
1355
} while ((pp = pp->p_friends) != tp);
1356
zts.tms_utime = zu_time;
1357
zts.tms_stime = zs_time;
1360
rts.tms_utime = u_time;
1361
rts.tms_stime = s_time;
1364
prusage(&zts, &rts, tetime, ztime);
1365
# endif /* !_SEQUENT_ */
1366
#endif /* !BSDTIMES */
1370
* dojobs - print all jobs
1378
register struct process *pp;
1379
register int flag = NUMBER | NAME | REASON;
1386
if (v[1] || !eq(*v, STRml))
1388
flag |= FANCY | JOBDIR;
1390
for (i = 1; i <= pmaxindex; i++)
1391
for (pp = proclist.p_next; pp; pp = pp->p_next)
1392
if (pp->p_index == i && pp->p_procid == pp->p_jobid) {
1393
pp->p_flags &= ~PNEEDNOTE;
1394
if (!(pprint(pp, flag) & (PRUNNING | PSTOPPED)))
1401
* dofg - builtin - put the job into the foreground
1409
register struct process *pp;
1416
if (!pstart(pp, 1)) {
1418
stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1426
#endif /* !BSDSIGS */
1428
} while (*v && *++v);
1432
* %... - builtin - put the job into the foreground
1440
register struct process *pp;
1445
if (!pstart(pp, 1)) {
1447
stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1455
#endif /* !BSDSIGS */
1460
* dobg - builtin - put the job into the background
1468
register struct process *pp;
1475
if (!pstart(pp, 0)) {
1477
stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1479
} while (*v && *++v);
1483
* %... & - builtin - put the job into the background
1491
register struct process *pp;
1495
if (!pstart(pp, 0)) {
1497
stderror(ERR_NAME|ERR_BADJOB, pp->p_command, strerror(errno));
1502
* dostop - builtin - stop the job
1512
pkill(++v, SIGSTOP);
1513
#endif /* BSDJOBS */
1517
* dokill - builtin - superset of kill (1)
1525
register int signum, len = 0;
1526
register char *name;
1532
if (v[0] && v[0][0] == '-') {
1533
if (v[0][1] == 'l') {
1534
for (signum = 0; signum <= nsig; signum++) {
1535
if ((name = mesg[signum].iname) != NULL) {
1536
len += strlen(name) + 1;
1537
if (len >= T_Cols - 1) {
1539
len = strlen(name) + 1;
1541
xprintf("%s ", name);
1547
if (Isdigit(v[0][1])) {
1548
signum = atoi(short2str(v[0] + 1));
1549
if (signum < 0 || signum > (MAXSIG-1))
1550
stderror(ERR_NAME | ERR_BADSIG);
1553
for (signum = 0; signum <= nsig; signum++)
1554
if (mesg[signum].iname &&
1555
eq(&v[0][1], str2short(mesg[signum].iname)))
1557
setname(short2str(&v[0][1]));
1558
stderror(ERR_NAME | ERR_UNKSIG);
1573
register struct process *pp, *np;
1574
int jobflags = 0, err1 = 0;
1578
#endif /* BSDSIGS */
1582
omask = sigmask(SIGCHLD);
1584
omask |= sigmask(SIGINT);
1585
omask = sigblock(omask) & ~omask;
1586
#else /* !BSDSIGS */
1588
(void) sighold(SIGINT);
1589
(void) sighold(SIGCHLD);
1590
#endif /* !BSDSIGS */
1592
/* Avoid globbing %?x patterns */
1593
for (vp = v; vp && *vp; vp++)
1597
gflag = 0, tglob(v);
1601
stderror(ERR_NAME | ERR_NOMATCH);
1604
v = gargv = saveblk(v);
1609
while (v && (cp = *v)) {
1611
np = pp = pfind(cp);
1613
jobflags |= np->p_flags;
1614
while ((np = np->p_friends) != pp);
1622
if ((jobflags & PRUNNING) == 0) {
1624
xprintf(CGETS(17, 12, "%S: Already suspended\n"), cp);
1625
# else /* !SUSPENDED */
1626
xprintf(CGETS(17, 13, "%S: Already stopped\n"), cp);
1627
# endif /* !SUSPENDED */
1633
* suspend a process, kill -CONT %, then type jobs; the shell
1634
* says it is suspended, but it is running; thanks jaap..
1637
if (!pstart(pp, 0)) {
1639
stderror(ERR_NAME|ERR_BADJOB, pp->p_command,
1646
#endif /* BSDJOBS */
1647
if (killpg(pp->p_jobid, signum) < 0) {
1648
xprintf("%S: %s\n", cp, strerror(errno));
1652
if (signum == SIGTERM || signum == SIGHUP)
1653
(void) killpg(pp->p_jobid, SIGCONT);
1654
#endif /* BSDJOBS */
1656
else if (!(Isdigit(*cp) || *cp == '-'))
1657
stderror(ERR_NAME | ERR_JOBARGS);
1660
pid = atoi(short2str(cp));
1662
pid = strtoul(short2str(cp),NULL,0);
1664
if (kill(pid, signum) < 0) {
1665
xprintf("%d: %s\n", pid, strerror(errno));
1670
if (signum == SIGTERM || signum == SIGHUP)
1671
(void) kill(pid, SIGCONT);
1672
#endif /* BSDJOBS */
1678
blkfree(gargv), gargv = 0;
1680
(void) sigsetmask(omask);
1681
#else /* !BSDSIGS */
1682
(void) sigrelse(SIGCHLD);
1684
(void) sigrelse(SIGINT);
1685
#endif /* !BSDSIGS */
1687
stderror(ERR_SILENT);
1691
* pstart - start the job in foreground/background
1695
register struct process *pp;
1699
register struct process *np;
1702
#endif /* BSDSIGS */
1703
/* We don't use jobflags in this function right now (see below) */
1704
/* long jobflags = 0; */
1707
omask = sigblock(sigmask(SIGCHLD));
1708
#else /* !BSDSIGS */
1709
(void) sighold(SIGCHLD);
1713
/* We don't use jobflags in this function right now (see below) */
1714
/* jobflags |= np->p_flags; */
1715
if (np->p_flags & (PRUNNING | PSTOPPED)) {
1716
np->p_flags |= PRUNNING;
1717
np->p_flags &= ~PSTOPPED;
1719
np->p_flags |= PFOREGND;
1721
np->p_flags &= ~PFOREGND;
1723
} while ((np = np->p_friends) != pp);
1726
(void) pprint(pp, foregnd ? NAME | JOBDIR : NUMBER | NAME | AMPERSAND);
1729
rv = tcsetpgrp(FSHTTY, pp->p_jobid);
1732
* 1. child process of csh (shell script) receives SIGTTIN/SIGTTOU
1733
* 2. parent process (csh) receives SIGCHLD
1734
* 3. The "csh" signal handling function pchild() is invoked
1735
* with a SIGCHLD signal.
1736
* 4. pchild() calls wait3(WNOHANG) which returns 0.
1737
* The child process is NOT ready to be waited for at this time.
1738
* pchild() returns without picking-up the correct status
1739
* for the child process which generated the SIGCHILD.
1740
* 5. CONSEQUENCE : csh is UNaware that the process is stopped
1741
* 6. THIS LINE HAS BEEN COMMENTED OUT : if (jobflags&PSTOPPED)
1742
* (beto@aixwiz.austin.ibm.com - aug/03/91)
1743
* 7. I removed the line completely and added extra checks for
1744
* pstart, so that if a job gets attached to and dies inside
1745
* a debugger it does not confuse the shell. [christos]
1746
* 8. on the nec sx-4 there seems to be a problem, which requires
1747
* a syscall(151, getpid(), getpid()) in osinit. Don't ask me
1748
* what this is doing. [schott@rzg.mpg.de]
1752
rv = killpg(pp->p_jobid, SIGCONT);
1753
#endif /* BSDJOBS */
1755
(void) sigsetmask(omask);
1756
#else /* !BSDSIGS */
1757
(void) sigrelse(SIGCHLD);
1758
#endif /* !BSDSIGS */
1766
register struct process *pp;
1769
for (pp = proclist.p_next; pp; pp = pp->p_next)
1770
if (pp->p_flags & PSTOPPED)
1771
stderror(ERR_STOPPED, neednl ? "\n" : "");
1778
register struct process *pp, *np;
1780
if (cp == 0 || cp[1] == 0 || eq(cp, STRcent2) || eq(cp, STRcentplus)) {
1781
if (pcurrent == NULL)
1782
stderror(ERR_NAME | ERR_JOBCUR);
1785
if (eq(cp, STRcentminus) || eq(cp, STRcenthash)) {
1786
if (pprevious == NULL)
1787
stderror(ERR_NAME | ERR_JOBPREV);
1790
if (Isdigit(cp[1])) {
1791
int idx = atoi(short2str(cp + 1));
1793
for (pp = proclist.p_next; pp; pp = pp->p_next)
1794
if (pp->p_index == idx && pp->p_procid == pp->p_jobid)
1796
stderror(ERR_NAME | ERR_NOSUCHJOB);
1799
for (pp = proclist.p_next; pp; pp = pp->p_next)
1800
if (pp->p_procid == pp->p_jobid) {
1804
for (dp = pp->p_command; *dp; dp++) {
1807
if (prefix(cp + 2, dp))
1811
else if (prefix(cp + 1, pp->p_command)) {
1814
stderror(ERR_NAME | ERR_AMBIG);
1820
stderror(ERR_NAME | (cp[1] == '?' ? ERR_JOBPAT : ERR_NOSUCHJOB));
1827
* pgetcurr - find most recent job that is not pp, preferably stopped
1829
static struct process *
1831
register struct process *pp;
1833
register struct process *np;
1834
register struct process *xp = NULL;
1836
for (np = proclist.p_next; np; np = np->p_next)
1837
if (np != pcurrent && np != pp && np->p_procid &&
1838
np->p_procid == np->p_jobid) {
1839
if (np->p_flags & PSTOPPED)
1848
* donotify - flag the job so as to report termination asynchronously
1856
register struct process *pp;
1860
pp->p_flags |= PNOTIFY;
1864
* Do the fork and whatever should be done in the child side that
1865
* should not be done if we are not forking at all (like for simple builtin's)
1866
* Also do everything that needs any signals fiddled with in the parent side
1868
* Wanttty tells whether process and/or tty pgrps are to be manipulated:
1869
* -1: leave tty alone; inherit pgrp from parent
1870
* 0: already have tty; manipulate process pgrps only
1871
* 1: want to claim tty; manipulate process and tty pgrps
1872
* It is usually just the value of tpgrp.
1877
struct command *t; /* command we are forking for */
1884
sigmask_t omask = 0;
1885
#endif /* BSDSIGS */
1888
static sigvec_t nsv = {synch_handler, (sigset_t) ~0, 0};
1889
#endif /* SIGSYNCH */
1892
* A child will be uninterruptible only under very special conditions.
1893
* Remember that the semantics of '&' is implemented by disconnecting the
1894
* process from the tty so signals do not need to ignored just for '&'.
1895
* Thus signals are set to default action for children unless: we have had
1896
* an "onintr -" (then specifically ignored) we are not playing with
1897
* signals (inherit action)
1900
ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
1901
|| (gointr && eq(gointr, STRminus));
1904
ignint |= gointr && eq(gointr, STRminus);
1905
#endif /* COHERENT */
1908
* Check for maximum nesting of 16 processes to avoid Forking loops
1911
stderror(ERR_NESTING, 16);
1913
if (mysigvec(SIGSYNCH, &nsv, &osv))
1914
stderror(ERR_SYSTEM, "pfork: sigvec set", strerror(errno));
1915
#endif /* SIGSYNCH */
1917
* Hold SIGCHLD until we have the process installed in our table.
1921
omask = sigblock(sigmask(SIGCHLD));
1922
#else /* !BSDSIGS */
1923
(void) sighold(SIGCHLD);
1924
#endif /* !BSDSIGS */
1926
while ((pid = fork()) == -1)
1928
(void) sleep(FORKSLEEP);
1932
(void) sigsetmask(omask);
1933
#else /* !BSDSIGS */
1934
(void) sigrelse(SIGCHLD);
1935
(void) sigrelse(SIGINT);
1936
#endif /* !BSDSIGS */
1937
stderror(ERR_NOPROC);
1941
pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
1944
#if !defined(BSDTIMES) && !defined(_SEQUENT_)
1946
#endif /* !defined(BSDTIMES) && !defined(_SEQUENT_) */
1949
setintr = 0; /* until I think otherwise */
1952
(void) sigrelse(SIGCHLD);
1953
#endif /* !BSDSIGS */
1955
* Children just get blown away on SIGINT, SIGQUIT unless "onintr
1958
(void) signal(SIGINT, ignint ? SIG_IGN : SIG_DFL);
1959
(void) signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL);
1962
/* make stoppable */
1963
(void) signal(SIGTSTP, SIG_DFL);
1964
(void) signal(SIGTTIN, SIG_DFL);
1965
(void) signal(SIGTTOU, SIG_DFL);
1967
#endif /* BSDJOBS */
1968
(void) signal(SIGTERM, parterm);
1970
else if (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT)) {
1971
(void) signal(SIGINT, SIG_IGN);
1972
(void) signal(SIGQUIT, SIG_IGN);
1975
sigignore(SIGIO); /* ignore SIGIO in child too */
1978
pgetty(wanttty, pgrp);
1980
* Nohup and nice apply only to NODE_COMMAND's but it would be nice
1981
* (?!?) if you could say "nohup (foo;bar)" Then the parser would have
1982
* to know about nice/nohup/time
1984
if (t->t_dflg & F_NOHUP)
1985
(void) signal(SIGHUP, SIG_IGN);
1986
if (t->t_dflg & F_NICE) {
1987
int nval = SIGN_EXTEND_CHAR(t->t_nice);
1989
(void) setpriority(PRIO_PROCESS, 0, nval);
1990
#else /* !BSDNICE */
1992
#endif /* !BSDNICE */
1995
if (t->t_dflg & F_VER) {
1996
tsetenv(STRSYSTYPE, t->t_systype ? STRbsd43 : STRsys53);
2001
/* rfw 8/89 now parent can continue */
2002
if (kill(getppid(), SIGSYNCH))
2003
stderror(ERR_SYSTEM, "pfork child: kill", strerror(errno));
2004
#endif /* SIGSYNCH */
2011
* `Walking' process group fix from Beto Appleton.
2012
* (beto@aixwiz.austin.ibm.com)
2013
* If setpgid fails at this point that means that
2014
* our process leader has died. We flush the current
2015
* job and become the process leader ourselves.
2016
* The parent will figure that out later.
2018
pgrp = pcurrjob ? pcurrjob->p_jobid : pid;
2019
if (setpgid(pid, pgrp) == -1 && errno == EPERM) {
2022
* We don't care if this causes an error here;
2023
* then we are already in the right process group
2025
(void) setpgid(pid, pgrp = pid);
2028
#endif /* POSIXJOBS */
2032
* rfw 8/89 Wait for child to own terminal. Solves half of ugly
2033
* synchronization problem. With this change, we know that the only
2034
* reason setpgrp to a previous process in a pipeline can fail is that
2035
* the previous process has already exited. Without this hack, he may
2036
* either have exited or not yet started to run. Two uglies become
2039
(void) sigpause(omask & ~SYNCHMASK);
2040
if (mysigvec(SIGSYNCH, &osv, NULL))
2041
stderror(ERR_SYSTEM, "pfork parent: sigvec restore",
2043
#endif /* SIGSYNCH */
2047
(void) sigsetmask(omask);
2048
#else /* !BSDSIGS */
2049
(void) sigrelse(SIGCHLD);
2050
#endif /* !BSDSIGS */
2060
stderror(ERR_JOBCONTROL);
2062
stderror(ERR_JOBCTRLSUB);
2071
* If we are piping out a builtin, eg. 'echo | more' things can go
2072
* out of sequence, i.e. the more can run before the echo. This
2073
* can happen even if we have vfork, since the echo will be forked
2074
* with the regular fork. In this case, we need to set the tty
2075
* pgrp ourselves. If that happens, then the process will be still
2076
* alive. And the tty process group will already be set.
2077
* This should fix the famous sequent problem as a side effect:
2078
* The controlling terminal is lost if all processes in the
2079
* terminal process group are zombies. In this case tcgetpgrp()
2080
* returns 0. If this happens we must set the terminal process
2083
if (tcgetpgrp(FSHTTY) != pgrp) {
2086
* tcsetpgrp will set SIGTTOU to all the the processes in
2087
* the background according to POSIX... We ignore this here.
2089
signalfun_t old = sigset(SIGTTOU, SIG_IGN);
2091
(void) tcsetpgrp(FSHTTY, pgrp);
2093
(void) sigset(SIGTTOU, old);
2101
* if we don't have vfork(), things can still go in the wrong order
2102
* resulting in the famous 'Stopped (tty output)'. But some systems
2103
* don't permit the setpgid() call, (these are more recent secure
2104
* systems such as ibm's aix), when they do. Then we'd rather print
2105
* an error message than hang the shell!
2106
* I am open to suggestions how to fix that.
2109
pgetty(wanttty, pgrp)
2113
# if defined(BSDSIGS) && defined(POSIXJOBS)
2114
sigmask_t omask = 0;
2115
# endif /* BSDSIGS && POSIXJOBS */
2118
xprintf("wanttty %d pid %d opgrp%d pgrp %d tpgrp %d\n",
2119
wanttty, getpid(), pgrp, mygetpgrp(), tcgetpgrp(FSHTTY));
2120
# endif /* JOBDEBUG */
2123
* christos: I am blocking the tty signals till I've set things
2128
omask = sigblock(sigmask(SIGTSTP)|sigmask(SIGTTIN));
2129
# else /* !BSDSIGS */
2131
(void) sighold(SIGTSTP);
2132
(void) sighold(SIGTTIN);
2134
# endif /* !BSDSIGS */
2135
# endif /* POSIXJOBS */
2140
# endif /* !POSIXJOBS */
2143
* From: Michael Schroeder <mlschroe@immd4.informatik.uni-erlangen.de>
2144
* Don't check for tpgrp >= 0 so even non-interactive shells give
2145
* background jobs process groups Same for the comparison in the other part
2149
if (setpgid(0, pgrp) == -1) {
2151
/* Walking process group fix; see above */
2152
if (setpgid(0, pgrp = getpid()) == -1) {
2153
# endif /* POSIXJOBS */
2154
stderror(ERR_SYSTEM, "setpgid child:\n", strerror(errno));
2158
wanttty = pgrp; /* Now we really want the tty, since we became the
2159
* the process group leader
2161
# endif /* POSIXJOBS */
2169
(void) sigsetmask(omask);
2170
# else /* BSDSIGS */
2171
(void) sigrelse(SIGTSTP);
2172
(void) sigrelse(SIGTTIN);
2173
# endif /* !BSDSIGS */
2174
# endif /* POSIXJOBS */
2177
xprintf("wanttty %d pid %d pgrp %d tpgrp %d\n",
2178
wanttty, getpid(), mygetpgrp(), tcgetpgrp(FSHTTY));
2179
# endif /* JOBDEBUG */
2182
tpgrp = 0; /* gave tty away */
2183
#endif /* BSDJOBS */