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 void service_close_socket_fd(Service
*s
) {
186 /* Undo the effect of service_set_socket_fd(). */
188 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
190 if (UNIT_ISSET(s
->accept_socket
)) {
191 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
192 unit_ref_unset(&s
->accept_socket
);
196 static void service_stop_watchdog(Service
*s
) {
199 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
200 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
203 static usec_t
service_get_watchdog_usec(Service
*s
) {
206 if (s
->watchdog_override_enable
)
207 return s
->watchdog_override_usec
;
209 return s
->watchdog_usec
;
212 static void service_start_watchdog(Service
*s
) {
214 usec_t watchdog_usec
;
218 watchdog_usec
= service_get_watchdog_usec(s
);
219 if (watchdog_usec
== 0 || watchdog_usec
== USEC_INFINITY
)
222 if (s
->watchdog_event_source
) {
223 r
= sd_event_source_set_time(s
->watchdog_event_source
, usec_add(s
->watchdog_timestamp
.monotonic
, watchdog_usec
));
225 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
229 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
231 r
= sd_event_add_time(
232 UNIT(s
)->manager
->event
,
233 &s
->watchdog_event_source
,
235 usec_add(s
->watchdog_timestamp
.monotonic
, watchdog_usec
), 0,
236 service_dispatch_watchdog
, s
);
238 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
242 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
244 /* Let's process everything else which might be a sign
245 * of living before we consider a service died. */
246 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
250 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
253 static void service_reset_watchdog(Service
*s
) {
256 dual_timestamp_get(&s
->watchdog_timestamp
);
257 service_start_watchdog(s
);
260 static void service_reset_watchdog_timeout(Service
*s
, usec_t watchdog_override_usec
) {
263 s
->watchdog_override_enable
= true;
264 s
->watchdog_override_usec
= watchdog_override_usec
;
265 service_reset_watchdog(s
);
267 log_unit_debug(UNIT(s
), "watchdog_usec="USEC_FMT
, s
->watchdog_usec
);
268 log_unit_debug(UNIT(s
), "watchdog_override_usec="USEC_FMT
, s
->watchdog_override_usec
);
271 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
277 assert(fs
->service
->n_fd_store
> 0);
278 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
279 fs
->service
->n_fd_store
--;
282 if (fs
->event_source
) {
283 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
284 sd_event_source_unref(fs
->event_source
);
292 static void service_release_resources(Unit
*u
) {
293 Service
*s
= SERVICE(u
);
297 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
300 log_unit_debug(u
, "Releasing all resources.");
302 s
->stdin_fd
= safe_close(s
->stdin_fd
);
303 s
->stdout_fd
= safe_close(s
->stdout_fd
);
304 s
->stderr_fd
= safe_close(s
->stderr_fd
);
307 service_fd_store_unlink(s
->fd_store
);
309 assert(s
->n_fd_store
== 0);
312 static void service_done(Unit
*u
) {
313 Service
*s
= SERVICE(u
);
317 s
->pid_file
= mfree(s
->pid_file
);
318 s
->status_text
= mfree(s
->status_text
);
320 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
321 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
322 s
->control_command
= NULL
;
323 s
->main_command
= NULL
;
325 dynamic_creds_unref(&s
->dynamic_creds
);
327 exit_status_set_free(&s
->restart_prevent_status
);
328 exit_status_set_free(&s
->restart_force_status
);
329 exit_status_set_free(&s
->success_status
);
331 /* This will leak a process, but at least no memory or any of
333 service_unwatch_main_pid(s
);
334 service_unwatch_control_pid(s
);
335 service_unwatch_pid_file(s
);
338 unit_unwatch_bus_name(u
, s
->bus_name
);
339 s
->bus_name
= mfree(s
->bus_name
);
342 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
344 service_close_socket_fd(s
);
346 unit_ref_unset(&s
->accept_socket
);
348 service_stop_watchdog(s
);
350 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
352 service_release_resources(u
);
355 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
356 ServiceFDStore
*fs
= userdata
;
361 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
362 service_fd_store_unlink(fs
);
366 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
373 if (s
->n_fd_store
>= s
->n_fd_store_max
)
376 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
377 r
= same_fd(fs
->fd
, fd
);
381 /* Already included */
387 fs
= new0(ServiceFDStore
, 1);
393 fs
->fdname
= strdup(name
?: "stored");
399 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
406 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
408 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
414 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
419 if (fdset_size(fds
) <= 0)
422 while (s
->n_fd_store
< s
->n_fd_store_max
) {
423 _cleanup_close_
int fd
= -1;
425 fd
= fdset_steal_first(fds
);
429 r
= service_add_fd_store(s
, fd
, name
);
431 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
433 log_unit_debug(UNIT(s
), "Added fd to fd store.");
438 if (fdset_size(fds
) > 0)
439 log_unit_warning(UNIT(s
), "Tried to store more fds than FileDescriptorStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
444 static int service_arm_timer(Service
*s
, usec_t usec
) {
449 if (s
->timer_event_source
) {
450 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
454 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
457 if (usec
== USEC_INFINITY
)
460 r
= sd_event_add_time(
461 UNIT(s
)->manager
->event
,
462 &s
->timer_event_source
,
465 service_dispatch_timer
, s
);
469 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
474 static int service_verify(Service
*s
) {
477 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
480 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
481 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
485 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
486 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
490 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
491 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
495 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
496 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
500 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
501 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
505 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
506 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
510 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
511 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
515 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
516 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
518 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
519 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
523 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
524 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
526 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
527 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
529 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
530 log_unit_warning(UNIT(s
), "MaxRuntimeSec= has no effect in combination with Type=oneshot. Ignoring.");
535 static int service_add_default_dependencies(Service
*s
) {
540 if (!UNIT(s
)->default_dependencies
)
543 /* Add a number of automatic dependencies useful for the
544 * majority of services. */
546 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
547 /* First, pull in the really early boot stuff, and
548 * require it, so that we fail if we can't acquire
551 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true);
556 /* In the --user instance there's no sysinit.target,
557 * in that case require basic.target instead. */
559 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
564 /* Second, if the rest of the base system is in the same
565 * transaction, order us after it, but do not pull it in or
566 * even require it. */
567 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true);
571 /* Third, add us in for normal shutdown. */
572 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
575 static void service_fix_output(Service
*s
) {
578 /* If nothing has been explicitly configured, patch default
579 * output in. If input is socket/tty we avoid this however,
580 * since in that case we want output to default to the same
581 * place as we read input from. */
583 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
584 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
585 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
586 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
588 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
589 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
590 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
593 static int service_setup_bus_name(Service
*s
) {
601 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true);
603 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
605 /* Regardless if kdbus is used or not, we always want to be ordered against dbus.socket if both are in the transaction. */
606 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, NULL
, true);
608 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
610 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
612 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
614 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
619 static int service_add_extras(Service
*s
) {
624 if (s
->type
== _SERVICE_TYPE_INVALID
) {
625 /* Figure out a type automatically */
627 s
->type
= SERVICE_DBUS
;
628 else if (s
->exec_command
[SERVICE_EXEC_START
])
629 s
->type
= SERVICE_SIMPLE
;
631 s
->type
= SERVICE_ONESHOT
;
634 /* Oneshot services have disabled start timeout by default */
635 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
636 s
->timeout_start_usec
= USEC_INFINITY
;
638 service_fix_output(s
);
640 r
= unit_patch_contexts(UNIT(s
));
644 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
648 r
= unit_set_default_slice(UNIT(s
));
652 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
653 s
->notify_access
= NOTIFY_MAIN
;
655 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
656 s
->notify_access
= NOTIFY_MAIN
;
658 r
= service_add_default_dependencies(s
);
662 r
= service_setup_bus_name(s
);
669 static int service_load(Unit
*u
) {
670 Service
*s
= SERVICE(u
);
675 /* Load a .service file */
676 r
= unit_load_fragment(u
);
680 /* Still nothing found? Then let's give up */
681 if (u
->load_state
== UNIT_STUB
)
684 /* This is a new unit? Then let's add in some extras */
685 if (u
->load_state
== UNIT_LOADED
) {
687 /* We were able to load something, then let's add in
688 * the dropin directories. */
689 r
= unit_load_dropin(u
);
693 /* This is a new unit? Then let's add in some
695 r
= service_add_extras(s
);
700 return service_verify(s
);
703 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
704 ServiceExecCommand c
;
705 Service
*s
= SERVICE(u
);
710 prefix
= strempty(prefix
);
711 prefix2
= strjoina(prefix
, "\t");
714 "%sService State: %s\n"
716 "%sReload Result: %s\n"
717 "%sPermissionsStartOnly: %s\n"
718 "%sRootDirectoryStartOnly: %s\n"
719 "%sRemainAfterExit: %s\n"
720 "%sGuessMainPID: %s\n"
723 "%sNotifyAccess: %s\n"
724 "%sNotifyState: %s\n",
725 prefix
, service_state_to_string(s
->state
),
726 prefix
, service_result_to_string(s
->result
),
727 prefix
, service_result_to_string(s
->reload_result
),
728 prefix
, yes_no(s
->permissions_start_only
),
729 prefix
, yes_no(s
->root_directory_start_only
),
730 prefix
, yes_no(s
->remain_after_exit
),
731 prefix
, yes_no(s
->guess_main_pid
),
732 prefix
, service_type_to_string(s
->type
),
733 prefix
, service_restart_to_string(s
->restart
),
734 prefix
, notify_access_to_string(s
->notify_access
),
735 prefix
, notify_state_to_string(s
->notify_state
));
737 if (s
->control_pid
> 0)
739 "%sControl PID: "PID_FMT
"\n",
740 prefix
, s
->control_pid
);
744 "%sMain PID: "PID_FMT
"\n"
745 "%sMain PID Known: %s\n"
746 "%sMain PID Alien: %s\n",
748 prefix
, yes_no(s
->main_pid_known
),
749 prefix
, yes_no(s
->main_pid_alien
));
754 prefix
, s
->pid_file
);
759 "%sBus Name Good: %s\n",
761 prefix
, yes_no(s
->bus_name_good
));
763 kill_context_dump(&s
->kill_context
, f
, prefix
);
764 exec_context_dump(&s
->exec_context
, f
, prefix
);
766 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
768 if (!s
->exec_command
[c
])
771 fprintf(f
, "%s-> %s:\n",
772 prefix
, service_exec_command_to_string(c
));
774 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
778 fprintf(f
, "%sStatus Text: %s\n",
779 prefix
, s
->status_text
);
781 if (s
->n_fd_store_max
> 0)
783 "%sFile Descriptor Store Max: %u\n"
784 "%sFile Descriptor Store Current: %u\n",
785 prefix
, s
->n_fd_store_max
,
786 prefix
, s
->n_fd_store
);
789 static int service_load_pid_file(Service
*s
, bool may_warn
) {
790 _cleanup_free_
char *k
= NULL
;
799 r
= read_one_line_file(s
->pid_file
, &k
);
802 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
));
806 r
= parse_pid(k
, &pid
);
809 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
813 if (!pid_is_alive(pid
)) {
815 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
819 if (s
->main_pid_known
) {
820 if (pid
== s
->main_pid
)
823 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
825 service_unwatch_main_pid(s
);
826 s
->main_pid_known
= false;
828 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
830 r
= service_set_main_pid(s
, pid
);
834 r
= unit_watch_pid(UNIT(s
), pid
);
836 /* FIXME: we need to do something here */
837 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
844 static void service_search_main_pid(Service
*s
) {
850 /* If we know it anyway, don't ever fallback to unreliable
852 if (s
->main_pid_known
)
855 if (!s
->guess_main_pid
)
858 assert(s
->main_pid
<= 0);
860 if (unit_search_main_pid(UNIT(s
), &pid
) < 0)
863 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
864 if (service_set_main_pid(s
, pid
) < 0)
867 r
= unit_watch_pid(UNIT(s
), pid
);
869 /* FIXME: we need to do something here */
870 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
873 static void service_set_state(Service
*s
, ServiceState state
) {
874 ServiceState old_state
;
875 const UnitActiveState
*table
;
879 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
881 old_state
= s
->state
;
884 service_unwatch_pid_file(s
);
887 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
890 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
891 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
892 SERVICE_AUTO_RESTART
))
893 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
896 SERVICE_START
, SERVICE_START_POST
,
897 SERVICE_RUNNING
, SERVICE_RELOAD
,
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_main_pid(s
);
901 s
->main_command
= NULL
;
905 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
907 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
908 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
909 service_unwatch_control_pid(s
);
910 s
->control_command
= NULL
;
911 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
914 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
915 unit_unwatch_all_pids(UNIT(s
));
918 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
919 SERVICE_RUNNING
, SERVICE_RELOAD
,
920 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
921 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
922 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
))
923 service_close_socket_fd(s
);
925 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
926 service_stop_watchdog(s
);
928 /* For the inactive states unit_notify() will trim the cgroup,
929 * but for exit we have to do that ourselves... */
930 if (state
== SERVICE_EXITED
&& !MANAGER_IS_RELOADING(UNIT(s
)->manager
))
931 unit_prune_cgroup(UNIT(s
));
933 /* For remain_after_exit services, let's see if we can "release" the
934 * hold on the console, since unit_notify() only does that in case of
936 if (state
== SERVICE_EXITED
&&
937 s
->remain_after_exit
&&
938 UNIT(s
)->manager
->n_on_console
> 0) {
942 ec
= unit_get_exec_context(UNIT(s
));
943 if (ec
&& exec_context_may_touch_console(ec
)) {
944 Manager
*m
= UNIT(s
)->manager
;
947 if (m
->n_on_console
== 0)
948 /* unset no_console_output flag, since the console is free */
949 m
->no_console_output
= false;
953 if (old_state
!= state
)
954 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
956 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
959 static usec_t
service_coldplug_timeout(Service
*s
) {
962 switch (s
->deserialized_state
) {
964 case SERVICE_START_PRE
:
966 case SERVICE_START_POST
:
968 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
970 case SERVICE_RUNNING
:
971 return usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
);
974 case SERVICE_STOP_SIGABRT
:
975 case SERVICE_STOP_SIGTERM
:
976 case SERVICE_STOP_SIGKILL
:
977 case SERVICE_STOP_POST
:
978 case SERVICE_FINAL_SIGTERM
:
979 case SERVICE_FINAL_SIGKILL
:
980 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
982 case SERVICE_AUTO_RESTART
:
983 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, s
->restart_usec
);
986 return USEC_INFINITY
;
990 static int service_coldplug(Unit
*u
) {
991 Service
*s
= SERVICE(u
);
995 assert(s
->state
== SERVICE_DEAD
);
997 if (s
->deserialized_state
== s
->state
)
1000 r
= service_arm_timer(s
, service_coldplug_timeout(s
));
1004 if (s
->main_pid
> 0 &&
1005 pid_is_unwaited(s
->main_pid
) &&
1006 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
1007 IN_SET(s
->deserialized_state
,
1008 SERVICE_START
, SERVICE_START_POST
,
1009 SERVICE_RUNNING
, SERVICE_RELOAD
,
1010 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1011 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
1012 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
1017 if (s
->control_pid
> 0 &&
1018 pid_is_unwaited(s
->control_pid
) &&
1019 IN_SET(s
->deserialized_state
,
1020 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1022 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1023 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1024 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1029 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1030 unit_watch_all_pids(UNIT(s
));
1032 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1033 service_start_watchdog(s
);
1035 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1036 (void) unit_setup_dynamic_creds(u
);
1038 service_set_state(s
, s
->deserialized_state
);
1042 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
1043 _cleanup_strv_free_
char **rfd_names
= NULL
;
1044 _cleanup_free_
int *rfds
= NULL
;
1051 if (s
->socket_fd
>= 0) {
1053 /* Pass the per-connection socket */
1058 rfds
[0] = s
->socket_fd
;
1060 rfd_names
= strv_new("connection", NULL
);
1069 /* Pass all our configured sockets for singleton services */
1071 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1072 _cleanup_free_
int *cfds
= NULL
;
1076 if (u
->type
!= UNIT_SOCKET
)
1081 cn_fds
= socket_collect_fds(sock
, &cfds
);
1096 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1100 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1106 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1112 if (s
->n_fd_store
> 0) {
1117 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1123 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1129 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1130 rfds
[rn_fds
] = fs
->fd
;
1131 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1132 if (!rfd_names
[rn_fds
])
1138 rfd_names
[rn_fds
] = NULL
;
1142 *fd_names
= rfd_names
;
1150 static int service_spawn(
1155 bool apply_permissions
,
1157 bool apply_tty_stdin
,
1161 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1162 _cleanup_free_
int *fds
= NULL
;
1163 unsigned n_fds
= 0, n_env
= 0;
1167 ExecParameters exec_params
= {
1168 .apply_permissions
= apply_permissions
,
1169 .apply_chroot
= apply_chroot
,
1170 .apply_tty_stdin
= apply_tty_stdin
,
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
));
1192 r
= unit_setup_dynamic_creds(UNIT(s
));
1197 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1198 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1199 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1201 r
= service_collect_fds(s
, &fds
, &fd_names
);
1208 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), timeout
));
1212 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1216 our_env
= new0(char*, 6);
1220 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1221 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1224 if (s
->main_pid
> 0)
1225 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0)
1228 if (!MANAGER_IS_SYSTEM(UNIT(s
)->manager
))
1229 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0)
1232 if (s
->socket_fd
>= 0) {
1233 union sockaddr_union sa
;
1234 socklen_t salen
= sizeof(sa
);
1236 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1240 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1241 _cleanup_free_
char *addr
= NULL
;
1245 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1249 t
= strappend("REMOTE_ADDR=", addr
);
1252 our_env
[n_env
++] = t
;
1254 port
= sockaddr_port(&sa
.sa
);
1258 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1260 our_env
[n_env
++] = t
;
1264 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1268 if (is_control
&& UNIT(s
)->cgroup_path
) {
1269 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1270 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1272 path
= UNIT(s
)->cgroup_path
;
1274 exec_params
.argv
= argv
;
1275 exec_params
.fds
= fds
;
1276 exec_params
.fd_names
= fd_names
;
1277 exec_params
.n_fds
= n_fds
;
1278 exec_params
.environment
= final_env
;
1279 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1280 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1281 exec_params
.cgroup_path
= path
;
1282 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1283 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1284 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1285 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1286 if (s
->type
== SERVICE_IDLE
)
1287 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1288 exec_params
.stdin_fd
= s
->stdin_fd
;
1289 exec_params
.stdout_fd
= s
->stdout_fd
;
1290 exec_params
.stderr_fd
= s
->stderr_fd
;
1292 r
= exec_spawn(UNIT(s
),
1302 r
= unit_watch_pid(UNIT(s
), pid
);
1304 /* FIXME: we need to do something here */
1312 static int main_pid_good(Service
*s
) {
1315 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1318 /* If we know the pid file, then let's just check if it is
1320 if (s
->main_pid_known
) {
1322 /* If it's an alien child let's check if it is still
1324 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1325 return pid_is_alive(s
->main_pid
);
1327 /* .. otherwise assume we'll get a SIGCHLD for it,
1328 * which we really should wait for to collect exit
1329 * status and code */
1330 return s
->main_pid
> 0;
1333 /* We don't know the pid */
1337 _pure_
static int control_pid_good(Service
*s
) {
1340 return s
->control_pid
> 0;
1343 static int cgroup_good(Service
*s
) {
1348 if (!UNIT(s
)->cgroup_path
)
1351 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1358 static bool service_shall_restart(Service
*s
) {
1361 /* Don't restart after manual stops */
1362 if (s
->forbid_restart
)
1365 /* Never restart if this is configured as special exception */
1366 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1369 /* Restart if the exit code/status are configured as restart triggers */
1370 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1373 switch (s
->restart
) {
1375 case SERVICE_RESTART_NO
:
1378 case SERVICE_RESTART_ALWAYS
:
1381 case SERVICE_RESTART_ON_SUCCESS
:
1382 return s
->result
== SERVICE_SUCCESS
;
1384 case SERVICE_RESTART_ON_FAILURE
:
1385 return s
->result
!= SERVICE_SUCCESS
;
1387 case SERVICE_RESTART_ON_ABNORMAL
:
1388 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1390 case SERVICE_RESTART_ON_WATCHDOG
:
1391 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1393 case SERVICE_RESTART_ON_ABORT
:
1394 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1397 assert_not_reached("unknown restart setting");
1401 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1405 if (f
!= SERVICE_SUCCESS
)
1408 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1410 if (s
->result
!= SERVICE_SUCCESS
) {
1411 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1412 failure_action(UNIT(s
)->manager
, s
->failure_action
, UNIT(s
)->reboot_arg
);
1415 if (allow_restart
&& service_shall_restart(s
)) {
1417 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1421 service_set_state(s
, SERVICE_AUTO_RESTART
);
1424 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1425 s
->forbid_restart
= false;
1427 /* We want fresh tmpdirs in case service is started again immediately */
1428 exec_runtime_destroy(s
->exec_runtime
);
1429 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1431 /* Also, remove the runtime directory */
1432 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1434 /* Release the user, and destroy it if we are the only remaining owner */
1435 dynamic_creds_destroy(&s
->dynamic_creds
);
1437 /* Try to delete the pid file. At this point it will be
1438 * out-of-date, and some software might be confused by it, so
1439 * let's remove it. */
1441 (void) unlink(s
->pid_file
);
1446 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1447 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1450 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1454 if (f
!= SERVICE_SUCCESS
)
1457 service_unwatch_control_pid(s
);
1458 unit_watch_all_pids(UNIT(s
));
1460 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1461 if (s
->control_command
) {
1462 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1464 r
= service_spawn(s
,
1466 s
->timeout_stop_usec
,
1468 !s
->permissions_start_only
,
1469 !s
->root_directory_start_only
,
1476 service_set_state(s
, SERVICE_STOP_POST
);
1478 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1483 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1484 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1487 static int state_to_kill_operation(ServiceState state
) {
1490 case SERVICE_STOP_SIGABRT
:
1493 case SERVICE_STOP_SIGTERM
:
1494 case SERVICE_FINAL_SIGTERM
:
1495 return KILL_TERMINATE
;
1497 case SERVICE_STOP_SIGKILL
:
1498 case SERVICE_FINAL_SIGKILL
:
1502 return _KILL_OPERATION_INVALID
;
1506 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1511 if (f
!= SERVICE_SUCCESS
)
1514 unit_watch_all_pids(UNIT(s
));
1516 r
= unit_kill_context(
1519 state_to_kill_operation(state
),
1528 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1532 service_set_state(s
, state
);
1533 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1534 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1535 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1536 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1537 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1538 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1540 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1545 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1547 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1548 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1550 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1553 static void service_enter_stop_by_notify(Service
*s
) {
1556 unit_watch_all_pids(UNIT(s
));
1558 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1560 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1561 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1564 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1569 if (f
!= SERVICE_SUCCESS
)
1572 service_unwatch_control_pid(s
);
1573 unit_watch_all_pids(UNIT(s
));
1575 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1576 if (s
->control_command
) {
1577 s
->control_command_id
= SERVICE_EXEC_STOP
;
1579 r
= service_spawn(s
,
1581 s
->timeout_stop_usec
,
1583 !s
->permissions_start_only
,
1584 !s
->root_directory_start_only
,
1591 service_set_state(s
, SERVICE_STOP
);
1593 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1598 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1599 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1602 static bool service_good(Service
*s
) {
1606 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1609 main_pid_ok
= main_pid_good(s
);
1610 if (main_pid_ok
> 0) /* It's alive */
1612 if (main_pid_ok
== 0) /* It's dead */
1615 /* OK, we don't know anything about the main PID, maybe
1616 * because there is none. Let's check the control group
1619 return cgroup_good(s
) != 0;
1622 static void service_enter_running(Service
*s
, ServiceResult f
) {
1625 if (f
!= SERVICE_SUCCESS
)
1628 service_unwatch_control_pid(s
);
1630 if (service_good(s
)) {
1632 /* If there are any queued up sd_notify()
1633 * notifications, process them now */
1634 if (s
->notify_state
== NOTIFY_RELOADING
)
1635 service_enter_reload_by_notify(s
);
1636 else if (s
->notify_state
== NOTIFY_STOPPING
)
1637 service_enter_stop_by_notify(s
);
1639 service_set_state(s
, SERVICE_RUNNING
);
1640 service_arm_timer(s
, usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
));
1643 } else if (s
->remain_after_exit
)
1644 service_set_state(s
, SERVICE_EXITED
);
1646 service_enter_stop(s
, SERVICE_SUCCESS
);
1649 static void service_enter_start_post(Service
*s
) {
1653 service_unwatch_control_pid(s
);
1654 service_reset_watchdog(s
);
1656 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1657 if (s
->control_command
) {
1658 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1660 r
= service_spawn(s
,
1662 s
->timeout_start_usec
,
1664 !s
->permissions_start_only
,
1665 !s
->root_directory_start_only
,
1672 service_set_state(s
, SERVICE_START_POST
);
1674 service_enter_running(s
, SERVICE_SUCCESS
);
1679 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1680 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1683 static void service_kill_control_processes(Service
*s
) {
1686 if (!UNIT(s
)->cgroup_path
)
1689 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1690 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, CGROUP_SIGCONT
|CGROUP_IGNORE_SELF
|CGROUP_REMOVE
, NULL
, NULL
, NULL
);
1693 static void service_enter_start(Service
*s
) {
1701 service_unwatch_control_pid(s
);
1702 service_unwatch_main_pid(s
);
1704 /* We want to ensure that nobody leaks processes from
1705 * START_PRE here, so let's go on a killing spree, People
1706 * should not spawn long running processes from START_PRE. */
1707 service_kill_control_processes(s
);
1709 if (s
->type
== SERVICE_FORKING
) {
1710 s
->control_command_id
= SERVICE_EXEC_START
;
1711 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1713 s
->main_command
= NULL
;
1715 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1716 s
->control_command
= NULL
;
1718 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1722 assert(s
->type
== SERVICE_ONESHOT
);
1723 service_enter_start_post(s
);
1727 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
1728 /* For simple + idle this is the main process. We don't apply any timeout here, but
1729 * service_enter_running() will later apply the .runtime_max_usec timeout. */
1730 timeout
= USEC_INFINITY
;
1732 timeout
= s
->timeout_start_usec
;
1734 r
= service_spawn(s
,
1746 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
1747 /* For simple services we immediately start
1748 * the START_POST binaries. */
1750 service_set_main_pid(s
, pid
);
1751 service_enter_start_post(s
);
1753 } else if (s
->type
== SERVICE_FORKING
) {
1755 /* For forking services we wait until the start
1756 * process exited. */
1758 s
->control_pid
= pid
;
1759 service_set_state(s
, SERVICE_START
);
1761 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
)) {
1763 /* For oneshot services we wait until the start
1764 * process exited, too, but it is our main process. */
1766 /* For D-Bus services we know the main pid right away,
1767 * but wait for the bus name to appear on the
1768 * bus. Notify services are similar. */
1770 service_set_main_pid(s
, pid
);
1771 service_set_state(s
, SERVICE_START
);
1773 assert_not_reached("Unknown service type");
1778 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1779 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1782 static void service_enter_start_pre(Service
*s
) {
1787 service_unwatch_control_pid(s
);
1789 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1790 if (s
->control_command
) {
1791 /* Before we start anything, let's clear up what might
1792 * be left from previous runs. */
1793 service_kill_control_processes(s
);
1795 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1797 r
= service_spawn(s
,
1799 s
->timeout_start_usec
,
1801 !s
->permissions_start_only
,
1802 !s
->root_directory_start_only
,
1809 service_set_state(s
, SERVICE_START_PRE
);
1811 service_enter_start(s
);
1816 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1817 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1820 static void service_enter_restart(Service
*s
) {
1821 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1826 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1827 /* Don't restart things if we are going down anyway */
1828 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1830 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1837 /* Any units that are bound to this service must also be
1838 * restarted. We use JOB_RESTART (instead of the more obvious
1839 * JOB_START) here so that those dependency jobs will be added
1841 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, &error
, NULL
);
1845 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1846 * it will be canceled as part of the service_stop() call that
1847 * is executed as part of JOB_RESTART. */
1849 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1853 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1854 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1857 static void service_enter_reload_by_notify(Service
*s
) {
1860 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_start_usec
));
1861 service_set_state(s
, SERVICE_RELOAD
);
1864 static void service_enter_reload(Service
*s
) {
1869 service_unwatch_control_pid(s
);
1870 s
->reload_result
= SERVICE_SUCCESS
;
1872 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1873 if (s
->control_command
) {
1874 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1876 r
= service_spawn(s
,
1878 s
->timeout_start_usec
,
1880 !s
->permissions_start_only
,
1881 !s
->root_directory_start_only
,
1888 service_set_state(s
, SERVICE_RELOAD
);
1890 service_enter_running(s
, SERVICE_SUCCESS
);
1895 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1896 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1897 service_enter_running(s
, SERVICE_SUCCESS
);
1900 static void service_run_next_control(Service
*s
) {
1905 assert(s
->control_command
);
1906 assert(s
->control_command
->command_next
);
1908 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1910 s
->control_command
= s
->control_command
->command_next
;
1911 service_unwatch_control_pid(s
);
1913 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1914 timeout
= s
->timeout_start_usec
;
1916 timeout
= s
->timeout_stop_usec
;
1918 r
= service_spawn(s
,
1922 !s
->permissions_start_only
,
1923 !s
->root_directory_start_only
,
1924 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1925 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1934 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1936 if (s
->state
== SERVICE_START_PRE
)
1937 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1938 else if (s
->state
== SERVICE_STOP
)
1939 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1940 else if (s
->state
== SERVICE_STOP_POST
)
1941 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1942 else if (s
->state
== SERVICE_RELOAD
) {
1943 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1944 service_enter_running(s
, SERVICE_SUCCESS
);
1946 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1949 static void service_run_next_main(Service
*s
) {
1954 assert(s
->main_command
);
1955 assert(s
->main_command
->command_next
);
1956 assert(s
->type
== SERVICE_ONESHOT
);
1958 s
->main_command
= s
->main_command
->command_next
;
1959 service_unwatch_main_pid(s
);
1961 r
= service_spawn(s
,
1963 s
->timeout_start_usec
,
1973 service_set_main_pid(s
, pid
);
1978 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1979 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1982 static int service_start(Unit
*u
) {
1983 Service
*s
= SERVICE(u
);
1988 /* We cannot fulfill this request right now, try again later
1990 if (IN_SET(s
->state
,
1991 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1992 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1995 /* Already on it! */
1996 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1999 /* A service that will be restarted must be stopped first to
2000 * trigger BindsTo and/or OnFailure dependencies. If a user
2001 * does not want to wait for the holdoff time to elapse, the
2002 * service should be manually restarted, not started. We
2003 * simply return EAGAIN here, so that any start jobs stay
2004 * queued, and assume that the auto restart timer will
2005 * eventually trigger the restart. */
2006 if (s
->state
== SERVICE_AUTO_RESTART
)
2009 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
2011 /* Make sure we don't enter a busy loop of some kind. */
2012 r
= unit_start_limit_test(u
);
2014 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT_HIT
, false);
2018 s
->result
= SERVICE_SUCCESS
;
2019 s
->reload_result
= SERVICE_SUCCESS
;
2020 s
->main_pid_known
= false;
2021 s
->main_pid_alien
= false;
2022 s
->forbid_restart
= false;
2023 s
->reset_cpu_usage
= true;
2025 s
->status_text
= mfree(s
->status_text
);
2026 s
->status_errno
= 0;
2028 s
->notify_state
= NOTIFY_UNKNOWN
;
2030 s
->watchdog_override_enable
= false;
2031 s
->watchdog_override_usec
= 0;
2033 service_enter_start_pre(s
);
2037 static int service_stop(Unit
*u
) {
2038 Service
*s
= SERVICE(u
);
2042 /* Don't create restart jobs from manual stops. */
2043 s
->forbid_restart
= true;
2046 if (IN_SET(s
->state
,
2047 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2048 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2051 /* A restart will be scheduled or is in progress. */
2052 if (s
->state
== SERVICE_AUTO_RESTART
) {
2053 service_set_state(s
, SERVICE_DEAD
);
2057 /* If there's already something running we go directly into
2059 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2060 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2064 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2066 service_enter_stop(s
, SERVICE_SUCCESS
);
2070 static int service_reload(Unit
*u
) {
2071 Service
*s
= SERVICE(u
);
2075 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2077 service_enter_reload(s
);
2081 _pure_
static bool service_can_reload(Unit
*u
) {
2082 Service
*s
= SERVICE(u
);
2086 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2089 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2090 Service
*s
= SERVICE(u
);
2098 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2099 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2100 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2102 if (s
->control_pid
> 0)
2103 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2105 if (s
->main_pid_known
&& s
->main_pid
> 0)
2106 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2108 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2109 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2110 unit_serialize_item(u
, f
, "bus-name-owner", s
->bus_name_owner
);
2112 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2116 /* FIXME: There's a minor uncleanliness here: if there are
2117 * multiple commands attached here, we will start from the
2118 * first one again */
2119 if (s
->control_command_id
>= 0)
2120 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2122 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2125 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2128 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2132 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2136 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2137 _cleanup_free_
char *c
= NULL
;
2140 copy
= fdset_put_dup(fds
, fs
->fd
);
2144 c
= cescape(fs
->fdname
);
2146 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2149 if (s
->main_exec_status
.pid
> 0) {
2150 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2151 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2152 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2154 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2155 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2156 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2160 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2162 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2164 if (s
->watchdog_override_enable
)
2165 unit_serialize_item_format(u
, f
, "watchdog-override-usec", USEC_FMT
, s
->watchdog_override_usec
);
2170 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2171 Service
*s
= SERVICE(u
);
2179 if (streq(key
, "state")) {
2182 state
= service_state_from_string(value
);
2184 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2186 s
->deserialized_state
= state
;
2187 } else if (streq(key
, "result")) {
2190 f
= service_result_from_string(value
);
2192 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2193 else if (f
!= SERVICE_SUCCESS
)
2196 } else if (streq(key
, "reload-result")) {
2199 f
= service_result_from_string(value
);
2201 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2202 else if (f
!= SERVICE_SUCCESS
)
2203 s
->reload_result
= f
;
2205 } else if (streq(key
, "control-pid")) {
2208 if (parse_pid(value
, &pid
) < 0)
2209 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2211 s
->control_pid
= pid
;
2212 } else if (streq(key
, "main-pid")) {
2215 if (parse_pid(value
, &pid
) < 0)
2216 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2218 service_set_main_pid(s
, pid
);
2219 unit_watch_pid(UNIT(s
), pid
);
2221 } else if (streq(key
, "main-pid-known")) {
2224 b
= parse_boolean(value
);
2226 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2228 s
->main_pid_known
= b
;
2229 } else if (streq(key
, "bus-name-good")) {
2232 b
= parse_boolean(value
);
2234 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2236 s
->bus_name_good
= b
;
2237 } else if (streq(key
, "bus-name-owner")) {
2238 r
= free_and_strdup(&s
->bus_name_owner
, value
);
2240 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
2241 } else if (streq(key
, "status-text")) {
2244 r
= cunescape(value
, 0, &t
);
2246 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2248 free(s
->status_text
);
2252 } else if (streq(key
, "control-command")) {
2253 ServiceExecCommand id
;
2255 id
= service_exec_command_from_string(value
);
2257 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2259 s
->control_command_id
= id
;
2260 s
->control_command
= s
->exec_command
[id
];
2262 } else if (streq(key
, "socket-fd")) {
2265 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2266 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2268 asynchronous_close(s
->socket_fd
);
2269 s
->socket_fd
= fdset_remove(fds
, fd
);
2271 } else if (streq(key
, "fd-store-fd")) {
2276 pf
= strcspn(value
, WHITESPACE
);
2277 fdv
= strndupa(value
, pf
);
2279 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2280 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2282 _cleanup_free_
char *t
= NULL
;
2286 fdn
+= strspn(fdn
, WHITESPACE
);
2287 (void) cunescape(fdn
, 0, &t
);
2289 r
= service_add_fd_store(s
, fd
, t
);
2291 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2293 fdset_remove(fds
, fd
);
2296 } else if (streq(key
, "main-exec-status-pid")) {
2299 if (parse_pid(value
, &pid
) < 0)
2300 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2302 s
->main_exec_status
.pid
= pid
;
2303 } else if (streq(key
, "main-exec-status-code")) {
2306 if (safe_atoi(value
, &i
) < 0)
2307 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2309 s
->main_exec_status
.code
= i
;
2310 } else if (streq(key
, "main-exec-status-status")) {
2313 if (safe_atoi(value
, &i
) < 0)
2314 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2316 s
->main_exec_status
.status
= i
;
2317 } else if (streq(key
, "main-exec-status-start"))
2318 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2319 else if (streq(key
, "main-exec-status-exit"))
2320 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2321 else if (streq(key
, "watchdog-timestamp"))
2322 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2323 else if (streq(key
, "forbid-restart")) {
2326 b
= parse_boolean(value
);
2328 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2330 s
->forbid_restart
= b
;
2331 } else if (streq(key
, "stdin-fd")) {
2334 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2335 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2337 asynchronous_close(s
->stdin_fd
);
2338 s
->stdin_fd
= fdset_remove(fds
, fd
);
2339 s
->exec_context
.stdio_as_fds
= true;
2341 } else if (streq(key
, "stdout-fd")) {
2344 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2345 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2347 asynchronous_close(s
->stdout_fd
);
2348 s
->stdout_fd
= fdset_remove(fds
, fd
);
2349 s
->exec_context
.stdio_as_fds
= true;
2351 } else if (streq(key
, "stderr-fd")) {
2354 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2355 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2357 asynchronous_close(s
->stderr_fd
);
2358 s
->stderr_fd
= fdset_remove(fds
, fd
);
2359 s
->exec_context
.stdio_as_fds
= true;
2361 } else if (streq(key
, "watchdog-override-usec")) {
2362 usec_t watchdog_override_usec
;
2363 if (timestamp_deserialize(value
, &watchdog_override_usec
) < 0)
2364 log_unit_debug(u
, "Failed to parse watchdog_override_usec value: %s", value
);
2366 s
->watchdog_override_enable
= true;
2367 s
->watchdog_override_usec
= watchdog_override_usec
;
2370 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2375 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2376 const UnitActiveState
*table
;
2380 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2382 return table
[SERVICE(u
)->state
];
2385 static const char *service_sub_state_to_string(Unit
*u
) {
2388 return service_state_to_string(SERVICE(u
)->state
);
2391 static bool service_check_gc(Unit
*u
) {
2392 Service
*s
= SERVICE(u
);
2396 /* Never clean up services that still have a process around,
2397 * even if the service is formally dead. */
2398 if (cgroup_good(s
) > 0 ||
2399 main_pid_good(s
) > 0 ||
2400 control_pid_good(s
) > 0)
2406 static int service_retry_pid_file(Service
*s
) {
2409 assert(s
->pid_file
);
2410 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2412 r
= service_load_pid_file(s
, false);
2416 service_unwatch_pid_file(s
);
2418 service_enter_running(s
, SERVICE_SUCCESS
);
2422 static int service_watch_pid_file(Service
*s
) {
2425 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2427 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2431 /* the pidfile might have appeared just before we set the watch */
2432 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2433 service_retry_pid_file(s
);
2437 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2438 service_unwatch_pid_file(s
);
2442 static int service_demand_pid_file(Service
*s
) {
2445 assert(s
->pid_file
);
2446 assert(!s
->pid_file_pathspec
);
2448 ps
= new0(PathSpec
, 1);
2453 ps
->path
= strdup(s
->pid_file
);
2459 path_kill_slashes(ps
->path
);
2461 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2462 * keep their PID file open all the time. */
2463 ps
->type
= PATH_MODIFIED
;
2464 ps
->inotify_fd
= -1;
2466 s
->pid_file_pathspec
= ps
;
2468 return service_watch_pid_file(s
);
2471 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2472 PathSpec
*p
= userdata
;
2477 s
= SERVICE(p
->unit
);
2481 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2482 assert(s
->pid_file_pathspec
);
2483 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2485 log_unit_debug(UNIT(s
), "inotify event");
2487 if (path_spec_fd_event(p
, events
) < 0)
2490 if (service_retry_pid_file(s
) == 0)
2493 if (service_watch_pid_file(s
) < 0)
2499 service_unwatch_pid_file(s
);
2500 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2504 static void service_notify_cgroup_empty_event(Unit
*u
) {
2505 Service
*s
= SERVICE(u
);
2509 log_unit_debug(u
, "cgroup is empty");
2513 /* Waiting for SIGCHLD is usually more interesting,
2514 * because it includes return codes/signals. Which is
2515 * why we ignore the cgroup events for most cases,
2516 * except when we don't know pid which to expect the
2520 case SERVICE_START_POST
:
2521 /* If we were hoping for the daemon to write its PID file,
2522 * we can give up now. */
2523 if (s
->pid_file_pathspec
) {
2524 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2526 service_unwatch_pid_file(s
);
2527 if (s
->state
== SERVICE_START
)
2528 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2530 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2534 case SERVICE_RUNNING
:
2535 /* service_enter_running() will figure out what to do */
2536 service_enter_running(s
, SERVICE_SUCCESS
);
2539 case SERVICE_STOP_SIGABRT
:
2540 case SERVICE_STOP_SIGTERM
:
2541 case SERVICE_STOP_SIGKILL
:
2543 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2544 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2548 case SERVICE_STOP_POST
:
2549 case SERVICE_FINAL_SIGTERM
:
2550 case SERVICE_FINAL_SIGKILL
:
2551 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2552 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2561 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2562 Service
*s
= SERVICE(u
);
2568 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2569 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2570 f
= SERVICE_SUCCESS
;
2571 else if (code
== CLD_EXITED
)
2572 f
= SERVICE_FAILURE_EXIT_CODE
;
2573 else if (code
== CLD_KILLED
)
2574 f
= SERVICE_FAILURE_SIGNAL
;
2575 else if (code
== CLD_DUMPED
)
2576 f
= SERVICE_FAILURE_CORE_DUMP
;
2578 assert_not_reached("Unknown code");
2580 if (s
->main_pid
== pid
) {
2581 /* Forking services may occasionally move to a new PID.
2582 * As long as they update the PID file before exiting the old
2583 * PID, they're fine. */
2584 if (service_load_pid_file(s
, false) == 0)
2588 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2590 if (s
->main_command
) {
2591 /* If this is not a forking service than the
2592 * main process got started and hence we copy
2593 * the exit status so that it is recorded both
2594 * as main and as control process exit
2597 s
->main_command
->exec_status
= s
->main_exec_status
;
2599 if (s
->main_command
->ignore
)
2600 f
= SERVICE_SUCCESS
;
2601 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2603 /* If this is a forked process, then we should
2604 * ignore the return value if this was
2605 * configured for the starter process */
2607 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2608 f
= SERVICE_SUCCESS
;
2611 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2613 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2614 sigchld_code_to_string(code
), status
,
2615 strna(code
== CLD_EXITED
2616 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2617 : signal_to_string(status
))),
2618 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2619 "EXIT_STATUS=%i", status
,
2622 if (f
!= SERVICE_SUCCESS
)
2625 if (s
->main_command
&&
2626 s
->main_command
->command_next
&&
2627 f
== SERVICE_SUCCESS
) {
2629 /* There is another command to *
2630 * execute, so let's do that. */
2632 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2633 service_run_next_main(s
);
2637 /* The service exited, so the service is officially
2639 s
->main_command
= NULL
;
2643 case SERVICE_START_POST
:
2644 case SERVICE_RELOAD
:
2646 /* Need to wait until the operation is
2651 if (s
->type
== SERVICE_ONESHOT
) {
2652 /* This was our main goal, so let's go on */
2653 if (f
== SERVICE_SUCCESS
)
2654 service_enter_start_post(s
);
2656 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2662 case SERVICE_RUNNING
:
2663 service_enter_running(s
, f
);
2666 case SERVICE_STOP_SIGABRT
:
2667 case SERVICE_STOP_SIGTERM
:
2668 case SERVICE_STOP_SIGKILL
:
2670 if (!control_pid_good(s
))
2671 service_enter_stop_post(s
, f
);
2673 /* If there is still a control process, wait for that first */
2676 case SERVICE_STOP_POST
:
2677 case SERVICE_FINAL_SIGTERM
:
2678 case SERVICE_FINAL_SIGKILL
:
2680 if (!control_pid_good(s
))
2681 service_enter_dead(s
, f
, true);
2685 assert_not_reached("Uh, main process died at wrong time.");
2689 } else if (s
->control_pid
== pid
) {
2692 if (s
->control_command
) {
2693 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2695 if (s
->control_command
->ignore
)
2696 f
= SERVICE_SUCCESS
;
2699 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2700 "Control process exited, code=%s status=%i",
2701 sigchld_code_to_string(code
), status
);
2703 if (f
!= SERVICE_SUCCESS
)
2706 /* Immediately get rid of the cgroup, so that the
2707 * kernel doesn't delay the cgroup empty messages for
2708 * the service cgroup any longer than necessary */
2709 service_kill_control_processes(s
);
2711 if (s
->control_command
&&
2712 s
->control_command
->command_next
&&
2713 f
== SERVICE_SUCCESS
) {
2715 /* There is another command to *
2716 * execute, so let's do that. */
2718 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2719 service_run_next_control(s
);
2722 /* No further commands for this step, so let's
2723 * figure out what to do next */
2725 s
->control_command
= NULL
;
2726 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2728 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2732 case SERVICE_START_PRE
:
2733 if (f
== SERVICE_SUCCESS
)
2734 service_enter_start(s
);
2736 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2740 if (s
->type
!= SERVICE_FORKING
)
2741 /* Maybe spurious event due to a reload that changed the type? */
2744 if (f
!= SERVICE_SUCCESS
) {
2745 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2750 bool has_start_post
;
2753 /* Let's try to load the pid file here if we can.
2754 * The PID file might actually be created by a START_POST
2755 * script. In that case don't worry if the loading fails. */
2757 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2758 r
= service_load_pid_file(s
, !has_start_post
);
2759 if (!has_start_post
&& r
< 0) {
2760 r
= service_demand_pid_file(s
);
2761 if (r
< 0 || !cgroup_good(s
))
2762 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2766 service_search_main_pid(s
);
2768 service_enter_start_post(s
);
2771 case SERVICE_START_POST
:
2772 if (f
!= SERVICE_SUCCESS
) {
2773 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2780 r
= service_load_pid_file(s
, true);
2782 r
= service_demand_pid_file(s
);
2783 if (r
< 0 || !cgroup_good(s
))
2784 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2788 service_search_main_pid(s
);
2790 service_enter_running(s
, SERVICE_SUCCESS
);
2793 case SERVICE_RELOAD
:
2794 if (f
== SERVICE_SUCCESS
)
2795 if (service_load_pid_file(s
, true) < 0)
2796 service_search_main_pid(s
);
2798 s
->reload_result
= f
;
2799 service_enter_running(s
, SERVICE_SUCCESS
);
2803 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2806 case SERVICE_STOP_SIGABRT
:
2807 case SERVICE_STOP_SIGTERM
:
2808 case SERVICE_STOP_SIGKILL
:
2809 if (main_pid_good(s
) <= 0)
2810 service_enter_stop_post(s
, f
);
2812 /* If there is still a service
2813 * process around, wait until
2814 * that one quit, too */
2817 case SERVICE_STOP_POST
:
2818 case SERVICE_FINAL_SIGTERM
:
2819 case SERVICE_FINAL_SIGKILL
:
2820 if (main_pid_good(s
) <= 0)
2821 service_enter_dead(s
, f
, true);
2825 assert_not_reached("Uh, control process died at wrong time.");
2830 /* Notify clients about changed exit status */
2831 unit_add_to_dbus_queue(u
);
2833 /* We got one SIGCHLD for the service, let's watch all
2834 * processes that are now running of the service, and watch
2835 * that. Among the PIDs we then watch will be children
2836 * reassigned to us, which hopefully allows us to identify
2837 * when all children are gone */
2838 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2839 unit_watch_all_pids(u
);
2841 /* If the PID set is empty now, then let's finish this off
2842 (On unified we use proper notifications) */
2843 if (cg_unified() <= 0 && set_isempty(u
->pids
))
2844 service_notify_cgroup_empty_event(u
);
2847 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2848 Service
*s
= SERVICE(userdata
);
2851 assert(source
== s
->timer_event_source
);
2855 case SERVICE_START_PRE
:
2857 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2858 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2861 case SERVICE_START_POST
:
2862 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2863 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2866 case SERVICE_RUNNING
:
2867 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
2868 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2871 case SERVICE_RELOAD
:
2872 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
2873 service_kill_control_processes(s
);
2874 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2875 service_enter_running(s
, SERVICE_SUCCESS
);
2879 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2880 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2883 case SERVICE_STOP_SIGABRT
:
2884 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2885 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2888 case SERVICE_STOP_SIGTERM
:
2889 if (s
->kill_context
.send_sigkill
) {
2890 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2891 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2893 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2894 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2899 case SERVICE_STOP_SIGKILL
:
2900 /* Uh, we sent a SIGKILL and it is still not gone?
2901 * Must be something we cannot kill, so let's just be
2902 * weirded out and continue */
2904 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2905 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2908 case SERVICE_STOP_POST
:
2909 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2910 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2913 case SERVICE_FINAL_SIGTERM
:
2914 if (s
->kill_context
.send_sigkill
) {
2915 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2916 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2918 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2919 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2924 case SERVICE_FINAL_SIGKILL
:
2925 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2926 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2929 case SERVICE_AUTO_RESTART
:
2930 log_unit_info(UNIT(s
),
2931 s
->restart_usec
> 0 ?
2932 "Service hold-off time over, scheduling restart." :
2933 "Service has no hold-off time, scheduling restart.");
2934 service_enter_restart(s
);
2938 assert_not_reached("Timeout at wrong time.");
2944 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2945 Service
*s
= SERVICE(userdata
);
2946 char t
[FORMAT_TIMESPAN_MAX
];
2947 usec_t watchdog_usec
;
2950 assert(source
== s
->watchdog_event_source
);
2952 watchdog_usec
= service_get_watchdog_usec(s
);
2954 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2955 format_timespan(t
, sizeof(t
), watchdog_usec
, 1));
2957 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2962 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2963 Service
*s
= SERVICE(u
);
2964 _cleanup_free_
char *cc
= NULL
;
2965 bool notify_dbus
= false;
2970 cc
= strv_join(tags
, ", ");
2972 if (s
->notify_access
== NOTIFY_NONE
) {
2973 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2975 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2976 if (s
->main_pid
!= 0)
2977 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2979 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2982 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2984 /* Interpret MAINPID= */
2985 e
= strv_find_startswith(tags
, "MAINPID=");
2986 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2987 if (parse_pid(e
, &pid
) < 0)
2988 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2990 service_set_main_pid(s
, pid
);
2991 unit_watch_pid(UNIT(s
), pid
);
2996 /* Interpret RELOADING= */
2997 if (strv_find(tags
, "RELOADING=1")) {
2999 s
->notify_state
= NOTIFY_RELOADING
;
3001 if (s
->state
== SERVICE_RUNNING
)
3002 service_enter_reload_by_notify(s
);
3007 /* Interpret READY= */
3008 if (strv_find(tags
, "READY=1")) {
3010 s
->notify_state
= NOTIFY_READY
;
3012 /* Type=notify services inform us about completed
3013 * initialization with READY=1 */
3014 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
3015 service_enter_start_post(s
);
3017 /* Sending READY=1 while we are reloading informs us
3018 * that the reloading is complete */
3019 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
3020 service_enter_running(s
, SERVICE_SUCCESS
);
3025 /* Interpret STOPPING= */
3026 if (strv_find(tags
, "STOPPING=1")) {
3028 s
->notify_state
= NOTIFY_STOPPING
;
3030 if (s
->state
== SERVICE_RUNNING
)
3031 service_enter_stop_by_notify(s
);
3036 /* Interpret STATUS= */
3037 e
= strv_find_startswith(tags
, "STATUS=");
3039 _cleanup_free_
char *t
= NULL
;
3042 if (!utf8_is_valid(e
))
3043 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3051 if (!streq_ptr(s
->status_text
, t
)) {
3053 free(s
->status_text
);
3061 /* Interpret ERRNO= */
3062 e
= strv_find_startswith(tags
, "ERRNO=");
3066 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3067 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3069 if (s
->status_errno
!= status_errno
) {
3070 s
->status_errno
= status_errno
;
3076 /* Interpret WATCHDOG= */
3077 if (strv_find(tags
, "WATCHDOG=1"))
3078 service_reset_watchdog(s
);
3080 if (strv_find(tags
, "FDSTORE=1")) {
3083 name
= strv_find_startswith(tags
, "FDNAME=");
3084 if (name
&& !fdname_is_valid(name
)) {
3085 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3089 service_add_fd_store_set(s
, fds
, name
);
3092 e
= strv_find_startswith(tags
, "WATCHDOG_USEC=");
3094 usec_t watchdog_override_usec
;
3095 if (safe_atou64(e
, &watchdog_override_usec
) < 0)
3096 log_unit_warning(u
, "Failed to parse WATCHDOG_USEC=%s", e
);
3098 service_reset_watchdog_timeout(s
, watchdog_override_usec
);
3101 /* Notify clients about changed status or main pid */
3103 unit_add_to_dbus_queue(u
);
3106 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
3107 Service
*s
= SERVICE(u
);
3111 if (!s
->timer_event_source
)
3114 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3117 if (t
== USEC_INFINITY
)
3124 static void service_bus_name_owner_change(
3127 const char *old_owner
,
3128 const char *new_owner
) {
3130 Service
*s
= SERVICE(u
);
3136 assert(streq(s
->bus_name
, name
));
3137 assert(old_owner
|| new_owner
);
3139 if (old_owner
&& new_owner
)
3140 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3142 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3144 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3146 s
->bus_name_good
= !!new_owner
;
3148 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3149 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
3151 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
3155 if (s
->type
== SERVICE_DBUS
) {
3157 /* service_enter_running() will figure out what to
3159 if (s
->state
== SERVICE_RUNNING
)
3160 service_enter_running(s
, SERVICE_SUCCESS
);
3161 else if (s
->state
== SERVICE_START
&& new_owner
)
3162 service_enter_start_post(s
);
3164 } else if (new_owner
&&
3166 (s
->state
== SERVICE_START
||
3167 s
->state
== SERVICE_START_POST
||
3168 s
->state
== SERVICE_RUNNING
||
3169 s
->state
== SERVICE_RELOAD
)) {
3171 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3174 /* Try to acquire PID from bus service */
3176 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3178 r
= sd_bus_creds_get_pid(creds
, &pid
);
3180 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3182 service_set_main_pid(s
, pid
);
3183 unit_watch_pid(UNIT(s
), pid
);
3188 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3189 _cleanup_free_
char *peer
= NULL
;
3195 /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs
3196 * to be configured. We take ownership of the passed fd on success. */
3198 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3201 if (s
->socket_fd
>= 0)
3204 if (s
->state
!= SERVICE_DEAD
)
3207 if (getpeername_pretty(fd
, true, &peer
) >= 0) {
3209 if (UNIT(s
)->description
) {
3210 _cleanup_free_
char *a
;
3212 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3216 r
= unit_set_description(UNIT(s
), a
);
3218 r
= unit_set_description(UNIT(s
), peer
);
3224 r
= unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3229 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3231 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3235 static void service_reset_failed(Unit
*u
) {
3236 Service
*s
= SERVICE(u
);
3240 if (s
->state
== SERVICE_FAILED
)
3241 service_set_state(s
, SERVICE_DEAD
);
3243 s
->result
= SERVICE_SUCCESS
;
3244 s
->reload_result
= SERVICE_SUCCESS
;
3247 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3248 Service
*s
= SERVICE(u
);
3250 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3253 static int service_main_pid(Unit
*u
) {
3254 Service
*s
= SERVICE(u
);
3261 static int service_control_pid(Unit
*u
) {
3262 Service
*s
= SERVICE(u
);
3266 return s
->control_pid
;
3269 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3270 [SERVICE_RESTART_NO
] = "no",
3271 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3272 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3273 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3274 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3275 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3276 [SERVICE_RESTART_ALWAYS
] = "always",
3279 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3281 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3282 [SERVICE_SIMPLE
] = "simple",
3283 [SERVICE_FORKING
] = "forking",
3284 [SERVICE_ONESHOT
] = "oneshot",
3285 [SERVICE_DBUS
] = "dbus",
3286 [SERVICE_NOTIFY
] = "notify",
3287 [SERVICE_IDLE
] = "idle"
3290 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3292 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3293 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3294 [SERVICE_EXEC_START
] = "ExecStart",
3295 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3296 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3297 [SERVICE_EXEC_STOP
] = "ExecStop",
3298 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3301 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3303 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3304 [NOTIFY_NONE
] = "none",
3305 [NOTIFY_MAIN
] = "main",
3306 [NOTIFY_ALL
] = "all"
3309 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3311 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3312 [NOTIFY_UNKNOWN
] = "unknown",
3313 [NOTIFY_READY
] = "ready",
3314 [NOTIFY_RELOADING
] = "reloading",
3315 [NOTIFY_STOPPING
] = "stopping",
3318 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3320 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3321 [SERVICE_SUCCESS
] = "success",
3322 [SERVICE_FAILURE_RESOURCES
] = "resources",
3323 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3324 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3325 [SERVICE_FAILURE_SIGNAL
] = "signal",
3326 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3327 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3328 [SERVICE_FAILURE_START_LIMIT_HIT
] = "start-limit-hit",
3331 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3333 const UnitVTable service_vtable
= {
3334 .object_size
= sizeof(Service
),
3335 .exec_context_offset
= offsetof(Service
, exec_context
),
3336 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3337 .kill_context_offset
= offsetof(Service
, kill_context
),
3338 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3339 .dynamic_creds_offset
= offsetof(Service
, dynamic_creds
),
3345 .private_section
= "Service",
3347 .init
= service_init
,
3348 .done
= service_done
,
3349 .load
= service_load
,
3350 .release_resources
= service_release_resources
,
3352 .coldplug
= service_coldplug
,
3354 .dump
= service_dump
,
3356 .start
= service_start
,
3357 .stop
= service_stop
,
3358 .reload
= service_reload
,
3360 .can_reload
= service_can_reload
,
3362 .kill
= service_kill
,
3364 .serialize
= service_serialize
,
3365 .deserialize_item
= service_deserialize_item
,
3367 .active_state
= service_active_state
,
3368 .sub_state_to_string
= service_sub_state_to_string
,
3370 .check_gc
= service_check_gc
,
3372 .sigchld_event
= service_sigchld_event
,
3374 .reset_failed
= service_reset_failed
,
3376 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3377 .notify_message
= service_notify_message
,
3379 .main_pid
= service_main_pid
,
3380 .control_pid
= service_control_pid
,
3382 .bus_name_owner_change
= service_bus_name_owner_change
,
3384 .bus_vtable
= bus_service_vtable
,
3385 .bus_set_property
= bus_service_set_property
,
3386 .bus_commit_properties
= bus_service_commit_properties
,
3388 .get_timeout
= service_get_timeout
,
3389 .can_transient
= true,
3391 .status_message_formats
= {
3392 .starting_stopping
= {
3393 [0] = "Starting %s...",
3394 [1] = "Stopping %s...",
3396 .finished_start_job
= {
3397 [JOB_DONE
] = "Started %s.",
3398 [JOB_FAILED
] = "Failed to start %s.",
3400 .finished_stop_job
= {
3401 [JOB_DONE
] = "Stopped %s.",
3402 [JOB_FAILED
] = "Stopped (with error) %s.",