]>
Commit | Line | Data |
---|---|---|
1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ | |
2 | ||
3 | #include <errno.h> | |
4 | #include <sys/stat.h> | |
5 | #include <sys/types.h> | |
6 | #include <unistd.h> | |
7 | ||
8 | #include "sd-messages.h" | |
9 | ||
10 | #include "alloc-util.h" | |
11 | #include "async.h" | |
12 | #include "bus-error.h" | |
13 | #include "bus-kernel.h" | |
14 | #include "bus-util.h" | |
15 | #include "chase-symlinks.h" | |
16 | #include "dbus-service.h" | |
17 | #include "dbus-unit.h" | |
18 | #include "def.h" | |
19 | #include "env-util.h" | |
20 | #include "escape.h" | |
21 | #include "exit-status.h" | |
22 | #include "fd-util.h" | |
23 | #include "fileio.h" | |
24 | #include "format-util.h" | |
25 | #include "load-dropin.h" | |
26 | #include "load-fragment.h" | |
27 | #include "log.h" | |
28 | #include "manager.h" | |
29 | #include "parse-util.h" | |
30 | #include "path-util.h" | |
31 | #include "process-util.h" | |
32 | #include "random-util.h" | |
33 | #include "serialize.h" | |
34 | #include "service.h" | |
35 | #include "signal-util.h" | |
36 | #include "special.h" | |
37 | #include "stdio-util.h" | |
38 | #include "string-table.h" | |
39 | #include "string-util.h" | |
40 | #include "strv.h" | |
41 | #include "unit-name.h" | |
42 | #include "unit.h" | |
43 | #include "utf8.h" | |
44 | #include "util.h" | |
45 | ||
46 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { | |
47 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
48 | [SERVICE_CONDITION] = UNIT_ACTIVATING, | |
49 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
50 | [SERVICE_START] = UNIT_ACTIVATING, | |
51 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
52 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
53 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
54 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
55 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
56 | [SERVICE_STOP_WATCHDOG] = UNIT_DEACTIVATING, | |
57 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
58 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
59 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
60 | [SERVICE_FINAL_WATCHDOG] = UNIT_DEACTIVATING, | |
61 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
62 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
63 | [SERVICE_FAILED] = UNIT_FAILED, | |
64 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, | |
65 | [SERVICE_CLEANING] = UNIT_MAINTENANCE, | |
66 | }; | |
67 | ||
68 | /* For Type=idle we never want to delay any other jobs, hence we | |
69 | * consider idle jobs active as soon as we start working on them */ | |
70 | static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = { | |
71 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
72 | [SERVICE_CONDITION] = UNIT_ACTIVE, | |
73 | [SERVICE_START_PRE] = UNIT_ACTIVE, | |
74 | [SERVICE_START] = UNIT_ACTIVE, | |
75 | [SERVICE_START_POST] = UNIT_ACTIVE, | |
76 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
77 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
78 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
79 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
80 | [SERVICE_STOP_WATCHDOG] = UNIT_DEACTIVATING, | |
81 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
82 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
83 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
84 | [SERVICE_FINAL_WATCHDOG] = UNIT_DEACTIVATING, | |
85 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
86 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
87 | [SERVICE_FAILED] = UNIT_FAILED, | |
88 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, | |
89 | [SERVICE_CLEANING] = UNIT_MAINTENANCE, | |
90 | }; | |
91 | ||
92 | static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata); | |
93 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); | |
94 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata); | |
95 | static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata); | |
96 | ||
97 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f); | |
98 | static void service_enter_reload_by_notify(Service *s); | |
99 | ||
100 | static void service_init(Unit *u) { | |
101 | Service *s = SERVICE(u); | |
102 | ||
103 | assert(u); | |
104 | assert(u->load_state == UNIT_STUB); | |
105 | ||
106 | s->timeout_start_usec = u->manager->default_timeout_start_usec; | |
107 | s->timeout_stop_usec = u->manager->default_timeout_stop_usec; | |
108 | s->timeout_abort_usec = u->manager->default_timeout_abort_usec; | |
109 | s->timeout_abort_set = u->manager->default_timeout_abort_set; | |
110 | s->restart_usec = u->manager->default_restart_usec; | |
111 | s->runtime_max_usec = USEC_INFINITY; | |
112 | s->type = _SERVICE_TYPE_INVALID; | |
113 | s->socket_fd = -1; | |
114 | s->stdin_fd = s->stdout_fd = s->stderr_fd = -1; | |
115 | s->guess_main_pid = true; | |
116 | ||
117 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
118 | ||
119 | s->exec_context.keyring_mode = MANAGER_IS_SYSTEM(u->manager) ? | |
120 | EXEC_KEYRING_PRIVATE : EXEC_KEYRING_INHERIT; | |
121 | ||
122 | s->watchdog_original_usec = USEC_INFINITY; | |
123 | ||
124 | s->oom_policy = _OOM_POLICY_INVALID; | |
125 | } | |
126 | ||
127 | static void service_unwatch_control_pid(Service *s) { | |
128 | assert(s); | |
129 | ||
130 | if (s->control_pid <= 0) | |
131 | return; | |
132 | ||
133 | unit_unwatch_pid(UNIT(s), TAKE_PID(s->control_pid)); | |
134 | } | |
135 | ||
136 | static void service_unwatch_main_pid(Service *s) { | |
137 | assert(s); | |
138 | ||
139 | if (s->main_pid <= 0) | |
140 | return; | |
141 | ||
142 | unit_unwatch_pid(UNIT(s), TAKE_PID(s->main_pid)); | |
143 | } | |
144 | ||
145 | static void service_unwatch_pid_file(Service *s) { | |
146 | if (!s->pid_file_pathspec) | |
147 | return; | |
148 | ||
149 | log_unit_debug(UNIT(s), "Stopping watch for PID file %s", s->pid_file_pathspec->path); | |
150 | path_spec_unwatch(s->pid_file_pathspec); | |
151 | path_spec_done(s->pid_file_pathspec); | |
152 | s->pid_file_pathspec = mfree(s->pid_file_pathspec); | |
153 | } | |
154 | ||
155 | static int service_set_main_pid(Service *s, pid_t pid) { | |
156 | assert(s); | |
157 | ||
158 | if (pid <= 1) | |
159 | return -EINVAL; | |
160 | ||
161 | if (pid == getpid_cached()) | |
162 | return -EINVAL; | |
163 | ||
164 | if (s->main_pid == pid && s->main_pid_known) | |
165 | return 0; | |
166 | ||
167 | if (s->main_pid != pid) { | |
168 | service_unwatch_main_pid(s); | |
169 | exec_status_start(&s->main_exec_status, pid); | |
170 | } | |
171 | ||
172 | s->main_pid = pid; | |
173 | s->main_pid_known = true; | |
174 | s->main_pid_alien = pid_is_my_child(pid) == 0; | |
175 | ||
176 | if (s->main_pid_alien) | |
177 | log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", pid); | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | void service_close_socket_fd(Service *s) { | |
183 | assert(s); | |
184 | ||
185 | /* Undo the effect of service_set_socket_fd(). */ | |
186 | ||
187 | s->socket_fd = asynchronous_close(s->socket_fd); | |
188 | ||
189 | if (UNIT_ISSET(s->accept_socket)) { | |
190 | socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket))); | |
191 | unit_ref_unset(&s->accept_socket); | |
192 | } | |
193 | ||
194 | s->socket_peer = socket_peer_unref(s->socket_peer); | |
195 | } | |
196 | ||
197 | static void service_stop_watchdog(Service *s) { | |
198 | assert(s); | |
199 | ||
200 | s->watchdog_event_source = sd_event_source_disable_unref(s->watchdog_event_source); | |
201 | s->watchdog_timestamp = DUAL_TIMESTAMP_NULL; | |
202 | } | |
203 | ||
204 | static void service_start_watchdog(Service *s) { | |
205 | usec_t watchdog_usec; | |
206 | int r; | |
207 | ||
208 | assert(s); | |
209 | ||
210 | watchdog_usec = service_get_watchdog_usec(s); | |
211 | if (IN_SET(watchdog_usec, 0, USEC_INFINITY)) { | |
212 | service_stop_watchdog(s); | |
213 | return; | |
214 | } | |
215 | ||
216 | if (s->watchdog_event_source) { | |
217 | r = sd_event_source_set_time(s->watchdog_event_source, usec_add(s->watchdog_timestamp.monotonic, watchdog_usec)); | |
218 | if (r < 0) { | |
219 | log_unit_warning_errno(UNIT(s), r, "Failed to reset watchdog timer: %m"); | |
220 | return; | |
221 | } | |
222 | ||
223 | r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT); | |
224 | } else { | |
225 | r = sd_event_add_time( | |
226 | UNIT(s)->manager->event, | |
227 | &s->watchdog_event_source, | |
228 | CLOCK_MONOTONIC, | |
229 | usec_add(s->watchdog_timestamp.monotonic, watchdog_usec), 0, | |
230 | service_dispatch_watchdog, s); | |
231 | if (r < 0) { | |
232 | log_unit_warning_errno(UNIT(s), r, "Failed to add watchdog timer: %m"); | |
233 | return; | |
234 | } | |
235 | ||
236 | (void) sd_event_source_set_description(s->watchdog_event_source, "service-watchdog"); | |
237 | ||
238 | /* Let's process everything else which might be a sign | |
239 | * of living before we consider a service died. */ | |
240 | r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE); | |
241 | } | |
242 | if (r < 0) | |
243 | log_unit_warning_errno(UNIT(s), r, "Failed to install watchdog timer: %m"); | |
244 | } | |
245 | ||
246 | static void service_extend_event_source_timeout(Service *s, sd_event_source *source, usec_t extended) { | |
247 | usec_t current; | |
248 | int r; | |
249 | ||
250 | assert(s); | |
251 | ||
252 | /* Extends the specified event source timer to at least the specified time, unless it is already later | |
253 | * anyway. */ | |
254 | ||
255 | if (!source) | |
256 | return; | |
257 | ||
258 | r = sd_event_source_get_time(source, ¤t); | |
259 | if (r < 0) { | |
260 | const char *desc; | |
261 | (void) sd_event_source_get_description(s->timer_event_source, &desc); | |
262 | log_unit_warning_errno(UNIT(s), r, "Failed to retrieve timeout time for event source '%s', ignoring: %m", strna(desc)); | |
263 | return; | |
264 | } | |
265 | ||
266 | if (current >= extended) /* Current timeout is already longer, ignore this. */ | |
267 | return; | |
268 | ||
269 | r = sd_event_source_set_time(source, extended); | |
270 | if (r < 0) { | |
271 | const char *desc; | |
272 | (void) sd_event_source_get_description(s->timer_event_source, &desc); | |
273 | log_unit_warning_errno(UNIT(s), r, "Failed to set timeout time for even source '%s', ignoring %m", strna(desc)); | |
274 | } | |
275 | } | |
276 | ||
277 | static void service_extend_timeout(Service *s, usec_t extend_timeout_usec) { | |
278 | usec_t extended; | |
279 | ||
280 | assert(s); | |
281 | ||
282 | if (IN_SET(extend_timeout_usec, 0, USEC_INFINITY)) | |
283 | return; | |
284 | ||
285 | extended = usec_add(now(CLOCK_MONOTONIC), extend_timeout_usec); | |
286 | ||
287 | service_extend_event_source_timeout(s, s->timer_event_source, extended); | |
288 | service_extend_event_source_timeout(s, s->watchdog_event_source, extended); | |
289 | } | |
290 | ||
291 | static void service_reset_watchdog(Service *s) { | |
292 | assert(s); | |
293 | ||
294 | dual_timestamp_get(&s->watchdog_timestamp); | |
295 | service_start_watchdog(s); | |
296 | } | |
297 | ||
298 | static void service_override_watchdog_timeout(Service *s, usec_t watchdog_override_usec) { | |
299 | assert(s); | |
300 | ||
301 | s->watchdog_override_enable = true; | |
302 | s->watchdog_override_usec = watchdog_override_usec; | |
303 | service_reset_watchdog(s); | |
304 | ||
305 | log_unit_debug(UNIT(s), "watchdog_usec="USEC_FMT, s->watchdog_usec); | |
306 | log_unit_debug(UNIT(s), "watchdog_override_usec="USEC_FMT, s->watchdog_override_usec); | |
307 | } | |
308 | ||
309 | static void service_fd_store_unlink(ServiceFDStore *fs) { | |
310 | ||
311 | if (!fs) | |
312 | return; | |
313 | ||
314 | if (fs->service) { | |
315 | assert(fs->service->n_fd_store > 0); | |
316 | LIST_REMOVE(fd_store, fs->service->fd_store, fs); | |
317 | fs->service->n_fd_store--; | |
318 | } | |
319 | ||
320 | sd_event_source_disable_unref(fs->event_source); | |
321 | ||
322 | free(fs->fdname); | |
323 | safe_close(fs->fd); | |
324 | free(fs); | |
325 | } | |
326 | ||
327 | static void service_release_fd_store(Service *s) { | |
328 | assert(s); | |
329 | ||
330 | if (s->n_keep_fd_store > 0) | |
331 | return; | |
332 | ||
333 | log_unit_debug(UNIT(s), "Releasing all stored fds"); | |
334 | while (s->fd_store) | |
335 | service_fd_store_unlink(s->fd_store); | |
336 | ||
337 | assert(s->n_fd_store == 0); | |
338 | } | |
339 | ||
340 | static void service_release_resources(Unit *u) { | |
341 | Service *s = SERVICE(u); | |
342 | ||
343 | assert(s); | |
344 | ||
345 | if (!s->fd_store && s->stdin_fd < 0 && s->stdout_fd < 0 && s->stderr_fd < 0) | |
346 | return; | |
347 | ||
348 | log_unit_debug(u, "Releasing resources."); | |
349 | ||
350 | s->stdin_fd = safe_close(s->stdin_fd); | |
351 | s->stdout_fd = safe_close(s->stdout_fd); | |
352 | s->stderr_fd = safe_close(s->stderr_fd); | |
353 | ||
354 | service_release_fd_store(s); | |
355 | } | |
356 | ||
357 | static void service_done(Unit *u) { | |
358 | Service *s = SERVICE(u); | |
359 | ||
360 | assert(s); | |
361 | ||
362 | s->pid_file = mfree(s->pid_file); | |
363 | s->status_text = mfree(s->status_text); | |
364 | ||
365 | s->exec_runtime = exec_runtime_unref(s->exec_runtime, false); | |
366 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); | |
367 | s->control_command = NULL; | |
368 | s->main_command = NULL; | |
369 | ||
370 | dynamic_creds_unref(&s->dynamic_creds); | |
371 | ||
372 | exit_status_set_free(&s->restart_prevent_status); | |
373 | exit_status_set_free(&s->restart_force_status); | |
374 | exit_status_set_free(&s->success_status); | |
375 | ||
376 | /* This will leak a process, but at least no memory or any of | |
377 | * our resources */ | |
378 | service_unwatch_main_pid(s); | |
379 | service_unwatch_control_pid(s); | |
380 | service_unwatch_pid_file(s); | |
381 | ||
382 | if (s->bus_name) { | |
383 | unit_unwatch_bus_name(u, s->bus_name); | |
384 | s->bus_name = mfree(s->bus_name); | |
385 | } | |
386 | ||
387 | s->bus_name_owner = mfree(s->bus_name_owner); | |
388 | ||
389 | s->usb_function_descriptors = mfree(s->usb_function_descriptors); | |
390 | s->usb_function_strings = mfree(s->usb_function_strings); | |
391 | ||
392 | service_close_socket_fd(s); | |
393 | ||
394 | unit_ref_unset(&s->accept_socket); | |
395 | ||
396 | service_stop_watchdog(s); | |
397 | ||
398 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); | |
399 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
400 | ||
401 | service_release_resources(u); | |
402 | } | |
403 | ||
404 | static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) { | |
405 | ServiceFDStore *fs = userdata; | |
406 | ||
407 | assert(e); | |
408 | assert(fs); | |
409 | ||
410 | /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */ | |
411 | log_unit_debug(UNIT(fs->service), | |
412 | "Received %s on stored fd %d (%s), closing.", | |
413 | revents & EPOLLERR ? "EPOLLERR" : "EPOLLHUP", | |
414 | fs->fd, strna(fs->fdname)); | |
415 | service_fd_store_unlink(fs); | |
416 | return 0; | |
417 | } | |
418 | ||
419 | static int service_add_fd_store(Service *s, int fd, const char *name, bool do_poll) { | |
420 | ServiceFDStore *fs; | |
421 | int r; | |
422 | ||
423 | /* fd is always consumed if we return >= 0 */ | |
424 | ||
425 | assert(s); | |
426 | assert(fd >= 0); | |
427 | ||
428 | if (s->n_fd_store >= s->n_fd_store_max) | |
429 | return -EXFULL; /* Our store is full. | |
430 | * Use this errno rather than E[NM]FILE to distinguish from | |
431 | * the case where systemd itself hits the file limit. */ | |
432 | ||
433 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
434 | r = same_fd(fs->fd, fd); | |
435 | if (r < 0) | |
436 | return r; | |
437 | if (r > 0) { | |
438 | safe_close(fd); | |
439 | return 0; /* fd already included */ | |
440 | } | |
441 | } | |
442 | ||
443 | fs = new(ServiceFDStore, 1); | |
444 | if (!fs) | |
445 | return -ENOMEM; | |
446 | ||
447 | *fs = (ServiceFDStore) { | |
448 | .fd = fd, | |
449 | .service = s, | |
450 | .do_poll = do_poll, | |
451 | .fdname = strdup(name ?: "stored"), | |
452 | }; | |
453 | ||
454 | if (!fs->fdname) { | |
455 | free(fs); | |
456 | return -ENOMEM; | |
457 | } | |
458 | ||
459 | if (do_poll) { | |
460 | r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fd, 0, on_fd_store_io, fs); | |
461 | if (r < 0 && r != -EPERM) { /* EPERM indicates fds that aren't pollable, which is OK */ | |
462 | free(fs->fdname); | |
463 | free(fs); | |
464 | return r; | |
465 | } else if (r >= 0) | |
466 | (void) sd_event_source_set_description(fs->event_source, "service-fd-store"); | |
467 | } | |
468 | ||
469 | LIST_PREPEND(fd_store, s->fd_store, fs); | |
470 | s->n_fd_store++; | |
471 | ||
472 | return 1; /* fd newly stored */ | |
473 | } | |
474 | ||
475 | static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name, bool do_poll) { | |
476 | int r; | |
477 | ||
478 | assert(s); | |
479 | ||
480 | while (fdset_size(fds) > 0) { | |
481 | _cleanup_close_ int fd = -1; | |
482 | ||
483 | fd = fdset_steal_first(fds); | |
484 | if (fd < 0) | |
485 | break; | |
486 | ||
487 | r = service_add_fd_store(s, fd, name, do_poll); | |
488 | if (r == -EXFULL) | |
489 | return log_unit_warning_errno(UNIT(s), r, | |
490 | "Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.", | |
491 | s->n_fd_store_max); | |
492 | if (r < 0) | |
493 | return log_unit_error_errno(UNIT(s), r, "Failed to add fd to store: %m"); | |
494 | if (r > 0) | |
495 | log_unit_debug(UNIT(s), "Added fd %u (%s) to fd store.", fd, strna(name)); | |
496 | fd = -1; | |
497 | } | |
498 | ||
499 | return 0; | |
500 | } | |
501 | ||
502 | static void service_remove_fd_store(Service *s, const char *name) { | |
503 | ServiceFDStore *fs, *n; | |
504 | ||
505 | assert(s); | |
506 | assert(name); | |
507 | ||
508 | LIST_FOREACH_SAFE(fd_store, fs, n, s->fd_store) { | |
509 | if (!streq(fs->fdname, name)) | |
510 | continue; | |
511 | ||
512 | log_unit_debug(UNIT(s), "Got explicit request to remove fd %i (%s), closing.", fs->fd, name); | |
513 | service_fd_store_unlink(fs); | |
514 | } | |
515 | } | |
516 | ||
517 | static usec_t service_running_timeout(Service *s) { | |
518 | usec_t delta = 0; | |
519 | ||
520 | assert(s); | |
521 | ||
522 | if (s->runtime_rand_extra_usec != 0) { | |
523 | delta = random_u64_range(s->runtime_rand_extra_usec); | |
524 | log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC)); | |
525 | } | |
526 | ||
527 | return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic, | |
528 | s->runtime_max_usec), | |
529 | delta); | |
530 | } | |
531 | ||
532 | static int service_arm_timer(Service *s, usec_t usec) { | |
533 | int r; | |
534 | ||
535 | assert(s); | |
536 | ||
537 | if (s->timer_event_source) { | |
538 | r = sd_event_source_set_time(s->timer_event_source, usec); | |
539 | if (r < 0) | |
540 | return r; | |
541 | ||
542 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
543 | } | |
544 | ||
545 | if (usec == USEC_INFINITY) | |
546 | return 0; | |
547 | ||
548 | r = sd_event_add_time( | |
549 | UNIT(s)->manager->event, | |
550 | &s->timer_event_source, | |
551 | CLOCK_MONOTONIC, | |
552 | usec, 0, | |
553 | service_dispatch_timer, s); | |
554 | if (r < 0) | |
555 | return r; | |
556 | ||
557 | (void) sd_event_source_set_description(s->timer_event_source, "service-timer"); | |
558 | ||
559 | return 0; | |
560 | } | |
561 | ||
562 | static int service_verify(Service *s) { | |
563 | assert(s); | |
564 | assert(UNIT(s)->load_state == UNIT_LOADED); | |
565 | ||
566 | for (ServiceExecCommand c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { | |
567 | ExecCommand *command; | |
568 | ||
569 | LIST_FOREACH(command, command, s->exec_command[c]) { | |
570 | if (!path_is_absolute(command->path) && !filename_is_valid(command->path)) | |
571 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), | |
572 | "Service %s= binary path \"%s\" is neither a valid executable name nor an absolute path. Refusing.", | |
573 | command->path, | |
574 | service_exec_command_to_string(c)); | |
575 | if (strv_isempty(command->argv)) | |
576 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), | |
577 | "Service has an empty argv in %s=. Refusing.", | |
578 | service_exec_command_to_string(c)); | |
579 | } | |
580 | } | |
581 | ||
582 | if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP] && | |
583 | UNIT(s)->success_action == EMERGENCY_ACTION_NONE) | |
584 | /* FailureAction= only makes sense if one of the start or stop commands is specified. | |
585 | * SuccessAction= will be executed unconditionally if no commands are specified. Hence, | |
586 | * either a command or SuccessAction= are required. */ | |
587 | ||
588 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing."); | |
589 | ||
590 | if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) | |
591 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); | |
592 | ||
593 | if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START] && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) | |
594 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= and no SuccessAction= settings and does not have RemainAfterExit=yes set. Refusing."); | |
595 | ||
596 | if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) | |
597 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); | |
598 | ||
599 | if (s->type == SERVICE_ONESHOT && | |
600 | !IN_SET(s->restart, SERVICE_RESTART_NO, SERVICE_RESTART_ON_FAILURE, SERVICE_RESTART_ON_ABNORMAL, SERVICE_RESTART_ON_WATCHDOG, SERVICE_RESTART_ON_ABORT)) | |
601 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has Restart= set to either always or on-success, which isn't allowed for Type=oneshot services. Refusing."); | |
602 | ||
603 | if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) | |
604 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing."); | |
605 | ||
606 | if (s->type == SERVICE_ONESHOT && s->exit_type == SERVICE_EXIT_CGROUP) | |
607 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has ExitType=cgroup set, which isn't allowed for Type=oneshot services. Refusing."); | |
608 | ||
609 | if (s->type == SERVICE_DBUS && !s->bus_name) | |
610 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing."); | |
611 | ||
612 | if (s->exec_context.pam_name && !IN_SET(s->kill_context.kill_mode, KILL_CONTROL_GROUP, KILL_MIXED)) | |
613 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing."); | |
614 | ||
615 | if (s->usb_function_descriptors && !s->usb_function_strings) | |
616 | log_unit_warning(UNIT(s), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring."); | |
617 | ||
618 | if (!s->usb_function_descriptors && s->usb_function_strings) | |
619 | log_unit_warning(UNIT(s), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring."); | |
620 | ||
621 | if (s->runtime_max_usec != USEC_INFINITY && s->type == SERVICE_ONESHOT) | |
622 | log_unit_warning(UNIT(s), "RuntimeMaxSec= has no effect in combination with Type=oneshot. Ignoring."); | |
623 | ||
624 | if (s->runtime_max_usec == USEC_INFINITY && s->runtime_rand_extra_usec != 0) | |
625 | log_unit_warning(UNIT(s), "Service has RuntimeRandomizedExtraSec= setting, but no RuntimeMaxSec=. Ignoring."); | |
626 | ||
627 | return 0; | |
628 | } | |
629 | ||
630 | static int service_add_default_dependencies(Service *s) { | |
631 | int r; | |
632 | ||
633 | assert(s); | |
634 | ||
635 | if (!UNIT(s)->default_dependencies) | |
636 | return 0; | |
637 | ||
638 | /* Add a number of automatic dependencies useful for the | |
639 | * majority of services. */ | |
640 | ||
641 | if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) { | |
642 | /* First, pull in the really early boot stuff, and | |
643 | * require it, so that we fail if we can't acquire | |
644 | * it. */ | |
645 | ||
646 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
647 | if (r < 0) | |
648 | return r; | |
649 | } else { | |
650 | ||
651 | /* In the --user instance there's no sysinit.target, | |
652 | * in that case require basic.target instead. */ | |
653 | ||
654 | r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
655 | if (r < 0) | |
656 | return r; | |
657 | } | |
658 | ||
659 | /* Second, if the rest of the base system is in the same | |
660 | * transaction, order us after it, but do not pull it in or | |
661 | * even require it. */ | |
662 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
663 | if (r < 0) | |
664 | return r; | |
665 | ||
666 | /* Third, add us in for normal shutdown. */ | |
667 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); | |
668 | } | |
669 | ||
670 | static void service_fix_stdio(Service *s) { | |
671 | assert(s); | |
672 | ||
673 | /* Note that EXEC_INPUT_NULL and EXEC_OUTPUT_INHERIT play a special role here: they are both the | |
674 | * default value that is subject to automatic overriding triggered by other settings and an explicit | |
675 | * choice the user can make. We don't distinguish between these cases currently. */ | |
676 | ||
677 | if (s->exec_context.std_input == EXEC_INPUT_NULL && | |
678 | s->exec_context.stdin_data_size > 0) | |
679 | s->exec_context.std_input = EXEC_INPUT_DATA; | |
680 | ||
681 | if (IN_SET(s->exec_context.std_input, | |
682 | EXEC_INPUT_TTY, | |
683 | EXEC_INPUT_TTY_FORCE, | |
684 | EXEC_INPUT_TTY_FAIL, | |
685 | EXEC_INPUT_SOCKET, | |
686 | EXEC_INPUT_NAMED_FD)) | |
687 | return; | |
688 | ||
689 | /* We assume these listed inputs refer to bidirectional streams, and hence duplicating them from | |
690 | * stdin to stdout/stderr makes sense and hence leaving EXEC_OUTPUT_INHERIT in place makes sense, | |
691 | * too. Outputs such as regular files or sealed data memfds otoh don't really make sense to be | |
692 | * duplicated for both input and output at the same time (since they then would cause a feedback | |
693 | * loop), hence override EXEC_OUTPUT_INHERIT with the default stderr/stdout setting. */ | |
694 | ||
695 | if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT && | |
696 | s->exec_context.std_output == EXEC_OUTPUT_INHERIT) | |
697 | s->exec_context.std_error = UNIT(s)->manager->default_std_error; | |
698 | ||
699 | if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT) | |
700 | s->exec_context.std_output = UNIT(s)->manager->default_std_output; | |
701 | } | |
702 | ||
703 | static int service_setup_bus_name(Service *s) { | |
704 | int r; | |
705 | ||
706 | assert(s); | |
707 | ||
708 | /* If s->bus_name is not set, then the unit will be refused by service_verify() later. */ | |
709 | if (s->type != SERVICE_DBUS || !s->bus_name) | |
710 | return 0; | |
711 | ||
712 | r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE); | |
713 | if (r < 0) | |
714 | return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m"); | |
715 | ||
716 | /* We always want to be ordered against dbus.socket if both are in the transaction. */ | |
717 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE); | |
718 | if (r < 0) | |
719 | return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m"); | |
720 | ||
721 | r = unit_watch_bus_name(UNIT(s), s->bus_name); | |
722 | if (r == -EEXIST) | |
723 | return log_unit_error_errno(UNIT(s), r, "Two services allocated for the same bus name %s, refusing operation.", s->bus_name); | |
724 | if (r < 0) | |
725 | return log_unit_error_errno(UNIT(s), r, "Cannot watch bus name %s: %m", s->bus_name); | |
726 | ||
727 | return 0; | |
728 | } | |
729 | ||
730 | static int service_add_extras(Service *s) { | |
731 | int r; | |
732 | ||
733 | assert(s); | |
734 | ||
735 | if (s->type == _SERVICE_TYPE_INVALID) { | |
736 | /* Figure out a type automatically */ | |
737 | if (s->bus_name) | |
738 | s->type = SERVICE_DBUS; | |
739 | else if (s->exec_command[SERVICE_EXEC_START]) | |
740 | s->type = SERVICE_SIMPLE; | |
741 | else | |
742 | s->type = SERVICE_ONESHOT; | |
743 | } | |
744 | ||
745 | /* Oneshot services have disabled start timeout by default */ | |
746 | if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined) | |
747 | s->timeout_start_usec = USEC_INFINITY; | |
748 | ||
749 | service_fix_stdio(s); | |
750 | ||
751 | r = unit_patch_contexts(UNIT(s)); | |
752 | if (r < 0) | |
753 | return r; | |
754 | ||
755 | r = unit_add_exec_dependencies(UNIT(s), &s->exec_context); | |
756 | if (r < 0) | |
757 | return r; | |
758 | ||
759 | r = unit_set_default_slice(UNIT(s)); | |
760 | if (r < 0) | |
761 | return r; | |
762 | ||
763 | /* If the service needs the notify socket, let's enable it automatically. */ | |
764 | if (s->notify_access == NOTIFY_NONE && | |
765 | (s->type == SERVICE_NOTIFY || s->watchdog_usec > 0 || s->n_fd_store_max > 0)) | |
766 | s->notify_access = NOTIFY_MAIN; | |
767 | ||
768 | /* If no OOM policy was explicitly set, then default to the configure default OOM policy. Except when | |
769 | * delegation is on, in that case it we assume the payload knows better what to do and can process | |
770 | * things in a more focused way. */ | |
771 | if (s->oom_policy < 0) | |
772 | s->oom_policy = s->cgroup_context.delegate ? OOM_CONTINUE : UNIT(s)->manager->default_oom_policy; | |
773 | ||
774 | /* Let the kernel do the killing if that's requested. */ | |
775 | s->cgroup_context.memory_oom_group = s->oom_policy == OOM_KILL; | |
776 | ||
777 | r = service_add_default_dependencies(s); | |
778 | if (r < 0) | |
779 | return r; | |
780 | ||
781 | r = service_setup_bus_name(s); | |
782 | if (r < 0) | |
783 | return r; | |
784 | ||
785 | return 0; | |
786 | } | |
787 | ||
788 | static int service_load(Unit *u) { | |
789 | Service *s = SERVICE(u); | |
790 | int r; | |
791 | ||
792 | r = unit_load_fragment_and_dropin(u, true); | |
793 | if (r < 0) | |
794 | return r; | |
795 | ||
796 | if (u->load_state != UNIT_LOADED) | |
797 | return 0; | |
798 | ||
799 | /* This is a new unit? Then let's add in some extras */ | |
800 | r = service_add_extras(s); | |
801 | if (r < 0) | |
802 | return r; | |
803 | ||
804 | return service_verify(s); | |
805 | } | |
806 | ||
807 | static void service_dump(Unit *u, FILE *f, const char *prefix) { | |
808 | ServiceExecCommand c; | |
809 | Service *s = SERVICE(u); | |
810 | const char *prefix2; | |
811 | ||
812 | assert(s); | |
813 | ||
814 | prefix = strempty(prefix); | |
815 | prefix2 = strjoina(prefix, "\t"); | |
816 | ||
817 | fprintf(f, | |
818 | "%sService State: %s\n" | |
819 | "%sResult: %s\n" | |
820 | "%sReload Result: %s\n" | |
821 | "%sClean Result: %s\n" | |
822 | "%sPermissionsStartOnly: %s\n" | |
823 | "%sRootDirectoryStartOnly: %s\n" | |
824 | "%sRemainAfterExit: %s\n" | |
825 | "%sGuessMainPID: %s\n" | |
826 | "%sType: %s\n" | |
827 | "%sRestart: %s\n" | |
828 | "%sNotifyAccess: %s\n" | |
829 | "%sNotifyState: %s\n" | |
830 | "%sOOMPolicy: %s\n", | |
831 | prefix, service_state_to_string(s->state), | |
832 | prefix, service_result_to_string(s->result), | |
833 | prefix, service_result_to_string(s->reload_result), | |
834 | prefix, service_result_to_string(s->clean_result), | |
835 | prefix, yes_no(s->permissions_start_only), | |
836 | prefix, yes_no(s->root_directory_start_only), | |
837 | prefix, yes_no(s->remain_after_exit), | |
838 | prefix, yes_no(s->guess_main_pid), | |
839 | prefix, service_type_to_string(s->type), | |
840 | prefix, service_restart_to_string(s->restart), | |
841 | prefix, notify_access_to_string(s->notify_access), | |
842 | prefix, notify_state_to_string(s->notify_state), | |
843 | prefix, oom_policy_to_string(s->oom_policy)); | |
844 | ||
845 | if (s->control_pid > 0) | |
846 | fprintf(f, | |
847 | "%sControl PID: "PID_FMT"\n", | |
848 | prefix, s->control_pid); | |
849 | ||
850 | if (s->main_pid > 0) | |
851 | fprintf(f, | |
852 | "%sMain PID: "PID_FMT"\n" | |
853 | "%sMain PID Known: %s\n" | |
854 | "%sMain PID Alien: %s\n", | |
855 | prefix, s->main_pid, | |
856 | prefix, yes_no(s->main_pid_known), | |
857 | prefix, yes_no(s->main_pid_alien)); | |
858 | ||
859 | if (s->pid_file) | |
860 | fprintf(f, | |
861 | "%sPIDFile: %s\n", | |
862 | prefix, s->pid_file); | |
863 | ||
864 | if (s->bus_name) | |
865 | fprintf(f, | |
866 | "%sBusName: %s\n" | |
867 | "%sBus Name Good: %s\n", | |
868 | prefix, s->bus_name, | |
869 | prefix, yes_no(s->bus_name_good)); | |
870 | ||
871 | if (UNIT_ISSET(s->accept_socket)) | |
872 | fprintf(f, | |
873 | "%sAccept Socket: %s\n", | |
874 | prefix, UNIT_DEREF(s->accept_socket)->id); | |
875 | ||
876 | fprintf(f, | |
877 | "%sRestartSec: %s\n" | |
878 | "%sTimeoutStartSec: %s\n" | |
879 | "%sTimeoutStopSec: %s\n" | |
880 | "%sTimeoutStartFailureMode: %s\n" | |
881 | "%sTimeoutStopFailureMode: %s\n", | |
882 | prefix, FORMAT_TIMESPAN(s->restart_usec, USEC_PER_SEC), | |
883 | prefix, FORMAT_TIMESPAN(s->timeout_start_usec, USEC_PER_SEC), | |
884 | prefix, FORMAT_TIMESPAN(s->timeout_stop_usec, USEC_PER_SEC), | |
885 | prefix, service_timeout_failure_mode_to_string(s->timeout_start_failure_mode), | |
886 | prefix, service_timeout_failure_mode_to_string(s->timeout_stop_failure_mode)); | |
887 | ||
888 | if (s->timeout_abort_set) | |
889 | fprintf(f, | |
890 | "%sTimeoutAbortSec: %s\n", | |
891 | prefix, FORMAT_TIMESPAN(s->timeout_abort_usec, USEC_PER_SEC)); | |
892 | ||
893 | fprintf(f, | |
894 | "%sRuntimeMaxSec: %s\n" | |
895 | "%sRuntimeRandomizedExtraSec: %s\n" | |
896 | "%sWatchdogSec: %s\n", | |
897 | prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC), | |
898 | prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC), | |
899 | prefix, FORMAT_TIMESPAN(s->watchdog_usec, USEC_PER_SEC)); | |
900 | ||
901 | kill_context_dump(&s->kill_context, f, prefix); | |
902 | exec_context_dump(&s->exec_context, f, prefix); | |
903 | ||
904 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { | |
905 | ||
906 | if (!s->exec_command[c]) | |
907 | continue; | |
908 | ||
909 | fprintf(f, "%s-> %s:\n", | |
910 | prefix, service_exec_command_to_string(c)); | |
911 | ||
912 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
913 | } | |
914 | ||
915 | if (s->status_text) | |
916 | fprintf(f, "%sStatus Text: %s\n", | |
917 | prefix, s->status_text); | |
918 | ||
919 | if (s->n_fd_store_max > 0) | |
920 | fprintf(f, | |
921 | "%sFile Descriptor Store Max: %u\n" | |
922 | "%sFile Descriptor Store Current: %zu\n", | |
923 | prefix, s->n_fd_store_max, | |
924 | prefix, s->n_fd_store); | |
925 | ||
926 | cgroup_context_dump(UNIT(s), f, prefix); | |
927 | } | |
928 | ||
929 | static int service_is_suitable_main_pid(Service *s, pid_t pid, int prio) { | |
930 | Unit *owner; | |
931 | ||
932 | assert(s); | |
933 | assert(pid_is_valid(pid)); | |
934 | ||
935 | /* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the | |
936 | * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is | |
937 | * good */ | |
938 | ||
939 | if (pid == getpid_cached() || pid == 1) | |
940 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the manager, refusing.", pid); | |
941 | ||
942 | if (pid == s->control_pid) | |
943 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the control process, refusing.", pid); | |
944 | ||
945 | if (!pid_is_alive(pid)) | |
946 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(ESRCH), "New main PID "PID_FMT" does not exist or is a zombie.", pid); | |
947 | ||
948 | owner = manager_get_unit_by_pid(UNIT(s)->manager, pid); | |
949 | if (owner == UNIT(s)) { | |
950 | log_unit_debug(UNIT(s), "New main PID "PID_FMT" belongs to service, we are happy.", pid); | |
951 | return 1; /* Yay, it's definitely a good PID */ | |
952 | } | |
953 | ||
954 | return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */ | |
955 | } | |
956 | ||
957 | static int service_load_pid_file(Service *s, bool may_warn) { | |
958 | bool questionable_pid_file = false; | |
959 | _cleanup_free_ char *k = NULL; | |
960 | _cleanup_close_ int fd = -1; | |
961 | int r, prio; | |
962 | pid_t pid; | |
963 | ||
964 | assert(s); | |
965 | ||
966 | if (!s->pid_file) | |
967 | return -ENOENT; | |
968 | ||
969 | prio = may_warn ? LOG_INFO : LOG_DEBUG; | |
970 | ||
971 | r = chase_symlinks(s->pid_file, NULL, CHASE_SAFE, NULL, &fd); | |
972 | if (r == -ENOLINK) { | |
973 | log_unit_debug_errno(UNIT(s), r, | |
974 | "Potentially unsafe symlink chain, will now retry with relaxed checks: %s", s->pid_file); | |
975 | ||
976 | questionable_pid_file = true; | |
977 | ||
978 | r = chase_symlinks(s->pid_file, NULL, 0, NULL, &fd); | |
979 | } | |
980 | if (r < 0) | |
981 | return log_unit_full_errno(UNIT(s), prio, fd, | |
982 | "Can't open PID file %s (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state)); | |
983 | ||
984 | /* Let's read the PID file now that we chased it down. But we need to convert the O_PATH fd | |
985 | * chase_symlinks() returned us into a proper fd first. */ | |
986 | r = read_one_line_file(FORMAT_PROC_FD_PATH(fd), &k); | |
987 | if (r < 0) | |
988 | return log_unit_error_errno(UNIT(s), r, | |
989 | "Can't convert PID files %s O_PATH file descriptor to proper file descriptor: %m", | |
990 | s->pid_file); | |
991 | ||
992 | r = parse_pid(k, &pid); | |
993 | if (r < 0) | |
994 | return log_unit_full_errno(UNIT(s), prio, r, "Failed to parse PID from file %s: %m", s->pid_file); | |
995 | ||
996 | if (s->main_pid_known && pid == s->main_pid) | |
997 | return 0; | |
998 | ||
999 | r = service_is_suitable_main_pid(s, pid, prio); | |
1000 | if (r < 0) | |
1001 | return r; | |
1002 | if (r == 0) { | |
1003 | struct stat st; | |
1004 | ||
1005 | if (questionable_pid_file) | |
1006 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), | |
1007 | "Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s->pid_file); | |
1008 | ||
1009 | /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */ | |
1010 | ||
1011 | if (fstat(fd, &st) < 0) | |
1012 | return log_unit_error_errno(UNIT(s), errno, "Failed to fstat() PID file O_PATH fd: %m"); | |
1013 | ||
1014 | if (st.st_uid != 0) | |
1015 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), | |
1016 | "New main PID "PID_FMT" does not belong to service, and PID file is not owned by root. Refusing.", pid); | |
1017 | ||
1018 | log_unit_debug(UNIT(s), "New main PID "PID_FMT" does not belong to service, but we'll accept it since PID file is owned by root.", pid); | |
1019 | } | |
1020 | ||
1021 | if (s->main_pid_known) { | |
1022 | log_unit_debug(UNIT(s), "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid); | |
1023 | ||
1024 | service_unwatch_main_pid(s); | |
1025 | s->main_pid_known = false; | |
1026 | } else | |
1027 | log_unit_debug(UNIT(s), "Main PID loaded: "PID_FMT, pid); | |
1028 | ||
1029 | r = service_set_main_pid(s, pid); | |
1030 | if (r < 0) | |
1031 | return r; | |
1032 | ||
1033 | r = unit_watch_pid(UNIT(s), pid, false); | |
1034 | if (r < 0) /* FIXME: we need to do something here */ | |
1035 | return log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" for service: %m", pid); | |
1036 | ||
1037 | return 1; | |
1038 | } | |
1039 | ||
1040 | static void service_search_main_pid(Service *s) { | |
1041 | pid_t pid = 0; | |
1042 | int r; | |
1043 | ||
1044 | assert(s); | |
1045 | ||
1046 | /* If we know it anyway, don't ever fall back to unreliable | |
1047 | * heuristics */ | |
1048 | if (s->main_pid_known) | |
1049 | return; | |
1050 | ||
1051 | if (!s->guess_main_pid) | |
1052 | return; | |
1053 | ||
1054 | assert(s->main_pid <= 0); | |
1055 | ||
1056 | if (unit_search_main_pid(UNIT(s), &pid) < 0) | |
1057 | return; | |
1058 | ||
1059 | log_unit_debug(UNIT(s), "Main PID guessed: "PID_FMT, pid); | |
1060 | if (service_set_main_pid(s, pid) < 0) | |
1061 | return; | |
1062 | ||
1063 | r = unit_watch_pid(UNIT(s), pid, false); | |
1064 | if (r < 0) | |
1065 | /* FIXME: we need to do something here */ | |
1066 | log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" from: %m", pid); | |
1067 | } | |
1068 | ||
1069 | static void service_set_state(Service *s, ServiceState state) { | |
1070 | ServiceState old_state; | |
1071 | const UnitActiveState *table; | |
1072 | ||
1073 | assert(s); | |
1074 | ||
1075 | if (s->state != state) | |
1076 | bus_unit_send_pending_change_signal(UNIT(s), false); | |
1077 | ||
1078 | table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; | |
1079 | ||
1080 | old_state = s->state; | |
1081 | s->state = state; | |
1082 | ||
1083 | service_unwatch_pid_file(s); | |
1084 | ||
1085 | if (!IN_SET(state, | |
1086 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
1087 | SERVICE_RUNNING, | |
1088 | SERVICE_RELOAD, | |
1089 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1090 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, | |
1091 | SERVICE_AUTO_RESTART, | |
1092 | SERVICE_CLEANING)) | |
1093 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); | |
1094 | ||
1095 | if (!IN_SET(state, | |
1096 | SERVICE_START, SERVICE_START_POST, | |
1097 | SERVICE_RUNNING, SERVICE_RELOAD, | |
1098 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1099 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { | |
1100 | service_unwatch_main_pid(s); | |
1101 | s->main_command = NULL; | |
1102 | } | |
1103 | ||
1104 | if (!IN_SET(state, | |
1105 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
1106 | SERVICE_RELOAD, | |
1107 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1108 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, | |
1109 | SERVICE_CLEANING)) { | |
1110 | service_unwatch_control_pid(s); | |
1111 | s->control_command = NULL; | |
1112 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
1113 | } | |
1114 | ||
1115 | if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) { | |
1116 | unit_unwatch_all_pids(UNIT(s)); | |
1117 | unit_dequeue_rewatch_pids(UNIT(s)); | |
1118 | } | |
1119 | ||
1120 | if (!IN_SET(state, | |
1121 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
1122 | SERVICE_RUNNING, SERVICE_RELOAD, | |
1123 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1124 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) && | |
1125 | !(state == SERVICE_DEAD && UNIT(s)->job)) | |
1126 | service_close_socket_fd(s); | |
1127 | ||
1128 | if (state != SERVICE_START) | |
1129 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
1130 | ||
1131 | if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) | |
1132 | service_stop_watchdog(s); | |
1133 | ||
1134 | /* For the inactive states unit_notify() will trim the cgroup, | |
1135 | * but for exit we have to do that ourselves... */ | |
1136 | if (state == SERVICE_EXITED && !MANAGER_IS_RELOADING(UNIT(s)->manager)) | |
1137 | unit_prune_cgroup(UNIT(s)); | |
1138 | ||
1139 | if (old_state != state) | |
1140 | log_unit_debug(UNIT(s), "Changed %s -> %s", service_state_to_string(old_state), service_state_to_string(state)); | |
1141 | ||
1142 | unit_notify(UNIT(s), table[old_state], table[state], | |
1143 | (s->reload_result == SERVICE_SUCCESS ? 0 : UNIT_NOTIFY_RELOAD_FAILURE) | | |
1144 | (s->will_auto_restart ? UNIT_NOTIFY_WILL_AUTO_RESTART : 0)); | |
1145 | } | |
1146 | ||
1147 | static usec_t service_coldplug_timeout(Service *s) { | |
1148 | assert(s); | |
1149 | ||
1150 | switch (s->deserialized_state) { | |
1151 | ||
1152 | case SERVICE_CONDITION: | |
1153 | case SERVICE_START_PRE: | |
1154 | case SERVICE_START: | |
1155 | case SERVICE_START_POST: | |
1156 | case SERVICE_RELOAD: | |
1157 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_start_usec); | |
1158 | ||
1159 | case SERVICE_RUNNING: | |
1160 | return service_running_timeout(s); | |
1161 | ||
1162 | case SERVICE_STOP: | |
1163 | case SERVICE_STOP_SIGTERM: | |
1164 | case SERVICE_STOP_SIGKILL: | |
1165 | case SERVICE_STOP_POST: | |
1166 | case SERVICE_FINAL_SIGTERM: | |
1167 | case SERVICE_FINAL_SIGKILL: | |
1168 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec); | |
1169 | ||
1170 | case SERVICE_STOP_WATCHDOG: | |
1171 | case SERVICE_FINAL_WATCHDOG: | |
1172 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, service_timeout_abort_usec(s)); | |
1173 | ||
1174 | case SERVICE_AUTO_RESTART: | |
1175 | return usec_add(UNIT(s)->inactive_enter_timestamp.monotonic, s->restart_usec); | |
1176 | ||
1177 | case SERVICE_CLEANING: | |
1178 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->exec_context.timeout_clean_usec); | |
1179 | ||
1180 | default: | |
1181 | return USEC_INFINITY; | |
1182 | } | |
1183 | } | |
1184 | ||
1185 | static int service_coldplug(Unit *u) { | |
1186 | Service *s = SERVICE(u); | |
1187 | int r; | |
1188 | ||
1189 | assert(s); | |
1190 | assert(s->state == SERVICE_DEAD); | |
1191 | ||
1192 | if (s->deserialized_state == s->state) | |
1193 | return 0; | |
1194 | ||
1195 | r = service_arm_timer(s, service_coldplug_timeout(s)); | |
1196 | if (r < 0) | |
1197 | return r; | |
1198 | ||
1199 | if (s->main_pid > 0 && | |
1200 | pid_is_unwaited(s->main_pid) && | |
1201 | (IN_SET(s->deserialized_state, | |
1202 | SERVICE_START, SERVICE_START_POST, | |
1203 | SERVICE_RUNNING, SERVICE_RELOAD, | |
1204 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1205 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) { | |
1206 | r = unit_watch_pid(UNIT(s), s->main_pid, false); | |
1207 | if (r < 0) | |
1208 | return r; | |
1209 | } | |
1210 | ||
1211 | if (s->control_pid > 0 && | |
1212 | pid_is_unwaited(s->control_pid) && | |
1213 | IN_SET(s->deserialized_state, | |
1214 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
1215 | SERVICE_RELOAD, | |
1216 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1217 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, | |
1218 | SERVICE_CLEANING)) { | |
1219 | r = unit_watch_pid(UNIT(s), s->control_pid, false); | |
1220 | if (r < 0) | |
1221 | return r; | |
1222 | } | |
1223 | ||
1224 | if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART, SERVICE_CLEANING)) { | |
1225 | (void) unit_enqueue_rewatch_pids(u); | |
1226 | (void) unit_setup_dynamic_creds(u); | |
1227 | (void) unit_setup_exec_runtime(u); | |
1228 | } | |
1229 | ||
1230 | if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) | |
1231 | service_start_watchdog(s); | |
1232 | ||
1233 | if (UNIT_ISSET(s->accept_socket)) { | |
1234 | Socket* socket = SOCKET(UNIT_DEREF(s->accept_socket)); | |
1235 | ||
1236 | if (socket->max_connections_per_source > 0) { | |
1237 | SocketPeer *peer; | |
1238 | ||
1239 | /* Make a best-effort attempt at bumping the connection count */ | |
1240 | if (socket_acquire_peer(socket, s->socket_fd, &peer) > 0) { | |
1241 | socket_peer_unref(s->socket_peer); | |
1242 | s->socket_peer = peer; | |
1243 | } | |
1244 | } | |
1245 | } | |
1246 | ||
1247 | service_set_state(s, s->deserialized_state); | |
1248 | return 0; | |
1249 | } | |
1250 | ||
1251 | static int service_collect_fds( | |
1252 | Service *s, | |
1253 | int **fds, | |
1254 | char ***fd_names, | |
1255 | size_t *n_socket_fds, | |
1256 | size_t *n_storage_fds) { | |
1257 | ||
1258 | _cleanup_strv_free_ char **rfd_names = NULL; | |
1259 | _cleanup_free_ int *rfds = NULL; | |
1260 | size_t rn_socket_fds = 0, rn_storage_fds = 0; | |
1261 | int r; | |
1262 | ||
1263 | assert(s); | |
1264 | assert(fds); | |
1265 | assert(fd_names); | |
1266 | assert(n_socket_fds); | |
1267 | assert(n_storage_fds); | |
1268 | ||
1269 | if (s->socket_fd >= 0) { | |
1270 | ||
1271 | /* Pass the per-connection socket */ | |
1272 | ||
1273 | rfds = new(int, 1); | |
1274 | if (!rfds) | |
1275 | return -ENOMEM; | |
1276 | rfds[0] = s->socket_fd; | |
1277 | ||
1278 | rfd_names = strv_new("connection"); | |
1279 | if (!rfd_names) | |
1280 | return -ENOMEM; | |
1281 | ||
1282 | rn_socket_fds = 1; | |
1283 | } else { | |
1284 | Unit *u; | |
1285 | ||
1286 | /* Pass all our configured sockets for singleton services */ | |
1287 | ||
1288 | UNIT_FOREACH_DEPENDENCY(u, UNIT(s), UNIT_ATOM_TRIGGERED_BY) { | |
1289 | _cleanup_free_ int *cfds = NULL; | |
1290 | Socket *sock; | |
1291 | int cn_fds; | |
1292 | ||
1293 | if (u->type != UNIT_SOCKET) | |
1294 | continue; | |
1295 | ||
1296 | sock = SOCKET(u); | |
1297 | ||
1298 | cn_fds = socket_collect_fds(sock, &cfds); | |
1299 | if (cn_fds < 0) | |
1300 | return cn_fds; | |
1301 | ||
1302 | if (cn_fds <= 0) | |
1303 | continue; | |
1304 | ||
1305 | if (!rfds) { | |
1306 | rfds = TAKE_PTR(cfds); | |
1307 | rn_socket_fds = cn_fds; | |
1308 | } else { | |
1309 | int *t; | |
1310 | ||
1311 | t = reallocarray(rfds, rn_socket_fds + cn_fds, sizeof(int)); | |
1312 | if (!t) | |
1313 | return -ENOMEM; | |
1314 | ||
1315 | memcpy(t + rn_socket_fds, cfds, cn_fds * sizeof(int)); | |
1316 | ||
1317 | rfds = t; | |
1318 | rn_socket_fds += cn_fds; | |
1319 | } | |
1320 | ||
1321 | r = strv_extend_n(&rfd_names, socket_fdname(sock), cn_fds); | |
1322 | if (r < 0) | |
1323 | return r; | |
1324 | } | |
1325 | } | |
1326 | ||
1327 | if (s->n_fd_store > 0) { | |
1328 | ServiceFDStore *fs; | |
1329 | size_t n_fds; | |
1330 | char **nl; | |
1331 | int *t; | |
1332 | ||
1333 | t = reallocarray(rfds, rn_socket_fds + s->n_fd_store, sizeof(int)); | |
1334 | if (!t) | |
1335 | return -ENOMEM; | |
1336 | ||
1337 | rfds = t; | |
1338 | ||
1339 | nl = reallocarray(rfd_names, rn_socket_fds + s->n_fd_store + 1, sizeof(char *)); | |
1340 | if (!nl) | |
1341 | return -ENOMEM; | |
1342 | ||
1343 | rfd_names = nl; | |
1344 | n_fds = rn_socket_fds; | |
1345 | ||
1346 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
1347 | rfds[n_fds] = fs->fd; | |
1348 | rfd_names[n_fds] = strdup(strempty(fs->fdname)); | |
1349 | if (!rfd_names[n_fds]) | |
1350 | return -ENOMEM; | |
1351 | ||
1352 | rn_storage_fds++; | |
1353 | n_fds++; | |
1354 | } | |
1355 | ||
1356 | rfd_names[n_fds] = NULL; | |
1357 | } | |
1358 | ||
1359 | *fds = TAKE_PTR(rfds); | |
1360 | *fd_names = TAKE_PTR(rfd_names); | |
1361 | *n_socket_fds = rn_socket_fds; | |
1362 | *n_storage_fds = rn_storage_fds; | |
1363 | ||
1364 | return 0; | |
1365 | } | |
1366 | ||
1367 | static int service_allocate_exec_fd_event_source( | |
1368 | Service *s, | |
1369 | int fd, | |
1370 | sd_event_source **ret_event_source) { | |
1371 | ||
1372 | _cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL; | |
1373 | int r; | |
1374 | ||
1375 | assert(s); | |
1376 | assert(fd >= 0); | |
1377 | assert(ret_event_source); | |
1378 | ||
1379 | r = sd_event_add_io(UNIT(s)->manager->event, &source, fd, 0, service_dispatch_exec_io, s); | |
1380 | if (r < 0) | |
1381 | return log_unit_error_errno(UNIT(s), r, "Failed to allocate exec_fd event source: %m"); | |
1382 | ||
1383 | /* This is a bit lower priority than SIGCHLD, as that carries a lot more interesting failure information */ | |
1384 | ||
1385 | r = sd_event_source_set_priority(source, SD_EVENT_PRIORITY_NORMAL-3); | |
1386 | if (r < 0) | |
1387 | return log_unit_error_errno(UNIT(s), r, "Failed to adjust priority of exec_fd event source: %m"); | |
1388 | ||
1389 | (void) sd_event_source_set_description(source, "service exec_fd"); | |
1390 | ||
1391 | r = sd_event_source_set_io_fd_own(source, true); | |
1392 | if (r < 0) | |
1393 | return log_unit_error_errno(UNIT(s), r, "Failed to pass ownership of fd to event source: %m"); | |
1394 | ||
1395 | *ret_event_source = TAKE_PTR(source); | |
1396 | return 0; | |
1397 | } | |
1398 | ||
1399 | static int service_allocate_exec_fd( | |
1400 | Service *s, | |
1401 | sd_event_source **ret_event_source, | |
1402 | int *ret_exec_fd) { | |
1403 | ||
1404 | _cleanup_close_pair_ int p[] = { -1, -1 }; | |
1405 | int r; | |
1406 | ||
1407 | assert(s); | |
1408 | assert(ret_event_source); | |
1409 | assert(ret_exec_fd); | |
1410 | ||
1411 | if (pipe2(p, O_CLOEXEC|O_NONBLOCK) < 0) | |
1412 | return log_unit_error_errno(UNIT(s), errno, "Failed to allocate exec_fd pipe: %m"); | |
1413 | ||
1414 | r = service_allocate_exec_fd_event_source(s, p[0], ret_event_source); | |
1415 | if (r < 0) | |
1416 | return r; | |
1417 | ||
1418 | TAKE_FD(p[0]); | |
1419 | *ret_exec_fd = TAKE_FD(p[1]); | |
1420 | ||
1421 | return 0; | |
1422 | } | |
1423 | ||
1424 | static bool service_exec_needs_notify_socket(Service *s, ExecFlags flags) { | |
1425 | assert(s); | |
1426 | ||
1427 | /* Notifications are accepted depending on the process and | |
1428 | * the access setting of the service: | |
1429 | * process: \ access: NONE MAIN EXEC ALL | |
1430 | * main no yes yes yes | |
1431 | * control no no yes yes | |
1432 | * other (forked) no no no yes */ | |
1433 | ||
1434 | if (flags & EXEC_IS_CONTROL) | |
1435 | /* A control process */ | |
1436 | return IN_SET(s->notify_access, NOTIFY_EXEC, NOTIFY_ALL); | |
1437 | ||
1438 | /* We only spawn main processes and control processes, so any | |
1439 | * process that is not a control process is a main process */ | |
1440 | return s->notify_access != NOTIFY_NONE; | |
1441 | } | |
1442 | ||
1443 | static int service_create_monitor_md_env(Job *j, char **ret) { | |
1444 | _cleanup_free_ char *var = NULL; | |
1445 | const char *list_delim = ";"; | |
1446 | bool first = true; | |
1447 | Unit *tu; | |
1448 | ||
1449 | assert(j); | |
1450 | assert(ret); | |
1451 | ||
1452 | /* Create an environment variable 'MONITOR_METADATA', if creation is successful | |
1453 | * a pointer to it is returned via ret. | |
1454 | * | |
1455 | * This variable contains a space separated set of fields which relate to | |
1456 | * the service(s) which triggered job 'j'. Job 'j' is the JOB_START job for | |
1457 | * an OnFailure= or OnSuccess= dependency. Format of the MONITOR_METADATA | |
1458 | * variable is as follows: | |
1459 | * | |
1460 | * MONITOR_METADATA="SERVICE_RESULT=<result-string0>,EXIT_CODE=<exit-code0>,EXIT_STATUS=<exit-status0>, | |
1461 | * INVOCATION_ID=<id>,UNIT=<triggering-unit0.service>; | |
1462 | * SERVICE_RESULT=<result-stringN>,EXIT_CODE=<exit-codeN>,EXIT_STATUS=<exit-statusN>, | |
1463 | * INVOCATION_ID=<id>,UNIT=<triggering-unitN.service>" | |
1464 | * | |
1465 | * Multiple results may be passed as in the above example if jobs are merged, i.e. | |
1466 | * some services a and b contain an OnFailure= or OnSuccess= dependency on the same | |
1467 | * service. | |
1468 | * | |
1469 | * For example: | |
1470 | * | |
1471 | * MONITOR_METADATA="SERVICE_RESULT=exit-code,EXIT_CODE=exited,EXIT_STATUS=1,INVOCATION_ID=02dd868af2f344b18edaf74b618b2f90,UNIT=failure.service; | |
1472 | * SERVICE_RESULT=exit-code,EXIT_CODE=exited,EXIT_STATUS=1,INVOCATION_ID=80cb228bd7344f77a090eda603a3cfe2,UNIT=failure2.service" | |
1473 | */ | |
1474 | ||
1475 | LIST_FOREACH(triggered_by, tu, j->triggered_by) { | |
1476 | Service *env_source = SERVICE(tu); | |
1477 | int r; | |
1478 | ||
1479 | if (!env_source) | |
1480 | continue; | |
1481 | ||
1482 | if (first) { | |
1483 | /* Add the environment variable name first. */ | |
1484 | r = strextendf(&var, "MONITOR_METADATA="); | |
1485 | if (r < 0) | |
1486 | return r; | |
1487 | ||
1488 | } | |
1489 | ||
1490 | r = strextendf(&var, "%sSERVICE_RESULT=%s", | |
1491 | !first ? list_delim : "", service_result_to_string(env_source->result)); | |
1492 | if (r < 0) | |
1493 | return r; | |
1494 | ||
1495 | first = false; | |
1496 | ||
1497 | if (env_source->main_exec_status.pid > 0 && | |
1498 | dual_timestamp_is_set(&env_source->main_exec_status.exit_timestamp)) { | |
1499 | r = strextendf(&var, ",EXIT_CODE=%s", | |
1500 | sigchld_code_to_string(env_source->main_exec_status.code)); | |
1501 | if (r < 0) | |
1502 | return r; | |
1503 | ||
1504 | if (env_source->main_exec_status.code == CLD_EXITED) | |
1505 | r = strextendf(&var, ",EXIT_STATUS=%i", | |
1506 | env_source->main_exec_status.status); | |
1507 | else | |
1508 | r = strextendf(&var, ",EXIT_STATUS=%s", | |
1509 | signal_to_string(env_source->main_exec_status.status)); | |
1510 | if (r < 0) | |
1511 | return r; | |
1512 | } | |
1513 | ||
1514 | if (!sd_id128_is_null(UNIT(env_source)->invocation_id)) { | |
1515 | r = strextendf(&var, ",INVOCATION_ID=" SD_ID128_FORMAT_STR, | |
1516 | SD_ID128_FORMAT_VAL(UNIT(env_source)->invocation_id)); | |
1517 | if (r < 0) | |
1518 | return r; | |
1519 | } | |
1520 | ||
1521 | r = strextendf(&var, ",UNIT=%s", UNIT(env_source)->id); | |
1522 | if (r < 0) | |
1523 | return r; | |
1524 | } | |
1525 | ||
1526 | *ret = TAKE_PTR(var); | |
1527 | return 0; | |
1528 | } | |
1529 | ||
1530 | static int service_spawn( | |
1531 | Service *s, | |
1532 | ExecCommand *c, | |
1533 | usec_t timeout, | |
1534 | ExecFlags flags, | |
1535 | pid_t *ret_pid) { | |
1536 | ||
1537 | _cleanup_(exec_params_clear) ExecParameters exec_params = { | |
1538 | .flags = flags, | |
1539 | .stdin_fd = -1, | |
1540 | .stdout_fd = -1, | |
1541 | .stderr_fd = -1, | |
1542 | .exec_fd = -1, | |
1543 | }; | |
1544 | _cleanup_(sd_event_source_unrefp) sd_event_source *exec_fd_source = NULL; | |
1545 | _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL; | |
1546 | size_t n_env = 0; | |
1547 | pid_t pid; | |
1548 | int r; | |
1549 | ||
1550 | assert(s); | |
1551 | assert(c); | |
1552 | assert(ret_pid); | |
1553 | ||
1554 | r = unit_prepare_exec(UNIT(s)); /* This realizes the cgroup, among other things */ | |
1555 | if (r < 0) | |
1556 | return r; | |
1557 | ||
1558 | assert(!s->exec_fd_event_source); | |
1559 | ||
1560 | if (flags & EXEC_IS_CONTROL) { | |
1561 | /* If this is a control process, mask the permissions/chroot application if this is requested. */ | |
1562 | if (s->permissions_start_only) | |
1563 | exec_params.flags &= ~EXEC_APPLY_SANDBOXING; | |
1564 | if (s->root_directory_start_only) | |
1565 | exec_params.flags &= ~EXEC_APPLY_CHROOT; | |
1566 | } | |
1567 | ||
1568 | if ((flags & EXEC_PASS_FDS) || | |
1569 | s->exec_context.std_input == EXEC_INPUT_SOCKET || | |
1570 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1571 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1572 | ||
1573 | r = service_collect_fds(s, | |
1574 | &exec_params.fds, | |
1575 | &exec_params.fd_names, | |
1576 | &exec_params.n_socket_fds, | |
1577 | &exec_params.n_storage_fds); | |
1578 | if (r < 0) | |
1579 | return r; | |
1580 | ||
1581 | log_unit_debug(UNIT(s), "Passing %zu fds to service", exec_params.n_socket_fds + exec_params.n_storage_fds); | |
1582 | } | |
1583 | ||
1584 | if (!FLAGS_SET(flags, EXEC_IS_CONTROL) && s->type == SERVICE_EXEC) { | |
1585 | r = service_allocate_exec_fd(s, &exec_fd_source, &exec_params.exec_fd); | |
1586 | if (r < 0) | |
1587 | return r; | |
1588 | } | |
1589 | ||
1590 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), timeout)); | |
1591 | if (r < 0) | |
1592 | return r; | |
1593 | ||
1594 | our_env = new0(char*, 10); | |
1595 | if (!our_env) | |
1596 | return -ENOMEM; | |
1597 | ||
1598 | if (service_exec_needs_notify_socket(s, flags)) { | |
1599 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) | |
1600 | return -ENOMEM; | |
1601 | ||
1602 | exec_params.notify_socket = UNIT(s)->manager->notify_socket; | |
1603 | } | |
1604 | ||
1605 | if (s->main_pid > 0) | |
1606 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) | |
1607 | return -ENOMEM; | |
1608 | ||
1609 | if (MANAGER_IS_USER(UNIT(s)->manager)) | |
1610 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid_cached()) < 0) | |
1611 | return -ENOMEM; | |
1612 | ||
1613 | if (s->pid_file) | |
1614 | if (asprintf(our_env + n_env++, "PIDFILE=%s", s->pid_file) < 0) | |
1615 | return -ENOMEM; | |
1616 | ||
1617 | if (s->socket_fd >= 0) { | |
1618 | union sockaddr_union sa; | |
1619 | socklen_t salen = sizeof(sa); | |
1620 | ||
1621 | /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something | |
1622 | * useful. Note that we do this only when we are still connected at this point in time, which we might | |
1623 | * very well not be. Hence we ignore all errors when retrieving peer information (as that might result | |
1624 | * in ENOTCONN), and just use whate we can use. */ | |
1625 | ||
1626 | if (getpeername(s->socket_fd, &sa.sa, &salen) >= 0 && | |
1627 | IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { | |
1628 | _cleanup_free_ char *addr = NULL; | |
1629 | char *t; | |
1630 | unsigned port; | |
1631 | ||
1632 | r = sockaddr_pretty(&sa.sa, salen, true, false, &addr); | |
1633 | if (r < 0) | |
1634 | return r; | |
1635 | ||
1636 | t = strjoin("REMOTE_ADDR=", addr); | |
1637 | if (!t) | |
1638 | return -ENOMEM; | |
1639 | our_env[n_env++] = t; | |
1640 | ||
1641 | r = sockaddr_port(&sa.sa, &port); | |
1642 | if (r < 0) | |
1643 | return r; | |
1644 | ||
1645 | if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) | |
1646 | return -ENOMEM; | |
1647 | our_env[n_env++] = t; | |
1648 | } | |
1649 | } | |
1650 | ||
1651 | if (flags & EXEC_SETENV_RESULT) { | |
1652 | if (asprintf(our_env + n_env++, "SERVICE_RESULT=%s", service_result_to_string(s->result)) < 0) | |
1653 | return -ENOMEM; | |
1654 | ||
1655 | if (s->main_exec_status.pid > 0 && | |
1656 | dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { | |
1657 | if (asprintf(our_env + n_env++, "EXIT_CODE=%s", sigchld_code_to_string(s->main_exec_status.code)) < 0) | |
1658 | return -ENOMEM; | |
1659 | ||
1660 | if (s->main_exec_status.code == CLD_EXITED) | |
1661 | r = asprintf(our_env + n_env++, "EXIT_STATUS=%i", s->main_exec_status.status); | |
1662 | else | |
1663 | r = asprintf(our_env + n_env++, "EXIT_STATUS=%s", signal_to_string(s->main_exec_status.status)); | |
1664 | ||
1665 | if (r < 0) | |
1666 | return -ENOMEM; | |
1667 | } | |
1668 | ||
1669 | } else if (flags & EXEC_SETENV_MONITOR_RESULT) { | |
1670 | Job *j = UNIT(s)->job; | |
1671 | if (j) { | |
1672 | r = service_create_monitor_md_env(j, our_env + n_env++); | |
1673 | if (r < 0) | |
1674 | return r; | |
1675 | } | |
1676 | } | |
1677 | ||
1678 | r = unit_set_exec_params(UNIT(s), &exec_params); | |
1679 | if (r < 0) | |
1680 | return r; | |
1681 | ||
1682 | final_env = strv_env_merge(exec_params.environment, our_env); | |
1683 | if (!final_env) | |
1684 | return -ENOMEM; | |
1685 | ||
1686 | /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */ | |
1687 | SET_FLAG(exec_params.flags, EXEC_NSS_BYPASS_BUS, | |
1688 | MANAGER_IS_SYSTEM(UNIT(s)->manager) && unit_has_name(UNIT(s), SPECIAL_DBUS_SERVICE)); | |
1689 | ||
1690 | strv_free_and_replace(exec_params.environment, final_env); | |
1691 | exec_params.watchdog_usec = service_get_watchdog_usec(s); | |
1692 | exec_params.selinux_context_net = s->socket_fd_selinux_context_net; | |
1693 | if (s->type == SERVICE_IDLE) | |
1694 | exec_params.idle_pipe = UNIT(s)->manager->idle_pipe; | |
1695 | exec_params.stdin_fd = s->stdin_fd; | |
1696 | exec_params.stdout_fd = s->stdout_fd; | |
1697 | exec_params.stderr_fd = s->stderr_fd; | |
1698 | ||
1699 | r = exec_spawn(UNIT(s), | |
1700 | c, | |
1701 | &s->exec_context, | |
1702 | &exec_params, | |
1703 | s->exec_runtime, | |
1704 | &s->dynamic_creds, | |
1705 | &pid); | |
1706 | if (r < 0) | |
1707 | return r; | |
1708 | ||
1709 | s->exec_fd_event_source = TAKE_PTR(exec_fd_source); | |
1710 | s->exec_fd_hot = false; | |
1711 | ||
1712 | r = unit_watch_pid(UNIT(s), pid, true); | |
1713 | if (r < 0) | |
1714 | return r; | |
1715 | ||
1716 | *ret_pid = pid; | |
1717 | ||
1718 | return 0; | |
1719 | } | |
1720 | ||
1721 | static int main_pid_good(Service *s) { | |
1722 | assert(s); | |
1723 | ||
1724 | /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */ | |
1725 | ||
1726 | /* If we know the pid file, then let's just check if it is | |
1727 | * still valid */ | |
1728 | if (s->main_pid_known) { | |
1729 | ||
1730 | /* If it's an alien child let's check if it is still | |
1731 | * alive ... */ | |
1732 | if (s->main_pid_alien && s->main_pid > 0) | |
1733 | return pid_is_alive(s->main_pid); | |
1734 | ||
1735 | /* .. otherwise assume we'll get a SIGCHLD for it, | |
1736 | * which we really should wait for to collect exit | |
1737 | * status and code */ | |
1738 | return s->main_pid > 0; | |
1739 | } | |
1740 | ||
1741 | /* We don't know the pid */ | |
1742 | return -EAGAIN; | |
1743 | } | |
1744 | ||
1745 | static int control_pid_good(Service *s) { | |
1746 | assert(s); | |
1747 | ||
1748 | /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to | |
1749 | * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also | |
1750 | * means: we can't figure it out. */ | |
1751 | ||
1752 | return s->control_pid > 0; | |
1753 | } | |
1754 | ||
1755 | static int cgroup_good(Service *s) { | |
1756 | int r; | |
1757 | ||
1758 | assert(s); | |
1759 | ||
1760 | /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't | |
1761 | * figure it out */ | |
1762 | ||
1763 | if (!UNIT(s)->cgroup_path) | |
1764 | return 0; | |
1765 | ||
1766 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path); | |
1767 | if (r < 0) | |
1768 | return r; | |
1769 | ||
1770 | return r == 0; | |
1771 | } | |
1772 | ||
1773 | static bool service_shall_restart(Service *s, const char **reason) { | |
1774 | assert(s); | |
1775 | ||
1776 | /* Don't restart after manual stops */ | |
1777 | if (s->forbid_restart) { | |
1778 | *reason = "manual stop"; | |
1779 | return false; | |
1780 | } | |
1781 | ||
1782 | /* Never restart if this is configured as special exception */ | |
1783 | if (exit_status_set_test(&s->restart_prevent_status, s->main_exec_status.code, s->main_exec_status.status)) { | |
1784 | *reason = "prevented by exit status"; | |
1785 | return false; | |
1786 | } | |
1787 | ||
1788 | /* Restart if the exit code/status are configured as restart triggers */ | |
1789 | if (exit_status_set_test(&s->restart_force_status, s->main_exec_status.code, s->main_exec_status.status)) { | |
1790 | *reason = "forced by exit status"; | |
1791 | return true; | |
1792 | } | |
1793 | ||
1794 | *reason = "restart setting"; | |
1795 | switch (s->restart) { | |
1796 | ||
1797 | case SERVICE_RESTART_NO: | |
1798 | return false; | |
1799 | ||
1800 | case SERVICE_RESTART_ALWAYS: | |
1801 | return s->result != SERVICE_SKIP_CONDITION; | |
1802 | ||
1803 | case SERVICE_RESTART_ON_SUCCESS: | |
1804 | return s->result == SERVICE_SUCCESS; | |
1805 | ||
1806 | case SERVICE_RESTART_ON_FAILURE: | |
1807 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_SKIP_CONDITION); | |
1808 | ||
1809 | case SERVICE_RESTART_ON_ABNORMAL: | |
1810 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE, SERVICE_SKIP_CONDITION); | |
1811 | ||
1812 | case SERVICE_RESTART_ON_WATCHDOG: | |
1813 | return s->result == SERVICE_FAILURE_WATCHDOG; | |
1814 | ||
1815 | case SERVICE_RESTART_ON_ABORT: | |
1816 | return IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP); | |
1817 | ||
1818 | default: | |
1819 | assert_not_reached(); | |
1820 | } | |
1821 | } | |
1822 | ||
1823 | static bool service_will_restart(Unit *u) { | |
1824 | Service *s = SERVICE(u); | |
1825 | ||
1826 | assert(s); | |
1827 | ||
1828 | if (s->will_auto_restart) | |
1829 | return true; | |
1830 | if (s->state == SERVICE_AUTO_RESTART) | |
1831 | return true; | |
1832 | ||
1833 | return unit_will_restart_default(u); | |
1834 | } | |
1835 | ||
1836 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { | |
1837 | ServiceState end_state; | |
1838 | int r; | |
1839 | ||
1840 | assert(s); | |
1841 | ||
1842 | /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not | |
1843 | * undo what has already been enqueued. */ | |
1844 | if (unit_stop_pending(UNIT(s))) | |
1845 | allow_restart = false; | |
1846 | ||
1847 | if (s->result == SERVICE_SUCCESS) | |
1848 | s->result = f; | |
1849 | ||
1850 | if (s->result == SERVICE_SUCCESS) { | |
1851 | unit_log_success(UNIT(s)); | |
1852 | end_state = SERVICE_DEAD; | |
1853 | } else if (s->result == SERVICE_SKIP_CONDITION) { | |
1854 | unit_log_skip(UNIT(s), service_result_to_string(s->result)); | |
1855 | end_state = SERVICE_DEAD; | |
1856 | } else { | |
1857 | unit_log_failure(UNIT(s), service_result_to_string(s->result)); | |
1858 | end_state = SERVICE_FAILED; | |
1859 | } | |
1860 | unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_stop); | |
1861 | ||
1862 | if (!allow_restart) | |
1863 | log_unit_debug(UNIT(s), "Service restart not allowed."); | |
1864 | else { | |
1865 | const char *reason; | |
1866 | bool shall_restart; | |
1867 | ||
1868 | shall_restart = service_shall_restart(s, &reason); | |
1869 | log_unit_debug(UNIT(s), "Service will %srestart (%s)", | |
1870 | shall_restart ? "" : "not ", | |
1871 | reason); | |
1872 | if (shall_restart) | |
1873 | s->will_auto_restart = true; | |
1874 | } | |
1875 | ||
1876 | /* Make sure service_release_resources() doesn't destroy our FD store, while we are changing through | |
1877 | * SERVICE_FAILED/SERVICE_DEAD before entering into SERVICE_AUTO_RESTART. */ | |
1878 | s->n_keep_fd_store ++; | |
1879 | ||
1880 | service_set_state(s, end_state); | |
1881 | ||
1882 | if (s->will_auto_restart) { | |
1883 | s->will_auto_restart = false; | |
1884 | ||
1885 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec)); | |
1886 | if (r < 0) { | |
1887 | s->n_keep_fd_store--; | |
1888 | goto fail; | |
1889 | } | |
1890 | ||
1891 | service_set_state(s, SERVICE_AUTO_RESTART); | |
1892 | } else | |
1893 | /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the | |
1894 | * user can still introspect the counter. Do so on the next start. */ | |
1895 | s->flush_n_restarts = true; | |
1896 | ||
1897 | /* The new state is in effect, let's decrease the fd store ref counter again. Let's also re-add us to the GC | |
1898 | * queue, so that the fd store is possibly gc'ed again */ | |
1899 | s->n_keep_fd_store--; | |
1900 | unit_add_to_gc_queue(UNIT(s)); | |
1901 | ||
1902 | /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */ | |
1903 | s->forbid_restart = false; | |
1904 | ||
1905 | /* We want fresh tmpdirs in case service is started again immediately */ | |
1906 | s->exec_runtime = exec_runtime_unref(s->exec_runtime, true); | |
1907 | ||
1908 | /* Also, remove the runtime directory */ | |
1909 | unit_destroy_runtime_data(UNIT(s), &s->exec_context); | |
1910 | ||
1911 | /* Get rid of the IPC bits of the user */ | |
1912 | unit_unref_uid_gid(UNIT(s), true); | |
1913 | ||
1914 | /* Release the user, and destroy it if we are the only remaining owner */ | |
1915 | dynamic_creds_destroy(&s->dynamic_creds); | |
1916 | ||
1917 | /* Try to delete the pid file. At this point it will be | |
1918 | * out-of-date, and some software might be confused by it, so | |
1919 | * let's remove it. */ | |
1920 | if (s->pid_file) | |
1921 | (void) unlink(s->pid_file); | |
1922 | ||
1923 | /* Reset TTY ownership if necessary */ | |
1924 | exec_context_revert_tty(&s->exec_context); | |
1925 | ||
1926 | return; | |
1927 | ||
1928 | fail: | |
1929 | log_unit_warning_errno(UNIT(s), r, "Failed to run install restart timer: %m"); | |
1930 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); | |
1931 | } | |
1932 | ||
1933 | static void service_enter_stop_post(Service *s, ServiceResult f) { | |
1934 | int r; | |
1935 | assert(s); | |
1936 | ||
1937 | if (s->result == SERVICE_SUCCESS) | |
1938 | s->result = f; | |
1939 | ||
1940 | service_unwatch_control_pid(s); | |
1941 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
1942 | ||
1943 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; | |
1944 | if (s->control_command) { | |
1945 | s->control_command_id = SERVICE_EXEC_STOP_POST; | |
1946 | ||
1947 | r = service_spawn(s, | |
1948 | s->control_command, | |
1949 | s->timeout_stop_usec, | |
1950 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_IS_CONTROL|EXEC_SETENV_RESULT|EXEC_CONTROL_CGROUP, | |
1951 | &s->control_pid); | |
1952 | if (r < 0) | |
1953 | goto fail; | |
1954 | ||
1955 | service_set_state(s, SERVICE_STOP_POST); | |
1956 | } else | |
1957 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); | |
1958 | ||
1959 | return; | |
1960 | ||
1961 | fail: | |
1962 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m"); | |
1963 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
1964 | } | |
1965 | ||
1966 | static int state_to_kill_operation(Service *s, ServiceState state) { | |
1967 | switch (state) { | |
1968 | ||
1969 | case SERVICE_STOP_WATCHDOG: | |
1970 | case SERVICE_FINAL_WATCHDOG: | |
1971 | return KILL_WATCHDOG; | |
1972 | ||
1973 | case SERVICE_STOP_SIGTERM: | |
1974 | if (unit_has_job_type(UNIT(s), JOB_RESTART)) | |
1975 | return KILL_RESTART; | |
1976 | _fallthrough_; | |
1977 | ||
1978 | case SERVICE_FINAL_SIGTERM: | |
1979 | return KILL_TERMINATE; | |
1980 | ||
1981 | case SERVICE_STOP_SIGKILL: | |
1982 | case SERVICE_FINAL_SIGKILL: | |
1983 | return KILL_KILL; | |
1984 | ||
1985 | default: | |
1986 | return _KILL_OPERATION_INVALID; | |
1987 | } | |
1988 | } | |
1989 | ||
1990 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { | |
1991 | int kill_operation, r; | |
1992 | ||
1993 | assert(s); | |
1994 | ||
1995 | if (s->result == SERVICE_SUCCESS) | |
1996 | s->result = f; | |
1997 | ||
1998 | /* Before sending any signal, make sure we track all members of this cgroup */ | |
1999 | (void) unit_watch_all_pids(UNIT(s)); | |
2000 | ||
2001 | /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have | |
2002 | * died now */ | |
2003 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
2004 | ||
2005 | kill_operation = state_to_kill_operation(s, state); | |
2006 | r = unit_kill_context( | |
2007 | UNIT(s), | |
2008 | &s->kill_context, | |
2009 | kill_operation, | |
2010 | s->main_pid, | |
2011 | s->control_pid, | |
2012 | s->main_pid_alien); | |
2013 | if (r < 0) | |
2014 | goto fail; | |
2015 | ||
2016 | if (r > 0) { | |
2017 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), | |
2018 | kill_operation == KILL_WATCHDOG ? service_timeout_abort_usec(s) : s->timeout_stop_usec)); | |
2019 | if (r < 0) | |
2020 | goto fail; | |
2021 | ||
2022 | service_set_state(s, state); | |
2023 | } else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM) && s->kill_context.send_sigkill) | |
2024 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); | |
2025 | else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) | |
2026 | service_enter_stop_post(s, SERVICE_SUCCESS); | |
2027 | else if (IN_SET(state, SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM) && s->kill_context.send_sigkill) | |
2028 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); | |
2029 | else | |
2030 | service_enter_dead(s, SERVICE_SUCCESS, true); | |
2031 | ||
2032 | return; | |
2033 | ||
2034 | fail: | |
2035 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); | |
2036 | ||
2037 | if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) | |
2038 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); | |
2039 | else | |
2040 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); | |
2041 | } | |
2042 | ||
2043 | static void service_enter_stop_by_notify(Service *s) { | |
2044 | assert(s); | |
2045 | ||
2046 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
2047 | ||
2048 | service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec)); | |
2049 | ||
2050 | /* The service told us it's stopping, so it's as if we SIGTERM'd it. */ | |
2051 | service_set_state(s, SERVICE_STOP_SIGTERM); | |
2052 | } | |
2053 | ||
2054 | static void service_enter_stop(Service *s, ServiceResult f) { | |
2055 | int r; | |
2056 | ||
2057 | assert(s); | |
2058 | ||
2059 | if (s->result == SERVICE_SUCCESS) | |
2060 | s->result = f; | |
2061 | ||
2062 | service_unwatch_control_pid(s); | |
2063 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
2064 | ||
2065 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; | |
2066 | if (s->control_command) { | |
2067 | s->control_command_id = SERVICE_EXEC_STOP; | |
2068 | ||
2069 | r = service_spawn(s, | |
2070 | s->control_command, | |
2071 | s->timeout_stop_usec, | |
2072 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_SETENV_RESULT|EXEC_CONTROL_CGROUP, | |
2073 | &s->control_pid); | |
2074 | if (r < 0) | |
2075 | goto fail; | |
2076 | ||
2077 | service_set_state(s, SERVICE_STOP); | |
2078 | } else | |
2079 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); | |
2080 | ||
2081 | return; | |
2082 | ||
2083 | fail: | |
2084 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop' task: %m"); | |
2085 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2086 | } | |
2087 | ||
2088 | static bool service_good(Service *s) { | |
2089 | int main_pid_ok; | |
2090 | assert(s); | |
2091 | ||
2092 | if (s->type == SERVICE_DBUS && !s->bus_name_good) | |
2093 | return false; | |
2094 | ||
2095 | main_pid_ok = main_pid_good(s); | |
2096 | if (main_pid_ok > 0) /* It's alive */ | |
2097 | return true; | |
2098 | if (main_pid_ok == 0) /* It's dead */ | |
2099 | return false; | |
2100 | ||
2101 | /* OK, we don't know anything about the main PID, maybe | |
2102 | * because there is none. Let's check the control group | |
2103 | * instead. */ | |
2104 | ||
2105 | return cgroup_good(s) != 0; | |
2106 | } | |
2107 | ||
2108 | static void service_enter_running(Service *s, ServiceResult f) { | |
2109 | assert(s); | |
2110 | ||
2111 | if (s->result == SERVICE_SUCCESS) | |
2112 | s->result = f; | |
2113 | ||
2114 | service_unwatch_control_pid(s); | |
2115 | ||
2116 | if (s->result != SERVICE_SUCCESS) | |
2117 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
2118 | else if (service_good(s)) { | |
2119 | ||
2120 | /* If there are any queued up sd_notify() notifications, process them now */ | |
2121 | if (s->notify_state == NOTIFY_RELOADING) | |
2122 | service_enter_reload_by_notify(s); | |
2123 | else if (s->notify_state == NOTIFY_STOPPING) | |
2124 | service_enter_stop_by_notify(s); | |
2125 | else { | |
2126 | service_set_state(s, SERVICE_RUNNING); | |
2127 | service_arm_timer(s, service_running_timeout(s)); | |
2128 | } | |
2129 | ||
2130 | } else if (s->remain_after_exit) | |
2131 | service_set_state(s, SERVICE_EXITED); | |
2132 | else | |
2133 | service_enter_stop(s, SERVICE_SUCCESS); | |
2134 | } | |
2135 | ||
2136 | static void service_enter_start_post(Service *s) { | |
2137 | int r; | |
2138 | assert(s); | |
2139 | ||
2140 | service_unwatch_control_pid(s); | |
2141 | service_reset_watchdog(s); | |
2142 | ||
2143 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; | |
2144 | if (s->control_command) { | |
2145 | s->control_command_id = SERVICE_EXEC_START_POST; | |
2146 | ||
2147 | r = service_spawn(s, | |
2148 | s->control_command, | |
2149 | s->timeout_start_usec, | |
2150 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_CONTROL_CGROUP, | |
2151 | &s->control_pid); | |
2152 | if (r < 0) | |
2153 | goto fail; | |
2154 | ||
2155 | service_set_state(s, SERVICE_START_POST); | |
2156 | } else | |
2157 | service_enter_running(s, SERVICE_SUCCESS); | |
2158 | ||
2159 | return; | |
2160 | ||
2161 | fail: | |
2162 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m"); | |
2163 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2164 | } | |
2165 | ||
2166 | static void service_kill_control_process(Service *s) { | |
2167 | int r; | |
2168 | ||
2169 | assert(s); | |
2170 | ||
2171 | if (s->control_pid <= 0) | |
2172 | return; | |
2173 | ||
2174 | r = kill_and_sigcont(s->control_pid, SIGKILL); | |
2175 | if (r < 0) { | |
2176 | _cleanup_free_ char *comm = NULL; | |
2177 | ||
2178 | (void) get_process_comm(s->control_pid, &comm); | |
2179 | ||
2180 | log_unit_debug_errno(UNIT(s), r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", | |
2181 | s->control_pid, strna(comm)); | |
2182 | } | |
2183 | } | |
2184 | ||
2185 | static int service_adverse_to_leftover_processes(Service *s) { | |
2186 | assert(s); | |
2187 | ||
2188 | /* KillMode=mixed and control group are used to indicate that all process should be killed off. | |
2189 | * SendSIGKILL= is used for services that require a clean shutdown. These are typically database | |
2190 | * service where a SigKilled process would result in a lengthy recovery and who's shutdown or startup | |
2191 | * time is quite variable (so Timeout settings aren't of use). | |
2192 | * | |
2193 | * Here we take these two factors and refuse to start a service if there are existing processes | |
2194 | * within a control group. Databases, while generally having some protection against multiple | |
2195 | * instances running, lets not stress the rigor of these. Also ExecStartPre= parts of the service | |
2196 | * aren't as rigoriously written to protect aganst against multiple use. */ | |
2197 | ||
2198 | if (unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_start) > 0 && | |
2199 | IN_SET(s->kill_context.kill_mode, KILL_MIXED, KILL_CONTROL_GROUP) && | |
2200 | !s->kill_context.send_sigkill) | |
2201 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EBUSY), | |
2202 | "Will not start SendSIGKILL=no service of type KillMode=control-group or mixed while processes exist"); | |
2203 | ||
2204 | return 0; | |
2205 | } | |
2206 | ||
2207 | static void service_enter_start(Service *s) { | |
2208 | ExecCommand *c; | |
2209 | usec_t timeout; | |
2210 | pid_t pid; | |
2211 | int r; | |
2212 | ||
2213 | assert(s); | |
2214 | ||
2215 | service_unwatch_control_pid(s); | |
2216 | service_unwatch_main_pid(s); | |
2217 | ||
2218 | r = service_adverse_to_leftover_processes(s); | |
2219 | if (r < 0) | |
2220 | goto fail; | |
2221 | ||
2222 | if (s->type == SERVICE_FORKING) { | |
2223 | s->control_command_id = SERVICE_EXEC_START; | |
2224 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
2225 | ||
2226 | s->main_command = NULL; | |
2227 | } else { | |
2228 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2229 | s->control_command = NULL; | |
2230 | ||
2231 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
2232 | } | |
2233 | ||
2234 | if (!c) { | |
2235 | if (s->type != SERVICE_ONESHOT) { | |
2236 | /* There's no command line configured for the main command? Hmm, that is strange. | |
2237 | * This can only happen if the configuration changes at runtime. In this case, | |
2238 | * let's enter a failure state. */ | |
2239 | r = log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENXIO), "There's no 'start' task anymore we could start."); | |
2240 | goto fail; | |
2241 | } | |
2242 | ||
2243 | /* We force a fake state transition here. Otherwise, the unit would go directly from | |
2244 | * SERVICE_DEAD to SERVICE_DEAD without SERVICE_ACTIVATING or SERVICE_ACTIVE | |
2245 | * in between. This way we can later trigger actions that depend on the state | |
2246 | * transition, including SuccessAction=. */ | |
2247 | service_set_state(s, SERVICE_START); | |
2248 | ||
2249 | service_enter_start_post(s); | |
2250 | return; | |
2251 | } | |
2252 | ||
2253 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) | |
2254 | /* For simple + idle this is the main process. We don't apply any timeout here, but | |
2255 | * service_enter_running() will later apply the .runtime_max_usec timeout. */ | |
2256 | timeout = USEC_INFINITY; | |
2257 | else | |
2258 | timeout = s->timeout_start_usec; | |
2259 | ||
2260 | r = service_spawn(s, | |
2261 | c, | |
2262 | timeout, | |
2263 | EXEC_PASS_FDS|EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_SET_WATCHDOG|EXEC_WRITE_CREDENTIALS|EXEC_SETENV_MONITOR_RESULT, | |
2264 | &pid); | |
2265 | if (r < 0) | |
2266 | goto fail; | |
2267 | ||
2268 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) { | |
2269 | /* For simple services we immediately start | |
2270 | * the START_POST binaries. */ | |
2271 | ||
2272 | service_set_main_pid(s, pid); | |
2273 | service_enter_start_post(s); | |
2274 | ||
2275 | } else if (s->type == SERVICE_FORKING) { | |
2276 | ||
2277 | /* For forking services we wait until the start | |
2278 | * process exited. */ | |
2279 | ||
2280 | s->control_pid = pid; | |
2281 | service_set_state(s, SERVICE_START); | |
2282 | ||
2283 | } else if (IN_SET(s->type, SERVICE_ONESHOT, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_EXEC)) { | |
2284 | ||
2285 | /* For oneshot services we wait until the start process exited, too, but it is our main process. */ | |
2286 | ||
2287 | /* For D-Bus services we know the main pid right away, but wait for the bus name to appear on the | |
2288 | * bus. 'notify' and 'exec' services are similar. */ | |
2289 | ||
2290 | service_set_main_pid(s, pid); | |
2291 | service_set_state(s, SERVICE_START); | |
2292 | } else | |
2293 | assert_not_reached(); | |
2294 | ||
2295 | return; | |
2296 | ||
2297 | fail: | |
2298 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start' task: %m"); | |
2299 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2300 | } | |
2301 | ||
2302 | static void service_enter_start_pre(Service *s) { | |
2303 | int r; | |
2304 | ||
2305 | assert(s); | |
2306 | ||
2307 | service_unwatch_control_pid(s); | |
2308 | ||
2309 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; | |
2310 | if (s->control_command) { | |
2311 | ||
2312 | r = service_adverse_to_leftover_processes(s); | |
2313 | if (r < 0) | |
2314 | goto fail; | |
2315 | ||
2316 | s->control_command_id = SERVICE_EXEC_START_PRE; | |
2317 | ||
2318 | r = service_spawn(s, | |
2319 | s->control_command, | |
2320 | s->timeout_start_usec, | |
2321 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_APPLY_TTY_STDIN|EXEC_SETENV_MONITOR_RESULT, | |
2322 | &s->control_pid); | |
2323 | if (r < 0) | |
2324 | goto fail; | |
2325 | ||
2326 | service_set_state(s, SERVICE_START_PRE); | |
2327 | } else | |
2328 | service_enter_start(s); | |
2329 | ||
2330 | return; | |
2331 | ||
2332 | fail: | |
2333 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m"); | |
2334 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); | |
2335 | } | |
2336 | ||
2337 | static void service_enter_condition(Service *s) { | |
2338 | int r; | |
2339 | ||
2340 | assert(s); | |
2341 | ||
2342 | service_unwatch_control_pid(s); | |
2343 | ||
2344 | s->control_command = s->exec_command[SERVICE_EXEC_CONDITION]; | |
2345 | if (s->control_command) { | |
2346 | ||
2347 | r = service_adverse_to_leftover_processes(s); | |
2348 | if (r < 0) | |
2349 | goto fail; | |
2350 | ||
2351 | s->control_command_id = SERVICE_EXEC_CONDITION; | |
2352 | ||
2353 | r = service_spawn(s, | |
2354 | s->control_command, | |
2355 | s->timeout_start_usec, | |
2356 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_APPLY_TTY_STDIN, | |
2357 | &s->control_pid); | |
2358 | ||
2359 | if (r < 0) | |
2360 | goto fail; | |
2361 | ||
2362 | service_set_state(s, SERVICE_CONDITION); | |
2363 | } else | |
2364 | service_enter_start_pre(s); | |
2365 | ||
2366 | return; | |
2367 | ||
2368 | fail: | |
2369 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'exec-condition' task: %m"); | |
2370 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); | |
2371 | } | |
2372 | ||
2373 | static void service_enter_restart(Service *s) { | |
2374 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2375 | int r; | |
2376 | ||
2377 | assert(s); | |
2378 | ||
2379 | if (unit_has_job_type(UNIT(s), JOB_STOP)) { | |
2380 | /* Don't restart things if we are going down anyway */ | |
2381 | log_unit_info(UNIT(s), "Stop job pending for unit, delaying automatic restart."); | |
2382 | ||
2383 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec)); | |
2384 | if (r < 0) | |
2385 | goto fail; | |
2386 | ||
2387 | return; | |
2388 | } | |
2389 | ||
2390 | /* Any units that are bound to this service must also be | |
2391 | * restarted. We use JOB_RESTART (instead of the more obvious | |
2392 | * JOB_START) here so that those dependency jobs will be added | |
2393 | * as well. */ | |
2394 | r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_REPLACE, NULL, &error, NULL); | |
2395 | if (r < 0) | |
2396 | goto fail; | |
2397 | ||
2398 | /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't fully | |
2399 | * stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset the counter | |
2400 | * explicitly however via the usual "systemctl reset-failure" logic. */ | |
2401 | s->n_restarts ++; | |
2402 | s->flush_n_restarts = false; | |
2403 | ||
2404 | log_unit_struct(UNIT(s), LOG_INFO, | |
2405 | "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR, | |
2406 | LOG_UNIT_INVOCATION_ID(UNIT(s)), | |
2407 | LOG_UNIT_MESSAGE(UNIT(s), "Scheduled restart job, restart counter is at %u.", s->n_restarts), | |
2408 | "N_RESTARTS=%u", s->n_restarts); | |
2409 | ||
2410 | /* Notify clients about changed restart counter */ | |
2411 | unit_add_to_dbus_queue(UNIT(s)); | |
2412 | ||
2413 | /* Note that we stay in the SERVICE_AUTO_RESTART state here, | |
2414 | * it will be canceled as part of the service_stop() call that | |
2415 | * is executed as part of JOB_RESTART. */ | |
2416 | ||
2417 | return; | |
2418 | ||
2419 | fail: | |
2420 | log_unit_warning(UNIT(s), "Failed to schedule restart job: %s", bus_error_message(&error, r)); | |
2421 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); | |
2422 | } | |
2423 | ||
2424 | static void service_enter_reload_by_notify(Service *s) { | |
2425 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
2426 | int r; | |
2427 | ||
2428 | assert(s); | |
2429 | ||
2430 | service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_start_usec)); | |
2431 | service_set_state(s, SERVICE_RELOAD); | |
2432 | ||
2433 | /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */ | |
2434 | r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); | |
2435 | if (r < 0) | |
2436 | log_unit_warning(UNIT(s), "Failed to schedule propagation of reload: %s", bus_error_message(&error, r)); | |
2437 | } | |
2438 | ||
2439 | static void service_enter_reload(Service *s) { | |
2440 | int r; | |
2441 | ||
2442 | assert(s); | |
2443 | ||
2444 | service_unwatch_control_pid(s); | |
2445 | s->reload_result = SERVICE_SUCCESS; | |
2446 | ||
2447 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; | |
2448 | if (s->control_command) { | |
2449 | s->control_command_id = SERVICE_EXEC_RELOAD; | |
2450 | ||
2451 | r = service_spawn(s, | |
2452 | s->control_command, | |
2453 | s->timeout_start_usec, | |
2454 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_CONTROL_CGROUP, | |
2455 | &s->control_pid); | |
2456 | if (r < 0) | |
2457 | goto fail; | |
2458 | ||
2459 | service_set_state(s, SERVICE_RELOAD); | |
2460 | } else | |
2461 | service_enter_running(s, SERVICE_SUCCESS); | |
2462 | ||
2463 | return; | |
2464 | ||
2465 | fail: | |
2466 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'reload' task: %m"); | |
2467 | s->reload_result = SERVICE_FAILURE_RESOURCES; | |
2468 | service_enter_running(s, SERVICE_SUCCESS); | |
2469 | } | |
2470 | ||
2471 | static void service_run_next_control(Service *s) { | |
2472 | usec_t timeout; | |
2473 | int r; | |
2474 | ||
2475 | assert(s); | |
2476 | assert(s->control_command); | |
2477 | assert(s->control_command->command_next); | |
2478 | ||
2479 | assert(s->control_command_id != SERVICE_EXEC_START); | |
2480 | ||
2481 | s->control_command = s->control_command->command_next; | |
2482 | service_unwatch_control_pid(s); | |
2483 | ||
2484 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) | |
2485 | timeout = s->timeout_start_usec; | |
2486 | else | |
2487 | timeout = s->timeout_stop_usec; | |
2488 | ||
2489 | r = service_spawn(s, | |
2490 | s->control_command, | |
2491 | timeout, | |
2492 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL| | |
2493 | (IN_SET(s->control_command_id, SERVICE_EXEC_CONDITION, SERVICE_EXEC_START_PRE, SERVICE_EXEC_STOP_POST) ? EXEC_APPLY_TTY_STDIN : 0)| | |
2494 | (IN_SET(s->control_command_id, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST) ? EXEC_SETENV_RESULT : 0)| | |
2495 | (IN_SET(s->control_command_id, SERVICE_EXEC_START_POST, SERVICE_EXEC_RELOAD, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST) ? EXEC_CONTROL_CGROUP : 0), | |
2496 | &s->control_pid); | |
2497 | if (r < 0) | |
2498 | goto fail; | |
2499 | ||
2500 | return; | |
2501 | ||
2502 | fail: | |
2503 | log_unit_warning_errno(UNIT(s), r, "Failed to run next control task: %m"); | |
2504 | ||
2505 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START_POST, SERVICE_STOP)) | |
2506 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2507 | else if (s->state == SERVICE_STOP_POST) | |
2508 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); | |
2509 | else if (s->state == SERVICE_RELOAD) { | |
2510 | s->reload_result = SERVICE_FAILURE_RESOURCES; | |
2511 | service_enter_running(s, SERVICE_SUCCESS); | |
2512 | } else | |
2513 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2514 | } | |
2515 | ||
2516 | static void service_run_next_main(Service *s) { | |
2517 | pid_t pid; | |
2518 | int r; | |
2519 | ||
2520 | assert(s); | |
2521 | assert(s->main_command); | |
2522 | assert(s->main_command->command_next); | |
2523 | assert(s->type == SERVICE_ONESHOT); | |
2524 | ||
2525 | s->main_command = s->main_command->command_next; | |
2526 | service_unwatch_main_pid(s); | |
2527 | ||
2528 | r = service_spawn(s, | |
2529 | s->main_command, | |
2530 | s->timeout_start_usec, | |
2531 | EXEC_PASS_FDS|EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_SET_WATCHDOG, | |
2532 | &pid); | |
2533 | if (r < 0) | |
2534 | goto fail; | |
2535 | ||
2536 | service_set_main_pid(s, pid); | |
2537 | ||
2538 | return; | |
2539 | ||
2540 | fail: | |
2541 | log_unit_warning_errno(UNIT(s), r, "Failed to run next main task: %m"); | |
2542 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2543 | } | |
2544 | ||
2545 | static int service_start(Unit *u) { | |
2546 | Service *s = SERVICE(u); | |
2547 | int r; | |
2548 | ||
2549 | assert(s); | |
2550 | ||
2551 | /* We cannot fulfill this request right now, try again later | |
2552 | * please! */ | |
2553 | if (IN_SET(s->state, | |
2554 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
2555 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, SERVICE_CLEANING)) | |
2556 | return -EAGAIN; | |
2557 | ||
2558 | /* Already on it! */ | |
2559 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST)) | |
2560 | return 0; | |
2561 | ||
2562 | /* A service that will be restarted must be stopped first to | |
2563 | * trigger BindsTo and/or OnFailure dependencies. If a user | |
2564 | * does not want to wait for the holdoff time to elapse, the | |
2565 | * service should be manually restarted, not started. We | |
2566 | * simply return EAGAIN here, so that any start jobs stay | |
2567 | * queued, and assume that the auto restart timer will | |
2568 | * eventually trigger the restart. */ | |
2569 | if (s->state == SERVICE_AUTO_RESTART) | |
2570 | return -EAGAIN; | |
2571 | ||
2572 | assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED)); | |
2573 | ||
2574 | r = unit_acquire_invocation_id(u); | |
2575 | if (r < 0) | |
2576 | return r; | |
2577 | ||
2578 | s->result = SERVICE_SUCCESS; | |
2579 | s->reload_result = SERVICE_SUCCESS; | |
2580 | s->main_pid_known = false; | |
2581 | s->main_pid_alien = false; | |
2582 | s->forbid_restart = false; | |
2583 | ||
2584 | s->status_text = mfree(s->status_text); | |
2585 | s->status_errno = 0; | |
2586 | ||
2587 | s->notify_state = NOTIFY_UNKNOWN; | |
2588 | ||
2589 | s->watchdog_original_usec = s->watchdog_usec; | |
2590 | s->watchdog_override_enable = false; | |
2591 | s->watchdog_override_usec = USEC_INFINITY; | |
2592 | ||
2593 | exec_command_reset_status_list_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); | |
2594 | exec_status_reset(&s->main_exec_status); | |
2595 | ||
2596 | /* This is not an automatic restart? Flush the restart counter then */ | |
2597 | if (s->flush_n_restarts) { | |
2598 | s->n_restarts = 0; | |
2599 | s->flush_n_restarts = false; | |
2600 | } | |
2601 | ||
2602 | u->reset_accounting = true; | |
2603 | ||
2604 | service_enter_condition(s); | |
2605 | return 1; | |
2606 | } | |
2607 | ||
2608 | static int service_stop(Unit *u) { | |
2609 | Service *s = SERVICE(u); | |
2610 | ||
2611 | assert(s); | |
2612 | ||
2613 | /* Don't create restart jobs from manual stops. */ | |
2614 | s->forbid_restart = true; | |
2615 | ||
2616 | /* Already on it */ | |
2617 | if (IN_SET(s->state, | |
2618 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
2619 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) | |
2620 | return 0; | |
2621 | ||
2622 | /* A restart will be scheduled or is in progress. */ | |
2623 | if (s->state == SERVICE_AUTO_RESTART) { | |
2624 | service_set_state(s, SERVICE_DEAD); | |
2625 | return 0; | |
2626 | } | |
2627 | ||
2628 | /* If there's already something running we go directly into | |
2629 | * kill mode. */ | |
2630 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD, SERVICE_STOP_WATCHDOG)) { | |
2631 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); | |
2632 | return 0; | |
2633 | } | |
2634 | ||
2635 | /* If we are currently cleaning, then abort it, brutally. */ | |
2636 | if (s->state == SERVICE_CLEANING) { | |
2637 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); | |
2638 | return 0; | |
2639 | } | |
2640 | ||
2641 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); | |
2642 | ||
2643 | service_enter_stop(s, SERVICE_SUCCESS); | |
2644 | return 1; | |
2645 | } | |
2646 | ||
2647 | static int service_reload(Unit *u) { | |
2648 | Service *s = SERVICE(u); | |
2649 | ||
2650 | assert(s); | |
2651 | ||
2652 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); | |
2653 | ||
2654 | service_enter_reload(s); | |
2655 | return 1; | |
2656 | } | |
2657 | ||
2658 | _pure_ static bool service_can_reload(Unit *u) { | |
2659 | Service *s = SERVICE(u); | |
2660 | ||
2661 | assert(s); | |
2662 | ||
2663 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
2664 | } | |
2665 | ||
2666 | static unsigned service_exec_command_index(Unit *u, ServiceExecCommand id, ExecCommand *current) { | |
2667 | Service *s = SERVICE(u); | |
2668 | unsigned idx = 0; | |
2669 | ExecCommand *first, *c; | |
2670 | ||
2671 | assert(s); | |
2672 | assert(id >= 0); | |
2673 | assert(id < _SERVICE_EXEC_COMMAND_MAX); | |
2674 | ||
2675 | first = s->exec_command[id]; | |
2676 | ||
2677 | /* Figure out where we are in the list by walking back to the beginning */ | |
2678 | for (c = current; c != first; c = c->command_prev) | |
2679 | idx++; | |
2680 | ||
2681 | return idx; | |
2682 | } | |
2683 | ||
2684 | static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command) { | |
2685 | _cleanup_free_ char *args = NULL, *p = NULL; | |
2686 | Service *s = SERVICE(u); | |
2687 | const char *type, *key; | |
2688 | ServiceExecCommand id; | |
2689 | size_t length = 0; | |
2690 | unsigned idx; | |
2691 | char **arg; | |
2692 | ||
2693 | assert(s); | |
2694 | assert(f); | |
2695 | ||
2696 | if (!command) | |
2697 | return 0; | |
2698 | ||
2699 | if (command == s->control_command) { | |
2700 | type = "control"; | |
2701 | id = s->control_command_id; | |
2702 | } else { | |
2703 | type = "main"; | |
2704 | id = SERVICE_EXEC_START; | |
2705 | } | |
2706 | ||
2707 | idx = service_exec_command_index(u, id, command); | |
2708 | ||
2709 | STRV_FOREACH(arg, command->argv) { | |
2710 | _cleanup_free_ char *e = NULL; | |
2711 | size_t n; | |
2712 | ||
2713 | e = cescape(*arg); | |
2714 | if (!e) | |
2715 | return log_oom(); | |
2716 | ||
2717 | n = strlen(e); | |
2718 | if (!GREEDY_REALLOC(args, length + 2 + n + 2)) | |
2719 | return log_oom(); | |
2720 | ||
2721 | if (length > 0) | |
2722 | args[length++] = ' '; | |
2723 | ||
2724 | args[length++] = '"'; | |
2725 | memcpy(args + length, e, n); | |
2726 | length += n; | |
2727 | args[length++] = '"'; | |
2728 | } | |
2729 | ||
2730 | if (!GREEDY_REALLOC(args, length + 1)) | |
2731 | return log_oom(); | |
2732 | ||
2733 | args[length++] = 0; | |
2734 | ||
2735 | p = cescape(command->path); | |
2736 | if (!p) | |
2737 | return log_oom(); | |
2738 | ||
2739 | key = strjoina(type, "-command"); | |
2740 | (void) serialize_item_format(f, key, "%s %u %s %s", service_exec_command_to_string(id), idx, p, args); | |
2741 | ||
2742 | return 0; | |
2743 | } | |
2744 | ||
2745 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { | |
2746 | Service *s = SERVICE(u); | |
2747 | ServiceFDStore *fs; | |
2748 | int r; | |
2749 | ||
2750 | assert(u); | |
2751 | assert(f); | |
2752 | assert(fds); | |
2753 | ||
2754 | (void) serialize_item(f, "state", service_state_to_string(s->state)); | |
2755 | (void) serialize_item(f, "result", service_result_to_string(s->result)); | |
2756 | (void) serialize_item(f, "reload-result", service_result_to_string(s->reload_result)); | |
2757 | ||
2758 | if (s->control_pid > 0) | |
2759 | (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid); | |
2760 | ||
2761 | if (s->main_pid_known && s->main_pid > 0) | |
2762 | (void) serialize_item_format(f, "main-pid", PID_FMT, s->main_pid); | |
2763 | ||
2764 | (void) serialize_bool(f, "main-pid-known", s->main_pid_known); | |
2765 | (void) serialize_bool(f, "bus-name-good", s->bus_name_good); | |
2766 | (void) serialize_bool(f, "bus-name-owner", s->bus_name_owner); | |
2767 | ||
2768 | (void) serialize_item_format(f, "n-restarts", "%u", s->n_restarts); | |
2769 | (void) serialize_bool(f, "flush-n-restarts", s->flush_n_restarts); | |
2770 | ||
2771 | r = serialize_item_escaped(f, "status-text", s->status_text); | |
2772 | if (r < 0) | |
2773 | return r; | |
2774 | ||
2775 | service_serialize_exec_command(u, f, s->control_command); | |
2776 | service_serialize_exec_command(u, f, s->main_command); | |
2777 | ||
2778 | r = serialize_fd(f, fds, "stdin-fd", s->stdin_fd); | |
2779 | if (r < 0) | |
2780 | return r; | |
2781 | r = serialize_fd(f, fds, "stdout-fd", s->stdout_fd); | |
2782 | if (r < 0) | |
2783 | return r; | |
2784 | r = serialize_fd(f, fds, "stderr-fd", s->stderr_fd); | |
2785 | if (r < 0) | |
2786 | return r; | |
2787 | ||
2788 | if (s->exec_fd_event_source) { | |
2789 | r = serialize_fd(f, fds, "exec-fd", sd_event_source_get_io_fd(s->exec_fd_event_source)); | |
2790 | if (r < 0) | |
2791 | return r; | |
2792 | ||
2793 | (void) serialize_bool(f, "exec-fd-hot", s->exec_fd_hot); | |
2794 | } | |
2795 | ||
2796 | if (UNIT_ISSET(s->accept_socket)) { | |
2797 | r = serialize_item(f, "accept-socket", UNIT_DEREF(s->accept_socket)->id); | |
2798 | if (r < 0) | |
2799 | return r; | |
2800 | } | |
2801 | ||
2802 | r = serialize_fd(f, fds, "socket-fd", s->socket_fd); | |
2803 | if (r < 0) | |
2804 | return r; | |
2805 | ||
2806 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
2807 | _cleanup_free_ char *c = NULL; | |
2808 | int copy; | |
2809 | ||
2810 | copy = fdset_put_dup(fds, fs->fd); | |
2811 | if (copy < 0) | |
2812 | return log_error_errno(copy, "Failed to copy file descriptor for serialization: %m"); | |
2813 | ||
2814 | c = cescape(fs->fdname); | |
2815 | if (!c) | |
2816 | return log_oom(); | |
2817 | ||
2818 | (void) serialize_item_format(f, "fd-store-fd", "%i \"%s\" %i", copy, c, fs->do_poll); | |
2819 | } | |
2820 | ||
2821 | if (s->main_exec_status.pid > 0) { | |
2822 | (void) serialize_item_format(f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid); | |
2823 | (void) serialize_dual_timestamp(f, "main-exec-status-start", &s->main_exec_status.start_timestamp); | |
2824 | (void) serialize_dual_timestamp(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp); | |
2825 | ||
2826 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { | |
2827 | (void) serialize_item_format(f, "main-exec-status-code", "%i", s->main_exec_status.code); | |
2828 | (void) serialize_item_format(f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
2829 | } | |
2830 | } | |
2831 | ||
2832 | (void) serialize_dual_timestamp(f, "watchdog-timestamp", &s->watchdog_timestamp); | |
2833 | (void) serialize_bool(f, "forbid-restart", s->forbid_restart); | |
2834 | ||
2835 | if (s->watchdog_override_enable) | |
2836 | (void) serialize_item_format(f, "watchdog-override-usec", USEC_FMT, s->watchdog_override_usec); | |
2837 | ||
2838 | if (s->watchdog_original_usec != USEC_INFINITY) | |
2839 | (void) serialize_item_format(f, "watchdog-original-usec", USEC_FMT, s->watchdog_original_usec); | |
2840 | ||
2841 | return 0; | |
2842 | } | |
2843 | ||
2844 | int service_deserialize_exec_command( | |
2845 | Unit *u, | |
2846 | const char *key, | |
2847 | const char *value) { | |
2848 | ||
2849 | Service *s = SERVICE(u); | |
2850 | int r; | |
2851 | unsigned idx = 0, i; | |
2852 | bool control, found = false; | |
2853 | ServiceExecCommand id = _SERVICE_EXEC_COMMAND_INVALID; | |
2854 | ExecCommand *command = NULL; | |
2855 | _cleanup_free_ char *path = NULL; | |
2856 | _cleanup_strv_free_ char **argv = NULL; | |
2857 | ||
2858 | enum ExecCommandState { | |
2859 | STATE_EXEC_COMMAND_TYPE, | |
2860 | STATE_EXEC_COMMAND_INDEX, | |
2861 | STATE_EXEC_COMMAND_PATH, | |
2862 | STATE_EXEC_COMMAND_ARGS, | |
2863 | _STATE_EXEC_COMMAND_MAX, | |
2864 | _STATE_EXEC_COMMAND_INVALID = -EINVAL, | |
2865 | } state; | |
2866 | ||
2867 | assert(s); | |
2868 | assert(key); | |
2869 | assert(value); | |
2870 | ||
2871 | control = streq(key, "control-command"); | |
2872 | ||
2873 | state = STATE_EXEC_COMMAND_TYPE; | |
2874 | ||
2875 | for (;;) { | |
2876 | _cleanup_free_ char *arg = NULL; | |
2877 | ||
2878 | r = extract_first_word(&value, &arg, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE); | |
2879 | if (r < 0) | |
2880 | return r; | |
2881 | if (r == 0) | |
2882 | break; | |
2883 | ||
2884 | switch (state) { | |
2885 | case STATE_EXEC_COMMAND_TYPE: | |
2886 | id = service_exec_command_from_string(arg); | |
2887 | if (id < 0) | |
2888 | return id; | |
2889 | ||
2890 | state = STATE_EXEC_COMMAND_INDEX; | |
2891 | break; | |
2892 | case STATE_EXEC_COMMAND_INDEX: | |
2893 | r = safe_atou(arg, &idx); | |
2894 | if (r < 0) | |
2895 | return r; | |
2896 | ||
2897 | state = STATE_EXEC_COMMAND_PATH; | |
2898 | break; | |
2899 | case STATE_EXEC_COMMAND_PATH: | |
2900 | path = TAKE_PTR(arg); | |
2901 | state = STATE_EXEC_COMMAND_ARGS; | |
2902 | break; | |
2903 | case STATE_EXEC_COMMAND_ARGS: | |
2904 | r = strv_extend(&argv, arg); | |
2905 | if (r < 0) | |
2906 | return -ENOMEM; | |
2907 | break; | |
2908 | default: | |
2909 | assert_not_reached(); | |
2910 | } | |
2911 | } | |
2912 | ||
2913 | if (state != STATE_EXEC_COMMAND_ARGS) | |
2914 | return -EINVAL; | |
2915 | if (strv_isempty(argv)) | |
2916 | return -EINVAL; /* At least argv[0] must be always present. */ | |
2917 | ||
2918 | /* Let's check whether exec command on given offset matches data that we just deserialized */ | |
2919 | for (command = s->exec_command[id], i = 0; command; command = command->command_next, i++) { | |
2920 | if (i != idx) | |
2921 | continue; | |
2922 | ||
2923 | found = strv_equal(argv, command->argv) && streq(command->path, path); | |
2924 | break; | |
2925 | } | |
2926 | ||
2927 | if (!found) { | |
2928 | /* Command at the index we serialized is different, let's look for command that exactly | |
2929 | * matches but is on different index. If there is no such command we will not resume execution. */ | |
2930 | for (command = s->exec_command[id]; command; command = command->command_next) | |
2931 | if (strv_equal(command->argv, argv) && streq(command->path, path)) | |
2932 | break; | |
2933 | } | |
2934 | ||
2935 | if (command && control) { | |
2936 | s->control_command = command; | |
2937 | s->control_command_id = id; | |
2938 | } else if (command) | |
2939 | s->main_command = command; | |
2940 | else | |
2941 | log_unit_warning(u, "Current command vanished from the unit file, execution of the command list won't be resumed."); | |
2942 | ||
2943 | return 0; | |
2944 | } | |
2945 | ||
2946 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
2947 | Service *s = SERVICE(u); | |
2948 | int r; | |
2949 | ||
2950 | assert(u); | |
2951 | assert(key); | |
2952 | assert(value); | |
2953 | assert(fds); | |
2954 | ||
2955 | if (streq(key, "state")) { | |
2956 | ServiceState state; | |
2957 | ||
2958 | state = service_state_from_string(value); | |
2959 | if (state < 0) | |
2960 | log_unit_debug(u, "Failed to parse state value: %s", value); | |
2961 | else | |
2962 | s->deserialized_state = state; | |
2963 | } else if (streq(key, "result")) { | |
2964 | ServiceResult f; | |
2965 | ||
2966 | f = service_result_from_string(value); | |
2967 | if (f < 0) | |
2968 | log_unit_debug(u, "Failed to parse result value: %s", value); | |
2969 | else if (f != SERVICE_SUCCESS) | |
2970 | s->result = f; | |
2971 | ||
2972 | } else if (streq(key, "reload-result")) { | |
2973 | ServiceResult f; | |
2974 | ||
2975 | f = service_result_from_string(value); | |
2976 | if (f < 0) | |
2977 | log_unit_debug(u, "Failed to parse reload result value: %s", value); | |
2978 | else if (f != SERVICE_SUCCESS) | |
2979 | s->reload_result = f; | |
2980 | ||
2981 | } else if (streq(key, "control-pid")) { | |
2982 | pid_t pid; | |
2983 | ||
2984 | if (parse_pid(value, &pid) < 0) | |
2985 | log_unit_debug(u, "Failed to parse control-pid value: %s", value); | |
2986 | else | |
2987 | s->control_pid = pid; | |
2988 | } else if (streq(key, "main-pid")) { | |
2989 | pid_t pid; | |
2990 | ||
2991 | if (parse_pid(value, &pid) < 0) | |
2992 | log_unit_debug(u, "Failed to parse main-pid value: %s", value); | |
2993 | else | |
2994 | (void) service_set_main_pid(s, pid); | |
2995 | } else if (streq(key, "main-pid-known")) { | |
2996 | int b; | |
2997 | ||
2998 | b = parse_boolean(value); | |
2999 | if (b < 0) | |
3000 | log_unit_debug(u, "Failed to parse main-pid-known value: %s", value); | |
3001 | else | |
3002 | s->main_pid_known = b; | |
3003 | } else if (streq(key, "bus-name-good")) { | |
3004 | int b; | |
3005 | ||
3006 | b = parse_boolean(value); | |
3007 | if (b < 0) | |
3008 | log_unit_debug(u, "Failed to parse bus-name-good value: %s", value); | |
3009 | else | |
3010 | s->bus_name_good = b; | |
3011 | } else if (streq(key, "bus-name-owner")) { | |
3012 | r = free_and_strdup(&s->bus_name_owner, value); | |
3013 | if (r < 0) | |
3014 | log_unit_error_errno(u, r, "Unable to deserialize current bus owner %s: %m", value); | |
3015 | } else if (streq(key, "status-text")) { | |
3016 | char *t; | |
3017 | ssize_t l; | |
3018 | ||
3019 | l = cunescape(value, 0, &t); | |
3020 | if (l < 0) | |
3021 | log_unit_debug_errno(u, l, "Failed to unescape status text '%s': %m", value); | |
3022 | else | |
3023 | free_and_replace(s->status_text, t); | |
3024 | ||
3025 | } else if (streq(key, "accept-socket")) { | |
3026 | Unit *socket; | |
3027 | ||
3028 | r = manager_load_unit(u->manager, value, NULL, NULL, &socket); | |
3029 | if (r < 0) | |
3030 | log_unit_debug_errno(u, r, "Failed to load accept-socket unit '%s': %m", value); | |
3031 | else { | |
3032 | unit_ref_set(&s->accept_socket, u, socket); | |
3033 | SOCKET(socket)->n_connections++; | |
3034 | } | |
3035 | ||
3036 | } else if (streq(key, "socket-fd")) { | |
3037 | int fd; | |
3038 | ||
3039 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3040 | log_unit_debug(u, "Failed to parse socket-fd value: %s", value); | |
3041 | else { | |
3042 | asynchronous_close(s->socket_fd); | |
3043 | s->socket_fd = fdset_remove(fds, fd); | |
3044 | } | |
3045 | } else if (streq(key, "fd-store-fd")) { | |
3046 | _cleanup_free_ char *fdv = NULL, *fdn = NULL, *fdp = NULL; | |
3047 | int fd; | |
3048 | int do_poll; | |
3049 | ||
3050 | r = extract_first_word(&value, &fdv, NULL, 0); | |
3051 | if (r <= 0 || safe_atoi(fdv, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) { | |
3052 | log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); | |
3053 | return 0; | |
3054 | } | |
3055 | ||
3056 | r = extract_first_word(&value, &fdn, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE); | |
3057 | if (r <= 0) { | |
3058 | log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); | |
3059 | return 0; | |
3060 | } | |
3061 | ||
3062 | r = extract_first_word(&value, &fdp, NULL, 0); | |
3063 | if (r == 0) { | |
3064 | /* If the value is not present, we assume the default */ | |
3065 | do_poll = 1; | |
3066 | } else if (r < 0 || safe_atoi(fdp, &do_poll) < 0) { | |
3067 | log_unit_debug_errno(u, r, "Failed to parse fd-store-fd value \"%s\": %m", value); | |
3068 | return 0; | |
3069 | } | |
3070 | ||
3071 | r = service_add_fd_store(s, fd, fdn, do_poll); | |
3072 | if (r < 0) | |
3073 | log_unit_error_errno(u, r, "Failed to add fd to store: %m"); | |
3074 | else | |
3075 | fdset_remove(fds, fd); | |
3076 | } else if (streq(key, "main-exec-status-pid")) { | |
3077 | pid_t pid; | |
3078 | ||
3079 | if (parse_pid(value, &pid) < 0) | |
3080 | log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value); | |
3081 | else | |
3082 | s->main_exec_status.pid = pid; | |
3083 | } else if (streq(key, "main-exec-status-code")) { | |
3084 | int i; | |
3085 | ||
3086 | if (safe_atoi(value, &i) < 0) | |
3087 | log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value); | |
3088 | else | |
3089 | s->main_exec_status.code = i; | |
3090 | } else if (streq(key, "main-exec-status-status")) { | |
3091 | int i; | |
3092 | ||
3093 | if (safe_atoi(value, &i) < 0) | |
3094 | log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value); | |
3095 | else | |
3096 | s->main_exec_status.status = i; | |
3097 | } else if (streq(key, "main-exec-status-start")) | |
3098 | deserialize_dual_timestamp(value, &s->main_exec_status.start_timestamp); | |
3099 | else if (streq(key, "main-exec-status-exit")) | |
3100 | deserialize_dual_timestamp(value, &s->main_exec_status.exit_timestamp); | |
3101 | else if (streq(key, "watchdog-timestamp")) | |
3102 | deserialize_dual_timestamp(value, &s->watchdog_timestamp); | |
3103 | else if (streq(key, "forbid-restart")) { | |
3104 | int b; | |
3105 | ||
3106 | b = parse_boolean(value); | |
3107 | if (b < 0) | |
3108 | log_unit_debug(u, "Failed to parse forbid-restart value: %s", value); | |
3109 | else | |
3110 | s->forbid_restart = b; | |
3111 | } else if (streq(key, "stdin-fd")) { | |
3112 | int fd; | |
3113 | ||
3114 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3115 | log_unit_debug(u, "Failed to parse stdin-fd value: %s", value); | |
3116 | else { | |
3117 | asynchronous_close(s->stdin_fd); | |
3118 | s->stdin_fd = fdset_remove(fds, fd); | |
3119 | s->exec_context.stdio_as_fds = true; | |
3120 | } | |
3121 | } else if (streq(key, "stdout-fd")) { | |
3122 | int fd; | |
3123 | ||
3124 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3125 | log_unit_debug(u, "Failed to parse stdout-fd value: %s", value); | |
3126 | else { | |
3127 | asynchronous_close(s->stdout_fd); | |
3128 | s->stdout_fd = fdset_remove(fds, fd); | |
3129 | s->exec_context.stdio_as_fds = true; | |
3130 | } | |
3131 | } else if (streq(key, "stderr-fd")) { | |
3132 | int fd; | |
3133 | ||
3134 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3135 | log_unit_debug(u, "Failed to parse stderr-fd value: %s", value); | |
3136 | else { | |
3137 | asynchronous_close(s->stderr_fd); | |
3138 | s->stderr_fd = fdset_remove(fds, fd); | |
3139 | s->exec_context.stdio_as_fds = true; | |
3140 | } | |
3141 | } else if (streq(key, "exec-fd")) { | |
3142 | int fd; | |
3143 | ||
3144 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3145 | log_unit_debug(u, "Failed to parse exec-fd value: %s", value); | |
3146 | else { | |
3147 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
3148 | ||
3149 | fd = fdset_remove(fds, fd); | |
3150 | if (service_allocate_exec_fd_event_source(s, fd, &s->exec_fd_event_source) < 0) | |
3151 | safe_close(fd); | |
3152 | } | |
3153 | } else if (streq(key, "watchdog-override-usec")) { | |
3154 | if (deserialize_usec(value, &s->watchdog_override_usec) < 0) | |
3155 | log_unit_debug(u, "Failed to parse watchdog_override_usec value: %s", value); | |
3156 | else | |
3157 | s->watchdog_override_enable = true; | |
3158 | ||
3159 | } else if (streq(key, "watchdog-original-usec")) { | |
3160 | if (deserialize_usec(value, &s->watchdog_original_usec) < 0) | |
3161 | log_unit_debug(u, "Failed to parse watchdog_original_usec value: %s", value); | |
3162 | ||
3163 | } else if (STR_IN_SET(key, "main-command", "control-command")) { | |
3164 | r = service_deserialize_exec_command(u, key, value); | |
3165 | if (r < 0) | |
3166 | log_unit_debug_errno(u, r, "Failed to parse serialized command \"%s\": %m", value); | |
3167 | ||
3168 | } else if (streq(key, "n-restarts")) { | |
3169 | r = safe_atou(value, &s->n_restarts); | |
3170 | if (r < 0) | |
3171 | log_unit_debug_errno(u, r, "Failed to parse serialized restart counter '%s': %m", value); | |
3172 | ||
3173 | } else if (streq(key, "flush-n-restarts")) { | |
3174 | r = parse_boolean(value); | |
3175 | if (r < 0) | |
3176 | log_unit_debug_errno(u, r, "Failed to parse serialized flush restart counter setting '%s': %m", value); | |
3177 | else | |
3178 | s->flush_n_restarts = r; | |
3179 | } else | |
3180 | log_unit_debug(u, "Unknown serialization key: %s", key); | |
3181 | ||
3182 | return 0; | |
3183 | } | |
3184 | ||
3185 | _pure_ static UnitActiveState service_active_state(Unit *u) { | |
3186 | const UnitActiveState *table; | |
3187 | ||
3188 | assert(u); | |
3189 | ||
3190 | table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; | |
3191 | ||
3192 | return table[SERVICE(u)->state]; | |
3193 | } | |
3194 | ||
3195 | static const char *service_sub_state_to_string(Unit *u) { | |
3196 | assert(u); | |
3197 | ||
3198 | return service_state_to_string(SERVICE(u)->state); | |
3199 | } | |
3200 | ||
3201 | static bool service_may_gc(Unit *u) { | |
3202 | Service *s = SERVICE(u); | |
3203 | ||
3204 | assert(s); | |
3205 | ||
3206 | /* Never clean up services that still have a process around, even if the service is formally dead. Note that | |
3207 | * unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they | |
3208 | * have moved outside of the cgroup. */ | |
3209 | ||
3210 | if (main_pid_good(s) > 0 || | |
3211 | control_pid_good(s) > 0) | |
3212 | return false; | |
3213 | ||
3214 | return true; | |
3215 | } | |
3216 | ||
3217 | static int service_retry_pid_file(Service *s) { | |
3218 | int r; | |
3219 | ||
3220 | assert(s->pid_file); | |
3221 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); | |
3222 | ||
3223 | r = service_load_pid_file(s, false); | |
3224 | if (r < 0) | |
3225 | return r; | |
3226 | ||
3227 | service_unwatch_pid_file(s); | |
3228 | ||
3229 | service_enter_running(s, SERVICE_SUCCESS); | |
3230 | return 0; | |
3231 | } | |
3232 | ||
3233 | static int service_watch_pid_file(Service *s) { | |
3234 | int r; | |
3235 | ||
3236 | log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path); | |
3237 | ||
3238 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_inotify_io); | |
3239 | if (r < 0) | |
3240 | goto fail; | |
3241 | ||
3242 | /* the pidfile might have appeared just before we set the watch */ | |
3243 | log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path); | |
3244 | service_retry_pid_file(s); | |
3245 | ||
3246 | return 0; | |
3247 | fail: | |
3248 | log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path); | |
3249 | service_unwatch_pid_file(s); | |
3250 | return r; | |
3251 | } | |
3252 | ||
3253 | static int service_demand_pid_file(Service *s) { | |
3254 | PathSpec *ps; | |
3255 | ||
3256 | assert(s->pid_file); | |
3257 | assert(!s->pid_file_pathspec); | |
3258 | ||
3259 | ps = new0(PathSpec, 1); | |
3260 | if (!ps) | |
3261 | return -ENOMEM; | |
3262 | ||
3263 | ps->unit = UNIT(s); | |
3264 | ps->path = strdup(s->pid_file); | |
3265 | if (!ps->path) { | |
3266 | free(ps); | |
3267 | return -ENOMEM; | |
3268 | } | |
3269 | ||
3270 | path_simplify(ps->path); | |
3271 | ||
3272 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that | |
3273 | * keep their PID file open all the time. */ | |
3274 | ps->type = PATH_MODIFIED; | |
3275 | ps->inotify_fd = -1; | |
3276 | ||
3277 | s->pid_file_pathspec = ps; | |
3278 | ||
3279 | return service_watch_pid_file(s); | |
3280 | } | |
3281 | ||
3282 | static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { | |
3283 | PathSpec *p = userdata; | |
3284 | Service *s; | |
3285 | ||
3286 | assert(p); | |
3287 | ||
3288 | s = SERVICE(p->unit); | |
3289 | ||
3290 | assert(s); | |
3291 | assert(fd >= 0); | |
3292 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); | |
3293 | assert(s->pid_file_pathspec); | |
3294 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); | |
3295 | ||
3296 | log_unit_debug(UNIT(s), "inotify event"); | |
3297 | ||
3298 | if (path_spec_fd_event(p, events) < 0) | |
3299 | goto fail; | |
3300 | ||
3301 | if (service_retry_pid_file(s) == 0) | |
3302 | return 0; | |
3303 | ||
3304 | if (service_watch_pid_file(s) < 0) | |
3305 | goto fail; | |
3306 | ||
3307 | return 0; | |
3308 | ||
3309 | fail: | |
3310 | service_unwatch_pid_file(s); | |
3311 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
3312 | return 0; | |
3313 | } | |
3314 | ||
3315 | static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { | |
3316 | Service *s = SERVICE(userdata); | |
3317 | ||
3318 | assert(s); | |
3319 | ||
3320 | log_unit_debug(UNIT(s), "got exec-fd event"); | |
3321 | ||
3322 | /* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve() | |
3323 | * successfully for it. We implement this through a pipe() towards the child, which the kernel automatically | |
3324 | * closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on the pipe in the | |
3325 | * parent. We need to be careful however, as there are other reasons that we might cause the child's side of | |
3326 | * the pipe to be closed (for example, a simple exit()). To deal with that we'll ignore EOFs on the pipe unless | |
3327 | * the child signalled us first that it is about to call the execve(). It does so by sending us a simple | |
3328 | * non-zero byte via the pipe. We also provide the child with a way to inform us in case execve() failed: if it | |
3329 | * sends a zero byte we'll ignore POLLHUP on the fd again. */ | |
3330 | ||
3331 | for (;;) { | |
3332 | uint8_t x; | |
3333 | ssize_t n; | |
3334 | ||
3335 | n = read(fd, &x, sizeof(x)); | |
3336 | if (n < 0) { | |
3337 | if (errno == EAGAIN) /* O_NONBLOCK in effect → everything queued has now been processed. */ | |
3338 | return 0; | |
3339 | ||
3340 | return log_unit_error_errno(UNIT(s), errno, "Failed to read from exec_fd: %m"); | |
3341 | } | |
3342 | if (n == 0) { /* EOF → the event we are waiting for */ | |
3343 | ||
3344 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
3345 | ||
3346 | if (s->exec_fd_hot) { /* Did the child tell us to expect EOF now? */ | |
3347 | log_unit_debug(UNIT(s), "Got EOF on exec-fd"); | |
3348 | ||
3349 | s->exec_fd_hot = false; | |
3350 | ||
3351 | /* Nice! This is what we have been waiting for. Transition to next state. */ | |
3352 | if (s->type == SERVICE_EXEC && s->state == SERVICE_START) | |
3353 | service_enter_start_post(s); | |
3354 | } else | |
3355 | log_unit_debug(UNIT(s), "Got EOF on exec-fd while it was disabled, ignoring."); | |
3356 | ||
3357 | return 0; | |
3358 | } | |
3359 | ||
3360 | /* A byte was read → this turns on/off the exec fd logic */ | |
3361 | assert(n == sizeof(x)); | |
3362 | s->exec_fd_hot = x; | |
3363 | } | |
3364 | ||
3365 | return 0; | |
3366 | } | |
3367 | ||
3368 | static void service_notify_cgroup_empty_event(Unit *u) { | |
3369 | Service *s = SERVICE(u); | |
3370 | ||
3371 | assert(u); | |
3372 | ||
3373 | log_unit_debug(u, "Control group is empty."); | |
3374 | ||
3375 | switch (s->state) { | |
3376 | ||
3377 | /* Waiting for SIGCHLD is usually more interesting, | |
3378 | * because it includes return codes/signals. Which is | |
3379 | * why we ignore the cgroup events for most cases, | |
3380 | * except when we don't know pid which to expect the | |
3381 | * SIGCHLD for. */ | |
3382 | ||
3383 | case SERVICE_START: | |
3384 | if (s->type == SERVICE_NOTIFY && | |
3385 | main_pid_good(s) == 0 && | |
3386 | control_pid_good(s) == 0) { | |
3387 | /* No chance of getting a ready notification anymore */ | |
3388 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); | |
3389 | break; | |
3390 | } | |
3391 | ||
3392 | if (s->exit_type == SERVICE_EXIT_CGROUP && main_pid_good(s) <= 0) | |
3393 | service_enter_start_post(s); | |
3394 | ||
3395 | _fallthrough_; | |
3396 | case SERVICE_START_POST: | |
3397 | if (s->pid_file_pathspec && | |
3398 | main_pid_good(s) == 0 && | |
3399 | control_pid_good(s) == 0) { | |
3400 | ||
3401 | /* Give up hoping for the daemon to write its PID file */ | |
3402 | log_unit_warning(u, "Daemon never wrote its PID file. Failing."); | |
3403 | ||
3404 | service_unwatch_pid_file(s); | |
3405 | if (s->state == SERVICE_START) | |
3406 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); | |
3407 | else | |
3408 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); | |
3409 | } | |
3410 | break; | |
3411 | ||
3412 | case SERVICE_RUNNING: | |
3413 | /* service_enter_running() will figure out what to do */ | |
3414 | service_enter_running(s, SERVICE_SUCCESS); | |
3415 | break; | |
3416 | ||
3417 | case SERVICE_STOP_WATCHDOG: | |
3418 | case SERVICE_STOP_SIGTERM: | |
3419 | case SERVICE_STOP_SIGKILL: | |
3420 | ||
3421 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) | |
3422 | service_enter_stop_post(s, SERVICE_SUCCESS); | |
3423 | ||
3424 | break; | |
3425 | ||
3426 | case SERVICE_STOP_POST: | |
3427 | case SERVICE_FINAL_WATCHDOG: | |
3428 | case SERVICE_FINAL_SIGTERM: | |
3429 | case SERVICE_FINAL_SIGKILL: | |
3430 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) | |
3431 | service_enter_dead(s, SERVICE_SUCCESS, true); | |
3432 | ||
3433 | break; | |
3434 | ||
3435 | /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean | |
3436 | * up the cgroup earlier and should do it now. */ | |
3437 | case SERVICE_DEAD: | |
3438 | case SERVICE_FAILED: | |
3439 | unit_prune_cgroup(u); | |
3440 | break; | |
3441 | ||
3442 | default: | |
3443 | ; | |
3444 | } | |
3445 | } | |
3446 | ||
3447 | static void service_notify_cgroup_oom_event(Unit *u) { | |
3448 | Service *s = SERVICE(u); | |
3449 | ||
3450 | log_unit_debug(u, "Process of control group was killed by the OOM killer."); | |
3451 | ||
3452 | if (s->oom_policy == OOM_CONTINUE) | |
3453 | return; | |
3454 | ||
3455 | switch (s->state) { | |
3456 | ||
3457 | case SERVICE_CONDITION: | |
3458 | case SERVICE_START_PRE: | |
3459 | case SERVICE_START: | |
3460 | case SERVICE_START_POST: | |
3461 | case SERVICE_STOP: | |
3462 | if (s->oom_policy == OOM_STOP) | |
3463 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_OOM_KILL); | |
3464 | else if (s->oom_policy == OOM_KILL) | |
3465 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3466 | ||
3467 | break; | |
3468 | ||
3469 | case SERVICE_EXITED: | |
3470 | case SERVICE_RUNNING: | |
3471 | if (s->oom_policy == OOM_STOP) | |
3472 | service_enter_stop(s, SERVICE_FAILURE_OOM_KILL); | |
3473 | else if (s->oom_policy == OOM_KILL) | |
3474 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3475 | ||
3476 | break; | |
3477 | ||
3478 | case SERVICE_STOP_WATCHDOG: | |
3479 | case SERVICE_STOP_SIGTERM: | |
3480 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3481 | break; | |
3482 | ||
3483 | case SERVICE_STOP_SIGKILL: | |
3484 | case SERVICE_FINAL_SIGKILL: | |
3485 | if (s->result == SERVICE_SUCCESS) | |
3486 | s->result = SERVICE_FAILURE_OOM_KILL; | |
3487 | break; | |
3488 | ||
3489 | case SERVICE_STOP_POST: | |
3490 | case SERVICE_FINAL_SIGTERM: | |
3491 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3492 | break; | |
3493 | ||
3494 | default: | |
3495 | ; | |
3496 | } | |
3497 | } | |
3498 | ||
3499 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { | |
3500 | bool notify_dbus = true; | |
3501 | Service *s = SERVICE(u); | |
3502 | ServiceResult f; | |
3503 | ExitClean clean_mode; | |
3504 | ||
3505 | assert(s); | |
3506 | assert(pid >= 0); | |
3507 | ||
3508 | /* Oneshot services and non-SERVICE_EXEC_START commands should not be | |
3509 | * considered daemons as they are typically not long running. */ | |
3510 | if (s->type == SERVICE_ONESHOT || (s->control_pid == pid && s->control_command_id != SERVICE_EXEC_START)) | |
3511 | clean_mode = EXIT_CLEAN_COMMAND; | |
3512 | else | |
3513 | clean_mode = EXIT_CLEAN_DAEMON; | |
3514 | ||
3515 | if (is_clean_exit(code, status, clean_mode, &s->success_status)) | |
3516 | f = SERVICE_SUCCESS; | |
3517 | else if (code == CLD_EXITED) | |
3518 | f = SERVICE_FAILURE_EXIT_CODE; | |
3519 | else if (code == CLD_KILLED) | |
3520 | f = SERVICE_FAILURE_SIGNAL; | |
3521 | else if (code == CLD_DUMPED) | |
3522 | f = SERVICE_FAILURE_CORE_DUMP; | |
3523 | else | |
3524 | assert_not_reached(); | |
3525 | ||
3526 | if (s->main_pid == pid) { | |
3527 | /* Clean up the exec_fd event source. We want to do this here, not later in | |
3528 | * service_set_state(), because service_enter_stop_post() calls service_spawn(). | |
3529 | * The source owns its end of the pipe, so this will close that too. */ | |
3530 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
3531 | ||
3532 | /* Forking services may occasionally move to a new PID. | |
3533 | * As long as they update the PID file before exiting the old | |
3534 | * PID, they're fine. */ | |
3535 | if (service_load_pid_file(s, false) > 0) | |
3536 | return; | |
3537 | ||
3538 | s->main_pid = 0; | |
3539 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); | |
3540 | ||
3541 | if (s->main_command) { | |
3542 | /* If this is not a forking service than the | |
3543 | * main process got started and hence we copy | |
3544 | * the exit status so that it is recorded both | |
3545 | * as main and as control process exit | |
3546 | * status */ | |
3547 | ||
3548 | s->main_command->exec_status = s->main_exec_status; | |
3549 | ||
3550 | if (s->main_command->flags & EXEC_COMMAND_IGNORE_FAILURE) | |
3551 | f = SERVICE_SUCCESS; | |
3552 | } else if (s->exec_command[SERVICE_EXEC_START]) { | |
3553 | ||
3554 | /* If this is a forked process, then we should | |
3555 | * ignore the return value if this was | |
3556 | * configured for the starter process */ | |
3557 | ||
3558 | if (s->exec_command[SERVICE_EXEC_START]->flags & EXEC_COMMAND_IGNORE_FAILURE) | |
3559 | f = SERVICE_SUCCESS; | |
3560 | } | |
3561 | ||
3562 | unit_log_process_exit( | |
3563 | u, | |
3564 | "Main process", | |
3565 | service_exec_command_to_string(SERVICE_EXEC_START), | |
3566 | f == SERVICE_SUCCESS, | |
3567 | code, status); | |
3568 | ||
3569 | if (s->result == SERVICE_SUCCESS) | |
3570 | s->result = f; | |
3571 | ||
3572 | if (s->main_command && | |
3573 | s->main_command->command_next && | |
3574 | s->type == SERVICE_ONESHOT && | |
3575 | f == SERVICE_SUCCESS) { | |
3576 | ||
3577 | /* There is another command to execute, so let's do that. */ | |
3578 | ||
3579 | log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state)); | |
3580 | service_run_next_main(s); | |
3581 | ||
3582 | } else { | |
3583 | s->main_command = NULL; | |
3584 | ||
3585 | /* Services with ExitType=cgroup do not act on main PID exiting, | |
3586 | * unless the cgroup is already empty */ | |
3587 | if (s->exit_type == SERVICE_EXIT_MAIN || cgroup_good(s) <= 0) { | |
3588 | /* The service exited, so the service is officially gone. */ | |
3589 | switch (s->state) { | |
3590 | ||
3591 | case SERVICE_START_POST: | |
3592 | case SERVICE_RELOAD: | |
3593 | /* If neither main nor control processes are running then | |
3594 | * the current state can never exit cleanly, hence immediately | |
3595 | * terminate the service. */ | |
3596 | if (control_pid_good(s) <= 0) | |
3597 | service_enter_stop(s, f); | |
3598 | ||
3599 | /* Otherwise need to wait until the operation is done. */ | |
3600 | break; | |
3601 | ||
3602 | case SERVICE_STOP: | |
3603 | /* Need to wait until the operation is done. */ | |
3604 | break; | |
3605 | ||
3606 | case SERVICE_START: | |
3607 | if (s->type == SERVICE_ONESHOT) { | |
3608 | /* This was our main goal, so let's go on */ | |
3609 | if (f == SERVICE_SUCCESS) | |
3610 | service_enter_start_post(s); | |
3611 | else | |
3612 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3613 | break; | |
3614 | } else if (s->type == SERVICE_NOTIFY) { | |
3615 | /* Only enter running through a notification, so that the | |
3616 | * SERVICE_START state signifies that no ready notification | |
3617 | * has been received */ | |
3618 | if (f != SERVICE_SUCCESS) | |
3619 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3620 | else if (!s->remain_after_exit || s->notify_access == NOTIFY_MAIN) | |
3621 | /* The service has never been and will never be active */ | |
3622 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); | |
3623 | break; | |
3624 | } | |
3625 | ||
3626 | _fallthrough_; | |
3627 | case SERVICE_RUNNING: | |
3628 | service_enter_running(s, f); | |
3629 | break; | |
3630 | ||
3631 | case SERVICE_STOP_WATCHDOG: | |
3632 | case SERVICE_STOP_SIGTERM: | |
3633 | case SERVICE_STOP_SIGKILL: | |
3634 | ||
3635 | if (control_pid_good(s) <= 0) | |
3636 | service_enter_stop_post(s, f); | |
3637 | ||
3638 | /* If there is still a control process, wait for that first */ | |
3639 | break; | |
3640 | ||
3641 | case SERVICE_STOP_POST: | |
3642 | ||
3643 | if (control_pid_good(s) <= 0) | |
3644 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
3645 | ||
3646 | break; | |
3647 | ||
3648 | case SERVICE_FINAL_WATCHDOG: | |
3649 | case SERVICE_FINAL_SIGTERM: | |
3650 | case SERVICE_FINAL_SIGKILL: | |
3651 | ||
3652 | if (control_pid_good(s) <= 0) | |
3653 | service_enter_dead(s, f, true); | |
3654 | break; | |
3655 | ||
3656 | default: | |
3657 | assert_not_reached(); | |
3658 | } | |
3659 | } | |
3660 | } | |
3661 | ||
3662 | } else if (s->control_pid == pid) { | |
3663 | const char *kind; | |
3664 | bool success; | |
3665 | ||
3666 | s->control_pid = 0; | |
3667 | ||
3668 | if (s->control_command) { | |
3669 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); | |
3670 | ||
3671 | if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE) | |
3672 | f = SERVICE_SUCCESS; | |
3673 | } | |
3674 | ||
3675 | /* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */ | |
3676 | if (s->state == SERVICE_CONDITION) { | |
3677 | if (f == SERVICE_FAILURE_EXIT_CODE && status < 255) { | |
3678 | UNIT(s)->condition_result = false; | |
3679 | f = SERVICE_SKIP_CONDITION; | |
3680 | success = true; | |
3681 | } else if (f == SERVICE_SUCCESS) { | |
3682 | UNIT(s)->condition_result = true; | |
3683 | success = true; | |
3684 | } else | |
3685 | success = false; | |
3686 | ||
3687 | kind = "Condition check process"; | |
3688 | } else { | |
3689 | kind = "Control process"; | |
3690 | success = f == SERVICE_SUCCESS; | |
3691 | } | |
3692 | ||
3693 | unit_log_process_exit( | |
3694 | u, | |
3695 | kind, | |
3696 | service_exec_command_to_string(s->control_command_id), | |
3697 | success, | |
3698 | code, status); | |
3699 | ||
3700 | if (s->state != SERVICE_RELOAD && s->result == SERVICE_SUCCESS) | |
3701 | s->result = f; | |
3702 | ||
3703 | if (s->control_command && | |
3704 | s->control_command->command_next && | |
3705 | f == SERVICE_SUCCESS) { | |
3706 | ||
3707 | /* There is another command to * | |
3708 | * execute, so let's do that. */ | |
3709 | ||
3710 | log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state)); | |
3711 | service_run_next_control(s); | |
3712 | ||
3713 | } else { | |
3714 | /* No further commands for this step, so let's | |
3715 | * figure out what to do next */ | |
3716 | ||
3717 | s->control_command = NULL; | |
3718 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
3719 | ||
3720 | log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state)); | |
3721 | ||
3722 | switch (s->state) { | |
3723 | ||
3724 | case SERVICE_CONDITION: | |
3725 | if (f == SERVICE_SUCCESS) | |
3726 | service_enter_start_pre(s); | |
3727 | else | |
3728 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3729 | break; | |
3730 | ||
3731 | case SERVICE_START_PRE: | |
3732 | if (f == SERVICE_SUCCESS) | |
3733 | service_enter_start(s); | |
3734 | else | |
3735 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3736 | break; | |
3737 | ||
3738 | case SERVICE_START: | |
3739 | if (s->type != SERVICE_FORKING) | |
3740 | /* Maybe spurious event due to a reload that changed the type? */ | |
3741 | break; | |
3742 | ||
3743 | if (f != SERVICE_SUCCESS) { | |
3744 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3745 | break; | |
3746 | } | |
3747 | ||
3748 | if (s->pid_file) { | |
3749 | bool has_start_post; | |
3750 | int r; | |
3751 | ||
3752 | /* Let's try to load the pid file here if we can. | |
3753 | * The PID file might actually be created by a START_POST | |
3754 | * script. In that case don't worry if the loading fails. */ | |
3755 | ||
3756 | has_start_post = s->exec_command[SERVICE_EXEC_START_POST]; | |
3757 | r = service_load_pid_file(s, !has_start_post); | |
3758 | if (!has_start_post && r < 0) { | |
3759 | r = service_demand_pid_file(s); | |
3760 | if (r < 0 || cgroup_good(s) == 0) | |
3761 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); | |
3762 | break; | |
3763 | } | |
3764 | } else | |
3765 | service_search_main_pid(s); | |
3766 | ||
3767 | service_enter_start_post(s); | |
3768 | break; | |
3769 | ||
3770 | case SERVICE_START_POST: | |
3771 | if (f != SERVICE_SUCCESS) { | |
3772 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3773 | break; | |
3774 | } | |
3775 | ||
3776 | if (s->pid_file) { | |
3777 | int r; | |
3778 | ||
3779 | r = service_load_pid_file(s, true); | |
3780 | if (r < 0) { | |
3781 | r = service_demand_pid_file(s); | |
3782 | if (r < 0 || cgroup_good(s) == 0) | |
3783 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); | |
3784 | break; | |
3785 | } | |
3786 | } else | |
3787 | service_search_main_pid(s); | |
3788 | ||
3789 | service_enter_running(s, SERVICE_SUCCESS); | |
3790 | break; | |
3791 | ||
3792 | case SERVICE_RELOAD: | |
3793 | if (f == SERVICE_SUCCESS) | |
3794 | if (service_load_pid_file(s, true) < 0) | |
3795 | service_search_main_pid(s); | |
3796 | ||
3797 | s->reload_result = f; | |
3798 | service_enter_running(s, SERVICE_SUCCESS); | |
3799 | break; | |
3800 | ||
3801 | case SERVICE_STOP: | |
3802 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3803 | break; | |
3804 | ||
3805 | case SERVICE_STOP_WATCHDOG: | |
3806 | case SERVICE_STOP_SIGTERM: | |
3807 | case SERVICE_STOP_SIGKILL: | |
3808 | if (main_pid_good(s) <= 0) | |
3809 | service_enter_stop_post(s, f); | |
3810 | ||
3811 | /* If there is still a service process around, wait until | |
3812 | * that one quit, too */ | |
3813 | break; | |
3814 | ||
3815 | case SERVICE_STOP_POST: | |
3816 | if (main_pid_good(s) <= 0) | |
3817 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
3818 | break; | |
3819 | ||
3820 | case SERVICE_FINAL_WATCHDOG: | |
3821 | case SERVICE_FINAL_SIGTERM: | |
3822 | case SERVICE_FINAL_SIGKILL: | |
3823 | if (main_pid_good(s) <= 0) | |
3824 | service_enter_dead(s, f, true); | |
3825 | break; | |
3826 | ||
3827 | case SERVICE_CLEANING: | |
3828 | ||
3829 | if (s->clean_result == SERVICE_SUCCESS) | |
3830 | s->clean_result = f; | |
3831 | ||
3832 | service_enter_dead(s, SERVICE_SUCCESS, false); | |
3833 | break; | |
3834 | ||
3835 | default: | |
3836 | assert_not_reached(); | |
3837 | } | |
3838 | } | |
3839 | } else /* Neither control nor main PID? If so, don't notify about anything */ | |
3840 | notify_dbus = false; | |
3841 | ||
3842 | /* Notify clients about changed exit status */ | |
3843 | if (notify_dbus) | |
3844 | unit_add_to_dbus_queue(u); | |
3845 | ||
3846 | /* We watch the main/control process otherwise we can't retrieve the unit they | |
3847 | * belong to with cgroupv1. But if they are not our direct child, we won't get a | |
3848 | * SIGCHLD for them. Therefore we need to look for others to watch so we can | |
3849 | * detect when the cgroup becomes empty. Note that the control process is always | |
3850 | * our child so it's pointless to watch all other processes. */ | |
3851 | if (!control_pid_good(s)) | |
3852 | if (!s->main_pid_known || s->main_pid_alien) | |
3853 | (void) unit_enqueue_rewatch_pids(u); | |
3854 | } | |
3855 | ||
3856 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { | |
3857 | Service *s = SERVICE(userdata); | |
3858 | ||
3859 | assert(s); | |
3860 | assert(source == s->timer_event_source); | |
3861 | ||
3862 | switch (s->state) { | |
3863 | ||
3864 | case SERVICE_CONDITION: | |
3865 | case SERVICE_START_PRE: | |
3866 | case SERVICE_START: | |
3867 | case SERVICE_START_POST: | |
3868 | switch (s->timeout_start_failure_mode) { | |
3869 | ||
3870 | case SERVICE_TIMEOUT_TERMINATE: | |
3871 | log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", service_state_to_string(s->state)); | |
3872 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
3873 | break; | |
3874 | ||
3875 | case SERVICE_TIMEOUT_ABORT: | |
3876 | log_unit_warning(UNIT(s), "%s operation timed out. Aborting.", service_state_to_string(s->state)); | |
3877 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3878 | break; | |
3879 | ||
3880 | case SERVICE_TIMEOUT_KILL: | |
3881 | if (s->kill_context.send_sigkill) { | |
3882 | log_unit_warning(UNIT(s), "%s operation timed out. Killing.", service_state_to_string(s->state)); | |
3883 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3884 | } else { | |
3885 | log_unit_warning(UNIT(s), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s->state)); | |
3886 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3887 | } | |
3888 | break; | |
3889 | ||
3890 | default: | |
3891 | assert_not_reached(); | |
3892 | } | |
3893 | break; | |
3894 | ||
3895 | case SERVICE_RUNNING: | |
3896 | log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping."); | |
3897 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); | |
3898 | break; | |
3899 | ||
3900 | case SERVICE_RELOAD: | |
3901 | log_unit_warning(UNIT(s), "Reload operation timed out. Killing reload process."); | |
3902 | service_kill_control_process(s); | |
3903 | s->reload_result = SERVICE_FAILURE_TIMEOUT; | |
3904 | service_enter_running(s, SERVICE_SUCCESS); | |
3905 | break; | |
3906 | ||
3907 | case SERVICE_STOP: | |
3908 | switch (s->timeout_stop_failure_mode) { | |
3909 | ||
3910 | case SERVICE_TIMEOUT_TERMINATE: | |
3911 | log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); | |
3912 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
3913 | break; | |
3914 | ||
3915 | case SERVICE_TIMEOUT_ABORT: | |
3916 | log_unit_warning(UNIT(s), "Stopping timed out. Aborting."); | |
3917 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3918 | break; | |
3919 | ||
3920 | case SERVICE_TIMEOUT_KILL: | |
3921 | if (s->kill_context.send_sigkill) { | |
3922 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); | |
3923 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3924 | } else { | |
3925 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); | |
3926 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3927 | } | |
3928 | break; | |
3929 | ||
3930 | default: | |
3931 | assert_not_reached(); | |
3932 | } | |
3933 | break; | |
3934 | ||
3935 | case SERVICE_STOP_WATCHDOG: | |
3936 | if (s->kill_context.send_sigkill) { | |
3937 | log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Killing."); | |
3938 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3939 | } else { | |
3940 | log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Skipping SIGKILL."); | |
3941 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3942 | } | |
3943 | break; | |
3944 | ||
3945 | case SERVICE_STOP_SIGTERM: | |
3946 | if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { | |
3947 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Aborting."); | |
3948 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3949 | } else if (s->kill_context.send_sigkill) { | |
3950 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing."); | |
3951 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3952 | } else { | |
3953 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL."); | |
3954 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3955 | } | |
3956 | ||
3957 | break; | |
3958 | ||
3959 | case SERVICE_STOP_SIGKILL: | |
3960 | /* Uh, we sent a SIGKILL and it is still not gone? | |
3961 | * Must be something we cannot kill, so let's just be | |
3962 | * weirded out and continue */ | |
3963 | ||
3964 | log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); | |
3965 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3966 | break; | |
3967 | ||
3968 | case SERVICE_STOP_POST: | |
3969 | switch (s->timeout_stop_failure_mode) { | |
3970 | ||
3971 | case SERVICE_TIMEOUT_TERMINATE: | |
3972 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating."); | |
3973 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
3974 | break; | |
3975 | ||
3976 | case SERVICE_TIMEOUT_ABORT: | |
3977 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Aborting."); | |
3978 | service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3979 | break; | |
3980 | ||
3981 | case SERVICE_TIMEOUT_KILL: | |
3982 | if (s->kill_context.send_sigkill) { | |
3983 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Killing."); | |
3984 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3985 | } else { | |
3986 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode."); | |
3987 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
3988 | } | |
3989 | break; | |
3990 | ||
3991 | default: | |
3992 | assert_not_reached(); | |
3993 | } | |
3994 | break; | |
3995 | ||
3996 | case SERVICE_FINAL_WATCHDOG: | |
3997 | if (s->kill_context.send_sigkill) { | |
3998 | log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Killing."); | |
3999 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4000 | } else { | |
4001 | log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode."); | |
4002 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
4003 | } | |
4004 | break; | |
4005 | ||
4006 | case SERVICE_FINAL_SIGTERM: | |
4007 | if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { | |
4008 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Aborting."); | |
4009 | service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4010 | } else if (s->kill_context.send_sigkill) { | |
4011 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Killing."); | |
4012 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4013 | } else { | |
4014 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode."); | |
4015 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
4016 | } | |
4017 | ||
4018 | break; | |
4019 | ||
4020 | case SERVICE_FINAL_SIGKILL: | |
4021 | log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode."); | |
4022 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); | |
4023 | break; | |
4024 | ||
4025 | case SERVICE_AUTO_RESTART: | |
4026 | if (s->restart_usec > 0) | |
4027 | log_unit_debug(UNIT(s), | |
4028 | "Service RestartSec=%s expired, scheduling restart.", | |
4029 | FORMAT_TIMESPAN(s->restart_usec, USEC_PER_SEC)); | |
4030 | else | |
4031 | log_unit_debug(UNIT(s), | |
4032 | "Service has no hold-off time (RestartSec=0), scheduling restart."); | |
4033 | ||
4034 | service_enter_restart(s); | |
4035 | break; | |
4036 | ||
4037 | case SERVICE_CLEANING: | |
4038 | log_unit_warning(UNIT(s), "Cleaning timed out. killing."); | |
4039 | ||
4040 | if (s->clean_result == SERVICE_SUCCESS) | |
4041 | s->clean_result = SERVICE_FAILURE_TIMEOUT; | |
4042 | ||
4043 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, 0); | |
4044 | break; | |
4045 | ||
4046 | default: | |
4047 | assert_not_reached(); | |
4048 | } | |
4049 | ||
4050 | return 0; | |
4051 | } | |
4052 | ||
4053 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { | |
4054 | Service *s = SERVICE(userdata); | |
4055 | usec_t watchdog_usec; | |
4056 | ||
4057 | assert(s); | |
4058 | assert(source == s->watchdog_event_source); | |
4059 | ||
4060 | watchdog_usec = service_get_watchdog_usec(s); | |
4061 | ||
4062 | if (UNIT(s)->manager->service_watchdogs) { | |
4063 | log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!", | |
4064 | FORMAT_TIMESPAN(watchdog_usec, 1)); | |
4065 | ||
4066 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); | |
4067 | } else | |
4068 | log_unit_warning(UNIT(s), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!", | |
4069 | FORMAT_TIMESPAN(watchdog_usec, 1)); | |
4070 | ||
4071 | return 0; | |
4072 | } | |
4073 | ||
4074 | static bool service_notify_message_authorized(Service *s, pid_t pid, FDSet *fds) { | |
4075 | assert(s); | |
4076 | ||
4077 | if (s->notify_access == NOTIFY_NONE) { | |
4078 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception is disabled.", pid); | |
4079 | return false; | |
4080 | } | |
4081 | ||
4082 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { | |
4083 | if (s->main_pid != 0) | |
4084 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid, s->main_pid); | |
4085 | else | |
4086 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", pid); | |
4087 | ||
4088 | return false; | |
4089 | } | |
4090 | ||
4091 | if (s->notify_access == NOTIFY_EXEC && pid != s->main_pid && pid != s->control_pid) { | |
4092 | if (s->main_pid != 0 && s->control_pid != 0) | |
4093 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT" and control PID "PID_FMT, | |
4094 | pid, s->main_pid, s->control_pid); | |
4095 | else if (s->main_pid != 0) | |
4096 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid, s->main_pid); | |
4097 | else if (s->control_pid != 0) | |
4098 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for control PID "PID_FMT, pid, s->control_pid); | |
4099 | else | |
4100 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID and control PID which are currently not known", pid); | |
4101 | ||
4102 | return false; | |
4103 | } | |
4104 | ||
4105 | return true; | |
4106 | } | |
4107 | ||
4108 | static void service_force_watchdog(Service *s) { | |
4109 | if (!UNIT(s)->manager->service_watchdogs) | |
4110 | return; | |
4111 | ||
4112 | log_unit_error(UNIT(s), "Watchdog request (last status: %s)!", | |
4113 | s->status_text ? s->status_text : "<unset>"); | |
4114 | ||
4115 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); | |
4116 | } | |
4117 | ||
4118 | static void service_notify_message( | |
4119 | Unit *u, | |
4120 | const struct ucred *ucred, | |
4121 | char * const *tags, | |
4122 | FDSet *fds) { | |
4123 | ||
4124 | Service *s = SERVICE(u); | |
4125 | bool notify_dbus = false; | |
4126 | const char *e; | |
4127 | char * const *i; | |
4128 | int r; | |
4129 | ||
4130 | assert(u); | |
4131 | assert(ucred); | |
4132 | ||
4133 | if (!service_notify_message_authorized(SERVICE(u), ucred->pid, fds)) | |
4134 | return; | |
4135 | ||
4136 | if (DEBUG_LOGGING) { | |
4137 | _cleanup_free_ char *cc = NULL; | |
4138 | ||
4139 | cc = strv_join(tags, ", "); | |
4140 | log_unit_debug(u, "Got notification message from PID "PID_FMT" (%s)", ucred->pid, isempty(cc) ? "n/a" : cc); | |
4141 | } | |
4142 | ||
4143 | /* Interpret MAINPID= */ | |
4144 | e = strv_find_startswith(tags, "MAINPID="); | |
4145 | if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) { | |
4146 | pid_t new_main_pid; | |
4147 | ||
4148 | if (parse_pid(e, &new_main_pid) < 0) | |
4149 | log_unit_warning(u, "Failed to parse MAINPID= field in notification message, ignoring: %s", e); | |
4150 | else if (!s->main_pid_known || new_main_pid != s->main_pid) { | |
4151 | ||
4152 | r = service_is_suitable_main_pid(s, new_main_pid, LOG_WARNING); | |
4153 | if (r == 0) { | |
4154 | /* The new main PID is a bit suspicious, which is OK if the sender is privileged. */ | |
4155 | ||
4156 | if (ucred->uid == 0) { | |
4157 | log_unit_debug(u, "New main PID "PID_FMT" does not belong to service, but we'll accept it as the request to change it came from a privileged process.", new_main_pid); | |
4158 | r = 1; | |
4159 | } else | |
4160 | log_unit_debug(u, "New main PID "PID_FMT" does not belong to service, refusing.", new_main_pid); | |
4161 | } | |
4162 | if (r > 0) { | |
4163 | service_set_main_pid(s, new_main_pid); | |
4164 | ||
4165 | r = unit_watch_pid(UNIT(s), new_main_pid, false); | |
4166 | if (r < 0) | |
4167 | log_unit_warning_errno(UNIT(s), r, "Failed to watch new main PID "PID_FMT" for service: %m", new_main_pid); | |
4168 | ||
4169 | notify_dbus = true; | |
4170 | } | |
4171 | } | |
4172 | } | |
4173 | ||
4174 | /* Interpret READY=/STOPPING=/RELOADING=. Last one wins. */ | |
4175 | STRV_FOREACH_BACKWARDS(i, tags) { | |
4176 | ||
4177 | if (streq(*i, "READY=1")) { | |
4178 | s->notify_state = NOTIFY_READY; | |
4179 | ||
4180 | /* Type=notify services inform us about completed | |
4181 | * initialization with READY=1 */ | |
4182 | if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START) | |
4183 | service_enter_start_post(s); | |
4184 | ||
4185 | /* Sending READY=1 while we are reloading informs us | |
4186 | * that the reloading is complete */ | |
4187 | if (s->state == SERVICE_RELOAD && s->control_pid == 0) | |
4188 | service_enter_running(s, SERVICE_SUCCESS); | |
4189 | ||
4190 | notify_dbus = true; | |
4191 | break; | |
4192 | ||
4193 | } else if (streq(*i, "RELOADING=1")) { | |
4194 | s->notify_state = NOTIFY_RELOADING; | |
4195 | ||
4196 | if (s->state == SERVICE_RUNNING) | |
4197 | service_enter_reload_by_notify(s); | |
4198 | ||
4199 | notify_dbus = true; | |
4200 | break; | |
4201 | ||
4202 | } else if (streq(*i, "STOPPING=1")) { | |
4203 | s->notify_state = NOTIFY_STOPPING; | |
4204 | ||
4205 | if (s->state == SERVICE_RUNNING) | |
4206 | service_enter_stop_by_notify(s); | |
4207 | ||
4208 | notify_dbus = true; | |
4209 | break; | |
4210 | } | |
4211 | } | |
4212 | ||
4213 | /* Interpret STATUS= */ | |
4214 | e = strv_find_startswith(tags, "STATUS="); | |
4215 | if (e) { | |
4216 | _cleanup_free_ char *t = NULL; | |
4217 | ||
4218 | if (!isempty(e)) { | |
4219 | /* Note that this size limit check is mostly paranoia: since the datagram size we are willing | |
4220 | * to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */ | |
4221 | if (strlen(e) > STATUS_TEXT_MAX) | |
4222 | log_unit_warning(u, "Status message overly long (%zu > %u), ignoring.", strlen(e), STATUS_TEXT_MAX); | |
4223 | else if (!utf8_is_valid(e)) | |
4224 | log_unit_warning(u, "Status message in notification message is not UTF-8 clean, ignoring."); | |
4225 | else { | |
4226 | t = strdup(e); | |
4227 | if (!t) | |
4228 | log_oom(); | |
4229 | } | |
4230 | } | |
4231 | ||
4232 | if (!streq_ptr(s->status_text, t)) { | |
4233 | free_and_replace(s->status_text, t); | |
4234 | notify_dbus = true; | |
4235 | } | |
4236 | } | |
4237 | ||
4238 | /* Interpret ERRNO= */ | |
4239 | e = strv_find_startswith(tags, "ERRNO="); | |
4240 | if (e) { | |
4241 | int status_errno; | |
4242 | ||
4243 | status_errno = parse_errno(e); | |
4244 | if (status_errno < 0) | |
4245 | log_unit_warning_errno(u, status_errno, | |
4246 | "Failed to parse ERRNO= field value '%s' in notification message: %m", e); | |
4247 | else if (s->status_errno != status_errno) { | |
4248 | s->status_errno = status_errno; | |
4249 | notify_dbus = true; | |
4250 | } | |
4251 | } | |
4252 | ||
4253 | /* Interpret EXTEND_TIMEOUT= */ | |
4254 | e = strv_find_startswith(tags, "EXTEND_TIMEOUT_USEC="); | |
4255 | if (e) { | |
4256 | usec_t extend_timeout_usec; | |
4257 | if (safe_atou64(e, &extend_timeout_usec) < 0) | |
4258 | log_unit_warning(u, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e); | |
4259 | else | |
4260 | service_extend_timeout(s, extend_timeout_usec); | |
4261 | } | |
4262 | ||
4263 | /* Interpret WATCHDOG= */ | |
4264 | e = strv_find_startswith(tags, "WATCHDOG="); | |
4265 | if (e) { | |
4266 | if (streq(e, "1")) | |
4267 | service_reset_watchdog(s); | |
4268 | else if (streq(e, "trigger")) | |
4269 | service_force_watchdog(s); | |
4270 | else | |
4271 | log_unit_warning(u, "Passed WATCHDOG= field is invalid, ignoring."); | |
4272 | } | |
4273 | ||
4274 | e = strv_find_startswith(tags, "WATCHDOG_USEC="); | |
4275 | if (e) { | |
4276 | usec_t watchdog_override_usec; | |
4277 | if (safe_atou64(e, &watchdog_override_usec) < 0) | |
4278 | log_unit_warning(u, "Failed to parse WATCHDOG_USEC=%s", e); | |
4279 | else | |
4280 | service_override_watchdog_timeout(s, watchdog_override_usec); | |
4281 | } | |
4282 | ||
4283 | /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases, | |
4284 | * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing | |
4285 | * fds, but optional when pushing in new fds, for compatibility reasons. */ | |
4286 | if (strv_find(tags, "FDSTOREREMOVE=1")) { | |
4287 | const char *name; | |
4288 | ||
4289 | name = strv_find_startswith(tags, "FDNAME="); | |
4290 | if (!name || !fdname_is_valid(name)) | |
4291 | log_unit_warning(u, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring."); | |
4292 | else | |
4293 | service_remove_fd_store(s, name); | |
4294 | ||
4295 | } else if (strv_find(tags, "FDSTORE=1")) { | |
4296 | const char *name; | |
4297 | ||
4298 | name = strv_find_startswith(tags, "FDNAME="); | |
4299 | if (name && !fdname_is_valid(name)) { | |
4300 | log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring."); | |
4301 | name = NULL; | |
4302 | } | |
4303 | ||
4304 | (void) service_add_fd_store_set(s, fds, name, !strv_contains(tags, "FDPOLL=0")); | |
4305 | } | |
4306 | ||
4307 | /* Notify clients about changed status or main pid */ | |
4308 | if (notify_dbus) | |
4309 | unit_add_to_dbus_queue(u); | |
4310 | } | |
4311 | ||
4312 | static int service_get_timeout(Unit *u, usec_t *timeout) { | |
4313 | Service *s = SERVICE(u); | |
4314 | uint64_t t; | |
4315 | int r; | |
4316 | ||
4317 | if (!s->timer_event_source) | |
4318 | return 0; | |
4319 | ||
4320 | r = sd_event_source_get_time(s->timer_event_source, &t); | |
4321 | if (r < 0) | |
4322 | return r; | |
4323 | if (t == USEC_INFINITY) | |
4324 | return 0; | |
4325 | ||
4326 | *timeout = t; | |
4327 | return 1; | |
4328 | } | |
4329 | ||
4330 | static void service_bus_name_owner_change(Unit *u, const char *new_owner) { | |
4331 | ||
4332 | Service *s = SERVICE(u); | |
4333 | int r; | |
4334 | ||
4335 | assert(s); | |
4336 | ||
4337 | if (new_owner) | |
4338 | log_unit_debug(u, "D-Bus name %s now owned by %s", s->bus_name, new_owner); | |
4339 | else | |
4340 | log_unit_debug(u, "D-Bus name %s now not owned by anyone.", s->bus_name); | |
4341 | ||
4342 | s->bus_name_good = new_owner; | |
4343 | ||
4344 | /* Track the current owner, so we can reconstruct changes after a daemon reload */ | |
4345 | r = free_and_strdup(&s->bus_name_owner, new_owner); | |
4346 | if (r < 0) { | |
4347 | log_unit_error_errno(u, r, "Unable to set new bus name owner %s: %m", new_owner); | |
4348 | return; | |
4349 | } | |
4350 | ||
4351 | if (s->type == SERVICE_DBUS) { | |
4352 | ||
4353 | /* service_enter_running() will figure out what to | |
4354 | * do */ | |
4355 | if (s->state == SERVICE_RUNNING) | |
4356 | service_enter_running(s, SERVICE_SUCCESS); | |
4357 | else if (s->state == SERVICE_START && new_owner) | |
4358 | service_enter_start_post(s); | |
4359 | ||
4360 | } else if (new_owner && | |
4361 | s->main_pid <= 0 && | |
4362 | IN_SET(s->state, | |
4363 | SERVICE_START, | |
4364 | SERVICE_START_POST, | |
4365 | SERVICE_RUNNING, | |
4366 | SERVICE_RELOAD)) { | |
4367 | ||
4368 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; | |
4369 | pid_t pid; | |
4370 | ||
4371 | /* Try to acquire PID from bus service */ | |
4372 | ||
4373 | r = sd_bus_get_name_creds(u->manager->api_bus, s->bus_name, SD_BUS_CREDS_PID, &creds); | |
4374 | if (r >= 0) | |
4375 | r = sd_bus_creds_get_pid(creds, &pid); | |
4376 | if (r >= 0) { | |
4377 | log_unit_debug(u, "D-Bus name %s is now owned by process " PID_FMT, s->bus_name, pid); | |
4378 | ||
4379 | service_set_main_pid(s, pid); | |
4380 | unit_watch_pid(UNIT(s), pid, false); | |
4381 | } | |
4382 | } | |
4383 | } | |
4384 | ||
4385 | int service_set_socket_fd( | |
4386 | Service *s, | |
4387 | int fd, | |
4388 | Socket *sock, | |
4389 | SocketPeer *peer, | |
4390 | bool selinux_context_net) { | |
4391 | ||
4392 | _cleanup_free_ char *peer_text = NULL; | |
4393 | int r; | |
4394 | ||
4395 | assert(s); | |
4396 | assert(fd >= 0); | |
4397 | ||
4398 | /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs | |
4399 | * to be configured. We take ownership of the passed fd on success. */ | |
4400 | ||
4401 | if (UNIT(s)->load_state != UNIT_LOADED) | |
4402 | return -EINVAL; | |
4403 | ||
4404 | if (s->socket_fd >= 0) | |
4405 | return -EBUSY; | |
4406 | ||
4407 | assert(!s->socket_peer); | |
4408 | ||
4409 | if (s->state != SERVICE_DEAD) | |
4410 | return -EAGAIN; | |
4411 | ||
4412 | if (getpeername_pretty(fd, true, &peer_text) >= 0) { | |
4413 | ||
4414 | if (UNIT(s)->description) { | |
4415 | _cleanup_free_ char *a = NULL; | |
4416 | ||
4417 | a = strjoin(UNIT(s)->description, " (", peer_text, ")"); | |
4418 | if (!a) | |
4419 | return -ENOMEM; | |
4420 | ||
4421 | r = unit_set_description(UNIT(s), a); | |
4422 | } else | |
4423 | r = unit_set_description(UNIT(s), peer_text); | |
4424 | if (r < 0) | |
4425 | return r; | |
4426 | } | |
4427 | ||
4428 | r = unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false, UNIT_DEPENDENCY_IMPLICIT); | |
4429 | if (r < 0) | |
4430 | return r; | |
4431 | ||
4432 | s->socket_fd = fd; | |
4433 | s->socket_peer = socket_peer_ref(peer); | |
4434 | s->socket_fd_selinux_context_net = selinux_context_net; | |
4435 | ||
4436 | unit_ref_set(&s->accept_socket, UNIT(s), UNIT(sock)); | |
4437 | return 0; | |
4438 | } | |
4439 | ||
4440 | static void service_reset_failed(Unit *u) { | |
4441 | Service *s = SERVICE(u); | |
4442 | ||
4443 | assert(s); | |
4444 | ||
4445 | if (s->state == SERVICE_FAILED) | |
4446 | service_set_state(s, SERVICE_DEAD); | |
4447 | ||
4448 | s->result = SERVICE_SUCCESS; | |
4449 | s->reload_result = SERVICE_SUCCESS; | |
4450 | s->clean_result = SERVICE_SUCCESS; | |
4451 | s->n_restarts = 0; | |
4452 | s->flush_n_restarts = false; | |
4453 | } | |
4454 | ||
4455 | static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { | |
4456 | Service *s = SERVICE(u); | |
4457 | ||
4458 | assert(s); | |
4459 | ||
4460 | return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error); | |
4461 | } | |
4462 | ||
4463 | static int service_main_pid(Unit *u) { | |
4464 | Service *s = SERVICE(u); | |
4465 | ||
4466 | assert(s); | |
4467 | ||
4468 | return s->main_pid; | |
4469 | } | |
4470 | ||
4471 | static int service_control_pid(Unit *u) { | |
4472 | Service *s = SERVICE(u); | |
4473 | ||
4474 | assert(s); | |
4475 | ||
4476 | return s->control_pid; | |
4477 | } | |
4478 | ||
4479 | static bool service_needs_console(Unit *u) { | |
4480 | Service *s = SERVICE(u); | |
4481 | ||
4482 | assert(s); | |
4483 | ||
4484 | /* We provide our own implementation of this here, instead of relying of the generic implementation | |
4485 | * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */ | |
4486 | ||
4487 | if (!exec_context_may_touch_console(&s->exec_context)) | |
4488 | return false; | |
4489 | ||
4490 | return IN_SET(s->state, | |
4491 | SERVICE_CONDITION, | |
4492 | SERVICE_START_PRE, | |
4493 | SERVICE_START, | |
4494 | SERVICE_START_POST, | |
4495 | SERVICE_RUNNING, | |
4496 | SERVICE_RELOAD, | |
4497 | SERVICE_STOP, | |
4498 | SERVICE_STOP_WATCHDOG, | |
4499 | SERVICE_STOP_SIGTERM, | |
4500 | SERVICE_STOP_SIGKILL, | |
4501 | SERVICE_STOP_POST, | |
4502 | SERVICE_FINAL_WATCHDOG, | |
4503 | SERVICE_FINAL_SIGTERM, | |
4504 | SERVICE_FINAL_SIGKILL); | |
4505 | } | |
4506 | ||
4507 | static int service_exit_status(Unit *u) { | |
4508 | Service *s = SERVICE(u); | |
4509 | ||
4510 | assert(u); | |
4511 | ||
4512 | if (s->main_exec_status.pid <= 0 || | |
4513 | !dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) | |
4514 | return -ENODATA; | |
4515 | ||
4516 | if (s->main_exec_status.code != CLD_EXITED) | |
4517 | return -EBADE; | |
4518 | ||
4519 | return s->main_exec_status.status; | |
4520 | } | |
4521 | ||
4522 | static const char* service_status_text(Unit *u) { | |
4523 | Service *s = SERVICE(u); | |
4524 | ||
4525 | assert(s); | |
4526 | ||
4527 | return s->status_text; | |
4528 | } | |
4529 | ||
4530 | static int service_clean(Unit *u, ExecCleanMask mask) { | |
4531 | _cleanup_strv_free_ char **l = NULL; | |
4532 | Service *s = SERVICE(u); | |
4533 | int r; | |
4534 | ||
4535 | assert(s); | |
4536 | assert(mask != 0); | |
4537 | ||
4538 | if (s->state != SERVICE_DEAD) | |
4539 | return -EBUSY; | |
4540 | ||
4541 | r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l); | |
4542 | if (r < 0) | |
4543 | return r; | |
4544 | ||
4545 | if (strv_isempty(l)) | |
4546 | return -EUNATCH; | |
4547 | ||
4548 | service_unwatch_control_pid(s); | |
4549 | s->clean_result = SERVICE_SUCCESS; | |
4550 | s->control_command = NULL; | |
4551 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
4552 | ||
4553 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->exec_context.timeout_clean_usec)); | |
4554 | if (r < 0) | |
4555 | goto fail; | |
4556 | ||
4557 | r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); | |
4558 | if (r < 0) | |
4559 | goto fail; | |
4560 | ||
4561 | service_set_state(s, SERVICE_CLEANING); | |
4562 | ||
4563 | return 0; | |
4564 | ||
4565 | fail: | |
4566 | log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m"); | |
4567 | s->clean_result = SERVICE_FAILURE_RESOURCES; | |
4568 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); | |
4569 | return r; | |
4570 | } | |
4571 | ||
4572 | static int service_can_clean(Unit *u, ExecCleanMask *ret) { | |
4573 | Service *s = SERVICE(u); | |
4574 | ||
4575 | assert(s); | |
4576 | ||
4577 | return exec_context_get_clean_mask(&s->exec_context, ret); | |
4578 | } | |
4579 | ||
4580 | static const char *service_finished_job(Unit *u, JobType t, JobResult result) { | |
4581 | if (t == JOB_START && | |
4582 | result == JOB_DONE && | |
4583 | SERVICE(u)->type == SERVICE_ONESHOT) | |
4584 | return "Finished %s."; | |
4585 | ||
4586 | /* Fall back to generic */ | |
4587 | return NULL; | |
4588 | } | |
4589 | ||
4590 | static int service_can_start(Unit *u) { | |
4591 | Service *s = SERVICE(u); | |
4592 | int r; | |
4593 | ||
4594 | assert(s); | |
4595 | ||
4596 | /* Make sure we don't enter a busy loop of some kind. */ | |
4597 | r = unit_test_start_limit(u); | |
4598 | if (r < 0) { | |
4599 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false); | |
4600 | return r; | |
4601 | } | |
4602 | ||
4603 | return 1; | |
4604 | } | |
4605 | ||
4606 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { | |
4607 | [SERVICE_RESTART_NO] = "no", | |
4608 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
4609 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", | |
4610 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", | |
4611 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", | |
4612 | [SERVICE_RESTART_ON_ABORT] = "on-abort", | |
4613 | [SERVICE_RESTART_ALWAYS] = "always", | |
4614 | }; | |
4615 | ||
4616 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
4617 | ||
4618 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
4619 | [SERVICE_SIMPLE] = "simple", | |
4620 | [SERVICE_FORKING] = "forking", | |
4621 | [SERVICE_ONESHOT] = "oneshot", | |
4622 | [SERVICE_DBUS] = "dbus", | |
4623 | [SERVICE_NOTIFY] = "notify", | |
4624 | [SERVICE_IDLE] = "idle", | |
4625 | [SERVICE_EXEC] = "exec", | |
4626 | }; | |
4627 | ||
4628 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
4629 | ||
4630 | static const char* const service_exit_type_table[_SERVICE_EXIT_TYPE_MAX] = { | |
4631 | [SERVICE_EXIT_MAIN] = "main", | |
4632 | [SERVICE_EXIT_CGROUP] = "cgroup", | |
4633 | }; | |
4634 | ||
4635 | DEFINE_STRING_TABLE_LOOKUP(service_exit_type, ServiceExitType); | |
4636 | ||
4637 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { | |
4638 | [SERVICE_EXEC_CONDITION] = "ExecCondition", | |
4639 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", | |
4640 | [SERVICE_EXEC_START] = "ExecStart", | |
4641 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
4642 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
4643 | [SERVICE_EXEC_STOP] = "ExecStop", | |
4644 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
4645 | }; | |
4646 | ||
4647 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
4648 | ||
4649 | static const char* const service_exec_ex_command_table[_SERVICE_EXEC_COMMAND_MAX] = { | |
4650 | [SERVICE_EXEC_CONDITION] = "ExecConditionEx", | |
4651 | [SERVICE_EXEC_START_PRE] = "ExecStartPreEx", | |
4652 | [SERVICE_EXEC_START] = "ExecStartEx", | |
4653 | [SERVICE_EXEC_START_POST] = "ExecStartPostEx", | |
4654 | [SERVICE_EXEC_RELOAD] = "ExecReloadEx", | |
4655 | [SERVICE_EXEC_STOP] = "ExecStopEx", | |
4656 | [SERVICE_EXEC_STOP_POST] = "ExecStopPostEx", | |
4657 | }; | |
4658 | ||
4659 | DEFINE_STRING_TABLE_LOOKUP(service_exec_ex_command, ServiceExecCommand); | |
4660 | ||
4661 | static const char* const notify_state_table[_NOTIFY_STATE_MAX] = { | |
4662 | [NOTIFY_UNKNOWN] = "unknown", | |
4663 | [NOTIFY_READY] = "ready", | |
4664 | [NOTIFY_RELOADING] = "reloading", | |
4665 | [NOTIFY_STOPPING] = "stopping", | |
4666 | }; | |
4667 | ||
4668 | DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); | |
4669 | ||
4670 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { | |
4671 | [SERVICE_SUCCESS] = "success", | |
4672 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
4673 | [SERVICE_FAILURE_PROTOCOL] = "protocol", | |
4674 | [SERVICE_FAILURE_TIMEOUT] = "timeout", | |
4675 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
4676 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
4677 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", | |
4678 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", | |
4679 | [SERVICE_FAILURE_START_LIMIT_HIT] = "start-limit-hit", | |
4680 | [SERVICE_FAILURE_OOM_KILL] = "oom-kill", | |
4681 | [SERVICE_SKIP_CONDITION] = "exec-condition", | |
4682 | }; | |
4683 | ||
4684 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
4685 | ||
4686 | static const char* const service_timeout_failure_mode_table[_SERVICE_TIMEOUT_FAILURE_MODE_MAX] = { | |
4687 | [SERVICE_TIMEOUT_TERMINATE] = "terminate", | |
4688 | [SERVICE_TIMEOUT_ABORT] = "abort", | |
4689 | [SERVICE_TIMEOUT_KILL] = "kill", | |
4690 | }; | |
4691 | ||
4692 | DEFINE_STRING_TABLE_LOOKUP(service_timeout_failure_mode, ServiceTimeoutFailureMode); | |
4693 | ||
4694 | const UnitVTable service_vtable = { | |
4695 | .object_size = sizeof(Service), | |
4696 | .exec_context_offset = offsetof(Service, exec_context), | |
4697 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
4698 | .kill_context_offset = offsetof(Service, kill_context), | |
4699 | .exec_runtime_offset = offsetof(Service, exec_runtime), | |
4700 | .dynamic_creds_offset = offsetof(Service, dynamic_creds), | |
4701 | ||
4702 | .sections = | |
4703 | "Unit\0" | |
4704 | "Service\0" | |
4705 | "Install\0", | |
4706 | .private_section = "Service", | |
4707 | ||
4708 | .can_transient = true, | |
4709 | .can_delegate = true, | |
4710 | .can_fail = true, | |
4711 | .can_set_managed_oom = true, | |
4712 | ||
4713 | .init = service_init, | |
4714 | .done = service_done, | |
4715 | .load = service_load, | |
4716 | .release_resources = service_release_resources, | |
4717 | ||
4718 | .coldplug = service_coldplug, | |
4719 | ||
4720 | .dump = service_dump, | |
4721 | ||
4722 | .start = service_start, | |
4723 | .stop = service_stop, | |
4724 | .reload = service_reload, | |
4725 | ||
4726 | .can_reload = service_can_reload, | |
4727 | ||
4728 | .kill = service_kill, | |
4729 | .clean = service_clean, | |
4730 | .can_clean = service_can_clean, | |
4731 | ||
4732 | .freeze = unit_freeze_vtable_common, | |
4733 | .thaw = unit_thaw_vtable_common, | |
4734 | ||
4735 | .serialize = service_serialize, | |
4736 | .deserialize_item = service_deserialize_item, | |
4737 | ||
4738 | .active_state = service_active_state, | |
4739 | .sub_state_to_string = service_sub_state_to_string, | |
4740 | ||
4741 | .will_restart = service_will_restart, | |
4742 | ||
4743 | .may_gc = service_may_gc, | |
4744 | ||
4745 | .sigchld_event = service_sigchld_event, | |
4746 | ||
4747 | .reset_failed = service_reset_failed, | |
4748 | ||
4749 | .notify_cgroup_empty = service_notify_cgroup_empty_event, | |
4750 | .notify_cgroup_oom = service_notify_cgroup_oom_event, | |
4751 | .notify_message = service_notify_message, | |
4752 | ||
4753 | .main_pid = service_main_pid, | |
4754 | .control_pid = service_control_pid, | |
4755 | ||
4756 | .bus_name_owner_change = service_bus_name_owner_change, | |
4757 | ||
4758 | .bus_set_property = bus_service_set_property, | |
4759 | .bus_commit_properties = bus_service_commit_properties, | |
4760 | ||
4761 | .get_timeout = service_get_timeout, | |
4762 | .needs_console = service_needs_console, | |
4763 | .exit_status = service_exit_status, | |
4764 | .status_text = service_status_text, | |
4765 | ||
4766 | .status_message_formats = { | |
4767 | .finished_start_job = { | |
4768 | [JOB_FAILED] = "Failed to start %s.", | |
4769 | }, | |
4770 | .finished_stop_job = { | |
4771 | [JOB_DONE] = "Stopped %s.", | |
4772 | [JOB_FAILED] = "Stopped (with error) %s.", | |
4773 | }, | |
4774 | .finished_job = service_finished_job, | |
4775 | }, | |
4776 | ||
4777 | .can_start = service_can_start, | |
4778 | }; |