1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
27 #include "bus-error.h"
28 #include "bus-kernel.h"
30 #include "dbus-service.h"
34 #include "exit-status.h"
36 #include "formats-util.h"
37 #include "load-dropin.h"
38 #include "load-fragment.h"
41 #include "path-util.h"
42 #include "process-util.h"
43 #include "signal-util.h"
45 #include "string-util.h"
47 #include "unit-name.h"
48 #include "unit-printf.h"
54 static const UnitActiveState state_translation_table
[_SERVICE_STATE_MAX
] = {
55 [SERVICE_DEAD
] = UNIT_INACTIVE
,
56 [SERVICE_START_PRE
] = UNIT_ACTIVATING
,
57 [SERVICE_START
] = UNIT_ACTIVATING
,
58 [SERVICE_START_POST
] = UNIT_ACTIVATING
,
59 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
60 [SERVICE_EXITED
] = UNIT_ACTIVE
,
61 [SERVICE_RELOAD
] = UNIT_RELOADING
,
62 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
63 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
64 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
65 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
66 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
67 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
68 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
69 [SERVICE_FAILED
] = UNIT_FAILED
,
70 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
73 /* For Type=idle we never want to delay any other jobs, hence we
74 * consider idle jobs active as soon as we start working on them */
75 static const UnitActiveState state_translation_table_idle
[_SERVICE_STATE_MAX
] = {
76 [SERVICE_DEAD
] = UNIT_INACTIVE
,
77 [SERVICE_START_PRE
] = UNIT_ACTIVE
,
78 [SERVICE_START
] = UNIT_ACTIVE
,
79 [SERVICE_START_POST
] = UNIT_ACTIVE
,
80 [SERVICE_RUNNING
] = UNIT_ACTIVE
,
81 [SERVICE_EXITED
] = UNIT_ACTIVE
,
82 [SERVICE_RELOAD
] = UNIT_RELOADING
,
83 [SERVICE_STOP
] = UNIT_DEACTIVATING
,
84 [SERVICE_STOP_SIGABRT
] = UNIT_DEACTIVATING
,
85 [SERVICE_STOP_SIGTERM
] = UNIT_DEACTIVATING
,
86 [SERVICE_STOP_SIGKILL
] = UNIT_DEACTIVATING
,
87 [SERVICE_STOP_POST
] = UNIT_DEACTIVATING
,
88 [SERVICE_FINAL_SIGTERM
] = UNIT_DEACTIVATING
,
89 [SERVICE_FINAL_SIGKILL
] = UNIT_DEACTIVATING
,
90 [SERVICE_FAILED
] = UNIT_FAILED
,
91 [SERVICE_AUTO_RESTART
] = UNIT_ACTIVATING
94 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
);
95 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
);
96 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
);
98 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
);
99 static void service_enter_reload_by_notify(Service
*s
);
101 static void service_init(Unit
*u
) {
102 Service
*s
= SERVICE(u
);
105 assert(u
->load_state
== UNIT_STUB
);
107 s
->timeout_start_usec
= u
->manager
->default_timeout_start_usec
;
108 s
->timeout_stop_usec
= u
->manager
->default_timeout_stop_usec
;
109 s
->restart_usec
= u
->manager
->default_restart_usec
;
110 s
->type
= _SERVICE_TYPE_INVALID
;
112 s
->bus_endpoint_fd
= -1;
113 s
->stdin_fd
= s
->stdout_fd
= s
->stderr_fd
= -1;
114 s
->guess_main_pid
= true;
116 RATELIMIT_INIT(s
->start_limit
, u
->manager
->default_start_limit_interval
, u
->manager
->default_start_limit_burst
);
118 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
121 static void service_unwatch_control_pid(Service
*s
) {
124 if (s
->control_pid
<= 0)
127 unit_unwatch_pid(UNIT(s
), s
->control_pid
);
131 static void service_unwatch_main_pid(Service
*s
) {
134 if (s
->main_pid
<= 0)
137 unit_unwatch_pid(UNIT(s
), s
->main_pid
);
141 static void service_unwatch_pid_file(Service
*s
) {
142 if (!s
->pid_file_pathspec
)
145 log_unit_debug(UNIT(s
), "Stopping watch for PID file %s", s
->pid_file_pathspec
->path
);
146 path_spec_unwatch(s
->pid_file_pathspec
);
147 path_spec_done(s
->pid_file_pathspec
);
148 s
->pid_file_pathspec
= mfree(s
->pid_file_pathspec
);
151 static int service_set_main_pid(Service
*s
, pid_t pid
) {
162 if (s
->main_pid
== pid
&& s
->main_pid_known
)
165 if (s
->main_pid
!= pid
) {
166 service_unwatch_main_pid(s
);
167 exec_status_start(&s
->main_exec_status
, pid
);
171 s
->main_pid_known
= true;
173 if (get_parent_of_pid(pid
, &ppid
) >= 0 && ppid
!= getpid()) {
174 log_unit_warning(UNIT(s
), "Supervising process "PID_FMT
" which is not our child. We'll most likely not notice when it exits.", pid
);
175 s
->main_pid_alien
= true;
177 s
->main_pid_alien
= false;
182 static void service_close_socket_fd(Service
*s
) {
185 s
->socket_fd
= asynchronous_close(s
->socket_fd
);
188 static void service_connection_unref(Service
*s
) {
191 if (!UNIT_ISSET(s
->accept_socket
))
194 socket_connection_unref(SOCKET(UNIT_DEREF(s
->accept_socket
)));
195 unit_ref_unset(&s
->accept_socket
);
198 static void service_stop_watchdog(Service
*s
) {
201 s
->watchdog_event_source
= sd_event_source_unref(s
->watchdog_event_source
);
202 s
->watchdog_timestamp
= DUAL_TIMESTAMP_NULL
;
205 static void service_start_watchdog(Service
*s
) {
210 if (s
->watchdog_usec
<= 0)
213 if (s
->watchdog_event_source
) {
214 r
= sd_event_source_set_time(s
->watchdog_event_source
, s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
);
216 log_unit_warning_errno(UNIT(s
), r
, "Failed to reset watchdog timer: %m");
220 r
= sd_event_source_set_enabled(s
->watchdog_event_source
, SD_EVENT_ONESHOT
);
222 r
= sd_event_add_time(
223 UNIT(s
)->manager
->event
,
224 &s
->watchdog_event_source
,
226 s
->watchdog_timestamp
.monotonic
+ s
->watchdog_usec
, 0,
227 service_dispatch_watchdog
, s
);
229 log_unit_warning_errno(UNIT(s
), r
, "Failed to add watchdog timer: %m");
233 (void) sd_event_source_set_description(s
->watchdog_event_source
, "service-watchdog");
235 /* Let's process everything else which might be a sign
236 * of living before we consider a service died. */
237 r
= sd_event_source_set_priority(s
->watchdog_event_source
, SD_EVENT_PRIORITY_IDLE
);
241 log_unit_warning_errno(UNIT(s
), r
, "Failed to install watchdog timer: %m");
244 static void service_reset_watchdog(Service
*s
) {
247 dual_timestamp_get(&s
->watchdog_timestamp
);
248 service_start_watchdog(s
);
251 static void service_fd_store_unlink(ServiceFDStore
*fs
) {
257 assert(fs
->service
->n_fd_store
> 0);
258 LIST_REMOVE(fd_store
, fs
->service
->fd_store
, fs
);
259 fs
->service
->n_fd_store
--;
262 if (fs
->event_source
) {
263 sd_event_source_set_enabled(fs
->event_source
, SD_EVENT_OFF
);
264 sd_event_source_unref(fs
->event_source
);
272 static void service_release_resources(Unit
*u
) {
273 Service
*s
= SERVICE(u
);
277 if (!s
->fd_store
&& s
->stdin_fd
< 0 && s
->stdout_fd
< 0 && s
->stderr_fd
< 0)
280 log_unit_debug(u
, "Releasing all resources.");
282 s
->stdin_fd
= safe_close(s
->stdin_fd
);
283 s
->stdout_fd
= safe_close(s
->stdout_fd
);
284 s
->stderr_fd
= safe_close(s
->stderr_fd
);
287 service_fd_store_unlink(s
->fd_store
);
289 assert(s
->n_fd_store
== 0);
292 static void service_done(Unit
*u
) {
293 Service
*s
= SERVICE(u
);
297 s
->pid_file
= mfree(s
->pid_file
);
298 s
->status_text
= mfree(s
->status_text
);
299 s
->reboot_arg
= mfree(s
->reboot_arg
);
301 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
302 exec_command_free_array(s
->exec_command
, _SERVICE_EXEC_COMMAND_MAX
);
303 s
->control_command
= NULL
;
304 s
->main_command
= NULL
;
306 exit_status_set_free(&s
->restart_prevent_status
);
307 exit_status_set_free(&s
->restart_force_status
);
308 exit_status_set_free(&s
->success_status
);
310 /* This will leak a process, but at least no memory or any of
312 service_unwatch_main_pid(s
);
313 service_unwatch_control_pid(s
);
314 service_unwatch_pid_file(s
);
317 unit_unwatch_bus_name(u
, s
->bus_name
);
318 s
->bus_name
= mfree(s
->bus_name
);
321 s
->bus_endpoint_fd
= safe_close(s
->bus_endpoint_fd
);
322 service_close_socket_fd(s
);
323 service_connection_unref(s
);
325 unit_ref_unset(&s
->accept_socket
);
327 service_stop_watchdog(s
);
329 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
331 service_release_resources(u
);
334 static int on_fd_store_io(sd_event_source
*e
, int fd
, uint32_t revents
, void *userdata
) {
335 ServiceFDStore
*fs
= userdata
;
340 /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */
341 service_fd_store_unlink(fs
);
345 static int service_add_fd_store(Service
*s
, int fd
, const char *name
) {
352 if (s
->n_fd_store
>= s
->n_fd_store_max
)
355 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
356 r
= same_fd(fs
->fd
, fd
);
360 /* Already included */
366 fs
= new0(ServiceFDStore
, 1);
372 fs
->fdname
= strdup(name
?: "stored");
378 r
= sd_event_add_io(UNIT(s
)->manager
->event
, &fs
->event_source
, fd
, 0, on_fd_store_io
, fs
);
385 (void) sd_event_source_set_description(fs
->event_source
, "service-fd-store");
387 LIST_PREPEND(fd_store
, s
->fd_store
, fs
);
393 static int service_add_fd_store_set(Service
*s
, FDSet
*fds
, const char *name
) {
398 if (fdset_size(fds
) <= 0)
401 while (s
->n_fd_store
< s
->n_fd_store_max
) {
402 _cleanup_close_
int fd
= -1;
404 fd
= fdset_steal_first(fds
);
408 r
= service_add_fd_store(s
, fd
, name
);
410 return log_unit_error_errno(UNIT(s
), r
, "Couldn't add fd to fd store: %m");
412 log_unit_debug(UNIT(s
), "Added fd to fd store.");
417 if (fdset_size(fds
) > 0)
418 log_unit_warning(UNIT(s
), "Tried to store more fds than FDStoreMax=%u allows, closing remaining.", s
->n_fd_store_max
);
423 static int service_arm_timer(Service
*s
, usec_t usec
) {
428 if (s
->timer_event_source
) {
429 r
= sd_event_source_set_time(s
->timer_event_source
, now(CLOCK_MONOTONIC
) + usec
);
433 return sd_event_source_set_enabled(s
->timer_event_source
, SD_EVENT_ONESHOT
);
436 r
= sd_event_add_time(
437 UNIT(s
)->manager
->event
,
438 &s
->timer_event_source
,
440 now(CLOCK_MONOTONIC
) + usec
, 0,
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.");
508 static int service_add_default_dependencies(Service
*s
) {
513 /* Add a number of automatic dependencies useful for the
514 * majority of services. */
516 /* First, pull in base system */
517 r
= unit_add_two_dependencies_by_name(UNIT(s
), UNIT_AFTER
, UNIT_REQUIRES
, SPECIAL_BASIC_TARGET
, NULL
, true);
521 /* Second, activate normal shutdown */
522 return unit_add_two_dependencies_by_name(UNIT(s
), UNIT_BEFORE
, UNIT_CONFLICTS
, SPECIAL_SHUTDOWN_TARGET
, NULL
, true);
525 static void service_fix_output(Service
*s
) {
528 /* If nothing has been explicitly configured, patch default
529 * output in. If input is socket/tty we avoid this however,
530 * since in that case we want output to default to the same
531 * place as we read input from. */
533 if (s
->exec_context
.std_error
== EXEC_OUTPUT_INHERIT
&&
534 s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
535 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
536 s
->exec_context
.std_error
= UNIT(s
)->manager
->default_std_error
;
538 if (s
->exec_context
.std_output
== EXEC_OUTPUT_INHERIT
&&
539 s
->exec_context
.std_input
== EXEC_INPUT_NULL
)
540 s
->exec_context
.std_output
= UNIT(s
)->manager
->default_std_output
;
543 static int service_add_extras(Service
*s
) {
548 if (s
->type
== _SERVICE_TYPE_INVALID
) {
549 /* Figure out a type automatically */
551 s
->type
= SERVICE_DBUS
;
552 else if (s
->exec_command
[SERVICE_EXEC_START
])
553 s
->type
= SERVICE_SIMPLE
;
555 s
->type
= SERVICE_ONESHOT
;
558 /* Oneshot services have disabled start timeout by default */
559 if (s
->type
== SERVICE_ONESHOT
&& !s
->start_timeout_defined
)
560 s
->timeout_start_usec
= 0;
562 service_fix_output(s
);
564 r
= unit_patch_contexts(UNIT(s
));
568 r
= unit_add_exec_dependencies(UNIT(s
), &s
->exec_context
);
572 r
= unit_set_default_slice(UNIT(s
));
576 if (s
->type
== SERVICE_NOTIFY
&& s
->notify_access
== NOTIFY_NONE
)
577 s
->notify_access
= NOTIFY_MAIN
;
579 if (s
->watchdog_usec
> 0 && s
->notify_access
== NOTIFY_NONE
)
580 s
->notify_access
= NOTIFY_MAIN
;
585 n
= strjoina(s
->bus_name
, ".busname");
586 r
= unit_add_dependency_by_name(UNIT(s
), UNIT_AFTER
, n
, NULL
, true);
590 r
= unit_watch_bus_name(UNIT(s
), s
->bus_name
);
592 return log_unit_error_errno(UNIT(s
), r
, "Two services allocated for the same bus name %s, refusing operation.", s
->bus_name
);
594 return log_unit_error_errno(UNIT(s
), r
, "Cannot watch bus name %s: %m", s
->bus_name
);
597 if (UNIT(s
)->default_dependencies
) {
598 r
= service_add_default_dependencies(s
);
606 static int service_load(Unit
*u
) {
607 Service
*s
= SERVICE(u
);
612 /* Load a .service file */
613 r
= unit_load_fragment(u
);
617 /* Still nothing found? Then let's give up */
618 if (u
->load_state
== UNIT_STUB
)
621 /* This is a new unit? Then let's add in some extras */
622 if (u
->load_state
== UNIT_LOADED
) {
624 /* We were able to load something, then let's add in
625 * the dropin directories. */
626 r
= unit_load_dropin(u
);
630 /* This is a new unit? Then let's add in some
632 r
= service_add_extras(s
);
637 return service_verify(s
);
640 static void service_dump(Unit
*u
, FILE *f
, const char *prefix
) {
641 ServiceExecCommand c
;
642 Service
*s
= SERVICE(u
);
647 prefix
= strempty(prefix
);
648 prefix2
= strjoina(prefix
, "\t");
651 "%sService State: %s\n"
653 "%sReload Result: %s\n"
654 "%sPermissionsStartOnly: %s\n"
655 "%sRootDirectoryStartOnly: %s\n"
656 "%sRemainAfterExit: %s\n"
657 "%sGuessMainPID: %s\n"
660 "%sNotifyAccess: %s\n"
661 "%sNotifyState: %s\n",
662 prefix
, service_state_to_string(s
->state
),
663 prefix
, service_result_to_string(s
->result
),
664 prefix
, service_result_to_string(s
->reload_result
),
665 prefix
, yes_no(s
->permissions_start_only
),
666 prefix
, yes_no(s
->root_directory_start_only
),
667 prefix
, yes_no(s
->remain_after_exit
),
668 prefix
, yes_no(s
->guess_main_pid
),
669 prefix
, service_type_to_string(s
->type
),
670 prefix
, service_restart_to_string(s
->restart
),
671 prefix
, notify_access_to_string(s
->notify_access
),
672 prefix
, notify_state_to_string(s
->notify_state
));
674 if (s
->control_pid
> 0)
676 "%sControl PID: "PID_FMT
"\n",
677 prefix
, s
->control_pid
);
681 "%sMain PID: "PID_FMT
"\n"
682 "%sMain PID Known: %s\n"
683 "%sMain PID Alien: %s\n",
685 prefix
, yes_no(s
->main_pid_known
),
686 prefix
, yes_no(s
->main_pid_alien
));
691 prefix
, s
->pid_file
);
696 "%sBus Name Good: %s\n",
698 prefix
, yes_no(s
->bus_name_good
));
700 kill_context_dump(&s
->kill_context
, f
, prefix
);
701 exec_context_dump(&s
->exec_context
, f
, prefix
);
703 for (c
= 0; c
< _SERVICE_EXEC_COMMAND_MAX
; c
++) {
705 if (!s
->exec_command
[c
])
708 fprintf(f
, "%s-> %s:\n",
709 prefix
, service_exec_command_to_string(c
));
711 exec_command_dump_list(s
->exec_command
[c
], f
, prefix2
);
715 fprintf(f
, "%sStatus Text: %s\n",
716 prefix
, s
->status_text
);
718 if (s
->n_fd_store_max
> 0)
720 "%sFile Descriptor Store Max: %u\n"
721 "%sFile Descriptor Store Current: %u\n",
722 prefix
, s
->n_fd_store_max
,
723 prefix
, s
->n_fd_store
);
726 static int service_load_pid_file(Service
*s
, bool may_warn
) {
727 _cleanup_free_
char *k
= NULL
;
736 r
= read_one_line_file(s
->pid_file
, &k
);
739 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
));
743 r
= parse_pid(k
, &pid
);
746 log_unit_info_errno(UNIT(s
), r
, "Failed to read PID from file %s: %m", s
->pid_file
);
750 if (!pid_is_alive(pid
)) {
752 log_unit_info(UNIT(s
), "PID "PID_FMT
" read from file %s does not exist or is a zombie.", pid
, s
->pid_file
);
756 if (s
->main_pid_known
) {
757 if (pid
== s
->main_pid
)
760 log_unit_debug(UNIT(s
), "Main PID changing: "PID_FMT
" -> "PID_FMT
, s
->main_pid
, pid
);
762 service_unwatch_main_pid(s
);
763 s
->main_pid_known
= false;
765 log_unit_debug(UNIT(s
), "Main PID loaded: "PID_FMT
, pid
);
767 r
= service_set_main_pid(s
, pid
);
771 r
= unit_watch_pid(UNIT(s
), pid
);
773 /* FIXME: we need to do something here */
774 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" for service: %m", pid
);
781 static int service_search_main_pid(Service
*s
) {
787 /* If we know it anyway, don't ever fallback to unreliable
789 if (s
->main_pid_known
)
792 if (!s
->guess_main_pid
)
795 assert(s
->main_pid
<= 0);
797 r
= unit_search_main_pid(UNIT(s
), &pid
);
801 log_unit_debug(UNIT(s
), "Main PID guessed: "PID_FMT
, pid
);
802 r
= service_set_main_pid(s
, pid
);
806 r
= unit_watch_pid(UNIT(s
), pid
);
808 /* FIXME: we need to do something here */
809 log_unit_warning_errno(UNIT(s
), r
, "Failed to watch PID "PID_FMT
" from: %m", pid
);
816 static void service_set_state(Service
*s
, ServiceState state
) {
817 ServiceState old_state
;
818 const UnitActiveState
*table
;
822 table
= s
->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
824 old_state
= s
->state
;
827 service_unwatch_pid_file(s
);
830 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
832 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
833 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
,
834 SERVICE_AUTO_RESTART
))
835 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
838 SERVICE_START
, SERVICE_START_POST
,
839 SERVICE_RUNNING
, SERVICE_RELOAD
,
840 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
841 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
842 service_unwatch_main_pid(s
);
843 s
->main_command
= NULL
;
847 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
849 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
850 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
851 service_unwatch_control_pid(s
);
852 s
->control_command
= NULL
;
853 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
856 if (IN_SET(state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
857 unit_unwatch_all_pids(UNIT(s
));
860 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
861 SERVICE_RUNNING
, SERVICE_RELOAD
,
862 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
863 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
) &&
864 !(state
== SERVICE_DEAD
&& UNIT(s
)->job
)) {
865 service_close_socket_fd(s
);
866 service_connection_unref(s
);
869 if (!IN_SET(state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
870 service_stop_watchdog(s
);
872 /* For the inactive states unit_notify() will trim the cgroup,
873 * but for exit we have to do that ourselves... */
874 if (state
== SERVICE_EXITED
&& UNIT(s
)->manager
->n_reloading
<= 0)
875 unit_prune_cgroup(UNIT(s
));
877 /* For remain_after_exit services, let's see if we can "release" the
878 * hold on the console, since unit_notify() only does that in case of
880 if (state
== SERVICE_EXITED
&&
881 s
->remain_after_exit
&&
882 UNIT(s
)->manager
->n_on_console
> 0) {
886 ec
= unit_get_exec_context(UNIT(s
));
887 if (ec
&& exec_context_may_touch_console(ec
)) {
888 Manager
*m
= UNIT(s
)->manager
;
891 if (m
->n_on_console
== 0)
892 /* unset no_console_output flag, since the console is free */
893 m
->no_console_output
= false;
897 if (old_state
!= state
)
898 log_unit_debug(UNIT(s
), "Changed %s -> %s", service_state_to_string(old_state
), service_state_to_string(state
));
900 unit_notify(UNIT(s
), table
[old_state
], table
[state
], s
->reload_result
== SERVICE_SUCCESS
);
903 static int service_coldplug(Unit
*u
) {
904 Service
*s
= SERVICE(u
);
908 assert(s
->state
== SERVICE_DEAD
);
910 if (s
->deserialized_state
!= s
->state
) {
912 if (IN_SET(s
->deserialized_state
,
913 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
915 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
916 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
920 k
= IN_SET(s
->deserialized_state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
;
922 /* For the start/stop timeouts 0 means off */
924 r
= service_arm_timer(s
, k
);
930 if (s
->deserialized_state
== SERVICE_AUTO_RESTART
) {
932 /* The restart timeouts 0 means immediately */
933 r
= service_arm_timer(s
, s
->restart_usec
);
938 if (pid_is_unwaited(s
->main_pid
) &&
939 ((s
->deserialized_state
== SERVICE_START
&& IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_ONESHOT
, SERVICE_NOTIFY
)) ||
940 IN_SET(s
->deserialized_state
,
941 SERVICE_START
, SERVICE_START_POST
,
942 SERVICE_RUNNING
, SERVICE_RELOAD
,
943 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
944 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))) {
945 r
= unit_watch_pid(UNIT(s
), s
->main_pid
);
950 if (pid_is_unwaited(s
->control_pid
) &&
951 IN_SET(s
->deserialized_state
,
952 SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
,
954 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
955 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
)) {
956 r
= unit_watch_pid(UNIT(s
), s
->control_pid
);
961 if (!IN_SET(s
->deserialized_state
, SERVICE_DEAD
, SERVICE_FAILED
, SERVICE_AUTO_RESTART
))
962 unit_watch_all_pids(UNIT(s
));
964 if (IN_SET(s
->deserialized_state
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
))
965 service_start_watchdog(s
);
967 service_set_state(s
, s
->deserialized_state
);
973 static int service_collect_fds(Service
*s
, int **fds
, char ***fd_names
) {
974 _cleanup_strv_free_
char **rfd_names
= NULL
;
975 _cleanup_free_
int *rfds
= NULL
;
982 if (s
->socket_fd
>= 0) {
984 /* Pass the per-connection socket */
989 rfds
[0] = s
->socket_fd
;
991 rfd_names
= strv_new("connection", NULL
);
1000 /* Pass all our configured sockets for singleton services */
1002 SET_FOREACH(u
, UNIT(s
)->dependencies
[UNIT_TRIGGERED_BY
], i
) {
1003 _cleanup_free_
int *cfds
= NULL
;
1007 if (u
->type
!= UNIT_SOCKET
)
1012 cn_fds
= socket_collect_fds(sock
, &cfds
);
1027 t
= realloc(rfds
, (rn_fds
+ cn_fds
) * sizeof(int));
1031 memcpy(t
+ rn_fds
, cfds
, cn_fds
* sizeof(int));
1037 r
= strv_extend_n(&rfd_names
, socket_fdname(sock
), cn_fds
);
1043 if (s
->n_fd_store
> 0) {
1048 t
= realloc(rfds
, (rn_fds
+ s
->n_fd_store
) * sizeof(int));
1054 nl
= realloc(rfd_names
, (rn_fds
+ s
->n_fd_store
+ 1) * sizeof(char*));
1060 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
1061 rfds
[rn_fds
] = fs
->fd
;
1062 rfd_names
[rn_fds
] = strdup(strempty(fs
->fdname
));
1063 if (!rfd_names
[rn_fds
])
1069 rfd_names
[rn_fds
] = NULL
;
1073 *fd_names
= rfd_names
;
1081 static int service_spawn(
1086 bool apply_permissions
,
1088 bool apply_tty_stdin
,
1092 _cleanup_strv_free_
char **argv
= NULL
, **final_env
= NULL
, **our_env
= NULL
, **fd_names
= NULL
;
1093 _cleanup_free_
char *bus_endpoint_path
= NULL
;
1094 _cleanup_free_
int *fds
= NULL
;
1095 unsigned n_fds
= 0, n_env
= 0;
1099 ExecParameters exec_params
= {
1100 .apply_permissions
= apply_permissions
,
1101 .apply_chroot
= apply_chroot
,
1102 .apply_tty_stdin
= apply_tty_stdin
,
1103 .bus_endpoint_fd
= -1,
1115 (void) unit_realize_cgroup(UNIT(s
));
1116 if (s
->reset_cpu_usage
) {
1117 (void) unit_reset_cpu_usage(UNIT(s
));
1118 s
->reset_cpu_usage
= false;
1121 r
= unit_setup_exec_runtime(UNIT(s
));
1126 s
->exec_context
.std_input
== EXEC_INPUT_SOCKET
||
1127 s
->exec_context
.std_output
== EXEC_OUTPUT_SOCKET
||
1128 s
->exec_context
.std_error
== EXEC_OUTPUT_SOCKET
) {
1130 r
= service_collect_fds(s
, &fds
, &fd_names
);
1138 r
= service_arm_timer(s
, timeout
);
1142 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1144 r
= unit_full_printf_strv(UNIT(s
), c
->argv
, &argv
);
1148 our_env
= new0(char*, 6);
1154 if (is_control
? s
->notify_access
== NOTIFY_ALL
: s
->notify_access
!= NOTIFY_NONE
)
1155 if (asprintf(our_env
+ n_env
++, "NOTIFY_SOCKET=%s", UNIT(s
)->manager
->notify_socket
) < 0) {
1160 if (s
->main_pid
> 0)
1161 if (asprintf(our_env
+ n_env
++, "MAINPID="PID_FMT
, s
->main_pid
) < 0) {
1166 if (UNIT(s
)->manager
->running_as
!= MANAGER_SYSTEM
)
1167 if (asprintf(our_env
+ n_env
++, "MANAGERPID="PID_FMT
, getpid()) < 0) {
1172 if (s
->socket_fd
>= 0) {
1173 union sockaddr_union sa
;
1174 socklen_t salen
= sizeof(sa
);
1176 r
= getpeername(s
->socket_fd
, &sa
.sa
, &salen
);
1182 if (IN_SET(sa
.sa
.sa_family
, AF_INET
, AF_INET6
)) {
1183 _cleanup_free_
char *addr
= NULL
;
1187 r
= sockaddr_pretty(&sa
.sa
, salen
, true, false, &addr
);
1191 t
= strappend("REMOTE_ADDR=", addr
);
1196 our_env
[n_env
++] = t
;
1198 port
= sockaddr_port(&sa
.sa
);
1204 if (asprintf(&t
, "REMOTE_PORT=%u", port
) < 0) {
1208 our_env
[n_env
++] = t
;
1212 final_env
= strv_env_merge(2, UNIT(s
)->manager
->environment
, our_env
, NULL
);
1218 if (is_control
&& UNIT(s
)->cgroup_path
) {
1219 path
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1220 (void) cg_create(SYSTEMD_CGROUP_CONTROLLER
, path
);
1222 path
= UNIT(s
)->cgroup_path
;
1224 if (s
->exec_context
.bus_endpoint
) {
1225 r
= bus_kernel_create_endpoint(UNIT(s
)->manager
->running_as
== MANAGER_SYSTEM
? "system" : "user",
1226 UNIT(s
)->id
, &bus_endpoint_path
);
1230 /* Pass the fd to the exec_params so that the child process can upload the policy.
1231 * Keep a reference to the fd in the service, so the endpoint is kept alive as long
1232 * as the service is running. */
1233 exec_params
.bus_endpoint_fd
= s
->bus_endpoint_fd
= r
;
1236 exec_params
.argv
= argv
;
1237 exec_params
.fds
= fds
;
1238 exec_params
.fd_names
= fd_names
;
1239 exec_params
.n_fds
= n_fds
;
1240 exec_params
.environment
= final_env
;
1241 exec_params
.confirm_spawn
= UNIT(s
)->manager
->confirm_spawn
;
1242 exec_params
.cgroup_supported
= UNIT(s
)->manager
->cgroup_supported
;
1243 exec_params
.cgroup_path
= path
;
1244 exec_params
.cgroup_delegate
= s
->cgroup_context
.delegate
;
1245 exec_params
.runtime_prefix
= manager_get_runtime_prefix(UNIT(s
)->manager
);
1246 exec_params
.watchdog_usec
= s
->watchdog_usec
;
1247 exec_params
.bus_endpoint_path
= bus_endpoint_path
;
1248 exec_params
.selinux_context_net
= s
->socket_fd_selinux_context_net
;
1249 if (s
->type
== SERVICE_IDLE
)
1250 exec_params
.idle_pipe
= UNIT(s
)->manager
->idle_pipe
;
1251 exec_params
.stdin_fd
= s
->stdin_fd
;
1252 exec_params
.stdout_fd
= s
->stdout_fd
;
1253 exec_params
.stderr_fd
= s
->stderr_fd
;
1255 r
= exec_spawn(UNIT(s
),
1264 r
= unit_watch_pid(UNIT(s
), pid
);
1266 /* FIXME: we need to do something here */
1275 s
->timer_event_source
= sd_event_source_unref(s
->timer_event_source
);
1280 static int main_pid_good(Service
*s
) {
1283 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1286 /* If we know the pid file, then let's just check if it is
1288 if (s
->main_pid_known
) {
1290 /* If it's an alien child let's check if it is still
1292 if (s
->main_pid_alien
&& s
->main_pid
> 0)
1293 return pid_is_alive(s
->main_pid
);
1295 /* .. otherwise assume we'll get a SIGCHLD for it,
1296 * which we really should wait for to collect exit
1297 * status and code */
1298 return s
->main_pid
> 0;
1301 /* We don't know the pid */
1305 _pure_
static int control_pid_good(Service
*s
) {
1308 return s
->control_pid
> 0;
1311 static int cgroup_good(Service
*s
) {
1316 if (!UNIT(s
)->cgroup_path
)
1319 r
= cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER
, UNIT(s
)->cgroup_path
);
1326 static bool service_shall_restart(Service
*s
) {
1329 /* Don't restart after manual stops */
1330 if (s
->forbid_restart
)
1333 /* Never restart if this is configured as special exception */
1334 if (exit_status_set_test(&s
->restart_prevent_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1337 /* Restart if the exit code/status are configured as restart triggers */
1338 if (exit_status_set_test(&s
->restart_force_status
, s
->main_exec_status
.code
, s
->main_exec_status
.status
))
1341 switch (s
->restart
) {
1343 case SERVICE_RESTART_NO
:
1346 case SERVICE_RESTART_ALWAYS
:
1349 case SERVICE_RESTART_ON_SUCCESS
:
1350 return s
->result
== SERVICE_SUCCESS
;
1352 case SERVICE_RESTART_ON_FAILURE
:
1353 return s
->result
!= SERVICE_SUCCESS
;
1355 case SERVICE_RESTART_ON_ABNORMAL
:
1356 return !IN_SET(s
->result
, SERVICE_SUCCESS
, SERVICE_FAILURE_EXIT_CODE
);
1358 case SERVICE_RESTART_ON_WATCHDOG
:
1359 return s
->result
== SERVICE_FAILURE_WATCHDOG
;
1361 case SERVICE_RESTART_ON_ABORT
:
1362 return IN_SET(s
->result
, SERVICE_FAILURE_SIGNAL
, SERVICE_FAILURE_CORE_DUMP
);
1365 assert_not_reached("unknown restart setting");
1369 static void service_enter_dead(Service
*s
, ServiceResult f
, bool allow_restart
) {
1373 if (f
!= SERVICE_SUCCESS
)
1376 service_set_state(s
, s
->result
!= SERVICE_SUCCESS
? SERVICE_FAILED
: SERVICE_DEAD
);
1378 if (s
->result
!= SERVICE_SUCCESS
) {
1379 log_unit_warning(UNIT(s
), "Failed with result '%s'.", service_result_to_string(s
->result
));
1380 failure_action(UNIT(s
)->manager
, s
->failure_action
, s
->reboot_arg
);
1383 if (allow_restart
&& service_shall_restart(s
)) {
1385 r
= service_arm_timer(s
, s
->restart_usec
);
1389 service_set_state(s
, SERVICE_AUTO_RESTART
);
1392 /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */
1393 s
->forbid_restart
= false;
1395 /* We want fresh tmpdirs in case service is started again immediately */
1396 exec_runtime_destroy(s
->exec_runtime
);
1397 s
->exec_runtime
= exec_runtime_unref(s
->exec_runtime
);
1399 /* Also, remove the runtime directory in */
1400 exec_context_destroy_runtime_directory(&s
->exec_context
, manager_get_runtime_prefix(UNIT(s
)->manager
));
1402 /* Try to delete the pid file. At this point it will be
1403 * out-of-date, and some software might be confused by it, so
1404 * let's remove it. */
1406 unlink_noerrno(s
->pid_file
);
1411 log_unit_warning_errno(UNIT(s
), r
, "Failed to run install restart timer: %m");
1412 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1415 static void service_enter_stop_post(Service
*s
, ServiceResult f
) {
1419 if (f
!= SERVICE_SUCCESS
)
1422 service_unwatch_control_pid(s
);
1423 unit_watch_all_pids(UNIT(s
));
1425 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP_POST
];
1426 if (s
->control_command
) {
1427 s
->control_command_id
= SERVICE_EXEC_STOP_POST
;
1429 r
= service_spawn(s
,
1431 s
->timeout_stop_usec
,
1433 !s
->permissions_start_only
,
1434 !s
->root_directory_start_only
,
1441 service_set_state(s
, SERVICE_STOP_POST
);
1443 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_SUCCESS
);
1448 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop-post' task: %m");
1449 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1452 static int state_to_kill_operation(ServiceState state
) {
1455 case SERVICE_STOP_SIGABRT
:
1458 case SERVICE_STOP_SIGTERM
:
1459 case SERVICE_FINAL_SIGTERM
:
1460 return KILL_TERMINATE
;
1462 case SERVICE_STOP_SIGKILL
:
1463 case SERVICE_FINAL_SIGKILL
:
1467 return _KILL_OPERATION_INVALID
;
1471 static void service_enter_signal(Service
*s
, ServiceState state
, ServiceResult f
) {
1476 if (f
!= SERVICE_SUCCESS
)
1479 unit_watch_all_pids(UNIT(s
));
1481 r
= unit_kill_context(
1484 state_to_kill_operation(state
),
1493 if (s
->timeout_stop_usec
> 0) {
1494 r
= service_arm_timer(s
, s
->timeout_stop_usec
);
1499 service_set_state(s
, state
);
1500 } else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
) && s
->kill_context
.send_sigkill
)
1501 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_SUCCESS
);
1502 else if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1503 service_enter_stop_post(s
, SERVICE_SUCCESS
);
1504 else if (state
== SERVICE_FINAL_SIGTERM
&& s
->kill_context
.send_sigkill
)
1505 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_SUCCESS
);
1507 service_enter_dead(s
, SERVICE_SUCCESS
, true);
1512 log_unit_warning_errno(UNIT(s
), r
, "Failed to kill processes: %m");
1514 if (IN_SET(state
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
))
1515 service_enter_stop_post(s
, SERVICE_FAILURE_RESOURCES
);
1517 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1520 static void service_enter_stop_by_notify(Service
*s
) {
1523 unit_watch_all_pids(UNIT(s
));
1525 if (s
->timeout_stop_usec
> 0)
1526 service_arm_timer(s
, s
->timeout_stop_usec
);
1528 /* The service told us it's stopping, so it's as if we SIGTERM'd it. */
1529 service_set_state(s
, SERVICE_STOP_SIGTERM
);
1532 static void service_enter_stop(Service
*s
, ServiceResult f
) {
1537 if (f
!= SERVICE_SUCCESS
)
1540 service_unwatch_control_pid(s
);
1541 unit_watch_all_pids(UNIT(s
));
1543 s
->control_command
= s
->exec_command
[SERVICE_EXEC_STOP
];
1544 if (s
->control_command
) {
1545 s
->control_command_id
= SERVICE_EXEC_STOP
;
1547 r
= service_spawn(s
,
1549 s
->timeout_stop_usec
,
1551 !s
->permissions_start_only
,
1552 !s
->root_directory_start_only
,
1559 service_set_state(s
, SERVICE_STOP
);
1561 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
1566 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'stop' task: %m");
1567 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1570 static bool service_good(Service
*s
) {
1574 if (s
->type
== SERVICE_DBUS
&& !s
->bus_name_good
)
1577 main_pid_ok
= main_pid_good(s
);
1578 if (main_pid_ok
> 0) /* It's alive */
1580 if (main_pid_ok
== 0) /* It's dead */
1583 /* OK, we don't know anything about the main PID, maybe
1584 * because there is none. Let's check the control group
1587 return cgroup_good(s
) != 0;
1590 static void service_enter_running(Service
*s
, ServiceResult f
) {
1593 if (f
!= SERVICE_SUCCESS
)
1596 if (service_good(s
)) {
1598 /* If there are any queued up sd_notify()
1599 * notifications, process them now */
1600 if (s
->notify_state
== NOTIFY_RELOADING
)
1601 service_enter_reload_by_notify(s
);
1602 else if (s
->notify_state
== NOTIFY_STOPPING
)
1603 service_enter_stop_by_notify(s
);
1605 service_set_state(s
, SERVICE_RUNNING
);
1607 } else if (s
->remain_after_exit
)
1608 service_set_state(s
, SERVICE_EXITED
);
1610 service_enter_stop(s
, SERVICE_SUCCESS
);
1613 static void service_enter_start_post(Service
*s
) {
1617 service_unwatch_control_pid(s
);
1618 service_reset_watchdog(s
);
1620 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_POST
];
1621 if (s
->control_command
) {
1622 s
->control_command_id
= SERVICE_EXEC_START_POST
;
1624 r
= service_spawn(s
,
1626 s
->timeout_start_usec
,
1628 !s
->permissions_start_only
,
1629 !s
->root_directory_start_only
,
1636 service_set_state(s
, SERVICE_START_POST
);
1638 service_enter_running(s
, SERVICE_SUCCESS
);
1643 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-post' task: %m");
1644 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1647 static void service_kill_control_processes(Service
*s
) {
1650 if (!UNIT(s
)->cgroup_path
)
1653 p
= strjoina(UNIT(s
)->cgroup_path
, "/control");
1654 cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER
, p
, SIGKILL
, true, true, true, NULL
);
1657 static void service_enter_start(Service
*s
) {
1664 service_unwatch_control_pid(s
);
1665 service_unwatch_main_pid(s
);
1667 /* We want to ensure that nobody leaks processes from
1668 * START_PRE here, so let's go on a killing spree, People
1669 * should not spawn long running processes from START_PRE. */
1670 service_kill_control_processes(s
);
1672 if (s
->type
== SERVICE_FORKING
) {
1673 s
->control_command_id
= SERVICE_EXEC_START
;
1674 c
= s
->control_command
= s
->exec_command
[SERVICE_EXEC_START
];
1676 s
->main_command
= NULL
;
1678 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
1679 s
->control_command
= NULL
;
1681 c
= s
->main_command
= s
->exec_command
[SERVICE_EXEC_START
];
1685 assert(s
->type
== SERVICE_ONESHOT
);
1686 service_enter_start_post(s
);
1690 r
= service_spawn(s
,
1692 IN_SET(s
->type
, SERVICE_FORKING
, SERVICE_DBUS
, SERVICE_NOTIFY
, SERVICE_ONESHOT
) ? s
->timeout_start_usec
: 0,
1702 if (s
->type
== SERVICE_SIMPLE
|| s
->type
== SERVICE_IDLE
) {
1703 /* For simple services we immediately start
1704 * the START_POST binaries. */
1706 service_set_main_pid(s
, pid
);
1707 service_enter_start_post(s
);
1709 } else if (s
->type
== SERVICE_FORKING
) {
1711 /* For forking services we wait until the start
1712 * process exited. */
1714 s
->control_pid
= pid
;
1715 service_set_state(s
, SERVICE_START
);
1717 } else if (s
->type
== SERVICE_ONESHOT
||
1718 s
->type
== SERVICE_DBUS
||
1719 s
->type
== SERVICE_NOTIFY
) {
1721 /* For oneshot services we wait until the start
1722 * process exited, too, but it is our main process. */
1724 /* For D-Bus services we know the main pid right away,
1725 * but wait for the bus name to appear on the
1726 * bus. Notify services are similar. */
1728 service_set_main_pid(s
, pid
);
1729 service_set_state(s
, SERVICE_START
);
1731 assert_not_reached("Unknown service type");
1736 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start' task: %m");
1737 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1740 static void service_enter_start_pre(Service
*s
) {
1745 service_unwatch_control_pid(s
);
1747 s
->control_command
= s
->exec_command
[SERVICE_EXEC_START_PRE
];
1748 if (s
->control_command
) {
1749 /* Before we start anything, let's clear up what might
1750 * be left from previous runs. */
1751 service_kill_control_processes(s
);
1753 s
->control_command_id
= SERVICE_EXEC_START_PRE
;
1755 r
= service_spawn(s
,
1757 s
->timeout_start_usec
,
1759 !s
->permissions_start_only
,
1760 !s
->root_directory_start_only
,
1767 service_set_state(s
, SERVICE_START_PRE
);
1769 service_enter_start(s
);
1774 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'start-pre' task: %m");
1775 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1778 static void service_enter_restart(Service
*s
) {
1779 _cleanup_bus_error_free_ sd_bus_error error
= SD_BUS_ERROR_NULL
;
1784 if (UNIT(s
)->job
&& UNIT(s
)->job
->type
== JOB_STOP
) {
1785 /* Don't restart things if we are going down anyway */
1786 log_unit_info(UNIT(s
), "Stop job pending for unit, delaying automatic restart.");
1788 r
= service_arm_timer(s
, s
->restart_usec
);
1795 /* Any units that are bound to this service must also be
1796 * restarted. We use JOB_RESTART (instead of the more obvious
1797 * JOB_START) here so that those dependency jobs will be added
1799 r
= manager_add_job(UNIT(s
)->manager
, JOB_RESTART
, UNIT(s
), JOB_FAIL
, false, &error
, NULL
);
1803 /* Note that we stay in the SERVICE_AUTO_RESTART state here,
1804 * it will be canceled as part of the service_stop() call that
1805 * is executed as part of JOB_RESTART. */
1807 log_unit_debug(UNIT(s
), "Scheduled restart job.");
1811 log_unit_warning(UNIT(s
), "Failed to schedule restart job: %s", bus_error_message(&error
, -r
));
1812 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, false);
1815 static void service_enter_reload_by_notify(Service
*s
) {
1818 if (s
->timeout_start_usec
> 0)
1819 service_arm_timer(s
, s
->timeout_start_usec
);
1821 service_set_state(s
, SERVICE_RELOAD
);
1824 static void service_enter_reload(Service
*s
) {
1829 service_unwatch_control_pid(s
);
1830 s
->reload_result
= SERVICE_SUCCESS
;
1832 s
->control_command
= s
->exec_command
[SERVICE_EXEC_RELOAD
];
1833 if (s
->control_command
) {
1834 s
->control_command_id
= SERVICE_EXEC_RELOAD
;
1836 r
= service_spawn(s
,
1838 s
->timeout_start_usec
,
1840 !s
->permissions_start_only
,
1841 !s
->root_directory_start_only
,
1848 service_set_state(s
, SERVICE_RELOAD
);
1850 service_enter_running(s
, SERVICE_SUCCESS
);
1855 log_unit_warning_errno(UNIT(s
), r
, "Failed to run 'reload' task: %m");
1856 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1857 service_enter_running(s
, SERVICE_SUCCESS
);
1860 static void service_run_next_control(Service
*s
) {
1864 assert(s
->control_command
);
1865 assert(s
->control_command
->command_next
);
1867 assert(s
->control_command_id
!= SERVICE_EXEC_START
);
1869 s
->control_command
= s
->control_command
->command_next
;
1870 service_unwatch_control_pid(s
);
1872 r
= service_spawn(s
,
1874 IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
) ? s
->timeout_start_usec
: s
->timeout_stop_usec
,
1876 !s
->permissions_start_only
,
1877 !s
->root_directory_start_only
,
1878 s
->control_command_id
== SERVICE_EXEC_START_PRE
||
1879 s
->control_command_id
== SERVICE_EXEC_STOP_POST
,
1888 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next control task: %m");
1890 if (s
->state
== SERVICE_START_PRE
)
1891 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1892 else if (s
->state
== SERVICE_STOP
)
1893 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
1894 else if (s
->state
== SERVICE_STOP_POST
)
1895 service_enter_dead(s
, SERVICE_FAILURE_RESOURCES
, true);
1896 else if (s
->state
== SERVICE_RELOAD
) {
1897 s
->reload_result
= SERVICE_FAILURE_RESOURCES
;
1898 service_enter_running(s
, SERVICE_SUCCESS
);
1900 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1903 static void service_run_next_main(Service
*s
) {
1908 assert(s
->main_command
);
1909 assert(s
->main_command
->command_next
);
1910 assert(s
->type
== SERVICE_ONESHOT
);
1912 s
->main_command
= s
->main_command
->command_next
;
1913 service_unwatch_main_pid(s
);
1915 r
= service_spawn(s
,
1917 s
->timeout_start_usec
,
1927 service_set_main_pid(s
, pid
);
1932 log_unit_warning_errno(UNIT(s
), r
, "Failed to run next main task: %m");
1933 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
1936 static int service_start_limit_test(Service
*s
) {
1939 if (ratelimit_test(&s
->start_limit
))
1942 log_unit_warning(UNIT(s
), "Start request repeated too quickly.");
1944 return failure_action(UNIT(s
)->manager
, s
->start_limit_action
, s
->reboot_arg
);
1947 static int service_start(Unit
*u
) {
1948 Service
*s
= SERVICE(u
);
1953 /* We cannot fulfill this request right now, try again later
1955 if (IN_SET(s
->state
,
1956 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
1957 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
1960 /* Already on it! */
1961 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
))
1964 /* A service that will be restarted must be stopped first to
1965 * trigger BindsTo and/or OnFailure dependencies. If a user
1966 * does not want to wait for the holdoff time to elapse, the
1967 * service should be manually restarted, not started. We
1968 * simply return EAGAIN here, so that any start jobs stay
1969 * queued, and assume that the auto restart timer will
1970 * eventually trigger the restart. */
1971 if (s
->state
== SERVICE_AUTO_RESTART
)
1974 assert(IN_SET(s
->state
, SERVICE_DEAD
, SERVICE_FAILED
));
1976 /* Make sure we don't enter a busy loop of some kind. */
1977 r
= service_start_limit_test(s
);
1979 service_enter_dead(s
, SERVICE_FAILURE_START_LIMIT
, false);
1983 s
->result
= SERVICE_SUCCESS
;
1984 s
->reload_result
= SERVICE_SUCCESS
;
1985 s
->main_pid_known
= false;
1986 s
->main_pid_alien
= false;
1987 s
->forbid_restart
= false;
1988 s
->reset_cpu_usage
= true;
1990 s
->status_text
= mfree(s
->status_text
);
1991 s
->status_errno
= 0;
1993 s
->notify_state
= NOTIFY_UNKNOWN
;
1995 service_enter_start_pre(s
);
1999 static int service_stop(Unit
*u
) {
2000 Service
*s
= SERVICE(u
);
2004 /* Don't create restart jobs from manual stops. */
2005 s
->forbid_restart
= true;
2008 if (IN_SET(s
->state
,
2009 SERVICE_STOP
, SERVICE_STOP_SIGABRT
, SERVICE_STOP_SIGTERM
, SERVICE_STOP_SIGKILL
, SERVICE_STOP_POST
,
2010 SERVICE_FINAL_SIGTERM
, SERVICE_FINAL_SIGKILL
))
2013 /* A restart will be scheduled or is in progress. */
2014 if (s
->state
== SERVICE_AUTO_RESTART
) {
2015 service_set_state(s
, SERVICE_DEAD
);
2019 /* If there's already something running we go directly into
2021 if (IN_SET(s
->state
, SERVICE_START_PRE
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RELOAD
)) {
2022 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_SUCCESS
);
2026 assert(IN_SET(s
->state
, SERVICE_RUNNING
, SERVICE_EXITED
));
2028 service_enter_stop(s
, SERVICE_SUCCESS
);
2032 static int service_reload(Unit
*u
) {
2033 Service
*s
= SERVICE(u
);
2037 assert(s
->state
== SERVICE_RUNNING
|| s
->state
== SERVICE_EXITED
);
2039 service_enter_reload(s
);
2043 _pure_
static bool service_can_reload(Unit
*u
) {
2044 Service
*s
= SERVICE(u
);
2048 return !!s
->exec_command
[SERVICE_EXEC_RELOAD
];
2051 static int service_serialize(Unit
*u
, FILE *f
, FDSet
*fds
) {
2052 Service
*s
= SERVICE(u
);
2060 unit_serialize_item(u
, f
, "state", service_state_to_string(s
->state
));
2061 unit_serialize_item(u
, f
, "result", service_result_to_string(s
->result
));
2062 unit_serialize_item(u
, f
, "reload-result", service_result_to_string(s
->reload_result
));
2064 if (s
->control_pid
> 0)
2065 unit_serialize_item_format(u
, f
, "control-pid", PID_FMT
, s
->control_pid
);
2067 if (s
->main_pid_known
&& s
->main_pid
> 0)
2068 unit_serialize_item_format(u
, f
, "main-pid", PID_FMT
, s
->main_pid
);
2070 unit_serialize_item(u
, f
, "main-pid-known", yes_no(s
->main_pid_known
));
2071 unit_serialize_item(u
, f
, "bus-name-good", yes_no(s
->bus_name_good
));
2073 r
= unit_serialize_item_escaped(u
, f
, "status-text", s
->status_text
);
2077 /* FIXME: There's a minor uncleanliness here: if there are
2078 * multiple commands attached here, we will start from the
2079 * first one again */
2080 if (s
->control_command_id
>= 0)
2081 unit_serialize_item(u
, f
, "control-command", service_exec_command_to_string(s
->control_command_id
));
2083 r
= unit_serialize_item_fd(u
, f
, fds
, "stdin-fd", s
->stdin_fd
);
2086 r
= unit_serialize_item_fd(u
, f
, fds
, "stdout-fd", s
->stdout_fd
);
2089 r
= unit_serialize_item_fd(u
, f
, fds
, "stderr-fd", s
->stderr_fd
);
2093 r
= unit_serialize_item_fd(u
, f
, fds
, "socket-fd", s
->socket_fd
);
2096 r
= unit_serialize_item_fd(u
, f
, fds
, "endpoint-fd", s
->bus_endpoint_fd
);
2100 LIST_FOREACH(fd_store
, fs
, s
->fd_store
) {
2101 _cleanup_free_
char *c
= NULL
;
2104 copy
= fdset_put_dup(fds
, fs
->fd
);
2108 c
= cescape(fs
->fdname
);
2110 unit_serialize_item_format(u
, f
, "fd-store-fd", "%i %s", copy
, strempty(c
));
2113 if (s
->main_exec_status
.pid
> 0) {
2114 unit_serialize_item_format(u
, f
, "main-exec-status-pid", PID_FMT
, s
->main_exec_status
.pid
);
2115 dual_timestamp_serialize(f
, "main-exec-status-start", &s
->main_exec_status
.start_timestamp
);
2116 dual_timestamp_serialize(f
, "main-exec-status-exit", &s
->main_exec_status
.exit_timestamp
);
2118 if (dual_timestamp_is_set(&s
->main_exec_status
.exit_timestamp
)) {
2119 unit_serialize_item_format(u
, f
, "main-exec-status-code", "%i", s
->main_exec_status
.code
);
2120 unit_serialize_item_format(u
, f
, "main-exec-status-status", "%i", s
->main_exec_status
.status
);
2124 if (dual_timestamp_is_set(&s
->watchdog_timestamp
))
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
, "status-text")) {
2202 r
= cunescape(value
, 0, &t
);
2204 log_unit_debug_errno(u
, r
, "Failed to unescape status text: %s", value
);
2206 free(s
->status_text
);
2210 } else if (streq(key
, "control-command")) {
2211 ServiceExecCommand id
;
2213 id
= service_exec_command_from_string(value
);
2215 log_unit_debug(u
, "Failed to parse exec-command value: %s", value
);
2217 s
->control_command_id
= id
;
2218 s
->control_command
= s
->exec_command
[id
];
2220 } else if (streq(key
, "socket-fd")) {
2223 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2224 log_unit_debug(u
, "Failed to parse socket-fd value: %s", value
);
2226 asynchronous_close(s
->socket_fd
);
2227 s
->socket_fd
= fdset_remove(fds
, fd
);
2229 } else if (streq(key
, "endpoint-fd")) {
2232 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2233 log_unit_debug(u
, "Failed to parse endpoint-fd value: %s", value
);
2235 safe_close(s
->bus_endpoint_fd
);
2236 s
->bus_endpoint_fd
= fdset_remove(fds
, fd
);
2238 } else if (streq(key
, "fd-store-fd")) {
2243 pf
= strcspn(value
, WHITESPACE
);
2244 fdv
= strndupa(value
, pf
);
2246 if (safe_atoi(fdv
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2247 log_unit_debug(u
, "Failed to parse fd-store-fd value: %s", value
);
2249 _cleanup_free_
char *t
= NULL
;
2253 fdn
+= strspn(fdn
, WHITESPACE
);
2254 (void) cunescape(fdn
, 0, &t
);
2256 r
= service_add_fd_store(s
, fd
, t
);
2258 log_unit_error_errno(u
, r
, "Failed to add fd to store: %m");
2260 fdset_remove(fds
, fd
);
2263 } else if (streq(key
, "main-exec-status-pid")) {
2266 if (parse_pid(value
, &pid
) < 0)
2267 log_unit_debug(u
, "Failed to parse main-exec-status-pid value: %s", value
);
2269 s
->main_exec_status
.pid
= pid
;
2270 } else if (streq(key
, "main-exec-status-code")) {
2273 if (safe_atoi(value
, &i
) < 0)
2274 log_unit_debug(u
, "Failed to parse main-exec-status-code value: %s", value
);
2276 s
->main_exec_status
.code
= i
;
2277 } else if (streq(key
, "main-exec-status-status")) {
2280 if (safe_atoi(value
, &i
) < 0)
2281 log_unit_debug(u
, "Failed to parse main-exec-status-status value: %s", value
);
2283 s
->main_exec_status
.status
= i
;
2284 } else if (streq(key
, "main-exec-status-start"))
2285 dual_timestamp_deserialize(value
, &s
->main_exec_status
.start_timestamp
);
2286 else if (streq(key
, "main-exec-status-exit"))
2287 dual_timestamp_deserialize(value
, &s
->main_exec_status
.exit_timestamp
);
2288 else if (streq(key
, "watchdog-timestamp"))
2289 dual_timestamp_deserialize(value
, &s
->watchdog_timestamp
);
2290 else if (streq(key
, "forbid-restart")) {
2293 b
= parse_boolean(value
);
2295 log_unit_debug(u
, "Failed to parse forbid-restart value: %s", value
);
2297 s
->forbid_restart
= b
;
2298 } else if (streq(key
, "stdin-fd")) {
2301 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2302 log_unit_debug(u
, "Failed to parse stdin-fd value: %s", value
);
2304 asynchronous_close(s
->stdin_fd
);
2305 s
->stdin_fd
= fdset_remove(fds
, fd
);
2307 } else if (streq(key
, "stdout-fd")) {
2310 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2311 log_unit_debug(u
, "Failed to parse stdout-fd value: %s", value
);
2313 asynchronous_close(s
->stdout_fd
);
2314 s
->stdout_fd
= fdset_remove(fds
, fd
);
2316 } else if (streq(key
, "stderr-fd")) {
2319 if (safe_atoi(value
, &fd
) < 0 || fd
< 0 || !fdset_contains(fds
, fd
))
2320 log_unit_debug(u
, "Failed to parse stderr-fd value: %s", value
);
2322 asynchronous_close(s
->stderr_fd
);
2323 s
->stderr_fd
= fdset_remove(fds
, fd
);
2326 log_unit_debug(u
, "Unknown serialization key: %s", key
);
2331 _pure_
static UnitActiveState
service_active_state(Unit
*u
) {
2332 const UnitActiveState
*table
;
2336 table
= SERVICE(u
)->type
== SERVICE_IDLE
? state_translation_table_idle
: state_translation_table
;
2338 return table
[SERVICE(u
)->state
];
2341 static const char *service_sub_state_to_string(Unit
*u
) {
2344 return service_state_to_string(SERVICE(u
)->state
);
2347 static bool service_check_gc(Unit
*u
) {
2348 Service
*s
= SERVICE(u
);
2352 /* Never clean up services that still have a process around,
2353 * even if the service is formally dead. */
2354 if (cgroup_good(s
) > 0 ||
2355 main_pid_good(s
) > 0 ||
2356 control_pid_good(s
) > 0)
2362 _pure_
static bool service_check_snapshot(Unit
*u
) {
2363 Service
*s
= SERVICE(u
);
2367 return s
->socket_fd
< 0;
2370 static int service_retry_pid_file(Service
*s
) {
2373 assert(s
->pid_file
);
2374 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2376 r
= service_load_pid_file(s
, false);
2380 service_unwatch_pid_file(s
);
2382 service_enter_running(s
, SERVICE_SUCCESS
);
2386 static int service_watch_pid_file(Service
*s
) {
2389 log_unit_debug(UNIT(s
), "Setting watch for PID file %s", s
->pid_file_pathspec
->path
);
2391 r
= path_spec_watch(s
->pid_file_pathspec
, service_dispatch_io
);
2395 /* the pidfile might have appeared just before we set the watch */
2396 log_unit_debug(UNIT(s
), "Trying to read PID file %s in case it changed", s
->pid_file_pathspec
->path
);
2397 service_retry_pid_file(s
);
2401 log_unit_error_errno(UNIT(s
), r
, "Failed to set a watch for PID file %s: %m", s
->pid_file_pathspec
->path
);
2402 service_unwatch_pid_file(s
);
2406 static int service_demand_pid_file(Service
*s
) {
2409 assert(s
->pid_file
);
2410 assert(!s
->pid_file_pathspec
);
2412 ps
= new0(PathSpec
, 1);
2417 ps
->path
= strdup(s
->pid_file
);
2423 path_kill_slashes(ps
->path
);
2425 /* PATH_CHANGED would not be enough. There are daemons (sendmail) that
2426 * keep their PID file open all the time. */
2427 ps
->type
= PATH_MODIFIED
;
2428 ps
->inotify_fd
= -1;
2430 s
->pid_file_pathspec
= ps
;
2432 return service_watch_pid_file(s
);
2435 static int service_dispatch_io(sd_event_source
*source
, int fd
, uint32_t events
, void *userdata
) {
2436 PathSpec
*p
= userdata
;
2441 s
= SERVICE(p
->unit
);
2445 assert(s
->state
== SERVICE_START
|| s
->state
== SERVICE_START_POST
);
2446 assert(s
->pid_file_pathspec
);
2447 assert(path_spec_owns_inotify_fd(s
->pid_file_pathspec
, fd
));
2449 log_unit_debug(UNIT(s
), "inotify event");
2451 if (path_spec_fd_event(p
, events
) < 0)
2454 if (service_retry_pid_file(s
) == 0)
2457 if (service_watch_pid_file(s
) < 0)
2463 service_unwatch_pid_file(s
);
2464 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2468 static void service_notify_cgroup_empty_event(Unit
*u
) {
2469 Service
*s
= SERVICE(u
);
2473 log_unit_debug(u
, "cgroup is empty");
2477 /* Waiting for SIGCHLD is usually more interesting,
2478 * because it includes return codes/signals. Which is
2479 * why we ignore the cgroup events for most cases,
2480 * except when we don't know pid which to expect the
2484 case SERVICE_START_POST
:
2485 /* If we were hoping for the daemon to write its PID file,
2486 * we can give up now. */
2487 if (s
->pid_file_pathspec
) {
2488 log_unit_warning(u
, "Daemon never wrote its PID file. Failing.");
2490 service_unwatch_pid_file(s
);
2491 if (s
->state
== SERVICE_START
)
2492 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2494 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2498 case SERVICE_RUNNING
:
2499 /* service_enter_running() will figure out what to do */
2500 service_enter_running(s
, SERVICE_SUCCESS
);
2503 case SERVICE_STOP_SIGABRT
:
2504 case SERVICE_STOP_SIGTERM
:
2505 case SERVICE_STOP_SIGKILL
:
2507 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2508 service_enter_stop_post(s
, SERVICE_SUCCESS
);
2512 case SERVICE_STOP_POST
:
2513 case SERVICE_FINAL_SIGTERM
:
2514 case SERVICE_FINAL_SIGKILL
:
2515 if (main_pid_good(s
) <= 0 && !control_pid_good(s
))
2516 service_enter_dead(s
, SERVICE_SUCCESS
, true);
2525 static void service_sigchld_event(Unit
*u
, pid_t pid
, int code
, int status
) {
2526 Service
*s
= SERVICE(u
);
2532 if (UNIT(s
)->fragment_path
? is_clean_exit(code
, status
, &s
->success_status
) :
2533 is_clean_exit_lsb(code
, status
, &s
->success_status
))
2534 f
= SERVICE_SUCCESS
;
2535 else if (code
== CLD_EXITED
)
2536 f
= SERVICE_FAILURE_EXIT_CODE
;
2537 else if (code
== CLD_KILLED
)
2538 f
= SERVICE_FAILURE_SIGNAL
;
2539 else if (code
== CLD_DUMPED
)
2540 f
= SERVICE_FAILURE_CORE_DUMP
;
2542 assert_not_reached("Unknown code");
2544 if (s
->main_pid
== pid
) {
2545 /* Forking services may occasionally move to a new PID.
2546 * As long as they update the PID file before exiting the old
2547 * PID, they're fine. */
2548 if (service_load_pid_file(s
, false) == 0)
2552 exec_status_exit(&s
->main_exec_status
, &s
->exec_context
, pid
, code
, status
);
2554 if (s
->main_command
) {
2555 /* If this is not a forking service than the
2556 * main process got started and hence we copy
2557 * the exit status so that it is recorded both
2558 * as main and as control process exit
2561 s
->main_command
->exec_status
= s
->main_exec_status
;
2563 if (s
->main_command
->ignore
)
2564 f
= SERVICE_SUCCESS
;
2565 } else if (s
->exec_command
[SERVICE_EXEC_START
]) {
2567 /* If this is a forked process, then we should
2568 * ignore the return value if this was
2569 * configured for the starter process */
2571 if (s
->exec_command
[SERVICE_EXEC_START
]->ignore
)
2572 f
= SERVICE_SUCCESS
;
2575 log_struct(f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
,
2577 LOG_UNIT_MESSAGE(u
, "Main process exited, code=%s, status=%i/%s",
2578 sigchld_code_to_string(code
), status
,
2579 strna(code
== CLD_EXITED
2580 ? exit_status_to_string(status
, EXIT_STATUS_FULL
)
2581 : signal_to_string(status
))),
2582 "EXIT_CODE=%s", sigchld_code_to_string(code
),
2583 "EXIT_STATUS=%i", status
,
2586 if (f
!= SERVICE_SUCCESS
)
2589 if (s
->main_command
&&
2590 s
->main_command
->command_next
&&
2591 f
== SERVICE_SUCCESS
) {
2593 /* There is another command to *
2594 * execute, so let's do that. */
2596 log_unit_debug(u
, "Running next main command for state %s.", service_state_to_string(s
->state
));
2597 service_run_next_main(s
);
2601 /* The service exited, so the service is officially
2603 s
->main_command
= NULL
;
2607 case SERVICE_START_POST
:
2608 case SERVICE_RELOAD
:
2610 /* Need to wait until the operation is
2615 if (s
->type
== SERVICE_ONESHOT
) {
2616 /* This was our main goal, so let's go on */
2617 if (f
== SERVICE_SUCCESS
)
2618 service_enter_start_post(s
);
2620 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2626 case SERVICE_RUNNING
:
2627 service_enter_running(s
, f
);
2630 case SERVICE_STOP_SIGABRT
:
2631 case SERVICE_STOP_SIGTERM
:
2632 case SERVICE_STOP_SIGKILL
:
2634 if (!control_pid_good(s
))
2635 service_enter_stop_post(s
, f
);
2637 /* If there is still a control process, wait for that first */
2640 case SERVICE_STOP_POST
:
2641 case SERVICE_FINAL_SIGTERM
:
2642 case SERVICE_FINAL_SIGKILL
:
2644 if (!control_pid_good(s
))
2645 service_enter_dead(s
, f
, true);
2649 assert_not_reached("Uh, main process died at wrong time.");
2653 } else if (s
->control_pid
== pid
) {
2656 if (s
->control_command
) {
2657 exec_status_exit(&s
->control_command
->exec_status
, &s
->exec_context
, pid
, code
, status
);
2659 if (s
->control_command
->ignore
)
2660 f
= SERVICE_SUCCESS
;
2663 log_unit_full(u
, f
== SERVICE_SUCCESS
? LOG_DEBUG
: LOG_NOTICE
, 0,
2664 "Control process exited, code=%s status=%i",
2665 sigchld_code_to_string(code
), status
);
2667 if (f
!= SERVICE_SUCCESS
)
2670 /* Immediately get rid of the cgroup, so that the
2671 * kernel doesn't delay the cgroup empty messages for
2672 * the service cgroup any longer than necessary */
2673 service_kill_control_processes(s
);
2675 if (s
->control_command
&&
2676 s
->control_command
->command_next
&&
2677 f
== SERVICE_SUCCESS
) {
2679 /* There is another command to *
2680 * execute, so let's do that. */
2682 log_unit_debug(u
, "Running next control command for state %s.", service_state_to_string(s
->state
));
2683 service_run_next_control(s
);
2686 /* No further commands for this step, so let's
2687 * figure out what to do next */
2689 s
->control_command
= NULL
;
2690 s
->control_command_id
= _SERVICE_EXEC_COMMAND_INVALID
;
2692 log_unit_debug(u
, "Got final SIGCHLD for state %s.", service_state_to_string(s
->state
));
2696 case SERVICE_START_PRE
:
2697 if (f
== SERVICE_SUCCESS
)
2698 service_enter_start(s
);
2700 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2704 if (s
->type
!= SERVICE_FORKING
)
2705 /* Maybe spurious event due to a reload that changed the type? */
2708 if (f
!= SERVICE_SUCCESS
) {
2709 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, f
);
2714 bool has_start_post
;
2717 /* Let's try to load the pid file here if we can.
2718 * The PID file might actually be created by a START_POST
2719 * script. In that case don't worry if the loading fails. */
2721 has_start_post
= !!s
->exec_command
[SERVICE_EXEC_START_POST
];
2722 r
= service_load_pid_file(s
, !has_start_post
);
2723 if (!has_start_post
&& r
< 0) {
2724 r
= service_demand_pid_file(s
);
2725 if (r
< 0 || !cgroup_good(s
))
2726 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_RESOURCES
);
2730 (void) service_search_main_pid(s
);
2732 service_enter_start_post(s
);
2735 case SERVICE_START_POST
:
2736 if (f
!= SERVICE_SUCCESS
) {
2737 service_enter_stop(s
, f
);
2744 r
= service_load_pid_file(s
, true);
2746 r
= service_demand_pid_file(s
);
2747 if (r
< 0 || !cgroup_good(s
))
2748 service_enter_stop(s
, SERVICE_FAILURE_RESOURCES
);
2752 (void) service_search_main_pid(s
);
2754 service_enter_running(s
, SERVICE_SUCCESS
);
2757 case SERVICE_RELOAD
:
2758 if (f
== SERVICE_SUCCESS
) {
2759 service_load_pid_file(s
, true);
2760 (void) service_search_main_pid(s
);
2763 s
->reload_result
= f
;
2764 service_enter_running(s
, SERVICE_SUCCESS
);
2768 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, f
);
2771 case SERVICE_STOP_SIGABRT
:
2772 case SERVICE_STOP_SIGTERM
:
2773 case SERVICE_STOP_SIGKILL
:
2774 if (main_pid_good(s
) <= 0)
2775 service_enter_stop_post(s
, f
);
2777 /* If there is still a service
2778 * process around, wait until
2779 * that one quit, too */
2782 case SERVICE_STOP_POST
:
2783 case SERVICE_FINAL_SIGTERM
:
2784 case SERVICE_FINAL_SIGKILL
:
2785 if (main_pid_good(s
) <= 0)
2786 service_enter_dead(s
, f
, true);
2790 assert_not_reached("Uh, control process died at wrong time.");
2795 /* Notify clients about changed exit status */
2796 unit_add_to_dbus_queue(u
);
2798 /* We got one SIGCHLD for the service, let's watch all
2799 * processes that are now running of the service, and watch
2800 * that. Among the PIDs we then watch will be children
2801 * reassigned to us, which hopefully allows us to identify
2802 * when all children are gone */
2803 unit_tidy_watch_pids(u
, s
->main_pid
, s
->control_pid
);
2804 unit_watch_all_pids(u
);
2806 /* If the PID set is empty now, then let's finish this off */
2807 if (set_isempty(u
->pids
))
2808 service_notify_cgroup_empty_event(u
);
2811 static int service_dispatch_timer(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2812 Service
*s
= SERVICE(userdata
);
2815 assert(source
== s
->timer_event_source
);
2819 case SERVICE_START_PRE
:
2821 log_unit_warning(UNIT(s
), "%s operation timed out. Terminating.", s
->state
== SERVICE_START
? "Start" : "Start-pre");
2822 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2825 case SERVICE_START_POST
:
2826 log_unit_warning(UNIT(s
), "Start-post operation timed out. Stopping.");
2827 service_enter_stop(s
, SERVICE_FAILURE_TIMEOUT
);
2830 case SERVICE_RELOAD
:
2831 log_unit_warning(UNIT(s
), "Reload operation timed out. Stopping.");
2832 service_unwatch_control_pid(s
);
2833 service_kill_control_processes(s
);
2834 s
->reload_result
= SERVICE_FAILURE_TIMEOUT
;
2835 service_enter_running(s
, SERVICE_SUCCESS
);
2839 log_unit_warning(UNIT(s
), "Stopping timed out. Terminating.");
2840 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2843 case SERVICE_STOP_SIGABRT
:
2844 log_unit_warning(UNIT(s
), "State 'stop-sigabrt' timed out. Terminating.");
2845 service_enter_signal(s
, SERVICE_STOP_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2848 case SERVICE_STOP_SIGTERM
:
2849 if (s
->kill_context
.send_sigkill
) {
2850 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Killing.");
2851 service_enter_signal(s
, SERVICE_STOP_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2853 log_unit_warning(UNIT(s
), "State 'stop-sigterm' timed out. Skipping SIGKILL.");
2854 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2859 case SERVICE_STOP_SIGKILL
:
2860 /* Uh, we sent a SIGKILL and it is still not gone?
2861 * Must be something we cannot kill, so let's just be
2862 * weirded out and continue */
2864 log_unit_warning(UNIT(s
), "Processes still around after SIGKILL. Ignoring.");
2865 service_enter_stop_post(s
, SERVICE_FAILURE_TIMEOUT
);
2868 case SERVICE_STOP_POST
:
2869 log_unit_warning(UNIT(s
), "State 'stop-post' timed out. Terminating.");
2870 service_enter_signal(s
, SERVICE_FINAL_SIGTERM
, SERVICE_FAILURE_TIMEOUT
);
2873 case SERVICE_FINAL_SIGTERM
:
2874 if (s
->kill_context
.send_sigkill
) {
2875 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Killing.");
2876 service_enter_signal(s
, SERVICE_FINAL_SIGKILL
, SERVICE_FAILURE_TIMEOUT
);
2878 log_unit_warning(UNIT(s
), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode.");
2879 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, false);
2884 case SERVICE_FINAL_SIGKILL
:
2885 log_unit_warning(UNIT(s
), "Processes still around after final SIGKILL. Entering failed mode.");
2886 service_enter_dead(s
, SERVICE_FAILURE_TIMEOUT
, true);
2889 case SERVICE_AUTO_RESTART
:
2890 log_unit_info(UNIT(s
),
2891 s
->restart_usec
> 0 ?
2892 "Service hold-off time over, scheduling restart." :
2893 "Service has no hold-off time, scheduling restart.");
2894 service_enter_restart(s
);
2898 assert_not_reached("Timeout at wrong time.");
2904 static int service_dispatch_watchdog(sd_event_source
*source
, usec_t usec
, void *userdata
) {
2905 Service
*s
= SERVICE(userdata
);
2906 char t
[FORMAT_TIMESPAN_MAX
];
2909 assert(source
== s
->watchdog_event_source
);
2911 log_unit_error(UNIT(s
), "Watchdog timeout (limit %s)!",
2912 format_timespan(t
, sizeof(t
), s
->watchdog_usec
, 1));
2914 service_enter_signal(s
, SERVICE_STOP_SIGABRT
, SERVICE_FAILURE_WATCHDOG
);
2919 static void service_notify_message(Unit
*u
, pid_t pid
, char **tags
, FDSet
*fds
) {
2920 Service
*s
= SERVICE(u
);
2921 _cleanup_free_
char *cc
= NULL
;
2922 bool notify_dbus
= false;
2927 cc
= strv_join(tags
, ", ");
2929 if (s
->notify_access
== NOTIFY_NONE
) {
2930 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception is disabled.", pid
);
2932 } else if (s
->notify_access
== NOTIFY_MAIN
&& pid
!= s
->main_pid
) {
2933 if (s
->main_pid
!= 0)
2934 log_unit_warning(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID "PID_FMT
, pid
, s
->main_pid
);
2936 log_unit_debug(u
, "Got notification message from PID "PID_FMT
", but reception only permitted for main PID which is currently not known", pid
);
2939 log_unit_debug(u
, "Got notification message from PID "PID_FMT
" (%s)", pid
, isempty(cc
) ? "n/a" : cc
);
2941 /* Interpret MAINPID= */
2942 e
= strv_find_startswith(tags
, "MAINPID=");
2943 if (e
&& IN_SET(s
->state
, SERVICE_START
, SERVICE_START_POST
, SERVICE_RUNNING
, SERVICE_RELOAD
)) {
2944 if (parse_pid(e
, &pid
) < 0)
2945 log_unit_warning(u
, "Failed to parse MAINPID= field in notification message: %s", e
);
2947 service_set_main_pid(s
, pid
);
2948 unit_watch_pid(UNIT(s
), pid
);
2953 /* Interpret RELOADING= */
2954 if (strv_find(tags
, "RELOADING=1")) {
2956 s
->notify_state
= NOTIFY_RELOADING
;
2958 if (s
->state
== SERVICE_RUNNING
)
2959 service_enter_reload_by_notify(s
);
2964 /* Interpret READY= */
2965 if (strv_find(tags
, "READY=1")) {
2967 s
->notify_state
= NOTIFY_READY
;
2969 /* Type=notify services inform us about completed
2970 * initialization with READY=1 */
2971 if (s
->type
== SERVICE_NOTIFY
&& s
->state
== SERVICE_START
)
2972 service_enter_start_post(s
);
2974 /* Sending READY=1 while we are reloading informs us
2975 * that the reloading is complete */
2976 if (s
->state
== SERVICE_RELOAD
&& s
->control_pid
== 0)
2977 service_enter_running(s
, SERVICE_SUCCESS
);
2982 /* Interpret STOPPING= */
2983 if (strv_find(tags
, "STOPPING=1")) {
2985 s
->notify_state
= NOTIFY_STOPPING
;
2987 if (s
->state
== SERVICE_RUNNING
)
2988 service_enter_stop_by_notify(s
);
2993 /* Interpret STATUS= */
2994 e
= strv_find_startswith(tags
, "STATUS=");
2996 _cleanup_free_
char *t
= NULL
;
2999 if (!utf8_is_valid(e
))
3000 log_unit_warning(u
, "Status message in notification message is not UTF-8 clean.");
3008 if (!streq_ptr(s
->status_text
, t
)) {
3010 free(s
->status_text
);
3018 /* Interpret ERRNO= */
3019 e
= strv_find_startswith(tags
, "ERRNO=");
3023 if (safe_atoi(e
, &status_errno
) < 0 || status_errno
< 0)
3024 log_unit_warning(u
, "Failed to parse ERRNO= field in notification message: %s", e
);
3026 if (s
->status_errno
!= status_errno
) {
3027 s
->status_errno
= status_errno
;
3033 /* Interpret WATCHDOG= */
3034 if (strv_find(tags
, "WATCHDOG=1"))
3035 service_reset_watchdog(s
);
3037 if (strv_find(tags
, "FDSTORE=1")) {
3040 name
= strv_find_startswith(tags
, "FDNAME=");
3041 if (name
&& !fdname_is_valid(name
)) {
3042 log_unit_warning(u
, "Passed FDNAME= name is invalid, ignoring.");
3046 service_add_fd_store_set(s
, fds
, name
);
3049 /* Notify clients about changed status or main pid */
3051 unit_add_to_dbus_queue(u
);
3054 static int service_get_timeout(Unit
*u
, uint64_t *timeout
) {
3055 Service
*s
= SERVICE(u
);
3058 if (!s
->timer_event_source
)
3061 r
= sd_event_source_get_time(s
->timer_event_source
, timeout
);
3068 static void service_bus_name_owner_change(
3071 const char *old_owner
,
3072 const char *new_owner
) {
3074 Service
*s
= SERVICE(u
);
3080 assert(streq(s
->bus_name
, name
));
3081 assert(old_owner
|| new_owner
);
3083 if (old_owner
&& new_owner
)
3084 log_unit_debug(u
, "D-Bus name %s changed owner from %s to %s", name
, old_owner
, new_owner
);
3086 log_unit_debug(u
, "D-Bus name %s no longer registered by %s", name
, old_owner
);
3088 log_unit_debug(u
, "D-Bus name %s now registered by %s", name
, new_owner
);
3090 s
->bus_name_good
= !!new_owner
;
3092 if (s
->type
== SERVICE_DBUS
) {
3094 /* service_enter_running() will figure out what to
3096 if (s
->state
== SERVICE_RUNNING
)
3097 service_enter_running(s
, SERVICE_SUCCESS
);
3098 else if (s
->state
== SERVICE_START
&& new_owner
)
3099 service_enter_start_post(s
);
3101 } else if (new_owner
&&
3103 (s
->state
== SERVICE_START
||
3104 s
->state
== SERVICE_START_POST
||
3105 s
->state
== SERVICE_RUNNING
||
3106 s
->state
== SERVICE_RELOAD
)) {
3108 _cleanup_bus_creds_unref_ sd_bus_creds
*creds
= NULL
;
3111 /* Try to acquire PID from bus service */
3113 r
= sd_bus_get_name_creds(u
->manager
->api_bus
, name
, SD_BUS_CREDS_PID
, &creds
);
3115 r
= sd_bus_creds_get_pid(creds
, &pid
);
3117 log_unit_debug(u
, "D-Bus name %s is now owned by process %u", name
, (unsigned) pid
);
3119 service_set_main_pid(s
, pid
);
3120 unit_watch_pid(UNIT(s
), pid
);
3125 int service_set_socket_fd(Service
*s
, int fd
, Socket
*sock
, bool selinux_context_net
) {
3126 _cleanup_free_
char *peer
= NULL
;
3132 /* This is called by the socket code when instantiating a new
3133 * service for a stream socket and the socket needs to be
3136 if (UNIT(s
)->load_state
!= UNIT_LOADED
)
3139 if (s
->socket_fd
>= 0)
3142 if (s
->state
!= SERVICE_DEAD
)
3145 if (getpeername_pretty(fd
, &peer
) >= 0) {
3147 if (UNIT(s
)->description
) {
3148 _cleanup_free_
char *a
;
3150 a
= strjoin(UNIT(s
)->description
, " (", peer
, ")", NULL
);
3154 r
= unit_set_description(UNIT(s
), a
);
3156 r
= unit_set_description(UNIT(s
), peer
);
3163 s
->socket_fd_selinux_context_net
= selinux_context_net
;
3165 unit_ref_set(&s
->accept_socket
, UNIT(sock
));
3167 return unit_add_two_dependencies(UNIT(sock
), UNIT_BEFORE
, UNIT_TRIGGERS
, UNIT(s
), false);
3170 static void service_reset_failed(Unit
*u
) {
3171 Service
*s
= SERVICE(u
);
3175 if (s
->state
== SERVICE_FAILED
)
3176 service_set_state(s
, SERVICE_DEAD
);
3178 s
->result
= SERVICE_SUCCESS
;
3179 s
->reload_result
= SERVICE_SUCCESS
;
3181 RATELIMIT_RESET(s
->start_limit
);
3184 static int service_kill(Unit
*u
, KillWho who
, int signo
, sd_bus_error
*error
) {
3185 Service
*s
= SERVICE(u
);
3187 return unit_kill_common(u
, who
, signo
, s
->main_pid
, s
->control_pid
, error
);
3190 static const char* const service_restart_table
[_SERVICE_RESTART_MAX
] = {
3191 [SERVICE_RESTART_NO
] = "no",
3192 [SERVICE_RESTART_ON_SUCCESS
] = "on-success",
3193 [SERVICE_RESTART_ON_FAILURE
] = "on-failure",
3194 [SERVICE_RESTART_ON_ABNORMAL
] = "on-abnormal",
3195 [SERVICE_RESTART_ON_WATCHDOG
] = "on-watchdog",
3196 [SERVICE_RESTART_ON_ABORT
] = "on-abort",
3197 [SERVICE_RESTART_ALWAYS
] = "always",
3200 DEFINE_STRING_TABLE_LOOKUP(service_restart
, ServiceRestart
);
3202 static const char* const service_type_table
[_SERVICE_TYPE_MAX
] = {
3203 [SERVICE_SIMPLE
] = "simple",
3204 [SERVICE_FORKING
] = "forking",
3205 [SERVICE_ONESHOT
] = "oneshot",
3206 [SERVICE_DBUS
] = "dbus",
3207 [SERVICE_NOTIFY
] = "notify",
3208 [SERVICE_IDLE
] = "idle"
3211 DEFINE_STRING_TABLE_LOOKUP(service_type
, ServiceType
);
3213 static const char* const service_exec_command_table
[_SERVICE_EXEC_COMMAND_MAX
] = {
3214 [SERVICE_EXEC_START_PRE
] = "ExecStartPre",
3215 [SERVICE_EXEC_START
] = "ExecStart",
3216 [SERVICE_EXEC_START_POST
] = "ExecStartPost",
3217 [SERVICE_EXEC_RELOAD
] = "ExecReload",
3218 [SERVICE_EXEC_STOP
] = "ExecStop",
3219 [SERVICE_EXEC_STOP_POST
] = "ExecStopPost",
3222 DEFINE_STRING_TABLE_LOOKUP(service_exec_command
, ServiceExecCommand
);
3224 static const char* const notify_access_table
[_NOTIFY_ACCESS_MAX
] = {
3225 [NOTIFY_NONE
] = "none",
3226 [NOTIFY_MAIN
] = "main",
3227 [NOTIFY_ALL
] = "all"
3230 DEFINE_STRING_TABLE_LOOKUP(notify_access
, NotifyAccess
);
3232 static const char* const notify_state_table
[_NOTIFY_STATE_MAX
] = {
3233 [NOTIFY_UNKNOWN
] = "unknown",
3234 [NOTIFY_READY
] = "ready",
3235 [NOTIFY_RELOADING
] = "reloading",
3236 [NOTIFY_STOPPING
] = "stopping",
3239 DEFINE_STRING_TABLE_LOOKUP(notify_state
, NotifyState
);
3241 static const char* const service_result_table
[_SERVICE_RESULT_MAX
] = {
3242 [SERVICE_SUCCESS
] = "success",
3243 [SERVICE_FAILURE_RESOURCES
] = "resources",
3244 [SERVICE_FAILURE_TIMEOUT
] = "timeout",
3245 [SERVICE_FAILURE_EXIT_CODE
] = "exit-code",
3246 [SERVICE_FAILURE_SIGNAL
] = "signal",
3247 [SERVICE_FAILURE_CORE_DUMP
] = "core-dump",
3248 [SERVICE_FAILURE_WATCHDOG
] = "watchdog",
3249 [SERVICE_FAILURE_START_LIMIT
] = "start-limit"
3252 DEFINE_STRING_TABLE_LOOKUP(service_result
, ServiceResult
);
3254 const UnitVTable service_vtable
= {
3255 .object_size
= sizeof(Service
),
3256 .exec_context_offset
= offsetof(Service
, exec_context
),
3257 .cgroup_context_offset
= offsetof(Service
, cgroup_context
),
3258 .kill_context_offset
= offsetof(Service
, kill_context
),
3259 .exec_runtime_offset
= offsetof(Service
, exec_runtime
),
3265 .private_section
= "Service",
3267 .init
= service_init
,
3268 .done
= service_done
,
3269 .load
= service_load
,
3270 .release_resources
= service_release_resources
,
3272 .coldplug
= service_coldplug
,
3274 .dump
= service_dump
,
3276 .start
= service_start
,
3277 .stop
= service_stop
,
3278 .reload
= service_reload
,
3280 .can_reload
= service_can_reload
,
3282 .kill
= service_kill
,
3284 .serialize
= service_serialize
,
3285 .deserialize_item
= service_deserialize_item
,
3287 .active_state
= service_active_state
,
3288 .sub_state_to_string
= service_sub_state_to_string
,
3290 .check_gc
= service_check_gc
,
3291 .check_snapshot
= service_check_snapshot
,
3293 .sigchld_event
= service_sigchld_event
,
3295 .reset_failed
= service_reset_failed
,
3297 .notify_cgroup_empty
= service_notify_cgroup_empty_event
,
3298 .notify_message
= service_notify_message
,
3300 .bus_name_owner_change
= service_bus_name_owner_change
,
3302 .bus_vtable
= bus_service_vtable
,
3303 .bus_set_property
= bus_service_set_property
,
3304 .bus_commit_properties
= bus_service_commit_properties
,
3306 .get_timeout
= service_get_timeout
,
3307 .can_transient
= true,
3309 .status_message_formats
= {
3310 .starting_stopping
= {
3311 [0] = "Starting %s...",
3312 [1] = "Stopping %s...",
3314 .finished_start_job
= {
3315 [JOB_DONE
] = "Started %s.",
3316 [JOB_FAILED
] = "Failed to start %s.",
3318 .finished_stop_job
= {
3319 [JOB_DONE
] = "Stopped %s.",
3320 [JOB_FAILED
] = "Stopped (with error) %s.",