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