-
Notifications
You must be signed in to change notification settings - Fork 182
/
jobs.c
5156 lines (4380 loc) · 136 KB
/
jobs.c
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
/* jobs.c - functions that make children, remember them, and handle their termination. */
/* This file works with both POSIX and BSD systems. It implements job
control. */
/* Copyright (C) 1989-2022 Free Software Foundation, Inc.
This file is part of GNU Bash, the Bourne Again SHell.
Bash is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Bash is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Bash. If not, see <http://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "bashtypes.h"
#include "trap.h"
#include <stdio.h>
#include <signal.h>
#include <errno.h>
#if defined (HAVE_UNISTD_H)
# include <unistd.h>
#endif
#include "posixtime.h"
#if defined (HAVE_SYS_RESOURCE_H) && defined (HAVE_WAIT3) && !defined (_POSIX_VERSION) && !defined (RLIMTYPE)
# include <sys/resource.h>
#endif /* !_POSIX_VERSION && HAVE_SYS_RESOURCE_H && HAVE_WAIT3 && !RLIMTYPE */
#if defined (HAVE_SYS_FILE_H)
# include <sys/file.h>
#endif
#include "filecntl.h"
#include <sys/ioctl.h>
#if defined (HAVE_SYS_PARAM_H)
#include <sys/param.h>
#endif
#if defined (BUFFERED_INPUT)
# include "input.h"
#endif
/* Need to include this up here for *_TTY_DRIVER definitions. */
#include "shtty.h"
/* Define this if your output is getting swallowed. It's a no-op on
machines with the termio or termios tty drivers. */
/* #define DRAIN_OUTPUT */
/* For the TIOCGPGRP and TIOCSPGRP ioctl parameters on HP-UX */
#if defined (hpux) && !defined (TERMIOS_TTY_DRIVER)
# include <bsdtty.h>
#endif /* hpux && !TERMIOS_TTY_DRIVER */
#include "bashansi.h"
#include "bashintl.h"
#include "shell.h"
#include "parser.h"
#include "jobs.h"
#include "execute_cmd.h"
#include "flags.h"
#include "typemax.h"
#include "builtins/builtext.h"
#include "builtins/common.h"
#if defined (READLINE)
# include <readline/readline.h>
#endif
#if !defined (errno)
extern int errno;
#endif /* !errno */
#if !defined (HAVE_KILLPG)
extern int killpg PARAMS((pid_t, int));
#endif
#if !DEFAULT_CHILD_MAX
# define DEFAULT_CHILD_MAX 4096
#endif
#if !MAX_CHILD_MAX
# define MAX_CHILD_MAX 32768
#endif
#if !defined (DEBUG)
#define MAX_JOBS_IN_ARRAY 4096 /* production */
#else
#define MAX_JOBS_IN_ARRAY 128 /* testing */
#endif
/* XXX for now */
#define PIDSTAT_TABLE_SZ 4096
#define BGPIDS_TABLE_SZ 512
/* Flag values for second argument to delete_job */
#define DEL_WARNSTOPPED 1 /* warn about deleting stopped jobs */
#define DEL_NOBGPID 2 /* don't add pgrp leader to bgpids */
/* Take care of system dependencies that must be handled when waiting for
children. The arguments to the WAITPID macro match those to the Posix.1
waitpid() function. */
#if defined (ultrix) && defined (mips) && defined (_POSIX_VERSION)
# define WAITPID(pid, statusp, options) \
wait3 ((union wait *)statusp, options, (struct rusage *)0)
#else
# if defined (_POSIX_VERSION) || defined (HAVE_WAITPID)
# define WAITPID(pid, statusp, options) \
waitpid ((pid_t)pid, statusp, options)
# else
# if defined (HAVE_WAIT3)
# define WAITPID(pid, statusp, options) \
wait3 (statusp, options, (struct rusage *)0)
# else
# define WAITPID(pid, statusp, options) \
wait3 (statusp, options, (int *)0)
# endif /* HAVE_WAIT3 */
# endif /* !_POSIX_VERSION && !HAVE_WAITPID*/
#endif /* !(Ultrix && mips && _POSIX_VERSION) */
/* getpgrp () varies between systems. Even systems that claim to be
Posix.1 compatible lie sometimes (Ultrix, SunOS4, apollo). */
#if defined (GETPGRP_VOID)
# define getpgid(p) getpgrp ()
#else
# define getpgid(p) getpgrp (p)
#endif /* !GETPGRP_VOID */
/* If the system needs it, REINSTALL_SIGCHLD_HANDLER will reinstall the
handler for SIGCHLD. */
#if defined (MUST_REINSTALL_SIGHANDLERS)
# define REINSTALL_SIGCHLD_HANDLER signal (SIGCHLD, sigchld_handler)
#else
# define REINSTALL_SIGCHLD_HANDLER
#endif /* !MUST_REINSTALL_SIGHANDLERS */
/* Some systems let waitpid(2) tell callers about stopped children. */
#if !defined (WCONTINUED) || defined (WCONTINUED_BROKEN)
# undef WCONTINUED
# define WCONTINUED 0
#endif
#if !defined (WIFCONTINUED)
# define WIFCONTINUED(s) (0)
#endif
/* The number of additional slots to allocate when we run out. */
#define JOB_SLOTS 8
typedef int sh_job_map_func_t PARAMS((JOB *, int, int, int));
/* Variables used here but defined in other files. */
extern WORD_LIST *subst_assign_varlist;
extern SigHandler **original_signals;
extern void set_original_signal PARAMS((int, SigHandler *));
static struct jobstats zerojs = { -1L, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NO_JOB, NO_JOB, 0, 0 };
struct jobstats js = { -1L, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NO_JOB, NO_JOB, 0, 0 };
ps_index_t pidstat_table[PIDSTAT_TABLE_SZ];
struct bgpids bgpids = { 0, 0, 0, 0 };
struct procchain procsubs = { 0, 0, 0 };
/* The array of known jobs. */
JOB **jobs = (JOB **)NULL;
#if 0
/* The number of slots currently allocated to JOBS. */
int job_slots = 0;
#endif
/* The controlling tty for this shell. */
int shell_tty = -1;
/* The shell's process group. */
pid_t shell_pgrp = NO_PID;
/* The terminal's process group. */
pid_t terminal_pgrp = NO_PID;
/* The process group of the shell's parent. */
pid_t original_pgrp = NO_PID;
/* The process group of the pipeline currently being made. */
pid_t pipeline_pgrp = (pid_t)0;
#if defined (PGRP_PIPE)
/* Pipes which each shell uses to communicate with the process group leader
until all of the processes in a pipeline have been started. Then the
process leader is allowed to continue. */
int pgrp_pipe[2] = { -1, -1 };
#endif
/* Last child made by the shell. */
volatile pid_t last_made_pid = NO_PID;
/* Pid of the last asynchronous child. */
volatile pid_t last_asynchronous_pid = NO_PID;
/* The pipeline currently being built. */
PROCESS *the_pipeline = (PROCESS *)NULL;
/* If this is non-zero, do job control. */
int job_control = 1;
/* Are we running in background? (terminal_pgrp != shell_pgrp) */
int running_in_background = 0;
/* Call this when you start making children. */
int already_making_children = 0;
/* If this is non-zero, $LINES and $COLUMNS are reset after every process
exits from get_tty_state(). */
int check_window_size = CHECKWINSIZE_DEFAULT;
PROCESS *last_procsub_child = (PROCESS *)NULL;
/* Functions local to this file. */
void debug_print_pgrps (void);
static sighandler wait_sigint_handler PARAMS((int));
static sighandler sigchld_handler PARAMS((int));
static sighandler sigcont_sighandler PARAMS((int));
static sighandler sigstop_sighandler PARAMS((int));
static int waitchld PARAMS((pid_t, int));
static PROCESS *find_pid_in_pipeline PARAMS((pid_t, PROCESS *, int));
static PROCESS *find_pipeline PARAMS((pid_t, int, int *));
static PROCESS *find_process PARAMS((pid_t, int, int *));
static char *current_working_directory PARAMS((void));
static char *job_working_directory PARAMS((void));
static char *j_strsignal PARAMS((int));
static char *printable_job_status PARAMS((int, PROCESS *, int));
static PROCESS *find_last_proc PARAMS((int, int));
static pid_t find_last_pid PARAMS((int, int));
static int set_new_line_discipline PARAMS((int));
static int map_over_jobs PARAMS((sh_job_map_func_t *, int, int));
static int job_last_stopped PARAMS((int));
static int job_last_running PARAMS((int));
static int most_recent_job_in_state PARAMS((int, JOB_STATE));
static int find_job PARAMS((pid_t, int, PROCESS **));
static int print_job PARAMS((JOB *, int, int, int));
static int process_exit_status PARAMS((WAIT));
static int process_exit_signal PARAMS((WAIT));
static int set_job_status_and_cleanup PARAMS((int));
static WAIT job_signal_status PARAMS((int));
static WAIT raw_job_exit_status PARAMS((int));
static void notify_of_job_status PARAMS((void));
static void reset_job_indices PARAMS((void));
static void cleanup_dead_jobs PARAMS((void));
static int processes_in_job PARAMS((int));
static void realloc_jobs_list PARAMS((void));
static int compact_jobs_list PARAMS((int));
static void add_process PARAMS((char *, pid_t));
static void print_pipeline PARAMS((PROCESS *, int, int, FILE *));
static void pretty_print_job PARAMS((int, int, FILE *));
static void set_current_job PARAMS((int));
static void reset_current PARAMS((void));
static void set_job_running PARAMS((int));
static void setjstatus PARAMS((int));
static int maybe_give_terminal_to PARAMS((pid_t, pid_t, int));
static void mark_all_jobs_as_dead PARAMS((void));
static void mark_dead_jobs_as_notified PARAMS((int));
static void restore_sigint_handler PARAMS((void));
#if defined (PGRP_PIPE)
static void pipe_read PARAMS((int *));
#endif
/* Hash table manipulation */
static ps_index_t *pshash_getbucket PARAMS((pid_t));
static void pshash_delindex PARAMS((ps_index_t));
/* Saved background process status management */
static struct pidstat *bgp_add PARAMS((pid_t, int));
static int bgp_delete PARAMS((pid_t));
static void bgp_clear PARAMS((void));
static int bgp_search PARAMS((pid_t));
static struct pipeline_saver *alloc_pipeline_saver PARAMS((void));
static ps_index_t bgp_getindex PARAMS((void));
static void bgp_resize PARAMS((void)); /* XXX */
#if defined (ARRAY_VARS)
static int *pstatuses; /* list of pipeline statuses */
static int statsize;
#endif
/* Used to synchronize between wait_for and other functions and the SIGCHLD
signal handler. */
static int sigchld;
static int queue_sigchld;
#define QUEUE_SIGCHLD(os) (os) = sigchld, queue_sigchld++
/* We set queue_sigchld around the call to waitchld to protect data structures
from a SIGCHLD arriving while waitchld is executing. */
#define UNQUEUE_SIGCHLD(os) \
do { \
queue_sigchld--; \
if (queue_sigchld == 0 && os != sigchld) \
{ \
queue_sigchld = 1; \
waitchld (-1, 0); \
queue_sigchld = 0; \
} \
} while (0)
static SigHandler *old_tstp, *old_ttou, *old_ttin;
static SigHandler *old_cont = (SigHandler *)SIG_DFL;
/* A place to temporarily save the current pipeline. */
static struct pipeline_saver *saved_pipeline;
static int saved_already_making_children;
/* Set this to non-zero whenever you don't want the jobs list to change at
all: no jobs deleted and no status change notifications. This is used,
for example, when executing SIGCHLD traps, which may run arbitrary
commands. */
static int jobs_list_frozen;
static char retcode_name_buffer[64];
#if !defined (_POSIX_VERSION)
/* These are definitions to map POSIX 1003.1 functions onto existing BSD
library functions and system calls. */
#define setpgid(pid, pgrp) setpgrp (pid, pgrp)
#define tcsetpgrp(fd, pgrp) ioctl ((fd), TIOCSPGRP, &(pgrp))
pid_t
tcgetpgrp (fd)
int fd;
{
pid_t pgrp;
/* ioctl will handle setting errno correctly. */
if (ioctl (fd, TIOCGPGRP, &pgrp) < 0)
return (-1);
return (pgrp);
}
#endif /* !_POSIX_VERSION */
/* Initialize the global job stats structure and other bookkeeping variables */
void
init_job_stats ()
{
js = zerojs;
}
/* Return the working directory for the current process. Unlike
job_working_directory, this does not call malloc (), nor do any
of the functions it calls. This is so that it can safely be called
from a signal handler. */
static char *
current_working_directory ()
{
char *dir;
static char d[PATH_MAX];
dir = get_string_value ("PWD");
if (dir == 0 && the_current_working_directory && no_symbolic_links)
dir = the_current_working_directory;
if (dir == 0)
{
dir = getcwd (d, sizeof(d));
if (dir)
dir = d;
}
return (dir == 0) ? "<unknown>" : dir;
}
/* Return the working directory for the current process. */
static char *
job_working_directory ()
{
char *dir;
dir = get_string_value ("PWD");
if (dir)
return (savestring (dir));
dir = get_working_directory ("job-working-directory");
if (dir)
return (dir);
return (savestring ("<unknown>"));
}
void
making_children ()
{
if (already_making_children)
return;
already_making_children = 1;
start_pipeline ();
}
void
stop_making_children ()
{
already_making_children = 0;
}
void
cleanup_the_pipeline ()
{
PROCESS *disposer;
sigset_t set, oset;
BLOCK_CHILD (set, oset);
disposer = the_pipeline;
the_pipeline = (PROCESS *)NULL;
UNBLOCK_CHILD (oset);
if (disposer)
discard_pipeline (disposer);
}
/* Not used right now */
void
discard_last_procsub_child ()
{
PROCESS *disposer;
sigset_t set, oset;
BLOCK_CHILD (set, oset);
disposer = last_procsub_child;
last_procsub_child = (PROCESS *)NULL;
UNBLOCK_CHILD (oset);
if (disposer)
discard_pipeline (disposer);
}
static struct pipeline_saver *
alloc_pipeline_saver ()
{
struct pipeline_saver *ret;
ret = (struct pipeline_saver *)xmalloc (sizeof (struct pipeline_saver));
ret->pipeline = 0;
ret->next = 0;
return ret;
}
void
save_pipeline (clear)
int clear;
{
sigset_t set, oset;
struct pipeline_saver *saver;
BLOCK_CHILD (set, oset);
saver = alloc_pipeline_saver ();
saver->pipeline = the_pipeline;
saver->next = saved_pipeline;
saved_pipeline = saver;
if (clear)
the_pipeline = (PROCESS *)NULL;
saved_already_making_children = already_making_children;
UNBLOCK_CHILD (oset);
}
PROCESS *
restore_pipeline (discard)
int discard;
{
PROCESS *old_pipeline;
sigset_t set, oset;
struct pipeline_saver *saver;
BLOCK_CHILD (set, oset);
old_pipeline = the_pipeline;
the_pipeline = saved_pipeline->pipeline;
saver = saved_pipeline;
saved_pipeline = saved_pipeline->next;
free (saver);
already_making_children = saved_already_making_children;
UNBLOCK_CHILD (oset);
if (discard && old_pipeline)
{
discard_pipeline (old_pipeline);
return ((PROCESS *)NULL);
}
return old_pipeline;
}
/* Start building a pipeline. */
void
start_pipeline ()
{
if (the_pipeline)
{
cleanup_the_pipeline ();
/* If job_control == 0, pipeline_pgrp will always be equal to shell_pgrp;
if job_control != 0, pipeline_pgrp == shell_pgrp for command and
process substitution, in which case we want it to be the same as
shell_pgrp for the lifetime of this shell instance. */
if (pipeline_pgrp != shell_pgrp)
pipeline_pgrp = 0;
#if defined (PGRP_PIPE)
sh_closepipe (pgrp_pipe);
#endif
}
#if defined (PGRP_PIPE)
if (job_control)
{
if (pipe (pgrp_pipe) == -1)
sys_error (_("start_pipeline: pgrp pipe"));
}
#endif
}
/* Stop building a pipeline. Install the process list in the job array.
This returns the index of the newly installed job.
DEFERRED is a command structure to be executed upon satisfactory
execution exit of this pipeline. */
int
stop_pipeline (async, deferred)
int async;
COMMAND *deferred;
{
register int i, j;
JOB *newjob;
sigset_t set, oset;
BLOCK_CHILD (set, oset);
#if defined (PGRP_PIPE)
/* The parent closes the process group synchronization pipe. */
sh_closepipe (pgrp_pipe);
#endif
cleanup_dead_jobs ();
if (js.j_jobslots == 0)
{
js.j_jobslots = JOB_SLOTS;
jobs = (JOB **)xmalloc (js.j_jobslots * sizeof (JOB *));
/* Now blank out these new entries. */
for (i = 0; i < js.j_jobslots; i++)
jobs[i] = (JOB *)NULL;
js.j_firstj = js.j_lastj = js.j_njobs = 0;
}
/* Scan from the last slot backward, looking for the next free one. */
/* XXX - revisit this interactive assumption */
/* XXX - this way for now */
if (interactive)
{
for (i = js.j_jobslots; i; i--)
if (jobs[i - 1])
break;
}
else
{
#if 0
/* This wraps around, but makes it inconvenient to extend the array */
for (i = js.j_lastj+1; i != js.j_lastj; i++)
{
if (i >= js.j_jobslots)
i = 0;
if (jobs[i] == 0)
break;
}
if (i == js.j_lastj)
i = js.j_jobslots;
#else
/* This doesn't wrap around yet. */
for (i = js.j_lastj ? js.j_lastj + 1 : js.j_lastj; i < js.j_jobslots; i++)
if (jobs[i] == 0)
break;
#endif
}
/* Do we need more room? */
/* First try compaction */
if ((interactive_shell == 0 || subshell_environment) && i == js.j_jobslots && js.j_jobslots >= MAX_JOBS_IN_ARRAY)
i = compact_jobs_list (0);
/* If we can't compact, reallocate */
if (i == js.j_jobslots)
{
js.j_jobslots += JOB_SLOTS;
jobs = (JOB **)xrealloc (jobs, (js.j_jobslots * sizeof (JOB *)));
for (j = i; j < js.j_jobslots; j++)
jobs[j] = (JOB *)NULL;
}
/* Add the current pipeline to the job list. */
if (the_pipeline)
{
register PROCESS *p;
int any_running, any_stopped, n;
newjob = (JOB *)xmalloc (sizeof (JOB));
for (n = 1, p = the_pipeline; p->next != the_pipeline; n++, p = p->next)
;
p->next = (PROCESS *)NULL;
newjob->pipe = REVERSE_LIST (the_pipeline, PROCESS *);
for (p = newjob->pipe; p->next; p = p->next)
;
p->next = newjob->pipe;
the_pipeline = (PROCESS *)NULL;
newjob->pgrp = pipeline_pgrp;
/* Invariant: if the shell is executing a command substitution,
pipeline_pgrp == shell_pgrp. Other parts of the shell assume this. */
if (pipeline_pgrp != shell_pgrp)
pipeline_pgrp = 0;
newjob->flags = 0;
if (pipefail_opt)
newjob->flags |= J_PIPEFAIL;
/* Flag to see if in another pgrp. */
if (job_control)
newjob->flags |= J_JOBCONTROL;
/* Set the state of this pipeline. */
p = newjob->pipe;
any_running = any_stopped = 0;
do
{
any_running |= PRUNNING (p);
any_stopped |= PSTOPPED (p);
p = p->next;
}
while (p != newjob->pipe);
newjob->state = any_running ? JRUNNING : (any_stopped ? JSTOPPED : JDEAD);
newjob->wd = job_working_directory ();
newjob->deferred = deferred;
newjob->j_cleanup = (sh_vptrfunc_t *)NULL;
newjob->cleanarg = (PTR_T) NULL;
jobs[i] = newjob;
if (newjob->state == JDEAD && (newjob->flags & J_FOREGROUND))
setjstatus (i);
if (newjob->state == JDEAD)
{
js.c_reaped += n; /* wouldn't have been done since this was not part of a job */
js.j_ndead++;
}
js.c_injobs += n;
js.j_lastj = i;
js.j_njobs++;
}
else
newjob = (JOB *)NULL;
if (newjob)
js.j_lastmade = newjob;
if (async)
{
if (newjob)
{
newjob->flags &= ~J_FOREGROUND;
newjob->flags |= J_ASYNC;
js.j_lastasync = newjob;
}
reset_current ();
}
else
{
if (newjob)
{
newjob->flags |= J_FOREGROUND;
/*
* !!!!! NOTE !!!!! ([email protected])
*
* The currently-accepted job control wisdom says to set the
* terminal's process group n+1 times in an n-step pipeline:
* once in the parent and once in each child. This is where
* the parent gives it away.
*
* Don't give the terminal away if this shell is an asynchronous
* subshell or if we're a (presumably non-interactive) shell running
* in the background.
*
*/
if (job_control && newjob->pgrp && (subshell_environment&SUBSHELL_ASYNC) == 0 && running_in_background == 0)
maybe_give_terminal_to (shell_pgrp, newjob->pgrp, 0);
}
}
stop_making_children ();
UNBLOCK_CHILD (oset);
return (newjob ? i : js.j_current);
}
/* Functions to manage the list of exited background pids whose status has
been saved.
pidstat_table:
The current implementation is a hash table using a single (separate) arena
for storage that can be allocated and freed as a unit. The size of the hash
table is a multiple of PIDSTAT_TABLE_SZ (4096) and multiple PIDs that hash
to the same value are chained through the bucket_next and bucket_prev
pointers (basically coalesced hashing for collision resolution).
bgpids.storage:
All pid/status storage is done using the circular buffer bgpids.storage.
This must contain at least js.c_childmax entries. The circular buffer is
used to supply the ordered list Posix requires ("the last CHILD_MAX
processes"). To avoid searching the entire storage table for a given PID,
the hash table (pidstat_table) holds pointers into the storage arena and
uses a doubly-linked list of cells (bucket_next/bucket_prev, also pointers
into the arena) to implement collision resolution. */
/* The number of elements in bgpids.storage always has to be > js.c_childmax for
the circular buffer to work right. */
static void
bgp_resize ()
{
ps_index_t nsize, nsize_cur, nsize_max;
ps_index_t psi;
if (bgpids.nalloc == 0)
{
/* invalidate hash table when bgpids table is reallocated */
for (psi = 0; psi < PIDSTAT_TABLE_SZ; psi++)
pidstat_table[psi] = NO_PIDSTAT;
nsize = BGPIDS_TABLE_SZ; /* should be power of 2 */
bgpids.head = 0;
}
else
nsize = bgpids.nalloc;
nsize_max = TYPE_MAXIMUM (ps_index_t);
nsize_cur = (ps_index_t)js.c_childmax;
if (nsize_cur < 0) /* overflow */
nsize_cur = MAX_CHILD_MAX;
while (nsize > 0 && nsize < nsize_cur) /* > 0 should catch overflow */
nsize <<= 1;
if (nsize > nsize_max || nsize <= 0) /* overflow? */
nsize = nsize_max;
if (nsize > MAX_CHILD_MAX)
nsize = nsize_max = MAX_CHILD_MAX; /* hard cap */
if (bgpids.nalloc < nsize_cur && bgpids.nalloc < nsize_max)
{
bgpids.storage = (struct pidstat *)xrealloc (bgpids.storage, nsize * sizeof (struct pidstat));
for (psi = bgpids.nalloc; psi < nsize; psi++)
bgpids.storage[psi].pid = NO_PID;
bgpids.nalloc = nsize;
}
else if (bgpids.head >= bgpids.nalloc) /* wrap around */
bgpids.head = 0;
}
static ps_index_t
bgp_getindex ()
{
if (bgpids.nalloc < (ps_index_t)js.c_childmax || bgpids.head >= bgpids.nalloc)
bgp_resize ();
pshash_delindex (bgpids.head); /* XXX - clear before reusing */
return bgpids.head++;
}
static ps_index_t *
pshash_getbucket (pid)
pid_t pid;
{
unsigned long hash; /* XXX - u_bits32_t */
hash = pid * 0x9e370001UL;
return (&pidstat_table[hash % PIDSTAT_TABLE_SZ]);
}
static struct pidstat *
bgp_add (pid, status)
pid_t pid;
int status;
{
ps_index_t *bucket, psi;
struct pidstat *ps;
/* bucket == existing chain of pids hashing to same value
psi = where were going to put this pid/status */
bucket = pshash_getbucket (pid); /* index into pidstat_table */
psi = bgp_getindex (); /* bgpids.head, index into storage */
/* XXX - what if psi == *bucket? */
if (psi == *bucket)
{
internal_debug ("hashed pid %d (pid %d) collides with bgpids.head, skipping", psi, pid);
bgpids.storage[psi].pid = NO_PID; /* make sure */
psi = bgp_getindex (); /* skip to next one */
}
ps = &bgpids.storage[psi];
ps->pid = pid;
ps->status = status;
ps->bucket_next = *bucket;
ps->bucket_prev = NO_PIDSTAT;
bgpids.npid++;
#if 0
if (bgpids.npid > js.c_childmax)
bgp_prune ();
#endif
if (ps->bucket_next != NO_PIDSTAT)
bgpids.storage[ps->bucket_next].bucket_prev = psi;
*bucket = psi; /* set chain head in hash table */
return ps;
}
static void
pshash_delindex (psi)
ps_index_t psi;
{
struct pidstat *ps;
ps_index_t *bucket;
ps = &bgpids.storage[psi];
if (ps->pid == NO_PID)
return;
if (ps->bucket_next != NO_PIDSTAT)
bgpids.storage[ps->bucket_next].bucket_prev = ps->bucket_prev;
if (ps->bucket_prev != NO_PIDSTAT)
bgpids.storage[ps->bucket_prev].bucket_next = ps->bucket_next;
else
{
bucket = pshash_getbucket (ps->pid);
*bucket = ps->bucket_next; /* deleting chain head in hash table */
}
/* clear out this cell, in case it gets reused. */
ps->pid = NO_PID;
ps->bucket_next = ps->bucket_prev = NO_PIDSTAT;
}
static int
bgp_delete (pid)
pid_t pid;
{
ps_index_t psi, orig_psi;
if (bgpids.storage == 0 || bgpids.nalloc == 0 || bgpids.npid == 0)
return 0;
/* Search chain using hash to find bucket in pidstat_table */
for (orig_psi = psi = *(pshash_getbucket (pid)); psi != NO_PIDSTAT; psi = bgpids.storage[psi].bucket_next)
{
if (bgpids.storage[psi].pid == pid)
break;
if (orig_psi == bgpids.storage[psi].bucket_next) /* catch reported bug */
{
internal_warning (_("bgp_delete: LOOP: psi (%d) == storage[psi].bucket_next"), psi);
return 0;
}
}
if (psi == NO_PIDSTAT)
return 0; /* not found */
#if 0
itrace("bgp_delete: deleting %d", pid);
#endif
pshash_delindex (psi); /* hash table management */
bgpids.npid--;
return 1;
}
/* Clear out the list of saved statuses */
static void
bgp_clear ()
{
if (bgpids.storage == 0 || bgpids.nalloc == 0)
return;
free (bgpids.storage);
bgpids.storage = 0;
bgpids.nalloc = 0;
bgpids.head = 0;
bgpids.npid = 0;
}
/* Search for PID in the list of saved background pids; return its status if
found. If not found, return -1. We hash to the right spot in pidstat_table
and follow the bucket chain to the end. */
static int
bgp_search (pid)
pid_t pid;
{
ps_index_t psi, orig_psi;
if (bgpids.storage == 0 || bgpids.nalloc == 0 || bgpids.npid == 0)
return -1;
/* Search chain using hash to find bucket in pidstat_table */
for (orig_psi = psi = *(pshash_getbucket (pid)); psi != NO_PIDSTAT; psi = bgpids.storage[psi].bucket_next)
{
if (bgpids.storage[psi].pid == pid)
return (bgpids.storage[psi].status);
if (orig_psi == bgpids.storage[psi].bucket_next) /* catch reported bug */
{
internal_warning (_("bgp_search: LOOP: psi (%d) == storage[psi].bucket_next"), psi);
return -1;
}
}
return -1;
}
#if 0
static void
bgp_prune ()
{
return;
}
#endif
/* External interface to bgp_add; takes care of blocking and unblocking
SIGCHLD. Not really used. */
void
save_proc_status (pid, status)
pid_t pid;
int status;
{
sigset_t set, oset;
BLOCK_CHILD (set, oset);
bgp_add (pid, status);
UNBLOCK_CHILD (oset);
}
#if defined (PROCESS_SUBSTITUTION)
/* Functions to add and remove PROCESS * children from the list of running
asynchronous process substitutions. The list is currently a simple singly
linked list of PROCESS *, so it works with the set of callers that want
a child. subst.c:process_substitute adds to the list, the various wait*
functions manipulate child->running and child->status, and processes are
eventually removed from the list and added to the bgpids table. */
static void
procsub_free (p)
PROCESS *p;