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"
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
)->id
, "Stopping watch for %s's PID file %s", UNIT(s
)->id
, 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
)->id
, "%s: Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", UNIT(s
)->id
, 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
)->id
, r
, "%s failed to reset watchdog timer: %m", UNIT(s
)->id
);
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
)->id
, r
, "%s failed to add watchdog timer: %m", UNIT(s
)->id
);
230 /* Let's process everything else which might be a sign
231 * of living before we consider a service died. */
232 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
236 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to install watchdog timer: %m", UNIT(s
)->id
);
239 static void service_reset_watchdog(Service
*s
) {
242 dual_timestamp_get(&s
->watchdog_timestamp
);
243 service_start_watchdog(s
);
246 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
252 assert(fs
->service
->n_fd_store
> 0);
253 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
254 fs
->service
->n_fd_store
--;
257 if (fs
->event_source
) {
258 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
259 sd_event_source_unref(fs
->event_source
);
266 static void service_release_resources(Unit
*u
) {
267 Service
*s
= SERVICE(u
);
274 log_debug("Releasing all resources for %s", u
->id
);
277 service_fd_store_unlink(s
->fd_store
);
279 assert(s
->n_fd_store
== 0);
282 static void service_done(Unit
*u
) {
283 Service
*s
= SERVICE(u
);
290 free(s
->status_text
);
291 s
->status_text
= NULL
;
294 s
->reboot_arg
= NULL
;
296 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
297 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
298 s
->control_command
= NULL
;
299 s
->main_command
= NULL
;
301 exit_status_set_free(&s
->restart_prevent_status
);
302 exit_status_set_free(&s
->restart_force_status
);
303 exit_status_set_free(&s
->success_status
);
305 /* This will leak a process, but at least no memory or any of
307 service_unwatch_main_pid(s
);
308 service_unwatch_control_pid(s
);
309 service_unwatch_pid_file(s
);
312 unit_unwatch_bus_name(u
, s
->bus_name
);
317 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
318 service_close_socket_fd(s
);
319 service_connection_unref(s
);
321 unit_ref_unset(&s
->accept_socket
);
323 service_stop_watchdog(s
);
325 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
327 service_release_resources(u
);
330 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
331 ServiceFDStore
*fs
= userdata
;
336 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
337 service_fd_store_unlink(fs
);
341 static int service_add_fd_store(Service
*s
, int fd
) {
348 if (s
->n_fd_store
>= s
->n_fd_store_max
)
351 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
352 r
= same_fd(fs
->fd
, fd
);
356 /* Already included */
362 fs
= new0(ServiceFDStore
, 1);
369 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
375 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
381 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
) {
386 if (fdset_size(fds
) <= 0)
389 while (s
->n_fd_store
< s
->n_fd_store_max
) {
390 _cleanup_close_
int fd
= -1;
392 fd
= fdset_steal_first(fds
);
396 r
= service_add_fd_store(s
, fd
);
398 return log_unit_error_errno(UNIT(s
)->id
, r
, "%s: Couldn't add fd to fd store: %m", UNIT(s
)->id
);
401 log_unit_debug(UNIT(s
)->id
, "%s: added fd to fd store.", UNIT(s
)->id
);
406 if (fdset_size(fds
) > 0)
407 log_unit_warning(UNIT(s
)->id
, "%s: tried to store more fds than FDStoreMax=%u allows, closing remaining.", UNIT(s
)->id
, s
->n_fd_store_max
);
412 static int service_arm_timer(Service
*s
, usec_t usec
) {
417 if (s
->timer_event_source
) {
418 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
422 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
425 return sd_event_add_time(
426 UNIT(s
)->manager
->event
,
427 &s
->timer_event_source
,
429 now(CLOCK_MONOTONIC
) + usec
, 0,
430 service_dispatch_timer
, s
);
433 static int service_verify(Service
*s
) {
436 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
439 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
440 log_unit_error(UNIT(s
)->id
, "%s lacks both ExecStart= and ExecStop= setting. Refusing.", UNIT(s
)->id
);
444 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
445 log_unit_error(UNIT(s
)->id
, "%s has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
449 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
450 log_unit_error(UNIT(s
)->id
, "%s has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.", UNIT(s
)->id
);
454 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
455 log_unit_error(UNIT(s
)->id
, "%s has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
459 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
460 log_unit_error(UNIT(s
)->id
, "%s has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
464 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
465 log_unit_error(UNIT(s
)->id
, "%s has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.", UNIT(s
)->id
);
469 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
470 log_unit_error(UNIT(s
)->id
, "%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s
)->id
);
474 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
475 log_unit_warning(UNIT(s
)->id
, "%s has a D-Bus service name specified, but is not of type dbus. Ignoring.", UNIT(s
)->id
);
477 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
478 log_unit_error(UNIT(s
)->id
, "%s has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.", UNIT(s
)->id
);
485 static int service_add_default_dependencies(Service
*s
) {
490 /* Add a number of automatic dependencies useful for the
491 * majority of services. */
493 /* First, pull in base system */
494 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
498 /* Second, activate normal shutdown */
499 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
502 static void service_fix_output(Service
*s
) {
505 /* If nothing has been explicitly configured, patch default
506 * output in. If input is socket/tty we avoid this however,
507 * since in that case we want output to default to the same
508 * place as we read input from. */
510 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
511 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
512 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
513 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
515 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
516 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
517 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
520 static int service_add_extras(Service
*s
) {
525 if (s
->type
== _SERVICE_TYPE_INVALID
) {
526 /* Figure out a type automatically */
528 s
->type
= SERVICE_DBUS
;
529 else if (s
->exec_command
[SERVICE_EXEC_START
])
530 s
->type
= SERVICE_SIMPLE
;
532 s
->type
= SERVICE_ONESHOT
;
535 /* Oneshot services have disabled start timeout by default */
536 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
537 s
->timeout_start_usec
= 0;
539 service_fix_output(s
);
541 r
= unit_patch_contexts(UNIT(s
));
545 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
549 r
= unit_add_default_slice(UNIT(s
), &s
->cgroup_context
);
553 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
554 s
->notify_access
= NOTIFY_MAIN
;
556 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
557 s
->notify_access
= NOTIFY_MAIN
;
563 n
= strjoina(s
->bus_name
, ".busname");
564 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
569 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
574 if (UNIT(s
)->default_dependencies
) {
575 r
= service_add_default_dependencies(s
);
583 static int service_load(Unit
*u
) {
584 Service
*s
= SERVICE(u
);
589 /* Load a .service file */
590 r
= unit_load_fragment(u
);
594 /* Still nothing found? Then let's give up */
595 if (u
->load_state
== UNIT_STUB
)
598 /* This is a new unit? Then let's add in some extras */
599 if (u
->load_state
== UNIT_LOADED
) {
601 /* We were able to load something, then let's add in
602 * the dropin directories. */
603 r
= unit_load_dropin(u
);
607 /* This is a new unit? Then let's add in some
609 r
= service_add_extras(s
);
614 return service_verify(s
);
617 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
618 ServiceExecCommand c
;
619 Service
*s
= SERVICE(u
);
624 prefix
= strempty(prefix
);
625 prefix2
= strjoina(prefix
, "\t");
628 "%sService State: %s\n"
630 "%sReload Result: %s\n"
631 "%sPermissionsStartOnly: %s\n"
632 "%sRootDirectoryStartOnly: %s\n"
633 "%sRemainAfterExit: %s\n"
634 "%sGuessMainPID: %s\n"
637 "%sNotifyAccess: %s\n"
638 "%sNotifyState: %s\n",
639 prefix
, service_state_to_string(s
->state
),
640 prefix
, service_result_to_string(s
->result
),
641 prefix
, service_result_to_string(s
->reload_result
),
642 prefix
, yes_no(s
->permissions_start_only
),
643 prefix
, yes_no(s
->root_directory_start_only
),
644 prefix
, yes_no(s
->remain_after_exit
),
645 prefix
, yes_no(s
->guess_main_pid
),
646 prefix
, service_type_to_string(s
->type
),
647 prefix
, service_restart_to_string(s
->restart
),
648 prefix
, notify_access_to_string(s
->notify_access
),
649 prefix
, notify_state_to_string(s
->notify_state
));
651 if (s
->control_pid
> 0)
653 "%sControl PID: "PID_FMT
"\n",
654 prefix
, s
->control_pid
);
658 "%sMain PID: "PID_FMT
"\n"
659 "%sMain PID Known: %s\n"
660 "%sMain PID Alien: %s\n",
662 prefix
, yes_no(s
->main_pid_known
),
663 prefix
, yes_no(s
->main_pid_alien
));
668 prefix
, s
->pid_file
);
673 "%sBus Name Good: %s\n",
675 prefix
, yes_no(s
->bus_name_good
));
677 kill_context_dump(&s
->kill_context
, f
, prefix
);
678 exec_context_dump(&s
->exec_context
, f
, prefix
);
680 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
682 if (!s
->exec_command
[c
])
685 fprintf(f
, "%s-> %s:\n",
686 prefix
, service_exec_command_to_string(c
));
688 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
692 fprintf(f
, "%sStatus Text: %s\n",
693 prefix
, s
->status_text
);
695 if (s
->n_fd_store_max
> 0) {
697 "%sFile Descriptor Store Max: %u\n"
698 "%sFile Descriptor Store Current: %u\n",
699 prefix
, s
->n_fd_store_max
,
700 prefix
, s
->n_fd_store
);
704 static int service_load_pid_file(Service
*s
, bool may_warn
) {
705 _cleanup_free_
char *k
= NULL
;
714 r
= read_one_line_file(s
->pid_file
, &k
);
717 log_unit_info(UNIT(s
)->id
, "PID file %s not readable (yet?) after %s.", s
->pid_file
, service_state_to_string(s
->state
));
721 r
= parse_pid(k
, &pid
);
724 log_unit_info_errno(UNIT(s
)->id
, r
, "Failed to read PID from file %s: %m", s
->pid_file
);
728 if (!pid_is_alive(pid
)) {
730 log_unit_info(UNIT(s
)->id
, "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
734 if (s
->main_pid_known
) {
735 if (pid
== s
->main_pid
)
738 log_unit_debug(UNIT(s
)->id
, "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
740 service_unwatch_main_pid(s
);
741 s
->main_pid_known
= false;
743 log_unit_debug(UNIT(s
)->id
, "Main PID loaded: "PID_FMT
, pid
);
745 r
= service_set_main_pid(s
, pid
);
749 r
= unit_watch_pid(UNIT(s
), pid
);
751 /* FIXME: we need to do something here */
752 log_unit_warning(UNIT(s
)->id
, "Failed to watch PID "PID_FMT
" from service %s", pid
, UNIT(s
)->id
);
759 static int service_search_main_pid(Service
*s
) {
765 /* If we know it anyway, don't ever fallback to unreliable
767 if (s
->main_pid_known
)
770 if (!s
->guess_main_pid
)
773 assert(s
->main_pid
<= 0);
775 pid
= unit_search_main_pid(UNIT(s
));
779 log_unit_debug(UNIT(s
)->id
, "Main PID guessed: "PID_FMT
, pid
);
780 r
= service_set_main_pid(s
, pid
);
784 r
= unit_watch_pid(UNIT(s
), pid
);
786 /* FIXME: we need to do something here */
787 log_unit_warning(UNIT(s
)->id
, "Failed to watch PID "PID_FMT
" from service %s", pid
, UNIT(s
)->id
);
794 static void service_set_state(Service
*s
, ServiceState state
) {
795 ServiceState old_state
;
796 const UnitActiveState
*table
;
800 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
802 old_state
= s
->state
;
805 service_unwatch_pid_file(s
);
808 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
810 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
811 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
812 SERVICE_AUTO_RESTART
))
813 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
816 SERVICE_START
, SERVICE_START_POST
,
817 SERVICE_RUNNING
, SERVICE_RELOAD
,
818 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
819 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
820 service_unwatch_main_pid(s
);
821 s
->main_command
= NULL
;
825 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
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_control_pid(s
);
830 s
->control_command
= NULL
;
831 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
834 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
835 unit_unwatch_all_pids(UNIT(s
));
838 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
839 SERVICE_RUNNING
, SERVICE_RELOAD
,
840 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
841 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
842 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
843 service_close_socket_fd(s
);
844 service_connection_unref(s
);
847 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
848 service_stop_watchdog(s
);
850 /* For the inactive states unit_notify() will trim the cgroup,
851 * but for exit we have to do that ourselves... */
852 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
853 unit_destroy_cgroup_if_empty(UNIT(s
));
855 /* For remain_after_exit services, let's see if we can "release" the
856 * hold on the console, since unit_notify() only does that in case of
858 if (state
== SERVICE_EXITED
&&
859 s
->remain_after_exit
&&
860 UNIT(s
)->manager
->n_on_console
> 0) {
864 ec
= unit_get_exec_context(UNIT(s
));
865 if (ec
&& exec_context_may_touch_console(ec
)) {
866 Manager
*m
= UNIT(s
)->manager
;
869 if (m
->n_on_console
== 0)
870 /* unset no_console_output flag, since the console is free */
871 m
->no_console_output
= false;
875 if (old_state
!= state
)
876 log_unit_debug(UNIT(s
)->id
, "%s changed %s -> %s", UNIT(s
)->id
, service_state_to_string(old_state
), service_state_to_string(state
));
878 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
879 s
->reload_result
= SERVICE_SUCCESS
;
882 static int service_coldplug(Unit
*u
, Hashmap
*deferred_work
) {
883 Service
*s
= SERVICE(u
);
887 assert(s
->state
== SERVICE_DEAD
);
889 if (s
->deserialized_state
!= s
->state
) {
891 if (IN_SET(s
->deserialized_state
,
892 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
894 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
895 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
899 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
901 /* For the start/stop timeouts 0 means off */
903 r
= service_arm_timer(s
, k
);
909 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
911 /* The restart timeouts 0 means immediately */
912 r
= service_arm_timer(s
, s
->restart_usec
);
917 if (pid_is_unwaited(s
->main_pid
) &&
918 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
919 IN_SET(s
->deserialized_state
,
920 SERVICE_START
, SERVICE_START_POST
,
921 SERVICE_RUNNING
, SERVICE_RELOAD
,
922 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
923 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
924 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
929 if (pid_is_unwaited(s
->control_pid
) &&
930 IN_SET(s
->deserialized_state
,
931 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
933 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
934 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
935 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
940 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
941 unit_watch_all_pids(UNIT(s
));
943 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
944 service_start_watchdog(s
);
946 service_set_state(s
, s
->deserialized_state
);
952 static int service_collect_fds(Service
*s
, int **fds
, unsigned *n_fds
) {
953 _cleanup_free_
int *rfds
= NULL
;
963 if (s
->socket_fd
>= 0)
966 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
971 if (u
->type
!= UNIT_SOCKET
)
976 r
= socket_collect_fds(sock
, &cfds
, &cn_fds
);
991 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
997 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1006 if (s
->n_fd_store
> 0) {
1010 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1015 LIST_FOREACH(fd_store
, fs
, s
->fd_store
)
1016 rfds
[rn_fds
++] = fs
->fd
;
1026 static int service_spawn(
1031 bool apply_permissions
,
1033 bool apply_tty_stdin
,
1040 _cleanup_free_
int *fdsbuf
= NULL
;
1041 unsigned n_fds
= 0, n_env
= 0;
1042 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1043 _cleanup_strv_free_
char
1044 **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
;
1046 ExecParameters exec_params
= {
1047 .apply_permissions
= apply_permissions
,
1048 .apply_chroot
= apply_chroot
,
1049 .apply_tty_stdin
= apply_tty_stdin
,
1050 .bus_endpoint_fd
= -1,
1051 .selinux_context_net
= s
->socket_fd_selinux_context_net
1058 (void) unit_realize_cgroup(UNIT(s
));
1059 if (s
->reset_cpu_usage
) {
1060 (void) unit_reset_cpu_usage(UNIT(s
));
1061 s
->reset_cpu_usage
= false;
1064 r
= unit_setup_exec_runtime(UNIT(s
));
1069 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1070 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1071 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1073 if (s
->socket_fd
>= 0) {
1074 fds
= &s
->socket_fd
;
1077 r
= service_collect_fds(s
, &fdsbuf
, &n_fds
);
1086 r
= service_arm_timer(s
, timeout
);
1090 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1092 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1096 our_env
= new0(char*, 6);
1102 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1103 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1108 if (s
->main_pid
> 0)
1109 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1114 if (UNIT(s
)->manager
->running_as
!= SYSTEMD_SYSTEM
)
1115 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1120 if (UNIT_DEREF(s
->accept_socket
)) {
1121 union sockaddr_union sa
;
1122 socklen_t salen
= sizeof(sa
);
1124 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1130 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1131 _cleanup_free_
char *addr
= NULL
;
1135 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1139 t
= strappend("REMOTE_ADDR=", addr
);
1144 our_env
[n_env
++] = t
;
1146 port
= sockaddr_port(&sa
.sa
);
1152 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0) {
1156 our_env
[n_env
++] = t
;
1160 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1166 if (is_control
&& UNIT(s
)->cgroup_path
) {
1167 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1168 cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1170 path
= UNIT(s
)->cgroup_path
;
1173 if (s
->exec_context
.bus_endpoint
) {
1174 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== SYSTEMD_SYSTEM
? "system" : "user",
1175 UNIT(s
)->id
, &bus_endpoint_path
);
1179 /* Pass the fd to the exec_params so that the child process can upload the policy.
1180 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1181 * as the service is running. */
1182 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1186 exec_params
.argv
= argv
;
1187 exec_params
.fds
= fds
;
1188 exec_params
.n_fds
= n_fds
;
1189 exec_params
.environment
= final_env
;
1190 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1191 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1192 exec_params
.cgroup_path
= path
;
1193 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1194 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1195 exec_params
.unit_id
= UNIT(s
)->id
;
1196 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1197 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1198 if (s
->type
== SERVICE_IDLE
)
1199 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1209 r
= unit_watch_pid(UNIT(s
), pid
);
1211 /* FIXME: we need to do something here */
1220 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1225 static int main_pid_good(Service
*s
) {
1228 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1231 /* If we know the pid file, then lets just check if it is
1233 if (s
->main_pid_known
) {
1235 /* If it's an alien child let's check if it is still
1237 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1238 return pid_is_alive(s
->main_pid
);
1240 /* .. otherwise assume we'll get a SIGCHLD for it,
1241 * which we really should wait for to collect exit
1242 * status and code */
1243 return s
->main_pid
> 0;
1246 /* We don't know the pid */
1250 _pure_
static int control_pid_good(Service
*s
) {
1253 return s
->control_pid
> 0;
1256 static int cgroup_good(Service
*s
) {
1261 if (!UNIT(s
)->cgroup_path
)
1264 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
, true);
1271 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1275 if (f
!= SERVICE_SUCCESS
)
1278 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1280 if (s
->result
!= SERVICE_SUCCESS
) {
1281 log_unit_warning(UNIT(s
)->id
, "%s failed.", UNIT(s
)->id
);
1282 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1285 if (allow_restart
&&
1286 !s
->forbid_restart
&&
1287 (s
->restart
== SERVICE_RESTART_ALWAYS
||
1288 (s
->restart
== SERVICE_RESTART_ON_SUCCESS
&& s
->result
== SERVICE_SUCCESS
) ||
1289 (s
->restart
== SERVICE_RESTART_ON_FAILURE
&& s
->result
!= SERVICE_SUCCESS
) ||
1290 (s
->restart
== SERVICE_RESTART_ON_ABNORMAL
&& !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
)) ||
1291 (s
->restart
== SERVICE_RESTART_ON_WATCHDOG
&& s
->result
== SERVICE_FAILURE_WATCHDOG
) ||
1292 (s
->restart
== SERVICE_RESTART_ON_ABORT
&& IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
)) ||
1293 (s
->main_exec_status
.code
== CLD_EXITED
&& set_contains(s
->restart_force_status
.status
, INT_TO_PTR(s
->main_exec_status
.status
))) ||
1294 (IN_SET(s
->main_exec_status
.code
, CLD_KILLED
, CLD_DUMPED
) && set_contains(s
->restart_force_status
.signal
, INT_TO_PTR(s
->main_exec_status
.status
)))) &&
1295 (s
->main_exec_status
.code
!= CLD_EXITED
|| !set_contains(s
->restart_prevent_status
.status
, INT_TO_PTR(s
->main_exec_status
.status
))) &&
1296 (!IN_SET(s
->main_exec_status
.code
, CLD_KILLED
, CLD_DUMPED
) || !set_contains(s
->restart_prevent_status
.signal
, INT_TO_PTR(s
->main_exec_status
.status
)))) {
1298 r
= service_arm_timer(s
, s
->restart_usec
);
1302 service_set_state(s
, SERVICE_AUTO_RESTART
);
1305 s
->forbid_restart
= false;
1307 /* We want fresh tmpdirs in case service is started again immediately */
1308 exec_runtime_destroy(s
->exec_runtime
);
1309 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1311 /* Also, remove the runtime directory in */
1312 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1314 /* Try to delete the pid file. At this point it will be
1315 * out-of-date, and some software might be confused by it, so
1316 * let's remove it. */
1318 unlink_noerrno(s
->pid_file
);
1323 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run install restart timer: %m", UNIT(s
)->id
);
1324 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1327 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1331 if (f
!= SERVICE_SUCCESS
)
1334 service_unwatch_control_pid(s
);
1335 unit_watch_all_pids(UNIT(s
));
1337 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1338 if (s
->control_command
) {
1339 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1341 r
= service_spawn(s
,
1343 s
->timeout_stop_usec
,
1345 !s
->permissions_start_only
,
1346 !s
->root_directory_start_only
,
1353 service_set_state(s
, SERVICE_STOP_POST
);
1355 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1360 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'stop-post' task: %m", UNIT(s
)->id
);
1361 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1364 static int state_to_kill_operation(ServiceState state
) {
1367 case SERVICE_STOP_SIGABRT
:
1370 case SERVICE_STOP_SIGTERM
:
1371 case SERVICE_FINAL_SIGTERM
:
1372 return KILL_TERMINATE
;
1374 case SERVICE_STOP_SIGKILL
:
1375 case SERVICE_FINAL_SIGKILL
:
1379 return _KILL_OPERATION_INVALID
;
1383 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1388 if (f
!= SERVICE_SUCCESS
)
1391 unit_watch_all_pids(UNIT(s
));
1393 r
= unit_kill_context(
1396 state_to_kill_operation(state
),
1405 if (s
->timeout_stop_usec
> 0) {
1406 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1411 service_set_state(s
, state
);
1412 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1413 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1414 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1415 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1416 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1417 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1419 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1424 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to kill processes: %m", UNIT(s
)->id
);
1426 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1427 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1429 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1432 static void service_enter_stop_by_notify(Service
*s
) {
1435 unit_watch_all_pids(UNIT(s
));
1437 if (s
->timeout_stop_usec
> 0)
1438 service_arm_timer(s
, s
->timeout_stop_usec
);
1440 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1441 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1444 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1449 if (f
!= SERVICE_SUCCESS
)
1452 service_unwatch_control_pid(s
);
1453 unit_watch_all_pids(UNIT(s
));
1455 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1456 if (s
->control_command
) {
1457 s
->control_command_id
= SERVICE_EXEC_STOP
;
1459 r
= service_spawn(s
,
1461 s
->timeout_stop_usec
,
1463 !s
->permissions_start_only
,
1464 !s
->root_directory_start_only
,
1471 service_set_state(s
, SERVICE_STOP
);
1473 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1478 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'stop' task: %m", UNIT(s
)->id
);
1479 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1482 static void service_enter_running(Service
*s
, ServiceResult f
) {
1483 int main_pid_ok
, cgroup_ok
;
1486 if (f
!= SERVICE_SUCCESS
)
1489 main_pid_ok
= main_pid_good(s
);
1490 cgroup_ok
= cgroup_good(s
);
1492 if ((main_pid_ok
> 0 || (main_pid_ok
< 0 && cgroup_ok
!= 0)) &&
1493 (s
->bus_name_good
|| s
->type
!= SERVICE_DBUS
)) {
1495 /* If there are any queued up sd_notify()
1496 * notifications, process them now */
1497 if (s
->notify_state
== NOTIFY_RELOADING
)
1498 service_enter_reload_by_notify(s
);
1499 else if (s
->notify_state
== NOTIFY_STOPPING
)
1500 service_enter_stop_by_notify(s
);
1502 service_set_state(s
, SERVICE_RUNNING
);
1504 } else if (s
->remain_after_exit
)
1505 service_set_state(s
, SERVICE_EXITED
);
1507 service_enter_stop(s
, SERVICE_SUCCESS
);
1510 static void service_enter_start_post(Service
*s
) {
1514 service_unwatch_control_pid(s
);
1515 service_reset_watchdog(s
);
1517 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1518 if (s
->control_command
) {
1519 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1521 r
= service_spawn(s
,
1523 s
->timeout_start_usec
,
1525 !s
->permissions_start_only
,
1526 !s
->root_directory_start_only
,
1533 service_set_state(s
, SERVICE_START_POST
);
1535 service_enter_running(s
, SERVICE_SUCCESS
);
1540 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'start-post' task: %m", UNIT(s
)->id
);
1541 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1544 static void service_kill_control_processes(Service
*s
) {
1547 if (!UNIT(s
)->cgroup_path
)
1550 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1551 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1554 static void service_enter_start(Service
*s
) {
1561 service_unwatch_control_pid(s
);
1562 service_unwatch_main_pid(s
);
1564 /* We want to ensure that nobody leaks processes from
1565 * START_PRE here, so let's go on a killing spree, People
1566 * should not spawn long running processes from START_PRE. */
1567 service_kill_control_processes(s
);
1569 if (s
->type
== SERVICE_FORKING
) {
1570 s
->control_command_id
= SERVICE_EXEC_START
;
1571 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1573 s
->main_command
= NULL
;
1575 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1576 s
->control_command
= NULL
;
1578 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1582 assert(s
->type
== SERVICE_ONESHOT
);
1583 service_enter_start_post(s
);
1587 r
= service_spawn(s
,
1589 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1599 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1600 /* For simple services we immediately start
1601 * the START_POST binaries. */
1603 service_set_main_pid(s
, pid
);
1604 service_enter_start_post(s
);
1606 } else if (s
->type
== SERVICE_FORKING
) {
1608 /* For forking services we wait until the start
1609 * process exited. */
1611 s
->control_pid
= pid
;
1612 service_set_state(s
, SERVICE_START
);
1614 } else if (s
->type
== SERVICE_ONESHOT
||
1615 s
->type
== SERVICE_DBUS
||
1616 s
->type
== SERVICE_NOTIFY
) {
1618 /* For oneshot services we wait until the start
1619 * process exited, too, but it is our main process. */
1621 /* For D-Bus services we know the main pid right away,
1622 * but wait for the bus name to appear on the
1623 * bus. Notify services are similar. */
1625 service_set_main_pid(s
, pid
);
1626 service_set_state(s
, SERVICE_START
);
1628 assert_not_reached("Unknown service type");
1633 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'start' task: %m", UNIT(s
)->id
);
1634 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1637 static void service_enter_start_pre(Service
*s
) {
1642 service_unwatch_control_pid(s
);
1644 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1645 if (s
->control_command
) {
1646 /* Before we start anything, let's clear up what might
1647 * be left from previous runs. */
1648 service_kill_control_processes(s
);
1650 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1652 r
= service_spawn(s
,
1654 s
->timeout_start_usec
,
1656 !s
->permissions_start_only
,
1657 !s
->root_directory_start_only
,
1664 service_set_state(s
, SERVICE_START_PRE
);
1666 service_enter_start(s
);
1671 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'start-pre' task: %m", UNIT(s
)->id
);
1672 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1675 static void service_enter_restart(Service
*s
) {
1676 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1681 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1682 /* Don't restart things if we are going down anyway */
1683 log_unit_info(UNIT(s
)->id
, "Stop job pending for unit, delaying automatic restart.");
1685 r
= service_arm_timer(s
, s
->restart_usec
);
1692 /* Any units that are bound to this service must also be
1693 * restarted. We use JOB_RESTART (instead of the more obvious
1694 * JOB_START) here so that those dependency jobs will be added
1696 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
1700 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1701 * it will be canceled as part of the service_stop() call that
1702 * is executed as part of JOB_RESTART. */
1704 log_unit_debug(UNIT(s
)->id
, "%s scheduled restart job.", UNIT(s
)->id
);
1708 log_unit_warning(UNIT(s
)->id
, "%s failed to schedule restart job: %s", UNIT(s
)->id
, bus_error_message(&error
, -r
));
1709 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1712 static void service_enter_reload_by_notify(Service
*s
) {
1715 if (s
->timeout_start_usec
> 0)
1716 service_arm_timer(s
, s
->timeout_start_usec
);
1718 service_set_state(s
, SERVICE_RELOAD
);
1721 static void service_enter_reload(Service
*s
) {
1726 service_unwatch_control_pid(s
);
1728 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1729 if (s
->control_command
) {
1730 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1732 r
= service_spawn(s
,
1734 s
->timeout_start_usec
,
1736 !s
->permissions_start_only
,
1737 !s
->root_directory_start_only
,
1744 service_set_state(s
, SERVICE_RELOAD
);
1746 service_enter_running(s
, SERVICE_SUCCESS
);
1751 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run 'reload' task: %m", UNIT(s
)->id
);
1752 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1753 service_enter_running(s
, SERVICE_SUCCESS
);
1756 static void service_run_next_control(Service
*s
) {
1760 assert(s
->control_command
);
1761 assert(s
->control_command
->command_next
);
1763 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1765 s
->control_command
= s
->control_command
->command_next
;
1766 service_unwatch_control_pid(s
);
1768 r
= service_spawn(s
,
1770 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1772 !s
->permissions_start_only
,
1773 !s
->root_directory_start_only
,
1774 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1775 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1784 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run next control task: %m", UNIT(s
)->id
);
1786 if (s
->state
== SERVICE_START_PRE
)
1787 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1788 else if (s
->state
== SERVICE_STOP
)
1789 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1790 else if (s
->state
== SERVICE_STOP_POST
)
1791 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1792 else if (s
->state
== SERVICE_RELOAD
) {
1793 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1794 service_enter_running(s
, SERVICE_SUCCESS
);
1796 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1799 static void service_run_next_main(Service
*s
) {
1804 assert(s
->main_command
);
1805 assert(s
->main_command
->command_next
);
1806 assert(s
->type
== SERVICE_ONESHOT
);
1808 s
->main_command
= s
->main_command
->command_next
;
1809 service_unwatch_main_pid(s
);
1811 r
= service_spawn(s
,
1813 s
->timeout_start_usec
,
1823 service_set_main_pid(s
, pid
);
1828 log_unit_warning_errno(UNIT(s
)->id
, r
, "%s failed to run next main task: %m", UNIT(s
)->id
);
1829 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1832 static int service_start_limit_test(Service
*s
) {
1835 if (ratelimit_test(&s
->start_limit
))
1838 log_unit_warning(UNIT(s
)->id
, "start request repeated too quickly for %s", UNIT(s
)->id
);
1840 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
1843 static int service_start(Unit
*u
) {
1844 Service
*s
= SERVICE(u
);
1849 /* We cannot fulfill this request right now, try again later
1851 if (IN_SET(s
->state
,
1852 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1853 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1856 /* Already on it! */
1857 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1860 /* A service that will be restarted must be stopped first to
1861 * trigger BindsTo and/or OnFailure dependencies. If a user
1862 * does not want to wait for the holdoff time to elapse, the
1863 * service should be manually restarted, not started. We
1864 * simply return EAGAIN here, so that any start jobs stay
1865 * queued, and assume that the auto restart timer will
1866 * eventually trigger the restart. */
1867 if (s
->state
== SERVICE_AUTO_RESTART
)
1870 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1872 /* Make sure we don't enter a busy loop of some kind. */
1873 r
= service_start_limit_test(s
);
1875 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
1879 s
->result
= SERVICE_SUCCESS
;
1880 s
->reload_result
= SERVICE_SUCCESS
;
1881 s
->main_pid_known
= false;
1882 s
->main_pid_alien
= false;
1883 s
->forbid_restart
= false;
1884 s
->reset_cpu_usage
= true;
1886 free(s
->status_text
);
1887 s
->status_text
= NULL
;
1888 s
->status_errno
= 0;
1890 s
->notify_state
= NOTIFY_UNKNOWN
;
1892 service_enter_start_pre(s
);
1896 static int service_stop(Unit
*u
) {
1897 Service
*s
= SERVICE(u
);
1901 /* Don't create restart jobs from here. */
1902 s
->forbid_restart
= true;
1905 if (IN_SET(s
->state
,
1906 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1907 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1910 /* A restart will be scheduled or is in progress. */
1911 if (s
->state
== SERVICE_AUTO_RESTART
) {
1912 service_set_state(s
, SERVICE_DEAD
);
1916 /* If there's already something running we go directly into
1918 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
1919 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1923 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
1925 service_enter_stop(s
, SERVICE_SUCCESS
);
1929 static int service_reload(Unit
*u
) {
1930 Service
*s
= SERVICE(u
);
1934 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
1936 service_enter_reload(s
);
1940 _pure_
static bool service_can_reload(Unit
*u
) {
1941 Service
*s
= SERVICE(u
);
1945 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
1948 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
1949 Service
*s
= SERVICE(u
);
1956 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
1957 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
1958 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
1960 if (s
->control_pid
> 0)
1961 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
1963 if (s
->main_pid_known
&& s
->main_pid
> 0)
1964 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
1966 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
1969 unit_serialize_item(u
, f
, "status-text", s
->status_text
);
1971 /* FIXME: There's a minor uncleanliness here: if there are
1972 * multiple commands attached here, we will start from the
1973 * first one again */
1974 if (s
->control_command_id
>= 0)
1975 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
1977 if (s
->socket_fd
>= 0) {
1980 copy
= fdset_put_dup(fds
, s
->socket_fd
);
1984 unit_serialize_item_format(u
, f
, "socket-fd", "%i", copy
);
1987 if (s
->bus_endpoint_fd
>= 0) {
1990 copy
= fdset_put_dup(fds
, s
->bus_endpoint_fd
);
1994 unit_serialize_item_format(u
, f
, "endpoint-fd", "%i", copy
);
1997 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2000 copy
= fdset_put_dup(fds
, fs
->fd
);
2004 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i", copy
);
2007 if (s
->main_exec_status
.pid
> 0) {
2008 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2009 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2010 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2012 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2013 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2014 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2018 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2019 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2021 if (s
->forbid_restart
)
2022 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2027 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2028 Service
*s
= SERVICE(u
);
2036 if (streq(key
, "state")) {
2039 state
= service_state_from_string(value
);
2041 log_unit_debug(u
->id
, "Failed to parse state value %s", value
);
2043 s
->deserialized_state
= state
;
2044 } else if (streq(key
, "result")) {
2047 f
= service_result_from_string(value
);
2049 log_unit_debug(u
->id
, "Failed to parse result value %s", value
);
2050 else if (f
!= SERVICE_SUCCESS
)
2053 } else if (streq(key
, "reload-result")) {
2056 f
= service_result_from_string(value
);
2058 log_unit_debug(u
->id
, "Failed to parse reload result value %s", value
);
2059 else if (f
!= SERVICE_SUCCESS
)
2060 s
->reload_result
= f
;
2062 } else if (streq(key
, "control-pid")) {
2065 if (parse_pid(value
, &pid
) < 0)
2066 log_unit_debug(u
->id
, "Failed to parse control-pid value %s", value
);
2068 s
->control_pid
= pid
;
2069 } else if (streq(key
, "main-pid")) {
2072 if (parse_pid(value
, &pid
) < 0)
2073 log_unit_debug(u
->id
, "Failed to parse main-pid value %s", value
);
2075 service_set_main_pid(s
, pid
);
2076 unit_watch_pid(UNIT(s
), pid
);
2078 } else if (streq(key
, "main-pid-known")) {
2081 b
= parse_boolean(value
);
2083 log_unit_debug(u
->id
, "Failed to parse main-pid-known value %s", value
);
2085 s
->main_pid_known
= b
;
2086 } else if (streq(key
, "status-text")) {
2093 free(s
->status_text
);
2097 } else if (streq(key
, "control-command")) {
2098 ServiceExecCommand id
;
2100 id
= service_exec_command_from_string(value
);
2102 log_unit_debug(u
->id
, "Failed to parse exec-command value %s", value
);
2104 s
->control_command_id
= id
;
2105 s
->control_command
= s
->exec_command
[id
];
2107 } else if (streq(key
, "socket-fd")) {
2110 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2111 log_unit_debug(u
->id
, "Failed to parse socket-fd value %s", value
);
2113 asynchronous_close(s
->socket_fd
);
2114 s
->socket_fd
= fdset_remove(fds
, fd
);
2116 } else if (streq(key
, "endpoint-fd")) {
2119 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2120 log_unit_debug(u
->id
, "Failed to parse endpoint-fd value %s", value
);
2122 safe_close(s
->bus_endpoint_fd
);
2123 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2125 } else if (streq(key
, "fd-store-fd")) {
2128 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2129 log_unit_debug(u
->id
, "Failed to parse fd-store-fd value %s", value
);
2131 r
= service_add_fd_store(s
, fd
);
2133 log_unit_error_errno(u
->id
, r
, "Failed to add fd to store: %m");
2135 fdset_remove(fds
, fd
);
2138 } else if (streq(key
, "main-exec-status-pid")) {
2141 if (parse_pid(value
, &pid
) < 0)
2142 log_unit_debug(u
->id
, "Failed to parse main-exec-status-pid value %s", value
);
2144 s
->main_exec_status
.pid
= pid
;
2145 } else if (streq(key
, "main-exec-status-code")) {
2148 if (safe_atoi(value
, &i
) < 0)
2149 log_unit_debug(u
->id
, "Failed to parse main-exec-status-code value %s", value
);
2151 s
->main_exec_status
.code
= i
;
2152 } else if (streq(key
, "main-exec-status-status")) {
2155 if (safe_atoi(value
, &i
) < 0)
2156 log_unit_debug(u
->id
, "Failed to parse main-exec-status-status value %s", value
);
2158 s
->main_exec_status
.status
= i
;
2159 } else if (streq(key
, "main-exec-status-start"))
2160 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2161 else if (streq(key
, "main-exec-status-exit"))
2162 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2163 else if (streq(key
, "watchdog-timestamp"))
2164 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2165 else if (streq(key
, "forbid-restart")) {
2168 b
= parse_boolean(value
);
2170 log_unit_debug(u
->id
, "Failed to parse forbid-restart value %s", value
);
2172 s
->forbid_restart
= b
;
2174 log_unit_debug(u
->id
, "Unknown serialization key '%s'", key
);
2179 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2180 const UnitActiveState
*table
;
2184 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2186 return table
[SERVICE(u
)->state
];
2189 static const char *service_sub_state_to_string(Unit
*u
) {
2192 return service_state_to_string(SERVICE(u
)->state
);
2195 static bool service_check_gc(Unit
*u
) {
2196 Service
*s
= SERVICE(u
);
2200 /* Never clean up services that still have a process around,
2201 * even if the service is formally dead. */
2202 if (cgroup_good(s
) > 0 ||
2203 main_pid_good(s
) > 0 ||
2204 control_pid_good(s
) > 0)
2210 _pure_
static bool service_check_snapshot(Unit
*u
) {
2211 Service
*s
= SERVICE(u
);
2215 return s
->socket_fd
< 0;
2218 static int service_retry_pid_file(Service
*s
) {
2221 assert(s
->pid_file
);
2222 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2224 r
= service_load_pid_file(s
, false);
2228 service_unwatch_pid_file(s
);
2230 service_enter_running(s
, SERVICE_SUCCESS
);
2234 static int service_watch_pid_file(Service
*s
) {
2237 log_unit_debug(UNIT(s
)->id
, "Setting watch for %s's PID file %s", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2239 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2243 /* the pidfile might have appeared just before we set the watch */
2244 log_unit_debug(UNIT(s
)->id
, "Trying to read %s's PID file %s in case it changed", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2245 service_retry_pid_file(s
);
2249 log_unit_error_errno(UNIT(s
)->id
, r
, "Failed to set a watch for %s's PID file %s: %m", UNIT(s
)->id
, s
->pid_file_pathspec
->path
);
2250 service_unwatch_pid_file(s
);
2254 static int service_demand_pid_file(Service
*s
) {
2257 assert(s
->pid_file
);
2258 assert(!s
->pid_file_pathspec
);
2260 ps
= new0(PathSpec
, 1);
2265 ps
->path
= strdup(s
->pid_file
);
2271 path_kill_slashes(ps
->path
);
2273 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2274 * keep their PID file open all the time. */
2275 ps
->type
= PATH_MODIFIED
;
2276 ps
->inotify_fd
= -1;
2278 s
->pid_file_pathspec
= ps
;
2280 return service_watch_pid_file(s
);
2283 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2284 PathSpec
*p
= userdata
;
2289 s
= SERVICE(p
->unit
);
2293 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2294 assert(s
->pid_file_pathspec
);
2295 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2297 log_unit_debug(UNIT(s
)->id
, "inotify event for %s", UNIT(s
)->id
);
2299 if (path_spec_fd_event(p
, events
) < 0)
2302 if (service_retry_pid_file(s
) == 0)
2305 if (service_watch_pid_file(s
) < 0)
2311 service_unwatch_pid_file(s
);
2312 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2316 static void service_notify_cgroup_empty_event(Unit
*u
) {
2317 Service
*s
= SERVICE(u
);
2321 log_unit_debug(u
->id
, "%s: cgroup is empty", u
->id
);
2325 /* Waiting for SIGCHLD is usually more interesting,
2326 * because it includes return codes/signals. Which is
2327 * why we ignore the cgroup events for most cases,
2328 * except when we don't know pid which to expect the
2332 case SERVICE_START_POST
:
2333 /* If we were hoping for the daemon to write its PID file,
2334 * we can give up now. */
2335 if (s
->pid_file_pathspec
) {
2336 log_unit_warning(u
->id
, "%s never wrote its PID file. Failing.", UNIT(s
)->id
);
2338 service_unwatch_pid_file(s
);
2339 if (s
->state
== SERVICE_START
)
2340 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2342 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2346 case SERVICE_RUNNING
:
2347 /* service_enter_running() will figure out what to do */
2348 service_enter_running(s
, SERVICE_SUCCESS
);
2351 case SERVICE_STOP_SIGABRT
:
2352 case SERVICE_STOP_SIGTERM
:
2353 case SERVICE_STOP_SIGKILL
:
2355 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2356 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2360 case SERVICE_STOP_POST
:
2361 case SERVICE_FINAL_SIGTERM
:
2362 case SERVICE_FINAL_SIGKILL
:
2363 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2364 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2373 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2374 Service
*s
= SERVICE(u
);
2380 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2381 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2382 f
= SERVICE_SUCCESS
;
2383 else if (code
== CLD_EXITED
)
2384 f
= SERVICE_FAILURE_EXIT_CODE
;
2385 else if (code
== CLD_KILLED
)
2386 f
= SERVICE_FAILURE_SIGNAL
;
2387 else if (code
== CLD_DUMPED
)
2388 f
= SERVICE_FAILURE_CORE_DUMP
;
2390 assert_not_reached("Unknown code");
2392 if (s
->main_pid
== pid
) {
2393 /* Forking services may occasionally move to a new PID.
2394 * As long as they update the PID file before exiting the old
2395 * PID, they're fine. */
2396 if (service_load_pid_file(s
, false) == 0)
2400 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2402 if (s
->main_command
) {
2403 /* If this is not a forking service than the
2404 * main process got started and hence we copy
2405 * the exit status so that it is recorded both
2406 * as main and as control process exit
2409 s
->main_command
->exec_status
= s
->main_exec_status
;
2411 if (s
->main_command
->ignore
)
2412 f
= SERVICE_SUCCESS
;
2413 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2415 /* If this is a forked process, then we should
2416 * ignore the return value if this was
2417 * configured for the starter process */
2419 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2420 f
= SERVICE_SUCCESS
;
2423 log_unit_struct(u
->id
,
2424 f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2425 LOG_MESSAGE("%s: main process exited, code=%s, status=%i/%s",
2426 u
->id
, sigchld_code_to_string(code
), status
,
2427 strna(code
== CLD_EXITED
2428 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2429 : signal_to_string(status
))),
2430 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2431 "EXIT_STATUS=%i", status
,
2434 if (f
!= SERVICE_SUCCESS
)
2437 if (s
->main_command
&&
2438 s
->main_command
->command_next
&&
2439 f
== SERVICE_SUCCESS
) {
2441 /* There is another command to *
2442 * execute, so let's do that. */
2444 log_unit_debug(u
->id
, "%s running next main command for state %s", u
->id
, service_state_to_string(s
->state
));
2445 service_run_next_main(s
);
2449 /* The service exited, so the service is officially
2451 s
->main_command
= NULL
;
2455 case SERVICE_START_POST
:
2456 case SERVICE_RELOAD
:
2458 /* Need to wait until the operation is
2463 if (s
->type
== SERVICE_ONESHOT
) {
2464 /* This was our main goal, so let's go on */
2465 if (f
== SERVICE_SUCCESS
)
2466 service_enter_start_post(s
);
2468 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2474 case SERVICE_RUNNING
:
2475 service_enter_running(s
, f
);
2478 case SERVICE_STOP_SIGABRT
:
2479 case SERVICE_STOP_SIGTERM
:
2480 case SERVICE_STOP_SIGKILL
:
2482 if (!control_pid_good(s
))
2483 service_enter_stop_post(s
, f
);
2485 /* If there is still a control process, wait for that first */
2488 case SERVICE_STOP_POST
:
2489 case SERVICE_FINAL_SIGTERM
:
2490 case SERVICE_FINAL_SIGKILL
:
2492 if (!control_pid_good(s
))
2493 service_enter_dead(s
, f
, true);
2497 assert_not_reached("Uh, main process died at wrong time.");
2501 } else if (s
->control_pid
== pid
) {
2504 if (s
->control_command
) {
2505 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2507 if (s
->control_command
->ignore
)
2508 f
= SERVICE_SUCCESS
;
2511 log_unit_full(u
->id
,
2512 f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2513 "%s: control process exited, code=%s status=%i",
2514 u
->id
, sigchld_code_to_string(code
), status
);
2516 if (f
!= SERVICE_SUCCESS
)
2519 /* Immediately get rid of the cgroup, so that the
2520 * kernel doesn't delay the cgroup empty messages for
2521 * the service cgroup any longer than necessary */
2522 service_kill_control_processes(s
);
2524 if (s
->control_command
&&
2525 s
->control_command
->command_next
&&
2526 f
== SERVICE_SUCCESS
) {
2528 /* There is another command to *
2529 * execute, so let's do that. */
2531 log_unit_debug(u
->id
, "%s running next control command for state %s", u
->id
, service_state_to_string(s
->state
));
2532 service_run_next_control(s
);
2535 /* No further commands for this step, so let's
2536 * figure out what to do next */
2538 s
->control_command
= NULL
;
2539 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2541 log_unit_debug(u
->id
, "%s got final SIGCHLD for state %s", u
->id
, service_state_to_string(s
->state
));
2545 case SERVICE_START_PRE
:
2546 if (f
== SERVICE_SUCCESS
)
2547 service_enter_start(s
);
2549 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2553 if (s
->type
!= SERVICE_FORKING
)
2554 /* Maybe spurious event due to a reload that changed the type? */
2557 if (f
!= SERVICE_SUCCESS
) {
2558 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2563 bool has_start_post
;
2566 /* Let's try to load the pid file here if we can.
2567 * The PID file might actually be created by a START_POST
2568 * script. In that case don't worry if the loading fails. */
2570 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2571 r
= service_load_pid_file(s
, !has_start_post
);
2572 if (!has_start_post
&& r
< 0) {
2573 r
= service_demand_pid_file(s
);
2574 if (r
< 0 || !cgroup_good(s
))
2575 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2579 service_search_main_pid(s
);
2581 service_enter_start_post(s
);
2584 case SERVICE_START_POST
:
2585 if (f
!= SERVICE_SUCCESS
) {
2586 service_enter_stop(s
, f
);
2593 r
= service_load_pid_file(s
, true);
2595 r
= service_demand_pid_file(s
);
2596 if (r
< 0 || !cgroup_good(s
))
2597 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2601 service_search_main_pid(s
);
2603 service_enter_running(s
, SERVICE_SUCCESS
);
2606 case SERVICE_RELOAD
:
2607 if (f
== SERVICE_SUCCESS
) {
2608 service_load_pid_file(s
, true);
2609 service_search_main_pid(s
);
2612 s
->reload_result
= f
;
2613 service_enter_running(s
, SERVICE_SUCCESS
);
2617 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2620 case SERVICE_STOP_SIGABRT
:
2621 case SERVICE_STOP_SIGTERM
:
2622 case SERVICE_STOP_SIGKILL
:
2623 if (main_pid_good(s
) <= 0)
2624 service_enter_stop_post(s
, f
);
2626 /* If there is still a service
2627 * process around, wait until
2628 * that one quit, too */
2631 case SERVICE_STOP_POST
:
2632 case SERVICE_FINAL_SIGTERM
:
2633 case SERVICE_FINAL_SIGKILL
:
2634 if (main_pid_good(s
) <= 0)
2635 service_enter_dead(s
, f
, true);
2639 assert_not_reached("Uh, control process died at wrong time.");
2644 /* Notify clients about changed exit status */
2645 unit_add_to_dbus_queue(u
);
2647 /* We got one SIGCHLD for the service, let's watch all
2648 * processes that are now running of the service, and watch
2649 * that. Among the PIDs we then watch will be children
2650 * reassigned to us, which hopefully allows us to identify
2651 * when all children are gone */
2652 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2653 unit_watch_all_pids(u
);
2655 /* If the PID set is empty now, then let's finish this off */
2656 if (set_isempty(u
->pids
))
2657 service_notify_cgroup_empty_event(u
);
2660 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2661 Service
*s
= SERVICE(userdata
);
2664 assert(source
== s
->timer_event_source
);
2668 case SERVICE_START_PRE
:
2670 log_unit_warning(UNIT(s
)->id
, "%s %s operation timed out. Terminating.", UNIT(s
)->id
, s
->state
== SERVICE_START
? "start" : "start-pre");
2671 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2674 case SERVICE_START_POST
:
2675 log_unit_warning(UNIT(s
)->id
, "%s start-post operation timed out. Stopping.", UNIT(s
)->id
);
2676 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2679 case SERVICE_RELOAD
:
2680 log_unit_warning(UNIT(s
)->id
, "%s reload operation timed out. Stopping.", UNIT(s
)->id
);
2681 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2682 service_enter_running(s
, SERVICE_SUCCESS
);
2686 log_unit_warning(UNIT(s
)->id
, "%s stopping timed out. Terminating.", UNIT(s
)->id
);
2687 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2690 case SERVICE_STOP_SIGABRT
:
2691 log_unit_warning(UNIT(s
)->id
, "%s stop-sigabrt timed out. Terminating.", UNIT(s
)->id
);
2692 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2695 case SERVICE_STOP_SIGTERM
:
2696 if (s
->kill_context
.send_sigkill
) {
2697 log_unit_warning(UNIT(s
)->id
, "%s stop-sigterm timed out. Killing.", UNIT(s
)->id
);
2698 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2700 log_unit_warning(UNIT(s
)->id
, "%s stop-sigterm timed out. Skipping SIGKILL.", UNIT(s
)->id
);
2701 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2706 case SERVICE_STOP_SIGKILL
:
2707 /* Uh, we sent a SIGKILL and it is still not gone?
2708 * Must be something we cannot kill, so let's just be
2709 * weirded out and continue */
2711 log_unit_warning(UNIT(s
)->id
, "%s still around after SIGKILL. Ignoring.", UNIT(s
)->id
);
2712 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2715 case SERVICE_STOP_POST
:
2716 log_unit_warning(UNIT(s
)->id
, "%s stop-post timed out. Terminating.", UNIT(s
)->id
);
2717 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2720 case SERVICE_FINAL_SIGTERM
:
2721 if (s
->kill_context
.send_sigkill
) {
2722 log_unit_warning(UNIT(s
)->id
, "%s stop-final-sigterm timed out. Killing.", UNIT(s
)->id
);
2723 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2725 log_unit_warning(UNIT(s
)->id
, "%s stop-final-sigterm timed out. Skipping SIGKILL. Entering failed mode.", UNIT(s
)->id
);
2726 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2731 case SERVICE_FINAL_SIGKILL
:
2732 log_unit_warning(UNIT(s
)->id
, "%s still around after final SIGKILL. Entering failed mode.", UNIT(s
)->id
);
2733 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2736 case SERVICE_AUTO_RESTART
:
2737 log_unit_info(UNIT(s
)->id
,
2738 s
->restart_usec
> 0 ?
2739 "%s holdoff time over, scheduling restart." :
2740 "%s has no holdoff time, scheduling restart.",
2742 service_enter_restart(s
);
2746 assert_not_reached("Timeout at wrong time.");
2752 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2753 Service
*s
= SERVICE(userdata
);
2754 char t
[FORMAT_TIMESPAN_MAX
];
2757 assert(source
== s
->watchdog_event_source
);
2759 log_unit_error(UNIT(s
)->id
, "%s watchdog timeout (limit %s)!", UNIT(s
)->id
,
2760 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2762 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2767 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2768 Service
*s
= SERVICE(u
);
2769 _cleanup_free_
char *cc
= NULL
;
2770 bool notify_dbus
= false;
2775 cc
= strv_join(tags
, ", ");
2776 log_unit_debug(u
->id
, "%s: Got notification message from PID "PID_FMT
" (%s)",
2777 u
->id
, pid
, isempty(cc
) ? "n/a" : cc
);
2779 if (s
->notify_access
== NOTIFY_NONE
) {
2780 log_unit_warning(u
->id
, "%s: Got notification message from PID "PID_FMT
", but reception is disabled.", u
->id
, pid
);
2784 if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2785 if (s
->main_pid
!= 0)
2786 log_unit_warning(u
->id
, "%s: Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, u
->id
, pid
, s
->main_pid
);
2788 log_unit_debug(u
->id
, "%s: Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", u
->id
, pid
);
2792 /* Interpret MAINPID= */
2793 e
= strv_find_startswith(tags
, "MAINPID=");
2794 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2795 if (parse_pid(e
, &pid
) < 0)
2796 log_unit_warning(u
->id
, "Failed to parse MAINPID= field in notification message: %s", e
);
2798 log_unit_debug(u
->id
, "%s: got MAINPID=%s", u
->id
, e
);
2800 service_set_main_pid(s
, pid
);
2801 unit_watch_pid(UNIT(s
), pid
);
2806 /* Interpret RELOADING= */
2807 if (strv_find(tags
, "RELOADING=1")) {
2809 log_unit_debug(u
->id
, "%s: got RELOADING=1", u
->id
);
2810 s
->notify_state
= NOTIFY_RELOADING
;
2812 if (s
->state
== SERVICE_RUNNING
)
2813 service_enter_reload_by_notify(s
);
2818 /* Interpret READY= */
2819 if (strv_find(tags
, "READY=1")) {
2821 log_unit_debug(u
->id
, "%s: got READY=1", u
->id
);
2822 s
->notify_state
= NOTIFY_READY
;
2824 /* Type=notify services inform us about completed
2825 * initialization with READY=1 */
2826 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2827 service_enter_start_post(s
);
2829 /* Sending READY=1 while we are reloading informs us
2830 * that the reloading is complete */
2831 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2832 service_enter_running(s
, SERVICE_SUCCESS
);
2837 /* Interpret STOPPING= */
2838 if (strv_find(tags
, "STOPPING=1")) {
2840 log_unit_debug(u
->id
, "%s: got STOPPING=1", u
->id
);
2841 s
->notify_state
= NOTIFY_STOPPING
;
2843 if (s
->state
== SERVICE_RUNNING
)
2844 service_enter_stop_by_notify(s
);
2849 /* Interpret STATUS= */
2850 e
= strv_find_startswith(tags
, "STATUS=");
2852 _cleanup_free_
char *t
= NULL
;
2855 if (!utf8_is_valid(e
))
2856 log_unit_warning(u
->id
, "Status message in notification is not UTF-8 clean.");
2858 log_unit_debug(u
->id
, "%s: got STATUS=%s", u
->id
, e
);
2866 if (!streq_ptr(s
->status_text
, t
)) {
2868 free(s
->status_text
);
2876 /* Interpret ERRNO= */
2877 e
= strv_find_startswith(tags
, "ERRNO=");
2881 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
2882 log_unit_warning(u
->id
, "Failed to parse ERRNO= field in notification message: %s", e
);
2884 log_unit_debug(u
->id
, "%s: got ERRNO=%s", u
->id
, e
);
2886 if (s
->status_errno
!= status_errno
) {
2887 s
->status_errno
= status_errno
;
2893 /* Interpret WATCHDOG= */
2894 if (strv_find(tags
, "WATCHDOG=1")) {
2895 log_unit_debug(u
->id
, "%s: got WATCHDOG=1", u
->id
);
2896 service_reset_watchdog(s
);
2899 /* Add the passed fds to the fd store */
2900 if (strv_find(tags
, "FDSTORE=1")) {
2901 log_unit_debug(u
->id
, "%s: got FDSTORE=1", u
->id
);
2902 service_add_fd_store_set(s
, fds
);
2905 /* Notify clients about changed status or main pid */
2907 unit_add_to_dbus_queue(u
);
2910 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
2911 Service
*s
= SERVICE(u
);
2914 if (!s
->timer_event_source
)
2917 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
2924 static void service_bus_name_owner_change(
2927 const char *old_owner
,
2928 const char *new_owner
) {
2930 Service
*s
= SERVICE(u
);
2936 assert(streq(s
->bus_name
, name
));
2937 assert(old_owner
|| new_owner
);
2939 if (old_owner
&& new_owner
)
2940 log_unit_debug(u
->id
, "%s's D-Bus name %s changed owner from %s to %s", u
->id
, name
, old_owner
, new_owner
);
2942 log_unit_debug(u
->id
, "%s's D-Bus name %s no longer registered by %s", u
->id
, name
, old_owner
);
2944 log_unit_debug(u
->id
, "%s's D-Bus name %s now registered by %s", u
->id
, name
, new_owner
);
2946 s
->bus_name_good
= !!new_owner
;
2948 if (s
->type
== SERVICE_DBUS
) {
2950 /* service_enter_running() will figure out what to
2952 if (s
->state
== SERVICE_RUNNING
)
2953 service_enter_running(s
, SERVICE_SUCCESS
);
2954 else if (s
->state
== SERVICE_START
&& new_owner
)
2955 service_enter_start_post(s
);
2957 } else if (new_owner
&&
2959 (s
->state
== SERVICE_START
||
2960 s
->state
== SERVICE_START_POST
||
2961 s
->state
== SERVICE_RUNNING
||
2962 s
->state
== SERVICE_RELOAD
)) {
2964 _cleanup_bus_creds_unref_ sd_bus_creds
*creds
= NULL
;
2967 /* Try to acquire PID from bus service */
2969 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
2971 r
= sd_bus_creds_get_pid(creds
, &pid
);
2973 log_unit_debug(u
->id
, "%s's D-Bus name %s is now owned by process %u", u
->id
, name
, (unsigned) pid
);
2975 service_set_main_pid(s
, pid
);
2976 unit_watch_pid(UNIT(s
), pid
);
2981 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
2982 _cleanup_free_
char *peer
= NULL
;
2988 /* This is called by the socket code when instantiating a new
2989 * service for a stream socket and the socket needs to be
2992 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
2995 if (s
->socket_fd
>= 0)
2998 if (s
->state
!= SERVICE_DEAD
)
3001 if (getpeername_pretty(fd
, &peer
) >= 0) {
3003 if (UNIT(s
)->description
) {
3004 _cleanup_free_
char *a
;
3006 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3010 r
= unit_set_description(UNIT(s
), a
);
3012 r
= unit_set_description(UNIT(s
), peer
);
3019 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3021 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3023 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3026 static void service_reset_failed(Unit
*u
) {
3027 Service
*s
= SERVICE(u
);
3031 if (s
->state
== SERVICE_FAILED
)
3032 service_set_state(s
, SERVICE_DEAD
);
3034 s
->result
= SERVICE_SUCCESS
;
3035 s
->reload_result
= SERVICE_SUCCESS
;
3037 RATELIMIT_RESET(s
->start_limit
);
3040 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3041 Service
*s
= SERVICE(u
);
3043 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3046 static const char* const service_state_table
[_SERVICE_STATE_MAX
] = {
3047 [SERVICE_DEAD
] = "dead",
3048 [SERVICE_START_PRE
] = "start-pre",
3049 [SERVICE_START
] = "start",
3050 [SERVICE_START_POST
] = "start-post",
3051 [SERVICE_RUNNING
] = "running",
3052 [SERVICE_EXITED
] = "exited",
3053 [SERVICE_RELOAD
] = "reload",
3054 [SERVICE_STOP
] = "stop",
3055 [SERVICE_STOP_SIGABRT
] = "stop-sigabrt",
3056 [SERVICE_STOP_SIGTERM
] = "stop-sigterm",
3057 [SERVICE_STOP_SIGKILL
] = "stop-sigkill",
3058 [SERVICE_STOP_POST
] = "stop-post",
3059 [SERVICE_FINAL_SIGTERM
] = "final-sigterm",
3060 [SERVICE_FINAL_SIGKILL
] = "final-sigkill",
3061 [SERVICE_FAILED
] = "failed",
3062 [SERVICE_AUTO_RESTART
] = "auto-restart",
3065 DEFINE_STRING_TABLE_LOOKUP(service_state
, ServiceState
);
3067 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3068 [SERVICE_RESTART_NO
] = "no",
3069 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3070 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3071 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3072 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3073 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3074 [SERVICE_RESTART_ALWAYS
] = "always",
3077 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3079 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3080 [SERVICE_SIMPLE
] = "simple",
3081 [SERVICE_FORKING
] = "forking",
3082 [SERVICE_ONESHOT
] = "oneshot",
3083 [SERVICE_DBUS
] = "dbus",
3084 [SERVICE_NOTIFY
] = "notify",
3085 [SERVICE_IDLE
] = "idle"
3088 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3090 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3091 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3092 [SERVICE_EXEC_START
] = "ExecStart",
3093 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3094 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3095 [SERVICE_EXEC_STOP
] = "ExecStop",
3096 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3099 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3101 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3102 [NOTIFY_NONE
] = "none",
3103 [NOTIFY_MAIN
] = "main",
3104 [NOTIFY_ALL
] = "all"
3107 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3109 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3110 [NOTIFY_UNKNOWN
] = "unknown",
3111 [NOTIFY_READY
] = "ready",
3112 [NOTIFY_RELOADING
] = "reloading",
3113 [NOTIFY_STOPPING
] = "stopping",
3116 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3118 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3119 [SERVICE_SUCCESS
] = "success",
3120 [SERVICE_FAILURE_RESOURCES
] = "resources",
3121 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3122 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3123 [SERVICE_FAILURE_SIGNAL
] = "signal",
3124 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3125 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3126 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3129 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3131 const UnitVTable service_vtable
= {
3132 .object_size
= sizeof(Service
),
3133 .exec_context_offset
= offsetof(Service
, exec_context
),
3134 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3135 .kill_context_offset
= offsetof(Service
, kill_context
),
3136 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3142 .private_section
= "Service",
3144 .init
= service_init
,
3145 .done
= service_done
,
3146 .load
= service_load
,
3147 .release_resources
= service_release_resources
,
3149 .coldplug
= service_coldplug
,
3151 .dump
= service_dump
,
3153 .start
= service_start
,
3154 .stop
= service_stop
,
3155 .reload
= service_reload
,
3157 .can_reload
= service_can_reload
,
3159 .kill
= service_kill
,
3161 .serialize
= service_serialize
,
3162 .deserialize_item
= service_deserialize_item
,
3164 .active_state
= service_active_state
,
3165 .sub_state_to_string
= service_sub_state_to_string
,
3167 .check_gc
= service_check_gc
,
3168 .check_snapshot
= service_check_snapshot
,
3170 .sigchld_event
= service_sigchld_event
,
3172 .reset_failed
= service_reset_failed
,
3174 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3175 .notify_message
= service_notify_message
,
3177 .bus_name_owner_change
= service_bus_name_owner_change
,
3179 .bus_interface
= "org.freedesktop.systemd1.Service",
3180 .bus_vtable
= bus_service_vtable
,
3181 .bus_set_property
= bus_service_set_property
,
3182 .bus_commit_properties
= bus_service_commit_properties
,
3184 .get_timeout
= service_get_timeout
,
3185 .can_transient
= true,
3187 .status_message_formats
= {
3188 .starting_stopping
= {
3189 [0] = "Starting %s...",
3190 [1] = "Stopping %s...",
3192 .finished_start_job
= {
3193 [JOB_DONE
] = "Started %s.",
3194 [JOB_FAILED
] = "Failed to start %s.",
3195 [JOB_DEPENDENCY
] = "Dependency failed for %s.",
3196 [JOB_TIMEOUT
] = "Timed out starting %s.",
3198 .finished_stop_job
= {
3199 [JOB_DONE
] = "Stopped %s.",
3200 [JOB_FAILED
] = "Stopped (with error) %s.",
3201 [JOB_TIMEOUT
] = "Timed out stopping %s.",