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