~xnox/ubuntu/vivid/upstart/fix-rotate-logs

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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* upstart
 *
 * Copyright  2010 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_H
#define INIT_JOB_H

#include <dbus/dbus.h>

#include <sys/types.h>

#include <time.h>

#include <nih/macros.h>
#include <nih/list.h>
#include <nih/timer.h>

#include <nih-dbus/dbus_message.h>

#include "job_class.h"
#include "event_operator.h"
#include "log.h"

#include "com.ubuntu.Upstart.Instance.h"


/**
 * JobGoal:
 *
 * There are two ultimate goals for any job, either it should be stopped
 * or it should be started.  In order to achieve these goals, we may need
 * to go through a number of different states (defined by JobState).
 **/
typedef enum job_goal {
	JOB_STOP,
	JOB_START,
	JOB_RESPAWN
} JobGoal;

/**
 * JobState:
 *
 * This is used to identify the current actual state of a job, suggesting
 * which process (pre-start, post-start, pre-stop, post-stop or the binary
 * itself) is running, or which interim state we are in.
 *
 * This is combined with the job's goal decide what to do with the
 * processes and which states to move into when changes in process state
 * (pid obtained or death) occur.
 **/
typedef enum job_state {
	JOB_WAITING,
	JOB_STARTING,
	JOB_SECURITY_SPAWNING,
	JOB_SECURITY,
	JOB_PRE_STARTING,
	JOB_PRE_START,
	JOB_SPAWNING,
	JOB_SPAWNED,
	JOB_POST_STARTING,
	JOB_POST_START,
	JOB_RUNNING,
	JOB_PRE_STOPPING,
	JOB_PRE_STOP,
	JOB_STOPPING,
	JOB_KILLED,
	JOB_POST_STOPPING,
	JOB_POST_STOP
} JobState;

/**
 * TraceState:
 *
 * We trace jobs to follow forks and detect execs in order to be able to
 * supervise daemon processes.  Unfortunately due to the "unique and arcane"
 * nature of ptrace(), we need to track some state.
 **/
typedef enum trace_state {
	TRACE_NONE,
	TRACE_NEW,
	TRACE_NEW_CHILD,
	TRACE_NORMAL
} TraceState;

typedef struct job_process_data JobProcessData;

/**
 * Job:
 * @entry: list header,
 * @name: unique instance name,
 * @class: pointer to job class,
 * @path: D-Bus path of instance,
 * @goal: whether the job is to be stopped or started,
 * @state: actual state of the job,
 * @env: NULL-terminated list of environment variables,
 * @start_env: environment to use next time the job is started,
 * @stop_env: environment to add for the next pre-stop script,
 * @stop_on: event operator expression that can stop this job.
 * @fds: array of file descriptors associated with events in parent
 *       JobClasses @start_on condition,
 * @num_fds: number of elements in @fds,
 * @pid: current process ids,
 * @blocker: emitted event we're waiting to finish,
 * @blocking: list of events we're blocking from finishing,
 * @kill_timer: timer to kill process,
 * @kill_process: process @kill_timer will kill,
 * @failed: whether the last process ran failed,
 * @failed_process: the last process that failed,
 * @exit_status: exit status of the last failed process,
 * @respawn_time: time job was first respawned,
 * @respawn_count: number of respawns since @respawn_time,
 * @trace_forks: number of forks traced,
 * @trace_state: state of trace,
 * @log: pointer to array of log objects for handling job output,
 * @process_data: transitory async job process metadata.
 *
 * This structure holds the state of an active job instance being tracked
 * by the init daemon, the configuration details of the job are available
 * from the @class member.
 **/
typedef struct job {
	NihList          entry;

	char            *name;
	JobClass        *class;
	char            *path;

	JobGoal          goal;
	JobState         state;
	char           **env;

	char           **start_env;
	char           **stop_env;
	EventOperator   *stop_on;

	int             *fds;
	size_t           num_fds;

	pid_t           *pid;
	Event           *blocker;
	NihList          blocking;

	NihTimer        *kill_timer;
	ProcessType      kill_process;

	int              failed;
	ProcessType      failed_process;
	int              exit_status;

	time_t           respawn_time;
	int              respawn_count;

	int              trace_forks;
	TraceState       trace_state;
	Log            **log;
	JobProcessData **process_data;

} Job;

/**
 * JobProcessData:
 *
 * @job: job,
 * @process: job process to run,
 * @script: optional script that job should run with,
 * @shell_fd: file descriptor attached to child that @script should be
 * written to (or -1 when @script is NULL),
 * @job_process_fd: open readable file descriptor attached to the child
 *  process used to detect child setup errors,
 * @status: exit status or signal in higher byte (iff 
 *  job_process_terminated() ran before the job_register_child_handler()
 *  handlers),
 * @valid: FALSE once the data has been handled, else TRUE.
 *
 * Used to keep track of asynchronously started job processes.
 **/
typedef struct job_process_data {
	Job           *job;
	ProcessType    process;
	char          *script;
	int            shell_fd;
	int            job_process_fd;
	int            status;
	int            valid;
} JobProcessData;

/**
 * job_register_child_handler:
 *
 * @_parent: parent pointer,
 * @_fd: file descriptor to monitor,
 * @_data: data to pass to nih_io_reopen().
 *
 * Register a watcher to monitor the job process child error file
 * descriptor @_fd.
 **/
#define job_register_child_handler(_parent, _fd, _data) \
	 while (! nih_io_reopen (_parent, _fd, NIH_IO_STREAM, \
			(NihIoReader)job_process_child_reader, \
			(NihIoCloseHandler)job_process_close_handler, \
			NULL, \
			_data)) { \
		NihError *err; \
		err = nih_error_get (); \
		if (err->number != ENOMEM) \
			nih_assert_not_reached (); \
		nih_free (err); \
	 }


NIH_BEGIN_EXTERN

Job *       job_new             (JobClass *class, const char *name)
	__attribute__ ((warn_unused_result));
void        job_register        (Job *job, DBusConnection *conn, int signal);

void        job_change_goal     (Job *job, JobGoal goal);

void        job_change_state    (Job *job, JobState state);
JobState    job_next_state      (Job *job);

void        job_failed          (Job *job, ProcessType process, int status);
void        job_finished        (Job *job, int failed);

Event      *job_emit_event      (Job *job);

const char *job_name            (Job *job);

const char *job_goal_name       (JobGoal goal)
	__attribute__ ((const));
JobGoal     job_goal_from_name  (const char *goal);

const char *job_state_name      (JobState state)
	__attribute__ ((const));
JobState    job_state_from_name (const char *state);

int         job_start           (Job *job, NihDBusMessage *message, int wait)
	__attribute__ ((warn_unused_result));
int         job_stop            (Job *job, NihDBusMessage *message, int wait)
	__attribute__ ((warn_unused_result));
int         job_restart         (Job *job, NihDBusMessage *message, int wait)
	__attribute__ ((warn_unused_result));
int         job_reload          (Job *job, NihDBusMessage *message)
	__attribute__ ((warn_unused_result));

int         job_get_name        (Job *job, NihDBusMessage *message,
				 char **name)
	__attribute__ ((warn_unused_result));
int         job_get_goal        (Job *job, NihDBusMessage *message,
				 char **goal)
	__attribute__ ((warn_unused_result));
int         job_get_state       (Job *job, NihDBusMessage *message,
				 char **state)
	__attribute__ ((warn_unused_result));

int         job_get_processes   (Job *job, NihDBusMessage *message,
				 JobProcessesElement ***processes)
	__attribute__ ((warn_unused_result));

json_object *job_serialise (const Job *job);
Job *job_deserialise (JobClass *parent, json_object *json);

json_object *job_serialise_all (const NihHash *jobs)
	__attribute__ ((warn_unused_result));

int         job_deserialise_all (JobClass *parent, json_object *json)
	__attribute__ ((warn_unused_result));

Job *       job_find            (const Session *session,
				 JobClass       *class,
				 const char     *job_class,
				 const char     *job_name)
	__attribute__ ((warn_unused_result));

const char *
job_state_enum_to_str (JobState state)
	__attribute__ ((warn_unused_result));

JobState
job_state_str_to_enum (const char *state)
	__attribute__ ((warn_unused_result));

void job_child_error_handler (Job *job, ProcessType process);

int job_needs_cgroups (const Job *job)
	__attribute__ ((warn_unused_result));

#ifdef ENABLE_CGROUPS

int job_last_process (const Job *job, ProcessType process)
	__attribute__ ((warn_unused_result));

#endif /* ENABLE_CGROUPS */

NIH_END_EXTERN

#endif /* INIT_JOB_H */