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