2 This file is part of systemd.
4 Copyright 2010 Lennart Poettering
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
24 #include "alloc-util.h"
26 #include "bus-error.h"
27 #include "bus-kernel.h"
29 #include "dbus-service.h"
33 #include "exit-status.h"
36 #include "formats-util.h"
38 #include "load-dropin.h"
39 #include "load-fragment.h"
42 #include "parse-util.h"
43 #include "path-util.h"
44 #include "process-util.h"
46 #include "signal-util.h"
48 #include "string-table.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
->runtime_max_usec
= USEC_INFINITY
;
114 s
->type
= _SERVICE_TYPE_INVALID
;
116 s
->bus_endpoint_fd
= -1;
117 s
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -1;
118 s
->guess_main_pid
= true;
120 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
123 static void service_unwatch_control_pid(Service
*s
) {
126 if (s
->control_pid
<= 0)
129 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
133 static void service_unwatch_main_pid(Service
*s
) {
136 if (s
->main_pid
<= 0)
139 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
143 static void service_unwatch_pid_file(Service
*s
) {
144 if (!s
->pid_file_pathspec
)
147 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
148 path_spec_unwatch(s
->pid_file_pathspec
);
149 path_spec_done(s
->pid_file_pathspec
);
150 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
153 static int service_set_main_pid(Service
*s
, pid_t pid
) {
164 if (s
->main_pid
== pid
&& s
->main_pid_known
)
167 if (s
->main_pid
!= pid
) {
168 service_unwatch_main_pid(s
);
169 exec_status_start(&s
->main_exec_status
, pid
);
173 s
->main_pid_known
= true;
175 if (get_process_ppid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
176 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
177 s
->main_pid_alien
= true;
179 s
->main_pid_alien
= false;
184 static void service_close_socket_fd(Service
*s
) {
187 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
190 static void service_connection_unref(Service
*s
) {
193 if (!UNIT_ISSET(s
->accept_socket
))
196 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
197 unit_ref_unset(&s
->accept_socket
);
200 static void service_stop_watchdog(Service
*s
) {
203 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
204 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
207 static void service_start_watchdog(Service
*s
) {
212 if (s
->watchdog_usec
<= 0)
215 if (s
->watchdog_event_source
) {
216 r
= sd_event_source_set_time(s
->watchdog_event_source
, usec_add(s
->watchdog_timestamp
.monotonic
, s
->watchdog_usec
));
218 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
222 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
224 r
= sd_event_add_time(
225 UNIT(s
)->manager
->event
,
226 &s
->watchdog_event_source
,
228 usec_add(s
->watchdog_timestamp
.monotonic
, s
->watchdog_usec
), 0,
229 service_dispatch_watchdog
, s
);
231 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
235 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
237 /* Let's process everything else which might be a sign
238 * of living before we consider a service died. */
239 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
243 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
246 static void service_reset_watchdog(Service
*s
) {
249 dual_timestamp_get(&s
->watchdog_timestamp
);
250 service_start_watchdog(s
);
253 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
259 assert(fs
->service
->n_fd_store
> 0);
260 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
261 fs
->service
->n_fd_store
--;
264 if (fs
->event_source
) {
265 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
266 sd_event_source_unref(fs
->event_source
);
274 static void service_release_resources(Unit
*u
) {
275 Service
*s
= SERVICE(u
);
279 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
282 log_unit_debug(u
, "Releasing all resources.");
284 s
->stdin_fd
= safe_close(s
->stdin_fd
);
285 s
->stdout_fd
= safe_close(s
->stdout_fd
);
286 s
->stderr_fd
= safe_close(s
->stderr_fd
);
289 service_fd_store_unlink(s
->fd_store
);
291 assert(s
->n_fd_store
== 0);
294 static void service_done(Unit
*u
) {
295 Service
*s
= SERVICE(u
);
299 s
->pid_file
= mfree(s
->pid_file
);
300 s
->status_text
= mfree(s
->status_text
);
302 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
303 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
304 s
->control_command
= NULL
;
305 s
->main_command
= NULL
;
307 exit_status_set_free(&s
->restart_prevent_status
);
308 exit_status_set_free(&s
->restart_force_status
);
309 exit_status_set_free(&s
->success_status
);
311 /* This will leak a process, but at least no memory or any of
313 service_unwatch_main_pid(s
);
314 service_unwatch_control_pid(s
);
315 service_unwatch_pid_file(s
);
318 unit_unwatch_bus_name(u
, s
->bus_name
);
319 s
->bus_name
= mfree(s
->bus_name
);
322 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
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 FileDescriptorStoreMax=%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
, usec
);
436 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
439 if (usec
== USEC_INFINITY
)
442 r
= sd_event_add_time(
443 UNIT(s
)->manager
->event
,
444 &s
->timer_event_source
,
447 service_dispatch_timer
, s
);
451 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
456 static int service_verify(Service
*s
) {
459 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
462 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
463 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
467 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
468 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
472 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
473 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
477 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
478 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
482 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
483 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
487 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
488 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
492 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
493 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
497 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
498 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
500 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
501 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
505 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
506 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
508 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
509 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
511 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
512 log_unit_warning(UNIT(s
), "MaxRuntimeSec= has no effect in combination with Type=oneshot. Ignoring.");
517 static int service_add_default_dependencies(Service
*s
) {
522 if (!UNIT(s
)->default_dependencies
)
525 /* Add a number of automatic dependencies useful for the
526 * majority of services. */
528 if (UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
) {
529 /* First, pull in the really early boot stuff, and
530 * require it, so that we fail if we can't acquire
533 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true);
538 /* In the --user instance there's no sysinit.target,
539 * in that case require basic.target instead. */
541 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
546 /* Second, if the rest of the base system is in the same
547 * transaction, order us after it, but do not pull it in or
548 * even require it. */
549 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true);
553 /* Third, add us in for normal shutdown. */
554 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
557 static void service_fix_output(Service
*s
) {
560 /* If nothing has been explicitly configured, patch default
561 * output in. If input is socket/tty we avoid this however,
562 * since in that case we want output to default to the same
563 * place as we read input from. */
565 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
566 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
567 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
568 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
570 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
571 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
572 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
575 static int service_setup_bus_name(Service
*s
) {
583 if (is_kdbus_available()) {
586 n
= strjoina(s
->bus_name
, ".busname");
587 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
589 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency to .busname unit: %m");
592 /* If kdbus is not available, we know the dbus socket is required, hence pull it in, and require it */
593 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true);
595 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
598 /* Regardless if kdbus is used or not, we always want to be ordered against dbus.socket if both are in the transaction. */
599 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, NULL
, true);
601 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
603 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
605 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
607 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
612 static int service_add_extras(Service
*s
) {
617 if (s
->type
== _SERVICE_TYPE_INVALID
) {
618 /* Figure out a type automatically */
620 s
->type
= SERVICE_DBUS
;
621 else if (s
->exec_command
[SERVICE_EXEC_START
])
622 s
->type
= SERVICE_SIMPLE
;
624 s
->type
= SERVICE_ONESHOT
;
627 /* Oneshot services have disabled start timeout by default */
628 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
629 s
->timeout_start_usec
= USEC_INFINITY
;
631 service_fix_output(s
);
633 r
= unit_patch_contexts(UNIT(s
));
637 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
641 r
= unit_set_default_slice(UNIT(s
));
645 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
646 s
->notify_access
= NOTIFY_MAIN
;
648 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
649 s
->notify_access
= NOTIFY_MAIN
;
651 r
= service_add_default_dependencies(s
);
655 r
= service_setup_bus_name(s
);
662 static int service_load(Unit
*u
) {
663 Service
*s
= SERVICE(u
);
668 /* Load a .service file */
669 r
= unit_load_fragment(u
);
673 /* Still nothing found? Then let's give up */
674 if (u
->load_state
== UNIT_STUB
)
677 /* This is a new unit? Then let's add in some extras */
678 if (u
->load_state
== UNIT_LOADED
) {
680 /* We were able to load something, then let's add in
681 * the dropin directories. */
682 r
= unit_load_dropin(u
);
686 /* This is a new unit? Then let's add in some
688 r
= service_add_extras(s
);
693 return service_verify(s
);
696 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
697 ServiceExecCommand c
;
698 Service
*s
= SERVICE(u
);
703 prefix
= strempty(prefix
);
704 prefix2
= strjoina(prefix
, "\t");
707 "%sService State: %s\n"
709 "%sReload Result: %s\n"
710 "%sPermissionsStartOnly: %s\n"
711 "%sRootDirectoryStartOnly: %s\n"
712 "%sRemainAfterExit: %s\n"
713 "%sGuessMainPID: %s\n"
716 "%sNotifyAccess: %s\n"
717 "%sNotifyState: %s\n",
718 prefix
, service_state_to_string(s
->state
),
719 prefix
, service_result_to_string(s
->result
),
720 prefix
, service_result_to_string(s
->reload_result
),
721 prefix
, yes_no(s
->permissions_start_only
),
722 prefix
, yes_no(s
->root_directory_start_only
),
723 prefix
, yes_no(s
->remain_after_exit
),
724 prefix
, yes_no(s
->guess_main_pid
),
725 prefix
, service_type_to_string(s
->type
),
726 prefix
, service_restart_to_string(s
->restart
),
727 prefix
, notify_access_to_string(s
->notify_access
),
728 prefix
, notify_state_to_string(s
->notify_state
));
730 if (s
->control_pid
> 0)
732 "%sControl PID: "PID_FMT
"\n",
733 prefix
, s
->control_pid
);
737 "%sMain PID: "PID_FMT
"\n"
738 "%sMain PID Known: %s\n"
739 "%sMain PID Alien: %s\n",
741 prefix
, yes_no(s
->main_pid_known
),
742 prefix
, yes_no(s
->main_pid_alien
));
747 prefix
, s
->pid_file
);
752 "%sBus Name Good: %s\n",
754 prefix
, yes_no(s
->bus_name_good
));
756 kill_context_dump(&s
->kill_context
, f
, prefix
);
757 exec_context_dump(&s
->exec_context
, f
, prefix
);
759 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
761 if (!s
->exec_command
[c
])
764 fprintf(f
, "%s-> %s:\n",
765 prefix
, service_exec_command_to_string(c
));
767 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
771 fprintf(f
, "%sStatus Text: %s\n",
772 prefix
, s
->status_text
);
774 if (s
->n_fd_store_max
> 0)
776 "%sFile Descriptor Store Max: %u\n"
777 "%sFile Descriptor Store Current: %u\n",
778 prefix
, s
->n_fd_store_max
,
779 prefix
, s
->n_fd_store
);
782 static int service_load_pid_file(Service
*s
, bool may_warn
) {
783 _cleanup_free_
char *k
= NULL
;
792 r
= read_one_line_file(s
->pid_file
, &k
);
795 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
));
799 r
= parse_pid(k
, &pid
);
802 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
806 if (!pid_is_alive(pid
)) {
808 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
812 if (s
->main_pid_known
) {
813 if (pid
== s
->main_pid
)
816 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
818 service_unwatch_main_pid(s
);
819 s
->main_pid_known
= false;
821 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
823 r
= service_set_main_pid(s
, pid
);
827 r
= unit_watch_pid(UNIT(s
), pid
);
829 /* FIXME: we need to do something here */
830 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
837 static int service_search_main_pid(Service
*s
) {
843 /* If we know it anyway, don't ever fallback to unreliable
845 if (s
->main_pid_known
)
848 if (!s
->guess_main_pid
)
851 assert(s
->main_pid
<= 0);
853 r
= unit_search_main_pid(UNIT(s
), &pid
);
857 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
858 r
= service_set_main_pid(s
, pid
);
862 r
= unit_watch_pid(UNIT(s
), pid
);
864 /* FIXME: we need to do something here */
865 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
872 static void service_set_state(Service
*s
, ServiceState state
) {
873 ServiceState old_state
;
874 const UnitActiveState
*table
;
878 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
880 old_state
= s
->state
;
883 service_unwatch_pid_file(s
);
886 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
889 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
890 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
891 SERVICE_AUTO_RESTART
))
892 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
895 SERVICE_START
, SERVICE_START_POST
,
896 SERVICE_RUNNING
, SERVICE_RELOAD
,
897 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
898 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
899 service_unwatch_main_pid(s
);
900 s
->main_command
= NULL
;
904 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
906 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
907 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
908 service_unwatch_control_pid(s
);
909 s
->control_command
= NULL
;
910 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
913 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
914 unit_unwatch_all_pids(UNIT(s
));
917 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
918 SERVICE_RUNNING
, SERVICE_RELOAD
,
919 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
920 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
921 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
922 service_close_socket_fd(s
);
923 service_connection_unref(s
);
926 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
927 service_stop_watchdog(s
);
929 /* For the inactive states unit_notify() will trim the cgroup,
930 * but for exit we have to do that ourselves... */
931 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
932 unit_prune_cgroup(UNIT(s
));
934 /* For remain_after_exit services, let's see if we can "release" the
935 * hold on the console, since unit_notify() only does that in case of
937 if (state
== SERVICE_EXITED
&&
938 s
->remain_after_exit
&&
939 UNIT(s
)->manager
->n_on_console
> 0) {
943 ec
= unit_get_exec_context(UNIT(s
));
944 if (ec
&& exec_context_may_touch_console(ec
)) {
945 Manager
*m
= UNIT(s
)->manager
;
948 if (m
->n_on_console
== 0)
949 /* unset no_console_output flag, since the console is free */
950 m
->no_console_output
= false;
954 if (old_state
!= state
)
955 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
957 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
960 static usec_t
service_coldplug_timeout(Service
*s
) {
963 switch (s
->deserialized_state
) {
965 case SERVICE_START_PRE
:
967 case SERVICE_START_POST
:
969 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
971 case SERVICE_RUNNING
:
972 return usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
);
975 case SERVICE_STOP_SIGABRT
:
976 case SERVICE_STOP_SIGTERM
:
977 case SERVICE_STOP_SIGKILL
:
978 case SERVICE_STOP_POST
:
979 case SERVICE_FINAL_SIGTERM
:
980 case SERVICE_FINAL_SIGKILL
:
981 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
983 case SERVICE_AUTO_RESTART
:
984 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, s
->restart_usec
);
987 return USEC_INFINITY
;
991 static int service_coldplug(Unit
*u
) {
992 Service
*s
= SERVICE(u
);
996 assert(s
->state
== SERVICE_DEAD
);
998 if (s
->deserialized_state
== s
->state
)
1001 r
= service_arm_timer(s
, service_coldplug_timeout(s
));
1005 if (s
->main_pid
> 0 &&
1006 pid_is_unwaited(s
->main_pid
) &&
1007 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
1008 IN_SET(s
->deserialized_state
,
1009 SERVICE_START
, SERVICE_START_POST
,
1010 SERVICE_RUNNING
, SERVICE_RELOAD
,
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
->main_pid
);
1018 if (s
->control_pid
> 0 &&
1019 pid_is_unwaited(s
->control_pid
) &&
1020 IN_SET(s
->deserialized_state
,
1021 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1023 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1024 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1025 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1030 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1031 unit_watch_all_pids(UNIT(s
));
1033 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1034 service_start_watchdog(s
);
1036 service_set_state(s
, s
->deserialized_state
);
1040 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
1041 _cleanup_strv_free_
char **rfd_names
= NULL
;
1042 _cleanup_free_
int *rfds
= NULL
;
1049 if (s
->socket_fd
>= 0) {
1051 /* Pass the per-connection socket */
1056 rfds
[0] = s
->socket_fd
;
1058 rfd_names
= strv_new("connection", NULL
);
1067 /* Pass all our configured sockets for singleton services */
1069 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1070 _cleanup_free_
int *cfds
= NULL
;
1074 if (u
->type
!= UNIT_SOCKET
)
1079 cn_fds
= socket_collect_fds(sock
, &cfds
);
1094 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1098 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1104 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1110 if (s
->n_fd_store
> 0) {
1115 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1121 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1127 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1128 rfds
[rn_fds
] = fs
->fd
;
1129 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1130 if (!rfd_names
[rn_fds
])
1136 rfd_names
[rn_fds
] = NULL
;
1140 *fd_names
= rfd_names
;
1148 static int service_spawn(
1153 bool apply_permissions
,
1155 bool apply_tty_stdin
,
1159 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1160 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1161 _cleanup_free_
int *fds
= NULL
;
1162 unsigned n_fds
= 0, n_env
= 0;
1166 ExecParameters exec_params
= {
1167 .apply_permissions
= apply_permissions
,
1168 .apply_chroot
= apply_chroot
,
1169 .apply_tty_stdin
= apply_tty_stdin
,
1170 .bus_endpoint_fd
= -1,
1182 (void) unit_realize_cgroup(UNIT(s
));
1183 if (s
->reset_cpu_usage
) {
1184 (void) unit_reset_cpu_usage(UNIT(s
));
1185 s
->reset_cpu_usage
= false;
1188 r
= unit_setup_exec_runtime(UNIT(s
));
1193 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1194 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1195 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1197 r
= service_collect_fds(s
, &fds
, &fd_names
);
1204 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), timeout
));
1208 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1212 our_env
= new0(char*, 6);
1216 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1217 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1220 if (s
->main_pid
> 0)
1221 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0)
1224 if (UNIT(s
)->manager
->running_as
!= MANAGER_SYSTEM
)
1225 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0)
1228 if (s
->socket_fd
>= 0) {
1229 union sockaddr_union sa
;
1230 socklen_t salen
= sizeof(sa
);
1232 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1236 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1237 _cleanup_free_
char *addr
= NULL
;
1241 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1245 t
= strappend("REMOTE_ADDR=", addr
);
1248 our_env
[n_env
++] = t
;
1250 port
= sockaddr_port(&sa
.sa
);
1254 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1256 our_env
[n_env
++] = t
;
1260 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1264 if (is_control
&& UNIT(s
)->cgroup_path
) {
1265 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1266 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1268 path
= UNIT(s
)->cgroup_path
;
1270 if (s
->exec_context
.bus_endpoint
) {
1271 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
? "system" : "user",
1272 UNIT(s
)->id
, &bus_endpoint_path
);
1276 /* Pass the fd to the exec_params so that the child process can upload the policy.
1277 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1278 * as the service is running. */
1279 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1282 exec_params
.argv
= argv
;
1283 exec_params
.fds
= fds
;
1284 exec_params
.fd_names
= fd_names
;
1285 exec_params
.n_fds
= n_fds
;
1286 exec_params
.environment
= final_env
;
1287 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1288 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1289 exec_params
.cgroup_path
= path
;
1290 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1291 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1292 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1293 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1294 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1295 if (s
->type
== SERVICE_IDLE
)
1296 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1297 exec_params
.stdin_fd
= s
->stdin_fd
;
1298 exec_params
.stdout_fd
= s
->stdout_fd
;
1299 exec_params
.stderr_fd
= s
->stderr_fd
;
1301 r
= exec_spawn(UNIT(s
),
1310 r
= unit_watch_pid(UNIT(s
), pid
);
1312 /* FIXME: we need to do something here */
1320 static int main_pid_good(Service
*s
) {
1323 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1326 /* If we know the pid file, then let's just check if it is
1328 if (s
->main_pid_known
) {
1330 /* If it's an alien child let's check if it is still
1332 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1333 return pid_is_alive(s
->main_pid
);
1335 /* .. otherwise assume we'll get a SIGCHLD for it,
1336 * which we really should wait for to collect exit
1337 * status and code */
1338 return s
->main_pid
> 0;
1341 /* We don't know the pid */
1345 _pure_
static int control_pid_good(Service
*s
) {
1348 return s
->control_pid
> 0;
1351 static int cgroup_good(Service
*s
) {
1356 if (!UNIT(s
)->cgroup_path
)
1359 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1366 static bool service_shall_restart(Service
*s
) {
1369 /* Don't restart after manual stops */
1370 if (s
->forbid_restart
)
1373 /* Never restart if this is configured as special exception */
1374 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1377 /* Restart if the exit code/status are configured as restart triggers */
1378 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1381 switch (s
->restart
) {
1383 case SERVICE_RESTART_NO
:
1386 case SERVICE_RESTART_ALWAYS
:
1389 case SERVICE_RESTART_ON_SUCCESS
:
1390 return s
->result
== SERVICE_SUCCESS
;
1392 case SERVICE_RESTART_ON_FAILURE
:
1393 return s
->result
!= SERVICE_SUCCESS
;
1395 case SERVICE_RESTART_ON_ABNORMAL
:
1396 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1398 case SERVICE_RESTART_ON_WATCHDOG
:
1399 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1401 case SERVICE_RESTART_ON_ABORT
:
1402 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1405 assert_not_reached("unknown restart setting");
1409 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1413 if (f
!= SERVICE_SUCCESS
)
1416 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1418 if (s
->result
!= SERVICE_SUCCESS
) {
1419 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1420 failure_action(UNIT(s
)->manager
, s
->failure_action
, UNIT(s
)->reboot_arg
);
1423 if (allow_restart
&& service_shall_restart(s
)) {
1425 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1429 service_set_state(s
, SERVICE_AUTO_RESTART
);
1432 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1433 s
->forbid_restart
= false;
1435 /* We want fresh tmpdirs in case service is started again immediately */
1436 exec_runtime_destroy(s
->exec_runtime
);
1437 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1439 /* Also, remove the runtime directory in */
1440 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1442 /* Try to delete the pid file. At this point it will be
1443 * out-of-date, and some software might be confused by it, so
1444 * let's remove it. */
1446 (void) unlink(s
->pid_file
);
1451 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1452 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1455 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1459 if (f
!= SERVICE_SUCCESS
)
1462 service_unwatch_control_pid(s
);
1463 unit_watch_all_pids(UNIT(s
));
1465 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1466 if (s
->control_command
) {
1467 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1469 r
= service_spawn(s
,
1471 s
->timeout_stop_usec
,
1473 !s
->permissions_start_only
,
1474 !s
->root_directory_start_only
,
1481 service_set_state(s
, SERVICE_STOP_POST
);
1483 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1488 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1489 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1492 static int state_to_kill_operation(ServiceState state
) {
1495 case SERVICE_STOP_SIGABRT
:
1498 case SERVICE_STOP_SIGTERM
:
1499 case SERVICE_FINAL_SIGTERM
:
1500 return KILL_TERMINATE
;
1502 case SERVICE_STOP_SIGKILL
:
1503 case SERVICE_FINAL_SIGKILL
:
1507 return _KILL_OPERATION_INVALID
;
1511 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1516 if (f
!= SERVICE_SUCCESS
)
1519 unit_watch_all_pids(UNIT(s
));
1521 r
= unit_kill_context(
1524 state_to_kill_operation(state
),
1533 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1537 service_set_state(s
, state
);
1538 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1539 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1540 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1541 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1542 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1543 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1545 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1550 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1552 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1553 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1555 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1558 static void service_enter_stop_by_notify(Service
*s
) {
1561 unit_watch_all_pids(UNIT(s
));
1563 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1565 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1566 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1569 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1574 if (f
!= SERVICE_SUCCESS
)
1577 service_unwatch_control_pid(s
);
1578 unit_watch_all_pids(UNIT(s
));
1580 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1581 if (s
->control_command
) {
1582 s
->control_command_id
= SERVICE_EXEC_STOP
;
1584 r
= service_spawn(s
,
1586 s
->timeout_stop_usec
,
1588 !s
->permissions_start_only
,
1589 !s
->root_directory_start_only
,
1596 service_set_state(s
, SERVICE_STOP
);
1598 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1603 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1604 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1607 static bool service_good(Service
*s
) {
1611 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1614 main_pid_ok
= main_pid_good(s
);
1615 if (main_pid_ok
> 0) /* It's alive */
1617 if (main_pid_ok
== 0) /* It's dead */
1620 /* OK, we don't know anything about the main PID, maybe
1621 * because there is none. Let's check the control group
1624 return cgroup_good(s
) != 0;
1627 static void service_enter_running(Service
*s
, ServiceResult f
) {
1630 if (f
!= SERVICE_SUCCESS
)
1633 service_unwatch_control_pid(s
);
1635 if (service_good(s
)) {
1637 /* If there are any queued up sd_notify()
1638 * notifications, process them now */
1639 if (s
->notify_state
== NOTIFY_RELOADING
)
1640 service_enter_reload_by_notify(s
);
1641 else if (s
->notify_state
== NOTIFY_STOPPING
)
1642 service_enter_stop_by_notify(s
);
1644 service_set_state(s
, SERVICE_RUNNING
);
1645 service_arm_timer(s
, usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
));
1648 } else if (s
->remain_after_exit
)
1649 service_set_state(s
, SERVICE_EXITED
);
1651 service_enter_stop(s
, SERVICE_SUCCESS
);
1654 static void service_enter_start_post(Service
*s
) {
1658 service_unwatch_control_pid(s
);
1659 service_reset_watchdog(s
);
1661 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1662 if (s
->control_command
) {
1663 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1665 r
= service_spawn(s
,
1667 s
->timeout_start_usec
,
1669 !s
->permissions_start_only
,
1670 !s
->root_directory_start_only
,
1677 service_set_state(s
, SERVICE_START_POST
);
1679 service_enter_running(s
, SERVICE_SUCCESS
);
1684 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1685 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1688 static void service_kill_control_processes(Service
*s
) {
1691 if (!UNIT(s
)->cgroup_path
)
1694 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1695 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1698 static void service_enter_start(Service
*s
) {
1706 service_unwatch_control_pid(s
);
1707 service_unwatch_main_pid(s
);
1709 /* We want to ensure that nobody leaks processes from
1710 * START_PRE here, so let's go on a killing spree, People
1711 * should not spawn long running processes from START_PRE. */
1712 service_kill_control_processes(s
);
1714 if (s
->type
== SERVICE_FORKING
) {
1715 s
->control_command_id
= SERVICE_EXEC_START
;
1716 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1718 s
->main_command
= NULL
;
1720 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1721 s
->control_command
= NULL
;
1723 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1727 assert(s
->type
== SERVICE_ONESHOT
);
1728 service_enter_start_post(s
);
1732 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
1733 /* For simple + idle this is the main process. We don't apply any timeout here, but
1734 * service_enter_running() will later apply the .runtime_max_usec timeout. */
1735 timeout
= USEC_INFINITY
;
1737 timeout
= s
->timeout_start_usec
;
1739 r
= service_spawn(s
,
1751 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
1752 /* For simple services we immediately start
1753 * the START_POST binaries. */
1755 service_set_main_pid(s
, pid
);
1756 service_enter_start_post(s
);
1758 } else if (s
->type
== SERVICE_FORKING
) {
1760 /* For forking services we wait until the start
1761 * process exited. */
1763 s
->control_pid
= pid
;
1764 service_set_state(s
, SERVICE_START
);
1766 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
)) {
1768 /* For oneshot services we wait until the start
1769 * process exited, too, but it is our main process. */
1771 /* For D-Bus services we know the main pid right away,
1772 * but wait for the bus name to appear on the
1773 * bus. Notify services are similar. */
1775 service_set_main_pid(s
, pid
);
1776 service_set_state(s
, SERVICE_START
);
1778 assert_not_reached("Unknown service type");
1783 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1784 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1787 static void service_enter_start_pre(Service
*s
) {
1792 service_unwatch_control_pid(s
);
1794 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1795 if (s
->control_command
) {
1796 /* Before we start anything, let's clear up what might
1797 * be left from previous runs. */
1798 service_kill_control_processes(s
);
1800 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1802 r
= service_spawn(s
,
1804 s
->timeout_start_usec
,
1806 !s
->permissions_start_only
,
1807 !s
->root_directory_start_only
,
1814 service_set_state(s
, SERVICE_START_PRE
);
1816 service_enter_start(s
);
1821 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1822 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1825 static void service_enter_restart(Service
*s
) {
1826 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1831 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1832 /* Don't restart things if we are going down anyway */
1833 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1835 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1842 /* Any units that are bound to this service must also be
1843 * restarted. We use JOB_RESTART (instead of the more obvious
1844 * JOB_START) here so that those dependency jobs will be added
1846 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, &error
, NULL
);
1850 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1851 * it will be canceled as part of the service_stop() call that
1852 * is executed as part of JOB_RESTART. */
1854 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1858 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1859 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1862 static void service_enter_reload_by_notify(Service
*s
) {
1865 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_start_usec
));
1866 service_set_state(s
, SERVICE_RELOAD
);
1869 static void service_enter_reload(Service
*s
) {
1874 service_unwatch_control_pid(s
);
1875 s
->reload_result
= SERVICE_SUCCESS
;
1877 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1878 if (s
->control_command
) {
1879 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1881 r
= service_spawn(s
,
1883 s
->timeout_start_usec
,
1885 !s
->permissions_start_only
,
1886 !s
->root_directory_start_only
,
1893 service_set_state(s
, SERVICE_RELOAD
);
1895 service_enter_running(s
, SERVICE_SUCCESS
);
1900 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1901 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1902 service_enter_running(s
, SERVICE_SUCCESS
);
1905 static void service_run_next_control(Service
*s
) {
1910 assert(s
->control_command
);
1911 assert(s
->control_command
->command_next
);
1913 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1915 s
->control_command
= s
->control_command
->command_next
;
1916 service_unwatch_control_pid(s
);
1918 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1919 timeout
= s
->timeout_start_usec
;
1921 timeout
= s
->timeout_stop_usec
;
1923 r
= service_spawn(s
,
1927 !s
->permissions_start_only
,
1928 !s
->root_directory_start_only
,
1929 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1930 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1939 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1941 if (s
->state
== SERVICE_START_PRE
)
1942 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1943 else if (s
->state
== SERVICE_STOP
)
1944 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1945 else if (s
->state
== SERVICE_STOP_POST
)
1946 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1947 else if (s
->state
== SERVICE_RELOAD
) {
1948 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1949 service_enter_running(s
, SERVICE_SUCCESS
);
1951 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1954 static void service_run_next_main(Service
*s
) {
1959 assert(s
->main_command
);
1960 assert(s
->main_command
->command_next
);
1961 assert(s
->type
== SERVICE_ONESHOT
);
1963 s
->main_command
= s
->main_command
->command_next
;
1964 service_unwatch_main_pid(s
);
1966 r
= service_spawn(s
,
1968 s
->timeout_start_usec
,
1978 service_set_main_pid(s
, pid
);
1983 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1984 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1987 static int service_start(Unit
*u
) {
1988 Service
*s
= SERVICE(u
);
1992 /* We cannot fulfill this request right now, try again later
1994 if (IN_SET(s
->state
,
1995 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1996 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1999 /* Already on it! */
2000 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
2003 /* A service that will be restarted must be stopped first to
2004 * trigger BindsTo and/or OnFailure dependencies. If a user
2005 * does not want to wait for the holdoff time to elapse, the
2006 * service should be manually restarted, not started. We
2007 * simply return EAGAIN here, so that any start jobs stay
2008 * queued, and assume that the auto restart timer will
2009 * eventually trigger the restart. */
2010 if (s
->state
== SERVICE_AUTO_RESTART
)
2013 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
2015 s
->result
= SERVICE_SUCCESS
;
2016 s
->reload_result
= SERVICE_SUCCESS
;
2017 s
->main_pid_known
= false;
2018 s
->main_pid_alien
= false;
2019 s
->forbid_restart
= false;
2020 s
->reset_cpu_usage
= true;
2022 s
->status_text
= mfree(s
->status_text
);
2023 s
->status_errno
= 0;
2025 s
->notify_state
= NOTIFY_UNKNOWN
;
2027 service_enter_start_pre(s
);
2031 static int service_stop(Unit
*u
) {
2032 Service
*s
= SERVICE(u
);
2036 /* Don't create restart jobs from manual stops. */
2037 s
->forbid_restart
= true;
2040 if (IN_SET(s
->state
,
2041 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2042 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2045 /* A restart will be scheduled or is in progress. */
2046 if (s
->state
== SERVICE_AUTO_RESTART
) {
2047 service_set_state(s
, SERVICE_DEAD
);
2051 /* If there's already something running we go directly into
2053 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2054 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2058 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2060 service_enter_stop(s
, SERVICE_SUCCESS
);
2064 static int service_reload(Unit
*u
) {
2065 Service
*s
= SERVICE(u
);
2069 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2071 service_enter_reload(s
);
2075 _pure_
static bool service_can_reload(Unit
*u
) {
2076 Service
*s
= SERVICE(u
);
2080 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2083 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2084 Service
*s
= SERVICE(u
);
2092 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2093 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2094 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2096 if (s
->control_pid
> 0)
2097 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2099 if (s
->main_pid_known
&& s
->main_pid
> 0)
2100 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2102 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2103 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2104 unit_serialize_item(u
, f
, "bus-name-owner", s
->bus_name_owner
);
2106 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2110 /* FIXME: There's a minor uncleanliness here: if there are
2111 * multiple commands attached here, we will start from the
2112 * first one again */
2113 if (s
->control_command_id
>= 0)
2114 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2116 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2119 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2122 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2126 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2129 r
= unit_serialize_item_fd(u
, f
, fds
, "endpoint-fd", s
->bus_endpoint_fd
);
2133 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2134 _cleanup_free_
char *c
= NULL
;
2137 copy
= fdset_put_dup(fds
, fs
->fd
);
2141 c
= cescape(fs
->fdname
);
2143 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2146 if (s
->main_exec_status
.pid
> 0) {
2147 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2148 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2149 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2151 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2152 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2153 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2157 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
2158 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2160 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2165 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2166 Service
*s
= SERVICE(u
);
2174 if (streq(key
, "state")) {
2177 state
= service_state_from_string(value
);
2179 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2181 s
->deserialized_state
= state
;
2182 } else if (streq(key
, "result")) {
2185 f
= service_result_from_string(value
);
2187 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2188 else if (f
!= SERVICE_SUCCESS
)
2191 } else if (streq(key
, "reload-result")) {
2194 f
= service_result_from_string(value
);
2196 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2197 else if (f
!= SERVICE_SUCCESS
)
2198 s
->reload_result
= f
;
2200 } else if (streq(key
, "control-pid")) {
2203 if (parse_pid(value
, &pid
) < 0)
2204 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2206 s
->control_pid
= pid
;
2207 } else if (streq(key
, "main-pid")) {
2210 if (parse_pid(value
, &pid
) < 0)
2211 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2213 service_set_main_pid(s
, pid
);
2214 unit_watch_pid(UNIT(s
), pid
);
2216 } else if (streq(key
, "main-pid-known")) {
2219 b
= parse_boolean(value
);
2221 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2223 s
->main_pid_known
= b
;
2224 } else if (streq(key
, "bus-name-good")) {
2227 b
= parse_boolean(value
);
2229 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2231 s
->bus_name_good
= b
;
2232 } else if (streq(key
, "bus-name-owner")) {
2233 r
= free_and_strdup(&s
->bus_name_owner
, value
);
2235 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
2236 } else if (streq(key
, "status-text")) {
2239 r
= cunescape(value
, 0, &t
);
2241 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2243 free(s
->status_text
);
2247 } else if (streq(key
, "control-command")) {
2248 ServiceExecCommand id
;
2250 id
= service_exec_command_from_string(value
);
2252 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2254 s
->control_command_id
= id
;
2255 s
->control_command
= s
->exec_command
[id
];
2257 } else if (streq(key
, "socket-fd")) {
2260 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2261 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2263 asynchronous_close(s
->socket_fd
);
2264 s
->socket_fd
= fdset_remove(fds
, fd
);
2266 } else if (streq(key
, "endpoint-fd")) {
2269 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2270 log_unit_debug(u
, "Failed to parse endpoint-fd value: %s", value
);
2272 safe_close(s
->bus_endpoint_fd
);
2273 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2275 } else if (streq(key
, "fd-store-fd")) {
2280 pf
= strcspn(value
, WHITESPACE
);
2281 fdv
= strndupa(value
, pf
);
2283 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2284 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2286 _cleanup_free_
char *t
= NULL
;
2290 fdn
+= strspn(fdn
, WHITESPACE
);
2291 (void) cunescape(fdn
, 0, &t
);
2293 r
= service_add_fd_store(s
, fd
, t
);
2295 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2297 fdset_remove(fds
, fd
);
2300 } else if (streq(key
, "main-exec-status-pid")) {
2303 if (parse_pid(value
, &pid
) < 0)
2304 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2306 s
->main_exec_status
.pid
= pid
;
2307 } else if (streq(key
, "main-exec-status-code")) {
2310 if (safe_atoi(value
, &i
) < 0)
2311 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2313 s
->main_exec_status
.code
= i
;
2314 } else if (streq(key
, "main-exec-status-status")) {
2317 if (safe_atoi(value
, &i
) < 0)
2318 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2320 s
->main_exec_status
.status
= i
;
2321 } else if (streq(key
, "main-exec-status-start"))
2322 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2323 else if (streq(key
, "main-exec-status-exit"))
2324 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2325 else if (streq(key
, "watchdog-timestamp"))
2326 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2327 else if (streq(key
, "forbid-restart")) {
2330 b
= parse_boolean(value
);
2332 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2334 s
->forbid_restart
= b
;
2335 } else if (streq(key
, "stdin-fd")) {
2338 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2339 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2341 asynchronous_close(s
->stdin_fd
);
2342 s
->stdin_fd
= fdset_remove(fds
, fd
);
2343 s
->exec_context
.stdio_as_fds
= true;
2345 } else if (streq(key
, "stdout-fd")) {
2348 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2349 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2351 asynchronous_close(s
->stdout_fd
);
2352 s
->stdout_fd
= fdset_remove(fds
, fd
);
2353 s
->exec_context
.stdio_as_fds
= true;
2355 } else if (streq(key
, "stderr-fd")) {
2358 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2359 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2361 asynchronous_close(s
->stderr_fd
);
2362 s
->stderr_fd
= fdset_remove(fds
, fd
);
2363 s
->exec_context
.stdio_as_fds
= true;
2366 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2371 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2372 const UnitActiveState
*table
;
2376 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2378 return table
[SERVICE(u
)->state
];
2381 static const char *service_sub_state_to_string(Unit
*u
) {
2384 return service_state_to_string(SERVICE(u
)->state
);
2387 static bool service_check_gc(Unit
*u
) {
2388 Service
*s
= SERVICE(u
);
2392 /* Never clean up services that still have a process around,
2393 * even if the service is formally dead. */
2394 if (cgroup_good(s
) > 0 ||
2395 main_pid_good(s
) > 0 ||
2396 control_pid_good(s
) > 0)
2402 static int service_retry_pid_file(Service
*s
) {
2405 assert(s
->pid_file
);
2406 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2408 r
= service_load_pid_file(s
, false);
2412 service_unwatch_pid_file(s
);
2414 service_enter_running(s
, SERVICE_SUCCESS
);
2418 static int service_watch_pid_file(Service
*s
) {
2421 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2423 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2427 /* the pidfile might have appeared just before we set the watch */
2428 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2429 service_retry_pid_file(s
);
2433 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2434 service_unwatch_pid_file(s
);
2438 static int service_demand_pid_file(Service
*s
) {
2441 assert(s
->pid_file
);
2442 assert(!s
->pid_file_pathspec
);
2444 ps
= new0(PathSpec
, 1);
2449 ps
->path
= strdup(s
->pid_file
);
2455 path_kill_slashes(ps
->path
);
2457 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2458 * keep their PID file open all the time. */
2459 ps
->type
= PATH_MODIFIED
;
2460 ps
->inotify_fd
= -1;
2462 s
->pid_file_pathspec
= ps
;
2464 return service_watch_pid_file(s
);
2467 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2468 PathSpec
*p
= userdata
;
2473 s
= SERVICE(p
->unit
);
2477 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2478 assert(s
->pid_file_pathspec
);
2479 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2481 log_unit_debug(UNIT(s
), "inotify event");
2483 if (path_spec_fd_event(p
, events
) < 0)
2486 if (service_retry_pid_file(s
) == 0)
2489 if (service_watch_pid_file(s
) < 0)
2495 service_unwatch_pid_file(s
);
2496 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2500 static void service_notify_cgroup_empty_event(Unit
*u
) {
2501 Service
*s
= SERVICE(u
);
2505 log_unit_debug(u
, "cgroup is empty");
2509 /* Waiting for SIGCHLD is usually more interesting,
2510 * because it includes return codes/signals. Which is
2511 * why we ignore the cgroup events for most cases,
2512 * except when we don't know pid which to expect the
2516 case SERVICE_START_POST
:
2517 /* If we were hoping for the daemon to write its PID file,
2518 * we can give up now. */
2519 if (s
->pid_file_pathspec
) {
2520 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2522 service_unwatch_pid_file(s
);
2523 if (s
->state
== SERVICE_START
)
2524 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2526 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2530 case SERVICE_RUNNING
:
2531 /* service_enter_running() will figure out what to do */
2532 service_enter_running(s
, SERVICE_SUCCESS
);
2535 case SERVICE_STOP_SIGABRT
:
2536 case SERVICE_STOP_SIGTERM
:
2537 case SERVICE_STOP_SIGKILL
:
2539 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2540 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2544 case SERVICE_STOP_POST
:
2545 case SERVICE_FINAL_SIGTERM
:
2546 case SERVICE_FINAL_SIGKILL
:
2547 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2548 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2557 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2558 Service
*s
= SERVICE(u
);
2564 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2565 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2566 f
= SERVICE_SUCCESS
;
2567 else if (code
== CLD_EXITED
)
2568 f
= SERVICE_FAILURE_EXIT_CODE
;
2569 else if (code
== CLD_KILLED
)
2570 f
= SERVICE_FAILURE_SIGNAL
;
2571 else if (code
== CLD_DUMPED
)
2572 f
= SERVICE_FAILURE_CORE_DUMP
;
2574 assert_not_reached("Unknown code");
2576 if (s
->main_pid
== pid
) {
2577 /* Forking services may occasionally move to a new PID.
2578 * As long as they update the PID file before exiting the old
2579 * PID, they're fine. */
2580 if (service_load_pid_file(s
, false) == 0)
2584 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2586 if (s
->main_command
) {
2587 /* If this is not a forking service than the
2588 * main process got started and hence we copy
2589 * the exit status so that it is recorded both
2590 * as main and as control process exit
2593 s
->main_command
->exec_status
= s
->main_exec_status
;
2595 if (s
->main_command
->ignore
)
2596 f
= SERVICE_SUCCESS
;
2597 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2599 /* If this is a forked process, then we should
2600 * ignore the return value if this was
2601 * configured for the starter process */
2603 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2604 f
= SERVICE_SUCCESS
;
2607 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2609 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2610 sigchld_code_to_string(code
), status
,
2611 strna(code
== CLD_EXITED
2612 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2613 : signal_to_string(status
))),
2614 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2615 "EXIT_STATUS=%i", status
,
2618 if (f
!= SERVICE_SUCCESS
)
2621 if (s
->main_command
&&
2622 s
->main_command
->command_next
&&
2623 f
== SERVICE_SUCCESS
) {
2625 /* There is another command to *
2626 * execute, so let's do that. */
2628 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2629 service_run_next_main(s
);
2633 /* The service exited, so the service is officially
2635 s
->main_command
= NULL
;
2639 case SERVICE_START_POST
:
2640 case SERVICE_RELOAD
:
2642 /* Need to wait until the operation is
2647 if (s
->type
== SERVICE_ONESHOT
) {
2648 /* This was our main goal, so let's go on */
2649 if (f
== SERVICE_SUCCESS
)
2650 service_enter_start_post(s
);
2652 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2658 case SERVICE_RUNNING
:
2659 service_enter_running(s
, f
);
2662 case SERVICE_STOP_SIGABRT
:
2663 case SERVICE_STOP_SIGTERM
:
2664 case SERVICE_STOP_SIGKILL
:
2666 if (!control_pid_good(s
))
2667 service_enter_stop_post(s
, f
);
2669 /* If there is still a control process, wait for that first */
2672 case SERVICE_STOP_POST
:
2673 case SERVICE_FINAL_SIGTERM
:
2674 case SERVICE_FINAL_SIGKILL
:
2676 if (!control_pid_good(s
))
2677 service_enter_dead(s
, f
, true);
2681 assert_not_reached("Uh, main process died at wrong time.");
2685 } else if (s
->control_pid
== pid
) {
2688 if (s
->control_command
) {
2689 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2691 if (s
->control_command
->ignore
)
2692 f
= SERVICE_SUCCESS
;
2695 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2696 "Control process exited, code=%s status=%i",
2697 sigchld_code_to_string(code
), status
);
2699 if (f
!= SERVICE_SUCCESS
)
2702 /* Immediately get rid of the cgroup, so that the
2703 * kernel doesn't delay the cgroup empty messages for
2704 * the service cgroup any longer than necessary */
2705 service_kill_control_processes(s
);
2707 if (s
->control_command
&&
2708 s
->control_command
->command_next
&&
2709 f
== SERVICE_SUCCESS
) {
2711 /* There is another command to *
2712 * execute, so let's do that. */
2714 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2715 service_run_next_control(s
);
2718 /* No further commands for this step, so let's
2719 * figure out what to do next */
2721 s
->control_command
= NULL
;
2722 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2724 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2728 case SERVICE_START_PRE
:
2729 if (f
== SERVICE_SUCCESS
)
2730 service_enter_start(s
);
2732 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2736 if (s
->type
!= SERVICE_FORKING
)
2737 /* Maybe spurious event due to a reload that changed the type? */
2740 if (f
!= SERVICE_SUCCESS
) {
2741 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2746 bool has_start_post
;
2749 /* Let's try to load the pid file here if we can.
2750 * The PID file might actually be created by a START_POST
2751 * script. In that case don't worry if the loading fails. */
2753 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2754 r
= service_load_pid_file(s
, !has_start_post
);
2755 if (!has_start_post
&& r
< 0) {
2756 r
= service_demand_pid_file(s
);
2757 if (r
< 0 || !cgroup_good(s
))
2758 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2762 (void) service_search_main_pid(s
);
2764 service_enter_start_post(s
);
2767 case SERVICE_START_POST
:
2768 if (f
!= SERVICE_SUCCESS
) {
2769 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2776 r
= service_load_pid_file(s
, true);
2778 r
= service_demand_pid_file(s
);
2779 if (r
< 0 || !cgroup_good(s
))
2780 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2784 (void) service_search_main_pid(s
);
2786 service_enter_running(s
, SERVICE_SUCCESS
);
2789 case SERVICE_RELOAD
:
2790 if (f
== SERVICE_SUCCESS
) {
2791 service_load_pid_file(s
, true);
2792 (void) service_search_main_pid(s
);
2795 s
->reload_result
= f
;
2796 service_enter_running(s
, SERVICE_SUCCESS
);
2800 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2803 case SERVICE_STOP_SIGABRT
:
2804 case SERVICE_STOP_SIGTERM
:
2805 case SERVICE_STOP_SIGKILL
:
2806 if (main_pid_good(s
) <= 0)
2807 service_enter_stop_post(s
, f
);
2809 /* If there is still a service
2810 * process around, wait until
2811 * that one quit, too */
2814 case SERVICE_STOP_POST
:
2815 case SERVICE_FINAL_SIGTERM
:
2816 case SERVICE_FINAL_SIGKILL
:
2817 if (main_pid_good(s
) <= 0)
2818 service_enter_dead(s
, f
, true);
2822 assert_not_reached("Uh, control process died at wrong time.");
2827 /* Notify clients about changed exit status */
2828 unit_add_to_dbus_queue(u
);
2830 /* We got one SIGCHLD for the service, let's watch all
2831 * processes that are now running of the service, and watch
2832 * that. Among the PIDs we then watch will be children
2833 * reassigned to us, which hopefully allows us to identify
2834 * when all children are gone */
2835 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2836 unit_watch_all_pids(u
);
2838 /* If the PID set is empty now, then let's finish this off */
2839 if (set_isempty(u
->pids
))
2840 service_notify_cgroup_empty_event(u
);
2843 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2844 Service
*s
= SERVICE(userdata
);
2847 assert(source
== s
->timer_event_source
);
2851 case SERVICE_START_PRE
:
2853 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2854 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2857 case SERVICE_START_POST
:
2858 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2859 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2862 case SERVICE_RUNNING
:
2863 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
2864 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2867 case SERVICE_RELOAD
:
2868 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
2869 service_kill_control_processes(s
);
2870 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2871 service_enter_running(s
, SERVICE_SUCCESS
);
2875 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2876 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2879 case SERVICE_STOP_SIGABRT
:
2880 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2881 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2884 case SERVICE_STOP_SIGTERM
:
2885 if (s
->kill_context
.send_sigkill
) {
2886 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2887 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2889 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2890 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2895 case SERVICE_STOP_SIGKILL
:
2896 /* Uh, we sent a SIGKILL and it is still not gone?
2897 * Must be something we cannot kill, so let's just be
2898 * weirded out and continue */
2900 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2901 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2904 case SERVICE_STOP_POST
:
2905 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2906 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2909 case SERVICE_FINAL_SIGTERM
:
2910 if (s
->kill_context
.send_sigkill
) {
2911 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2912 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2914 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2915 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2920 case SERVICE_FINAL_SIGKILL
:
2921 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2922 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2925 case SERVICE_AUTO_RESTART
:
2926 log_unit_info(UNIT(s
),
2927 s
->restart_usec
> 0 ?
2928 "Service hold-off time over, scheduling restart." :
2929 "Service has no hold-off time, scheduling restart.");
2930 service_enter_restart(s
);
2934 assert_not_reached("Timeout at wrong time.");
2940 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2941 Service
*s
= SERVICE(userdata
);
2942 char t
[FORMAT_TIMESPAN_MAX
];
2945 assert(source
== s
->watchdog_event_source
);
2947 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2948 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2950 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2955 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2956 Service
*s
= SERVICE(u
);
2957 _cleanup_free_
char *cc
= NULL
;
2958 bool notify_dbus
= false;
2963 cc
= strv_join(tags
, ", ");
2965 if (s
->notify_access
== NOTIFY_NONE
) {
2966 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2968 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2969 if (s
->main_pid
!= 0)
2970 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2972 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2975 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2977 /* Interpret MAINPID= */
2978 e
= strv_find_startswith(tags
, "MAINPID=");
2979 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2980 if (parse_pid(e
, &pid
) < 0)
2981 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2983 service_set_main_pid(s
, pid
);
2984 unit_watch_pid(UNIT(s
), pid
);
2989 /* Interpret RELOADING= */
2990 if (strv_find(tags
, "RELOADING=1")) {
2992 s
->notify_state
= NOTIFY_RELOADING
;
2994 if (s
->state
== SERVICE_RUNNING
)
2995 service_enter_reload_by_notify(s
);
3000 /* Interpret READY= */
3001 if (strv_find(tags
, "READY=1")) {
3003 s
->notify_state
= NOTIFY_READY
;
3005 /* Type=notify services inform us about completed
3006 * initialization with READY=1 */
3007 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
3008 service_enter_start_post(s
);
3010 /* Sending READY=1 while we are reloading informs us
3011 * that the reloading is complete */
3012 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
3013 service_enter_running(s
, SERVICE_SUCCESS
);
3018 /* Interpret STOPPING= */
3019 if (strv_find(tags
, "STOPPING=1")) {
3021 s
->notify_state
= NOTIFY_STOPPING
;
3023 if (s
->state
== SERVICE_RUNNING
)
3024 service_enter_stop_by_notify(s
);
3029 /* Interpret STATUS= */
3030 e
= strv_find_startswith(tags
, "STATUS=");
3032 _cleanup_free_
char *t
= NULL
;
3035 if (!utf8_is_valid(e
))
3036 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3044 if (!streq_ptr(s
->status_text
, t
)) {
3046 free(s
->status_text
);
3054 /* Interpret ERRNO= */
3055 e
= strv_find_startswith(tags
, "ERRNO=");
3059 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3060 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3062 if (s
->status_errno
!= status_errno
) {
3063 s
->status_errno
= status_errno
;
3069 /* Interpret WATCHDOG= */
3070 if (strv_find(tags
, "WATCHDOG=1"))
3071 service_reset_watchdog(s
);
3073 if (strv_find(tags
, "FDSTORE=1")) {
3076 name
= strv_find_startswith(tags
, "FDNAME=");
3077 if (name
&& !fdname_is_valid(name
)) {
3078 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3082 service_add_fd_store_set(s
, fds
, name
);
3085 /* Notify clients about changed status or main pid */
3087 unit_add_to_dbus_queue(u
);
3090 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
3091 Service
*s
= SERVICE(u
);
3095 if (!s
->timer_event_source
)
3098 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3101 if (t
== USEC_INFINITY
)
3108 static void service_bus_name_owner_change(
3111 const char *old_owner
,
3112 const char *new_owner
) {
3114 Service
*s
= SERVICE(u
);
3120 assert(streq(s
->bus_name
, name
));
3121 assert(old_owner
|| new_owner
);
3123 if (old_owner
&& new_owner
)
3124 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3126 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3128 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3130 s
->bus_name_good
= !!new_owner
;
3132 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3133 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
3135 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
3139 if (s
->type
== SERVICE_DBUS
) {
3141 /* service_enter_running() will figure out what to
3143 if (s
->state
== SERVICE_RUNNING
)
3144 service_enter_running(s
, SERVICE_SUCCESS
);
3145 else if (s
->state
== SERVICE_START
&& new_owner
)
3146 service_enter_start_post(s
);
3148 } else if (new_owner
&&
3150 (s
->state
== SERVICE_START
||
3151 s
->state
== SERVICE_START_POST
||
3152 s
->state
== SERVICE_RUNNING
||
3153 s
->state
== SERVICE_RELOAD
)) {
3155 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3158 /* Try to acquire PID from bus service */
3160 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3162 r
= sd_bus_creds_get_pid(creds
, &pid
);
3164 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3166 service_set_main_pid(s
, pid
);
3167 unit_watch_pid(UNIT(s
), pid
);
3172 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3173 _cleanup_free_
char *peer
= NULL
;
3179 /* This is called by the socket code when instantiating a new
3180 * service for a stream socket and the socket needs to be
3183 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3186 if (s
->socket_fd
>= 0)
3189 if (s
->state
!= SERVICE_DEAD
)
3192 if (getpeername_pretty(fd
, true, &peer
) >= 0) {
3194 if (UNIT(s
)->description
) {
3195 _cleanup_free_
char *a
;
3197 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3201 r
= unit_set_description(UNIT(s
), a
);
3203 r
= unit_set_description(UNIT(s
), peer
);
3210 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3212 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3214 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3217 static void service_reset_failed(Unit
*u
) {
3218 Service
*s
= SERVICE(u
);
3222 if (s
->state
== SERVICE_FAILED
)
3223 service_set_state(s
, SERVICE_DEAD
);
3225 s
->result
= SERVICE_SUCCESS
;
3226 s
->reload_result
= SERVICE_SUCCESS
;
3229 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3230 Service
*s
= SERVICE(u
);
3232 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3235 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3236 [SERVICE_RESTART_NO
] = "no",
3237 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3238 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3239 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3240 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3241 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3242 [SERVICE_RESTART_ALWAYS
] = "always",
3245 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3247 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3248 [SERVICE_SIMPLE
] = "simple",
3249 [SERVICE_FORKING
] = "forking",
3250 [SERVICE_ONESHOT
] = "oneshot",
3251 [SERVICE_DBUS
] = "dbus",
3252 [SERVICE_NOTIFY
] = "notify",
3253 [SERVICE_IDLE
] = "idle"
3256 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3258 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3259 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3260 [SERVICE_EXEC_START
] = "ExecStart",
3261 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3262 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3263 [SERVICE_EXEC_STOP
] = "ExecStop",
3264 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3267 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3269 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3270 [NOTIFY_NONE
] = "none",
3271 [NOTIFY_MAIN
] = "main",
3272 [NOTIFY_ALL
] = "all"
3275 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3277 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3278 [NOTIFY_UNKNOWN
] = "unknown",
3279 [NOTIFY_READY
] = "ready",
3280 [NOTIFY_RELOADING
] = "reloading",
3281 [NOTIFY_STOPPING
] = "stopping",
3284 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3286 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3287 [SERVICE_SUCCESS
] = "success",
3288 [SERVICE_FAILURE_RESOURCES
] = "resources",
3289 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3290 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3291 [SERVICE_FAILURE_SIGNAL
] = "signal",
3292 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3293 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3296 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3298 const UnitVTable service_vtable
= {
3299 .object_size
= sizeof(Service
),
3300 .exec_context_offset
= offsetof(Service
, exec_context
),
3301 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3302 .kill_context_offset
= offsetof(Service
, kill_context
),
3303 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3309 .private_section
= "Service",
3311 .init
= service_init
,
3312 .done
= service_done
,
3313 .load
= service_load
,
3314 .release_resources
= service_release_resources
,
3316 .coldplug
= service_coldplug
,
3318 .dump
= service_dump
,
3320 .start
= service_start
,
3321 .stop
= service_stop
,
3322 .reload
= service_reload
,
3324 .can_reload
= service_can_reload
,
3326 .kill
= service_kill
,
3328 .serialize
= service_serialize
,
3329 .deserialize_item
= service_deserialize_item
,
3331 .active_state
= service_active_state
,
3332 .sub_state_to_string
= service_sub_state_to_string
,
3334 .check_gc
= service_check_gc
,
3336 .sigchld_event
= service_sigchld_event
,
3338 .reset_failed
= service_reset_failed
,
3340 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3341 .notify_message
= service_notify_message
,
3343 .bus_name_owner_change
= service_bus_name_owner_change
,
3345 .bus_vtable
= bus_service_vtable
,
3346 .bus_set_property
= bus_service_set_property
,
3347 .bus_commit_properties
= bus_service_commit_properties
,
3349 .get_timeout
= service_get_timeout
,
3350 .can_transient
= true,
3352 .status_message_formats
= {
3353 .starting_stopping
= {
3354 [0] = "Starting %s...",
3355 [1] = "Stopping %s...",
3357 .finished_start_job
= {
3358 [JOB_DONE
] = "Started %s.",
3359 [JOB_FAILED
] = "Failed to start %s.",
3361 .finished_stop_job
= {
3362 [JOB_DONE
] = "Stopped %s.",
3363 [JOB_FAILED
] = "Stopped (with error) %s.",