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