-
Notifications
You must be signed in to change notification settings - Fork 80
/
jobs.c
4240 lines (3617 loc) · 109 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-2009 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>
#include <sys/param.h>
#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 "jobs.h"
#include "execute_cmd.h"
#include "flags.h"
#include "builtins/builtext.h"
#include "builtins/common.h"
#if !defined (errno)
extern int errno;
#endif /* !errno */
#define DEFAULT_CHILD_MAX 32
#if !defined (DEBUG)
#define MAX_JOBS_IN_ARRAY 4096 /* production */
#else
#define MAX_JOBS_IN_ARRAY 128 /* testing */
#endif
/* 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 __P((JOB *, int, int, int));
/* Variables used here but defined in other files. */
extern int subshell_environment, line_number;
extern int posixly_correct, shell_level;
extern int last_command_exit_value, last_command_exit_signal;
extern int loop_level, breaking;
extern int executing_list;
extern int sourcelevel;
extern int running_trap;
extern sh_builtin_func_t *this_shell_builtin;
extern char *shell_name, *this_command_name;
extern sigset_t top_level_mask;
extern procenv_t wait_intr_buf;
extern int wait_signal_received;
extern WORD_LIST *subst_assign_varlist;
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 };
struct bgpids bgpids = { 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
#if 0
/* The job which is current; i.e. the one that `%+' stands for. */
int current_job = NO_JOB;
/* The previous job; i.e. the one that `%-' stands for. */
int previous_job = NO_JOB;
#endif
/* Last child made by the shell. */
pid_t last_made_pid = NO_PID;
/* Pid of the last asynchronous child. */
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;
/* 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;
/* Functions local to this file. */
static sighandler wait_sigint_handler __P((int));
static sighandler sigchld_handler __P((int));
static sighandler sigcont_sighandler __P((int));
static sighandler sigstop_sighandler __P((int));
static int waitchld __P((pid_t, int));
static PROCESS *find_pipeline __P((pid_t, int, int *));
static PROCESS *find_process __P((pid_t, int, int *));
static char *current_working_directory __P((void));
static char *job_working_directory __P((void));
static char *j_strsignal __P((int));
static char *printable_job_status __P((int, PROCESS *, int));
static PROCESS *find_last_proc __P((int, int));
static pid_t find_last_pid __P((int, int));
static int set_new_line_discipline __P((int));
static int map_over_jobs __P((sh_job_map_func_t *, int, int));
static int job_last_stopped __P((int));
static int job_last_running __P((int));
static int most_recent_job_in_state __P((int, JOB_STATE));
static int find_job __P((pid_t, int, PROCESS **));
static int print_job __P((JOB *, int, int, int));
static int process_exit_status __P((WAIT));
static int process_exit_signal __P((WAIT));
static int job_exit_status __P((int));
static int job_exit_signal __P((int));
static int set_job_status_and_cleanup __P((int));
static WAIT job_signal_status __P((int));
static WAIT raw_job_exit_status __P((int));
static void notify_of_job_status __P((void));
static void reset_job_indices __P((void));
static void cleanup_dead_jobs __P((void));
static int processes_in_job __P((int));
static void realloc_jobs_list __P((void));
static int compact_jobs_list __P((int));
static int discard_pipeline __P((PROCESS *));
static void add_process __P((char *, pid_t));
static void print_pipeline __P((PROCESS *, int, int, FILE *));
static void pretty_print_job __P((int, int, FILE *));
static void set_current_job __P((int));
static void reset_current __P((void));
static void set_job_running __P((int));
static void setjstatus __P((int));
static int maybe_give_terminal_to __P((pid_t, pid_t, int));
static void mark_all_jobs_as_dead __P((void));
static void mark_dead_jobs_as_notified __P((int));
static void restore_sigint_handler __P((void));
#if defined (PGRP_PIPE)
static void pipe_read __P((int *));
#endif
static struct pidstat *bgp_alloc __P((pid_t, int));
static struct pidstat *bgp_add __P((pid_t, int));
static int bgp_delete __P((pid_t));
static void bgp_clear __P((void));
static int bgp_search __P((pid_t));
static void bgp_prune __P((void));
#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++
#define UNQUEUE_SIGCHLD(os) \
do { \
queue_sigchld--; \
if (queue_sigchld == 0 && os != sigchld) \
waitchld (-1, 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 PROCESS *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];
/* flags to detect pid wraparound */
static pid_t first_pid = NO_PID;
static int pid_wrap = -1;
#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;
first_pid = NO_PID;
pid_wrap = -1;
}
/* 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);
}
void
save_pipeline (clear)
int clear;
{
saved_pipeline = the_pipeline;
if (clear)
the_pipeline = (PROCESS *)NULL;
saved_already_making_children = already_making_children;
}
void
restore_pipeline (discard)
int discard;
{
PROCESS *old_pipeline;
old_pipeline = the_pipeline;
the_pipeline = saved_pipeline;
already_making_children = saved_already_making_children;
if (discard && old_pipeline)
discard_pipeline (old_pipeline);
}
/* Start building a pipeline. */
void
start_pipeline ()
{
if (the_pipeline)
{
cleanup_the_pipeline ();
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;
pipeline_pgrp = 0;
newjob->flags = 0;
/* 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.
*
*/
if (job_control && newjob->pgrp && (subshell_environment&SUBSHELL_ASYNC) == 0)
maybe_give_terminal_to (shell_pgrp, newjob->pgrp, 0);
}
}
stop_making_children ();
UNBLOCK_CHILD (oset);
return (js.j_current);
}
/* Functions to manage the list of exited background pids whose status has
been saved. */
static struct pidstat *
bgp_alloc (pid, status)
pid_t pid;
int status;
{
struct pidstat *ps;
ps = (struct pidstat *)xmalloc (sizeof (struct pidstat));
ps->pid = pid;
ps->status = status;
ps->next = (struct pidstat *)0;
return ps;
}
static struct pidstat *
bgp_add (pid, status)
pid_t pid;
int status;
{
struct pidstat *ps;
ps = bgp_alloc (pid, status);
if (bgpids.list == 0)
{
bgpids.list = bgpids.end = ps;
bgpids.npid = 0; /* just to make sure */
}
else
{
bgpids.end->next = ps;
bgpids.end = ps;
}
bgpids.npid++;
if (bgpids.npid > js.c_childmax)
bgp_prune ();
return ps;
}
static int
bgp_delete (pid)
pid_t pid;
{
struct pidstat *prev, *p;
for (prev = p = bgpids.list; p; prev = p, p = p->next)
if (p->pid == pid)
{
prev->next = p->next; /* remove from list */
break;
}
if (p == 0)
return 0; /* not found */
#if defined (DEBUG)
itrace("bgp_delete: deleting %d", pid);
#endif
/* Housekeeping in the border cases. */
if (p == bgpids.list)
bgpids.list = bgpids.list->next;
else if (p == bgpids.end)
bgpids.end = prev;
bgpids.npid--;
if (bgpids.npid == 0)
bgpids.list = bgpids.end = 0;
else if (bgpids.npid == 1)
bgpids.end = bgpids.list; /* just to make sure */
free (p);
return 1;
}
/* Clear out the list of saved statuses */
static void
bgp_clear ()
{
struct pidstat *ps, *p;
for (ps = bgpids.list; ps; )
{
p = ps;
ps = ps->next;
free (p);
}
bgpids.list = bgpids.end = 0;
bgpids.npid = 0;
}
/* Search for PID in the list of saved background pids; return its status if
found. If not found, return -1. */
static int
bgp_search (pid)
pid_t pid;
{
struct pidstat *ps;
for (ps = bgpids.list ; ps; ps = ps->next)
if (ps->pid == pid)
return ps->status;
return -1;
}
static void
bgp_prune ()
{
struct pidstat *ps;
while (bgpids.npid > js.c_childmax)
{
ps = bgpids.list;
bgpids.list = bgpids.list->next;
free (ps);
bgpids.npid--;
}
}
/* Reset the values of js.j_lastj and js.j_firstj after one or both have
been deleted. The caller should check whether js.j_njobs is 0 before
calling this. This wraps around, but the rest of the code does not. At
this point, it should not matter. */
static void
reset_job_indices ()
{
int old;
if (jobs[js.j_firstj] == 0)
{
old = js.j_firstj++;
if (old >= js.j_jobslots)
old = js.j_jobslots - 1;
while (js.j_firstj != old)
{
if (js.j_firstj >= js.j_jobslots)
js.j_firstj = 0;
if (jobs[js.j_firstj] || js.j_firstj == old) /* needed if old == 0 */
break;
js.j_firstj++;
}
if (js.j_firstj == old)
js.j_firstj = js.j_lastj = js.j_njobs = 0;
}
if (jobs[js.j_lastj] == 0)
{
old = js.j_lastj--;
if (old < 0)
old = 0;
while (js.j_lastj != old)
{
if (js.j_lastj < 0)
js.j_lastj = js.j_jobslots - 1;
if (jobs[js.j_lastj] || js.j_lastj == old) /* needed if old == js.j_jobslots */
break;
js.j_lastj--;
}
if (js.j_lastj == old)
js.j_firstj = js.j_lastj = js.j_njobs = 0;
}
}
/* Delete all DEAD jobs that the user had received notification about. */
static void
cleanup_dead_jobs ()
{
register int i;
int os;
if (js.j_jobslots == 0 || jobs_list_frozen)
return;
QUEUE_SIGCHLD(os);
/* XXX could use js.j_firstj and js.j_lastj here */
for (i = 0; i < js.j_jobslots; i++)
{
#if defined (DEBUG)
if (i < js.j_firstj && jobs[i])
itrace("cleanup_dead_jobs: job %d non-null before js.j_firstj (%d)", i, js.j_firstj);
if (i > js.j_lastj && jobs[i])
itrace("cleanup_dead_jobs: job %d non-null after js.j_lastj (%d)", i, js.j_lastj);
#endif
if (jobs[i] && DEADJOB (i) && IS_NOTIFIED (i))
delete_job (i, 0);
}
#if defined (COPROCESS_SUPPORT)
coproc_reap ();
#endif
UNQUEUE_SIGCHLD(os);
}
static int
processes_in_job (job)
int job;
{
int nproc;
register PROCESS *p;
nproc = 0;
p = jobs[job]->pipe;
do
{
p = p->next;
nproc++;
}
while (p != jobs[job]->pipe);
return nproc;
}
static void
delete_old_job (pid)
pid_t pid;
{
PROCESS *p;
int job;
job = find_job (pid, 0, &p);
if (job != NO_JOB)
{
#ifdef DEBUG
itrace ("delete_old_job: found pid %d in job %d with state %d", pid, job, jobs[job]->state);
#endif
if (JOBSTATE (job) == JDEAD)
delete_job (job, DEL_NOBGPID);
else
{
internal_warning (_("forked pid %d appears in running job %d"), pid, job);
if (p)
p->pid = 0;
}
}
}
/* Reallocate and compress the jobs list. This returns with a jobs array
whose size is a multiple of JOB_SLOTS and can hold the current number of
jobs. Heuristics are used to minimize the number of new reallocs. */
static void
realloc_jobs_list ()
{
sigset_t set, oset;
int nsize, i, j, ncur, nprev;
JOB **nlist;
ncur = nprev = NO_JOB;
nsize = ((js.j_njobs + JOB_SLOTS - 1) / JOB_SLOTS);
nsize *= JOB_SLOTS;
i = js.j_njobs % JOB_SLOTS;
if (i == 0 || i > (JOB_SLOTS >> 1))
nsize += JOB_SLOTS;
BLOCK_CHILD (set, oset);
nlist = (js.j_jobslots == nsize) ? jobs : (JOB **) xmalloc (nsize * sizeof (JOB *));
js.c_reaped = js.j_ndead = 0;
for (i = j = 0; i < js.j_jobslots; i++)
if (jobs[i])
{
if (i == js.j_current)
ncur = j;
if (i == js.j_previous)
nprev = j;
nlist[j++] = jobs[i];
if (jobs[i]->state == JDEAD)
{
js.j_ndead++;
js.c_reaped += processes_in_job (i);
}
}
#if defined (DEBUG)
itrace ("realloc_jobs_list: resize jobs list from %d to %d", js.j_jobslots, nsize);
itrace ("realloc_jobs_list: j_lastj changed from %d to %d", js.j_lastj, (j > 0) ? j - 1 : 0);
itrace ("realloc_jobs_list: j_njobs changed from %d to %d", js.j_njobs, j);
itrace ("realloc_jobs_list: js.j_ndead %d js.c_reaped %d", js.j_ndead, js.c_reaped);
#endif
js.j_firstj = 0;
js.j_lastj = (j > 0) ? j - 1 : 0;
js.j_njobs = j;
js.j_jobslots = nsize;
/* Zero out remaining slots in new jobs list */
for ( ; j < nsize; j++)
nlist[j] = (JOB *)NULL;
if (jobs != nlist)
{
free (jobs);
jobs = nlist;
}
if (ncur != NO_JOB)
js.j_current = ncur;
if (nprev != NO_JOB)
js.j_previous = nprev;
/* Need to reset these */
if (js.j_current == NO_JOB || js.j_previous == NO_JOB || js.j_current > js.j_lastj || js.j_previous > js.j_lastj)
reset_current ();
#ifdef DEBUG
itrace ("realloc_jobs_list: reset js.j_current (%d) and js.j_previous (%d)", js.j_current, js.j_previous);
#endif
UNBLOCK_CHILD (oset);
}
/* Compact the jobs list by removing dead jobs. Assumed that we have filled
the jobs array to some predefined maximum. Called when the shell is not
the foreground process (subshell_environment != 0). Returns the first
available slot in the compacted list. If that value is js.j_jobslots, then
the list needs to be reallocated. The jobs array may be in new memory if
this returns > 0 and < js.j_jobslots. FLAGS is reserved for future use. */
static int
compact_jobs_list (flags)
int flags;
{
if (js.j_jobslots == 0 || jobs_list_frozen)
return js.j_jobslots;
reap_dead_jobs ();
realloc_jobs_list ();
#ifdef DEBUG
itrace("compact_jobs_list: returning %d", (js.j_lastj || jobs[js.j_lastj]) ? js.j_lastj + 1 : 0);
#endif
return ((js.j_lastj || jobs[js.j_lastj]) ? js.j_lastj + 1 : 0);
}
/* Delete the job at INDEX from the job list. Must be called
with SIGCHLD blocked. */
void
delete_job (job_index, dflags)
int job_index, dflags;
{
register JOB *temp;
PROCESS *proc;
int ndel;