1 /***********************************************************************
2 *                                                                      *
3 *               This software is part of the ast package               *
4 *          Copyright (c) 1982-2009 AT&T Intellectual Property          *
5 *                      and is licensed under the                       *
6 *                  Common Public License, Version 1.0                  *
7 *                    by AT&T Intellectual Property                     *
8 *                                                                      *
9 *                A copy of the License is available at                 *
10 *            http://www.opensource.org/licenses/cpl1.0.txt             *
11 *         (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9)         *
12 *                                                                      *
13 *              Information and Software Systems Research               *
14 *                            AT&T Research                             *
15 *                           Florham Park NJ                            *
16 *                                                                      *
17 *                  David Korn <dgk@research.att.com>                   *
18 *                                                                      *
19 ***********************************************************************/
20 #pragma prototyped
21 #ifndef JOB_NFLAG
22 /*
23  *	Interface to job control for shell
24  *	written by David Korn
25  *
26  */
27 
28 #define JOBTTY	2
29 
30 #include	<ast.h>
31 #include	<sfio.h>
32 #ifndef SIGINT
33 #   include	<signal.h>
34 #endif /* !SIGINT */
35 #include	"FEATURE/options"
36 
37 #undef JOBS
38 #if defined(SIGCLD) && !defined(SIGCHLD)
39 #   define SIGCHLD	SIGCLD
40 #endif
41 #ifdef SIGCHLD
42 #   define JOBS	1
43 #   include	"terminal.h"
44 #   ifdef FIOLOOKLD
45 	/* Ninth edition */
46 	extern int tty_ld, ntty_ld;
47 #	define OTTYDISC	tty_ld
48 #	define NTTYDISC	ntty_ld
49 #   endif	/* FIOLOOKLD */
50 #else
51 #   undef SIGTSTP
52 #   undef SH_MONITOR
53 #   define SH_MONITOR	0
54 #   define job_set(x)
55 #   define job_reset(x)
56 #endif
57 
58 struct process
59 {
60 	struct process *p_nxtjob;	/* next job structure */
61 	struct process *p_nxtproc;	/* next process in current job */
62 	pid_t		p_pid;		/* process id */
63 	pid_t		p_pgrp;		/* process group */
64 	pid_t		p_fgrp;		/* process group when stopped */
65 	short		p_job;		/* job number of process */
66 	unsigned short	p_exit;		/* exit value or signal number */
67 	unsigned short	p_exitmin;	/* minimum exit value for xargs */
68 	unsigned short	p_flag;		/* flags - see below */
69 	int		p_env;		/* subshell environment number */
70 #ifdef JOBS
71 	off_t		p_name;		/* history file offset for command */
72 	struct termios	p_stty;		/* terminal state for job */
73 #endif /* JOBS */
74 };
75 
76 struct jobs
77 {
78 	struct process	*pwlist;	/* head of process list */
79 	pid_t		curpgid;	/* current process gid id */
80 	pid_t		parent;		/* set by fork() */
81 	pid_t		mypid;		/* process id of shell */
82 	pid_t		mypgid;		/* process group id of shell */
83 	pid_t		mytgid;		/* terminal group id of shell */
84 	unsigned int	in_critical;	/* >0 => in critical region */
85 	int		savesig;	/* active signal */
86 	int		numpost;	/* number of posted jobs */
87 #ifdef SHOPT_BGX
88 	int		numbjob;	/* number of background jobs */
89 #endif /* SHOPT_BGX */
90 	short		fd;		/* tty descriptor number */
91 #ifdef JOBS
92 	int		suspend;	/* suspend character */
93 	int		linedisc;	/* line dicipline */
94 #endif /* JOBS */
95 	char		jobcontrol;	/* turned on for real job control */
96 	char		waitsafe;	/* wait will not block */
97 	char		waitall;	/* wait for all jobs in pipe */
98 	char		toclear;	/* job table needs clearing */
99 	unsigned char	*freejobs;	/* free jobs numbers */
100 };
101 
102 /* flags for joblist */
103 #define JOB_LFLAG	1
104 #define JOB_NFLAG	2
105 #define JOB_PFLAG	4
106 #define JOB_NLFLAG	8
107 
108 extern struct jobs job;
109 
110 #ifdef JOBS
111 
112 #if !_std_malloc
113 #include <vmalloc.h>
114 #if VMALLOC_VERSION >= 20070911L
115 #define vmbusy()	(vmstat(0,0)!=0)
116 #endif
117 #endif
118 #ifndef vmbusy
119 #define vmbusy()	0
120 #endif
121 
122 #define job_lock()	(job.in_critical++)
123 #define job_unlock()	\
124 	do { \
125 		int	sig; \
126 		if (!--job.in_critical && (sig = job.savesig)) \
127 		{ \
128 			if (!job.in_critical++ && !vmbusy()) \
129 				job_reap(sig); \
130 			job.in_critical--; \
131 		} \
132 	} while(0)
133 
134 extern const char	e_jobusage[];
135 extern const char	e_done[];
136 extern const char	e_running[];
137 extern const char	e_coredump[];
138 extern const char	e_no_proc[];
139 extern const char	e_no_job[];
140 extern const char	e_jobsrunning[];
141 extern const char	e_nlspace[];
142 extern const char	e_access[];
143 extern const char	e_terminate[];
144 extern const char	e_no_jctl[];
145 extern const char	e_signo[];
146 #ifdef SIGTSTP
147    extern const char	e_no_start[];
148 #endif /* SIGTSTP */
149 #ifdef NTTYDISC
150    extern const char	e_newtty[];
151    extern const char	e_oldtty[];
152 #endif /* NTTYDISC */
153 #endif	/* JOBS */
154 
155 /*
156  * The following are defined in jobs.c
157  */
158 
159 extern void	job_clear(void);
160 extern void	job_bwait(char**);
161 extern int	job_walk(Sfio_t*,int(*)(struct process*,int),int,char*[]);
162 extern int	job_kill(struct process*,int);
163 extern int	job_wait(pid_t);
164 extern int	job_post(pid_t,pid_t);
165 extern void	*job_subsave(void);
166 extern void	job_subrestore(void*);
167 #ifdef SHOPT_BGX
168 extern void	job_chldtrap(Shell_t*, const char*,int);
169 #endif /* SHOPT_BGX */
170 #ifdef JOBS
171 	extern void	job_init(Shell_t*,int);
172 	extern int	job_close(Shell_t*);
173 	extern int	job_list(struct process*,int);
174 	extern int	job_terminate(struct process*,int);
175 	extern int	job_switch(struct process*,int);
176 	extern void	job_fork(pid_t);
177 	extern int	job_reap(int);
178 #else
179 #	define job_init(s,flag)
180 #	define job_close(s)	(0)
181 #	define job_fork(p)
182 #endif	/* JOBS */
183 
184 
185 #endif /* !JOB_NFLAG */
186