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