~ubuntu-branches/ubuntu/gutsy/upstart/gutsy

« back to all changes in this revision

Viewing changes to init/job.h

  • Committer: Bazaar Package Importer
  • Author(s): Scott James Remnant
  • Date: 2007-02-12 13:51:40 UTC
  • mfrom: (1.1.8 upstream)
  • Revision ID: james.westby@ubuntu.com-20070212135140-pe7cqvezgocni1ou
Tags: 0.3.5-1
* New upstream release:
  - inotify file descriptor leak fixed.  LP: #83099.
  - inotify support is no longer required.  LP: #68904.
  - new job state machine
  - new event structure, can now include arguments and environment

* Applied 00-libnih-update.patch; this updates the libnih library to the
  latest bzr trunk version, required for the complex-event-config patch.
* Applied 10-cant-stop-execless-job.patch from upstream; this corrects a
  bug where jobs without an "exec" or "script" stanza cannot be stopped.
* Applied 20-complex-event-config.patch from upstream; this is an
  experimental implementation of the "on" keyword that allows definition
  of complex system states.

* System V compatibility jobs updated to match new event names.
* rcS job now sets PREVLEVEL and RUNLEVEL.  LP: #76304.

* NOTE: After this upgrade, init will appear to have "forgotten" the
  process ids of your gettys, etc.  This is not a critical problem and
  will be fixed before release.  Shutdown will still work as normal.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* upstart
2
2
 *
3
 
 * Copyright © 2006 Canonical Ltd.
 
3
 * Copyright © 2007 Canonical Ltd.
4
4
 * Author: Scott James Remnant <scott@ubuntu.com>.
5
5
 *
6
6
 * This program is free software; you can redistribute it and/or modify
29
29
 
30
30
#include <nih/macros.h>
31
31
#include <nih/list.h>
 
32
#include <nih/hash.h>
32
33
#include <nih/timer.h>
33
34
#include <nih/main.h>
34
35
 
35
 
#include <upstart/job.h>
 
36
#include <upstart/enum.h>
36
37
 
37
38
#include "event.h"
38
39
 
77
78
#define JOB_DEFAULT_UMASK 022
78
79
 
79
80
 
80
 
 
 
81
/**
 
82
 * JobProcess:
 
83
 * @script: whether a shell will be required,
 
84
 * @command: command or script to be run.
 
85
 *
 
86
 * This structure represents an individual process within the job that
 
87
 * can be run.  When @script is FALSE, @command is checked for shell
 
88
 * characters; if there are none, it is split on whitespace and executed
 
89
 * directly using exec().  If there are shell characters, or @script is
 
90
 * TRUE, @command is executed using a shell.
 
91
 **/
 
92
typedef struct job_process {
 
93
        int   script;
 
94
        char *command;
 
95
} JobProcess;
81
96
 
82
97
/**
83
98
 * Job:
86
101
 * @description: description of the job; intended for humans,
87
102
 * @author: author of the job; intended for humans,
88
103
 * @version: version of the job; intended for humans,
 
104
 * @instance_of: job this is an instance of,
 
105
 * @delete: job should be deleted once stopped,
89
106
 * @goal: whether the job is to be stopped or started,
90
107
 * @state: actual state of the job,
 
108
 * @pid: current process id,
 
109
 * @aux_pid: additional process id (for post-start or pre-stop),
 
110
 * @cause: cause of last goal change,
 
111
 * @blocked: emitted event we're waiting to finish,
 
112
 * @failed: whether the last process ran failed,
 
113
 * @failed_state: state the job was in for the last failed process,
 
114
 * @exit_status: exit status of the last failed process,
91
115
 * @start_events: list of events that can start this job,
92
116
 * @stop_events; list of events that can stop this job.
93
 
 * @depends: list of dependency jobs,
94
 
 * @process_state: what we're waiting for from the process,
95
 
 * @pid: current process id,
 
117
 * @emits: list of additional events that this job can emit,
96
118
 * @kill_timeout: time to wait between sending TERM and KILL signals,
97
119
 * @kill_timer: timer to kill process,
98
 
 * @spawns_instance: job is always waiting and spawns instances,
99
 
 * @is_instance: job should be cleaned up instead of waiting,
 
120
 * @instance: job is always waiting and spawns instances,
 
121
 * @service: job has reached its goal when running,
100
122
 * @respawn: process should be restarted if it fails,
101
123
 * @respawn_limit: number of respawns in @respawn_interval that we permit,
102
124
 * @respawn_interval: barrier for @respawn_limit,
105
127
 * @normalexit: array of exit codes that prevent a respawn,
106
128
 * @normalexit_len: length of @normalexit array,
107
129
 * @daemon: process forks into background; pid needs to be obtained,
108
 
 * @pidfile: obtain pid by reading this file,
109
 
 * @binary: obtain pid by locating this binary,
 
130
 * @pid_file: obtain pid by reading this file,
 
131
 * @pid_binary: obtain pid by locating this binary,
110
132
 * @pid_timeout: time to wait before giving up obtaining pid,
111
133
 * @pid_timer: timer for pid location,
112
 
 * @command: command to be run as the primary process,
113
 
 * @script: script to run instead of @command,
114
 
 * @start_script: script to run before @command is started,
115
 
 * @stop_script: script to run after @command is stopped,
 
134
 * @process: primary process to be run,
 
135
 * @pre_start: process to be run before job is started,
 
136
 * @post_start: process to be run after job is started.
 
137
 * @pre_stop: process to be run before job is stopped,
 
138
 * @post_stop: process to be run after job is stopped,
116
139
 * @respawn_script: script to run between @command respawns,
117
140
 * @console: how to arrange the job's stdin/out/err file descriptors,
118
141
 * @env: NULL-terminated list of environment strings to set,
126
149
 * by the init daemon; as tasks and services are fundamentally identical,
127
150
 * except for the handling when the main process terminates, they are both
128
151
 * collated together in this structure and only differ in the value of the
129
 
 * @respawn member.
 
152
 * @service member.
130
153
 **/
131
 
typedef struct job {
 
154
typedef struct job Job;
 
155
struct job {
132
156
        NihList        entry;
133
157
 
134
158
        char          *name;
136
160
        char          *author;
137
161
        char          *version;
138
162
 
 
163
        Job           *instance_of;
 
164
        int            delete;
 
165
 
139
166
        JobGoal        goal;
140
167
        JobState       state;
 
168
        pid_t          pid;
 
169
        pid_t          aux_pid;
 
170
 
 
171
        EventEmission *cause;
 
172
        EventEmission *blocked;
 
173
 
 
174
        int            failed;
 
175
        JobState       failed_state;
 
176
        int            exit_status;
141
177
 
142
178
        NihList        start_events;
143
179
        NihList        stop_events;
144
 
        NihList        depends;
145
 
 
146
 
        ProcessState   process_state;
147
 
        pid_t          pid;
 
180
        NihList        emits;
 
181
 
 
182
        int           *normalexit;
 
183
        size_t         normalexit_len;
 
184
 
148
185
        time_t         kill_timeout;
149
186
        NihTimer      *kill_timer;
150
187
 
151
 
        int            spawns_instance;
152
 
        int            is_instance;
153
 
 
 
188
        int            instance;
 
189
        int            service;
154
190
        int            respawn;
155
191
        int            respawn_limit;
156
192
        time_t         respawn_interval;
157
193
        int            respawn_count;
158
194
        time_t         respawn_time;
159
 
        int           *normalexit;
160
 
        size_t         normalexit_len;
161
195
 
162
196
        int            daemon;
163
 
        char          *pidfile;
164
 
        char          *binary;
 
197
        char          *pid_file;
 
198
        char          *pid_binary;
165
199
        time_t         pid_timeout;
166
200
        NihTimer      *pid_timer;
167
201
 
168
 
        char          *command;
169
 
        char          *script;
170
 
        char          *start_script;
171
 
        char          *stop_script;
172
 
        char          *respawn_script;
 
202
        JobProcess    *process;
 
203
        JobProcess    *pre_start;
 
204
        JobProcess    *post_start;
 
205
        JobProcess    *pre_stop;
 
206
        JobProcess    *post_stop;
173
207
 
174
208
        ConsoleType    console;
175
209
        char         **env;
179
213
        struct rlimit *limits[RLIMIT_NLIMITS];
180
214
        char          *chroot;
181
215
        char          *chdir;
182
 
} Job;
183
 
 
184
 
/**
185
 
 * JobName:
186
 
 * @entry: list header,
187
 
 * @name: name of job.
188
 
 *
189
 
 * This structure is used to form lists of job names, for example in the
190
 
 * depends list of an ordinary Job.
191
 
 **/
192
 
typedef struct job_name {
193
 
        NihList  entry;
194
 
        char    *name;
195
 
} JobName;
 
216
};
196
217
 
197
218
 
198
219
NIH_BEGIN_EXTERN
199
220
 
200
 
NihList *   job_list            (void);
201
 
 
202
 
Job *       job_new             (const void *parent, const char *name);
203
 
 
204
 
Job *       job_find_by_name    (const char *name);
205
 
Job *       job_find_by_pid     (pid_t pid);
206
 
 
207
 
void        job_change_state    (Job *job, JobState state);
208
 
JobState    job_next_state      (Job *job);
209
 
 
210
 
void        job_run_command     (Job *job, const char *command);
211
 
void        job_run_script      (Job *job, const char *script);
212
 
 
213
 
void        job_kill_process    (Job *job);
214
 
 
215
 
void        job_child_reaper    (void *ptr, pid_t pid, int killed, int status);
216
 
 
217
 
void        job_start           (Job *job);
218
 
void        job_stop            (Job *job);
219
 
 
220
 
void        job_release_depends (Job *job);
221
 
 
222
 
void        job_start_event     (Job *job, Event *event);
223
 
void        job_stop_event      (Job *job, Event *event);
224
 
void        job_handle_event    (Event *event);
225
 
 
226
 
void        job_detect_idle     (void);
227
 
void        job_set_idle_event  (const char *name);
228
 
 
229
 
Job *       job_read_state      (Job *job, char *buf);
230
 
void        job_write_state     (FILE *state);
 
221
NihHash *jobs;
 
222
 
 
223
 
 
224
void     job_init                  (void);
 
225
 
 
226
Job *    job_new                   (const void *parent, const char *name)
 
227
        __attribute__ ((warn_unused_result, malloc));
 
228
Job *    job_copy                  (const void *parent, const Job *old_job)
 
229
        __attribute__ ((warn_unused_result, malloc));
 
230
 
 
231
Job *    job_find_by_name          (const char *name);
 
232
Job *    job_find_by_pid           (pid_t pid);
 
233
 
 
234
void     job_change_goal           (Job *job, JobGoal goal,
 
235
                                    EventEmission *emission);
 
236
 
 
237
void     job_change_state          (Job *job, JobState state);
 
238
JobState job_next_state            (Job *job);
 
239
 
 
240
void     job_run_process           (Job *job, JobProcess *process);
 
241
 
 
242
void     job_kill_process          (Job *job);
 
243
 
 
244
void     job_child_reaper          (void *ptr, pid_t pid, int
 
245
                                    killed, int status);
 
246
 
 
247
void     job_handle_event          (EventEmission *emission);
 
248
void     job_handle_event_finished (EventEmission *emission);
 
249
 
 
250
void     job_detect_stalled        (void);
 
251
void     job_free_deleted          (void);
231
252
 
232
253
NIH_END_EXTERN
233
254