chiark / gitweb /
core: introduce new Restart=on-abnormal setting
[elogind.git] / src / core / service.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 typedef struct Service Service;
25
26 #include "unit.h"
27 #include "path.h"
28 #include "ratelimit.h"
29 #include "kill.h"
30 #include "exit-status.h"
31
32 typedef enum ServiceState {
33         SERVICE_DEAD,
34         SERVICE_START_PRE,
35         SERVICE_START,
36         SERVICE_START_POST,
37         SERVICE_RUNNING,
38         SERVICE_EXITED,            /* Nothing is running anymore, but RemainAfterExit is true hence this is OK */
39         SERVICE_RELOAD,
40         SERVICE_STOP,              /* No STOP_PRE state, instead just register multiple STOP executables */
41         SERVICE_STOP_SIGTERM,
42         SERVICE_STOP_SIGKILL,
43         SERVICE_STOP_POST,
44         SERVICE_FINAL_SIGTERM,     /* In case the STOP_POST executable hangs, we shoot that down, too */
45         SERVICE_FINAL_SIGKILL,
46         SERVICE_FAILED,
47         SERVICE_AUTO_RESTART,
48         _SERVICE_STATE_MAX,
49         _SERVICE_STATE_INVALID = -1
50 } ServiceState;
51
52 typedef enum ServiceRestart {
53         SERVICE_RESTART_NO,
54         SERVICE_RESTART_ON_SUCCESS,
55         SERVICE_RESTART_ON_FAILURE,
56         SERVICE_RESTART_ON_ABNORMAL,
57         SERVICE_RESTART_ON_WATCHDOG,
58         SERVICE_RESTART_ON_ABORT,
59         SERVICE_RESTART_ALWAYS,
60         _SERVICE_RESTART_MAX,
61         _SERVICE_RESTART_INVALID = -1
62 } ServiceRestart;
63
64 typedef enum ServiceType {
65         SERVICE_SIMPLE,   /* we fork and go on right-away (i.e. modern socket activated daemons) */
66         SERVICE_FORKING,  /* forks by itself (i.e. traditional daemons) */
67         SERVICE_ONESHOT,  /* we fork and wait until the program finishes (i.e. programs like fsck which run and need to finish before we continue) */
68         SERVICE_DBUS,     /* we fork and wait until a specific D-Bus name appears on the bus */
69         SERVICE_NOTIFY,   /* we fork and wait until a daemon sends us a ready message with sd_notify() */
70         SERVICE_IDLE,     /* much like simple, but delay exec() until all jobs are dispatched. */
71         _SERVICE_TYPE_MAX,
72         _SERVICE_TYPE_INVALID = -1
73 } ServiceType;
74
75 typedef enum ServiceExecCommand {
76         SERVICE_EXEC_START_PRE,
77         SERVICE_EXEC_START,
78         SERVICE_EXEC_START_POST,
79         SERVICE_EXEC_RELOAD,
80         SERVICE_EXEC_STOP,
81         SERVICE_EXEC_STOP_POST,
82         _SERVICE_EXEC_COMMAND_MAX,
83         _SERVICE_EXEC_COMMAND_INVALID = -1
84 } ServiceExecCommand;
85
86 typedef enum NotifyAccess {
87         NOTIFY_NONE,
88         NOTIFY_ALL,
89         NOTIFY_MAIN,
90         _NOTIFY_ACCESS_MAX,
91         _NOTIFY_ACCESS_INVALID = -1
92 } NotifyAccess;
93
94 typedef enum ServiceResult {
95         SERVICE_SUCCESS,
96         SERVICE_FAILURE_RESOURCES,
97         SERVICE_FAILURE_TIMEOUT,
98         SERVICE_FAILURE_EXIT_CODE,
99         SERVICE_FAILURE_SIGNAL,
100         SERVICE_FAILURE_CORE_DUMP,
101         SERVICE_FAILURE_WATCHDOG,
102         SERVICE_FAILURE_START_LIMIT,
103         _SERVICE_RESULT_MAX,
104         _SERVICE_RESULT_INVALID = -1
105 } ServiceResult;
106
107 typedef enum FailureAction {
108         SERVICE_FAILURE_ACTION_NONE,
109         SERVICE_FAILURE_ACTION_REBOOT,
110         SERVICE_FAILURE_ACTION_REBOOT_FORCE,
111         SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE,
112         _SERVICE_FAILURE_ACTION_MAX,
113         _SERVICE_FAILURE_ACTION_INVALID = -1
114 } FailureAction;
115
116 struct Service {
117         Unit meta;
118
119         ServiceType type;
120         ServiceRestart restart;
121         ExitStatusSet restart_ignore_status;
122         ExitStatusSet success_status;
123
124         /* If set we'll read the main daemon PID from this file */
125         char *pid_file;
126
127         usec_t restart_usec;
128         usec_t timeout_start_usec;
129         usec_t timeout_stop_usec;
130
131         dual_timestamp watchdog_timestamp;
132         usec_t watchdog_usec;
133         sd_event_source *watchdog_event_source;
134
135         ExecCommand* exec_command[_SERVICE_EXEC_COMMAND_MAX];
136
137         ExecContext exec_context;
138         KillContext kill_context;
139         CGroupContext cgroup_context;
140
141         ServiceState state, deserialized_state;
142
143         /* The exit status of the real main process */
144         ExecStatus main_exec_status;
145
146         /* The currently executed control process */
147         ExecCommand *control_command;
148
149         /* The currently executed main process, which may be NULL if
150          * the main process got started via forking mode and not by
151          * us */
152         ExecCommand *main_command;
153
154         /* The ID of the control command currently being executed */
155         ServiceExecCommand control_command_id;
156
157         /* Runtime data of the execution context */
158         ExecRuntime *exec_runtime;
159
160         pid_t main_pid, control_pid;
161         int socket_fd;
162
163         bool permissions_start_only;
164         bool root_directory_start_only;
165         bool remain_after_exit;
166         bool guess_main_pid;
167
168         /* If we shut down, remember why */
169         ServiceResult result;
170         ServiceResult reload_result;
171
172         bool main_pid_known:1;
173         bool main_pid_alien:1;
174         bool bus_name_good:1;
175         bool forbid_restart:1;
176         bool start_timeout_defined:1;
177 #ifdef HAVE_SYSV_COMPAT
178         bool is_sysv:1;
179         bool sysv_has_lsb:1;
180         bool sysv_enabled:1;
181         int sysv_start_priority_from_rcnd;
182         int sysv_start_priority;
183
184         char *sysv_runlevels;
185 #endif
186
187         char *bus_name;
188
189         char *status_text;
190
191         FailureAction failure_action;
192
193         RateLimit start_limit;
194         FailureAction start_limit_action;
195         char *reboot_arg;
196
197         UnitRef accept_socket;
198
199         sd_event_source *timer_event_source;
200         PathSpec *pid_file_pathspec;
201
202         NotifyAccess notify_access;
203 };
204
205 extern const UnitVTable service_vtable;
206
207 struct Socket;
208
209 int service_set_socket_fd(Service *s, int fd, struct Socket *socket);
210
211 const char* service_state_to_string(ServiceState i) _const_;
212 ServiceState service_state_from_string(const char *s) _pure_;
213
214 const char* service_restart_to_string(ServiceRestart i) _const_;
215 ServiceRestart service_restart_from_string(const char *s) _pure_;
216
217 const char* service_type_to_string(ServiceType i) _const_;
218 ServiceType service_type_from_string(const char *s) _pure_;
219
220 const char* service_exec_command_to_string(ServiceExecCommand i) _const_;
221 ServiceExecCommand service_exec_command_from_string(const char *s) _pure_;
222
223 const char* notify_access_to_string(NotifyAccess i) _const_;
224 NotifyAccess notify_access_from_string(const char *s) _pure_;
225
226 const char* service_result_to_string(ServiceResult i) _const_;
227 ServiceResult service_result_from_string(const char *s) _pure_;
228
229 const char* failure_action_to_string(FailureAction i) _const_;
230 FailureAction failure_action_from_string(const char *s) _pure_;