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/>.
26 #include "alloc-util.h"
28 #include "bus-error.h"
29 #include "bus-kernel.h"
31 #include "dbus-service.h"
35 #include "exit-status.h"
38 #include "formats-util.h"
40 #include "load-dropin.h"
41 #include "load-fragment.h"
44 #include "parse-util.h"
45 #include "path-util.h"
46 #include "process-util.h"
48 #include "signal-util.h"
50 #include "string-table.h"
51 #include "string-util.h"
53 #include "unit-name.h"
54 #include "unit-printf.h"
59 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
60 [SERVICE_DEAD
] = UNIT_INACTIVE
,
61 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
62 [SERVICE_START
] = UNIT_ACTIVATING
,
63 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
64 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
65 [SERVICE_EXITED
] = UNIT_ACTIVE
,
66 [SERVICE_RELOAD
] = UNIT_RELOADING
,
67 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
68 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
69 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
70 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
71 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
72 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
73 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
74 [SERVICE_FAILED
] = UNIT_FAILED
,
75 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
78 /* For Type=idle we never want to delay any other jobs, hence we
79 * consider idle jobs active as soon as we start working on them */
80 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
81 [SERVICE_DEAD
] = UNIT_INACTIVE
,
82 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
83 [SERVICE_START
] = UNIT_ACTIVE
,
84 [SERVICE_START_POST
] = UNIT_ACTIVE
,
85 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
86 [SERVICE_EXITED
] = UNIT_ACTIVE
,
87 [SERVICE_RELOAD
] = UNIT_RELOADING
,
88 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
89 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
90 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
91 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
92 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
93 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
94 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
95 [SERVICE_FAILED
] = UNIT_FAILED
,
96 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
99 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
100 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
101 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
103 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
104 static void service_enter_reload_by_notify(Service
*s
);
106 static void service_init(Unit
*u
) {
107 Service
*s
= SERVICE(u
);
110 assert(u
->load_state
== UNIT_STUB
);
112 s
->timeout_start_usec
= u
->manager
->default_timeout_start_usec
;
113 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
114 s
->restart_usec
= u
->manager
->default_restart_usec
;
115 s
->type
= _SERVICE_TYPE_INVALID
;
117 s
->bus_endpoint_fd
= -1;
118 s
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -1;
119 s
->guess_main_pid
= true;
121 RATELIMIT_INIT(s
->start_limit
, u
->manager
->default_start_limit_interval
, u
->manager
->default_start_limit_burst
);
123 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
126 static void service_unwatch_control_pid(Service
*s
) {
129 if (s
->control_pid
<= 0)
132 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
136 static void service_unwatch_main_pid(Service
*s
) {
139 if (s
->main_pid
<= 0)
142 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
146 static void service_unwatch_pid_file(Service
*s
) {
147 if (!s
->pid_file_pathspec
)
150 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
151 path_spec_unwatch(s
->pid_file_pathspec
);
152 path_spec_done(s
->pid_file_pathspec
);
153 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
156 static int service_set_main_pid(Service
*s
, pid_t pid
) {
167 if (s
->main_pid
== pid
&& s
->main_pid_known
)
170 if (s
->main_pid
!= pid
) {
171 service_unwatch_main_pid(s
);
172 exec_status_start(&s
->main_exec_status
, pid
);
176 s
->main_pid_known
= true;
178 if (get_process_ppid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
179 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
180 s
->main_pid_alien
= true;
182 s
->main_pid_alien
= false;
187 static void service_close_socket_fd(Service
*s
) {
190 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
193 static void service_connection_unref(Service
*s
) {
196 if (!UNIT_ISSET(s
->accept_socket
))
199 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
200 unit_ref_unset(&s
->accept_socket
);
203 static void service_stop_watchdog(Service
*s
) {
206 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
207 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
210 static void service_start_watchdog(Service
*s
) {
215 if (s
->watchdog_usec
<= 0)
218 if (s
->watchdog_event_source
) {
219 r
= sd_event_source_set_time(s
->watchdog_event_source
, s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
);
221 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
225 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
227 r
= sd_event_add_time(
228 UNIT(s
)->manager
->event
,
229 &s
->watchdog_event_source
,
231 s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
, 0,
232 service_dispatch_watchdog
, s
);
234 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
238 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
240 /* Let's process everything else which might be a sign
241 * of living before we consider a service died. */
242 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
246 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
249 static void service_reset_watchdog(Service
*s
) {
252 dual_timestamp_get(&s
->watchdog_timestamp
);
253 service_start_watchdog(s
);
256 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
262 assert(fs
->service
->n_fd_store
> 0);
263 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
264 fs
->service
->n_fd_store
--;
267 if (fs
->event_source
) {
268 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
269 sd_event_source_unref(fs
->event_source
);
277 static void service_release_resources(Unit
*u
) {
278 Service
*s
= SERVICE(u
);
282 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
285 log_unit_debug(u
, "Releasing all resources.");
287 s
->stdin_fd
= safe_close(s
->stdin_fd
);
288 s
->stdout_fd
= safe_close(s
->stdout_fd
);
289 s
->stderr_fd
= safe_close(s
->stderr_fd
);
292 service_fd_store_unlink(s
->fd_store
);
294 assert(s
->n_fd_store
== 0);
297 static void service_done(Unit
*u
) {
298 Service
*s
= SERVICE(u
);
302 s
->pid_file
= mfree(s
->pid_file
);
303 s
->status_text
= mfree(s
->status_text
);
304 s
->reboot_arg
= mfree(s
->reboot_arg
);
306 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
307 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
308 s
->control_command
= NULL
;
309 s
->main_command
= NULL
;
311 exit_status_set_free(&s
->restart_prevent_status
);
312 exit_status_set_free(&s
->restart_force_status
);
313 exit_status_set_free(&s
->success_status
);
315 /* This will leak a process, but at least no memory or any of
317 service_unwatch_main_pid(s
);
318 service_unwatch_control_pid(s
);
319 service_unwatch_pid_file(s
);
322 unit_unwatch_bus_name(u
, s
->bus_name
);
323 s
->bus_name
= mfree(s
->bus_name
);
326 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
328 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
329 service_close_socket_fd(s
);
330 service_connection_unref(s
);
332 unit_ref_unset(&s
->accept_socket
);
334 service_stop_watchdog(s
);
336 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
338 service_release_resources(u
);
341 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
342 ServiceFDStore
*fs
= userdata
;
347 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
348 service_fd_store_unlink(fs
);
352 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
359 if (s
->n_fd_store
>= s
->n_fd_store_max
)
362 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
363 r
= same_fd(fs
->fd
, fd
);
367 /* Already included */
373 fs
= new0(ServiceFDStore
, 1);
379 fs
->fdname
= strdup(name
?: "stored");
385 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
392 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
394 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
400 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
405 if (fdset_size(fds
) <= 0)
408 while (s
->n_fd_store
< s
->n_fd_store_max
) {
409 _cleanup_close_
int fd
= -1;
411 fd
= fdset_steal_first(fds
);
415 r
= service_add_fd_store(s
, fd
, name
);
417 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
419 log_unit_debug(UNIT(s
), "Added fd to fd store.");
424 if (fdset_size(fds
) > 0)
425 log_unit_warning(UNIT(s
), "Tried to store more fds than FileDescriptorStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
430 static int service_arm_timer(Service
*s
, usec_t usec
) {
435 if (s
->timer_event_source
) {
436 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
440 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
443 r
= sd_event_add_time(
444 UNIT(s
)->manager
->event
,
445 &s
->timer_event_source
,
447 now(CLOCK_MONOTONIC
) + usec
, 0,
448 service_dispatch_timer
, s
);
452 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
457 static int service_verify(Service
*s
) {
460 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
463 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
464 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
468 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
469 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
473 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
474 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
478 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
479 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
483 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
484 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
488 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
489 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
493 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
494 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
498 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
499 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
501 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
502 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
506 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
507 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
509 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
510 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
515 static int service_add_default_dependencies(Service
*s
) {
520 if (!UNIT(s
)->default_dependencies
)
523 /* Add a number of automatic dependencies useful for the
524 * majority of services. */
526 if (UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
) {
527 /* First, pull in the really early boot stuff, and
528 * require it, so that we fail if we can't acquire
531 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true);
536 /* In the --user instance there's no sysinit.target,
537 * in that case require basic.target instead. */
539 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
544 /* Second, if the rest of the base system is in the same
545 * transaction, order us after it, but do not pull it in or
546 * even require it. */
547 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true);
551 /* Third, add us in for normal shutdown. */
552 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
555 static void service_fix_output(Service
*s
) {
558 /* If nothing has been explicitly configured, patch default
559 * output in. If input is socket/tty we avoid this however,
560 * since in that case we want output to default to the same
561 * place as we read input from. */
563 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
564 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
565 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
566 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
568 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
569 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
570 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
573 static int service_setup_bus_name(Service
*s
) {
581 if (is_kdbus_available()) {
584 n
= strjoina(s
->bus_name
, ".busname");
585 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
587 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency to .busname unit: %m");
590 /* If kdbus is not available, we know the dbus socket is required, hence pull it in, and require it */
591 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true);
593 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
596 /* Regardless if kdbus is used or not, we always want to be ordered against dbus.socket if both are in the transaction. */
597 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, NULL
, true);
599 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
601 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
603 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
605 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
610 static int service_add_extras(Service
*s
) {
615 if (s
->type
== _SERVICE_TYPE_INVALID
) {
616 /* Figure out a type automatically */
618 s
->type
= SERVICE_DBUS
;
619 else if (s
->exec_command
[SERVICE_EXEC_START
])
620 s
->type
= SERVICE_SIMPLE
;
622 s
->type
= SERVICE_ONESHOT
;
625 /* Oneshot services have disabled start timeout by default */
626 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
627 s
->timeout_start_usec
= 0;
629 service_fix_output(s
);
631 r
= unit_patch_contexts(UNIT(s
));
635 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
639 r
= unit_set_default_slice(UNIT(s
));
643 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
644 s
->notify_access
= NOTIFY_MAIN
;
646 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
647 s
->notify_access
= NOTIFY_MAIN
;
649 r
= service_add_default_dependencies(s
);
653 r
= service_setup_bus_name(s
);
660 static int service_load(Unit
*u
) {
661 Service
*s
= SERVICE(u
);
666 /* Load a .service file */
667 r
= unit_load_fragment(u
);
671 /* Still nothing found? Then let's give up */
672 if (u
->load_state
== UNIT_STUB
)
675 /* This is a new unit? Then let's add in some extras */
676 if (u
->load_state
== UNIT_LOADED
) {
678 /* We were able to load something, then let's add in
679 * the dropin directories. */
680 r
= unit_load_dropin(u
);
684 /* This is a new unit? Then let's add in some
686 r
= service_add_extras(s
);
691 return service_verify(s
);
694 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
695 ServiceExecCommand c
;
696 Service
*s
= SERVICE(u
);
701 prefix
= strempty(prefix
);
702 prefix2
= strjoina(prefix
, "\t");
705 "%sService State: %s\n"
707 "%sReload Result: %s\n"
708 "%sPermissionsStartOnly: %s\n"
709 "%sRootDirectoryStartOnly: %s\n"
710 "%sRemainAfterExit: %s\n"
711 "%sGuessMainPID: %s\n"
714 "%sNotifyAccess: %s\n"
715 "%sNotifyState: %s\n",
716 prefix
, service_state_to_string(s
->state
),
717 prefix
, service_result_to_string(s
->result
),
718 prefix
, service_result_to_string(s
->reload_result
),
719 prefix
, yes_no(s
->permissions_start_only
),
720 prefix
, yes_no(s
->root_directory_start_only
),
721 prefix
, yes_no(s
->remain_after_exit
),
722 prefix
, yes_no(s
->guess_main_pid
),
723 prefix
, service_type_to_string(s
->type
),
724 prefix
, service_restart_to_string(s
->restart
),
725 prefix
, notify_access_to_string(s
->notify_access
),
726 prefix
, notify_state_to_string(s
->notify_state
));
728 if (s
->control_pid
> 0)
730 "%sControl PID: "PID_FMT
"\n",
731 prefix
, s
->control_pid
);
735 "%sMain PID: "PID_FMT
"\n"
736 "%sMain PID Known: %s\n"
737 "%sMain PID Alien: %s\n",
739 prefix
, yes_no(s
->main_pid_known
),
740 prefix
, yes_no(s
->main_pid_alien
));
745 prefix
, s
->pid_file
);
750 "%sBus Name Good: %s\n",
752 prefix
, yes_no(s
->bus_name_good
));
754 kill_context_dump(&s
->kill_context
, f
, prefix
);
755 exec_context_dump(&s
->exec_context
, f
, prefix
);
757 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
759 if (!s
->exec_command
[c
])
762 fprintf(f
, "%s-> %s:\n",
763 prefix
, service_exec_command_to_string(c
));
765 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
769 fprintf(f
, "%sStatus Text: %s\n",
770 prefix
, s
->status_text
);
772 if (s
->n_fd_store_max
> 0)
774 "%sFile Descriptor Store Max: %u\n"
775 "%sFile Descriptor Store Current: %u\n",
776 prefix
, s
->n_fd_store_max
,
777 prefix
, s
->n_fd_store
);
780 static int service_load_pid_file(Service
*s
, bool may_warn
) {
781 _cleanup_free_
char *k
= NULL
;
790 r
= read_one_line_file(s
->pid_file
, &k
);
793 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
));
797 r
= parse_pid(k
, &pid
);
800 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
804 if (!pid_is_alive(pid
)) {
806 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
810 if (s
->main_pid_known
) {
811 if (pid
== s
->main_pid
)
814 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
816 service_unwatch_main_pid(s
);
817 s
->main_pid_known
= false;
819 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
821 r
= service_set_main_pid(s
, pid
);
825 r
= unit_watch_pid(UNIT(s
), pid
);
827 /* FIXME: we need to do something here */
828 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
835 static int service_search_main_pid(Service
*s
) {
841 /* If we know it anyway, don't ever fallback to unreliable
843 if (s
->main_pid_known
)
846 if (!s
->guess_main_pid
)
849 assert(s
->main_pid
<= 0);
851 r
= unit_search_main_pid(UNIT(s
), &pid
);
855 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
856 r
= service_set_main_pid(s
, pid
);
860 r
= unit_watch_pid(UNIT(s
), pid
);
862 /* FIXME: we need to do something here */
863 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
870 static void service_set_state(Service
*s
, ServiceState state
) {
871 ServiceState old_state
;
872 const UnitActiveState
*table
;
876 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
878 old_state
= s
->state
;
881 service_unwatch_pid_file(s
);
884 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
886 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
887 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
888 SERVICE_AUTO_RESTART
))
889 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
892 SERVICE_START
, SERVICE_START_POST
,
893 SERVICE_RUNNING
, SERVICE_RELOAD
,
894 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
895 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
896 service_unwatch_main_pid(s
);
897 s
->main_command
= NULL
;
901 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
903 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
904 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
905 service_unwatch_control_pid(s
);
906 s
->control_command
= NULL
;
907 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
910 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
911 unit_unwatch_all_pids(UNIT(s
));
914 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
915 SERVICE_RUNNING
, SERVICE_RELOAD
,
916 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
917 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
918 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
919 service_close_socket_fd(s
);
920 service_connection_unref(s
);
923 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
924 service_stop_watchdog(s
);
926 /* For the inactive states unit_notify() will trim the cgroup,
927 * but for exit we have to do that ourselves... */
928 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
929 unit_prune_cgroup(UNIT(s
));
931 /* For remain_after_exit services, let's see if we can "release" the
932 * hold on the console, since unit_notify() only does that in case of
934 if (state
== SERVICE_EXITED
&&
935 s
->remain_after_exit
&&
936 UNIT(s
)->manager
->n_on_console
> 0) {
940 ec
= unit_get_exec_context(UNIT(s
));
941 if (ec
&& exec_context_may_touch_console(ec
)) {
942 Manager
*m
= UNIT(s
)->manager
;
945 if (m
->n_on_console
== 0)
946 /* unset no_console_output flag, since the console is free */
947 m
->no_console_output
= false;
951 if (old_state
!= state
)
952 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
954 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
957 static int service_coldplug(Unit
*u
) {
958 Service
*s
= SERVICE(u
);
962 assert(s
->state
== SERVICE_DEAD
);
964 if (s
->deserialized_state
== s
->state
)
967 if (IN_SET(s
->deserialized_state
,
968 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
970 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
971 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
975 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
977 /* For the start/stop timeouts 0 means off */
979 r
= service_arm_timer(s
, k
);
985 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
987 /* The restart timeouts 0 means immediately */
988 r
= service_arm_timer(s
, s
->restart_usec
);
993 if (s
->main_pid
> 0 &&
994 pid_is_unwaited(s
->main_pid
) &&
995 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
996 IN_SET(s
->deserialized_state
,
997 SERVICE_START
, SERVICE_START_POST
,
998 SERVICE_RUNNING
, SERVICE_RELOAD
,
999 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1000 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
1001 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
1006 if (s
->control_pid
> 0 &&
1007 pid_is_unwaited(s
->control_pid
) &&
1008 IN_SET(s
->deserialized_state
,
1009 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1011 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1012 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1013 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1018 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1019 unit_watch_all_pids(UNIT(s
));
1021 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1022 service_start_watchdog(s
);
1024 service_set_state(s
, s
->deserialized_state
);
1028 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
1029 _cleanup_strv_free_
char **rfd_names
= NULL
;
1030 _cleanup_free_
int *rfds
= NULL
;
1037 if (s
->socket_fd
>= 0) {
1039 /* Pass the per-connection socket */
1044 rfds
[0] = s
->socket_fd
;
1046 rfd_names
= strv_new("connection", NULL
);
1055 /* Pass all our configured sockets for singleton services */
1057 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1058 _cleanup_free_
int *cfds
= NULL
;
1062 if (u
->type
!= UNIT_SOCKET
)
1067 cn_fds
= socket_collect_fds(sock
, &cfds
);
1082 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1086 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1092 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1098 if (s
->n_fd_store
> 0) {
1103 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1109 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1115 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1116 rfds
[rn_fds
] = fs
->fd
;
1117 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1118 if (!rfd_names
[rn_fds
])
1124 rfd_names
[rn_fds
] = NULL
;
1128 *fd_names
= rfd_names
;
1136 static int service_spawn(
1141 bool apply_permissions
,
1143 bool apply_tty_stdin
,
1147 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1148 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1149 _cleanup_free_
int *fds
= NULL
;
1150 unsigned n_fds
= 0, n_env
= 0;
1154 ExecParameters exec_params
= {
1155 .apply_permissions
= apply_permissions
,
1156 .apply_chroot
= apply_chroot
,
1157 .apply_tty_stdin
= apply_tty_stdin
,
1158 .bus_endpoint_fd
= -1,
1170 (void) unit_realize_cgroup(UNIT(s
));
1171 if (s
->reset_cpu_usage
) {
1172 (void) unit_reset_cpu_usage(UNIT(s
));
1173 s
->reset_cpu_usage
= false;
1176 r
= unit_setup_exec_runtime(UNIT(s
));
1181 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1182 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1183 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1185 r
= service_collect_fds(s
, &fds
, &fd_names
);
1193 r
= service_arm_timer(s
, timeout
);
1197 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1199 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1203 our_env
= new0(char*, 6);
1209 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1210 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1215 if (s
->main_pid
> 0)
1216 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1221 if (UNIT(s
)->manager
->running_as
!= MANAGER_SYSTEM
)
1222 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1227 if (s
->socket_fd
>= 0) {
1228 union sockaddr_union sa
;
1229 socklen_t salen
= sizeof(sa
);
1231 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1237 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1238 _cleanup_free_
char *addr
= NULL
;
1242 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1246 t
= strappend("REMOTE_ADDR=", addr
);
1251 our_env
[n_env
++] = t
;
1253 port
= sockaddr_port(&sa
.sa
);
1259 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0) {
1263 our_env
[n_env
++] = t
;
1267 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1273 if (is_control
&& UNIT(s
)->cgroup_path
) {
1274 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1275 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1277 path
= UNIT(s
)->cgroup_path
;
1279 if (s
->exec_context
.bus_endpoint
) {
1280 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
? "system" : "user",
1281 UNIT(s
)->id
, &bus_endpoint_path
);
1285 /* Pass the fd to the exec_params so that the child process can upload the policy.
1286 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1287 * as the service is running. */
1288 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1291 exec_params
.argv
= argv
;
1292 exec_params
.fds
= fds
;
1293 exec_params
.fd_names
= fd_names
;
1294 exec_params
.n_fds
= n_fds
;
1295 exec_params
.environment
= final_env
;
1296 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1297 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1298 exec_params
.cgroup_path
= path
;
1299 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1300 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1301 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1302 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1303 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1304 if (s
->type
== SERVICE_IDLE
)
1305 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1306 exec_params
.stdin_fd
= s
->stdin_fd
;
1307 exec_params
.stdout_fd
= s
->stdout_fd
;
1308 exec_params
.stderr_fd
= s
->stderr_fd
;
1310 r
= exec_spawn(UNIT(s
),
1319 r
= unit_watch_pid(UNIT(s
), pid
);
1321 /* FIXME: we need to do something here */
1330 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1335 static int main_pid_good(Service
*s
) {
1338 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1341 /* If we know the pid file, then let's just check if it is
1343 if (s
->main_pid_known
) {
1345 /* If it's an alien child let's check if it is still
1347 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1348 return pid_is_alive(s
->main_pid
);
1350 /* .. otherwise assume we'll get a SIGCHLD for it,
1351 * which we really should wait for to collect exit
1352 * status and code */
1353 return s
->main_pid
> 0;
1356 /* We don't know the pid */
1360 _pure_
static int control_pid_good(Service
*s
) {
1363 return s
->control_pid
> 0;
1366 static int cgroup_good(Service
*s
) {
1371 if (!UNIT(s
)->cgroup_path
)
1374 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1381 static bool service_shall_restart(Service
*s
) {
1384 /* Don't restart after manual stops */
1385 if (s
->forbid_restart
)
1388 /* Never restart if this is configured as special exception */
1389 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1392 /* Restart if the exit code/status are configured as restart triggers */
1393 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1396 switch (s
->restart
) {
1398 case SERVICE_RESTART_NO
:
1401 case SERVICE_RESTART_ALWAYS
:
1404 case SERVICE_RESTART_ON_SUCCESS
:
1405 return s
->result
== SERVICE_SUCCESS
;
1407 case SERVICE_RESTART_ON_FAILURE
:
1408 return s
->result
!= SERVICE_SUCCESS
;
1410 case SERVICE_RESTART_ON_ABNORMAL
:
1411 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1413 case SERVICE_RESTART_ON_WATCHDOG
:
1414 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1416 case SERVICE_RESTART_ON_ABORT
:
1417 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1420 assert_not_reached("unknown restart setting");
1424 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1428 if (f
!= SERVICE_SUCCESS
)
1431 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1433 if (s
->result
!= SERVICE_SUCCESS
) {
1434 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1435 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1438 if (allow_restart
&& service_shall_restart(s
)) {
1440 r
= service_arm_timer(s
, s
->restart_usec
);
1444 service_set_state(s
, SERVICE_AUTO_RESTART
);
1447 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1448 s
->forbid_restart
= false;
1450 /* We want fresh tmpdirs in case service is started again immediately */
1451 exec_runtime_destroy(s
->exec_runtime
);
1452 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1454 /* Also, remove the runtime directory in */
1455 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1457 /* Try to delete the pid file. At this point it will be
1458 * out-of-date, and some software might be confused by it, so
1459 * let's remove it. */
1461 unlink_noerrno(s
->pid_file
);
1466 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1467 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1470 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1474 if (f
!= SERVICE_SUCCESS
)
1477 service_unwatch_control_pid(s
);
1478 unit_watch_all_pids(UNIT(s
));
1480 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1481 if (s
->control_command
) {
1482 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1484 r
= service_spawn(s
,
1486 s
->timeout_stop_usec
,
1488 !s
->permissions_start_only
,
1489 !s
->root_directory_start_only
,
1496 service_set_state(s
, SERVICE_STOP_POST
);
1498 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1503 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1504 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1507 static int state_to_kill_operation(ServiceState state
) {
1510 case SERVICE_STOP_SIGABRT
:
1513 case SERVICE_STOP_SIGTERM
:
1514 case SERVICE_FINAL_SIGTERM
:
1515 return KILL_TERMINATE
;
1517 case SERVICE_STOP_SIGKILL
:
1518 case SERVICE_FINAL_SIGKILL
:
1522 return _KILL_OPERATION_INVALID
;
1526 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1531 if (f
!= SERVICE_SUCCESS
)
1534 unit_watch_all_pids(UNIT(s
));
1536 r
= unit_kill_context(
1539 state_to_kill_operation(state
),
1548 if (s
->timeout_stop_usec
> 0) {
1549 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1554 service_set_state(s
, state
);
1555 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1556 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1557 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1558 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1559 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1560 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1562 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1567 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1569 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1570 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1572 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1575 static void service_enter_stop_by_notify(Service
*s
) {
1578 unit_watch_all_pids(UNIT(s
));
1580 if (s
->timeout_stop_usec
> 0)
1581 service_arm_timer(s
, s
->timeout_stop_usec
);
1583 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1584 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1587 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1592 if (f
!= SERVICE_SUCCESS
)
1595 service_unwatch_control_pid(s
);
1596 unit_watch_all_pids(UNIT(s
));
1598 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1599 if (s
->control_command
) {
1600 s
->control_command_id
= SERVICE_EXEC_STOP
;
1602 r
= service_spawn(s
,
1604 s
->timeout_stop_usec
,
1606 !s
->permissions_start_only
,
1607 !s
->root_directory_start_only
,
1614 service_set_state(s
, SERVICE_STOP
);
1616 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1621 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1622 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1625 static bool service_good(Service
*s
) {
1629 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1632 main_pid_ok
= main_pid_good(s
);
1633 if (main_pid_ok
> 0) /* It's alive */
1635 if (main_pid_ok
== 0) /* It's dead */
1638 /* OK, we don't know anything about the main PID, maybe
1639 * because there is none. Let's check the control group
1642 return cgroup_good(s
) != 0;
1645 static void service_enter_running(Service
*s
, ServiceResult f
) {
1648 if (f
!= SERVICE_SUCCESS
)
1651 if (service_good(s
)) {
1653 /* If there are any queued up sd_notify()
1654 * notifications, process them now */
1655 if (s
->notify_state
== NOTIFY_RELOADING
)
1656 service_enter_reload_by_notify(s
);
1657 else if (s
->notify_state
== NOTIFY_STOPPING
)
1658 service_enter_stop_by_notify(s
);
1660 service_set_state(s
, SERVICE_RUNNING
);
1662 } else if (s
->remain_after_exit
)
1663 service_set_state(s
, SERVICE_EXITED
);
1665 service_enter_stop(s
, SERVICE_SUCCESS
);
1668 static void service_enter_start_post(Service
*s
) {
1672 service_unwatch_control_pid(s
);
1673 service_reset_watchdog(s
);
1675 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1676 if (s
->control_command
) {
1677 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1679 r
= service_spawn(s
,
1681 s
->timeout_start_usec
,
1683 !s
->permissions_start_only
,
1684 !s
->root_directory_start_only
,
1691 service_set_state(s
, SERVICE_START_POST
);
1693 service_enter_running(s
, SERVICE_SUCCESS
);
1698 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1699 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1702 static void service_kill_control_processes(Service
*s
) {
1705 if (!UNIT(s
)->cgroup_path
)
1708 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1709 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1712 static void service_enter_start(Service
*s
) {
1719 service_unwatch_control_pid(s
);
1720 service_unwatch_main_pid(s
);
1722 /* We want to ensure that nobody leaks processes from
1723 * START_PRE here, so let's go on a killing spree, People
1724 * should not spawn long running processes from START_PRE. */
1725 service_kill_control_processes(s
);
1727 if (s
->type
== SERVICE_FORKING
) {
1728 s
->control_command_id
= SERVICE_EXEC_START
;
1729 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1731 s
->main_command
= NULL
;
1733 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1734 s
->control_command
= NULL
;
1736 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1740 assert(s
->type
== SERVICE_ONESHOT
);
1741 service_enter_start_post(s
);
1745 r
= service_spawn(s
,
1747 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1757 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1758 /* For simple services we immediately start
1759 * the START_POST binaries. */
1761 service_set_main_pid(s
, pid
);
1762 service_enter_start_post(s
);
1764 } else if (s
->type
== SERVICE_FORKING
) {
1766 /* For forking services we wait until the start
1767 * process exited. */
1769 s
->control_pid
= pid
;
1770 service_set_state(s
, SERVICE_START
);
1772 } else if (s
->type
== SERVICE_ONESHOT
||
1773 s
->type
== SERVICE_DBUS
||
1774 s
->type
== SERVICE_NOTIFY
) {
1776 /* For oneshot services we wait until the start
1777 * process exited, too, but it is our main process. */
1779 /* For D-Bus services we know the main pid right away,
1780 * but wait for the bus name to appear on the
1781 * bus. Notify services are similar. */
1783 service_set_main_pid(s
, pid
);
1784 service_set_state(s
, SERVICE_START
);
1786 assert_not_reached("Unknown service type");
1791 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1792 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1795 static void service_enter_start_pre(Service
*s
) {
1800 service_unwatch_control_pid(s
);
1802 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1803 if (s
->control_command
) {
1804 /* Before we start anything, let's clear up what might
1805 * be left from previous runs. */
1806 service_kill_control_processes(s
);
1808 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1810 r
= service_spawn(s
,
1812 s
->timeout_start_usec
,
1814 !s
->permissions_start_only
,
1815 !s
->root_directory_start_only
,
1822 service_set_state(s
, SERVICE_START_PRE
);
1824 service_enter_start(s
);
1829 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1830 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1833 static void service_enter_restart(Service
*s
) {
1834 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1839 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1840 /* Don't restart things if we are going down anyway */
1841 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1843 r
= service_arm_timer(s
, s
->restart_usec
);
1850 /* Any units that are bound to this service must also be
1851 * restarted. We use JOB_RESTART (instead of the more obvious
1852 * JOB_START) here so that those dependency jobs will be added
1854 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, &error
, NULL
);
1858 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1859 * it will be canceled as part of the service_stop() call that
1860 * is executed as part of JOB_RESTART. */
1862 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1866 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1867 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1870 static void service_enter_reload_by_notify(Service
*s
) {
1873 if (s
->timeout_start_usec
> 0)
1874 service_arm_timer(s
, s
->timeout_start_usec
);
1876 service_set_state(s
, SERVICE_RELOAD
);
1879 static void service_enter_reload(Service
*s
) {
1884 service_unwatch_control_pid(s
);
1885 s
->reload_result
= SERVICE_SUCCESS
;
1887 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1888 if (s
->control_command
) {
1889 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1891 r
= service_spawn(s
,
1893 s
->timeout_start_usec
,
1895 !s
->permissions_start_only
,
1896 !s
->root_directory_start_only
,
1903 service_set_state(s
, SERVICE_RELOAD
);
1905 service_enter_running(s
, SERVICE_SUCCESS
);
1910 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1911 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1912 service_enter_running(s
, SERVICE_SUCCESS
);
1915 static void service_run_next_control(Service
*s
) {
1919 assert(s
->control_command
);
1920 assert(s
->control_command
->command_next
);
1922 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1924 s
->control_command
= s
->control_command
->command_next
;
1925 service_unwatch_control_pid(s
);
1927 r
= service_spawn(s
,
1929 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1931 !s
->permissions_start_only
,
1932 !s
->root_directory_start_only
,
1933 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1934 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1943 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1945 if (s
->state
== SERVICE_START_PRE
)
1946 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1947 else if (s
->state
== SERVICE_STOP
)
1948 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1949 else if (s
->state
== SERVICE_STOP_POST
)
1950 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1951 else if (s
->state
== SERVICE_RELOAD
) {
1952 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1953 service_enter_running(s
, SERVICE_SUCCESS
);
1955 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1958 static void service_run_next_main(Service
*s
) {
1963 assert(s
->main_command
);
1964 assert(s
->main_command
->command_next
);
1965 assert(s
->type
== SERVICE_ONESHOT
);
1967 s
->main_command
= s
->main_command
->command_next
;
1968 service_unwatch_main_pid(s
);
1970 r
= service_spawn(s
,
1972 s
->timeout_start_usec
,
1982 service_set_main_pid(s
, pid
);
1987 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1988 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1991 static int service_start_limit_test(Service
*s
) {
1994 if (ratelimit_test(&s
->start_limit
))
1997 log_unit_warning(UNIT(s
), "Start request repeated too quickly.");
1999 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
2002 static int service_start(Unit
*u
) {
2003 Service
*s
= SERVICE(u
);
2008 /* We cannot fulfill this request right now, try again later
2010 if (IN_SET(s
->state
,
2011 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2012 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2015 /* Already on it! */
2016 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
2019 /* A service that will be restarted must be stopped first to
2020 * trigger BindsTo and/or OnFailure dependencies. If a user
2021 * does not want to wait for the holdoff time to elapse, the
2022 * service should be manually restarted, not started. We
2023 * simply return EAGAIN here, so that any start jobs stay
2024 * queued, and assume that the auto restart timer will
2025 * eventually trigger the restart. */
2026 if (s
->state
== SERVICE_AUTO_RESTART
)
2029 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
2031 /* Make sure we don't enter a busy loop of some kind. */
2032 r
= service_start_limit_test(s
);
2034 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
2038 s
->result
= SERVICE_SUCCESS
;
2039 s
->reload_result
= SERVICE_SUCCESS
;
2040 s
->main_pid_known
= false;
2041 s
->main_pid_alien
= false;
2042 s
->forbid_restart
= false;
2043 s
->reset_cpu_usage
= true;
2045 s
->status_text
= mfree(s
->status_text
);
2046 s
->status_errno
= 0;
2048 s
->notify_state
= NOTIFY_UNKNOWN
;
2050 service_enter_start_pre(s
);
2054 static int service_stop(Unit
*u
) {
2055 Service
*s
= SERVICE(u
);
2059 /* Don't create restart jobs from manual stops. */
2060 s
->forbid_restart
= true;
2063 if (IN_SET(s
->state
,
2064 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2065 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2068 /* A restart will be scheduled or is in progress. */
2069 if (s
->state
== SERVICE_AUTO_RESTART
) {
2070 service_set_state(s
, SERVICE_DEAD
);
2074 /* If there's already something running we go directly into
2076 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2077 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2081 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2083 service_enter_stop(s
, SERVICE_SUCCESS
);
2087 static int service_reload(Unit
*u
) {
2088 Service
*s
= SERVICE(u
);
2092 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2094 service_enter_reload(s
);
2098 _pure_
static bool service_can_reload(Unit
*u
) {
2099 Service
*s
= SERVICE(u
);
2103 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2106 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2107 Service
*s
= SERVICE(u
);
2115 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2116 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2117 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2119 if (s
->control_pid
> 0)
2120 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2122 if (s
->main_pid_known
&& s
->main_pid
> 0)
2123 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2125 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2126 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2127 unit_serialize_item(u
, f
, "bus-name-owner", s
->bus_name_owner
);
2129 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2133 /* FIXME: There's a minor uncleanliness here: if there are
2134 * multiple commands attached here, we will start from the
2135 * first one again */
2136 if (s
->control_command_id
>= 0)
2137 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2139 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2142 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2145 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2149 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2152 r
= unit_serialize_item_fd(u
, f
, fds
, "endpoint-fd", s
->bus_endpoint_fd
);
2156 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2157 _cleanup_free_
char *c
= NULL
;
2160 copy
= fdset_put_dup(fds
, fs
->fd
);
2164 c
= cescape(fs
->fdname
);
2166 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2169 if (s
->main_exec_status
.pid
> 0) {
2170 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2171 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2172 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2174 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2175 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2176 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2180 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2181 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2183 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2188 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2189 Service
*s
= SERVICE(u
);
2197 if (streq(key
, "state")) {
2200 state
= service_state_from_string(value
);
2202 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2204 s
->deserialized_state
= state
;
2205 } else if (streq(key
, "result")) {
2208 f
= service_result_from_string(value
);
2210 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2211 else if (f
!= SERVICE_SUCCESS
)
2214 } else if (streq(key
, "reload-result")) {
2217 f
= service_result_from_string(value
);
2219 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2220 else if (f
!= SERVICE_SUCCESS
)
2221 s
->reload_result
= f
;
2223 } else if (streq(key
, "control-pid")) {
2226 if (parse_pid(value
, &pid
) < 0)
2227 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2229 s
->control_pid
= pid
;
2230 } else if (streq(key
, "main-pid")) {
2233 if (parse_pid(value
, &pid
) < 0)
2234 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2236 service_set_main_pid(s
, pid
);
2237 unit_watch_pid(UNIT(s
), pid
);
2239 } else if (streq(key
, "main-pid-known")) {
2242 b
= parse_boolean(value
);
2244 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2246 s
->main_pid_known
= b
;
2247 } else if (streq(key
, "bus-name-good")) {
2250 b
= parse_boolean(value
);
2252 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2254 s
->bus_name_good
= b
;
2255 } else if (streq(key
, "bus-name-owner")) {
2256 r
= free_and_strdup(&s
->bus_name_owner
, value
);
2258 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
2259 } else if (streq(key
, "status-text")) {
2262 r
= cunescape(value
, 0, &t
);
2264 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2266 free(s
->status_text
);
2270 } else if (streq(key
, "control-command")) {
2271 ServiceExecCommand id
;
2273 id
= service_exec_command_from_string(value
);
2275 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2277 s
->control_command_id
= id
;
2278 s
->control_command
= s
->exec_command
[id
];
2280 } else if (streq(key
, "socket-fd")) {
2283 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2284 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2286 asynchronous_close(s
->socket_fd
);
2287 s
->socket_fd
= fdset_remove(fds
, fd
);
2289 } else if (streq(key
, "endpoint-fd")) {
2292 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2293 log_unit_debug(u
, "Failed to parse endpoint-fd value: %s", value
);
2295 safe_close(s
->bus_endpoint_fd
);
2296 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2298 } else if (streq(key
, "fd-store-fd")) {
2303 pf
= strcspn(value
, WHITESPACE
);
2304 fdv
= strndupa(value
, pf
);
2306 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2307 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2309 _cleanup_free_
char *t
= NULL
;
2313 fdn
+= strspn(fdn
, WHITESPACE
);
2314 (void) cunescape(fdn
, 0, &t
);
2316 r
= service_add_fd_store(s
, fd
, t
);
2318 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2320 fdset_remove(fds
, fd
);
2323 } else if (streq(key
, "main-exec-status-pid")) {
2326 if (parse_pid(value
, &pid
) < 0)
2327 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2329 s
->main_exec_status
.pid
= pid
;
2330 } else if (streq(key
, "main-exec-status-code")) {
2333 if (safe_atoi(value
, &i
) < 0)
2334 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2336 s
->main_exec_status
.code
= i
;
2337 } else if (streq(key
, "main-exec-status-status")) {
2340 if (safe_atoi(value
, &i
) < 0)
2341 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2343 s
->main_exec_status
.status
= i
;
2344 } else if (streq(key
, "main-exec-status-start"))
2345 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2346 else if (streq(key
, "main-exec-status-exit"))
2347 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2348 else if (streq(key
, "watchdog-timestamp"))
2349 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2350 else if (streq(key
, "forbid-restart")) {
2353 b
= parse_boolean(value
);
2355 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2357 s
->forbid_restart
= b
;
2358 } else if (streq(key
, "stdin-fd")) {
2361 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2362 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2364 asynchronous_close(s
->stdin_fd
);
2365 s
->stdin_fd
= fdset_remove(fds
, fd
);
2367 } else if (streq(key
, "stdout-fd")) {
2370 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2371 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2373 asynchronous_close(s
->stdout_fd
);
2374 s
->stdout_fd
= fdset_remove(fds
, fd
);
2376 } else if (streq(key
, "stderr-fd")) {
2379 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2380 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2382 asynchronous_close(s
->stderr_fd
);
2383 s
->stderr_fd
= fdset_remove(fds
, fd
);
2386 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2391 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2392 const UnitActiveState
*table
;
2396 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2398 return table
[SERVICE(u
)->state
];
2401 static const char *service_sub_state_to_string(Unit
*u
) {
2404 return service_state_to_string(SERVICE(u
)->state
);
2407 static bool service_check_gc(Unit
*u
) {
2408 Service
*s
= SERVICE(u
);
2412 /* Never clean up services that still have a process around,
2413 * even if the service is formally dead. */
2414 if (cgroup_good(s
) > 0 ||
2415 main_pid_good(s
) > 0 ||
2416 control_pid_good(s
) > 0)
2422 static int service_retry_pid_file(Service
*s
) {
2425 assert(s
->pid_file
);
2426 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2428 r
= service_load_pid_file(s
, false);
2432 service_unwatch_pid_file(s
);
2434 service_enter_running(s
, SERVICE_SUCCESS
);
2438 static int service_watch_pid_file(Service
*s
) {
2441 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2443 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2447 /* the pidfile might have appeared just before we set the watch */
2448 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2449 service_retry_pid_file(s
);
2453 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2454 service_unwatch_pid_file(s
);
2458 static int service_demand_pid_file(Service
*s
) {
2461 assert(s
->pid_file
);
2462 assert(!s
->pid_file_pathspec
);
2464 ps
= new0(PathSpec
, 1);
2469 ps
->path
= strdup(s
->pid_file
);
2475 path_kill_slashes(ps
->path
);
2477 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2478 * keep their PID file open all the time. */
2479 ps
->type
= PATH_MODIFIED
;
2480 ps
->inotify_fd
= -1;
2482 s
->pid_file_pathspec
= ps
;
2484 return service_watch_pid_file(s
);
2487 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2488 PathSpec
*p
= userdata
;
2493 s
= SERVICE(p
->unit
);
2497 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2498 assert(s
->pid_file_pathspec
);
2499 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2501 log_unit_debug(UNIT(s
), "inotify event");
2503 if (path_spec_fd_event(p
, events
) < 0)
2506 if (service_retry_pid_file(s
) == 0)
2509 if (service_watch_pid_file(s
) < 0)
2515 service_unwatch_pid_file(s
);
2516 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2520 static void service_notify_cgroup_empty_event(Unit
*u
) {
2521 Service
*s
= SERVICE(u
);
2525 log_unit_debug(u
, "cgroup is empty");
2529 /* Waiting for SIGCHLD is usually more interesting,
2530 * because it includes return codes/signals. Which is
2531 * why we ignore the cgroup events for most cases,
2532 * except when we don't know pid which to expect the
2536 case SERVICE_START_POST
:
2537 /* If we were hoping for the daemon to write its PID file,
2538 * we can give up now. */
2539 if (s
->pid_file_pathspec
) {
2540 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2542 service_unwatch_pid_file(s
);
2543 if (s
->state
== SERVICE_START
)
2544 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2546 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2550 case SERVICE_RUNNING
:
2551 /* service_enter_running() will figure out what to do */
2552 service_enter_running(s
, SERVICE_SUCCESS
);
2555 case SERVICE_STOP_SIGABRT
:
2556 case SERVICE_STOP_SIGTERM
:
2557 case SERVICE_STOP_SIGKILL
:
2559 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2560 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2564 case SERVICE_STOP_POST
:
2565 case SERVICE_FINAL_SIGTERM
:
2566 case SERVICE_FINAL_SIGKILL
:
2567 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2568 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2577 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2578 Service
*s
= SERVICE(u
);
2584 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2585 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2586 f
= SERVICE_SUCCESS
;
2587 else if (code
== CLD_EXITED
)
2588 f
= SERVICE_FAILURE_EXIT_CODE
;
2589 else if (code
== CLD_KILLED
)
2590 f
= SERVICE_FAILURE_SIGNAL
;
2591 else if (code
== CLD_DUMPED
)
2592 f
= SERVICE_FAILURE_CORE_DUMP
;
2594 assert_not_reached("Unknown code");
2596 if (s
->main_pid
== pid
) {
2597 /* Forking services may occasionally move to a new PID.
2598 * As long as they update the PID file before exiting the old
2599 * PID, they're fine. */
2600 if (service_load_pid_file(s
, false) == 0)
2604 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2606 if (s
->main_command
) {
2607 /* If this is not a forking service than the
2608 * main process got started and hence we copy
2609 * the exit status so that it is recorded both
2610 * as main and as control process exit
2613 s
->main_command
->exec_status
= s
->main_exec_status
;
2615 if (s
->main_command
->ignore
)
2616 f
= SERVICE_SUCCESS
;
2617 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2619 /* If this is a forked process, then we should
2620 * ignore the return value if this was
2621 * configured for the starter process */
2623 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2624 f
= SERVICE_SUCCESS
;
2627 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2629 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2630 sigchld_code_to_string(code
), status
,
2631 strna(code
== CLD_EXITED
2632 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2633 : signal_to_string(status
))),
2634 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2635 "EXIT_STATUS=%i", status
,
2638 if (f
!= SERVICE_SUCCESS
)
2641 if (s
->main_command
&&
2642 s
->main_command
->command_next
&&
2643 f
== SERVICE_SUCCESS
) {
2645 /* There is another command to *
2646 * execute, so let's do that. */
2648 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2649 service_run_next_main(s
);
2653 /* The service exited, so the service is officially
2655 s
->main_command
= NULL
;
2659 case SERVICE_START_POST
:
2660 case SERVICE_RELOAD
:
2662 /* Need to wait until the operation is
2667 if (s
->type
== SERVICE_ONESHOT
) {
2668 /* This was our main goal, so let's go on */
2669 if (f
== SERVICE_SUCCESS
)
2670 service_enter_start_post(s
);
2672 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2678 case SERVICE_RUNNING
:
2679 service_enter_running(s
, f
);
2682 case SERVICE_STOP_SIGABRT
:
2683 case SERVICE_STOP_SIGTERM
:
2684 case SERVICE_STOP_SIGKILL
:
2686 if (!control_pid_good(s
))
2687 service_enter_stop_post(s
, f
);
2689 /* If there is still a control process, wait for that first */
2692 case SERVICE_STOP_POST
:
2693 case SERVICE_FINAL_SIGTERM
:
2694 case SERVICE_FINAL_SIGKILL
:
2696 if (!control_pid_good(s
))
2697 service_enter_dead(s
, f
, true);
2701 assert_not_reached("Uh, main process died at wrong time.");
2705 } else if (s
->control_pid
== pid
) {
2708 if (s
->control_command
) {
2709 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2711 if (s
->control_command
->ignore
)
2712 f
= SERVICE_SUCCESS
;
2715 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2716 "Control process exited, code=%s status=%i",
2717 sigchld_code_to_string(code
), status
);
2719 if (f
!= SERVICE_SUCCESS
)
2722 /* Immediately get rid of the cgroup, so that the
2723 * kernel doesn't delay the cgroup empty messages for
2724 * the service cgroup any longer than necessary */
2725 service_kill_control_processes(s
);
2727 if (s
->control_command
&&
2728 s
->control_command
->command_next
&&
2729 f
== SERVICE_SUCCESS
) {
2731 /* There is another command to *
2732 * execute, so let's do that. */
2734 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2735 service_run_next_control(s
);
2738 /* No further commands for this step, so let's
2739 * figure out what to do next */
2741 s
->control_command
= NULL
;
2742 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2744 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2748 case SERVICE_START_PRE
:
2749 if (f
== SERVICE_SUCCESS
)
2750 service_enter_start(s
);
2752 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2756 if (s
->type
!= SERVICE_FORKING
)
2757 /* Maybe spurious event due to a reload that changed the type? */
2760 if (f
!= SERVICE_SUCCESS
) {
2761 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2766 bool has_start_post
;
2769 /* Let's try to load the pid file here if we can.
2770 * The PID file might actually be created by a START_POST
2771 * script. In that case don't worry if the loading fails. */
2773 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2774 r
= service_load_pid_file(s
, !has_start_post
);
2775 if (!has_start_post
&& r
< 0) {
2776 r
= service_demand_pid_file(s
);
2777 if (r
< 0 || !cgroup_good(s
))
2778 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2782 (void) service_search_main_pid(s
);
2784 service_enter_start_post(s
);
2787 case SERVICE_START_POST
:
2788 if (f
!= SERVICE_SUCCESS
) {
2789 service_enter_stop(s
, f
);
2796 r
= service_load_pid_file(s
, true);
2798 r
= service_demand_pid_file(s
);
2799 if (r
< 0 || !cgroup_good(s
))
2800 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2804 (void) service_search_main_pid(s
);
2806 service_enter_running(s
, SERVICE_SUCCESS
);
2809 case SERVICE_RELOAD
:
2810 if (f
== SERVICE_SUCCESS
) {
2811 service_load_pid_file(s
, true);
2812 (void) service_search_main_pid(s
);
2815 s
->reload_result
= f
;
2816 service_enter_running(s
, SERVICE_SUCCESS
);
2820 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2823 case SERVICE_STOP_SIGABRT
:
2824 case SERVICE_STOP_SIGTERM
:
2825 case SERVICE_STOP_SIGKILL
:
2826 if (main_pid_good(s
) <= 0)
2827 service_enter_stop_post(s
, f
);
2829 /* If there is still a service
2830 * process around, wait until
2831 * that one quit, too */
2834 case SERVICE_STOP_POST
:
2835 case SERVICE_FINAL_SIGTERM
:
2836 case SERVICE_FINAL_SIGKILL
:
2837 if (main_pid_good(s
) <= 0)
2838 service_enter_dead(s
, f
, true);
2842 assert_not_reached("Uh, control process died at wrong time.");
2847 /* Notify clients about changed exit status */
2848 unit_add_to_dbus_queue(u
);
2850 /* We got one SIGCHLD for the service, let's watch all
2851 * processes that are now running of the service, and watch
2852 * that. Among the PIDs we then watch will be children
2853 * reassigned to us, which hopefully allows us to identify
2854 * when all children are gone */
2855 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2856 unit_watch_all_pids(u
);
2858 /* If the PID set is empty now, then let's finish this off */
2859 if (set_isempty(u
->pids
))
2860 service_notify_cgroup_empty_event(u
);
2863 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2864 Service
*s
= SERVICE(userdata
);
2867 assert(source
== s
->timer_event_source
);
2871 case SERVICE_START_PRE
:
2873 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2874 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2877 case SERVICE_START_POST
:
2878 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2879 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2882 case SERVICE_RELOAD
:
2883 log_unit_warning(UNIT(s
), "Reload operation timed out. Stopping.");
2884 service_unwatch_control_pid(s
);
2885 service_kill_control_processes(s
);
2886 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2887 service_enter_running(s
, SERVICE_SUCCESS
);
2891 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2892 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2895 case SERVICE_STOP_SIGABRT
:
2896 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2897 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2900 case SERVICE_STOP_SIGTERM
:
2901 if (s
->kill_context
.send_sigkill
) {
2902 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2903 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2905 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2906 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2911 case SERVICE_STOP_SIGKILL
:
2912 /* Uh, we sent a SIGKILL and it is still not gone?
2913 * Must be something we cannot kill, so let's just be
2914 * weirded out and continue */
2916 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2917 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2920 case SERVICE_STOP_POST
:
2921 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2922 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2925 case SERVICE_FINAL_SIGTERM
:
2926 if (s
->kill_context
.send_sigkill
) {
2927 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2928 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2930 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2931 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2936 case SERVICE_FINAL_SIGKILL
:
2937 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2938 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2941 case SERVICE_AUTO_RESTART
:
2942 log_unit_info(UNIT(s
),
2943 s
->restart_usec
> 0 ?
2944 "Service hold-off time over, scheduling restart." :
2945 "Service has no hold-off time, scheduling restart.");
2946 service_enter_restart(s
);
2950 assert_not_reached("Timeout at wrong time.");
2956 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2957 Service
*s
= SERVICE(userdata
);
2958 char t
[FORMAT_TIMESPAN_MAX
];
2961 assert(source
== s
->watchdog_event_source
);
2963 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2964 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2966 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2971 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2972 Service
*s
= SERVICE(u
);
2973 _cleanup_free_
char *cc
= NULL
;
2974 bool notify_dbus
= false;
2979 cc
= strv_join(tags
, ", ");
2981 if (s
->notify_access
== NOTIFY_NONE
) {
2982 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2984 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2985 if (s
->main_pid
!= 0)
2986 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2988 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2991 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2993 /* Interpret MAINPID= */
2994 e
= strv_find_startswith(tags
, "MAINPID=");
2995 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2996 if (parse_pid(e
, &pid
) < 0)
2997 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2999 service_set_main_pid(s
, pid
);
3000 unit_watch_pid(UNIT(s
), pid
);
3005 /* Interpret RELOADING= */
3006 if (strv_find(tags
, "RELOADING=1")) {
3008 s
->notify_state
= NOTIFY_RELOADING
;
3010 if (s
->state
== SERVICE_RUNNING
)
3011 service_enter_reload_by_notify(s
);
3016 /* Interpret READY= */
3017 if (strv_find(tags
, "READY=1")) {
3019 s
->notify_state
= NOTIFY_READY
;
3021 /* Type=notify services inform us about completed
3022 * initialization with READY=1 */
3023 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
3024 service_enter_start_post(s
);
3026 /* Sending READY=1 while we are reloading informs us
3027 * that the reloading is complete */
3028 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
3029 service_enter_running(s
, SERVICE_SUCCESS
);
3034 /* Interpret STOPPING= */
3035 if (strv_find(tags
, "STOPPING=1")) {
3037 s
->notify_state
= NOTIFY_STOPPING
;
3039 if (s
->state
== SERVICE_RUNNING
)
3040 service_enter_stop_by_notify(s
);
3045 /* Interpret STATUS= */
3046 e
= strv_find_startswith(tags
, "STATUS=");
3048 _cleanup_free_
char *t
= NULL
;
3051 if (!utf8_is_valid(e
))
3052 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3060 if (!streq_ptr(s
->status_text
, t
)) {
3062 free(s
->status_text
);
3070 /* Interpret ERRNO= */
3071 e
= strv_find_startswith(tags
, "ERRNO=");
3075 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3076 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3078 if (s
->status_errno
!= status_errno
) {
3079 s
->status_errno
= status_errno
;
3085 /* Interpret WATCHDOG= */
3086 if (strv_find(tags
, "WATCHDOG=1"))
3087 service_reset_watchdog(s
);
3089 if (strv_find(tags
, "FDSTORE=1")) {
3092 name
= strv_find_startswith(tags
, "FDNAME=");
3093 if (name
&& !fdname_is_valid(name
)) {
3094 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3098 service_add_fd_store_set(s
, fds
, name
);
3101 /* Notify clients about changed status or main pid */
3103 unit_add_to_dbus_queue(u
);
3106 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
3107 Service
*s
= SERVICE(u
);
3110 if (!s
->timer_event_source
)
3113 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
3120 static void service_bus_name_owner_change(
3123 const char *old_owner
,
3124 const char *new_owner
) {
3126 Service
*s
= SERVICE(u
);
3132 assert(streq(s
->bus_name
, name
));
3133 assert(old_owner
|| new_owner
);
3135 if (old_owner
&& new_owner
)
3136 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3138 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3140 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3142 s
->bus_name_good
= !!new_owner
;
3144 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3145 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
3147 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
3151 if (s
->type
== SERVICE_DBUS
) {
3153 /* service_enter_running() will figure out what to
3155 if (s
->state
== SERVICE_RUNNING
)
3156 service_enter_running(s
, SERVICE_SUCCESS
);
3157 else if (s
->state
== SERVICE_START
&& new_owner
)
3158 service_enter_start_post(s
);
3160 } else if (new_owner
&&
3162 (s
->state
== SERVICE_START
||
3163 s
->state
== SERVICE_START_POST
||
3164 s
->state
== SERVICE_RUNNING
||
3165 s
->state
== SERVICE_RELOAD
)) {
3167 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3170 /* Try to acquire PID from bus service */
3172 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3174 r
= sd_bus_creds_get_pid(creds
, &pid
);
3176 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3178 service_set_main_pid(s
, pid
);
3179 unit_watch_pid(UNIT(s
), pid
);
3184 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3185 _cleanup_free_
char *peer
= NULL
;
3191 /* This is called by the socket code when instantiating a new
3192 * service for a stream socket and the socket needs to be
3195 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3198 if (s
->socket_fd
>= 0)
3201 if (s
->state
!= SERVICE_DEAD
)
3204 if (getpeername_pretty(fd
, &peer
) >= 0) {
3206 if (UNIT(s
)->description
) {
3207 _cleanup_free_
char *a
;
3209 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3213 r
= unit_set_description(UNIT(s
), a
);
3215 r
= unit_set_description(UNIT(s
), peer
);
3222 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3224 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3226 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3229 static void service_reset_failed(Unit
*u
) {
3230 Service
*s
= SERVICE(u
);
3234 if (s
->state
== SERVICE_FAILED
)
3235 service_set_state(s
, SERVICE_DEAD
);
3237 s
->result
= SERVICE_SUCCESS
;
3238 s
->reload_result
= SERVICE_SUCCESS
;
3240 RATELIMIT_RESET(s
->start_limit
);
3243 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3244 Service
*s
= SERVICE(u
);
3246 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3249 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3250 [SERVICE_RESTART_NO
] = "no",
3251 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3252 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3253 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3254 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3255 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3256 [SERVICE_RESTART_ALWAYS
] = "always",
3259 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3261 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3262 [SERVICE_SIMPLE
] = "simple",
3263 [SERVICE_FORKING
] = "forking",
3264 [SERVICE_ONESHOT
] = "oneshot",
3265 [SERVICE_DBUS
] = "dbus",
3266 [SERVICE_NOTIFY
] = "notify",
3267 [SERVICE_IDLE
] = "idle"
3270 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3272 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3273 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3274 [SERVICE_EXEC_START
] = "ExecStart",
3275 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3276 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3277 [SERVICE_EXEC_STOP
] = "ExecStop",
3278 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3281 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3283 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3284 [NOTIFY_NONE
] = "none",
3285 [NOTIFY_MAIN
] = "main",
3286 [NOTIFY_ALL
] = "all"
3289 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3291 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3292 [NOTIFY_UNKNOWN
] = "unknown",
3293 [NOTIFY_READY
] = "ready",
3294 [NOTIFY_RELOADING
] = "reloading",
3295 [NOTIFY_STOPPING
] = "stopping",
3298 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3300 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3301 [SERVICE_SUCCESS
] = "success",
3302 [SERVICE_FAILURE_RESOURCES
] = "resources",
3303 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3304 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3305 [SERVICE_FAILURE_SIGNAL
] = "signal",
3306 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3307 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3308 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3311 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3313 const UnitVTable service_vtable
= {
3314 .object_size
= sizeof(Service
),
3315 .exec_context_offset
= offsetof(Service
, exec_context
),
3316 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3317 .kill_context_offset
= offsetof(Service
, kill_context
),
3318 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3324 .private_section
= "Service",
3326 .init
= service_init
,
3327 .done
= service_done
,
3328 .load
= service_load
,
3329 .release_resources
= service_release_resources
,
3331 .coldplug
= service_coldplug
,
3333 .dump
= service_dump
,
3335 .start
= service_start
,
3336 .stop
= service_stop
,
3337 .reload
= service_reload
,
3339 .can_reload
= service_can_reload
,
3341 .kill
= service_kill
,
3343 .serialize
= service_serialize
,
3344 .deserialize_item
= service_deserialize_item
,
3346 .active_state
= service_active_state
,
3347 .sub_state_to_string
= service_sub_state_to_string
,
3349 .check_gc
= service_check_gc
,
3351 .sigchld_event
= service_sigchld_event
,
3353 .reset_failed
= service_reset_failed
,
3355 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3356 .notify_message
= service_notify_message
,
3358 .bus_name_owner_change
= service_bus_name_owner_change
,
3360 .bus_vtable
= bus_service_vtable
,
3361 .bus_set_property
= bus_service_set_property
,
3362 .bus_commit_properties
= bus_service_commit_properties
,
3364 .get_timeout
= service_get_timeout
,
3365 .can_transient
= true,
3367 .status_message_formats
= {
3368 .starting_stopping
= {
3369 [0] = "Starting %s...",
3370 [1] = "Stopping %s...",
3372 .finished_start_job
= {
3373 [JOB_DONE
] = "Started %s.",
3374 [JOB_FAILED
] = "Failed to start %s.",
3376 .finished_stop_job
= {
3377 [JOB_DONE
] = "Stopped %s.",
3378 [JOB_FAILED
] = "Stopped (with error) %s.",