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 void service_start_watchdog(Service
*s
) {
208 if (s
->watchdog_usec
<= 0)
211 if (s
->watchdog_event_source
) {
212 r
= sd_event_source_set_time(s
->watchdog_event_source
, usec_add(s
->watchdog_timestamp
.monotonic
, s
->watchdog_usec
));
214 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
218 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
220 r
= sd_event_add_time(
221 UNIT(s
)->manager
->event
,
222 &s
->watchdog_event_source
,
224 usec_add(s
->watchdog_timestamp
.monotonic
, s
->watchdog_usec
), 0,
225 service_dispatch_watchdog
, s
);
227 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
231 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
233 /* Let's process everything else which might be a sign
234 * of living before we consider a service died. */
235 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
239 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
242 static void service_reset_watchdog(Service
*s
) {
245 dual_timestamp_get(&s
->watchdog_timestamp
);
246 service_start_watchdog(s
);
249 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
255 assert(fs
->service
->n_fd_store
> 0);
256 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
257 fs
->service
->n_fd_store
--;
260 if (fs
->event_source
) {
261 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
262 sd_event_source_unref(fs
->event_source
);
270 static void service_release_resources(Unit
*u
) {
271 Service
*s
= SERVICE(u
);
275 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
278 log_unit_debug(u
, "Releasing all resources.");
280 s
->stdin_fd
= safe_close(s
->stdin_fd
);
281 s
->stdout_fd
= safe_close(s
->stdout_fd
);
282 s
->stderr_fd
= safe_close(s
->stderr_fd
);
285 service_fd_store_unlink(s
->fd_store
);
287 assert(s
->n_fd_store
== 0);
290 static void service_done(Unit
*u
) {
291 Service
*s
= SERVICE(u
);
295 s
->pid_file
= mfree(s
->pid_file
);
296 s
->status_text
= mfree(s
->status_text
);
298 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
299 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
300 s
->control_command
= NULL
;
301 s
->main_command
= NULL
;
303 exit_status_set_free(&s
->restart_prevent_status
);
304 exit_status_set_free(&s
->restart_force_status
);
305 exit_status_set_free(&s
->success_status
);
307 /* This will leak a process, but at least no memory or any of
309 service_unwatch_main_pid(s
);
310 service_unwatch_control_pid(s
);
311 service_unwatch_pid_file(s
);
314 unit_unwatch_bus_name(u
, s
->bus_name
);
315 s
->bus_name
= mfree(s
->bus_name
);
318 s
->bus_name_owner
= mfree(s
->bus_name_owner
);
320 service_close_socket_fd(s
);
322 unit_ref_unset(&s
->accept_socket
);
324 service_stop_watchdog(s
);
326 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
328 service_release_resources(u
);
331 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
332 ServiceFDStore
*fs
= userdata
;
337 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
338 service_fd_store_unlink(fs
);
342 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
349 if (s
->n_fd_store
>= s
->n_fd_store_max
)
352 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
353 r
= same_fd(fs
->fd
, fd
);
357 /* Already included */
363 fs
= new0(ServiceFDStore
, 1);
369 fs
->fdname
= strdup(name
?: "stored");
375 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
382 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
384 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
390 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
395 if (fdset_size(fds
) <= 0)
398 while (s
->n_fd_store
< s
->n_fd_store_max
) {
399 _cleanup_close_
int fd
= -1;
401 fd
= fdset_steal_first(fds
);
405 r
= service_add_fd_store(s
, fd
, name
);
407 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
409 log_unit_debug(UNIT(s
), "Added fd to fd store.");
414 if (fdset_size(fds
) > 0)
415 log_unit_warning(UNIT(s
), "Tried to store more fds than FileDescriptorStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
420 static int service_arm_timer(Service
*s
, usec_t usec
) {
425 if (s
->timer_event_source
) {
426 r
= sd_event_source_set_time(s
->timer_event_source
, usec
);
430 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
433 if (usec
== USEC_INFINITY
)
436 r
= sd_event_add_time(
437 UNIT(s
)->manager
->event
,
438 &s
->timer_event_source
,
441 service_dispatch_timer
, s
);
445 (void) sd_event_source_set_description(s
->timer_event_source
, "service-timer");
450 static int service_verify(Service
*s
) {
453 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
456 if (!s
->exec_command
[SERVICE_EXEC_START
] && !s
->exec_command
[SERVICE_EXEC_STOP
]) {
457 log_unit_error(UNIT(s
), "Service lacks both ExecStart= and ExecStop= setting. Refusing.");
461 if (s
->type
!= SERVICE_ONESHOT
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
462 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
466 if (!s
->remain_after_exit
&& !s
->exec_command
[SERVICE_EXEC_START
]) {
467 log_unit_error(UNIT(s
), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing.");
471 if (s
->type
!= SERVICE_ONESHOT
&& s
->exec_command
[SERVICE_EXEC_START
]->command_next
) {
472 log_unit_error(UNIT(s
), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing.");
476 if (s
->type
== SERVICE_ONESHOT
&& s
->restart
!= SERVICE_RESTART_NO
) {
477 log_unit_error(UNIT(s
), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing.");
481 if (s
->type
== SERVICE_ONESHOT
&& !exit_status_set_is_empty(&s
->restart_force_status
)) {
482 log_unit_error(UNIT(s
), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing.");
486 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name
) {
487 log_unit_error(UNIT(s
), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing.");
491 if (s
->bus_name
&& s
->type
!= SERVICE_DBUS
)
492 log_unit_warning(UNIT(s
), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring.");
494 if (s
->exec_context
.pam_name
&& !(s
->kill_context
.kill_mode
== KILL_CONTROL_GROUP
|| s
->kill_context
.kill_mode
== KILL_MIXED
)) {
495 log_unit_error(UNIT(s
), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing.");
499 if (s
->usb_function_descriptors
&& !s
->usb_function_strings
)
500 log_unit_warning(UNIT(s
), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring.");
502 if (!s
->usb_function_descriptors
&& s
->usb_function_strings
)
503 log_unit_warning(UNIT(s
), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring.");
505 if (s
->runtime_max_usec
!= USEC_INFINITY
&& s
->type
== SERVICE_ONESHOT
)
506 log_unit_warning(UNIT(s
), "MaxRuntimeSec= has no effect in combination with Type=oneshot. Ignoring.");
511 static int service_add_default_dependencies(Service
*s
) {
516 if (!UNIT(s
)->default_dependencies
)
519 /* Add a number of automatic dependencies useful for the
520 * majority of services. */
522 if (MANAGER_IS_SYSTEM(UNIT(s
)->manager
)) {
523 /* First, pull in the really early boot stuff, and
524 * require it, so that we fail if we can't acquire
527 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_SYSINIT_TARGET
, NULL
, true);
532 /* In the --user instance there's no sysinit.target,
533 * in that case require basic.target instead. */
535 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
540 /* Second, if the rest of the base system is in the same
541 * transaction, order us after it, but do not pull it in or
542 * even require it. */
543 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_BASIC_TARGET
, NULL
, true);
547 /* Third, add us in for normal shutdown. */
548 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
551 static void service_fix_output(Service
*s
) {
554 /* If nothing has been explicitly configured, patch default
555 * output in. If input is socket/tty we avoid this however,
556 * since in that case we want output to default to the same
557 * place as we read input from. */
559 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
560 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
561 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
562 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
564 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
565 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
566 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
569 static int service_setup_bus_name(Service
*s
) {
577 if (is_kdbus_available()) {
580 n
= strjoina(s
->bus_name
, ".busname");
581 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
583 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency to .busname unit: %m");
586 /* If kdbus is not available, we know the dbus socket is required, hence pull it in, and require it */
587 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_REQUIRES
, SPECIAL_DBUS_SOCKET
, NULL
, true);
589 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
592 /* Regardless if kdbus is used or not, we always want to be ordered against dbus.socket if both are in the transaction. */
593 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, SPECIAL_DBUS_SOCKET
, NULL
, true);
595 return log_unit_error_errno(UNIT(s
), r
, "Failed to add dependency on " SPECIAL_DBUS_SOCKET
": %m");
597 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
599 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
601 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
606 static int service_add_extras(Service
*s
) {
611 if (s
->type
== _SERVICE_TYPE_INVALID
) {
612 /* Figure out a type automatically */
614 s
->type
= SERVICE_DBUS
;
615 else if (s
->exec_command
[SERVICE_EXEC_START
])
616 s
->type
= SERVICE_SIMPLE
;
618 s
->type
= SERVICE_ONESHOT
;
621 /* Oneshot services have disabled start timeout by default */
622 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
623 s
->timeout_start_usec
= USEC_INFINITY
;
625 service_fix_output(s
);
627 r
= unit_patch_contexts(UNIT(s
));
631 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
635 r
= unit_set_default_slice(UNIT(s
));
639 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
640 s
->notify_access
= NOTIFY_MAIN
;
642 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
643 s
->notify_access
= NOTIFY_MAIN
;
645 r
= service_add_default_dependencies(s
);
649 r
= service_setup_bus_name(s
);
656 static int service_load(Unit
*u
) {
657 Service
*s
= SERVICE(u
);
662 /* Load a .service file */
663 r
= unit_load_fragment(u
);
667 /* Still nothing found? Then let's give up */
668 if (u
->load_state
== UNIT_STUB
)
671 /* This is a new unit? Then let's add in some extras */
672 if (u
->load_state
== UNIT_LOADED
) {
674 /* We were able to load something, then let's add in
675 * the dropin directories. */
676 r
= unit_load_dropin(u
);
680 /* This is a new unit? Then let's add in some
682 r
= service_add_extras(s
);
687 return service_verify(s
);
690 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
691 ServiceExecCommand c
;
692 Service
*s
= SERVICE(u
);
697 prefix
= strempty(prefix
);
698 prefix2
= strjoina(prefix
, "\t");
701 "%sService State: %s\n"
703 "%sReload Result: %s\n"
704 "%sPermissionsStartOnly: %s\n"
705 "%sRootDirectoryStartOnly: %s\n"
706 "%sRemainAfterExit: %s\n"
707 "%sGuessMainPID: %s\n"
710 "%sNotifyAccess: %s\n"
711 "%sNotifyState: %s\n",
712 prefix
, service_state_to_string(s
->state
),
713 prefix
, service_result_to_string(s
->result
),
714 prefix
, service_result_to_string(s
->reload_result
),
715 prefix
, yes_no(s
->permissions_start_only
),
716 prefix
, yes_no(s
->root_directory_start_only
),
717 prefix
, yes_no(s
->remain_after_exit
),
718 prefix
, yes_no(s
->guess_main_pid
),
719 prefix
, service_type_to_string(s
->type
),
720 prefix
, service_restart_to_string(s
->restart
),
721 prefix
, notify_access_to_string(s
->notify_access
),
722 prefix
, notify_state_to_string(s
->notify_state
));
724 if (s
->control_pid
> 0)
726 "%sControl PID: "PID_FMT
"\n",
727 prefix
, s
->control_pid
);
731 "%sMain PID: "PID_FMT
"\n"
732 "%sMain PID Known: %s\n"
733 "%sMain PID Alien: %s\n",
735 prefix
, yes_no(s
->main_pid_known
),
736 prefix
, yes_no(s
->main_pid_alien
));
741 prefix
, s
->pid_file
);
746 "%sBus Name Good: %s\n",
748 prefix
, yes_no(s
->bus_name_good
));
750 kill_context_dump(&s
->kill_context
, f
, prefix
);
751 exec_context_dump(&s
->exec_context
, f
, prefix
);
753 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
755 if (!s
->exec_command
[c
])
758 fprintf(f
, "%s-> %s:\n",
759 prefix
, service_exec_command_to_string(c
));
761 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
765 fprintf(f
, "%sStatus Text: %s\n",
766 prefix
, s
->status_text
);
768 if (s
->n_fd_store_max
> 0)
770 "%sFile Descriptor Store Max: %u\n"
771 "%sFile Descriptor Store Current: %u\n",
772 prefix
, s
->n_fd_store_max
,
773 prefix
, s
->n_fd_store
);
776 static int service_load_pid_file(Service
*s
, bool may_warn
) {
777 _cleanup_free_
char *k
= NULL
;
786 r
= read_one_line_file(s
->pid_file
, &k
);
789 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
));
793 r
= parse_pid(k
, &pid
);
796 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
800 if (!pid_is_alive(pid
)) {
802 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
806 if (s
->main_pid_known
) {
807 if (pid
== s
->main_pid
)
810 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
812 service_unwatch_main_pid(s
);
813 s
->main_pid_known
= false;
815 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
817 r
= service_set_main_pid(s
, pid
);
821 r
= unit_watch_pid(UNIT(s
), pid
);
823 /* FIXME: we need to do something here */
824 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
831 static void service_search_main_pid(Service
*s
) {
837 /* If we know it anyway, don't ever fallback to unreliable
839 if (s
->main_pid_known
)
842 if (!s
->guess_main_pid
)
845 assert(s
->main_pid
<= 0);
847 if (unit_search_main_pid(UNIT(s
), &pid
) < 0)
850 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
851 if (service_set_main_pid(s
, pid
) < 0)
854 r
= unit_watch_pid(UNIT(s
), pid
);
856 /* FIXME: we need to do something here */
857 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
860 static void service_set_state(Service
*s
, ServiceState state
) {
861 ServiceState old_state
;
862 const UnitActiveState
*table
;
866 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
868 old_state
= s
->state
;
871 service_unwatch_pid_file(s
);
874 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
877 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
878 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
879 SERVICE_AUTO_RESTART
))
880 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
883 SERVICE_START
, SERVICE_START_POST
,
884 SERVICE_RUNNING
, SERVICE_RELOAD
,
885 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
886 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
887 service_unwatch_main_pid(s
);
888 s
->main_command
= NULL
;
892 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
894 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
895 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
896 service_unwatch_control_pid(s
);
897 s
->control_command
= NULL
;
898 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
901 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
902 unit_unwatch_all_pids(UNIT(s
));
905 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
906 SERVICE_RUNNING
, SERVICE_RELOAD
,
907 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
908 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
909 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
))
910 service_close_socket_fd(s
);
912 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
913 service_stop_watchdog(s
);
915 /* For the inactive states unit_notify() will trim the cgroup,
916 * but for exit we have to do that ourselves... */
917 if (state
== SERVICE_EXITED
&& !MANAGER_IS_RELOADING(UNIT(s
)->manager
))
918 unit_prune_cgroup(UNIT(s
));
920 /* For remain_after_exit services, let's see if we can "release" the
921 * hold on the console, since unit_notify() only does that in case of
923 if (state
== SERVICE_EXITED
&&
924 s
->remain_after_exit
&&
925 UNIT(s
)->manager
->n_on_console
> 0) {
929 ec
= unit_get_exec_context(UNIT(s
));
930 if (ec
&& exec_context_may_touch_console(ec
)) {
931 Manager
*m
= UNIT(s
)->manager
;
934 if (m
->n_on_console
== 0)
935 /* unset no_console_output flag, since the console is free */
936 m
->no_console_output
= false;
940 if (old_state
!= state
)
941 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
943 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
946 static usec_t
service_coldplug_timeout(Service
*s
) {
949 switch (s
->deserialized_state
) {
951 case SERVICE_START_PRE
:
953 case SERVICE_START_POST
:
955 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_start_usec
);
957 case SERVICE_RUNNING
:
958 return usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
);
961 case SERVICE_STOP_SIGABRT
:
962 case SERVICE_STOP_SIGTERM
:
963 case SERVICE_STOP_SIGKILL
:
964 case SERVICE_STOP_POST
:
965 case SERVICE_FINAL_SIGTERM
:
966 case SERVICE_FINAL_SIGKILL
:
967 return usec_add(UNIT(s
)->state_change_timestamp
.monotonic
, s
->timeout_stop_usec
);
969 case SERVICE_AUTO_RESTART
:
970 return usec_add(UNIT(s
)->inactive_enter_timestamp
.monotonic
, s
->restart_usec
);
973 return USEC_INFINITY
;
977 static int service_coldplug(Unit
*u
) {
978 Service
*s
= SERVICE(u
);
982 assert(s
->state
== SERVICE_DEAD
);
984 if (s
->deserialized_state
== s
->state
)
987 r
= service_arm_timer(s
, service_coldplug_timeout(s
));
991 if (s
->main_pid
> 0 &&
992 pid_is_unwaited(s
->main_pid
) &&
993 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
994 IN_SET(s
->deserialized_state
,
995 SERVICE_START
, SERVICE_START_POST
,
996 SERVICE_RUNNING
, SERVICE_RELOAD
,
997 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
998 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
999 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
1004 if (s
->control_pid
> 0 &&
1005 pid_is_unwaited(s
->control_pid
) &&
1006 IN_SET(s
->deserialized_state
,
1007 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
1009 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1010 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
1011 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
1016 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
1017 unit_watch_all_pids(UNIT(s
));
1019 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1020 service_start_watchdog(s
);
1022 service_set_state(s
, s
->deserialized_state
);
1026 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
1027 _cleanup_strv_free_
char **rfd_names
= NULL
;
1028 _cleanup_free_
int *rfds
= NULL
;
1035 if (s
->socket_fd
>= 0) {
1037 /* Pass the per-connection socket */
1042 rfds
[0] = s
->socket_fd
;
1044 rfd_names
= strv_new("connection", NULL
);
1053 /* Pass all our configured sockets for singleton services */
1055 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1056 _cleanup_free_
int *cfds
= NULL
;
1060 if (u
->type
!= UNIT_SOCKET
)
1065 cn_fds
= socket_collect_fds(sock
, &cfds
);
1080 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1084 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1090 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1096 if (s
->n_fd_store
> 0) {
1101 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1107 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1113 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1114 rfds
[rn_fds
] = fs
->fd
;
1115 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1116 if (!rfd_names
[rn_fds
])
1122 rfd_names
[rn_fds
] = NULL
;
1126 *fd_names
= rfd_names
;
1134 static int service_spawn(
1139 bool apply_permissions
,
1141 bool apply_tty_stdin
,
1145 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1146 _cleanup_free_
int *fds
= NULL
;
1147 unsigned n_fds
= 0, n_env
= 0;
1151 ExecParameters exec_params
= {
1152 .apply_permissions
= apply_permissions
,
1153 .apply_chroot
= apply_chroot
,
1154 .apply_tty_stdin
= apply_tty_stdin
,
1166 (void) unit_realize_cgroup(UNIT(s
));
1167 if (s
->reset_cpu_usage
) {
1168 (void) unit_reset_cpu_usage(UNIT(s
));
1169 s
->reset_cpu_usage
= false;
1172 r
= unit_setup_exec_runtime(UNIT(s
));
1177 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1178 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1179 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1181 r
= service_collect_fds(s
, &fds
, &fd_names
);
1188 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), timeout
));
1192 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1196 our_env
= new0(char*, 6);
1200 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1201 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0)
1204 if (s
->main_pid
> 0)
1205 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0)
1208 if (!MANAGER_IS_SYSTEM(UNIT(s
)->manager
))
1209 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0)
1212 if (s
->socket_fd
>= 0) {
1213 union sockaddr_union sa
;
1214 socklen_t salen
= sizeof(sa
);
1216 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1220 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1221 _cleanup_free_
char *addr
= NULL
;
1225 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1229 t
= strappend("REMOTE_ADDR=", addr
);
1232 our_env
[n_env
++] = t
;
1234 port
= sockaddr_port(&sa
.sa
);
1238 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0)
1240 our_env
[n_env
++] = t
;
1244 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1248 if (is_control
&& UNIT(s
)->cgroup_path
) {
1249 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1250 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1252 path
= UNIT(s
)->cgroup_path
;
1254 exec_params
.argv
= argv
;
1255 exec_params
.fds
= fds
;
1256 exec_params
.fd_names
= fd_names
;
1257 exec_params
.n_fds
= n_fds
;
1258 exec_params
.environment
= final_env
;
1259 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1260 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1261 exec_params
.cgroup_path
= path
;
1262 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1263 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1264 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1265 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1266 if (s
->type
== SERVICE_IDLE
)
1267 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1268 exec_params
.stdin_fd
= s
->stdin_fd
;
1269 exec_params
.stdout_fd
= s
->stdout_fd
;
1270 exec_params
.stderr_fd
= s
->stderr_fd
;
1272 r
= exec_spawn(UNIT(s
),
1281 r
= unit_watch_pid(UNIT(s
), pid
);
1283 /* FIXME: we need to do something here */
1291 static int main_pid_good(Service
*s
) {
1294 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1297 /* If we know the pid file, then let's just check if it is
1299 if (s
->main_pid_known
) {
1301 /* If it's an alien child let's check if it is still
1303 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1304 return pid_is_alive(s
->main_pid
);
1306 /* .. otherwise assume we'll get a SIGCHLD for it,
1307 * which we really should wait for to collect exit
1308 * status and code */
1309 return s
->main_pid
> 0;
1312 /* We don't know the pid */
1316 _pure_
static int control_pid_good(Service
*s
) {
1319 return s
->control_pid
> 0;
1322 static int cgroup_good(Service
*s
) {
1327 if (!UNIT(s
)->cgroup_path
)
1330 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1337 static bool service_shall_restart(Service
*s
) {
1340 /* Don't restart after manual stops */
1341 if (s
->forbid_restart
)
1344 /* Never restart if this is configured as special exception */
1345 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1348 /* Restart if the exit code/status are configured as restart triggers */
1349 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1352 switch (s
->restart
) {
1354 case SERVICE_RESTART_NO
:
1357 case SERVICE_RESTART_ALWAYS
:
1360 case SERVICE_RESTART_ON_SUCCESS
:
1361 return s
->result
== SERVICE_SUCCESS
;
1363 case SERVICE_RESTART_ON_FAILURE
:
1364 return s
->result
!= SERVICE_SUCCESS
;
1366 case SERVICE_RESTART_ON_ABNORMAL
:
1367 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1369 case SERVICE_RESTART_ON_WATCHDOG
:
1370 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1372 case SERVICE_RESTART_ON_ABORT
:
1373 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1376 assert_not_reached("unknown restart setting");
1380 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1384 if (f
!= SERVICE_SUCCESS
)
1387 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1389 if (s
->result
!= SERVICE_SUCCESS
) {
1390 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1391 failure_action(UNIT(s
)->manager
, s
->failure_action
, UNIT(s
)->reboot_arg
);
1394 if (allow_restart
&& service_shall_restart(s
)) {
1396 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1400 service_set_state(s
, SERVICE_AUTO_RESTART
);
1403 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1404 s
->forbid_restart
= false;
1406 /* We want fresh tmpdirs in case service is started again immediately */
1407 exec_runtime_destroy(s
->exec_runtime
);
1408 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1410 /* Also, remove the runtime directory in */
1411 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1413 /* Try to delete the pid file. At this point it will be
1414 * out-of-date, and some software might be confused by it, so
1415 * let's remove it. */
1417 (void) unlink(s
->pid_file
);
1422 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1423 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1426 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1430 if (f
!= SERVICE_SUCCESS
)
1433 service_unwatch_control_pid(s
);
1434 unit_watch_all_pids(UNIT(s
));
1436 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1437 if (s
->control_command
) {
1438 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1440 r
= service_spawn(s
,
1442 s
->timeout_stop_usec
,
1444 !s
->permissions_start_only
,
1445 !s
->root_directory_start_only
,
1452 service_set_state(s
, SERVICE_STOP_POST
);
1454 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1459 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1460 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1463 static int state_to_kill_operation(ServiceState state
) {
1466 case SERVICE_STOP_SIGABRT
:
1469 case SERVICE_STOP_SIGTERM
:
1470 case SERVICE_FINAL_SIGTERM
:
1471 return KILL_TERMINATE
;
1473 case SERVICE_STOP_SIGKILL
:
1474 case SERVICE_FINAL_SIGKILL
:
1478 return _KILL_OPERATION_INVALID
;
1482 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1487 if (f
!= SERVICE_SUCCESS
)
1490 unit_watch_all_pids(UNIT(s
));
1492 r
= unit_kill_context(
1495 state_to_kill_operation(state
),
1504 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1508 service_set_state(s
, state
);
1509 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1510 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1511 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1512 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1513 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1514 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1516 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1521 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1523 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1524 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1526 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1529 static void service_enter_stop_by_notify(Service
*s
) {
1532 unit_watch_all_pids(UNIT(s
));
1534 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_stop_usec
));
1536 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1537 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1540 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1545 if (f
!= SERVICE_SUCCESS
)
1548 service_unwatch_control_pid(s
);
1549 unit_watch_all_pids(UNIT(s
));
1551 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1552 if (s
->control_command
) {
1553 s
->control_command_id
= SERVICE_EXEC_STOP
;
1555 r
= service_spawn(s
,
1557 s
->timeout_stop_usec
,
1559 !s
->permissions_start_only
,
1560 !s
->root_directory_start_only
,
1567 service_set_state(s
, SERVICE_STOP
);
1569 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1574 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1575 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1578 static bool service_good(Service
*s
) {
1582 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1585 main_pid_ok
= main_pid_good(s
);
1586 if (main_pid_ok
> 0) /* It's alive */
1588 if (main_pid_ok
== 0) /* It's dead */
1591 /* OK, we don't know anything about the main PID, maybe
1592 * because there is none. Let's check the control group
1595 return cgroup_good(s
) != 0;
1598 static void service_enter_running(Service
*s
, ServiceResult f
) {
1601 if (f
!= SERVICE_SUCCESS
)
1604 service_unwatch_control_pid(s
);
1606 if (service_good(s
)) {
1608 /* If there are any queued up sd_notify()
1609 * notifications, process them now */
1610 if (s
->notify_state
== NOTIFY_RELOADING
)
1611 service_enter_reload_by_notify(s
);
1612 else if (s
->notify_state
== NOTIFY_STOPPING
)
1613 service_enter_stop_by_notify(s
);
1615 service_set_state(s
, SERVICE_RUNNING
);
1616 service_arm_timer(s
, usec_add(UNIT(s
)->active_enter_timestamp
.monotonic
, s
->runtime_max_usec
));
1619 } else if (s
->remain_after_exit
)
1620 service_set_state(s
, SERVICE_EXITED
);
1622 service_enter_stop(s
, SERVICE_SUCCESS
);
1625 static void service_enter_start_post(Service
*s
) {
1629 service_unwatch_control_pid(s
);
1630 service_reset_watchdog(s
);
1632 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1633 if (s
->control_command
) {
1634 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1636 r
= service_spawn(s
,
1638 s
->timeout_start_usec
,
1640 !s
->permissions_start_only
,
1641 !s
->root_directory_start_only
,
1648 service_set_state(s
, SERVICE_START_POST
);
1650 service_enter_running(s
, SERVICE_SUCCESS
);
1655 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1656 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1659 static void service_kill_control_processes(Service
*s
) {
1662 if (!UNIT(s
)->cgroup_path
)
1665 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1666 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1669 static void service_enter_start(Service
*s
) {
1677 service_unwatch_control_pid(s
);
1678 service_unwatch_main_pid(s
);
1680 /* We want to ensure that nobody leaks processes from
1681 * START_PRE here, so let's go on a killing spree, People
1682 * should not spawn long running processes from START_PRE. */
1683 service_kill_control_processes(s
);
1685 if (s
->type
== SERVICE_FORKING
) {
1686 s
->control_command_id
= SERVICE_EXEC_START
;
1687 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1689 s
->main_command
= NULL
;
1691 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1692 s
->control_command
= NULL
;
1694 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1698 assert(s
->type
== SERVICE_ONESHOT
);
1699 service_enter_start_post(s
);
1703 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
))
1704 /* For simple + idle this is the main process. We don't apply any timeout here, but
1705 * service_enter_running() will later apply the .runtime_max_usec timeout. */
1706 timeout
= USEC_INFINITY
;
1708 timeout
= s
->timeout_start_usec
;
1710 r
= service_spawn(s
,
1722 if (IN_SET(s
->type
, SERVICE_SIMPLE
, SERVICE_IDLE
)) {
1723 /* For simple services we immediately start
1724 * the START_POST binaries. */
1726 service_set_main_pid(s
, pid
);
1727 service_enter_start_post(s
);
1729 } else if (s
->type
== SERVICE_FORKING
) {
1731 /* For forking services we wait until the start
1732 * process exited. */
1734 s
->control_pid
= pid
;
1735 service_set_state(s
, SERVICE_START
);
1737 } else if (IN_SET(s
->type
, SERVICE_ONESHOT
, SERVICE_DBUS
, SERVICE_NOTIFY
)) {
1739 /* For oneshot services we wait until the start
1740 * process exited, too, but it is our main process. */
1742 /* For D-Bus services we know the main pid right away,
1743 * but wait for the bus name to appear on the
1744 * bus. Notify services are similar. */
1746 service_set_main_pid(s
, pid
);
1747 service_set_state(s
, SERVICE_START
);
1749 assert_not_reached("Unknown service type");
1754 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1755 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1758 static void service_enter_start_pre(Service
*s
) {
1763 service_unwatch_control_pid(s
);
1765 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1766 if (s
->control_command
) {
1767 /* Before we start anything, let's clear up what might
1768 * be left from previous runs. */
1769 service_kill_control_processes(s
);
1771 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1773 r
= service_spawn(s
,
1775 s
->timeout_start_usec
,
1777 !s
->permissions_start_only
,
1778 !s
->root_directory_start_only
,
1785 service_set_state(s
, SERVICE_START_PRE
);
1787 service_enter_start(s
);
1792 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1793 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1796 static void service_enter_restart(Service
*s
) {
1797 _cleanup_(sd_bus_error_free
) sd_bus_error error
= SD_BUS_ERROR_NULL
;
1802 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1803 /* Don't restart things if we are going down anyway */
1804 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1806 r
= service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->restart_usec
));
1813 /* Any units that are bound to this service must also be
1814 * restarted. We use JOB_RESTART (instead of the more obvious
1815 * JOB_START) here so that those dependency jobs will be added
1817 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, &error
, NULL
);
1821 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1822 * it will be canceled as part of the service_stop() call that
1823 * is executed as part of JOB_RESTART. */
1825 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1829 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1830 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1833 static void service_enter_reload_by_notify(Service
*s
) {
1836 service_arm_timer(s
, usec_add(now(CLOCK_MONOTONIC
), s
->timeout_start_usec
));
1837 service_set_state(s
, SERVICE_RELOAD
);
1840 static void service_enter_reload(Service
*s
) {
1845 service_unwatch_control_pid(s
);
1846 s
->reload_result
= SERVICE_SUCCESS
;
1848 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1849 if (s
->control_command
) {
1850 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1852 r
= service_spawn(s
,
1854 s
->timeout_start_usec
,
1856 !s
->permissions_start_only
,
1857 !s
->root_directory_start_only
,
1864 service_set_state(s
, SERVICE_RELOAD
);
1866 service_enter_running(s
, SERVICE_SUCCESS
);
1871 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1872 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1873 service_enter_running(s
, SERVICE_SUCCESS
);
1876 static void service_run_next_control(Service
*s
) {
1881 assert(s
->control_command
);
1882 assert(s
->control_command
->command_next
);
1884 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1886 s
->control_command
= s
->control_command
->command_next
;
1887 service_unwatch_control_pid(s
);
1889 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
1890 timeout
= s
->timeout_start_usec
;
1892 timeout
= s
->timeout_stop_usec
;
1894 r
= service_spawn(s
,
1898 !s
->permissions_start_only
,
1899 !s
->root_directory_start_only
,
1900 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1901 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1910 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1912 if (s
->state
== SERVICE_START_PRE
)
1913 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1914 else if (s
->state
== SERVICE_STOP
)
1915 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1916 else if (s
->state
== SERVICE_STOP_POST
)
1917 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1918 else if (s
->state
== SERVICE_RELOAD
) {
1919 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1920 service_enter_running(s
, SERVICE_SUCCESS
);
1922 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1925 static void service_run_next_main(Service
*s
) {
1930 assert(s
->main_command
);
1931 assert(s
->main_command
->command_next
);
1932 assert(s
->type
== SERVICE_ONESHOT
);
1934 s
->main_command
= s
->main_command
->command_next
;
1935 service_unwatch_main_pid(s
);
1937 r
= service_spawn(s
,
1939 s
->timeout_start_usec
,
1949 service_set_main_pid(s
, pid
);
1954 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1955 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1958 static int service_start(Unit
*u
) {
1959 Service
*s
= SERVICE(u
);
1963 /* We cannot fulfill this request right now, try again later
1965 if (IN_SET(s
->state
,
1966 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1967 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1970 /* Already on it! */
1971 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1974 /* A service that will be restarted must be stopped first to
1975 * trigger BindsTo and/or OnFailure dependencies. If a user
1976 * does not want to wait for the holdoff time to elapse, the
1977 * service should be manually restarted, not started. We
1978 * simply return EAGAIN here, so that any start jobs stay
1979 * queued, and assume that the auto restart timer will
1980 * eventually trigger the restart. */
1981 if (s
->state
== SERVICE_AUTO_RESTART
)
1984 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1986 s
->result
= SERVICE_SUCCESS
;
1987 s
->reload_result
= SERVICE_SUCCESS
;
1988 s
->main_pid_known
= false;
1989 s
->main_pid_alien
= false;
1990 s
->forbid_restart
= false;
1991 s
->reset_cpu_usage
= true;
1993 s
->status_text
= mfree(s
->status_text
);
1994 s
->status_errno
= 0;
1996 s
->notify_state
= NOTIFY_UNKNOWN
;
1998 service_enter_start_pre(s
);
2002 static int service_stop(Unit
*u
) {
2003 Service
*s
= SERVICE(u
);
2007 /* Don't create restart jobs from manual stops. */
2008 s
->forbid_restart
= true;
2011 if (IN_SET(s
->state
,
2012 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2013 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2016 /* A restart will be scheduled or is in progress. */
2017 if (s
->state
== SERVICE_AUTO_RESTART
) {
2018 service_set_state(s
, SERVICE_DEAD
);
2022 /* If there's already something running we go directly into
2024 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2025 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2029 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2031 service_enter_stop(s
, SERVICE_SUCCESS
);
2035 static int service_reload(Unit
*u
) {
2036 Service
*s
= SERVICE(u
);
2040 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2042 service_enter_reload(s
);
2046 _pure_
static bool service_can_reload(Unit
*u
) {
2047 Service
*s
= SERVICE(u
);
2051 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2054 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2055 Service
*s
= SERVICE(u
);
2063 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2064 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2065 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2067 if (s
->control_pid
> 0)
2068 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2070 if (s
->main_pid_known
&& s
->main_pid
> 0)
2071 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2073 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2074 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2075 unit_serialize_item(u
, f
, "bus-name-owner", s
->bus_name_owner
);
2077 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2081 /* FIXME: There's a minor uncleanliness here: if there are
2082 * multiple commands attached here, we will start from the
2083 * first one again */
2084 if (s
->control_command_id
>= 0)
2085 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2087 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2090 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2093 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2097 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2101 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2102 _cleanup_free_
char *c
= NULL
;
2105 copy
= fdset_put_dup(fds
, fs
->fd
);
2109 c
= cescape(fs
->fdname
);
2111 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2114 if (s
->main_exec_status
.pid
> 0) {
2115 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2116 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2117 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2119 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2120 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2121 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2125 dual_timestamp_serialize(f
, "watchdog-timestamp", &s
->watchdog_timestamp
);
2127 unit_serialize_item(u
, f
, "forbid-restart", yes_no(s
->forbid_restart
));
2132 static int service_deserialize_item(Unit
*u
, const char *key
, const char *value
, FDSet
*fds
) {
2133 Service
*s
= SERVICE(u
);
2141 if (streq(key
, "state")) {
2144 state
= service_state_from_string(value
);
2146 log_unit_debug(u
, "Failed to parse state value: %s", value
);
2148 s
->deserialized_state
= state
;
2149 } else if (streq(key
, "result")) {
2152 f
= service_result_from_string(value
);
2154 log_unit_debug(u
, "Failed to parse result value: %s", value
);
2155 else if (f
!= SERVICE_SUCCESS
)
2158 } else if (streq(key
, "reload-result")) {
2161 f
= service_result_from_string(value
);
2163 log_unit_debug(u
, "Failed to parse reload result value: %s", value
);
2164 else if (f
!= SERVICE_SUCCESS
)
2165 s
->reload_result
= f
;
2167 } else if (streq(key
, "control-pid")) {
2170 if (parse_pid(value
, &pid
) < 0)
2171 log_unit_debug(u
, "Failed to parse control-pid value: %s", value
);
2173 s
->control_pid
= pid
;
2174 } else if (streq(key
, "main-pid")) {
2177 if (parse_pid(value
, &pid
) < 0)
2178 log_unit_debug(u
, "Failed to parse main-pid value: %s", value
);
2180 service_set_main_pid(s
, pid
);
2181 unit_watch_pid(UNIT(s
), pid
);
2183 } else if (streq(key
, "main-pid-known")) {
2186 b
= parse_boolean(value
);
2188 log_unit_debug(u
, "Failed to parse main-pid-known value: %s", value
);
2190 s
->main_pid_known
= b
;
2191 } else if (streq(key
, "bus-name-good")) {
2194 b
= parse_boolean(value
);
2196 log_unit_debug(u
, "Failed to parse bus-name-good value: %s", value
);
2198 s
->bus_name_good
= b
;
2199 } else if (streq(key
, "bus-name-owner")) {
2200 r
= free_and_strdup(&s
->bus_name_owner
, value
);
2202 log_unit_error_errno(u
, r
, "Unable to deserialize current bus owner %s: %m", value
);
2203 } else if (streq(key
, "status-text")) {
2206 r
= cunescape(value
, 0, &t
);
2208 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2210 free(s
->status_text
);
2214 } else if (streq(key
, "control-command")) {
2215 ServiceExecCommand id
;
2217 id
= service_exec_command_from_string(value
);
2219 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2221 s
->control_command_id
= id
;
2222 s
->control_command
= s
->exec_command
[id
];
2224 } else if (streq(key
, "socket-fd")) {
2227 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2228 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2230 asynchronous_close(s
->socket_fd
);
2231 s
->socket_fd
= fdset_remove(fds
, fd
);
2233 } else if (streq(key
, "fd-store-fd")) {
2238 pf
= strcspn(value
, WHITESPACE
);
2239 fdv
= strndupa(value
, pf
);
2241 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2242 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2244 _cleanup_free_
char *t
= NULL
;
2248 fdn
+= strspn(fdn
, WHITESPACE
);
2249 (void) cunescape(fdn
, 0, &t
);
2251 r
= service_add_fd_store(s
, fd
, t
);
2253 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2255 fdset_remove(fds
, fd
);
2258 } else if (streq(key
, "main-exec-status-pid")) {
2261 if (parse_pid(value
, &pid
) < 0)
2262 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2264 s
->main_exec_status
.pid
= pid
;
2265 } else if (streq(key
, "main-exec-status-code")) {
2268 if (safe_atoi(value
, &i
) < 0)
2269 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2271 s
->main_exec_status
.code
= i
;
2272 } else if (streq(key
, "main-exec-status-status")) {
2275 if (safe_atoi(value
, &i
) < 0)
2276 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2278 s
->main_exec_status
.status
= i
;
2279 } else if (streq(key
, "main-exec-status-start"))
2280 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2281 else if (streq(key
, "main-exec-status-exit"))
2282 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2283 else if (streq(key
, "watchdog-timestamp"))
2284 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2285 else if (streq(key
, "forbid-restart")) {
2288 b
= parse_boolean(value
);
2290 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2292 s
->forbid_restart
= b
;
2293 } else if (streq(key
, "stdin-fd")) {
2296 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2297 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2299 asynchronous_close(s
->stdin_fd
);
2300 s
->stdin_fd
= fdset_remove(fds
, fd
);
2301 s
->exec_context
.stdio_as_fds
= true;
2303 } else if (streq(key
, "stdout-fd")) {
2306 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2307 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2309 asynchronous_close(s
->stdout_fd
);
2310 s
->stdout_fd
= fdset_remove(fds
, fd
);
2311 s
->exec_context
.stdio_as_fds
= true;
2313 } else if (streq(key
, "stderr-fd")) {
2316 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2317 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2319 asynchronous_close(s
->stderr_fd
);
2320 s
->stderr_fd
= fdset_remove(fds
, fd
);
2321 s
->exec_context
.stdio_as_fds
= true;
2324 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2329 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2330 const UnitActiveState
*table
;
2334 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2336 return table
[SERVICE(u
)->state
];
2339 static const char *service_sub_state_to_string(Unit
*u
) {
2342 return service_state_to_string(SERVICE(u
)->state
);
2345 static bool service_check_gc(Unit
*u
) {
2346 Service
*s
= SERVICE(u
);
2350 /* Never clean up services that still have a process around,
2351 * even if the service is formally dead. */
2352 if (cgroup_good(s
) > 0 ||
2353 main_pid_good(s
) > 0 ||
2354 control_pid_good(s
) > 0)
2360 static int service_retry_pid_file(Service
*s
) {
2363 assert(s
->pid_file
);
2364 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2366 r
= service_load_pid_file(s
, false);
2370 service_unwatch_pid_file(s
);
2372 service_enter_running(s
, SERVICE_SUCCESS
);
2376 static int service_watch_pid_file(Service
*s
) {
2379 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2381 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2385 /* the pidfile might have appeared just before we set the watch */
2386 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2387 service_retry_pid_file(s
);
2391 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2392 service_unwatch_pid_file(s
);
2396 static int service_demand_pid_file(Service
*s
) {
2399 assert(s
->pid_file
);
2400 assert(!s
->pid_file_pathspec
);
2402 ps
= new0(PathSpec
, 1);
2407 ps
->path
= strdup(s
->pid_file
);
2413 path_kill_slashes(ps
->path
);
2415 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2416 * keep their PID file open all the time. */
2417 ps
->type
= PATH_MODIFIED
;
2418 ps
->inotify_fd
= -1;
2420 s
->pid_file_pathspec
= ps
;
2422 return service_watch_pid_file(s
);
2425 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2426 PathSpec
*p
= userdata
;
2431 s
= SERVICE(p
->unit
);
2435 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2436 assert(s
->pid_file_pathspec
);
2437 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2439 log_unit_debug(UNIT(s
), "inotify event");
2441 if (path_spec_fd_event(p
, events
) < 0)
2444 if (service_retry_pid_file(s
) == 0)
2447 if (service_watch_pid_file(s
) < 0)
2453 service_unwatch_pid_file(s
);
2454 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2458 static void service_notify_cgroup_empty_event(Unit
*u
) {
2459 Service
*s
= SERVICE(u
);
2463 log_unit_debug(u
, "cgroup is empty");
2467 /* Waiting for SIGCHLD is usually more interesting,
2468 * because it includes return codes/signals. Which is
2469 * why we ignore the cgroup events for most cases,
2470 * except when we don't know pid which to expect the
2474 case SERVICE_START_POST
:
2475 /* If we were hoping for the daemon to write its PID file,
2476 * we can give up now. */
2477 if (s
->pid_file_pathspec
) {
2478 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2480 service_unwatch_pid_file(s
);
2481 if (s
->state
== SERVICE_START
)
2482 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2484 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2488 case SERVICE_RUNNING
:
2489 /* service_enter_running() will figure out what to do */
2490 service_enter_running(s
, SERVICE_SUCCESS
);
2493 case SERVICE_STOP_SIGABRT
:
2494 case SERVICE_STOP_SIGTERM
:
2495 case SERVICE_STOP_SIGKILL
:
2497 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2498 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2502 case SERVICE_STOP_POST
:
2503 case SERVICE_FINAL_SIGTERM
:
2504 case SERVICE_FINAL_SIGKILL
:
2505 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2506 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2515 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2516 Service
*s
= SERVICE(u
);
2522 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2523 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2524 f
= SERVICE_SUCCESS
;
2525 else if (code
== CLD_EXITED
)
2526 f
= SERVICE_FAILURE_EXIT_CODE
;
2527 else if (code
== CLD_KILLED
)
2528 f
= SERVICE_FAILURE_SIGNAL
;
2529 else if (code
== CLD_DUMPED
)
2530 f
= SERVICE_FAILURE_CORE_DUMP
;
2532 assert_not_reached("Unknown code");
2534 if (s
->main_pid
== pid
) {
2535 /* Forking services may occasionally move to a new PID.
2536 * As long as they update the PID file before exiting the old
2537 * PID, they're fine. */
2538 if (service_load_pid_file(s
, false) == 0)
2542 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2544 if (s
->main_command
) {
2545 /* If this is not a forking service than the
2546 * main process got started and hence we copy
2547 * the exit status so that it is recorded both
2548 * as main and as control process exit
2551 s
->main_command
->exec_status
= s
->main_exec_status
;
2553 if (s
->main_command
->ignore
)
2554 f
= SERVICE_SUCCESS
;
2555 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2557 /* If this is a forked process, then we should
2558 * ignore the return value if this was
2559 * configured for the starter process */
2561 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2562 f
= SERVICE_SUCCESS
;
2565 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2567 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2568 sigchld_code_to_string(code
), status
,
2569 strna(code
== CLD_EXITED
2570 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2571 : signal_to_string(status
))),
2572 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2573 "EXIT_STATUS=%i", status
,
2576 if (f
!= SERVICE_SUCCESS
)
2579 if (s
->main_command
&&
2580 s
->main_command
->command_next
&&
2581 f
== SERVICE_SUCCESS
) {
2583 /* There is another command to *
2584 * execute, so let's do that. */
2586 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2587 service_run_next_main(s
);
2591 /* The service exited, so the service is officially
2593 s
->main_command
= NULL
;
2597 case SERVICE_START_POST
:
2598 case SERVICE_RELOAD
:
2600 /* Need to wait until the operation is
2605 if (s
->type
== SERVICE_ONESHOT
) {
2606 /* This was our main goal, so let's go on */
2607 if (f
== SERVICE_SUCCESS
)
2608 service_enter_start_post(s
);
2610 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2616 case SERVICE_RUNNING
:
2617 service_enter_running(s
, f
);
2620 case SERVICE_STOP_SIGABRT
:
2621 case SERVICE_STOP_SIGTERM
:
2622 case SERVICE_STOP_SIGKILL
:
2624 if (!control_pid_good(s
))
2625 service_enter_stop_post(s
, f
);
2627 /* If there is still a control process, wait for that first */
2630 case SERVICE_STOP_POST
:
2631 case SERVICE_FINAL_SIGTERM
:
2632 case SERVICE_FINAL_SIGKILL
:
2634 if (!control_pid_good(s
))
2635 service_enter_dead(s
, f
, true);
2639 assert_not_reached("Uh, main process died at wrong time.");
2643 } else if (s
->control_pid
== pid
) {
2646 if (s
->control_command
) {
2647 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2649 if (s
->control_command
->ignore
)
2650 f
= SERVICE_SUCCESS
;
2653 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2654 "Control process exited, code=%s status=%i",
2655 sigchld_code_to_string(code
), status
);
2657 if (f
!= SERVICE_SUCCESS
)
2660 /* Immediately get rid of the cgroup, so that the
2661 * kernel doesn't delay the cgroup empty messages for
2662 * the service cgroup any longer than necessary */
2663 service_kill_control_processes(s
);
2665 if (s
->control_command
&&
2666 s
->control_command
->command_next
&&
2667 f
== SERVICE_SUCCESS
) {
2669 /* There is another command to *
2670 * execute, so let's do that. */
2672 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2673 service_run_next_control(s
);
2676 /* No further commands for this step, so let's
2677 * figure out what to do next */
2679 s
->control_command
= NULL
;
2680 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2682 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2686 case SERVICE_START_PRE
:
2687 if (f
== SERVICE_SUCCESS
)
2688 service_enter_start(s
);
2690 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2694 if (s
->type
!= SERVICE_FORKING
)
2695 /* Maybe spurious event due to a reload that changed the type? */
2698 if (f
!= SERVICE_SUCCESS
) {
2699 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2704 bool has_start_post
;
2707 /* Let's try to load the pid file here if we can.
2708 * The PID file might actually be created by a START_POST
2709 * script. In that case don't worry if the loading fails. */
2711 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2712 r
= service_load_pid_file(s
, !has_start_post
);
2713 if (!has_start_post
&& r
< 0) {
2714 r
= service_demand_pid_file(s
);
2715 if (r
< 0 || !cgroup_good(s
))
2716 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2720 service_search_main_pid(s
);
2722 service_enter_start_post(s
);
2725 case SERVICE_START_POST
:
2726 if (f
!= SERVICE_SUCCESS
) {
2727 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2734 r
= service_load_pid_file(s
, true);
2736 r
= service_demand_pid_file(s
);
2737 if (r
< 0 || !cgroup_good(s
))
2738 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2742 service_search_main_pid(s
);
2744 service_enter_running(s
, SERVICE_SUCCESS
);
2747 case SERVICE_RELOAD
:
2748 if (f
== SERVICE_SUCCESS
)
2749 if (service_load_pid_file(s
, true) < 0)
2750 service_search_main_pid(s
);
2752 s
->reload_result
= f
;
2753 service_enter_running(s
, SERVICE_SUCCESS
);
2757 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2760 case SERVICE_STOP_SIGABRT
:
2761 case SERVICE_STOP_SIGTERM
:
2762 case SERVICE_STOP_SIGKILL
:
2763 if (main_pid_good(s
) <= 0)
2764 service_enter_stop_post(s
, f
);
2766 /* If there is still a service
2767 * process around, wait until
2768 * that one quit, too */
2771 case SERVICE_STOP_POST
:
2772 case SERVICE_FINAL_SIGTERM
:
2773 case SERVICE_FINAL_SIGKILL
:
2774 if (main_pid_good(s
) <= 0)
2775 service_enter_dead(s
, f
, true);
2779 assert_not_reached("Uh, control process died at wrong time.");
2784 /* Notify clients about changed exit status */
2785 unit_add_to_dbus_queue(u
);
2787 /* We got one SIGCHLD for the service, let's watch all
2788 * processes that are now running of the service, and watch
2789 * that. Among the PIDs we then watch will be children
2790 * reassigned to us, which hopefully allows us to identify
2791 * when all children are gone */
2792 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2793 unit_watch_all_pids(u
);
2795 /* If the PID set is empty now, then let's finish this off */
2796 if (set_isempty(u
->pids
))
2797 service_notify_cgroup_empty_event(u
);
2800 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2801 Service
*s
= SERVICE(userdata
);
2804 assert(source
== s
->timer_event_source
);
2808 case SERVICE_START_PRE
:
2810 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2811 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2814 case SERVICE_START_POST
:
2815 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2816 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2819 case SERVICE_RUNNING
:
2820 log_unit_warning(UNIT(s
), "Service reached runtime time limit. Stopping.");
2821 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2824 case SERVICE_RELOAD
:
2825 log_unit_warning(UNIT(s
), "Reload operation timed out. Killing reload process.");
2826 service_kill_control_processes(s
);
2827 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2828 service_enter_running(s
, SERVICE_SUCCESS
);
2832 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2833 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2836 case SERVICE_STOP_SIGABRT
:
2837 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2838 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2841 case SERVICE_STOP_SIGTERM
:
2842 if (s
->kill_context
.send_sigkill
) {
2843 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2844 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2846 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2847 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2852 case SERVICE_STOP_SIGKILL
:
2853 /* Uh, we sent a SIGKILL and it is still not gone?
2854 * Must be something we cannot kill, so let's just be
2855 * weirded out and continue */
2857 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2858 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2861 case SERVICE_STOP_POST
:
2862 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2863 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2866 case SERVICE_FINAL_SIGTERM
:
2867 if (s
->kill_context
.send_sigkill
) {
2868 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2869 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2871 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2872 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2877 case SERVICE_FINAL_SIGKILL
:
2878 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2879 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2882 case SERVICE_AUTO_RESTART
:
2883 log_unit_info(UNIT(s
),
2884 s
->restart_usec
> 0 ?
2885 "Service hold-off time over, scheduling restart." :
2886 "Service has no hold-off time, scheduling restart.");
2887 service_enter_restart(s
);
2891 assert_not_reached("Timeout at wrong time.");
2897 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2898 Service
*s
= SERVICE(userdata
);
2899 char t
[FORMAT_TIMESPAN_MAX
];
2902 assert(source
== s
->watchdog_event_source
);
2904 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2905 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2907 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2912 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2913 Service
*s
= SERVICE(u
);
2914 _cleanup_free_
char *cc
= NULL
;
2915 bool notify_dbus
= false;
2920 cc
= strv_join(tags
, ", ");
2922 if (s
->notify_access
== NOTIFY_NONE
) {
2923 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2925 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2926 if (s
->main_pid
!= 0)
2927 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2929 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2932 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2934 /* Interpret MAINPID= */
2935 e
= strv_find_startswith(tags
, "MAINPID=");
2936 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2937 if (parse_pid(e
, &pid
) < 0)
2938 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2940 service_set_main_pid(s
, pid
);
2941 unit_watch_pid(UNIT(s
), pid
);
2946 /* Interpret RELOADING= */
2947 if (strv_find(tags
, "RELOADING=1")) {
2949 s
->notify_state
= NOTIFY_RELOADING
;
2951 if (s
->state
== SERVICE_RUNNING
)
2952 service_enter_reload_by_notify(s
);
2957 /* Interpret READY= */
2958 if (strv_find(tags
, "READY=1")) {
2960 s
->notify_state
= NOTIFY_READY
;
2962 /* Type=notify services inform us about completed
2963 * initialization with READY=1 */
2964 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2965 service_enter_start_post(s
);
2967 /* Sending READY=1 while we are reloading informs us
2968 * that the reloading is complete */
2969 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2970 service_enter_running(s
, SERVICE_SUCCESS
);
2975 /* Interpret STOPPING= */
2976 if (strv_find(tags
, "STOPPING=1")) {
2978 s
->notify_state
= NOTIFY_STOPPING
;
2980 if (s
->state
== SERVICE_RUNNING
)
2981 service_enter_stop_by_notify(s
);
2986 /* Interpret STATUS= */
2987 e
= strv_find_startswith(tags
, "STATUS=");
2989 _cleanup_free_
char *t
= NULL
;
2992 if (!utf8_is_valid(e
))
2993 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3001 if (!streq_ptr(s
->status_text
, t
)) {
3003 free(s
->status_text
);
3011 /* Interpret ERRNO= */
3012 e
= strv_find_startswith(tags
, "ERRNO=");
3016 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3017 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3019 if (s
->status_errno
!= status_errno
) {
3020 s
->status_errno
= status_errno
;
3026 /* Interpret WATCHDOG= */
3027 if (strv_find(tags
, "WATCHDOG=1"))
3028 service_reset_watchdog(s
);
3030 if (strv_find(tags
, "FDSTORE=1")) {
3033 name
= strv_find_startswith(tags
, "FDNAME=");
3034 if (name
&& !fdname_is_valid(name
)) {
3035 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3039 service_add_fd_store_set(s
, fds
, name
);
3042 /* Notify clients about changed status or main pid */
3044 unit_add_to_dbus_queue(u
);
3047 static int service_get_timeout(Unit
*u
, usec_t
*timeout
) {
3048 Service
*s
= SERVICE(u
);
3052 if (!s
->timer_event_source
)
3055 r
= sd_event_source_get_time(s
->timer_event_source
, &t
);
3058 if (t
== USEC_INFINITY
)
3065 static void service_bus_name_owner_change(
3068 const char *old_owner
,
3069 const char *new_owner
) {
3071 Service
*s
= SERVICE(u
);
3077 assert(streq(s
->bus_name
, name
));
3078 assert(old_owner
|| new_owner
);
3080 if (old_owner
&& new_owner
)
3081 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3083 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3085 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3087 s
->bus_name_good
= !!new_owner
;
3089 /* Track the current owner, so we can reconstruct changes after a daemon reload */
3090 r
= free_and_strdup(&s
->bus_name_owner
, new_owner
);
3092 log_unit_error_errno(u
, r
, "Unable to set new bus name owner %s: %m", new_owner
);
3096 if (s
->type
== SERVICE_DBUS
) {
3098 /* service_enter_running() will figure out what to
3100 if (s
->state
== SERVICE_RUNNING
)
3101 service_enter_running(s
, SERVICE_SUCCESS
);
3102 else if (s
->state
== SERVICE_START
&& new_owner
)
3103 service_enter_start_post(s
);
3105 } else if (new_owner
&&
3107 (s
->state
== SERVICE_START
||
3108 s
->state
== SERVICE_START_POST
||
3109 s
->state
== SERVICE_RUNNING
||
3110 s
->state
== SERVICE_RELOAD
)) {
3112 _cleanup_(sd_bus_creds_unrefp
) sd_bus_creds
*creds
= NULL
;
3115 /* Try to acquire PID from bus service */
3117 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3119 r
= sd_bus_creds_get_pid(creds
, &pid
);
3121 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3123 service_set_main_pid(s
, pid
);
3124 unit_watch_pid(UNIT(s
), pid
);
3129 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3130 _cleanup_free_
char *peer
= NULL
;
3136 /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs
3137 * to be configured. We take ownership of the passed fd on success. */
3139 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3142 if (s
->socket_fd
>= 0)
3145 if (s
->state
!= SERVICE_DEAD
)
3148 if (getpeername_pretty(fd
, true, &peer
) >= 0) {
3150 if (UNIT(s
)->description
) {
3151 _cleanup_free_
char *a
;
3153 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3157 r
= unit_set_description(UNIT(s
), a
);
3159 r
= unit_set_description(UNIT(s
), peer
);
3165 r
= unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3170 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3172 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3176 static void service_reset_failed(Unit
*u
) {
3177 Service
*s
= SERVICE(u
);
3181 if (s
->state
== SERVICE_FAILED
)
3182 service_set_state(s
, SERVICE_DEAD
);
3184 s
->result
= SERVICE_SUCCESS
;
3185 s
->reload_result
= SERVICE_SUCCESS
;
3188 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3189 Service
*s
= SERVICE(u
);
3191 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3194 static int service_main_pid(Unit
*u
) {
3195 Service
*s
= SERVICE(u
);
3202 static int service_control_pid(Unit
*u
) {
3203 Service
*s
= SERVICE(u
);
3207 return s
->control_pid
;
3210 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3211 [SERVICE_RESTART_NO
] = "no",
3212 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3213 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3214 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3215 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3216 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3217 [SERVICE_RESTART_ALWAYS
] = "always",
3220 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3222 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3223 [SERVICE_SIMPLE
] = "simple",
3224 [SERVICE_FORKING
] = "forking",
3225 [SERVICE_ONESHOT
] = "oneshot",
3226 [SERVICE_DBUS
] = "dbus",
3227 [SERVICE_NOTIFY
] = "notify",
3228 [SERVICE_IDLE
] = "idle"
3231 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3233 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3234 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3235 [SERVICE_EXEC_START
] = "ExecStart",
3236 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3237 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3238 [SERVICE_EXEC_STOP
] = "ExecStop",
3239 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3242 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3244 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3245 [NOTIFY_NONE
] = "none",
3246 [NOTIFY_MAIN
] = "main",
3247 [NOTIFY_ALL
] = "all"
3250 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3252 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3253 [NOTIFY_UNKNOWN
] = "unknown",
3254 [NOTIFY_READY
] = "ready",
3255 [NOTIFY_RELOADING
] = "reloading",
3256 [NOTIFY_STOPPING
] = "stopping",
3259 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3261 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3262 [SERVICE_SUCCESS
] = "success",
3263 [SERVICE_FAILURE_RESOURCES
] = "resources",
3264 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3265 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3266 [SERVICE_FAILURE_SIGNAL
] = "signal",
3267 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3268 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3271 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3273 const UnitVTable service_vtable
= {
3274 .object_size
= sizeof(Service
),
3275 .exec_context_offset
= offsetof(Service
, exec_context
),
3276 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3277 .kill_context_offset
= offsetof(Service
, kill_context
),
3278 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3284 .private_section
= "Service",
3286 .init
= service_init
,
3287 .done
= service_done
,
3288 .load
= service_load
,
3289 .release_resources
= service_release_resources
,
3291 .coldplug
= service_coldplug
,
3293 .dump
= service_dump
,
3295 .start
= service_start
,
3296 .stop
= service_stop
,
3297 .reload
= service_reload
,
3299 .can_reload
= service_can_reload
,
3301 .kill
= service_kill
,
3303 .serialize
= service_serialize
,
3304 .deserialize_item
= service_deserialize_item
,
3306 .active_state
= service_active_state
,
3307 .sub_state_to_string
= service_sub_state_to_string
,
3309 .check_gc
= service_check_gc
,
3311 .sigchld_event
= service_sigchld_event
,
3313 .reset_failed
= service_reset_failed
,
3315 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3316 .notify_message
= service_notify_message
,
3318 .main_pid
= service_main_pid
,
3319 .control_pid
= service_control_pid
,
3321 .bus_name_owner_change
= service_bus_name_owner_change
,
3323 .bus_vtable
= bus_service_vtable
,
3324 .bus_set_property
= bus_service_set_property
,
3325 .bus_commit_properties
= bus_service_commit_properties
,
3327 .get_timeout
= service_get_timeout
,
3328 .can_transient
= true,
3330 .status_message_formats
= {
3331 .starting_stopping
= {
3332 [0] = "Starting %s...",
3333 [1] = "Stopping %s...",
3335 .finished_start_job
= {
3336 [JOB_DONE
] = "Started %s.",
3337 [JOB_FAILED
] = "Failed to start %s.",
3339 .finished_stop_job
= {
3340 [JOB_DONE
] = "Stopped %s.",
3341 [JOB_FAILED
] = "Stopped (with error) %s.",