]> git.ipfire.org Git - thirdparty/bash.git/blame - jobs.h
Bash-4.4 distribution sources and documentation
[thirdparty/bash.git] / jobs.h
CommitLineData
3185942a 1/* jobs.h -- structures and definitions used by the jobs.c file. */
726f6388 2
a0c0a00f 3/* Copyright (C) 1993-2015 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
3185942a
JA
41/* The max time to sleep while retrying fork() on EAGAIN failure */
42#define FORKSLEEP_MAX 16
43
726f6388
JA
44/* We keep an array of jobs. Each entry in the array is a linked list
45 of processes that are piped together. The first process encountered is
46 the group leader. */
47
f73dda09
JA
48/* Values for the `running' field of a struct process. */
49#define PS_DONE 0
50#define PS_RUNNING 1
51#define PS_STOPPED 2
95732b49 52#define PS_RECYCLED 4
f73dda09 53
95732b49
JA
54/* Each child of the shell is remembered in a STRUCT PROCESS. A circular
55 chain of such structures is a pipeline. */
726f6388
JA
56typedef struct process {
57 struct process *next; /* Next process in the pipeline. A circular chain. */
58 pid_t pid; /* Process ID. */
59 WAIT status; /* The status of this command as returned by wait. */
60 int running; /* Non-zero if this process is running. */
61 char *command; /* The particular program that is running. */
62} PROCESS;
63
a0c0a00f
CR
64struct pipeline_saver {
65 struct process *pipeline;
66 struct pipeline_saver *next;
67};
68
95732b49 69/* PALIVE really means `not exited' */
7117c2d2 70#define PSTOPPED(p) (WIFSTOPPED((p)->status))
95732b49
JA
71#define PRUNNING(p) ((p)->running == PS_RUNNING)
72#define PALIVE(p) (PRUNNING(p) || PSTOPPED(p))
73
74#define PEXITED(p) ((p)->running == PS_DONE)
75#if defined (RECYCLES_PIDS)
76# define PRECYCLED(p) ((p)->running == PS_RECYCLED)
77#else
78# define PRECYCLED(p) (0)
79#endif
80#define PDEADPROC(p) (PEXITED(p) || PRECYCLED(p))
81
82#define get_job_by_jid(ind) (jobs[(ind)])
7117c2d2 83
726f6388 84/* A description of a pipeline's state. */
3185942a 85typedef enum { JNONE = -1, JRUNNING = 1, JSTOPPED = 2, JDEAD = 4, JMIXED = 8 } JOB_STATE;
95732b49
JA
86#define JOBSTATE(job) (jobs[(job)]->state)
87#define J_JOBSTATE(j) ((j)->state)
726f6388 88
ccc6cda3
JA
89#define STOPPED(j) (jobs[(j)]->state == JSTOPPED)
90#define RUNNING(j) (jobs[(j)]->state == JRUNNING)
91#define DEADJOB(j) (jobs[(j)]->state == JDEAD)
92
95732b49
JA
93#define INVALID_JOB(j) ((j) < 0 || (j) >= js.j_jobslots || get_job_by_jid(j) == 0)
94
726f6388
JA
95/* Values for the FLAGS field in the JOB struct below. */
96#define J_FOREGROUND 0x01 /* Non-zero if this is running in the foreground. */
97#define J_NOTIFIED 0x02 /* Non-zero if already notified about job state. */
98#define J_JOBCONTROL 0x04 /* Non-zero if this job started under job control. */
ccc6cda3 99#define J_NOHUP 0x08 /* Don't send SIGHUP to job if shell gets SIGHUP. */
95732b49
JA
100#define J_STATSAVED 0x10 /* A process in this job had had status saved via $! */
101#define J_ASYNC 0x20 /* Job was started asynchronously */
ccc6cda3
JA
102
103#define IS_FOREGROUND(j) ((jobs[j]->flags & J_FOREGROUND) != 0)
104#define IS_NOTIFIED(j) ((jobs[j]->flags & J_NOTIFIED) != 0)
105#define IS_JOBCONTROL(j) ((jobs[j]->flags & J_JOBCONTROL) != 0)
95732b49 106#define IS_ASYNC(j) ((jobs[j]->flags & J_ASYNC) != 0)
726f6388
JA
107
108typedef struct job {
109 char *wd; /* The working directory at time of invocation. */
110 PROCESS *pipe; /* The pipeline of processes that make up this job. */
111 pid_t pgrp; /* The process ID of the process group (necessary). */
112 JOB_STATE state; /* The state that this job is in. */
113 int flags; /* Flags word: J_NOTIFIED, J_FOREGROUND, or J_JOBCONTROL. */
114#if defined (JOB_CONTROL)
115 COMMAND *deferred; /* Commands that will execute when this job is done. */
f73dda09 116 sh_vptrfunc_t *j_cleanup; /* Cleanup function to call when job marked JDEAD */
ccc6cda3 117 PTR_T cleanarg; /* Argument passed to (*j_cleanup)() */
726f6388
JA
118#endif /* JOB_CONTROL */
119} JOB;
120
95732b49
JA
121struct jobstats {
122 /* limits */
123 long c_childmax;
124 /* child process statistics */
125 int c_living; /* running or stopped child processes */
126 int c_reaped; /* exited child processes still in jobs list */
127 int c_injobs; /* total number of child processes in jobs list */
128 /* child process totals */
129 int c_totforked; /* total number of children this shell has forked */
130 int c_totreaped; /* total number of children this shell has reaped */
131 /* job counters and indices */
132 int j_jobslots; /* total size of jobs array */
133 int j_lastj; /* last (newest) job allocated */
134 int j_firstj; /* first (oldest) job allocated */
135 int j_njobs; /* number of non-NULL jobs in jobs array */
136 int j_ndead; /* number of JDEAD jobs in jobs array */
137 /* */
138 int j_current; /* current job */
139 int j_previous; /* previous job */
140 /* */
141 JOB *j_lastmade; /* last job allocated by stop_pipeline */
142 JOB *j_lastasync; /* last async job allocated by stop_pipeline */
143};
144
a0c0a00f
CR
145/* Revised to accommodate new hash table bgpids implementation. */
146typedef pid_t ps_index_t;
147
95732b49 148struct pidstat {
a0c0a00f
CR
149 ps_index_t bucket_next;
150 ps_index_t bucket_prev;
151
152 pid_t pid;
153 bits16_t status; /* only 8 bits really needed */
95732b49
JA
154};
155
156struct bgpids {
a0c0a00f
CR
157 struct pidstat *storage; /* storage arena */
158
159 ps_index_t head;
160 ps_index_t nalloc;
161
95732b49
JA
162 int npid;
163};
164
a0c0a00f
CR
165#define NO_PIDSTAT (ps_index_t)-1
166
726f6388
JA
167#define NO_JOB -1 /* An impossible job array index. */
168#define DUP_JOB -2 /* A possible return value for get_job_spec (). */
b80f6443 169#define BAD_JOBSPEC -3 /* Bad syntax for job spec. */
726f6388
JA
170
171/* A value which cannot be a process ID. */
172#define NO_PID (pid_t)-1
173
ac50fbac
CR
174#define ANY_PID (pid_t)-1
175
726f6388 176/* System calls. */
ccc6cda3 177#if !defined (HAVE_UNISTD_H)
726f6388 178extern pid_t fork (), getpid (), getpgrp ();
ccc6cda3 179#endif /* !HAVE_UNISTD_H */
726f6388
JA
180
181/* Stuff from the jobs.c file. */
95732b49
JA
182extern struct jobstats js;
183
bb70624e 184extern pid_t original_pgrp, shell_pgrp, pipeline_pgrp;
ac50fbac 185extern volatile pid_t last_made_pid, last_asynchronous_pid;
726f6388 186extern int asynchronous_notification;
95732b49 187
726f6388 188extern JOB **jobs;
726f6388
JA
189
190extern void making_children __P((void));
191extern void stop_making_children __P((void));
192extern void cleanup_the_pipeline __P((void));
ccc6cda3 193extern void save_pipeline __P((int));
a0c0a00f 194extern PROCESS *restore_pipeline __P((int));
726f6388
JA
195extern void start_pipeline __P((void));
196extern int stop_pipeline __P((int, COMMAND *));
a0c0a00f 197extern int discard_pipeline __P((PROCESS *));
495aee44 198extern void append_process __P((char *, pid_t, int, int));
d166f048 199
cce855bc 200extern void delete_job __P((int, int));
ccc6cda3 201extern void nohup_job __P((int));
cce855bc
JA
202extern void delete_all_jobs __P((int));
203extern void nohup_all_jobs __P((int));
726f6388 204
bb70624e
JA
205extern int count_all_jobs __P((void));
206
726f6388
JA
207extern void terminate_current_pipeline __P((void));
208extern void terminate_stopped_jobs __P((void));
209extern void hangup_all_jobs __P((void));
210extern void kill_current_pipeline __P((void));
211
212#if defined (__STDC__) && defined (pid_t)
d166f048 213extern int get_job_by_pid __P((int, int));
726f6388
JA
214extern void describe_pid __P((int));
215#else
d166f048 216extern int get_job_by_pid __P((pid_t, int));
726f6388
JA
217extern void describe_pid __P((pid_t));
218#endif
219
ccc6cda3
JA
220extern void list_one_job __P((JOB *, int, int, int));
221extern void list_all_jobs __P((int));
222extern void list_stopped_jobs __P((int));
223extern void list_running_jobs __P((int));
726f6388
JA
224
225extern pid_t make_child __P((char *, int));
bb70624e 226
726f6388
JA
227extern int get_tty_state __P((void));
228extern int set_tty_state __P((void));
229
495aee44
CR
230extern int job_exit_status __P((int));
231extern int job_exit_signal __P((int));
232
a0c0a00f 233extern int wait_for_single_pid __P((pid_t, int));
726f6388
JA
234extern void wait_for_background_pids __P((void));
235extern int wait_for __P((pid_t));
236extern int wait_for_job __P((int));
ac50fbac 237extern int wait_for_any_job __P((void));
726f6388 238
a0c0a00f
CR
239extern void wait_sigint_cleanup __P((void));
240
726f6388
JA
241extern void notify_and_cleanup __P((void));
242extern void reap_dead_jobs __P((void));
243extern int start_job __P((int, int));
244extern int kill_pid __P((pid_t, int, int));
d166f048 245extern int initialize_job_control __P((int));
726f6388 246extern void initialize_job_signals __P((void));
28ef6c31 247extern int give_terminal_to __P((pid_t, int));
726f6388 248
3185942a
JA
249extern void run_sigchld_trap __P((int));
250
a0c0a00f 251extern int freeze_jobs_list __P((void));
ccc6cda3 252extern void unfreeze_jobs_list __P((void));
726f6388
JA
253extern int set_job_control __P((int));
254extern void without_job_control __P((void));
255extern void end_job_control __P((void));
256extern void restart_job_control __P((void));
257extern void set_sigchld_handler __P((void));
ccc6cda3
JA
258extern void ignore_tty_job_signals __P((void));
259extern void default_tty_job_signals __P((void));
a0c0a00f 260extern void get_original_tty_job_signals __P((void));
726f6388 261
95732b49
JA
262extern void init_job_stats __P((void));
263
3185942a 264extern void close_pgrp_pipe __P((void));
89a92869
CR
265extern void save_pgrp_pipe __P((int *, int));
266extern void restore_pgrp_pipe __P((int *));
3185942a 267
ac50fbac
CR
268extern void set_maxchild __P((int));
269
270extern int job_control; /* set to 0 in nojobs.c */
726f6388 271
ccc6cda3 272#endif /* _JOBS_H_ */