]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
5cb5a6ff | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
5430f7f2 LP |
9 | under the terms of the GNU Lesser General Public License as published by |
10 | the Free Software Foundation; either version 2.1 of the License, or | |
a7334b09 LP |
11 | (at your option) any later version. |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
5430f7f2 | 16 | Lesser General Public License for more details. |
a7334b09 | 17 | |
5430f7f2 | 18 | You should have received a copy of the GNU Lesser General Public License |
a7334b09 LP |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. |
20 | ***/ | |
21 | ||
5cb5a6ff | 22 | #include <errno.h> |
034c6ed7 | 23 | #include <signal.h> |
2c4104f0 LP |
24 | #include <dirent.h> |
25 | #include <unistd.h> | |
4b939747 | 26 | #include <sys/reboot.h> |
efe6e7d3 MO |
27 | #include <linux/reboot.h> |
28 | #include <sys/syscall.h> | |
5cb5a6ff | 29 | |
574634bc | 30 | #include "async.h" |
4b939747 | 31 | #include "manager.h" |
87f0e418 | 32 | #include "unit.h" |
5cb5a6ff LP |
33 | #include "service.h" |
34 | #include "load-fragment.h" | |
35 | #include "load-dropin.h" | |
034c6ed7 | 36 | #include "log.h" |
2c4104f0 | 37 | #include "strv.h" |
9e2f7c11 | 38 | #include "unit-name.h" |
41f9172f | 39 | #include "unit-printf.h" |
4139c1b2 | 40 | #include "dbus-service.h" |
514f4ef5 | 41 | #include "special.h" |
9a57c629 | 42 | #include "exit-status.h" |
f6a6225e | 43 | #include "def.h" |
9eb977db | 44 | #include "path-util.h" |
f6a6225e | 45 | #include "util.h" |
7f110ff9 | 46 | #include "utf8.h" |
4d1a6904 | 47 | #include "env-util.h" |
a5c32cff | 48 | #include "fileio.h" |
718db961 LP |
49 | #include "bus-error.h" |
50 | #include "bus-util.h" | |
034c6ed7 | 51 | |
acbb0225 | 52 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
87f0e418 LP |
53 | [SERVICE_DEAD] = UNIT_INACTIVE, |
54 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
55 | [SERVICE_START] = UNIT_ACTIVATING, | |
56 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
57 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
80876c20 | 58 | [SERVICE_EXITED] = UNIT_ACTIVE, |
032ff4af | 59 | [SERVICE_RELOAD] = UNIT_RELOADING, |
87f0e418 LP |
60 | [SERVICE_STOP] = UNIT_DEACTIVATING, |
61 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
62 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
63 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
64 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
65 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 66 | [SERVICE_FAILED] = UNIT_FAILED, |
6124958c | 67 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING |
034c6ed7 | 68 | }; |
5cb5a6ff | 69 | |
e056b01d LP |
70 | /* For Type=idle we never want to delay any other jobs, hence we |
71 | * consider idle jobs active as soon as we start working on them */ | |
72 | static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = { | |
73 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
74 | [SERVICE_START_PRE] = UNIT_ACTIVE, | |
75 | [SERVICE_START] = UNIT_ACTIVE, | |
76 | [SERVICE_START_POST] = UNIT_ACTIVE, | |
77 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
78 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
79 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
80 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
81 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
82 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
83 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
84 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
85 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
86 | [SERVICE_FAILED] = UNIT_FAILED, | |
87 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING | |
88 | }; | |
89 | ||
718db961 LP |
90 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
91 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); | |
92 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata); | |
93 | ||
842129f5 LP |
94 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f); |
95 | ||
a16e1123 LP |
96 | static void service_init(Unit *u) { |
97 | Service *s = SERVICE(u); | |
98 | ||
99 | assert(u); | |
ac155bb8 | 100 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 101 | |
1f19a534 OS |
102 | s->timeout_start_usec = u->manager->default_timeout_start_usec; |
103 | s->timeout_stop_usec = u->manager->default_timeout_stop_usec; | |
104 | s->restart_usec = u->manager->default_restart_usec; | |
0b86feac | 105 | s->type = _SERVICE_TYPE_INVALID; |
a16e1123 | 106 | s->socket_fd = -1; |
3185a36b | 107 | s->guess_main_pid = true; |
a16e1123 | 108 | |
085afe36 | 109 | RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst); |
a16e1123 LP |
110 | |
111 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
112 | } | |
113 | ||
5e94833f LP |
114 | static void service_unwatch_control_pid(Service *s) { |
115 | assert(s); | |
116 | ||
117 | if (s->control_pid <= 0) | |
118 | return; | |
119 | ||
120 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
121 | s->control_pid = 0; | |
122 | } | |
123 | ||
124 | static void service_unwatch_main_pid(Service *s) { | |
125 | assert(s); | |
126 | ||
127 | if (s->main_pid <= 0) | |
128 | return; | |
129 | ||
130 | unit_unwatch_pid(UNIT(s), s->main_pid); | |
131 | s->main_pid = 0; | |
132 | } | |
133 | ||
3e52541e MS |
134 | static void service_unwatch_pid_file(Service *s) { |
135 | if (!s->pid_file_pathspec) | |
136 | return; | |
137 | ||
66870f90 ZJS |
138 | log_debug_unit(UNIT(s)->id, "Stopping watch for %s's PID file %s", |
139 | UNIT(s)->id, s->pid_file_pathspec->path); | |
718db961 | 140 | path_spec_unwatch(s->pid_file_pathspec); |
3e52541e MS |
141 | path_spec_done(s->pid_file_pathspec); |
142 | free(s->pid_file_pathspec); | |
143 | s->pid_file_pathspec = NULL; | |
144 | } | |
145 | ||
5925dd3c | 146 | static int service_set_main_pid(Service *s, pid_t pid) { |
e55224ca LP |
147 | pid_t ppid; |
148 | ||
5925dd3c LP |
149 | assert(s); |
150 | ||
151 | if (pid <= 1) | |
152 | return -EINVAL; | |
153 | ||
154 | if (pid == getpid()) | |
155 | return -EINVAL; | |
156 | ||
7400b9d2 LP |
157 | if (s->main_pid == pid && s->main_pid_known) |
158 | return 0; | |
159 | ||
160 | if (s->main_pid != pid) { | |
161 | service_unwatch_main_pid(s); | |
162 | exec_status_start(&s->main_exec_status, pid); | |
163 | } | |
41efeaec | 164 | |
6dfa5494 LP |
165 | s->main_pid = pid; |
166 | s->main_pid_known = true; | |
167 | ||
168 | if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) { | |
66870f90 | 169 | log_warning_unit(UNIT(s)->id, |
ccd06097 ZJS |
170 | "%s: Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", |
171 | UNIT(s)->id, pid); | |
e55224ca | 172 | |
6dfa5494 LP |
173 | s->main_pid_alien = true; |
174 | } else | |
175 | s->main_pid_alien = false; | |
5925dd3c LP |
176 | |
177 | return 0; | |
178 | } | |
179 | ||
4f2d528d LP |
180 | static void service_close_socket_fd(Service *s) { |
181 | assert(s); | |
182 | ||
574634bc | 183 | s->socket_fd = asynchronous_close(s->socket_fd); |
4f2d528d LP |
184 | } |
185 | ||
6cf6bbc2 LP |
186 | static void service_connection_unref(Service *s) { |
187 | assert(s); | |
188 | ||
9444b1f2 | 189 | if (!UNIT_ISSET(s->accept_socket)) |
6cf6bbc2 LP |
190 | return; |
191 | ||
57020a3a LP |
192 | socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket))); |
193 | unit_ref_unset(&s->accept_socket); | |
6cf6bbc2 LP |
194 | } |
195 | ||
a6927d7f MO |
196 | static void service_stop_watchdog(Service *s) { |
197 | assert(s); | |
198 | ||
718db961 | 199 | s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source); |
842129f5 | 200 | s->watchdog_timestamp = DUAL_TIMESTAMP_NULL; |
a6927d7f MO |
201 | } |
202 | ||
842129f5 | 203 | static void service_start_watchdog(Service *s) { |
bb242b7b MO |
204 | int r; |
205 | ||
206 | assert(s); | |
207 | ||
842129f5 | 208 | if (s->watchdog_usec <= 0) |
bb242b7b MO |
209 | return; |
210 | ||
718db961 LP |
211 | if (s->watchdog_event_source) { |
212 | r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec); | |
213 | if (r < 0) { | |
214 | log_warning_unit(UNIT(s)->id, "%s failed to reset watchdog timer: %s", UNIT(s)->id, strerror(-r)); | |
215 | return; | |
216 | } | |
217 | ||
842129f5 | 218 | r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT); |
c4ef3317 | 219 | } else { |
6a0f1f6d LP |
220 | r = sd_event_add_time( |
221 | UNIT(s)->manager->event, | |
222 | &s->watchdog_event_source, | |
223 | CLOCK_MONOTONIC, | |
224 | s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, | |
225 | service_dispatch_watchdog, s); | |
c4ef3317 LP |
226 | if (r < 0) { |
227 | log_warning_unit(UNIT(s)->id, "%s failed to add watchdog timer: %s", UNIT(s)->id, strerror(-r)); | |
228 | return; | |
229 | } | |
230 | ||
231 | /* Let's process everything else which might be a sign | |
232 | * of living before we consider a service died. */ | |
233 | r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE); | |
234 | } | |
718db961 | 235 | |
bb242b7b | 236 | if (r < 0) |
842129f5 | 237 | log_warning_unit(UNIT(s)->id, "%s failed to install watchdog timer: %s", UNIT(s)->id, strerror(-r)); |
bb242b7b MO |
238 | } |
239 | ||
a6927d7f MO |
240 | static void service_reset_watchdog(Service *s) { |
241 | assert(s); | |
242 | ||
243 | dual_timestamp_get(&s->watchdog_timestamp); | |
842129f5 | 244 | service_start_watchdog(s); |
a6927d7f MO |
245 | } |
246 | ||
87f0e418 LP |
247 | static void service_done(Unit *u) { |
248 | Service *s = SERVICE(u); | |
44d8db9e LP |
249 | |
250 | assert(s); | |
251 | ||
252 | free(s->pid_file); | |
253 | s->pid_file = NULL; | |
254 | ||
8c47c732 LP |
255 | free(s->status_text); |
256 | s->status_text = NULL; | |
257 | ||
efe6e7d3 MO |
258 | free(s->reboot_arg); |
259 | s->reboot_arg = NULL; | |
260 | ||
613b411c | 261 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); |
e537352b | 262 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
44d8db9e | 263 | s->control_command = NULL; |
867b3b7d | 264 | s->main_command = NULL; |
44d8db9e | 265 | |
37520c1b LP |
266 | exit_status_set_free(&s->restart_prevent_status); |
267 | exit_status_set_free(&s->restart_force_status); | |
268 | exit_status_set_free(&s->success_status); | |
96342de6 | 269 | |
44d8db9e LP |
270 | /* This will leak a process, but at least no memory or any of |
271 | * our resources */ | |
5e94833f LP |
272 | service_unwatch_main_pid(s); |
273 | service_unwatch_control_pid(s); | |
3e52541e | 274 | service_unwatch_pid_file(s); |
44d8db9e | 275 | |
05e343b7 | 276 | if (s->bus_name) { |
ac155bb8 | 277 | unit_unwatch_bus_name(u, s->bus_name); |
05e343b7 LP |
278 | free(s->bus_name); |
279 | s->bus_name = NULL; | |
280 | } | |
281 | ||
4f2d528d | 282 | service_close_socket_fd(s); |
6cf6bbc2 | 283 | service_connection_unref(s); |
4f2d528d | 284 | |
57020a3a | 285 | unit_ref_unset(&s->accept_socket); |
f976f3f6 | 286 | |
bb242b7b MO |
287 | service_stop_watchdog(s); |
288 | ||
718db961 LP |
289 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
290 | } | |
291 | ||
292 | static int service_arm_timer(Service *s, usec_t usec) { | |
293 | int r; | |
294 | ||
295 | assert(s); | |
296 | ||
718db961 LP |
297 | if (s->timer_event_source) { |
298 | r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec); | |
299 | if (r < 0) | |
300 | return r; | |
301 | ||
302 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
303 | } | |
304 | ||
6a0f1f6d LP |
305 | return sd_event_add_time( |
306 | UNIT(s)->manager->event, | |
307 | &s->timer_event_source, | |
308 | CLOCK_MONOTONIC, | |
309 | now(CLOCK_MONOTONIC) + usec, 0, | |
310 | service_dispatch_timer, s); | |
44d8db9e LP |
311 | } |
312 | ||
243b1432 LP |
313 | static int service_verify(Service *s) { |
314 | assert(s); | |
315 | ||
1124fe6f | 316 | if (UNIT(s)->load_state != UNIT_LOADED) |
243b1432 LP |
317 | return 0; |
318 | ||
319 | if (!s->exec_command[SERVICE_EXEC_START]) { | |
ebc2259d | 320 | log_error_unit(UNIT(s)->id, "%s lacks ExecStart setting. Refusing.", UNIT(s)->id); |
243b1432 LP |
321 | return -EINVAL; |
322 | } | |
323 | ||
34e9ba66 LP |
324 | if (s->type != SERVICE_ONESHOT && |
325 | s->exec_command[SERVICE_EXEC_START]->command_next) { | |
ebc2259d | 326 | log_error_unit(UNIT(s)->id, "%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s)->id); |
6cf6bbc2 LP |
327 | return -EINVAL; |
328 | } | |
329 | ||
b0693d30 | 330 | if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) { |
37520c1b LP |
331 | log_error_unit(UNIT(s)->id, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id); |
332 | return -EINVAL; | |
333 | } | |
334 | ||
55ebf98c | 335 | if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) { |
37520c1b | 336 | log_error_unit(UNIT(s)->id, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s)->id); |
b0693d30 MW |
337 | return -EINVAL; |
338 | } | |
339 | ||
05e343b7 | 340 | if (s->type == SERVICE_DBUS && !s->bus_name) { |
ebc2259d | 341 | log_error_unit(UNIT(s)->id, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->id); |
4d0e5dbd LP |
342 | return -EINVAL; |
343 | } | |
344 | ||
7e2668c6 | 345 | if (s->bus_name && s->type != SERVICE_DBUS) |
ebc2259d | 346 | log_warning_unit(UNIT(s)->id, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s)->id); |
7e2668c6 | 347 | |
ebc2259d LP |
348 | if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) { |
349 | log_error_unit(UNIT(s)->id, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s)->id); | |
05e343b7 LP |
350 | return -EINVAL; |
351 | } | |
352 | ||
243b1432 LP |
353 | return 0; |
354 | } | |
355 | ||
a40eb732 LP |
356 | static int service_add_default_dependencies(Service *s) { |
357 | int r; | |
358 | ||
359 | assert(s); | |
360 | ||
361 | /* Add a number of automatic dependencies useful for the | |
362 | * majority of services. */ | |
363 | ||
364 | /* First, pull in base system */ | |
fccd44ec KS |
365 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, |
366 | SPECIAL_BASIC_TARGET, NULL, true); | |
367 | if (r < 0) | |
368 | return r; | |
a40eb732 LP |
369 | |
370 | /* Second, activate normal shutdown */ | |
117dcc57 ZJS |
371 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, |
372 | SPECIAL_SHUTDOWN_TARGET, NULL, true); | |
373 | return r; | |
a40eb732 LP |
374 | } |
375 | ||
4dfc092a LP |
376 | static void service_fix_output(Service *s) { |
377 | assert(s); | |
378 | ||
379 | /* If nothing has been explicitly configured, patch default | |
380 | * output in. If input is socket/tty we avoid this however, | |
381 | * since in that case we want output to default to the same | |
382 | * place as we read input from. */ | |
383 | ||
384 | if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT && | |
385 | s->exec_context.std_output == EXEC_OUTPUT_INHERIT && | |
386 | s->exec_context.std_input == EXEC_INPUT_NULL) | |
1124fe6f | 387 | s->exec_context.std_error = UNIT(s)->manager->default_std_error; |
4dfc092a LP |
388 | |
389 | if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT && | |
390 | s->exec_context.std_input == EXEC_INPUT_NULL) | |
1124fe6f | 391 | s->exec_context.std_output = UNIT(s)->manager->default_std_output; |
4dfc092a LP |
392 | } |
393 | ||
e537352b LP |
394 | static int service_load(Unit *u) { |
395 | int r; | |
396 | Service *s = SERVICE(u); | |
397 | ||
398 | assert(s); | |
1e2e8133 | 399 | |
5cb5a6ff | 400 | /* Load a .service file */ |
c2756a68 LP |
401 | r = unit_load_fragment(u); |
402 | if (r < 0) | |
5cb5a6ff LP |
403 | return r; |
404 | ||
23a177ef | 405 | /* Still nothing found? Then let's give up */ |
ac155bb8 | 406 | if (u->load_state == UNIT_STUB) |
23a177ef | 407 | return -ENOENT; |
034c6ed7 | 408 | |
23a177ef | 409 | /* This is a new unit? Then let's add in some extras */ |
ac155bb8 | 410 | if (u->load_state == UNIT_LOADED) { |
c2756a68 LP |
411 | |
412 | /* We were able to load something, then let's add in | |
413 | * the dropin directories. */ | |
414 | r = unit_load_dropin(u); | |
415 | if (r < 0) | |
416 | return r; | |
417 | ||
0b86feac LP |
418 | if (s->type == _SERVICE_TYPE_INVALID) |
419 | s->type = s->bus_name ? SERVICE_DBUS : SERVICE_SIMPLE; | |
420 | ||
d568a335 MS |
421 | /* Oneshot services have disabled start timeout by default */ |
422 | if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined) | |
423 | s->timeout_start_usec = 0; | |
98709151 | 424 | |
4e2b0f9b LP |
425 | service_fix_output(s); |
426 | ||
598459ce LP |
427 | r = unit_patch_contexts(u); |
428 | if (r < 0) | |
429 | return r; | |
430 | ||
117dcc57 ZJS |
431 | r = unit_add_exec_dependencies(u, &s->exec_context); |
432 | if (r < 0) | |
23a177ef LP |
433 | return r; |
434 | ||
598459ce | 435 | r = unit_add_default_slice(u, &s->cgroup_context); |
a016b922 LP |
436 | if (r < 0) |
437 | return r; | |
438 | ||
c952c6ec LP |
439 | if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE) |
440 | s->notify_access = NOTIFY_MAIN; | |
a40eb732 | 441 | |
02c4ef9c LP |
442 | if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE) |
443 | s->notify_access = NOTIFY_MAIN; | |
444 | ||
598459ce LP |
445 | if (s->bus_name) { |
446 | r = unit_watch_bus_name(u, s->bus_name); | |
117dcc57 | 447 | if (r < 0) |
a40eb732 | 448 | return r; |
117dcc57 | 449 | } |
e06c73cc | 450 | |
598459ce LP |
451 | if (u->default_dependencies) { |
452 | r = service_add_default_dependencies(s); | |
453 | if (r < 0) | |
817e224b | 454 | |
598459ce LP |
455 | return r; |
456 | } | |
8e274523 LP |
457 | } |
458 | ||
243b1432 | 459 | return service_verify(s); |
034c6ed7 LP |
460 | } |
461 | ||
87f0e418 | 462 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 463 | ServiceExecCommand c; |
87f0e418 | 464 | Service *s = SERVICE(u); |
47be870b | 465 | const char *prefix2; |
5cb5a6ff LP |
466 | |
467 | assert(s); | |
468 | ||
4c940960 LP |
469 | prefix = strempty(prefix); |
470 | prefix2 = strappenda(prefix, "\t"); | |
44d8db9e | 471 | |
5cb5a6ff | 472 | fprintf(f, |
81a2b7ce | 473 | "%sService State: %s\n" |
f42806df LP |
474 | "%sResult: %s\n" |
475 | "%sReload Result: %s\n" | |
81a2b7ce | 476 | "%sPermissionsStartOnly: %s\n" |
8e274523 | 477 | "%sRootDirectoryStartOnly: %s\n" |
02ee865a | 478 | "%sRemainAfterExit: %s\n" |
3185a36b | 479 | "%sGuessMainPID: %s\n" |
c952c6ec | 480 | "%sType: %s\n" |
2cf3143a | 481 | "%sRestart: %s\n" |
c952c6ec | 482 | "%sNotifyAccess: %s\n", |
81a2b7ce | 483 | prefix, service_state_to_string(s->state), |
f42806df LP |
484 | prefix, service_result_to_string(s->result), |
485 | prefix, service_result_to_string(s->reload_result), | |
81a2b7ce | 486 | prefix, yes_no(s->permissions_start_only), |
8e274523 | 487 | prefix, yes_no(s->root_directory_start_only), |
02ee865a | 488 | prefix, yes_no(s->remain_after_exit), |
3185a36b | 489 | prefix, yes_no(s->guess_main_pid), |
c952c6ec | 490 | prefix, service_type_to_string(s->type), |
2cf3143a | 491 | prefix, service_restart_to_string(s->restart), |
c952c6ec | 492 | prefix, notify_access_to_string(s->notify_access)); |
5cb5a6ff | 493 | |
70123e68 LP |
494 | if (s->control_pid > 0) |
495 | fprintf(f, | |
ccd06097 ZJS |
496 | "%sControl PID: "PID_FMT"\n", |
497 | prefix, s->control_pid); | |
70123e68 LP |
498 | |
499 | if (s->main_pid > 0) | |
500 | fprintf(f, | |
ccd06097 | 501 | "%sMain PID: "PID_FMT"\n" |
6dfa5494 LP |
502 | "%sMain PID Known: %s\n" |
503 | "%sMain PID Alien: %s\n", | |
ccd06097 | 504 | prefix, s->main_pid, |
6dfa5494 LP |
505 | prefix, yes_no(s->main_pid_known), |
506 | prefix, yes_no(s->main_pid_alien)); | |
70123e68 | 507 | |
034c6ed7 LP |
508 | if (s->pid_file) |
509 | fprintf(f, | |
510 | "%sPIDFile: %s\n", | |
511 | prefix, s->pid_file); | |
512 | ||
05e343b7 LP |
513 | if (s->bus_name) |
514 | fprintf(f, | |
515 | "%sBusName: %s\n" | |
516 | "%sBus Name Good: %s\n", | |
517 | prefix, s->bus_name, | |
518 | prefix, yes_no(s->bus_name_good)); | |
519 | ||
4819ff03 | 520 | kill_context_dump(&s->kill_context, f, prefix); |
5cb5a6ff LP |
521 | exec_context_dump(&s->exec_context, f, prefix); |
522 | ||
e537352b | 523 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
5cb5a6ff | 524 | |
44d8db9e LP |
525 | if (!s->exec_command[c]) |
526 | continue; | |
527 | ||
40d50879 | 528 | fprintf(f, "%s-> %s:\n", |
94f04347 | 529 | prefix, service_exec_command_to_string(c)); |
44d8db9e LP |
530 | |
531 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 532 | } |
44d8db9e | 533 | |
07459bb6 | 534 | #ifdef HAVE_SYSV_COMPAT |
2c4104f0 LP |
535 | if (s->sysv_start_priority >= 0) |
536 | fprintf(f, | |
9fff8a1f LP |
537 | "%sSysVStartPriority: %i\n", |
538 | prefix, s->sysv_start_priority); | |
07459bb6 | 539 | #endif |
23a177ef | 540 | |
8c47c732 LP |
541 | if (s->status_text) |
542 | fprintf(f, "%sStatus Text: %s\n", | |
543 | prefix, s->status_text); | |
5cb5a6ff LP |
544 | } |
545 | ||
c5419d42 | 546 | static int service_load_pid_file(Service *s, bool may_warn) { |
7fd1b19b | 547 | _cleanup_free_ char *k = NULL; |
034c6ed7 | 548 | int r; |
5925dd3c | 549 | pid_t pid; |
034c6ed7 LP |
550 | |
551 | assert(s); | |
552 | ||
034c6ed7 | 553 | if (!s->pid_file) |
13230d5d | 554 | return -ENOENT; |
034c6ed7 | 555 | |
117dcc57 ZJS |
556 | r = read_one_line_file(s->pid_file, &k); |
557 | if (r < 0) { | |
c5419d42 | 558 | if (may_warn) |
66870f90 ZJS |
559 | log_info_unit(UNIT(s)->id, |
560 | "PID file %s not readable (yet?) after %s.", | |
561 | s->pid_file, service_state_to_string(s->state)); | |
034c6ed7 | 562 | return r; |
5375410b | 563 | } |
034c6ed7 | 564 | |
5925dd3c | 565 | r = parse_pid(k, &pid); |
bc41f93e ZJS |
566 | if (r < 0) { |
567 | if (may_warn) | |
568 | log_info_unit(UNIT(s)->id, | |
569 | "Failed to read PID from file %s: %s", | |
570 | s->pid_file, strerror(-r)); | |
5925dd3c | 571 | return r; |
bc41f93e | 572 | } |
406eaf93 | 573 | |
9f5650ae | 574 | if (!pid_is_alive(pid)) { |
c5419d42 | 575 | if (may_warn) |
9f5650ae | 576 | log_info_unit(UNIT(s)->id, "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file); |
b8c597d5 | 577 | |
e10c9985 YS |
578 | return -ESRCH; |
579 | } | |
580 | ||
db01f8b3 MS |
581 | if (s->main_pid_known) { |
582 | if (pid == s->main_pid) | |
583 | return 0; | |
584 | ||
66870f90 | 585 | log_debug_unit(UNIT(s)->id, |
ccd06097 ZJS |
586 | "Main PID changing: "PID_FMT" -> "PID_FMT, |
587 | s->main_pid, pid); | |
db01f8b3 MS |
588 | service_unwatch_main_pid(s); |
589 | s->main_pid_known = false; | |
3a111838 | 590 | } else |
66870f90 | 591 | log_debug_unit(UNIT(s)->id, |
ccd06097 | 592 | "Main PID loaded: "PID_FMT, pid); |
db01f8b3 | 593 | |
117dcc57 ZJS |
594 | r = service_set_main_pid(s, pid); |
595 | if (r < 0) | |
16f6025e LP |
596 | return r; |
597 | ||
117dcc57 | 598 | r = unit_watch_pid(UNIT(s), pid); |
bc41f93e | 599 | if (r < 0) { |
5925dd3c | 600 | /* FIXME: we need to do something here */ |
bc41f93e | 601 | log_warning_unit(UNIT(s)->id, |
ccd06097 ZJS |
602 | "Failed to watch PID "PID_FMT" from service %s", |
603 | pid, UNIT(s)->id); | |
5925dd3c | 604 | return r; |
bc41f93e | 605 | } |
034c6ed7 LP |
606 | |
607 | return 0; | |
608 | } | |
609 | ||
4fbf50b3 LP |
610 | static int service_search_main_pid(Service *s) { |
611 | pid_t pid; | |
612 | int r; | |
613 | ||
614 | assert(s); | |
615 | ||
3185a36b LP |
616 | /* If we know it anyway, don't ever fallback to unreliable |
617 | * heuristics */ | |
4fbf50b3 LP |
618 | if (s->main_pid_known) |
619 | return 0; | |
620 | ||
3185a36b LP |
621 | if (!s->guess_main_pid) |
622 | return 0; | |
623 | ||
4fbf50b3 LP |
624 | assert(s->main_pid <= 0); |
625 | ||
4ad49000 | 626 | pid = unit_search_main_pid(UNIT(s)); |
117dcc57 | 627 | if (pid <= 0) |
4fbf50b3 LP |
628 | return -ENOENT; |
629 | ||
66870f90 | 630 | log_debug_unit(UNIT(s)->id, |
ccd06097 | 631 | "Main PID guessed: "PID_FMT, pid); |
117dcc57 ZJS |
632 | r = service_set_main_pid(s, pid); |
633 | if (r < 0) | |
4fbf50b3 LP |
634 | return r; |
635 | ||
117dcc57 ZJS |
636 | r = unit_watch_pid(UNIT(s), pid); |
637 | if (r < 0) | |
4fbf50b3 | 638 | /* FIXME: we need to do something here */ |
a827e373 | 639 | log_warning_unit(UNIT(s)->id, |
ccd06097 ZJS |
640 | "Failed to watch PID "PID_FMT" from service %s", |
641 | pid, UNIT(s)->id); | |
bdd13f6b | 642 | return r; |
4fbf50b3 LP |
643 | } |
644 | ||
034c6ed7 LP |
645 | static void service_set_state(Service *s, ServiceState state) { |
646 | ServiceState old_state; | |
e056b01d | 647 | const UnitActiveState *table; |
842129f5 | 648 | |
5cb5a6ff LP |
649 | assert(s); |
650 | ||
e056b01d LP |
651 | table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
652 | ||
034c6ed7 | 653 | old_state = s->state; |
5cb5a6ff | 654 | s->state = state; |
034c6ed7 | 655 | |
3a111838 MS |
656 | service_unwatch_pid_file(s); |
657 | ||
842129f5 LP |
658 | if (!IN_SET(state, |
659 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
660 | SERVICE_RELOAD, | |
bf108e55 LP |
661 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, |
662 | SERVICE_STOP_POST, | |
842129f5 LP |
663 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
664 | SERVICE_AUTO_RESTART)) | |
718db961 | 665 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
034c6ed7 | 666 | |
842129f5 LP |
667 | if (!IN_SET(state, |
668 | SERVICE_START, SERVICE_START_POST, | |
669 | SERVICE_RUNNING, SERVICE_RELOAD, | |
bf108e55 LP |
670 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, |
671 | SERVICE_STOP_POST, | |
672 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { | |
5e94833f | 673 | service_unwatch_main_pid(s); |
867b3b7d LP |
674 | s->main_command = NULL; |
675 | } | |
034c6ed7 | 676 | |
842129f5 LP |
677 | if (!IN_SET(state, |
678 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
679 | SERVICE_RELOAD, | |
bf108e55 LP |
680 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, |
681 | SERVICE_STOP_POST, | |
842129f5 | 682 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
5e94833f | 683 | service_unwatch_control_pid(s); |
034c6ed7 | 684 | s->control_command = NULL; |
a16e1123 | 685 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
e537352b | 686 | } |
034c6ed7 | 687 | |
a911bb9a LP |
688 | if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
689 | unit_unwatch_all_pids(UNIT(s)); | |
690 | ||
842129f5 LP |
691 | if (!IN_SET(state, |
692 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
693 | SERVICE_RUNNING, SERVICE_RELOAD, | |
694 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
695 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) && | |
1124fe6f | 696 | !(state == SERVICE_DEAD && UNIT(s)->job)) { |
4f2d528d | 697 | service_close_socket_fd(s); |
6cf6bbc2 LP |
698 | service_connection_unref(s); |
699 | } | |
4f2d528d | 700 | |
7596e9e1 | 701 | if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
a6927d7f MO |
702 | service_stop_watchdog(s); |
703 | ||
f6023656 LP |
704 | /* For the inactive states unit_notify() will trim the cgroup, |
705 | * but for exit we have to do that ourselves... */ | |
1124fe6f | 706 | if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0) |
4ad49000 | 707 | unit_destroy_cgroup(UNIT(s)); |
f6023656 | 708 | |
9cd86184 OB |
709 | /* For remain_after_exit services, let's see if we can "release" the |
710 | * hold on the console, since unit_notify() only does that in case of | |
711 | * change of state */ | |
712 | if (state == SERVICE_EXITED && s->remain_after_exit && | |
713 | UNIT(s)->manager->n_on_console > 0) { | |
714 | ExecContext *ec = unit_get_exec_context(UNIT(s)); | |
715 | if (ec && exec_context_may_touch_console(ec)) { | |
716 | Manager *m = UNIT(s)->manager; | |
717 | ||
718 | m->n_on_console --; | |
719 | if (m->n_on_console == 0) | |
720 | /* unset no_console_output flag, since the console is free */ | |
721 | m->no_console_output = false; | |
722 | } | |
723 | } | |
724 | ||
e537352b | 725 | if (old_state != state) |
842129f5 | 726 | log_debug_unit(UNIT(s)->id, "%s changed %s -> %s", UNIT(s)->id, service_state_to_string(old_state), service_state_to_string(state)); |
acbb0225 | 727 | |
e056b01d | 728 | unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS); |
f42806df | 729 | s->reload_result = SERVICE_SUCCESS; |
034c6ed7 LP |
730 | } |
731 | ||
a16e1123 LP |
732 | static int service_coldplug(Unit *u) { |
733 | Service *s = SERVICE(u); | |
734 | int r; | |
735 | ||
736 | assert(s); | |
737 | assert(s->state == SERVICE_DEAD); | |
738 | ||
739 | if (s->deserialized_state != s->state) { | |
740 | ||
bf108e55 LP |
741 | if (IN_SET(s->deserialized_state, |
742 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
743 | SERVICE_RELOAD, | |
744 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, | |
745 | SERVICE_STOP_POST, | |
746 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { | |
92c1622e LP |
747 | |
748 | usec_t k; | |
6c12b52e | 749 | |
bf108e55 | 750 | k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec; |
e558336f | 751 | |
92c1622e LP |
752 | /* For the start/stop timeouts 0 means off */ |
753 | if (k > 0) { | |
754 | r = service_arm_timer(s, k); | |
36697dc0 | 755 | if (r < 0) |
e558336f LP |
756 | return r; |
757 | } | |
758 | } | |
a16e1123 | 759 | |
92c1622e LP |
760 | if (s->deserialized_state == SERVICE_AUTO_RESTART) { |
761 | ||
762 | /* The restart timeouts 0 means immediately */ | |
763 | r = service_arm_timer(s, s->restart_usec); | |
764 | if (r < 0) | |
765 | return r; | |
766 | } | |
767 | ||
9f5650ae | 768 | if (pid_is_unwaited(s->main_pid) && |
bf108e55 LP |
769 | ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) || |
770 | IN_SET(s->deserialized_state, | |
771 | SERVICE_START, SERVICE_START_POST, | |
772 | SERVICE_RUNNING, SERVICE_RELOAD, | |
773 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, | |
774 | SERVICE_STOP_POST, | |
775 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) { | |
776 | r = unit_watch_pid(UNIT(s), s->main_pid); | |
777 | if (r < 0) | |
778 | return r; | |
779 | } | |
a16e1123 | 780 | |
9f5650ae | 781 | if (pid_is_unwaited(s->control_pid) && |
bf108e55 LP |
782 | IN_SET(s->deserialized_state, |
783 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
784 | SERVICE_RELOAD, | |
785 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, | |
786 | SERVICE_STOP_POST, | |
787 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { | |
788 | r = unit_watch_pid(UNIT(s), s->control_pid); | |
789 | if (r < 0) | |
790 | return r; | |
791 | } | |
a16e1123 | 792 | |
a911bb9a LP |
793 | if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
794 | unit_watch_all_pids(UNIT(s)); | |
795 | ||
842129f5 LP |
796 | if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
797 | service_start_watchdog(s); | |
bb242b7b | 798 | |
a16e1123 LP |
799 | service_set_state(s, s->deserialized_state); |
800 | } | |
92c1622e | 801 | |
a16e1123 LP |
802 | return 0; |
803 | } | |
804 | ||
44d8db9e LP |
805 | static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { |
806 | Iterator i; | |
807 | int r; | |
808 | int *rfds = NULL; | |
809 | unsigned rn_fds = 0; | |
57020a3a | 810 | Unit *u; |
44d8db9e LP |
811 | |
812 | assert(s); | |
813 | assert(fds); | |
814 | assert(n_fds); | |
815 | ||
6cf6bbc2 LP |
816 | if (s->socket_fd >= 0) |
817 | return 0; | |
818 | ||
1124fe6f | 819 | SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) { |
44d8db9e LP |
820 | int *cfds; |
821 | unsigned cn_fds; | |
57020a3a LP |
822 | Socket *sock; |
823 | ||
ac155bb8 | 824 | if (u->type != UNIT_SOCKET) |
57020a3a LP |
825 | continue; |
826 | ||
827 | sock = SOCKET(u); | |
44d8db9e | 828 | |
117dcc57 ZJS |
829 | r = socket_collect_fds(sock, &cfds, &cn_fds); |
830 | if (r < 0) | |
44d8db9e LP |
831 | goto fail; |
832 | ||
833 | if (!cfds) | |
834 | continue; | |
835 | ||
836 | if (!rfds) { | |
837 | rfds = cfds; | |
838 | rn_fds = cn_fds; | |
839 | } else { | |
840 | int *t; | |
841 | ||
117dcc57 ZJS |
842 | t = new(int, rn_fds+cn_fds); |
843 | if (!t) { | |
44d8db9e LP |
844 | free(cfds); |
845 | r = -ENOMEM; | |
846 | goto fail; | |
847 | } | |
848 | ||
9c1b183c LP |
849 | memcpy(t, rfds, rn_fds * sizeof(int)); |
850 | memcpy(t+rn_fds, cfds, cn_fds * sizeof(int)); | |
44d8db9e LP |
851 | free(rfds); |
852 | free(cfds); | |
853 | ||
854 | rfds = t; | |
855 | rn_fds = rn_fds+cn_fds; | |
856 | } | |
857 | } | |
858 | ||
859 | *fds = rfds; | |
860 | *n_fds = rn_fds; | |
3e33402a | 861 | |
44d8db9e LP |
862 | return 0; |
863 | ||
864 | fail: | |
865 | free(rfds); | |
3e33402a | 866 | |
44d8db9e LP |
867 | return r; |
868 | } | |
869 | ||
81a2b7ce LP |
870 | static int service_spawn( |
871 | Service *s, | |
872 | ExecCommand *c, | |
873 | bool timeout, | |
874 | bool pass_fds, | |
875 | bool apply_permissions, | |
876 | bool apply_chroot, | |
1e3ad081 | 877 | bool apply_tty_stdin, |
c952c6ec | 878 | bool set_notify_socket, |
ecedd90f | 879 | bool is_control, |
81a2b7ce LP |
880 | pid_t *_pid) { |
881 | ||
034c6ed7 LP |
882 | pid_t pid; |
883 | int r; | |
117dcc57 | 884 | int *fds = NULL; |
7fd1b19b | 885 | _cleanup_free_ int *fdsbuf = NULL; |
2105e76a | 886 | unsigned n_fds = 0, n_env = 0; |
7fd1b19b | 887 | _cleanup_strv_free_ char |
117dcc57 | 888 | **argv = NULL, **final_env = NULL, **our_env = NULL; |
4ad49000 | 889 | const char *path; |
034c6ed7 LP |
890 | |
891 | assert(s); | |
892 | assert(c); | |
893 | assert(_pid); | |
894 | ||
4ad49000 LP |
895 | unit_realize_cgroup(UNIT(s)); |
896 | ||
613b411c LP |
897 | r = unit_setup_exec_runtime(UNIT(s)); |
898 | if (r < 0) | |
899 | goto fail; | |
900 | ||
6cf6bbc2 LP |
901 | if (pass_fds || |
902 | s->exec_context.std_input == EXEC_INPUT_SOCKET || | |
903 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
904 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
905 | ||
4f2d528d LP |
906 | if (s->socket_fd >= 0) { |
907 | fds = &s->socket_fd; | |
908 | n_fds = 1; | |
6cf6bbc2 | 909 | } else { |
117dcc57 ZJS |
910 | r = service_collect_fds(s, &fdsbuf, &n_fds); |
911 | if (r < 0) | |
6cf6bbc2 LP |
912 | goto fail; |
913 | ||
914 | fds = fdsbuf; | |
915 | } | |
4f2d528d | 916 | } |
44d8db9e | 917 | |
92c1622e LP |
918 | if (timeout && s->timeout_start_usec > 0) { |
919 | r = service_arm_timer(s, s->timeout_start_usec); | |
920 | if (r < 0) | |
921 | goto fail; | |
922 | } else | |
923 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
034c6ed7 | 924 | |
19f6d710 LP |
925 | r = unit_full_printf_strv(UNIT(s), c->argv, &argv); |
926 | if (r < 0) | |
9e2f7c11 | 927 | goto fail; |
9e2f7c11 | 928 | |
09812eb7 | 929 | our_env = new0(char*, 4); |
97ae63e2 | 930 | if (!our_env) { |
2105e76a LP |
931 | r = -ENOMEM; |
932 | goto fail; | |
933 | } | |
c952c6ec | 934 | |
2105e76a | 935 | if (set_notify_socket) |
1124fe6f | 936 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) { |
c952c6ec LP |
937 | r = -ENOMEM; |
938 | goto fail; | |
939 | } | |
940 | ||
2105e76a | 941 | if (s->main_pid > 0) |
ccd06097 | 942 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) { |
c952c6ec LP |
943 | r = -ENOMEM; |
944 | goto fail; | |
945 | } | |
2105e76a | 946 | |
4ad49000 | 947 | if (UNIT(s)->manager->running_as != SYSTEMD_SYSTEM) |
ccd06097 | 948 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) { |
97ae63e2 LP |
949 | r = -ENOMEM; |
950 | goto fail; | |
951 | } | |
952 | ||
953 | final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL); | |
954 | if (!final_env) { | |
2105e76a LP |
955 | r = -ENOMEM; |
956 | goto fail; | |
957 | } | |
c952c6ec | 958 | |
4ad49000 LP |
959 | if (is_control && UNIT(s)->cgroup_path) { |
960 | path = strappenda(UNIT(s)->cgroup_path, "/control"); | |
961 | cg_create(SYSTEMD_CGROUP_CONTROLLER, path); | |
962 | } else | |
963 | path = UNIT(s)->cgroup_path; | |
964 | ||
9e2f7c11 LP |
965 | r = exec_spawn(c, |
966 | argv, | |
967 | &s->exec_context, | |
968 | fds, n_fds, | |
2105e76a | 969 | final_env, |
9e2f7c11 LP |
970 | apply_permissions, |
971 | apply_chroot, | |
1e3ad081 | 972 | apply_tty_stdin, |
1124fe6f | 973 | UNIT(s)->manager->confirm_spawn, |
13b84ec7 | 974 | UNIT(s)->manager->cgroup_supported, |
4ad49000 | 975 | path, |
e66cf1a3 | 976 | manager_get_runtime_prefix(UNIT(s)->manager), |
62bca2c6 | 977 | UNIT(s)->id, |
09812eb7 | 978 | s->watchdog_usec, |
f2b68789 | 979 | s->type == SERVICE_IDLE ? UNIT(s)->manager->idle_pipe : NULL, |
613b411c | 980 | s->exec_runtime, |
9e2f7c11 | 981 | &pid); |
9e2f7c11 | 982 | if (r < 0) |
034c6ed7 LP |
983 | goto fail; |
984 | ||
117dcc57 ZJS |
985 | r = unit_watch_pid(UNIT(s), pid); |
986 | if (r < 0) | |
034c6ed7 LP |
987 | /* FIXME: we need to do something here */ |
988 | goto fail; | |
989 | ||
990 | *_pid = pid; | |
991 | ||
5cb5a6ff | 992 | return 0; |
034c6ed7 LP |
993 | |
994 | fail: | |
995 | if (timeout) | |
718db961 | 996 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
034c6ed7 LP |
997 | |
998 | return r; | |
999 | } | |
1000 | ||
80876c20 LP |
1001 | static int main_pid_good(Service *s) { |
1002 | assert(s); | |
1003 | ||
1004 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
1005 | * don't know */ | |
1006 | ||
1007 | /* If we know the pid file, then lets just check if it is | |
1008 | * still valid */ | |
6dfa5494 LP |
1009 | if (s->main_pid_known) { |
1010 | ||
1011 | /* If it's an alien child let's check if it is still | |
1012 | * alive ... */ | |
62220cf7 | 1013 | if (s->main_pid_alien && s->main_pid > 0) |
9f5650ae | 1014 | return pid_is_alive(s->main_pid); |
6dfa5494 LP |
1015 | |
1016 | /* .. otherwise assume we'll get a SIGCHLD for it, | |
1017 | * which we really should wait for to collect exit | |
1018 | * status and code */ | |
80876c20 | 1019 | return s->main_pid > 0; |
6dfa5494 | 1020 | } |
80876c20 LP |
1021 | |
1022 | /* We don't know the pid */ | |
1023 | return -EAGAIN; | |
1024 | } | |
1025 | ||
44a6b1b6 | 1026 | _pure_ static int control_pid_good(Service *s) { |
80876c20 LP |
1027 | assert(s); |
1028 | ||
1029 | return s->control_pid > 0; | |
1030 | } | |
1031 | ||
1032 | static int cgroup_good(Service *s) { | |
1033 | int r; | |
1034 | ||
1035 | assert(s); | |
1036 | ||
4ad49000 LP |
1037 | if (!UNIT(s)->cgroup_path) |
1038 | return 0; | |
1039 | ||
1040 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path, true); | |
117dcc57 | 1041 | if (r < 0) |
80876c20 LP |
1042 | return r; |
1043 | ||
1044 | return !r; | |
1045 | } | |
1046 | ||
bf500566 | 1047 | static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none); |
93ae25e6 | 1048 | |
f42806df | 1049 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
034c6ed7 LP |
1050 | int r; |
1051 | assert(s); | |
1052 | ||
f42806df LP |
1053 | if (f != SERVICE_SUCCESS) |
1054 | s->result = f; | |
034c6ed7 | 1055 | |
0c7f15b3 MS |
1056 | service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD); |
1057 | ||
93ae25e6 MO |
1058 | if (s->result != SERVICE_SUCCESS) |
1059 | service_execute_action(s, s->failure_action, "failed", false); | |
1060 | ||
034c6ed7 | 1061 | if (allow_restart && |
47342320 | 1062 | !s->forbid_restart && |
034c6ed7 | 1063 | (s->restart == SERVICE_RESTART_ALWAYS || |
f42806df LP |
1064 | (s->restart == SERVICE_RESTART_ON_SUCCESS && s->result == SERVICE_SUCCESS) || |
1065 | (s->restart == SERVICE_RESTART_ON_FAILURE && s->result != SERVICE_SUCCESS) || | |
6cfe2fde | 1066 | (s->restart == SERVICE_RESTART_ON_ABNORMAL && !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE)) || |
dc99a976 | 1067 | (s->restart == SERVICE_RESTART_ON_WATCHDOG && s->result == SERVICE_FAILURE_WATCHDOG) || |
37520c1b | 1068 | (s->restart == SERVICE_RESTART_ON_ABORT && IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP)) || |
3e2d435b | 1069 | (s->main_exec_status.code == CLD_EXITED && set_contains(s->restart_force_status.status, INT_TO_PTR(s->main_exec_status.status))) || |
37520c1b | 1070 | (IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) && set_contains(s->restart_force_status.signal, INT_TO_PTR(s->main_exec_status.status)))) && |
3e2d435b | 1071 | (s->main_exec_status.code != CLD_EXITED || !set_contains(s->restart_prevent_status.status, INT_TO_PTR(s->main_exec_status.status))) && |
37520c1b | 1072 | (!IN_SET(s->main_exec_status.code, CLD_KILLED, CLD_DUMPED) || !set_contains(s->restart_prevent_status.signal, INT_TO_PTR(s->main_exec_status.status)))) { |
034c6ed7 | 1073 | |
718db961 | 1074 | r = service_arm_timer(s, s->restart_usec); |
f42806df | 1075 | if (r < 0) |
034c6ed7 LP |
1076 | goto fail; |
1077 | ||
1078 | service_set_state(s, SERVICE_AUTO_RESTART); | |
0c7f15b3 | 1079 | } |
034c6ed7 | 1080 | |
47342320 LP |
1081 | s->forbid_restart = false; |
1082 | ||
e66cf1a3 | 1083 | /* We want fresh tmpdirs in case service is started again immediately */ |
613b411c LP |
1084 | exec_runtime_destroy(s->exec_runtime); |
1085 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); | |
c17ec25e | 1086 | |
e66cf1a3 LP |
1087 | /* Also, remove the runtime directory in */ |
1088 | exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager)); | |
1089 | ||
9285c9ff LN |
1090 | /* Try to delete the pid file. At this point it will be |
1091 | * out-of-date, and some software might be confused by it, so | |
1092 | * let's remove it. */ | |
1093 | if (s->pid_file) | |
1094 | unlink_noerrno(s->pid_file); | |
1095 | ||
034c6ed7 LP |
1096 | return; |
1097 | ||
1098 | fail: | |
66870f90 ZJS |
1099 | log_warning_unit(UNIT(s)->id, |
1100 | "%s failed to run install restart timer: %s", | |
1101 | UNIT(s)->id, strerror(-r)); | |
f42806df | 1102 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1103 | } |
1104 | ||
f42806df | 1105 | static void service_enter_stop_post(Service *s, ServiceResult f) { |
034c6ed7 LP |
1106 | int r; |
1107 | assert(s); | |
1108 | ||
f42806df LP |
1109 | if (f != SERVICE_SUCCESS) |
1110 | s->result = f; | |
034c6ed7 | 1111 | |
5e94833f | 1112 | service_unwatch_control_pid(s); |
a911bb9a | 1113 | unit_watch_all_pids(UNIT(s)); |
5e94833f | 1114 | |
117dcc57 ZJS |
1115 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; |
1116 | if (s->control_command) { | |
867b3b7d LP |
1117 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
1118 | ||
ecedd90f LP |
1119 | r = service_spawn(s, |
1120 | s->control_command, | |
1121 | true, | |
1122 | false, | |
1123 | !s->permissions_start_only, | |
1124 | !s->root_directory_start_only, | |
1125 | true, | |
1126 | false, | |
1127 | true, | |
1128 | &s->control_pid); | |
1129 | if (r < 0) | |
034c6ed7 LP |
1130 | goto fail; |
1131 | ||
80876c20 LP |
1132 | service_set_state(s, SERVICE_STOP_POST); |
1133 | } else | |
ac84d1fb | 1134 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1135 | |
1136 | return; | |
1137 | ||
1138 | fail: | |
66870f90 ZJS |
1139 | log_warning_unit(UNIT(s)->id, |
1140 | "%s failed to run 'stop-post' task: %s", | |
1141 | UNIT(s)->id, strerror(-r)); | |
f42806df | 1142 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1143 | } |
1144 | ||
f42806df | 1145 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
034c6ed7 | 1146 | int r; |
034c6ed7 LP |
1147 | |
1148 | assert(s); | |
1149 | ||
f42806df LP |
1150 | if (f != SERVICE_SUCCESS) |
1151 | s->result = f; | |
034c6ed7 | 1152 | |
a911bb9a LP |
1153 | unit_watch_all_pids(UNIT(s)); |
1154 | ||
cd2086fe LP |
1155 | r = unit_kill_context( |
1156 | UNIT(s), | |
1157 | &s->kill_context, | |
1158 | state != SERVICE_STOP_SIGTERM && state != SERVICE_FINAL_SIGTERM, | |
1159 | s->main_pid, | |
1160 | s->control_pid, | |
1161 | s->main_pid_alien); | |
ac84d1fb | 1162 | |
cd2086fe LP |
1163 | if (r < 0) |
1164 | goto fail; | |
034c6ed7 | 1165 | |
cd2086fe | 1166 | if (r > 0) { |
d568a335 | 1167 | if (s->timeout_stop_usec > 0) { |
718db961 | 1168 | r = service_arm_timer(s, s->timeout_stop_usec); |
d568a335 | 1169 | if (r < 0) |
e558336f | 1170 | goto fail; |
d568a335 | 1171 | } |
d6ea93e3 | 1172 | |
80876c20 | 1173 | service_set_state(s, state); |
ac84d1fb LP |
1174 | } else if (state == SERVICE_STOP_SIGTERM) |
1175 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); | |
1176 | else if (state == SERVICE_STOP_SIGKILL) | |
f42806df | 1177 | service_enter_stop_post(s, SERVICE_SUCCESS); |
ac84d1fb LP |
1178 | else if (state == SERVICE_FINAL_SIGTERM) |
1179 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); | |
80876c20 | 1180 | else |
f42806df | 1181 | service_enter_dead(s, SERVICE_SUCCESS, true); |
034c6ed7 LP |
1182 | |
1183 | return; | |
1184 | ||
1185 | fail: | |
66870f90 ZJS |
1186 | log_warning_unit(UNIT(s)->id, |
1187 | "%s failed to kill processes: %s", UNIT(s)->id, strerror(-r)); | |
034c6ed7 | 1188 | |
80876c20 | 1189 | if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) |
f42806df | 1190 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 1191 | else |
f42806df | 1192 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
1193 | } |
1194 | ||
f42806df | 1195 | static void service_enter_stop(Service *s, ServiceResult f) { |
034c6ed7 | 1196 | int r; |
5925dd3c | 1197 | |
034c6ed7 LP |
1198 | assert(s); |
1199 | ||
f42806df LP |
1200 | if (f != SERVICE_SUCCESS) |
1201 | s->result = f; | |
034c6ed7 | 1202 | |
5e94833f | 1203 | service_unwatch_control_pid(s); |
a911bb9a | 1204 | unit_watch_all_pids(UNIT(s)); |
5e94833f | 1205 | |
117dcc57 ZJS |
1206 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; |
1207 | if (s->control_command) { | |
867b3b7d LP |
1208 | s->control_command_id = SERVICE_EXEC_STOP; |
1209 | ||
ecedd90f LP |
1210 | r = service_spawn(s, |
1211 | s->control_command, | |
1212 | true, | |
1213 | false, | |
1214 | !s->permissions_start_only, | |
1215 | !s->root_directory_start_only, | |
1216 | false, | |
1217 | false, | |
1218 | true, | |
1219 | &s->control_pid); | |
1220 | if (r < 0) | |
034c6ed7 LP |
1221 | goto fail; |
1222 | ||
80876c20 LP |
1223 | service_set_state(s, SERVICE_STOP); |
1224 | } else | |
f42806df | 1225 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1226 | |
1227 | return; | |
1228 | ||
1229 | fail: | |
66870f90 ZJS |
1230 | log_warning_unit(UNIT(s)->id, |
1231 | "%s failed to run 'stop' task: %s", UNIT(s)->id, strerror(-r)); | |
f42806df | 1232 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1233 | } |
1234 | ||
f42806df | 1235 | static void service_enter_running(Service *s, ServiceResult f) { |
4eab639f | 1236 | int main_pid_ok, cgroup_ok; |
80876c20 LP |
1237 | assert(s); |
1238 | ||
f42806df LP |
1239 | if (f != SERVICE_SUCCESS) |
1240 | s->result = f; | |
80876c20 | 1241 | |
4eab639f LP |
1242 | main_pid_ok = main_pid_good(s); |
1243 | cgroup_ok = cgroup_good(s); | |
1244 | ||
1245 | if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) && | |
05e343b7 | 1246 | (s->bus_name_good || s->type != SERVICE_DBUS)) |
80876c20 | 1247 | service_set_state(s, SERVICE_RUNNING); |
02ee865a | 1248 | else if (s->remain_after_exit) |
80876c20 LP |
1249 | service_set_state(s, SERVICE_EXITED); |
1250 | else | |
f42806df | 1251 | service_enter_stop(s, SERVICE_SUCCESS); |
80876c20 LP |
1252 | } |
1253 | ||
034c6ed7 LP |
1254 | static void service_enter_start_post(Service *s) { |
1255 | int r; | |
1256 | assert(s); | |
1257 | ||
5e94833f | 1258 | service_unwatch_control_pid(s); |
842129f5 | 1259 | service_reset_watchdog(s); |
bb242b7b | 1260 | |
117dcc57 ZJS |
1261 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; |
1262 | if (s->control_command) { | |
867b3b7d LP |
1263 | s->control_command_id = SERVICE_EXEC_START_POST; |
1264 | ||
ecedd90f LP |
1265 | r = service_spawn(s, |
1266 | s->control_command, | |
1267 | true, | |
1268 | false, | |
1269 | !s->permissions_start_only, | |
1270 | !s->root_directory_start_only, | |
1271 | false, | |
1272 | false, | |
1273 | true, | |
1274 | &s->control_pid); | |
1275 | if (r < 0) | |
034c6ed7 LP |
1276 | goto fail; |
1277 | ||
80876c20 LP |
1278 | service_set_state(s, SERVICE_START_POST); |
1279 | } else | |
f42806df | 1280 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
1281 | |
1282 | return; | |
1283 | ||
1284 | fail: | |
66870f90 ZJS |
1285 | log_warning_unit(UNIT(s)->id, |
1286 | "%s failed to run 'start-post' task: %s", UNIT(s)->id, strerror(-r)); | |
f42806df | 1287 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1288 | } |
1289 | ||
4ad49000 LP |
1290 | static void service_kill_control_processes(Service *s) { |
1291 | char *p; | |
1292 | ||
1293 | if (!UNIT(s)->cgroup_path) | |
1294 | return; | |
1295 | ||
1296 | p = strappenda(UNIT(s)->cgroup_path, "/control"); | |
4ad49000 LP |
1297 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL); |
1298 | } | |
1299 | ||
034c6ed7 | 1300 | static void service_enter_start(Service *s) { |
4ad49000 | 1301 | ExecCommand *c; |
034c6ed7 LP |
1302 | pid_t pid; |
1303 | int r; | |
1304 | ||
1305 | assert(s); | |
1306 | ||
1307 | assert(s->exec_command[SERVICE_EXEC_START]); | |
34e9ba66 | 1308 | assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT); |
034c6ed7 | 1309 | |
41efeaec LP |
1310 | service_unwatch_control_pid(s); |
1311 | service_unwatch_main_pid(s); | |
80876c20 | 1312 | |
8f53a7b8 LP |
1313 | /* We want to ensure that nobody leaks processes from |
1314 | * START_PRE here, so let's go on a killing spree, People | |
1315 | * should not spawn long running processes from START_PRE. */ | |
4ad49000 | 1316 | service_kill_control_processes(s); |
8f53a7b8 | 1317 | |
867b3b7d LP |
1318 | if (s->type == SERVICE_FORKING) { |
1319 | s->control_command_id = SERVICE_EXEC_START; | |
1320 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
1321 | ||
1322 | s->main_command = NULL; | |
1323 | } else { | |
1324 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
1325 | s->control_command = NULL; | |
1326 | ||
1327 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
1328 | } | |
34e9ba66 | 1329 | |
ecedd90f LP |
1330 | r = service_spawn(s, |
1331 | c, | |
117dcc57 ZJS |
1332 | s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || |
1333 | s->type == SERVICE_NOTIFY || s->type == SERVICE_ONESHOT, | |
ecedd90f LP |
1334 | true, |
1335 | true, | |
1336 | true, | |
1337 | true, | |
1338 | s->notify_access != NOTIFY_NONE, | |
1339 | false, | |
1340 | &pid); | |
1341 | if (r < 0) | |
034c6ed7 LP |
1342 | goto fail; |
1343 | ||
f2b68789 | 1344 | if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) { |
034c6ed7 LP |
1345 | /* For simple services we immediately start |
1346 | * the START_POST binaries. */ | |
1347 | ||
5925dd3c | 1348 | service_set_main_pid(s, pid); |
034c6ed7 LP |
1349 | service_enter_start_post(s); |
1350 | ||
1351 | } else if (s->type == SERVICE_FORKING) { | |
1352 | ||
1353 | /* For forking services we wait until the start | |
1354 | * process exited. */ | |
1355 | ||
e55224ca | 1356 | s->control_pid = pid; |
80876c20 LP |
1357 | service_set_state(s, SERVICE_START); |
1358 | ||
34e9ba66 | 1359 | } else if (s->type == SERVICE_ONESHOT || |
8c47c732 LP |
1360 | s->type == SERVICE_DBUS || |
1361 | s->type == SERVICE_NOTIFY) { | |
7d55e835 | 1362 | |
34e9ba66 | 1363 | /* For oneshot services we wait until the start |
7d55e835 LP |
1364 | * process exited, too, but it is our main process. */ |
1365 | ||
05e343b7 | 1366 | /* For D-Bus services we know the main pid right away, |
8c47c732 LP |
1367 | * but wait for the bus name to appear on the |
1368 | * bus. Notify services are similar. */ | |
05e343b7 | 1369 | |
5925dd3c | 1370 | service_set_main_pid(s, pid); |
80876c20 | 1371 | service_set_state(s, SERVICE_START); |
034c6ed7 LP |
1372 | } else |
1373 | assert_not_reached("Unknown service type"); | |
1374 | ||
1375 | return; | |
1376 | ||
1377 | fail: | |
66870f90 ZJS |
1378 | log_warning_unit(UNIT(s)->id, |
1379 | "%s failed to run 'start' task: %s", UNIT(s)->id, strerror(-r)); | |
f42806df | 1380 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1381 | } |
1382 | ||
1383 | static void service_enter_start_pre(Service *s) { | |
1384 | int r; | |
1385 | ||
1386 | assert(s); | |
1387 | ||
5e94833f LP |
1388 | service_unwatch_control_pid(s); |
1389 | ||
117dcc57 ZJS |
1390 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; |
1391 | if (s->control_command) { | |
8f53a7b8 LP |
1392 | /* Before we start anything, let's clear up what might |
1393 | * be left from previous runs. */ | |
4ad49000 | 1394 | service_kill_control_processes(s); |
8f53a7b8 | 1395 | |
867b3b7d LP |
1396 | s->control_command_id = SERVICE_EXEC_START_PRE; |
1397 | ||
ecedd90f LP |
1398 | r = service_spawn(s, |
1399 | s->control_command, | |
1400 | true, | |
1401 | false, | |
1402 | !s->permissions_start_only, | |
1403 | !s->root_directory_start_only, | |
1404 | true, | |
1405 | false, | |
1406 | true, | |
1407 | &s->control_pid); | |
1408 | if (r < 0) | |
034c6ed7 LP |
1409 | goto fail; |
1410 | ||
80876c20 LP |
1411 | service_set_state(s, SERVICE_START_PRE); |
1412 | } else | |
034c6ed7 LP |
1413 | service_enter_start(s); |
1414 | ||
1415 | return; | |
1416 | ||
1417 | fail: | |
37520c1b | 1418 | log_warning_unit(UNIT(s)->id, "%s failed to run 'start-pre' task: %s", UNIT(s)->id, strerror(-r)); |
f42806df | 1419 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
1420 | } |
1421 | ||
1422 | static void service_enter_restart(Service *s) { | |
718db961 | 1423 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 | 1424 | int r; |
398ef8ba | 1425 | |
034c6ed7 LP |
1426 | assert(s); |
1427 | ||
a8bb2e65 LP |
1428 | if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) { |
1429 | /* Don't restart things if we are going down anyway */ | |
37520c1b | 1430 | log_info_unit(UNIT(s)->id, "Stop job pending for unit, delaying automatic restart."); |
2edfa366 | 1431 | |
718db961 | 1432 | r = service_arm_timer(s, s->restart_usec); |
a8bb2e65 | 1433 | if (r < 0) |
2edfa366 | 1434 | goto fail; |
feae8adb DW |
1435 | |
1436 | return; | |
2edfa366 LP |
1437 | } |
1438 | ||
48bb5876 DW |
1439 | /* Any units that are bound to this service must also be |
1440 | * restarted. We use JOB_RESTART (instead of the more obvious | |
1441 | * JOB_START) here so that those dependency jobs will be added | |
1442 | * as well. */ | |
1443 | r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL); | |
1444 | if (r < 0) | |
034c6ed7 LP |
1445 | goto fail; |
1446 | ||
a8bb2e65 LP |
1447 | /* Note that we stay in the SERVICE_AUTO_RESTART state here, |
1448 | * it will be canceled as part of the service_stop() call that | |
1449 | * is executed as part of JOB_RESTART. */ | |
1450 | ||
37520c1b | 1451 | log_debug_unit(UNIT(s)->id, "%s scheduled restart job.", UNIT(s)->id); |
034c6ed7 LP |
1452 | return; |
1453 | ||
1454 | fail: | |
66870f90 ZJS |
1455 | log_warning_unit(UNIT(s)->id, |
1456 | "%s failed to schedule restart job: %s", | |
718db961 | 1457 | UNIT(s)->id, bus_error_message(&error, -r)); |
f42806df | 1458 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1459 | } |
1460 | ||
1461 | static void service_enter_reload(Service *s) { | |
1462 | int r; | |
1463 | ||
1464 | assert(s); | |
1465 | ||
5e94833f LP |
1466 | service_unwatch_control_pid(s); |
1467 | ||
117dcc57 ZJS |
1468 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; |
1469 | if (s->control_command) { | |
867b3b7d LP |
1470 | s->control_command_id = SERVICE_EXEC_RELOAD; |
1471 | ||
ecedd90f LP |
1472 | r = service_spawn(s, |
1473 | s->control_command, | |
1474 | true, | |
1475 | false, | |
1476 | !s->permissions_start_only, | |
1477 | !s->root_directory_start_only, | |
1478 | false, | |
1479 | false, | |
1480 | true, | |
1481 | &s->control_pid); | |
1482 | if (r < 0) | |
034c6ed7 LP |
1483 | goto fail; |
1484 | ||
80876c20 LP |
1485 | service_set_state(s, SERVICE_RELOAD); |
1486 | } else | |
f42806df | 1487 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
1488 | |
1489 | return; | |
1490 | ||
1491 | fail: | |
66870f90 ZJS |
1492 | log_warning_unit(UNIT(s)->id, |
1493 | "%s failed to run 'reload' task: %s", | |
1494 | UNIT(s)->id, strerror(-r)); | |
f42806df LP |
1495 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
1496 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
1497 | } |
1498 | ||
f42806df | 1499 | static void service_run_next_control(Service *s) { |
034c6ed7 LP |
1500 | int r; |
1501 | ||
1502 | assert(s); | |
1503 | assert(s->control_command); | |
1504 | assert(s->control_command->command_next); | |
1505 | ||
34e9ba66 | 1506 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 1507 | |
34e9ba66 | 1508 | s->control_command = s->control_command->command_next; |
5e94833f LP |
1509 | service_unwatch_control_pid(s); |
1510 | ||
ecedd90f LP |
1511 | r = service_spawn(s, |
1512 | s->control_command, | |
1513 | true, | |
1514 | false, | |
1515 | !s->permissions_start_only, | |
1516 | !s->root_directory_start_only, | |
1517 | s->control_command_id == SERVICE_EXEC_START_PRE || | |
1518 | s->control_command_id == SERVICE_EXEC_STOP_POST, | |
1519 | false, | |
1520 | true, | |
1521 | &s->control_pid); | |
1522 | if (r < 0) | |
034c6ed7 LP |
1523 | goto fail; |
1524 | ||
1525 | return; | |
1526 | ||
1527 | fail: | |
66870f90 ZJS |
1528 | log_warning_unit(UNIT(s)->id, |
1529 | "%s failed to run next control task: %s", | |
1530 | UNIT(s)->id, strerror(-r)); | |
034c6ed7 | 1531 | |
80876c20 | 1532 | if (s->state == SERVICE_START_PRE) |
f42806df | 1533 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
80876c20 | 1534 | else if (s->state == SERVICE_STOP) |
f42806df | 1535 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 1536 | else if (s->state == SERVICE_STOP_POST) |
f42806df | 1537 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
e2f3b44c | 1538 | else if (s->state == SERVICE_RELOAD) { |
f42806df LP |
1539 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
1540 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c | 1541 | } else |
f42806df | 1542 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
5cb5a6ff LP |
1543 | } |
1544 | ||
f42806df | 1545 | static void service_run_next_main(Service *s) { |
34e9ba66 LP |
1546 | pid_t pid; |
1547 | int r; | |
1548 | ||
1549 | assert(s); | |
867b3b7d LP |
1550 | assert(s->main_command); |
1551 | assert(s->main_command->command_next); | |
1552 | assert(s->type == SERVICE_ONESHOT); | |
34e9ba66 | 1553 | |
867b3b7d | 1554 | s->main_command = s->main_command->command_next; |
34e9ba66 LP |
1555 | service_unwatch_main_pid(s); |
1556 | ||
ecedd90f LP |
1557 | r = service_spawn(s, |
1558 | s->main_command, | |
98709151 | 1559 | true, |
ecedd90f LP |
1560 | true, |
1561 | true, | |
1562 | true, | |
1563 | true, | |
1564 | s->notify_access != NOTIFY_NONE, | |
1565 | false, | |
1566 | &pid); | |
1567 | if (r < 0) | |
34e9ba66 LP |
1568 | goto fail; |
1569 | ||
1570 | service_set_main_pid(s, pid); | |
1571 | ||
1572 | return; | |
1573 | ||
1574 | fail: | |
66870f90 ZJS |
1575 | log_warning_unit(UNIT(s)->id, |
1576 | "%s failed to run next main task: %s", UNIT(s)->id, strerror(-r)); | |
f42806df | 1577 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
34e9ba66 LP |
1578 | } |
1579 | ||
bf500566 | 1580 | static int service_execute_action(Service *s, FailureAction action, const char *reason, bool log_action_none) { |
4b939747 MO |
1581 | assert(s); |
1582 | ||
bf500566 MO |
1583 | if (action == SERVICE_FAILURE_ACTION_REBOOT || |
1584 | action == SERVICE_FAILURE_ACTION_REBOOT_FORCE) | |
efe6e7d3 MO |
1585 | update_reboot_param_file(s->reboot_arg); |
1586 | ||
93ae25e6 | 1587 | switch (action) { |
4b939747 | 1588 | |
bf500566 | 1589 | case SERVICE_FAILURE_ACTION_NONE: |
93ae25e6 MO |
1590 | if (log_action_none) |
1591 | log_warning_unit(UNIT(s)->id, | |
1592 | "%s %s, refusing to start.", UNIT(s)->id, reason); | |
4b939747 MO |
1593 | break; |
1594 | ||
bf500566 | 1595 | case SERVICE_FAILURE_ACTION_REBOOT: { |
718db961 | 1596 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
4b939747 MO |
1597 | int r; |
1598 | ||
66870f90 | 1599 | log_warning_unit(UNIT(s)->id, |
93ae25e6 | 1600 | "%s %s, rebooting.", UNIT(s)->id, reason); |
4b939747 | 1601 | |
117dcc57 ZJS |
1602 | r = manager_add_job_by_name(UNIT(s)->manager, JOB_START, |
1603 | SPECIAL_REBOOT_TARGET, JOB_REPLACE, | |
1604 | true, &error, NULL); | |
718db961 | 1605 | if (r < 0) |
66870f90 | 1606 | log_error_unit(UNIT(s)->id, |
718db961 | 1607 | "Failed to reboot: %s.", bus_error_message(&error, r)); |
4b939747 MO |
1608 | |
1609 | break; | |
1610 | } | |
1611 | ||
bf500566 | 1612 | case SERVICE_FAILURE_ACTION_REBOOT_FORCE: |
66870f90 | 1613 | log_warning_unit(UNIT(s)->id, |
93ae25e6 | 1614 | "%s %s, forcibly rebooting.", UNIT(s)->id, reason); |
4b939747 MO |
1615 | UNIT(s)->manager->exit_code = MANAGER_REBOOT; |
1616 | break; | |
1617 | ||
bf500566 | 1618 | case SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE: |
66870f90 | 1619 | log_warning_unit(UNIT(s)->id, |
93ae25e6 | 1620 | "%s %s, rebooting immediately.", UNIT(s)->id, reason); |
0049f05a | 1621 | sync(); |
efe6e7d3 MO |
1622 | if (s->reboot_arg) { |
1623 | log_info("Rebooting with argument '%s'.", s->reboot_arg); | |
1624 | syscall(SYS_reboot, LINUX_REBOOT_MAGIC1, LINUX_REBOOT_MAGIC2, | |
1625 | LINUX_REBOOT_CMD_RESTART2, s->reboot_arg); | |
1626 | } | |
1627 | ||
1628 | log_info("Rebooting."); | |
4b939747 MO |
1629 | reboot(RB_AUTOBOOT); |
1630 | break; | |
1631 | ||
1632 | default: | |
66870f90 | 1633 | log_error_unit(UNIT(s)->id, |
bf500566 MO |
1634 | "failure action=%i", action); |
1635 | assert_not_reached("Unknown FailureAction."); | |
4b939747 MO |
1636 | } |
1637 | ||
1638 | return -ECANCELED; | |
1639 | } | |
1640 | ||
93ae25e6 MO |
1641 | static int service_start_limit_test(Service *s) { |
1642 | assert(s); | |
1643 | ||
1644 | if (ratelimit_test(&s->start_limit)) | |
1645 | return 0; | |
1646 | ||
1647 | return service_execute_action(s, s->start_limit_action, "start request repeated too quickly", true); | |
1648 | } | |
1649 | ||
87f0e418 LP |
1650 | static int service_start(Unit *u) { |
1651 | Service *s = SERVICE(u); | |
4b939747 | 1652 | int r; |
5cb5a6ff LP |
1653 | |
1654 | assert(s); | |
1655 | ||
034c6ed7 LP |
1656 | /* We cannot fulfill this request right now, try again later |
1657 | * please! */ | |
1658 | if (s->state == SERVICE_STOP || | |
1659 | s->state == SERVICE_STOP_SIGTERM || | |
1660 | s->state == SERVICE_STOP_SIGKILL || | |
1661 | s->state == SERVICE_STOP_POST || | |
1662 | s->state == SERVICE_FINAL_SIGTERM || | |
1663 | s->state == SERVICE_FINAL_SIGKILL) | |
5cb5a6ff LP |
1664 | return -EAGAIN; |
1665 | ||
034c6ed7 LP |
1666 | /* Already on it! */ |
1667 | if (s->state == SERVICE_START_PRE || | |
1668 | s->state == SERVICE_START || | |
1669 | s->state == SERVICE_START_POST) | |
1670 | return 0; | |
1671 | ||
2e9d6c12 | 1672 | /* A service that will be restarted must be stopped first to |
7f2cddae | 1673 | * trigger BindsTo and/or OnFailure dependencies. If a user |
2e9d6c12 | 1674 | * does not want to wait for the holdoff time to elapse, the |
d4943dc7 LP |
1675 | * service should be manually restarted, not started. We |
1676 | * simply return EAGAIN here, so that any start jobs stay | |
1677 | * queued, and assume that the auto restart timer will | |
1678 | * eventually trigger the restart. */ | |
1679 | if (s->state == SERVICE_AUTO_RESTART) | |
a8bb2e65 | 1680 | return -EAGAIN; |
2e9d6c12 DW |
1681 | |
1682 | assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED); | |
5cb5a6ff | 1683 | |
1e2e8133 | 1684 | /* Make sure we don't enter a busy loop of some kind. */ |
4b939747 | 1685 | r = service_start_limit_test(s); |
c2f34808 | 1686 | if (r < 0) { |
8d1b002a | 1687 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false); |
4b939747 | 1688 | return r; |
c2f34808 | 1689 | } |
1e2e8133 | 1690 | |
f42806df LP |
1691 | s->result = SERVICE_SUCCESS; |
1692 | s->reload_result = SERVICE_SUCCESS; | |
034c6ed7 | 1693 | s->main_pid_known = false; |
6dfa5494 | 1694 | s->main_pid_alien = false; |
47342320 | 1695 | s->forbid_restart = false; |
034c6ed7 | 1696 | |
8cfdb077 LP |
1697 | free(s->status_text); |
1698 | s->status_text = NULL; | |
1699 | s->status_errno = 0; | |
1700 | ||
034c6ed7 LP |
1701 | service_enter_start_pre(s); |
1702 | return 0; | |
5cb5a6ff LP |
1703 | } |
1704 | ||
87f0e418 LP |
1705 | static int service_stop(Unit *u) { |
1706 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
1707 | |
1708 | assert(s); | |
1709 | ||
f0c7b229 | 1710 | /* Don't create restart jobs from here. */ |
47342320 | 1711 | s->forbid_restart = true; |
034c6ed7 | 1712 | |
e537352b LP |
1713 | /* Already on it */ |
1714 | if (s->state == SERVICE_STOP || | |
1715 | s->state == SERVICE_STOP_SIGTERM || | |
1716 | s->state == SERVICE_STOP_SIGKILL || | |
1717 | s->state == SERVICE_STOP_POST || | |
1718 | s->state == SERVICE_FINAL_SIGTERM || | |
1719 | s->state == SERVICE_FINAL_SIGKILL) | |
1720 | return 0; | |
1721 | ||
f0c7b229 | 1722 | /* A restart will be scheduled or is in progress. */ |
034c6ed7 | 1723 | if (s->state == SERVICE_AUTO_RESTART) { |
0c7f15b3 | 1724 | service_set_state(s, SERVICE_DEAD); |
034c6ed7 LP |
1725 | return 0; |
1726 | } | |
1727 | ||
3f6c78dc LP |
1728 | /* If there's already something running we go directly into |
1729 | * kill mode. */ | |
1730 | if (s->state == SERVICE_START_PRE || | |
1731 | s->state == SERVICE_START || | |
1732 | s->state == SERVICE_START_POST || | |
1733 | s->state == SERVICE_RELOAD) { | |
f42806df | 1734 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
3f6c78dc LP |
1735 | return 0; |
1736 | } | |
5cb5a6ff | 1737 | |
3f6c78dc LP |
1738 | assert(s->state == SERVICE_RUNNING || |
1739 | s->state == SERVICE_EXITED); | |
3a762661 | 1740 | |
f42806df | 1741 | service_enter_stop(s, SERVICE_SUCCESS); |
5cb5a6ff LP |
1742 | return 0; |
1743 | } | |
1744 | ||
87f0e418 LP |
1745 | static int service_reload(Unit *u) { |
1746 | Service *s = SERVICE(u); | |
034c6ed7 LP |
1747 | |
1748 | assert(s); | |
1749 | ||
80876c20 | 1750 | assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED); |
034c6ed7 LP |
1751 | |
1752 | service_enter_reload(s); | |
5cb5a6ff LP |
1753 | return 0; |
1754 | } | |
1755 | ||
44a6b1b6 | 1756 | _pure_ static bool service_can_reload(Unit *u) { |
87f0e418 | 1757 | Service *s = SERVICE(u); |
034c6ed7 LP |
1758 | |
1759 | assert(s); | |
1760 | ||
1761 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
1762 | } | |
1763 | ||
a16e1123 LP |
1764 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
1765 | Service *s = SERVICE(u); | |
1766 | ||
1767 | assert(u); | |
1768 | assert(f); | |
1769 | assert(fds); | |
1770 | ||
1771 | unit_serialize_item(u, f, "state", service_state_to_string(s->state)); | |
f42806df LP |
1772 | unit_serialize_item(u, f, "result", service_result_to_string(s->result)); |
1773 | unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result)); | |
a16e1123 LP |
1774 | |
1775 | if (s->control_pid > 0) | |
ccd06097 ZJS |
1776 | unit_serialize_item_format(u, f, "control-pid", PID_FMT, |
1777 | s->control_pid); | |
a16e1123 | 1778 | |
5925dd3c | 1779 | if (s->main_pid_known && s->main_pid > 0) |
ccd06097 | 1780 | unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid); |
a16e1123 LP |
1781 | |
1782 | unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known)); | |
1783 | ||
3a2776bc LP |
1784 | if (s->status_text) |
1785 | unit_serialize_item(u, f, "status-text", s->status_text); | |
1786 | ||
cfc4eb4c LP |
1787 | /* FIXME: There's a minor uncleanliness here: if there are |
1788 | * multiple commands attached here, we will start from the | |
1789 | * first one again */ | |
a16e1123 | 1790 | if (s->control_command_id >= 0) |
117dcc57 ZJS |
1791 | unit_serialize_item(u, f, "control-command", |
1792 | service_exec_command_to_string(s->control_command_id)); | |
a16e1123 LP |
1793 | |
1794 | if (s->socket_fd >= 0) { | |
1795 | int copy; | |
1796 | ||
1797 | if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0) | |
1798 | return copy; | |
1799 | ||
1800 | unit_serialize_item_format(u, f, "socket-fd", "%i", copy); | |
1801 | } | |
1802 | ||
ecdbca40 | 1803 | if (s->main_exec_status.pid > 0) { |
ccd06097 ZJS |
1804 | unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT, |
1805 | s->main_exec_status.pid); | |
117dcc57 ZJS |
1806 | dual_timestamp_serialize(f, "main-exec-status-start", |
1807 | &s->main_exec_status.start_timestamp); | |
1808 | dual_timestamp_serialize(f, "main-exec-status-exit", | |
1809 | &s->main_exec_status.exit_timestamp); | |
ecdbca40 | 1810 | |
799fd0fd | 1811 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { |
117dcc57 ZJS |
1812 | unit_serialize_item_format(u, f, "main-exec-status-code", "%i", |
1813 | s->main_exec_status.code); | |
1814 | unit_serialize_item_format(u, f, "main-exec-status-status", "%i", | |
1815 | s->main_exec_status.status); | |
ecdbca40 LP |
1816 | } |
1817 | } | |
a6927d7f | 1818 | if (dual_timestamp_is_set(&s->watchdog_timestamp)) |
842129f5 | 1819 | dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp); |
ecdbca40 | 1820 | |
6aca9a58 | 1821 | if (s->forbid_restart) |
8d1a2802 | 1822 | unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart)); |
6aca9a58 | 1823 | |
a16e1123 LP |
1824 | return 0; |
1825 | } | |
1826 | ||
1827 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
1828 | Service *s = SERVICE(u); | |
a16e1123 LP |
1829 | |
1830 | assert(u); | |
1831 | assert(key); | |
1832 | assert(value); | |
1833 | assert(fds); | |
1834 | ||
1835 | if (streq(key, "state")) { | |
1836 | ServiceState state; | |
1837 | ||
117dcc57 ZJS |
1838 | state = service_state_from_string(value); |
1839 | if (state < 0) | |
66870f90 | 1840 | log_debug_unit(u->id, "Failed to parse state value %s", value); |
a16e1123 LP |
1841 | else |
1842 | s->deserialized_state = state; | |
f42806df LP |
1843 | } else if (streq(key, "result")) { |
1844 | ServiceResult f; | |
1845 | ||
1846 | f = service_result_from_string(value); | |
1847 | if (f < 0) | |
66870f90 | 1848 | log_debug_unit(u->id, "Failed to parse result value %s", value); |
f42806df LP |
1849 | else if (f != SERVICE_SUCCESS) |
1850 | s->result = f; | |
1851 | ||
1852 | } else if (streq(key, "reload-result")) { | |
1853 | ServiceResult f; | |
1854 | ||
1855 | f = service_result_from_string(value); | |
1856 | if (f < 0) | |
66870f90 | 1857 | log_debug_unit(u->id, "Failed to parse reload result value %s", value); |
f42806df LP |
1858 | else if (f != SERVICE_SUCCESS) |
1859 | s->reload_result = f; | |
a16e1123 | 1860 | |
a16e1123 | 1861 | } else if (streq(key, "control-pid")) { |
5925dd3c | 1862 | pid_t pid; |
a16e1123 | 1863 | |
e364ad06 | 1864 | if (parse_pid(value, &pid) < 0) |
66870f90 | 1865 | log_debug_unit(u->id, "Failed to parse control-pid value %s", value); |
a16e1123 | 1866 | else |
e55224ca | 1867 | s->control_pid = pid; |
a16e1123 | 1868 | } else if (streq(key, "main-pid")) { |
5925dd3c | 1869 | pid_t pid; |
a16e1123 | 1870 | |
e364ad06 | 1871 | if (parse_pid(value, &pid) < 0) |
66870f90 | 1872 | log_debug_unit(u->id, "Failed to parse main-pid value %s", value); |
7400b9d2 LP |
1873 | else { |
1874 | service_set_main_pid(s, pid); | |
1875 | unit_watch_pid(UNIT(s), pid); | |
1876 | } | |
a16e1123 LP |
1877 | } else if (streq(key, "main-pid-known")) { |
1878 | int b; | |
1879 | ||
117dcc57 ZJS |
1880 | b = parse_boolean(value); |
1881 | if (b < 0) | |
66870f90 | 1882 | log_debug_unit(u->id, "Failed to parse main-pid-known value %s", value); |
a16e1123 LP |
1883 | else |
1884 | s->main_pid_known = b; | |
3a2776bc LP |
1885 | } else if (streq(key, "status-text")) { |
1886 | char *t; | |
1887 | ||
117dcc57 ZJS |
1888 | t = strdup(value); |
1889 | if (!t) | |
1890 | log_oom(); | |
1891 | else { | |
3a2776bc LP |
1892 | free(s->status_text); |
1893 | s->status_text = t; | |
1894 | } | |
1895 | ||
a16e1123 LP |
1896 | } else if (streq(key, "control-command")) { |
1897 | ServiceExecCommand id; | |
1898 | ||
117dcc57 ZJS |
1899 | id = service_exec_command_from_string(value); |
1900 | if (id < 0) | |
66870f90 | 1901 | log_debug_unit(u->id, "Failed to parse exec-command value %s", value); |
a16e1123 LP |
1902 | else { |
1903 | s->control_command_id = id; | |
1904 | s->control_command = s->exec_command[id]; | |
1905 | } | |
1906 | } else if (streq(key, "socket-fd")) { | |
1907 | int fd; | |
1908 | ||
1909 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
66870f90 | 1910 | log_debug_unit(u->id, "Failed to parse socket-fd value %s", value); |
a16e1123 LP |
1911 | else { |
1912 | ||
574634bc | 1913 | asynchronous_close(s->socket_fd); |
a16e1123 LP |
1914 | s->socket_fd = fdset_remove(fds, fd); |
1915 | } | |
ecdbca40 LP |
1916 | } else if (streq(key, "main-exec-status-pid")) { |
1917 | pid_t pid; | |
1918 | ||
e364ad06 | 1919 | if (parse_pid(value, &pid) < 0) |
66870f90 | 1920 | log_debug_unit(u->id, "Failed to parse main-exec-status-pid value %s", value); |
ecdbca40 LP |
1921 | else |
1922 | s->main_exec_status.pid = pid; | |
1923 | } else if (streq(key, "main-exec-status-code")) { | |
1924 | int i; | |
1925 | ||
e364ad06 | 1926 | if (safe_atoi(value, &i) < 0) |
66870f90 | 1927 | log_debug_unit(u->id, "Failed to parse main-exec-status-code value %s", value); |
ecdbca40 LP |
1928 | else |
1929 | s->main_exec_status.code = i; | |
1930 | } else if (streq(key, "main-exec-status-status")) { | |
1931 | int i; | |
1932 | ||
e364ad06 | 1933 | if (safe_atoi(value, &i) < 0) |
66870f90 | 1934 | log_debug_unit(u->id, "Failed to parse main-exec-status-status value %s", value); |
ecdbca40 LP |
1935 | else |
1936 | s->main_exec_status.status = i; | |
799fd0fd LP |
1937 | } else if (streq(key, "main-exec-status-start")) |
1938 | dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp); | |
1939 | else if (streq(key, "main-exec-status-exit")) | |
1940 | dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp); | |
a6927d7f MO |
1941 | else if (streq(key, "watchdog-timestamp")) |
1942 | dual_timestamp_deserialize(value, &s->watchdog_timestamp); | |
613b411c | 1943 | else if (streq(key, "forbid-restart")) { |
6aca9a58 SE |
1944 | int b; |
1945 | ||
1946 | b = parse_boolean(value); | |
1947 | if (b < 0) | |
8d1a2802 | 1948 | log_debug_unit(u->id, "Failed to parse forbid-restart value %s", value); |
6aca9a58 SE |
1949 | else |
1950 | s->forbid_restart = b; | |
c17ec25e | 1951 | } else |
66870f90 | 1952 | log_debug_unit(u->id, "Unknown serialization key '%s'", key); |
a16e1123 LP |
1953 | |
1954 | return 0; | |
1955 | } | |
1956 | ||
44a6b1b6 | 1957 | _pure_ static UnitActiveState service_active_state(Unit *u) { |
e056b01d LP |
1958 | const UnitActiveState *table; |
1959 | ||
87f0e418 | 1960 | assert(u); |
5cb5a6ff | 1961 | |
e056b01d LP |
1962 | table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
1963 | ||
1964 | return table[SERVICE(u)->state]; | |
034c6ed7 LP |
1965 | } |
1966 | ||
10a94420 LP |
1967 | static const char *service_sub_state_to_string(Unit *u) { |
1968 | assert(u); | |
1969 | ||
1970 | return service_state_to_string(SERVICE(u)->state); | |
1971 | } | |
1972 | ||
701cc384 LP |
1973 | static bool service_check_gc(Unit *u) { |
1974 | Service *s = SERVICE(u); | |
1975 | ||
1976 | assert(s); | |
1977 | ||
6d55002a LP |
1978 | /* Never clean up services that still have a process around, |
1979 | * even if the service is formally dead. */ | |
1980 | if (cgroup_good(s) > 0 || | |
1981 | main_pid_good(s) > 0 || | |
1982 | control_pid_good(s) > 0) | |
1983 | return true; | |
1984 | ||
6d55002a LP |
1985 | return false; |
1986 | } | |
1987 | ||
44a6b1b6 | 1988 | _pure_ static bool service_check_snapshot(Unit *u) { |
701cc384 LP |
1989 | Service *s = SERVICE(u); |
1990 | ||
1991 | assert(s); | |
1992 | ||
99f37ad8 | 1993 | return (s->socket_fd < 0); |
701cc384 LP |
1994 | } |
1995 | ||
3a111838 MS |
1996 | static int service_retry_pid_file(Service *s) { |
1997 | int r; | |
1998 | ||
1999 | assert(s->pid_file); | |
2000 | assert(s->state == SERVICE_START || s->state == SERVICE_START_POST); | |
2001 | ||
2002 | r = service_load_pid_file(s, false); | |
2003 | if (r < 0) | |
2004 | return r; | |
2005 | ||
2006 | service_unwatch_pid_file(s); | |
2007 | ||
f42806df | 2008 | service_enter_running(s, SERVICE_SUCCESS); |
3a111838 MS |
2009 | return 0; |
2010 | } | |
2011 | ||
2012 | static int service_watch_pid_file(Service *s) { | |
2013 | int r; | |
2014 | ||
66870f90 ZJS |
2015 | log_debug_unit(UNIT(s)->id, |
2016 | "Setting watch for %s's PID file %s", | |
2017 | UNIT(s)->id, s->pid_file_pathspec->path); | |
718db961 | 2018 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io); |
3a111838 MS |
2019 | if (r < 0) |
2020 | goto fail; | |
2021 | ||
2022 | /* the pidfile might have appeared just before we set the watch */ | |
bc41f93e ZJS |
2023 | log_debug_unit(UNIT(s)->id, |
2024 | "Trying to read %s's PID file %s in case it changed", | |
2025 | UNIT(s)->id, s->pid_file_pathspec->path); | |
3a111838 MS |
2026 | service_retry_pid_file(s); |
2027 | ||
2028 | return 0; | |
2029 | fail: | |
66870f90 ZJS |
2030 | log_error_unit(UNIT(s)->id, |
2031 | "Failed to set a watch for %s's PID file %s: %s", | |
2032 | UNIT(s)->id, s->pid_file_pathspec->path, strerror(-r)); | |
3a111838 MS |
2033 | service_unwatch_pid_file(s); |
2034 | return r; | |
2035 | } | |
2036 | ||
2037 | static int service_demand_pid_file(Service *s) { | |
2038 | PathSpec *ps; | |
2039 | ||
2040 | assert(s->pid_file); | |
2041 | assert(!s->pid_file_pathspec); | |
2042 | ||
2043 | ps = new0(PathSpec, 1); | |
2044 | if (!ps) | |
2045 | return -ENOMEM; | |
2046 | ||
718db961 | 2047 | ps->unit = UNIT(s); |
3a111838 MS |
2048 | ps->path = strdup(s->pid_file); |
2049 | if (!ps->path) { | |
2050 | free(ps); | |
2051 | return -ENOMEM; | |
2052 | } | |
2053 | ||
2054 | path_kill_slashes(ps->path); | |
2055 | ||
2056 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that | |
2057 | * keep their PID file open all the time. */ | |
2058 | ps->type = PATH_MODIFIED; | |
2059 | ps->inotify_fd = -1; | |
2060 | ||
2061 | s->pid_file_pathspec = ps; | |
2062 | ||
2063 | return service_watch_pid_file(s); | |
2064 | } | |
2065 | ||
718db961 | 2066 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
e14c2802 LP |
2067 | PathSpec *p = userdata; |
2068 | Service *s; | |
2069 | ||
2070 | assert(p); | |
2071 | ||
2072 | s = SERVICE(p->unit); | |
3a111838 MS |
2073 | |
2074 | assert(s); | |
2075 | assert(fd >= 0); | |
2076 | assert(s->state == SERVICE_START || s->state == SERVICE_START_POST); | |
2077 | assert(s->pid_file_pathspec); | |
57020a3a | 2078 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); |
3a111838 | 2079 | |
718db961 | 2080 | log_debug_unit(UNIT(s)->id, "inotify event for %s", UNIT(s)->id); |
3a111838 | 2081 | |
e14c2802 | 2082 | if (path_spec_fd_event(p, events) < 0) |
3a111838 MS |
2083 | goto fail; |
2084 | ||
2085 | if (service_retry_pid_file(s) == 0) | |
718db961 | 2086 | return 0; |
3a111838 MS |
2087 | |
2088 | if (service_watch_pid_file(s) < 0) | |
2089 | goto fail; | |
2090 | ||
718db961 LP |
2091 | return 0; |
2092 | ||
3a111838 MS |
2093 | fail: |
2094 | service_unwatch_pid_file(s); | |
f42806df | 2095 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
718db961 | 2096 | return 0; |
3a111838 MS |
2097 | } |
2098 | ||
a911bb9a LP |
2099 | static void service_notify_cgroup_empty_event(Unit *u) { |
2100 | Service *s = SERVICE(u); | |
2101 | ||
2102 | assert(u); | |
2103 | ||
2104 | log_debug_unit(u->id, "%s: cgroup is empty", u->id); | |
2105 | ||
2106 | switch (s->state) { | |
2107 | ||
2108 | /* Waiting for SIGCHLD is usually more interesting, | |
2109 | * because it includes return codes/signals. Which is | |
2110 | * why we ignore the cgroup events for most cases, | |
2111 | * except when we don't know pid which to expect the | |
2112 | * SIGCHLD for. */ | |
2113 | ||
2114 | case SERVICE_START: | |
2115 | case SERVICE_START_POST: | |
2116 | /* If we were hoping for the daemon to write its PID file, | |
2117 | * we can give up now. */ | |
2118 | if (s->pid_file_pathspec) { | |
2119 | log_warning_unit(u->id, | |
2120 | "%s never wrote its PID file. Failing.", UNIT(s)->id); | |
2121 | service_unwatch_pid_file(s); | |
2122 | if (s->state == SERVICE_START) | |
2123 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2124 | else | |
2125 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2126 | } | |
2127 | break; | |
2128 | ||
2129 | case SERVICE_RUNNING: | |
2130 | /* service_enter_running() will figure out what to do */ | |
2131 | service_enter_running(s, SERVICE_SUCCESS); | |
2132 | break; | |
2133 | ||
2134 | case SERVICE_STOP_SIGTERM: | |
2135 | case SERVICE_STOP_SIGKILL: | |
2136 | ||
2137 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2138 | service_enter_stop_post(s, SERVICE_SUCCESS); | |
2139 | ||
2140 | break; | |
2141 | ||
2142 | case SERVICE_STOP_POST: | |
2143 | case SERVICE_FINAL_SIGTERM: | |
2144 | case SERVICE_FINAL_SIGKILL: | |
2145 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2146 | service_enter_dead(s, SERVICE_SUCCESS, true); | |
2147 | ||
2148 | break; | |
2149 | ||
2150 | default: | |
2151 | ; | |
2152 | } | |
2153 | } | |
2154 | ||
87f0e418 LP |
2155 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
2156 | Service *s = SERVICE(u); | |
f42806df | 2157 | ServiceResult f; |
5cb5a6ff LP |
2158 | |
2159 | assert(s); | |
034c6ed7 LP |
2160 | assert(pid >= 0); |
2161 | ||
96342de6 LN |
2162 | if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) : |
2163 | is_clean_exit_lsb(code, status, &s->success_status)) | |
f42806df LP |
2164 | f = SERVICE_SUCCESS; |
2165 | else if (code == CLD_EXITED) | |
2166 | f = SERVICE_FAILURE_EXIT_CODE; | |
2167 | else if (code == CLD_KILLED) | |
2168 | f = SERVICE_FAILURE_SIGNAL; | |
2169 | else if (code == CLD_DUMPED) | |
2170 | f = SERVICE_FAILURE_CORE_DUMP; | |
d06dacd0 | 2171 | else |
cfc4eb4c | 2172 | assert_not_reached("Unknown code"); |
034c6ed7 LP |
2173 | |
2174 | if (s->main_pid == pid) { | |
db01f8b3 MS |
2175 | /* Forking services may occasionally move to a new PID. |
2176 | * As long as they update the PID file before exiting the old | |
2177 | * PID, they're fine. */ | |
5375410b | 2178 | if (service_load_pid_file(s, false) == 0) |
db01f8b3 | 2179 | return; |
034c6ed7 | 2180 | |
034c6ed7 | 2181 | s->main_pid = 0; |
6ea832a2 | 2182 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); |
034c6ed7 | 2183 | |
867b3b7d | 2184 | if (s->main_command) { |
fbeefb45 LP |
2185 | /* If this is not a forking service than the |
2186 | * main process got started and hence we copy | |
2187 | * the exit status so that it is recorded both | |
2188 | * as main and as control process exit | |
2189 | * status */ | |
2190 | ||
867b3b7d | 2191 | s->main_command->exec_status = s->main_exec_status; |
b708e7ce | 2192 | |
867b3b7d | 2193 | if (s->main_command->ignore) |
f42806df | 2194 | f = SERVICE_SUCCESS; |
fbeefb45 LP |
2195 | } else if (s->exec_command[SERVICE_EXEC_START]) { |
2196 | ||
2197 | /* If this is a forked process, then we should | |
2198 | * ignore the return value if this was | |
2199 | * configured for the starter process */ | |
2200 | ||
2201 | if (s->exec_command[SERVICE_EXEC_START]->ignore) | |
2202 | f = SERVICE_SUCCESS; | |
034c6ed7 LP |
2203 | } |
2204 | ||
bbc9006e MT |
2205 | log_struct_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, |
2206 | u->id, | |
23635a85 ZJS |
2207 | "MESSAGE=%s: main process exited, code=%s, status=%i/%s", |
2208 | u->id, sigchld_code_to_string(code), status, | |
2209 | strna(code == CLD_EXITED | |
2210 | ? exit_status_to_string(status, EXIT_STATUS_FULL) | |
2211 | : signal_to_string(status)), | |
23635a85 ZJS |
2212 | "EXIT_CODE=%s", sigchld_code_to_string(code), |
2213 | "EXIT_STATUS=%i", status, | |
2214 | NULL); | |
f42806df LP |
2215 | |
2216 | if (f != SERVICE_SUCCESS) | |
2217 | s->result = f; | |
034c6ed7 | 2218 | |
867b3b7d LP |
2219 | if (s->main_command && |
2220 | s->main_command->command_next && | |
f42806df | 2221 | f == SERVICE_SUCCESS) { |
034c6ed7 | 2222 | |
34e9ba66 LP |
2223 | /* There is another command to * |
2224 | * execute, so let's do that. */ | |
034c6ed7 | 2225 | |
66870f90 ZJS |
2226 | log_debug_unit(u->id, |
2227 | "%s running next main command for state %s", | |
2228 | u->id, service_state_to_string(s->state)); | |
f42806df | 2229 | service_run_next_main(s); |
034c6ed7 | 2230 | |
34e9ba66 LP |
2231 | } else { |
2232 | ||
2233 | /* The service exited, so the service is officially | |
2234 | * gone. */ | |
867b3b7d | 2235 | s->main_command = NULL; |
34e9ba66 LP |
2236 | |
2237 | switch (s->state) { | |
2238 | ||
2239 | case SERVICE_START_POST: | |
2240 | case SERVICE_RELOAD: | |
2241 | case SERVICE_STOP: | |
2242 | /* Need to wait until the operation is | |
2243 | * done */ | |
c4653a4d | 2244 | break; |
7d55e835 | 2245 | |
34e9ba66 LP |
2246 | case SERVICE_START: |
2247 | if (s->type == SERVICE_ONESHOT) { | |
2248 | /* This was our main goal, so let's go on */ | |
f42806df | 2249 | if (f == SERVICE_SUCCESS) |
34e9ba66 LP |
2250 | service_enter_start_post(s); |
2251 | else | |
f42806df | 2252 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
34e9ba66 | 2253 | break; |
34e9ba66 | 2254 | } |
034c6ed7 | 2255 | |
bfba3256 LP |
2256 | /* Fall through */ |
2257 | ||
34e9ba66 | 2258 | case SERVICE_RUNNING: |
f42806df | 2259 | service_enter_running(s, f); |
34e9ba66 | 2260 | break; |
034c6ed7 | 2261 | |
34e9ba66 LP |
2262 | case SERVICE_STOP_SIGTERM: |
2263 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 2264 | |
34e9ba66 | 2265 | if (!control_pid_good(s)) |
f42806df | 2266 | service_enter_stop_post(s, f); |
5cb5a6ff | 2267 | |
34e9ba66 LP |
2268 | /* If there is still a control process, wait for that first */ |
2269 | break; | |
2270 | ||
bf108e55 LP |
2271 | case SERVICE_STOP_POST: |
2272 | case SERVICE_FINAL_SIGTERM: | |
2273 | case SERVICE_FINAL_SIGKILL: | |
2274 | ||
2275 | if (!control_pid_good(s)) | |
2276 | service_enter_dead(s, f, true); | |
2277 | break; | |
2278 | ||
34e9ba66 LP |
2279 | default: |
2280 | assert_not_reached("Uh, main process died at wrong time."); | |
2281 | } | |
034c6ed7 | 2282 | } |
5cb5a6ff | 2283 | |
034c6ed7 | 2284 | } else if (s->control_pid == pid) { |
34e9ba66 LP |
2285 | s->control_pid = 0; |
2286 | ||
b708e7ce | 2287 | if (s->control_command) { |
117dcc57 ZJS |
2288 | exec_status_exit(&s->control_command->exec_status, |
2289 | &s->exec_context, pid, code, status); | |
a16e1123 | 2290 | |
b708e7ce | 2291 | if (s->control_command->ignore) |
f42806df | 2292 | f = SERVICE_SUCCESS; |
b708e7ce LP |
2293 | } |
2294 | ||
66870f90 ZJS |
2295 | log_full_unit(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, u->id, |
2296 | "%s: control process exited, code=%s status=%i", | |
2297 | u->id, sigchld_code_to_string(code), status); | |
f42806df LP |
2298 | |
2299 | if (f != SERVICE_SUCCESS) | |
2300 | s->result = f; | |
034c6ed7 | 2301 | |
88f3e0c9 LP |
2302 | /* Immediately get rid of the cgroup, so that the |
2303 | * kernel doesn't delay the cgroup empty messages for | |
2304 | * the service cgroup any longer than necessary */ | |
4ad49000 | 2305 | service_kill_control_processes(s); |
88f3e0c9 | 2306 | |
34e9ba66 LP |
2307 | if (s->control_command && |
2308 | s->control_command->command_next && | |
f42806df | 2309 | f == SERVICE_SUCCESS) { |
034c6ed7 LP |
2310 | |
2311 | /* There is another command to * | |
2312 | * execute, so let's do that. */ | |
2313 | ||
66870f90 ZJS |
2314 | log_debug_unit(u->id, |
2315 | "%s running next control command for state %s", | |
2316 | u->id, service_state_to_string(s->state)); | |
f42806df | 2317 | service_run_next_control(s); |
034c6ed7 | 2318 | |
80876c20 | 2319 | } else { |
034c6ed7 LP |
2320 | /* No further commands for this step, so let's |
2321 | * figure out what to do next */ | |
2322 | ||
a16e1123 LP |
2323 | s->control_command = NULL; |
2324 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2325 | ||
66870f90 ZJS |
2326 | log_debug_unit(u->id, |
2327 | "%s got final SIGCHLD for state %s", | |
2328 | u->id, service_state_to_string(s->state)); | |
bd982a8b | 2329 | |
034c6ed7 LP |
2330 | switch (s->state) { |
2331 | ||
2332 | case SERVICE_START_PRE: | |
f42806df | 2333 | if (f == SERVICE_SUCCESS) |
034c6ed7 LP |
2334 | service_enter_start(s); |
2335 | else | |
f42806df | 2336 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
034c6ed7 LP |
2337 | break; |
2338 | ||
2339 | case SERVICE_START: | |
bfba3256 LP |
2340 | if (s->type != SERVICE_FORKING) |
2341 | /* Maybe spurious event due to a reload that changed the type? */ | |
2342 | break; | |
034c6ed7 | 2343 | |
f42806df LP |
2344 | if (f != SERVICE_SUCCESS) { |
2345 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
3a111838 MS |
2346 | break; |
2347 | } | |
034c6ed7 | 2348 | |
3a111838 | 2349 | if (s->pid_file) { |
f42806df LP |
2350 | bool has_start_post; |
2351 | int r; | |
2352 | ||
3a111838 MS |
2353 | /* Let's try to load the pid file here if we can. |
2354 | * The PID file might actually be created by a START_POST | |
2355 | * script. In that case don't worry if the loading fails. */ | |
f42806df LP |
2356 | |
2357 | has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST]; | |
2358 | r = service_load_pid_file(s, !has_start_post); | |
3a111838 MS |
2359 | if (!has_start_post && r < 0) { |
2360 | r = service_demand_pid_file(s); | |
2361 | if (r < 0 || !cgroup_good(s)) | |
f42806df | 2362 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
3a111838 MS |
2363 | break; |
2364 | } | |
034c6ed7 | 2365 | } else |
3a111838 | 2366 | service_search_main_pid(s); |
034c6ed7 | 2367 | |
3a111838 | 2368 | service_enter_start_post(s); |
034c6ed7 LP |
2369 | break; |
2370 | ||
2371 | case SERVICE_START_POST: | |
f42806df LP |
2372 | if (f != SERVICE_SUCCESS) { |
2373 | service_enter_stop(s, f); | |
2096e009 | 2374 | break; |
034c6ed7 LP |
2375 | } |
2376 | ||
2096e009 | 2377 | if (s->pid_file) { |
f42806df LP |
2378 | int r; |
2379 | ||
2380 | r = service_load_pid_file(s, true); | |
2096e009 MS |
2381 | if (r < 0) { |
2382 | r = service_demand_pid_file(s); | |
2383 | if (r < 0 || !cgroup_good(s)) | |
f42806df | 2384 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
2096e009 MS |
2385 | break; |
2386 | } | |
2387 | } else | |
2388 | service_search_main_pid(s); | |
2389 | ||
f42806df | 2390 | service_enter_running(s, SERVICE_SUCCESS); |
3185a36b | 2391 | break; |
034c6ed7 LP |
2392 | |
2393 | case SERVICE_RELOAD: | |
f42806df | 2394 | if (f == SERVICE_SUCCESS) { |
5375410b | 2395 | service_load_pid_file(s, true); |
3185a36b LP |
2396 | service_search_main_pid(s); |
2397 | } | |
2398 | ||
f42806df LP |
2399 | s->reload_result = f; |
2400 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
2401 | break; |
2402 | ||
2403 | case SERVICE_STOP: | |
f42806df | 2404 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
2405 | break; |
2406 | ||
2407 | case SERVICE_STOP_SIGTERM: | |
2408 | case SERVICE_STOP_SIGKILL: | |
2409 | if (main_pid_good(s) <= 0) | |
f42806df | 2410 | service_enter_stop_post(s, f); |
034c6ed7 LP |
2411 | |
2412 | /* If there is still a service | |
2413 | * process around, wait until | |
2414 | * that one quit, too */ | |
2415 | break; | |
2416 | ||
2417 | case SERVICE_STOP_POST: | |
2418 | case SERVICE_FINAL_SIGTERM: | |
2419 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 LP |
2420 | if (main_pid_good(s) <= 0) |
2421 | service_enter_dead(s, f, true); | |
034c6ed7 LP |
2422 | break; |
2423 | ||
2424 | default: | |
2425 | assert_not_reached("Uh, control process died at wrong time."); | |
2426 | } | |
2427 | } | |
8c47c732 | 2428 | } |
c4e2ceae LP |
2429 | |
2430 | /* Notify clients about changed exit status */ | |
2431 | unit_add_to_dbus_queue(u); | |
a911bb9a LP |
2432 | |
2433 | /* We got one SIGCHLD for the service, let's watch all | |
2434 | * processes that are now running of the service, and watch | |
2435 | * that. Among the PIDs we then watch will be children | |
2436 | * reassigned to us, which hopefully allows us to identify | |
2437 | * when all children are gone */ | |
2438 | unit_tidy_watch_pids(u, s->main_pid, s->control_pid); | |
2439 | unit_watch_all_pids(u); | |
2440 | ||
2441 | /* If the PID set is empty now, then let's finish this off */ | |
2442 | if (set_isempty(u->pids)) | |
2443 | service_notify_cgroup_empty_event(u); | |
034c6ed7 LP |
2444 | } |
2445 | ||
718db961 LP |
2446 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
2447 | Service *s = SERVICE(userdata); | |
034c6ed7 LP |
2448 | |
2449 | assert(s); | |
718db961 | 2450 | assert(source == s->timer_event_source); |
034c6ed7 LP |
2451 | |
2452 | switch (s->state) { | |
2453 | ||
2454 | case SERVICE_START_PRE: | |
2455 | case SERVICE_START: | |
718db961 | 2456 | log_warning_unit(UNIT(s)->id, |
41aef6fc ZJS |
2457 | "%s %s operation timed out. Terminating.", |
2458 | UNIT(s)->id, | |
2459 | s->state == SERVICE_START ? "start" : "start-pre"); | |
f42806df | 2460 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
80876c20 LP |
2461 | break; |
2462 | ||
034c6ed7 | 2463 | case SERVICE_START_POST: |
718db961 | 2464 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2465 | "%s start-post operation timed out. Stopping.", UNIT(s)->id); |
f42806df | 2466 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2467 | break; |
2468 | ||
e2f3b44c | 2469 | case SERVICE_RELOAD: |
718db961 | 2470 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2471 | "%s reload operation timed out. Stopping.", UNIT(s)->id); |
f42806df LP |
2472 | s->reload_result = SERVICE_FAILURE_TIMEOUT; |
2473 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c LP |
2474 | break; |
2475 | ||
034c6ed7 | 2476 | case SERVICE_STOP: |
718db961 LP |
2477 | log_warning_unit(UNIT(s)->id, |
2478 | "%s stopping timed out. Terminating.", UNIT(s)->id); | |
f42806df | 2479 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2480 | break; |
2481 | ||
2482 | case SERVICE_STOP_SIGTERM: | |
4819ff03 | 2483 | if (s->kill_context.send_sigkill) { |
718db961 | 2484 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2485 | "%s stop-sigterm timed out. Killing.", UNIT(s)->id); |
f42806df | 2486 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 2487 | } else { |
718db961 | 2488 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2489 | "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s)->id); |
f42806df | 2490 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
ba035df2 LP |
2491 | } |
2492 | ||
034c6ed7 LP |
2493 | break; |
2494 | ||
2495 | case SERVICE_STOP_SIGKILL: | |
35b8ca3a | 2496 | /* Uh, we sent a SIGKILL and it is still not gone? |
034c6ed7 LP |
2497 | * Must be something we cannot kill, so let's just be |
2498 | * weirded out and continue */ | |
2499 | ||
718db961 LP |
2500 | log_warning_unit(UNIT(s)->id, |
2501 | "%s still around after SIGKILL. Ignoring.", UNIT(s)->id); | |
f42806df | 2502 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2503 | break; |
2504 | ||
2505 | case SERVICE_STOP_POST: | |
718db961 | 2506 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2507 | "%s stop-post timed out. Terminating.", UNIT(s)->id); |
f42806df | 2508 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2509 | break; |
2510 | ||
2511 | case SERVICE_FINAL_SIGTERM: | |
4819ff03 | 2512 | if (s->kill_context.send_sigkill) { |
718db961 | 2513 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2514 | "%s stop-final-sigterm timed out. Killing.", UNIT(s)->id); |
f42806df | 2515 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 2516 | } else { |
718db961 | 2517 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2518 | "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", |
718db961 | 2519 | UNIT(s)->id); |
f42806df | 2520 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
ba035df2 LP |
2521 | } |
2522 | ||
034c6ed7 LP |
2523 | break; |
2524 | ||
2525 | case SERVICE_FINAL_SIGKILL: | |
718db961 | 2526 | log_warning_unit(UNIT(s)->id, |
41aef6fc | 2527 | "%s still around after final SIGKILL. Entering failed mode.", UNIT(s)->id); |
f42806df | 2528 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
034c6ed7 LP |
2529 | break; |
2530 | ||
2531 | case SERVICE_AUTO_RESTART: | |
718db961 | 2532 | log_info_unit(UNIT(s)->id, |
ef417cfd ZJS |
2533 | s->restart_usec > 0 ? |
2534 | "%s holdoff time over, scheduling restart." : | |
2535 | "%s has no holdoff time, scheduling restart.", | |
2536 | UNIT(s)->id); | |
034c6ed7 LP |
2537 | service_enter_restart(s); |
2538 | break; | |
2539 | ||
2540 | default: | |
2541 | assert_not_reached("Timeout at wrong time."); | |
2542 | } | |
718db961 LP |
2543 | |
2544 | return 0; | |
2545 | } | |
2546 | ||
2547 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { | |
2548 | Service *s = SERVICE(userdata); | |
a7850c7d | 2549 | char t[FORMAT_TIMESPAN_MAX]; |
718db961 LP |
2550 | |
2551 | assert(s); | |
2552 | assert(source == s->watchdog_event_source); | |
2553 | ||
a7850c7d ZJS |
2554 | log_error_unit(UNIT(s)->id, |
2555 | "%s watchdog timeout (limit %s)!", | |
2556 | UNIT(s)->id, | |
2557 | format_timespan(t, sizeof(t), s->watchdog_usec, 1)); | |
842129f5 LP |
2558 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_WATCHDOG); |
2559 | ||
718db961 | 2560 | return 0; |
5cb5a6ff LP |
2561 | } |
2562 | ||
c952c6ec | 2563 | static void service_notify_message(Unit *u, pid_t pid, char **tags) { |
8c47c732 LP |
2564 | Service *s = SERVICE(u); |
2565 | const char *e; | |
30b5275a | 2566 | bool notify_dbus = false; |
8c47c732 LP |
2567 | |
2568 | assert(u); | |
2569 | ||
ccd06097 ZJS |
2570 | log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT" (%s...)", |
2571 | u->id, pid, tags && *tags ? tags[0] : "(empty)"); | |
da13d4d2 | 2572 | |
c952c6ec | 2573 | if (s->notify_access == NOTIFY_NONE) { |
66870f90 | 2574 | log_warning_unit(u->id, |
ccd06097 ZJS |
2575 | "%s: Got notification message from PID "PID_FMT", but reception is disabled.", |
2576 | u->id, pid); | |
c952c6ec LP |
2577 | return; |
2578 | } | |
2579 | ||
336c6e46 | 2580 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { |
336c6e46 LP |
2581 | if (s->main_pid != 0) |
2582 | log_warning_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, u->id, pid, s->main_pid); | |
2583 | else | |
3a33e61d | 2584 | log_debug_unit(u->id, "%s: Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", u->id, pid); |
c952c6ec LP |
2585 | return; |
2586 | } | |
2587 | ||
8c47c732 | 2588 | /* Interpret MAINPID= */ |
5e56b378 LP |
2589 | e = strv_find_prefix(tags, "MAINPID="); |
2590 | if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) { | |
5925dd3c | 2591 | if (parse_pid(e + 8, &pid) < 0) |
5e56b378 | 2592 | log_warning_unit(u->id, "Failed to parse MAINPID= field in notification message: %s", e); |
8c47c732 | 2593 | else { |
30b5275a | 2594 | log_debug_unit(u->id, "%s: got %s", u->id, e); |
5925dd3c | 2595 | service_set_main_pid(s, pid); |
7400b9d2 | 2596 | unit_watch_pid(UNIT(s), pid); |
30b5275a | 2597 | notify_dbus = true; |
8c47c732 LP |
2598 | } |
2599 | } | |
2600 | ||
2601 | /* Interpret READY= */ | |
30b5275a LP |
2602 | if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START && strv_find(tags, "READY=1")) { |
2603 | log_debug_unit(u->id, "%s: got READY=1", u->id); | |
8c47c732 | 2604 | service_enter_start_post(s); |
30b5275a | 2605 | notify_dbus = true; |
8c47c732 LP |
2606 | } |
2607 | ||
2608 | /* Interpret STATUS= */ | |
7f110ff9 LP |
2609 | e = strv_find_prefix(tags, "STATUS="); |
2610 | if (e) { | |
8c47c732 LP |
2611 | char *t; |
2612 | ||
3a2776bc | 2613 | if (e[7]) { |
7f110ff9 | 2614 | if (!utf8_is_valid(e+7)) { |
30b5275a | 2615 | log_warning_unit(u->id, "Status message in notification is not UTF-8 clean."); |
7f110ff9 LP |
2616 | return; |
2617 | } | |
2618 | ||
30b5275a LP |
2619 | log_debug_unit(u->id, "%s: got %s", u->id, e); |
2620 | ||
7f110ff9 LP |
2621 | t = strdup(e+7); |
2622 | if (!t) { | |
30b5275a | 2623 | log_oom(); |
3a2776bc LP |
2624 | return; |
2625 | } | |
2626 | ||
30b5275a LP |
2627 | } else |
2628 | t = NULL; | |
8c47c732 | 2629 | |
30b5275a | 2630 | if (!streq_ptr(s->status_text, t)) { |
3a2776bc LP |
2631 | free(s->status_text); |
2632 | s->status_text = t; | |
30b5275a LP |
2633 | notify_dbus = true; |
2634 | } else | |
2635 | free(t); | |
8c47c732 | 2636 | } |
842129f5 | 2637 | |
4774e357 MAA |
2638 | /* Interpret ERRNO= */ |
2639 | e = strv_find_prefix(tags, "ERRNO="); | |
2640 | if (e) { | |
2641 | int status_errno; | |
2642 | ||
2040ccf1 | 2643 | if (safe_atoi(e + 6, &status_errno) < 0 || status_errno < 0) |
4774e357 MAA |
2644 | log_warning_unit(u->id, "Failed to parse ERRNO= field in notification message: %s", e); |
2645 | else { | |
2646 | log_debug_unit(u->id, "%s: got %s", u->id, e); | |
2647 | ||
2648 | if (s->status_errno != status_errno) { | |
2649 | s->status_errno = status_errno; | |
2650 | notify_dbus = true; | |
2651 | } | |
2652 | } | |
2653 | } | |
2654 | ||
6f285378 | 2655 | /* Interpret WATCHDOG= */ |
a6927d7f | 2656 | if (strv_find(tags, "WATCHDOG=1")) { |
842129f5 LP |
2657 | log_debug_unit(u->id, "%s: got WATCHDOG=1", u->id); |
2658 | service_reset_watchdog(s); | |
a6927d7f | 2659 | } |
c4e2ceae LP |
2660 | |
2661 | /* Notify clients about changed status or main pid */ | |
30b5275a LP |
2662 | if (notify_dbus) |
2663 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
2664 | } |
2665 | ||
68db7a3b ZJS |
2666 | static int service_get_timeout(Unit *u, uint64_t *timeout) { |
2667 | Service *s = SERVICE(u); | |
2668 | int r; | |
2669 | ||
2670 | if (!s->timer_event_source) | |
2671 | return 0; | |
2672 | ||
2673 | r = sd_event_source_get_time(s->timer_event_source, timeout); | |
2674 | if (r < 0) | |
2675 | return r; | |
2676 | ||
2677 | return 1; | |
2678 | } | |
2679 | ||
05e343b7 LP |
2680 | static void service_bus_name_owner_change( |
2681 | Unit *u, | |
2682 | const char *name, | |
2683 | const char *old_owner, | |
2684 | const char *new_owner) { | |
2685 | ||
2686 | Service *s = SERVICE(u); | |
718db961 | 2687 | int r; |
05e343b7 LP |
2688 | |
2689 | assert(s); | |
2690 | assert(name); | |
2691 | ||
2692 | assert(streq(s->bus_name, name)); | |
2693 | assert(old_owner || new_owner); | |
2694 | ||
2695 | if (old_owner && new_owner) | |
66870f90 ZJS |
2696 | log_debug_unit(u->id, |
2697 | "%s's D-Bus name %s changed owner from %s to %s", | |
2698 | u->id, name, old_owner, new_owner); | |
05e343b7 | 2699 | else if (old_owner) |
66870f90 ZJS |
2700 | log_debug_unit(u->id, |
2701 | "%s's D-Bus name %s no longer registered by %s", | |
2702 | u->id, name, old_owner); | |
05e343b7 | 2703 | else |
66870f90 ZJS |
2704 | log_debug_unit(u->id, |
2705 | "%s's D-Bus name %s now registered by %s", | |
2706 | u->id, name, new_owner); | |
05e343b7 LP |
2707 | |
2708 | s->bus_name_good = !!new_owner; | |
2709 | ||
2710 | if (s->type == SERVICE_DBUS) { | |
2711 | ||
2712 | /* service_enter_running() will figure out what to | |
2713 | * do */ | |
2714 | if (s->state == SERVICE_RUNNING) | |
f42806df | 2715 | service_enter_running(s, SERVICE_SUCCESS); |
05e343b7 LP |
2716 | else if (s->state == SERVICE_START && new_owner) |
2717 | service_enter_start_post(s); | |
2718 | ||
2719 | } else if (new_owner && | |
2720 | s->main_pid <= 0 && | |
2721 | (s->state == SERVICE_START || | |
2722 | s->state == SERVICE_START_POST || | |
2723 | s->state == SERVICE_RUNNING || | |
2724 | s->state == SERVICE_RELOAD)) { | |
2725 | ||
5b12334d | 2726 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
718db961 | 2727 | pid_t pid; |
05e343b7 | 2728 | |
718db961 | 2729 | /* Try to acquire PID from bus service */ |
05e343b7 | 2730 | |
49b832c5 | 2731 | r = sd_bus_get_owner(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds); |
5b12334d LP |
2732 | if (r >= 0) |
2733 | r = sd_bus_creds_get_pid(creds, &pid); | |
718db961 LP |
2734 | if (r >= 0) { |
2735 | log_debug_unit(u->id, "%s's D-Bus name %s is now owned by process %u", u->id, name, (unsigned) pid); | |
05e343b7 | 2736 | |
718db961 LP |
2737 | service_set_main_pid(s, pid); |
2738 | unit_watch_pid(UNIT(s), pid); | |
2739 | } | |
7400b9d2 | 2740 | } |
05e343b7 LP |
2741 | } |
2742 | ||
6cf6bbc2 | 2743 | int service_set_socket_fd(Service *s, int fd, Socket *sock) { |
79a98c60 LP |
2744 | _cleanup_free_ char *peer = NULL; |
2745 | int r; | |
57020a3a | 2746 | |
4f2d528d LP |
2747 | assert(s); |
2748 | assert(fd >= 0); | |
2749 | ||
2750 | /* This is called by the socket code when instantiating a new | |
2751 | * service for a stream socket and the socket needs to be | |
2752 | * configured. */ | |
2753 | ||
1124fe6f | 2754 | if (UNIT(s)->load_state != UNIT_LOADED) |
4f2d528d LP |
2755 | return -EINVAL; |
2756 | ||
2757 | if (s->socket_fd >= 0) | |
2758 | return -EBUSY; | |
2759 | ||
2760 | if (s->state != SERVICE_DEAD) | |
2761 | return -EAGAIN; | |
2762 | ||
79a98c60 LP |
2763 | if (getpeername_pretty(fd, &peer) >= 0) { |
2764 | ||
2765 | if (UNIT(s)->description) { | |
2766 | _cleanup_free_ char *a; | |
2767 | ||
2768 | a = strjoin(UNIT(s)->description, " (", peer, ")", NULL); | |
2769 | if (!a) | |
2770 | return -ENOMEM; | |
2771 | ||
2772 | r = unit_set_description(UNIT(s), a); | |
2773 | } else | |
2774 | r = unit_set_description(UNIT(s), peer); | |
2775 | ||
2776 | if (r < 0) | |
2777 | return r; | |
2778 | } | |
2779 | ||
4f2d528d | 2780 | s->socket_fd = fd; |
6cf6bbc2 | 2781 | |
57020a3a LP |
2782 | unit_ref_set(&s->accept_socket, UNIT(sock)); |
2783 | ||
2784 | return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false); | |
4f2d528d LP |
2785 | } |
2786 | ||
fdf20a31 | 2787 | static void service_reset_failed(Unit *u) { |
5632e374 LP |
2788 | Service *s = SERVICE(u); |
2789 | ||
2790 | assert(s); | |
2791 | ||
fdf20a31 | 2792 | if (s->state == SERVICE_FAILED) |
5632e374 LP |
2793 | service_set_state(s, SERVICE_DEAD); |
2794 | ||
f42806df LP |
2795 | s->result = SERVICE_SUCCESS; |
2796 | s->reload_result = SERVICE_SUCCESS; | |
451b34cc LP |
2797 | |
2798 | RATELIMIT_RESET(s->start_limit); | |
5632e374 LP |
2799 | } |
2800 | ||
718db961 | 2801 | static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
8a0867d6 | 2802 | Service *s = SERVICE(u); |
41efeaec | 2803 | |
814cc562 | 2804 | return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error); |
8a0867d6 LP |
2805 | } |
2806 | ||
94f04347 LP |
2807 | static const char* const service_state_table[_SERVICE_STATE_MAX] = { |
2808 | [SERVICE_DEAD] = "dead", | |
2809 | [SERVICE_START_PRE] = "start-pre", | |
2810 | [SERVICE_START] = "start", | |
2811 | [SERVICE_START_POST] = "start-post", | |
2812 | [SERVICE_RUNNING] = "running", | |
80876c20 | 2813 | [SERVICE_EXITED] = "exited", |
94f04347 LP |
2814 | [SERVICE_RELOAD] = "reload", |
2815 | [SERVICE_STOP] = "stop", | |
2816 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", | |
2817 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
2818 | [SERVICE_STOP_POST] = "stop-post", | |
2819 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
2820 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
fdf20a31 | 2821 | [SERVICE_FAILED] = "failed", |
94f04347 LP |
2822 | [SERVICE_AUTO_RESTART] = "auto-restart", |
2823 | }; | |
2824 | ||
2825 | DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); | |
2826 | ||
2827 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { | |
525ee6f4 LP |
2828 | [SERVICE_RESTART_NO] = "no", |
2829 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
50caaedb | 2830 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", |
6cfe2fde | 2831 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
dc99a976 | 2832 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
50caaedb | 2833 | [SERVICE_RESTART_ON_ABORT] = "on-abort", |
6cfe2fde | 2834 | [SERVICE_RESTART_ALWAYS] = "always", |
94f04347 LP |
2835 | }; |
2836 | ||
2837 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
2838 | ||
2839 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
94f04347 | 2840 | [SERVICE_SIMPLE] = "simple", |
0d624a78 | 2841 | [SERVICE_FORKING] = "forking", |
34e9ba66 | 2842 | [SERVICE_ONESHOT] = "oneshot", |
8c47c732 | 2843 | [SERVICE_DBUS] = "dbus", |
f2b68789 LP |
2844 | [SERVICE_NOTIFY] = "notify", |
2845 | [SERVICE_IDLE] = "idle" | |
94f04347 LP |
2846 | }; |
2847 | ||
2848 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
2849 | ||
e537352b | 2850 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
94f04347 LP |
2851 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", |
2852 | [SERVICE_EXEC_START] = "ExecStart", | |
2853 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
2854 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
2855 | [SERVICE_EXEC_STOP] = "ExecStop", | |
2856 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
2857 | }; | |
2858 | ||
2859 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
2860 | ||
c952c6ec LP |
2861 | static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = { |
2862 | [NOTIFY_NONE] = "none", | |
2863 | [NOTIFY_MAIN] = "main", | |
2864 | [NOTIFY_ALL] = "all" | |
2865 | }; | |
2866 | ||
2867 | DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess); | |
2868 | ||
f42806df LP |
2869 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
2870 | [SERVICE_SUCCESS] = "success", | |
2871 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
2872 | [SERVICE_FAILURE_TIMEOUT] = "timeout", | |
2873 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
2874 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
bb242b7b | 2875 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", |
8d1b002a LP |
2876 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", |
2877 | [SERVICE_FAILURE_START_LIMIT] = "start-limit" | |
f42806df LP |
2878 | }; |
2879 | ||
2880 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
2881 | ||
bf500566 MO |
2882 | static const char* const failure_action_table[_SERVICE_FAILURE_ACTION_MAX] = { |
2883 | [SERVICE_FAILURE_ACTION_NONE] = "none", | |
2884 | [SERVICE_FAILURE_ACTION_REBOOT] = "reboot", | |
2885 | [SERVICE_FAILURE_ACTION_REBOOT_FORCE] = "reboot-force", | |
2886 | [SERVICE_FAILURE_ACTION_REBOOT_IMMEDIATE] = "reboot-immediate" | |
4b939747 | 2887 | }; |
bf500566 | 2888 | DEFINE_STRING_TABLE_LOOKUP(failure_action, FailureAction); |
4b939747 | 2889 | |
87f0e418 | 2890 | const UnitVTable service_vtable = { |
7d17cfbc | 2891 | .object_size = sizeof(Service), |
718db961 LP |
2892 | .exec_context_offset = offsetof(Service, exec_context), |
2893 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
2894 | .kill_context_offset = offsetof(Service, kill_context), | |
613b411c | 2895 | .exec_runtime_offset = offsetof(Service, exec_runtime), |
3ef63c31 | 2896 | |
f975e971 LP |
2897 | .sections = |
2898 | "Unit\0" | |
2899 | "Service\0" | |
2900 | "Install\0", | |
4ad49000 | 2901 | .private_section = "Service", |
71645aca | 2902 | |
034c6ed7 LP |
2903 | .init = service_init, |
2904 | .done = service_done, | |
a16e1123 LP |
2905 | .load = service_load, |
2906 | ||
2907 | .coldplug = service_coldplug, | |
034c6ed7 | 2908 | |
5cb5a6ff LP |
2909 | .dump = service_dump, |
2910 | ||
2911 | .start = service_start, | |
2912 | .stop = service_stop, | |
2913 | .reload = service_reload, | |
2914 | ||
034c6ed7 LP |
2915 | .can_reload = service_can_reload, |
2916 | ||
8a0867d6 LP |
2917 | .kill = service_kill, |
2918 | ||
a16e1123 LP |
2919 | .serialize = service_serialize, |
2920 | .deserialize_item = service_deserialize_item, | |
2921 | ||
5cb5a6ff | 2922 | .active_state = service_active_state, |
10a94420 | 2923 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 2924 | |
701cc384 LP |
2925 | .check_gc = service_check_gc, |
2926 | .check_snapshot = service_check_snapshot, | |
2927 | ||
034c6ed7 | 2928 | .sigchld_event = service_sigchld_event, |
2c4104f0 | 2929 | |
fdf20a31 | 2930 | .reset_failed = service_reset_failed, |
5632e374 | 2931 | |
4ad49000 | 2932 | .notify_cgroup_empty = service_notify_cgroup_empty_event, |
8c47c732 | 2933 | .notify_message = service_notify_message, |
8e274523 | 2934 | |
05e343b7 | 2935 | .bus_name_owner_change = service_bus_name_owner_change, |
05e343b7 | 2936 | |
c4e2ceae | 2937 | .bus_interface = "org.freedesktop.systemd1.Service", |
718db961 | 2938 | .bus_vtable = bus_service_vtable, |
74c964d3 LP |
2939 | .bus_set_property = bus_service_set_property, |
2940 | .bus_commit_properties = bus_service_commit_properties, | |
4139c1b2 | 2941 | |
68db7a3b | 2942 | .get_timeout = service_get_timeout, |
718db961 LP |
2943 | .can_transient = true, |
2944 | ||
c6918296 MS |
2945 | .status_message_formats = { |
2946 | .starting_stopping = { | |
2947 | [0] = "Starting %s...", | |
2948 | [1] = "Stopping %s...", | |
2949 | }, | |
2950 | .finished_start_job = { | |
2951 | [JOB_DONE] = "Started %s.", | |
2952 | [JOB_FAILED] = "Failed to start %s.", | |
2953 | [JOB_DEPENDENCY] = "Dependency failed for %s.", | |
2954 | [JOB_TIMEOUT] = "Timed out starting %s.", | |
2955 | }, | |
2956 | .finished_stop_job = { | |
2957 | [JOB_DONE] = "Stopped %s.", | |
2958 | [JOB_FAILED] = "Stopped (with error) %s.", | |
2959 | [JOB_TIMEOUT] = "Timed out stopping %s.", | |
2960 | }, | |
2961 | }, | |
5cb5a6ff | 2962 | }; |