Patch-ID: bash32-011
[bash.git] / jobs.h
blob93a42fb2acebfe5aac15d0a31bec3639a22466f1
1 /* jobs.h -- structures and stuff used by the jobs.c file. */
3 /* Copyright (C) 1993-2005 Free Software Foundation, Inc.
5 This file is part of GNU Bash, the Bourne Again SHell.
7 Bash is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 Bash is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License along
18 with Bash; see the file COPYING. If not, write to the Free Software
19 Foundation, 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
21 #if !defined (_JOBS_H_)
22 # define _JOBS_H_
24 #include "quit.h"
25 #include "siglist.h"
27 #include "stdc.h"
29 #include "posixwait.h"
31 /* Defines controlling the fashion in which jobs are listed. */
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
38 /* I looked it up. For pretty_print_job (). The real answer is 24. */
39 #define LONGEST_SIGNAL_DESC 24
41 /* We keep an array of jobs. Each entry in the array is a linked list
42 of processes that are piped together. The first process encountered is
43 the group leader. */
45 /* Values for the `running' field of a struct process. */
46 #define PS_DONE 0
47 #define PS_RUNNING 1
48 #define PS_STOPPED 2
49 #define PS_RECYCLED 4
51 /* Each child of the shell is remembered in a STRUCT PROCESS. A circular
52 chain of such structures is a pipeline. */
53 typedef struct process {
54 struct process *next; /* Next process in the pipeline. A circular chain. */
55 pid_t pid; /* Process ID. */
56 WAIT status; /* The status of this command as returned by wait. */
57 int running; /* Non-zero if this process is running. */
58 char *command; /* The particular program that is running. */
59 } PROCESS;
61 /* PALIVE really means `not exited' */
62 #define PSTOPPED(p) (WIFSTOPPED((p)->status))
63 #define PRUNNING(p) ((p)->running == PS_RUNNING)
64 #define PALIVE(p) (PRUNNING(p) || PSTOPPED(p))
66 #define PEXITED(p) ((p)->running == PS_DONE)
67 #if defined (RECYCLES_PIDS)
68 # define PRECYCLED(p) ((p)->running == PS_RECYCLED)
69 #else
70 # define PRECYCLED(p) (0)
71 #endif
72 #define PDEADPROC(p) (PEXITED(p) || PRECYCLED(p))
74 #define get_job_by_jid(ind) (jobs[(ind)])
76 /* A description of a pipeline's state. */
77 typedef enum { JRUNNING, JSTOPPED, JDEAD, JMIXED } JOB_STATE;
78 #define JOBSTATE(job) (jobs[(job)]->state)
79 #define J_JOBSTATE(j) ((j)->state)
81 #define STOPPED(j) (jobs[(j)]->state == JSTOPPED)
82 #define RUNNING(j) (jobs[(j)]->state == JRUNNING)
83 #define DEADJOB(j) (jobs[(j)]->state == JDEAD)
85 #define INVALID_JOB(j) ((j) < 0 || (j) >= js.j_jobslots || get_job_by_jid(j) == 0)
87 /* Values for the FLAGS field in the JOB struct below. */
88 #define J_FOREGROUND 0x01 /* Non-zero if this is running in the foreground. */
89 #define J_NOTIFIED 0x02 /* Non-zero if already notified about job state. */
90 #define J_JOBCONTROL 0x04 /* Non-zero if this job started under job control. */
91 #define J_NOHUP 0x08 /* Don't send SIGHUP to job if shell gets SIGHUP. */
92 #define J_STATSAVED 0x10 /* A process in this job had had status saved via $! */
93 #define J_ASYNC 0x20 /* Job was started asynchronously */
95 #define IS_FOREGROUND(j) ((jobs[j]->flags & J_FOREGROUND) != 0)
96 #define IS_NOTIFIED(j) ((jobs[j]->flags & J_NOTIFIED) != 0)
97 #define IS_JOBCONTROL(j) ((jobs[j]->flags & J_JOBCONTROL) != 0)
98 #define IS_ASYNC(j) ((jobs[j]->flags & J_ASYNC) != 0)
100 typedef struct job {
101 char *wd; /* The working directory at time of invocation. */
102 PROCESS *pipe; /* The pipeline of processes that make up this job. */
103 pid_t pgrp; /* The process ID of the process group (necessary). */
104 JOB_STATE state; /* The state that this job is in. */
105 int flags; /* Flags word: J_NOTIFIED, J_FOREGROUND, or J_JOBCONTROL. */
106 #if defined (JOB_CONTROL)
107 COMMAND *deferred; /* Commands that will execute when this job is done. */
108 sh_vptrfunc_t *j_cleanup; /* Cleanup function to call when job marked JDEAD */
109 PTR_T cleanarg; /* Argument passed to (*j_cleanup)() */
110 #endif /* JOB_CONTROL */
111 } JOB;
113 struct jobstats {
114 /* limits */
115 long c_childmax;
116 /* child process statistics */
117 int c_living; /* running or stopped child processes */
118 int c_reaped; /* exited child processes still in jobs list */
119 int c_injobs; /* total number of child processes in jobs list */
120 /* child process totals */
121 int c_totforked; /* total number of children this shell has forked */
122 int c_totreaped; /* total number of children this shell has reaped */
123 /* job counters and indices */
124 int j_jobslots; /* total size of jobs array */
125 int j_lastj; /* last (newest) job allocated */
126 int j_firstj; /* first (oldest) job allocated */
127 int j_njobs; /* number of non-NULL jobs in jobs array */
128 int j_ndead; /* number of JDEAD jobs in jobs array */
129 /* */
130 int j_current; /* current job */
131 int j_previous; /* previous job */
132 /* */
133 JOB *j_lastmade; /* last job allocated by stop_pipeline */
134 JOB *j_lastasync; /* last async job allocated by stop_pipeline */
137 struct pidstat {
138 struct pidstat *next;
139 pid_t pid;
140 int status;
143 struct bgpids {
144 struct pidstat *list;
145 struct pidstat *end;
146 int npid;
149 #define NO_JOB -1 /* An impossible job array index. */
150 #define DUP_JOB -2 /* A possible return value for get_job_spec (). */
151 #define BAD_JOBSPEC -3 /* Bad syntax for job spec. */
153 /* A value which cannot be a process ID. */
154 #define NO_PID (pid_t)-1
156 /* System calls. */
157 #if !defined (HAVE_UNISTD_H)
158 extern pid_t fork (), getpid (), getpgrp ();
159 #endif /* !HAVE_UNISTD_H */
161 /* Stuff from the jobs.c file. */
162 extern struct jobstats js;
164 extern pid_t original_pgrp, shell_pgrp, pipeline_pgrp;
165 extern pid_t last_made_pid, last_asynchronous_pid;
166 extern int asynchronous_notification;
168 extern JOB **jobs;
170 extern void making_children __P((void));
171 extern void stop_making_children __P((void));
172 extern void cleanup_the_pipeline __P((void));
173 extern void save_pipeline __P((int));
174 extern void restore_pipeline __P((int));
175 extern void start_pipeline __P((void));
176 extern int stop_pipeline __P((int, COMMAND *));
178 extern void delete_job __P((int, int));
179 extern void nohup_job __P((int));
180 extern void delete_all_jobs __P((int));
181 extern void nohup_all_jobs __P((int));
183 extern int count_all_jobs __P((void));
185 extern void terminate_current_pipeline __P((void));
186 extern void terminate_stopped_jobs __P((void));
187 extern void hangup_all_jobs __P((void));
188 extern void kill_current_pipeline __P((void));
190 #if defined (__STDC__) && defined (pid_t)
191 extern int get_job_by_pid __P((int, int));
192 extern void describe_pid __P((int));
193 #else
194 extern int get_job_by_pid __P((pid_t, int));
195 extern void describe_pid __P((pid_t));
196 #endif
198 extern void list_one_job __P((JOB *, int, int, int));
199 extern void list_all_jobs __P((int));
200 extern void list_stopped_jobs __P((int));
201 extern void list_running_jobs __P((int));
203 extern pid_t make_child __P((char *, int));
205 extern int get_tty_state __P((void));
206 extern int set_tty_state __P((void));
208 extern int wait_for_single_pid __P((pid_t));
209 extern void wait_for_background_pids __P((void));
210 extern int wait_for __P((pid_t));
211 extern int wait_for_job __P((int));
213 extern void notify_and_cleanup __P((void));
214 extern void reap_dead_jobs __P((void));
215 extern int start_job __P((int, int));
216 extern int kill_pid __P((pid_t, int, int));
217 extern int initialize_job_control __P((int));
218 extern void initialize_job_signals __P((void));
219 extern int give_terminal_to __P((pid_t, int));
221 extern void unfreeze_jobs_list __P((void));
222 extern int set_job_control __P((int));
223 extern void without_job_control __P((void));
224 extern void end_job_control __P((void));
225 extern void restart_job_control __P((void));
226 extern void set_sigchld_handler __P((void));
227 extern void ignore_tty_job_signals __P((void));
228 extern void default_tty_job_signals __P((void));
230 extern void init_job_stats __P((void));
232 #if defined (JOB_CONTROL)
233 extern int job_control;
234 #endif
236 #endif /* _JOBS_H_ */