~ubuntu-branches/debian/wheezy/upstart/wheezy

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
/* upstart
 *
 * Copyright © 2006 Canonical Ltd.
 * Author: Scott James Remnant <scott@ubuntu.com>.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * 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 St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#ifndef INIT_JOB_H
#define INIT_JOB_H

#include <sys/types.h>
#include <sys/resource.h>

#include <time.h>

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

#include <upstart/job.h>

#include "event.h"


/**
 * JOB_DEFAULT_PID_TIMEOUT:
 *
 * The default length of time to wait after spawning a daemon process for
 * the pid to be obtained before giving up and assuming the job did not
 * start.
 **/
#define JOB_DEFAULT_PID_TIMEOUT 10

/**
 * JOB_DEFAULT_KILL_TIMEOUT:
 *
 * The default length of time to wait after sending a process the TERM
 * signal before sending the KILL signal if it hasn't terminated.
 **/
#define JOB_DEFAULT_KILL_TIMEOUT 5

/**
 * JOB_DEFAULT_UMASK:
 *
 * The default file creation mark for processes.
 **/
#define JOB_DEFAULT_UMASK 022




/**
 * Job:
 * @entry: list header,
 * @name: string name of the job; namespace shared with events,
 * @description: description of the job; intended for humans,
 * @author: author of the job; intended for humans,
 * @version: version of the job; intended for humans,
 * @goal: whether the job is to be stopped or started,
 * @state: actual state of the job,
 * @start_events: list of events that can start this job,
 * @stop_events; list of events that can stop this job.
 * @depends: list of dependency jobs,
 * @process_state: what we're waiting for from the process,
 * @pid: current process id,
 * @kill_timeout: time to wait between sending TERM and KILL signals,
 * @kill_timer: timer to kill process,
 * @spawns_instance: job is always waiting and spawns instances,
 * @is_instance: job should be cleaned up instead of waiting,
 * @respawn: process should be restarted if it fails,
 * @normalexit: array of exit codes that prevent a respawn,
 * @normalexit_len: length of @normalexit array,
 * @daemon: process forks into background; pid needs to be obtained,
 * @pidfile: obtain pid by reading this file,
 * @binary: obtain pid by locating this binary,
 * @pid_timeout: time to wait before giving up obtaining pid,
 * @pid_timer: timer for pid location,
 * @command: command to be run as the primary process,
 * @script: script to run instead of @command,
 * @start_script: script to run before @command is started,
 * @stop_script: script to run after @command is stopped,
 * @respawn_script: script to run between @command respawns,
 * @console: how to arrange the job's stdin/out/err file descriptors,
 * @env: %NULL-terminated list of environment strings to set,
 * @umask: file mode creation mask,
 * @nice: process priority,
 * @limits: resource limits indexed by resource,
 * @chroot: root directory of process (implies @chdir if not set),
 * @chdir: working directory of process,
 *
 * This structure represents a known task or service that should be tracked
 * by the init daemon; as tasks and services are fundamentally identical,
 * except for the handling when the main process terminates, they are both
 * collated together in this structure and only differ in the value of the
 * @respawn member.
 **/
typedef struct job {
	NihList        entry;

	char          *name;
	char          *description;
	char          *author;
	char          *version;

	JobGoal        goal;
	JobState       state;

	NihList        start_events;
	NihList        stop_events;
	NihList        depends;

	ProcessState   process_state;
	pid_t          pid;
	time_t         kill_timeout;
	NihTimer      *kill_timer;

	int            spawns_instance;
	int            is_instance;

	int            respawn;
	int           *normalexit;
	size_t         normalexit_len;

	int            daemon;
	char          *pidfile;
	char          *binary;
	time_t         pid_timeout;
	NihTimer      *pid_timer;

	char          *command;
	char          *script;
	char          *start_script;
	char          *stop_script;
	char          *respawn_script;

	ConsoleType    console;
	char         **env;

	mode_t         umask;
	int            nice;
	struct rlimit *limits[RLIMIT_NLIMITS];
	char          *chroot;
	char          *chdir;
} Job;

/**
 * JobName:
 * @entry: list header,
 * @name: name of job.
 *
 * This structure is used to form lists of job names, for example in the
 * depends list of an ordinary Job.
 **/
typedef struct job_name {
	NihList  entry;
	char    *name;
} JobName;


NIH_BEGIN_EXTERN

Job *       job_new             (void *parent, const char *name);

Job *       job_find_by_name    (const char *name);
Job *       job_find_by_pid     (pid_t pid);

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

void        job_run_command     (Job *job, const char *command);
void        job_run_script      (Job *job, const char *script);

void        job_kill_process    (Job *job);

void        job_child_reaper    (void *ptr, pid_t pid, int killed, int status);

void        job_start           (Job *job);
void        job_stop            (Job *job);

void        job_release_depends (Job *job);

void        job_start_event     (Job *job, Event *event);
void        job_stop_event      (Job *job, Event *event);
void        job_handle_event    (Event *event);

NIH_END_EXTERN

#endif /* INIT_JOB_H */