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 free(s
->pid_file_pathspec
);
146 s
->pid_file_pathspec
= NULL
;
149 static int service_set_main_pid(Service
*s
, pid_t pid
) {
160 if (s
->main_pid
== pid
&& s
->main_pid_known
)
163 if (s
->main_pid
!= pid
) {
164 service_unwatch_main_pid(s
);
165 exec_status_start(&s
->main_exec_status
, pid
);
169 s
->main_pid_known
= true;
171 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
172 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
173 s
->main_pid_alien
= true;
175 s
->main_pid_alien
= false;
180 static void service_close_socket_fd(Service
*s
) {
183 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
186 static void service_connection_unref(Service
*s
) {
189 if (!UNIT_ISSET(s
->accept_socket
))
192 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
193 unit_ref_unset(&s
->accept_socket
);
196 static void service_stop_watchdog(Service
*s
) {
199 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
200 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
203 static void service_start_watchdog(Service
*s
) {
208 if (s
->watchdog_usec
<= 0)
211 if (s
->watchdog_event_source
) {
212 r
= sd_event_source_set_time(s
->watchdog_event_source
, s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
);
214 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
218 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
220 r
= sd_event_add_time(
221 UNIT(s
)->manager
->event
,
222 &s
->watchdog_event_source
,
224 s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
, 0,
225 service_dispatch_watchdog
, s
);
227 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
231 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
233 /* Let's process everything else which might be a sign
234 * of living before we consider a service died. */
235 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
239 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
242 static void service_reset_watchdog(Service
*s
) {
245 dual_timestamp_get(&s
->watchdog_timestamp
);
246 service_start_watchdog(s
);
249 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
255 assert(fs
->service
->n_fd_store
> 0);
256 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
257 fs
->service
->n_fd_store
--;
260 if (fs
->event_source
) {
261 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
262 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
);
293 free(s
->status_text
);
294 s
->status_text
= NULL
;
297 s
->reboot_arg
= NULL
;
299 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
300 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
301 s
->control_command
= NULL
;
302 s
->main_command
= NULL
;
304 exit_status_set_free(&s
->restart_prevent_status
);
305 exit_status_set_free(&s
->restart_force_status
);
306 exit_status_set_free(&s
->success_status
);
308 /* This will leak a process, but at least no memory or any of
310 service_unwatch_main_pid(s
);
311 service_unwatch_control_pid(s
);
312 service_unwatch_pid_file(s
);
315 unit_unwatch_bus_name(u
, s
->bus_name
);
320 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
321 service_close_socket_fd(s
);
322 service_connection_unref(s
);
324 unit_ref_unset(&s
->accept_socket
);
326 service_stop_watchdog(s
);
328 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
330 service_release_resources(u
);
333 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
334 ServiceFDStore
*fs
= userdata
;
339 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
340 service_fd_store_unlink(fs
);
344 static int service_add_fd_store(Service
*s
, int fd
) {
351 if (s
->n_fd_store
>= s
->n_fd_store_max
)
354 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
355 r
= same_fd(fs
->fd
, fd
);
359 /* Already included */
365 fs
= new0(ServiceFDStore
, 1);
372 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
378 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
380 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
386 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
) {
391 if (fdset_size(fds
) <= 0)
394 while (s
->n_fd_store
< s
->n_fd_store_max
) {
395 _cleanup_close_
int fd
= -1;
397 fd
= fdset_steal_first(fds
);
401 r
= service_add_fd_store(s
, fd
);
403 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
405 log_unit_debug(UNIT(s
), "Added fd to fd store.");
410 if (fdset_size(fds
) > 0)
411 log_unit_warning(UNIT(s
), "Tried to store more fds than FDStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
416 static int service_arm_timer(Service
*s
, usec_t usec
) {
421 if (s
->timer_event_source
) {
422 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
426 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
429 r
= sd_event_add_time(
430 UNIT(s
)->manager
->event
,
431 &s
->timer_event_source
,
433 now(CLOCK_MONOTONIC
) + usec
, 0,
434 service_dispatch_timer
, s
);
438 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
443 static int service_verify(Service
*s
) {
446 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
449 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
450 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
454 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
455 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
459 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
460 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
464 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
465 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
469 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
470 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
474 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
475 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
479 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
480 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
484 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
485 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
487 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
488 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
495 static int service_add_default_dependencies(Service
*s
) {
500 /* Add a number of automatic dependencies useful for the
501 * majority of services. */
503 /* First, pull in base system */
504 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
508 /* Second, activate normal shutdown */
509 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
512 static void service_fix_output(Service
*s
) {
515 /* If nothing has been explicitly configured, patch default
516 * output in. If input is socket/tty we avoid this however,
517 * since in that case we want output to default to the same
518 * place as we read input from. */
520 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
521 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
522 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
523 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
525 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
526 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
527 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
530 static int service_add_extras(Service
*s
) {
535 if (s
->type
== _SERVICE_TYPE_INVALID
) {
536 /* Figure out a type automatically */
538 s
->type
= SERVICE_DBUS
;
539 else if (s
->exec_command
[SERVICE_EXEC_START
])
540 s
->type
= SERVICE_SIMPLE
;
542 s
->type
= SERVICE_ONESHOT
;
545 /* Oneshot services have disabled start timeout by default */
546 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
547 s
->timeout_start_usec
= 0;
549 service_fix_output(s
);
551 r
= unit_patch_contexts(UNIT(s
));
555 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
559 r
= unit_add_default_slice(UNIT(s
), &s
->cgroup_context
);
563 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
564 s
->notify_access
= NOTIFY_MAIN
;
566 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
567 s
->notify_access
= NOTIFY_MAIN
;
572 n
= strjoina(s
->bus_name
, ".busname");
573 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
577 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
579 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
581 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
584 if (UNIT(s
)->default_dependencies
) {
585 r
= service_add_default_dependencies(s
);
593 static int service_load(Unit
*u
) {
594 Service
*s
= SERVICE(u
);
599 /* Load a .service file */
600 r
= unit_load_fragment(u
);
604 /* Still nothing found? Then let's give up */
605 if (u
->load_state
== UNIT_STUB
)
608 /* This is a new unit? Then let's add in some extras */
609 if (u
->load_state
== UNIT_LOADED
) {
611 /* We were able to load something, then let's add in
612 * the dropin directories. */
613 r
= unit_load_dropin(u
);
617 /* This is a new unit? Then let's add in some
619 r
= service_add_extras(s
);
624 return service_verify(s
);
627 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
628 ServiceExecCommand c
;
629 Service
*s
= SERVICE(u
);
634 prefix
= strempty(prefix
);
635 prefix2
= strjoina(prefix
, "\t");
638 "%sService State: %s\n"
640 "%sReload Result: %s\n"
641 "%sPermissionsStartOnly: %s\n"
642 "%sRootDirectoryStartOnly: %s\n"
643 "%sRemainAfterExit: %s\n"
644 "%sGuessMainPID: %s\n"
647 "%sNotifyAccess: %s\n"
648 "%sNotifyState: %s\n",
649 prefix
, service_state_to_string(s
->state
),
650 prefix
, service_result_to_string(s
->result
),
651 prefix
, service_result_to_string(s
->reload_result
),
652 prefix
, yes_no(s
->permissions_start_only
),
653 prefix
, yes_no(s
->root_directory_start_only
),
654 prefix
, yes_no(s
->remain_after_exit
),
655 prefix
, yes_no(s
->guess_main_pid
),
656 prefix
, service_type_to_string(s
->type
),
657 prefix
, service_restart_to_string(s
->restart
),
658 prefix
, notify_access_to_string(s
->notify_access
),
659 prefix
, notify_state_to_string(s
->notify_state
));
661 if (s
->control_pid
> 0)
663 "%sControl PID: "PID_FMT
"\n",
664 prefix
, s
->control_pid
);
668 "%sMain PID: "PID_FMT
"\n"
669 "%sMain PID Known: %s\n"
670 "%sMain PID Alien: %s\n",
672 prefix
, yes_no(s
->main_pid_known
),
673 prefix
, yes_no(s
->main_pid_alien
));
678 prefix
, s
->pid_file
);
683 "%sBus Name Good: %s\n",
685 prefix
, yes_no(s
->bus_name_good
));
687 kill_context_dump(&s
->kill_context
, f
, prefix
);
688 exec_context_dump(&s
->exec_context
, f
, prefix
);
690 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
692 if (!s
->exec_command
[c
])
695 fprintf(f
, "%s-> %s:\n",
696 prefix
, service_exec_command_to_string(c
));
698 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
702 fprintf(f
, "%sStatus Text: %s\n",
703 prefix
, s
->status_text
);
705 if (s
->n_fd_store_max
> 0) {
707 "%sFile Descriptor Store Max: %u\n"
708 "%sFile Descriptor Store Current: %u\n",
709 prefix
, s
->n_fd_store_max
,
710 prefix
, s
->n_fd_store
);
714 static int service_load_pid_file(Service
*s
, bool may_warn
) {
715 _cleanup_free_
char *k
= NULL
;
724 r
= read_one_line_file(s
->pid_file
, &k
);
727 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
));
731 r
= parse_pid(k
, &pid
);
734 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
738 if (!pid_is_alive(pid
)) {
740 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
744 if (s
->main_pid_known
) {
745 if (pid
== s
->main_pid
)
748 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
750 service_unwatch_main_pid(s
);
751 s
->main_pid_known
= false;
753 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
755 r
= service_set_main_pid(s
, pid
);
759 r
= unit_watch_pid(UNIT(s
), pid
);
761 /* FIXME: we need to do something here */
762 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
769 static int service_search_main_pid(Service
*s
) {
775 /* If we know it anyway, don't ever fallback to unreliable
777 if (s
->main_pid_known
)
780 if (!s
->guess_main_pid
)
783 assert(s
->main_pid
<= 0);
785 pid
= unit_search_main_pid(UNIT(s
));
789 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
790 r
= service_set_main_pid(s
, pid
);
794 r
= unit_watch_pid(UNIT(s
), pid
);
796 /* FIXME: we need to do something here */
797 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
804 static void service_set_state(Service
*s
, ServiceState state
) {
805 ServiceState old_state
;
806 const UnitActiveState
*table
;
810 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
812 old_state
= s
->state
;
815 service_unwatch_pid_file(s
);
818 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
820 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
821 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
822 SERVICE_AUTO_RESTART
))
823 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
826 SERVICE_START
, SERVICE_START_POST
,
827 SERVICE_RUNNING
, SERVICE_RELOAD
,
828 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
829 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
830 service_unwatch_main_pid(s
);
831 s
->main_command
= NULL
;
835 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
837 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
838 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
839 service_unwatch_control_pid(s
);
840 s
->control_command
= NULL
;
841 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
844 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
845 unit_unwatch_all_pids(UNIT(s
));
848 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
849 SERVICE_RUNNING
, SERVICE_RELOAD
,
850 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
851 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
852 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
853 service_close_socket_fd(s
);
854 service_connection_unref(s
);
857 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
858 service_stop_watchdog(s
);
860 /* For the inactive states unit_notify() will trim the cgroup,
861 * but for exit we have to do that ourselves... */
862 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
863 unit_destroy_cgroup_if_empty(UNIT(s
));
865 /* For remain_after_exit services, let's see if we can "release" the
866 * hold on the console, since unit_notify() only does that in case of
868 if (state
== SERVICE_EXITED
&&
869 s
->remain_after_exit
&&
870 UNIT(s
)->manager
->n_on_console
> 0) {
874 ec
= unit_get_exec_context(UNIT(s
));
875 if (ec
&& exec_context_may_touch_console(ec
)) {
876 Manager
*m
= UNIT(s
)->manager
;
879 if (m
->n_on_console
== 0)
880 /* unset no_console_output flag, since the console is free */
881 m
->no_console_output
= false;
885 if (old_state
!= state
)
886 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
888 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
889 s
->reload_result
= SERVICE_SUCCESS
;
892 static int service_coldplug(Unit
*u
) {
893 Service
*s
= SERVICE(u
);
897 assert(s
->state
== SERVICE_DEAD
);
899 if (s
->deserialized_state
!= s
->state
) {
901 if (IN_SET(s
->deserialized_state
,
902 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
904 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
905 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
909 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
911 /* For the start/stop timeouts 0 means off */
913 r
= service_arm_timer(s
, k
);
919 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
921 /* The restart timeouts 0 means immediately */
922 r
= service_arm_timer(s
, s
->restart_usec
);
927 if (pid_is_unwaited(s
->main_pid
) &&
928 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
929 IN_SET(s
->deserialized_state
,
930 SERVICE_START
, SERVICE_START_POST
,
931 SERVICE_RUNNING
, SERVICE_RELOAD
,
932 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
933 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
934 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
939 if (pid_is_unwaited(s
->control_pid
) &&
940 IN_SET(s
->deserialized_state
,
941 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
943 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
944 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
945 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
950 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
951 unit_watch_all_pids(UNIT(s
));
953 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
954 service_start_watchdog(s
);
956 service_set_state(s
, s
->deserialized_state
);
962 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
963 _cleanup_free_
int *rfds
= NULL
;
973 if (s
->socket_fd
>= 0)
976 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
981 if (u
->type
!= UNIT_SOCKET
)
986 r
= socket_collect_fds(sock
, &cfds
, &cn_fds
);
1001 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1007 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1016 if (s
->n_fd_store
> 0) {
1020 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1025 LIST_FOREACH(fd_store
, fs
, s
->fd_store
)
1026 rfds
[rn_fds
++] = fs
->fd
;
1036 static int service_spawn(
1041 bool apply_permissions
,
1043 bool apply_tty_stdin
,
1050 _cleanup_free_
int *fdsbuf
= NULL
;
1051 unsigned n_fds
= 0, n_env
= 0;
1052 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1053 _cleanup_strv_free_
char
1054 **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1056 ExecParameters exec_params
= {
1057 .apply_permissions
= apply_permissions
,
1058 .apply_chroot
= apply_chroot
,
1059 .apply_tty_stdin
= apply_tty_stdin
,
1060 .bus_endpoint_fd
= -1,
1061 .selinux_context_net
= s
->socket_fd_selinux_context_net
1068 (void) unit_realize_cgroup(UNIT(s
));
1069 if (s
->reset_cpu_usage
) {
1070 (void) unit_reset_cpu_usage(UNIT(s
));
1071 s
->reset_cpu_usage
= false;
1074 r
= unit_setup_exec_runtime(UNIT(s
));
1079 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1080 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1081 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1083 if (s
->socket_fd
>= 0) {
1084 fds
= &s
->socket_fd
;
1087 r
= service_collect_fds(s
, &fdsbuf
, &n_fds
);
1096 r
= service_arm_timer(s
, timeout
);
1100 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1102 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1106 our_env
= new0(char*, 6);
1112 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1113 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1118 if (s
->main_pid
> 0)
1119 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1124 if (UNIT(s
)->manager
->running_as
!= MANAGER_SYSTEM
)
1125 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1130 if (UNIT_DEREF(s
->accept_socket
)) {
1131 union sockaddr_union sa
;
1132 socklen_t salen
= sizeof(sa
);
1134 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1140 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1141 _cleanup_free_
char *addr
= NULL
;
1145 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1149 t
= strappend("REMOTE_ADDR=", addr
);
1154 our_env
[n_env
++] = t
;
1156 port
= sockaddr_port(&sa
.sa
);
1162 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0) {
1166 our_env
[n_env
++] = t
;
1170 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1176 if (is_control
&& UNIT(s
)->cgroup_path
) {
1177 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1178 cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1180 path
= UNIT(s
)->cgroup_path
;
1182 if (s
->exec_context
.bus_endpoint
) {
1183 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
? "system" : "user",
1184 UNIT(s
)->id
, &bus_endpoint_path
);
1188 /* Pass the fd to the exec_params so that the child process can upload the policy.
1189 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1190 * as the service is running. */
1191 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1194 exec_params
.argv
= argv
;
1195 exec_params
.fds
= fds
;
1196 exec_params
.n_fds
= n_fds
;
1197 exec_params
.environment
= final_env
;
1198 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1199 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1200 exec_params
.cgroup_path
= path
;
1201 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1202 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1203 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1204 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1205 if (s
->type
== SERVICE_IDLE
)
1206 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1208 r
= exec_spawn(UNIT(s
),
1217 r
= unit_watch_pid(UNIT(s
), pid
);
1219 /* FIXME: we need to do something here */
1228 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1233 static int main_pid_good(Service
*s
) {
1236 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1239 /* If we know the pid file, then let's just check if it is
1241 if (s
->main_pid_known
) {
1243 /* If it's an alien child let's check if it is still
1245 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1246 return pid_is_alive(s
->main_pid
);
1248 /* .. otherwise assume we'll get a SIGCHLD for it,
1249 * which we really should wait for to collect exit
1250 * status and code */
1251 return s
->main_pid
> 0;
1254 /* We don't know the pid */
1258 _pure_
static int control_pid_good(Service
*s
) {
1261 return s
->control_pid
> 0;
1264 static int cgroup_good(Service
*s
) {
1269 if (!UNIT(s
)->cgroup_path
)
1272 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
, true);
1279 static bool service_shall_restart(Service
*s
) {
1282 /* Don't restart after manual stops */
1283 if (s
->forbid_restart
)
1286 /* Never restart if this is configured as special exception */
1287 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1290 /* Restart if the exit code/status are configured as restart triggers */
1291 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1294 switch (s
->restart
) {
1296 case SERVICE_RESTART_NO
:
1299 case SERVICE_RESTART_ALWAYS
:
1302 case SERVICE_RESTART_ON_SUCCESS
:
1303 return s
->result
== SERVICE_SUCCESS
;
1305 case SERVICE_RESTART_ON_FAILURE
:
1306 return s
->result
!= SERVICE_SUCCESS
;
1308 case SERVICE_RESTART_ON_ABNORMAL
:
1309 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1311 case SERVICE_RESTART_ON_WATCHDOG
:
1312 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1314 case SERVICE_RESTART_ON_ABORT
:
1315 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1318 assert_not_reached("unknown restart setting");
1322 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1326 if (f
!= SERVICE_SUCCESS
)
1329 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1331 if (s
->result
!= SERVICE_SUCCESS
) {
1332 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1333 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1336 if (allow_restart
&& service_shall_restart(s
)) {
1338 r
= service_arm_timer(s
, s
->restart_usec
);
1342 service_set_state(s
, SERVICE_AUTO_RESTART
);
1345 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1346 s
->forbid_restart
= false;
1348 /* We want fresh tmpdirs in case service is started again immediately */
1349 exec_runtime_destroy(s
->exec_runtime
);
1350 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1352 /* Also, remove the runtime directory in */
1353 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1355 /* Try to delete the pid file. At this point it will be
1356 * out-of-date, and some software might be confused by it, so
1357 * let's remove it. */
1359 unlink_noerrno(s
->pid_file
);
1364 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1365 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1368 static void service_enter_stop_post(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_POST
];
1379 if (s
->control_command
) {
1380 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
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_POST
);
1396 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1401 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1402 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1405 static int state_to_kill_operation(ServiceState state
) {
1408 case SERVICE_STOP_SIGABRT
:
1411 case SERVICE_STOP_SIGTERM
:
1412 case SERVICE_FINAL_SIGTERM
:
1413 return KILL_TERMINATE
;
1415 case SERVICE_STOP_SIGKILL
:
1416 case SERVICE_FINAL_SIGKILL
:
1420 return _KILL_OPERATION_INVALID
;
1424 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1429 if (f
!= SERVICE_SUCCESS
)
1432 unit_watch_all_pids(UNIT(s
));
1434 r
= unit_kill_context(
1437 state_to_kill_operation(state
),
1446 if (s
->timeout_stop_usec
> 0) {
1447 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1452 service_set_state(s
, state
);
1453 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1454 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1455 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1456 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1457 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1458 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1460 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1465 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1467 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1468 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1470 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1473 static void service_enter_stop_by_notify(Service
*s
) {
1476 unit_watch_all_pids(UNIT(s
));
1478 if (s
->timeout_stop_usec
> 0)
1479 service_arm_timer(s
, s
->timeout_stop_usec
);
1481 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1482 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1485 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1490 if (f
!= SERVICE_SUCCESS
)
1493 service_unwatch_control_pid(s
);
1494 unit_watch_all_pids(UNIT(s
));
1496 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1497 if (s
->control_command
) {
1498 s
->control_command_id
= SERVICE_EXEC_STOP
;
1500 r
= service_spawn(s
,
1502 s
->timeout_stop_usec
,
1504 !s
->permissions_start_only
,
1505 !s
->root_directory_start_only
,
1512 service_set_state(s
, SERVICE_STOP
);
1514 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1519 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1520 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1523 static void service_enter_running(Service
*s
, ServiceResult f
) {
1524 int main_pid_ok
, cgroup_ok
;
1527 if (f
!= SERVICE_SUCCESS
)
1530 main_pid_ok
= main_pid_good(s
);
1531 cgroup_ok
= cgroup_good(s
);
1533 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
1534 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
)) {
1536 /* If there are any queued up sd_notify()
1537 * notifications, process them now */
1538 if (s
->notify_state
== NOTIFY_RELOADING
)
1539 service_enter_reload_by_notify(s
);
1540 else if (s
->notify_state
== NOTIFY_STOPPING
)
1541 service_enter_stop_by_notify(s
);
1543 service_set_state(s
, SERVICE_RUNNING
);
1545 } else if (s
->remain_after_exit
)
1546 service_set_state(s
, SERVICE_EXITED
);
1548 service_enter_stop(s
, SERVICE_SUCCESS
);
1551 static void service_enter_start_post(Service
*s
) {
1555 service_unwatch_control_pid(s
);
1556 service_reset_watchdog(s
);
1558 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1559 if (s
->control_command
) {
1560 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1562 r
= service_spawn(s
,
1564 s
->timeout_start_usec
,
1566 !s
->permissions_start_only
,
1567 !s
->root_directory_start_only
,
1574 service_set_state(s
, SERVICE_START_POST
);
1576 service_enter_running(s
, SERVICE_SUCCESS
);
1581 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1582 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1585 static void service_kill_control_processes(Service
*s
) {
1588 if (!UNIT(s
)->cgroup_path
)
1591 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1592 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1595 static void service_enter_start(Service
*s
) {
1602 service_unwatch_control_pid(s
);
1603 service_unwatch_main_pid(s
);
1605 /* We want to ensure that nobody leaks processes from
1606 * START_PRE here, so let's go on a killing spree, People
1607 * should not spawn long running processes from START_PRE. */
1608 service_kill_control_processes(s
);
1610 if (s
->type
== SERVICE_FORKING
) {
1611 s
->control_command_id
= SERVICE_EXEC_START
;
1612 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1614 s
->main_command
= NULL
;
1616 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1617 s
->control_command
= NULL
;
1619 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1623 assert(s
->type
== SERVICE_ONESHOT
);
1624 service_enter_start_post(s
);
1628 r
= service_spawn(s
,
1630 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1640 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1641 /* For simple services we immediately start
1642 * the START_POST binaries. */
1644 service_set_main_pid(s
, pid
);
1645 service_enter_start_post(s
);
1647 } else if (s
->type
== SERVICE_FORKING
) {
1649 /* For forking services we wait until the start
1650 * process exited. */
1652 s
->control_pid
= pid
;
1653 service_set_state(s
, SERVICE_START
);
1655 } else if (s
->type
== SERVICE_ONESHOT
||
1656 s
->type
== SERVICE_DBUS
||
1657 s
->type
== SERVICE_NOTIFY
) {
1659 /* For oneshot services we wait until the start
1660 * process exited, too, but it is our main process. */
1662 /* For D-Bus services we know the main pid right away,
1663 * but wait for the bus name to appear on the
1664 * bus. Notify services are similar. */
1666 service_set_main_pid(s
, pid
);
1667 service_set_state(s
, SERVICE_START
);
1669 assert_not_reached("Unknown service type");
1674 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1675 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1678 static void service_enter_start_pre(Service
*s
) {
1683 service_unwatch_control_pid(s
);
1685 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1686 if (s
->control_command
) {
1687 /* Before we start anything, let's clear up what might
1688 * be left from previous runs. */
1689 service_kill_control_processes(s
);
1691 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1693 r
= service_spawn(s
,
1695 s
->timeout_start_usec
,
1697 !s
->permissions_start_only
,
1698 !s
->root_directory_start_only
,
1705 service_set_state(s
, SERVICE_START_PRE
);
1707 service_enter_start(s
);
1712 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1713 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1716 static void service_enter_restart(Service
*s
) {
1717 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1722 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1723 /* Don't restart things if we are going down anyway */
1724 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1726 r
= service_arm_timer(s
, s
->restart_usec
);
1733 /* Any units that are bound to this service must also be
1734 * restarted. We use JOB_RESTART (instead of the more obvious
1735 * JOB_START) here so that those dependency jobs will be added
1737 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
1741 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1742 * it will be canceled as part of the service_stop() call that
1743 * is executed as part of JOB_RESTART. */
1745 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1749 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1750 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1753 static void service_enter_reload_by_notify(Service
*s
) {
1756 if (s
->timeout_start_usec
> 0)
1757 service_arm_timer(s
, s
->timeout_start_usec
);
1759 service_set_state(s
, SERVICE_RELOAD
);
1762 static void service_enter_reload(Service
*s
) {
1767 service_unwatch_control_pid(s
);
1769 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1770 if (s
->control_command
) {
1771 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1773 r
= service_spawn(s
,
1775 s
->timeout_start_usec
,
1777 !s
->permissions_start_only
,
1778 !s
->root_directory_start_only
,
1785 service_set_state(s
, SERVICE_RELOAD
);
1787 service_enter_running(s
, SERVICE_SUCCESS
);
1792 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1793 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1794 service_enter_running(s
, SERVICE_SUCCESS
);
1797 static void service_run_next_control(Service
*s
) {
1801 assert(s
->control_command
);
1802 assert(s
->control_command
->command_next
);
1804 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1806 s
->control_command
= s
->control_command
->command_next
;
1807 service_unwatch_control_pid(s
);
1809 r
= service_spawn(s
,
1811 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1813 !s
->permissions_start_only
,
1814 !s
->root_directory_start_only
,
1815 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1816 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1825 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1827 if (s
->state
== SERVICE_START_PRE
)
1828 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1829 else if (s
->state
== SERVICE_STOP
)
1830 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1831 else if (s
->state
== SERVICE_STOP_POST
)
1832 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1833 else if (s
->state
== SERVICE_RELOAD
) {
1834 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1835 service_enter_running(s
, SERVICE_SUCCESS
);
1837 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1840 static void service_run_next_main(Service
*s
) {
1845 assert(s
->main_command
);
1846 assert(s
->main_command
->command_next
);
1847 assert(s
->type
== SERVICE_ONESHOT
);
1849 s
->main_command
= s
->main_command
->command_next
;
1850 service_unwatch_main_pid(s
);
1852 r
= service_spawn(s
,
1854 s
->timeout_start_usec
,
1864 service_set_main_pid(s
, pid
);
1869 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1870 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1873 static int service_start_limit_test(Service
*s
) {
1876 if (ratelimit_test(&s
->start_limit
))
1879 log_unit_warning(UNIT(s
), "Start request repeated too quickly.");
1881 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
1884 static int service_start(Unit
*u
) {
1885 Service
*s
= SERVICE(u
);
1890 /* We cannot fulfill this request right now, try again later
1892 if (IN_SET(s
->state
,
1893 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1894 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1897 /* Already on it! */
1898 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1901 /* A service that will be restarted must be stopped first to
1902 * trigger BindsTo and/or OnFailure dependencies. If a user
1903 * does not want to wait for the holdoff time to elapse, the
1904 * service should be manually restarted, not started. We
1905 * simply return EAGAIN here, so that any start jobs stay
1906 * queued, and assume that the auto restart timer will
1907 * eventually trigger the restart. */
1908 if (s
->state
== SERVICE_AUTO_RESTART
)
1911 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1913 /* Make sure we don't enter a busy loop of some kind. */
1914 r
= service_start_limit_test(s
);
1916 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
1920 s
->result
= SERVICE_SUCCESS
;
1921 s
->reload_result
= SERVICE_SUCCESS
;
1922 s
->main_pid_known
= false;
1923 s
->main_pid_alien
= false;
1924 s
->forbid_restart
= false;
1925 s
->reset_cpu_usage
= true;
1927 free(s
->status_text
);
1928 s
->status_text
= NULL
;
1929 s
->status_errno
= 0;
1931 s
->notify_state
= NOTIFY_UNKNOWN
;
1933 service_enter_start_pre(s
);
1937 static int service_stop(Unit
*u
) {
1938 Service
*s
= SERVICE(u
);
1942 /* Don't create restart jobs from manual stops. */
1943 s
->forbid_restart
= true;
1946 if (IN_SET(s
->state
,
1947 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1948 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1951 /* A restart will be scheduled or is in progress. */
1952 if (s
->state
== SERVICE_AUTO_RESTART
) {
1953 service_set_state(s
, SERVICE_DEAD
);
1957 /* If there's already something running we go directly into
1959 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
1960 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1964 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
1966 service_enter_stop(s
, SERVICE_SUCCESS
);
1970 static int service_reload(Unit
*u
) {
1971 Service
*s
= SERVICE(u
);
1975 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1977 service_enter_reload(s
);
1981 _pure_
static bool service_can_reload(Unit
*u
) {
1982 Service
*s
= SERVICE(u
);
1986 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1989 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1990 Service
*s
= SERVICE(u
);
1997 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1998 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
1999 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2001 if (s
->control_pid
> 0)
2002 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2004 if (s
->main_pid_known
&& s
->main_pid
> 0)
2005 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2007 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2008 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2010 if (s
->status_text
) {
2011 _cleanup_free_
char *c
= NULL
;
2013 c
= cescape(s
->status_text
);
2014 unit_serialize_item(u
, f
, "status-text", strempty(c
));
2017 /* FIXME: There's a minor uncleanliness here: if there are
2018 * multiple commands attached here, we will start from the
2019 * first one again */
2020 if (s
->control_command_id
>= 0)
2021 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2023 if (s
->socket_fd
>= 0) {
2026 copy
= fdset_put_dup(fds
, s
->socket_fd
);
2030 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2033 if (s
->bus_endpoint_fd
>= 0) {
2036 copy
= fdset_put_dup(fds
, s
->bus_endpoint_fd
);
2040 unit_serialize_item_format(u
, f
, "endpoint-fd", "%i", copy
);
2043 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2046 copy
= fdset_put_dup(fds
, fs
->fd
);
2050 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i", copy
);
2053 if (s
->main_exec_status
.pid
> 0) {
2054 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2055 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2056 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2058 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2059 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2060 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2064 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2065 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2067 if (s
->forbid_restart
)
2068 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2073 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2074 Service
*s
= SERVICE(u
);
2082 if (streq(key
, "state")) {
2085 state
= service_state_from_string(value
);
2087 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2089 s
->deserialized_state
= state
;
2090 } else if (streq(key
, "result")) {
2093 f
= service_result_from_string(value
);
2095 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2096 else if (f
!= SERVICE_SUCCESS
)
2099 } else if (streq(key
, "reload-result")) {
2102 f
= service_result_from_string(value
);
2104 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2105 else if (f
!= SERVICE_SUCCESS
)
2106 s
->reload_result
= f
;
2108 } else if (streq(key
, "control-pid")) {
2111 if (parse_pid(value
, &pid
) < 0)
2112 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2114 s
->control_pid
= pid
;
2115 } else if (streq(key
, "main-pid")) {
2118 if (parse_pid(value
, &pid
) < 0)
2119 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2121 service_set_main_pid(s
, pid
);
2122 unit_watch_pid(UNIT(s
), pid
);
2124 } else if (streq(key
, "main-pid-known")) {
2127 b
= parse_boolean(value
);
2129 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2131 s
->main_pid_known
= b
;
2132 } else if (streq(key
, "bus-name-good")) {
2135 b
= parse_boolean(value
);
2137 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2139 s
->bus_name_good
= b
;
2140 } else if (streq(key
, "status-text")) {
2143 r
= cunescape(value
, 0, &t
);
2145 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2147 free(s
->status_text
);
2151 } else if (streq(key
, "control-command")) {
2152 ServiceExecCommand id
;
2154 id
= service_exec_command_from_string(value
);
2156 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2158 s
->control_command_id
= id
;
2159 s
->control_command
= s
->exec_command
[id
];
2161 } else if (streq(key
, "socket-fd")) {
2164 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2165 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2167 asynchronous_close(s
->socket_fd
);
2168 s
->socket_fd
= fdset_remove(fds
, fd
);
2170 } else if (streq(key
, "endpoint-fd")) {
2173 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2174 log_unit_debug(u
, "Failed to parse endpoint-fd value: %s", value
);
2176 safe_close(s
->bus_endpoint_fd
);
2177 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2179 } else if (streq(key
, "fd-store-fd")) {
2182 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2183 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2185 r
= service_add_fd_store(s
, fd
);
2187 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2189 fdset_remove(fds
, fd
);
2192 } else if (streq(key
, "main-exec-status-pid")) {
2195 if (parse_pid(value
, &pid
) < 0)
2196 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2198 s
->main_exec_status
.pid
= pid
;
2199 } else if (streq(key
, "main-exec-status-code")) {
2202 if (safe_atoi(value
, &i
) < 0)
2203 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2205 s
->main_exec_status
.code
= i
;
2206 } else if (streq(key
, "main-exec-status-status")) {
2209 if (safe_atoi(value
, &i
) < 0)
2210 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2212 s
->main_exec_status
.status
= i
;
2213 } else if (streq(key
, "main-exec-status-start"))
2214 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2215 else if (streq(key
, "main-exec-status-exit"))
2216 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2217 else if (streq(key
, "watchdog-timestamp"))
2218 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2219 else if (streq(key
, "forbid-restart")) {
2222 b
= parse_boolean(value
);
2224 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2226 s
->forbid_restart
= b
;
2228 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2233 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2234 const UnitActiveState
*table
;
2238 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2240 return table
[SERVICE(u
)->state
];
2243 static const char *service_sub_state_to_string(Unit
*u
) {
2246 return service_state_to_string(SERVICE(u
)->state
);
2249 static bool service_check_gc(Unit
*u
) {
2250 Service
*s
= SERVICE(u
);
2254 /* Never clean up services that still have a process around,
2255 * even if the service is formally dead. */
2256 if (cgroup_good(s
) > 0 ||
2257 main_pid_good(s
) > 0 ||
2258 control_pid_good(s
) > 0)
2264 _pure_
static bool service_check_snapshot(Unit
*u
) {
2265 Service
*s
= SERVICE(u
);
2269 return s
->socket_fd
< 0;
2272 static int service_retry_pid_file(Service
*s
) {
2275 assert(s
->pid_file
);
2276 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2278 r
= service_load_pid_file(s
, false);
2282 service_unwatch_pid_file(s
);
2284 service_enter_running(s
, SERVICE_SUCCESS
);
2288 static int service_watch_pid_file(Service
*s
) {
2291 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2293 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2297 /* the pidfile might have appeared just before we set the watch */
2298 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2299 service_retry_pid_file(s
);
2303 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2304 service_unwatch_pid_file(s
);
2308 static int service_demand_pid_file(Service
*s
) {
2311 assert(s
->pid_file
);
2312 assert(!s
->pid_file_pathspec
);
2314 ps
= new0(PathSpec
, 1);
2319 ps
->path
= strdup(s
->pid_file
);
2325 path_kill_slashes(ps
->path
);
2327 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2328 * keep their PID file open all the time. */
2329 ps
->type
= PATH_MODIFIED
;
2330 ps
->inotify_fd
= -1;
2332 s
->pid_file_pathspec
= ps
;
2334 return service_watch_pid_file(s
);
2337 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2338 PathSpec
*p
= userdata
;
2343 s
= SERVICE(p
->unit
);
2347 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2348 assert(s
->pid_file_pathspec
);
2349 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2351 log_unit_debug(UNIT(s
), "inotify event");
2353 if (path_spec_fd_event(p
, events
) < 0)
2356 if (service_retry_pid_file(s
) == 0)
2359 if (service_watch_pid_file(s
) < 0)
2365 service_unwatch_pid_file(s
);
2366 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2370 static void service_notify_cgroup_empty_event(Unit
*u
) {
2371 Service
*s
= SERVICE(u
);
2375 log_unit_debug(u
, "cgroup is empty");
2379 /* Waiting for SIGCHLD is usually more interesting,
2380 * because it includes return codes/signals. Which is
2381 * why we ignore the cgroup events for most cases,
2382 * except when we don't know pid which to expect the
2386 case SERVICE_START_POST
:
2387 /* If we were hoping for the daemon to write its PID file,
2388 * we can give up now. */
2389 if (s
->pid_file_pathspec
) {
2390 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2392 service_unwatch_pid_file(s
);
2393 if (s
->state
== SERVICE_START
)
2394 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2396 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2400 case SERVICE_RUNNING
:
2401 /* service_enter_running() will figure out what to do */
2402 service_enter_running(s
, SERVICE_SUCCESS
);
2405 case SERVICE_STOP_SIGABRT
:
2406 case SERVICE_STOP_SIGTERM
:
2407 case SERVICE_STOP_SIGKILL
:
2409 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2410 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2414 case SERVICE_STOP_POST
:
2415 case SERVICE_FINAL_SIGTERM
:
2416 case SERVICE_FINAL_SIGKILL
:
2417 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2418 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2427 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2428 Service
*s
= SERVICE(u
);
2434 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2435 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2436 f
= SERVICE_SUCCESS
;
2437 else if (code
== CLD_EXITED
)
2438 f
= SERVICE_FAILURE_EXIT_CODE
;
2439 else if (code
== CLD_KILLED
)
2440 f
= SERVICE_FAILURE_SIGNAL
;
2441 else if (code
== CLD_DUMPED
)
2442 f
= SERVICE_FAILURE_CORE_DUMP
;
2444 assert_not_reached("Unknown code");
2446 if (s
->main_pid
== pid
) {
2447 /* Forking services may occasionally move to a new PID.
2448 * As long as they update the PID file before exiting the old
2449 * PID, they're fine. */
2450 if (service_load_pid_file(s
, false) == 0)
2454 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2456 if (s
->main_command
) {
2457 /* If this is not a forking service than the
2458 * main process got started and hence we copy
2459 * the exit status so that it is recorded both
2460 * as main and as control process exit
2463 s
->main_command
->exec_status
= s
->main_exec_status
;
2465 if (s
->main_command
->ignore
)
2466 f
= SERVICE_SUCCESS
;
2467 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2469 /* If this is a forked process, then we should
2470 * ignore the return value if this was
2471 * configured for the starter process */
2473 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2474 f
= SERVICE_SUCCESS
;
2477 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2479 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2480 sigchld_code_to_string(code
), status
,
2481 strna(code
== CLD_EXITED
2482 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2483 : signal_to_string(status
))),
2484 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2485 "EXIT_STATUS=%i", status
,
2488 if (f
!= SERVICE_SUCCESS
)
2491 if (s
->main_command
&&
2492 s
->main_command
->command_next
&&
2493 f
== SERVICE_SUCCESS
) {
2495 /* There is another command to *
2496 * execute, so let's do that. */
2498 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2499 service_run_next_main(s
);
2503 /* The service exited, so the service is officially
2505 s
->main_command
= NULL
;
2509 case SERVICE_START_POST
:
2510 case SERVICE_RELOAD
:
2512 /* Need to wait until the operation is
2517 if (s
->type
== SERVICE_ONESHOT
) {
2518 /* This was our main goal, so let's go on */
2519 if (f
== SERVICE_SUCCESS
)
2520 service_enter_start_post(s
);
2522 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2528 case SERVICE_RUNNING
:
2529 service_enter_running(s
, f
);
2532 case SERVICE_STOP_SIGABRT
:
2533 case SERVICE_STOP_SIGTERM
:
2534 case SERVICE_STOP_SIGKILL
:
2536 if (!control_pid_good(s
))
2537 service_enter_stop_post(s
, f
);
2539 /* If there is still a control process, wait for that first */
2542 case SERVICE_STOP_POST
:
2543 case SERVICE_FINAL_SIGTERM
:
2544 case SERVICE_FINAL_SIGKILL
:
2546 if (!control_pid_good(s
))
2547 service_enter_dead(s
, f
, true);
2551 assert_not_reached("Uh, main process died at wrong time.");
2555 } else if (s
->control_pid
== pid
) {
2558 if (s
->control_command
) {
2559 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2561 if (s
->control_command
->ignore
)
2562 f
= SERVICE_SUCCESS
;
2565 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2566 "Control process exited, code=%s status=%i",
2567 sigchld_code_to_string(code
), status
);
2569 if (f
!= SERVICE_SUCCESS
)
2572 /* Immediately get rid of the cgroup, so that the
2573 * kernel doesn't delay the cgroup empty messages for
2574 * the service cgroup any longer than necessary */
2575 service_kill_control_processes(s
);
2577 if (s
->control_command
&&
2578 s
->control_command
->command_next
&&
2579 f
== SERVICE_SUCCESS
) {
2581 /* There is another command to *
2582 * execute, so let's do that. */
2584 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2585 service_run_next_control(s
);
2588 /* No further commands for this step, so let's
2589 * figure out what to do next */
2591 s
->control_command
= NULL
;
2592 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2594 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2598 case SERVICE_START_PRE
:
2599 if (f
== SERVICE_SUCCESS
)
2600 service_enter_start(s
);
2602 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2606 if (s
->type
!= SERVICE_FORKING
)
2607 /* Maybe spurious event due to a reload that changed the type? */
2610 if (f
!= SERVICE_SUCCESS
) {
2611 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2616 bool has_start_post
;
2619 /* Let's try to load the pid file here if we can.
2620 * The PID file might actually be created by a START_POST
2621 * script. In that case don't worry if the loading fails. */
2623 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2624 r
= service_load_pid_file(s
, !has_start_post
);
2625 if (!has_start_post
&& r
< 0) {
2626 r
= service_demand_pid_file(s
);
2627 if (r
< 0 || !cgroup_good(s
))
2628 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2632 service_search_main_pid(s
);
2634 service_enter_start_post(s
);
2637 case SERVICE_START_POST
:
2638 if (f
!= SERVICE_SUCCESS
) {
2639 service_enter_stop(s
, f
);
2646 r
= service_load_pid_file(s
, true);
2648 r
= service_demand_pid_file(s
);
2649 if (r
< 0 || !cgroup_good(s
))
2650 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2654 service_search_main_pid(s
);
2656 service_enter_running(s
, SERVICE_SUCCESS
);
2659 case SERVICE_RELOAD
:
2660 if (f
== SERVICE_SUCCESS
) {
2661 service_load_pid_file(s
, true);
2662 service_search_main_pid(s
);
2665 s
->reload_result
= f
;
2666 service_enter_running(s
, SERVICE_SUCCESS
);
2670 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2673 case SERVICE_STOP_SIGABRT
:
2674 case SERVICE_STOP_SIGTERM
:
2675 case SERVICE_STOP_SIGKILL
:
2676 if (main_pid_good(s
) <= 0)
2677 service_enter_stop_post(s
, f
);
2679 /* If there is still a service
2680 * process around, wait until
2681 * that one quit, too */
2684 case SERVICE_STOP_POST
:
2685 case SERVICE_FINAL_SIGTERM
:
2686 case SERVICE_FINAL_SIGKILL
:
2687 if (main_pid_good(s
) <= 0)
2688 service_enter_dead(s
, f
, true);
2692 assert_not_reached("Uh, control process died at wrong time.");
2697 /* Notify clients about changed exit status */
2698 unit_add_to_dbus_queue(u
);
2700 /* We got one SIGCHLD for the service, let's watch all
2701 * processes that are now running of the service, and watch
2702 * that. Among the PIDs we then watch will be children
2703 * reassigned to us, which hopefully allows us to identify
2704 * when all children are gone */
2705 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2706 unit_watch_all_pids(u
);
2708 /* If the PID set is empty now, then let's finish this off */
2709 if (set_isempty(u
->pids
))
2710 service_notify_cgroup_empty_event(u
);
2713 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2714 Service
*s
= SERVICE(userdata
);
2717 assert(source
== s
->timer_event_source
);
2721 case SERVICE_START_PRE
:
2723 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2724 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2727 case SERVICE_START_POST
:
2728 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2729 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2732 case SERVICE_RELOAD
:
2733 log_unit_warning(UNIT(s
), "Reload operation timed out. Stopping.");
2734 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2735 service_enter_running(s
, SERVICE_SUCCESS
);
2739 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2740 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2743 case SERVICE_STOP_SIGABRT
:
2744 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2745 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2748 case SERVICE_STOP_SIGTERM
:
2749 if (s
->kill_context
.send_sigkill
) {
2750 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2751 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2753 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2754 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2759 case SERVICE_STOP_SIGKILL
:
2760 /* Uh, we sent a SIGKILL and it is still not gone?
2761 * Must be something we cannot kill, so let's just be
2762 * weirded out and continue */
2764 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2765 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2768 case SERVICE_STOP_POST
:
2769 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2770 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2773 case SERVICE_FINAL_SIGTERM
:
2774 if (s
->kill_context
.send_sigkill
) {
2775 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2776 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2778 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2779 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2784 case SERVICE_FINAL_SIGKILL
:
2785 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2786 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2789 case SERVICE_AUTO_RESTART
:
2790 log_unit_info(UNIT(s
),
2791 s
->restart_usec
> 0 ?
2792 "Service hold-off time over, scheduling restart." :
2793 "Service has no hold-off time, scheduling restart.");
2794 service_enter_restart(s
);
2798 assert_not_reached("Timeout at wrong time.");
2804 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2805 Service
*s
= SERVICE(userdata
);
2806 char t
[FORMAT_TIMESPAN_MAX
];
2809 assert(source
== s
->watchdog_event_source
);
2811 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2812 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2814 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2819 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2820 Service
*s
= SERVICE(u
);
2821 _cleanup_free_
char *cc
= NULL
;
2822 bool notify_dbus
= false;
2827 cc
= strv_join(tags
, ", ");
2829 if (s
->notify_access
== NOTIFY_NONE
) {
2830 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2832 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2833 if (s
->main_pid
!= 0)
2834 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2836 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2839 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2841 /* Interpret MAINPID= */
2842 e
= strv_find_startswith(tags
, "MAINPID=");
2843 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2844 if (parse_pid(e
, &pid
) < 0)
2845 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2847 service_set_main_pid(s
, pid
);
2848 unit_watch_pid(UNIT(s
), pid
);
2853 /* Interpret RELOADING= */
2854 if (strv_find(tags
, "RELOADING=1")) {
2856 s
->notify_state
= NOTIFY_RELOADING
;
2858 if (s
->state
== SERVICE_RUNNING
)
2859 service_enter_reload_by_notify(s
);
2864 /* Interpret READY= */
2865 if (strv_find(tags
, "READY=1")) {
2867 s
->notify_state
= NOTIFY_READY
;
2869 /* Type=notify services inform us about completed
2870 * initialization with READY=1 */
2871 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2872 service_enter_start_post(s
);
2874 /* Sending READY=1 while we are reloading informs us
2875 * that the reloading is complete */
2876 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2877 service_enter_running(s
, SERVICE_SUCCESS
);
2882 /* Interpret STOPPING= */
2883 if (strv_find(tags
, "STOPPING=1")) {
2885 s
->notify_state
= NOTIFY_STOPPING
;
2887 if (s
->state
== SERVICE_RUNNING
)
2888 service_enter_stop_by_notify(s
);
2893 /* Interpret STATUS= */
2894 e
= strv_find_startswith(tags
, "STATUS=");
2896 _cleanup_free_
char *t
= NULL
;
2899 if (!utf8_is_valid(e
))
2900 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
2908 if (!streq_ptr(s
->status_text
, t
)) {
2910 free(s
->status_text
);
2918 /* Interpret ERRNO= */
2919 e
= strv_find_startswith(tags
, "ERRNO=");
2923 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
2924 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
2926 if (s
->status_errno
!= status_errno
) {
2927 s
->status_errno
= status_errno
;
2933 /* Interpret WATCHDOG= */
2934 if (strv_find(tags
, "WATCHDOG=1")) {
2935 service_reset_watchdog(s
);
2938 /* Add the passed fds to the fd store */
2939 if (strv_find(tags
, "FDSTORE=1")) {
2940 service_add_fd_store_set(s
, fds
);
2943 /* Notify clients about changed status or main pid */
2945 unit_add_to_dbus_queue(u
);
2948 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
2949 Service
*s
= SERVICE(u
);
2952 if (!s
->timer_event_source
)
2955 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
2962 static void service_bus_name_owner_change(
2965 const char *old_owner
,
2966 const char *new_owner
) {
2968 Service
*s
= SERVICE(u
);
2974 assert(streq(s
->bus_name
, name
));
2975 assert(old_owner
|| new_owner
);
2977 if (old_owner
&& new_owner
)
2978 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
2980 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
2982 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
2984 s
->bus_name_good
= !!new_owner
;
2986 if (s
->type
== SERVICE_DBUS
) {
2988 /* service_enter_running() will figure out what to
2990 if (s
->state
== SERVICE_RUNNING
)
2991 service_enter_running(s
, SERVICE_SUCCESS
);
2992 else if (s
->state
== SERVICE_START
&& new_owner
)
2993 service_enter_start_post(s
);
2995 } else if (new_owner
&&
2997 (s
->state
== SERVICE_START
||
2998 s
->state
== SERVICE_START_POST
||
2999 s
->state
== SERVICE_RUNNING
||
3000 s
->state
== SERVICE_RELOAD
)) {
3002 _cleanup_bus_creds_unref_ sd_bus_creds
*creds
= NULL
;
3005 /* Try to acquire PID from bus service */
3007 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3009 r
= sd_bus_creds_get_pid(creds
, &pid
);
3011 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3013 service_set_main_pid(s
, pid
);
3014 unit_watch_pid(UNIT(s
), pid
);
3019 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3020 _cleanup_free_
char *peer
= NULL
;
3026 /* This is called by the socket code when instantiating a new
3027 * service for a stream socket and the socket needs to be
3030 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3033 if (s
->socket_fd
>= 0)
3036 if (s
->state
!= SERVICE_DEAD
)
3039 if (getpeername_pretty(fd
, &peer
) >= 0) {
3041 if (UNIT(s
)->description
) {
3042 _cleanup_free_
char *a
;
3044 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3048 r
= unit_set_description(UNIT(s
), a
);
3050 r
= unit_set_description(UNIT(s
), peer
);
3057 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3059 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3061 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3064 static void service_reset_failed(Unit
*u
) {
3065 Service
*s
= SERVICE(u
);
3069 if (s
->state
== SERVICE_FAILED
)
3070 service_set_state(s
, SERVICE_DEAD
);
3072 s
->result
= SERVICE_SUCCESS
;
3073 s
->reload_result
= SERVICE_SUCCESS
;
3075 RATELIMIT_RESET(s
->start_limit
);
3078 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3079 Service
*s
= SERVICE(u
);
3081 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3084 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
3085 [SERVICE_DEAD
] = "dead",
3086 [SERVICE_START_PRE
] = "start-pre",
3087 [SERVICE_START
] = "start",
3088 [SERVICE_START_POST
] = "start-post",
3089 [SERVICE_RUNNING
] = "running",
3090 [SERVICE_EXITED
] = "exited",
3091 [SERVICE_RELOAD
] = "reload",
3092 [SERVICE_STOP
] = "stop",
3093 [SERVICE_STOP_SIGABRT
] = "stop-sigabrt",
3094 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3095 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3096 [SERVICE_STOP_POST
] = "stop-post",
3097 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3098 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3099 [SERVICE_FAILED
] = "failed",
3100 [SERVICE_AUTO_RESTART
] = "auto-restart",
3103 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3105 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3106 [SERVICE_RESTART_NO
] = "no",
3107 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3108 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3109 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3110 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3111 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3112 [SERVICE_RESTART_ALWAYS
] = "always",
3115 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3117 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3118 [SERVICE_SIMPLE
] = "simple",
3119 [SERVICE_FORKING
] = "forking",
3120 [SERVICE_ONESHOT
] = "oneshot",
3121 [SERVICE_DBUS
] = "dbus",
3122 [SERVICE_NOTIFY
] = "notify",
3123 [SERVICE_IDLE
] = "idle"
3126 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3128 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3129 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3130 [SERVICE_EXEC_START
] = "ExecStart",
3131 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3132 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3133 [SERVICE_EXEC_STOP
] = "ExecStop",
3134 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3137 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3139 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3140 [NOTIFY_NONE
] = "none",
3141 [NOTIFY_MAIN
] = "main",
3142 [NOTIFY_ALL
] = "all"
3145 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3147 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3148 [NOTIFY_UNKNOWN
] = "unknown",
3149 [NOTIFY_READY
] = "ready",
3150 [NOTIFY_RELOADING
] = "reloading",
3151 [NOTIFY_STOPPING
] = "stopping",
3154 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3156 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3157 [SERVICE_SUCCESS
] = "success",
3158 [SERVICE_FAILURE_RESOURCES
] = "resources",
3159 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3160 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3161 [SERVICE_FAILURE_SIGNAL
] = "signal",
3162 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3163 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3164 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3167 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3169 const UnitVTable service_vtable
= {
3170 .object_size
= sizeof(Service
),
3171 .exec_context_offset
= offsetof(Service
, exec_context
),
3172 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3173 .kill_context_offset
= offsetof(Service
, kill_context
),
3174 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3180 .private_section
= "Service",
3182 .init
= service_init
,
3183 .done
= service_done
,
3184 .load
= service_load
,
3185 .release_resources
= service_release_resources
,
3187 .coldplug
= service_coldplug
,
3189 .dump
= service_dump
,
3191 .start
= service_start
,
3192 .stop
= service_stop
,
3193 .reload
= service_reload
,
3195 .can_reload
= service_can_reload
,
3197 .kill
= service_kill
,
3199 .serialize
= service_serialize
,
3200 .deserialize_item
= service_deserialize_item
,
3202 .active_state
= service_active_state
,
3203 .sub_state_to_string
= service_sub_state_to_string
,
3205 .check_gc
= service_check_gc
,
3206 .check_snapshot
= service_check_snapshot
,
3208 .sigchld_event
= service_sigchld_event
,
3210 .reset_failed
= service_reset_failed
,
3212 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3213 .notify_message
= service_notify_message
,
3215 .bus_name_owner_change
= service_bus_name_owner_change
,
3217 .bus_vtable
= bus_service_vtable
,
3218 .bus_set_property
= bus_service_set_property
,
3219 .bus_commit_properties
= bus_service_commit_properties
,
3221 .get_timeout
= service_get_timeout
,
3222 .can_transient
= true,
3224 .status_message_formats
= {
3225 .starting_stopping
= {
3226 [0] = "Starting %s...",
3227 [1] = "Stopping %s...",
3229 .finished_start_job
= {
3230 [JOB_DONE
] = "Started %s.",
3231 [JOB_FAILED
] = "Failed to start %s.",
3233 .finished_stop_job
= {
3234 [JOB_DONE
] = "Stopped %s.",
3235 [JOB_FAILED
] = "Stopped (with error) %s.",