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/>.
27 #include "bus-error.h"
28 #include "bus-kernel.h"
30 #include "dbus-service.h"
34 #include "exit-status.h"
37 #include "formats-util.h"
39 #include "load-dropin.h"
40 #include "load-fragment.h"
43 #include "parse-util.h"
44 #include "path-util.h"
45 #include "process-util.h"
47 #include "signal-util.h"
49 #include "string-util.h"
51 #include "unit-name.h"
52 #include "unit-printf.h"
57 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
58 [SERVICE_DEAD
] = UNIT_INACTIVE
,
59 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
60 [SERVICE_START
] = UNIT_ACTIVATING
,
61 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
62 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
63 [SERVICE_EXITED
] = UNIT_ACTIVE
,
64 [SERVICE_RELOAD
] = UNIT_RELOADING
,
65 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
66 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
67 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
68 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
69 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
70 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
71 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
72 [SERVICE_FAILED
] = UNIT_FAILED
,
73 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
76 /* For Type=idle we never want to delay any other jobs, hence we
77 * consider idle jobs active as soon as we start working on them */
78 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
79 [SERVICE_DEAD
] = UNIT_INACTIVE
,
80 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
81 [SERVICE_START
] = UNIT_ACTIVE
,
82 [SERVICE_START_POST
] = UNIT_ACTIVE
,
83 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
84 [SERVICE_EXITED
] = UNIT_ACTIVE
,
85 [SERVICE_RELOAD
] = UNIT_RELOADING
,
86 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
87 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
88 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
89 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
90 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
91 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
92 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
93 [SERVICE_FAILED
] = UNIT_FAILED
,
94 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
97 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
98 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
99 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
101 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
102 static void service_enter_reload_by_notify(Service
*s
);
104 static void service_init(Unit
*u
) {
105 Service
*s
= SERVICE(u
);
108 assert(u
->load_state
== UNIT_STUB
);
110 s
->timeout_start_usec
= u
->manager
->default_timeout_start_usec
;
111 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
112 s
->restart_usec
= u
->manager
->default_restart_usec
;
113 s
->type
= _SERVICE_TYPE_INVALID
;
115 s
->bus_endpoint_fd
= -1;
116 s
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -1;
117 s
->guess_main_pid
= true;
119 RATELIMIT_INIT(s
->start_limit
, u
->manager
->default_start_limit_interval
, u
->manager
->default_start_limit_burst
);
121 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
124 static void service_unwatch_control_pid(Service
*s
) {
127 if (s
->control_pid
<= 0)
130 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
134 static void service_unwatch_main_pid(Service
*s
) {
137 if (s
->main_pid
<= 0)
140 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
144 static void service_unwatch_pid_file(Service
*s
) {
145 if (!s
->pid_file_pathspec
)
148 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
149 path_spec_unwatch(s
->pid_file_pathspec
);
150 path_spec_done(s
->pid_file_pathspec
);
151 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
154 static int service_set_main_pid(Service
*s
, pid_t pid
) {
165 if (s
->main_pid
== pid
&& s
->main_pid_known
)
168 if (s
->main_pid
!= pid
) {
169 service_unwatch_main_pid(s
);
170 exec_status_start(&s
->main_exec_status
, pid
);
174 s
->main_pid_known
= true;
176 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
177 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
178 s
->main_pid_alien
= true;
180 s
->main_pid_alien
= false;
185 static void service_close_socket_fd(Service
*s
) {
188 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
191 static void service_connection_unref(Service
*s
) {
194 if (!UNIT_ISSET(s
->accept_socket
))
197 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
198 unit_ref_unset(&s
->accept_socket
);
201 static void service_stop_watchdog(Service
*s
) {
204 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
205 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
208 static void service_start_watchdog(Service
*s
) {
213 if (s
->watchdog_usec
<= 0)
216 if (s
->watchdog_event_source
) {
217 r
= sd_event_source_set_time(s
->watchdog_event_source
, s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
);
219 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
223 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
225 r
= sd_event_add_time(
226 UNIT(s
)->manager
->event
,
227 &s
->watchdog_event_source
,
229 s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
, 0,
230 service_dispatch_watchdog
, s
);
232 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
236 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
238 /* Let's process everything else which might be a sign
239 * of living before we consider a service died. */
240 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
244 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
247 static void service_reset_watchdog(Service
*s
) {
250 dual_timestamp_get(&s
->watchdog_timestamp
);
251 service_start_watchdog(s
);
254 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
260 assert(fs
->service
->n_fd_store
> 0);
261 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
262 fs
->service
->n_fd_store
--;
265 if (fs
->event_source
) {
266 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
267 sd_event_source_unref(fs
->event_source
);
275 static void service_release_resources(Unit
*u
) {
276 Service
*s
= SERVICE(u
);
280 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
283 log_unit_debug(u
, "Releasing all resources.");
285 s
->stdin_fd
= safe_close(s
->stdin_fd
);
286 s
->stdout_fd
= safe_close(s
->stdout_fd
);
287 s
->stderr_fd
= safe_close(s
->stderr_fd
);
290 service_fd_store_unlink(s
->fd_store
);
292 assert(s
->n_fd_store
== 0);
295 static void service_done(Unit
*u
) {
296 Service
*s
= SERVICE(u
);
300 s
->pid_file
= mfree(s
->pid_file
);
301 s
->status_text
= mfree(s
->status_text
);
302 s
->reboot_arg
= mfree(s
->reboot_arg
);
304 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
305 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
306 s
->control_command
= NULL
;
307 s
->main_command
= NULL
;
309 exit_status_set_free(&s
->restart_prevent_status
);
310 exit_status_set_free(&s
->restart_force_status
);
311 exit_status_set_free(&s
->success_status
);
313 /* This will leak a process, but at least no memory or any of
315 service_unwatch_main_pid(s
);
316 service_unwatch_control_pid(s
);
317 service_unwatch_pid_file(s
);
320 unit_unwatch_bus_name(u
, s
->bus_name
);
321 s
->bus_name
= mfree(s
->bus_name
);
324 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
325 service_close_socket_fd(s
);
326 service_connection_unref(s
);
328 unit_ref_unset(&s
->accept_socket
);
330 service_stop_watchdog(s
);
332 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
334 service_release_resources(u
);
337 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
338 ServiceFDStore
*fs
= userdata
;
343 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
344 service_fd_store_unlink(fs
);
348 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
355 if (s
->n_fd_store
>= s
->n_fd_store_max
)
358 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
359 r
= same_fd(fs
->fd
, fd
);
363 /* Already included */
369 fs
= new0(ServiceFDStore
, 1);
375 fs
->fdname
= strdup(name
?: "stored");
381 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
388 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
390 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
396 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
401 if (fdset_size(fds
) <= 0)
404 while (s
->n_fd_store
< s
->n_fd_store_max
) {
405 _cleanup_close_
int fd
= -1;
407 fd
= fdset_steal_first(fds
);
411 r
= service_add_fd_store(s
, fd
, name
);
413 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
415 log_unit_debug(UNIT(s
), "Added fd to fd store.");
420 if (fdset_size(fds
) > 0)
421 log_unit_warning(UNIT(s
), "Tried to store more fds than FDStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
426 static int service_arm_timer(Service
*s
, usec_t usec
) {
431 if (s
->timer_event_source
) {
432 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
436 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
439 r
= sd_event_add_time(
440 UNIT(s
)->manager
->event
,
441 &s
->timer_event_source
,
443 now(CLOCK_MONOTONIC
) + usec
, 0,
444 service_dispatch_timer
, s
);
448 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
453 static int service_verify(Service
*s
) {
456 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
459 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
460 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
464 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
465 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
469 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
470 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
474 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
475 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
479 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
480 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
484 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
485 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
489 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
490 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
494 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
495 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
497 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
498 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
502 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
503 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
505 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
506 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
511 static int service_add_default_dependencies(Service
*s
) {
516 /* Add a number of automatic dependencies useful for the
517 * majority of services. */
519 /* First, pull in base system */
520 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
524 /* Second, activate normal shutdown */
525 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
528 static void service_fix_output(Service
*s
) {
531 /* If nothing has been explicitly configured, patch default
532 * output in. If input is socket/tty we avoid this however,
533 * since in that case we want output to default to the same
534 * place as we read input from. */
536 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
537 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
538 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
539 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
541 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
542 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
543 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
546 static int service_add_extras(Service
*s
) {
551 if (s
->type
== _SERVICE_TYPE_INVALID
) {
552 /* Figure out a type automatically */
554 s
->type
= SERVICE_DBUS
;
555 else if (s
->exec_command
[SERVICE_EXEC_START
])
556 s
->type
= SERVICE_SIMPLE
;
558 s
->type
= SERVICE_ONESHOT
;
561 /* Oneshot services have disabled start timeout by default */
562 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
563 s
->timeout_start_usec
= 0;
565 service_fix_output(s
);
567 r
= unit_patch_contexts(UNIT(s
));
571 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
575 r
= unit_set_default_slice(UNIT(s
));
579 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
580 s
->notify_access
= NOTIFY_MAIN
;
582 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
583 s
->notify_access
= NOTIFY_MAIN
;
588 n
= strjoina(s
->bus_name
, ".busname");
589 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
593 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
595 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
597 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
600 if (UNIT(s
)->default_dependencies
) {
601 r
= service_add_default_dependencies(s
);
609 static int service_load(Unit
*u
) {
610 Service
*s
= SERVICE(u
);
615 /* Load a .service file */
616 r
= unit_load_fragment(u
);
620 /* Still nothing found? Then let's give up */
621 if (u
->load_state
== UNIT_STUB
)
624 /* This is a new unit? Then let's add in some extras */
625 if (u
->load_state
== UNIT_LOADED
) {
627 /* We were able to load something, then let's add in
628 * the dropin directories. */
629 r
= unit_load_dropin(u
);
633 /* This is a new unit? Then let's add in some
635 r
= service_add_extras(s
);
640 return service_verify(s
);
643 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
644 ServiceExecCommand c
;
645 Service
*s
= SERVICE(u
);
650 prefix
= strempty(prefix
);
651 prefix2
= strjoina(prefix
, "\t");
654 "%sService State: %s\n"
656 "%sReload Result: %s\n"
657 "%sPermissionsStartOnly: %s\n"
658 "%sRootDirectoryStartOnly: %s\n"
659 "%sRemainAfterExit: %s\n"
660 "%sGuessMainPID: %s\n"
663 "%sNotifyAccess: %s\n"
664 "%sNotifyState: %s\n",
665 prefix
, service_state_to_string(s
->state
),
666 prefix
, service_result_to_string(s
->result
),
667 prefix
, service_result_to_string(s
->reload_result
),
668 prefix
, yes_no(s
->permissions_start_only
),
669 prefix
, yes_no(s
->root_directory_start_only
),
670 prefix
, yes_no(s
->remain_after_exit
),
671 prefix
, yes_no(s
->guess_main_pid
),
672 prefix
, service_type_to_string(s
->type
),
673 prefix
, service_restart_to_string(s
->restart
),
674 prefix
, notify_access_to_string(s
->notify_access
),
675 prefix
, notify_state_to_string(s
->notify_state
));
677 if (s
->control_pid
> 0)
679 "%sControl PID: "PID_FMT
"\n",
680 prefix
, s
->control_pid
);
684 "%sMain PID: "PID_FMT
"\n"
685 "%sMain PID Known: %s\n"
686 "%sMain PID Alien: %s\n",
688 prefix
, yes_no(s
->main_pid_known
),
689 prefix
, yes_no(s
->main_pid_alien
));
694 prefix
, s
->pid_file
);
699 "%sBus Name Good: %s\n",
701 prefix
, yes_no(s
->bus_name_good
));
703 kill_context_dump(&s
->kill_context
, f
, prefix
);
704 exec_context_dump(&s
->exec_context
, f
, prefix
);
706 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
708 if (!s
->exec_command
[c
])
711 fprintf(f
, "%s-> %s:\n",
712 prefix
, service_exec_command_to_string(c
));
714 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
718 fprintf(f
, "%sStatus Text: %s\n",
719 prefix
, s
->status_text
);
721 if (s
->n_fd_store_max
> 0)
723 "%sFile Descriptor Store Max: %u\n"
724 "%sFile Descriptor Store Current: %u\n",
725 prefix
, s
->n_fd_store_max
,
726 prefix
, s
->n_fd_store
);
729 static int service_load_pid_file(Service
*s
, bool may_warn
) {
730 _cleanup_free_
char *k
= NULL
;
739 r
= read_one_line_file(s
->pid_file
, &k
);
742 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
));
746 r
= parse_pid(k
, &pid
);
749 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
753 if (!pid_is_alive(pid
)) {
755 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
759 if (s
->main_pid_known
) {
760 if (pid
== s
->main_pid
)
763 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
765 service_unwatch_main_pid(s
);
766 s
->main_pid_known
= false;
768 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
770 r
= service_set_main_pid(s
, pid
);
774 r
= unit_watch_pid(UNIT(s
), pid
);
776 /* FIXME: we need to do something here */
777 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
784 static int service_search_main_pid(Service
*s
) {
790 /* If we know it anyway, don't ever fallback to unreliable
792 if (s
->main_pid_known
)
795 if (!s
->guess_main_pid
)
798 assert(s
->main_pid
<= 0);
800 r
= unit_search_main_pid(UNIT(s
), &pid
);
804 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
805 r
= service_set_main_pid(s
, pid
);
809 r
= unit_watch_pid(UNIT(s
), pid
);
811 /* FIXME: we need to do something here */
812 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
819 static void service_set_state(Service
*s
, ServiceState state
) {
820 ServiceState old_state
;
821 const UnitActiveState
*table
;
825 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
827 old_state
= s
->state
;
830 service_unwatch_pid_file(s
);
833 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
835 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
836 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
837 SERVICE_AUTO_RESTART
))
838 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
841 SERVICE_START
, SERVICE_START_POST
,
842 SERVICE_RUNNING
, SERVICE_RELOAD
,
843 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
844 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
845 service_unwatch_main_pid(s
);
846 s
->main_command
= NULL
;
850 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
852 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
853 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
854 service_unwatch_control_pid(s
);
855 s
->control_command
= NULL
;
856 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
859 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
860 unit_unwatch_all_pids(UNIT(s
));
863 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
864 SERVICE_RUNNING
, SERVICE_RELOAD
,
865 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
866 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
867 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
868 service_close_socket_fd(s
);
869 service_connection_unref(s
);
872 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
873 service_stop_watchdog(s
);
875 /* For the inactive states unit_notify() will trim the cgroup,
876 * but for exit we have to do that ourselves... */
877 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
878 unit_prune_cgroup(UNIT(s
));
880 /* For remain_after_exit services, let's see if we can "release" the
881 * hold on the console, since unit_notify() only does that in case of
883 if (state
== SERVICE_EXITED
&&
884 s
->remain_after_exit
&&
885 UNIT(s
)->manager
->n_on_console
> 0) {
889 ec
= unit_get_exec_context(UNIT(s
));
890 if (ec
&& exec_context_may_touch_console(ec
)) {
891 Manager
*m
= UNIT(s
)->manager
;
894 if (m
->n_on_console
== 0)
895 /* unset no_console_output flag, since the console is free */
896 m
->no_console_output
= false;
900 if (old_state
!= state
)
901 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
903 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
906 static int service_coldplug(Unit
*u
) {
907 Service
*s
= SERVICE(u
);
911 assert(s
->state
== SERVICE_DEAD
);
913 if (s
->deserialized_state
!= s
->state
) {
915 if (IN_SET(s
->deserialized_state
,
916 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
918 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
919 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
923 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
925 /* For the start/stop timeouts 0 means off */
927 r
= service_arm_timer(s
, k
);
933 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
935 /* The restart timeouts 0 means immediately */
936 r
= service_arm_timer(s
, s
->restart_usec
);
941 if (pid_is_unwaited(s
->main_pid
) &&
942 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
943 IN_SET(s
->deserialized_state
,
944 SERVICE_START
, SERVICE_START_POST
,
945 SERVICE_RUNNING
, SERVICE_RELOAD
,
946 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
947 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
948 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
953 if (pid_is_unwaited(s
->control_pid
) &&
954 IN_SET(s
->deserialized_state
,
955 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
957 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
958 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
959 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
964 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
965 unit_watch_all_pids(UNIT(s
));
967 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
968 service_start_watchdog(s
);
970 service_set_state(s
, s
->deserialized_state
);
976 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
977 _cleanup_strv_free_
char **rfd_names
= NULL
;
978 _cleanup_free_
int *rfds
= NULL
;
985 if (s
->socket_fd
>= 0) {
987 /* Pass the per-connection socket */
992 rfds
[0] = s
->socket_fd
;
994 rfd_names
= strv_new("connection", NULL
);
1003 /* Pass all our configured sockets for singleton services */
1005 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1006 _cleanup_free_
int *cfds
= NULL
;
1010 if (u
->type
!= UNIT_SOCKET
)
1015 cn_fds
= socket_collect_fds(sock
, &cfds
);
1030 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1034 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1040 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1046 if (s
->n_fd_store
> 0) {
1051 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1057 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1063 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1064 rfds
[rn_fds
] = fs
->fd
;
1065 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1066 if (!rfd_names
[rn_fds
])
1072 rfd_names
[rn_fds
] = NULL
;
1076 *fd_names
= rfd_names
;
1084 static int service_spawn(
1089 bool apply_permissions
,
1091 bool apply_tty_stdin
,
1095 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1096 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1097 _cleanup_free_
int *fds
= NULL
;
1098 unsigned n_fds
= 0, n_env
= 0;
1102 ExecParameters exec_params
= {
1103 .apply_permissions
= apply_permissions
,
1104 .apply_chroot
= apply_chroot
,
1105 .apply_tty_stdin
= apply_tty_stdin
,
1106 .bus_endpoint_fd
= -1,
1118 (void) unit_realize_cgroup(UNIT(s
));
1119 if (s
->reset_cpu_usage
) {
1120 (void) unit_reset_cpu_usage(UNIT(s
));
1121 s
->reset_cpu_usage
= false;
1124 r
= unit_setup_exec_runtime(UNIT(s
));
1129 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1130 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1131 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1133 r
= service_collect_fds(s
, &fds
, &fd_names
);
1141 r
= service_arm_timer(s
, timeout
);
1145 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1147 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1151 our_env
= new0(char*, 6);
1157 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1158 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1163 if (s
->main_pid
> 0)
1164 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1169 if (UNIT(s
)->manager
->running_as
!= MANAGER_SYSTEM
)
1170 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1175 if (s
->socket_fd
>= 0) {
1176 union sockaddr_union sa
;
1177 socklen_t salen
= sizeof(sa
);
1179 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1185 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1186 _cleanup_free_
char *addr
= NULL
;
1190 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1194 t
= strappend("REMOTE_ADDR=", addr
);
1199 our_env
[n_env
++] = t
;
1201 port
= sockaddr_port(&sa
.sa
);
1207 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0) {
1211 our_env
[n_env
++] = t
;
1215 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1221 if (is_control
&& UNIT(s
)->cgroup_path
) {
1222 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1223 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1225 path
= UNIT(s
)->cgroup_path
;
1227 if (s
->exec_context
.bus_endpoint
) {
1228 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
? "system" : "user",
1229 UNIT(s
)->id
, &bus_endpoint_path
);
1233 /* Pass the fd to the exec_params so that the child process can upload the policy.
1234 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1235 * as the service is running. */
1236 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1239 exec_params
.argv
= argv
;
1240 exec_params
.fds
= fds
;
1241 exec_params
.fd_names
= fd_names
;
1242 exec_params
.n_fds
= n_fds
;
1243 exec_params
.environment
= final_env
;
1244 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1245 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1246 exec_params
.cgroup_path
= path
;
1247 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1248 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1249 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1250 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1251 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1252 if (s
->type
== SERVICE_IDLE
)
1253 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1254 exec_params
.stdin_fd
= s
->stdin_fd
;
1255 exec_params
.stdout_fd
= s
->stdout_fd
;
1256 exec_params
.stderr_fd
= s
->stderr_fd
;
1258 r
= exec_spawn(UNIT(s
),
1267 r
= unit_watch_pid(UNIT(s
), pid
);
1269 /* FIXME: we need to do something here */
1278 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1283 static int main_pid_good(Service
*s
) {
1286 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1289 /* If we know the pid file, then let's just check if it is
1291 if (s
->main_pid_known
) {
1293 /* If it's an alien child let's check if it is still
1295 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1296 return pid_is_alive(s
->main_pid
);
1298 /* .. otherwise assume we'll get a SIGCHLD for it,
1299 * which we really should wait for to collect exit
1300 * status and code */
1301 return s
->main_pid
> 0;
1304 /* We don't know the pid */
1308 _pure_
static int control_pid_good(Service
*s
) {
1311 return s
->control_pid
> 0;
1314 static int cgroup_good(Service
*s
) {
1319 if (!UNIT(s
)->cgroup_path
)
1322 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1329 static bool service_shall_restart(Service
*s
) {
1332 /* Don't restart after manual stops */
1333 if (s
->forbid_restart
)
1336 /* Never restart if this is configured as special exception */
1337 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1340 /* Restart if the exit code/status are configured as restart triggers */
1341 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1344 switch (s
->restart
) {
1346 case SERVICE_RESTART_NO
:
1349 case SERVICE_RESTART_ALWAYS
:
1352 case SERVICE_RESTART_ON_SUCCESS
:
1353 return s
->result
== SERVICE_SUCCESS
;
1355 case SERVICE_RESTART_ON_FAILURE
:
1356 return s
->result
!= SERVICE_SUCCESS
;
1358 case SERVICE_RESTART_ON_ABNORMAL
:
1359 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1361 case SERVICE_RESTART_ON_WATCHDOG
:
1362 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1364 case SERVICE_RESTART_ON_ABORT
:
1365 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1368 assert_not_reached("unknown restart setting");
1372 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1376 if (f
!= SERVICE_SUCCESS
)
1379 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1381 if (s
->result
!= SERVICE_SUCCESS
) {
1382 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1383 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1386 if (allow_restart
&& service_shall_restart(s
)) {
1388 r
= service_arm_timer(s
, s
->restart_usec
);
1392 service_set_state(s
, SERVICE_AUTO_RESTART
);
1395 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1396 s
->forbid_restart
= false;
1398 /* We want fresh tmpdirs in case service is started again immediately */
1399 exec_runtime_destroy(s
->exec_runtime
);
1400 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1402 /* Also, remove the runtime directory in */
1403 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1405 /* Try to delete the pid file. At this point it will be
1406 * out-of-date, and some software might be confused by it, so
1407 * let's remove it. */
1409 unlink_noerrno(s
->pid_file
);
1414 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1415 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1418 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1422 if (f
!= SERVICE_SUCCESS
)
1425 service_unwatch_control_pid(s
);
1426 unit_watch_all_pids(UNIT(s
));
1428 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1429 if (s
->control_command
) {
1430 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1432 r
= service_spawn(s
,
1434 s
->timeout_stop_usec
,
1436 !s
->permissions_start_only
,
1437 !s
->root_directory_start_only
,
1444 service_set_state(s
, SERVICE_STOP_POST
);
1446 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1451 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1452 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1455 static int state_to_kill_operation(ServiceState state
) {
1458 case SERVICE_STOP_SIGABRT
:
1461 case SERVICE_STOP_SIGTERM
:
1462 case SERVICE_FINAL_SIGTERM
:
1463 return KILL_TERMINATE
;
1465 case SERVICE_STOP_SIGKILL
:
1466 case SERVICE_FINAL_SIGKILL
:
1470 return _KILL_OPERATION_INVALID
;
1474 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1479 if (f
!= SERVICE_SUCCESS
)
1482 unit_watch_all_pids(UNIT(s
));
1484 r
= unit_kill_context(
1487 state_to_kill_operation(state
),
1496 if (s
->timeout_stop_usec
> 0) {
1497 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1502 service_set_state(s
, state
);
1503 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1504 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1505 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1506 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1507 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1508 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1510 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1515 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1517 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1518 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1520 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1523 static void service_enter_stop_by_notify(Service
*s
) {
1526 unit_watch_all_pids(UNIT(s
));
1528 if (s
->timeout_stop_usec
> 0)
1529 service_arm_timer(s
, s
->timeout_stop_usec
);
1531 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1532 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1535 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1540 if (f
!= SERVICE_SUCCESS
)
1543 service_unwatch_control_pid(s
);
1544 unit_watch_all_pids(UNIT(s
));
1546 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1547 if (s
->control_command
) {
1548 s
->control_command_id
= SERVICE_EXEC_STOP
;
1550 r
= service_spawn(s
,
1552 s
->timeout_stop_usec
,
1554 !s
->permissions_start_only
,
1555 !s
->root_directory_start_only
,
1562 service_set_state(s
, SERVICE_STOP
);
1564 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1569 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1570 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1573 static bool service_good(Service
*s
) {
1577 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1580 main_pid_ok
= main_pid_good(s
);
1581 if (main_pid_ok
> 0) /* It's alive */
1583 if (main_pid_ok
== 0) /* It's dead */
1586 /* OK, we don't know anything about the main PID, maybe
1587 * because there is none. Let's check the control group
1590 return cgroup_good(s
) != 0;
1593 static void service_enter_running(Service
*s
, ServiceResult f
) {
1596 if (f
!= SERVICE_SUCCESS
)
1599 if (service_good(s
)) {
1601 /* If there are any queued up sd_notify()
1602 * notifications, process them now */
1603 if (s
->notify_state
== NOTIFY_RELOADING
)
1604 service_enter_reload_by_notify(s
);
1605 else if (s
->notify_state
== NOTIFY_STOPPING
)
1606 service_enter_stop_by_notify(s
);
1608 service_set_state(s
, SERVICE_RUNNING
);
1610 } else if (s
->remain_after_exit
)
1611 service_set_state(s
, SERVICE_EXITED
);
1613 service_enter_stop(s
, SERVICE_SUCCESS
);
1616 static void service_enter_start_post(Service
*s
) {
1620 service_unwatch_control_pid(s
);
1621 service_reset_watchdog(s
);
1623 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1624 if (s
->control_command
) {
1625 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1627 r
= service_spawn(s
,
1629 s
->timeout_start_usec
,
1631 !s
->permissions_start_only
,
1632 !s
->root_directory_start_only
,
1639 service_set_state(s
, SERVICE_START_POST
);
1641 service_enter_running(s
, SERVICE_SUCCESS
);
1646 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1647 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1650 static void service_kill_control_processes(Service
*s
) {
1653 if (!UNIT(s
)->cgroup_path
)
1656 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1657 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1660 static void service_enter_start(Service
*s
) {
1667 service_unwatch_control_pid(s
);
1668 service_unwatch_main_pid(s
);
1670 /* We want to ensure that nobody leaks processes from
1671 * START_PRE here, so let's go on a killing spree, People
1672 * should not spawn long running processes from START_PRE. */
1673 service_kill_control_processes(s
);
1675 if (s
->type
== SERVICE_FORKING
) {
1676 s
->control_command_id
= SERVICE_EXEC_START
;
1677 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1679 s
->main_command
= NULL
;
1681 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1682 s
->control_command
= NULL
;
1684 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1688 assert(s
->type
== SERVICE_ONESHOT
);
1689 service_enter_start_post(s
);
1693 r
= service_spawn(s
,
1695 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1705 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1706 /* For simple services we immediately start
1707 * the START_POST binaries. */
1709 service_set_main_pid(s
, pid
);
1710 service_enter_start_post(s
);
1712 } else if (s
->type
== SERVICE_FORKING
) {
1714 /* For forking services we wait until the start
1715 * process exited. */
1717 s
->control_pid
= pid
;
1718 service_set_state(s
, SERVICE_START
);
1720 } else if (s
->type
== SERVICE_ONESHOT
||
1721 s
->type
== SERVICE_DBUS
||
1722 s
->type
== SERVICE_NOTIFY
) {
1724 /* For oneshot services we wait until the start
1725 * process exited, too, but it is our main process. */
1727 /* For D-Bus services we know the main pid right away,
1728 * but wait for the bus name to appear on the
1729 * bus. Notify services are similar. */
1731 service_set_main_pid(s
, pid
);
1732 service_set_state(s
, SERVICE_START
);
1734 assert_not_reached("Unknown service type");
1739 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1740 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1743 static void service_enter_start_pre(Service
*s
) {
1748 service_unwatch_control_pid(s
);
1750 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1751 if (s
->control_command
) {
1752 /* Before we start anything, let's clear up what might
1753 * be left from previous runs. */
1754 service_kill_control_processes(s
);
1756 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1758 r
= service_spawn(s
,
1760 s
->timeout_start_usec
,
1762 !s
->permissions_start_only
,
1763 !s
->root_directory_start_only
,
1770 service_set_state(s
, SERVICE_START_PRE
);
1772 service_enter_start(s
);
1777 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1778 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1781 static void service_enter_restart(Service
*s
) {
1782 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1787 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1788 /* Don't restart things if we are going down anyway */
1789 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1791 r
= service_arm_timer(s
, s
->restart_usec
);
1798 /* Any units that are bound to this service must also be
1799 * restarted. We use JOB_RESTART (instead of the more obvious
1800 * JOB_START) here so that those dependency jobs will be added
1802 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
1806 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1807 * it will be canceled as part of the service_stop() call that
1808 * is executed as part of JOB_RESTART. */
1810 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1814 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1815 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1818 static void service_enter_reload_by_notify(Service
*s
) {
1821 if (s
->timeout_start_usec
> 0)
1822 service_arm_timer(s
, s
->timeout_start_usec
);
1824 service_set_state(s
, SERVICE_RELOAD
);
1827 static void service_enter_reload(Service
*s
) {
1832 service_unwatch_control_pid(s
);
1833 s
->reload_result
= SERVICE_SUCCESS
;
1835 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1836 if (s
->control_command
) {
1837 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1839 r
= service_spawn(s
,
1841 s
->timeout_start_usec
,
1843 !s
->permissions_start_only
,
1844 !s
->root_directory_start_only
,
1851 service_set_state(s
, SERVICE_RELOAD
);
1853 service_enter_running(s
, SERVICE_SUCCESS
);
1858 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1859 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1860 service_enter_running(s
, SERVICE_SUCCESS
);
1863 static void service_run_next_control(Service
*s
) {
1867 assert(s
->control_command
);
1868 assert(s
->control_command
->command_next
);
1870 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1872 s
->control_command
= s
->control_command
->command_next
;
1873 service_unwatch_control_pid(s
);
1875 r
= service_spawn(s
,
1877 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1879 !s
->permissions_start_only
,
1880 !s
->root_directory_start_only
,
1881 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1882 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1891 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1893 if (s
->state
== SERVICE_START_PRE
)
1894 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1895 else if (s
->state
== SERVICE_STOP
)
1896 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1897 else if (s
->state
== SERVICE_STOP_POST
)
1898 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1899 else if (s
->state
== SERVICE_RELOAD
) {
1900 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1901 service_enter_running(s
, SERVICE_SUCCESS
);
1903 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1906 static void service_run_next_main(Service
*s
) {
1911 assert(s
->main_command
);
1912 assert(s
->main_command
->command_next
);
1913 assert(s
->type
== SERVICE_ONESHOT
);
1915 s
->main_command
= s
->main_command
->command_next
;
1916 service_unwatch_main_pid(s
);
1918 r
= service_spawn(s
,
1920 s
->timeout_start_usec
,
1930 service_set_main_pid(s
, pid
);
1935 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1936 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1939 static int service_start_limit_test(Service
*s
) {
1942 if (ratelimit_test(&s
->start_limit
))
1945 log_unit_warning(UNIT(s
), "Start request repeated too quickly.");
1947 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
1950 static int service_start(Unit
*u
) {
1951 Service
*s
= SERVICE(u
);
1956 /* We cannot fulfill this request right now, try again later
1958 if (IN_SET(s
->state
,
1959 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1960 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1963 /* Already on it! */
1964 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1967 /* A service that will be restarted must be stopped first to
1968 * trigger BindsTo and/or OnFailure dependencies. If a user
1969 * does not want to wait for the holdoff time to elapse, the
1970 * service should be manually restarted, not started. We
1971 * simply return EAGAIN here, so that any start jobs stay
1972 * queued, and assume that the auto restart timer will
1973 * eventually trigger the restart. */
1974 if (s
->state
== SERVICE_AUTO_RESTART
)
1977 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1979 /* Make sure we don't enter a busy loop of some kind. */
1980 r
= service_start_limit_test(s
);
1982 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
1986 s
->result
= SERVICE_SUCCESS
;
1987 s
->reload_result
= SERVICE_SUCCESS
;
1988 s
->main_pid_known
= false;
1989 s
->main_pid_alien
= false;
1990 s
->forbid_restart
= false;
1991 s
->reset_cpu_usage
= true;
1993 s
->status_text
= mfree(s
->status_text
);
1994 s
->status_errno
= 0;
1996 s
->notify_state
= NOTIFY_UNKNOWN
;
1998 service_enter_start_pre(s
);
2002 static int service_stop(Unit
*u
) {
2003 Service
*s
= SERVICE(u
);
2007 /* Don't create restart jobs from manual stops. */
2008 s
->forbid_restart
= true;
2011 if (IN_SET(s
->state
,
2012 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2013 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2016 /* A restart will be scheduled or is in progress. */
2017 if (s
->state
== SERVICE_AUTO_RESTART
) {
2018 service_set_state(s
, SERVICE_DEAD
);
2022 /* If there's already something running we go directly into
2024 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2025 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2029 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2031 service_enter_stop(s
, SERVICE_SUCCESS
);
2035 static int service_reload(Unit
*u
) {
2036 Service
*s
= SERVICE(u
);
2040 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2042 service_enter_reload(s
);
2046 _pure_
static bool service_can_reload(Unit
*u
) {
2047 Service
*s
= SERVICE(u
);
2051 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2054 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2055 Service
*s
= SERVICE(u
);
2063 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2064 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2065 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2067 if (s
->control_pid
> 0)
2068 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2070 if (s
->main_pid_known
&& s
->main_pid
> 0)
2071 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2073 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2074 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2076 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2080 /* FIXME: There's a minor uncleanliness here: if there are
2081 * multiple commands attached here, we will start from the
2082 * first one again */
2083 if (s
->control_command_id
>= 0)
2084 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2086 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2089 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2092 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2096 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2099 r
= unit_serialize_item_fd(u
, f
, fds
, "endpoint-fd", s
->bus_endpoint_fd
);
2103 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2104 _cleanup_free_
char *c
= NULL
;
2107 copy
= fdset_put_dup(fds
, fs
->fd
);
2111 c
= cescape(fs
->fdname
);
2113 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2116 if (s
->main_exec_status
.pid
> 0) {
2117 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2118 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2119 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2121 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2122 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2123 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2127 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2128 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2130 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2135 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2136 Service
*s
= SERVICE(u
);
2144 if (streq(key
, "state")) {
2147 state
= service_state_from_string(value
);
2149 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2151 s
->deserialized_state
= state
;
2152 } else if (streq(key
, "result")) {
2155 f
= service_result_from_string(value
);
2157 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2158 else if (f
!= SERVICE_SUCCESS
)
2161 } else if (streq(key
, "reload-result")) {
2164 f
= service_result_from_string(value
);
2166 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2167 else if (f
!= SERVICE_SUCCESS
)
2168 s
->reload_result
= f
;
2170 } else if (streq(key
, "control-pid")) {
2173 if (parse_pid(value
, &pid
) < 0)
2174 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2176 s
->control_pid
= pid
;
2177 } else if (streq(key
, "main-pid")) {
2180 if (parse_pid(value
, &pid
) < 0)
2181 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2183 service_set_main_pid(s
, pid
);
2184 unit_watch_pid(UNIT(s
), pid
);
2186 } else if (streq(key
, "main-pid-known")) {
2189 b
= parse_boolean(value
);
2191 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2193 s
->main_pid_known
= b
;
2194 } else if (streq(key
, "bus-name-good")) {
2197 b
= parse_boolean(value
);
2199 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2201 s
->bus_name_good
= b
;
2202 } else if (streq(key
, "status-text")) {
2205 r
= cunescape(value
, 0, &t
);
2207 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2209 free(s
->status_text
);
2213 } else if (streq(key
, "control-command")) {
2214 ServiceExecCommand id
;
2216 id
= service_exec_command_from_string(value
);
2218 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2220 s
->control_command_id
= id
;
2221 s
->control_command
= s
->exec_command
[id
];
2223 } else if (streq(key
, "socket-fd")) {
2226 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2227 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2229 asynchronous_close(s
->socket_fd
);
2230 s
->socket_fd
= fdset_remove(fds
, fd
);
2232 } else if (streq(key
, "endpoint-fd")) {
2235 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2236 log_unit_debug(u
, "Failed to parse endpoint-fd value: %s", value
);
2238 safe_close(s
->bus_endpoint_fd
);
2239 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2241 } else if (streq(key
, "fd-store-fd")) {
2246 pf
= strcspn(value
, WHITESPACE
);
2247 fdv
= strndupa(value
, pf
);
2249 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2250 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2252 _cleanup_free_
char *t
= NULL
;
2256 fdn
+= strspn(fdn
, WHITESPACE
);
2257 (void) cunescape(fdn
, 0, &t
);
2259 r
= service_add_fd_store(s
, fd
, t
);
2261 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2263 fdset_remove(fds
, fd
);
2266 } else if (streq(key
, "main-exec-status-pid")) {
2269 if (parse_pid(value
, &pid
) < 0)
2270 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2272 s
->main_exec_status
.pid
= pid
;
2273 } else if (streq(key
, "main-exec-status-code")) {
2276 if (safe_atoi(value
, &i
) < 0)
2277 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2279 s
->main_exec_status
.code
= i
;
2280 } else if (streq(key
, "main-exec-status-status")) {
2283 if (safe_atoi(value
, &i
) < 0)
2284 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2286 s
->main_exec_status
.status
= i
;
2287 } else if (streq(key
, "main-exec-status-start"))
2288 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2289 else if (streq(key
, "main-exec-status-exit"))
2290 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2291 else if (streq(key
, "watchdog-timestamp"))
2292 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2293 else if (streq(key
, "forbid-restart")) {
2296 b
= parse_boolean(value
);
2298 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2300 s
->forbid_restart
= b
;
2301 } else if (streq(key
, "stdin-fd")) {
2304 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2305 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2307 asynchronous_close(s
->stdin_fd
);
2308 s
->stdin_fd
= fdset_remove(fds
, fd
);
2310 } else if (streq(key
, "stdout-fd")) {
2313 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2314 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2316 asynchronous_close(s
->stdout_fd
);
2317 s
->stdout_fd
= fdset_remove(fds
, fd
);
2319 } else if (streq(key
, "stderr-fd")) {
2322 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2323 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2325 asynchronous_close(s
->stderr_fd
);
2326 s
->stderr_fd
= fdset_remove(fds
, fd
);
2329 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2334 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2335 const UnitActiveState
*table
;
2339 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2341 return table
[SERVICE(u
)->state
];
2344 static const char *service_sub_state_to_string(Unit
*u
) {
2347 return service_state_to_string(SERVICE(u
)->state
);
2350 static bool service_check_gc(Unit
*u
) {
2351 Service
*s
= SERVICE(u
);
2355 /* Never clean up services that still have a process around,
2356 * even if the service is formally dead. */
2357 if (cgroup_good(s
) > 0 ||
2358 main_pid_good(s
) > 0 ||
2359 control_pid_good(s
) > 0)
2365 _pure_
static bool service_check_snapshot(Unit
*u
) {
2366 Service
*s
= SERVICE(u
);
2370 return s
->socket_fd
< 0;
2373 static int service_retry_pid_file(Service
*s
) {
2376 assert(s
->pid_file
);
2377 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2379 r
= service_load_pid_file(s
, false);
2383 service_unwatch_pid_file(s
);
2385 service_enter_running(s
, SERVICE_SUCCESS
);
2389 static int service_watch_pid_file(Service
*s
) {
2392 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2394 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2398 /* the pidfile might have appeared just before we set the watch */
2399 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2400 service_retry_pid_file(s
);
2404 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2405 service_unwatch_pid_file(s
);
2409 static int service_demand_pid_file(Service
*s
) {
2412 assert(s
->pid_file
);
2413 assert(!s
->pid_file_pathspec
);
2415 ps
= new0(PathSpec
, 1);
2420 ps
->path
= strdup(s
->pid_file
);
2426 path_kill_slashes(ps
->path
);
2428 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2429 * keep their PID file open all the time. */
2430 ps
->type
= PATH_MODIFIED
;
2431 ps
->inotify_fd
= -1;
2433 s
->pid_file_pathspec
= ps
;
2435 return service_watch_pid_file(s
);
2438 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2439 PathSpec
*p
= userdata
;
2444 s
= SERVICE(p
->unit
);
2448 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2449 assert(s
->pid_file_pathspec
);
2450 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2452 log_unit_debug(UNIT(s
), "inotify event");
2454 if (path_spec_fd_event(p
, events
) < 0)
2457 if (service_retry_pid_file(s
) == 0)
2460 if (service_watch_pid_file(s
) < 0)
2466 service_unwatch_pid_file(s
);
2467 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2471 static void service_notify_cgroup_empty_event(Unit
*u
) {
2472 Service
*s
= SERVICE(u
);
2476 log_unit_debug(u
, "cgroup is empty");
2480 /* Waiting for SIGCHLD is usually more interesting,
2481 * because it includes return codes/signals. Which is
2482 * why we ignore the cgroup events for most cases,
2483 * except when we don't know pid which to expect the
2487 case SERVICE_START_POST
:
2488 /* If we were hoping for the daemon to write its PID file,
2489 * we can give up now. */
2490 if (s
->pid_file_pathspec
) {
2491 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2493 service_unwatch_pid_file(s
);
2494 if (s
->state
== SERVICE_START
)
2495 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2497 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2501 case SERVICE_RUNNING
:
2502 /* service_enter_running() will figure out what to do */
2503 service_enter_running(s
, SERVICE_SUCCESS
);
2506 case SERVICE_STOP_SIGABRT
:
2507 case SERVICE_STOP_SIGTERM
:
2508 case SERVICE_STOP_SIGKILL
:
2510 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2511 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2515 case SERVICE_STOP_POST
:
2516 case SERVICE_FINAL_SIGTERM
:
2517 case SERVICE_FINAL_SIGKILL
:
2518 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2519 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2528 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2529 Service
*s
= SERVICE(u
);
2535 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2536 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2537 f
= SERVICE_SUCCESS
;
2538 else if (code
== CLD_EXITED
)
2539 f
= SERVICE_FAILURE_EXIT_CODE
;
2540 else if (code
== CLD_KILLED
)
2541 f
= SERVICE_FAILURE_SIGNAL
;
2542 else if (code
== CLD_DUMPED
)
2543 f
= SERVICE_FAILURE_CORE_DUMP
;
2545 assert_not_reached("Unknown code");
2547 if (s
->main_pid
== pid
) {
2548 /* Forking services may occasionally move to a new PID.
2549 * As long as they update the PID file before exiting the old
2550 * PID, they're fine. */
2551 if (service_load_pid_file(s
, false) == 0)
2555 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2557 if (s
->main_command
) {
2558 /* If this is not a forking service than the
2559 * main process got started and hence we copy
2560 * the exit status so that it is recorded both
2561 * as main and as control process exit
2564 s
->main_command
->exec_status
= s
->main_exec_status
;
2566 if (s
->main_command
->ignore
)
2567 f
= SERVICE_SUCCESS
;
2568 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2570 /* If this is a forked process, then we should
2571 * ignore the return value if this was
2572 * configured for the starter process */
2574 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2575 f
= SERVICE_SUCCESS
;
2578 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2580 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2581 sigchld_code_to_string(code
), status
,
2582 strna(code
== CLD_EXITED
2583 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2584 : signal_to_string(status
))),
2585 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2586 "EXIT_STATUS=%i", status
,
2589 if (f
!= SERVICE_SUCCESS
)
2592 if (s
->main_command
&&
2593 s
->main_command
->command_next
&&
2594 f
== SERVICE_SUCCESS
) {
2596 /* There is another command to *
2597 * execute, so let's do that. */
2599 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2600 service_run_next_main(s
);
2604 /* The service exited, so the service is officially
2606 s
->main_command
= NULL
;
2610 case SERVICE_START_POST
:
2611 case SERVICE_RELOAD
:
2613 /* Need to wait until the operation is
2618 if (s
->type
== SERVICE_ONESHOT
) {
2619 /* This was our main goal, so let's go on */
2620 if (f
== SERVICE_SUCCESS
)
2621 service_enter_start_post(s
);
2623 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2629 case SERVICE_RUNNING
:
2630 service_enter_running(s
, f
);
2633 case SERVICE_STOP_SIGABRT
:
2634 case SERVICE_STOP_SIGTERM
:
2635 case SERVICE_STOP_SIGKILL
:
2637 if (!control_pid_good(s
))
2638 service_enter_stop_post(s
, f
);
2640 /* If there is still a control process, wait for that first */
2643 case SERVICE_STOP_POST
:
2644 case SERVICE_FINAL_SIGTERM
:
2645 case SERVICE_FINAL_SIGKILL
:
2647 if (!control_pid_good(s
))
2648 service_enter_dead(s
, f
, true);
2652 assert_not_reached("Uh, main process died at wrong time.");
2656 } else if (s
->control_pid
== pid
) {
2659 if (s
->control_command
) {
2660 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2662 if (s
->control_command
->ignore
)
2663 f
= SERVICE_SUCCESS
;
2666 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2667 "Control process exited, code=%s status=%i",
2668 sigchld_code_to_string(code
), status
);
2670 if (f
!= SERVICE_SUCCESS
)
2673 /* Immediately get rid of the cgroup, so that the
2674 * kernel doesn't delay the cgroup empty messages for
2675 * the service cgroup any longer than necessary */
2676 service_kill_control_processes(s
);
2678 if (s
->control_command
&&
2679 s
->control_command
->command_next
&&
2680 f
== SERVICE_SUCCESS
) {
2682 /* There is another command to *
2683 * execute, so let's do that. */
2685 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2686 service_run_next_control(s
);
2689 /* No further commands for this step, so let's
2690 * figure out what to do next */
2692 s
->control_command
= NULL
;
2693 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2695 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2699 case SERVICE_START_PRE
:
2700 if (f
== SERVICE_SUCCESS
)
2701 service_enter_start(s
);
2703 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2707 if (s
->type
!= SERVICE_FORKING
)
2708 /* Maybe spurious event due to a reload that changed the type? */
2711 if (f
!= SERVICE_SUCCESS
) {
2712 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2717 bool has_start_post
;
2720 /* Let's try to load the pid file here if we can.
2721 * The PID file might actually be created by a START_POST
2722 * script. In that case don't worry if the loading fails. */
2724 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2725 r
= service_load_pid_file(s
, !has_start_post
);
2726 if (!has_start_post
&& r
< 0) {
2727 r
= service_demand_pid_file(s
);
2728 if (r
< 0 || !cgroup_good(s
))
2729 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2733 (void) service_search_main_pid(s
);
2735 service_enter_start_post(s
);
2738 case SERVICE_START_POST
:
2739 if (f
!= SERVICE_SUCCESS
) {
2740 service_enter_stop(s
, f
);
2747 r
= service_load_pid_file(s
, true);
2749 r
= service_demand_pid_file(s
);
2750 if (r
< 0 || !cgroup_good(s
))
2751 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2755 (void) service_search_main_pid(s
);
2757 service_enter_running(s
, SERVICE_SUCCESS
);
2760 case SERVICE_RELOAD
:
2761 if (f
== SERVICE_SUCCESS
) {
2762 service_load_pid_file(s
, true);
2763 (void) service_search_main_pid(s
);
2766 s
->reload_result
= f
;
2767 service_enter_running(s
, SERVICE_SUCCESS
);
2771 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2774 case SERVICE_STOP_SIGABRT
:
2775 case SERVICE_STOP_SIGTERM
:
2776 case SERVICE_STOP_SIGKILL
:
2777 if (main_pid_good(s
) <= 0)
2778 service_enter_stop_post(s
, f
);
2780 /* If there is still a service
2781 * process around, wait until
2782 * that one quit, too */
2785 case SERVICE_STOP_POST
:
2786 case SERVICE_FINAL_SIGTERM
:
2787 case SERVICE_FINAL_SIGKILL
:
2788 if (main_pid_good(s
) <= 0)
2789 service_enter_dead(s
, f
, true);
2793 assert_not_reached("Uh, control process died at wrong time.");
2798 /* Notify clients about changed exit status */
2799 unit_add_to_dbus_queue(u
);
2801 /* We got one SIGCHLD for the service, let's watch all
2802 * processes that are now running of the service, and watch
2803 * that. Among the PIDs we then watch will be children
2804 * reassigned to us, which hopefully allows us to identify
2805 * when all children are gone */
2806 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2807 unit_watch_all_pids(u
);
2809 /* If the PID set is empty now, then let's finish this off */
2810 if (set_isempty(u
->pids
))
2811 service_notify_cgroup_empty_event(u
);
2814 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2815 Service
*s
= SERVICE(userdata
);
2818 assert(source
== s
->timer_event_source
);
2822 case SERVICE_START_PRE
:
2824 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2825 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2828 case SERVICE_START_POST
:
2829 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2830 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2833 case SERVICE_RELOAD
:
2834 log_unit_warning(UNIT(s
), "Reload operation timed out. Stopping.");
2835 service_unwatch_control_pid(s
);
2836 service_kill_control_processes(s
);
2837 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2838 service_enter_running(s
, SERVICE_SUCCESS
);
2842 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2843 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2846 case SERVICE_STOP_SIGABRT
:
2847 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2848 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2851 case SERVICE_STOP_SIGTERM
:
2852 if (s
->kill_context
.send_sigkill
) {
2853 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2854 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2856 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2857 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2862 case SERVICE_STOP_SIGKILL
:
2863 /* Uh, we sent a SIGKILL and it is still not gone?
2864 * Must be something we cannot kill, so let's just be
2865 * weirded out and continue */
2867 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2868 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2871 case SERVICE_STOP_POST
:
2872 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2873 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2876 case SERVICE_FINAL_SIGTERM
:
2877 if (s
->kill_context
.send_sigkill
) {
2878 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2879 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2881 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2882 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2887 case SERVICE_FINAL_SIGKILL
:
2888 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2889 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2892 case SERVICE_AUTO_RESTART
:
2893 log_unit_info(UNIT(s
),
2894 s
->restart_usec
> 0 ?
2895 "Service hold-off time over, scheduling restart." :
2896 "Service has no hold-off time, scheduling restart.");
2897 service_enter_restart(s
);
2901 assert_not_reached("Timeout at wrong time.");
2907 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2908 Service
*s
= SERVICE(userdata
);
2909 char t
[FORMAT_TIMESPAN_MAX
];
2912 assert(source
== s
->watchdog_event_source
);
2914 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2915 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2917 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2922 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2923 Service
*s
= SERVICE(u
);
2924 _cleanup_free_
char *cc
= NULL
;
2925 bool notify_dbus
= false;
2930 cc
= strv_join(tags
, ", ");
2932 if (s
->notify_access
== NOTIFY_NONE
) {
2933 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2935 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2936 if (s
->main_pid
!= 0)
2937 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2939 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2942 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2944 /* Interpret MAINPID= */
2945 e
= strv_find_startswith(tags
, "MAINPID=");
2946 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2947 if (parse_pid(e
, &pid
) < 0)
2948 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2950 service_set_main_pid(s
, pid
);
2951 unit_watch_pid(UNIT(s
), pid
);
2956 /* Interpret RELOADING= */
2957 if (strv_find(tags
, "RELOADING=1")) {
2959 s
->notify_state
= NOTIFY_RELOADING
;
2961 if (s
->state
== SERVICE_RUNNING
)
2962 service_enter_reload_by_notify(s
);
2967 /* Interpret READY= */
2968 if (strv_find(tags
, "READY=1")) {
2970 s
->notify_state
= NOTIFY_READY
;
2972 /* Type=notify services inform us about completed
2973 * initialization with READY=1 */
2974 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2975 service_enter_start_post(s
);
2977 /* Sending READY=1 while we are reloading informs us
2978 * that the reloading is complete */
2979 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2980 service_enter_running(s
, SERVICE_SUCCESS
);
2985 /* Interpret STOPPING= */
2986 if (strv_find(tags
, "STOPPING=1")) {
2988 s
->notify_state
= NOTIFY_STOPPING
;
2990 if (s
->state
== SERVICE_RUNNING
)
2991 service_enter_stop_by_notify(s
);
2996 /* Interpret STATUS= */
2997 e
= strv_find_startswith(tags
, "STATUS=");
2999 _cleanup_free_
char *t
= NULL
;
3002 if (!utf8_is_valid(e
))
3003 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3011 if (!streq_ptr(s
->status_text
, t
)) {
3013 free(s
->status_text
);
3021 /* Interpret ERRNO= */
3022 e
= strv_find_startswith(tags
, "ERRNO=");
3026 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3027 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3029 if (s
->status_errno
!= status_errno
) {
3030 s
->status_errno
= status_errno
;
3036 /* Interpret WATCHDOG= */
3037 if (strv_find(tags
, "WATCHDOG=1"))
3038 service_reset_watchdog(s
);
3040 if (strv_find(tags
, "FDSTORE=1")) {
3043 name
= strv_find_startswith(tags
, "FDNAME=");
3044 if (name
&& !fdname_is_valid(name
)) {
3045 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3049 service_add_fd_store_set(s
, fds
, name
);
3052 /* Notify clients about changed status or main pid */
3054 unit_add_to_dbus_queue(u
);
3057 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
3058 Service
*s
= SERVICE(u
);
3061 if (!s
->timer_event_source
)
3064 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
3071 static void service_bus_name_owner_change(
3074 const char *old_owner
,
3075 const char *new_owner
) {
3077 Service
*s
= SERVICE(u
);
3083 assert(streq(s
->bus_name
, name
));
3084 assert(old_owner
|| new_owner
);
3086 if (old_owner
&& new_owner
)
3087 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3089 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3091 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3093 s
->bus_name_good
= !!new_owner
;
3095 if (s
->type
== SERVICE_DBUS
) {
3097 /* service_enter_running() will figure out what to
3099 if (s
->state
== SERVICE_RUNNING
)
3100 service_enter_running(s
, SERVICE_SUCCESS
);
3101 else if (s
->state
== SERVICE_START
&& new_owner
)
3102 service_enter_start_post(s
);
3104 } else if (new_owner
&&
3106 (s
->state
== SERVICE_START
||
3107 s
->state
== SERVICE_START_POST
||
3108 s
->state
== SERVICE_RUNNING
||
3109 s
->state
== SERVICE_RELOAD
)) {
3111 _cleanup_bus_creds_unref_ sd_bus_creds
*creds
= NULL
;
3114 /* Try to acquire PID from bus service */
3116 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3118 r
= sd_bus_creds_get_pid(creds
, &pid
);
3120 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3122 service_set_main_pid(s
, pid
);
3123 unit_watch_pid(UNIT(s
), pid
);
3128 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3129 _cleanup_free_
char *peer
= NULL
;
3135 /* This is called by the socket code when instantiating a new
3136 * service for a stream socket and the socket needs to be
3139 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3142 if (s
->socket_fd
>= 0)
3145 if (s
->state
!= SERVICE_DEAD
)
3148 if (getpeername_pretty(fd
, &peer
) >= 0) {
3150 if (UNIT(s
)->description
) {
3151 _cleanup_free_
char *a
;
3153 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3157 r
= unit_set_description(UNIT(s
), a
);
3159 r
= unit_set_description(UNIT(s
), peer
);
3166 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3168 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3170 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3173 static void service_reset_failed(Unit
*u
) {
3174 Service
*s
= SERVICE(u
);
3178 if (s
->state
== SERVICE_FAILED
)
3179 service_set_state(s
, SERVICE_DEAD
);
3181 s
->result
= SERVICE_SUCCESS
;
3182 s
->reload_result
= SERVICE_SUCCESS
;
3184 RATELIMIT_RESET(s
->start_limit
);
3187 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3188 Service
*s
= SERVICE(u
);
3190 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3193 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3194 [SERVICE_RESTART_NO
] = "no",
3195 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3196 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3197 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3198 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3199 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3200 [SERVICE_RESTART_ALWAYS
] = "always",
3203 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3205 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3206 [SERVICE_SIMPLE
] = "simple",
3207 [SERVICE_FORKING
] = "forking",
3208 [SERVICE_ONESHOT
] = "oneshot",
3209 [SERVICE_DBUS
] = "dbus",
3210 [SERVICE_NOTIFY
] = "notify",
3211 [SERVICE_IDLE
] = "idle"
3214 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3216 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3217 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3218 [SERVICE_EXEC_START
] = "ExecStart",
3219 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3220 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3221 [SERVICE_EXEC_STOP
] = "ExecStop",
3222 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3225 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3227 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3228 [NOTIFY_NONE
] = "none",
3229 [NOTIFY_MAIN
] = "main",
3230 [NOTIFY_ALL
] = "all"
3233 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3235 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3236 [NOTIFY_UNKNOWN
] = "unknown",
3237 [NOTIFY_READY
] = "ready",
3238 [NOTIFY_RELOADING
] = "reloading",
3239 [NOTIFY_STOPPING
] = "stopping",
3242 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3244 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3245 [SERVICE_SUCCESS
] = "success",
3246 [SERVICE_FAILURE_RESOURCES
] = "resources",
3247 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3248 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3249 [SERVICE_FAILURE_SIGNAL
] = "signal",
3250 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3251 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3252 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3255 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3257 const UnitVTable service_vtable
= {
3258 .object_size
= sizeof(Service
),
3259 .exec_context_offset
= offsetof(Service
, exec_context
),
3260 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3261 .kill_context_offset
= offsetof(Service
, kill_context
),
3262 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3268 .private_section
= "Service",
3270 .init
= service_init
,
3271 .done
= service_done
,
3272 .load
= service_load
,
3273 .release_resources
= service_release_resources
,
3275 .coldplug
= service_coldplug
,
3277 .dump
= service_dump
,
3279 .start
= service_start
,
3280 .stop
= service_stop
,
3281 .reload
= service_reload
,
3283 .can_reload
= service_can_reload
,
3285 .kill
= service_kill
,
3287 .serialize
= service_serialize
,
3288 .deserialize_item
= service_deserialize_item
,
3290 .active_state
= service_active_state
,
3291 .sub_state_to_string
= service_sub_state_to_string
,
3293 .check_gc
= service_check_gc
,
3294 .check_snapshot
= service_check_snapshot
,
3296 .sigchld_event
= service_sigchld_event
,
3298 .reset_failed
= service_reset_failed
,
3300 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3301 .notify_message
= service_notify_message
,
3303 .bus_name_owner_change
= service_bus_name_owner_change
,
3305 .bus_vtable
= bus_service_vtable
,
3306 .bus_set_property
= bus_service_set_property
,
3307 .bus_commit_properties
= bus_service_commit_properties
,
3309 .get_timeout
= service_get_timeout
,
3310 .can_transient
= true,
3312 .status_message_formats
= {
3313 .starting_stopping
= {
3314 [0] = "Starting %s...",
3315 [1] = "Stopping %s...",
3317 .finished_start_job
= {
3318 [JOB_DONE
] = "Started %s.",
3319 [JOB_FAILED
] = "Failed to start %s.",
3321 .finished_stop_job
= {
3322 [JOB_DONE
] = "Stopped %s.",
3323 [JOB_FAILED
] = "Stopped (with error) %s.",