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