~upstart-devel/upstart/trunk

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
/* upstart
 *
 * Copyright  2009,2010,2011 Canonical Ltd.
 * Author: Scott James Remnant <scott@netsplit.com>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 2, as
 * published by the Free Software Foundation.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#ifndef INIT_JOB_PROCESS_H
#define INIT_JOB_PROCESS_H

#include <sys/types.h>

#include <nih/macros.h>
#include <nih/child.h>
#include <nih/error.h>

#include "process.h"
#include "job_class.h"
#include "job.h"


/**
 * JOB_PROCESS_SCRIPT_FD:
 *
 * The special fd used to pass the script to the shell process, this can be
 * anything from 3-9 (0-2 are stdin/out/err, 10 and above aren't guaranteed
 * by POSIX).
 **/
#define JOB_PROCESS_SCRIPT_FD 9

/**
 * JOB_PROCESS_LOG_REMAP_FROM_CHAR:
 * JOB_PROCESS_LOG_REMAP_TO_CHAR:
 *
 * All logs are written to a single directory so any jobs containing
 * slashes must be remapped.
 **/
#ifndef JOB_PROCESS_LOG_REMAP_FROM_CHAR
#define JOB_PROCESS_LOG_REMAP_FROM_CHAR  '/'
#endif
#ifndef JOB_PROCESS_LOG_REMAP_TO_CHAR
#define JOB_PROCESS_LOG_REMAP_TO_CHAR    '_'
#endif

/**
 * JOB_PROCESS_LOG_FILE_EXT:
 *
 * Extension for log files.
 **/
#ifndef JOB_PROCESS_LOG_FILE_EXT
#define JOB_PROCESS_LOG_FILE_EXT ".log"
#endif

/**
 * JobProcessErrorType:
 *
 * These constants represent the different steps of process spawning that
 * can produce an error.
 **/
typedef enum job_process_error_type {
	JOB_PROCESS_ERROR_DUP,
	JOB_PROCESS_ERROR_CONSOLE,
	JOB_PROCESS_ERROR_RLIMIT,
	JOB_PROCESS_ERROR_PRIORITY,
	JOB_PROCESS_ERROR_OOM_ADJ,
	JOB_PROCESS_ERROR_CHROOT,
	JOB_PROCESS_ERROR_CHDIR,
	JOB_PROCESS_ERROR_PTRACE,
	JOB_PROCESS_ERROR_EXEC,
	JOB_PROCESS_ERROR_GETPWNAM,
	JOB_PROCESS_ERROR_GETGRNAM,
	JOB_PROCESS_ERROR_GETPWUID,
	JOB_PROCESS_ERROR_BAD_SETUID,
	JOB_PROCESS_ERROR_BAD_SETGID,
	JOB_PROCESS_ERROR_SETUID,
	JOB_PROCESS_ERROR_SETGID,
	JOB_PROCESS_ERROR_CHOWN,
	JOB_PROCESS_ERROR_UNLOCKPT,
	JOB_PROCESS_ERROR_GRANTPT,
	JOB_PROCESS_ERROR_PTSNAME,
	JOB_PROCESS_ERROR_OPENPT_SLAVE,
	JOB_PROCESS_ERROR_SIGNAL,
	JOB_PROCESS_ERROR_ALLOC,
	JOB_PROCESS_ERROR_INITGROUPS,
	JOB_PROCESS_ERROR_GETGRGID,
	JOB_PROCESS_ERROR_SECURITY,
	JOB_PROCESS_ERROR_CGROUP_MGR_CONNECT,
	JOB_PROCESS_ERROR_CGROUP_SETUP,
	JOB_PROCESS_ERROR_CGROUP_ENTER,
	JOB_PROCESS_ERROR_CGROUP_CLEAR
} JobProcessErrorType;

/**
 * JobProcessError:
 * @error: ordinary NihError,
 * @type: specific error,
 * @arg: relevant argument to @type,
 * @errnum: system error number.
 *
 * This structure builds on NihError to include additional fields useful
 * for an error generated by spawning a process.  @error includes the single
 * error number and human-readable message which are sufficient for many
 * purposes.
 *
 * @type indicates which step of the spawning process failed, @arg is any
 * information relevant to @type (such as the resource limit that could not
 * be set) and @errnum is the actual system error number.
 *
 * If you receive a JOB_PROCESS_ERROR, the returned NihError structure is
 * actually this structure and can be cast to get the additional fields.
 **/
typedef struct job_process_error {
	NihError            error;
	JobProcessErrorType type;
	int                 arg;
	int                 errnum;
} JobProcessError;


/**
 * JobProcessErrorHandler:
 *
 * @job: job,
 * @state: required state job is attempting to achieve,
 * @process: job process that failed.
 *
 * Function that is called when a job process @process fails to start.
 **/
typedef void (*JobProcessErrorHandler) (Job *job, JobState state, ProcessType process);

NIH_BEGIN_EXTERN

void   job_process_start      (Job *job, ProcessType process);
void   job_process_run_bottom (JobProcessData *handler_data);

void   job_process_child_reader (JobProcessData *handler_data, NihIo *io,
		const char *buf, size_t len);

void   job_process_close_handler (JobProcessData *handler_data, NihIo *io);

pid_t  job_process_spawn   (Job *job, char * const argv[],
			    char * const *env, int trace, int script_fd,
			    ProcessType   process)
	__attribute__ ((warn_unused_result));

pid_t  job_process_spawn_with_fd   (Job *job, char * const argv[],
			    char * const *env, int trace, int script_fd,
			    ProcessType process, int *job_process_fd)
	__attribute__ ((warn_unused_result));


void   job_process_kill    (Job *job, ProcessType process);

void   job_process_handler (void *ptr, pid_t pid,
			    NihChildEvents event, int status);

Job   *job_process_find     (pid_t pid, ProcessType *process);

char  *job_process_log_path (Job *job, int user_job)
	__attribute__ ((warn_unused_result));

void   job_process_set_kill_timer (Job          *job,
				   ProcessType   process,
				   time_t        timeout);

void   job_process_adj_kill_timer  (Job *job, time_t due);

int    job_process_jobs_running (void);

void   job_process_stop_all (void);

JobProcessData *
job_process_data_new (void *parent, Job *job, ProcessType process, int job_process_fd)
	__attribute__ ((warn_unused_result));

json_object *
job_process_data_serialise (const Job *job, const JobProcessData *handler_data)
	__attribute__ ((warn_unused_result));

JobProcessData *
job_process_data_deserialise (void *parent, Job *job, json_object *json)
	__attribute__ ((warn_unused_result));

void job_process_error_handler (const char *buf, size_t len);

void job_process_error_abort     (int fd, JobProcessErrorType type,
					 int arg)
	__attribute__ ((noreturn));

NIH_END_EXTERN

#endif /* INIT_JOB_PROCESS_H */