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