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