]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/core/service.c
core: when a service's ExecStartPre= times out, skip ExecStop=
[thirdparty/systemd.git] / src / core / service.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <signal.h>
24 #include <unistd.h>
25
26 #include "alloc-util.h"
27 #include "async.h"
28 #include "bus-error.h"
29 #include "bus-kernel.h"
30 #include "bus-util.h"
31 #include "dbus-service.h"
32 #include "def.h"
33 #include "env-util.h"
34 #include "escape.h"
35 #include "exit-status.h"
36 #include "fd-util.h"
37 #include "fileio.h"
38 #include "formats-util.h"
39 #include "fs-util.h"
40 #include "load-dropin.h"
41 #include "load-fragment.h"
42 #include "log.h"
43 #include "manager.h"
44 #include "parse-util.h"
45 #include "path-util.h"
46 #include "process-util.h"
47 #include "service.h"
48 #include "signal-util.h"
49 #include "special.h"
50 #include "string-table.h"
51 #include "string-util.h"
52 #include "strv.h"
53 #include "unit-name.h"
54 #include "unit-printf.h"
55 #include "unit.h"
56 #include "utf8.h"
57 #include "util.h"
58
59 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
60 [SERVICE_DEAD] = UNIT_INACTIVE,
61 [SERVICE_START_PRE] = UNIT_ACTIVATING,
62 [SERVICE_START] = UNIT_ACTIVATING,
63 [SERVICE_START_POST] = UNIT_ACTIVATING,
64 [SERVICE_RUNNING] = UNIT_ACTIVE,
65 [SERVICE_EXITED] = UNIT_ACTIVE,
66 [SERVICE_RELOAD] = UNIT_RELOADING,
67 [SERVICE_STOP] = UNIT_DEACTIVATING,
68 [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING,
69 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
70 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
71 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
72 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
73 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
74 [SERVICE_FAILED] = UNIT_FAILED,
75 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
76 };
77
78 /* For Type=idle we never want to delay any other jobs, hence we
79 * consider idle jobs active as soon as we start working on them */
80 static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = {
81 [SERVICE_DEAD] = UNIT_INACTIVE,
82 [SERVICE_START_PRE] = UNIT_ACTIVE,
83 [SERVICE_START] = UNIT_ACTIVE,
84 [SERVICE_START_POST] = UNIT_ACTIVE,
85 [SERVICE_RUNNING] = UNIT_ACTIVE,
86 [SERVICE_EXITED] = UNIT_ACTIVE,
87 [SERVICE_RELOAD] = UNIT_RELOADING,
88 [SERVICE_STOP] = UNIT_DEACTIVATING,
89 [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING,
90 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
91 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
92 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
93 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
94 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
95 [SERVICE_FAILED] = UNIT_FAILED,
96 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
97 };
98
99 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata);
100 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata);
101 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata);
102
103 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f);
104 static void service_enter_reload_by_notify(Service *s);
105
106 static void service_init(Unit *u) {
107 Service *s = SERVICE(u);
108
109 assert(u);
110 assert(u->load_state == UNIT_STUB);
111
112 s->timeout_start_usec = u->manager->default_timeout_start_usec;
113 s->timeout_stop_usec = u->manager->default_timeout_stop_usec;
114 s->restart_usec = u->manager->default_restart_usec;
115 s->runtime_max_usec = USEC_INFINITY;
116 s->type = _SERVICE_TYPE_INVALID;
117 s->socket_fd = -1;
118 s->bus_endpoint_fd = -1;
119 s->stdin_fd = s->stdout_fd = s->stderr_fd = -1;
120 s->guess_main_pid = true;
121
122 RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst);
123
124 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
125 }
126
127 static void service_unwatch_control_pid(Service *s) {
128 assert(s);
129
130 if (s->control_pid <= 0)
131 return;
132
133 unit_unwatch_pid(UNIT(s), s->control_pid);
134 s->control_pid = 0;
135 }
136
137 static void service_unwatch_main_pid(Service *s) {
138 assert(s);
139
140 if (s->main_pid <= 0)
141 return;
142
143 unit_unwatch_pid(UNIT(s), s->main_pid);
144 s->main_pid = 0;
145 }
146
147 static void service_unwatch_pid_file(Service *s) {
148 if (!s->pid_file_pathspec)
149 return;
150
151 log_unit_debug(UNIT(s), "Stopping watch for PID file %s", s->pid_file_pathspec->path);
152 path_spec_unwatch(s->pid_file_pathspec);
153 path_spec_done(s->pid_file_pathspec);
154 s->pid_file_pathspec = mfree(s->pid_file_pathspec);
155 }
156
157 static int service_set_main_pid(Service *s, pid_t pid) {
158 pid_t ppid;
159
160 assert(s);
161
162 if (pid <= 1)
163 return -EINVAL;
164
165 if (pid == getpid())
166 return -EINVAL;
167
168 if (s->main_pid == pid && s->main_pid_known)
169 return 0;
170
171 if (s->main_pid != pid) {
172 service_unwatch_main_pid(s);
173 exec_status_start(&s->main_exec_status, pid);
174 }
175
176 s->main_pid = pid;
177 s->main_pid_known = true;
178
179 if (get_process_ppid(pid, &ppid) >= 0 && ppid != getpid()) {
180 log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", pid);
181 s->main_pid_alien = true;
182 } else
183 s->main_pid_alien = false;
184
185 return 0;
186 }
187
188 static void service_close_socket_fd(Service *s) {
189 assert(s);
190
191 s->socket_fd = asynchronous_close(s->socket_fd);
192 }
193
194 static void service_connection_unref(Service *s) {
195 assert(s);
196
197 if (!UNIT_ISSET(s->accept_socket))
198 return;
199
200 socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket)));
201 unit_ref_unset(&s->accept_socket);
202 }
203
204 static void service_stop_watchdog(Service *s) {
205 assert(s);
206
207 s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source);
208 s->watchdog_timestamp = DUAL_TIMESTAMP_NULL;
209 }
210
211 static void service_start_watchdog(Service *s) {
212 int r;
213
214 assert(s);
215
216 if (s->watchdog_usec <= 0)
217 return;
218
219 if (s->watchdog_event_source) {
220 r = sd_event_source_set_time(s->watchdog_event_source, usec_add(s->watchdog_timestamp.monotonic, s->watchdog_usec));
221 if (r < 0) {
222 log_unit_warning_errno(UNIT(s), r, "Failed to reset watchdog timer: %m");
223 return;
224 }
225
226 r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT);
227 } else {
228 r = sd_event_add_time(
229 UNIT(s)->manager->event,
230 &s->watchdog_event_source,
231 CLOCK_MONOTONIC,
232 usec_add(s->watchdog_timestamp.monotonic, s->watchdog_usec), 0,
233 service_dispatch_watchdog, s);
234 if (r < 0) {
235 log_unit_warning_errno(UNIT(s), r, "Failed to add watchdog timer: %m");
236 return;
237 }
238
239 (void) sd_event_source_set_description(s->watchdog_event_source, "service-watchdog");
240
241 /* Let's process everything else which might be a sign
242 * of living before we consider a service died. */
243 r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE);
244 }
245
246 if (r < 0)
247 log_unit_warning_errno(UNIT(s), r, "Failed to install watchdog timer: %m");
248 }
249
250 static void service_reset_watchdog(Service *s) {
251 assert(s);
252
253 dual_timestamp_get(&s->watchdog_timestamp);
254 service_start_watchdog(s);
255 }
256
257 static void service_fd_store_unlink(ServiceFDStore *fs) {
258
259 if (!fs)
260 return;
261
262 if (fs->service) {
263 assert(fs->service->n_fd_store > 0);
264 LIST_REMOVE(fd_store, fs->service->fd_store, fs);
265 fs->service->n_fd_store--;
266 }
267
268 if (fs->event_source) {
269 sd_event_source_set_enabled(fs->event_source, SD_EVENT_OFF);
270 sd_event_source_unref(fs->event_source);
271 }
272
273 free(fs->fdname);
274 safe_close(fs->fd);
275 free(fs);
276 }
277
278 static void service_release_resources(Unit *u) {
279 Service *s = SERVICE(u);
280
281 assert(s);
282
283 if (!s->fd_store && s->stdin_fd < 0 && s->stdout_fd < 0 && s->stderr_fd < 0)
284 return;
285
286 log_unit_debug(u, "Releasing all resources.");
287
288 s->stdin_fd = safe_close(s->stdin_fd);
289 s->stdout_fd = safe_close(s->stdout_fd);
290 s->stderr_fd = safe_close(s->stderr_fd);
291
292 while (s->fd_store)
293 service_fd_store_unlink(s->fd_store);
294
295 assert(s->n_fd_store == 0);
296 }
297
298 static void service_done(Unit *u) {
299 Service *s = SERVICE(u);
300
301 assert(s);
302
303 s->pid_file = mfree(s->pid_file);
304 s->status_text = mfree(s->status_text);
305 s->reboot_arg = mfree(s->reboot_arg);
306
307 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
308 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
309 s->control_command = NULL;
310 s->main_command = NULL;
311
312 exit_status_set_free(&s->restart_prevent_status);
313 exit_status_set_free(&s->restart_force_status);
314 exit_status_set_free(&s->success_status);
315
316 /* This will leak a process, but at least no memory or any of
317 * our resources */
318 service_unwatch_main_pid(s);
319 service_unwatch_control_pid(s);
320 service_unwatch_pid_file(s);
321
322 if (s->bus_name) {
323 unit_unwatch_bus_name(u, s->bus_name);
324 s->bus_name = mfree(s->bus_name);
325 }
326
327 s->bus_name_owner = mfree(s->bus_name_owner);
328
329 s->bus_endpoint_fd = safe_close(s->bus_endpoint_fd);
330 service_close_socket_fd(s);
331 service_connection_unref(s);
332
333 unit_ref_unset(&s->accept_socket);
334
335 service_stop_watchdog(s);
336
337 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
338
339 service_release_resources(u);
340 }
341
342 static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) {
343 ServiceFDStore *fs = userdata;
344
345 assert(e);
346 assert(fs);
347
348 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
349 service_fd_store_unlink(fs);
350 return 0;
351 }
352
353 static int service_add_fd_store(Service *s, int fd, const char *name) {
354 ServiceFDStore *fs;
355 int r;
356
357 assert(s);
358 assert(fd >= 0);
359
360 if (s->n_fd_store >= s->n_fd_store_max)
361 return 0;
362
363 LIST_FOREACH(fd_store, fs, s->fd_store) {
364 r = same_fd(fs->fd, fd);
365 if (r < 0)
366 return r;
367 if (r > 0) {
368 /* Already included */
369 safe_close(fd);
370 return 1;
371 }
372 }
373
374 fs = new0(ServiceFDStore, 1);
375 if (!fs)
376 return -ENOMEM;
377
378 fs->fd = fd;
379 fs->service = s;
380 fs->fdname = strdup(name ?: "stored");
381 if (!fs->fdname) {
382 free(fs);
383 return -ENOMEM;
384 }
385
386 r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fd, 0, on_fd_store_io, fs);
387 if (r < 0) {
388 free(fs->fdname);
389 free(fs);
390 return r;
391 }
392
393 (void) sd_event_source_set_description(fs->event_source, "service-fd-store");
394
395 LIST_PREPEND(fd_store, s->fd_store, fs);
396 s->n_fd_store++;
397
398 return 1;
399 }
400
401 static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name) {
402 int r;
403
404 assert(s);
405
406 if (fdset_size(fds) <= 0)
407 return 0;
408
409 while (s->n_fd_store < s->n_fd_store_max) {
410 _cleanup_close_ int fd = -1;
411
412 fd = fdset_steal_first(fds);
413 if (fd < 0)
414 break;
415
416 r = service_add_fd_store(s, fd, name);
417 if (r < 0)
418 return log_unit_error_errno(UNIT(s), r, "Couldn't add fd to fd store: %m");
419 if (r > 0) {
420 log_unit_debug(UNIT(s), "Added fd to fd store.");
421 fd = -1;
422 }
423 }
424
425 if (fdset_size(fds) > 0)
426 log_unit_warning(UNIT(s), "Tried to store more fds than FileDescriptorStoreMax=%u allows, closing remaining.", s->n_fd_store_max);
427
428 return 0;
429 }
430
431 static int service_arm_timer(Service *s, usec_t usec) {
432 int r;
433
434 assert(s);
435
436 if (s->timer_event_source) {
437 r = sd_event_source_set_time(s->timer_event_source, usec);
438 if (r < 0)
439 return r;
440
441 return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT);
442 }
443
444 if (usec == USEC_INFINITY)
445 return 0;
446
447 r = sd_event_add_time(
448 UNIT(s)->manager->event,
449 &s->timer_event_source,
450 CLOCK_MONOTONIC,
451 usec, 0,
452 service_dispatch_timer, s);
453 if (r < 0)
454 return r;
455
456 (void) sd_event_source_set_description(s->timer_event_source, "service-timer");
457
458 return 0;
459 }
460
461 static int service_verify(Service *s) {
462 assert(s);
463
464 if (UNIT(s)->load_state != UNIT_LOADED)
465 return 0;
466
467 if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]) {
468 log_unit_error(UNIT(s), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
469 return -EINVAL;
470 }
471
472 if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) {
473 log_unit_error(UNIT(s), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
474 return -EINVAL;
475 }
476
477 if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START]) {
478 log_unit_error(UNIT(s), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
479 return -EINVAL;
480 }
481
482 if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) {
483 log_unit_error(UNIT(s), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
484 return -EINVAL;
485 }
486
487 if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) {
488 log_unit_error(UNIT(s), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
489 return -EINVAL;
490 }
491
492 if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) {
493 log_unit_error(UNIT(s), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
494 return -EINVAL;
495 }
496
497 if (s->type == SERVICE_DBUS && !s->bus_name) {
498 log_unit_error(UNIT(s), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
499 return -EINVAL;
500 }
501
502 if (s->bus_name && s->type != SERVICE_DBUS)
503 log_unit_warning(UNIT(s), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
504
505 if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) {
506 log_unit_error(UNIT(s), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
507 return -EINVAL;
508 }
509
510 if (s->usb_function_descriptors && !s->usb_function_strings)
511 log_unit_warning(UNIT(s), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
512
513 if (!s->usb_function_descriptors && s->usb_function_strings)
514 log_unit_warning(UNIT(s), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
515
516 if (s->runtime_max_usec != USEC_INFINITY && s->type == SERVICE_ONESHOT)
517 log_unit_warning(UNIT(s), "MaxRuntimeSec= has no effect in combination with Type=oneshot. Ignoring.");
518
519 return 0;
520 }
521
522 static int service_add_default_dependencies(Service *s) {
523 int r;
524
525 assert(s);
526
527 if (!UNIT(s)->default_dependencies)
528 return 0;
529
530 /* Add a number of automatic dependencies useful for the
531 * majority of services. */
532
533 if (UNIT(s)->manager->running_as == MANAGER_SYSTEM) {
534 /* First, pull in the really early boot stuff, and
535 * require it, so that we fail if we can't acquire
536 * it. */
537
538 r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true);
539 if (r < 0)
540 return r;
541 } else {
542
543 /* In the --user instance there's no sysinit.target,
544 * in that case require basic.target instead. */
545
546 r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true);
547 if (r < 0)
548 return r;
549 }
550
551 /* Second, if the rest of the base system is in the same
552 * transaction, order us after it, but do not pull it in or
553 * even require it. */
554 r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true);
555 if (r < 0)
556 return r;
557
558 /* Third, add us in for normal shutdown. */
559 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true);
560 }
561
562 static void service_fix_output(Service *s) {
563 assert(s);
564
565 /* If nothing has been explicitly configured, patch default
566 * output in. If input is socket/tty we avoid this however,
567 * since in that case we want output to default to the same
568 * place as we read input from. */
569
570 if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT &&
571 s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
572 s->exec_context.std_input == EXEC_INPUT_NULL)
573 s->exec_context.std_error = UNIT(s)->manager->default_std_error;
574
575 if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT &&
576 s->exec_context.std_input == EXEC_INPUT_NULL)
577 s->exec_context.std_output = UNIT(s)->manager->default_std_output;
578 }
579
580 static int service_setup_bus_name(Service *s) {
581 int r;
582
583 assert(s);
584
585 if (!s->bus_name)
586 return 0;
587
588 if (is_kdbus_available()) {
589 const char *n;
590
591 n = strjoina(s->bus_name, ".busname");
592 r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, n, NULL, true);
593 if (r < 0)
594 return log_unit_error_errno(UNIT(s), r, "Failed to add dependency to .busname unit: %m");
595
596 } else {
597 /* If kdbus is not available, we know the dbus socket is required, hence pull it in, and require it */
598 r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true);
599 if (r < 0)
600 return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m");
601 }
602
603 /* Regardless if kdbus is used or not, we always want to be ordered against dbus.socket if both are in the transaction. */
604 r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_DBUS_SOCKET, NULL, true);
605 if (r < 0)
606 return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m");
607
608 r = unit_watch_bus_name(UNIT(s), s->bus_name);
609 if (r == -EEXIST)
610 return log_unit_error_errno(UNIT(s), r, "Two services allocated for the same bus name %s, refusing operation.", s->bus_name);
611 if (r < 0)
612 return log_unit_error_errno(UNIT(s), r, "Cannot watch bus name %s: %m", s->bus_name);
613
614 return 0;
615 }
616
617 static int service_add_extras(Service *s) {
618 int r;
619
620 assert(s);
621
622 if (s->type == _SERVICE_TYPE_INVALID) {
623 /* Figure out a type automatically */
624 if (s->bus_name)
625 s->type = SERVICE_DBUS;
626 else if (s->exec_command[SERVICE_EXEC_START])
627 s->type = SERVICE_SIMPLE;
628 else
629 s->type = SERVICE_ONESHOT;
630 }
631
632 /* Oneshot services have disabled start timeout by default */
633 if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined)
634 s->timeout_start_usec = USEC_INFINITY;
635
636 service_fix_output(s);
637
638 r = unit_patch_contexts(UNIT(s));
639 if (r < 0)
640 return r;
641
642 r = unit_add_exec_dependencies(UNIT(s), &s->exec_context);
643 if (r < 0)
644 return r;
645
646 r = unit_set_default_slice(UNIT(s));
647 if (r < 0)
648 return r;
649
650 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
651 s->notify_access = NOTIFY_MAIN;
652
653 if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE)
654 s->notify_access = NOTIFY_MAIN;
655
656 r = service_add_default_dependencies(s);
657 if (r < 0)
658 return r;
659
660 r = service_setup_bus_name(s);
661 if (r < 0)
662 return r;
663
664 return 0;
665 }
666
667 static int service_load(Unit *u) {
668 Service *s = SERVICE(u);
669 int r;
670
671 assert(s);
672
673 /* Load a .service file */
674 r = unit_load_fragment(u);
675 if (r < 0)
676 return r;
677
678 /* Still nothing found? Then let's give up */
679 if (u->load_state == UNIT_STUB)
680 return -ENOENT;
681
682 /* This is a new unit? Then let's add in some extras */
683 if (u->load_state == UNIT_LOADED) {
684
685 /* We were able to load something, then let's add in
686 * the dropin directories. */
687 r = unit_load_dropin(u);
688 if (r < 0)
689 return r;
690
691 /* This is a new unit? Then let's add in some
692 * extras */
693 r = service_add_extras(s);
694 if (r < 0)
695 return r;
696 }
697
698 return service_verify(s);
699 }
700
701 static void service_dump(Unit *u, FILE *f, const char *prefix) {
702 ServiceExecCommand c;
703 Service *s = SERVICE(u);
704 const char *prefix2;
705
706 assert(s);
707
708 prefix = strempty(prefix);
709 prefix2 = strjoina(prefix, "\t");
710
711 fprintf(f,
712 "%sService State: %s\n"
713 "%sResult: %s\n"
714 "%sReload Result: %s\n"
715 "%sPermissionsStartOnly: %s\n"
716 "%sRootDirectoryStartOnly: %s\n"
717 "%sRemainAfterExit: %s\n"
718 "%sGuessMainPID: %s\n"
719 "%sType: %s\n"
720 "%sRestart: %s\n"
721 "%sNotifyAccess: %s\n"
722 "%sNotifyState: %s\n",
723 prefix, service_state_to_string(s->state),
724 prefix, service_result_to_string(s->result),
725 prefix, service_result_to_string(s->reload_result),
726 prefix, yes_no(s->permissions_start_only),
727 prefix, yes_no(s->root_directory_start_only),
728 prefix, yes_no(s->remain_after_exit),
729 prefix, yes_no(s->guess_main_pid),
730 prefix, service_type_to_string(s->type),
731 prefix, service_restart_to_string(s->restart),
732 prefix, notify_access_to_string(s->notify_access),
733 prefix, notify_state_to_string(s->notify_state));
734
735 if (s->control_pid > 0)
736 fprintf(f,
737 "%sControl PID: "PID_FMT"\n",
738 prefix, s->control_pid);
739
740 if (s->main_pid > 0)
741 fprintf(f,
742 "%sMain PID: "PID_FMT"\n"
743 "%sMain PID Known: %s\n"
744 "%sMain PID Alien: %s\n",
745 prefix, s->main_pid,
746 prefix, yes_no(s->main_pid_known),
747 prefix, yes_no(s->main_pid_alien));
748
749 if (s->pid_file)
750 fprintf(f,
751 "%sPIDFile: %s\n",
752 prefix, s->pid_file);
753
754 if (s->bus_name)
755 fprintf(f,
756 "%sBusName: %s\n"
757 "%sBus Name Good: %s\n",
758 prefix, s->bus_name,
759 prefix, yes_no(s->bus_name_good));
760
761 kill_context_dump(&s->kill_context, f, prefix);
762 exec_context_dump(&s->exec_context, f, prefix);
763
764 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
765
766 if (!s->exec_command[c])
767 continue;
768
769 fprintf(f, "%s-> %s:\n",
770 prefix, service_exec_command_to_string(c));
771
772 exec_command_dump_list(s->exec_command[c], f, prefix2);
773 }
774
775 if (s->status_text)
776 fprintf(f, "%sStatus Text: %s\n",
777 prefix, s->status_text);
778
779 if (s->n_fd_store_max > 0)
780 fprintf(f,
781 "%sFile Descriptor Store Max: %u\n"
782 "%sFile Descriptor Store Current: %u\n",
783 prefix, s->n_fd_store_max,
784 prefix, s->n_fd_store);
785 }
786
787 static int service_load_pid_file(Service *s, bool may_warn) {
788 _cleanup_free_ char *k = NULL;
789 int r;
790 pid_t pid;
791
792 assert(s);
793
794 if (!s->pid_file)
795 return -ENOENT;
796
797 r = read_one_line_file(s->pid_file, &k);
798 if (r < 0) {
799 if (may_warn)
800 log_unit_info_errno(UNIT(s), r, "PID file %s not readable (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state));
801 return r;
802 }
803
804 r = parse_pid(k, &pid);
805 if (r < 0) {
806 if (may_warn)
807 log_unit_info_errno(UNIT(s), r, "Failed to read PID from file %s: %m", s->pid_file);
808 return r;
809 }
810
811 if (!pid_is_alive(pid)) {
812 if (may_warn)
813 log_unit_info(UNIT(s), "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file);
814 return -ESRCH;
815 }
816
817 if (s->main_pid_known) {
818 if (pid == s->main_pid)
819 return 0;
820
821 log_unit_debug(UNIT(s), "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid);
822
823 service_unwatch_main_pid(s);
824 s->main_pid_known = false;
825 } else
826 log_unit_debug(UNIT(s), "Main PID loaded: "PID_FMT, pid);
827
828 r = service_set_main_pid(s, pid);
829 if (r < 0)
830 return r;
831
832 r = unit_watch_pid(UNIT(s), pid);
833 if (r < 0) {
834 /* FIXME: we need to do something here */
835 log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" for service: %m", pid);
836 return r;
837 }
838
839 return 0;
840 }
841
842 static int service_search_main_pid(Service *s) {
843 pid_t pid = 0;
844 int r;
845
846 assert(s);
847
848 /* If we know it anyway, don't ever fallback to unreliable
849 * heuristics */
850 if (s->main_pid_known)
851 return 0;
852
853 if (!s->guess_main_pid)
854 return 0;
855
856 assert(s->main_pid <= 0);
857
858 r = unit_search_main_pid(UNIT(s), &pid);
859 if (r < 0)
860 return r;
861
862 log_unit_debug(UNIT(s), "Main PID guessed: "PID_FMT, pid);
863 r = service_set_main_pid(s, pid);
864 if (r < 0)
865 return r;
866
867 r = unit_watch_pid(UNIT(s), pid);
868 if (r < 0) {
869 /* FIXME: we need to do something here */
870 log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" from: %m", pid);
871 return r;
872 }
873
874 return 0;
875 }
876
877 static void service_set_state(Service *s, ServiceState state) {
878 ServiceState old_state;
879 const UnitActiveState *table;
880
881 assert(s);
882
883 table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
884
885 old_state = s->state;
886 s->state = state;
887
888 service_unwatch_pid_file(s);
889
890 if (!IN_SET(state,
891 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
892 SERVICE_RUNNING,
893 SERVICE_RELOAD,
894 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
895 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL,
896 SERVICE_AUTO_RESTART))
897 s->timer_event_source = sd_event_source_unref(s->timer_event_source);
898
899 if (!IN_SET(state,
900 SERVICE_START, SERVICE_START_POST,
901 SERVICE_RUNNING, SERVICE_RELOAD,
902 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
903 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
904 service_unwatch_main_pid(s);
905 s->main_command = NULL;
906 }
907
908 if (!IN_SET(state,
909 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
910 SERVICE_RELOAD,
911 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
912 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
913 service_unwatch_control_pid(s);
914 s->control_command = NULL;
915 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
916 }
917
918 if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
919 unit_unwatch_all_pids(UNIT(s));
920
921 if (!IN_SET(state,
922 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
923 SERVICE_RUNNING, SERVICE_RELOAD,
924 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
925 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) &&
926 !(state == SERVICE_DEAD && UNIT(s)->job)) {
927 service_close_socket_fd(s);
928 service_connection_unref(s);
929 }
930
931 if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
932 service_stop_watchdog(s);
933
934 /* For the inactive states unit_notify() will trim the cgroup,
935 * but for exit we have to do that ourselves... */
936 if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0)
937 unit_prune_cgroup(UNIT(s));
938
939 /* For remain_after_exit services, let's see if we can "release" the
940 * hold on the console, since unit_notify() only does that in case of
941 * change of state */
942 if (state == SERVICE_EXITED &&
943 s->remain_after_exit &&
944 UNIT(s)->manager->n_on_console > 0) {
945
946 ExecContext *ec;
947
948 ec = unit_get_exec_context(UNIT(s));
949 if (ec && exec_context_may_touch_console(ec)) {
950 Manager *m = UNIT(s)->manager;
951
952 m->n_on_console --;
953 if (m->n_on_console == 0)
954 /* unset no_console_output flag, since the console is free */
955 m->no_console_output = false;
956 }
957 }
958
959 if (old_state != state)
960 log_unit_debug(UNIT(s), "Changed %s -> %s", service_state_to_string(old_state), service_state_to_string(state));
961
962 unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS);
963 }
964
965 static usec_t service_coldplug_timeout(Service *s) {
966 assert(s);
967
968 switch (s->deserialized_state) {
969
970 case SERVICE_START_PRE:
971 case SERVICE_START:
972 case SERVICE_START_POST:
973 case SERVICE_RELOAD:
974 return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_start_usec);
975
976 case SERVICE_RUNNING:
977 return usec_add(UNIT(s)->active_enter_timestamp.monotonic, s->runtime_max_usec);
978
979 case SERVICE_STOP:
980 case SERVICE_STOP_SIGABRT:
981 case SERVICE_STOP_SIGTERM:
982 case SERVICE_STOP_SIGKILL:
983 case SERVICE_STOP_POST:
984 case SERVICE_FINAL_SIGTERM:
985 case SERVICE_FINAL_SIGKILL:
986 return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec);
987
988 case SERVICE_AUTO_RESTART:
989 return usec_add(UNIT(s)->inactive_enter_timestamp.monotonic, s->restart_usec);
990
991 default:
992 return USEC_INFINITY;
993 }
994 }
995
996 static int service_coldplug(Unit *u) {
997 Service *s = SERVICE(u);
998 int r;
999
1000 assert(s);
1001 assert(s->state == SERVICE_DEAD);
1002
1003 if (s->deserialized_state == s->state)
1004 return 0;
1005
1006 r = service_arm_timer(s, service_coldplug_timeout(s));
1007 if (r < 0)
1008 return r;
1009
1010 if (s->main_pid > 0 &&
1011 pid_is_unwaited(s->main_pid) &&
1012 ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) ||
1013 IN_SET(s->deserialized_state,
1014 SERVICE_START, SERVICE_START_POST,
1015 SERVICE_RUNNING, SERVICE_RELOAD,
1016 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1017 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) {
1018 r = unit_watch_pid(UNIT(s), s->main_pid);
1019 if (r < 0)
1020 return r;
1021 }
1022
1023 if (s->control_pid > 0 &&
1024 pid_is_unwaited(s->control_pid) &&
1025 IN_SET(s->deserialized_state,
1026 SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST,
1027 SERVICE_RELOAD,
1028 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
1029 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) {
1030 r = unit_watch_pid(UNIT(s), s->control_pid);
1031 if (r < 0)
1032 return r;
1033 }
1034
1035 if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART))
1036 unit_watch_all_pids(UNIT(s));
1037
1038 if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1039 service_start_watchdog(s);
1040
1041 service_set_state(s, s->deserialized_state);
1042 return 0;
1043 }
1044
1045 static int service_collect_fds(Service *s, int **fds, char ***fd_names) {
1046 _cleanup_strv_free_ char **rfd_names = NULL;
1047 _cleanup_free_ int *rfds = NULL;
1048 int rn_fds = 0, r;
1049
1050 assert(s);
1051 assert(fds);
1052 assert(fd_names);
1053
1054 if (s->socket_fd >= 0) {
1055
1056 /* Pass the per-connection socket */
1057
1058 rfds = new(int, 1);
1059 if (!rfds)
1060 return -ENOMEM;
1061 rfds[0] = s->socket_fd;
1062
1063 rfd_names = strv_new("connection", NULL);
1064 if (!rfd_names)
1065 return -ENOMEM;
1066
1067 rn_fds = 1;
1068 } else {
1069 Iterator i;
1070 Unit *u;
1071
1072 /* Pass all our configured sockets for singleton services */
1073
1074 SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) {
1075 _cleanup_free_ int *cfds = NULL;
1076 Socket *sock;
1077 int cn_fds;
1078
1079 if (u->type != UNIT_SOCKET)
1080 continue;
1081
1082 sock = SOCKET(u);
1083
1084 cn_fds = socket_collect_fds(sock, &cfds);
1085 if (cn_fds < 0)
1086 return cn_fds;
1087
1088 if (cn_fds <= 0)
1089 continue;
1090
1091 if (!rfds) {
1092 rfds = cfds;
1093 rn_fds = cn_fds;
1094
1095 cfds = NULL;
1096 } else {
1097 int *t;
1098
1099 t = realloc(rfds, (rn_fds + cn_fds) * sizeof(int));
1100 if (!t)
1101 return -ENOMEM;
1102
1103 memcpy(t + rn_fds, cfds, cn_fds * sizeof(int));
1104
1105 rfds = t;
1106 rn_fds += cn_fds;
1107 }
1108
1109 r = strv_extend_n(&rfd_names, socket_fdname(sock), cn_fds);
1110 if (r < 0)
1111 return r;
1112 }
1113 }
1114
1115 if (s->n_fd_store > 0) {
1116 ServiceFDStore *fs;
1117 char **nl;
1118 int *t;
1119
1120 t = realloc(rfds, (rn_fds + s->n_fd_store) * sizeof(int));
1121 if (!t)
1122 return -ENOMEM;
1123
1124 rfds = t;
1125
1126 nl = realloc(rfd_names, (rn_fds + s->n_fd_store + 1) * sizeof(char*));
1127 if (!nl)
1128 return -ENOMEM;
1129
1130 rfd_names = nl;
1131
1132 LIST_FOREACH(fd_store, fs, s->fd_store) {
1133 rfds[rn_fds] = fs->fd;
1134 rfd_names[rn_fds] = strdup(strempty(fs->fdname));
1135 if (!rfd_names[rn_fds])
1136 return -ENOMEM;
1137
1138 rn_fds++;
1139 }
1140
1141 rfd_names[rn_fds] = NULL;
1142 }
1143
1144 *fds = rfds;
1145 *fd_names = rfd_names;
1146
1147 rfds = NULL;
1148 rfd_names = NULL;
1149
1150 return rn_fds;
1151 }
1152
1153 static int service_spawn(
1154 Service *s,
1155 ExecCommand *c,
1156 usec_t timeout,
1157 bool pass_fds,
1158 bool apply_permissions,
1159 bool apply_chroot,
1160 bool apply_tty_stdin,
1161 bool is_control,
1162 pid_t *_pid) {
1163
1164 _cleanup_strv_free_ char **argv = NULL, **final_env = NULL, **our_env = NULL, **fd_names = NULL;
1165 _cleanup_free_ char *bus_endpoint_path = NULL;
1166 _cleanup_free_ int *fds = NULL;
1167 unsigned n_fds = 0, n_env = 0;
1168 const char *path;
1169 pid_t pid;
1170
1171 ExecParameters exec_params = {
1172 .apply_permissions = apply_permissions,
1173 .apply_chroot = apply_chroot,
1174 .apply_tty_stdin = apply_tty_stdin,
1175 .bus_endpoint_fd = -1,
1176 .stdin_fd = -1,
1177 .stdout_fd = -1,
1178 .stderr_fd = -1,
1179 };
1180
1181 int r;
1182
1183 assert(s);
1184 assert(c);
1185 assert(_pid);
1186
1187 (void) unit_realize_cgroup(UNIT(s));
1188 if (s->reset_cpu_usage) {
1189 (void) unit_reset_cpu_usage(UNIT(s));
1190 s->reset_cpu_usage = false;
1191 }
1192
1193 r = unit_setup_exec_runtime(UNIT(s));
1194 if (r < 0)
1195 return r;
1196
1197 if (pass_fds ||
1198 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1199 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1200 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1201
1202 r = service_collect_fds(s, &fds, &fd_names);
1203 if (r < 0)
1204 return r;
1205
1206 n_fds = r;
1207 }
1208
1209 r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), timeout));
1210 if (r < 0)
1211 return r;
1212
1213 r = unit_full_printf_strv(UNIT(s), c->argv, &argv);
1214 if (r < 0)
1215 return r;
1216
1217 our_env = new0(char*, 6);
1218 if (!our_env)
1219 return -ENOMEM;
1220
1221 if (is_control ? s->notify_access == NOTIFY_ALL : s->notify_access != NOTIFY_NONE)
1222 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0)
1223 return -ENOMEM;
1224
1225 if (s->main_pid > 0)
1226 if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0)
1227 return -ENOMEM;
1228
1229 if (UNIT(s)->manager->running_as != MANAGER_SYSTEM)
1230 if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0)
1231 return -ENOMEM;
1232
1233 if (s->socket_fd >= 0) {
1234 union sockaddr_union sa;
1235 socklen_t salen = sizeof(sa);
1236
1237 r = getpeername(s->socket_fd, &sa.sa, &salen);
1238 if (r < 0)
1239 return -errno;
1240
1241 if (IN_SET(sa.sa.sa_family, AF_INET, AF_INET6)) {
1242 _cleanup_free_ char *addr = NULL;
1243 char *t;
1244 int port;
1245
1246 r = sockaddr_pretty(&sa.sa, salen, true, false, &addr);
1247 if (r < 0)
1248 return r;
1249
1250 t = strappend("REMOTE_ADDR=", addr);
1251 if (!t)
1252 return -ENOMEM;
1253 our_env[n_env++] = t;
1254
1255 port = sockaddr_port(&sa.sa);
1256 if (port < 0)
1257 return port;
1258
1259 if (asprintf(&t, "REMOTE_PORT=%u", port) < 0)
1260 return -ENOMEM;
1261 our_env[n_env++] = t;
1262 }
1263 }
1264
1265 final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL);
1266 if (!final_env)
1267 return -ENOMEM;
1268
1269 if (is_control && UNIT(s)->cgroup_path) {
1270 path = strjoina(UNIT(s)->cgroup_path, "/control");
1271 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER, path);
1272 } else
1273 path = UNIT(s)->cgroup_path;
1274
1275 if (s->exec_context.bus_endpoint) {
1276 r = bus_kernel_create_endpoint(UNIT(s)->manager->running_as == MANAGER_SYSTEM ? "system" : "user",
1277 UNIT(s)->id, &bus_endpoint_path);
1278 if (r < 0)
1279 return r;
1280
1281 /* Pass the fd to the exec_params so that the child process can upload the policy.
1282 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1283 * as the service is running. */
1284 exec_params.bus_endpoint_fd = s->bus_endpoint_fd = r;
1285 }
1286
1287 exec_params.argv = argv;
1288 exec_params.fds = fds;
1289 exec_params.fd_names = fd_names;
1290 exec_params.n_fds = n_fds;
1291 exec_params.environment = final_env;
1292 exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn;
1293 exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported;
1294 exec_params.cgroup_path = path;
1295 exec_params.cgroup_delegate = s->cgroup_context.delegate;
1296 exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager);
1297 exec_params.watchdog_usec = s->watchdog_usec;
1298 exec_params.bus_endpoint_path = bus_endpoint_path;
1299 exec_params.selinux_context_net = s->socket_fd_selinux_context_net;
1300 if (s->type == SERVICE_IDLE)
1301 exec_params.idle_pipe = UNIT(s)->manager->idle_pipe;
1302 exec_params.stdin_fd = s->stdin_fd;
1303 exec_params.stdout_fd = s->stdout_fd;
1304 exec_params.stderr_fd = s->stderr_fd;
1305
1306 r = exec_spawn(UNIT(s),
1307 c,
1308 &s->exec_context,
1309 &exec_params,
1310 s->exec_runtime,
1311 &pid);
1312 if (r < 0)
1313 return r;
1314
1315 r = unit_watch_pid(UNIT(s), pid);
1316 if (r < 0)
1317 /* FIXME: we need to do something here */
1318 return r;
1319
1320 *_pid = pid;
1321
1322 return 0;
1323 }
1324
1325 static int main_pid_good(Service *s) {
1326 assert(s);
1327
1328 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1329 * don't know */
1330
1331 /* If we know the pid file, then let's just check if it is
1332 * still valid */
1333 if (s->main_pid_known) {
1334
1335 /* If it's an alien child let's check if it is still
1336 * alive ... */
1337 if (s->main_pid_alien && s->main_pid > 0)
1338 return pid_is_alive(s->main_pid);
1339
1340 /* .. otherwise assume we'll get a SIGCHLD for it,
1341 * which we really should wait for to collect exit
1342 * status and code */
1343 return s->main_pid > 0;
1344 }
1345
1346 /* We don't know the pid */
1347 return -EAGAIN;
1348 }
1349
1350 _pure_ static int control_pid_good(Service *s) {
1351 assert(s);
1352
1353 return s->control_pid > 0;
1354 }
1355
1356 static int cgroup_good(Service *s) {
1357 int r;
1358
1359 assert(s);
1360
1361 if (!UNIT(s)->cgroup_path)
1362 return 0;
1363
1364 r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path);
1365 if (r < 0)
1366 return r;
1367
1368 return !r;
1369 }
1370
1371 static bool service_shall_restart(Service *s) {
1372 assert(s);
1373
1374 /* Don't restart after manual stops */
1375 if (s->forbid_restart)
1376 return false;
1377
1378 /* Never restart if this is configured as special exception */
1379 if (exit_status_set_test(&s->restart_prevent_status, s->main_exec_status.code, s->main_exec_status.status))
1380 return false;
1381
1382 /* Restart if the exit code/status are configured as restart triggers */
1383 if (exit_status_set_test(&s->restart_force_status, s->main_exec_status.code, s->main_exec_status.status))
1384 return true;
1385
1386 switch (s->restart) {
1387
1388 case SERVICE_RESTART_NO:
1389 return false;
1390
1391 case SERVICE_RESTART_ALWAYS:
1392 return true;
1393
1394 case SERVICE_RESTART_ON_SUCCESS:
1395 return s->result == SERVICE_SUCCESS;
1396
1397 case SERVICE_RESTART_ON_FAILURE:
1398 return s->result != SERVICE_SUCCESS;
1399
1400 case SERVICE_RESTART_ON_ABNORMAL:
1401 return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE);
1402
1403 case SERVICE_RESTART_ON_WATCHDOG:
1404 return s->result == SERVICE_FAILURE_WATCHDOG;
1405
1406 case SERVICE_RESTART_ON_ABORT:
1407 return IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP);
1408
1409 default:
1410 assert_not_reached("unknown restart setting");
1411 }
1412 }
1413
1414 static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) {
1415 int r;
1416 assert(s);
1417
1418 if (f != SERVICE_SUCCESS)
1419 s->result = f;
1420
1421 service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD);
1422
1423 if (s->result != SERVICE_SUCCESS) {
1424 log_unit_warning(UNIT(s), "Failed with result '%s'.", service_result_to_string(s->result));
1425 failure_action(UNIT(s)->manager, s->failure_action, s->reboot_arg);
1426 }
1427
1428 if (allow_restart && service_shall_restart(s)) {
1429
1430 r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec));
1431 if (r < 0)
1432 goto fail;
1433
1434 service_set_state(s, SERVICE_AUTO_RESTART);
1435 }
1436
1437 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1438 s->forbid_restart = false;
1439
1440 /* We want fresh tmpdirs in case service is started again immediately */
1441 exec_runtime_destroy(s->exec_runtime);
1442 s->exec_runtime = exec_runtime_unref(s->exec_runtime);
1443
1444 /* Also, remove the runtime directory in */
1445 exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager));
1446
1447 /* Try to delete the pid file. At this point it will be
1448 * out-of-date, and some software might be confused by it, so
1449 * let's remove it. */
1450 if (s->pid_file)
1451 (void) unlink(s->pid_file);
1452
1453 return;
1454
1455 fail:
1456 log_unit_warning_errno(UNIT(s), r, "Failed to run install restart timer: %m");
1457 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1458 }
1459
1460 static void service_enter_stop_post(Service *s, ServiceResult f) {
1461 int r;
1462 assert(s);
1463
1464 if (f != SERVICE_SUCCESS)
1465 s->result = f;
1466
1467 service_unwatch_control_pid(s);
1468 unit_watch_all_pids(UNIT(s));
1469
1470 s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST];
1471 if (s->control_command) {
1472 s->control_command_id = SERVICE_EXEC_STOP_POST;
1473
1474 r = service_spawn(s,
1475 s->control_command,
1476 s->timeout_stop_usec,
1477 false,
1478 !s->permissions_start_only,
1479 !s->root_directory_start_only,
1480 true,
1481 true,
1482 &s->control_pid);
1483 if (r < 0)
1484 goto fail;
1485
1486 service_set_state(s, SERVICE_STOP_POST);
1487 } else
1488 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS);
1489
1490 return;
1491
1492 fail:
1493 log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m");
1494 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1495 }
1496
1497 static int state_to_kill_operation(ServiceState state) {
1498 switch (state) {
1499
1500 case SERVICE_STOP_SIGABRT:
1501 return KILL_ABORT;
1502
1503 case SERVICE_STOP_SIGTERM:
1504 case SERVICE_FINAL_SIGTERM:
1505 return KILL_TERMINATE;
1506
1507 case SERVICE_STOP_SIGKILL:
1508 case SERVICE_FINAL_SIGKILL:
1509 return KILL_KILL;
1510
1511 default:
1512 return _KILL_OPERATION_INVALID;
1513 }
1514 }
1515
1516 static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) {
1517 int r;
1518
1519 assert(s);
1520
1521 if (f != SERVICE_SUCCESS)
1522 s->result = f;
1523
1524 unit_watch_all_pids(UNIT(s));
1525
1526 r = unit_kill_context(
1527 UNIT(s),
1528 &s->kill_context,
1529 state_to_kill_operation(state),
1530 s->main_pid,
1531 s->control_pid,
1532 s->main_pid_alien);
1533
1534 if (r < 0)
1535 goto fail;
1536
1537 if (r > 0) {
1538 r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
1539 if (r < 0)
1540 goto fail;
1541
1542 service_set_state(s, state);
1543 } else if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM) && s->kill_context.send_sigkill)
1544 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS);
1545 else if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL))
1546 service_enter_stop_post(s, SERVICE_SUCCESS);
1547 else if (state == SERVICE_FINAL_SIGTERM && s->kill_context.send_sigkill)
1548 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS);
1549 else
1550 service_enter_dead(s, SERVICE_SUCCESS, true);
1551
1552 return;
1553
1554 fail:
1555 log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m");
1556
1557 if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL))
1558 service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES);
1559 else
1560 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1561 }
1562
1563 static void service_enter_stop_by_notify(Service *s) {
1564 assert(s);
1565
1566 unit_watch_all_pids(UNIT(s));
1567
1568 service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec));
1569
1570 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1571 service_set_state(s, SERVICE_STOP_SIGTERM);
1572 }
1573
1574 static void service_enter_stop(Service *s, ServiceResult f) {
1575 int r;
1576
1577 assert(s);
1578
1579 if (f != SERVICE_SUCCESS)
1580 s->result = f;
1581
1582 service_unwatch_control_pid(s);
1583 unit_watch_all_pids(UNIT(s));
1584
1585 s->control_command = s->exec_command[SERVICE_EXEC_STOP];
1586 if (s->control_command) {
1587 s->control_command_id = SERVICE_EXEC_STOP;
1588
1589 r = service_spawn(s,
1590 s->control_command,
1591 s->timeout_stop_usec,
1592 false,
1593 !s->permissions_start_only,
1594 !s->root_directory_start_only,
1595 false,
1596 true,
1597 &s->control_pid);
1598 if (r < 0)
1599 goto fail;
1600
1601 service_set_state(s, SERVICE_STOP);
1602 } else
1603 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
1604
1605 return;
1606
1607 fail:
1608 log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop' task: %m");
1609 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1610 }
1611
1612 static bool service_good(Service *s) {
1613 int main_pid_ok;
1614 assert(s);
1615
1616 if (s->type == SERVICE_DBUS && !s->bus_name_good)
1617 return false;
1618
1619 main_pid_ok = main_pid_good(s);
1620 if (main_pid_ok > 0) /* It's alive */
1621 return true;
1622 if (main_pid_ok == 0) /* It's dead */
1623 return false;
1624
1625 /* OK, we don't know anything about the main PID, maybe
1626 * because there is none. Let's check the control group
1627 * instead. */
1628
1629 return cgroup_good(s) != 0;
1630 }
1631
1632 static void service_enter_running(Service *s, ServiceResult f) {
1633 assert(s);
1634
1635 if (f != SERVICE_SUCCESS)
1636 s->result = f;
1637
1638 if (service_good(s)) {
1639
1640 /* If there are any queued up sd_notify()
1641 * notifications, process them now */
1642 if (s->notify_state == NOTIFY_RELOADING)
1643 service_enter_reload_by_notify(s);
1644 else if (s->notify_state == NOTIFY_STOPPING)
1645 service_enter_stop_by_notify(s);
1646 else {
1647 service_set_state(s, SERVICE_RUNNING);
1648 service_arm_timer(s, usec_add(UNIT(s)->active_enter_timestamp.monotonic, s->runtime_max_usec));
1649 }
1650
1651 } else if (s->remain_after_exit)
1652 service_set_state(s, SERVICE_EXITED);
1653 else
1654 service_enter_stop(s, SERVICE_SUCCESS);
1655 }
1656
1657 static void service_enter_start_post(Service *s) {
1658 int r;
1659 assert(s);
1660
1661 service_unwatch_control_pid(s);
1662 service_reset_watchdog(s);
1663
1664 s->control_command = s->exec_command[SERVICE_EXEC_START_POST];
1665 if (s->control_command) {
1666 s->control_command_id = SERVICE_EXEC_START_POST;
1667
1668 r = service_spawn(s,
1669 s->control_command,
1670 s->timeout_start_usec,
1671 false,
1672 !s->permissions_start_only,
1673 !s->root_directory_start_only,
1674 false,
1675 true,
1676 &s->control_pid);
1677 if (r < 0)
1678 goto fail;
1679
1680 service_set_state(s, SERVICE_START_POST);
1681 } else
1682 service_enter_running(s, SERVICE_SUCCESS);
1683
1684 return;
1685
1686 fail:
1687 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m");
1688 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1689 }
1690
1691 static void service_kill_control_processes(Service *s) {
1692 char *p;
1693
1694 if (!UNIT(s)->cgroup_path)
1695 return;
1696
1697 p = strjoina(UNIT(s)->cgroup_path, "/control");
1698 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL);
1699 }
1700
1701 static void service_enter_start(Service *s) {
1702 ExecCommand *c;
1703 usec_t timeout;
1704 pid_t pid;
1705 int r;
1706
1707 assert(s);
1708
1709 service_unwatch_control_pid(s);
1710 service_unwatch_main_pid(s);
1711
1712 /* We want to ensure that nobody leaks processes from
1713 * START_PRE here, so let's go on a killing spree, People
1714 * should not spawn long running processes from START_PRE. */
1715 service_kill_control_processes(s);
1716
1717 if (s->type == SERVICE_FORKING) {
1718 s->control_command_id = SERVICE_EXEC_START;
1719 c = s->control_command = s->exec_command[SERVICE_EXEC_START];
1720
1721 s->main_command = NULL;
1722 } else {
1723 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1724 s->control_command = NULL;
1725
1726 c = s->main_command = s->exec_command[SERVICE_EXEC_START];
1727 }
1728
1729 if (!c) {
1730 assert(s->type == SERVICE_ONESHOT);
1731 service_enter_start_post(s);
1732 return;
1733 }
1734
1735 if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE))
1736 /* For simple + idle this is the main process. We don't apply any timeout here, but
1737 * service_enter_running() will later apply the .runtime_max_usec timeout. */
1738 timeout = USEC_INFINITY;
1739 else
1740 timeout = s->timeout_start_usec;
1741
1742 r = service_spawn(s,
1743 c,
1744 timeout,
1745 true,
1746 true,
1747 true,
1748 true,
1749 false,
1750 &pid);
1751 if (r < 0)
1752 goto fail;
1753
1754 if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) {
1755 /* For simple services we immediately start
1756 * the START_POST binaries. */
1757
1758 service_set_main_pid(s, pid);
1759 service_enter_start_post(s);
1760
1761 } else if (s->type == SERVICE_FORKING) {
1762
1763 /* For forking services we wait until the start
1764 * process exited. */
1765
1766 s->control_pid = pid;
1767 service_set_state(s, SERVICE_START);
1768
1769 } else if (IN_SET(s->type, SERVICE_ONESHOT, SERVICE_DBUS, SERVICE_NOTIFY)) {
1770
1771 /* For oneshot services we wait until the start
1772 * process exited, too, but it is our main process. */
1773
1774 /* For D-Bus services we know the main pid right away,
1775 * but wait for the bus name to appear on the
1776 * bus. Notify services are similar. */
1777
1778 service_set_main_pid(s, pid);
1779 service_set_state(s, SERVICE_START);
1780 } else
1781 assert_not_reached("Unknown service type");
1782
1783 return;
1784
1785 fail:
1786 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start' task: %m");
1787 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1788 }
1789
1790 static void service_enter_start_pre(Service *s) {
1791 int r;
1792
1793 assert(s);
1794
1795 service_unwatch_control_pid(s);
1796
1797 s->control_command = s->exec_command[SERVICE_EXEC_START_PRE];
1798 if (s->control_command) {
1799 /* Before we start anything, let's clear up what might
1800 * be left from previous runs. */
1801 service_kill_control_processes(s);
1802
1803 s->control_command_id = SERVICE_EXEC_START_PRE;
1804
1805 r = service_spawn(s,
1806 s->control_command,
1807 s->timeout_start_usec,
1808 false,
1809 !s->permissions_start_only,
1810 !s->root_directory_start_only,
1811 true,
1812 true,
1813 &s->control_pid);
1814 if (r < 0)
1815 goto fail;
1816
1817 service_set_state(s, SERVICE_START_PRE);
1818 } else
1819 service_enter_start(s);
1820
1821 return;
1822
1823 fail:
1824 log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m");
1825 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1826 }
1827
1828 static void service_enter_restart(Service *s) {
1829 _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL;
1830 int r;
1831
1832 assert(s);
1833
1834 if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) {
1835 /* Don't restart things if we are going down anyway */
1836 log_unit_info(UNIT(s), "Stop job pending for unit, delaying automatic restart.");
1837
1838 r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec));
1839 if (r < 0)
1840 goto fail;
1841
1842 return;
1843 }
1844
1845 /* Any units that are bound to this service must also be
1846 * restarted. We use JOB_RESTART (instead of the more obvious
1847 * JOB_START) here so that those dependency jobs will be added
1848 * as well. */
1849 r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, &error, NULL);
1850 if (r < 0)
1851 goto fail;
1852
1853 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1854 * it will be canceled as part of the service_stop() call that
1855 * is executed as part of JOB_RESTART. */
1856
1857 log_unit_debug(UNIT(s), "Scheduled restart job.");
1858 return;
1859
1860 fail:
1861 log_unit_warning(UNIT(s), "Failed to schedule restart job: %s", bus_error_message(&error, -r));
1862 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false);
1863 }
1864
1865 static void service_enter_reload_by_notify(Service *s) {
1866 assert(s);
1867
1868 service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_start_usec));
1869 service_set_state(s, SERVICE_RELOAD);
1870 }
1871
1872 static void service_enter_reload(Service *s) {
1873 int r;
1874
1875 assert(s);
1876
1877 service_unwatch_control_pid(s);
1878 s->reload_result = SERVICE_SUCCESS;
1879
1880 s->control_command = s->exec_command[SERVICE_EXEC_RELOAD];
1881 if (s->control_command) {
1882 s->control_command_id = SERVICE_EXEC_RELOAD;
1883
1884 r = service_spawn(s,
1885 s->control_command,
1886 s->timeout_start_usec,
1887 false,
1888 !s->permissions_start_only,
1889 !s->root_directory_start_only,
1890 false,
1891 true,
1892 &s->control_pid);
1893 if (r < 0)
1894 goto fail;
1895
1896 service_set_state(s, SERVICE_RELOAD);
1897 } else
1898 service_enter_running(s, SERVICE_SUCCESS);
1899
1900 return;
1901
1902 fail:
1903 log_unit_warning_errno(UNIT(s), r, "Failed to run 'reload' task: %m");
1904 s->reload_result = SERVICE_FAILURE_RESOURCES;
1905 service_enter_running(s, SERVICE_SUCCESS);
1906 }
1907
1908 static void service_run_next_control(Service *s) {
1909 usec_t timeout;
1910 int r;
1911
1912 assert(s);
1913 assert(s->control_command);
1914 assert(s->control_command->command_next);
1915
1916 assert(s->control_command_id != SERVICE_EXEC_START);
1917
1918 s->control_command = s->control_command->command_next;
1919 service_unwatch_control_pid(s);
1920
1921 if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD))
1922 timeout = s->timeout_start_usec;
1923 else
1924 timeout = s->timeout_stop_usec;
1925
1926 r = service_spawn(s,
1927 s->control_command,
1928 timeout,
1929 false,
1930 !s->permissions_start_only,
1931 !s->root_directory_start_only,
1932 s->control_command_id == SERVICE_EXEC_START_PRE ||
1933 s->control_command_id == SERVICE_EXEC_STOP_POST,
1934 true,
1935 &s->control_pid);
1936 if (r < 0)
1937 goto fail;
1938
1939 return;
1940
1941 fail:
1942 log_unit_warning_errno(UNIT(s), r, "Failed to run next control task: %m");
1943
1944 if (s->state == SERVICE_START_PRE)
1945 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
1946 else if (s->state == SERVICE_STOP)
1947 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
1948 else if (s->state == SERVICE_STOP_POST)
1949 service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true);
1950 else if (s->state == SERVICE_RELOAD) {
1951 s->reload_result = SERVICE_FAILURE_RESOURCES;
1952 service_enter_running(s, SERVICE_SUCCESS);
1953 } else
1954 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1955 }
1956
1957 static void service_run_next_main(Service *s) {
1958 pid_t pid;
1959 int r;
1960
1961 assert(s);
1962 assert(s->main_command);
1963 assert(s->main_command->command_next);
1964 assert(s->type == SERVICE_ONESHOT);
1965
1966 s->main_command = s->main_command->command_next;
1967 service_unwatch_main_pid(s);
1968
1969 r = service_spawn(s,
1970 s->main_command,
1971 s->timeout_start_usec,
1972 true,
1973 true,
1974 true,
1975 true,
1976 false,
1977 &pid);
1978 if (r < 0)
1979 goto fail;
1980
1981 service_set_main_pid(s, pid);
1982
1983 return;
1984
1985 fail:
1986 log_unit_warning_errno(UNIT(s), r, "Failed to run next main task: %m");
1987 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
1988 }
1989
1990 static int service_start_limit_test(Service *s) {
1991 assert(s);
1992
1993 if (ratelimit_test(&s->start_limit))
1994 return 0;
1995
1996 log_unit_warning(UNIT(s), "Start request repeated too quickly.");
1997
1998 return failure_action(UNIT(s)->manager, s->start_limit_action, s->reboot_arg);
1999 }
2000
2001 static int service_start(Unit *u) {
2002 Service *s = SERVICE(u);
2003 int r;
2004
2005 assert(s);
2006
2007 /* We cannot fulfill this request right now, try again later
2008 * please! */
2009 if (IN_SET(s->state,
2010 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
2011 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))
2012 return -EAGAIN;
2013
2014 /* Already on it! */
2015 if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST))
2016 return 0;
2017
2018 /* A service that will be restarted must be stopped first to
2019 * trigger BindsTo and/or OnFailure dependencies. If a user
2020 * does not want to wait for the holdoff time to elapse, the
2021 * service should be manually restarted, not started. We
2022 * simply return EAGAIN here, so that any start jobs stay
2023 * queued, and assume that the auto restart timer will
2024 * eventually trigger the restart. */
2025 if (s->state == SERVICE_AUTO_RESTART)
2026 return -EAGAIN;
2027
2028 assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED));
2029
2030 /* Make sure we don't enter a busy loop of some kind. */
2031 r = service_start_limit_test(s);
2032 if (r < 0) {
2033 service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false);
2034 return r;
2035 }
2036
2037 s->result = SERVICE_SUCCESS;
2038 s->reload_result = SERVICE_SUCCESS;
2039 s->main_pid_known = false;
2040 s->main_pid_alien = false;
2041 s->forbid_restart = false;
2042 s->reset_cpu_usage = true;
2043
2044 s->status_text = mfree(s->status_text);
2045 s->status_errno = 0;
2046
2047 s->notify_state = NOTIFY_UNKNOWN;
2048
2049 service_enter_start_pre(s);
2050 return 1;
2051 }
2052
2053 static int service_stop(Unit *u) {
2054 Service *s = SERVICE(u);
2055
2056 assert(s);
2057
2058 /* Don't create restart jobs from manual stops. */
2059 s->forbid_restart = true;
2060
2061 /* Already on it */
2062 if (IN_SET(s->state,
2063 SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST,
2064 SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))
2065 return 0;
2066
2067 /* A restart will be scheduled or is in progress. */
2068 if (s->state == SERVICE_AUTO_RESTART) {
2069 service_set_state(s, SERVICE_DEAD);
2070 return 0;
2071 }
2072
2073 /* If there's already something running we go directly into
2074 * kill mode. */
2075 if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD)) {
2076 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS);
2077 return 0;
2078 }
2079
2080 assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED));
2081
2082 service_enter_stop(s, SERVICE_SUCCESS);
2083 return 1;
2084 }
2085
2086 static int service_reload(Unit *u) {
2087 Service *s = SERVICE(u);
2088
2089 assert(s);
2090
2091 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2092
2093 service_enter_reload(s);
2094 return 1;
2095 }
2096
2097 _pure_ static bool service_can_reload(Unit *u) {
2098 Service *s = SERVICE(u);
2099
2100 assert(s);
2101
2102 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2103 }
2104
2105 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2106 Service *s = SERVICE(u);
2107 ServiceFDStore *fs;
2108 int r;
2109
2110 assert(u);
2111 assert(f);
2112 assert(fds);
2113
2114 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2115 unit_serialize_item(u, f, "result", service_result_to_string(s->result));
2116 unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result));
2117
2118 if (s->control_pid > 0)
2119 unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid);
2120
2121 if (s->main_pid_known && s->main_pid > 0)
2122 unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid);
2123
2124 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2125 unit_serialize_item(u, f, "bus-name-good", yes_no(s->bus_name_good));
2126 unit_serialize_item(u, f, "bus-name-owner", s->bus_name_owner);
2127
2128 r = unit_serialize_item_escaped(u, f, "status-text", s->status_text);
2129 if (r < 0)
2130 return r;
2131
2132 /* FIXME: There's a minor uncleanliness here: if there are
2133 * multiple commands attached here, we will start from the
2134 * first one again */
2135 if (s->control_command_id >= 0)
2136 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2137
2138 r = unit_serialize_item_fd(u, f, fds, "stdin-fd", s->stdin_fd);
2139 if (r < 0)
2140 return r;
2141 r = unit_serialize_item_fd(u, f, fds, "stdout-fd", s->stdout_fd);
2142 if (r < 0)
2143 return r;
2144 r = unit_serialize_item_fd(u, f, fds, "stderr-fd", s->stderr_fd);
2145 if (r < 0)
2146 return r;
2147
2148 r = unit_serialize_item_fd(u, f, fds, "socket-fd", s->socket_fd);
2149 if (r < 0)
2150 return r;
2151 r = unit_serialize_item_fd(u, f, fds, "endpoint-fd", s->bus_endpoint_fd);
2152 if (r < 0)
2153 return r;
2154
2155 LIST_FOREACH(fd_store, fs, s->fd_store) {
2156 _cleanup_free_ char *c = NULL;
2157 int copy;
2158
2159 copy = fdset_put_dup(fds, fs->fd);
2160 if (copy < 0)
2161 return copy;
2162
2163 c = cescape(fs->fdname);
2164
2165 unit_serialize_item_format(u, f, "fd-store-fd", "%i %s", copy, strempty(c));
2166 }
2167
2168 if (s->main_exec_status.pid > 0) {
2169 unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid);
2170 dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp);
2171 dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp);
2172
2173 if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) {
2174 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2175 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2176 }
2177 }
2178
2179 if (dual_timestamp_is_set(&s->watchdog_timestamp))
2180 dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp);
2181
2182 unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart));
2183
2184 return 0;
2185 }
2186
2187 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2188 Service *s = SERVICE(u);
2189 int r;
2190
2191 assert(u);
2192 assert(key);
2193 assert(value);
2194 assert(fds);
2195
2196 if (streq(key, "state")) {
2197 ServiceState state;
2198
2199 state = service_state_from_string(value);
2200 if (state < 0)
2201 log_unit_debug(u, "Failed to parse state value: %s", value);
2202 else
2203 s->deserialized_state = state;
2204 } else if (streq(key, "result")) {
2205 ServiceResult f;
2206
2207 f = service_result_from_string(value);
2208 if (f < 0)
2209 log_unit_debug(u, "Failed to parse result value: %s", value);
2210 else if (f != SERVICE_SUCCESS)
2211 s->result = f;
2212
2213 } else if (streq(key, "reload-result")) {
2214 ServiceResult f;
2215
2216 f = service_result_from_string(value);
2217 if (f < 0)
2218 log_unit_debug(u, "Failed to parse reload result value: %s", value);
2219 else if (f != SERVICE_SUCCESS)
2220 s->reload_result = f;
2221
2222 } else if (streq(key, "control-pid")) {
2223 pid_t pid;
2224
2225 if (parse_pid(value, &pid) < 0)
2226 log_unit_debug(u, "Failed to parse control-pid value: %s", value);
2227 else
2228 s->control_pid = pid;
2229 } else if (streq(key, "main-pid")) {
2230 pid_t pid;
2231
2232 if (parse_pid(value, &pid) < 0)
2233 log_unit_debug(u, "Failed to parse main-pid value: %s", value);
2234 else {
2235 service_set_main_pid(s, pid);
2236 unit_watch_pid(UNIT(s), pid);
2237 }
2238 } else if (streq(key, "main-pid-known")) {
2239 int b;
2240
2241 b = parse_boolean(value);
2242 if (b < 0)
2243 log_unit_debug(u, "Failed to parse main-pid-known value: %s", value);
2244 else
2245 s->main_pid_known = b;
2246 } else if (streq(key, "bus-name-good")) {
2247 int b;
2248
2249 b = parse_boolean(value);
2250 if (b < 0)
2251 log_unit_debug(u, "Failed to parse bus-name-good value: %s", value);
2252 else
2253 s->bus_name_good = b;
2254 } else if (streq(key, "bus-name-owner")) {
2255 r = free_and_strdup(&s->bus_name_owner, value);
2256 if (r < 0)
2257 log_unit_error_errno(u, r, "Unable to deserialize current bus owner %s: %m", value);
2258 } else if (streq(key, "status-text")) {
2259 char *t;
2260
2261 r = cunescape(value, 0, &t);
2262 if (r < 0)
2263 log_unit_debug_errno(u, r, "Failed to unescape status text: %s", value);
2264 else {
2265 free(s->status_text);
2266 s->status_text = t;
2267 }
2268
2269 } else if (streq(key, "control-command")) {
2270 ServiceExecCommand id;
2271
2272 id = service_exec_command_from_string(value);
2273 if (id < 0)
2274 log_unit_debug(u, "Failed to parse exec-command value: %s", value);
2275 else {
2276 s->control_command_id = id;
2277 s->control_command = s->exec_command[id];
2278 }
2279 } else if (streq(key, "socket-fd")) {
2280 int fd;
2281
2282 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2283 log_unit_debug(u, "Failed to parse socket-fd value: %s", value);
2284 else {
2285 asynchronous_close(s->socket_fd);
2286 s->socket_fd = fdset_remove(fds, fd);
2287 }
2288 } else if (streq(key, "endpoint-fd")) {
2289 int fd;
2290
2291 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2292 log_unit_debug(u, "Failed to parse endpoint-fd value: %s", value);
2293 else {
2294 safe_close(s->bus_endpoint_fd);
2295 s->bus_endpoint_fd = fdset_remove(fds, fd);
2296 }
2297 } else if (streq(key, "fd-store-fd")) {
2298 const char *fdv;
2299 size_t pf;
2300 int fd;
2301
2302 pf = strcspn(value, WHITESPACE);
2303 fdv = strndupa(value, pf);
2304
2305 if (safe_atoi(fdv, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2306 log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value);
2307 else {
2308 _cleanup_free_ char *t = NULL;
2309 const char *fdn;
2310
2311 fdn = value + pf;
2312 fdn += strspn(fdn, WHITESPACE);
2313 (void) cunescape(fdn, 0, &t);
2314
2315 r = service_add_fd_store(s, fd, t);
2316 if (r < 0)
2317 log_unit_error_errno(u, r, "Failed to add fd to store: %m");
2318 else if (r > 0)
2319 fdset_remove(fds, fd);
2320 }
2321
2322 } else if (streq(key, "main-exec-status-pid")) {
2323 pid_t pid;
2324
2325 if (parse_pid(value, &pid) < 0)
2326 log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value);
2327 else
2328 s->main_exec_status.pid = pid;
2329 } else if (streq(key, "main-exec-status-code")) {
2330 int i;
2331
2332 if (safe_atoi(value, &i) < 0)
2333 log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value);
2334 else
2335 s->main_exec_status.code = i;
2336 } else if (streq(key, "main-exec-status-status")) {
2337 int i;
2338
2339 if (safe_atoi(value, &i) < 0)
2340 log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value);
2341 else
2342 s->main_exec_status.status = i;
2343 } else if (streq(key, "main-exec-status-start"))
2344 dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp);
2345 else if (streq(key, "main-exec-status-exit"))
2346 dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp);
2347 else if (streq(key, "watchdog-timestamp"))
2348 dual_timestamp_deserialize(value, &s->watchdog_timestamp);
2349 else if (streq(key, "forbid-restart")) {
2350 int b;
2351
2352 b = parse_boolean(value);
2353 if (b < 0)
2354 log_unit_debug(u, "Failed to parse forbid-restart value: %s", value);
2355 else
2356 s->forbid_restart = b;
2357 } else if (streq(key, "stdin-fd")) {
2358 int fd;
2359
2360 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2361 log_unit_debug(u, "Failed to parse stdin-fd value: %s", value);
2362 else {
2363 asynchronous_close(s->stdin_fd);
2364 s->stdin_fd = fdset_remove(fds, fd);
2365 s->exec_context.stdio_as_fds = true;
2366 }
2367 } else if (streq(key, "stdout-fd")) {
2368 int fd;
2369
2370 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2371 log_unit_debug(u, "Failed to parse stdout-fd value: %s", value);
2372 else {
2373 asynchronous_close(s->stdout_fd);
2374 s->stdout_fd = fdset_remove(fds, fd);
2375 s->exec_context.stdio_as_fds = true;
2376 }
2377 } else if (streq(key, "stderr-fd")) {
2378 int fd;
2379
2380 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2381 log_unit_debug(u, "Failed to parse stderr-fd value: %s", value);
2382 else {
2383 asynchronous_close(s->stderr_fd);
2384 s->stderr_fd = fdset_remove(fds, fd);
2385 s->exec_context.stdio_as_fds = true;
2386 }
2387 } else
2388 log_unit_debug(u, "Unknown serialization key: %s", key);
2389
2390 return 0;
2391 }
2392
2393 _pure_ static UnitActiveState service_active_state(Unit *u) {
2394 const UnitActiveState *table;
2395
2396 assert(u);
2397
2398 table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table;
2399
2400 return table[SERVICE(u)->state];
2401 }
2402
2403 static const char *service_sub_state_to_string(Unit *u) {
2404 assert(u);
2405
2406 return service_state_to_string(SERVICE(u)->state);
2407 }
2408
2409 static bool service_check_gc(Unit *u) {
2410 Service *s = SERVICE(u);
2411
2412 assert(s);
2413
2414 /* Never clean up services that still have a process around,
2415 * even if the service is formally dead. */
2416 if (cgroup_good(s) > 0 ||
2417 main_pid_good(s) > 0 ||
2418 control_pid_good(s) > 0)
2419 return true;
2420
2421 return false;
2422 }
2423
2424 static int service_retry_pid_file(Service *s) {
2425 int r;
2426
2427 assert(s->pid_file);
2428 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2429
2430 r = service_load_pid_file(s, false);
2431 if (r < 0)
2432 return r;
2433
2434 service_unwatch_pid_file(s);
2435
2436 service_enter_running(s, SERVICE_SUCCESS);
2437 return 0;
2438 }
2439
2440 static int service_watch_pid_file(Service *s) {
2441 int r;
2442
2443 log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path);
2444
2445 r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io);
2446 if (r < 0)
2447 goto fail;
2448
2449 /* the pidfile might have appeared just before we set the watch */
2450 log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path);
2451 service_retry_pid_file(s);
2452
2453 return 0;
2454 fail:
2455 log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path);
2456 service_unwatch_pid_file(s);
2457 return r;
2458 }
2459
2460 static int service_demand_pid_file(Service *s) {
2461 PathSpec *ps;
2462
2463 assert(s->pid_file);
2464 assert(!s->pid_file_pathspec);
2465
2466 ps = new0(PathSpec, 1);
2467 if (!ps)
2468 return -ENOMEM;
2469
2470 ps->unit = UNIT(s);
2471 ps->path = strdup(s->pid_file);
2472 if (!ps->path) {
2473 free(ps);
2474 return -ENOMEM;
2475 }
2476
2477 path_kill_slashes(ps->path);
2478
2479 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2480 * keep their PID file open all the time. */
2481 ps->type = PATH_MODIFIED;
2482 ps->inotify_fd = -1;
2483
2484 s->pid_file_pathspec = ps;
2485
2486 return service_watch_pid_file(s);
2487 }
2488
2489 static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) {
2490 PathSpec *p = userdata;
2491 Service *s;
2492
2493 assert(p);
2494
2495 s = SERVICE(p->unit);
2496
2497 assert(s);
2498 assert(fd >= 0);
2499 assert(s->state == SERVICE_START || s->state == SERVICE_START_POST);
2500 assert(s->pid_file_pathspec);
2501 assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd));
2502
2503 log_unit_debug(UNIT(s), "inotify event");
2504
2505 if (path_spec_fd_event(p, events) < 0)
2506 goto fail;
2507
2508 if (service_retry_pid_file(s) == 0)
2509 return 0;
2510
2511 if (service_watch_pid_file(s) < 0)
2512 goto fail;
2513
2514 return 0;
2515
2516 fail:
2517 service_unwatch_pid_file(s);
2518 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES);
2519 return 0;
2520 }
2521
2522 static void service_notify_cgroup_empty_event(Unit *u) {
2523 Service *s = SERVICE(u);
2524
2525 assert(u);
2526
2527 log_unit_debug(u, "cgroup is empty");
2528
2529 switch (s->state) {
2530
2531 /* Waiting for SIGCHLD is usually more interesting,
2532 * because it includes return codes/signals. Which is
2533 * why we ignore the cgroup events for most cases,
2534 * except when we don't know pid which to expect the
2535 * SIGCHLD for. */
2536
2537 case SERVICE_START:
2538 case SERVICE_START_POST:
2539 /* If we were hoping for the daemon to write its PID file,
2540 * we can give up now. */
2541 if (s->pid_file_pathspec) {
2542 log_unit_warning(u, "Daemon never wrote its PID file. Failing.");
2543
2544 service_unwatch_pid_file(s);
2545 if (s->state == SERVICE_START)
2546 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2547 else
2548 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2549 }
2550 break;
2551
2552 case SERVICE_RUNNING:
2553 /* service_enter_running() will figure out what to do */
2554 service_enter_running(s, SERVICE_SUCCESS);
2555 break;
2556
2557 case SERVICE_STOP_SIGABRT:
2558 case SERVICE_STOP_SIGTERM:
2559 case SERVICE_STOP_SIGKILL:
2560
2561 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2562 service_enter_stop_post(s, SERVICE_SUCCESS);
2563
2564 break;
2565
2566 case SERVICE_STOP_POST:
2567 case SERVICE_FINAL_SIGTERM:
2568 case SERVICE_FINAL_SIGKILL:
2569 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2570 service_enter_dead(s, SERVICE_SUCCESS, true);
2571
2572 break;
2573
2574 default:
2575 ;
2576 }
2577 }
2578
2579 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2580 Service *s = SERVICE(u);
2581 ServiceResult f;
2582
2583 assert(s);
2584 assert(pid >= 0);
2585
2586 if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) :
2587 is_clean_exit_lsb(code, status, &s->success_status))
2588 f = SERVICE_SUCCESS;
2589 else if (code == CLD_EXITED)
2590 f = SERVICE_FAILURE_EXIT_CODE;
2591 else if (code == CLD_KILLED)
2592 f = SERVICE_FAILURE_SIGNAL;
2593 else if (code == CLD_DUMPED)
2594 f = SERVICE_FAILURE_CORE_DUMP;
2595 else
2596 assert_not_reached("Unknown code");
2597
2598 if (s->main_pid == pid) {
2599 /* Forking services may occasionally move to a new PID.
2600 * As long as they update the PID file before exiting the old
2601 * PID, they're fine. */
2602 if (service_load_pid_file(s, false) == 0)
2603 return;
2604
2605 s->main_pid = 0;
2606 exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status);
2607
2608 if (s->main_command) {
2609 /* If this is not a forking service than the
2610 * main process got started and hence we copy
2611 * the exit status so that it is recorded both
2612 * as main and as control process exit
2613 * status */
2614
2615 s->main_command->exec_status = s->main_exec_status;
2616
2617 if (s->main_command->ignore)
2618 f = SERVICE_SUCCESS;
2619 } else if (s->exec_command[SERVICE_EXEC_START]) {
2620
2621 /* If this is a forked process, then we should
2622 * ignore the return value if this was
2623 * configured for the starter process */
2624
2625 if (s->exec_command[SERVICE_EXEC_START]->ignore)
2626 f = SERVICE_SUCCESS;
2627 }
2628
2629 log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE,
2630 LOG_UNIT_ID(u),
2631 LOG_UNIT_MESSAGE(u, "Main process exited, code=%s, status=%i/%s",
2632 sigchld_code_to_string(code), status,
2633 strna(code == CLD_EXITED
2634 ? exit_status_to_string(status, EXIT_STATUS_FULL)
2635 : signal_to_string(status))),
2636 "EXIT_CODE=%s", sigchld_code_to_string(code),
2637 "EXIT_STATUS=%i", status,
2638 NULL);
2639
2640 if (f != SERVICE_SUCCESS)
2641 s->result = f;
2642
2643 if (s->main_command &&
2644 s->main_command->command_next &&
2645 f == SERVICE_SUCCESS) {
2646
2647 /* There is another command to *
2648 * execute, so let's do that. */
2649
2650 log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state));
2651 service_run_next_main(s);
2652
2653 } else {
2654
2655 /* The service exited, so the service is officially
2656 * gone. */
2657 s->main_command = NULL;
2658
2659 switch (s->state) {
2660
2661 case SERVICE_START_POST:
2662 case SERVICE_RELOAD:
2663 case SERVICE_STOP:
2664 /* Need to wait until the operation is
2665 * done */
2666 break;
2667
2668 case SERVICE_START:
2669 if (s->type == SERVICE_ONESHOT) {
2670 /* This was our main goal, so let's go on */
2671 if (f == SERVICE_SUCCESS)
2672 service_enter_start_post(s);
2673 else
2674 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2675 break;
2676 }
2677
2678 /* Fall through */
2679
2680 case SERVICE_RUNNING:
2681 service_enter_running(s, f);
2682 break;
2683
2684 case SERVICE_STOP_SIGABRT:
2685 case SERVICE_STOP_SIGTERM:
2686 case SERVICE_STOP_SIGKILL:
2687
2688 if (!control_pid_good(s))
2689 service_enter_stop_post(s, f);
2690
2691 /* If there is still a control process, wait for that first */
2692 break;
2693
2694 case SERVICE_STOP_POST:
2695 case SERVICE_FINAL_SIGTERM:
2696 case SERVICE_FINAL_SIGKILL:
2697
2698 if (!control_pid_good(s))
2699 service_enter_dead(s, f, true);
2700 break;
2701
2702 default:
2703 assert_not_reached("Uh, main process died at wrong time.");
2704 }
2705 }
2706
2707 } else if (s->control_pid == pid) {
2708 s->control_pid = 0;
2709
2710 if (s->control_command) {
2711 exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status);
2712
2713 if (s->control_command->ignore)
2714 f = SERVICE_SUCCESS;
2715 }
2716
2717 log_unit_full(u, f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0,
2718 "Control process exited, code=%s status=%i",
2719 sigchld_code_to_string(code), status);
2720
2721 if (f != SERVICE_SUCCESS)
2722 s->result = f;
2723
2724 /* Immediately get rid of the cgroup, so that the
2725 * kernel doesn't delay the cgroup empty messages for
2726 * the service cgroup any longer than necessary */
2727 service_kill_control_processes(s);
2728
2729 if (s->control_command &&
2730 s->control_command->command_next &&
2731 f == SERVICE_SUCCESS) {
2732
2733 /* There is another command to *
2734 * execute, so let's do that. */
2735
2736 log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state));
2737 service_run_next_control(s);
2738
2739 } else {
2740 /* No further commands for this step, so let's
2741 * figure out what to do next */
2742
2743 s->control_command = NULL;
2744 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2745
2746 log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state));
2747
2748 switch (s->state) {
2749
2750 case SERVICE_START_PRE:
2751 if (f == SERVICE_SUCCESS)
2752 service_enter_start(s);
2753 else
2754 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2755 break;
2756
2757 case SERVICE_START:
2758 if (s->type != SERVICE_FORKING)
2759 /* Maybe spurious event due to a reload that changed the type? */
2760 break;
2761
2762 if (f != SERVICE_SUCCESS) {
2763 service_enter_signal(s, SERVICE_FINAL_SIGTERM, f);
2764 break;
2765 }
2766
2767 if (s->pid_file) {
2768 bool has_start_post;
2769 int r;
2770
2771 /* Let's try to load the pid file here if we can.
2772 * The PID file might actually be created by a START_POST
2773 * script. In that case don't worry if the loading fails. */
2774
2775 has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST];
2776 r = service_load_pid_file(s, !has_start_post);
2777 if (!has_start_post && r < 0) {
2778 r = service_demand_pid_file(s);
2779 if (r < 0 || !cgroup_good(s))
2780 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES);
2781 break;
2782 }
2783 } else
2784 (void) service_search_main_pid(s);
2785
2786 service_enter_start_post(s);
2787 break;
2788
2789 case SERVICE_START_POST:
2790 if (f != SERVICE_SUCCESS) {
2791 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2792 break;
2793 }
2794
2795 if (s->pid_file) {
2796 int r;
2797
2798 r = service_load_pid_file(s, true);
2799 if (r < 0) {
2800 r = service_demand_pid_file(s);
2801 if (r < 0 || !cgroup_good(s))
2802 service_enter_stop(s, SERVICE_FAILURE_RESOURCES);
2803 break;
2804 }
2805 } else
2806 (void) service_search_main_pid(s);
2807
2808 service_enter_running(s, SERVICE_SUCCESS);
2809 break;
2810
2811 case SERVICE_RELOAD:
2812 if (f == SERVICE_SUCCESS) {
2813 service_load_pid_file(s, true);
2814 (void) service_search_main_pid(s);
2815 }
2816
2817 s->reload_result = f;
2818 service_enter_running(s, SERVICE_SUCCESS);
2819 break;
2820
2821 case SERVICE_STOP:
2822 service_enter_signal(s, SERVICE_STOP_SIGTERM, f);
2823 break;
2824
2825 case SERVICE_STOP_SIGABRT:
2826 case SERVICE_STOP_SIGTERM:
2827 case SERVICE_STOP_SIGKILL:
2828 if (main_pid_good(s) <= 0)
2829 service_enter_stop_post(s, f);
2830
2831 /* If there is still a service
2832 * process around, wait until
2833 * that one quit, too */
2834 break;
2835
2836 case SERVICE_STOP_POST:
2837 case SERVICE_FINAL_SIGTERM:
2838 case SERVICE_FINAL_SIGKILL:
2839 if (main_pid_good(s) <= 0)
2840 service_enter_dead(s, f, true);
2841 break;
2842
2843 default:
2844 assert_not_reached("Uh, control process died at wrong time.");
2845 }
2846 }
2847 }
2848
2849 /* Notify clients about changed exit status */
2850 unit_add_to_dbus_queue(u);
2851
2852 /* We got one SIGCHLD for the service, let's watch all
2853 * processes that are now running of the service, and watch
2854 * that. Among the PIDs we then watch will be children
2855 * reassigned to us, which hopefully allows us to identify
2856 * when all children are gone */
2857 unit_tidy_watch_pids(u, s->main_pid, s->control_pid);
2858 unit_watch_all_pids(u);
2859
2860 /* If the PID set is empty now, then let's finish this off */
2861 if (set_isempty(u->pids))
2862 service_notify_cgroup_empty_event(u);
2863 }
2864
2865 static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) {
2866 Service *s = SERVICE(userdata);
2867
2868 assert(s);
2869 assert(source == s->timer_event_source);
2870
2871 switch (s->state) {
2872
2873 case SERVICE_START_PRE:
2874 case SERVICE_START:
2875 log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", s->state == SERVICE_START ? "Start" : "Start-pre");
2876 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2877 break;
2878
2879 case SERVICE_START_POST:
2880 log_unit_warning(UNIT(s), "Start-post operation timed out. Stopping.");
2881 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2882 break;
2883
2884 case SERVICE_RUNNING:
2885 log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping.");
2886 service_enter_stop(s, SERVICE_FAILURE_TIMEOUT);
2887 break;
2888
2889 case SERVICE_RELOAD:
2890 log_unit_warning(UNIT(s), "Reload operation timed out. Stopping.");
2891 service_unwatch_control_pid(s);
2892 service_kill_control_processes(s);
2893 s->reload_result = SERVICE_FAILURE_TIMEOUT;
2894 service_enter_running(s, SERVICE_SUCCESS);
2895 break;
2896
2897 case SERVICE_STOP:
2898 log_unit_warning(UNIT(s), "Stopping timed out. Terminating.");
2899 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2900 break;
2901
2902 case SERVICE_STOP_SIGABRT:
2903 log_unit_warning(UNIT(s), "State 'stop-sigabrt' timed out. Terminating.");
2904 service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2905 break;
2906
2907 case SERVICE_STOP_SIGTERM:
2908 if (s->kill_context.send_sigkill) {
2909 log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing.");
2910 service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2911 } else {
2912 log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2913 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2914 }
2915
2916 break;
2917
2918 case SERVICE_STOP_SIGKILL:
2919 /* Uh, we sent a SIGKILL and it is still not gone?
2920 * Must be something we cannot kill, so let's just be
2921 * weirded out and continue */
2922
2923 log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring.");
2924 service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT);
2925 break;
2926
2927 case SERVICE_STOP_POST:
2928 log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating.");
2929 service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT);
2930 break;
2931
2932 case SERVICE_FINAL_SIGTERM:
2933 if (s->kill_context.send_sigkill) {
2934 log_unit_warning(UNIT(s), "State 'stop-final-sigterm' timed out. Killing.");
2935 service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT);
2936 } else {
2937 log_unit_warning(UNIT(s), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2938 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false);
2939 }
2940
2941 break;
2942
2943 case SERVICE_FINAL_SIGKILL:
2944 log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode.");
2945 service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true);
2946 break;
2947
2948 case SERVICE_AUTO_RESTART:
2949 log_unit_info(UNIT(s),
2950 s->restart_usec > 0 ?
2951 "Service hold-off time over, scheduling restart." :
2952 "Service has no hold-off time, scheduling restart.");
2953 service_enter_restart(s);
2954 break;
2955
2956 default:
2957 assert_not_reached("Timeout at wrong time.");
2958 }
2959
2960 return 0;
2961 }
2962
2963 static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) {
2964 Service *s = SERVICE(userdata);
2965 char t[FORMAT_TIMESPAN_MAX];
2966
2967 assert(s);
2968 assert(source == s->watchdog_event_source);
2969
2970 log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!",
2971 format_timespan(t, sizeof(t), s->watchdog_usec, 1));
2972
2973 service_enter_signal(s, SERVICE_STOP_SIGABRT, SERVICE_FAILURE_WATCHDOG);
2974
2975 return 0;
2976 }
2977
2978 static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds) {
2979 Service *s = SERVICE(u);
2980 _cleanup_free_ char *cc = NULL;
2981 bool notify_dbus = false;
2982 const char *e;
2983
2984 assert(u);
2985
2986 cc = strv_join(tags, ", ");
2987
2988 if (s->notify_access == NOTIFY_NONE) {
2989 log_unit_warning(u, "Got notification message from PID "PID_FMT", but reception is disabled.", pid);
2990 return;
2991 } else if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2992 if (s->main_pid != 0)
2993 log_unit_warning(u, "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid, s->main_pid);
2994 else
2995 log_unit_debug(u, "Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", pid);
2996 return;
2997 } else
2998 log_unit_debug(u, "Got notification message from PID "PID_FMT" (%s)", pid, isempty(cc) ? "n/a" : cc);
2999
3000 /* Interpret MAINPID= */
3001 e = strv_find_startswith(tags, "MAINPID=");
3002 if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) {
3003 if (parse_pid(e, &pid) < 0)
3004 log_unit_warning(u, "Failed to parse MAINPID= field in notification message: %s", e);
3005 else {
3006 service_set_main_pid(s, pid);
3007 unit_watch_pid(UNIT(s), pid);
3008 notify_dbus = true;
3009 }
3010 }
3011
3012 /* Interpret RELOADING= */
3013 if (strv_find(tags, "RELOADING=1")) {
3014
3015 s->notify_state = NOTIFY_RELOADING;
3016
3017 if (s->state == SERVICE_RUNNING)
3018 service_enter_reload_by_notify(s);
3019
3020 notify_dbus = true;
3021 }
3022
3023 /* Interpret READY= */
3024 if (strv_find(tags, "READY=1")) {
3025
3026 s->notify_state = NOTIFY_READY;
3027
3028 /* Type=notify services inform us about completed
3029 * initialization with READY=1 */
3030 if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START)
3031 service_enter_start_post(s);
3032
3033 /* Sending READY=1 while we are reloading informs us
3034 * that the reloading is complete */
3035 if (s->state == SERVICE_RELOAD && s->control_pid == 0)
3036 service_enter_running(s, SERVICE_SUCCESS);
3037
3038 notify_dbus = true;
3039 }
3040
3041 /* Interpret STOPPING= */
3042 if (strv_find(tags, "STOPPING=1")) {
3043
3044 s->notify_state = NOTIFY_STOPPING;
3045
3046 if (s->state == SERVICE_RUNNING)
3047 service_enter_stop_by_notify(s);
3048
3049 notify_dbus = true;
3050 }
3051
3052 /* Interpret STATUS= */
3053 e = strv_find_startswith(tags, "STATUS=");
3054 if (e) {
3055 _cleanup_free_ char *t = NULL;
3056
3057 if (!isempty(e)) {
3058 if (!utf8_is_valid(e))
3059 log_unit_warning(u, "Status message in notification message is not UTF-8 clean.");
3060 else {
3061 t = strdup(e);
3062 if (!t)
3063 log_oom();
3064 }
3065 }
3066
3067 if (!streq_ptr(s->status_text, t)) {
3068
3069 free(s->status_text);
3070 s->status_text = t;
3071 t = NULL;
3072
3073 notify_dbus = true;
3074 }
3075 }
3076
3077 /* Interpret ERRNO= */
3078 e = strv_find_startswith(tags, "ERRNO=");
3079 if (e) {
3080 int status_errno;
3081
3082 if (safe_atoi(e, &status_errno) < 0 || status_errno < 0)
3083 log_unit_warning(u, "Failed to parse ERRNO= field in notification message: %s", e);
3084 else {
3085 if (s->status_errno != status_errno) {
3086 s->status_errno = status_errno;
3087 notify_dbus = true;
3088 }
3089 }
3090 }
3091
3092 /* Interpret WATCHDOG= */
3093 if (strv_find(tags, "WATCHDOG=1"))
3094 service_reset_watchdog(s);
3095
3096 if (strv_find(tags, "FDSTORE=1")) {
3097 const char *name;
3098
3099 name = strv_find_startswith(tags, "FDNAME=");
3100 if (name && !fdname_is_valid(name)) {
3101 log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring.");
3102 name = NULL;
3103 }
3104
3105 service_add_fd_store_set(s, fds, name);
3106 }
3107
3108 /* Notify clients about changed status or main pid */
3109 if (notify_dbus)
3110 unit_add_to_dbus_queue(u);
3111 }
3112
3113 static int service_get_timeout(Unit *u, uint64_t *timeout) {
3114 Service *s = SERVICE(u);
3115 int r;
3116
3117 if (!s->timer_event_source)
3118 return 0;
3119
3120 r = sd_event_source_get_time(s->timer_event_source, timeout);
3121 if (r < 0)
3122 return r;
3123
3124 return 1;
3125 }
3126
3127 static void service_bus_name_owner_change(
3128 Unit *u,
3129 const char *name,
3130 const char *old_owner,
3131 const char *new_owner) {
3132
3133 Service *s = SERVICE(u);
3134 int r;
3135
3136 assert(s);
3137 assert(name);
3138
3139 assert(streq(s->bus_name, name));
3140 assert(old_owner || new_owner);
3141
3142 if (old_owner && new_owner)
3143 log_unit_debug(u, "D-Bus name %s changed owner from %s to %s", name, old_owner, new_owner);
3144 else if (old_owner)
3145 log_unit_debug(u, "D-Bus name %s no longer registered by %s", name, old_owner);
3146 else
3147 log_unit_debug(u, "D-Bus name %s now registered by %s", name, new_owner);
3148
3149 s->bus_name_good = !!new_owner;
3150
3151 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3152 r = free_and_strdup(&s->bus_name_owner, new_owner);
3153 if (r < 0) {
3154 log_unit_error_errno(u, r, "Unable to set new bus name owner %s: %m", new_owner);
3155 return;
3156 }
3157
3158 if (s->type == SERVICE_DBUS) {
3159
3160 /* service_enter_running() will figure out what to
3161 * do */
3162 if (s->state == SERVICE_RUNNING)
3163 service_enter_running(s, SERVICE_SUCCESS);
3164 else if (s->state == SERVICE_START && new_owner)
3165 service_enter_start_post(s);
3166
3167 } else if (new_owner &&
3168 s->main_pid <= 0 &&
3169 (s->state == SERVICE_START ||
3170 s->state == SERVICE_START_POST ||
3171 s->state == SERVICE_RUNNING ||
3172 s->state == SERVICE_RELOAD)) {
3173
3174 _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL;
3175 pid_t pid;
3176
3177 /* Try to acquire PID from bus service */
3178
3179 r = sd_bus_get_name_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds);
3180 if (r >= 0)
3181 r = sd_bus_creds_get_pid(creds, &pid);
3182 if (r >= 0) {
3183 log_unit_debug(u, "D-Bus name %s is now owned by process %u", name, (unsigned) pid);
3184
3185 service_set_main_pid(s, pid);
3186 unit_watch_pid(UNIT(s), pid);
3187 }
3188 }
3189 }
3190
3191 int service_set_socket_fd(Service *s, int fd, Socket *sock, bool selinux_context_net) {
3192 _cleanup_free_ char *peer = NULL;
3193 int r;
3194
3195 assert(s);
3196 assert(fd >= 0);
3197
3198 /* This is called by the socket code when instantiating a new
3199 * service for a stream socket and the socket needs to be
3200 * configured. */
3201
3202 if (UNIT(s)->load_state != UNIT_LOADED)
3203 return -EINVAL;
3204
3205 if (s->socket_fd >= 0)
3206 return -EBUSY;
3207
3208 if (s->state != SERVICE_DEAD)
3209 return -EAGAIN;
3210
3211 if (getpeername_pretty(fd, true, &peer) >= 0) {
3212
3213 if (UNIT(s)->description) {
3214 _cleanup_free_ char *a;
3215
3216 a = strjoin(UNIT(s)->description, " (", peer, ")", NULL);
3217 if (!a)
3218 return -ENOMEM;
3219
3220 r = unit_set_description(UNIT(s), a);
3221 } else
3222 r = unit_set_description(UNIT(s), peer);
3223
3224 if (r < 0)
3225 return r;
3226 }
3227
3228 s->socket_fd = fd;
3229 s->socket_fd_selinux_context_net = selinux_context_net;
3230
3231 unit_ref_set(&s->accept_socket, UNIT(sock));
3232
3233 return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false);
3234 }
3235
3236 static void service_reset_failed(Unit *u) {
3237 Service *s = SERVICE(u);
3238
3239 assert(s);
3240
3241 if (s->state == SERVICE_FAILED)
3242 service_set_state(s, SERVICE_DEAD);
3243
3244 s->result = SERVICE_SUCCESS;
3245 s->reload_result = SERVICE_SUCCESS;
3246
3247 RATELIMIT_RESET(s->start_limit);
3248 }
3249
3250 static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) {
3251 Service *s = SERVICE(u);
3252
3253 return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error);
3254 }
3255
3256 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3257 [SERVICE_RESTART_NO] = "no",
3258 [SERVICE_RESTART_ON_SUCCESS] = "on-success",
3259 [SERVICE_RESTART_ON_FAILURE] = "on-failure",
3260 [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal",
3261 [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog",
3262 [SERVICE_RESTART_ON_ABORT] = "on-abort",
3263 [SERVICE_RESTART_ALWAYS] = "always",
3264 };
3265
3266 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3267
3268 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
3269 [SERVICE_SIMPLE] = "simple",
3270 [SERVICE_FORKING] = "forking",
3271 [SERVICE_ONESHOT] = "oneshot",
3272 [SERVICE_DBUS] = "dbus",
3273 [SERVICE_NOTIFY] = "notify",
3274 [SERVICE_IDLE] = "idle"
3275 };
3276
3277 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3278
3279 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
3280 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3281 [SERVICE_EXEC_START] = "ExecStart",
3282 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3283 [SERVICE_EXEC_RELOAD] = "ExecReload",
3284 [SERVICE_EXEC_STOP] = "ExecStop",
3285 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3286 };
3287
3288 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3289
3290 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3291 [NOTIFY_NONE] = "none",
3292 [NOTIFY_MAIN] = "main",
3293 [NOTIFY_ALL] = "all"
3294 };
3295
3296 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3297
3298 static const char* const notify_state_table[_NOTIFY_STATE_MAX] = {
3299 [NOTIFY_UNKNOWN] = "unknown",
3300 [NOTIFY_READY] = "ready",
3301 [NOTIFY_RELOADING] = "reloading",
3302 [NOTIFY_STOPPING] = "stopping",
3303 };
3304
3305 DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState);
3306
3307 static const char* const service_result_table[_SERVICE_RESULT_MAX] = {
3308 [SERVICE_SUCCESS] = "success",
3309 [SERVICE_FAILURE_RESOURCES] = "resources",
3310 [SERVICE_FAILURE_TIMEOUT] = "timeout",
3311 [SERVICE_FAILURE_EXIT_CODE] = "exit-code",
3312 [SERVICE_FAILURE_SIGNAL] = "signal",
3313 [SERVICE_FAILURE_CORE_DUMP] = "core-dump",
3314 [SERVICE_FAILURE_WATCHDOG] = "watchdog",
3315 [SERVICE_FAILURE_START_LIMIT] = "start-limit"
3316 };
3317
3318 DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult);
3319
3320 const UnitVTable service_vtable = {
3321 .object_size = sizeof(Service),
3322 .exec_context_offset = offsetof(Service, exec_context),
3323 .cgroup_context_offset = offsetof(Service, cgroup_context),
3324 .kill_context_offset = offsetof(Service, kill_context),
3325 .exec_runtime_offset = offsetof(Service, exec_runtime),
3326
3327 .sections =
3328 "Unit\0"
3329 "Service\0"
3330 "Install\0",
3331 .private_section = "Service",
3332
3333 .init = service_init,
3334 .done = service_done,
3335 .load = service_load,
3336 .release_resources = service_release_resources,
3337
3338 .coldplug = service_coldplug,
3339
3340 .dump = service_dump,
3341
3342 .start = service_start,
3343 .stop = service_stop,
3344 .reload = service_reload,
3345
3346 .can_reload = service_can_reload,
3347
3348 .kill = service_kill,
3349
3350 .serialize = service_serialize,
3351 .deserialize_item = service_deserialize_item,
3352
3353 .active_state = service_active_state,
3354 .sub_state_to_string = service_sub_state_to_string,
3355
3356 .check_gc = service_check_gc,
3357
3358 .sigchld_event = service_sigchld_event,
3359
3360 .reset_failed = service_reset_failed,
3361
3362 .notify_cgroup_empty = service_notify_cgroup_empty_event,
3363 .notify_message = service_notify_message,
3364
3365 .bus_name_owner_change = service_bus_name_owner_change,
3366
3367 .bus_vtable = bus_service_vtable,
3368 .bus_set_property = bus_service_set_property,
3369 .bus_commit_properties = bus_service_commit_properties,
3370
3371 .get_timeout = service_get_timeout,
3372 .can_transient = true,
3373
3374 .status_message_formats = {
3375 .starting_stopping = {
3376 [0] = "Starting %s...",
3377 [1] = "Stopping %s...",
3378 },
3379 .finished_start_job = {
3380 [JOB_DONE] = "Started %s.",
3381 [JOB_FAILED] = "Failed to start %s.",
3382 },
3383 .finished_stop_job = {
3384 [JOB_DONE] = "Stopped %s.",
3385 [JOB_FAILED] = "Stopped (with error) %s.",
3386 },
3387 },
3388 };