1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Job control for UNIX Shell
*/
#include <sys/termio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/param.h>
#include <fcntl.h>
#include <errno.h>
#include <signal.h>
#include "defs.h"
/*
* one of these for each active job
*/
struct job
{
struct job *j_nxtp; /* next job in job ID order */
struct job *j_curp; /* next job in job currency order */
struct termios j_stty; /* termio save area when job stops */
pid_t j_pid; /* job leader's process ID */
pid_t j_pgid; /* job's process group ID */
pid_t j_tgid; /* job's foreground process group ID */
uint_t j_jid; /* job ID */
ushort_t j_xval; /* exit code, or exit or stop signal */
ushort_t j_flag; /* various status flags defined below */
char *j_pwd; /* job's working directory */
char *j_cmd; /* cmd used to invoke this job */
};
/* defines for j_flag */
#define J_DUMPED 0001 /* job has core dumped */
#define J_NOTIFY 0002 /* job has changed status */
#define J_SAVETTY 0004 /* job was stopped in foreground, and its */
/* termio settings were saved */
#define J_STOPPED 0010 /* job has been stopped */
#define J_SIGNALED 0020 /* job has received signal; j_xval has it */
#define J_DONE 0040 /* job has finished */
#define J_RUNNING 0100 /* job is currently running */
#define J_FOREGND 0200 /* job was put in foreground by shell */
/* options to the printjob() function defined below */
#define PR_CUR 00001 /* print job currency ('+', '-', or ' ') */
#define PR_JID 00002 /* print job ID */
#define PR_PGID 00004 /* print job's process group ID */
#define PR_STAT 00010 /* print status obtained from wait */
#define PR_CMD 00020 /* print cmd that invoked job */
#define PR_AMP 00040 /* print a '&' if in the background */
#define PR_PWD 00100 /* print jobs present working directory */
#define PR_DFL (PR_CUR|PR_JID|PR_STAT|PR_CMD) /* default options */
#define PR_LONG (PR_DFL|PR_PGID|PR_PWD) /* long options */
static struct termios mystty; /* default termio settings */
static int eofflg,
jobcnt, /* number of active jobs */
jobdone, /* number of active but finished jobs */
jobnote; /* jobs requiring notification */
static pid_t svpgid, /* saved process group ID */
svtgid; /* saved foreground process group ID */
static struct job *jobcur, /* active jobs listed in currency order */
**nextjob,
*thisjob,
*joblst; /* active jobs listed in job ID order */
static void printjob(struct job *, int);
static struct job *
pgid2job(pid_t pgid)
{
struct job *jp;
for (jp = joblst; jp != 0 && jp->j_pid != pgid; jp = jp->j_nxtp)
continue;
return (jp);
}
static struct job *
str2job(char *cmd, char *job, int mustbejob)
{
struct job *jp, *njp;
int i;
if (*job != '%')
jp = pgid2job(stoi(job));
else if (*++job == 0 || *job == '+' || *job == '%' || *job == '-') {
jp = jobcur;
if (*job == '-' && jp)
jp = jp->j_curp;
} else if (*job >= '0' && *job <= '9') {
i = stoi(job);
for (jp = joblst; jp && jp->j_jid != i; jp = jp->j_nxtp)
continue;
} else if (*job == '?') {
int j;
char *p;
i = strlen(++job);
jp = 0;
for (njp = jobcur; njp; njp = njp->j_curp) {
if (njp->j_jid == 0)
continue;
for (p = njp->j_cmd, j = strlen(p); j >= i; p++, j--) {
if (strncmp(job, p, i) == 0) {
if (jp != 0)
failed((unsigned char *)cmd,
ambiguous);
jp = njp;
break;
}
}
}
} else {
i = strlen(job);
jp = 0;
for (njp = jobcur; njp; njp = njp->j_curp) {
if (njp->j_jid == 0)
continue;
if (strncmp(job, njp->j_cmd, i) == 0) {
if (jp != 0)
failed((unsigned char *)cmd, ambiguous);
jp = njp;
}
}
}
if (mustbejob && (jp == 0 || jp->j_jid == 0))
failed((unsigned char *)cmd, nosuchjob);
return (jp);
}
static void
freejob(struct job *jp)
{
struct job **njp;
struct job **cjp;
for (njp = &joblst; *njp != jp; njp = &(*njp)->j_nxtp)
continue;
for (cjp = &jobcur; *cjp != jp; cjp = &(*cjp)->j_curp)
continue;
*njp = jp->j_nxtp;
*cjp = jp->j_curp;
free(jp);
jobcnt--;
jobdone--;
}
/*
* Collect the foreground job.
* Used in the case where the subshell wants
* to exit, but needs to wait until the fg job
* is done.
*/
void
collect_fg_job(void)
{
struct job *jp;
pid_t pid;
int stat;
for (jp = joblst; jp; jp = jp->j_nxtp)
if (jp->j_flag & J_FOREGND)
break;
if (!jp)
/* no foreground job */
return;
/*
* Wait on fg job until wait succeeds
* or it fails due to no waitable children.
*/
while (1) {
errno = 0;
pid = waitpid(jp->j_pid, &stat, 0);
if (pid == jp->j_pid || (pid == -1 && errno == ECHILD))
break;
}
}
/*
* analyze the status of a job
*/
static int
statjob(struct job *jp, int stat, int fg, int rc)
{
pid_t tgid;
int done = 0;
if (WIFCONTINUED(stat)) {
if (jp->j_flag & J_STOPPED) {
jp->j_flag &= ~(J_STOPPED|J_SIGNALED|J_SAVETTY);
jp->j_flag |= J_RUNNING;
if (!fg && jp->j_jid) {
jp->j_flag |= J_NOTIFY;
jobnote++;
}
}
} else if (WIFSTOPPED(stat)) {
jp->j_xval = WSTOPSIG(stat);
jp->j_flag &= ~J_RUNNING;
jp->j_flag |= (J_SIGNALED|J_STOPPED);
jp->j_pgid = getpgid(jp->j_pid);
jp->j_tgid = jp->j_pgid;
if (fg) {
if (tgid = settgid(mypgid, jp->j_pgid))
jp->j_tgid = tgid;
else {
jp->j_flag |= J_SAVETTY;
tcgetattr(0, &jp->j_stty);
(void) tcsetattr(0, TCSANOW, &mystty);
}
}
if (jp->j_jid) {
jp->j_flag |= J_NOTIFY;
jobnote++;
}
} else {
jp->j_flag &= ~J_RUNNING;
jp->j_flag |= J_DONE;
done++;
jobdone++;
if (WIFSIGNALED(stat)) {
jp->j_xval = WTERMSIG(stat);
jp->j_flag |= J_SIGNALED;
if (WCOREDUMP(stat))
jp->j_flag |= J_DUMPED;
if (!fg || jp->j_xval != SIGINT) {
jp->j_flag |= J_NOTIFY;
jobnote++;
}
} else { /* WIFEXITED */
jp->j_xval = WEXITSTATUS(stat);
jp->j_flag &= ~J_SIGNALED;
if (!fg && jp->j_jid) {
jp->j_flag |= J_NOTIFY;
jobnote++;
}
}
if (fg) {
if (!settgid(mypgid, jp->j_pgid) ||
!settgid(mypgid, getpgid(jp->j_pid)))
tcgetattr(0, &mystty);
}
}
if (rc) {
exitval = jp->j_xval;
if (jp->j_flag & J_SIGNALED)
exitval |= SIGFLG;
exitset();
}
if (done && !(jp->j_flag & J_NOTIFY))
freejob(jp);
return (done);
}
/*
* collect the status of jobs that have recently exited or stopped -
* if wnohang == WNOHANG, wait until error, or all jobs are accounted for;
*
* called after each command is executed, with wnohang == 0, and as part
* of "wait" builtin with wnohang == WNOHANG
*
* We do not need to call chktrap here if waitpid(2) is called with
* wnohang == 0, because that only happens from syswait() which is called
* from builtin() where chktrap() is already called.
*/
static void
collectjobs(wnohang)
{
pid_t pid;
struct job *jp;
int stat, n;
int wflags;
if ((flags & (monitorflg|jcflg|jcoff)) == (monitorflg|jcflg))
wflags = WUNTRACED|WCONTINUED;
else
wflags = 0;
for (n = jobcnt - jobdone; n > 0; n--) {
if ((pid = waitpid(-1, &stat, wnohang|wflags)) <= 0)
break;
if (jp = pgid2job(pid))
(void) statjob(jp, stat, 0, 0);
}
}
void
freejobs()
{
struct job *jp;
collectjobs(WNOHANG);
if (jobnote) {
int savefd = setb(2);
for (jp = joblst; jp; jp = jp->j_nxtp) {
if (jp->j_flag & J_NOTIFY) {
if (jp->j_jid)
printjob(jp, PR_DFL);
else if (jp->j_flag & J_FOREGND)
printjob(jp, PR_STAT);
else
printjob(jp, PR_STAT|PR_PGID);
}
}
(void) setb(savefd);
}
if (jobdone) {
for (jp = joblst; jp; jp = jp->j_nxtp) {
if (jp->j_flag & J_DONE)
freejob(jp);
}
}
}
static void
waitjob(struct job *jp)
{
int stat;
int done;
pid_t pid = jp->j_pid;
int wflags;
int ret = 0;
int err = 0;
if ((flags & (monitorflg|jcflg|jcoff)) == (monitorflg|jcflg))
wflags = WUNTRACED;
else
wflags = 0;
do {
errno = 0;
ret = waitpid(pid, &stat, wflags|WNOWAIT);
err = errno;
if (ret == -1 && err == ECHILD) {
stat = 0;
break;
}
} while (ret != pid);
done = statjob(jp, stat, 1, 1);
waitpid(pid, 0, wflags);
if (done && exitval && (flags & errflg))
exitsh(exitval);
flags |= eflag;
}
/*
* modify the foreground process group to *new* only if the
* current foreground process group is equal to *expected*
*/
int
settgid(new, expected)
pid_t new, expected;
{
pid_t current = tcgetpgrp(0);
if (current != expected)
return (current);
if (new != current)
tcsetpgrp(0, new);
return (0);
}
static void
restartjob(struct job *jp, int fg)
{
if (jp != jobcur) {
struct job *t;
for (t = jobcur; t->j_curp != jp; t = t->j_curp)
;
t->j_curp = jp->j_curp;
jp->j_curp = jobcur;
jobcur = jp;
}
if (fg) {
if (jp->j_flag & J_SAVETTY) {
jp->j_stty.c_lflag &= ~TOSTOP;
jp->j_stty.c_lflag |= (mystty.c_lflag&TOSTOP);
jp->j_stty.c_cc[VSUSP] = mystty.c_cc[VSUSP];
jp->j_stty.c_cc[VDSUSP] = mystty.c_cc[VDSUSP];
(void) tcsetattr(0, TCSADRAIN, &jp->j_stty);
}
(void) settgid(jp->j_tgid, mypgid);
}
(void) kill(-(jp->j_pgid), SIGCONT);
if (jp->j_tgid != jp->j_pgid)
(void) kill(-(jp->j_tgid), SIGCONT);
jp->j_flag &= ~(J_STOPPED|J_SIGNALED|J_SAVETTY);
jp->j_flag |= J_RUNNING;
if (fg) {
jp->j_flag |= J_FOREGND;
printjob(jp, PR_JID|PR_CMD);
waitjob(jp);
} else {
jp->j_flag &= ~J_FOREGND;
printjob(jp, PR_JID|PR_CMD|PR_AMP);
}
}
static void
printjob(struct job *jp, int propts)
{
int sp = 0;
if (jp->j_flag & J_NOTIFY) {
jobnote--;
jp->j_flag &= ~J_NOTIFY;
}
if (propts & PR_JID) {
prc_buff('[');
prn_buff(jp->j_jid);
prc_buff(']');
sp = 1;
}
if (propts & PR_CUR) {
while (sp-- > 0)
prc_buff(SPACE);
sp = 1;
if (jobcur == jp)
prc_buff('+');
else if (jobcur != 0 && jobcur->j_curp == jp)
prc_buff('-');
else
sp++;
}
if (propts & PR_PGID) {
while (sp-- > 0)
prc_buff(SPACE);
prn_buff(jp->j_pid);
sp = 1;
}
if (propts & PR_STAT) {
char *gmsg;
while (sp-- > 0)
prc_buff(SPACE);
sp = 28;
if (jp->j_flag & J_SIGNALED) {
char *sigstr, *strsignal();
if ((sigstr = strsignal(jp->j_xval)) != NULL) {
sp -= strlen(sigstr);
prs_buff(sigstr);
} else {
itos(jp->j_xval);
gmsg = gettext(signalnum);
sp -= strlen(numbuf) + strlen(gmsg);
prs_buff((unsigned char *)gmsg);
prs_buff(numbuf);
}
if (jp->j_flag & J_DUMPED) {
gmsg = gettext(coredump);
sp -= strlen(gmsg);
prs_buff((unsigned char *)gmsg);
}
} else if (jp->j_flag & J_DONE) {
itos(jp->j_xval);
gmsg = gettext(exited);
sp -= strlen(gmsg) + strlen(numbuf) + 2;
prs_buff((unsigned char *)gmsg);
prc_buff('(');
itos(jp->j_xval);
prs_buff(numbuf);
prc_buff(')');
} else {
gmsg = gettext(running);
sp -= strlen(gmsg);
prs_buff((unsigned char *)gmsg);
}
if (sp < 1)
sp = 1;
}
if (propts & PR_CMD) {
while (sp-- > 0)
prc_buff(SPACE);
prs_buff(jp->j_cmd);
sp = 1;
}
if (propts & PR_AMP) {
while (sp-- > 0)
prc_buff(SPACE);
prc_buff('&');
sp = 1;
}
if (propts & PR_PWD) {
while (sp-- > 0)
prc_buff(SPACE);
prs_buff("(wd: ");
prs_buff(jp->j_pwd);
prc_buff(')');
}
prc_buff(NL);
flushb();
}
/*
* called to initialize job control for each new input file to the shell,
* and after the "exec" builtin
*/
void
startjobs()
{
svpgid = mypgid;
if (tcgetattr(0, &mystty) == -1 || (svtgid = tcgetpgrp(0)) == -1) {
flags &= ~jcflg;
return;
}
flags |= jcflg;
handle(SIGTTOU, SIG_IGN);
handle(SIGTSTP, SIG_DFL);
if (mysid != mypgid) {
setpgid(0, 0);
mypgid = mypid;
(void) settgid(mypgid, svpgid);
}
}
int
endjobs(check_if)
int check_if;
{
if ((flags & (jcoff|jcflg)) != jcflg)
return (1);
if (check_if && jobcnt && eofflg++ == 0) {
struct job *jp;
if (check_if & JOB_STOPPED) {
for (jp = joblst; jp; jp = jp->j_nxtp) {
if (jp->j_jid && (jp->j_flag & J_STOPPED)) {
prs(_gettext(jobsstopped));
prc(NL);
return (0);
}
}
}
if (check_if & JOB_RUNNING) {
for (jp = joblst; jp; jp = jp->j_nxtp) {
if (jp->j_jid && (jp->j_flag & J_RUNNING)) {
prs(_gettext(jobsrunning));
prc(NL);
return (0);
}
}
}
}
if (svpgid != mypgid) {
(void) settgid(svtgid, mypgid);
setpgid(0, svpgid);
}
return (1);
}
/*
* called by the shell to reserve a job slot for a job about to be spawned
*/
void
deallocjob()
{
free(thisjob);
jobcnt--;
}
void
allocjob(char *cmd, uchar_t *cwd, int monitor)
{
struct job *jp, **jpp;
int jid, cmdlen, cwdlen;
cmdlen = strlen(cmd) + 1;
if (cmd[cmdlen-2] == '&') {
cmd[cmdlen-3] = 0;
cmdlen -= 2;
}
cwdlen = strlen(cwd) + 1;
jp = (struct job *) alloc(sizeof (struct job) + cmdlen + cwdlen);
if (jp == 0)
error(nostack);
jobcnt++;
jp->j_cmd = ((char *)jp) + sizeof (struct job);
strcpy(jp->j_cmd, cmd);
jp->j_pwd = jp->j_cmd + cmdlen;
strcpy(jp->j_pwd, cwd);
jpp = &joblst;
if (monitor) {
for (; *jpp; jpp = &(*jpp)->j_nxtp)
if ((*jpp)->j_jid != 0)
break;
for (jid = 1; *jpp; jpp = &(*jpp)->j_nxtp, jid++)
if ((*jpp)->j_jid != jid)
break;
} else
jid = 0;
jp->j_jid = jid;
nextjob = jpp;
thisjob = jp;
}
void
clearjobs(void)
{
struct job *jp, *sjp;
for (jp = joblst; jp; jp = sjp) {
sjp = jp->j_nxtp;
free(jp);
}
joblst = NULL;
jobcnt = 0;
jobnote = 0;
jobdone = 0;
}
void
makejob(int monitor, int fg)
{
if (monitor) {
mypgid = mypid;
setpgid(0, 0);
if (fg)
tcsetpgrp(0, mypid);
handle(SIGTTOU, SIG_DFL);
handle(SIGTSTP, SIG_DFL);
} else if (!fg) {
#ifdef NICE
nice(NICE);
#endif
handle(SIGTTIN, SIG_IGN);
handle(SIGINT, SIG_IGN);
handle(SIGQUIT, SIG_IGN);
if (!ioset)
renamef(chkopen(devnull, 0), 0);
}
}
/*
* called by the shell after job has been spawned, to fill in the
* job slot, and wait for the job if in the foreground
*/
void
postjob(pid, fg)
pid_t pid;
int fg;
{
int propts;
thisjob->j_nxtp = *nextjob;
*nextjob = thisjob;
thisjob->j_curp = jobcur;
jobcur = thisjob;
if (thisjob->j_jid) {
thisjob->j_pgid = pid;
propts = PR_JID|PR_PGID;
} else {
thisjob->j_pgid = mypgid;
propts = PR_PGID;
}
thisjob->j_flag = J_RUNNING;
thisjob->j_tgid = thisjob->j_pgid;
thisjob->j_pid = pid;
eofflg = 0;
if (fg) {
thisjob->j_flag |= J_FOREGND;
waitjob(thisjob);
} else {
if (flags & ttyflg)
printjob(thisjob, propts);
assnum(&pcsadr, (long)pid);
}
}
/*
* the builtin "jobs" command
*/
void
sysjobs(argc, argv)
int argc;
char *argv[];
{
char *cmd = *argv;
struct job *jp;
int propts, c;
extern int opterr, i;
int savoptind = optind;
int loptind = -1;
int savopterr = opterr;
int savsp = _sp;
char *savoptarg = optarg;
optind = 1;
opterr = 0;
_sp = 1;
propts = 0;
if ((flags & jcflg) == 0)
failed((unsigned char *)cmd, nojc);
while ((c = getopt(argc, argv, "lpx")) != -1) {
if (propts) {
gfailure(usage, jobsuse);
goto err;
}
switch (c) {
case 'x':
propts = -1;
break;
case 'p':
propts = PR_PGID;
break;
case 'l':
propts = PR_LONG;
break;
case '?':
gfailure(usage, jobsuse);
goto err;
}
}
loptind = optind;
err:
optind = savoptind;
optarg = savoptarg;
opterr = savopterr;
_sp = savsp;
if (loptind == -1)
return;
if (propts == -1) {
unsigned char *bp;
char *cp;
unsigned char *savebp;
for (savebp = bp = locstak(); loptind < argc; loptind++) {
cp = argv[loptind];
if (*cp == '%') {
jp = str2job(cmd, cp, 1);
itos(jp->j_pid);
cp = (char *)numbuf;
}
while (*cp) {
if (bp >= brkend)
growstak(bp);
*bp++ = *cp++;
}
if (bp >= brkend)
growstak(bp);
*bp++ = SPACE;
}
endstak(bp);
execexp(savebp, 0);
return;
}
collectjobs(WNOHANG);
if (propts == 0)
propts = PR_DFL;
if (loptind == argc) {
for (jp = joblst; jp; jp = jp->j_nxtp) {
if (jp->j_jid)
printjob(jp, propts);
}
} else do
printjob(str2job(cmd, argv[loptind++], 1), propts);
while (loptind < argc);
}
/*
* the builtin "fg" and "bg" commands
*/
void
sysfgbg(int argc, char *argv[])
{
char *cmd = *argv;
int fg;
if ((flags & jcflg) == 0)
failed((unsigned char *)cmd, nojc);
fg = eq("fg", cmd);
if (*++argv == 0) {
struct job *jp;
for (jp = jobcur; ; jp = jp->j_curp) {
if (jp == 0)
failed((unsigned char *)cmd, nocurjob);
if (jp->j_jid)
break;
}
restartjob(jp, fg);
} else {
do {
restartjob(str2job(cmd, *argv, 1), fg);
} while (*++argv);
}
}
/*
* the builtin "wait" commands
*/
void
syswait(argc, argv)
int argc;
char *argv[];
{
char *cmd = *argv;
struct job *jp;
int stat;
int wflags;
if ((flags & (monitorflg|jcflg|jcoff)) == (monitorflg|jcflg))
wflags = WUNTRACED;
else
wflags = 0;
if (argc == 1)
collectjobs(0);
else while (--argc) {
if ((jp = str2job(cmd, *++argv, 0)) == 0)
continue;
if (!(jp->j_flag & J_RUNNING))
continue;
if (waitpid(jp->j_pid, &stat, wflags) <= 0)
break;
(void) statjob(jp, stat, 0, 1);
}
}
static void
sigv(char *cmd, int sig, char *args)
{
int pgrp = 0;
int stopme = 0;
pid_t id;
if (*args == '%') {
struct job *jp;
jp = str2job(cmd, args, 1);
id = jp->j_pgid;
pgrp++;
} else {
if (*args == '-') {
pgrp++;
args++;
}
id = 0;
do {
if (*args < '0' || *args > '9') {
failure(cmd, badid);
return;
}
id = (id * 10) + (*args - '0');
} while (*++args);
if (id == 0) {
id = mypgid;
pgrp++;
}
}
if (sig == SIGSTOP) {
if (id == mysid || id == mypid && mypgid == mysid) {
failure(cmd, loginsh);
return;
}
if (id == mypgid && mypgid != svpgid) {
(void) settgid(svtgid, mypgid);
setpgid(0, svpgid);
stopme++;
}
}
if (pgrp)
id = -id;
if (kill(id, sig) < 0) {
switch (errno) {
case EPERM:
failure(cmd, eacces);
break;
case EINVAL:
failure(cmd, badsig);
break;
default:
if (pgrp)
failure(cmd, nosuchpgid);
else
failure(cmd, nosuchpid);
break;
}
} else if (sig == SIGTERM && pgrp)
(void) kill(id, SIGCONT);
if (stopme) {
setpgid(0, mypgid);
(void) settgid(mypgid, svpgid);
}
}
void
sysstop(int argc, char *argv[])
{
char *cmd = *argv;
if (argc <= 1) {
gfailure(usage, stopuse);
return;
}
while (*++argv)
sigv(cmd, SIGSTOP, *argv);
}
void
syskill(int argc, char *argv[])
{
char *cmd = *argv;
int sig = SIGTERM;
if (argc == 1) {
gfailure(usage, killuse);
return;
}
if (argv[1][0] == '-') {
if (argc == 2) {
int i;
int cnt = 0;
char sep = 0;
char buf[12];
if (!eq(argv[1], "-l")) {
gfailure(usage, killuse);
return;
}
for (i = 1; i < MAXTRAP; i++) {
if (sig2str(i, buf) < 0)
continue;
if (sep)
prc_buff(sep);
prs_buff(buf);
if ((flags & ttyflg) && (++cnt % 10))
sep = TAB;
else
sep = NL;
}
prc_buff(NL);
return;
}
if (str2sig(&argv[1][1], &sig)) {
failure(cmd, badsig);
return;
}
argv++;
}
while (*++argv)
sigv(cmd, sig, *argv);
}
void
syssusp(int argc, char *argv[])
{
if (argc != 1)
failed((unsigned char *)argv[0], badopt);
sigv(argv[0], SIGSTOP, "0");
}
void
hupforegnd(void)
{
struct job *jp;
(void) sighold(SIGCHLD);
for (jp = joblst; jp != NULL; jp = jp->j_nxtp) {
if (jp->j_flag & J_FOREGND) {
(void) kill(jp->j_pid, SIGHUP);
break;
}
}
(void) sigrelse(SIGCHLD);
}
|