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