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
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -1;
117 s
->guess_main_pid
= true;
119 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
122 static void service_unwatch_control_pid(Service
*s
) {
125 if (s
->control_pid
<= 0)
128 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
132 static void service_unwatch_main_pid(Service
*s
) {
135 if (s
->main_pid
<= 0)
138 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
142 static void service_unwatch_pid_file(Service
*s
) {
143 if (!s
->pid_file_pathspec
)
146 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
147 path_spec_unwatch(s
->pid_file_pathspec
);
148 path_spec_done(s
->pid_file_pathspec
);
149 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
152 static int service_set_main_pid(Service
*s
, pid_t pid
) {
163 if (s
->main_pid
== pid
&& s
->main_pid_known
)
166 if (s
->main_pid
!= pid
) {
167 service_unwatch_main_pid(s
);
168 exec_status_start(&s
->main_exec_status
, pid
);
172 s
->main_pid_known
= true;
174 if (get_process_ppid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
175 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
176 s
->main_pid_alien
= true;
178 s
->main_pid_alien
= false;
183 static void service_close_socket_fd(Service
*s
) {
186 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
189 static void service_connection_unref(Service
*s
) {
192 if (!UNIT_ISSET(s
->accept_socket
))
195 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
196 unit_ref_unset(&s
->accept_socket
);
199 static void service_stop_watchdog(Service
*s
) {
202 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
203 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
206 static void service_start_watchdog(Service
*s
) {
211 if (s
->watchdog_usec
<= 0)
214 if (s
->watchdog_event_source
) {
215 r
= sd_event_source_set_time(s
->watchdog_event_source
, usec_add(s
->watchdog_timestamp
.monotonic
, s
->watchdog_usec
));
217 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
221 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
223 r
= sd_event_add_time(
224 UNIT(s
)->manager
->event
,
225 &s
->watchdog_event_source
,
227 usec_add(s
->watchdog_timestamp
.monotonic
, s
->watchdog_usec
), 0,
228 service_dispatch_watchdog
, s
);
230 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
234 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
236 /* Let's process everything else which might be a sign
237 * of living before we consider a service died. */
238 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
242 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
245 static void service_reset_watchdog(Service
*s
) {
248 dual_timestamp_get(&s
->watchdog_timestamp
);
249 service_start_watchdog(s
);
252 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
258 assert(fs
->service
->n_fd_store
> 0);
259 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
260 fs
->service
->n_fd_store
--;
263 if (fs
->event_source
) {
264 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
265 sd_event_source_unref(fs
->event_source
);
273 static void service_release_resources(Unit
*u
) {
274 Service
*s
= SERVICE(u
);
278 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
281 log_unit_debug(u
, "Releasing all resources.");
283 s
->stdin_fd
= safe_close(s
->stdin_fd
);
284 s
->stdout_fd
= safe_close(s
->stdout_fd
);
285 s
->stderr_fd
= safe_close(s
->stderr_fd
);
288 service_fd_store_unlink(s
->fd_store
);
290 assert(s
->n_fd_store
== 0);
293 static void service_done(Unit
*u
) {
294 Service
*s
= SERVICE(u
);
298 s
->pid_file
= mfree(s
->pid_file
);
299 s
->status_text
= mfree(s
->status_text
);
301 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
302 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
303 s
->control_command
= NULL
;
304 s
->main_command
= NULL
;
306 exit_status_set_free(&s
->restart_prevent_status
);
307 exit_status_set_free(&s
->restart_force_status
);
308 exit_status_set_free(&s
->success_status
);
310 /* This will leak a process, but at least no memory or any of
312 service_unwatch_main_pid(s
);
313 service_unwatch_control_pid(s
);
314 service_unwatch_pid_file(s
);
317 unit_unwatch_bus_name(u
, s
->bus_name
);
318 s
->bus_name
= mfree(s
->bus_name
);
321 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
323 service_close_socket_fd(s
);
324 service_connection_unref(s
);
326 unit_ref_unset(&s
->accept_socket
);
328 service_stop_watchdog(s
);
330 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
332 service_release_resources(u
);
335 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
336 ServiceFDStore
*fs
= userdata
;
341 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
342 service_fd_store_unlink(fs
);
346 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
353 if (s
->n_fd_store
>= s
->n_fd_store_max
)
356 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
357 r
= same_fd(fs
->fd
, fd
);
361 /* Already included */
367 fs
= new0(ServiceFDStore
, 1);
373 fs
->fdname
= strdup(name
?: "stored");
379 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
386 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
388 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
394 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
399 if (fdset_size(fds
) <= 0)
402 while (s
->n_fd_store
< s
->n_fd_store_max
) {
403 _cleanup_close_
int fd
= -1;
405 fd
= fdset_steal_first(fds
);
409 r
= service_add_fd_store(s
, fd
, name
);
411 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
413 log_unit_debug(UNIT(s
), "Added fd to fd store.");
418 if (fdset_size(fds
) > 0)
419 log_unit_warning(UNIT(s
), "Tried to store more fds than FileDescriptorStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
424 static int service_arm_timer(Service
*s
, usec_t usec
) {
429 if (s
->timer_event_source
) {
430 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
434 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
437 if (usec
== USEC_INFINITY
)
440 r
= sd_event_add_time(
441 UNIT(s
)->manager
->event
,
442 &s
->timer_event_source
,
445 service_dispatch_timer
, s
);
449 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
454 static int service_verify(Service
*s
) {
457 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
460 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
461 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
465 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
466 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
470 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
471 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
475 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
476 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
480 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
481 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
485 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
486 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
490 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
491 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
495 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
496 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
498 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
499 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
503 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
504 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
506 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
507 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
509 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
510 log_unit_warning(UNIT(s
), "MaxRuntimeSec= has no effect in combination with Type=oneshot. 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 (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
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
= USEC_INFINITY
;
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 void 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 if (unit_search_main_pid(UNIT(s
), &pid
) < 0)
854 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
855 if (service_set_main_pid(s
, pid
) < 0)
858 r
= unit_watch_pid(UNIT(s
), pid
);
860 /* FIXME: we need to do something here */
861 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
864 static void service_set_state(Service
*s
, ServiceState state
) {
865 ServiceState old_state
;
866 const UnitActiveState
*table
;
870 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
872 old_state
= s
->state
;
875 service_unwatch_pid_file(s
);
878 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
881 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
882 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
883 SERVICE_AUTO_RESTART
))
884 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
887 SERVICE_START
, SERVICE_START_POST
,
888 SERVICE_RUNNING
, SERVICE_RELOAD
,
889 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
890 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
891 service_unwatch_main_pid(s
);
892 s
->main_command
= NULL
;
896 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
898 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
899 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
900 service_unwatch_control_pid(s
);
901 s
->control_command
= NULL
;
902 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
905 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
906 unit_unwatch_all_pids(UNIT(s
));
909 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
910 SERVICE_RUNNING
, SERVICE_RELOAD
,
911 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
912 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
913 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
914 service_close_socket_fd(s
);
915 service_connection_unref(s
);
918 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
919 service_stop_watchdog(s
);
921 /* For the inactive states unit_notify() will trim the cgroup,
922 * but for exit we have to do that ourselves... */
923 if (state
== SERVICE_EXITED
&& !MANAGER_IS_RELOADING(UNIT(s
)->manager
))
924 unit_prune_cgroup(UNIT(s
));
926 /* For remain_after_exit services, let's see if we can "release" the
927 * hold on the console, since unit_notify() only does that in case of
929 if (state
== SERVICE_EXITED
&&
930 s
->remain_after_exit
&&
931 UNIT(s
)->manager
->n_on_console
> 0) {
935 ec
= unit_get_exec_context(UNIT(s
));
936 if (ec
&& exec_context_may_touch_console(ec
)) {
937 Manager
*m
= UNIT(s
)->manager
;
940 if (m
->n_on_console
== 0)
941 /* unset no_console_output flag, since the console is free */
942 m
->no_console_output
= false;
946 if (old_state
!= state
)
947 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
949 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
952 static usec_t
service_coldplug_timeout(Service
*s
) {
955 switch (s
->deserialized_state
) {
957 case SERVICE_START_PRE
:
959 case SERVICE_START_POST
:
961 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
963 case SERVICE_RUNNING
:
964 return usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
);
967 case SERVICE_STOP_SIGABRT
:
968 case SERVICE_STOP_SIGTERM
:
969 case SERVICE_STOP_SIGKILL
:
970 case SERVICE_STOP_POST
:
971 case SERVICE_FINAL_SIGTERM
:
972 case SERVICE_FINAL_SIGKILL
:
973 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
975 case SERVICE_AUTO_RESTART
:
976 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, s
->restart_usec
);
979 return USEC_INFINITY
;
983 static int service_coldplug(Unit
*u
) {
984 Service
*s
= SERVICE(u
);
988 assert(s
->state
== SERVICE_DEAD
);
990 if (s
->deserialized_state
== s
->state
)
993 r
= service_arm_timer(s
, service_coldplug_timeout(s
));
997 if (s
->main_pid
> 0 &&
998 pid_is_unwaited(s
->main_pid
) &&
999 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
1000 IN_SET(s
->deserialized_state
,
1001 SERVICE_START
, SERVICE_START_POST
,
1002 SERVICE_RUNNING
, SERVICE_RELOAD
,
1003 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1004 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
1005 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
1010 if (s
->control_pid
> 0 &&
1011 pid_is_unwaited(s
->control_pid
) &&
1012 IN_SET(s
->deserialized_state
,
1013 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1015 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1016 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1017 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1022 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1023 unit_watch_all_pids(UNIT(s
));
1025 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1026 service_start_watchdog(s
);
1028 service_set_state(s
, s
->deserialized_state
);
1032 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
1033 _cleanup_strv_free_
char **rfd_names
= NULL
;
1034 _cleanup_free_
int *rfds
= NULL
;
1041 if (s
->socket_fd
>= 0) {
1043 /* Pass the per-connection socket */
1048 rfds
[0] = s
->socket_fd
;
1050 rfd_names
= strv_new("connection", NULL
);
1059 /* Pass all our configured sockets for singleton services */
1061 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1062 _cleanup_free_
int *cfds
= NULL
;
1066 if (u
->type
!= UNIT_SOCKET
)
1071 cn_fds
= socket_collect_fds(sock
, &cfds
);
1086 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1090 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1096 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1102 if (s
->n_fd_store
> 0) {
1107 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1113 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1119 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1120 rfds
[rn_fds
] = fs
->fd
;
1121 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1122 if (!rfd_names
[rn_fds
])
1128 rfd_names
[rn_fds
] = NULL
;
1132 *fd_names
= rfd_names
;
1140 static int service_spawn(
1145 bool apply_permissions
,
1147 bool apply_tty_stdin
,
1151 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1152 _cleanup_free_
int *fds
= NULL
;
1153 unsigned n_fds
= 0, n_env
= 0;
1157 ExecParameters exec_params
= {
1158 .apply_permissions
= apply_permissions
,
1159 .apply_chroot
= apply_chroot
,
1160 .apply_tty_stdin
= apply_tty_stdin
,
1172 (void) unit_realize_cgroup(UNIT(s
));
1173 if (s
->reset_cpu_usage
) {
1174 (void) unit_reset_cpu_usage(UNIT(s
));
1175 s
->reset_cpu_usage
= false;
1178 r
= unit_setup_exec_runtime(UNIT(s
));
1183 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1184 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1185 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1187 r
= service_collect_fds(s
, &fds
, &fd_names
);
1194 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), timeout
));
1198 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1202 our_env
= new0(char*, 6);
1206 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1207 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1210 if (s
->main_pid
> 0)
1211 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0)
1214 if (!MANAGER_IS_SYSTEM(UNIT(s
)->manager
))
1215 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0)
1218 if (s
->socket_fd
>= 0) {
1219 union sockaddr_union sa
;
1220 socklen_t salen
= sizeof(sa
);
1222 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1226 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1227 _cleanup_free_
char *addr
= NULL
;
1231 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1235 t
= strappend("REMOTE_ADDR=", addr
);
1238 our_env
[n_env
++] = t
;
1240 port
= sockaddr_port(&sa
.sa
);
1244 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1246 our_env
[n_env
++] = t
;
1250 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1254 if (is_control
&& UNIT(s
)->cgroup_path
) {
1255 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1256 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1258 path
= UNIT(s
)->cgroup_path
;
1260 exec_params
.argv
= argv
;
1261 exec_params
.fds
= fds
;
1262 exec_params
.fd_names
= fd_names
;
1263 exec_params
.n_fds
= n_fds
;
1264 exec_params
.environment
= final_env
;
1265 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1266 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1267 exec_params
.cgroup_path
= path
;
1268 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1269 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1270 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1271 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1272 if (s
->type
== SERVICE_IDLE
)
1273 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1274 exec_params
.stdin_fd
= s
->stdin_fd
;
1275 exec_params
.stdout_fd
= s
->stdout_fd
;
1276 exec_params
.stderr_fd
= s
->stderr_fd
;
1278 r
= exec_spawn(UNIT(s
),
1287 r
= unit_watch_pid(UNIT(s
), pid
);
1289 /* FIXME: we need to do something here */
1297 static int main_pid_good(Service
*s
) {
1300 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1303 /* If we know the pid file, then let's just check if it is
1305 if (s
->main_pid_known
) {
1307 /* If it's an alien child let's check if it is still
1309 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1310 return pid_is_alive(s
->main_pid
);
1312 /* .. otherwise assume we'll get a SIGCHLD for it,
1313 * which we really should wait for to collect exit
1314 * status and code */
1315 return s
->main_pid
> 0;
1318 /* We don't know the pid */
1322 _pure_
static int control_pid_good(Service
*s
) {
1325 return s
->control_pid
> 0;
1328 static int cgroup_good(Service
*s
) {
1333 if (!UNIT(s
)->cgroup_path
)
1336 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1343 static bool service_shall_restart(Service
*s
) {
1346 /* Don't restart after manual stops */
1347 if (s
->forbid_restart
)
1350 /* Never restart if this is configured as special exception */
1351 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1354 /* Restart if the exit code/status are configured as restart triggers */
1355 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1358 switch (s
->restart
) {
1360 case SERVICE_RESTART_NO
:
1363 case SERVICE_RESTART_ALWAYS
:
1366 case SERVICE_RESTART_ON_SUCCESS
:
1367 return s
->result
== SERVICE_SUCCESS
;
1369 case SERVICE_RESTART_ON_FAILURE
:
1370 return s
->result
!= SERVICE_SUCCESS
;
1372 case SERVICE_RESTART_ON_ABNORMAL
:
1373 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1375 case SERVICE_RESTART_ON_WATCHDOG
:
1376 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1378 case SERVICE_RESTART_ON_ABORT
:
1379 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1382 assert_not_reached("unknown restart setting");
1386 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1390 if (f
!= SERVICE_SUCCESS
)
1393 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1395 if (s
->result
!= SERVICE_SUCCESS
) {
1396 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1397 failure_action(UNIT(s
)->manager
, s
->failure_action
, UNIT(s
)->reboot_arg
);
1400 if (allow_restart
&& service_shall_restart(s
)) {
1402 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1406 service_set_state(s
, SERVICE_AUTO_RESTART
);
1409 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1410 s
->forbid_restart
= false;
1412 /* We want fresh tmpdirs in case service is started again immediately */
1413 exec_runtime_destroy(s
->exec_runtime
);
1414 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1416 /* Also, remove the runtime directory in */
1417 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1419 /* Try to delete the pid file. At this point it will be
1420 * out-of-date, and some software might be confused by it, so
1421 * let's remove it. */
1423 (void) unlink(s
->pid_file
);
1428 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1429 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1432 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1436 if (f
!= SERVICE_SUCCESS
)
1439 service_unwatch_control_pid(s
);
1440 unit_watch_all_pids(UNIT(s
));
1442 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1443 if (s
->control_command
) {
1444 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1446 r
= service_spawn(s
,
1448 s
->timeout_stop_usec
,
1450 !s
->permissions_start_only
,
1451 !s
->root_directory_start_only
,
1458 service_set_state(s
, SERVICE_STOP_POST
);
1460 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1465 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1466 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1469 static int state_to_kill_operation(ServiceState state
) {
1472 case SERVICE_STOP_SIGABRT
:
1475 case SERVICE_STOP_SIGTERM
:
1476 case SERVICE_FINAL_SIGTERM
:
1477 return KILL_TERMINATE
;
1479 case SERVICE_STOP_SIGKILL
:
1480 case SERVICE_FINAL_SIGKILL
:
1484 return _KILL_OPERATION_INVALID
;
1488 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1493 if (f
!= SERVICE_SUCCESS
)
1496 unit_watch_all_pids(UNIT(s
));
1498 r
= unit_kill_context(
1501 state_to_kill_operation(state
),
1510 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1514 service_set_state(s
, state
);
1515 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1516 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1517 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1518 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1519 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1520 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1522 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1527 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1529 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1530 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1532 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1535 static void service_enter_stop_by_notify(Service
*s
) {
1538 unit_watch_all_pids(UNIT(s
));
1540 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1542 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1543 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1546 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1551 if (f
!= SERVICE_SUCCESS
)
1554 service_unwatch_control_pid(s
);
1555 unit_watch_all_pids(UNIT(s
));
1557 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1558 if (s
->control_command
) {
1559 s
->control_command_id
= SERVICE_EXEC_STOP
;
1561 r
= service_spawn(s
,
1563 s
->timeout_stop_usec
,
1565 !s
->permissions_start_only
,
1566 !s
->root_directory_start_only
,
1573 service_set_state(s
, SERVICE_STOP
);
1575 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1580 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1581 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1584 static bool service_good(Service
*s
) {
1588 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1591 main_pid_ok
= main_pid_good(s
);
1592 if (main_pid_ok
> 0) /* It's alive */
1594 if (main_pid_ok
== 0) /* It's dead */
1597 /* OK, we don't know anything about the main PID, maybe
1598 * because there is none. Let's check the control group
1601 return cgroup_good(s
) != 0;
1604 static void service_enter_running(Service
*s
, ServiceResult f
) {
1607 if (f
!= SERVICE_SUCCESS
)
1610 service_unwatch_control_pid(s
);
1612 if (service_good(s
)) {
1614 /* If there are any queued up sd_notify()
1615 * notifications, process them now */
1616 if (s
->notify_state
== NOTIFY_RELOADING
)
1617 service_enter_reload_by_notify(s
);
1618 else if (s
->notify_state
== NOTIFY_STOPPING
)
1619 service_enter_stop_by_notify(s
);
1621 service_set_state(s
, SERVICE_RUNNING
);
1622 service_arm_timer(s
, usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
));
1625 } else if (s
->remain_after_exit
)
1626 service_set_state(s
, SERVICE_EXITED
);
1628 service_enter_stop(s
, SERVICE_SUCCESS
);
1631 static void service_enter_start_post(Service
*s
) {
1635 service_unwatch_control_pid(s
);
1636 service_reset_watchdog(s
);
1638 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1639 if (s
->control_command
) {
1640 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1642 r
= service_spawn(s
,
1644 s
->timeout_start_usec
,
1646 !s
->permissions_start_only
,
1647 !s
->root_directory_start_only
,
1654 service_set_state(s
, SERVICE_START_POST
);
1656 service_enter_running(s
, SERVICE_SUCCESS
);
1661 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1662 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1665 static void service_kill_control_processes(Service
*s
) {
1668 if (!UNIT(s
)->cgroup_path
)
1671 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1672 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1675 static void service_enter_start(Service
*s
) {
1683 service_unwatch_control_pid(s
);
1684 service_unwatch_main_pid(s
);
1686 /* We want to ensure that nobody leaks processes from
1687 * START_PRE here, so let's go on a killing spree, People
1688 * should not spawn long running processes from START_PRE. */
1689 service_kill_control_processes(s
);
1691 if (s
->type
== SERVICE_FORKING
) {
1692 s
->control_command_id
= SERVICE_EXEC_START
;
1693 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1695 s
->main_command
= NULL
;
1697 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1698 s
->control_command
= NULL
;
1700 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1704 assert(s
->type
== SERVICE_ONESHOT
);
1705 service_enter_start_post(s
);
1709 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
1710 /* For simple + idle this is the main process. We don't apply any timeout here, but
1711 * service_enter_running() will later apply the .runtime_max_usec timeout. */
1712 timeout
= USEC_INFINITY
;
1714 timeout
= s
->timeout_start_usec
;
1716 r
= service_spawn(s
,
1728 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
1729 /* For simple services we immediately start
1730 * the START_POST binaries. */
1732 service_set_main_pid(s
, pid
);
1733 service_enter_start_post(s
);
1735 } else if (s
->type
== SERVICE_FORKING
) {
1737 /* For forking services we wait until the start
1738 * process exited. */
1740 s
->control_pid
= pid
;
1741 service_set_state(s
, SERVICE_START
);
1743 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
)) {
1745 /* For oneshot services we wait until the start
1746 * process exited, too, but it is our main process. */
1748 /* For D-Bus services we know the main pid right away,
1749 * but wait for the bus name to appear on the
1750 * bus. Notify services are similar. */
1752 service_set_main_pid(s
, pid
);
1753 service_set_state(s
, SERVICE_START
);
1755 assert_not_reached("Unknown service type");
1760 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1761 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1764 static void service_enter_start_pre(Service
*s
) {
1769 service_unwatch_control_pid(s
);
1771 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1772 if (s
->control_command
) {
1773 /* Before we start anything, let's clear up what might
1774 * be left from previous runs. */
1775 service_kill_control_processes(s
);
1777 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1779 r
= service_spawn(s
,
1781 s
->timeout_start_usec
,
1783 !s
->permissions_start_only
,
1784 !s
->root_directory_start_only
,
1791 service_set_state(s
, SERVICE_START_PRE
);
1793 service_enter_start(s
);
1798 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1799 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1802 static void service_enter_restart(Service
*s
) {
1803 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1808 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1809 /* Don't restart things if we are going down anyway */
1810 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1812 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1819 /* Any units that are bound to this service must also be
1820 * restarted. We use JOB_RESTART (instead of the more obvious
1821 * JOB_START) here so that those dependency jobs will be added
1823 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, &error
, NULL
);
1827 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1828 * it will be canceled as part of the service_stop() call that
1829 * is executed as part of JOB_RESTART. */
1831 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1835 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1836 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1839 static void service_enter_reload_by_notify(Service
*s
) {
1842 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_start_usec
));
1843 service_set_state(s
, SERVICE_RELOAD
);
1846 static void service_enter_reload(Service
*s
) {
1851 service_unwatch_control_pid(s
);
1852 s
->reload_result
= SERVICE_SUCCESS
;
1854 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1855 if (s
->control_command
) {
1856 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1858 r
= service_spawn(s
,
1860 s
->timeout_start_usec
,
1862 !s
->permissions_start_only
,
1863 !s
->root_directory_start_only
,
1870 service_set_state(s
, SERVICE_RELOAD
);
1872 service_enter_running(s
, SERVICE_SUCCESS
);
1877 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1878 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1879 service_enter_running(s
, SERVICE_SUCCESS
);
1882 static void service_run_next_control(Service
*s
) {
1887 assert(s
->control_command
);
1888 assert(s
->control_command
->command_next
);
1890 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1892 s
->control_command
= s
->control_command
->command_next
;
1893 service_unwatch_control_pid(s
);
1895 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1896 timeout
= s
->timeout_start_usec
;
1898 timeout
= s
->timeout_stop_usec
;
1900 r
= service_spawn(s
,
1904 !s
->permissions_start_only
,
1905 !s
->root_directory_start_only
,
1906 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1907 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1916 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1918 if (s
->state
== SERVICE_START_PRE
)
1919 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1920 else if (s
->state
== SERVICE_STOP
)
1921 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1922 else if (s
->state
== SERVICE_STOP_POST
)
1923 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1924 else if (s
->state
== SERVICE_RELOAD
) {
1925 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1926 service_enter_running(s
, SERVICE_SUCCESS
);
1928 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1931 static void service_run_next_main(Service
*s
) {
1936 assert(s
->main_command
);
1937 assert(s
->main_command
->command_next
);
1938 assert(s
->type
== SERVICE_ONESHOT
);
1940 s
->main_command
= s
->main_command
->command_next
;
1941 service_unwatch_main_pid(s
);
1943 r
= service_spawn(s
,
1945 s
->timeout_start_usec
,
1955 service_set_main_pid(s
, pid
);
1960 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1961 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1964 static int service_start(Unit
*u
) {
1965 Service
*s
= SERVICE(u
);
1969 /* We cannot fulfill this request right now, try again later
1971 if (IN_SET(s
->state
,
1972 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1973 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1976 /* Already on it! */
1977 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1980 /* A service that will be restarted must be stopped first to
1981 * trigger BindsTo and/or OnFailure dependencies. If a user
1982 * does not want to wait for the holdoff time to elapse, the
1983 * service should be manually restarted, not started. We
1984 * simply return EAGAIN here, so that any start jobs stay
1985 * queued, and assume that the auto restart timer will
1986 * eventually trigger the restart. */
1987 if (s
->state
== SERVICE_AUTO_RESTART
)
1990 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1992 s
->result
= SERVICE_SUCCESS
;
1993 s
->reload_result
= SERVICE_SUCCESS
;
1994 s
->main_pid_known
= false;
1995 s
->main_pid_alien
= false;
1996 s
->forbid_restart
= false;
1997 s
->reset_cpu_usage
= true;
1999 s
->status_text
= mfree(s
->status_text
);
2000 s
->status_errno
= 0;
2002 s
->notify_state
= NOTIFY_UNKNOWN
;
2004 service_enter_start_pre(s
);
2008 static int service_stop(Unit
*u
) {
2009 Service
*s
= SERVICE(u
);
2013 /* Don't create restart jobs from manual stops. */
2014 s
->forbid_restart
= true;
2017 if (IN_SET(s
->state
,
2018 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2019 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2022 /* A restart will be scheduled or is in progress. */
2023 if (s
->state
== SERVICE_AUTO_RESTART
) {
2024 service_set_state(s
, SERVICE_DEAD
);
2028 /* If there's already something running we go directly into
2030 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2031 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2035 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2037 service_enter_stop(s
, SERVICE_SUCCESS
);
2041 static int service_reload(Unit
*u
) {
2042 Service
*s
= SERVICE(u
);
2046 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2048 service_enter_reload(s
);
2052 _pure_
static bool service_can_reload(Unit
*u
) {
2053 Service
*s
= SERVICE(u
);
2057 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2060 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2061 Service
*s
= SERVICE(u
);
2069 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2070 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2071 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2073 if (s
->control_pid
> 0)
2074 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2076 if (s
->main_pid_known
&& s
->main_pid
> 0)
2077 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2079 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2080 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2081 unit_serialize_item(u
, f
, "bus-name-owner", s
->bus_name_owner
);
2083 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2087 /* FIXME: There's a minor uncleanliness here: if there are
2088 * multiple commands attached here, we will start from the
2089 * first one again */
2090 if (s
->control_command_id
>= 0)
2091 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2093 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2096 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2099 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2103 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2107 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2108 _cleanup_free_
char *c
= NULL
;
2111 copy
= fdset_put_dup(fds
, fs
->fd
);
2115 c
= cescape(fs
->fdname
);
2117 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2120 if (s
->main_exec_status
.pid
> 0) {
2121 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2122 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2123 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2125 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2126 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2127 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2131 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2133 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2138 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2139 Service
*s
= SERVICE(u
);
2147 if (streq(key
, "state")) {
2150 state
= service_state_from_string(value
);
2152 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2154 s
->deserialized_state
= state
;
2155 } else if (streq(key
, "result")) {
2158 f
= service_result_from_string(value
);
2160 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2161 else if (f
!= SERVICE_SUCCESS
)
2164 } else if (streq(key
, "reload-result")) {
2167 f
= service_result_from_string(value
);
2169 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2170 else if (f
!= SERVICE_SUCCESS
)
2171 s
->reload_result
= f
;
2173 } else if (streq(key
, "control-pid")) {
2176 if (parse_pid(value
, &pid
) < 0)
2177 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2179 s
->control_pid
= pid
;
2180 } else if (streq(key
, "main-pid")) {
2183 if (parse_pid(value
, &pid
) < 0)
2184 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2186 service_set_main_pid(s
, pid
);
2187 unit_watch_pid(UNIT(s
), pid
);
2189 } else if (streq(key
, "main-pid-known")) {
2192 b
= parse_boolean(value
);
2194 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2196 s
->main_pid_known
= b
;
2197 } else if (streq(key
, "bus-name-good")) {
2200 b
= parse_boolean(value
);
2202 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2204 s
->bus_name_good
= b
;
2205 } else if (streq(key
, "bus-name-owner")) {
2206 r
= free_and_strdup(&s
->bus_name_owner
, value
);
2208 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
2209 } else if (streq(key
, "status-text")) {
2212 r
= cunescape(value
, 0, &t
);
2214 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2216 free(s
->status_text
);
2220 } else if (streq(key
, "control-command")) {
2221 ServiceExecCommand id
;
2223 id
= service_exec_command_from_string(value
);
2225 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2227 s
->control_command_id
= id
;
2228 s
->control_command
= s
->exec_command
[id
];
2230 } else if (streq(key
, "socket-fd")) {
2233 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2234 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2236 asynchronous_close(s
->socket_fd
);
2237 s
->socket_fd
= fdset_remove(fds
, fd
);
2239 } else if (streq(key
, "fd-store-fd")) {
2244 pf
= strcspn(value
, WHITESPACE
);
2245 fdv
= strndupa(value
, pf
);
2247 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2248 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2250 _cleanup_free_
char *t
= NULL
;
2254 fdn
+= strspn(fdn
, WHITESPACE
);
2255 (void) cunescape(fdn
, 0, &t
);
2257 r
= service_add_fd_store(s
, fd
, t
);
2259 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2261 fdset_remove(fds
, fd
);
2264 } else if (streq(key
, "main-exec-status-pid")) {
2267 if (parse_pid(value
, &pid
) < 0)
2268 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2270 s
->main_exec_status
.pid
= pid
;
2271 } else if (streq(key
, "main-exec-status-code")) {
2274 if (safe_atoi(value
, &i
) < 0)
2275 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2277 s
->main_exec_status
.code
= i
;
2278 } else if (streq(key
, "main-exec-status-status")) {
2281 if (safe_atoi(value
, &i
) < 0)
2282 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2284 s
->main_exec_status
.status
= i
;
2285 } else if (streq(key
, "main-exec-status-start"))
2286 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2287 else if (streq(key
, "main-exec-status-exit"))
2288 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2289 else if (streq(key
, "watchdog-timestamp"))
2290 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2291 else if (streq(key
, "forbid-restart")) {
2294 b
= parse_boolean(value
);
2296 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2298 s
->forbid_restart
= b
;
2299 } else if (streq(key
, "stdin-fd")) {
2302 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2303 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2305 asynchronous_close(s
->stdin_fd
);
2306 s
->stdin_fd
= fdset_remove(fds
, fd
);
2307 s
->exec_context
.stdio_as_fds
= true;
2309 } else if (streq(key
, "stdout-fd")) {
2312 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2313 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2315 asynchronous_close(s
->stdout_fd
);
2316 s
->stdout_fd
= fdset_remove(fds
, fd
);
2317 s
->exec_context
.stdio_as_fds
= true;
2319 } else if (streq(key
, "stderr-fd")) {
2322 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2323 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2325 asynchronous_close(s
->stderr_fd
);
2326 s
->stderr_fd
= fdset_remove(fds
, fd
);
2327 s
->exec_context
.stdio_as_fds
= true;
2330 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2335 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2336 const UnitActiveState
*table
;
2340 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2342 return table
[SERVICE(u
)->state
];
2345 static const char *service_sub_state_to_string(Unit
*u
) {
2348 return service_state_to_string(SERVICE(u
)->state
);
2351 static bool service_check_gc(Unit
*u
) {
2352 Service
*s
= SERVICE(u
);
2356 /* Never clean up services that still have a process around,
2357 * even if the service is formally dead. */
2358 if (cgroup_good(s
) > 0 ||
2359 main_pid_good(s
) > 0 ||
2360 control_pid_good(s
) > 0)
2366 static int service_retry_pid_file(Service
*s
) {
2369 assert(s
->pid_file
);
2370 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2372 r
= service_load_pid_file(s
, false);
2376 service_unwatch_pid_file(s
);
2378 service_enter_running(s
, SERVICE_SUCCESS
);
2382 static int service_watch_pid_file(Service
*s
) {
2385 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2387 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2391 /* the pidfile might have appeared just before we set the watch */
2392 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2393 service_retry_pid_file(s
);
2397 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2398 service_unwatch_pid_file(s
);
2402 static int service_demand_pid_file(Service
*s
) {
2405 assert(s
->pid_file
);
2406 assert(!s
->pid_file_pathspec
);
2408 ps
= new0(PathSpec
, 1);
2413 ps
->path
= strdup(s
->pid_file
);
2419 path_kill_slashes(ps
->path
);
2421 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2422 * keep their PID file open all the time. */
2423 ps
->type
= PATH_MODIFIED
;
2424 ps
->inotify_fd
= -1;
2426 s
->pid_file_pathspec
= ps
;
2428 return service_watch_pid_file(s
);
2431 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2432 PathSpec
*p
= userdata
;
2437 s
= SERVICE(p
->unit
);
2441 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2442 assert(s
->pid_file_pathspec
);
2443 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2445 log_unit_debug(UNIT(s
), "inotify event");
2447 if (path_spec_fd_event(p
, events
) < 0)
2450 if (service_retry_pid_file(s
) == 0)
2453 if (service_watch_pid_file(s
) < 0)
2459 service_unwatch_pid_file(s
);
2460 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2464 static void service_notify_cgroup_empty_event(Unit
*u
) {
2465 Service
*s
= SERVICE(u
);
2469 log_unit_debug(u
, "cgroup is empty");
2473 /* Waiting for SIGCHLD is usually more interesting,
2474 * because it includes return codes/signals. Which is
2475 * why we ignore the cgroup events for most cases,
2476 * except when we don't know pid which to expect the
2480 case SERVICE_START_POST
:
2481 /* If we were hoping for the daemon to write its PID file,
2482 * we can give up now. */
2483 if (s
->pid_file_pathspec
) {
2484 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2486 service_unwatch_pid_file(s
);
2487 if (s
->state
== SERVICE_START
)
2488 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2490 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2494 case SERVICE_RUNNING
:
2495 /* service_enter_running() will figure out what to do */
2496 service_enter_running(s
, SERVICE_SUCCESS
);
2499 case SERVICE_STOP_SIGABRT
:
2500 case SERVICE_STOP_SIGTERM
:
2501 case SERVICE_STOP_SIGKILL
:
2503 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2504 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2508 case SERVICE_STOP_POST
:
2509 case SERVICE_FINAL_SIGTERM
:
2510 case SERVICE_FINAL_SIGKILL
:
2511 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2512 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2521 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2522 Service
*s
= SERVICE(u
);
2528 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2529 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2530 f
= SERVICE_SUCCESS
;
2531 else if (code
== CLD_EXITED
)
2532 f
= SERVICE_FAILURE_EXIT_CODE
;
2533 else if (code
== CLD_KILLED
)
2534 f
= SERVICE_FAILURE_SIGNAL
;
2535 else if (code
== CLD_DUMPED
)
2536 f
= SERVICE_FAILURE_CORE_DUMP
;
2538 assert_not_reached("Unknown code");
2540 if (s
->main_pid
== pid
) {
2541 /* Forking services may occasionally move to a new PID.
2542 * As long as they update the PID file before exiting the old
2543 * PID, they're fine. */
2544 if (service_load_pid_file(s
, false) == 0)
2548 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2550 if (s
->main_command
) {
2551 /* If this is not a forking service than the
2552 * main process got started and hence we copy
2553 * the exit status so that it is recorded both
2554 * as main and as control process exit
2557 s
->main_command
->exec_status
= s
->main_exec_status
;
2559 if (s
->main_command
->ignore
)
2560 f
= SERVICE_SUCCESS
;
2561 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2563 /* If this is a forked process, then we should
2564 * ignore the return value if this was
2565 * configured for the starter process */
2567 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2568 f
= SERVICE_SUCCESS
;
2571 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2573 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2574 sigchld_code_to_string(code
), status
,
2575 strna(code
== CLD_EXITED
2576 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2577 : signal_to_string(status
))),
2578 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2579 "EXIT_STATUS=%i", status
,
2582 if (f
!= SERVICE_SUCCESS
)
2585 if (s
->main_command
&&
2586 s
->main_command
->command_next
&&
2587 f
== SERVICE_SUCCESS
) {
2589 /* There is another command to *
2590 * execute, so let's do that. */
2592 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2593 service_run_next_main(s
);
2597 /* The service exited, so the service is officially
2599 s
->main_command
= NULL
;
2603 case SERVICE_START_POST
:
2604 case SERVICE_RELOAD
:
2606 /* Need to wait until the operation is
2611 if (s
->type
== SERVICE_ONESHOT
) {
2612 /* This was our main goal, so let's go on */
2613 if (f
== SERVICE_SUCCESS
)
2614 service_enter_start_post(s
);
2616 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2622 case SERVICE_RUNNING
:
2623 service_enter_running(s
, f
);
2626 case SERVICE_STOP_SIGABRT
:
2627 case SERVICE_STOP_SIGTERM
:
2628 case SERVICE_STOP_SIGKILL
:
2630 if (!control_pid_good(s
))
2631 service_enter_stop_post(s
, f
);
2633 /* If there is still a control process, wait for that first */
2636 case SERVICE_STOP_POST
:
2637 case SERVICE_FINAL_SIGTERM
:
2638 case SERVICE_FINAL_SIGKILL
:
2640 if (!control_pid_good(s
))
2641 service_enter_dead(s
, f
, true);
2645 assert_not_reached("Uh, main process died at wrong time.");
2649 } else if (s
->control_pid
== pid
) {
2652 if (s
->control_command
) {
2653 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2655 if (s
->control_command
->ignore
)
2656 f
= SERVICE_SUCCESS
;
2659 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2660 "Control process exited, code=%s status=%i",
2661 sigchld_code_to_string(code
), status
);
2663 if (f
!= SERVICE_SUCCESS
)
2666 /* Immediately get rid of the cgroup, so that the
2667 * kernel doesn't delay the cgroup empty messages for
2668 * the service cgroup any longer than necessary */
2669 service_kill_control_processes(s
);
2671 if (s
->control_command
&&
2672 s
->control_command
->command_next
&&
2673 f
== SERVICE_SUCCESS
) {
2675 /* There is another command to *
2676 * execute, so let's do that. */
2678 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2679 service_run_next_control(s
);
2682 /* No further commands for this step, so let's
2683 * figure out what to do next */
2685 s
->control_command
= NULL
;
2686 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2688 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2692 case SERVICE_START_PRE
:
2693 if (f
== SERVICE_SUCCESS
)
2694 service_enter_start(s
);
2696 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2700 if (s
->type
!= SERVICE_FORKING
)
2701 /* Maybe spurious event due to a reload that changed the type? */
2704 if (f
!= SERVICE_SUCCESS
) {
2705 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2710 bool has_start_post
;
2713 /* Let's try to load the pid file here if we can.
2714 * The PID file might actually be created by a START_POST
2715 * script. In that case don't worry if the loading fails. */
2717 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2718 r
= service_load_pid_file(s
, !has_start_post
);
2719 if (!has_start_post
&& r
< 0) {
2720 r
= service_demand_pid_file(s
);
2721 if (r
< 0 || !cgroup_good(s
))
2722 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2726 service_search_main_pid(s
);
2728 service_enter_start_post(s
);
2731 case SERVICE_START_POST
:
2732 if (f
!= SERVICE_SUCCESS
) {
2733 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2740 r
= service_load_pid_file(s
, true);
2742 r
= service_demand_pid_file(s
);
2743 if (r
< 0 || !cgroup_good(s
))
2744 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2748 service_search_main_pid(s
);
2750 service_enter_running(s
, SERVICE_SUCCESS
);
2753 case SERVICE_RELOAD
:
2754 if (f
== SERVICE_SUCCESS
)
2755 if (service_load_pid_file(s
, true) < 0)
2756 service_search_main_pid(s
);
2758 s
->reload_result
= f
;
2759 service_enter_running(s
, SERVICE_SUCCESS
);
2763 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2766 case SERVICE_STOP_SIGABRT
:
2767 case SERVICE_STOP_SIGTERM
:
2768 case SERVICE_STOP_SIGKILL
:
2769 if (main_pid_good(s
) <= 0)
2770 service_enter_stop_post(s
, f
);
2772 /* If there is still a service
2773 * process around, wait until
2774 * that one quit, too */
2777 case SERVICE_STOP_POST
:
2778 case SERVICE_FINAL_SIGTERM
:
2779 case SERVICE_FINAL_SIGKILL
:
2780 if (main_pid_good(s
) <= 0)
2781 service_enter_dead(s
, f
, true);
2785 assert_not_reached("Uh, control process died at wrong time.");
2790 /* Notify clients about changed exit status */
2791 unit_add_to_dbus_queue(u
);
2793 /* We got one SIGCHLD for the service, let's watch all
2794 * processes that are now running of the service, and watch
2795 * that. Among the PIDs we then watch will be children
2796 * reassigned to us, which hopefully allows us to identify
2797 * when all children are gone */
2798 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2799 unit_watch_all_pids(u
);
2801 /* If the PID set is empty now, then let's finish this off */
2802 if (set_isempty(u
->pids
))
2803 service_notify_cgroup_empty_event(u
);
2806 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2807 Service
*s
= SERVICE(userdata
);
2810 assert(source
== s
->timer_event_source
);
2814 case SERVICE_START_PRE
:
2816 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2817 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2820 case SERVICE_START_POST
:
2821 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2822 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2825 case SERVICE_RUNNING
:
2826 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
2827 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2830 case SERVICE_RELOAD
:
2831 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
2832 service_kill_control_processes(s
);
2833 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2834 service_enter_running(s
, SERVICE_SUCCESS
);
2838 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2839 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2842 case SERVICE_STOP_SIGABRT
:
2843 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2844 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2847 case SERVICE_STOP_SIGTERM
:
2848 if (s
->kill_context
.send_sigkill
) {
2849 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2850 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2852 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2853 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2858 case SERVICE_STOP_SIGKILL
:
2859 /* Uh, we sent a SIGKILL and it is still not gone?
2860 * Must be something we cannot kill, so let's just be
2861 * weirded out and continue */
2863 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2864 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2867 case SERVICE_STOP_POST
:
2868 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2869 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2872 case SERVICE_FINAL_SIGTERM
:
2873 if (s
->kill_context
.send_sigkill
) {
2874 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2875 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2877 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2878 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2883 case SERVICE_FINAL_SIGKILL
:
2884 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2885 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2888 case SERVICE_AUTO_RESTART
:
2889 log_unit_info(UNIT(s
),
2890 s
->restart_usec
> 0 ?
2891 "Service hold-off time over, scheduling restart." :
2892 "Service has no hold-off time, scheduling restart.");
2893 service_enter_restart(s
);
2897 assert_not_reached("Timeout at wrong time.");
2903 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2904 Service
*s
= SERVICE(userdata
);
2905 char t
[FORMAT_TIMESPAN_MAX
];
2908 assert(source
== s
->watchdog_event_source
);
2910 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2911 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2913 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2918 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2919 Service
*s
= SERVICE(u
);
2920 _cleanup_free_
char *cc
= NULL
;
2921 bool notify_dbus
= false;
2926 cc
= strv_join(tags
, ", ");
2928 if (s
->notify_access
== NOTIFY_NONE
) {
2929 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2931 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2932 if (s
->main_pid
!= 0)
2933 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2935 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2938 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2940 /* Interpret MAINPID= */
2941 e
= strv_find_startswith(tags
, "MAINPID=");
2942 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2943 if (parse_pid(e
, &pid
) < 0)
2944 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2946 service_set_main_pid(s
, pid
);
2947 unit_watch_pid(UNIT(s
), pid
);
2952 /* Interpret RELOADING= */
2953 if (strv_find(tags
, "RELOADING=1")) {
2955 s
->notify_state
= NOTIFY_RELOADING
;
2957 if (s
->state
== SERVICE_RUNNING
)
2958 service_enter_reload_by_notify(s
);
2963 /* Interpret READY= */
2964 if (strv_find(tags
, "READY=1")) {
2966 s
->notify_state
= NOTIFY_READY
;
2968 /* Type=notify services inform us about completed
2969 * initialization with READY=1 */
2970 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2971 service_enter_start_post(s
);
2973 /* Sending READY=1 while we are reloading informs us
2974 * that the reloading is complete */
2975 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2976 service_enter_running(s
, SERVICE_SUCCESS
);
2981 /* Interpret STOPPING= */
2982 if (strv_find(tags
, "STOPPING=1")) {
2984 s
->notify_state
= NOTIFY_STOPPING
;
2986 if (s
->state
== SERVICE_RUNNING
)
2987 service_enter_stop_by_notify(s
);
2992 /* Interpret STATUS= */
2993 e
= strv_find_startswith(tags
, "STATUS=");
2995 _cleanup_free_
char *t
= NULL
;
2998 if (!utf8_is_valid(e
))
2999 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3007 if (!streq_ptr(s
->status_text
, t
)) {
3009 free(s
->status_text
);
3017 /* Interpret ERRNO= */
3018 e
= strv_find_startswith(tags
, "ERRNO=");
3022 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3023 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3025 if (s
->status_errno
!= status_errno
) {
3026 s
->status_errno
= status_errno
;
3032 /* Interpret WATCHDOG= */
3033 if (strv_find(tags
, "WATCHDOG=1"))
3034 service_reset_watchdog(s
);
3036 if (strv_find(tags
, "FDSTORE=1")) {
3039 name
= strv_find_startswith(tags
, "FDNAME=");
3040 if (name
&& !fdname_is_valid(name
)) {
3041 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3045 service_add_fd_store_set(s
, fds
, name
);
3048 /* Notify clients about changed status or main pid */
3050 unit_add_to_dbus_queue(u
);
3053 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
3054 Service
*s
= SERVICE(u
);
3058 if (!s
->timer_event_source
)
3061 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3064 if (t
== USEC_INFINITY
)
3071 static void service_bus_name_owner_change(
3074 const char *old_owner
,
3075 const char *new_owner
) {
3077 Service
*s
= SERVICE(u
);
3083 assert(streq(s
->bus_name
, name
));
3084 assert(old_owner
|| new_owner
);
3086 if (old_owner
&& new_owner
)
3087 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3089 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3091 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3093 s
->bus_name_good
= !!new_owner
;
3095 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3096 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
3098 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
3102 if (s
->type
== SERVICE_DBUS
) {
3104 /* service_enter_running() will figure out what to
3106 if (s
->state
== SERVICE_RUNNING
)
3107 service_enter_running(s
, SERVICE_SUCCESS
);
3108 else if (s
->state
== SERVICE_START
&& new_owner
)
3109 service_enter_start_post(s
);
3111 } else if (new_owner
&&
3113 (s
->state
== SERVICE_START
||
3114 s
->state
== SERVICE_START_POST
||
3115 s
->state
== SERVICE_RUNNING
||
3116 s
->state
== SERVICE_RELOAD
)) {
3118 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3121 /* Try to acquire PID from bus service */
3123 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3125 r
= sd_bus_creds_get_pid(creds
, &pid
);
3127 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3129 service_set_main_pid(s
, pid
);
3130 unit_watch_pid(UNIT(s
), pid
);
3135 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3136 _cleanup_free_
char *peer
= NULL
;
3142 /* This is called by the socket code when instantiating a new
3143 * service for a stream socket and the socket needs to be
3146 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3149 if (s
->socket_fd
>= 0)
3152 if (s
->state
!= SERVICE_DEAD
)
3155 if (getpeername_pretty(fd
, true, &peer
) >= 0) {
3157 if (UNIT(s
)->description
) {
3158 _cleanup_free_
char *a
;
3160 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3164 r
= unit_set_description(UNIT(s
), a
);
3166 r
= unit_set_description(UNIT(s
), peer
);
3173 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3175 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3177 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3180 static void service_reset_failed(Unit
*u
) {
3181 Service
*s
= SERVICE(u
);
3185 if (s
->state
== SERVICE_FAILED
)
3186 service_set_state(s
, SERVICE_DEAD
);
3188 s
->result
= SERVICE_SUCCESS
;
3189 s
->reload_result
= SERVICE_SUCCESS
;
3192 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3193 Service
*s
= SERVICE(u
);
3195 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3198 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3199 [SERVICE_RESTART_NO
] = "no",
3200 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3201 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3202 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3203 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3204 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3205 [SERVICE_RESTART_ALWAYS
] = "always",
3208 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3210 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3211 [SERVICE_SIMPLE
] = "simple",
3212 [SERVICE_FORKING
] = "forking",
3213 [SERVICE_ONESHOT
] = "oneshot",
3214 [SERVICE_DBUS
] = "dbus",
3215 [SERVICE_NOTIFY
] = "notify",
3216 [SERVICE_IDLE
] = "idle"
3219 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3221 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3222 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3223 [SERVICE_EXEC_START
] = "ExecStart",
3224 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3225 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3226 [SERVICE_EXEC_STOP
] = "ExecStop",
3227 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3230 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3232 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3233 [NOTIFY_NONE
] = "none",
3234 [NOTIFY_MAIN
] = "main",
3235 [NOTIFY_ALL
] = "all"
3238 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3240 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3241 [NOTIFY_UNKNOWN
] = "unknown",
3242 [NOTIFY_READY
] = "ready",
3243 [NOTIFY_RELOADING
] = "reloading",
3244 [NOTIFY_STOPPING
] = "stopping",
3247 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3249 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3250 [SERVICE_SUCCESS
] = "success",
3251 [SERVICE_FAILURE_RESOURCES
] = "resources",
3252 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3253 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3254 [SERVICE_FAILURE_SIGNAL
] = "signal",
3255 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3256 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3259 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3261 const UnitVTable service_vtable
= {
3262 .object_size
= sizeof(Service
),
3263 .exec_context_offset
= offsetof(Service
, exec_context
),
3264 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3265 .kill_context_offset
= offsetof(Service
, kill_context
),
3266 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3272 .private_section
= "Service",
3274 .init
= service_init
,
3275 .done
= service_done
,
3276 .load
= service_load
,
3277 .release_resources
= service_release_resources
,
3279 .coldplug
= service_coldplug
,
3281 .dump
= service_dump
,
3283 .start
= service_start
,
3284 .stop
= service_stop
,
3285 .reload
= service_reload
,
3287 .can_reload
= service_can_reload
,
3289 .kill
= service_kill
,
3291 .serialize
= service_serialize
,
3292 .deserialize_item
= service_deserialize_item
,
3294 .active_state
= service_active_state
,
3295 .sub_state_to_string
= service_sub_state_to_string
,
3297 .check_gc
= service_check_gc
,
3299 .sigchld_event
= service_sigchld_event
,
3301 .reset_failed
= service_reset_failed
,
3303 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3304 .notify_message
= service_notify_message
,
3306 .bus_name_owner_change
= service_bus_name_owner_change
,
3308 .bus_vtable
= bus_service_vtable
,
3309 .bus_set_property
= bus_service_set_property
,
3310 .bus_commit_properties
= bus_service_commit_properties
,
3312 .get_timeout
= service_get_timeout
,
3313 .can_transient
= true,
3315 .status_message_formats
= {
3316 .starting_stopping
= {
3317 [0] = "Starting %s...",
3318 [1] = "Stopping %s...",
3320 .finished_start_job
= {
3321 [JOB_DONE
] = "Started %s.",
3322 [JOB_FAILED
] = "Failed to start %s.",
3324 .finished_stop_job
= {
3325 [JOB_DONE
] = "Stopped %s.",
3326 [JOB_FAILED
] = "Stopped (with error) %s.",