1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
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.
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.
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/>.
30 #include "load-fragment.h"
31 #include "load-dropin.h"
34 #include "unit-name.h"
35 #include "unit-printf.h"
36 #include "dbus-service.h"
38 #include "exit-status.h"
40 #include "path-util.h"
45 #include "bus-error.h"
47 #include "bus-kernel.h"
48 #include "formats-util.h"
49 #include "process-util.h"
50 #include "signal-util.h"
52 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
53 [SERVICE_DEAD
] = UNIT_INACTIVE
,
54 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
55 [SERVICE_START
] = UNIT_ACTIVATING
,
56 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
57 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
58 [SERVICE_EXITED
] = UNIT_ACTIVE
,
59 [SERVICE_RELOAD
] = UNIT_RELOADING
,
60 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
61 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
62 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
63 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
64 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
65 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
66 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
67 [SERVICE_FAILED
] = UNIT_FAILED
,
68 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
71 /* For Type=idle we never want to delay any other jobs, hence we
72 * consider idle jobs active as soon as we start working on them */
73 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
74 [SERVICE_DEAD
] = UNIT_INACTIVE
,
75 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
76 [SERVICE_START
] = UNIT_ACTIVE
,
77 [SERVICE_START_POST
] = UNIT_ACTIVE
,
78 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
79 [SERVICE_EXITED
] = UNIT_ACTIVE
,
80 [SERVICE_RELOAD
] = UNIT_RELOADING
,
81 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
82 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
83 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
84 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
85 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
86 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
87 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
88 [SERVICE_FAILED
] = UNIT_FAILED
,
89 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
92 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
93 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
94 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
96 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
97 static void service_enter_reload_by_notify(Service
*s
);
99 static void service_init(Unit
*u
) {
100 Service
*s
= SERVICE(u
);
103 assert(u
->load_state
== UNIT_STUB
);
105 s
->timeout_start_usec
= u
->manager
->default_timeout_start_usec
;
106 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
107 s
->restart_usec
= u
->manager
->default_restart_usec
;
108 s
->type
= _SERVICE_TYPE_INVALID
;
110 s
->bus_endpoint_fd
= -1;
111 s
->guess_main_pid
= true;
113 RATELIMIT_INIT(s
->start_limit
, u
->manager
->default_start_limit_interval
, u
->manager
->default_start_limit_burst
);
115 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
118 static void service_unwatch_control_pid(Service
*s
) {
121 if (s
->control_pid
<= 0)
124 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
128 static void service_unwatch_main_pid(Service
*s
) {
131 if (s
->main_pid
<= 0)
134 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
138 static void service_unwatch_pid_file(Service
*s
) {
139 if (!s
->pid_file_pathspec
)
142 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
143 path_spec_unwatch(s
->pid_file_pathspec
);
144 path_spec_done(s
->pid_file_pathspec
);
145 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
148 static int service_set_main_pid(Service
*s
, pid_t pid
) {
159 if (s
->main_pid
== pid
&& s
->main_pid_known
)
162 if (s
->main_pid
!= pid
) {
163 service_unwatch_main_pid(s
);
164 exec_status_start(&s
->main_exec_status
, pid
);
168 s
->main_pid_known
= true;
170 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
171 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
172 s
->main_pid_alien
= true;
174 s
->main_pid_alien
= false;
179 static void service_close_socket_fd(Service
*s
) {
182 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
185 static void service_connection_unref(Service
*s
) {
188 if (!UNIT_ISSET(s
->accept_socket
))
191 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
192 unit_ref_unset(&s
->accept_socket
);
195 static void service_stop_watchdog(Service
*s
) {
198 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
199 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
202 static void service_start_watchdog(Service
*s
) {
207 if (s
->watchdog_usec
<= 0)
210 if (s
->watchdog_event_source
) {
211 r
= sd_event_source_set_time(s
->watchdog_event_source
, s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
);
213 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
217 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
219 r
= sd_event_add_time(
220 UNIT(s
)->manager
->event
,
221 &s
->watchdog_event_source
,
223 s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
, 0,
224 service_dispatch_watchdog
, s
);
226 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
230 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
232 /* Let's process everything else which might be a sign
233 * of living before we consider a service died. */
234 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
238 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
241 static void service_reset_watchdog(Service
*s
) {
244 dual_timestamp_get(&s
->watchdog_timestamp
);
245 service_start_watchdog(s
);
248 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
254 assert(fs
->service
->n_fd_store
> 0);
255 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
256 fs
->service
->n_fd_store
--;
259 if (fs
->event_source
) {
260 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
261 sd_event_source_unref(fs
->event_source
);
269 static void service_release_resources(Unit
*u
) {
270 Service
*s
= SERVICE(u
);
277 log_unit_debug(u
, "Releasing all resources.");
280 service_fd_store_unlink(s
->fd_store
);
282 assert(s
->n_fd_store
== 0);
285 static void service_done(Unit
*u
) {
286 Service
*s
= SERVICE(u
);
290 s
->pid_file
= mfree(s
->pid_file
);
291 s
->status_text
= mfree(s
->status_text
);
292 s
->reboot_arg
= mfree(s
->reboot_arg
);
294 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
295 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
296 s
->control_command
= NULL
;
297 s
->main_command
= NULL
;
299 exit_status_set_free(&s
->restart_prevent_status
);
300 exit_status_set_free(&s
->restart_force_status
);
301 exit_status_set_free(&s
->success_status
);
303 /* This will leak a process, but at least no memory or any of
305 service_unwatch_main_pid(s
);
306 service_unwatch_control_pid(s
);
307 service_unwatch_pid_file(s
);
310 unit_unwatch_bus_name(u
, s
->bus_name
);
311 s
->bus_name
= mfree(s
->bus_name
);
314 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
315 service_close_socket_fd(s
);
316 service_connection_unref(s
);
318 unit_ref_unset(&s
->accept_socket
);
320 service_stop_watchdog(s
);
322 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
324 service_release_resources(u
);
327 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
328 ServiceFDStore
*fs
= userdata
;
333 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
334 service_fd_store_unlink(fs
);
338 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
345 if (s
->n_fd_store
>= s
->n_fd_store_max
)
348 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
349 r
= same_fd(fs
->fd
, fd
);
353 /* Already included */
359 fs
= new0(ServiceFDStore
, 1);
365 fs
->fdname
= strdup(name
?: "stored");
369 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
376 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
378 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
384 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
389 if (fdset_size(fds
) <= 0)
392 while (s
->n_fd_store
< s
->n_fd_store_max
) {
393 _cleanup_close_
int fd
= -1;
395 fd
= fdset_steal_first(fds
);
399 r
= service_add_fd_store(s
, fd
, name
);
401 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
403 log_unit_debug(UNIT(s
), "Added fd to fd store.");
408 if (fdset_size(fds
) > 0)
409 log_unit_warning(UNIT(s
), "Tried to store more fds than FDStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
414 static int service_arm_timer(Service
*s
, usec_t usec
) {
419 if (s
->timer_event_source
) {
420 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
424 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
427 r
= sd_event_add_time(
428 UNIT(s
)->manager
->event
,
429 &s
->timer_event_source
,
431 now(CLOCK_MONOTONIC
) + usec
, 0,
432 service_dispatch_timer
, s
);
436 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
441 static int service_verify(Service
*s
) {
444 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
447 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
448 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
452 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
453 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
457 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
458 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
462 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
463 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
467 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
468 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
472 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
473 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
477 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
478 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
482 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
483 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
485 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
486 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
490 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
491 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
493 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
494 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
499 static int service_add_default_dependencies(Service
*s
) {
504 /* Add a number of automatic dependencies useful for the
505 * majority of services. */
507 /* First, pull in base system */
508 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
512 /* Second, activate normal shutdown */
513 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
516 static void service_fix_output(Service
*s
) {
519 /* If nothing has been explicitly configured, patch default
520 * output in. If input is socket/tty we avoid this however,
521 * since in that case we want output to default to the same
522 * place as we read input from. */
524 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
525 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
526 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
527 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
529 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
530 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
531 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
534 static int service_add_extras(Service
*s
) {
539 if (s
->type
== _SERVICE_TYPE_INVALID
) {
540 /* Figure out a type automatically */
542 s
->type
= SERVICE_DBUS
;
543 else if (s
->exec_command
[SERVICE_EXEC_START
])
544 s
->type
= SERVICE_SIMPLE
;
546 s
->type
= SERVICE_ONESHOT
;
549 /* Oneshot services have disabled start timeout by default */
550 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
551 s
->timeout_start_usec
= 0;
553 service_fix_output(s
);
555 r
= unit_patch_contexts(UNIT(s
));
559 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
563 r
= unit_set_default_slice(UNIT(s
));
567 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
568 s
->notify_access
= NOTIFY_MAIN
;
570 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
571 s
->notify_access
= NOTIFY_MAIN
;
576 n
= strjoina(s
->bus_name
, ".busname");
577 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
581 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
583 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
585 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
588 if (UNIT(s
)->default_dependencies
) {
589 r
= service_add_default_dependencies(s
);
597 static int service_load(Unit
*u
) {
598 Service
*s
= SERVICE(u
);
603 /* Load a .service file */
604 r
= unit_load_fragment(u
);
608 /* Still nothing found? Then let's give up */
609 if (u
->load_state
== UNIT_STUB
)
612 /* This is a new unit? Then let's add in some extras */
613 if (u
->load_state
== UNIT_LOADED
) {
615 /* We were able to load something, then let's add in
616 * the dropin directories. */
617 r
= unit_load_dropin(u
);
621 /* This is a new unit? Then let's add in some
623 r
= service_add_extras(s
);
628 return service_verify(s
);
631 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
632 ServiceExecCommand c
;
633 Service
*s
= SERVICE(u
);
638 prefix
= strempty(prefix
);
639 prefix2
= strjoina(prefix
, "\t");
642 "%sService State: %s\n"
644 "%sReload Result: %s\n"
645 "%sPermissionsStartOnly: %s\n"
646 "%sRootDirectoryStartOnly: %s\n"
647 "%sRemainAfterExit: %s\n"
648 "%sGuessMainPID: %s\n"
651 "%sNotifyAccess: %s\n"
652 "%sNotifyState: %s\n",
653 prefix
, service_state_to_string(s
->state
),
654 prefix
, service_result_to_string(s
->result
),
655 prefix
, service_result_to_string(s
->reload_result
),
656 prefix
, yes_no(s
->permissions_start_only
),
657 prefix
, yes_no(s
->root_directory_start_only
),
658 prefix
, yes_no(s
->remain_after_exit
),
659 prefix
, yes_no(s
->guess_main_pid
),
660 prefix
, service_type_to_string(s
->type
),
661 prefix
, service_restart_to_string(s
->restart
),
662 prefix
, notify_access_to_string(s
->notify_access
),
663 prefix
, notify_state_to_string(s
->notify_state
));
665 if (s
->control_pid
> 0)
667 "%sControl PID: "PID_FMT
"\n",
668 prefix
, s
->control_pid
);
672 "%sMain PID: "PID_FMT
"\n"
673 "%sMain PID Known: %s\n"
674 "%sMain PID Alien: %s\n",
676 prefix
, yes_no(s
->main_pid_known
),
677 prefix
, yes_no(s
->main_pid_alien
));
682 prefix
, s
->pid_file
);
687 "%sBus Name Good: %s\n",
689 prefix
, yes_no(s
->bus_name_good
));
691 kill_context_dump(&s
->kill_context
, f
, prefix
);
692 exec_context_dump(&s
->exec_context
, f
, prefix
);
694 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
696 if (!s
->exec_command
[c
])
699 fprintf(f
, "%s-> %s:\n",
700 prefix
, service_exec_command_to_string(c
));
702 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
706 fprintf(f
, "%sStatus Text: %s\n",
707 prefix
, s
->status_text
);
709 if (s
->n_fd_store_max
> 0)
711 "%sFile Descriptor Store Max: %u\n"
712 "%sFile Descriptor Store Current: %u\n",
713 prefix
, s
->n_fd_store_max
,
714 prefix
, s
->n_fd_store
);
717 static int service_load_pid_file(Service
*s
, bool may_warn
) {
718 _cleanup_free_
char *k
= NULL
;
727 r
= read_one_line_file(s
->pid_file
, &k
);
730 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
));
734 r
= parse_pid(k
, &pid
);
737 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
741 if (!pid_is_alive(pid
)) {
743 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
747 if (s
->main_pid_known
) {
748 if (pid
== s
->main_pid
)
751 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
753 service_unwatch_main_pid(s
);
754 s
->main_pid_known
= false;
756 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
758 r
= service_set_main_pid(s
, pid
);
762 r
= unit_watch_pid(UNIT(s
), pid
);
764 /* FIXME: we need to do something here */
765 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
772 static int service_search_main_pid(Service
*s
) {
778 /* If we know it anyway, don't ever fallback to unreliable
780 if (s
->main_pid_known
)
783 if (!s
->guess_main_pid
)
786 assert(s
->main_pid
<= 0);
788 r
= unit_search_main_pid(UNIT(s
), &pid
);
792 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
793 r
= service_set_main_pid(s
, pid
);
797 r
= unit_watch_pid(UNIT(s
), pid
);
799 /* FIXME: we need to do something here */
800 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
807 static void service_set_state(Service
*s
, ServiceState state
) {
808 ServiceState old_state
;
809 const UnitActiveState
*table
;
813 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
815 old_state
= s
->state
;
818 service_unwatch_pid_file(s
);
821 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
823 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
824 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
825 SERVICE_AUTO_RESTART
))
826 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
829 SERVICE_START
, SERVICE_START_POST
,
830 SERVICE_RUNNING
, SERVICE_RELOAD
,
831 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
832 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
833 service_unwatch_main_pid(s
);
834 s
->main_command
= NULL
;
838 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
840 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
841 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
842 service_unwatch_control_pid(s
);
843 s
->control_command
= NULL
;
844 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
847 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
848 unit_unwatch_all_pids(UNIT(s
));
851 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
852 SERVICE_RUNNING
, SERVICE_RELOAD
,
853 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
854 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
855 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
856 service_close_socket_fd(s
);
857 service_connection_unref(s
);
860 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
861 service_stop_watchdog(s
);
863 /* For the inactive states unit_notify() will trim the cgroup,
864 * but for exit we have to do that ourselves... */
865 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
866 unit_prune_cgroup(UNIT(s
));
868 /* For remain_after_exit services, let's see if we can "release" the
869 * hold on the console, since unit_notify() only does that in case of
871 if (state
== SERVICE_EXITED
&&
872 s
->remain_after_exit
&&
873 UNIT(s
)->manager
->n_on_console
> 0) {
877 ec
= unit_get_exec_context(UNIT(s
));
878 if (ec
&& exec_context_may_touch_console(ec
)) {
879 Manager
*m
= UNIT(s
)->manager
;
882 if (m
->n_on_console
== 0)
883 /* unset no_console_output flag, since the console is free */
884 m
->no_console_output
= false;
888 if (old_state
!= state
)
889 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
891 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
892 s
->reload_result
= SERVICE_SUCCESS
;
895 static int service_coldplug(Unit
*u
) {
896 Service
*s
= SERVICE(u
);
900 assert(s
->state
== SERVICE_DEAD
);
902 if (s
->deserialized_state
!= s
->state
) {
904 if (IN_SET(s
->deserialized_state
,
905 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
907 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
908 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
912 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
914 /* For the start/stop timeouts 0 means off */
916 r
= service_arm_timer(s
, k
);
922 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
924 /* The restart timeouts 0 means immediately */
925 r
= service_arm_timer(s
, s
->restart_usec
);
930 if (pid_is_unwaited(s
->main_pid
) &&
931 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
932 IN_SET(s
->deserialized_state
,
933 SERVICE_START
, SERVICE_START_POST
,
934 SERVICE_RUNNING
, SERVICE_RELOAD
,
935 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
936 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
937 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
942 if (pid_is_unwaited(s
->control_pid
) &&
943 IN_SET(s
->deserialized_state
,
944 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
946 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
947 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
948 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
953 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
954 unit_watch_all_pids(UNIT(s
));
956 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
957 service_start_watchdog(s
);
959 service_set_state(s
, s
->deserialized_state
);
965 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
966 _cleanup_strv_free_
char **rfd_names
= NULL
;
967 _cleanup_free_
int *rfds
= NULL
;
974 if (s
->socket_fd
>= 0) {
976 /* Pass the per-connection socket */
981 rfds
[0] = s
->socket_fd
;
983 rfd_names
= strv_new("connection", NULL
);
992 /* Pass all our configured sockets for singleton services */
994 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
995 _cleanup_free_
int *cfds
= NULL
;
999 if (u
->type
!= UNIT_SOCKET
)
1004 cn_fds
= socket_collect_fds(sock
, &cfds
);
1019 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1023 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1029 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1035 if (s
->n_fd_store
> 0) {
1040 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1046 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1052 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1053 rfds
[rn_fds
] = fs
->fd
;
1054 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1055 if (!rfd_names
[rn_fds
])
1061 rfd_names
[rn_fds
] = NULL
;
1065 *fd_names
= rfd_names
;
1073 static int service_spawn(
1078 bool apply_permissions
,
1080 bool apply_tty_stdin
,
1084 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1085 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1086 _cleanup_free_
int *fds
= NULL
;
1087 unsigned n_fds
= 0, n_env
= 0;
1091 ExecParameters exec_params
= {
1092 .apply_permissions
= apply_permissions
,
1093 .apply_chroot
= apply_chroot
,
1094 .apply_tty_stdin
= apply_tty_stdin
,
1095 .bus_endpoint_fd
= -1,
1096 .selinux_context_net
= s
->socket_fd_selinux_context_net
1105 (void) unit_realize_cgroup(UNIT(s
));
1106 if (s
->reset_cpu_usage
) {
1107 (void) unit_reset_cpu_usage(UNIT(s
));
1108 s
->reset_cpu_usage
= false;
1111 r
= unit_setup_exec_runtime(UNIT(s
));
1116 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1117 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1118 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1120 r
= service_collect_fds(s
, &fds
, &fd_names
);
1128 r
= service_arm_timer(s
, timeout
);
1132 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1134 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1138 our_env
= new0(char*, 6);
1144 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1145 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1150 if (s
->main_pid
> 0)
1151 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1156 if (UNIT(s
)->manager
->running_as
!= MANAGER_SYSTEM
)
1157 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1162 if (s
->socket_fd
>= 0) {
1163 union sockaddr_union sa
;
1164 socklen_t salen
= sizeof(sa
);
1166 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1172 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1173 _cleanup_free_
char *addr
= NULL
;
1177 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1181 t
= strappend("REMOTE_ADDR=", addr
);
1186 our_env
[n_env
++] = t
;
1188 port
= sockaddr_port(&sa
.sa
);
1194 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0) {
1198 our_env
[n_env
++] = t
;
1202 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1208 if (is_control
&& UNIT(s
)->cgroup_path
) {
1209 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1210 cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1212 path
= UNIT(s
)->cgroup_path
;
1214 if (s
->exec_context
.bus_endpoint
) {
1215 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
? "system" : "user",
1216 UNIT(s
)->id
, &bus_endpoint_path
);
1220 /* Pass the fd to the exec_params so that the child process can upload the policy.
1221 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1222 * as the service is running. */
1223 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1226 exec_params
.argv
= argv
;
1227 exec_params
.fds
= fds
;
1228 exec_params
.fd_names
= fd_names
;
1229 exec_params
.n_fds
= n_fds
;
1230 exec_params
.environment
= final_env
;
1231 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1232 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1233 exec_params
.cgroup_path
= path
;
1234 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1235 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1236 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1237 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1238 if (s
->type
== SERVICE_IDLE
)
1239 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1241 r
= exec_spawn(UNIT(s
),
1250 r
= unit_watch_pid(UNIT(s
), pid
);
1252 /* FIXME: we need to do something here */
1261 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1266 static int main_pid_good(Service
*s
) {
1269 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1272 /* If we know the pid file, then let's just check if it is
1274 if (s
->main_pid_known
) {
1276 /* If it's an alien child let's check if it is still
1278 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1279 return pid_is_alive(s
->main_pid
);
1281 /* .. otherwise assume we'll get a SIGCHLD for it,
1282 * which we really should wait for to collect exit
1283 * status and code */
1284 return s
->main_pid
> 0;
1287 /* We don't know the pid */
1291 _pure_
static int control_pid_good(Service
*s
) {
1294 return s
->control_pid
> 0;
1297 static int cgroup_good(Service
*s
) {
1302 if (!UNIT(s
)->cgroup_path
)
1305 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1312 static bool service_shall_restart(Service
*s
) {
1315 /* Don't restart after manual stops */
1316 if (s
->forbid_restart
)
1319 /* Never restart if this is configured as special exception */
1320 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1323 /* Restart if the exit code/status are configured as restart triggers */
1324 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1327 switch (s
->restart
) {
1329 case SERVICE_RESTART_NO
:
1332 case SERVICE_RESTART_ALWAYS
:
1335 case SERVICE_RESTART_ON_SUCCESS
:
1336 return s
->result
== SERVICE_SUCCESS
;
1338 case SERVICE_RESTART_ON_FAILURE
:
1339 return s
->result
!= SERVICE_SUCCESS
;
1341 case SERVICE_RESTART_ON_ABNORMAL
:
1342 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1344 case SERVICE_RESTART_ON_WATCHDOG
:
1345 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1347 case SERVICE_RESTART_ON_ABORT
:
1348 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1351 assert_not_reached("unknown restart setting");
1355 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1359 if (f
!= SERVICE_SUCCESS
)
1362 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1364 if (s
->result
!= SERVICE_SUCCESS
) {
1365 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1366 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1369 if (allow_restart
&& service_shall_restart(s
)) {
1371 r
= service_arm_timer(s
, s
->restart_usec
);
1375 service_set_state(s
, SERVICE_AUTO_RESTART
);
1378 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1379 s
->forbid_restart
= false;
1381 /* We want fresh tmpdirs in case service is started again immediately */
1382 exec_runtime_destroy(s
->exec_runtime
);
1383 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1385 /* Also, remove the runtime directory in */
1386 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1388 /* Try to delete the pid file. At this point it will be
1389 * out-of-date, and some software might be confused by it, so
1390 * let's remove it. */
1392 unlink_noerrno(s
->pid_file
);
1397 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1398 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1401 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1405 if (f
!= SERVICE_SUCCESS
)
1408 service_unwatch_control_pid(s
);
1409 unit_watch_all_pids(UNIT(s
));
1411 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1412 if (s
->control_command
) {
1413 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1415 r
= service_spawn(s
,
1417 s
->timeout_stop_usec
,
1419 !s
->permissions_start_only
,
1420 !s
->root_directory_start_only
,
1427 service_set_state(s
, SERVICE_STOP_POST
);
1429 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1434 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1435 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1438 static int state_to_kill_operation(ServiceState state
) {
1441 case SERVICE_STOP_SIGABRT
:
1444 case SERVICE_STOP_SIGTERM
:
1445 case SERVICE_FINAL_SIGTERM
:
1446 return KILL_TERMINATE
;
1448 case SERVICE_STOP_SIGKILL
:
1449 case SERVICE_FINAL_SIGKILL
:
1453 return _KILL_OPERATION_INVALID
;
1457 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1462 if (f
!= SERVICE_SUCCESS
)
1465 unit_watch_all_pids(UNIT(s
));
1467 r
= unit_kill_context(
1470 state_to_kill_operation(state
),
1479 if (s
->timeout_stop_usec
> 0) {
1480 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1485 service_set_state(s
, state
);
1486 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1487 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1488 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1489 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1490 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1491 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1493 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1498 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1500 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1501 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1503 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1506 static void service_enter_stop_by_notify(Service
*s
) {
1509 unit_watch_all_pids(UNIT(s
));
1511 if (s
->timeout_stop_usec
> 0)
1512 service_arm_timer(s
, s
->timeout_stop_usec
);
1514 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1515 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1518 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1523 if (f
!= SERVICE_SUCCESS
)
1526 service_unwatch_control_pid(s
);
1527 unit_watch_all_pids(UNIT(s
));
1529 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1530 if (s
->control_command
) {
1531 s
->control_command_id
= SERVICE_EXEC_STOP
;
1533 r
= service_spawn(s
,
1535 s
->timeout_stop_usec
,
1537 !s
->permissions_start_only
,
1538 !s
->root_directory_start_only
,
1545 service_set_state(s
, SERVICE_STOP
);
1547 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1552 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1553 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1556 static bool service_good(Service
*s
) {
1560 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1563 main_pid_ok
= main_pid_good(s
);
1564 if (main_pid_ok
> 0) /* It's alive */
1566 if (main_pid_ok
== 0) /* It's dead */
1569 /* OK, we don't know anything about the main PID, maybe
1570 * because there is none. Let's check the control group
1573 return cgroup_good(s
) != 0;
1576 static void service_enter_running(Service
*s
, ServiceResult f
) {
1579 if (f
!= SERVICE_SUCCESS
)
1582 if (service_good(s
)) {
1584 /* If there are any queued up sd_notify()
1585 * notifications, process them now */
1586 if (s
->notify_state
== NOTIFY_RELOADING
)
1587 service_enter_reload_by_notify(s
);
1588 else if (s
->notify_state
== NOTIFY_STOPPING
)
1589 service_enter_stop_by_notify(s
);
1591 service_set_state(s
, SERVICE_RUNNING
);
1593 } else if (s
->remain_after_exit
)
1594 service_set_state(s
, SERVICE_EXITED
);
1596 service_enter_stop(s
, SERVICE_SUCCESS
);
1599 static void service_enter_start_post(Service
*s
) {
1603 service_unwatch_control_pid(s
);
1604 service_reset_watchdog(s
);
1606 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1607 if (s
->control_command
) {
1608 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1610 r
= service_spawn(s
,
1612 s
->timeout_start_usec
,
1614 !s
->permissions_start_only
,
1615 !s
->root_directory_start_only
,
1622 service_set_state(s
, SERVICE_START_POST
);
1624 service_enter_running(s
, SERVICE_SUCCESS
);
1629 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1630 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1633 static void service_kill_control_processes(Service
*s
) {
1636 if (!UNIT(s
)->cgroup_path
)
1639 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1640 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1643 static void service_enter_start(Service
*s
) {
1650 service_unwatch_control_pid(s
);
1651 service_unwatch_main_pid(s
);
1653 /* We want to ensure that nobody leaks processes from
1654 * START_PRE here, so let's go on a killing spree, People
1655 * should not spawn long running processes from START_PRE. */
1656 service_kill_control_processes(s
);
1658 if (s
->type
== SERVICE_FORKING
) {
1659 s
->control_command_id
= SERVICE_EXEC_START
;
1660 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1662 s
->main_command
= NULL
;
1664 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1665 s
->control_command
= NULL
;
1667 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1671 assert(s
->type
== SERVICE_ONESHOT
);
1672 service_enter_start_post(s
);
1676 r
= service_spawn(s
,
1678 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1688 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1689 /* For simple services we immediately start
1690 * the START_POST binaries. */
1692 service_set_main_pid(s
, pid
);
1693 service_enter_start_post(s
);
1695 } else if (s
->type
== SERVICE_FORKING
) {
1697 /* For forking services we wait until the start
1698 * process exited. */
1700 s
->control_pid
= pid
;
1701 service_set_state(s
, SERVICE_START
);
1703 } else if (s
->type
== SERVICE_ONESHOT
||
1704 s
->type
== SERVICE_DBUS
||
1705 s
->type
== SERVICE_NOTIFY
) {
1707 /* For oneshot services we wait until the start
1708 * process exited, too, but it is our main process. */
1710 /* For D-Bus services we know the main pid right away,
1711 * but wait for the bus name to appear on the
1712 * bus. Notify services are similar. */
1714 service_set_main_pid(s
, pid
);
1715 service_set_state(s
, SERVICE_START
);
1717 assert_not_reached("Unknown service type");
1722 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1723 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1726 static void service_enter_start_pre(Service
*s
) {
1731 service_unwatch_control_pid(s
);
1733 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1734 if (s
->control_command
) {
1735 /* Before we start anything, let's clear up what might
1736 * be left from previous runs. */
1737 service_kill_control_processes(s
);
1739 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1741 r
= service_spawn(s
,
1743 s
->timeout_start_usec
,
1745 !s
->permissions_start_only
,
1746 !s
->root_directory_start_only
,
1753 service_set_state(s
, SERVICE_START_PRE
);
1755 service_enter_start(s
);
1760 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1761 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1764 static void service_enter_restart(Service
*s
) {
1765 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1770 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1771 /* Don't restart things if we are going down anyway */
1772 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1774 r
= service_arm_timer(s
, s
->restart_usec
);
1781 /* Any units that are bound to this service must also be
1782 * restarted. We use JOB_RESTART (instead of the more obvious
1783 * JOB_START) here so that those dependency jobs will be added
1785 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
1789 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1790 * it will be canceled as part of the service_stop() call that
1791 * is executed as part of JOB_RESTART. */
1793 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1797 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1798 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1801 static void service_enter_reload_by_notify(Service
*s
) {
1804 if (s
->timeout_start_usec
> 0)
1805 service_arm_timer(s
, s
->timeout_start_usec
);
1807 service_set_state(s
, SERVICE_RELOAD
);
1810 static void service_enter_reload(Service
*s
) {
1815 service_unwatch_control_pid(s
);
1817 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1818 if (s
->control_command
) {
1819 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1821 r
= service_spawn(s
,
1823 s
->timeout_start_usec
,
1825 !s
->permissions_start_only
,
1826 !s
->root_directory_start_only
,
1833 service_set_state(s
, SERVICE_RELOAD
);
1835 service_enter_running(s
, SERVICE_SUCCESS
);
1840 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1841 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1842 service_enter_running(s
, SERVICE_SUCCESS
);
1845 static void service_run_next_control(Service
*s
) {
1849 assert(s
->control_command
);
1850 assert(s
->control_command
->command_next
);
1852 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1854 s
->control_command
= s
->control_command
->command_next
;
1855 service_unwatch_control_pid(s
);
1857 r
= service_spawn(s
,
1859 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1861 !s
->permissions_start_only
,
1862 !s
->root_directory_start_only
,
1863 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1864 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1873 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1875 if (s
->state
== SERVICE_START_PRE
)
1876 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1877 else if (s
->state
== SERVICE_STOP
)
1878 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1879 else if (s
->state
== SERVICE_STOP_POST
)
1880 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1881 else if (s
->state
== SERVICE_RELOAD
) {
1882 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1883 service_enter_running(s
, SERVICE_SUCCESS
);
1885 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1888 static void service_run_next_main(Service
*s
) {
1893 assert(s
->main_command
);
1894 assert(s
->main_command
->command_next
);
1895 assert(s
->type
== SERVICE_ONESHOT
);
1897 s
->main_command
= s
->main_command
->command_next
;
1898 service_unwatch_main_pid(s
);
1900 r
= service_spawn(s
,
1902 s
->timeout_start_usec
,
1912 service_set_main_pid(s
, pid
);
1917 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1918 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1921 static int service_start_limit_test(Service
*s
) {
1924 if (ratelimit_test(&s
->start_limit
))
1927 log_unit_warning(UNIT(s
), "Start request repeated too quickly.");
1929 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
1932 static int service_start(Unit
*u
) {
1933 Service
*s
= SERVICE(u
);
1938 /* We cannot fulfill this request right now, try again later
1940 if (IN_SET(s
->state
,
1941 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1942 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1945 /* Already on it! */
1946 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1949 /* A service that will be restarted must be stopped first to
1950 * trigger BindsTo and/or OnFailure dependencies. If a user
1951 * does not want to wait for the holdoff time to elapse, the
1952 * service should be manually restarted, not started. We
1953 * simply return EAGAIN here, so that any start jobs stay
1954 * queued, and assume that the auto restart timer will
1955 * eventually trigger the restart. */
1956 if (s
->state
== SERVICE_AUTO_RESTART
)
1959 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1961 /* Make sure we don't enter a busy loop of some kind. */
1962 r
= service_start_limit_test(s
);
1964 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
1968 s
->result
= SERVICE_SUCCESS
;
1969 s
->reload_result
= SERVICE_SUCCESS
;
1970 s
->main_pid_known
= false;
1971 s
->main_pid_alien
= false;
1972 s
->forbid_restart
= false;
1973 s
->reset_cpu_usage
= true;
1975 s
->status_text
= mfree(s
->status_text
);
1976 s
->status_errno
= 0;
1978 s
->notify_state
= NOTIFY_UNKNOWN
;
1980 service_enter_start_pre(s
);
1984 static int service_stop(Unit
*u
) {
1985 Service
*s
= SERVICE(u
);
1989 /* Don't create restart jobs from manual stops. */
1990 s
->forbid_restart
= true;
1993 if (IN_SET(s
->state
,
1994 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1995 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1998 /* A restart will be scheduled or is in progress. */
1999 if (s
->state
== SERVICE_AUTO_RESTART
) {
2000 service_set_state(s
, SERVICE_DEAD
);
2004 /* If there's already something running we go directly into
2006 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2007 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2011 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2013 service_enter_stop(s
, SERVICE_SUCCESS
);
2017 static int service_reload(Unit
*u
) {
2018 Service
*s
= SERVICE(u
);
2022 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2024 service_enter_reload(s
);
2028 _pure_
static bool service_can_reload(Unit
*u
) {
2029 Service
*s
= SERVICE(u
);
2033 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2036 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2037 Service
*s
= SERVICE(u
);
2044 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2045 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2046 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2048 if (s
->control_pid
> 0)
2049 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2051 if (s
->main_pid_known
&& s
->main_pid
> 0)
2052 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2054 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2055 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2057 if (s
->status_text
) {
2058 _cleanup_free_
char *c
= NULL
;
2060 c
= cescape(s
->status_text
);
2061 unit_serialize_item(u
, f
, "status-text", strempty(c
));
2064 /* FIXME: There's a minor uncleanliness here: if there are
2065 * multiple commands attached here, we will start from the
2066 * first one again */
2067 if (s
->control_command_id
>= 0)
2068 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2070 if (s
->socket_fd
>= 0) {
2073 copy
= fdset_put_dup(fds
, s
->socket_fd
);
2077 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2080 if (s
->bus_endpoint_fd
>= 0) {
2083 copy
= fdset_put_dup(fds
, s
->bus_endpoint_fd
);
2087 unit_serialize_item_format(u
, f
, "endpoint-fd", "%i", copy
);
2090 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2091 _cleanup_free_
char *c
= NULL
;
2094 copy
= fdset_put_dup(fds
, fs
->fd
);
2098 c
= cescape(fs
->fdname
);
2100 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2103 if (s
->main_exec_status
.pid
> 0) {
2104 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2105 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2106 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2108 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2109 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2110 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2114 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2115 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2117 if (s
->forbid_restart
)
2118 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2123 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2124 Service
*s
= SERVICE(u
);
2132 if (streq(key
, "state")) {
2135 state
= service_state_from_string(value
);
2137 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2139 s
->deserialized_state
= state
;
2140 } else if (streq(key
, "result")) {
2143 f
= service_result_from_string(value
);
2145 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2146 else if (f
!= SERVICE_SUCCESS
)
2149 } else if (streq(key
, "reload-result")) {
2152 f
= service_result_from_string(value
);
2154 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2155 else if (f
!= SERVICE_SUCCESS
)
2156 s
->reload_result
= f
;
2158 } else if (streq(key
, "control-pid")) {
2161 if (parse_pid(value
, &pid
) < 0)
2162 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2164 s
->control_pid
= pid
;
2165 } else if (streq(key
, "main-pid")) {
2168 if (parse_pid(value
, &pid
) < 0)
2169 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2171 service_set_main_pid(s
, pid
);
2172 unit_watch_pid(UNIT(s
), pid
);
2174 } else if (streq(key
, "main-pid-known")) {
2177 b
= parse_boolean(value
);
2179 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2181 s
->main_pid_known
= b
;
2182 } else if (streq(key
, "bus-name-good")) {
2185 b
= parse_boolean(value
);
2187 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2189 s
->bus_name_good
= b
;
2190 } else if (streq(key
, "status-text")) {
2193 r
= cunescape(value
, 0, &t
);
2195 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2197 free(s
->status_text
);
2201 } else if (streq(key
, "control-command")) {
2202 ServiceExecCommand id
;
2204 id
= service_exec_command_from_string(value
);
2206 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2208 s
->control_command_id
= id
;
2209 s
->control_command
= s
->exec_command
[id
];
2211 } else if (streq(key
, "socket-fd")) {
2214 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2215 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2217 asynchronous_close(s
->socket_fd
);
2218 s
->socket_fd
= fdset_remove(fds
, fd
);
2220 } else if (streq(key
, "endpoint-fd")) {
2223 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2224 log_unit_debug(u
, "Failed to parse endpoint-fd value: %s", value
);
2226 safe_close(s
->bus_endpoint_fd
);
2227 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2229 } else if (streq(key
, "fd-store-fd")) {
2234 pf
= strcspn(value
, WHITESPACE
);
2235 fdv
= strndupa(value
, pf
);
2237 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2238 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2240 _cleanup_free_
char *t
= NULL
;
2244 fdn
+= strspn(fdn
, WHITESPACE
);
2245 (void) cunescape(fdn
, 0, &t
);
2247 r
= service_add_fd_store(s
, fd
, t
);
2249 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2251 fdset_remove(fds
, fd
);
2254 } else if (streq(key
, "main-exec-status-pid")) {
2257 if (parse_pid(value
, &pid
) < 0)
2258 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2260 s
->main_exec_status
.pid
= pid
;
2261 } else if (streq(key
, "main-exec-status-code")) {
2264 if (safe_atoi(value
, &i
) < 0)
2265 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2267 s
->main_exec_status
.code
= i
;
2268 } else if (streq(key
, "main-exec-status-status")) {
2271 if (safe_atoi(value
, &i
) < 0)
2272 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2274 s
->main_exec_status
.status
= i
;
2275 } else if (streq(key
, "main-exec-status-start"))
2276 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2277 else if (streq(key
, "main-exec-status-exit"))
2278 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2279 else if (streq(key
, "watchdog-timestamp"))
2280 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2281 else if (streq(key
, "forbid-restart")) {
2284 b
= parse_boolean(value
);
2286 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2288 s
->forbid_restart
= b
;
2290 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2295 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2296 const UnitActiveState
*table
;
2300 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2302 return table
[SERVICE(u
)->state
];
2305 static const char *service_sub_state_to_string(Unit
*u
) {
2308 return service_state_to_string(SERVICE(u
)->state
);
2311 static bool service_check_gc(Unit
*u
) {
2312 Service
*s
= SERVICE(u
);
2316 /* Never clean up services that still have a process around,
2317 * even if the service is formally dead. */
2318 if (cgroup_good(s
) > 0 ||
2319 main_pid_good(s
) > 0 ||
2320 control_pid_good(s
) > 0)
2326 _pure_
static bool service_check_snapshot(Unit
*u
) {
2327 Service
*s
= SERVICE(u
);
2331 return s
->socket_fd
< 0;
2334 static int service_retry_pid_file(Service
*s
) {
2337 assert(s
->pid_file
);
2338 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2340 r
= service_load_pid_file(s
, false);
2344 service_unwatch_pid_file(s
);
2346 service_enter_running(s
, SERVICE_SUCCESS
);
2350 static int service_watch_pid_file(Service
*s
) {
2353 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2355 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2359 /* the pidfile might have appeared just before we set the watch */
2360 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2361 service_retry_pid_file(s
);
2365 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2366 service_unwatch_pid_file(s
);
2370 static int service_demand_pid_file(Service
*s
) {
2373 assert(s
->pid_file
);
2374 assert(!s
->pid_file_pathspec
);
2376 ps
= new0(PathSpec
, 1);
2381 ps
->path
= strdup(s
->pid_file
);
2387 path_kill_slashes(ps
->path
);
2389 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2390 * keep their PID file open all the time. */
2391 ps
->type
= PATH_MODIFIED
;
2392 ps
->inotify_fd
= -1;
2394 s
->pid_file_pathspec
= ps
;
2396 return service_watch_pid_file(s
);
2399 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2400 PathSpec
*p
= userdata
;
2405 s
= SERVICE(p
->unit
);
2409 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2410 assert(s
->pid_file_pathspec
);
2411 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2413 log_unit_debug(UNIT(s
), "inotify event");
2415 if (path_spec_fd_event(p
, events
) < 0)
2418 if (service_retry_pid_file(s
) == 0)
2421 if (service_watch_pid_file(s
) < 0)
2427 service_unwatch_pid_file(s
);
2428 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2432 static void service_notify_cgroup_empty_event(Unit
*u
) {
2433 Service
*s
= SERVICE(u
);
2437 log_unit_debug(u
, "cgroup is empty");
2441 /* Waiting for SIGCHLD is usually more interesting,
2442 * because it includes return codes/signals. Which is
2443 * why we ignore the cgroup events for most cases,
2444 * except when we don't know pid which to expect the
2448 case SERVICE_START_POST
:
2449 /* If we were hoping for the daemon to write its PID file,
2450 * we can give up now. */
2451 if (s
->pid_file_pathspec
) {
2452 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2454 service_unwatch_pid_file(s
);
2455 if (s
->state
== SERVICE_START
)
2456 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2458 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2462 case SERVICE_RUNNING
:
2463 /* service_enter_running() will figure out what to do */
2464 service_enter_running(s
, SERVICE_SUCCESS
);
2467 case SERVICE_STOP_SIGABRT
:
2468 case SERVICE_STOP_SIGTERM
:
2469 case SERVICE_STOP_SIGKILL
:
2471 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2472 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2476 case SERVICE_STOP_POST
:
2477 case SERVICE_FINAL_SIGTERM
:
2478 case SERVICE_FINAL_SIGKILL
:
2479 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2480 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2489 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2490 Service
*s
= SERVICE(u
);
2496 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2497 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2498 f
= SERVICE_SUCCESS
;
2499 else if (code
== CLD_EXITED
)
2500 f
= SERVICE_FAILURE_EXIT_CODE
;
2501 else if (code
== CLD_KILLED
)
2502 f
= SERVICE_FAILURE_SIGNAL
;
2503 else if (code
== CLD_DUMPED
)
2504 f
= SERVICE_FAILURE_CORE_DUMP
;
2506 assert_not_reached("Unknown code");
2508 if (s
->main_pid
== pid
) {
2509 /* Forking services may occasionally move to a new PID.
2510 * As long as they update the PID file before exiting the old
2511 * PID, they're fine. */
2512 if (service_load_pid_file(s
, false) == 0)
2516 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2518 if (s
->main_command
) {
2519 /* If this is not a forking service than the
2520 * main process got started and hence we copy
2521 * the exit status so that it is recorded both
2522 * as main and as control process exit
2525 s
->main_command
->exec_status
= s
->main_exec_status
;
2527 if (s
->main_command
->ignore
)
2528 f
= SERVICE_SUCCESS
;
2529 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2531 /* If this is a forked process, then we should
2532 * ignore the return value if this was
2533 * configured for the starter process */
2535 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2536 f
= SERVICE_SUCCESS
;
2539 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2541 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2542 sigchld_code_to_string(code
), status
,
2543 strna(code
== CLD_EXITED
2544 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2545 : signal_to_string(status
))),
2546 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2547 "EXIT_STATUS=%i", status
,
2550 if (f
!= SERVICE_SUCCESS
)
2553 if (s
->main_command
&&
2554 s
->main_command
->command_next
&&
2555 f
== SERVICE_SUCCESS
) {
2557 /* There is another command to *
2558 * execute, so let's do that. */
2560 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2561 service_run_next_main(s
);
2565 /* The service exited, so the service is officially
2567 s
->main_command
= NULL
;
2571 case SERVICE_START_POST
:
2572 case SERVICE_RELOAD
:
2574 /* Need to wait until the operation is
2579 if (s
->type
== SERVICE_ONESHOT
) {
2580 /* This was our main goal, so let's go on */
2581 if (f
== SERVICE_SUCCESS
)
2582 service_enter_start_post(s
);
2584 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2590 case SERVICE_RUNNING
:
2591 service_enter_running(s
, f
);
2594 case SERVICE_STOP_SIGABRT
:
2595 case SERVICE_STOP_SIGTERM
:
2596 case SERVICE_STOP_SIGKILL
:
2598 if (!control_pid_good(s
))
2599 service_enter_stop_post(s
, f
);
2601 /* If there is still a control process, wait for that first */
2604 case SERVICE_STOP_POST
:
2605 case SERVICE_FINAL_SIGTERM
:
2606 case SERVICE_FINAL_SIGKILL
:
2608 if (!control_pid_good(s
))
2609 service_enter_dead(s
, f
, true);
2613 assert_not_reached("Uh, main process died at wrong time.");
2617 } else if (s
->control_pid
== pid
) {
2620 if (s
->control_command
) {
2621 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2623 if (s
->control_command
->ignore
)
2624 f
= SERVICE_SUCCESS
;
2627 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2628 "Control process exited, code=%s status=%i",
2629 sigchld_code_to_string(code
), status
);
2631 if (f
!= SERVICE_SUCCESS
)
2634 /* Immediately get rid of the cgroup, so that the
2635 * kernel doesn't delay the cgroup empty messages for
2636 * the service cgroup any longer than necessary */
2637 service_kill_control_processes(s
);
2639 if (s
->control_command
&&
2640 s
->control_command
->command_next
&&
2641 f
== SERVICE_SUCCESS
) {
2643 /* There is another command to *
2644 * execute, so let's do that. */
2646 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2647 service_run_next_control(s
);
2650 /* No further commands for this step, so let's
2651 * figure out what to do next */
2653 s
->control_command
= NULL
;
2654 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2656 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2660 case SERVICE_START_PRE
:
2661 if (f
== SERVICE_SUCCESS
)
2662 service_enter_start(s
);
2664 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2668 if (s
->type
!= SERVICE_FORKING
)
2669 /* Maybe spurious event due to a reload that changed the type? */
2672 if (f
!= SERVICE_SUCCESS
) {
2673 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2678 bool has_start_post
;
2681 /* Let's try to load the pid file here if we can.
2682 * The PID file might actually be created by a START_POST
2683 * script. In that case don't worry if the loading fails. */
2685 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2686 r
= service_load_pid_file(s
, !has_start_post
);
2687 if (!has_start_post
&& r
< 0) {
2688 r
= service_demand_pid_file(s
);
2689 if (r
< 0 || !cgroup_good(s
))
2690 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2694 (void) service_search_main_pid(s
);
2696 service_enter_start_post(s
);
2699 case SERVICE_START_POST
:
2700 if (f
!= SERVICE_SUCCESS
) {
2701 service_enter_stop(s
, f
);
2708 r
= service_load_pid_file(s
, true);
2710 r
= service_demand_pid_file(s
);
2711 if (r
< 0 || !cgroup_good(s
))
2712 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2716 (void) service_search_main_pid(s
);
2718 service_enter_running(s
, SERVICE_SUCCESS
);
2721 case SERVICE_RELOAD
:
2722 if (f
== SERVICE_SUCCESS
) {
2723 service_load_pid_file(s
, true);
2724 (void) service_search_main_pid(s
);
2727 s
->reload_result
= f
;
2728 service_enter_running(s
, SERVICE_SUCCESS
);
2732 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2735 case SERVICE_STOP_SIGABRT
:
2736 case SERVICE_STOP_SIGTERM
:
2737 case SERVICE_STOP_SIGKILL
:
2738 if (main_pid_good(s
) <= 0)
2739 service_enter_stop_post(s
, f
);
2741 /* If there is still a service
2742 * process around, wait until
2743 * that one quit, too */
2746 case SERVICE_STOP_POST
:
2747 case SERVICE_FINAL_SIGTERM
:
2748 case SERVICE_FINAL_SIGKILL
:
2749 if (main_pid_good(s
) <= 0)
2750 service_enter_dead(s
, f
, true);
2754 assert_not_reached("Uh, control process died at wrong time.");
2759 /* Notify clients about changed exit status */
2760 unit_add_to_dbus_queue(u
);
2762 /* We got one SIGCHLD for the service, let's watch all
2763 * processes that are now running of the service, and watch
2764 * that. Among the PIDs we then watch will be children
2765 * reassigned to us, which hopefully allows us to identify
2766 * when all children are gone */
2767 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2768 unit_watch_all_pids(u
);
2770 /* If the PID set is empty now, then let's finish this off */
2771 if (set_isempty(u
->pids
))
2772 service_notify_cgroup_empty_event(u
);
2775 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2776 Service
*s
= SERVICE(userdata
);
2779 assert(source
== s
->timer_event_source
);
2783 case SERVICE_START_PRE
:
2785 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2786 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2789 case SERVICE_START_POST
:
2790 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2791 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2794 case SERVICE_RELOAD
:
2795 log_unit_warning(UNIT(s
), "Reload operation timed out. Stopping.");
2796 service_unwatch_control_pid(s
);
2797 service_kill_control_processes(s
);
2798 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2799 service_enter_running(s
, SERVICE_SUCCESS
);
2803 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2804 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2807 case SERVICE_STOP_SIGABRT
:
2808 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2809 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2812 case SERVICE_STOP_SIGTERM
:
2813 if (s
->kill_context
.send_sigkill
) {
2814 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2815 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2817 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2818 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2823 case SERVICE_STOP_SIGKILL
:
2824 /* Uh, we sent a SIGKILL and it is still not gone?
2825 * Must be something we cannot kill, so let's just be
2826 * weirded out and continue */
2828 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2829 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2832 case SERVICE_STOP_POST
:
2833 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2834 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2837 case SERVICE_FINAL_SIGTERM
:
2838 if (s
->kill_context
.send_sigkill
) {
2839 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2840 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2842 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2843 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2848 case SERVICE_FINAL_SIGKILL
:
2849 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2850 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2853 case SERVICE_AUTO_RESTART
:
2854 log_unit_info(UNIT(s
),
2855 s
->restart_usec
> 0 ?
2856 "Service hold-off time over, scheduling restart." :
2857 "Service has no hold-off time, scheduling restart.");
2858 service_enter_restart(s
);
2862 assert_not_reached("Timeout at wrong time.");
2868 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2869 Service
*s
= SERVICE(userdata
);
2870 char t
[FORMAT_TIMESPAN_MAX
];
2873 assert(source
== s
->watchdog_event_source
);
2875 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2876 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2878 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2883 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2884 Service
*s
= SERVICE(u
);
2885 _cleanup_free_
char *cc
= NULL
;
2886 bool notify_dbus
= false;
2891 cc
= strv_join(tags
, ", ");
2893 if (s
->notify_access
== NOTIFY_NONE
) {
2894 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2896 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2897 if (s
->main_pid
!= 0)
2898 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2900 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2903 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2905 /* Interpret MAINPID= */
2906 e
= strv_find_startswith(tags
, "MAINPID=");
2907 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2908 if (parse_pid(e
, &pid
) < 0)
2909 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2911 service_set_main_pid(s
, pid
);
2912 unit_watch_pid(UNIT(s
), pid
);
2917 /* Interpret RELOADING= */
2918 if (strv_find(tags
, "RELOADING=1")) {
2920 s
->notify_state
= NOTIFY_RELOADING
;
2922 if (s
->state
== SERVICE_RUNNING
)
2923 service_enter_reload_by_notify(s
);
2928 /* Interpret READY= */
2929 if (strv_find(tags
, "READY=1")) {
2931 s
->notify_state
= NOTIFY_READY
;
2933 /* Type=notify services inform us about completed
2934 * initialization with READY=1 */
2935 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2936 service_enter_start_post(s
);
2938 /* Sending READY=1 while we are reloading informs us
2939 * that the reloading is complete */
2940 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2941 service_enter_running(s
, SERVICE_SUCCESS
);
2946 /* Interpret STOPPING= */
2947 if (strv_find(tags
, "STOPPING=1")) {
2949 s
->notify_state
= NOTIFY_STOPPING
;
2951 if (s
->state
== SERVICE_RUNNING
)
2952 service_enter_stop_by_notify(s
);
2957 /* Interpret STATUS= */
2958 e
= strv_find_startswith(tags
, "STATUS=");
2960 _cleanup_free_
char *t
= NULL
;
2963 if (!utf8_is_valid(e
))
2964 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
2972 if (!streq_ptr(s
->status_text
, t
)) {
2974 free(s
->status_text
);
2982 /* Interpret ERRNO= */
2983 e
= strv_find_startswith(tags
, "ERRNO=");
2987 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
2988 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
2990 if (s
->status_errno
!= status_errno
) {
2991 s
->status_errno
= status_errno
;
2997 /* Interpret WATCHDOG= */
2998 if (strv_find(tags
, "WATCHDOG=1"))
2999 service_reset_watchdog(s
);
3001 if (strv_find(tags
, "FDSTORE=1")) {
3004 name
= strv_find_startswith(tags
, "FDNAME=");
3005 if (name
&& !fdname_is_valid(name
)) {
3006 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3010 service_add_fd_store_set(s
, fds
, name
);
3013 /* Notify clients about changed status or main pid */
3015 unit_add_to_dbus_queue(u
);
3018 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
3019 Service
*s
= SERVICE(u
);
3022 if (!s
->timer_event_source
)
3025 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
3032 static void service_bus_name_owner_change(
3035 const char *old_owner
,
3036 const char *new_owner
) {
3038 Service
*s
= SERVICE(u
);
3044 assert(streq(s
->bus_name
, name
));
3045 assert(old_owner
|| new_owner
);
3047 if (old_owner
&& new_owner
)
3048 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3050 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3052 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3054 s
->bus_name_good
= !!new_owner
;
3056 if (s
->type
== SERVICE_DBUS
) {
3058 /* service_enter_running() will figure out what to
3060 if (s
->state
== SERVICE_RUNNING
)
3061 service_enter_running(s
, SERVICE_SUCCESS
);
3062 else if (s
->state
== SERVICE_START
&& new_owner
)
3063 service_enter_start_post(s
);
3065 } else if (new_owner
&&
3067 (s
->state
== SERVICE_START
||
3068 s
->state
== SERVICE_START_POST
||
3069 s
->state
== SERVICE_RUNNING
||
3070 s
->state
== SERVICE_RELOAD
)) {
3072 _cleanup_bus_creds_unref_ sd_bus_creds
*creds
= NULL
;
3075 /* Try to acquire PID from bus service */
3077 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3079 r
= sd_bus_creds_get_pid(creds
, &pid
);
3081 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3083 service_set_main_pid(s
, pid
);
3084 unit_watch_pid(UNIT(s
), pid
);
3089 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3090 _cleanup_free_
char *peer
= NULL
;
3096 /* This is called by the socket code when instantiating a new
3097 * service for a stream socket and the socket needs to be
3100 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3103 if (s
->socket_fd
>= 0)
3106 if (s
->state
!= SERVICE_DEAD
)
3109 if (getpeername_pretty(fd
, &peer
) >= 0) {
3111 if (UNIT(s
)->description
) {
3112 _cleanup_free_
char *a
;
3114 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3118 r
= unit_set_description(UNIT(s
), a
);
3120 r
= unit_set_description(UNIT(s
), peer
);
3127 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3129 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3131 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3134 static void service_reset_failed(Unit
*u
) {
3135 Service
*s
= SERVICE(u
);
3139 if (s
->state
== SERVICE_FAILED
)
3140 service_set_state(s
, SERVICE_DEAD
);
3142 s
->result
= SERVICE_SUCCESS
;
3143 s
->reload_result
= SERVICE_SUCCESS
;
3145 RATELIMIT_RESET(s
->start_limit
);
3148 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3149 Service
*s
= SERVICE(u
);
3151 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3154 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3155 [SERVICE_RESTART_NO
] = "no",
3156 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3157 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3158 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3159 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3160 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3161 [SERVICE_RESTART_ALWAYS
] = "always",
3164 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3166 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3167 [SERVICE_SIMPLE
] = "simple",
3168 [SERVICE_FORKING
] = "forking",
3169 [SERVICE_ONESHOT
] = "oneshot",
3170 [SERVICE_DBUS
] = "dbus",
3171 [SERVICE_NOTIFY
] = "notify",
3172 [SERVICE_IDLE
] = "idle"
3175 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3177 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3178 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3179 [SERVICE_EXEC_START
] = "ExecStart",
3180 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3181 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3182 [SERVICE_EXEC_STOP
] = "ExecStop",
3183 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3186 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3188 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3189 [NOTIFY_NONE
] = "none",
3190 [NOTIFY_MAIN
] = "main",
3191 [NOTIFY_ALL
] = "all"
3194 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3196 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3197 [NOTIFY_UNKNOWN
] = "unknown",
3198 [NOTIFY_READY
] = "ready",
3199 [NOTIFY_RELOADING
] = "reloading",
3200 [NOTIFY_STOPPING
] = "stopping",
3203 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3205 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3206 [SERVICE_SUCCESS
] = "success",
3207 [SERVICE_FAILURE_RESOURCES
] = "resources",
3208 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3209 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3210 [SERVICE_FAILURE_SIGNAL
] = "signal",
3211 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3212 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3213 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3216 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3218 const UnitVTable service_vtable
= {
3219 .object_size
= sizeof(Service
),
3220 .exec_context_offset
= offsetof(Service
, exec_context
),
3221 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3222 .kill_context_offset
= offsetof(Service
, kill_context
),
3223 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3229 .private_section
= "Service",
3231 .init
= service_init
,
3232 .done
= service_done
,
3233 .load
= service_load
,
3234 .release_resources
= service_release_resources
,
3236 .coldplug
= service_coldplug
,
3238 .dump
= service_dump
,
3240 .start
= service_start
,
3241 .stop
= service_stop
,
3242 .reload
= service_reload
,
3244 .can_reload
= service_can_reload
,
3246 .kill
= service_kill
,
3248 .serialize
= service_serialize
,
3249 .deserialize_item
= service_deserialize_item
,
3251 .active_state
= service_active_state
,
3252 .sub_state_to_string
= service_sub_state_to_string
,
3254 .check_gc
= service_check_gc
,
3255 .check_snapshot
= service_check_snapshot
,
3257 .sigchld_event
= service_sigchld_event
,
3259 .reset_failed
= service_reset_failed
,
3261 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3262 .notify_message
= service_notify_message
,
3264 .bus_name_owner_change
= service_bus_name_owner_change
,
3266 .bus_vtable
= bus_service_vtable
,
3267 .bus_set_property
= bus_service_set_property
,
3268 .bus_commit_properties
= bus_service_commit_properties
,
3270 .get_timeout
= service_get_timeout
,
3271 .can_transient
= true,
3273 .status_message_formats
= {
3274 .starting_stopping
= {
3275 [0] = "Starting %s...",
3276 [1] = "Stopping %s...",
3278 .finished_start_job
= {
3279 [JOB_DONE
] = "Started %s.",
3280 [JOB_FAILED
] = "Failed to start %s.",
3282 .finished_stop_job
= {
3283 [JOB_DONE
] = "Stopped %s.",
3284 [JOB_FAILED
] = "Stopped (with error) %s.",