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"
46 #include "bus-kernel.h"
47 #include "formats-util.h"
48 #include "process-util.h"
49 #include "signal-util.h"
51 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
52 [SERVICE_DEAD
] = UNIT_INACTIVE
,
53 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
54 [SERVICE_START
] = UNIT_ACTIVATING
,
55 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
56 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
57 [SERVICE_EXITED
] = UNIT_ACTIVE
,
58 [SERVICE_RELOAD
] = UNIT_RELOADING
,
59 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
60 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
61 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
62 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
63 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
64 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
65 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
66 [SERVICE_FAILED
] = UNIT_FAILED
,
67 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
70 /* For Type=idle we never want to delay any other jobs, hence we
71 * consider idle jobs active as soon as we start working on them */
72 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
73 [SERVICE_DEAD
] = UNIT_INACTIVE
,
74 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
75 [SERVICE_START
] = UNIT_ACTIVE
,
76 [SERVICE_START_POST
] = UNIT_ACTIVE
,
77 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
78 [SERVICE_EXITED
] = UNIT_ACTIVE
,
79 [SERVICE_RELOAD
] = UNIT_RELOADING
,
80 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
81 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
82 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
83 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
84 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
85 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
86 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
87 [SERVICE_FAILED
] = UNIT_FAILED
,
88 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
91 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
92 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
93 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
95 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
96 static void service_enter_reload_by_notify(Service
*s
);
98 static void service_init(Unit
*u
) {
99 Service
*s
= SERVICE(u
);
102 assert(u
->load_state
== UNIT_STUB
);
104 s
->timeout_start_usec
= u
->manager
->default_timeout_start_usec
;
105 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
106 s
->restart_usec
= u
->manager
->default_restart_usec
;
107 s
->type
= _SERVICE_TYPE_INVALID
;
109 s
->bus_endpoint_fd
= -1;
110 s
->guess_main_pid
= true;
112 RATELIMIT_INIT(s
->start_limit
, u
->manager
->default_start_limit_interval
, u
->manager
->default_start_limit_burst
);
114 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
117 static void service_unwatch_control_pid(Service
*s
) {
120 if (s
->control_pid
<= 0)
123 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
127 static void service_unwatch_main_pid(Service
*s
) {
130 if (s
->main_pid
<= 0)
133 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
137 static void service_unwatch_pid_file(Service
*s
) {
138 if (!s
->pid_file_pathspec
)
141 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
142 path_spec_unwatch(s
->pid_file_pathspec
);
143 path_spec_done(s
->pid_file_pathspec
);
144 free(s
->pid_file_pathspec
);
145 s
->pid_file_pathspec
= NULL
;
148 static int service_set_main_pid(Service
*s
, pid_t pid
) {
159 if (s
->main_pid
== pid
&& s
->main_pid_known
)
162 if (s
->main_pid
!= pid
) {
163 service_unwatch_main_pid(s
);
164 exec_status_start(&s
->main_exec_status
, pid
);
168 s
->main_pid_known
= true;
170 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
171 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
172 s
->main_pid_alien
= true;
174 s
->main_pid_alien
= false;
179 static void service_close_socket_fd(Service
*s
) {
182 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
185 static void service_connection_unref(Service
*s
) {
188 if (!UNIT_ISSET(s
->accept_socket
))
191 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
192 unit_ref_unset(&s
->accept_socket
);
195 static void service_stop_watchdog(Service
*s
) {
198 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
199 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
202 static void service_start_watchdog(Service
*s
) {
207 if (s
->watchdog_usec
<= 0)
210 if (s
->watchdog_event_source
) {
211 r
= sd_event_source_set_time(s
->watchdog_event_source
, s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
);
213 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
217 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
219 r
= sd_event_add_time(
220 UNIT(s
)->manager
->event
,
221 &s
->watchdog_event_source
,
223 s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
, 0,
224 service_dispatch_watchdog
, s
);
226 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
230 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
232 /* Let's process everything else which might be a sign
233 * of living before we consider a service died. */
234 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
238 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
241 static void service_reset_watchdog(Service
*s
) {
244 dual_timestamp_get(&s
->watchdog_timestamp
);
245 service_start_watchdog(s
);
248 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
254 assert(fs
->service
->n_fd_store
> 0);
255 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
256 fs
->service
->n_fd_store
--;
259 if (fs
->event_source
) {
260 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
261 sd_event_source_unref(fs
->event_source
);
268 static void service_release_resources(Unit
*u
) {
269 Service
*s
= SERVICE(u
);
276 log_unit_debug(u
, "Releasing all resources.");
279 service_fd_store_unlink(s
->fd_store
);
281 assert(s
->n_fd_store
== 0);
284 static void service_done(Unit
*u
) {
285 Service
*s
= SERVICE(u
);
292 free(s
->status_text
);
293 s
->status_text
= NULL
;
296 s
->reboot_arg
= NULL
;
298 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
299 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
300 s
->control_command
= NULL
;
301 s
->main_command
= NULL
;
303 exit_status_set_free(&s
->restart_prevent_status
);
304 exit_status_set_free(&s
->restart_force_status
);
305 exit_status_set_free(&s
->success_status
);
307 /* This will leak a process, but at least no memory or any of
309 service_unwatch_main_pid(s
);
310 service_unwatch_control_pid(s
);
311 service_unwatch_pid_file(s
);
314 unit_unwatch_bus_name(u
, s
->bus_name
);
319 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
320 service_close_socket_fd(s
);
321 service_connection_unref(s
);
323 unit_ref_unset(&s
->accept_socket
);
325 service_stop_watchdog(s
);
327 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
329 service_release_resources(u
);
332 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
333 ServiceFDStore
*fs
= userdata
;
338 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
339 service_fd_store_unlink(fs
);
343 static int service_add_fd_store(Service
*s
, int fd
) {
350 if (s
->n_fd_store
>= s
->n_fd_store_max
)
353 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
354 r
= same_fd(fs
->fd
, fd
);
358 /* Already included */
364 fs
= new0(ServiceFDStore
, 1);
371 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
377 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
379 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
385 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
) {
390 if (fdset_size(fds
) <= 0)
393 while (s
->n_fd_store
< s
->n_fd_store_max
) {
394 _cleanup_close_
int fd
= -1;
396 fd
= fdset_steal_first(fds
);
400 r
= service_add_fd_store(s
, fd
);
402 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
404 log_unit_debug(UNIT(s
), "Added fd to fd store.");
409 if (fdset_size(fds
) > 0)
410 log_unit_warning(UNIT(s
), "Tried to store more fds than FDStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
415 static int service_arm_timer(Service
*s
, usec_t usec
) {
420 if (s
->timer_event_source
) {
421 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
425 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
428 r
= sd_event_add_time(
429 UNIT(s
)->manager
->event
,
430 &s
->timer_event_source
,
432 now(CLOCK_MONOTONIC
) + usec
, 0,
433 service_dispatch_timer
, s
);
437 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
442 static int service_verify(Service
*s
) {
445 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
448 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
449 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
453 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
454 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
458 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
459 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
463 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
464 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
468 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
469 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
473 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
474 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
478 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
479 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
483 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
484 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
486 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
487 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
494 static int service_add_default_dependencies(Service
*s
) {
499 /* Add a number of automatic dependencies useful for the
500 * majority of services. */
502 /* First, pull in base system */
503 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
507 /* Second, activate normal shutdown */
508 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
511 static void service_fix_output(Service
*s
) {
514 /* If nothing has been explicitly configured, patch default
515 * output in. If input is socket/tty we avoid this however,
516 * since in that case we want output to default to the same
517 * place as we read input from. */
519 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
520 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
521 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
522 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
524 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
525 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
526 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
529 static int service_add_extras(Service
*s
) {
534 if (s
->type
== _SERVICE_TYPE_INVALID
) {
535 /* Figure out a type automatically */
537 s
->type
= SERVICE_DBUS
;
538 else if (s
->exec_command
[SERVICE_EXEC_START
])
539 s
->type
= SERVICE_SIMPLE
;
541 s
->type
= SERVICE_ONESHOT
;
544 /* Oneshot services have disabled start timeout by default */
545 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
546 s
->timeout_start_usec
= 0;
548 service_fix_output(s
);
550 r
= unit_patch_contexts(UNIT(s
));
554 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
558 r
= unit_add_default_slice(UNIT(s
), &s
->cgroup_context
);
562 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
563 s
->notify_access
= NOTIFY_MAIN
;
565 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
566 s
->notify_access
= NOTIFY_MAIN
;
571 n
= strjoina(s
->bus_name
, ".busname");
572 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
576 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
578 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
580 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
583 if (UNIT(s
)->default_dependencies
) {
584 r
= service_add_default_dependencies(s
);
592 static int service_load(Unit
*u
) {
593 Service
*s
= SERVICE(u
);
598 /* Load a .service file */
599 r
= unit_load_fragment(u
);
603 /* Still nothing found? Then let's give up */
604 if (u
->load_state
== UNIT_STUB
)
607 /* This is a new unit? Then let's add in some extras */
608 if (u
->load_state
== UNIT_LOADED
) {
610 /* We were able to load something, then let's add in
611 * the dropin directories. */
612 r
= unit_load_dropin(u
);
616 /* This is a new unit? Then let's add in some
618 r
= service_add_extras(s
);
623 return service_verify(s
);
626 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
627 ServiceExecCommand c
;
628 Service
*s
= SERVICE(u
);
633 prefix
= strempty(prefix
);
634 prefix2
= strjoina(prefix
, "\t");
637 "%sService State: %s\n"
639 "%sReload Result: %s\n"
640 "%sPermissionsStartOnly: %s\n"
641 "%sRootDirectoryStartOnly: %s\n"
642 "%sRemainAfterExit: %s\n"
643 "%sGuessMainPID: %s\n"
646 "%sNotifyAccess: %s\n"
647 "%sNotifyState: %s\n",
648 prefix
, service_state_to_string(s
->state
),
649 prefix
, service_result_to_string(s
->result
),
650 prefix
, service_result_to_string(s
->reload_result
),
651 prefix
, yes_no(s
->permissions_start_only
),
652 prefix
, yes_no(s
->root_directory_start_only
),
653 prefix
, yes_no(s
->remain_after_exit
),
654 prefix
, yes_no(s
->guess_main_pid
),
655 prefix
, service_type_to_string(s
->type
),
656 prefix
, service_restart_to_string(s
->restart
),
657 prefix
, notify_access_to_string(s
->notify_access
),
658 prefix
, notify_state_to_string(s
->notify_state
));
660 if (s
->control_pid
> 0)
662 "%sControl PID: "PID_FMT
"\n",
663 prefix
, s
->control_pid
);
667 "%sMain PID: "PID_FMT
"\n"
668 "%sMain PID Known: %s\n"
669 "%sMain PID Alien: %s\n",
671 prefix
, yes_no(s
->main_pid_known
),
672 prefix
, yes_no(s
->main_pid_alien
));
677 prefix
, s
->pid_file
);
682 "%sBus Name Good: %s\n",
684 prefix
, yes_no(s
->bus_name_good
));
686 kill_context_dump(&s
->kill_context
, f
, prefix
);
687 exec_context_dump(&s
->exec_context
, f
, prefix
);
689 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
691 if (!s
->exec_command
[c
])
694 fprintf(f
, "%s-> %s:\n",
695 prefix
, service_exec_command_to_string(c
));
697 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
701 fprintf(f
, "%sStatus Text: %s\n",
702 prefix
, s
->status_text
);
704 if (s
->n_fd_store_max
> 0) {
706 "%sFile Descriptor Store Max: %u\n"
707 "%sFile Descriptor Store Current: %u\n",
708 prefix
, s
->n_fd_store_max
,
709 prefix
, s
->n_fd_store
);
713 static int service_load_pid_file(Service
*s
, bool may_warn
) {
714 _cleanup_free_
char *k
= NULL
;
723 r
= read_one_line_file(s
->pid_file
, &k
);
726 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
));
730 r
= parse_pid(k
, &pid
);
733 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
737 if (!pid_is_alive(pid
)) {
739 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
743 if (s
->main_pid_known
) {
744 if (pid
== s
->main_pid
)
747 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
749 service_unwatch_main_pid(s
);
750 s
->main_pid_known
= false;
752 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
754 r
= service_set_main_pid(s
, pid
);
758 r
= unit_watch_pid(UNIT(s
), pid
);
760 /* FIXME: we need to do something here */
761 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
768 static int service_search_main_pid(Service
*s
) {
774 /* If we know it anyway, don't ever fallback to unreliable
776 if (s
->main_pid_known
)
779 if (!s
->guess_main_pid
)
782 assert(s
->main_pid
<= 0);
784 pid
= unit_search_main_pid(UNIT(s
));
788 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
789 r
= service_set_main_pid(s
, pid
);
793 r
= unit_watch_pid(UNIT(s
), pid
);
795 /* FIXME: we need to do something here */
796 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
803 static void service_set_state(Service
*s
, ServiceState state
) {
804 ServiceState old_state
;
805 const UnitActiveState
*table
;
809 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
811 old_state
= s
->state
;
814 service_unwatch_pid_file(s
);
817 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
819 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
820 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
821 SERVICE_AUTO_RESTART
))
822 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
825 SERVICE_START
, SERVICE_START_POST
,
826 SERVICE_RUNNING
, SERVICE_RELOAD
,
827 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
828 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
829 service_unwatch_main_pid(s
);
830 s
->main_command
= NULL
;
834 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
836 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
837 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
838 service_unwatch_control_pid(s
);
839 s
->control_command
= NULL
;
840 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
843 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
844 unit_unwatch_all_pids(UNIT(s
));
847 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
848 SERVICE_RUNNING
, SERVICE_RELOAD
,
849 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
850 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
851 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
852 service_close_socket_fd(s
);
853 service_connection_unref(s
);
856 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
857 service_stop_watchdog(s
);
859 /* For the inactive states unit_notify() will trim the cgroup,
860 * but for exit we have to do that ourselves... */
861 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
862 unit_destroy_cgroup_if_empty(UNIT(s
));
864 /* For remain_after_exit services, let's see if we can "release" the
865 * hold on the console, since unit_notify() only does that in case of
867 if (state
== SERVICE_EXITED
&&
868 s
->remain_after_exit
&&
869 UNIT(s
)->manager
->n_on_console
> 0) {
873 ec
= unit_get_exec_context(UNIT(s
));
874 if (ec
&& exec_context_may_touch_console(ec
)) {
875 Manager
*m
= UNIT(s
)->manager
;
878 if (m
->n_on_console
== 0)
879 /* unset no_console_output flag, since the console is free */
880 m
->no_console_output
= false;
884 if (old_state
!= state
)
885 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
887 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
888 s
->reload_result
= SERVICE_SUCCESS
;
891 static int service_coldplug(Unit
*u
) {
892 Service
*s
= SERVICE(u
);
896 assert(s
->state
== SERVICE_DEAD
);
898 if (s
->deserialized_state
!= s
->state
) {
900 if (IN_SET(s
->deserialized_state
,
901 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
903 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
904 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
908 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
910 /* For the start/stop timeouts 0 means off */
912 r
= service_arm_timer(s
, k
);
918 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
920 /* The restart timeouts 0 means immediately */
921 r
= service_arm_timer(s
, s
->restart_usec
);
926 if (pid_is_unwaited(s
->main_pid
) &&
927 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
928 IN_SET(s
->deserialized_state
,
929 SERVICE_START
, SERVICE_START_POST
,
930 SERVICE_RUNNING
, SERVICE_RELOAD
,
931 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
932 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
933 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
938 if (pid_is_unwaited(s
->control_pid
) &&
939 IN_SET(s
->deserialized_state
,
940 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
942 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
943 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
944 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
949 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
950 unit_watch_all_pids(UNIT(s
));
952 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
953 service_start_watchdog(s
);
955 service_set_state(s
, s
->deserialized_state
);
961 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
962 _cleanup_free_
int *rfds
= NULL
;
972 if (s
->socket_fd
>= 0)
975 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
980 if (u
->type
!= UNIT_SOCKET
)
985 r
= socket_collect_fds(sock
, &cfds
, &cn_fds
);
1000 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1006 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1015 if (s
->n_fd_store
> 0) {
1019 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1024 LIST_FOREACH(fd_store
, fs
, s
->fd_store
)
1025 rfds
[rn_fds
++] = fs
->fd
;
1035 static int service_spawn(
1040 bool apply_permissions
,
1042 bool apply_tty_stdin
,
1049 _cleanup_free_
int *fdsbuf
= NULL
;
1050 unsigned n_fds
= 0, n_env
= 0;
1051 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1052 _cleanup_strv_free_
char
1053 **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1055 ExecParameters exec_params
= {
1056 .apply_permissions
= apply_permissions
,
1057 .apply_chroot
= apply_chroot
,
1058 .apply_tty_stdin
= apply_tty_stdin
,
1059 .bus_endpoint_fd
= -1,
1060 .selinux_context_net
= s
->socket_fd_selinux_context_net
1067 (void) unit_realize_cgroup(UNIT(s
));
1068 if (s
->reset_cpu_usage
) {
1069 (void) unit_reset_cpu_usage(UNIT(s
));
1070 s
->reset_cpu_usage
= false;
1073 r
= unit_setup_exec_runtime(UNIT(s
));
1078 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1079 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1080 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1082 if (s
->socket_fd
>= 0) {
1083 fds
= &s
->socket_fd
;
1086 r
= service_collect_fds(s
, &fdsbuf
, &n_fds
);
1095 r
= service_arm_timer(s
, timeout
);
1099 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1101 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1105 our_env
= new0(char*, 6);
1111 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1112 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1117 if (s
->main_pid
> 0)
1118 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1123 if (UNIT(s
)->manager
->running_as
!= MANAGER_SYSTEM
)
1124 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1129 if (UNIT_DEREF(s
->accept_socket
)) {
1130 union sockaddr_union sa
;
1131 socklen_t salen
= sizeof(sa
);
1133 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1139 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1140 _cleanup_free_
char *addr
= NULL
;
1144 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1148 t
= strappend("REMOTE_ADDR=", addr
);
1153 our_env
[n_env
++] = t
;
1155 port
= sockaddr_port(&sa
.sa
);
1161 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0) {
1165 our_env
[n_env
++] = t
;
1169 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1175 if (is_control
&& UNIT(s
)->cgroup_path
) {
1176 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1177 cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1179 path
= UNIT(s
)->cgroup_path
;
1181 if (s
->exec_context
.bus_endpoint
) {
1182 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
? "system" : "user",
1183 UNIT(s
)->id
, &bus_endpoint_path
);
1187 /* Pass the fd to the exec_params so that the child process can upload the policy.
1188 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1189 * as the service is running. */
1190 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1193 exec_params
.argv
= argv
;
1194 exec_params
.fds
= fds
;
1195 exec_params
.n_fds
= n_fds
;
1196 exec_params
.environment
= final_env
;
1197 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1198 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1199 exec_params
.cgroup_path
= path
;
1200 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1201 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1202 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1203 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1204 if (s
->type
== SERVICE_IDLE
)
1205 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1207 r
= exec_spawn(UNIT(s
),
1216 r
= unit_watch_pid(UNIT(s
), pid
);
1218 /* FIXME: we need to do something here */
1227 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1232 static int main_pid_good(Service
*s
) {
1235 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1238 /* If we know the pid file, then let's just check if it is
1240 if (s
->main_pid_known
) {
1242 /* If it's an alien child let's check if it is still
1244 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1245 return pid_is_alive(s
->main_pid
);
1247 /* .. otherwise assume we'll get a SIGCHLD for it,
1248 * which we really should wait for to collect exit
1249 * status and code */
1250 return s
->main_pid
> 0;
1253 /* We don't know the pid */
1257 _pure_
static int control_pid_good(Service
*s
) {
1260 return s
->control_pid
> 0;
1263 static int cgroup_good(Service
*s
) {
1268 if (!UNIT(s
)->cgroup_path
)
1271 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
, true);
1278 static bool service_shall_restart(Service
*s
) {
1281 /* Don't restart after manual stops */
1282 if (s
->forbid_restart
)
1285 /* Never restart if this is configured as special exception */
1286 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1289 /* Restart if the exit code/status are configured as restart triggers */
1290 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1293 switch (s
->restart
) {
1295 case SERVICE_RESTART_NO
:
1298 case SERVICE_RESTART_ALWAYS
:
1301 case SERVICE_RESTART_ON_SUCCESS
:
1302 return s
->result
== SERVICE_SUCCESS
;
1304 case SERVICE_RESTART_ON_FAILURE
:
1305 return s
->result
!= SERVICE_SUCCESS
;
1307 case SERVICE_RESTART_ON_ABNORMAL
:
1308 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1310 case SERVICE_RESTART_ON_WATCHDOG
:
1311 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1313 case SERVICE_RESTART_ON_ABORT
:
1314 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1317 assert_not_reached("unknown restart setting");
1321 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1325 if (f
!= SERVICE_SUCCESS
)
1328 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1330 if (s
->result
!= SERVICE_SUCCESS
) {
1331 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1332 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1335 if (allow_restart
&& service_shall_restart(s
)) {
1337 r
= service_arm_timer(s
, s
->restart_usec
);
1341 service_set_state(s
, SERVICE_AUTO_RESTART
);
1344 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1345 s
->forbid_restart
= false;
1347 /* We want fresh tmpdirs in case service is started again immediately */
1348 exec_runtime_destroy(s
->exec_runtime
);
1349 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1351 /* Also, remove the runtime directory in */
1352 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1354 /* Try to delete the pid file. At this point it will be
1355 * out-of-date, and some software might be confused by it, so
1356 * let's remove it. */
1358 unlink_noerrno(s
->pid_file
);
1363 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1364 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1367 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1371 if (f
!= SERVICE_SUCCESS
)
1374 service_unwatch_control_pid(s
);
1375 unit_watch_all_pids(UNIT(s
));
1377 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1378 if (s
->control_command
) {
1379 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1381 r
= service_spawn(s
,
1383 s
->timeout_stop_usec
,
1385 !s
->permissions_start_only
,
1386 !s
->root_directory_start_only
,
1393 service_set_state(s
, SERVICE_STOP_POST
);
1395 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1400 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1401 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1404 static int state_to_kill_operation(ServiceState state
) {
1407 case SERVICE_STOP_SIGABRT
:
1410 case SERVICE_STOP_SIGTERM
:
1411 case SERVICE_FINAL_SIGTERM
:
1412 return KILL_TERMINATE
;
1414 case SERVICE_STOP_SIGKILL
:
1415 case SERVICE_FINAL_SIGKILL
:
1419 return _KILL_OPERATION_INVALID
;
1423 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1428 if (f
!= SERVICE_SUCCESS
)
1431 unit_watch_all_pids(UNIT(s
));
1433 r
= unit_kill_context(
1436 state_to_kill_operation(state
),
1445 if (s
->timeout_stop_usec
> 0) {
1446 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1451 service_set_state(s
, state
);
1452 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1453 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1454 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1455 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1456 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1457 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1459 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1464 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1466 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1467 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1469 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1472 static void service_enter_stop_by_notify(Service
*s
) {
1475 unit_watch_all_pids(UNIT(s
));
1477 if (s
->timeout_stop_usec
> 0)
1478 service_arm_timer(s
, s
->timeout_stop_usec
);
1480 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1481 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1484 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1489 if (f
!= SERVICE_SUCCESS
)
1492 service_unwatch_control_pid(s
);
1493 unit_watch_all_pids(UNIT(s
));
1495 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1496 if (s
->control_command
) {
1497 s
->control_command_id
= SERVICE_EXEC_STOP
;
1499 r
= service_spawn(s
,
1501 s
->timeout_stop_usec
,
1503 !s
->permissions_start_only
,
1504 !s
->root_directory_start_only
,
1511 service_set_state(s
, SERVICE_STOP
);
1513 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1518 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1519 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1522 static void service_enter_running(Service
*s
, ServiceResult f
) {
1523 int main_pid_ok
, cgroup_ok
;
1526 if (f
!= SERVICE_SUCCESS
)
1529 main_pid_ok
= main_pid_good(s
);
1530 cgroup_ok
= cgroup_good(s
);
1532 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
1533 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
)) {
1535 /* If there are any queued up sd_notify()
1536 * notifications, process them now */
1537 if (s
->notify_state
== NOTIFY_RELOADING
)
1538 service_enter_reload_by_notify(s
);
1539 else if (s
->notify_state
== NOTIFY_STOPPING
)
1540 service_enter_stop_by_notify(s
);
1542 service_set_state(s
, SERVICE_RUNNING
);
1544 } else if (s
->remain_after_exit
)
1545 service_set_state(s
, SERVICE_EXITED
);
1547 service_enter_stop(s
, SERVICE_SUCCESS
);
1550 static void service_enter_start_post(Service
*s
) {
1554 service_unwatch_control_pid(s
);
1555 service_reset_watchdog(s
);
1557 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1558 if (s
->control_command
) {
1559 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1561 r
= service_spawn(s
,
1563 s
->timeout_start_usec
,
1565 !s
->permissions_start_only
,
1566 !s
->root_directory_start_only
,
1573 service_set_state(s
, SERVICE_START_POST
);
1575 service_enter_running(s
, SERVICE_SUCCESS
);
1580 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1581 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1584 static void service_kill_control_processes(Service
*s
) {
1587 if (!UNIT(s
)->cgroup_path
)
1590 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1591 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1594 static void service_enter_start(Service
*s
) {
1601 service_unwatch_control_pid(s
);
1602 service_unwatch_main_pid(s
);
1604 /* We want to ensure that nobody leaks processes from
1605 * START_PRE here, so let's go on a killing spree, People
1606 * should not spawn long running processes from START_PRE. */
1607 service_kill_control_processes(s
);
1609 if (s
->type
== SERVICE_FORKING
) {
1610 s
->control_command_id
= SERVICE_EXEC_START
;
1611 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1613 s
->main_command
= NULL
;
1615 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1616 s
->control_command
= NULL
;
1618 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1622 assert(s
->type
== SERVICE_ONESHOT
);
1623 service_enter_start_post(s
);
1627 r
= service_spawn(s
,
1629 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1639 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1640 /* For simple services we immediately start
1641 * the START_POST binaries. */
1643 service_set_main_pid(s
, pid
);
1644 service_enter_start_post(s
);
1646 } else if (s
->type
== SERVICE_FORKING
) {
1648 /* For forking services we wait until the start
1649 * process exited. */
1651 s
->control_pid
= pid
;
1652 service_set_state(s
, SERVICE_START
);
1654 } else if (s
->type
== SERVICE_ONESHOT
||
1655 s
->type
== SERVICE_DBUS
||
1656 s
->type
== SERVICE_NOTIFY
) {
1658 /* For oneshot services we wait until the start
1659 * process exited, too, but it is our main process. */
1661 /* For D-Bus services we know the main pid right away,
1662 * but wait for the bus name to appear on the
1663 * bus. Notify services are similar. */
1665 service_set_main_pid(s
, pid
);
1666 service_set_state(s
, SERVICE_START
);
1668 assert_not_reached("Unknown service type");
1673 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1674 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1677 static void service_enter_start_pre(Service
*s
) {
1682 service_unwatch_control_pid(s
);
1684 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1685 if (s
->control_command
) {
1686 /* Before we start anything, let's clear up what might
1687 * be left from previous runs. */
1688 service_kill_control_processes(s
);
1690 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1692 r
= service_spawn(s
,
1694 s
->timeout_start_usec
,
1696 !s
->permissions_start_only
,
1697 !s
->root_directory_start_only
,
1704 service_set_state(s
, SERVICE_START_PRE
);
1706 service_enter_start(s
);
1711 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1712 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1715 static void service_enter_restart(Service
*s
) {
1716 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1721 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1722 /* Don't restart things if we are going down anyway */
1723 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1725 r
= service_arm_timer(s
, s
->restart_usec
);
1732 /* Any units that are bound to this service must also be
1733 * restarted. We use JOB_RESTART (instead of the more obvious
1734 * JOB_START) here so that those dependency jobs will be added
1736 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
1740 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1741 * it will be canceled as part of the service_stop() call that
1742 * is executed as part of JOB_RESTART. */
1744 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1748 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1749 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1752 static void service_enter_reload_by_notify(Service
*s
) {
1755 if (s
->timeout_start_usec
> 0)
1756 service_arm_timer(s
, s
->timeout_start_usec
);
1758 service_set_state(s
, SERVICE_RELOAD
);
1761 static void service_enter_reload(Service
*s
) {
1766 service_unwatch_control_pid(s
);
1768 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1769 if (s
->control_command
) {
1770 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1772 r
= service_spawn(s
,
1774 s
->timeout_start_usec
,
1776 !s
->permissions_start_only
,
1777 !s
->root_directory_start_only
,
1784 service_set_state(s
, SERVICE_RELOAD
);
1786 service_enter_running(s
, SERVICE_SUCCESS
);
1791 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1792 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1793 service_enter_running(s
, SERVICE_SUCCESS
);
1796 static void service_run_next_control(Service
*s
) {
1800 assert(s
->control_command
);
1801 assert(s
->control_command
->command_next
);
1803 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1805 s
->control_command
= s
->control_command
->command_next
;
1806 service_unwatch_control_pid(s
);
1808 r
= service_spawn(s
,
1810 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1812 !s
->permissions_start_only
,
1813 !s
->root_directory_start_only
,
1814 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1815 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1824 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1826 if (s
->state
== SERVICE_START_PRE
)
1827 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1828 else if (s
->state
== SERVICE_STOP
)
1829 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1830 else if (s
->state
== SERVICE_STOP_POST
)
1831 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1832 else if (s
->state
== SERVICE_RELOAD
) {
1833 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1834 service_enter_running(s
, SERVICE_SUCCESS
);
1836 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1839 static void service_run_next_main(Service
*s
) {
1844 assert(s
->main_command
);
1845 assert(s
->main_command
->command_next
);
1846 assert(s
->type
== SERVICE_ONESHOT
);
1848 s
->main_command
= s
->main_command
->command_next
;
1849 service_unwatch_main_pid(s
);
1851 r
= service_spawn(s
,
1853 s
->timeout_start_usec
,
1863 service_set_main_pid(s
, pid
);
1868 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1869 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1872 static int service_start_limit_test(Service
*s
) {
1875 if (ratelimit_test(&s
->start_limit
))
1878 log_unit_warning(UNIT(s
), "Start request repeated too quickly.");
1880 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
1883 static int service_start(Unit
*u
) {
1884 Service
*s
= SERVICE(u
);
1889 /* We cannot fulfill this request right now, try again later
1891 if (IN_SET(s
->state
,
1892 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1893 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1896 /* Already on it! */
1897 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1900 /* A service that will be restarted must be stopped first to
1901 * trigger BindsTo and/or OnFailure dependencies. If a user
1902 * does not want to wait for the holdoff time to elapse, the
1903 * service should be manually restarted, not started. We
1904 * simply return EAGAIN here, so that any start jobs stay
1905 * queued, and assume that the auto restart timer will
1906 * eventually trigger the restart. */
1907 if (s
->state
== SERVICE_AUTO_RESTART
)
1910 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1912 /* Make sure we don't enter a busy loop of some kind. */
1913 r
= service_start_limit_test(s
);
1915 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
1919 s
->result
= SERVICE_SUCCESS
;
1920 s
->reload_result
= SERVICE_SUCCESS
;
1921 s
->main_pid_known
= false;
1922 s
->main_pid_alien
= false;
1923 s
->forbid_restart
= false;
1924 s
->reset_cpu_usage
= true;
1926 free(s
->status_text
);
1927 s
->status_text
= NULL
;
1928 s
->status_errno
= 0;
1930 s
->notify_state
= NOTIFY_UNKNOWN
;
1932 service_enter_start_pre(s
);
1936 static int service_stop(Unit
*u
) {
1937 Service
*s
= SERVICE(u
);
1941 /* Don't create restart jobs from manual stops. */
1942 s
->forbid_restart
= true;
1945 if (IN_SET(s
->state
,
1946 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1947 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1950 /* A restart will be scheduled or is in progress. */
1951 if (s
->state
== SERVICE_AUTO_RESTART
) {
1952 service_set_state(s
, SERVICE_DEAD
);
1956 /* If there's already something running we go directly into
1958 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
1959 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1963 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
1965 service_enter_stop(s
, SERVICE_SUCCESS
);
1969 static int service_reload(Unit
*u
) {
1970 Service
*s
= SERVICE(u
);
1974 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1976 service_enter_reload(s
);
1980 _pure_
static bool service_can_reload(Unit
*u
) {
1981 Service
*s
= SERVICE(u
);
1985 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1988 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1989 Service
*s
= SERVICE(u
);
1996 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1997 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
1998 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2000 if (s
->control_pid
> 0)
2001 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2003 if (s
->main_pid_known
&& s
->main_pid
> 0)
2004 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2006 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2007 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2009 if (s
->status_text
) {
2010 _cleanup_free_
char *c
= NULL
;
2012 c
= cescape(s
->status_text
);
2013 unit_serialize_item(u
, f
, "status-text", strempty(c
));
2016 /* FIXME: There's a minor uncleanliness here: if there are
2017 * multiple commands attached here, we will start from the
2018 * first one again */
2019 if (s
->control_command_id
>= 0)
2020 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2022 if (s
->socket_fd
>= 0) {
2025 copy
= fdset_put_dup(fds
, s
->socket_fd
);
2029 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
2032 if (s
->bus_endpoint_fd
>= 0) {
2035 copy
= fdset_put_dup(fds
, s
->bus_endpoint_fd
);
2039 unit_serialize_item_format(u
, f
, "endpoint-fd", "%i", copy
);
2042 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2045 copy
= fdset_put_dup(fds
, fs
->fd
);
2049 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i", copy
);
2052 if (s
->main_exec_status
.pid
> 0) {
2053 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2054 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2055 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2057 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2058 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2059 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2063 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2064 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2066 if (s
->forbid_restart
)
2067 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2072 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2073 Service
*s
= SERVICE(u
);
2081 if (streq(key
, "state")) {
2084 state
= service_state_from_string(value
);
2086 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2088 s
->deserialized_state
= state
;
2089 } else if (streq(key
, "result")) {
2092 f
= service_result_from_string(value
);
2094 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2095 else if (f
!= SERVICE_SUCCESS
)
2098 } else if (streq(key
, "reload-result")) {
2101 f
= service_result_from_string(value
);
2103 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2104 else if (f
!= SERVICE_SUCCESS
)
2105 s
->reload_result
= f
;
2107 } else if (streq(key
, "control-pid")) {
2110 if (parse_pid(value
, &pid
) < 0)
2111 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2113 s
->control_pid
= pid
;
2114 } else if (streq(key
, "main-pid")) {
2117 if (parse_pid(value
, &pid
) < 0)
2118 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2120 service_set_main_pid(s
, pid
);
2121 unit_watch_pid(UNIT(s
), pid
);
2123 } else if (streq(key
, "main-pid-known")) {
2126 b
= parse_boolean(value
);
2128 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2130 s
->main_pid_known
= b
;
2131 } else if (streq(key
, "bus-name-good")) {
2134 b
= parse_boolean(value
);
2136 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2138 s
->bus_name_good
= b
;
2139 } else if (streq(key
, "status-text")) {
2142 r
= cunescape(value
, 0, &t
);
2144 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2146 free(s
->status_text
);
2150 } else if (streq(key
, "control-command")) {
2151 ServiceExecCommand id
;
2153 id
= service_exec_command_from_string(value
);
2155 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2157 s
->control_command_id
= id
;
2158 s
->control_command
= s
->exec_command
[id
];
2160 } else if (streq(key
, "socket-fd")) {
2163 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2164 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2166 asynchronous_close(s
->socket_fd
);
2167 s
->socket_fd
= fdset_remove(fds
, fd
);
2169 } else if (streq(key
, "endpoint-fd")) {
2172 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2173 log_unit_debug(u
, "Failed to parse endpoint-fd value: %s", value
);
2175 safe_close(s
->bus_endpoint_fd
);
2176 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2178 } else if (streq(key
, "fd-store-fd")) {
2181 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2182 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2184 r
= service_add_fd_store(s
, fd
);
2186 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2188 fdset_remove(fds
, fd
);
2191 } else if (streq(key
, "main-exec-status-pid")) {
2194 if (parse_pid(value
, &pid
) < 0)
2195 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2197 s
->main_exec_status
.pid
= pid
;
2198 } else if (streq(key
, "main-exec-status-code")) {
2201 if (safe_atoi(value
, &i
) < 0)
2202 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2204 s
->main_exec_status
.code
= i
;
2205 } else if (streq(key
, "main-exec-status-status")) {
2208 if (safe_atoi(value
, &i
) < 0)
2209 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2211 s
->main_exec_status
.status
= i
;
2212 } else if (streq(key
, "main-exec-status-start"))
2213 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2214 else if (streq(key
, "main-exec-status-exit"))
2215 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2216 else if (streq(key
, "watchdog-timestamp"))
2217 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2218 else if (streq(key
, "forbid-restart")) {
2221 b
= parse_boolean(value
);
2223 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2225 s
->forbid_restart
= b
;
2227 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2232 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2233 const UnitActiveState
*table
;
2237 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2239 return table
[SERVICE(u
)->state
];
2242 static const char *service_sub_state_to_string(Unit
*u
) {
2245 return service_state_to_string(SERVICE(u
)->state
);
2248 static bool service_check_gc(Unit
*u
) {
2249 Service
*s
= SERVICE(u
);
2253 /* Never clean up services that still have a process around,
2254 * even if the service is formally dead. */
2255 if (cgroup_good(s
) > 0 ||
2256 main_pid_good(s
) > 0 ||
2257 control_pid_good(s
) > 0)
2263 _pure_
static bool service_check_snapshot(Unit
*u
) {
2264 Service
*s
= SERVICE(u
);
2268 return s
->socket_fd
< 0;
2271 static int service_retry_pid_file(Service
*s
) {
2274 assert(s
->pid_file
);
2275 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2277 r
= service_load_pid_file(s
, false);
2281 service_unwatch_pid_file(s
);
2283 service_enter_running(s
, SERVICE_SUCCESS
);
2287 static int service_watch_pid_file(Service
*s
) {
2290 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2292 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2296 /* the pidfile might have appeared just before we set the watch */
2297 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2298 service_retry_pid_file(s
);
2302 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2303 service_unwatch_pid_file(s
);
2307 static int service_demand_pid_file(Service
*s
) {
2310 assert(s
->pid_file
);
2311 assert(!s
->pid_file_pathspec
);
2313 ps
= new0(PathSpec
, 1);
2318 ps
->path
= strdup(s
->pid_file
);
2324 path_kill_slashes(ps
->path
);
2326 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2327 * keep their PID file open all the time. */
2328 ps
->type
= PATH_MODIFIED
;
2329 ps
->inotify_fd
= -1;
2331 s
->pid_file_pathspec
= ps
;
2333 return service_watch_pid_file(s
);
2336 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2337 PathSpec
*p
= userdata
;
2342 s
= SERVICE(p
->unit
);
2346 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2347 assert(s
->pid_file_pathspec
);
2348 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2350 log_unit_debug(UNIT(s
), "inotify event");
2352 if (path_spec_fd_event(p
, events
) < 0)
2355 if (service_retry_pid_file(s
) == 0)
2358 if (service_watch_pid_file(s
) < 0)
2364 service_unwatch_pid_file(s
);
2365 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2369 static void service_notify_cgroup_empty_event(Unit
*u
) {
2370 Service
*s
= SERVICE(u
);
2374 log_unit_debug(u
, "cgroup is empty");
2378 /* Waiting for SIGCHLD is usually more interesting,
2379 * because it includes return codes/signals. Which is
2380 * why we ignore the cgroup events for most cases,
2381 * except when we don't know pid which to expect the
2385 case SERVICE_START_POST
:
2386 /* If we were hoping for the daemon to write its PID file,
2387 * we can give up now. */
2388 if (s
->pid_file_pathspec
) {
2389 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2391 service_unwatch_pid_file(s
);
2392 if (s
->state
== SERVICE_START
)
2393 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2395 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2399 case SERVICE_RUNNING
:
2400 /* service_enter_running() will figure out what to do */
2401 service_enter_running(s
, SERVICE_SUCCESS
);
2404 case SERVICE_STOP_SIGABRT
:
2405 case SERVICE_STOP_SIGTERM
:
2406 case SERVICE_STOP_SIGKILL
:
2408 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2409 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2413 case SERVICE_STOP_POST
:
2414 case SERVICE_FINAL_SIGTERM
:
2415 case SERVICE_FINAL_SIGKILL
:
2416 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2417 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2426 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2427 Service
*s
= SERVICE(u
);
2433 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2434 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2435 f
= SERVICE_SUCCESS
;
2436 else if (code
== CLD_EXITED
)
2437 f
= SERVICE_FAILURE_EXIT_CODE
;
2438 else if (code
== CLD_KILLED
)
2439 f
= SERVICE_FAILURE_SIGNAL
;
2440 else if (code
== CLD_DUMPED
)
2441 f
= SERVICE_FAILURE_CORE_DUMP
;
2443 assert_not_reached("Unknown code");
2445 if (s
->main_pid
== pid
) {
2446 /* Forking services may occasionally move to a new PID.
2447 * As long as they update the PID file before exiting the old
2448 * PID, they're fine. */
2449 if (service_load_pid_file(s
, false) == 0)
2453 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2455 if (s
->main_command
) {
2456 /* If this is not a forking service than the
2457 * main process got started and hence we copy
2458 * the exit status so that it is recorded both
2459 * as main and as control process exit
2462 s
->main_command
->exec_status
= s
->main_exec_status
;
2464 if (s
->main_command
->ignore
)
2465 f
= SERVICE_SUCCESS
;
2466 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2468 /* If this is a forked process, then we should
2469 * ignore the return value if this was
2470 * configured for the starter process */
2472 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2473 f
= SERVICE_SUCCESS
;
2476 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2478 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2479 sigchld_code_to_string(code
), status
,
2480 strna(code
== CLD_EXITED
2481 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2482 : signal_to_string(status
))),
2483 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2484 "EXIT_STATUS=%i", status
,
2487 if (f
!= SERVICE_SUCCESS
)
2490 if (s
->main_command
&&
2491 s
->main_command
->command_next
&&
2492 f
== SERVICE_SUCCESS
) {
2494 /* There is another command to *
2495 * execute, so let's do that. */
2497 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2498 service_run_next_main(s
);
2502 /* The service exited, so the service is officially
2504 s
->main_command
= NULL
;
2508 case SERVICE_START_POST
:
2509 case SERVICE_RELOAD
:
2511 /* Need to wait until the operation is
2516 if (s
->type
== SERVICE_ONESHOT
) {
2517 /* This was our main goal, so let's go on */
2518 if (f
== SERVICE_SUCCESS
)
2519 service_enter_start_post(s
);
2521 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2527 case SERVICE_RUNNING
:
2528 service_enter_running(s
, f
);
2531 case SERVICE_STOP_SIGABRT
:
2532 case SERVICE_STOP_SIGTERM
:
2533 case SERVICE_STOP_SIGKILL
:
2535 if (!control_pid_good(s
))
2536 service_enter_stop_post(s
, f
);
2538 /* If there is still a control process, wait for that first */
2541 case SERVICE_STOP_POST
:
2542 case SERVICE_FINAL_SIGTERM
:
2543 case SERVICE_FINAL_SIGKILL
:
2545 if (!control_pid_good(s
))
2546 service_enter_dead(s
, f
, true);
2550 assert_not_reached("Uh, main process died at wrong time.");
2554 } else if (s
->control_pid
== pid
) {
2557 if (s
->control_command
) {
2558 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2560 if (s
->control_command
->ignore
)
2561 f
= SERVICE_SUCCESS
;
2564 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2565 "Control process exited, code=%s status=%i",
2566 sigchld_code_to_string(code
), status
);
2568 if (f
!= SERVICE_SUCCESS
)
2571 /* Immediately get rid of the cgroup, so that the
2572 * kernel doesn't delay the cgroup empty messages for
2573 * the service cgroup any longer than necessary */
2574 service_kill_control_processes(s
);
2576 if (s
->control_command
&&
2577 s
->control_command
->command_next
&&
2578 f
== SERVICE_SUCCESS
) {
2580 /* There is another command to *
2581 * execute, so let's do that. */
2583 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2584 service_run_next_control(s
);
2587 /* No further commands for this step, so let's
2588 * figure out what to do next */
2590 s
->control_command
= NULL
;
2591 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2593 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2597 case SERVICE_START_PRE
:
2598 if (f
== SERVICE_SUCCESS
)
2599 service_enter_start(s
);
2601 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2605 if (s
->type
!= SERVICE_FORKING
)
2606 /* Maybe spurious event due to a reload that changed the type? */
2609 if (f
!= SERVICE_SUCCESS
) {
2610 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2615 bool has_start_post
;
2618 /* Let's try to load the pid file here if we can.
2619 * The PID file might actually be created by a START_POST
2620 * script. In that case don't worry if the loading fails. */
2622 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2623 r
= service_load_pid_file(s
, !has_start_post
);
2624 if (!has_start_post
&& r
< 0) {
2625 r
= service_demand_pid_file(s
);
2626 if (r
< 0 || !cgroup_good(s
))
2627 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2631 service_search_main_pid(s
);
2633 service_enter_start_post(s
);
2636 case SERVICE_START_POST
:
2637 if (f
!= SERVICE_SUCCESS
) {
2638 service_enter_stop(s
, f
);
2645 r
= service_load_pid_file(s
, true);
2647 r
= service_demand_pid_file(s
);
2648 if (r
< 0 || !cgroup_good(s
))
2649 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2653 service_search_main_pid(s
);
2655 service_enter_running(s
, SERVICE_SUCCESS
);
2658 case SERVICE_RELOAD
:
2659 if (f
== SERVICE_SUCCESS
) {
2660 service_load_pid_file(s
, true);
2661 service_search_main_pid(s
);
2664 s
->reload_result
= f
;
2665 service_enter_running(s
, SERVICE_SUCCESS
);
2669 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2672 case SERVICE_STOP_SIGABRT
:
2673 case SERVICE_STOP_SIGTERM
:
2674 case SERVICE_STOP_SIGKILL
:
2675 if (main_pid_good(s
) <= 0)
2676 service_enter_stop_post(s
, f
);
2678 /* If there is still a service
2679 * process around, wait until
2680 * that one quit, too */
2683 case SERVICE_STOP_POST
:
2684 case SERVICE_FINAL_SIGTERM
:
2685 case SERVICE_FINAL_SIGKILL
:
2686 if (main_pid_good(s
) <= 0)
2687 service_enter_dead(s
, f
, true);
2691 assert_not_reached("Uh, control process died at wrong time.");
2696 /* Notify clients about changed exit status */
2697 unit_add_to_dbus_queue(u
);
2699 /* We got one SIGCHLD for the service, let's watch all
2700 * processes that are now running of the service, and watch
2701 * that. Among the PIDs we then watch will be children
2702 * reassigned to us, which hopefully allows us to identify
2703 * when all children are gone */
2704 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2705 unit_watch_all_pids(u
);
2707 /* If the PID set is empty now, then let's finish this off */
2708 if (set_isempty(u
->pids
))
2709 service_notify_cgroup_empty_event(u
);
2712 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2713 Service
*s
= SERVICE(userdata
);
2716 assert(source
== s
->timer_event_source
);
2720 case SERVICE_START_PRE
:
2722 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2723 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2726 case SERVICE_START_POST
:
2727 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2728 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2731 case SERVICE_RELOAD
:
2732 log_unit_warning(UNIT(s
), "Reload operation timed out. Stopping.");
2733 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2734 service_enter_running(s
, SERVICE_SUCCESS
);
2738 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2739 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2742 case SERVICE_STOP_SIGABRT
:
2743 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2744 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2747 case SERVICE_STOP_SIGTERM
:
2748 if (s
->kill_context
.send_sigkill
) {
2749 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2750 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2752 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2753 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2758 case SERVICE_STOP_SIGKILL
:
2759 /* Uh, we sent a SIGKILL and it is still not gone?
2760 * Must be something we cannot kill, so let's just be
2761 * weirded out and continue */
2763 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2764 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2767 case SERVICE_STOP_POST
:
2768 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2769 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2772 case SERVICE_FINAL_SIGTERM
:
2773 if (s
->kill_context
.send_sigkill
) {
2774 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2775 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2777 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2778 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2783 case SERVICE_FINAL_SIGKILL
:
2784 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2785 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2788 case SERVICE_AUTO_RESTART
:
2789 log_unit_info(UNIT(s
),
2790 s
->restart_usec
> 0 ?
2791 "Service hold-off time over, scheduling restart." :
2792 "Service has no hold-off time, scheduling restart.");
2793 service_enter_restart(s
);
2797 assert_not_reached("Timeout at wrong time.");
2803 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2804 Service
*s
= SERVICE(userdata
);
2805 char t
[FORMAT_TIMESPAN_MAX
];
2808 assert(source
== s
->watchdog_event_source
);
2810 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2811 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2813 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2818 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2819 Service
*s
= SERVICE(u
);
2820 _cleanup_free_
char *cc
= NULL
;
2821 bool notify_dbus
= false;
2826 cc
= strv_join(tags
, ", ");
2828 if (s
->notify_access
== NOTIFY_NONE
) {
2829 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2831 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2832 if (s
->main_pid
!= 0)
2833 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2835 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2838 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2840 /* Interpret MAINPID= */
2841 e
= strv_find_startswith(tags
, "MAINPID=");
2842 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2843 if (parse_pid(e
, &pid
) < 0)
2844 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2846 service_set_main_pid(s
, pid
);
2847 unit_watch_pid(UNIT(s
), pid
);
2852 /* Interpret RELOADING= */
2853 if (strv_find(tags
, "RELOADING=1")) {
2855 s
->notify_state
= NOTIFY_RELOADING
;
2857 if (s
->state
== SERVICE_RUNNING
)
2858 service_enter_reload_by_notify(s
);
2863 /* Interpret READY= */
2864 if (strv_find(tags
, "READY=1")) {
2866 s
->notify_state
= NOTIFY_READY
;
2868 /* Type=notify services inform us about completed
2869 * initialization with READY=1 */
2870 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2871 service_enter_start_post(s
);
2873 /* Sending READY=1 while we are reloading informs us
2874 * that the reloading is complete */
2875 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2876 service_enter_running(s
, SERVICE_SUCCESS
);
2881 /* Interpret STOPPING= */
2882 if (strv_find(tags
, "STOPPING=1")) {
2884 s
->notify_state
= NOTIFY_STOPPING
;
2886 if (s
->state
== SERVICE_RUNNING
)
2887 service_enter_stop_by_notify(s
);
2892 /* Interpret STATUS= */
2893 e
= strv_find_startswith(tags
, "STATUS=");
2895 _cleanup_free_
char *t
= NULL
;
2898 if (!utf8_is_valid(e
))
2899 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
2907 if (!streq_ptr(s
->status_text
, t
)) {
2909 free(s
->status_text
);
2917 /* Interpret ERRNO= */
2918 e
= strv_find_startswith(tags
, "ERRNO=");
2922 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
2923 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
2925 if (s
->status_errno
!= status_errno
) {
2926 s
->status_errno
= status_errno
;
2932 /* Interpret WATCHDOG= */
2933 if (strv_find(tags
, "WATCHDOG=1")) {
2934 service_reset_watchdog(s
);
2937 /* Add the passed fds to the fd store */
2938 if (strv_find(tags
, "FDSTORE=1")) {
2939 service_add_fd_store_set(s
, fds
);
2942 /* Notify clients about changed status or main pid */
2944 unit_add_to_dbus_queue(u
);
2947 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
2948 Service
*s
= SERVICE(u
);
2951 if (!s
->timer_event_source
)
2954 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
2961 static void service_bus_name_owner_change(
2964 const char *old_owner
,
2965 const char *new_owner
) {
2967 Service
*s
= SERVICE(u
);
2973 assert(streq(s
->bus_name
, name
));
2974 assert(old_owner
|| new_owner
);
2976 if (old_owner
&& new_owner
)
2977 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
2979 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
2981 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
2983 s
->bus_name_good
= !!new_owner
;
2985 if (s
->type
== SERVICE_DBUS
) {
2987 /* service_enter_running() will figure out what to
2989 if (s
->state
== SERVICE_RUNNING
)
2990 service_enter_running(s
, SERVICE_SUCCESS
);
2991 else if (s
->state
== SERVICE_START
&& new_owner
)
2992 service_enter_start_post(s
);
2994 } else if (new_owner
&&
2996 (s
->state
== SERVICE_START
||
2997 s
->state
== SERVICE_START_POST
||
2998 s
->state
== SERVICE_RUNNING
||
2999 s
->state
== SERVICE_RELOAD
)) {
3001 _cleanup_bus_creds_unref_ sd_bus_creds
*creds
= NULL
;
3004 /* Try to acquire PID from bus service */
3006 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3008 r
= sd_bus_creds_get_pid(creds
, &pid
);
3010 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3012 service_set_main_pid(s
, pid
);
3013 unit_watch_pid(UNIT(s
), pid
);
3018 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3019 _cleanup_free_
char *peer
= NULL
;
3025 /* This is called by the socket code when instantiating a new
3026 * service for a stream socket and the socket needs to be
3029 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3032 if (s
->socket_fd
>= 0)
3035 if (s
->state
!= SERVICE_DEAD
)
3038 if (getpeername_pretty(fd
, &peer
) >= 0) {
3040 if (UNIT(s
)->description
) {
3041 _cleanup_free_
char *a
;
3043 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3047 r
= unit_set_description(UNIT(s
), a
);
3049 r
= unit_set_description(UNIT(s
), peer
);
3056 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3058 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3060 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3063 static void service_reset_failed(Unit
*u
) {
3064 Service
*s
= SERVICE(u
);
3068 if (s
->state
== SERVICE_FAILED
)
3069 service_set_state(s
, SERVICE_DEAD
);
3071 s
->result
= SERVICE_SUCCESS
;
3072 s
->reload_result
= SERVICE_SUCCESS
;
3074 RATELIMIT_RESET(s
->start_limit
);
3077 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3078 Service
*s
= SERVICE(u
);
3080 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3083 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
3084 [SERVICE_DEAD
] = "dead",
3085 [SERVICE_START_PRE
] = "start-pre",
3086 [SERVICE_START
] = "start",
3087 [SERVICE_START_POST
] = "start-post",
3088 [SERVICE_RUNNING
] = "running",
3089 [SERVICE_EXITED
] = "exited",
3090 [SERVICE_RELOAD
] = "reload",
3091 [SERVICE_STOP
] = "stop",
3092 [SERVICE_STOP_SIGABRT
] = "stop-sigabrt",
3093 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3094 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3095 [SERVICE_STOP_POST
] = "stop-post",
3096 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3097 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3098 [SERVICE_FAILED
] = "failed",
3099 [SERVICE_AUTO_RESTART
] = "auto-restart",
3102 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3104 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3105 [SERVICE_RESTART_NO
] = "no",
3106 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3107 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3108 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3109 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3110 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3111 [SERVICE_RESTART_ALWAYS
] = "always",
3114 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3116 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3117 [SERVICE_SIMPLE
] = "simple",
3118 [SERVICE_FORKING
] = "forking",
3119 [SERVICE_ONESHOT
] = "oneshot",
3120 [SERVICE_DBUS
] = "dbus",
3121 [SERVICE_NOTIFY
] = "notify",
3122 [SERVICE_IDLE
] = "idle"
3125 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3127 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3128 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3129 [SERVICE_EXEC_START
] = "ExecStart",
3130 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3131 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3132 [SERVICE_EXEC_STOP
] = "ExecStop",
3133 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3136 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3138 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3139 [NOTIFY_NONE
] = "none",
3140 [NOTIFY_MAIN
] = "main",
3141 [NOTIFY_ALL
] = "all"
3144 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3146 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3147 [NOTIFY_UNKNOWN
] = "unknown",
3148 [NOTIFY_READY
] = "ready",
3149 [NOTIFY_RELOADING
] = "reloading",
3150 [NOTIFY_STOPPING
] = "stopping",
3153 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3155 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3156 [SERVICE_SUCCESS
] = "success",
3157 [SERVICE_FAILURE_RESOURCES
] = "resources",
3158 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3159 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3160 [SERVICE_FAILURE_SIGNAL
] = "signal",
3161 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3162 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3163 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3166 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3168 const UnitVTable service_vtable
= {
3169 .object_size
= sizeof(Service
),
3170 .exec_context_offset
= offsetof(Service
, exec_context
),
3171 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3172 .kill_context_offset
= offsetof(Service
, kill_context
),
3173 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3179 .private_section
= "Service",
3181 .init
= service_init
,
3182 .done
= service_done
,
3183 .load
= service_load
,
3184 .release_resources
= service_release_resources
,
3186 .coldplug
= service_coldplug
,
3188 .dump
= service_dump
,
3190 .start
= service_start
,
3191 .stop
= service_stop
,
3192 .reload
= service_reload
,
3194 .can_reload
= service_can_reload
,
3196 .kill
= service_kill
,
3198 .serialize
= service_serialize
,
3199 .deserialize_item
= service_deserialize_item
,
3201 .active_state
= service_active_state
,
3202 .sub_state_to_string
= service_sub_state_to_string
,
3204 .check_gc
= service_check_gc
,
3205 .check_snapshot
= service_check_snapshot
,
3207 .sigchld_event
= service_sigchld_event
,
3209 .reset_failed
= service_reset_failed
,
3211 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3212 .notify_message
= service_notify_message
,
3214 .bus_name_owner_change
= service_bus_name_owner_change
,
3216 .bus_interface
= "org.freedesktop.systemd1.Service",
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.",