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