]>
Commit | Line | Data |
---|---|---|
3185942a | 1 | /* jobs.h -- structures and definitions used by the jobs.c file. */ |
726f6388 | 2 | |
d233b485 | 3 | /* Copyright (C) 1993-2017 Free Software Foundation, Inc. |
726f6388 JA |
4 | |
5 | This file is part of GNU Bash, the Bourne Again SHell. | |
6 | ||
3185942a JA |
7 | Bash is free software: you can redistribute it and/or modify |
8 | it under the terms of the GNU General Public License as published by | |
9 | the Free Software Foundation, either version 3 of the License, or | |
10 | (at your option) any later version. | |
726f6388 | 11 | |
3185942a JA |
12 | Bash is distributed in the hope that it will be useful, |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
726f6388 | 16 | |
3185942a JA |
17 | You should have received a copy of the GNU General Public License |
18 | along with Bash. If not, see <http://www.gnu.org/licenses/>. | |
19 | */ | |
726f6388 | 20 | |
ccc6cda3 JA |
21 | #if !defined (_JOBS_H_) |
22 | # define _JOBS_H_ | |
726f6388 JA |
23 | |
24 | #include "quit.h" | |
25 | #include "siglist.h" | |
26 | ||
27 | #include "stdc.h" | |
28 | ||
d166f048 JA |
29 | #include "posixwait.h" |
30 | ||
726f6388 | 31 | /* Defines controlling the fashion in which jobs are listed. */ |
ccc6cda3 JA |
32 | #define JLIST_STANDARD 0 |
33 | #define JLIST_LONG 1 | |
34 | #define JLIST_PID_ONLY 2 | |
35 | #define JLIST_CHANGED_ONLY 3 | |
36 | #define JLIST_NONINTERACTIVE 4 | |
37 | ||
726f6388 JA |
38 | /* I looked it up. For pretty_print_job (). The real answer is 24. */ |
39 | #define LONGEST_SIGNAL_DESC 24 | |
40 | ||
d233b485 CR |
41 | /* Defines for the wait_for functions and for the wait builtin to use */ |
42 | #define JWAIT_PERROR 0x01 | |
43 | #define JWAIT_FORCE 0x02 | |
44 | ||
3185942a JA |
45 | /* The max time to sleep while retrying fork() on EAGAIN failure */ |
46 | #define FORKSLEEP_MAX 16 | |
47 | ||
726f6388 JA |
48 | /* We keep an array of jobs. Each entry in the array is a linked list |
49 | of processes that are piped together. The first process encountered is | |
50 | the group leader. */ | |
51 | ||
f73dda09 JA |
52 | /* Values for the `running' field of a struct process. */ |
53 | #define PS_DONE 0 | |
54 | #define PS_RUNNING 1 | |
55 | #define PS_STOPPED 2 | |
95732b49 | 56 | #define PS_RECYCLED 4 |
f73dda09 | 57 | |
95732b49 JA |
58 | /* Each child of the shell is remembered in a STRUCT PROCESS. A circular |
59 | chain of such structures is a pipeline. */ | |
726f6388 JA |
60 | typedef struct process { |
61 | struct process *next; /* Next process in the pipeline. A circular chain. */ | |
62 | pid_t pid; /* Process ID. */ | |
63 | WAIT status; /* The status of this command as returned by wait. */ | |
64 | int running; /* Non-zero if this process is running. */ | |
65 | char *command; /* The particular program that is running. */ | |
66 | } PROCESS; | |
67 | ||
a0c0a00f CR |
68 | struct pipeline_saver { |
69 | struct process *pipeline; | |
70 | struct pipeline_saver *next; | |
71 | }; | |
72 | ||
95732b49 | 73 | /* PALIVE really means `not exited' */ |
7117c2d2 | 74 | #define PSTOPPED(p) (WIFSTOPPED((p)->status)) |
95732b49 JA |
75 | #define PRUNNING(p) ((p)->running == PS_RUNNING) |
76 | #define PALIVE(p) (PRUNNING(p) || PSTOPPED(p)) | |
77 | ||
78 | #define PEXITED(p) ((p)->running == PS_DONE) | |
79 | #if defined (RECYCLES_PIDS) | |
80 | # define PRECYCLED(p) ((p)->running == PS_RECYCLED) | |
81 | #else | |
82 | # define PRECYCLED(p) (0) | |
83 | #endif | |
84 | #define PDEADPROC(p) (PEXITED(p) || PRECYCLED(p)) | |
85 | ||
86 | #define get_job_by_jid(ind) (jobs[(ind)]) | |
7117c2d2 | 87 | |
726f6388 | 88 | /* A description of a pipeline's state. */ |
3185942a | 89 | typedef enum { JNONE = -1, JRUNNING = 1, JSTOPPED = 2, JDEAD = 4, JMIXED = 8 } JOB_STATE; |
95732b49 JA |
90 | #define JOBSTATE(job) (jobs[(job)]->state) |
91 | #define J_JOBSTATE(j) ((j)->state) | |
726f6388 | 92 | |
ccc6cda3 JA |
93 | #define STOPPED(j) (jobs[(j)]->state == JSTOPPED) |
94 | #define RUNNING(j) (jobs[(j)]->state == JRUNNING) | |
95 | #define DEADJOB(j) (jobs[(j)]->state == JDEAD) | |
96 | ||
95732b49 JA |
97 | #define INVALID_JOB(j) ((j) < 0 || (j) >= js.j_jobslots || get_job_by_jid(j) == 0) |
98 | ||
726f6388 JA |
99 | /* Values for the FLAGS field in the JOB struct below. */ |
100 | #define J_FOREGROUND 0x01 /* Non-zero if this is running in the foreground. */ | |
101 | #define J_NOTIFIED 0x02 /* Non-zero if already notified about job state. */ | |
102 | #define J_JOBCONTROL 0x04 /* Non-zero if this job started under job control. */ | |
ccc6cda3 | 103 | #define J_NOHUP 0x08 /* Don't send SIGHUP to job if shell gets SIGHUP. */ |
95732b49 JA |
104 | #define J_STATSAVED 0x10 /* A process in this job had had status saved via $! */ |
105 | #define J_ASYNC 0x20 /* Job was started asynchronously */ | |
d233b485 | 106 | #define J_PIPEFAIL 0x40 /* pipefail set when job was started */ |
ccc6cda3 JA |
107 | |
108 | #define IS_FOREGROUND(j) ((jobs[j]->flags & J_FOREGROUND) != 0) | |
109 | #define IS_NOTIFIED(j) ((jobs[j]->flags & J_NOTIFIED) != 0) | |
110 | #define IS_JOBCONTROL(j) ((jobs[j]->flags & J_JOBCONTROL) != 0) | |
95732b49 | 111 | #define IS_ASYNC(j) ((jobs[j]->flags & J_ASYNC) != 0) |
726f6388 JA |
112 | |
113 | typedef struct job { | |
114 | char *wd; /* The working directory at time of invocation. */ | |
115 | PROCESS *pipe; /* The pipeline of processes that make up this job. */ | |
116 | pid_t pgrp; /* The process ID of the process group (necessary). */ | |
117 | JOB_STATE state; /* The state that this job is in. */ | |
118 | int flags; /* Flags word: J_NOTIFIED, J_FOREGROUND, or J_JOBCONTROL. */ | |
119 | #if defined (JOB_CONTROL) | |
120 | COMMAND *deferred; /* Commands that will execute when this job is done. */ | |
f73dda09 | 121 | sh_vptrfunc_t *j_cleanup; /* Cleanup function to call when job marked JDEAD */ |
ccc6cda3 | 122 | PTR_T cleanarg; /* Argument passed to (*j_cleanup)() */ |
726f6388 JA |
123 | #endif /* JOB_CONTROL */ |
124 | } JOB; | |
125 | ||
95732b49 JA |
126 | struct jobstats { |
127 | /* limits */ | |
128 | long c_childmax; | |
129 | /* child process statistics */ | |
130 | int c_living; /* running or stopped child processes */ | |
131 | int c_reaped; /* exited child processes still in jobs list */ | |
132 | int c_injobs; /* total number of child processes in jobs list */ | |
133 | /* child process totals */ | |
134 | int c_totforked; /* total number of children this shell has forked */ | |
135 | int c_totreaped; /* total number of children this shell has reaped */ | |
136 | /* job counters and indices */ | |
137 | int j_jobslots; /* total size of jobs array */ | |
138 | int j_lastj; /* last (newest) job allocated */ | |
139 | int j_firstj; /* first (oldest) job allocated */ | |
140 | int j_njobs; /* number of non-NULL jobs in jobs array */ | |
141 | int j_ndead; /* number of JDEAD jobs in jobs array */ | |
142 | /* */ | |
143 | int j_current; /* current job */ | |
144 | int j_previous; /* previous job */ | |
145 | /* */ | |
146 | JOB *j_lastmade; /* last job allocated by stop_pipeline */ | |
147 | JOB *j_lastasync; /* last async job allocated by stop_pipeline */ | |
148 | }; | |
149 | ||
a0c0a00f CR |
150 | /* Revised to accommodate new hash table bgpids implementation. */ |
151 | typedef pid_t ps_index_t; | |
152 | ||
95732b49 | 153 | struct pidstat { |
a0c0a00f CR |
154 | ps_index_t bucket_next; |
155 | ps_index_t bucket_prev; | |
156 | ||
157 | pid_t pid; | |
158 | bits16_t status; /* only 8 bits really needed */ | |
95732b49 JA |
159 | }; |
160 | ||
161 | struct bgpids { | |
a0c0a00f CR |
162 | struct pidstat *storage; /* storage arena */ |
163 | ||
164 | ps_index_t head; | |
165 | ps_index_t nalloc; | |
166 | ||
95732b49 JA |
167 | int npid; |
168 | }; | |
169 | ||
a0c0a00f CR |
170 | #define NO_PIDSTAT (ps_index_t)-1 |
171 | ||
726f6388 JA |
172 | #define NO_JOB -1 /* An impossible job array index. */ |
173 | #define DUP_JOB -2 /* A possible return value for get_job_spec (). */ | |
b80f6443 | 174 | #define BAD_JOBSPEC -3 /* Bad syntax for job spec. */ |
726f6388 JA |
175 | |
176 | /* A value which cannot be a process ID. */ | |
177 | #define NO_PID (pid_t)-1 | |
178 | ||
ac50fbac CR |
179 | #define ANY_PID (pid_t)-1 |
180 | ||
726f6388 | 181 | /* System calls. */ |
ccc6cda3 | 182 | #if !defined (HAVE_UNISTD_H) |
726f6388 | 183 | extern pid_t fork (), getpid (), getpgrp (); |
ccc6cda3 | 184 | #endif /* !HAVE_UNISTD_H */ |
726f6388 JA |
185 | |
186 | /* Stuff from the jobs.c file. */ | |
95732b49 JA |
187 | extern struct jobstats js; |
188 | ||
bb70624e | 189 | extern pid_t original_pgrp, shell_pgrp, pipeline_pgrp; |
ac50fbac | 190 | extern volatile pid_t last_made_pid, last_asynchronous_pid; |
726f6388 | 191 | extern int asynchronous_notification; |
95732b49 | 192 | |
d233b485 CR |
193 | extern int already_making_children; |
194 | extern int running_in_background; | |
195 | ||
196 | extern PROCESS *last_procsub_child; | |
197 | ||
726f6388 | 198 | extern JOB **jobs; |
726f6388 JA |
199 | |
200 | extern void making_children __P((void)); | |
201 | extern void stop_making_children __P((void)); | |
202 | extern void cleanup_the_pipeline __P((void)); | |
2965eca9 | 203 | extern void discard_last_procsub_child __P((void)); |
ccc6cda3 | 204 | extern void save_pipeline __P((int)); |
a0c0a00f | 205 | extern PROCESS *restore_pipeline __P((int)); |
726f6388 JA |
206 | extern void start_pipeline __P((void)); |
207 | extern int stop_pipeline __P((int, COMMAND *)); | |
a0c0a00f | 208 | extern int discard_pipeline __P((PROCESS *)); |
495aee44 | 209 | extern void append_process __P((char *, pid_t, int, int)); |
d166f048 | 210 | |
cce855bc | 211 | extern void delete_job __P((int, int)); |
ccc6cda3 | 212 | extern void nohup_job __P((int)); |
cce855bc JA |
213 | extern void delete_all_jobs __P((int)); |
214 | extern void nohup_all_jobs __P((int)); | |
726f6388 | 215 | |
bb70624e JA |
216 | extern int count_all_jobs __P((void)); |
217 | ||
726f6388 JA |
218 | extern void terminate_current_pipeline __P((void)); |
219 | extern void terminate_stopped_jobs __P((void)); | |
220 | extern void hangup_all_jobs __P((void)); | |
221 | extern void kill_current_pipeline __P((void)); | |
222 | ||
223 | #if defined (__STDC__) && defined (pid_t) | |
d166f048 | 224 | extern int get_job_by_pid __P((int, int)); |
726f6388 JA |
225 | extern void describe_pid __P((int)); |
226 | #else | |
d166f048 | 227 | extern int get_job_by_pid __P((pid_t, int)); |
726f6388 JA |
228 | extern void describe_pid __P((pid_t)); |
229 | #endif | |
230 | ||
ccc6cda3 JA |
231 | extern void list_one_job __P((JOB *, int, int, int)); |
232 | extern void list_all_jobs __P((int)); | |
233 | extern void list_stopped_jobs __P((int)); | |
234 | extern void list_running_jobs __P((int)); | |
726f6388 JA |
235 | |
236 | extern pid_t make_child __P((char *, int)); | |
bb70624e | 237 | |
726f6388 JA |
238 | extern int get_tty_state __P((void)); |
239 | extern int set_tty_state __P((void)); | |
240 | ||
495aee44 CR |
241 | extern int job_exit_status __P((int)); |
242 | extern int job_exit_signal __P((int)); | |
243 | ||
a0c0a00f | 244 | extern int wait_for_single_pid __P((pid_t, int)); |
726f6388 JA |
245 | extern void wait_for_background_pids __P((void)); |
246 | extern int wait_for __P((pid_t)); | |
d233b485 CR |
247 | extern int wait_for_job __P((int, int)); |
248 | extern int wait_for_any_job __P((int)); | |
726f6388 | 249 | |
a0c0a00f CR |
250 | extern void wait_sigint_cleanup __P((void)); |
251 | ||
726f6388 JA |
252 | extern void notify_and_cleanup __P((void)); |
253 | extern void reap_dead_jobs __P((void)); | |
254 | extern int start_job __P((int, int)); | |
255 | extern int kill_pid __P((pid_t, int, int)); | |
d166f048 | 256 | extern int initialize_job_control __P((int)); |
726f6388 | 257 | extern void initialize_job_signals __P((void)); |
28ef6c31 | 258 | extern int give_terminal_to __P((pid_t, int)); |
726f6388 | 259 | |
3185942a JA |
260 | extern void run_sigchld_trap __P((int)); |
261 | ||
a0c0a00f | 262 | extern int freeze_jobs_list __P((void)); |
ccc6cda3 | 263 | extern void unfreeze_jobs_list __P((void)); |
d233b485 | 264 | extern void set_jobs_list_frozen __P((int)); |
726f6388 JA |
265 | extern int set_job_control __P((int)); |
266 | extern void without_job_control __P((void)); | |
267 | extern void end_job_control __P((void)); | |
268 | extern void restart_job_control __P((void)); | |
269 | extern void set_sigchld_handler __P((void)); | |
ccc6cda3 JA |
270 | extern void ignore_tty_job_signals __P((void)); |
271 | extern void default_tty_job_signals __P((void)); | |
a0c0a00f | 272 | extern void get_original_tty_job_signals __P((void)); |
726f6388 | 273 | |
95732b49 JA |
274 | extern void init_job_stats __P((void)); |
275 | ||
3185942a | 276 | extern void close_pgrp_pipe __P((void)); |
89a92869 CR |
277 | extern void save_pgrp_pipe __P((int *, int)); |
278 | extern void restore_pgrp_pipe __P((int *)); | |
3185942a | 279 | |
ac50fbac CR |
280 | extern void set_maxchild __P((int)); |
281 | ||
282 | extern int job_control; /* set to 0 in nojobs.c */ | |
726f6388 | 283 | |
ccc6cda3 | 284 | #endif /* _JOBS_H_ */ |