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/>.
31 #include "load-fragment.h"
32 #include "load-dropin.h"
35 #include "unit-name.h"
36 #include "unit-printf.h"
37 #include "dbus-service.h"
39 #include "exit-status.h"
41 #include "path-util.h"
46 #include "bus-error.h"
48 #include "bus-kernel.h"
50 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
51 [SERVICE_DEAD
] = UNIT_INACTIVE
,
52 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
53 [SERVICE_START
] = UNIT_ACTIVATING
,
54 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
55 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
56 [SERVICE_EXITED
] = UNIT_ACTIVE
,
57 [SERVICE_RELOAD
] = UNIT_RELOADING
,
58 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
59 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
60 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
61 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
62 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
63 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
64 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
65 [SERVICE_FAILED
] = UNIT_FAILED
,
66 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
69 /* For Type=idle we never want to delay any other jobs, hence we
70 * consider idle jobs active as soon as we start working on them */
71 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
72 [SERVICE_DEAD
] = UNIT_INACTIVE
,
73 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
74 [SERVICE_START
] = UNIT_ACTIVE
,
75 [SERVICE_START_POST
] = UNIT_ACTIVE
,
76 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
77 [SERVICE_EXITED
] = UNIT_ACTIVE
,
78 [SERVICE_RELOAD
] = UNIT_RELOADING
,
79 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
80 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
81 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
82 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
83 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
84 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
85 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
86 [SERVICE_FAILED
] = UNIT_FAILED
,
87 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
90 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
91 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
92 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
94 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
95 static void service_enter_reload_by_notify(Service
*s
);
97 static void service_init(Unit
*u
) {
98 Service
*s
= SERVICE(u
);
101 assert(u
->load_state
== UNIT_STUB
);
103 s
->timeout_start_usec
= u
->manager
->default_timeout_start_usec
;
104 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
105 s
->restart_usec
= u
->manager
->default_restart_usec
;
106 s
->type
= _SERVICE_TYPE_INVALID
;
108 s
->bus_endpoint_fd
= -1;
109 s
->guess_main_pid
= true;
111 RATELIMIT_INIT(s
->start_limit
, u
->manager
->default_start_limit_interval
, u
->manager
->default_start_limit_burst
);
113 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
116 static void service_unwatch_control_pid(Service
*s
) {
119 if (s
->control_pid
<= 0)
122 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
126 static void service_unwatch_main_pid(Service
*s
) {
129 if (s
->main_pid
<= 0)
132 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
136 static void service_unwatch_pid_file(Service
*s
) {
137 if (!s
->pid_file_pathspec
)
140 log_unit_debug(UNIT(s
)->id
, "Stopping watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
141 path_spec_unwatch(s
->pid_file_pathspec
);
142 path_spec_done(s
->pid_file_pathspec
);
143 free(s
->pid_file_pathspec
);
144 s
->pid_file_pathspec
= NULL
;
147 static int service_set_main_pid(Service
*s
, pid_t pid
) {
158 if (s
->main_pid
== pid
&& s
->main_pid_known
)
161 if (s
->main_pid
!= pid
) {
162 service_unwatch_main_pid(s
);
163 exec_status_start(&s
->main_exec_status
, pid
);
167 s
->main_pid_known
= true;
169 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
170 log_unit_warning(UNIT(s
)->id
, "%s: Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", UNIT(s
)->id
, pid
);
171 s
->main_pid_alien
= true;
173 s
->main_pid_alien
= false;
178 static void service_close_socket_fd(Service
*s
) {
181 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
184 static void service_connection_unref(Service
*s
) {
187 if (!UNIT_ISSET(s
->accept_socket
))
190 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
191 unit_ref_unset(&s
->accept_socket
);
194 static void service_stop_watchdog(Service
*s
) {
197 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
198 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
201 static void service_start_watchdog(Service
*s
) {
206 if (s
->watchdog_usec
<= 0)
209 if (s
->watchdog_event_source
) {
210 r
= sd_event_source_set_time(s
->watchdog_event_source
, s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
);
212 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to reset watchdog timer: %m", UNIT(s
)->id
);
216 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
218 r
= sd_event_add_time(
219 UNIT(s
)->manager
->event
,
220 &s
->watchdog_event_source
,
222 s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
, 0,
223 service_dispatch_watchdog
, s
);
225 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to add watchdog timer: %m", UNIT(s
)->id
);
229 /* Let's process everything else which might be a sign
230 * of living before we consider a service died. */
231 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
235 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to install watchdog timer: %m", UNIT(s
)->id
);
238 static void service_reset_watchdog(Service
*s
) {
241 dual_timestamp_get(&s
->watchdog_timestamp
);
242 service_start_watchdog(s
);
245 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
251 assert(fs
->service
->n_fd_store
> 0);
252 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
253 fs
->service
->n_fd_store
--;
256 if (fs
->event_source
) {
257 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
258 sd_event_source_unref(fs
->event_source
);
265 static void service_release_resources(Unit
*u
) {
266 Service
*s
= SERVICE(u
);
273 log_debug("Releasing all resources for %s", u
->id
);
276 service_fd_store_unlink(s
->fd_store
);
278 assert(s
->n_fd_store
== 0);
281 static void service_done(Unit
*u
) {
282 Service
*s
= SERVICE(u
);
289 free(s
->status_text
);
290 s
->status_text
= NULL
;
293 s
->reboot_arg
= NULL
;
295 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
296 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
297 s
->control_command
= NULL
;
298 s
->main_command
= NULL
;
300 exit_status_set_free(&s
->restart_prevent_status
);
301 exit_status_set_free(&s
->restart_force_status
);
302 exit_status_set_free(&s
->success_status
);
304 /* This will leak a process, but at least no memory or any of
306 service_unwatch_main_pid(s
);
307 service_unwatch_control_pid(s
);
308 service_unwatch_pid_file(s
);
311 unit_unwatch_bus_name(u
, s
->bus_name
);
316 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
317 service_close_socket_fd(s
);
318 service_connection_unref(s
);
320 unit_ref_unset(&s
->accept_socket
);
322 service_stop_watchdog(s
);
324 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
326 service_release_resources(u
);
329 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
330 ServiceFDStore
*fs
= userdata
;
335 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
336 service_fd_store_unlink(fs
);
340 static int service_add_fd_store(Service
*s
, int fd
) {
347 if (s
->n_fd_store
>= s
->n_fd_store_max
)
350 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
351 r
= same_fd(fs
->fd
, fd
);
355 /* Already included */
361 fs
= new0(ServiceFDStore
, 1);
368 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
374 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
380 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
) {
385 if (fdset_size(fds
) <= 0)
388 while (s
->n_fd_store
< s
->n_fd_store_max
) {
389 _cleanup_close_
int fd
= -1;
391 fd
= fdset_steal_first(fds
);
395 r
= service_add_fd_store(s
, fd
);
397 return log_unit_error_errno(UNIT(s
)->id
, r
, "%s: Couldn't add fd to fd store: %m", UNIT(s
)->id
);
400 log_unit_debug(UNIT(s
)->id
, "%s: added fd to fd store.", UNIT(s
)->id
);
405 if (fdset_size(fds
) > 0)
406 log_unit_warning(UNIT(s
)->id
, "%s: tried to store more fds than FDStoreMax=%u allows, closing remaining.", UNIT(s
)->id
, s
->n_fd_store_max
);
411 static int service_arm_timer(Service
*s
, usec_t usec
) {
416 if (s
->timer_event_source
) {
417 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
421 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
424 return sd_event_add_time(
425 UNIT(s
)->manager
->event
,
426 &s
->timer_event_source
,
428 now(CLOCK_MONOTONIC
) + usec
, 0,
429 service_dispatch_timer
, s
);
432 static int service_verify(Service
*s
) {
435 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
438 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
439 log_unit_error(UNIT(s
)->id
, "%s lacks both ExecStart= and ExecStop= setting. Refusing.", UNIT(s
)->id
);
443 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
444 log_unit_error(UNIT(s
)->id
, "%s has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
448 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
449 log_unit_error(UNIT(s
)->id
, "%s has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.", UNIT(s
)->id
);
453 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
454 log_unit_error(UNIT(s
)->id
, "%s has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
458 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
459 log_unit_error(UNIT(s
)->id
, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
463 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
464 log_unit_error(UNIT(s
)->id
, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
468 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
469 log_unit_error(UNIT(s
)->id
, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->id
);
473 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
474 log_unit_warning(UNIT(s
)->id
, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s
)->id
);
476 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
477 log_unit_error(UNIT(s
)->id
, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s
)->id
);
484 static int service_add_default_dependencies(Service
*s
) {
489 /* Add a number of automatic dependencies useful for the
490 * majority of services. */
492 /* First, pull in base system */
493 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
497 /* Second, activate normal shutdown */
498 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
502 static void service_fix_output(Service
*s
) {
505 /* If nothing has been explicitly configured, patch default
506 * output in. If input is socket/tty we avoid this however,
507 * since in that case we want output to default to the same
508 * place as we read input from. */
510 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
511 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
512 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
513 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
515 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
516 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
517 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
520 static int service_load(Unit
*u
) {
521 Service
*s
= SERVICE(u
);
526 /* Load a .service file */
527 r
= unit_load_fragment(u
);
531 /* Still nothing found? Then let's give up */
532 if (u
->load_state
== UNIT_STUB
)
535 /* This is a new unit? Then let's add in some extras */
536 if (u
->load_state
== UNIT_LOADED
) {
538 /* We were able to load something, then let's add in
539 * the dropin directories. */
540 r
= unit_load_dropin(u
);
544 if (s
->type
== _SERVICE_TYPE_INVALID
) {
545 /* Figure out a type automatically */
547 s
->type
= SERVICE_DBUS
;
548 else if (s
->exec_command
[SERVICE_EXEC_START
])
549 s
->type
= SERVICE_SIMPLE
;
551 s
->type
= SERVICE_ONESHOT
;
554 /* Oneshot services have disabled start timeout by default */
555 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
556 s
->timeout_start_usec
= 0;
558 service_fix_output(s
);
560 r
= unit_patch_contexts(u
);
564 r
= unit_add_exec_dependencies(u
, &s
->exec_context
);
568 r
= unit_add_default_slice(u
, &s
->cgroup_context
);
572 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
573 s
->notify_access
= NOTIFY_MAIN
;
575 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
576 s
->notify_access
= NOTIFY_MAIN
;
579 r
= unit_watch_bus_name(u
, s
->bus_name
);
584 if (u
->default_dependencies
) {
585 r
= service_add_default_dependencies(s
);
592 return service_verify(s
);
595 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
596 ServiceExecCommand c
;
597 Service
*s
= SERVICE(u
);
602 prefix
= strempty(prefix
);
603 prefix2
= strappenda(prefix
, "\t");
606 "%sService State: %s\n"
608 "%sReload Result: %s\n"
609 "%sPermissionsStartOnly: %s\n"
610 "%sRootDirectoryStartOnly: %s\n"
611 "%sRemainAfterExit: %s\n"
612 "%sGuessMainPID: %s\n"
615 "%sNotifyAccess: %s\n"
616 "%sNotifyState: %s\n",
617 prefix
, service_state_to_string(s
->state
),
618 prefix
, service_result_to_string(s
->result
),
619 prefix
, service_result_to_string(s
->reload_result
),
620 prefix
, yes_no(s
->permissions_start_only
),
621 prefix
, yes_no(s
->root_directory_start_only
),
622 prefix
, yes_no(s
->remain_after_exit
),
623 prefix
, yes_no(s
->guess_main_pid
),
624 prefix
, service_type_to_string(s
->type
),
625 prefix
, service_restart_to_string(s
->restart
),
626 prefix
, notify_access_to_string(s
->notify_access
),
627 prefix
, notify_state_to_string(s
->notify_state
));
629 if (s
->control_pid
> 0)
631 "%sControl PID: "PID_FMT
"\n",
632 prefix
, s
->control_pid
);
636 "%sMain PID: "PID_FMT
"\n"
637 "%sMain PID Known: %s\n"
638 "%sMain PID Alien: %s\n",
640 prefix
, yes_no(s
->main_pid_known
),
641 prefix
, yes_no(s
->main_pid_alien
));
646 prefix
, s
->pid_file
);
651 "%sBus Name Good: %s\n",
653 prefix
, yes_no(s
->bus_name_good
));
655 kill_context_dump(&s
->kill_context
, f
, prefix
);
656 exec_context_dump(&s
->exec_context
, f
, prefix
);
658 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
660 if (!s
->exec_command
[c
])
663 fprintf(f
, "%s-> %s:\n",
664 prefix
, service_exec_command_to_string(c
));
666 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
670 fprintf(f
, "%sStatus Text: %s\n",
671 prefix
, s
->status_text
);
673 if (s
->n_fd_store_max
> 0) {
675 "%sFile Descriptor Store Max: %u\n"
676 "%sFile Descriptor Store Current: %u\n",
677 prefix
, s
->n_fd_store_max
,
678 prefix
, s
->n_fd_store
);
682 static int service_load_pid_file(Service
*s
, bool may_warn
) {
683 _cleanup_free_
char *k
= NULL
;
692 r
= read_one_line_file(s
->pid_file
, &k
);
695 log_unit_info(UNIT(s
)->id
, "PID file %s not readable (yet?) after %s.", s
->pid_file
, service_state_to_string(s
->state
));
699 r
= parse_pid(k
, &pid
);
702 log_unit_info_errno(UNIT(s
)->id
, r
, "Failed to read PID from file %s: %m", s
->pid_file
);
706 if (!pid_is_alive(pid
)) {
708 log_unit_info(UNIT(s
)->id
, "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
712 if (s
->main_pid_known
) {
713 if (pid
== s
->main_pid
)
716 log_unit_debug(UNIT(s
)->id
, "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
718 service_unwatch_main_pid(s
);
719 s
->main_pid_known
= false;
721 log_unit_debug(UNIT(s
)->id
, "Main PID loaded: "PID_FMT
, pid
);
723 r
= service_set_main_pid(s
, pid
);
727 r
= unit_watch_pid(UNIT(s
), pid
);
729 /* FIXME: we need to do something here */
730 log_unit_warning(UNIT(s
)->id
, "Failed to watch PID "PID_FMT
" from service %s", pid
, UNIT(s
)->id
);
737 static int service_search_main_pid(Service
*s
) {
743 /* If we know it anyway, don't ever fallback to unreliable
745 if (s
->main_pid_known
)
748 if (!s
->guess_main_pid
)
751 assert(s
->main_pid
<= 0);
753 pid
= unit_search_main_pid(UNIT(s
));
757 log_unit_debug(UNIT(s
)->id
, "Main PID guessed: "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(UNIT(s
)->id
, "Failed to watch PID "PID_FMT
" from service %s", pid
, UNIT(s
)->id
);
772 static void service_set_state(Service
*s
, ServiceState state
) {
773 ServiceState old_state
;
774 const UnitActiveState
*table
;
778 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
780 old_state
= s
->state
;
783 service_unwatch_pid_file(s
);
786 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
788 SERVICE_STOP
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
,
789 SERVICE_STOP_SIGABRT
, SERVICE_STOP_POST
,
790 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
791 SERVICE_AUTO_RESTART
))
792 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
795 SERVICE_START
, SERVICE_START_POST
,
796 SERVICE_RUNNING
, SERVICE_RELOAD
,
797 SERVICE_STOP
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
,
798 SERVICE_STOP_SIGABRT
, SERVICE_STOP_POST
,
799 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
800 service_unwatch_main_pid(s
);
801 s
->main_command
= NULL
;
805 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
807 SERVICE_STOP
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
,
808 SERVICE_STOP_SIGABRT
, SERVICE_STOP_POST
,
809 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
810 service_unwatch_control_pid(s
);
811 s
->control_command
= NULL
;
812 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
815 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
816 unit_unwatch_all_pids(UNIT(s
));
819 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
820 SERVICE_RUNNING
, SERVICE_RELOAD
,
821 SERVICE_STOP
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
822 SERVICE_STOP_SIGABRT
, SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
823 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
824 service_close_socket_fd(s
);
825 service_connection_unref(s
);
828 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
829 service_stop_watchdog(s
);
831 /* For the inactive states unit_notify() will trim the cgroup,
832 * but for exit we have to do that ourselves... */
833 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
834 unit_destroy_cgroup_if_empty(UNIT(s
));
836 /* For remain_after_exit services, let's see if we can "release" the
837 * hold on the console, since unit_notify() only does that in case of
839 if (state
== SERVICE_EXITED
&&
840 s
->remain_after_exit
&&
841 UNIT(s
)->manager
->n_on_console
> 0) {
845 ec
= unit_get_exec_context(UNIT(s
));
846 if (ec
&& exec_context_may_touch_console(ec
)) {
847 Manager
*m
= UNIT(s
)->manager
;
850 if (m
->n_on_console
== 0)
851 /* unset no_console_output flag, since the console is free */
852 m
->no_console_output
= false;
856 if (old_state
!= state
)
857 log_unit_debug(UNIT(s
)->id
, "%s changed %s -> %s", UNIT(s
)->id
, service_state_to_string(old_state
), service_state_to_string(state
));
859 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
860 s
->reload_result
= SERVICE_SUCCESS
;
863 static int service_coldplug(Unit
*u
) {
864 Service
*s
= SERVICE(u
);
868 assert(s
->state
== SERVICE_DEAD
);
870 if (s
->deserialized_state
!= s
->state
) {
872 if (IN_SET(s
->deserialized_state
,
873 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
875 SERVICE_STOP
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
,
876 SERVICE_STOP_SIGABRT
, SERVICE_STOP_POST
,
877 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
881 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
883 /* For the start/stop timeouts 0 means off */
885 r
= service_arm_timer(s
, k
);
891 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
893 /* The restart timeouts 0 means immediately */
894 r
= service_arm_timer(s
, s
->restart_usec
);
899 if (pid_is_unwaited(s
->main_pid
) &&
900 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
901 IN_SET(s
->deserialized_state
,
902 SERVICE_START
, SERVICE_START_POST
,
903 SERVICE_RUNNING
, SERVICE_RELOAD
,
904 SERVICE_STOP
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
,
905 SERVICE_STOP_SIGABRT
, SERVICE_STOP_POST
,
906 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
907 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
912 if (pid_is_unwaited(s
->control_pid
) &&
913 IN_SET(s
->deserialized_state
,
914 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
916 SERVICE_STOP
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
,
917 SERVICE_STOP_SIGABRT
, SERVICE_STOP_POST
,
918 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
919 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
924 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
925 unit_watch_all_pids(UNIT(s
));
927 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
928 service_start_watchdog(s
);
930 service_set_state(s
, s
->deserialized_state
);
936 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
937 _cleanup_free_
int *rfds
= NULL
;
947 if (s
->socket_fd
>= 0)
950 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
955 if (u
->type
!= UNIT_SOCKET
)
960 r
= socket_collect_fds(sock
, &cfds
, &cn_fds
);
975 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
981 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
990 if (s
->n_fd_store
> 0) {
994 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
999 LIST_FOREACH(fd_store
, fs
, s
->fd_store
)
1000 rfds
[rn_fds
++] = fs
->fd
;
1010 static int service_spawn(
1015 bool apply_permissions
,
1017 bool apply_tty_stdin
,
1024 _cleanup_free_
int *fdsbuf
= NULL
;
1025 unsigned n_fds
= 0, n_env
= 0;
1026 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1027 _cleanup_strv_free_
char
1028 **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1030 ExecParameters exec_params
= {
1031 .apply_permissions
= apply_permissions
,
1032 .apply_chroot
= apply_chroot
,
1033 .apply_tty_stdin
= apply_tty_stdin
,
1034 .bus_endpoint_fd
= -1,
1035 .selinux_context_net
= s
->socket_fd_selinux_context_net
1042 unit_realize_cgroup(UNIT(s
));
1044 r
= unit_setup_exec_runtime(UNIT(s
));
1049 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1050 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1051 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1053 if (s
->socket_fd
>= 0) {
1054 fds
= &s
->socket_fd
;
1057 r
= service_collect_fds(s
, &fdsbuf
, &n_fds
);
1066 r
= service_arm_timer(s
, timeout
);
1070 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1072 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1076 our_env
= new0(char*, 4);
1082 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1083 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1088 if (s
->main_pid
> 0)
1089 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1094 if (UNIT(s
)->manager
->running_as
!= SYSTEMD_SYSTEM
)
1095 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1100 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1106 if (is_control
&& UNIT(s
)->cgroup_path
) {
1107 path
= strappenda(UNIT(s
)->cgroup_path
, "/control");
1108 cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1110 path
= UNIT(s
)->cgroup_path
;
1113 if (s
->exec_context
.bus_endpoint
) {
1114 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== SYSTEMD_SYSTEM
? "system" : "user",
1115 UNIT(s
)->id
, &bus_endpoint_path
);
1119 /* Pass the fd to the exec_params so that the child process can upload the policy.
1120 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1121 * as the service is running. */
1122 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1126 exec_params
.argv
= argv
;
1127 exec_params
.fds
= fds
;
1128 exec_params
.n_fds
= n_fds
;
1129 exec_params
.environment
= final_env
;
1130 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1131 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1132 exec_params
.cgroup_path
= path
;
1133 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1134 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1135 exec_params
.unit_id
= UNIT(s
)->id
;
1136 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1137 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1138 if (s
->type
== SERVICE_IDLE
)
1139 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1149 r
= unit_watch_pid(UNIT(s
), pid
);
1151 /* FIXME: we need to do something here */
1160 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1165 static int main_pid_good(Service
*s
) {
1168 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1171 /* If we know the pid file, then lets just check if it is
1173 if (s
->main_pid_known
) {
1175 /* If it's an alien child let's check if it is still
1177 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1178 return pid_is_alive(s
->main_pid
);
1180 /* .. otherwise assume we'll get a SIGCHLD for it,
1181 * which we really should wait for to collect exit
1182 * status and code */
1183 return s
->main_pid
> 0;
1186 /* We don't know the pid */
1190 _pure_
static int control_pid_good(Service
*s
) {
1193 return s
->control_pid
> 0;
1196 static int cgroup_good(Service
*s
) {
1201 if (!UNIT(s
)->cgroup_path
)
1204 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
, true);
1211 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1215 if (f
!= SERVICE_SUCCESS
)
1218 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1220 if (s
->result
!= SERVICE_SUCCESS
) {
1221 log_unit_warning(UNIT(s
)->id
, "%s failed.", UNIT(s
)->id
);
1222 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1225 if (allow_restart
&&
1226 !s
->forbid_restart
&&
1227 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1228 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& s
->result
== SERVICE_SUCCESS
) ||
1229 (s
->restart
== SERVICE_RESTART_ON_FAILURE
&& s
->result
!= SERVICE_SUCCESS
) ||
1230 (s
->restart
== SERVICE_RESTART_ON_ABNORMAL
&& !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
)) ||
1231 (s
->restart
== SERVICE_RESTART_ON_WATCHDOG
&& s
->result
== SERVICE_FAILURE_WATCHDOG
) ||
1232 (s
->restart
== SERVICE_RESTART_ON_ABORT
&& IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
)) ||
1233 (s
->main_exec_status
.code
== CLD_EXITED
&& set_contains(s
->restart_force_status
.status
, INT_TO_PTR(s
->main_exec_status
.status
))) ||
1234 (IN_SET(s
->main_exec_status
.code
, CLD_KILLED
, CLD_DUMPED
) && set_contains(s
->restart_force_status
.signal
, INT_TO_PTR(s
->main_exec_status
.status
)))) &&
1235 (s
->main_exec_status
.code
!= CLD_EXITED
|| !set_contains(s
->restart_prevent_status
.status
, INT_TO_PTR(s
->main_exec_status
.status
))) &&
1236 (!IN_SET(s
->main_exec_status
.code
, CLD_KILLED
, CLD_DUMPED
) || !set_contains(s
->restart_prevent_status
.signal
, INT_TO_PTR(s
->main_exec_status
.status
)))) {
1238 r
= service_arm_timer(s
, s
->restart_usec
);
1242 service_set_state(s
, SERVICE_AUTO_RESTART
);
1245 s
->forbid_restart
= false;
1247 /* We want fresh tmpdirs in case service is started again immediately */
1248 exec_runtime_destroy(s
->exec_runtime
);
1249 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1251 /* Also, remove the runtime directory in */
1252 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1254 /* Try to delete the pid file. At this point it will be
1255 * out-of-date, and some software might be confused by it, so
1256 * let's remove it. */
1258 unlink_noerrno(s
->pid_file
);
1263 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run install restart timer: %m", UNIT(s
)->id
);
1264 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1267 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1271 if (f
!= SERVICE_SUCCESS
)
1274 service_unwatch_control_pid(s
);
1275 unit_watch_all_pids(UNIT(s
));
1277 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1278 if (s
->control_command
) {
1279 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1281 r
= service_spawn(s
,
1283 s
->timeout_stop_usec
,
1285 !s
->permissions_start_only
,
1286 !s
->root_directory_start_only
,
1293 service_set_state(s
, SERVICE_STOP_POST
);
1295 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1300 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'stop-post' task: %m", UNIT(s
)->id
);
1301 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1304 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1309 if (f
!= SERVICE_SUCCESS
)
1312 unit_watch_all_pids(UNIT(s
));
1314 r
= unit_kill_context(
1317 (state
!= SERVICE_STOP_SIGTERM
&& state
!= SERVICE_FINAL_SIGTERM
&& state
!= SERVICE_STOP_SIGABRT
) ?
1318 KILL_KILL
: (state
== SERVICE_STOP_SIGABRT
? KILL_ABORT
: KILL_TERMINATE
),
1327 if (s
->timeout_stop_usec
> 0) {
1328 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1333 service_set_state(s
, state
);
1334 } else if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGABRT
)
1335 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1336 else if (state
== SERVICE_STOP_SIGKILL
)
1337 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1338 else if (state
== SERVICE_FINAL_SIGTERM
)
1339 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1341 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1346 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to kill processes: %m", UNIT(s
)->id
);
1348 if (state
== SERVICE_STOP_SIGTERM
|| state
== SERVICE_STOP_SIGKILL
||
1349 state
== SERVICE_STOP_SIGABRT
)
1350 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1352 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1355 static void service_enter_stop_by_notify(Service
*s
) {
1358 unit_watch_all_pids(UNIT(s
));
1360 if (s
->timeout_stop_usec
> 0)
1361 service_arm_timer(s
, s
->timeout_stop_usec
);
1363 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1364 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1367 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1372 if (f
!= SERVICE_SUCCESS
)
1375 service_unwatch_control_pid(s
);
1376 unit_watch_all_pids(UNIT(s
));
1378 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1379 if (s
->control_command
) {
1380 s
->control_command_id
= SERVICE_EXEC_STOP
;
1382 r
= service_spawn(s
,
1384 s
->timeout_stop_usec
,
1386 !s
->permissions_start_only
,
1387 !s
->root_directory_start_only
,
1394 service_set_state(s
, SERVICE_STOP
);
1396 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1401 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'stop' task: %m", UNIT(s
)->id
);
1402 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1405 static void service_enter_running(Service
*s
, ServiceResult f
) {
1406 int main_pid_ok
, cgroup_ok
;
1409 if (f
!= SERVICE_SUCCESS
)
1412 main_pid_ok
= main_pid_good(s
);
1413 cgroup_ok
= cgroup_good(s
);
1415 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
1416 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
)) {
1418 /* If there are any queued up sd_notify()
1419 * notifications, process them now */
1420 if (s
->notify_state
== NOTIFY_RELOADING
)
1421 service_enter_reload_by_notify(s
);
1422 else if (s
->notify_state
== NOTIFY_STOPPING
)
1423 service_enter_stop_by_notify(s
);
1425 service_set_state(s
, SERVICE_RUNNING
);
1427 } else if (s
->remain_after_exit
)
1428 service_set_state(s
, SERVICE_EXITED
);
1430 service_enter_stop(s
, SERVICE_SUCCESS
);
1433 static void service_enter_start_post(Service
*s
) {
1437 service_unwatch_control_pid(s
);
1438 service_reset_watchdog(s
);
1440 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1441 if (s
->control_command
) {
1442 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1444 r
= service_spawn(s
,
1446 s
->timeout_start_usec
,
1448 !s
->permissions_start_only
,
1449 !s
->root_directory_start_only
,
1456 service_set_state(s
, SERVICE_START_POST
);
1458 service_enter_running(s
, SERVICE_SUCCESS
);
1463 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'start-post' task: %m", UNIT(s
)->id
);
1464 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1467 static void service_kill_control_processes(Service
*s
) {
1470 if (!UNIT(s
)->cgroup_path
)
1473 p
= strappenda(UNIT(s
)->cgroup_path
, "/control");
1474 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1477 static void service_enter_start(Service
*s
) {
1484 service_unwatch_control_pid(s
);
1485 service_unwatch_main_pid(s
);
1487 /* We want to ensure that nobody leaks processes from
1488 * START_PRE here, so let's go on a killing spree, People
1489 * should not spawn long running processes from START_PRE. */
1490 service_kill_control_processes(s
);
1492 if (s
->type
== SERVICE_FORKING
) {
1493 s
->control_command_id
= SERVICE_EXEC_START
;
1494 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1496 s
->main_command
= NULL
;
1498 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1499 s
->control_command
= NULL
;
1501 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1505 assert(s
->type
== SERVICE_ONESHOT
);
1506 service_enter_start_post(s
);
1510 r
= service_spawn(s
,
1512 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1522 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1523 /* For simple services we immediately start
1524 * the START_POST binaries. */
1526 service_set_main_pid(s
, pid
);
1527 service_enter_start_post(s
);
1529 } else if (s
->type
== SERVICE_FORKING
) {
1531 /* For forking services we wait until the start
1532 * process exited. */
1534 s
->control_pid
= pid
;
1535 service_set_state(s
, SERVICE_START
);
1537 } else if (s
->type
== SERVICE_ONESHOT
||
1538 s
->type
== SERVICE_DBUS
||
1539 s
->type
== SERVICE_NOTIFY
) {
1541 /* For oneshot services we wait until the start
1542 * process exited, too, but it is our main process. */
1544 /* For D-Bus services we know the main pid right away,
1545 * but wait for the bus name to appear on the
1546 * bus. Notify services are similar. */
1548 service_set_main_pid(s
, pid
);
1549 service_set_state(s
, SERVICE_START
);
1551 assert_not_reached("Unknown service type");
1556 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'start' task: %m", UNIT(s
)->id
);
1557 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1560 static void service_enter_start_pre(Service
*s
) {
1565 service_unwatch_control_pid(s
);
1567 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1568 if (s
->control_command
) {
1569 /* Before we start anything, let's clear up what might
1570 * be left from previous runs. */
1571 service_kill_control_processes(s
);
1573 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1575 r
= service_spawn(s
,
1577 s
->timeout_start_usec
,
1579 !s
->permissions_start_only
,
1580 !s
->root_directory_start_only
,
1587 service_set_state(s
, SERVICE_START_PRE
);
1589 service_enter_start(s
);
1594 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'start-pre' task: %m", UNIT(s
)->id
);
1595 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1598 static void service_enter_restart(Service
*s
) {
1599 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1604 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1605 /* Don't restart things if we are going down anyway */
1606 log_unit_info(UNIT(s
)->id
, "Stop job pending for unit, delaying automatic restart.");
1608 r
= service_arm_timer(s
, s
->restart_usec
);
1615 /* Any units that are bound to this service must also be
1616 * restarted. We use JOB_RESTART (instead of the more obvious
1617 * JOB_START) here so that those dependency jobs will be added
1619 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
1623 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1624 * it will be canceled as part of the service_stop() call that
1625 * is executed as part of JOB_RESTART. */
1627 log_unit_debug(UNIT(s
)->id
, "%s scheduled restart job.", UNIT(s
)->id
);
1631 log_unit_warning(UNIT(s
)->id
, "%s failed to schedule restart job: %s", UNIT(s
)->id
, bus_error_message(&error
, -r
));
1632 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1635 static void service_enter_reload_by_notify(Service
*s
) {
1638 if (s
->timeout_start_usec
> 0)
1639 service_arm_timer(s
, s
->timeout_start_usec
);
1641 service_set_state(s
, SERVICE_RELOAD
);
1644 static void service_enter_reload(Service
*s
) {
1649 service_unwatch_control_pid(s
);
1651 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1652 if (s
->control_command
) {
1653 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1655 r
= service_spawn(s
,
1657 s
->timeout_start_usec
,
1659 !s
->permissions_start_only
,
1660 !s
->root_directory_start_only
,
1667 service_set_state(s
, SERVICE_RELOAD
);
1669 service_enter_running(s
, SERVICE_SUCCESS
);
1674 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'reload' task: %m", UNIT(s
)->id
);
1675 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1676 service_enter_running(s
, SERVICE_SUCCESS
);
1679 static void service_run_next_control(Service
*s
) {
1683 assert(s
->control_command
);
1684 assert(s
->control_command
->command_next
);
1686 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1688 s
->control_command
= s
->control_command
->command_next
;
1689 service_unwatch_control_pid(s
);
1691 r
= service_spawn(s
,
1693 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1695 !s
->permissions_start_only
,
1696 !s
->root_directory_start_only
,
1697 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1698 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1707 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run next control task: %m", UNIT(s
)->id
);
1709 if (s
->state
== SERVICE_START_PRE
)
1710 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1711 else if (s
->state
== SERVICE_STOP
)
1712 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1713 else if (s
->state
== SERVICE_STOP_POST
)
1714 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1715 else if (s
->state
== SERVICE_RELOAD
) {
1716 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1717 service_enter_running(s
, SERVICE_SUCCESS
);
1719 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1722 static void service_run_next_main(Service
*s
) {
1727 assert(s
->main_command
);
1728 assert(s
->main_command
->command_next
);
1729 assert(s
->type
== SERVICE_ONESHOT
);
1731 s
->main_command
= s
->main_command
->command_next
;
1732 service_unwatch_main_pid(s
);
1734 r
= service_spawn(s
,
1736 s
->timeout_start_usec
,
1746 service_set_main_pid(s
, pid
);
1751 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run next main task: %m", UNIT(s
)->id
);
1752 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1755 static int service_start_limit_test(Service
*s
) {
1758 if (ratelimit_test(&s
->start_limit
))
1761 log_unit_warning(UNIT(s
)->id
, "start request repeated too quickly for %s", UNIT(s
)->id
);
1763 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
1766 static int service_start(Unit
*u
) {
1767 Service
*s
= SERVICE(u
);
1772 /* We cannot fulfill this request right now, try again later
1774 if (s
->state
== SERVICE_STOP
||
1775 s
->state
== SERVICE_STOP_SIGABRT
||
1776 s
->state
== SERVICE_STOP_SIGTERM
||
1777 s
->state
== SERVICE_STOP_SIGKILL
||
1778 s
->state
== SERVICE_STOP_POST
||
1779 s
->state
== SERVICE_FINAL_SIGTERM
||
1780 s
->state
== SERVICE_FINAL_SIGKILL
)
1783 /* Already on it! */
1784 if (s
->state
== SERVICE_START_PRE
||
1785 s
->state
== SERVICE_START
||
1786 s
->state
== SERVICE_START_POST
)
1789 /* A service that will be restarted must be stopped first to
1790 * trigger BindsTo and/or OnFailure dependencies. If a user
1791 * does not want to wait for the holdoff time to elapse, the
1792 * service should be manually restarted, not started. We
1793 * simply return EAGAIN here, so that any start jobs stay
1794 * queued, and assume that the auto restart timer will
1795 * eventually trigger the restart. */
1796 if (s
->state
== SERVICE_AUTO_RESTART
)
1799 assert(s
->state
== SERVICE_DEAD
|| s
->state
== SERVICE_FAILED
);
1801 /* Make sure we don't enter a busy loop of some kind. */
1802 r
= service_start_limit_test(s
);
1804 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
1808 s
->result
= SERVICE_SUCCESS
;
1809 s
->reload_result
= SERVICE_SUCCESS
;
1810 s
->main_pid_known
= false;
1811 s
->main_pid_alien
= false;
1812 s
->forbid_restart
= false;
1814 free(s
->status_text
);
1815 s
->status_text
= NULL
;
1816 s
->status_errno
= 0;
1818 s
->notify_state
= NOTIFY_UNKNOWN
;
1820 service_enter_start_pre(s
);
1824 static int service_stop(Unit
*u
) {
1825 Service
*s
= SERVICE(u
);
1829 /* Don't create restart jobs from here. */
1830 s
->forbid_restart
= true;
1833 if (s
->state
== SERVICE_STOP
||
1834 s
->state
== SERVICE_STOP_SIGABRT
||
1835 s
->state
== SERVICE_STOP_SIGTERM
||
1836 s
->state
== SERVICE_STOP_SIGKILL
||
1837 s
->state
== SERVICE_STOP_POST
||
1838 s
->state
== SERVICE_FINAL_SIGTERM
||
1839 s
->state
== SERVICE_FINAL_SIGKILL
)
1842 /* A restart will be scheduled or is in progress. */
1843 if (s
->state
== SERVICE_AUTO_RESTART
) {
1844 service_set_state(s
, SERVICE_DEAD
);
1848 /* If there's already something running we go directly into
1850 if (s
->state
== SERVICE_START_PRE
||
1851 s
->state
== SERVICE_START
||
1852 s
->state
== SERVICE_START_POST
||
1853 s
->state
== SERVICE_RELOAD
) {
1854 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1858 assert(s
->state
== SERVICE_RUNNING
||
1859 s
->state
== SERVICE_EXITED
);
1861 service_enter_stop(s
, SERVICE_SUCCESS
);
1865 static int service_reload(Unit
*u
) {
1866 Service
*s
= SERVICE(u
);
1870 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1872 service_enter_reload(s
);
1876 _pure_
static bool service_can_reload(Unit
*u
) {
1877 Service
*s
= SERVICE(u
);
1881 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1884 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1885 Service
*s
= SERVICE(u
);
1892 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1893 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
1894 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
1896 if (s
->control_pid
> 0)
1897 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
,
1900 if (s
->main_pid_known
&& s
->main_pid
> 0)
1901 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
1903 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
1906 unit_serialize_item(u
, f
, "status-text", s
->status_text
);
1908 /* FIXME: There's a minor uncleanliness here: if there are
1909 * multiple commands attached here, we will start from the
1910 * first one again */
1911 if (s
->control_command_id
>= 0)
1912 unit_serialize_item(u
, f
, "control-command",
1913 service_exec_command_to_string(s
->control_command_id
));
1915 if (s
->socket_fd
>= 0) {
1918 copy
= fdset_put_dup(fds
, s
->socket_fd
);
1922 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
1925 if (s
->bus_endpoint_fd
>= 0) {
1928 copy
= fdset_put_dup(fds
, s
->bus_endpoint_fd
);
1932 unit_serialize_item_format(u
, f
, "endpoint-fd", "%i", copy
);
1935 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1938 copy
= fdset_put_dup(fds
, fs
->fd
);
1942 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i", copy
);
1945 if (s
->main_exec_status
.pid
> 0) {
1946 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
,
1947 s
->main_exec_status
.pid
);
1948 dual_timestamp_serialize(f
, "main-exec-status-start",
1949 &s
->main_exec_status
.start_timestamp
);
1950 dual_timestamp_serialize(f
, "main-exec-status-exit",
1951 &s
->main_exec_status
.exit_timestamp
);
1953 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
1954 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i",
1955 s
->main_exec_status
.code
);
1956 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i",
1957 s
->main_exec_status
.status
);
1960 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
1961 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
1963 if (s
->forbid_restart
)
1964 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
1969 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
1970 Service
*s
= SERVICE(u
);
1978 if (streq(key
, "state")) {
1981 state
= service_state_from_string(value
);
1983 log_unit_debug(u
->id
, "Failed to parse state value %s", value
);
1985 s
->deserialized_state
= state
;
1986 } else if (streq(key
, "result")) {
1989 f
= service_result_from_string(value
);
1991 log_unit_debug(u
->id
, "Failed to parse result value %s", value
);
1992 else if (f
!= SERVICE_SUCCESS
)
1995 } else if (streq(key
, "reload-result")) {
1998 f
= service_result_from_string(value
);
2000 log_unit_debug(u
->id
, "Failed to parse reload result value %s", value
);
2001 else if (f
!= SERVICE_SUCCESS
)
2002 s
->reload_result
= f
;
2004 } else if (streq(key
, "control-pid")) {
2007 if (parse_pid(value
, &pid
) < 0)
2008 log_unit_debug(u
->id
, "Failed to parse control-pid value %s", value
);
2010 s
->control_pid
= pid
;
2011 } else if (streq(key
, "main-pid")) {
2014 if (parse_pid(value
, &pid
) < 0)
2015 log_unit_debug(u
->id
, "Failed to parse main-pid value %s", value
);
2017 service_set_main_pid(s
, pid
);
2018 unit_watch_pid(UNIT(s
), pid
);
2020 } else if (streq(key
, "main-pid-known")) {
2023 b
= parse_boolean(value
);
2025 log_unit_debug(u
->id
, "Failed to parse main-pid-known value %s", value
);
2027 s
->main_pid_known
= b
;
2028 } else if (streq(key
, "status-text")) {
2035 free(s
->status_text
);
2039 } else if (streq(key
, "control-command")) {
2040 ServiceExecCommand id
;
2042 id
= service_exec_command_from_string(value
);
2044 log_unit_debug(u
->id
, "Failed to parse exec-command value %s", value
);
2046 s
->control_command_id
= id
;
2047 s
->control_command
= s
->exec_command
[id
];
2049 } else if (streq(key
, "socket-fd")) {
2052 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2053 log_unit_debug(u
->id
, "Failed to parse socket-fd value %s", value
);
2055 asynchronous_close(s
->socket_fd
);
2056 s
->socket_fd
= fdset_remove(fds
, fd
);
2058 } else if (streq(key
, "endpoint-fd")) {
2061 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2062 log_unit_debug(u
->id
, "Failed to parse endpoint-fd value %s", value
);
2064 safe_close(s
->bus_endpoint_fd
);
2065 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2067 } else if (streq(key
, "fd-store-fd")) {
2070 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2071 log_unit_debug(u
->id
, "Failed to parse fd-store-fd value %s", value
);
2073 r
= service_add_fd_store(s
, fd
);
2075 log_unit_error_errno(u
->id
, r
, "Failed to add fd to store: %m");
2077 fdset_remove(fds
, fd
);
2080 } else if (streq(key
, "main-exec-status-pid")) {
2083 if (parse_pid(value
, &pid
) < 0)
2084 log_unit_debug(u
->id
, "Failed to parse main-exec-status-pid value %s", value
);
2086 s
->main_exec_status
.pid
= pid
;
2087 } else if (streq(key
, "main-exec-status-code")) {
2090 if (safe_atoi(value
, &i
) < 0)
2091 log_unit_debug(u
->id
, "Failed to parse main-exec-status-code value %s", value
);
2093 s
->main_exec_status
.code
= i
;
2094 } else if (streq(key
, "main-exec-status-status")) {
2097 if (safe_atoi(value
, &i
) < 0)
2098 log_unit_debug(u
->id
, "Failed to parse main-exec-status-status value %s", value
);
2100 s
->main_exec_status
.status
= i
;
2101 } else if (streq(key
, "main-exec-status-start"))
2102 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2103 else if (streq(key
, "main-exec-status-exit"))
2104 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2105 else if (streq(key
, "watchdog-timestamp"))
2106 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2107 else if (streq(key
, "forbid-restart")) {
2110 b
= parse_boolean(value
);
2112 log_unit_debug(u
->id
, "Failed to parse forbid-restart value %s", value
);
2114 s
->forbid_restart
= b
;
2116 log_unit_debug(u
->id
, "Unknown serialization key '%s'", key
);
2121 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2122 const UnitActiveState
*table
;
2126 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2128 return table
[SERVICE(u
)->state
];
2131 static const char *service_sub_state_to_string(Unit
*u
) {
2134 return service_state_to_string(SERVICE(u
)->state
);
2137 static bool service_check_gc(Unit
*u
) {
2138 Service
*s
= SERVICE(u
);
2142 /* Never clean up services that still have a process around,
2143 * even if the service is formally dead. */
2144 if (cgroup_good(s
) > 0 ||
2145 main_pid_good(s
) > 0 ||
2146 control_pid_good(s
) > 0)
2152 _pure_
static bool service_check_snapshot(Unit
*u
) {
2153 Service
*s
= SERVICE(u
);
2157 return s
->socket_fd
< 0;
2160 static int service_retry_pid_file(Service
*s
) {
2163 assert(s
->pid_file
);
2164 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2166 r
= service_load_pid_file(s
, false);
2170 service_unwatch_pid_file(s
);
2172 service_enter_running(s
, SERVICE_SUCCESS
);
2176 static int service_watch_pid_file(Service
*s
) {
2179 log_unit_debug(UNIT(s
)->id
, "Setting watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2181 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2185 /* the pidfile might have appeared just before we set the watch */
2186 log_unit_debug(UNIT(s
)->id
, "Trying to read %s's PID file %s in case it changed", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2187 service_retry_pid_file(s
);
2191 log_unit_error_errno(UNIT(s
)->id
, r
, "Failed to set a watch for %s's PID file %s: %m", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2192 service_unwatch_pid_file(s
);
2196 static int service_demand_pid_file(Service
*s
) {
2199 assert(s
->pid_file
);
2200 assert(!s
->pid_file_pathspec
);
2202 ps
= new0(PathSpec
, 1);
2207 ps
->path
= strdup(s
->pid_file
);
2213 path_kill_slashes(ps
->path
);
2215 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2216 * keep their PID file open all the time. */
2217 ps
->type
= PATH_MODIFIED
;
2218 ps
->inotify_fd
= -1;
2220 s
->pid_file_pathspec
= ps
;
2222 return service_watch_pid_file(s
);
2225 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2226 PathSpec
*p
= userdata
;
2231 s
= SERVICE(p
->unit
);
2235 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2236 assert(s
->pid_file_pathspec
);
2237 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2239 log_unit_debug(UNIT(s
)->id
, "inotify event for %s", UNIT(s
)->id
);
2241 if (path_spec_fd_event(p
, events
) < 0)
2244 if (service_retry_pid_file(s
) == 0)
2247 if (service_watch_pid_file(s
) < 0)
2253 service_unwatch_pid_file(s
);
2254 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2258 static void service_notify_cgroup_empty_event(Unit
*u
) {
2259 Service
*s
= SERVICE(u
);
2263 log_unit_debug(u
->id
, "%s: cgroup is empty", u
->id
);
2267 /* Waiting for SIGCHLD is usually more interesting,
2268 * because it includes return codes/signals. Which is
2269 * why we ignore the cgroup events for most cases,
2270 * except when we don't know pid which to expect the
2274 case SERVICE_START_POST
:
2275 /* If we were hoping for the daemon to write its PID file,
2276 * we can give up now. */
2277 if (s
->pid_file_pathspec
) {
2278 log_unit_warning(u
->id
, "%s never wrote its PID file. Failing.", UNIT(s
)->id
);
2280 service_unwatch_pid_file(s
);
2281 if (s
->state
== SERVICE_START
)
2282 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2284 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2288 case SERVICE_RUNNING
:
2289 /* service_enter_running() will figure out what to do */
2290 service_enter_running(s
, SERVICE_SUCCESS
);
2293 case SERVICE_STOP_SIGABRT
:
2294 case SERVICE_STOP_SIGTERM
:
2295 case SERVICE_STOP_SIGKILL
:
2297 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2298 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2302 case SERVICE_STOP_POST
:
2303 case SERVICE_FINAL_SIGTERM
:
2304 case SERVICE_FINAL_SIGKILL
:
2305 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2306 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2315 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2316 Service
*s
= SERVICE(u
);
2322 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2323 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2324 f
= SERVICE_SUCCESS
;
2325 else if (code
== CLD_EXITED
)
2326 f
= SERVICE_FAILURE_EXIT_CODE
;
2327 else if (code
== CLD_KILLED
)
2328 f
= SERVICE_FAILURE_SIGNAL
;
2329 else if (code
== CLD_DUMPED
)
2330 f
= SERVICE_FAILURE_CORE_DUMP
;
2332 assert_not_reached("Unknown code");
2334 if (s
->main_pid
== pid
) {
2335 /* Forking services may occasionally move to a new PID.
2336 * As long as they update the PID file before exiting the old
2337 * PID, they're fine. */
2338 if (service_load_pid_file(s
, false) == 0)
2342 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2344 if (s
->main_command
) {
2345 /* If this is not a forking service than the
2346 * main process got started and hence we copy
2347 * the exit status so that it is recorded both
2348 * as main and as control process exit
2351 s
->main_command
->exec_status
= s
->main_exec_status
;
2353 if (s
->main_command
->ignore
)
2354 f
= SERVICE_SUCCESS
;
2355 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2357 /* If this is a forked process, then we should
2358 * ignore the return value if this was
2359 * configured for the starter process */
2361 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2362 f
= SERVICE_SUCCESS
;
2365 log_unit_struct(u
->id
,
2366 f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2367 LOG_MESSAGE("%s: main process exited, code=%s, status=%i/%s",
2368 u
->id
, sigchld_code_to_string(code
), status
,
2369 strna(code
== CLD_EXITED
2370 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2371 : signal_to_string(status
))),
2372 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2373 "EXIT_STATUS=%i", status
,
2376 if (f
!= SERVICE_SUCCESS
)
2379 if (s
->main_command
&&
2380 s
->main_command
->command_next
&&
2381 f
== SERVICE_SUCCESS
) {
2383 /* There is another command to *
2384 * execute, so let's do that. */
2386 log_unit_debug(u
->id
, "%s running next main command for state %s", u
->id
, service_state_to_string(s
->state
));
2387 service_run_next_main(s
);
2391 /* The service exited, so the service is officially
2393 s
->main_command
= NULL
;
2397 case SERVICE_START_POST
:
2398 case SERVICE_RELOAD
:
2400 /* Need to wait until the operation is
2405 if (s
->type
== SERVICE_ONESHOT
) {
2406 /* This was our main goal, so let's go on */
2407 if (f
== SERVICE_SUCCESS
)
2408 service_enter_start_post(s
);
2410 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2416 case SERVICE_RUNNING
:
2417 service_enter_running(s
, f
);
2420 case SERVICE_STOP_SIGABRT
:
2421 case SERVICE_STOP_SIGTERM
:
2422 case SERVICE_STOP_SIGKILL
:
2424 if (!control_pid_good(s
))
2425 service_enter_stop_post(s
, f
);
2427 /* If there is still a control process, wait for that first */
2430 case SERVICE_STOP_POST
:
2431 case SERVICE_FINAL_SIGTERM
:
2432 case SERVICE_FINAL_SIGKILL
:
2434 if (!control_pid_good(s
))
2435 service_enter_dead(s
, f
, true);
2439 assert_not_reached("Uh, main process died at wrong time.");
2443 } else if (s
->control_pid
== pid
) {
2446 if (s
->control_command
) {
2447 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2449 if (s
->control_command
->ignore
)
2450 f
= SERVICE_SUCCESS
;
2453 log_unit_full(u
->id
,
2454 f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2455 "%s: control process exited, code=%s status=%i",
2456 u
->id
, sigchld_code_to_string(code
), status
);
2458 if (f
!= SERVICE_SUCCESS
)
2461 /* Immediately get rid of the cgroup, so that the
2462 * kernel doesn't delay the cgroup empty messages for
2463 * the service cgroup any longer than necessary */
2464 service_kill_control_processes(s
);
2466 if (s
->control_command
&&
2467 s
->control_command
->command_next
&&
2468 f
== SERVICE_SUCCESS
) {
2470 /* There is another command to *
2471 * execute, so let's do that. */
2473 log_unit_debug(u
->id
, "%s running next control command for state %s", u
->id
, service_state_to_string(s
->state
));
2474 service_run_next_control(s
);
2477 /* No further commands for this step, so let's
2478 * figure out what to do next */
2480 s
->control_command
= NULL
;
2481 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2483 log_unit_debug(u
->id
, "%s got final SIGCHLD for state %s", u
->id
, service_state_to_string(s
->state
));
2487 case SERVICE_START_PRE
:
2488 if (f
== SERVICE_SUCCESS
)
2489 service_enter_start(s
);
2491 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2495 if (s
->type
!= SERVICE_FORKING
)
2496 /* Maybe spurious event due to a reload that changed the type? */
2499 if (f
!= SERVICE_SUCCESS
) {
2500 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2505 bool has_start_post
;
2508 /* Let's try to load the pid file here if we can.
2509 * The PID file might actually be created by a START_POST
2510 * script. In that case don't worry if the loading fails. */
2512 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2513 r
= service_load_pid_file(s
, !has_start_post
);
2514 if (!has_start_post
&& r
< 0) {
2515 r
= service_demand_pid_file(s
);
2516 if (r
< 0 || !cgroup_good(s
))
2517 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2521 service_search_main_pid(s
);
2523 service_enter_start_post(s
);
2526 case SERVICE_START_POST
:
2527 if (f
!= SERVICE_SUCCESS
) {
2528 service_enter_stop(s
, f
);
2535 r
= service_load_pid_file(s
, true);
2537 r
= service_demand_pid_file(s
);
2538 if (r
< 0 || !cgroup_good(s
))
2539 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2543 service_search_main_pid(s
);
2545 service_enter_running(s
, SERVICE_SUCCESS
);
2548 case SERVICE_RELOAD
:
2549 if (f
== SERVICE_SUCCESS
) {
2550 service_load_pid_file(s
, true);
2551 service_search_main_pid(s
);
2554 s
->reload_result
= f
;
2555 service_enter_running(s
, SERVICE_SUCCESS
);
2559 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2562 case SERVICE_STOP_SIGABRT
:
2563 case SERVICE_STOP_SIGTERM
:
2564 case SERVICE_STOP_SIGKILL
:
2565 if (main_pid_good(s
) <= 0)
2566 service_enter_stop_post(s
, f
);
2568 /* If there is still a service
2569 * process around, wait until
2570 * that one quit, too */
2573 case SERVICE_STOP_POST
:
2574 case SERVICE_FINAL_SIGTERM
:
2575 case SERVICE_FINAL_SIGKILL
:
2576 if (main_pid_good(s
) <= 0)
2577 service_enter_dead(s
, f
, true);
2581 assert_not_reached("Uh, control process died at wrong time.");
2586 /* Notify clients about changed exit status */
2587 unit_add_to_dbus_queue(u
);
2589 /* We got one SIGCHLD for the service, let's watch all
2590 * processes that are now running of the service, and watch
2591 * that. Among the PIDs we then watch will be children
2592 * reassigned to us, which hopefully allows us to identify
2593 * when all children are gone */
2594 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2595 unit_watch_all_pids(u
);
2597 /* If the PID set is empty now, then let's finish this off */
2598 if (set_isempty(u
->pids
))
2599 service_notify_cgroup_empty_event(u
);
2602 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2603 Service
*s
= SERVICE(userdata
);
2606 assert(source
== s
->timer_event_source
);
2610 case SERVICE_START_PRE
:
2612 log_unit_warning(UNIT(s
)->id
, "%s %s operation timed out. Terminating.", UNIT(s
)->id
, s
->state
== SERVICE_START
? "start" : "start-pre");
2613 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2616 case SERVICE_START_POST
:
2617 log_unit_warning(UNIT(s
)->id
, "%s start-post operation timed out. Stopping.", UNIT(s
)->id
);
2618 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2621 case SERVICE_RELOAD
:
2622 log_unit_warning(UNIT(s
)->id
, "%s reload operation timed out. Stopping.", UNIT(s
)->id
);
2623 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2624 service_enter_running(s
, SERVICE_SUCCESS
);
2628 log_unit_warning(UNIT(s
)->id
, "%s stopping timed out. Terminating.", UNIT(s
)->id
);
2629 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2632 case SERVICE_STOP_SIGABRT
:
2633 log_unit_warning(UNIT(s
)->id
,
2634 "%s stop-sigabrt timed out. Terminating.", UNIT(s
)->id
);
2635 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, s
->result
);
2638 case SERVICE_STOP_SIGTERM
:
2639 if (s
->kill_context
.send_sigkill
) {
2640 log_unit_warning(UNIT(s
)->id
, "%s stop-sigterm timed out. Killing.", UNIT(s
)->id
);
2641 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2643 log_unit_warning(UNIT(s
)->id
, "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s
)->id
);
2644 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2649 case SERVICE_STOP_SIGKILL
:
2650 /* Uh, we sent a SIGKILL and it is still not gone?
2651 * Must be something we cannot kill, so let's just be
2652 * weirded out and continue */
2654 log_unit_warning(UNIT(s
)->id
, "%s still around after SIGKILL. Ignoring.", UNIT(s
)->id
);
2655 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2658 case SERVICE_STOP_POST
:
2659 log_unit_warning(UNIT(s
)->id
, "%s stop-post timed out. Terminating.", UNIT(s
)->id
);
2660 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2663 case SERVICE_FINAL_SIGTERM
:
2664 if (s
->kill_context
.send_sigkill
) {
2665 log_unit_warning(UNIT(s
)->id
, "%s stop-final-sigterm timed out. Killing.", UNIT(s
)->id
);
2666 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2668 log_unit_warning(UNIT(s
)->id
, "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", UNIT(s
)->id
);
2669 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2674 case SERVICE_FINAL_SIGKILL
:
2675 log_unit_warning(UNIT(s
)->id
, "%s still around after final SIGKILL. Entering failed mode.", UNIT(s
)->id
);
2676 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2679 case SERVICE_AUTO_RESTART
:
2680 log_unit_info(UNIT(s
)->id
,
2681 s
->restart_usec
> 0 ?
2682 "%s holdoff time over, scheduling restart." :
2683 "%s has no holdoff time, scheduling restart.",
2685 service_enter_restart(s
);
2689 assert_not_reached("Timeout at wrong time.");
2695 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2696 Service
*s
= SERVICE(userdata
);
2697 char t
[FORMAT_TIMESPAN_MAX
];
2700 assert(source
== s
->watchdog_event_source
);
2702 log_unit_error(UNIT(s
)->id
, "%s watchdog timeout (limit %s)!", UNIT(s
)->id
,
2703 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2705 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2710 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2711 Service
*s
= SERVICE(u
);
2712 _cleanup_free_
char *cc
= NULL
;
2713 bool notify_dbus
= false;
2718 cc
= strv_join(tags
, ", ");
2719 log_unit_debug(u
->id
, "%s: Got notification message from PID "PID_FMT
" (%s)",
2720 u
->id
, pid
, isempty(cc
) ? "n/a" : cc
);
2722 if (s
->notify_access
== NOTIFY_NONE
) {
2723 log_unit_warning(u
->id
, "%s: Got notification message from PID "PID_FMT
", but reception is disabled.", u
->id
, pid
);
2727 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2728 if (s
->main_pid
!= 0)
2729 log_unit_warning(u
->id
, "%s: Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, u
->id
, pid
, s
->main_pid
);
2731 log_unit_debug(u
->id
, "%s: Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", u
->id
, pid
);
2735 /* Interpret MAINPID= */
2736 e
= strv_find_startswith(tags
, "MAINPID=");
2737 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2738 if (parse_pid(e
, &pid
) < 0)
2739 log_unit_warning(u
->id
, "Failed to parse MAINPID= field in notification message: %s", e
);
2741 log_unit_debug(u
->id
, "%s: got MAINPID=%s", u
->id
, e
);
2743 service_set_main_pid(s
, pid
);
2744 unit_watch_pid(UNIT(s
), pid
);
2749 /* Interpret RELOADING= */
2750 if (strv_find(tags
, "RELOADING=1")) {
2752 log_unit_debug(u
->id
, "%s: got RELOADING=1", u
->id
);
2753 s
->notify_state
= NOTIFY_RELOADING
;
2755 if (s
->state
== SERVICE_RUNNING
)
2756 service_enter_reload_by_notify(s
);
2761 /* Interpret READY= */
2762 if (strv_find(tags
, "READY=1")) {
2764 log_unit_debug(u
->id
, "%s: got READY=1", u
->id
);
2765 s
->notify_state
= NOTIFY_READY
;
2767 /* Type=notify services inform us about completed
2768 * initialization with READY=1 */
2769 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2770 service_enter_start_post(s
);
2772 /* Sending READY=1 while we are reloading informs us
2773 * that the reloading is complete */
2774 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2775 service_enter_running(s
, SERVICE_SUCCESS
);
2780 /* Interpret STOPPING= */
2781 if (strv_find(tags
, "STOPPING=1")) {
2783 log_unit_debug(u
->id
, "%s: got STOPPING=1", u
->id
);
2784 s
->notify_state
= NOTIFY_STOPPING
;
2786 if (s
->state
== SERVICE_RUNNING
)
2787 service_enter_stop_by_notify(s
);
2792 /* Interpret STATUS= */
2793 e
= strv_find_startswith(tags
, "STATUS=");
2795 _cleanup_free_
char *t
= NULL
;
2798 if (!utf8_is_valid(e
))
2799 log_unit_warning(u
->id
, "Status message in notification is not UTF-8 clean.");
2801 log_unit_debug(u
->id
, "%s: got STATUS=%s", u
->id
, e
);
2809 if (!streq_ptr(s
->status_text
, t
)) {
2811 free(s
->status_text
);
2819 /* Interpret ERRNO= */
2820 e
= strv_find_startswith(tags
, "ERRNO=");
2824 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
2825 log_unit_warning(u
->id
, "Failed to parse ERRNO= field in notification message: %s", e
);
2827 log_unit_debug(u
->id
, "%s: got ERRNO=%s", u
->id
, e
);
2829 if (s
->status_errno
!= status_errno
) {
2830 s
->status_errno
= status_errno
;
2836 /* Interpret WATCHDOG= */
2837 if (strv_find(tags
, "WATCHDOG=1")) {
2838 log_unit_debug(u
->id
, "%s: got WATCHDOG=1", u
->id
);
2839 service_reset_watchdog(s
);
2842 /* Add the passed fds to the fd store */
2843 if (strv_find(tags
, "FDSTORE=1")) {
2844 log_unit_debug(u
->id
, "%s: got FDSTORE=1", u
->id
);
2845 service_add_fd_store_set(s
, fds
);
2848 /* Notify clients about changed status or main pid */
2850 unit_add_to_dbus_queue(u
);
2853 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
2854 Service
*s
= SERVICE(u
);
2857 if (!s
->timer_event_source
)
2860 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
2867 static void service_bus_name_owner_change(
2870 const char *old_owner
,
2871 const char *new_owner
) {
2873 Service
*s
= SERVICE(u
);
2879 assert(streq(s
->bus_name
, name
));
2880 assert(old_owner
|| new_owner
);
2882 if (old_owner
&& new_owner
)
2883 log_unit_debug(u
->id
, "%s's D-Bus name %s changed owner from %s to %s", u
->id
, name
, old_owner
, new_owner
);
2885 log_unit_debug(u
->id
, "%s's D-Bus name %s no longer registered by %s", u
->id
, name
, old_owner
);
2887 log_unit_debug(u
->id
, "%s's D-Bus name %s now registered by %s", u
->id
, name
, new_owner
);
2889 s
->bus_name_good
= !!new_owner
;
2891 if (s
->type
== SERVICE_DBUS
) {
2893 /* service_enter_running() will figure out what to
2895 if (s
->state
== SERVICE_RUNNING
)
2896 service_enter_running(s
, SERVICE_SUCCESS
);
2897 else if (s
->state
== SERVICE_START
&& new_owner
)
2898 service_enter_start_post(s
);
2900 } else if (new_owner
&&
2902 (s
->state
== SERVICE_START
||
2903 s
->state
== SERVICE_START_POST
||
2904 s
->state
== SERVICE_RUNNING
||
2905 s
->state
== SERVICE_RELOAD
)) {
2907 _cleanup_bus_creds_unref_ sd_bus_creds
*creds
= NULL
;
2910 /* Try to acquire PID from bus service */
2912 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
2914 r
= sd_bus_creds_get_pid(creds
, &pid
);
2916 log_unit_debug(u
->id
, "%s's D-Bus name %s is now owned by process %u", u
->id
, name
, (unsigned) pid
);
2918 service_set_main_pid(s
, pid
);
2919 unit_watch_pid(UNIT(s
), pid
);
2924 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
2925 _cleanup_free_
char *peer
= NULL
;
2931 /* This is called by the socket code when instantiating a new
2932 * service for a stream socket and the socket needs to be
2935 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
2938 if (s
->socket_fd
>= 0)
2941 if (s
->state
!= SERVICE_DEAD
)
2944 if (getpeername_pretty(fd
, &peer
) >= 0) {
2946 if (UNIT(s
)->description
) {
2947 _cleanup_free_
char *a
;
2949 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
2953 r
= unit_set_description(UNIT(s
), a
);
2955 r
= unit_set_description(UNIT(s
), peer
);
2962 s
->socket_fd_selinux_context_net
= selinux_context_net
;
2964 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
2966 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
2969 static void service_reset_failed(Unit
*u
) {
2970 Service
*s
= SERVICE(u
);
2974 if (s
->state
== SERVICE_FAILED
)
2975 service_set_state(s
, SERVICE_DEAD
);
2977 s
->result
= SERVICE_SUCCESS
;
2978 s
->reload_result
= SERVICE_SUCCESS
;
2980 RATELIMIT_RESET(s
->start_limit
);
2983 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
2984 Service
*s
= SERVICE(u
);
2986 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
2989 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
2990 [SERVICE_DEAD
] = "dead",
2991 [SERVICE_START_PRE
] = "start-pre",
2992 [SERVICE_START
] = "start",
2993 [SERVICE_START_POST
] = "start-post",
2994 [SERVICE_RUNNING
] = "running",
2995 [SERVICE_EXITED
] = "exited",
2996 [SERVICE_RELOAD
] = "reload",
2997 [SERVICE_STOP
] = "stop",
2998 [SERVICE_STOP_SIGABRT
] = "stop-sigabrt",
2999 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3000 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3001 [SERVICE_STOP_POST
] = "stop-post",
3002 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3003 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3004 [SERVICE_FAILED
] = "failed",
3005 [SERVICE_AUTO_RESTART
] = "auto-restart",
3008 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3010 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3011 [SERVICE_RESTART_NO
] = "no",
3012 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3013 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3014 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3015 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3016 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3017 [SERVICE_RESTART_ALWAYS
] = "always",
3020 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3022 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3023 [SERVICE_SIMPLE
] = "simple",
3024 [SERVICE_FORKING
] = "forking",
3025 [SERVICE_ONESHOT
] = "oneshot",
3026 [SERVICE_DBUS
] = "dbus",
3027 [SERVICE_NOTIFY
] = "notify",
3028 [SERVICE_IDLE
] = "idle"
3031 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3033 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3034 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3035 [SERVICE_EXEC_START
] = "ExecStart",
3036 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3037 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3038 [SERVICE_EXEC_STOP
] = "ExecStop",
3039 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3042 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3044 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3045 [NOTIFY_NONE
] = "none",
3046 [NOTIFY_MAIN
] = "main",
3047 [NOTIFY_ALL
] = "all"
3050 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3052 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3053 [NOTIFY_UNKNOWN
] = "unknown",
3054 [NOTIFY_READY
] = "ready",
3055 [NOTIFY_RELOADING
] = "reloading",
3056 [NOTIFY_STOPPING
] = "stopping",
3059 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3061 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3062 [SERVICE_SUCCESS
] = "success",
3063 [SERVICE_FAILURE_RESOURCES
] = "resources",
3064 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3065 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3066 [SERVICE_FAILURE_SIGNAL
] = "signal",
3067 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3068 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3069 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3072 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3074 const UnitVTable service_vtable
= {
3075 .object_size
= sizeof(Service
),
3076 .exec_context_offset
= offsetof(Service
, exec_context
),
3077 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3078 .kill_context_offset
= offsetof(Service
, kill_context
),
3079 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3085 .private_section
= "Service",
3087 .init
= service_init
,
3088 .done
= service_done
,
3089 .load
= service_load
,
3090 .release_resources
= service_release_resources
,
3092 .coldplug
= service_coldplug
,
3094 .dump
= service_dump
,
3096 .start
= service_start
,
3097 .stop
= service_stop
,
3098 .reload
= service_reload
,
3100 .can_reload
= service_can_reload
,
3102 .kill
= service_kill
,
3104 .serialize
= service_serialize
,
3105 .deserialize_item
= service_deserialize_item
,
3107 .active_state
= service_active_state
,
3108 .sub_state_to_string
= service_sub_state_to_string
,
3110 .check_gc
= service_check_gc
,
3111 .check_snapshot
= service_check_snapshot
,
3113 .sigchld_event
= service_sigchld_event
,
3115 .reset_failed
= service_reset_failed
,
3117 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3118 .notify_message
= service_notify_message
,
3120 .bus_name_owner_change
= service_bus_name_owner_change
,
3122 .bus_interface
= "org.freedesktop.systemd1.Service",
3123 .bus_vtable
= bus_service_vtable
,
3124 .bus_set_property
= bus_service_set_property
,
3125 .bus_commit_properties
= bus_service_commit_properties
,
3127 .get_timeout
= service_get_timeout
,
3128 .can_transient
= true,
3130 .status_message_formats
= {
3131 .starting_stopping
= {
3132 [0] = "Starting %s...",
3133 [1] = "Stopping %s...",
3135 .finished_start_job
= {
3136 [JOB_DONE
] = "Started %s.",
3137 [JOB_FAILED
] = "Failed to start %s.",
3138 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
3139 [JOB_TIMEOUT
] = "Timed out starting %s.",
3141 .finished_stop_job
= {
3142 [JOB_DONE
] = "Stopped %s.",
3143 [JOB_FAILED
] = "Stopped (with error) %s.",
3144 [JOB_TIMEOUT
] = "Timed out stopping %s.",