proc.c revision 1.26 1 /* $NetBSD: proc.c,v 1.26 2003/02/08 19:40:30 christos Exp $ */
2
3 /*-
4 * Copyright (c) 1980, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)proc.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: proc.c,v 1.26 2003/02/08 19:40:30 christos Exp $");
42 #endif
43 #endif /* not lint */
44
45 #include <sys/types.h>
46 #include <sys/wait.h>
47
48 #include <errno.h>
49 #include <stdarg.h>
50 #include <stdlib.h>
51 #include <string.h>
52 #include <unistd.h>
53
54 #include "csh.h"
55 #include "dir.h"
56 #include "extern.h"
57 #include "proc.h"
58
59 #define BIGINDEX 9 /* largest desirable job index */
60
61 extern int insource;
62
63 static void pflushall(void);
64 static void pflush(struct process *);
65 static void pclrcurr(struct process *);
66 static void padd(struct command *);
67 static int pprint(struct process *, int);
68 static void ptprint(struct process *);
69 static void pads(Char *);
70 static void pkill(Char **v, int);
71 static struct process *pgetcurr(struct process *);
72 static void okpcntl(void);
73
74 /*
75 * pchild - called at interrupt level by the SIGCHLD signal
76 * indicating that at least one child has terminated or stopped
77 * thus at least one wait system call will definitely return a
78 * childs status. Top level routines (like pwait) must be sure
79 * to mask interrupts when playing with the proclist data structures!
80 */
81 /* ARGSUSED */
82 void
83 pchild(int notused)
84 {
85 struct rusage ru;
86 struct process *fp, *pp;
87 int jobflags, pid, w;
88
89 loop:
90 errno = 0; /* reset, just in case */
91 pid = wait3(&w,
92 (setintr && (intty || insource) ? WNOHANG | WUNTRACED : WNOHANG), &ru);
93
94 if (pid <= 0) {
95 if (errno == EINTR) {
96 errno = 0;
97 goto loop;
98 }
99 pnoprocesses = pid == -1;
100 return;
101 }
102 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
103 if (pid == pp->p_pid)
104 goto found;
105 goto loop;
106 found:
107 if (pid == atoi(short2str(value(STRchild))))
108 unsetv(STRchild);
109 pp->p_flags &= ~(PRUNNING | PSTOPPED | PREPORTED);
110 if (WIFSTOPPED(w)) {
111 pp->p_flags |= PSTOPPED;
112 pp->p_reason = WSTOPSIG(w);
113 }
114 else {
115 if (pp->p_flags & (PTIME | PPTIME) || adrof(STRtime))
116 (void)gettimeofday(&pp->p_etime, NULL);
117
118 pp->p_rusage = ru;
119 if (WIFSIGNALED(w)) {
120 if (WTERMSIG(w) == SIGINT)
121 pp->p_flags |= PINTERRUPTED;
122 else
123 pp->p_flags |= PSIGNALED;
124 if (WCOREDUMP(w))
125 pp->p_flags |= PDUMPED;
126 pp->p_reason = WTERMSIG(w);
127 }
128 else {
129 pp->p_reason = WEXITSTATUS(w);
130 if (pp->p_reason != 0)
131 pp->p_flags |= PAEXITED;
132 else
133 pp->p_flags |= PNEXITED;
134 }
135 }
136 jobflags = 0;
137 fp = pp;
138 do {
139 if ((fp->p_flags & (PPTIME | PRUNNING | PSTOPPED)) == 0 &&
140 !child && adrof(STRtime) &&
141 fp->p_rusage.ru_utime.tv_sec + fp->p_rusage.ru_stime.tv_sec
142 >= atoi(short2str(value(STRtime))))
143 fp->p_flags |= PTIME;
144 jobflags |= fp->p_flags;
145 } while ((fp = fp->p_friends) != pp);
146 pp->p_flags &= ~PFOREGND;
147 if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
148 pp->p_flags &= ~PPTIME;
149 pp->p_flags |= PTIME;
150 }
151 if ((jobflags & (PRUNNING | PREPORTED)) == 0) {
152 fp = pp;
153 do {
154 if (fp->p_flags & PSTOPPED)
155 fp->p_flags |= PREPORTED;
156 } while ((fp = fp->p_friends) != pp);
157 while (fp->p_pid != fp->p_jobid)
158 fp = fp->p_friends;
159 if (jobflags & PSTOPPED) {
160 if (pcurrent && pcurrent != fp)
161 pprevious = pcurrent;
162 pcurrent = fp;
163 }
164 else
165 pclrcurr(fp);
166 if (jobflags & PFOREGND) {
167 if (jobflags & (PSIGNALED | PSTOPPED | PPTIME) ||
168 #ifdef IIASA
169 jobflags & PAEXITED ||
170 #endif
171 !eq(dcwd->di_name, fp->p_cwd->di_name)) {
172 ; /* print in pjwait */
173 }
174 /* PWP: print a newline after ^C */
175 else if (jobflags & PINTERRUPTED) {
176 (void)vis_fputc('\r' | QUOTE, cshout);
177 (void)fputc('\n', cshout);
178 }
179 }
180 else {
181 if (jobflags & PNOTIFY || adrof(STRnotify)) {
182 (void)vis_fputc('\r' | QUOTE, cshout);
183 (void)fputc('\n', cshout);
184 (void)pprint(pp, NUMBER | NAME | REASON);
185 if ((jobflags & PSTOPPED) == 0)
186 pflush(pp);
187 }
188 else {
189 fp->p_flags |= PNEEDNOTE;
190 neednote++;
191 }
192 }
193 }
194 goto loop;
195 }
196
197 void
198 pnote(void)
199 {
200 struct process *pp;
201 sigset_t osigset, nsigset;
202 int flags;
203
204 neednote = 0;
205 sigemptyset(&nsigset);
206 (void)sigaddset(&nsigset, SIGCHLD);
207 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next) {
208 if (pp->p_flags & PNEEDNOTE) {
209 (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
210 pp->p_flags &= ~PNEEDNOTE;
211 flags = pprint(pp, NUMBER | NAME | REASON);
212 if ((flags & (PRUNNING | PSTOPPED)) == 0)
213 pflush(pp);
214 (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
215 }
216 }
217 }
218
219 /*
220 * pwait - wait for current job to terminate, maintaining integrity
221 * of current and previous job indicators.
222 */
223 void
224 pwait(void)
225 {
226 struct process *fp, *pp;
227 sigset_t osigset, nsigset;
228
229 /*
230 * Here's where dead procs get flushed.
231 */
232 sigemptyset(&nsigset);
233 (void)sigaddset(&nsigset, SIGCHLD);
234 (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
235 for (pp = (fp = &proclist)->p_next; pp != NULL; pp = (fp = pp)->p_next)
236 if (pp->p_pid == 0) {
237 fp->p_next = pp->p_next;
238 xfree((ptr_t) pp->p_command);
239 if (pp->p_cwd && --pp->p_cwd->di_count == 0)
240 if (pp->p_cwd->di_next == 0)
241 dfree(pp->p_cwd);
242 xfree((ptr_t) pp);
243 pp = fp;
244 }
245 (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
246 pjwait(pcurrjob);
247 }
248
249
250 /*
251 * pjwait - wait for a job to finish or become stopped
252 * It is assumed to be in the foreground state (PFOREGND)
253 */
254 void
255 pjwait(struct process *pp)
256 {
257 struct process *fp;
258 sigset_t osigset, nsigset;
259 int jobflags, reason;
260
261 while (pp->p_pid != pp->p_jobid)
262 pp = pp->p_friends;
263 fp = pp;
264
265 do {
266 if ((fp->p_flags & (PFOREGND | PRUNNING)) == PRUNNING)
267 (void)fprintf(csherr, "BUG: waiting for background job!\n");
268 } while ((fp = fp->p_friends) != pp);
269 /*
270 * Now keep pausing as long as we are not interrupted (SIGINT), and the
271 * target process, or any of its friends, are running
272 */
273 fp = pp;
274 sigemptyset(&nsigset);
275 (void)sigaddset(&nsigset, SIGCHLD);
276 (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
277 for (;;) {
278 sigemptyset(&nsigset);
279 (void)sigaddset(&nsigset, SIGCHLD);
280 (void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
281 jobflags = 0;
282 do
283 jobflags |= fp->p_flags;
284 while ((fp = (fp->p_friends)) != pp);
285 if ((jobflags & PRUNNING) == 0)
286 break;
287 #ifdef JOBDEBUG
288 (void)fprintf(csherr, "starting to sigsuspend for SIGCHLD on %d\n",
289 fp->p_pid);
290 #endif /* JOBDEBUG */
291 nsigset = osigset;
292 (void)sigdelset(&nsigset, SIGCHLD);
293 (void)sigsuspend(&nsigset);
294 }
295 (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
296 if (tpgrp > 0) /* get tty back */
297 (void)tcsetpgrp(FSHTTY, tpgrp);
298 if ((jobflags & (PSIGNALED | PSTOPPED | PTIME)) ||
299 !eq(dcwd->di_name, fp->p_cwd->di_name)) {
300 if (jobflags & PSTOPPED) {
301 (void) fputc('\n', cshout);
302 if (adrof(STRlistjobs)) {
303 Char *jobcommand[3];
304
305 jobcommand[0] = STRjobs;
306 if (eq(value(STRlistjobs), STRlong))
307 jobcommand[1] = STRml;
308 else
309 jobcommand[1] = NULL;
310 jobcommand[2] = NULL;
311
312 dojobs(jobcommand, NULL);
313 (void)pprint(pp, SHELLDIR);
314 }
315 else
316 (void)pprint(pp, AREASON | SHELLDIR);
317 }
318 else
319 (void)pprint(pp, AREASON | SHELLDIR);
320 }
321 if ((jobflags & (PINTERRUPTED | PSTOPPED)) && setintr &&
322 (!gointr || !eq(gointr, STRminus))) {
323 if ((jobflags & PSTOPPED) == 0)
324 pflush(pp);
325 pintr1(0);
326 }
327 reason = 0;
328 fp = pp;
329 do {
330 if (fp->p_reason)
331 reason = fp->p_flags & (PSIGNALED | PINTERRUPTED) ?
332 fp->p_reason | META : fp->p_reason;
333 } while ((fp = fp->p_friends) != pp);
334 if ((reason != 0) && (adrof(STRprintexitvalue))) {
335 (void)fprintf(cshout, "Exit %d\n", reason);
336 }
337 set(STRstatus, putn(reason));
338 if (reason && exiterr)
339 exitstat();
340 pflush(pp);
341 }
342
343 /*
344 * dowait - wait for all processes to finish
345 */
346 void
347 /*ARGSUSED*/
348 dowait(Char **v, struct command *t)
349 {
350 struct process *pp;
351 sigset_t osigset, nsigset;
352
353 pjobs++;
354 sigemptyset(&nsigset);
355 (void)sigaddset(&nsigset, SIGCHLD);
356 (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
357 loop:
358 for (pp = proclist.p_next; pp; pp = pp->p_next)
359 if (pp->p_pid && /* pp->p_pid == pp->p_jobid && */
360 pp->p_flags & PRUNNING) {
361 sigemptyset(&nsigset);
362 (void)sigsuspend(&nsigset);
363 goto loop;
364 }
365 (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
366 pjobs = 0;
367 }
368
369 /*
370 * pflushall - flush all jobs from list (e.g. at fork())
371 */
372 static void
373 pflushall(void)
374 {
375 struct process *pp;
376
377 for (pp = proclist.p_next; pp != NULL; pp = pp->p_next)
378 if (pp->p_pid)
379 pflush(pp);
380 }
381
382 /*
383 * pflush - flag all process structures in the same job as the
384 * the argument process for deletion. The actual free of the
385 * space is not done here since pflush is called at interrupt level.
386 */
387 static void
388 pflush(struct process *pp)
389 {
390 struct process *np;
391 int idx;
392
393 if (pp->p_pid == 0) {
394 (void)fprintf(csherr, "BUG: process flushed twice");
395 return;
396 }
397 while (pp->p_pid != pp->p_jobid)
398 pp = pp->p_friends;
399 pclrcurr(pp);
400 if (pp == pcurrjob)
401 pcurrjob = 0;
402 idx = pp->p_index;
403 np = pp;
404 do {
405 np->p_index = np->p_pid = 0;
406 np->p_flags &= ~PNEEDNOTE;
407 } while ((np = np->p_friends) != pp);
408 if (idx == pmaxindex) {
409 for (np = proclist.p_next, idx = 0; np; np = np->p_next)
410 if (np->p_index > idx)
411 idx = np->p_index;
412 pmaxindex = idx;
413 }
414 }
415
416 /*
417 * pclrcurr - make sure the given job is not the current or previous job;
418 * pp MUST be the job leader
419 */
420 static void
421 pclrcurr(struct process *pp)
422 {
423 if (pp == pcurrent) {
424 if (pprevious != NULL) {
425 pcurrent = pprevious;
426 pprevious = pgetcurr(pp);
427 }
428 else {
429 pcurrent = pgetcurr(pp);
430 pprevious = pgetcurr(pp);
431 }
432 } else if (pp == pprevious)
433 pprevious = pgetcurr(pp);
434 }
435
436 /* +4 here is 1 for '\0', 1 ea for << >& >> */
437 static Char command[PMAXLEN + 4];
438 static int cmdlen;
439 static Char *cmdp;
440
441 /*
442 * palloc - allocate a process structure and fill it up.
443 * an important assumption is made that the process is running.
444 */
445 void
446 palloc(int pid, struct command *t)
447 {
448 struct process *pp;
449 int i;
450
451 pp = (struct process *)xcalloc(1, (size_t)sizeof(struct process));
452 pp->p_pid = pid;
453 pp->p_flags = t->t_dflg & F_AMPERSAND ? PRUNNING : PRUNNING | PFOREGND;
454 if (t->t_dflg & F_TIME)
455 pp->p_flags |= PPTIME;
456 cmdp = command;
457 cmdlen = 0;
458 padd(t);
459 *cmdp++ = 0;
460 if (t->t_dflg & F_PIPEOUT) {
461 pp->p_flags |= PPOU;
462 if (t->t_dflg & F_STDERR)
463 pp->p_flags |= PERR;
464 }
465 pp->p_command = Strsave(command);
466 if (pcurrjob) {
467 struct process *fp;
468
469 /* careful here with interrupt level */
470 pp->p_cwd = 0;
471 pp->p_index = pcurrjob->p_index;
472 pp->p_friends = pcurrjob;
473 pp->p_jobid = pcurrjob->p_pid;
474 for (fp = pcurrjob; fp->p_friends != pcurrjob; fp = fp->p_friends)
475 continue;
476 fp->p_friends = pp;
477 }
478 else {
479 pcurrjob = pp;
480 pp->p_jobid = pid;
481 pp->p_friends = pp;
482 pp->p_cwd = dcwd;
483 dcwd->di_count++;
484 if (pmaxindex < BIGINDEX)
485 pp->p_index = ++pmaxindex;
486 else {
487 struct process *np;
488
489 for (i = 1;; i++) {
490 for (np = proclist.p_next; np; np = np->p_next)
491 if (np->p_index == i)
492 goto tryagain;
493 pp->p_index = i;
494 if (i > pmaxindex)
495 pmaxindex = i;
496 break;
497 tryagain:;
498 }
499 }
500 if (pcurrent == NULL)
501 pcurrent = pp;
502 else if (pprevious == NULL)
503 pprevious = pp;
504 }
505 pp->p_next = proclist.p_next;
506 proclist.p_next = pp;
507 (void)gettimeofday(&pp->p_btime, NULL);
508 }
509
510 static void
511 padd(struct command *t)
512 {
513 Char **argp;
514
515 if (t == 0)
516 return;
517 switch (t->t_dtyp) {
518 case NODE_PAREN:
519 pads(STRLparensp);
520 padd(t->t_dspr);
521 pads(STRspRparen);
522 break;
523 case NODE_COMMAND:
524 for (argp = t->t_dcom; *argp; argp++) {
525 pads(*argp);
526 if (argp[1])
527 pads(STRspace);
528 }
529 break;
530 case NODE_OR:
531 case NODE_AND:
532 case NODE_PIPE:
533 case NODE_LIST:
534 padd(t->t_dcar);
535 switch (t->t_dtyp) {
536 case NODE_OR:
537 pads(STRspor2sp);
538 break;
539 case NODE_AND:
540 pads(STRspand2sp);
541 break;
542 case NODE_PIPE:
543 pads(STRsporsp);
544 break;
545 case NODE_LIST:
546 pads(STRsemisp);
547 break;
548 }
549 padd(t->t_dcdr);
550 return;
551 }
552 if ((t->t_dflg & F_PIPEIN) == 0 && t->t_dlef) {
553 pads((t->t_dflg & F_READ) ? STRspLarrow2sp : STRspLarrowsp);
554 pads(t->t_dlef);
555 }
556 if ((t->t_dflg & F_PIPEOUT) == 0 && t->t_drit) {
557 pads((t->t_dflg & F_APPEND) ? STRspRarrow2 : STRspRarrow);
558 if (t->t_dflg & F_STDERR)
559 pads(STRand);
560 pads(STRspace);
561 pads(t->t_drit);
562 }
563 }
564
565 static void
566 pads(Char *cp)
567 {
568 int i;
569
570 /*
571 * Avoid the Quoted Space alias hack! Reported by:
572 * sam (at) john-bigboote.ICS.UCI.EDU (Sam Horrocks)
573 */
574 if (cp[0] == STRQNULL[0])
575 cp++;
576
577 i = Strlen(cp);
578
579 if (cmdlen >= PMAXLEN)
580 return;
581 if (cmdlen + i >= PMAXLEN) {
582 (void)Strcpy(cmdp, STRsp3dots);
583 cmdlen = PMAXLEN;
584 cmdp += 4;
585 return;
586 }
587 (void)Strcpy(cmdp, cp);
588 cmdp += i;
589 cmdlen += i;
590 }
591
592 /*
593 * psavejob - temporarily save the current job on a one level stack
594 * so another job can be created. Used for { } in exp6
595 * and `` in globbing.
596 */
597 void
598 psavejob(void)
599 {
600 pholdjob = pcurrjob;
601 pcurrjob = NULL;
602 }
603
604 /*
605 * prestjob - opposite of psavejob. This may be missed if we are interrupted
606 * somewhere, but pendjob cleans up anyway.
607 */
608 void
609 prestjob(void)
610 {
611 pcurrjob = pholdjob;
612 pholdjob = NULL;
613 }
614
615 /*
616 * pendjob - indicate that a job (set of commands) has been completed
617 * or is about to begin.
618 */
619 void
620 pendjob(void)
621 {
622 struct process *pp, *tp;
623
624 if (pcurrjob && (pcurrjob->p_flags & (PFOREGND | PSTOPPED)) == 0) {
625 pp = pcurrjob;
626 while (pp->p_pid != pp->p_jobid)
627 pp = pp->p_friends;
628 (void)fprintf(cshout, "[%d]", pp->p_index);
629 tp = pp;
630 do {
631 (void)fprintf(cshout, " %ld", (long)pp->p_pid);
632 pp = pp->p_friends;
633 } while (pp != tp);
634 (void)fputc('\n', cshout);
635 }
636 pholdjob = pcurrjob = 0;
637 }
638
639 /*
640 * pprint - print a job
641 */
642 static int
643 pprint(struct process *pp, bool flag)
644 {
645 static struct rusage zru;
646 struct process *tp;
647 char *format;
648 int jobflags, pstatus, reason, status;
649 bool hadnl;
650
651 hadnl = 1; /* did we just have a newline */
652 (void)fpurge(cshout);
653
654 while (pp->p_pid != pp->p_jobid)
655 pp = pp->p_friends;
656 if (pp == pp->p_friends && (pp->p_flags & PPTIME)) {
657 pp->p_flags &= ~PPTIME;
658 pp->p_flags |= PTIME;
659 }
660 tp = pp;
661 status = reason = -1;
662 jobflags = 0;
663 do {
664 jobflags |= pp->p_flags;
665 pstatus = pp->p_flags & PALLSTATES;
666 if (tp != pp && !hadnl && !(flag & FANCY) &&
667 ((pstatus == status && pp->p_reason == reason) ||
668 !(flag & REASON))) {
669 (void)fputc(' ', cshout);
670 hadnl = 0;
671 }
672 else {
673 if (tp != pp && !hadnl) {
674 (void)fputc('\n', cshout);
675 hadnl = 1;
676 }
677 if (flag & NUMBER) {
678 if (pp == tp)
679 (void)fprintf(cshout, "[%d]%s %c ", pp->p_index,
680 pp->p_index < 10 ? " " : "",
681 pp == pcurrent ? '+' :
682 (pp == pprevious ? '-' : ' '));
683 else
684 (void)fprintf(cshout, " ");
685 hadnl = 0;
686 }
687 if (flag & FANCY) {
688 (void)fprintf(cshout, "%5ld ", (long)pp->p_pid);
689 hadnl = 0;
690 }
691 if (flag & (REASON | AREASON)) {
692 if (flag & NAME)
693 format = "%-23s";
694 else
695 format = "%s";
696 if (pstatus == status) {
697 if (pp->p_reason == reason) {
698 (void)fprintf(cshout, format, "");
699 hadnl = 0;
700 goto prcomd;
701 }
702 else
703 reason = pp->p_reason;
704 } else {
705 status = pstatus;
706 reason = pp->p_reason;
707 }
708 switch (status) {
709 case PRUNNING:
710 (void)fprintf(cshout, format, "Running ");
711 hadnl = 0;
712 break;
713 case PINTERRUPTED:
714 case PSTOPPED:
715 case PSIGNALED:
716 /*
717 * tell what happened to the background job
718 * From: Michael Schroeder
719 * <mlschroe (at) immd4.informatik.uni-erlangen.de>
720 */
721 if ((flag & REASON)
722 || ((flag & AREASON)
723 && reason != SIGINT
724 && (reason != SIGPIPE
725 || (pp->p_flags & PPOU) == 0))) {
726 (void)fprintf(cshout, format,
727 sys_siglist[(unsigned char)
728 pp->p_reason]);
729 hadnl = 0;
730 }
731 break;
732 case PNEXITED:
733 case PAEXITED:
734 if (flag & REASON) {
735 if (pp->p_reason)
736 (void)fprintf(cshout, "Exit %-18d", pp->p_reason);
737 else
738 (void)fprintf(cshout, format, "Done");
739 hadnl = 0;
740 }
741 break;
742 default:
743 (void)fprintf(csherr, "BUG: status=%-9o", status);
744 }
745 }
746 }
747 prcomd:
748 if (flag & NAME) {
749 (void)fprintf(cshout, "%s", vis_str(pp->p_command));
750 if (pp->p_flags & PPOU)
751 (void)fprintf(cshout, " |");
752 if (pp->p_flags & PERR)
753 (void)fputc('&', cshout);
754 hadnl = 0;
755 }
756 if (flag & (REASON | AREASON) && pp->p_flags & PDUMPED) {
757 (void)fprintf(cshout, " (core dumped)");
758 hadnl = 0;
759 }
760 if (tp == pp->p_friends) {
761 if (flag & AMPERSAND) {
762 (void)fprintf(cshout, " &");
763 hadnl = 0;
764 }
765 if (flag & JOBDIR &&
766 !eq(tp->p_cwd->di_name, dcwd->di_name)) {
767 (void)fprintf(cshout, " (wd: ");
768 dtildepr(value(STRhome), tp->p_cwd->di_name);
769 (void)fputc(')', cshout);
770 hadnl = 0;
771 }
772 }
773 if (pp->p_flags & PPTIME && !(status & (PSTOPPED | PRUNNING))) {
774 if (!hadnl)
775 (void)fprintf(cshout, "\n\t");
776 prusage(&zru, &pp->p_rusage, &pp->p_etime,
777 &pp->p_btime);
778 hadnl = 1;
779 }
780 if (tp == pp->p_friends) {
781 if (!hadnl) {
782 (void)fputc('\n', cshout);
783 hadnl = 1;
784 }
785 if (flag & SHELLDIR && !eq(tp->p_cwd->di_name, dcwd->di_name)) {
786 (void)fprintf(cshout, "(wd now: ");
787 dtildepr(value(STRhome), dcwd->di_name);
788 (void)fprintf(cshout, ")\n");
789 hadnl = 1;
790 }
791 }
792 } while ((pp = pp->p_friends) != tp);
793 if (jobflags & PTIME && (jobflags & (PSTOPPED | PRUNNING)) == 0) {
794 if (jobflags & NUMBER)
795 (void)fprintf(cshout, " ");
796 ptprint(tp);
797 hadnl = 1;
798 }
799 (void)fflush(cshout);
800 return (jobflags);
801 }
802
803 static void
804 ptprint(struct process *tp)
805 {
806 static struct rusage zru;
807 static struct timeval ztime;
808 struct rusage ru;
809 struct timeval tetime, diff;
810 struct process *pp;
811
812 pp = tp;
813 ru = zru;
814 tetime = ztime;
815 do {
816 ruadd(&ru, &pp->p_rusage);
817 timersub(&pp->p_etime, &pp->p_btime, &diff);
818 if (timercmp(&diff, &tetime, >))
819 tetime = diff;
820 } while ((pp = pp->p_friends) != tp);
821 prusage(&zru, &ru, &tetime, &ztime);
822 }
823
824 /*
825 * dojobs - print all jobs
826 */
827 void
828 /*ARGSUSED*/
829 dojobs(Char **v, struct command *t)
830 {
831 struct process *pp;
832 int flag, i;
833
834 flag = NUMBER | NAME | REASON;
835 if (chkstop)
836 chkstop = 2;
837 if (*++v) {
838 if (v[1] || !eq(*v, STRml))
839 stderror(ERR_JOBS);
840 flag |= FANCY | JOBDIR;
841 }
842 for (i = 1; i <= pmaxindex; i++)
843 for (pp = proclist.p_next; pp; pp = pp->p_next)
844 if (pp->p_index == i && pp->p_pid == pp->p_jobid) {
845 pp->p_flags &= ~PNEEDNOTE;
846 if (!(pprint(pp, flag) & (PRUNNING | PSTOPPED)))
847 pflush(pp);
848 break;
849 }
850 }
851
852 /*
853 * dofg - builtin - put the job into the foreground
854 */
855 void
856 /*ARGSUSED*/
857 dofg(Char **v, struct command *t)
858 {
859 struct process *pp;
860
861 okpcntl();
862 ++v;
863 do {
864 pp = pfind(*v);
865 pstart(pp, 1);
866 pjwait(pp);
867 } while (*v && *++v);
868 }
869
870 /*
871 * %... - builtin - put the job into the foreground
872 */
873 void
874 /*ARGSUSED*/
875 dofg1(Char **v, struct command *t)
876 {
877 struct process *pp;
878
879 okpcntl();
880 pp = pfind(v[0]);
881 pstart(pp, 1);
882 pjwait(pp);
883 }
884
885 /*
886 * dobg - builtin - put the job into the background
887 */
888 void
889 /*ARGSUSED*/
890 dobg(Char **v, struct command *t)
891 {
892 struct process *pp;
893
894 okpcntl();
895 ++v;
896 do {
897 pp = pfind(*v);
898 pstart(pp, 0);
899 } while (*v && *++v);
900 }
901
902 /*
903 * %... & - builtin - put the job into the background
904 */
905 void
906 /*ARGSUSED*/
907 dobg1(Char **v, struct command *t)
908 {
909 struct process *pp;
910
911 pp = pfind(v[0]);
912 pstart(pp, 0);
913 }
914
915 /*
916 * dostop - builtin - stop the job
917 */
918 void
919 /*ARGSUSED*/
920 dostop(Char **v, struct command *t)
921 {
922 pkill(++v, SIGSTOP);
923 }
924
925 /*
926 * dokill - builtin - superset of kill (1)
927 */
928 void
929 /*ARGSUSED*/
930 dokill(Char **v, struct command *t)
931 {
932 Char *signame;
933 char *name;
934 int signum;
935 char *ep;
936
937 signum = SIGTERM;
938 v++;
939 if (v[0] && v[0][0] == '-') {
940 if (v[0][1] == 'l') {
941 if (v[1]) {
942 if (!Isdigit(v[1][0]))
943 stderror(ERR_NAME | ERR_BADSIG);
944
945 signum = strtol(short2str(v[1]), &ep, 10);
946 if (signum < 0 || signum >= NSIG)
947 stderror(ERR_NAME | ERR_BADSIG);
948 else if (signum == 0)
949 (void)fputc('0', cshout); /* 0's symbolic name is '0' */
950 else
951 (void)fprintf(cshout, "%s ", sys_signame[signum]);
952 } else {
953 for (signum = 1; signum < NSIG; signum++) {
954 (void)fprintf(cshout, "%s ", sys_signame[signum]);
955 if (signum == NSIG / 2)
956 (void)fputc('\n', cshout);
957 }
958 }
959 (void)fputc('\n', cshout);
960 return;
961 }
962 if (Isdigit(v[0][1])) {
963 signum = strtol(short2str(v[0] + 1), &ep, 10);
964 if (signum < 0 || signum > NSIG || *ep)
965 stderror(ERR_NAME | ERR_BADSIG);
966 }
967 else {
968 if (v[0][1] == 's' && v[0][2] == '\0')
969 signame = *(++v);
970 else
971 signame = &v[0][1];
972
973 if (signame == NULL || v[1] == NULL)
974 stderror(ERR_NAME | ERR_TOOFEW);
975
976 name = short2str(signame);
977 for (signum = 1; signum < NSIG; signum++)
978 if (!strcasecmp(sys_signame[signum], name) ||
979 (!strncasecmp("SIG", name, 3) && /* skip "SIG" prefix */
980 !strcasecmp(sys_signame[signum], name + 3)))
981 break;
982
983 if (signum == NSIG) {
984 if (signame[0] == '0')
985 signum = 0;
986 else {
987 setname(vis_str(signame));
988 stderror(ERR_NAME | ERR_UNKSIG);
989 }
990 }
991 }
992 v++;
993 }
994 pkill(v, signum);
995 }
996
997 static void
998 pkill(Char **v, int signum)
999 {
1000 struct process *pp, *np;
1001 Char *cp;
1002 sigset_t nsigset;
1003 int err1, jobflags, pid;
1004 char *ep;
1005
1006 jobflags = 0;
1007 err1 = 0;
1008 sigemptyset(&nsigset);
1009 (void)sigaddset(&nsigset, SIGCHLD);
1010 if (setintr)
1011 (void)sigaddset(&nsigset, SIGINT);
1012 (void)sigprocmask(SIG_BLOCK, &nsigset, NULL);
1013 gflag = 0, tglob(v);
1014 if (gflag) {
1015 v = globall(v);
1016 if (v == 0)
1017 stderror(ERR_NAME | ERR_NOMATCH);
1018 }
1019 else {
1020 v = gargv = saveblk(v);
1021 trim(v);
1022 }
1023
1024 while (v && (cp = *v)) {
1025 if (*cp == '%') {
1026 np = pp = pfind(cp);
1027 do
1028 jobflags |= np->p_flags;
1029 while ((np = np->p_friends) != pp);
1030 switch (signum) {
1031 case SIGSTOP:
1032 case SIGTSTP:
1033 case SIGTTIN:
1034 case SIGTTOU:
1035 if ((jobflags & PRUNNING) == 0) {
1036 (void)fprintf(csherr, "%s: Already suspended\n",
1037 vis_str(cp));
1038 err1++;
1039 goto cont;
1040 }
1041 break;
1042 /*
1043 * suspend a process, kill -CONT %, then type jobs; the shell
1044 * says it is suspended, but it is running; thanks jaap..
1045 */
1046 case SIGCONT:
1047 pstart(pp, 0);
1048 goto cont;
1049 }
1050 if (kill(-pp->p_jobid, signum) < 0) {
1051 (void)fprintf(csherr, "%s: %s\n", vis_str(cp),
1052 strerror(errno));
1053 err1++;
1054 }
1055 if (signum == SIGTERM || signum == SIGHUP)
1056 (void)kill(-pp->p_jobid, SIGCONT);
1057 }
1058 else if (!(Isdigit(*cp) || *cp == '-'))
1059 stderror(ERR_NAME | ERR_JOBARGS);
1060 else {
1061 pid = strtoul(short2str(cp), &ep, 0);
1062 if (*ep) {
1063 (void)fprintf(csherr, "%s: Badly formed number\n",
1064 short2str(cp));
1065 err1++;
1066 goto cont;
1067 } else if (kill(pid, signum) < 0) {
1068 (void)fprintf(csherr, "%d: %s\n", pid, strerror(errno));
1069 err1++;
1070 goto cont;
1071 }
1072 if (signum == SIGTERM || signum == SIGHUP)
1073 (void)kill((pid_t) pid, SIGCONT);
1074 }
1075 cont:
1076 v++;
1077 }
1078 if (gargv)
1079 blkfree(gargv), gargv = 0;
1080 (void)sigprocmask(SIG_UNBLOCK, &nsigset, NULL);
1081 if (err1)
1082 stderror(ERR_SILENT);
1083 }
1084
1085 /*
1086 * pstart - start the job in foreground/background
1087 */
1088 void
1089 pstart(struct process *pp, int foregnd)
1090 {
1091 struct process *np;
1092 sigset_t osigset, nsigset;
1093 long jobflags;
1094
1095 jobflags = 0;
1096 sigemptyset(&nsigset);
1097 (void)sigaddset(&nsigset, SIGCHLD);
1098 (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
1099 np = pp;
1100 do {
1101 jobflags |= np->p_flags;
1102 if (np->p_flags & (PRUNNING | PSTOPPED)) {
1103 np->p_flags |= PRUNNING;
1104 np->p_flags &= ~PSTOPPED;
1105 if (foregnd)
1106 np->p_flags |= PFOREGND;
1107 else
1108 np->p_flags &= ~PFOREGND;
1109 }
1110 } while ((np = np->p_friends) != pp);
1111 if (!foregnd)
1112 pclrcurr(pp);
1113 (void)pprint(pp, foregnd ? NAME | JOBDIR : NUMBER | NAME | AMPERSAND);
1114 if (foregnd)
1115 (void)tcsetpgrp(FSHTTY, pp->p_jobid);
1116 if (jobflags & PSTOPPED)
1117 (void)kill(-pp->p_jobid, SIGCONT);
1118 (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
1119 }
1120
1121 void
1122 panystop(bool neednl)
1123 {
1124 struct process *pp;
1125
1126 chkstop = 2;
1127 for (pp = proclist.p_next; pp; pp = pp->p_next)
1128 if (pp->p_flags & PSTOPPED)
1129 stderror(ERR_STOPPED, neednl ? "\n" : "");
1130 }
1131
1132 struct process *
1133 pfind(Char *cp)
1134 {
1135 struct process *pp, *np;
1136
1137 if (cp == 0 || cp[1] == 0 || eq(cp, STRcent2) || eq(cp, STRcentplus)) {
1138 if (pcurrent == NULL)
1139 stderror(ERR_NAME | ERR_JOBCUR);
1140 return (pcurrent);
1141 }
1142 if (eq(cp, STRcentminus) || eq(cp, STRcenthash)) {
1143 if (pprevious == NULL)
1144 stderror(ERR_NAME | ERR_JOBPREV);
1145 return (pprevious);
1146 }
1147 if (Isdigit(cp[1])) {
1148 int idx = atoi(short2str(cp + 1));
1149
1150 for (pp = proclist.p_next; pp; pp = pp->p_next)
1151 if (pp->p_index == idx && pp->p_pid == pp->p_jobid)
1152 return (pp);
1153 stderror(ERR_NAME | ERR_NOSUCHJOB);
1154 }
1155 np = NULL;
1156 for (pp = proclist.p_next; pp; pp = pp->p_next)
1157 if (pp->p_pid == pp->p_jobid) {
1158 if (cp[1] == '?') {
1159 Char *dp;
1160
1161 for (dp = pp->p_command; *dp; dp++) {
1162 if (*dp != cp[2])
1163 continue;
1164 if (prefix(cp + 2, dp))
1165 goto match;
1166 }
1167 }
1168 else if (prefix(cp + 1, pp->p_command)) {
1169 match:
1170 if (np)
1171 stderror(ERR_NAME | ERR_AMBIG);
1172 np = pp;
1173 }
1174 }
1175 if (np)
1176 return (np);
1177 stderror(ERR_NAME | (cp[1] == '?' ? ERR_JOBPAT : ERR_NOSUCHJOB));
1178 /* NOTREACHED */
1179 }
1180
1181 /*
1182 * pgetcurr - find most recent job that is not pp, preferably stopped
1183 */
1184 static struct process *
1185 pgetcurr(struct process *pp)
1186 {
1187 struct process *np, *xp;
1188
1189 xp = NULL;
1190 for (np = proclist.p_next; np; np = np->p_next)
1191 if (np != pcurrent && np != pp && np->p_pid &&
1192 np->p_pid == np->p_jobid) {
1193 if (np->p_flags & PSTOPPED)
1194 return (np);
1195 if (xp == NULL)
1196 xp = np;
1197 }
1198 return (xp);
1199 }
1200
1201 /*
1202 * donotify - flag the job so as to report termination asynchronously
1203 */
1204 void
1205 /*ARGSUSED*/
1206 donotify(Char **v, struct command *t)
1207 {
1208 struct process *pp;
1209
1210 pp = pfind(*++v);
1211 pp->p_flags |= PNOTIFY;
1212 }
1213
1214 /*
1215 * Do the fork and whatever should be done in the child side that
1216 * should not be done if we are not forking at all (like for simple builtin's)
1217 * Also do everything that needs any signals fiddled with in the parent side
1218 *
1219 * Wanttty tells whether process and/or tty pgrps are to be manipulated:
1220 * -1: leave tty alone; inherit pgrp from parent
1221 * 0: already have tty; manipulate process pgrps only
1222 * 1: want to claim tty; manipulate process and tty pgrps
1223 * It is usually just the value of tpgrp.
1224 */
1225
1226 int
1227 pfork(struct command *t /* command we are forking for */, int wanttty)
1228 {
1229 int pgrp, pid;
1230 sigset_t osigset, nsigset;
1231 bool ignint;
1232
1233 ignint = 0;
1234 /*
1235 * A child will be uninterruptible only under very special conditions.
1236 * Remember that the semantics of '&' is implemented by disconnecting the
1237 * process from the tty so signals do not need to ignored just for '&'.
1238 * Thus signals are set to default action for children unless: we have had
1239 * an "onintr -" (then specifically ignored) we are not playing with
1240 * signals (inherit action)
1241 */
1242 if (setintr)
1243 ignint = (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT))
1244 || (gointr && eq(gointr, STRminus));
1245 /*
1246 * Check for maximum nesting of 16 processes to avoid Forking loops
1247 */
1248 if (child == 16)
1249 stderror(ERR_NESTING, 16);
1250 /*
1251 * Hold SIGCHLD until we have the process installed in our table.
1252 */
1253 sigemptyset(&nsigset);
1254 (void)sigaddset(&nsigset, SIGCHLD);
1255 (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
1256 while ((pid = fork()) < 0)
1257 if (setintr == 0)
1258 (void)sleep(FORKSLEEP);
1259 else {
1260 (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
1261 stderror(ERR_NOPROC);
1262 }
1263 if (pid == 0) {
1264 settimes();
1265 pgrp = pcurrjob ? pcurrjob->p_jobid : getpid();
1266 pflushall();
1267 pcurrjob = NULL;
1268 child++;
1269 if (setintr) {
1270 setintr = 0; /* until I think otherwise */
1271 /*
1272 * Children just get blown away on SIGINT, SIGQUIT unless "onintr
1273 * -" seen.
1274 */
1275 (void)signal(SIGINT, ignint ? SIG_IGN : SIG_DFL);
1276 (void)signal(SIGQUIT, ignint ? SIG_IGN : SIG_DFL);
1277 if (wanttty >= 0) {
1278 /* make stoppable */
1279 (void)signal(SIGTSTP, SIG_DFL);
1280 (void)signal(SIGTTIN, SIG_DFL);
1281 (void)signal(SIGTTOU, SIG_DFL);
1282 }
1283 (void)signal(SIGTERM, parterm);
1284 }
1285 else if (tpgrp == -1 && (t->t_dflg & F_NOINTERRUPT)) {
1286 (void)signal(SIGINT, SIG_IGN);
1287 (void)signal(SIGQUIT, SIG_IGN);
1288 }
1289 pgetty(wanttty, pgrp);
1290 /*
1291 * Nohup and nice apply only to NODE_COMMAND's but it would be nice
1292 * (?!?) if you could say "nohup (foo;bar)" Then the parser would have
1293 * to know about nice/nohup/time
1294 */
1295 if (t->t_dflg & F_NOHUP)
1296 (void)signal(SIGHUP, SIG_IGN);
1297 if (t->t_dflg & F_NICE)
1298 (void)setpriority(PRIO_PROCESS, 0, t->t_nice);
1299 }
1300 else {
1301 if (wanttty >= 0)
1302 (void)setpgid(pid, pcurrjob ? pcurrjob->p_jobid : pid);
1303 palloc(pid, t);
1304 (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
1305 }
1306
1307 return (pid);
1308 }
1309
1310 static void
1311 okpcntl(void)
1312 {
1313 if (tpgrp == -1)
1314 stderror(ERR_JOBCONTROL);
1315 if (tpgrp == 0)
1316 stderror(ERR_JOBCTRLSUB);
1317 /* NOTREACHED */
1318 }
1319
1320 /*
1321 * if we don't have vfork(), things can still go in the wrong order
1322 * resulting in the famous 'Stopped (tty output)'. But some systems
1323 * don't permit the setpgid() call, (these are more recent secure
1324 * systems such as ibm's aix). Then we'd rather print an error message
1325 * than hang the shell!
1326 * I am open to suggestions how to fix that.
1327 */
1328 void
1329 pgetty(int wanttty, int pgrp)
1330 {
1331 sigset_t osigset, nsigset;
1332
1333 /*
1334 * christos: I am blocking the tty signals till I've set things
1335 * correctly....
1336 */
1337 if (wanttty > 0) {
1338 sigemptyset(&nsigset);
1339 (void)sigaddset(&nsigset, SIGTSTP);
1340 (void)sigaddset(&nsigset, SIGTTIN);
1341 (void)sigaddset(&nsigset, SIGTTOU);
1342 (void)sigprocmask(SIG_BLOCK, &nsigset, &osigset);
1343 }
1344 /*
1345 * From: Michael Schroeder <mlschroe (at) immd4.informatik.uni-erlangen.de>
1346 * Don't check for tpgrp >= 0 so even non-interactive shells give
1347 * background jobs process groups Same for the comparison in the other part
1348 * of the #ifdef
1349 */
1350 if (wanttty >= 0)
1351 if (setpgid(0, pgrp) == -1) {
1352 (void)fprintf(csherr, "csh: setpgid error.\n");
1353 xexit(0);
1354 }
1355
1356 if (wanttty > 0) {
1357 (void)tcsetpgrp(FSHTTY, pgrp);
1358 (void)sigprocmask(SIG_SETMASK, &osigset, NULL);
1359 }
1360
1361 if (tpgrp > 0)
1362 tpgrp = 0; /* gave tty away */
1363 }
1364