]>
Commit | Line | Data |
---|---|---|
db9ecf05 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
a7334b09 | 2 | |
5cb5a6ff | 3 | #include <errno.h> |
ca78ad1d ZJS |
4 | #include <sys/stat.h> |
5 | #include <sys/types.h> | |
2c4104f0 | 6 | #include <unistd.h> |
5cb5a6ff | 7 | |
7a0019d3 LP |
8 | #include "sd-messages.h" |
9 | ||
b5efdb8a | 10 | #include "alloc-util.h" |
574634bc | 11 | #include "async.h" |
4f5dd394 LP |
12 | #include "bus-error.h" |
13 | #include "bus-kernel.h" | |
14 | #include "bus-util.h" | |
f4351959 | 15 | #include "chase-symlinks.h" |
28db6fbf | 16 | #include "constants.h" |
4139c1b2 | 17 | #include "dbus-service.h" |
6fcbec6f | 18 | #include "dbus-unit.h" |
4d1a6904 | 19 | #include "env-util.h" |
4f5dd394 LP |
20 | #include "escape.h" |
21 | #include "exit-status.h" | |
3ffd4af2 | 22 | #include "fd-util.h" |
a5c32cff | 23 | #include "fileio.h" |
f97b34a6 | 24 | #include "format-util.h" |
4f5dd394 LP |
25 | #include "load-dropin.h" |
26 | #include "load-fragment.h" | |
27 | #include "log.h" | |
28 | #include "manager.h" | |
cd48e23f | 29 | #include "open-file.h" |
6bedfcbb | 30 | #include "parse-util.h" |
4f5dd394 | 31 | #include "path-util.h" |
0b452006 | 32 | #include "process-util.h" |
5918a933 | 33 | #include "random-util.h" |
d68c645b | 34 | #include "serialize.h" |
3ffd4af2 | 35 | #include "service.h" |
24882e06 | 36 | #include "signal-util.h" |
4f5dd394 | 37 | #include "special.h" |
e266c068 | 38 | #include "stdio-util.h" |
8b43440b | 39 | #include "string-table.h" |
07630cea | 40 | #include "string-util.h" |
4f5dd394 LP |
41 | #include "strv.h" |
42 | #include "unit-name.h" | |
4f5dd394 LP |
43 | #include "unit.h" |
44 | #include "utf8.h" | |
034c6ed7 | 45 | |
edbf8984 ZJS |
46 | #define service_spawn(...) service_spawn_internal(__func__, __VA_ARGS__) |
47 | ||
acbb0225 | 48 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
87f0e418 | 49 | [SERVICE_DEAD] = UNIT_INACTIVE, |
31cd5f63 | 50 | [SERVICE_CONDITION] = UNIT_ACTIVATING, |
87f0e418 LP |
51 | [SERVICE_START_PRE] = UNIT_ACTIVATING, |
52 | [SERVICE_START] = UNIT_ACTIVATING, | |
53 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
54 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
80876c20 | 55 | [SERVICE_EXITED] = UNIT_ACTIVE, |
032ff4af | 56 | [SERVICE_RELOAD] = UNIT_RELOADING, |
3bd28bf7 LP |
57 | [SERVICE_RELOAD_SIGNAL] = UNIT_RELOADING, |
58 | [SERVICE_RELOAD_NOTIFY] = UNIT_RELOADING, | |
87f0e418 | 59 | [SERVICE_STOP] = UNIT_DEACTIVATING, |
c87700a1 | 60 | [SERVICE_STOP_WATCHDOG] = UNIT_DEACTIVATING, |
87f0e418 LP |
61 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
62 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
63 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
bf760801 | 64 | [SERVICE_FINAL_WATCHDOG] = UNIT_DEACTIVATING, |
87f0e418 LP |
65 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, |
66 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 67 | [SERVICE_FAILED] = UNIT_FAILED, |
4c2f5842 LP |
68 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, |
69 | [SERVICE_CLEANING] = UNIT_MAINTENANCE, | |
034c6ed7 | 70 | }; |
5cb5a6ff | 71 | |
e056b01d LP |
72 | /* For Type=idle we never want to delay any other jobs, hence we |
73 | * consider idle jobs active as soon as we start working on them */ | |
74 | static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = { | |
75 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
31cd5f63 | 76 | [SERVICE_CONDITION] = UNIT_ACTIVE, |
e056b01d LP |
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, | |
3bd28bf7 LP |
83 | [SERVICE_RELOAD_SIGNAL] = UNIT_RELOADING, |
84 | [SERVICE_RELOAD_NOTIFY] = UNIT_RELOADING, | |
e056b01d | 85 | [SERVICE_STOP] = UNIT_DEACTIVATING, |
c87700a1 | 86 | [SERVICE_STOP_WATCHDOG] = UNIT_DEACTIVATING, |
e056b01d LP |
87 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
88 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
89 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
bf760801 | 90 | [SERVICE_FINAL_WATCHDOG] = UNIT_DEACTIVATING, |
e056b01d LP |
91 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, |
92 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
93 | [SERVICE_FAILED] = UNIT_FAILED, | |
4c2f5842 LP |
94 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, |
95 | [SERVICE_CLEANING] = UNIT_MAINTENANCE, | |
e056b01d LP |
96 | }; |
97 | ||
5686391b | 98 | static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
718db961 LP |
99 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); |
100 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata); | |
5686391b | 101 | static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
718db961 | 102 | |
842129f5 | 103 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f); |
308d72dc | 104 | static void service_enter_reload_by_notify(Service *s); |
842129f5 | 105 | |
a16e1123 LP |
106 | static void service_init(Unit *u) { |
107 | Service *s = SERVICE(u); | |
108 | ||
109 | assert(u); | |
ac155bb8 | 110 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 111 | |
1f19a534 OS |
112 | s->timeout_start_usec = u->manager->default_timeout_start_usec; |
113 | s->timeout_stop_usec = u->manager->default_timeout_stop_usec; | |
dc653bf4 JK |
114 | s->timeout_abort_usec = u->manager->default_timeout_abort_usec; |
115 | s->timeout_abort_set = u->manager->default_timeout_abort_set; | |
1f19a534 | 116 | s->restart_usec = u->manager->default_restart_usec; |
36c16a7c | 117 | s->runtime_max_usec = USEC_INFINITY; |
0b86feac | 118 | s->type = _SERVICE_TYPE_INVALID; |
254d1313 ZJS |
119 | s->socket_fd = -EBADF; |
120 | s->stdin_fd = s->stdout_fd = s->stderr_fd = -EBADF; | |
3185a36b | 121 | s->guess_main_pid = true; |
a16e1123 | 122 | |
a16e1123 | 123 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
00f5ad93 LP |
124 | |
125 | s->exec_context.keyring_mode = MANAGER_IS_SYSTEM(u->manager) ? | |
126 | EXEC_KEYRING_PRIVATE : EXEC_KEYRING_INHERIT; | |
aa8c4bbf LP |
127 | |
128 | s->watchdog_original_usec = USEC_INFINITY; | |
afcfaa69 LP |
129 | |
130 | s->oom_policy = _OOM_POLICY_INVALID; | |
3bd28bf7 LP |
131 | s->reload_begin_usec = USEC_INFINITY; |
132 | s->reload_signal = SIGHUP; | |
a16e1123 LP |
133 | } |
134 | ||
5e94833f LP |
135 | static void service_unwatch_control_pid(Service *s) { |
136 | assert(s); | |
137 | ||
138 | if (s->control_pid <= 0) | |
139 | return; | |
140 | ||
8f03de53 | 141 | unit_unwatch_pid(UNIT(s), TAKE_PID(s->control_pid)); |
5e94833f LP |
142 | } |
143 | ||
144 | static void service_unwatch_main_pid(Service *s) { | |
145 | assert(s); | |
146 | ||
147 | if (s->main_pid <= 0) | |
148 | return; | |
149 | ||
8f03de53 | 150 | unit_unwatch_pid(UNIT(s), TAKE_PID(s->main_pid)); |
5e94833f LP |
151 | } |
152 | ||
3e52541e MS |
153 | static void service_unwatch_pid_file(Service *s) { |
154 | if (!s->pid_file_pathspec) | |
155 | return; | |
156 | ||
f2341e0a | 157 | log_unit_debug(UNIT(s), "Stopping watch for PID file %s", s->pid_file_pathspec->path); |
718db961 | 158 | path_spec_unwatch(s->pid_file_pathspec); |
3e52541e | 159 | path_spec_done(s->pid_file_pathspec); |
a1e58e8e | 160 | s->pid_file_pathspec = mfree(s->pid_file_pathspec); |
3e52541e MS |
161 | } |
162 | ||
5925dd3c LP |
163 | static int service_set_main_pid(Service *s, pid_t pid) { |
164 | assert(s); | |
165 | ||
166 | if (pid <= 1) | |
167 | return -EINVAL; | |
168 | ||
df0ff127 | 169 | if (pid == getpid_cached()) |
5925dd3c LP |
170 | return -EINVAL; |
171 | ||
7400b9d2 LP |
172 | if (s->main_pid == pid && s->main_pid_known) |
173 | return 0; | |
174 | ||
175 | if (s->main_pid != pid) { | |
176 | service_unwatch_main_pid(s); | |
177 | exec_status_start(&s->main_exec_status, pid); | |
178 | } | |
41efeaec | 179 | |
6dfa5494 LP |
180 | s->main_pid = pid; |
181 | s->main_pid_known = true; | |
4d051546 | 182 | s->main_pid_alien = pid_is_my_child(pid) == 0; |
6dfa5494 | 183 | |
4d051546 | 184 | if (s->main_pid_alien) |
f2341e0a | 185 | log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", pid); |
5925dd3c LP |
186 | |
187 | return 0; | |
188 | } | |
189 | ||
3e7a1f50 | 190 | void service_close_socket_fd(Service *s) { |
4f2d528d LP |
191 | assert(s); |
192 | ||
5cc3985e | 193 | /* Undo the effect of service_set_socket_fd(). */ |
4f2d528d | 194 | |
5cc3985e | 195 | s->socket_fd = asynchronous_close(s->socket_fd); |
6cf6bbc2 | 196 | |
5cc3985e LP |
197 | if (UNIT_ISSET(s->accept_socket)) { |
198 | socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket))); | |
199 | unit_ref_unset(&s->accept_socket); | |
200 | } | |
3fabebf4 LP |
201 | |
202 | s->socket_peer = socket_peer_unref(s->socket_peer); | |
6cf6bbc2 LP |
203 | } |
204 | ||
a6927d7f MO |
205 | static void service_stop_watchdog(Service *s) { |
206 | assert(s); | |
207 | ||
5dcadb4c | 208 | s->watchdog_event_source = sd_event_source_disable_unref(s->watchdog_event_source); |
842129f5 | 209 | s->watchdog_timestamp = DUAL_TIMESTAMP_NULL; |
a6927d7f MO |
210 | } |
211 | ||
842129f5 | 212 | static void service_start_watchdog(Service *s) { |
2787d83c | 213 | usec_t watchdog_usec; |
9fb1cdb4 | 214 | int r; |
bb242b7b MO |
215 | |
216 | assert(s); | |
217 | ||
2787d83c | 218 | watchdog_usec = service_get_watchdog_usec(s); |
0da36375 | 219 | if (!timestamp_is_set(watchdog_usec)) { |
9fb1cdb4 | 220 | service_stop_watchdog(s); |
bb242b7b | 221 | return; |
9fb1cdb4 | 222 | } |
bb242b7b | 223 | |
718db961 | 224 | if (s->watchdog_event_source) { |
2787d83c | 225 | r = sd_event_source_set_time(s->watchdog_event_source, usec_add(s->watchdog_timestamp.monotonic, watchdog_usec)); |
718db961 | 226 | if (r < 0) { |
f2341e0a | 227 | log_unit_warning_errno(UNIT(s), r, "Failed to reset watchdog timer: %m"); |
718db961 LP |
228 | return; |
229 | } | |
230 | ||
842129f5 | 231 | r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT); |
c4ef3317 | 232 | } else { |
6a0f1f6d LP |
233 | r = sd_event_add_time( |
234 | UNIT(s)->manager->event, | |
235 | &s->watchdog_event_source, | |
236 | CLOCK_MONOTONIC, | |
2787d83c | 237 | usec_add(s->watchdog_timestamp.monotonic, watchdog_usec), 0, |
6a0f1f6d | 238 | service_dispatch_watchdog, s); |
c4ef3317 | 239 | if (r < 0) { |
f2341e0a | 240 | log_unit_warning_errno(UNIT(s), r, "Failed to add watchdog timer: %m"); |
c4ef3317 LP |
241 | return; |
242 | } | |
243 | ||
7dfbe2e3 TG |
244 | (void) sd_event_source_set_description(s->watchdog_event_source, "service-watchdog"); |
245 | ||
c4ef3317 LP |
246 | /* Let's process everything else which might be a sign |
247 | * of living before we consider a service died. */ | |
248 | r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE); | |
249 | } | |
bb242b7b | 250 | if (r < 0) |
f2341e0a | 251 | log_unit_warning_errno(UNIT(s), r, "Failed to install watchdog timer: %m"); |
bb242b7b MO |
252 | } |
253 | ||
ec35a7f6 LP |
254 | static void service_extend_event_source_timeout(Service *s, sd_event_source *source, usec_t extended) { |
255 | usec_t current; | |
256 | int r; | |
257 | ||
a327431b DB |
258 | assert(s); |
259 | ||
ec35a7f6 LP |
260 | /* Extends the specified event source timer to at least the specified time, unless it is already later |
261 | * anyway. */ | |
a327431b | 262 | |
ec35a7f6 LP |
263 | if (!source) |
264 | return; | |
a327431b | 265 | |
ec35a7f6 LP |
266 | r = sd_event_source_get_time(source, ¤t); |
267 | if (r < 0) { | |
268 | const char *desc; | |
269 | (void) sd_event_source_get_description(s->timer_event_source, &desc); | |
270 | log_unit_warning_errno(UNIT(s), r, "Failed to retrieve timeout time for event source '%s', ignoring: %m", strna(desc)); | |
271 | return; | |
272 | } | |
a327431b | 273 | |
ec35a7f6 LP |
274 | if (current >= extended) /* Current timeout is already longer, ignore this. */ |
275 | return; | |
a327431b | 276 | |
ec35a7f6 LP |
277 | r = sd_event_source_set_time(source, extended); |
278 | if (r < 0) { | |
279 | const char *desc; | |
280 | (void) sd_event_source_get_description(s->timer_event_source, &desc); | |
365c2885 | 281 | log_unit_warning_errno(UNIT(s), r, "Failed to set timeout time for event source '%s', ignoring %m", strna(desc)); |
a327431b DB |
282 | } |
283 | } | |
284 | ||
ec35a7f6 LP |
285 | static void service_extend_timeout(Service *s, usec_t extend_timeout_usec) { |
286 | usec_t extended; | |
287 | ||
288 | assert(s); | |
289 | ||
0da36375 | 290 | if (!timestamp_is_set(extend_timeout_usec)) |
ec35a7f6 LP |
291 | return; |
292 | ||
293 | extended = usec_add(now(CLOCK_MONOTONIC), extend_timeout_usec); | |
294 | ||
295 | service_extend_event_source_timeout(s, s->timer_event_source, extended); | |
296 | service_extend_event_source_timeout(s, s->watchdog_event_source, extended); | |
297 | } | |
298 | ||
a6927d7f MO |
299 | static void service_reset_watchdog(Service *s) { |
300 | assert(s); | |
301 | ||
302 | dual_timestamp_get(&s->watchdog_timestamp); | |
842129f5 | 303 | service_start_watchdog(s); |
a6927d7f MO |
304 | } |
305 | ||
95d0d8ed | 306 | static void service_override_watchdog_timeout(Service *s, usec_t watchdog_override_usec) { |
2787d83c M |
307 | assert(s); |
308 | ||
309 | s->watchdog_override_enable = true; | |
310 | s->watchdog_override_usec = watchdog_override_usec; | |
311 | service_reset_watchdog(s); | |
312 | ||
313 | log_unit_debug(UNIT(s), "watchdog_usec="USEC_FMT, s->watchdog_usec); | |
314 | log_unit_debug(UNIT(s), "watchdog_override_usec="USEC_FMT, s->watchdog_override_usec); | |
315 | } | |
316 | ||
a354329f LP |
317 | static void service_fd_store_unlink(ServiceFDStore *fs) { |
318 | ||
319 | if (!fs) | |
320 | return; | |
321 | ||
322 | if (fs->service) { | |
323 | assert(fs->service->n_fd_store > 0); | |
324 | LIST_REMOVE(fd_store, fs->service->fd_store, fs); | |
325 | fs->service->n_fd_store--; | |
326 | } | |
327 | ||
1d3fe304 | 328 | sd_event_source_disable_unref(fs->event_source); |
a354329f | 329 | |
8dd4c05b | 330 | free(fs->fdname); |
a354329f LP |
331 | safe_close(fs->fd); |
332 | free(fs); | |
333 | } | |
334 | ||
f0bfbfac ZJS |
335 | static void service_release_fd_store(Service *s) { |
336 | assert(s); | |
337 | ||
7eb2a8a1 LP |
338 | if (s->n_keep_fd_store > 0) |
339 | return; | |
340 | ||
f0bfbfac ZJS |
341 | log_unit_debug(UNIT(s), "Releasing all stored fds"); |
342 | while (s->fd_store) | |
343 | service_fd_store_unlink(s->fd_store); | |
344 | ||
345 | assert(s->n_fd_store == 0); | |
346 | } | |
347 | ||
7eb2a8a1 | 348 | static void service_release_resources(Unit *u) { |
a354329f LP |
349 | Service *s = SERVICE(u); |
350 | ||
351 | assert(s); | |
352 | ||
a34ceba6 | 353 | if (!s->fd_store && s->stdin_fd < 0 && s->stdout_fd < 0 && s->stderr_fd < 0) |
a354329f LP |
354 | return; |
355 | ||
f0bfbfac | 356 | log_unit_debug(u, "Releasing resources."); |
a354329f | 357 | |
a34ceba6 LP |
358 | s->stdin_fd = safe_close(s->stdin_fd); |
359 | s->stdout_fd = safe_close(s->stdout_fd); | |
360 | s->stderr_fd = safe_close(s->stderr_fd); | |
361 | ||
7eb2a8a1 | 362 | service_release_fd_store(s); |
a354329f LP |
363 | } |
364 | ||
87f0e418 LP |
365 | static void service_done(Unit *u) { |
366 | Service *s = SERVICE(u); | |
44d8db9e LP |
367 | |
368 | assert(s); | |
369 | ||
cd48e23f RP |
370 | open_file_free_many(&s->open_files); |
371 | ||
a1e58e8e LP |
372 | s->pid_file = mfree(s->pid_file); |
373 | s->status_text = mfree(s->status_text); | |
efe6e7d3 | 374 | |
e8a565cb | 375 | s->exec_runtime = exec_runtime_unref(s->exec_runtime, false); |
e537352b | 376 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
44d8db9e | 377 | s->control_command = NULL; |
867b3b7d | 378 | s->main_command = NULL; |
44d8db9e | 379 | |
29206d46 LP |
380 | dynamic_creds_unref(&s->dynamic_creds); |
381 | ||
37520c1b LP |
382 | exit_status_set_free(&s->restart_prevent_status); |
383 | exit_status_set_free(&s->restart_force_status); | |
384 | exit_status_set_free(&s->success_status); | |
96342de6 | 385 | |
44d8db9e LP |
386 | /* This will leak a process, but at least no memory or any of |
387 | * our resources */ | |
5e94833f LP |
388 | service_unwatch_main_pid(s); |
389 | service_unwatch_control_pid(s); | |
3e52541e | 390 | service_unwatch_pid_file(s); |
44d8db9e | 391 | |
05e343b7 | 392 | if (s->bus_name) { |
ac155bb8 | 393 | unit_unwatch_bus_name(u, s->bus_name); |
a1e58e8e | 394 | s->bus_name = mfree(s->bus_name); |
05e343b7 LP |
395 | } |
396 | ||
d8ccf5fd DM |
397 | s->bus_name_owner = mfree(s->bus_name_owner); |
398 | ||
064c5938 ZJS |
399 | s->usb_function_descriptors = mfree(s->usb_function_descriptors); |
400 | s->usb_function_strings = mfree(s->usb_function_strings); | |
401 | ||
4f2d528d LP |
402 | service_close_socket_fd(s); |
403 | ||
57020a3a | 404 | unit_ref_unset(&s->accept_socket); |
f976f3f6 | 405 | |
bb242b7b MO |
406 | service_stop_watchdog(s); |
407 | ||
5dcadb4c ZJS |
408 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
409 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
a354329f | 410 | |
e39eb045 LP |
411 | s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); |
412 | ||
7eb2a8a1 | 413 | service_release_resources(u); |
718db961 LP |
414 | } |
415 | ||
2339fc93 | 416 | static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) { |
99534007 | 417 | ServiceFDStore *fs = ASSERT_PTR(userdata); |
2339fc93 LP |
418 | |
419 | assert(e); | |
2339fc93 LP |
420 | |
421 | /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */ | |
16f70d63 ZJS |
422 | log_unit_debug(UNIT(fs->service), |
423 | "Received %s on stored fd %d (%s), closing.", | |
424 | revents & EPOLLERR ? "EPOLLERR" : "EPOLLHUP", | |
425 | fs->fd, strna(fs->fdname)); | |
2339fc93 LP |
426 | service_fd_store_unlink(fs); |
427 | return 0; | |
428 | } | |
429 | ||
cb5a46b8 | 430 | static int service_add_fd_store(Service *s, int fd, const char *name, bool do_poll) { |
2339fc93 LP |
431 | ServiceFDStore *fs; |
432 | int r; | |
433 | ||
9021ff17 ZJS |
434 | /* fd is always consumed if we return >= 0 */ |
435 | ||
2339fc93 LP |
436 | assert(s); |
437 | assert(fd >= 0); | |
438 | ||
439 | if (s->n_fd_store >= s->n_fd_store_max) | |
b0924635 ZJS |
440 | return -EXFULL; /* Our store is full. |
441 | * Use this errno rather than E[NM]FILE to distinguish from | |
442 | * the case where systemd itself hits the file limit. */ | |
2339fc93 | 443 | |
03677889 YW |
444 | LIST_FOREACH(fd_store, i, s->fd_store) { |
445 | r = same_fd(i->fd, fd); | |
2339fc93 LP |
446 | if (r < 0) |
447 | return r; | |
448 | if (r > 0) { | |
2339fc93 | 449 | safe_close(fd); |
9021ff17 | 450 | return 0; /* fd already included */ |
2339fc93 LP |
451 | } |
452 | } | |
453 | ||
b0cea477 | 454 | fs = new(ServiceFDStore, 1); |
2339fc93 LP |
455 | if (!fs) |
456 | return -ENOMEM; | |
457 | ||
b0cea477 LP |
458 | *fs = (ServiceFDStore) { |
459 | .fd = fd, | |
460 | .service = s, | |
461 | .do_poll = do_poll, | |
462 | .fdname = strdup(name ?: "stored"), | |
463 | }; | |
464 | ||
17dec0f7 | 465 | if (!fs->fdname) { |
466 | free(fs); | |
8dd4c05b | 467 | return -ENOMEM; |
17dec0f7 | 468 | } |
2339fc93 | 469 | |
cb5a46b8 KL |
470 | if (do_poll) { |
471 | r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fd, 0, on_fd_store_io, fs); | |
472 | if (r < 0 && r != -EPERM) { /* EPERM indicates fds that aren't pollable, which is OK */ | |
473 | free(fs->fdname); | |
474 | free(fs); | |
475 | return r; | |
476 | } else if (r >= 0) | |
477 | (void) sd_event_source_set_description(fs->event_source, "service-fd-store"); | |
478 | } | |
7dfbe2e3 | 479 | |
2339fc93 LP |
480 | LIST_PREPEND(fd_store, s->fd_store, fs); |
481 | s->n_fd_store++; | |
482 | ||
9021ff17 | 483 | return 1; /* fd newly stored */ |
2339fc93 LP |
484 | } |
485 | ||
cb5a46b8 | 486 | static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name, bool do_poll) { |
2339fc93 LP |
487 | int r; |
488 | ||
489 | assert(s); | |
490 | ||
b0924635 | 491 | while (fdset_size(fds) > 0) { |
254d1313 | 492 | _cleanup_close_ int fd = -EBADF; |
2339fc93 LP |
493 | |
494 | fd = fdset_steal_first(fds); | |
495 | if (fd < 0) | |
496 | break; | |
497 | ||
cb5a46b8 | 498 | r = service_add_fd_store(s, fd, name, do_poll); |
b0924635 ZJS |
499 | if (r == -EXFULL) |
500 | return log_unit_warning_errno(UNIT(s), r, | |
501 | "Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.", | |
502 | s->n_fd_store_max); | |
2339fc93 | 503 | if (r < 0) |
b0924635 | 504 | return log_unit_error_errno(UNIT(s), r, "Failed to add fd to store: %m"); |
9021ff17 | 505 | if (r > 0) |
c0f86d66 | 506 | log_unit_debug(UNIT(s), "Added fd %i (%s) to fd store.", fd, strna(name)); |
254d1313 | 507 | fd = -EBADF; |
2339fc93 LP |
508 | } |
509 | ||
2339fc93 LP |
510 | return 0; |
511 | } | |
512 | ||
e78ee06d | 513 | static void service_remove_fd_store(Service *s, const char *name) { |
e78ee06d LP |
514 | assert(s); |
515 | assert(name); | |
516 | ||
80a226b2 | 517 | LIST_FOREACH(fd_store, fs, s->fd_store) { |
e78ee06d LP |
518 | if (!streq(fs->fdname, name)) |
519 | continue; | |
520 | ||
521 | log_unit_debug(UNIT(s), "Got explicit request to remove fd %i (%s), closing.", fs->fd, name); | |
522 | service_fd_store_unlink(fs); | |
523 | } | |
524 | } | |
525 | ||
ecea250d | 526 | static usec_t service_running_timeout(Service *s) { |
5918a933 AB |
527 | usec_t delta = 0; |
528 | ||
529 | assert(s); | |
530 | ||
531 | if (s->runtime_rand_extra_usec != 0) { | |
532 | delta = random_u64_range(s->runtime_rand_extra_usec); | |
533 | log_unit_debug(UNIT(s), "Adding delta of %s sec to timeout", FORMAT_TIMESPAN(delta, USEC_PER_SEC)); | |
534 | } | |
535 | ||
536 | return usec_add(usec_add(UNIT(s)->active_enter_timestamp.monotonic, | |
537 | s->runtime_max_usec), | |
538 | delta); | |
539 | } | |
540 | ||
e5d6dcce | 541 | static int service_arm_timer(Service *s, bool relative, usec_t usec) { |
718db961 LP |
542 | int r; |
543 | ||
544 | assert(s); | |
545 | ||
718db961 | 546 | if (s->timer_event_source) { |
e5d6dcce | 547 | r = (relative ? sd_event_source_set_time_relative : sd_event_source_set_time)(s->timer_event_source, usec); |
718db961 LP |
548 | if (r < 0) |
549 | return r; | |
550 | ||
551 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
552 | } | |
553 | ||
36c16a7c LP |
554 | if (usec == USEC_INFINITY) |
555 | return 0; | |
556 | ||
e5d6dcce | 557 | r = (relative ? sd_event_add_time_relative : sd_event_add_time)( |
6a0f1f6d LP |
558 | UNIT(s)->manager->event, |
559 | &s->timer_event_source, | |
560 | CLOCK_MONOTONIC, | |
36c16a7c | 561 | usec, 0, |
6a0f1f6d | 562 | service_dispatch_timer, s); |
7dfbe2e3 TG |
563 | if (r < 0) |
564 | return r; | |
565 | ||
566 | (void) sd_event_source_set_description(s->timer_event_source, "service-timer"); | |
567 | ||
568 | return 0; | |
44d8db9e LP |
569 | } |
570 | ||
243b1432 LP |
571 | static int service_verify(Service *s) { |
572 | assert(s); | |
e0cfed4c | 573 | assert(UNIT(s)->load_state == UNIT_LOADED); |
243b1432 | 574 | |
03677889 | 575 | for (ServiceExecCommand c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) |
8688a389 YW |
576 | LIST_FOREACH(command, command, s->exec_command[c]) { |
577 | if (!path_is_absolute(command->path) && !filename_is_valid(command->path)) | |
578 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), | |
579 | "Service %s= binary path \"%s\" is neither a valid executable name nor an absolute path. Refusing.", | |
580 | command->path, | |
581 | service_exec_command_to_string(c)); | |
29500cf8 HC |
582 | if (strv_isempty(command->argv)) |
583 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), | |
584 | "Service has an empty argv in %s=. Refusing.", | |
585 | service_exec_command_to_string(c)); | |
8688a389 | 586 | } |
29500cf8 | 587 | |
d85ff944 YW |
588 | if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP] && |
589 | UNIT(s)->success_action == EMERGENCY_ACTION_NONE) | |
3f00d379 ZJS |
590 | /* FailureAction= only makes sense if one of the start or stop commands is specified. |
591 | * SuccessAction= will be executed unconditionally if no commands are specified. Hence, | |
592 | * either a command or SuccessAction= are required. */ | |
593 | ||
d85ff944 | 594 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart=, ExecStop=, or SuccessAction=. Refusing."); |
243b1432 | 595 | |
d85ff944 YW |
596 | if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) |
597 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); | |
96fb8242 | 598 | |
d85ff944 YW |
599 | if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START] && UNIT(s)->success_action == EMERGENCY_ACTION_NONE) |
600 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has no ExecStart= and no SuccessAction= settings and does not have RemainAfterExit=yes set. Refusing."); | |
96fb8242 | 601 | |
d85ff944 YW |
602 | if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) |
603 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); | |
6cf6bbc2 | 604 | |
d85ff944 YW |
605 | if (s->type == SERVICE_ONESHOT && |
606 | !IN_SET(s->restart, SERVICE_RESTART_NO, SERVICE_RESTART_ON_FAILURE, SERVICE_RESTART_ON_ABNORMAL, SERVICE_RESTART_ON_WATCHDOG, SERVICE_RESTART_ON_ABORT)) | |
607 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has Restart= set to either always or on-success, which isn't allowed for Type=oneshot services. Refusing."); | |
37520c1b | 608 | |
d85ff944 | 609 | if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) |
9e670fdc | 610 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has RestartForceExitStatus= set, which isn't allowed for Type=oneshot services. Refusing."); |
b0693d30 | 611 | |
596e4470 HC |
612 | if (s->type == SERVICE_ONESHOT && s->exit_type == SERVICE_EXIT_CGROUP) |
613 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has ExitType=cgroup set, which isn't allowed for Type=oneshot services. Refusing."); | |
614 | ||
d85ff944 YW |
615 | if (s->type == SERVICE_DBUS && !s->bus_name) |
616 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing."); | |
4d0e5dbd | 617 | |
d85ff944 YW |
618 | if (s->exec_context.pam_name && !IN_SET(s->kill_context.kill_mode, KILL_CONTROL_GROUP, KILL_MIXED)) |
619 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENOEXEC), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing."); | |
05e343b7 | 620 | |
6b7e5923 PS |
621 | if (s->usb_function_descriptors && !s->usb_function_strings) |
622 | log_unit_warning(UNIT(s), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring."); | |
623 | ||
624 | if (!s->usb_function_descriptors && s->usb_function_strings) | |
625 | log_unit_warning(UNIT(s), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring."); | |
626 | ||
36c16a7c | 627 | if (s->runtime_max_usec != USEC_INFINITY && s->type == SERVICE_ONESHOT) |
226a08f2 | 628 | log_unit_warning(UNIT(s), "RuntimeMaxSec= has no effect in combination with Type=oneshot. Ignoring."); |
36c16a7c | 629 | |
5918a933 AB |
630 | if (s->runtime_max_usec == USEC_INFINITY && s->runtime_rand_extra_usec != 0) |
631 | log_unit_warning(UNIT(s), "Service has RuntimeRandomizedExtraSec= setting, but no RuntimeMaxSec=. Ignoring."); | |
632 | ||
86838bf0 LB |
633 | if (s->exit_type == SERVICE_EXIT_CGROUP && cg_unified() < CGROUP_UNIFIED_SYSTEMD) |
634 | log_unit_warning(UNIT(s), "Service has ExitType=cgroup set, but we are running with legacy cgroups v1, which might not work correctly. Continuing."); | |
635 | ||
243b1432 LP |
636 | return 0; |
637 | } | |
638 | ||
a40eb732 LP |
639 | static int service_add_default_dependencies(Service *s) { |
640 | int r; | |
641 | ||
642 | assert(s); | |
643 | ||
45f06b34 LP |
644 | if (!UNIT(s)->default_dependencies) |
645 | return 0; | |
646 | ||
a40eb732 LP |
647 | /* Add a number of automatic dependencies useful for the |
648 | * majority of services. */ | |
649 | ||
463d0d15 | 650 | if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) { |
cb4c247d LP |
651 | /* First, pull in the really early boot stuff, and |
652 | * require it, so that we fail if we can't acquire | |
653 | * it. */ | |
654 | ||
5a724170 | 655 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
cb4c247d LP |
656 | if (r < 0) |
657 | return r; | |
658 | } else { | |
659 | ||
660 | /* In the --user instance there's no sysinit.target, | |
661 | * in that case require basic.target instead. */ | |
662 | ||
35d8c19a | 663 | r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
cb4c247d LP |
664 | if (r < 0) |
665 | return r; | |
666 | } | |
667 | ||
668 | /* Second, if the rest of the base system is in the same | |
669 | * transaction, order us after it, but do not pull it in or | |
670 | * even require it. */ | |
35d8c19a | 671 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_BASIC_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
fccd44ec KS |
672 | if (r < 0) |
673 | return r; | |
a40eb732 | 674 | |
cb4c247d | 675 | /* Third, add us in for normal shutdown. */ |
5a724170 | 676 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, true, UNIT_DEPENDENCY_DEFAULT); |
a40eb732 LP |
677 | } |
678 | ||
3b7f79dc | 679 | static void service_fix_stdio(Service *s) { |
4dfc092a LP |
680 | assert(s); |
681 | ||
3b7f79dc LP |
682 | /* Note that EXEC_INPUT_NULL and EXEC_OUTPUT_INHERIT play a special role here: they are both the |
683 | * default value that is subject to automatic overriding triggered by other settings and an explicit | |
162392b7 | 684 | * choice the user can make. We don't distinguish between these cases currently. */ |
3b7f79dc LP |
685 | |
686 | if (s->exec_context.std_input == EXEC_INPUT_NULL && | |
687 | s->exec_context.stdin_data_size > 0) | |
688 | s->exec_context.std_input = EXEC_INPUT_DATA; | |
689 | ||
690 | if (IN_SET(s->exec_context.std_input, | |
691 | EXEC_INPUT_TTY, | |
692 | EXEC_INPUT_TTY_FORCE, | |
693 | EXEC_INPUT_TTY_FAIL, | |
694 | EXEC_INPUT_SOCKET, | |
695 | EXEC_INPUT_NAMED_FD)) | |
696 | return; | |
697 | ||
698 | /* We assume these listed inputs refer to bidirectional streams, and hence duplicating them from | |
699 | * stdin to stdout/stderr makes sense and hence leaving EXEC_OUTPUT_INHERIT in place makes sense, | |
700 | * too. Outputs such as regular files or sealed data memfds otoh don't really make sense to be | |
701 | * duplicated for both input and output at the same time (since they then would cause a feedback | |
702 | * loop), hence override EXEC_OUTPUT_INHERIT with the default stderr/stdout setting. */ | |
4dfc092a LP |
703 | |
704 | if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT && | |
3b7f79dc | 705 | s->exec_context.std_output == EXEC_OUTPUT_INHERIT) |
1124fe6f | 706 | s->exec_context.std_error = UNIT(s)->manager->default_std_error; |
4dfc092a | 707 | |
3b7f79dc | 708 | if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT) |
1124fe6f | 709 | s->exec_context.std_output = UNIT(s)->manager->default_std_output; |
4dfc092a LP |
710 | } |
711 | ||
45f06b34 LP |
712 | static int service_setup_bus_name(Service *s) { |
713 | int r; | |
714 | ||
715 | assert(s); | |
716 | ||
0f97b7c3 | 717 | /* If s->bus_name is not set, then the unit will be refused by service_verify() later. */ |
1e8b312e | 718 | if (!s->bus_name) |
45f06b34 LP |
719 | return 0; |
720 | ||
1e8b312e LP |
721 | if (s->type == SERVICE_DBUS) { |
722 | r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE); | |
723 | if (r < 0) | |
724 | return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m"); | |
45f06b34 | 725 | |
1e8b312e LP |
726 | /* We always want to be ordered against dbus.socket if both are in the transaction. */ |
727 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_DBUS_SOCKET, true, UNIT_DEPENDENCY_FILE); | |
728 | if (r < 0) | |
729 | return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m"); | |
730 | } | |
45f06b34 LP |
731 | |
732 | r = unit_watch_bus_name(UNIT(s), s->bus_name); | |
733 | if (r == -EEXIST) | |
734 | return log_unit_error_errno(UNIT(s), r, "Two services allocated for the same bus name %s, refusing operation.", s->bus_name); | |
735 | if (r < 0) | |
736 | return log_unit_error_errno(UNIT(s), r, "Cannot watch bus name %s: %m", s->bus_name); | |
737 | ||
738 | return 0; | |
739 | } | |
740 | ||
8545f7ce LP |
741 | static int service_add_extras(Service *s) { |
742 | int r; | |
743 | ||
744 | assert(s); | |
745 | ||
746 | if (s->type == _SERVICE_TYPE_INVALID) { | |
747 | /* Figure out a type automatically */ | |
748 | if (s->bus_name) | |
749 | s->type = SERVICE_DBUS; | |
750 | else if (s->exec_command[SERVICE_EXEC_START]) | |
751 | s->type = SERVICE_SIMPLE; | |
752 | else | |
753 | s->type = SERVICE_ONESHOT; | |
754 | } | |
755 | ||
756 | /* Oneshot services have disabled start timeout by default */ | |
757 | if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined) | |
36c16a7c | 758 | s->timeout_start_usec = USEC_INFINITY; |
8545f7ce | 759 | |
3b7f79dc | 760 | service_fix_stdio(s); |
8545f7ce LP |
761 | |
762 | r = unit_patch_contexts(UNIT(s)); | |
763 | if (r < 0) | |
764 | return r; | |
765 | ||
766 | r = unit_add_exec_dependencies(UNIT(s), &s->exec_context); | |
767 | if (r < 0) | |
768 | return r; | |
769 | ||
d79200e2 | 770 | r = unit_set_default_slice(UNIT(s)); |
8545f7ce LP |
771 | if (r < 0) |
772 | return r; | |
773 | ||
4330dc03 AJ |
774 | /* If the service needs the notify socket, let's enable it automatically. */ |
775 | if (s->notify_access == NOTIFY_NONE && | |
3bd28bf7 | 776 | (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) || s->watchdog_usec > 0 || s->n_fd_store_max > 0)) |
8545f7ce LP |
777 | s->notify_access = NOTIFY_MAIN; |
778 | ||
afcfaa69 LP |
779 | /* If no OOM policy was explicitly set, then default to the configure default OOM policy. Except when |
780 | * delegation is on, in that case it we assume the payload knows better what to do and can process | |
5238e957 | 781 | * things in a more focused way. */ |
afcfaa69 LP |
782 | if (s->oom_policy < 0) |
783 | s->oom_policy = s->cgroup_context.delegate ? OOM_CONTINUE : UNIT(s)->manager->default_oom_policy; | |
784 | ||
785 | /* Let the kernel do the killing if that's requested. */ | |
786 | s->cgroup_context.memory_oom_group = s->oom_policy == OOM_KILL; | |
787 | ||
45f06b34 LP |
788 | r = service_add_default_dependencies(s); |
789 | if (r < 0) | |
790 | return r; | |
8545f7ce | 791 | |
45f06b34 LP |
792 | r = service_setup_bus_name(s); |
793 | if (r < 0) | |
794 | return r; | |
8545f7ce LP |
795 | |
796 | return 0; | |
797 | } | |
798 | ||
e537352b | 799 | static int service_load(Unit *u) { |
e537352b | 800 | Service *s = SERVICE(u); |
8bb2d17d | 801 | int r; |
e537352b | 802 | |
e0cfed4c | 803 | r = unit_load_fragment_and_dropin(u, true); |
c2756a68 | 804 | if (r < 0) |
5cb5a6ff LP |
805 | return r; |
806 | ||
e0cfed4c ZJS |
807 | if (u->load_state != UNIT_LOADED) |
808 | return 0; | |
034c6ed7 | 809 | |
23a177ef | 810 | /* This is a new unit? Then let's add in some extras */ |
e0cfed4c ZJS |
811 | r = service_add_extras(s); |
812 | if (r < 0) | |
813 | return r; | |
8e274523 | 814 | |
243b1432 | 815 | return service_verify(s); |
034c6ed7 LP |
816 | } |
817 | ||
87f0e418 | 818 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 819 | ServiceExecCommand c; |
87f0e418 | 820 | Service *s = SERVICE(u); |
47be870b | 821 | const char *prefix2; |
5cb5a6ff LP |
822 | |
823 | assert(s); | |
824 | ||
4c940960 | 825 | prefix = strempty(prefix); |
63c372cb | 826 | prefix2 = strjoina(prefix, "\t"); |
44d8db9e | 827 | |
5cb5a6ff | 828 | fprintf(f, |
81a2b7ce | 829 | "%sService State: %s\n" |
f42806df LP |
830 | "%sResult: %s\n" |
831 | "%sReload Result: %s\n" | |
4c2f5842 | 832 | "%sClean Result: %s\n" |
81a2b7ce | 833 | "%sPermissionsStartOnly: %s\n" |
8e274523 | 834 | "%sRootDirectoryStartOnly: %s\n" |
02ee865a | 835 | "%sRemainAfterExit: %s\n" |
3185a36b | 836 | "%sGuessMainPID: %s\n" |
c952c6ec | 837 | "%sType: %s\n" |
2cf3143a | 838 | "%sRestart: %s\n" |
308d72dc | 839 | "%sNotifyAccess: %s\n" |
afcfaa69 | 840 | "%sNotifyState: %s\n" |
3bd28bf7 LP |
841 | "%sOOMPolicy: %s\n" |
842 | "%sReloadSignal: %s\n", | |
81a2b7ce | 843 | prefix, service_state_to_string(s->state), |
f42806df LP |
844 | prefix, service_result_to_string(s->result), |
845 | prefix, service_result_to_string(s->reload_result), | |
4c2f5842 | 846 | prefix, service_result_to_string(s->clean_result), |
81a2b7ce | 847 | prefix, yes_no(s->permissions_start_only), |
8e274523 | 848 | prefix, yes_no(s->root_directory_start_only), |
02ee865a | 849 | prefix, yes_no(s->remain_after_exit), |
3185a36b | 850 | prefix, yes_no(s->guess_main_pid), |
c952c6ec | 851 | prefix, service_type_to_string(s->type), |
2cf3143a | 852 | prefix, service_restart_to_string(s->restart), |
308d72dc | 853 | prefix, notify_access_to_string(s->notify_access), |
afcfaa69 | 854 | prefix, notify_state_to_string(s->notify_state), |
3bd28bf7 LP |
855 | prefix, oom_policy_to_string(s->oom_policy), |
856 | prefix, signal_to_string(s->reload_signal)); | |
5cb5a6ff | 857 | |
70123e68 LP |
858 | if (s->control_pid > 0) |
859 | fprintf(f, | |
ccd06097 ZJS |
860 | "%sControl PID: "PID_FMT"\n", |
861 | prefix, s->control_pid); | |
70123e68 LP |
862 | |
863 | if (s->main_pid > 0) | |
864 | fprintf(f, | |
ccd06097 | 865 | "%sMain PID: "PID_FMT"\n" |
6dfa5494 LP |
866 | "%sMain PID Known: %s\n" |
867 | "%sMain PID Alien: %s\n", | |
ccd06097 | 868 | prefix, s->main_pid, |
6dfa5494 LP |
869 | prefix, yes_no(s->main_pid_known), |
870 | prefix, yes_no(s->main_pid_alien)); | |
70123e68 | 871 | |
034c6ed7 LP |
872 | if (s->pid_file) |
873 | fprintf(f, | |
874 | "%sPIDFile: %s\n", | |
875 | prefix, s->pid_file); | |
876 | ||
05e343b7 LP |
877 | if (s->bus_name) |
878 | fprintf(f, | |
879 | "%sBusName: %s\n" | |
880 | "%sBus Name Good: %s\n", | |
881 | prefix, s->bus_name, | |
882 | prefix, yes_no(s->bus_name_good)); | |
883 | ||
9dfb64f8 ZJS |
884 | if (UNIT_ISSET(s->accept_socket)) |
885 | fprintf(f, | |
886 | "%sAccept Socket: %s\n", | |
887 | prefix, UNIT_DEREF(s->accept_socket)->id); | |
888 | ||
c9d41699 YW |
889 | fprintf(f, |
890 | "%sRestartSec: %s\n" | |
891 | "%sTimeoutStartSec: %s\n" | |
bf760801 JK |
892 | "%sTimeoutStopSec: %s\n" |
893 | "%sTimeoutStartFailureMode: %s\n" | |
894 | "%sTimeoutStopFailureMode: %s\n", | |
5291f26d ZJS |
895 | prefix, FORMAT_TIMESPAN(s->restart_usec, USEC_PER_SEC), |
896 | prefix, FORMAT_TIMESPAN(s->timeout_start_usec, USEC_PER_SEC), | |
897 | prefix, FORMAT_TIMESPAN(s->timeout_stop_usec, USEC_PER_SEC), | |
bf760801 JK |
898 | prefix, service_timeout_failure_mode_to_string(s->timeout_start_failure_mode), |
899 | prefix, service_timeout_failure_mode_to_string(s->timeout_stop_failure_mode)); | |
dcab85be YW |
900 | |
901 | if (s->timeout_abort_set) | |
902 | fprintf(f, | |
903 | "%sTimeoutAbortSec: %s\n", | |
5291f26d | 904 | prefix, FORMAT_TIMESPAN(s->timeout_abort_usec, USEC_PER_SEC)); |
dcab85be YW |
905 | |
906 | fprintf(f, | |
907 | "%sRuntimeMaxSec: %s\n" | |
5918a933 | 908 | "%sRuntimeRandomizedExtraSec: %s\n" |
dcab85be | 909 | "%sWatchdogSec: %s\n", |
5291f26d | 910 | prefix, FORMAT_TIMESPAN(s->runtime_max_usec, USEC_PER_SEC), |
5918a933 | 911 | prefix, FORMAT_TIMESPAN(s->runtime_rand_extra_usec, USEC_PER_SEC), |
5291f26d | 912 | prefix, FORMAT_TIMESPAN(s->watchdog_usec, USEC_PER_SEC)); |
c9d41699 | 913 | |
4819ff03 | 914 | kill_context_dump(&s->kill_context, f, prefix); |
5cb5a6ff LP |
915 | exec_context_dump(&s->exec_context, f, prefix); |
916 | ||
e537352b | 917 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
5cb5a6ff | 918 | |
44d8db9e LP |
919 | if (!s->exec_command[c]) |
920 | continue; | |
921 | ||
40d50879 | 922 | fprintf(f, "%s-> %s:\n", |
94f04347 | 923 | prefix, service_exec_command_to_string(c)); |
44d8db9e LP |
924 | |
925 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 926 | } |
44d8db9e | 927 | |
8c47c732 LP |
928 | if (s->status_text) |
929 | fprintf(f, "%sStatus Text: %s\n", | |
930 | prefix, s->status_text); | |
a354329f | 931 | |
ece174c5 | 932 | if (s->n_fd_store_max > 0) |
a354329f LP |
933 | fprintf(f, |
934 | "%sFile Descriptor Store Max: %u\n" | |
da6053d0 | 935 | "%sFile Descriptor Store Current: %zu\n", |
a354329f LP |
936 | prefix, s->n_fd_store_max, |
937 | prefix, s->n_fd_store); | |
18f573aa | 938 | |
cd48e23f RP |
939 | if (s->open_files) |
940 | LIST_FOREACH(open_files, of, s->open_files) { | |
941 | _cleanup_free_ char *ofs = NULL; | |
942 | int r; | |
943 | ||
944 | r = open_file_to_string(of, &ofs); | |
945 | if (r < 0) { | |
946 | log_debug_errno(r, | |
947 | "Failed to convert OpenFile= setting to string, ignoring: %m"); | |
948 | continue; | |
949 | } | |
950 | ||
951 | fprintf(f, "%sOpen File: %s\n", prefix, ofs); | |
952 | } | |
953 | ||
bc0623df | 954 | cgroup_context_dump(UNIT(s), f, prefix); |
5cb5a6ff LP |
955 | } |
956 | ||
db256aab LP |
957 | static int service_is_suitable_main_pid(Service *s, pid_t pid, int prio) { |
958 | Unit *owner; | |
959 | ||
960 | assert(s); | |
961 | assert(pid_is_valid(pid)); | |
962 | ||
963 | /* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the | |
964 | * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is | |
965 | * good */ | |
966 | ||
d85ff944 YW |
967 | if (pid == getpid_cached() || pid == 1) |
968 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the manager, refusing.", pid); | |
db256aab | 969 | |
d85ff944 YW |
970 | if (pid == s->control_pid) |
971 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(EPERM), "New main PID "PID_FMT" is the control process, refusing.", pid); | |
db256aab | 972 | |
d85ff944 YW |
973 | if (!pid_is_alive(pid)) |
974 | return log_unit_full_errno(UNIT(s), prio, SYNTHETIC_ERRNO(ESRCH), "New main PID "PID_FMT" does not exist or is a zombie.", pid); | |
db256aab LP |
975 | |
976 | owner = manager_get_unit_by_pid(UNIT(s)->manager, pid); | |
977 | if (owner == UNIT(s)) { | |
978 | log_unit_debug(UNIT(s), "New main PID "PID_FMT" belongs to service, we are happy.", pid); | |
979 | return 1; /* Yay, it's definitely a good PID */ | |
980 | } | |
981 | ||
982 | return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */ | |
983 | } | |
984 | ||
c5419d42 | 985 | static int service_load_pid_file(Service *s, bool may_warn) { |
73969ab6 | 986 | bool questionable_pid_file = false; |
7fd1b19b | 987 | _cleanup_free_ char *k = NULL; |
254d1313 | 988 | _cleanup_close_ int fd = -EBADF; |
db256aab | 989 | int r, prio; |
5925dd3c | 990 | pid_t pid; |
034c6ed7 LP |
991 | |
992 | assert(s); | |
993 | ||
034c6ed7 | 994 | if (!s->pid_file) |
13230d5d | 995 | return -ENOENT; |
034c6ed7 | 996 | |
db256aab LP |
997 | prio = may_warn ? LOG_INFO : LOG_DEBUG; |
998 | ||
a5648b80 ZJS |
999 | r = chase_symlinks(s->pid_file, NULL, CHASE_SAFE, NULL, &fd); |
1000 | if (r == -ENOLINK) { | |
8ed6f81b YW |
1001 | log_unit_debug_errno(UNIT(s), r, |
1002 | "Potentially unsafe symlink chain, will now retry with relaxed checks: %s", s->pid_file); | |
73969ab6 LP |
1003 | |
1004 | questionable_pid_file = true; | |
1005 | ||
a5648b80 | 1006 | r = chase_symlinks(s->pid_file, NULL, 0, NULL, &fd); |
73969ab6 | 1007 | } |
a5648b80 | 1008 | if (r < 0) |
8ed6f81b YW |
1009 | return log_unit_full_errno(UNIT(s), prio, fd, |
1010 | "Can't open PID file %s (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state)); | |
db256aab | 1011 | |
a5648b80 ZJS |
1012 | /* Let's read the PID file now that we chased it down. But we need to convert the O_PATH fd |
1013 | * chase_symlinks() returned us into a proper fd first. */ | |
ddb6eeaf | 1014 | r = read_one_line_file(FORMAT_PROC_FD_PATH(fd), &k); |
db256aab | 1015 | if (r < 0) |
a5648b80 ZJS |
1016 | return log_unit_error_errno(UNIT(s), r, |
1017 | "Can't convert PID files %s O_PATH file descriptor to proper file descriptor: %m", | |
1018 | s->pid_file); | |
034c6ed7 | 1019 | |
5925dd3c | 1020 | r = parse_pid(k, &pid); |
db256aab | 1021 | if (r < 0) |
8ed6f81b | 1022 | return log_unit_full_errno(UNIT(s), prio, r, "Failed to parse PID from file %s: %m", s->pid_file); |
db256aab LP |
1023 | |
1024 | if (s->main_pid_known && pid == s->main_pid) | |
1025 | return 0; | |
1026 | ||
1027 | r = service_is_suitable_main_pid(s, pid, prio); | |
1028 | if (r < 0) | |
5925dd3c | 1029 | return r; |
db256aab LP |
1030 | if (r == 0) { |
1031 | struct stat st; | |
406eaf93 | 1032 | |
d85ff944 YW |
1033 | if (questionable_pid_file) |
1034 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), | |
1035 | "Refusing to accept PID outside of service control group, acquired through unsafe symlink chain: %s", s->pid_file); | |
73969ab6 | 1036 | |
db256aab LP |
1037 | /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */ |
1038 | ||
1039 | if (fstat(fd, &st) < 0) | |
1040 | return log_unit_error_errno(UNIT(s), errno, "Failed to fstat() PID file O_PATH fd: %m"); | |
1041 | ||
d85ff944 YW |
1042 | if (st.st_uid != 0) |
1043 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EPERM), | |
1044 | "New main PID "PID_FMT" does not belong to service, and PID file is not owned by root. Refusing.", pid); | |
db256aab LP |
1045 | |
1046 | log_unit_debug(UNIT(s), "New main PID "PID_FMT" does not belong to service, but we'll accept it since PID file is owned by root.", pid); | |
e10c9985 YS |
1047 | } |
1048 | ||
db01f8b3 | 1049 | if (s->main_pid_known) { |
f2341e0a | 1050 | log_unit_debug(UNIT(s), "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid); |
8bb2d17d | 1051 | |
db01f8b3 MS |
1052 | service_unwatch_main_pid(s); |
1053 | s->main_pid_known = false; | |
3a111838 | 1054 | } else |
f2341e0a | 1055 | log_unit_debug(UNIT(s), "Main PID loaded: "PID_FMT, pid); |
db01f8b3 | 1056 | |
117dcc57 ZJS |
1057 | r = service_set_main_pid(s, pid); |
1058 | if (r < 0) | |
16f6025e LP |
1059 | return r; |
1060 | ||
f75f613d | 1061 | r = unit_watch_pid(UNIT(s), pid, false); |
e8b509d3 LP |
1062 | if (r < 0) /* FIXME: we need to do something here */ |
1063 | return log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" for service: %m", pid); | |
034c6ed7 | 1064 | |
db256aab | 1065 | return 1; |
034c6ed7 LP |
1066 | } |
1067 | ||
783e05d6 | 1068 | static void service_search_main_pid(Service *s) { |
efdb0237 | 1069 | pid_t pid = 0; |
4fbf50b3 LP |
1070 | int r; |
1071 | ||
1072 | assert(s); | |
1073 | ||
2aed63f4 | 1074 | /* If we know it anyway, don't ever fall back to unreliable |
3185a36b | 1075 | * heuristics */ |
4fbf50b3 | 1076 | if (s->main_pid_known) |
783e05d6 | 1077 | return; |
4fbf50b3 | 1078 | |
3185a36b | 1079 | if (!s->guess_main_pid) |
783e05d6 | 1080 | return; |
3185a36b | 1081 | |
4fbf50b3 LP |
1082 | assert(s->main_pid <= 0); |
1083 | ||
783e05d6 ZJS |
1084 | if (unit_search_main_pid(UNIT(s), &pid) < 0) |
1085 | return; | |
4fbf50b3 | 1086 | |
f2341e0a | 1087 | log_unit_debug(UNIT(s), "Main PID guessed: "PID_FMT, pid); |
783e05d6 ZJS |
1088 | if (service_set_main_pid(s, pid) < 0) |
1089 | return; | |
4fbf50b3 | 1090 | |
f75f613d | 1091 | r = unit_watch_pid(UNIT(s), pid, false); |
783e05d6 | 1092 | if (r < 0) |
4fbf50b3 | 1093 | /* FIXME: we need to do something here */ |
f2341e0a | 1094 | log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" from: %m", pid); |
4fbf50b3 LP |
1095 | } |
1096 | ||
034c6ed7 LP |
1097 | static void service_set_state(Service *s, ServiceState state) { |
1098 | ServiceState old_state; | |
e056b01d | 1099 | const UnitActiveState *table; |
842129f5 | 1100 | |
5cb5a6ff LP |
1101 | assert(s); |
1102 | ||
6fcbec6f LP |
1103 | if (s->state != state) |
1104 | bus_unit_send_pending_change_signal(UNIT(s), false); | |
1105 | ||
e056b01d LP |
1106 | table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
1107 | ||
034c6ed7 | 1108 | old_state = s->state; |
5cb5a6ff | 1109 | s->state = state; |
034c6ed7 | 1110 | |
3a111838 MS |
1111 | service_unwatch_pid_file(s); |
1112 | ||
842129f5 | 1113 | if (!IN_SET(state, |
31cd5f63 | 1114 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, |
36c16a7c | 1115 | SERVICE_RUNNING, |
3bd28bf7 | 1116 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, |
c87700a1 | 1117 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 1118 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
4c2f5842 LP |
1119 | SERVICE_AUTO_RESTART, |
1120 | SERVICE_CLEANING)) | |
5dcadb4c | 1121 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
034c6ed7 | 1122 | |
842129f5 LP |
1123 | if (!IN_SET(state, |
1124 | SERVICE_START, SERVICE_START_POST, | |
3bd28bf7 LP |
1125 | SERVICE_RUNNING, |
1126 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, | |
c87700a1 | 1127 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 1128 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
5e94833f | 1129 | service_unwatch_main_pid(s); |
867b3b7d LP |
1130 | s->main_command = NULL; |
1131 | } | |
034c6ed7 | 1132 | |
842129f5 | 1133 | if (!IN_SET(state, |
31cd5f63 | 1134 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, |
3bd28bf7 | 1135 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, |
c87700a1 | 1136 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 1137 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
4c2f5842 | 1138 | SERVICE_CLEANING)) { |
5e94833f | 1139 | service_unwatch_control_pid(s); |
034c6ed7 | 1140 | s->control_command = NULL; |
a16e1123 | 1141 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
e537352b | 1142 | } |
034c6ed7 | 1143 | |
50be4f4a | 1144 | if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) { |
a911bb9a | 1145 | unit_unwatch_all_pids(UNIT(s)); |
50be4f4a LP |
1146 | unit_dequeue_rewatch_pids(UNIT(s)); |
1147 | } | |
a911bb9a | 1148 | |
842129f5 | 1149 | if (!IN_SET(state, |
31cd5f63 | 1150 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, |
3bd28bf7 LP |
1151 | SERVICE_RUNNING, |
1152 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, | |
c87700a1 | 1153 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 1154 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) && |
5cc3985e | 1155 | !(state == SERVICE_DEAD && UNIT(s)->job)) |
4f2d528d LP |
1156 | service_close_socket_fd(s); |
1157 | ||
5686391b | 1158 | if (state != SERVICE_START) |
5dcadb4c | 1159 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
5686391b | 1160 | |
3bd28bf7 | 1161 | if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY)) |
a6927d7f MO |
1162 | service_stop_watchdog(s); |
1163 | ||
f6023656 LP |
1164 | /* For the inactive states unit_notify() will trim the cgroup, |
1165 | * but for exit we have to do that ourselves... */ | |
2c289ea8 | 1166 | if (state == SERVICE_EXITED && !MANAGER_IS_RELOADING(UNIT(s)->manager)) |
efdb0237 | 1167 | unit_prune_cgroup(UNIT(s)); |
f6023656 | 1168 | |
e537352b | 1169 | if (old_state != state) |
f2341e0a | 1170 | log_unit_debug(UNIT(s), "Changed %s -> %s", service_state_to_string(old_state), service_state_to_string(state)); |
acbb0225 | 1171 | |
2ad2e41a LP |
1172 | unit_notify(UNIT(s), table[old_state], table[state], |
1173 | (s->reload_result == SERVICE_SUCCESS ? 0 : UNIT_NOTIFY_RELOAD_FAILURE) | | |
42e6f549 | 1174 | (s->will_auto_restart ? UNIT_NOTIFY_WILL_AUTO_RESTART : 0)); |
034c6ed7 LP |
1175 | } |
1176 | ||
36c16a7c LP |
1177 | static usec_t service_coldplug_timeout(Service *s) { |
1178 | assert(s); | |
1179 | ||
1180 | switch (s->deserialized_state) { | |
1181 | ||
31cd5f63 | 1182 | case SERVICE_CONDITION: |
36c16a7c LP |
1183 | case SERVICE_START_PRE: |
1184 | case SERVICE_START: | |
1185 | case SERVICE_START_POST: | |
1186 | case SERVICE_RELOAD: | |
3bd28bf7 LP |
1187 | case SERVICE_RELOAD_SIGNAL: |
1188 | case SERVICE_RELOAD_NOTIFY: | |
36c16a7c LP |
1189 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_start_usec); |
1190 | ||
1191 | case SERVICE_RUNNING: | |
5918a933 | 1192 | return service_running_timeout(s); |
36c16a7c LP |
1193 | |
1194 | case SERVICE_STOP: | |
36c16a7c LP |
1195 | case SERVICE_STOP_SIGTERM: |
1196 | case SERVICE_STOP_SIGKILL: | |
1197 | case SERVICE_STOP_POST: | |
1198 | case SERVICE_FINAL_SIGTERM: | |
1199 | case SERVICE_FINAL_SIGKILL: | |
1200 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec); | |
1201 | ||
dc653bf4 | 1202 | case SERVICE_STOP_WATCHDOG: |
bf760801 | 1203 | case SERVICE_FINAL_WATCHDOG: |
dc653bf4 JK |
1204 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, service_timeout_abort_usec(s)); |
1205 | ||
36c16a7c LP |
1206 | case SERVICE_AUTO_RESTART: |
1207 | return usec_add(UNIT(s)->inactive_enter_timestamp.monotonic, s->restart_usec); | |
1208 | ||
4c2f5842 | 1209 | case SERVICE_CLEANING: |
12213aed | 1210 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->exec_context.timeout_clean_usec); |
4c2f5842 | 1211 | |
36c16a7c LP |
1212 | default: |
1213 | return USEC_INFINITY; | |
1214 | } | |
1215 | } | |
1216 | ||
be847e82 | 1217 | static int service_coldplug(Unit *u) { |
a16e1123 LP |
1218 | Service *s = SERVICE(u); |
1219 | int r; | |
1220 | ||
1221 | assert(s); | |
1222 | assert(s->state == SERVICE_DEAD); | |
1223 | ||
930d2838 LP |
1224 | if (s->deserialized_state == s->state) |
1225 | return 0; | |
6c12b52e | 1226 | |
e5d6dcce | 1227 | r = service_arm_timer(s, /* relative= */ false, service_coldplug_timeout(s)); |
36c16a7c LP |
1228 | if (r < 0) |
1229 | return r; | |
a911bb9a | 1230 | |
930d2838 LP |
1231 | if (s->main_pid > 0 && |
1232 | pid_is_unwaited(s->main_pid) && | |
9acac212 | 1233 | (IN_SET(s->deserialized_state, |
930d2838 | 1234 | SERVICE_START, SERVICE_START_POST, |
3bd28bf7 LP |
1235 | SERVICE_RUNNING, |
1236 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, | |
c87700a1 | 1237 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 1238 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) { |
f75f613d | 1239 | r = unit_watch_pid(UNIT(s), s->main_pid, false); |
930d2838 LP |
1240 | if (r < 0) |
1241 | return r; | |
1242 | } | |
bb242b7b | 1243 | |
930d2838 LP |
1244 | if (s->control_pid > 0 && |
1245 | pid_is_unwaited(s->control_pid) && | |
1246 | IN_SET(s->deserialized_state, | |
31cd5f63 | 1247 | SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, |
3bd28bf7 | 1248 | SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, |
c87700a1 | 1249 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 1250 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
4c2f5842 | 1251 | SERVICE_CLEANING)) { |
f75f613d | 1252 | r = unit_watch_pid(UNIT(s), s->control_pid, false); |
930d2838 LP |
1253 | if (r < 0) |
1254 | return r; | |
a16e1123 | 1255 | } |
92c1622e | 1256 | |
4c2f5842 | 1257 | if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART, SERVICE_CLEANING)) { |
50be4f4a | 1258 | (void) unit_enqueue_rewatch_pids(u); |
29206d46 | 1259 | (void) unit_setup_dynamic_creds(u); |
e8a565cb YW |
1260 | (void) unit_setup_exec_runtime(u); |
1261 | } | |
29206d46 | 1262 | |
3bd28bf7 | 1263 | if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY)) |
50be4f4a LP |
1264 | service_start_watchdog(s); |
1265 | ||
3ebcd323 ZJS |
1266 | if (UNIT_ISSET(s->accept_socket)) { |
1267 | Socket* socket = SOCKET(UNIT_DEREF(s->accept_socket)); | |
1268 | ||
1269 | if (socket->max_connections_per_source > 0) { | |
1270 | SocketPeer *peer; | |
1271 | ||
1272 | /* Make a best-effort attempt at bumping the connection count */ | |
1273 | if (socket_acquire_peer(socket, s->socket_fd, &peer) > 0) { | |
3fabebf4 LP |
1274 | socket_peer_unref(s->socket_peer); |
1275 | s->socket_peer = peer; | |
3ebcd323 ZJS |
1276 | } |
1277 | } | |
1278 | } | |
1279 | ||
930d2838 | 1280 | service_set_state(s, s->deserialized_state); |
a16e1123 LP |
1281 | return 0; |
1282 | } | |
1283 | ||
25b583d7 LP |
1284 | static int service_collect_fds( |
1285 | Service *s, | |
1286 | int **fds, | |
1287 | char ***fd_names, | |
1288 | size_t *n_socket_fds, | |
1289 | size_t *n_storage_fds) { | |
4c47affc | 1290 | |
8dd4c05b | 1291 | _cleanup_strv_free_ char **rfd_names = NULL; |
a354329f | 1292 | _cleanup_free_ int *rfds = NULL; |
25b583d7 | 1293 | size_t rn_socket_fds = 0, rn_storage_fds = 0; |
4c47affc | 1294 | int r; |
44d8db9e LP |
1295 | |
1296 | assert(s); | |
1297 | assert(fds); | |
8dd4c05b | 1298 | assert(fd_names); |
9b141911 | 1299 | assert(n_socket_fds); |
25b583d7 | 1300 | assert(n_storage_fds); |
44d8db9e | 1301 | |
8dd4c05b | 1302 | if (s->socket_fd >= 0) { |
6cf6bbc2 | 1303 | |
8dd4c05b | 1304 | /* Pass the per-connection socket */ |
57020a3a | 1305 | |
8dd4c05b LP |
1306 | rfds = new(int, 1); |
1307 | if (!rfds) | |
1308 | return -ENOMEM; | |
1309 | rfds[0] = s->socket_fd; | |
57020a3a | 1310 | |
bea1a013 | 1311 | rfd_names = strv_new("connection"); |
8dd4c05b LP |
1312 | if (!rfd_names) |
1313 | return -ENOMEM; | |
44d8db9e | 1314 | |
4c47affc | 1315 | rn_socket_fds = 1; |
8dd4c05b | 1316 | } else { |
8dd4c05b | 1317 | Unit *u; |
44d8db9e | 1318 | |
8dd4c05b | 1319 | /* Pass all our configured sockets for singleton services */ |
44d8db9e | 1320 | |
15ed3c3a | 1321 | UNIT_FOREACH_DEPENDENCY(u, UNIT(s), UNIT_ATOM_TRIGGERED_BY) { |
8dd4c05b LP |
1322 | _cleanup_free_ int *cfds = NULL; |
1323 | Socket *sock; | |
1324 | int cn_fds; | |
44d8db9e | 1325 | |
8dd4c05b LP |
1326 | if (u->type != UNIT_SOCKET) |
1327 | continue; | |
44d8db9e | 1328 | |
8dd4c05b | 1329 | sock = SOCKET(u); |
a354329f | 1330 | |
8dd4c05b LP |
1331 | cn_fds = socket_collect_fds(sock, &cfds); |
1332 | if (cn_fds < 0) | |
1333 | return cn_fds; | |
44d8db9e | 1334 | |
8dd4c05b LP |
1335 | if (cn_fds <= 0) |
1336 | continue; | |
79c7626d | 1337 | |
8dd4c05b | 1338 | if (!rfds) { |
1cc6c93a | 1339 | rfds = TAKE_PTR(cfds); |
4c47affc | 1340 | rn_socket_fds = cn_fds; |
8dd4c05b LP |
1341 | } else { |
1342 | int *t; | |
1343 | ||
62d74c78 | 1344 | t = reallocarray(rfds, rn_socket_fds + cn_fds, sizeof(int)); |
8dd4c05b LP |
1345 | if (!t) |
1346 | return -ENOMEM; | |
1347 | ||
4c47affc | 1348 | memcpy(t + rn_socket_fds, cfds, cn_fds * sizeof(int)); |
8dd4c05b LP |
1349 | |
1350 | rfds = t; | |
4c47affc | 1351 | rn_socket_fds += cn_fds; |
8dd4c05b LP |
1352 | } |
1353 | ||
1354 | r = strv_extend_n(&rfd_names, socket_fdname(sock), cn_fds); | |
1355 | if (r < 0) | |
1356 | return r; | |
44d8db9e LP |
1357 | } |
1358 | } | |
1359 | ||
a354329f | 1360 | if (s->n_fd_store > 0) { |
25b583d7 | 1361 | size_t n_fds; |
8dd4c05b | 1362 | char **nl; |
a354329f LP |
1363 | int *t; |
1364 | ||
62d74c78 | 1365 | t = reallocarray(rfds, rn_socket_fds + s->n_fd_store, sizeof(int)); |
a354329f LP |
1366 | if (!t) |
1367 | return -ENOMEM; | |
1368 | ||
1369 | rfds = t; | |
8dd4c05b | 1370 | |
62d74c78 | 1371 | nl = reallocarray(rfd_names, rn_socket_fds + s->n_fd_store + 1, sizeof(char *)); |
8dd4c05b LP |
1372 | if (!nl) |
1373 | return -ENOMEM; | |
1374 | ||
1375 | rfd_names = nl; | |
4c47affc | 1376 | n_fds = rn_socket_fds; |
8dd4c05b LP |
1377 | |
1378 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
4c47affc FB |
1379 | rfds[n_fds] = fs->fd; |
1380 | rfd_names[n_fds] = strdup(strempty(fs->fdname)); | |
1381 | if (!rfd_names[n_fds]) | |
8dd4c05b LP |
1382 | return -ENOMEM; |
1383 | ||
4c47affc FB |
1384 | rn_storage_fds++; |
1385 | n_fds++; | |
8dd4c05b LP |
1386 | } |
1387 | ||
4c47affc | 1388 | rfd_names[n_fds] = NULL; |
a354329f LP |
1389 | } |
1390 | ||
1cc6c93a YW |
1391 | *fds = TAKE_PTR(rfds); |
1392 | *fd_names = TAKE_PTR(rfd_names); | |
9b141911 | 1393 | *n_socket_fds = rn_socket_fds; |
4c47affc | 1394 | *n_storage_fds = rn_storage_fds; |
3e33402a | 1395 | |
4c47affc | 1396 | return 0; |
44d8db9e LP |
1397 | } |
1398 | ||
5686391b LP |
1399 | static int service_allocate_exec_fd_event_source( |
1400 | Service *s, | |
1401 | int fd, | |
1402 | sd_event_source **ret_event_source) { | |
1403 | ||
1404 | _cleanup_(sd_event_source_unrefp) sd_event_source *source = NULL; | |
1405 | int r; | |
1406 | ||
1407 | assert(s); | |
1408 | assert(fd >= 0); | |
1409 | assert(ret_event_source); | |
1410 | ||
1411 | r = sd_event_add_io(UNIT(s)->manager->event, &source, fd, 0, service_dispatch_exec_io, s); | |
1412 | if (r < 0) | |
1413 | return log_unit_error_errno(UNIT(s), r, "Failed to allocate exec_fd event source: %m"); | |
1414 | ||
1415 | /* This is a bit lower priority than SIGCHLD, as that carries a lot more interesting failure information */ | |
1416 | ||
1417 | r = sd_event_source_set_priority(source, SD_EVENT_PRIORITY_NORMAL-3); | |
1418 | if (r < 0) | |
1419 | return log_unit_error_errno(UNIT(s), r, "Failed to adjust priority of exec_fd event source: %m"); | |
1420 | ||
bc989831 | 1421 | (void) sd_event_source_set_description(source, "service exec_fd"); |
5686391b LP |
1422 | |
1423 | r = sd_event_source_set_io_fd_own(source, true); | |
1424 | if (r < 0) | |
1425 | return log_unit_error_errno(UNIT(s), r, "Failed to pass ownership of fd to event source: %m"); | |
1426 | ||
1427 | *ret_event_source = TAKE_PTR(source); | |
1428 | return 0; | |
1429 | } | |
1430 | ||
1431 | static int service_allocate_exec_fd( | |
1432 | Service *s, | |
1433 | sd_event_source **ret_event_source, | |
e78695d4 | 1434 | int *ret_exec_fd) { |
5686391b | 1435 | |
19ee48a6 | 1436 | _cleanup_close_pair_ int p[] = PIPE_EBADF; |
5686391b LP |
1437 | int r; |
1438 | ||
1439 | assert(s); | |
1440 | assert(ret_event_source); | |
1441 | assert(ret_exec_fd); | |
1442 | ||
1443 | if (pipe2(p, O_CLOEXEC|O_NONBLOCK) < 0) | |
1444 | return log_unit_error_errno(UNIT(s), errno, "Failed to allocate exec_fd pipe: %m"); | |
1445 | ||
1446 | r = service_allocate_exec_fd_event_source(s, p[0], ret_event_source); | |
1447 | if (r < 0) | |
1448 | return r; | |
1449 | ||
e78695d4 | 1450 | TAKE_FD(p[0]); |
5686391b LP |
1451 | *ret_exec_fd = TAKE_FD(p[1]); |
1452 | ||
1453 | return 0; | |
1454 | } | |
1455 | ||
6375bd20 JW |
1456 | static bool service_exec_needs_notify_socket(Service *s, ExecFlags flags) { |
1457 | assert(s); | |
1458 | ||
1459 | /* Notifications are accepted depending on the process and | |
1460 | * the access setting of the service: | |
1461 | * process: \ access: NONE MAIN EXEC ALL | |
1462 | * main no yes yes yes | |
1463 | * control no no yes yes | |
1464 | * other (forked) no no no yes */ | |
1465 | ||
1466 | if (flags & EXEC_IS_CONTROL) | |
1467 | /* A control process */ | |
1468 | return IN_SET(s->notify_access, NOTIFY_EXEC, NOTIFY_ALL); | |
1469 | ||
1470 | /* We only spawn main processes and control processes, so any | |
1471 | * process that is not a control process is a main process */ | |
1472 | return s->notify_access != NOTIFY_NONE; | |
1473 | } | |
1474 | ||
95c81c55 LB |
1475 | static Service *service_get_triggering_service(Service *s) { |
1476 | Unit *candidate = NULL, *other; | |
cdebedb4 | 1477 | |
95c81c55 | 1478 | assert(s); |
cdebedb4 | 1479 | |
95c81c55 LB |
1480 | /* Return the service which triggered service 's', this means dependency |
1481 | * types which include the UNIT_ATOM_ON_{FAILURE,SUCCESS}_OF atoms. | |
cdebedb4 | 1482 | * |
95c81c55 LB |
1483 | * N.B. if there are multiple services which could trigger 's' via OnFailure= |
1484 | * or OnSuccess= then we return NULL. This is since we don't know from which | |
1485 | * one to propagate the exit status. */ | |
1486 | ||
1487 | UNIT_FOREACH_DEPENDENCY(other, UNIT(s), UNIT_ATOM_ON_FAILURE_OF) { | |
7a5049c7 ZJS |
1488 | if (candidate) |
1489 | goto have_other; | |
95c81c55 LB |
1490 | candidate = other; |
1491 | } | |
1492 | ||
1493 | UNIT_FOREACH_DEPENDENCY(other, UNIT(s), UNIT_ATOM_ON_SUCCESS_OF) { | |
7a5049c7 ZJS |
1494 | if (candidate) |
1495 | goto have_other; | |
95c81c55 | 1496 | candidate = other; |
cdebedb4 PM |
1497 | } |
1498 | ||
95c81c55 | 1499 | return SERVICE(candidate); |
7a5049c7 ZJS |
1500 | |
1501 | have_other: | |
1502 | log_unit_warning(UNIT(s), "multiple trigger source candidates for exit status propagation (%s, %s), skipping.", | |
1503 | candidate->id, other->id); | |
1504 | return NULL; | |
cdebedb4 PM |
1505 | } |
1506 | ||
edbf8984 ZJS |
1507 | static int service_spawn_internal( |
1508 | const char *caller, | |
81a2b7ce LP |
1509 | Service *s, |
1510 | ExecCommand *c, | |
21b2ce39 | 1511 | usec_t timeout, |
c39f1ce2 | 1512 | ExecFlags flags, |
e78695d4 | 1513 | pid_t *ret_pid) { |
81a2b7ce | 1514 | |
b9c04eaf | 1515 | _cleanup_(exec_params_clear) ExecParameters exec_params = { |
c3f8a065 | 1516 | .flags = flags, |
254d1313 ZJS |
1517 | .stdin_fd = -EBADF, |
1518 | .stdout_fd = -EBADF, | |
1519 | .stderr_fd = -EBADF, | |
1520 | .exec_fd = -EBADF, | |
9fa95f85 | 1521 | }; |
5686391b | 1522 | _cleanup_(sd_event_source_unrefp) sd_event_source *exec_fd_source = NULL; |
c3f8a065 LP |
1523 | _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL; |
1524 | size_t n_env = 0; | |
3c7416b6 | 1525 | pid_t pid; |
8dd4c05b LP |
1526 | int r; |
1527 | ||
edbf8984 | 1528 | assert(caller); |
034c6ed7 LP |
1529 | assert(s); |
1530 | assert(c); | |
e78695d4 | 1531 | assert(ret_pid); |
034c6ed7 | 1532 | |
edbf8984 ZJS |
1533 | log_unit_debug(UNIT(s), "Will spawn child (%s): %s", caller, c->path); |
1534 | ||
78f93209 | 1535 | r = unit_prepare_exec(UNIT(s)); /* This realizes the cgroup, among other things */ |
3c7416b6 LP |
1536 | if (r < 0) |
1537 | return r; | |
1538 | ||
bc989831 ZJS |
1539 | assert(!s->exec_fd_event_source); |
1540 | ||
9c1a61ad LP |
1541 | if (flags & EXEC_IS_CONTROL) { |
1542 | /* If this is a control process, mask the permissions/chroot application if this is requested. */ | |
1543 | if (s->permissions_start_only) | |
1703fa41 | 1544 | exec_params.flags &= ~EXEC_APPLY_SANDBOXING; |
9c1a61ad LP |
1545 | if (s->root_directory_start_only) |
1546 | exec_params.flags &= ~EXEC_APPLY_CHROOT; | |
1547 | } | |
1548 | ||
c39f1ce2 | 1549 | if ((flags & EXEC_PASS_FDS) || |
6cf6bbc2 LP |
1550 | s->exec_context.std_input == EXEC_INPUT_SOCKET || |
1551 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1552 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1553 | ||
c3f8a065 LP |
1554 | r = service_collect_fds(s, |
1555 | &exec_params.fds, | |
1556 | &exec_params.fd_names, | |
1557 | &exec_params.n_socket_fds, | |
1558 | &exec_params.n_storage_fds); | |
8dd4c05b | 1559 | if (r < 0) |
36c16a7c | 1560 | return r; |
6cf6bbc2 | 1561 | |
cd48e23f RP |
1562 | exec_params.open_files = s->open_files; |
1563 | ||
c3f8a065 | 1564 | log_unit_debug(UNIT(s), "Passing %zu fds to service", exec_params.n_socket_fds + exec_params.n_storage_fds); |
4f2d528d | 1565 | } |
44d8db9e | 1566 | |
5686391b | 1567 | if (!FLAGS_SET(flags, EXEC_IS_CONTROL) && s->type == SERVICE_EXEC) { |
c3f8a065 | 1568 | r = service_allocate_exec_fd(s, &exec_fd_source, &exec_params.exec_fd); |
5686391b LP |
1569 | if (r < 0) |
1570 | return r; | |
1571 | } | |
1572 | ||
e5d6dcce | 1573 | r = service_arm_timer(s, /* relative= */ true, timeout); |
36c16a7c LP |
1574 | if (r < 0) |
1575 | return r; | |
034c6ed7 | 1576 | |
95c81c55 | 1577 | our_env = new0(char*, 12); |
36c16a7c LP |
1578 | if (!our_env) |
1579 | return -ENOMEM; | |
c952c6ec | 1580 | |
3bdc25a4 | 1581 | if (service_exec_needs_notify_socket(s, flags)) { |
36c16a7c LP |
1582 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) |
1583 | return -ENOMEM; | |
c952c6ec | 1584 | |
3bdc25a4 LP |
1585 | exec_params.notify_socket = UNIT(s)->manager->notify_socket; |
1586 | } | |
1587 | ||
2105e76a | 1588 | if (s->main_pid > 0) |
36c16a7c LP |
1589 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) |
1590 | return -ENOMEM; | |
2105e76a | 1591 | |
c39f1ce2 | 1592 | if (MANAGER_IS_USER(UNIT(s)->manager)) |
df0ff127 | 1593 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid_cached()) < 0) |
36c16a7c | 1594 | return -ENOMEM; |
97ae63e2 | 1595 | |
dcf3c3c3 LP |
1596 | if (s->pid_file) |
1597 | if (asprintf(our_env + n_env++, "PIDFILE=%s", s->pid_file) < 0) | |
1598 | return -ENOMEM; | |
1599 | ||
8dd4c05b | 1600 | if (s->socket_fd >= 0) { |
3b1c5241 SL |
1601 | union sockaddr_union sa; |
1602 | socklen_t salen = sizeof(sa); | |
1603 | ||
f56e7bfe LP |
1604 | /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something |
1605 | * useful. Note that we do this only when we are still connected at this point in time, which we might | |
1606 | * very well not be. Hence we ignore all errors when retrieving peer information (as that might result | |
1607 | * in ENOTCONN), and just use whate we can use. */ | |
f2dbd059 | 1608 | |
f56e7bfe LP |
1609 | if (getpeername(s->socket_fd, &sa.sa, &salen) >= 0 && |
1610 | IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { | |
3b1c5241 SL |
1611 | _cleanup_free_ char *addr = NULL; |
1612 | char *t; | |
882ac6e7 | 1613 | unsigned port; |
3b1c5241 SL |
1614 | |
1615 | r = sockaddr_pretty(&sa.sa, salen, true, false, &addr); | |
1616 | if (r < 0) | |
36c16a7c | 1617 | return r; |
3b1c5241 | 1618 | |
b910cc72 | 1619 | t = strjoin("REMOTE_ADDR=", addr); |
36c16a7c LP |
1620 | if (!t) |
1621 | return -ENOMEM; | |
3b1c5241 SL |
1622 | our_env[n_env++] = t; |
1623 | ||
882ac6e7 SH |
1624 | r = sockaddr_port(&sa.sa, &port); |
1625 | if (r < 0) | |
1626 | return r; | |
3b1c5241 | 1627 | |
36c16a7c LP |
1628 | if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) |
1629 | return -ENOMEM; | |
3b1c5241 SL |
1630 | our_env[n_env++] = t; |
1631 | } | |
1632 | } | |
1633 | ||
95c81c55 LB |
1634 | Service *env_source = NULL; |
1635 | const char *monitor_prefix; | |
136dc4c4 | 1636 | if (flags & EXEC_SETENV_RESULT) { |
95c81c55 LB |
1637 | env_source = s; |
1638 | monitor_prefix = ""; | |
1639 | } else if (flags & EXEC_SETENV_MONITOR_RESULT) { | |
1640 | env_source = service_get_triggering_service(s); | |
1641 | monitor_prefix = "MONITOR_"; | |
1642 | } | |
1643 | ||
1644 | if (env_source) { | |
1645 | if (asprintf(our_env + n_env++, "%sSERVICE_RESULT=%s", monitor_prefix, service_result_to_string(env_source->result)) < 0) | |
136dc4c4 LP |
1646 | return -ENOMEM; |
1647 | ||
95c81c55 LB |
1648 | if (env_source->main_exec_status.pid > 0 && |
1649 | dual_timestamp_is_set(&env_source->main_exec_status.exit_timestamp)) { | |
1650 | if (asprintf(our_env + n_env++, "%sEXIT_CODE=%s", monitor_prefix, sigchld_code_to_string(env_source->main_exec_status.code)) < 0) | |
136dc4c4 LP |
1651 | return -ENOMEM; |
1652 | ||
95c81c55 LB |
1653 | if (env_source->main_exec_status.code == CLD_EXITED) |
1654 | r = asprintf(our_env + n_env++, "%sEXIT_STATUS=%i", monitor_prefix, env_source->main_exec_status.status); | |
136dc4c4 | 1655 | else |
95c81c55 | 1656 | r = asprintf(our_env + n_env++, "%sEXIT_STATUS=%s", monitor_prefix, signal_to_string(env_source->main_exec_status.status)); |
cdebedb4 | 1657 | |
136dc4c4 LP |
1658 | if (r < 0) |
1659 | return -ENOMEM; | |
1660 | } | |
cdebedb4 | 1661 | |
95c81c55 LB |
1662 | if (env_source != s) { |
1663 | if (!sd_id128_is_null(UNIT(env_source)->invocation_id)) { | |
1664 | r = asprintf(our_env + n_env++, "%sINVOCATION_ID=" SD_ID128_FORMAT_STR, | |
1665 | monitor_prefix, SD_ID128_FORMAT_VAL(UNIT(env_source)->invocation_id)); | |
1666 | if (r < 0) | |
1667 | return -ENOMEM; | |
1668 | } | |
1669 | ||
1670 | if (asprintf(our_env + n_env++, "%sUNIT=%s", monitor_prefix, UNIT(env_source)->id) < 0) | |
1671 | return -ENOMEM; | |
cdebedb4 | 1672 | } |
136dc4c4 LP |
1673 | } |
1674 | ||
48b92b37 LB |
1675 | if (UNIT(s)->activation_details) { |
1676 | r = activation_details_append_env(UNIT(s)->activation_details, &our_env); | |
1677 | if (r < 0) | |
1678 | return r; | |
1679 | /* The number of env vars added here can vary, rather than keeping the allocation block in | |
1680 | * sync manually, these functions simply use the strv methods to append to it, so we need | |
1681 | * to update n_env when we are done in case of future usage. */ | |
1682 | n_env += r; | |
1683 | } | |
1684 | ||
1ad6e8b3 LP |
1685 | r = unit_set_exec_params(UNIT(s), &exec_params); |
1686 | if (r < 0) | |
1687 | return r; | |
3536f49e | 1688 | |
4ab3d29f | 1689 | final_env = strv_env_merge(exec_params.environment, our_env); |
36c16a7c LP |
1690 | if (!final_env) |
1691 | return -ENOMEM; | |
c952c6ec | 1692 | |
ac647978 | 1693 | /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */ |
de90700f | 1694 | SET_FLAG(exec_params.flags, EXEC_NSS_DYNAMIC_BYPASS, |
ac647978 | 1695 | MANAGER_IS_SYSTEM(UNIT(s)->manager) && unit_has_name(UNIT(s), SPECIAL_DBUS_SERVICE)); |
4ad49000 | 1696 | |
b9c04eaf | 1697 | strv_free_and_replace(exec_params.environment, final_env); |
34b3f625 | 1698 | exec_params.watchdog_usec = service_get_watchdog_usec(s); |
a34ceba6 | 1699 | exec_params.selinux_context_net = s->socket_fd_selinux_context_net; |
9fa95f85 DM |
1700 | if (s->type == SERVICE_IDLE) |
1701 | exec_params.idle_pipe = UNIT(s)->manager->idle_pipe; | |
a34ceba6 LP |
1702 | exec_params.stdin_fd = s->stdin_fd; |
1703 | exec_params.stdout_fd = s->stdout_fd; | |
1704 | exec_params.stderr_fd = s->stderr_fd; | |
9fa95f85 | 1705 | |
f2341e0a LP |
1706 | r = exec_spawn(UNIT(s), |
1707 | c, | |
9e2f7c11 | 1708 | &s->exec_context, |
9fa95f85 | 1709 | &exec_params, |
613b411c | 1710 | s->exec_runtime, |
29206d46 | 1711 | &s->dynamic_creds, |
9e2f7c11 | 1712 | &pid); |
9e2f7c11 | 1713 | if (r < 0) |
36c16a7c | 1714 | return r; |
034c6ed7 | 1715 | |
5686391b LP |
1716 | s->exec_fd_event_source = TAKE_PTR(exec_fd_source); |
1717 | s->exec_fd_hot = false; | |
1718 | ||
f75f613d FB |
1719 | r = unit_watch_pid(UNIT(s), pid, true); |
1720 | if (r < 0) | |
36c16a7c | 1721 | return r; |
034c6ed7 | 1722 | |
e78695d4 | 1723 | *ret_pid = pid; |
034c6ed7 | 1724 | |
5cb5a6ff | 1725 | return 0; |
034c6ed7 LP |
1726 | } |
1727 | ||
80876c20 LP |
1728 | static int main_pid_good(Service *s) { |
1729 | assert(s); | |
1730 | ||
51894d70 | 1731 | /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */ |
80876c20 | 1732 | |
fc08079e | 1733 | /* If we know the pid file, then let's just check if it is |
80876c20 | 1734 | * still valid */ |
6dfa5494 LP |
1735 | if (s->main_pid_known) { |
1736 | ||
1737 | /* If it's an alien child let's check if it is still | |
1738 | * alive ... */ | |
62220cf7 | 1739 | if (s->main_pid_alien && s->main_pid > 0) |
9f5650ae | 1740 | return pid_is_alive(s->main_pid); |
6dfa5494 LP |
1741 | |
1742 | /* .. otherwise assume we'll get a SIGCHLD for it, | |
1743 | * which we really should wait for to collect exit | |
1744 | * status and code */ | |
80876c20 | 1745 | return s->main_pid > 0; |
6dfa5494 | 1746 | } |
80876c20 LP |
1747 | |
1748 | /* We don't know the pid */ | |
1749 | return -EAGAIN; | |
1750 | } | |
1751 | ||
019be286 | 1752 | static int control_pid_good(Service *s) { |
80876c20 LP |
1753 | assert(s); |
1754 | ||
07697d7e LP |
1755 | /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to |
1756 | * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also | |
1757 | * means: we can't figure it out. */ | |
1758 | ||
80876c20 LP |
1759 | return s->control_pid > 0; |
1760 | } | |
1761 | ||
cb0e818f HC |
1762 | static int cgroup_good(Service *s) { |
1763 | int r; | |
1764 | ||
abaf5edd ZJS |
1765 | assert(s); |
1766 | ||
cb0e818f HC |
1767 | /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't |
1768 | * figure it out */ | |
1769 | ||
abaf5edd ZJS |
1770 | if (!UNIT(s)->cgroup_path) |
1771 | return 0; | |
1772 | ||
1773 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path); | |
117dcc57 | 1774 | if (r < 0) |
80876c20 LP |
1775 | return r; |
1776 | ||
b13ddbbc | 1777 | return r == 0; |
80876c20 LP |
1778 | } |
1779 | ||
ebc57b89 | 1780 | static bool service_shall_restart(Service *s, const char **reason) { |
a509f0e6 LP |
1781 | assert(s); |
1782 | ||
1783 | /* Don't restart after manual stops */ | |
ebc57b89 ZJS |
1784 | if (s->forbid_restart) { |
1785 | *reason = "manual stop"; | |
a509f0e6 | 1786 | return false; |
ebc57b89 | 1787 | } |
a509f0e6 LP |
1788 | |
1789 | /* Never restart if this is configured as special exception */ | |
ebc57b89 ZJS |
1790 | if (exit_status_set_test(&s->restart_prevent_status, s->main_exec_status.code, s->main_exec_status.status)) { |
1791 | *reason = "prevented by exit status"; | |
a509f0e6 | 1792 | return false; |
ebc57b89 | 1793 | } |
a509f0e6 LP |
1794 | |
1795 | /* Restart if the exit code/status are configured as restart triggers */ | |
ebc57b89 ZJS |
1796 | if (exit_status_set_test(&s->restart_force_status, s->main_exec_status.code, s->main_exec_status.status)) { |
1797 | *reason = "forced by exit status"; | |
a509f0e6 | 1798 | return true; |
ebc57b89 | 1799 | } |
a509f0e6 | 1800 | |
ebc57b89 | 1801 | *reason = "restart setting"; |
a509f0e6 LP |
1802 | switch (s->restart) { |
1803 | ||
1804 | case SERVICE_RESTART_NO: | |
1805 | return false; | |
1806 | ||
1807 | case SERVICE_RESTART_ALWAYS: | |
abb99360 | 1808 | return s->result != SERVICE_SKIP_CONDITION; |
a509f0e6 LP |
1809 | |
1810 | case SERVICE_RESTART_ON_SUCCESS: | |
1811 | return s->result == SERVICE_SUCCESS; | |
1812 | ||
1813 | case SERVICE_RESTART_ON_FAILURE: | |
bb924478 | 1814 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_SKIP_CONDITION); |
a509f0e6 LP |
1815 | |
1816 | case SERVICE_RESTART_ON_ABNORMAL: | |
bb924478 | 1817 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE, SERVICE_SKIP_CONDITION); |
a509f0e6 LP |
1818 | |
1819 | case SERVICE_RESTART_ON_WATCHDOG: | |
1820 | return s->result == SERVICE_FAILURE_WATCHDOG; | |
1821 | ||
1822 | case SERVICE_RESTART_ON_ABORT: | |
1823 | return IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP); | |
1824 | ||
1825 | default: | |
04499a70 | 1826 | assert_not_reached(); |
a509f0e6 LP |
1827 | } |
1828 | } | |
1829 | ||
deb4e708 MK |
1830 | static bool service_will_restart(Unit *u) { |
1831 | Service *s = SERVICE(u); | |
1832 | ||
53f47dfc YW |
1833 | assert(s); |
1834 | ||
deb4e708 MK |
1835 | if (s->will_auto_restart) |
1836 | return true; | |
53f47dfc YW |
1837 | if (s->state == SERVICE_AUTO_RESTART) |
1838 | return true; | |
2ad2e41a | 1839 | |
52a12341 | 1840 | return unit_will_restart_default(u); |
53f47dfc YW |
1841 | } |
1842 | ||
f42806df | 1843 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
31cd5f63 | 1844 | ServiceState end_state; |
034c6ed7 | 1845 | int r; |
0f52f8e5 | 1846 | |
034c6ed7 LP |
1847 | assert(s); |
1848 | ||
0f52f8e5 LP |
1849 | /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not |
1850 | * undo what has already been enqueued. */ | |
1851 | if (unit_stop_pending(UNIT(s))) | |
1852 | allow_restart = false; | |
1853 | ||
a0fef983 | 1854 | if (s->result == SERVICE_SUCCESS) |
f42806df | 1855 | s->result = f; |
034c6ed7 | 1856 | |
31cd5f63 AZ |
1857 | if (s->result == SERVICE_SUCCESS) { |
1858 | unit_log_success(UNIT(s)); | |
1859 | end_state = SERVICE_DEAD; | |
1860 | } else if (s->result == SERVICE_SKIP_CONDITION) { | |
1861 | unit_log_skip(UNIT(s), service_result_to_string(s->result)); | |
1862 | end_state = SERVICE_DEAD; | |
1863 | } else { | |
1864 | unit_log_failure(UNIT(s), service_result_to_string(s->result)); | |
1865 | end_state = SERVICE_FAILED; | |
1866 | } | |
4c425434 | 1867 | unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_stop); |
ed77d407 | 1868 | |
ebc57b89 ZJS |
1869 | if (!allow_restart) |
1870 | log_unit_debug(UNIT(s), "Service restart not allowed."); | |
1871 | else { | |
1872 | const char *reason; | |
1873 | bool shall_restart; | |
1874 | ||
1875 | shall_restart = service_shall_restart(s, &reason); | |
1876 | log_unit_debug(UNIT(s), "Service will %srestart (%s)", | |
1877 | shall_restart ? "" : "not ", | |
1878 | reason); | |
1879 | if (shall_restart) | |
1880 | s->will_auto_restart = true; | |
1881 | } | |
deb4e708 | 1882 | |
7eb2a8a1 LP |
1883 | /* Make sure service_release_resources() doesn't destroy our FD store, while we are changing through |
1884 | * SERVICE_FAILED/SERVICE_DEAD before entering into SERVICE_AUTO_RESTART. */ | |
1885 | s->n_keep_fd_store ++; | |
1886 | ||
31cd5f63 | 1887 | service_set_state(s, end_state); |
0c7f15b3 | 1888 | |
deb4e708 MK |
1889 | if (s->will_auto_restart) { |
1890 | s->will_auto_restart = false; | |
034c6ed7 | 1891 | |
e5d6dcce | 1892 | r = service_arm_timer(s, /* relative= */ true, s->restart_usec); |
7eb2a8a1 LP |
1893 | if (r < 0) { |
1894 | s->n_keep_fd_store--; | |
034c6ed7 | 1895 | goto fail; |
7eb2a8a1 | 1896 | } |
034c6ed7 LP |
1897 | |
1898 | service_set_state(s, SERVICE_AUTO_RESTART); | |
7a0019d3 LP |
1899 | } else |
1900 | /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the | |
1901 | * user can still introspect the counter. Do so on the next start. */ | |
1902 | s->flush_n_restarts = true; | |
034c6ed7 | 1903 | |
5238e957 | 1904 | /* The new state is in effect, let's decrease the fd store ref counter again. Let's also re-add us to the GC |
7eb2a8a1 LP |
1905 | * queue, so that the fd store is possibly gc'ed again */ |
1906 | s->n_keep_fd_store--; | |
1907 | unit_add_to_gc_queue(UNIT(s)); | |
1908 | ||
f2341e0a | 1909 | /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */ |
47342320 LP |
1910 | s->forbid_restart = false; |
1911 | ||
e66cf1a3 | 1912 | /* We want fresh tmpdirs in case service is started again immediately */ |
e8a565cb | 1913 | s->exec_runtime = exec_runtime_unref(s->exec_runtime, true); |
c17ec25e | 1914 | |
95939aed | 1915 | /* Also, remove the runtime directory */ |
bb0c0d6f | 1916 | unit_destroy_runtime_data(UNIT(s), &s->exec_context); |
e66cf1a3 | 1917 | |
00d9ef85 LP |
1918 | /* Get rid of the IPC bits of the user */ |
1919 | unit_unref_uid_gid(UNIT(s), true); | |
1920 | ||
29206d46 LP |
1921 | /* Release the user, and destroy it if we are the only remaining owner */ |
1922 | dynamic_creds_destroy(&s->dynamic_creds); | |
1923 | ||
9285c9ff LN |
1924 | /* Try to delete the pid file. At this point it will be |
1925 | * out-of-date, and some software might be confused by it, so | |
1926 | * let's remove it. */ | |
1927 | if (s->pid_file) | |
fabab190 | 1928 | (void) unlink(s->pid_file); |
9285c9ff | 1929 | |
6f765baf LP |
1930 | /* Reset TTY ownership if necessary */ |
1931 | exec_context_revert_tty(&s->exec_context); | |
1932 | ||
034c6ed7 LP |
1933 | return; |
1934 | ||
1935 | fail: | |
f2341e0a | 1936 | log_unit_warning_errno(UNIT(s), r, "Failed to run install restart timer: %m"); |
f42806df | 1937 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1938 | } |
1939 | ||
f42806df | 1940 | static void service_enter_stop_post(Service *s, ServiceResult f) { |
034c6ed7 LP |
1941 | int r; |
1942 | assert(s); | |
1943 | ||
a0fef983 | 1944 | if (s->result == SERVICE_SUCCESS) |
f42806df | 1945 | s->result = f; |
034c6ed7 | 1946 | |
5e94833f | 1947 | service_unwatch_control_pid(s); |
50be4f4a | 1948 | (void) unit_enqueue_rewatch_pids(UNIT(s)); |
5e94833f | 1949 | |
117dcc57 ZJS |
1950 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; |
1951 | if (s->control_command) { | |
867b3b7d LP |
1952 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
1953 | ||
ecedd90f LP |
1954 | r = service_spawn(s, |
1955 | s->control_command, | |
21b2ce39 | 1956 | s->timeout_stop_usec, |
78f93209 | 1957 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_IS_CONTROL|EXEC_SETENV_RESULT|EXEC_CONTROL_CGROUP, |
ecedd90f LP |
1958 | &s->control_pid); |
1959 | if (r < 0) | |
034c6ed7 LP |
1960 | goto fail; |
1961 | ||
80876c20 LP |
1962 | service_set_state(s, SERVICE_STOP_POST); |
1963 | } else | |
ac84d1fb | 1964 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1965 | |
1966 | return; | |
1967 | ||
1968 | fail: | |
f2341e0a | 1969 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m"); |
f42806df | 1970 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1971 | } |
1972 | ||
a232ebcc | 1973 | static int state_to_kill_operation(Service *s, ServiceState state) { |
4940c0b0 LP |
1974 | switch (state) { |
1975 | ||
c87700a1 | 1976 | case SERVICE_STOP_WATCHDOG: |
bf760801 | 1977 | case SERVICE_FINAL_WATCHDOG: |
c87700a1 | 1978 | return KILL_WATCHDOG; |
4940c0b0 LP |
1979 | |
1980 | case SERVICE_STOP_SIGTERM: | |
a232ebcc ZJS |
1981 | if (unit_has_job_type(UNIT(s), JOB_RESTART)) |
1982 | return KILL_RESTART; | |
1983 | _fallthrough_; | |
1984 | ||
4940c0b0 LP |
1985 | case SERVICE_FINAL_SIGTERM: |
1986 | return KILL_TERMINATE; | |
1987 | ||
1988 | case SERVICE_STOP_SIGKILL: | |
1989 | case SERVICE_FINAL_SIGKILL: | |
1990 | return KILL_KILL; | |
1991 | ||
1992 | default: | |
1993 | return _KILL_OPERATION_INVALID; | |
1994 | } | |
1995 | } | |
1996 | ||
f42806df | 1997 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
bf760801 | 1998 | int kill_operation, r; |
034c6ed7 LP |
1999 | |
2000 | assert(s); | |
2001 | ||
a0fef983 | 2002 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2003 | s->result = f; |
034c6ed7 | 2004 | |
50be4f4a LP |
2005 | /* Before sending any signal, make sure we track all members of this cgroup */ |
2006 | (void) unit_watch_all_pids(UNIT(s)); | |
2007 | ||
2008 | /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have | |
2009 | * died now */ | |
2010 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
a911bb9a | 2011 | |
bf760801 | 2012 | kill_operation = state_to_kill_operation(s, state); |
cd2086fe LP |
2013 | r = unit_kill_context( |
2014 | UNIT(s), | |
2015 | &s->kill_context, | |
bf760801 | 2016 | kill_operation, |
cd2086fe LP |
2017 | s->main_pid, |
2018 | s->control_pid, | |
2019 | s->main_pid_alien); | |
2020 | if (r < 0) | |
2021 | goto fail; | |
034c6ed7 | 2022 | |
cd2086fe | 2023 | if (r > 0) { |
e5d6dcce LP |
2024 | r = service_arm_timer(s, /* relative= */ true, |
2025 | kill_operation == KILL_WATCHDOG ? service_timeout_abort_usec(s) : s->timeout_stop_usec); | |
36c16a7c LP |
2026 | if (r < 0) |
2027 | goto fail; | |
d6ea93e3 | 2028 | |
80876c20 | 2029 | service_set_state(s, state); |
c87700a1 | 2030 | } else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM) && s->kill_context.send_sigkill) |
ac84d1fb | 2031 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); |
c87700a1 | 2032 | else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 2033 | service_enter_stop_post(s, SERVICE_SUCCESS); |
bf760801 | 2034 | else if (IN_SET(state, SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM) && s->kill_context.send_sigkill) |
ac84d1fb | 2035 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); |
80876c20 | 2036 | else |
f42806df | 2037 | service_enter_dead(s, SERVICE_SUCCESS, true); |
034c6ed7 LP |
2038 | |
2039 | return; | |
2040 | ||
2041 | fail: | |
f2341e0a | 2042 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
034c6ed7 | 2043 | |
c87700a1 | 2044 | if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 2045 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 2046 | else |
f42806df | 2047 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
2048 | } |
2049 | ||
308d72dc LP |
2050 | static void service_enter_stop_by_notify(Service *s) { |
2051 | assert(s); | |
2052 | ||
50be4f4a | 2053 | (void) unit_enqueue_rewatch_pids(UNIT(s)); |
308d72dc | 2054 | |
e5d6dcce | 2055 | service_arm_timer(s, /* relative= */ true, s->timeout_stop_usec); |
308d72dc | 2056 | |
6041a7ee MS |
2057 | /* The service told us it's stopping, so it's as if we SIGTERM'd it. */ |
2058 | service_set_state(s, SERVICE_STOP_SIGTERM); | |
308d72dc LP |
2059 | } |
2060 | ||
f42806df | 2061 | static void service_enter_stop(Service *s, ServiceResult f) { |
034c6ed7 | 2062 | int r; |
5925dd3c | 2063 | |
034c6ed7 LP |
2064 | assert(s); |
2065 | ||
a0fef983 | 2066 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2067 | s->result = f; |
034c6ed7 | 2068 | |
5e94833f | 2069 | service_unwatch_control_pid(s); |
50be4f4a | 2070 | (void) unit_enqueue_rewatch_pids(UNIT(s)); |
5e94833f | 2071 | |
117dcc57 ZJS |
2072 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; |
2073 | if (s->control_command) { | |
867b3b7d LP |
2074 | s->control_command_id = SERVICE_EXEC_STOP; |
2075 | ||
ecedd90f LP |
2076 | r = service_spawn(s, |
2077 | s->control_command, | |
21b2ce39 | 2078 | s->timeout_stop_usec, |
78f93209 | 2079 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_SETENV_RESULT|EXEC_CONTROL_CGROUP, |
ecedd90f LP |
2080 | &s->control_pid); |
2081 | if (r < 0) | |
034c6ed7 LP |
2082 | goto fail; |
2083 | ||
80876c20 LP |
2084 | service_set_state(s, SERVICE_STOP); |
2085 | } else | |
f42806df | 2086 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
2087 | |
2088 | return; | |
2089 | ||
2090 | fail: | |
f2341e0a | 2091 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop' task: %m"); |
f42806df | 2092 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
2093 | } |
2094 | ||
957c3cf9 LP |
2095 | static bool service_good(Service *s) { |
2096 | int main_pid_ok; | |
2097 | assert(s); | |
2098 | ||
2099 | if (s->type == SERVICE_DBUS && !s->bus_name_good) | |
2100 | return false; | |
2101 | ||
2102 | main_pid_ok = main_pid_good(s); | |
2103 | if (main_pid_ok > 0) /* It's alive */ | |
2104 | return true; | |
2105 | if (main_pid_ok == 0) /* It's dead */ | |
2106 | return false; | |
2107 | ||
2108 | /* OK, we don't know anything about the main PID, maybe | |
2109 | * because there is none. Let's check the control group | |
2110 | * instead. */ | |
2111 | ||
2112 | return cgroup_good(s) != 0; | |
2113 | } | |
2114 | ||
f42806df | 2115 | static void service_enter_running(Service *s, ServiceResult f) { |
80876c20 LP |
2116 | assert(s); |
2117 | ||
a0fef983 | 2118 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2119 | s->result = f; |
80876c20 | 2120 | |
089b64d5 LP |
2121 | service_unwatch_control_pid(s); |
2122 | ||
ec5b1452 LP |
2123 | if (s->result != SERVICE_SUCCESS) |
2124 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
2125 | else if (service_good(s)) { | |
308d72dc | 2126 | |
ec5b1452 | 2127 | /* If there are any queued up sd_notify() notifications, process them now */ |
308d72dc LP |
2128 | if (s->notify_state == NOTIFY_RELOADING) |
2129 | service_enter_reload_by_notify(s); | |
2130 | else if (s->notify_state == NOTIFY_STOPPING) | |
2131 | service_enter_stop_by_notify(s); | |
36c16a7c | 2132 | else { |
308d72dc | 2133 | service_set_state(s, SERVICE_RUNNING); |
e5d6dcce | 2134 | service_arm_timer(s, /* relative= */ false, service_running_timeout(s)); |
36c16a7c | 2135 | } |
308d72dc | 2136 | |
ec5b1452 | 2137 | } else if (s->remain_after_exit) |
80876c20 LP |
2138 | service_set_state(s, SERVICE_EXITED); |
2139 | else | |
f42806df | 2140 | service_enter_stop(s, SERVICE_SUCCESS); |
80876c20 LP |
2141 | } |
2142 | ||
034c6ed7 LP |
2143 | static void service_enter_start_post(Service *s) { |
2144 | int r; | |
2145 | assert(s); | |
2146 | ||
5e94833f | 2147 | service_unwatch_control_pid(s); |
842129f5 | 2148 | service_reset_watchdog(s); |
bb242b7b | 2149 | |
117dcc57 ZJS |
2150 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; |
2151 | if (s->control_command) { | |
867b3b7d LP |
2152 | s->control_command_id = SERVICE_EXEC_START_POST; |
2153 | ||
ecedd90f LP |
2154 | r = service_spawn(s, |
2155 | s->control_command, | |
21b2ce39 | 2156 | s->timeout_start_usec, |
78f93209 | 2157 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_CONTROL_CGROUP, |
ecedd90f LP |
2158 | &s->control_pid); |
2159 | if (r < 0) | |
034c6ed7 LP |
2160 | goto fail; |
2161 | ||
80876c20 LP |
2162 | service_set_state(s, SERVICE_START_POST); |
2163 | } else | |
f42806df | 2164 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
2165 | |
2166 | return; | |
2167 | ||
2168 | fail: | |
f2341e0a | 2169 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m"); |
f42806df | 2170 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
2171 | } |
2172 | ||
e9a4f676 | 2173 | static void service_kill_control_process(Service *s) { |
a6951a50 | 2174 | int r; |
4ad49000 | 2175 | |
a6951a50 LP |
2176 | assert(s); |
2177 | ||
e9a4f676 LP |
2178 | if (s->control_pid <= 0) |
2179 | return; | |
4ad49000 | 2180 | |
e9a4f676 LP |
2181 | r = kill_and_sigcont(s->control_pid, SIGKILL); |
2182 | if (r < 0) { | |
2183 | _cleanup_free_ char *comm = NULL; | |
a6951a50 | 2184 | |
e9a4f676 | 2185 | (void) get_process_comm(s->control_pid, &comm); |
a6951a50 | 2186 | |
e9a4f676 LP |
2187 | log_unit_debug_errno(UNIT(s), r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", |
2188 | s->control_pid, strna(comm)); | |
a6951a50 | 2189 | } |
4ad49000 LP |
2190 | } |
2191 | ||
c53d2d54 DB |
2192 | static int service_adverse_to_leftover_processes(Service *s) { |
2193 | assert(s); | |
2194 | ||
2195 | /* KillMode=mixed and control group are used to indicate that all process should be killed off. | |
4c425434 LP |
2196 | * SendSIGKILL= is used for services that require a clean shutdown. These are typically database |
2197 | * service where a SigKilled process would result in a lengthy recovery and who's shutdown or startup | |
2198 | * time is quite variable (so Timeout settings aren't of use). | |
c53d2d54 DB |
2199 | * |
2200 | * Here we take these two factors and refuse to start a service if there are existing processes | |
2201 | * within a control group. Databases, while generally having some protection against multiple | |
4c425434 | 2202 | * instances running, lets not stress the rigor of these. Also ExecStartPre= parts of the service |
c53d2d54 | 2203 | * aren't as rigoriously written to protect aganst against multiple use. */ |
4c425434 LP |
2204 | |
2205 | if (unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_start) > 0 && | |
c53d2d54 | 2206 | IN_SET(s->kill_context.kill_mode, KILL_MIXED, KILL_CONTROL_GROUP) && |
569554d9 ZJS |
2207 | !s->kill_context.send_sigkill) |
2208 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EBUSY), | |
2209 | "Will not start SendSIGKILL=no service of type KillMode=control-group or mixed while processes exist"); | |
2210 | ||
c53d2d54 DB |
2211 | return 0; |
2212 | } | |
2213 | ||
034c6ed7 | 2214 | static void service_enter_start(Service *s) { |
4ad49000 | 2215 | ExecCommand *c; |
36c16a7c | 2216 | usec_t timeout; |
034c6ed7 LP |
2217 | pid_t pid; |
2218 | int r; | |
2219 | ||
2220 | assert(s); | |
2221 | ||
41efeaec LP |
2222 | service_unwatch_control_pid(s); |
2223 | service_unwatch_main_pid(s); | |
80876c20 | 2224 | |
c53d2d54 DB |
2225 | r = service_adverse_to_leftover_processes(s); |
2226 | if (r < 0) | |
2227 | goto fail; | |
a4634b21 | 2228 | |
867b3b7d LP |
2229 | if (s->type == SERVICE_FORKING) { |
2230 | s->control_command_id = SERVICE_EXEC_START; | |
2231 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
2232 | ||
2233 | s->main_command = NULL; | |
2234 | } else { | |
2235 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2236 | s->control_command = NULL; | |
2237 | ||
2238 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
2239 | } | |
34e9ba66 | 2240 | |
96fb8242 | 2241 | if (!c) { |
47fffb35 | 2242 | if (s->type != SERVICE_ONESHOT) { |
cbc056c8 ZJS |
2243 | /* There's no command line configured for the main command? Hmm, that is strange. |
2244 | * This can only happen if the configuration changes at runtime. In this case, | |
2245 | * let's enter a failure state. */ | |
d85ff944 | 2246 | r = log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENXIO), "There's no 'start' task anymore we could start."); |
47fffb35 LP |
2247 | goto fail; |
2248 | } | |
2249 | ||
ef5ae8e7 ZJS |
2250 | /* We force a fake state transition here. Otherwise, the unit would go directly from |
2251 | * SERVICE_DEAD to SERVICE_DEAD without SERVICE_ACTIVATING or SERVICE_ACTIVE | |
5238e957 | 2252 | * in between. This way we can later trigger actions that depend on the state |
ef5ae8e7 ZJS |
2253 | * transition, including SuccessAction=. */ |
2254 | service_set_state(s, SERVICE_START); | |
2255 | ||
96fb8242 LP |
2256 | service_enter_start_post(s); |
2257 | return; | |
2258 | } | |
2259 | ||
36c16a7c LP |
2260 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) |
2261 | /* For simple + idle this is the main process. We don't apply any timeout here, but | |
2262 | * service_enter_running() will later apply the .runtime_max_usec timeout. */ | |
2263 | timeout = USEC_INFINITY; | |
2264 | else | |
2265 | timeout = s->timeout_start_usec; | |
2266 | ||
ecedd90f LP |
2267 | r = service_spawn(s, |
2268 | c, | |
36c16a7c | 2269 | timeout, |
cdebedb4 | 2270 | EXEC_PASS_FDS|EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_SET_WATCHDOG|EXEC_WRITE_CREDENTIALS|EXEC_SETENV_MONITOR_RESULT, |
ecedd90f LP |
2271 | &pid); |
2272 | if (r < 0) | |
034c6ed7 LP |
2273 | goto fail; |
2274 | ||
36c16a7c | 2275 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) { |
034c6ed7 LP |
2276 | /* For simple services we immediately start |
2277 | * the START_POST binaries. */ | |
2278 | ||
5c95eb28 | 2279 | (void) service_set_main_pid(s, pid); |
034c6ed7 LP |
2280 | service_enter_start_post(s); |
2281 | ||
2282 | } else if (s->type == SERVICE_FORKING) { | |
2283 | ||
2284 | /* For forking services we wait until the start | |
2285 | * process exited. */ | |
2286 | ||
e55224ca | 2287 | s->control_pid = pid; |
80876c20 LP |
2288 | service_set_state(s, SERVICE_START); |
2289 | ||
3bd28bf7 | 2290 | } else if (IN_SET(s->type, SERVICE_ONESHOT, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD, SERVICE_EXEC)) { |
7d55e835 | 2291 | |
5686391b | 2292 | /* For oneshot services we wait until the start process exited, too, but it is our main process. */ |
7d55e835 | 2293 | |
5686391b LP |
2294 | /* For D-Bus services we know the main pid right away, but wait for the bus name to appear on the |
2295 | * bus. 'notify' and 'exec' services are similar. */ | |
05e343b7 | 2296 | |
5c95eb28 | 2297 | (void) service_set_main_pid(s, pid); |
80876c20 | 2298 | service_set_state(s, SERVICE_START); |
034c6ed7 | 2299 | } else |
04499a70 | 2300 | assert_not_reached(); |
034c6ed7 LP |
2301 | |
2302 | return; | |
2303 | ||
2304 | fail: | |
f2341e0a | 2305 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start' task: %m"); |
c3fda31d | 2306 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
2307 | } |
2308 | ||
2309 | static void service_enter_start_pre(Service *s) { | |
2310 | int r; | |
2311 | ||
2312 | assert(s); | |
2313 | ||
5e94833f LP |
2314 | service_unwatch_control_pid(s); |
2315 | ||
117dcc57 ZJS |
2316 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; |
2317 | if (s->control_command) { | |
8f53a7b8 | 2318 | |
c53d2d54 DB |
2319 | r = service_adverse_to_leftover_processes(s); |
2320 | if (r < 0) | |
2321 | goto fail; | |
a4634b21 | 2322 | |
867b3b7d LP |
2323 | s->control_command_id = SERVICE_EXEC_START_PRE; |
2324 | ||
ecedd90f LP |
2325 | r = service_spawn(s, |
2326 | s->control_command, | |
21b2ce39 | 2327 | s->timeout_start_usec, |
e7f64b89 | 2328 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_APPLY_TTY_STDIN|EXEC_SETENV_MONITOR_RESULT|EXEC_WRITE_CREDENTIALS, |
ecedd90f LP |
2329 | &s->control_pid); |
2330 | if (r < 0) | |
034c6ed7 LP |
2331 | goto fail; |
2332 | ||
80876c20 LP |
2333 | service_set_state(s, SERVICE_START_PRE); |
2334 | } else | |
034c6ed7 LP |
2335 | service_enter_start(s); |
2336 | ||
2337 | return; | |
2338 | ||
2339 | fail: | |
f2341e0a | 2340 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m"); |
f42806df | 2341 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
2342 | } |
2343 | ||
31cd5f63 AZ |
2344 | static void service_enter_condition(Service *s) { |
2345 | int r; | |
2346 | ||
2347 | assert(s); | |
2348 | ||
2349 | service_unwatch_control_pid(s); | |
2350 | ||
2351 | s->control_command = s->exec_command[SERVICE_EXEC_CONDITION]; | |
2352 | if (s->control_command) { | |
2353 | ||
2354 | r = service_adverse_to_leftover_processes(s); | |
2355 | if (r < 0) | |
2356 | goto fail; | |
2357 | ||
2358 | s->control_command_id = SERVICE_EXEC_CONDITION; | |
2359 | ||
2360 | r = service_spawn(s, | |
2361 | s->control_command, | |
2362 | s->timeout_start_usec, | |
2363 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_APPLY_TTY_STDIN, | |
2364 | &s->control_pid); | |
2365 | ||
2366 | if (r < 0) | |
2367 | goto fail; | |
2368 | ||
2369 | service_set_state(s, SERVICE_CONDITION); | |
2370 | } else | |
2371 | service_enter_start_pre(s); | |
2372 | ||
2373 | return; | |
2374 | ||
2375 | fail: | |
2376 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'exec-condition' task: %m"); | |
2377 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); | |
2378 | } | |
2379 | ||
034c6ed7 | 2380 | static void service_enter_restart(Service *s) { |
4afd3348 | 2381 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 | 2382 | int r; |
398ef8ba | 2383 | |
034c6ed7 LP |
2384 | assert(s); |
2385 | ||
28a2dfe8 | 2386 | if (unit_has_job_type(UNIT(s), JOB_STOP)) { |
a8bb2e65 | 2387 | /* Don't restart things if we are going down anyway */ |
c9728806 | 2388 | log_unit_info(UNIT(s), "Stop job pending for unit, skipping automatic restart."); |
feae8adb | 2389 | return; |
2edfa366 LP |
2390 | } |
2391 | ||
48bb5876 DW |
2392 | /* Any units that are bound to this service must also be |
2393 | * restarted. We use JOB_RESTART (instead of the more obvious | |
2394 | * JOB_START) here so that those dependency jobs will be added | |
2395 | * as well. */ | |
50cbaba4 | 2396 | r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_REPLACE, NULL, &error, NULL); |
48bb5876 | 2397 | if (r < 0) |
034c6ed7 LP |
2398 | goto fail; |
2399 | ||
7a0019d3 | 2400 | /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't fully |
f0c7e594 | 2401 | * stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset the counter |
7a0019d3 LP |
2402 | * explicitly however via the usual "systemctl reset-failure" logic. */ |
2403 | s->n_restarts ++; | |
2404 | s->flush_n_restarts = false; | |
2405 | ||
c2503e35 RH |
2406 | log_unit_struct(UNIT(s), LOG_INFO, |
2407 | "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR, | |
2408 | LOG_UNIT_INVOCATION_ID(UNIT(s)), | |
92663a5e ZJS |
2409 | LOG_UNIT_MESSAGE(UNIT(s), |
2410 | "Scheduled restart job, restart counter is at %u.", s->n_restarts), | |
c2503e35 | 2411 | "N_RESTARTS=%u", s->n_restarts); |
7a0019d3 LP |
2412 | |
2413 | /* Notify clients about changed restart counter */ | |
2414 | unit_add_to_dbus_queue(UNIT(s)); | |
2415 | ||
a8bb2e65 LP |
2416 | /* Note that we stay in the SERVICE_AUTO_RESTART state here, |
2417 | * it will be canceled as part of the service_stop() call that | |
2418 | * is executed as part of JOB_RESTART. */ | |
2419 | ||
034c6ed7 LP |
2420 | return; |
2421 | ||
2422 | fail: | |
2a03b9ed | 2423 | log_unit_warning(UNIT(s), "Failed to schedule restart job: %s", bus_error_message(&error, r)); |
f42806df | 2424 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
2425 | } |
2426 | ||
308d72dc | 2427 | static void service_enter_reload_by_notify(Service *s) { |
15d167f8 JW |
2428 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2429 | int r; | |
2430 | ||
308d72dc LP |
2431 | assert(s); |
2432 | ||
e5d6dcce | 2433 | service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); |
3bd28bf7 | 2434 | service_set_state(s, SERVICE_RELOAD_NOTIFY); |
15d167f8 JW |
2435 | |
2436 | /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */ | |
2437 | r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); | |
2438 | if (r < 0) | |
2a03b9ed | 2439 | log_unit_warning(UNIT(s), "Failed to schedule propagation of reload: %s", bus_error_message(&error, r)); |
308d72dc LP |
2440 | } |
2441 | ||
034c6ed7 | 2442 | static void service_enter_reload(Service *s) { |
3bd28bf7 | 2443 | bool killed = false; |
034c6ed7 LP |
2444 | int r; |
2445 | ||
2446 | assert(s); | |
2447 | ||
5e94833f | 2448 | service_unwatch_control_pid(s); |
95c906ae | 2449 | s->reload_result = SERVICE_SUCCESS; |
5e94833f | 2450 | |
3bd28bf7 LP |
2451 | usec_t ts = now(CLOCK_MONOTONIC); |
2452 | ||
2453 | if (s->type == SERVICE_NOTIFY_RELOAD && s->main_pid > 0) { | |
2454 | r = kill_and_sigcont(s->main_pid, s->reload_signal); | |
2455 | if (r < 0) { | |
2456 | log_unit_warning_errno(UNIT(s), r, "Failed to send reload signal: %m"); | |
2457 | goto fail; | |
2458 | } | |
2459 | ||
2460 | killed = true; | |
2461 | } | |
2462 | ||
117dcc57 ZJS |
2463 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; |
2464 | if (s->control_command) { | |
867b3b7d LP |
2465 | s->control_command_id = SERVICE_EXEC_RELOAD; |
2466 | ||
ecedd90f LP |
2467 | r = service_spawn(s, |
2468 | s->control_command, | |
21b2ce39 | 2469 | s->timeout_start_usec, |
f8a8f613 | 2470 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_CONTROL_CGROUP, |
ecedd90f | 2471 | &s->control_pid); |
3bd28bf7 LP |
2472 | if (r < 0) { |
2473 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'reload' task: %m"); | |
034c6ed7 | 2474 | goto fail; |
3bd28bf7 | 2475 | } |
034c6ed7 | 2476 | |
80876c20 | 2477 | service_set_state(s, SERVICE_RELOAD); |
3bd28bf7 LP |
2478 | } else if (killed) { |
2479 | service_arm_timer(s, /* relative= */ true, s->timeout_start_usec); | |
2480 | service_set_state(s, SERVICE_RELOAD_SIGNAL); | |
2481 | } else { | |
f42806df | 2482 | service_enter_running(s, SERVICE_SUCCESS); |
3bd28bf7 LP |
2483 | return; |
2484 | } | |
034c6ed7 | 2485 | |
3bd28bf7 LP |
2486 | /* Store the timestamp when we started reloading: when reloading via SIGHUP we won't leave the reload |
2487 | * state until we received both RELOADING=1 and READY=1 with MONOTONIC_USEC= set to a value above | |
2488 | * this. Thus we know for sure the reload cycle was executed *after* we requested it, and is not one | |
2489 | * that was already in progress before. */ | |
2490 | s->reload_begin_usec = ts; | |
034c6ed7 LP |
2491 | return; |
2492 | ||
2493 | fail: | |
f42806df LP |
2494 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
2495 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
2496 | } |
2497 | ||
f42806df | 2498 | static void service_run_next_control(Service *s) { |
36c16a7c | 2499 | usec_t timeout; |
034c6ed7 LP |
2500 | int r; |
2501 | ||
2502 | assert(s); | |
2503 | assert(s->control_command); | |
2504 | assert(s->control_command->command_next); | |
2505 | ||
34e9ba66 | 2506 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 2507 | |
34e9ba66 | 2508 | s->control_command = s->control_command->command_next; |
5e94833f LP |
2509 | service_unwatch_control_pid(s); |
2510 | ||
31cd5f63 | 2511 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
36c16a7c LP |
2512 | timeout = s->timeout_start_usec; |
2513 | else | |
2514 | timeout = s->timeout_stop_usec; | |
2515 | ||
ecedd90f LP |
2516 | r = service_spawn(s, |
2517 | s->control_command, | |
36c16a7c | 2518 | timeout, |
1703fa41 | 2519 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL| |
31cd5f63 | 2520 | (IN_SET(s->control_command_id, SERVICE_EXEC_CONDITION, SERVICE_EXEC_START_PRE, SERVICE_EXEC_STOP_POST) ? EXEC_APPLY_TTY_STDIN : 0)| |
78f93209 | 2521 | (IN_SET(s->control_command_id, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST) ? EXEC_SETENV_RESULT : 0)| |
a257c941 | 2522 | (IN_SET(s->control_command_id, SERVICE_EXEC_START_PRE, SERVICE_EXEC_START) ? EXEC_SETENV_MONITOR_RESULT : 0)| |
78f93209 | 2523 | (IN_SET(s->control_command_id, SERVICE_EXEC_START_POST, SERVICE_EXEC_RELOAD, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST) ? EXEC_CONTROL_CGROUP : 0), |
ecedd90f LP |
2524 | &s->control_pid); |
2525 | if (r < 0) | |
034c6ed7 LP |
2526 | goto fail; |
2527 | ||
2528 | return; | |
2529 | ||
2530 | fail: | |
f2341e0a | 2531 | log_unit_warning_errno(UNIT(s), r, "Failed to run next control task: %m"); |
034c6ed7 | 2532 | |
31cd5f63 | 2533 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START_POST, SERVICE_STOP)) |
f42806df | 2534 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 2535 | else if (s->state == SERVICE_STOP_POST) |
f42806df | 2536 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
e2f3b44c | 2537 | else if (s->state == SERVICE_RELOAD) { |
f42806df LP |
2538 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
2539 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c | 2540 | } else |
f42806df | 2541 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
5cb5a6ff LP |
2542 | } |
2543 | ||
f42806df | 2544 | static void service_run_next_main(Service *s) { |
34e9ba66 LP |
2545 | pid_t pid; |
2546 | int r; | |
2547 | ||
2548 | assert(s); | |
867b3b7d LP |
2549 | assert(s->main_command); |
2550 | assert(s->main_command->command_next); | |
2551 | assert(s->type == SERVICE_ONESHOT); | |
34e9ba66 | 2552 | |
867b3b7d | 2553 | s->main_command = s->main_command->command_next; |
34e9ba66 LP |
2554 | service_unwatch_main_pid(s); |
2555 | ||
ecedd90f LP |
2556 | r = service_spawn(s, |
2557 | s->main_command, | |
21b2ce39 | 2558 | s->timeout_start_usec, |
a257c941 | 2559 | EXEC_PASS_FDS|EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_SET_WATCHDOG|EXEC_SETENV_MONITOR_RESULT, |
ecedd90f LP |
2560 | &pid); |
2561 | if (r < 0) | |
34e9ba66 LP |
2562 | goto fail; |
2563 | ||
5c95eb28 | 2564 | (void) service_set_main_pid(s, pid); |
34e9ba66 LP |
2565 | |
2566 | return; | |
2567 | ||
2568 | fail: | |
f2341e0a | 2569 | log_unit_warning_errno(UNIT(s), r, "Failed to run next main task: %m"); |
f42806df | 2570 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
34e9ba66 LP |
2571 | } |
2572 | ||
87f0e418 LP |
2573 | static int service_start(Unit *u) { |
2574 | Service *s = SERVICE(u); | |
07299350 | 2575 | int r; |
5cb5a6ff LP |
2576 | |
2577 | assert(s); | |
2578 | ||
034c6ed7 LP |
2579 | /* We cannot fulfill this request right now, try again later |
2580 | * please! */ | |
a00973af | 2581 | if (IN_SET(s->state, |
c87700a1 | 2582 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 2583 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, SERVICE_CLEANING)) |
5cb5a6ff LP |
2584 | return -EAGAIN; |
2585 | ||
034c6ed7 | 2586 | /* Already on it! */ |
31cd5f63 | 2587 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST)) |
034c6ed7 LP |
2588 | return 0; |
2589 | ||
2e9d6c12 | 2590 | /* A service that will be restarted must be stopped first to |
7f2cddae | 2591 | * trigger BindsTo and/or OnFailure dependencies. If a user |
2e9d6c12 | 2592 | * does not want to wait for the holdoff time to elapse, the |
d4943dc7 LP |
2593 | * service should be manually restarted, not started. We |
2594 | * simply return EAGAIN here, so that any start jobs stay | |
2595 | * queued, and assume that the auto restart timer will | |
2596 | * eventually trigger the restart. */ | |
2597 | if (s->state == SERVICE_AUTO_RESTART) | |
a8bb2e65 | 2598 | return -EAGAIN; |
2e9d6c12 | 2599 | |
a00973af | 2600 | assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED)); |
5cb5a6ff | 2601 | |
4b58153d LP |
2602 | r = unit_acquire_invocation_id(u); |
2603 | if (r < 0) | |
2604 | return r; | |
2605 | ||
f42806df LP |
2606 | s->result = SERVICE_SUCCESS; |
2607 | s->reload_result = SERVICE_SUCCESS; | |
034c6ed7 | 2608 | s->main_pid_known = false; |
6dfa5494 | 2609 | s->main_pid_alien = false; |
47342320 | 2610 | s->forbid_restart = false; |
3c7416b6 | 2611 | |
a1e58e8e | 2612 | s->status_text = mfree(s->status_text); |
8cfdb077 LP |
2613 | s->status_errno = 0; |
2614 | ||
308d72dc LP |
2615 | s->notify_state = NOTIFY_UNKNOWN; |
2616 | ||
aa8c4bbf | 2617 | s->watchdog_original_usec = s->watchdog_usec; |
2787d83c | 2618 | s->watchdog_override_enable = false; |
aa8c4bbf | 2619 | s->watchdog_override_usec = USEC_INFINITY; |
2787d83c | 2620 | |
6a1d4d9f LP |
2621 | exec_command_reset_status_list_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
2622 | exec_status_reset(&s->main_exec_status); | |
2623 | ||
7a0019d3 LP |
2624 | /* This is not an automatic restart? Flush the restart counter then */ |
2625 | if (s->flush_n_restarts) { | |
2626 | s->n_restarts = 0; | |
2627 | s->flush_n_restarts = false; | |
2628 | } | |
2629 | ||
6a1d4d9f LP |
2630 | u->reset_accounting = true; |
2631 | ||
31cd5f63 | 2632 | service_enter_condition(s); |
82a2b6bb | 2633 | return 1; |
5cb5a6ff LP |
2634 | } |
2635 | ||
87f0e418 LP |
2636 | static int service_stop(Unit *u) { |
2637 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
2638 | |
2639 | assert(s); | |
2640 | ||
a509f0e6 | 2641 | /* Don't create restart jobs from manual stops. */ |
47342320 | 2642 | s->forbid_restart = true; |
034c6ed7 | 2643 | |
e537352b | 2644 | /* Already on it */ |
a00973af | 2645 | if (IN_SET(s->state, |
dc653bf4 | 2646 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 2647 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) |
e537352b LP |
2648 | return 0; |
2649 | ||
f0c7b229 | 2650 | /* A restart will be scheduled or is in progress. */ |
034c6ed7 | 2651 | if (s->state == SERVICE_AUTO_RESTART) { |
0c7f15b3 | 2652 | service_set_state(s, SERVICE_DEAD); |
034c6ed7 LP |
2653 | return 0; |
2654 | } | |
2655 | ||
3bd28bf7 LP |
2656 | /* If there's already something running we go directly into kill mode. */ |
2657 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY, SERVICE_STOP_WATCHDOG)) { | |
f42806df | 2658 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
3f6c78dc LP |
2659 | return 0; |
2660 | } | |
5cb5a6ff | 2661 | |
4c2f5842 LP |
2662 | /* If we are currently cleaning, then abort it, brutally. */ |
2663 | if (s->state == SERVICE_CLEANING) { | |
2664 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); | |
2665 | return 0; | |
2666 | } | |
2667 | ||
a00973af | 2668 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
3a762661 | 2669 | |
f42806df | 2670 | service_enter_stop(s, SERVICE_SUCCESS); |
82a2b6bb | 2671 | return 1; |
5cb5a6ff LP |
2672 | } |
2673 | ||
87f0e418 LP |
2674 | static int service_reload(Unit *u) { |
2675 | Service *s = SERVICE(u); | |
034c6ed7 LP |
2676 | |
2677 | assert(s); | |
2678 | ||
3742095b | 2679 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
034c6ed7 LP |
2680 | |
2681 | service_enter_reload(s); | |
2d018ae2 | 2682 | return 1; |
5cb5a6ff LP |
2683 | } |
2684 | ||
44a6b1b6 | 2685 | _pure_ static bool service_can_reload(Unit *u) { |
87f0e418 | 2686 | Service *s = SERVICE(u); |
034c6ed7 LP |
2687 | |
2688 | assert(s); | |
2689 | ||
3bd28bf7 LP |
2690 | return s->exec_command[SERVICE_EXEC_RELOAD] || |
2691 | s->type == SERVICE_NOTIFY_RELOAD; | |
034c6ed7 LP |
2692 | } |
2693 | ||
502096b5 | 2694 | static unsigned service_exec_command_index(Unit *u, ServiceExecCommand id, const ExecCommand *current) { |
e266c068 MS |
2695 | Service *s = SERVICE(u); |
2696 | unsigned idx = 0; | |
e266c068 MS |
2697 | |
2698 | assert(s); | |
5b99bd5f LP |
2699 | assert(id >= 0); |
2700 | assert(id < _SERVICE_EXEC_COMMAND_MAX); | |
e266c068 | 2701 | |
502096b5 | 2702 | const ExecCommand *first = s->exec_command[id]; |
e266c068 MS |
2703 | |
2704 | /* Figure out where we are in the list by walking back to the beginning */ | |
502096b5 | 2705 | for (const ExecCommand *c = current; c != first; c = c->command_prev) |
e266c068 MS |
2706 | idx++; |
2707 | ||
2708 | return idx; | |
2709 | } | |
2710 | ||
502096b5 | 2711 | static int service_serialize_exec_command(Unit *u, FILE *f, const ExecCommand *command) { |
d68c645b | 2712 | _cleanup_free_ char *args = NULL, *p = NULL; |
e266c068 | 2713 | Service *s = SERVICE(u); |
d68c645b | 2714 | const char *type, *key; |
e266c068 | 2715 | ServiceExecCommand id; |
319a4f4b | 2716 | size_t length = 0; |
e266c068 | 2717 | unsigned idx; |
e266c068 MS |
2718 | |
2719 | assert(s); | |
2720 | assert(f); | |
2721 | ||
2722 | if (!command) | |
2723 | return 0; | |
2724 | ||
2725 | if (command == s->control_command) { | |
2726 | type = "control"; | |
2727 | id = s->control_command_id; | |
2728 | } else { | |
2729 | type = "main"; | |
2730 | id = SERVICE_EXEC_START; | |
2731 | } | |
2732 | ||
2733 | idx = service_exec_command_index(u, id, command); | |
2734 | ||
2735 | STRV_FOREACH(arg, command->argv) { | |
e266c068 | 2736 | _cleanup_free_ char *e = NULL; |
d68c645b | 2737 | size_t n; |
e266c068 | 2738 | |
d68c645b | 2739 | e = cescape(*arg); |
e266c068 | 2740 | if (!e) |
d68c645b | 2741 | return log_oom(); |
e266c068 MS |
2742 | |
2743 | n = strlen(e); | |
319a4f4b | 2744 | if (!GREEDY_REALLOC(args, length + 2 + n + 2)) |
d68c645b | 2745 | return log_oom(); |
e266c068 MS |
2746 | |
2747 | if (length > 0) | |
2748 | args[length++] = ' '; | |
2749 | ||
334c0979 | 2750 | args[length++] = '"'; |
e266c068 MS |
2751 | memcpy(args + length, e, n); |
2752 | length += n; | |
334c0979 | 2753 | args[length++] = '"'; |
e266c068 MS |
2754 | } |
2755 | ||
319a4f4b | 2756 | if (!GREEDY_REALLOC(args, length + 1)) |
d68c645b LP |
2757 | return log_oom(); |
2758 | ||
e266c068 MS |
2759 | args[length++] = 0; |
2760 | ||
d68c645b | 2761 | p = cescape(command->path); |
e266c068 | 2762 | if (!p) |
5b99bd5f | 2763 | return log_oom(); |
e266c068 | 2764 | |
d68c645b | 2765 | key = strjoina(type, "-command"); |
5b99bd5f LP |
2766 | (void) serialize_item_format(f, key, "%s %u %s %s", service_exec_command_to_string(id), idx, p, args); |
2767 | ||
2768 | return 0; | |
e266c068 MS |
2769 | } |
2770 | ||
a16e1123 LP |
2771 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
2772 | Service *s = SERVICE(u); | |
a34ceba6 | 2773 | int r; |
a16e1123 LP |
2774 | |
2775 | assert(u); | |
2776 | assert(f); | |
2777 | assert(fds); | |
2778 | ||
d68c645b LP |
2779 | (void) serialize_item(f, "state", service_state_to_string(s->state)); |
2780 | (void) serialize_item(f, "result", service_result_to_string(s->result)); | |
2781 | (void) serialize_item(f, "reload-result", service_result_to_string(s->reload_result)); | |
a16e1123 LP |
2782 | |
2783 | if (s->control_pid > 0) | |
d68c645b | 2784 | (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid); |
a16e1123 | 2785 | |
5925dd3c | 2786 | if (s->main_pid_known && s->main_pid > 0) |
d68c645b | 2787 | (void) serialize_item_format(f, "main-pid", PID_FMT, s->main_pid); |
a16e1123 | 2788 | |
d68c645b LP |
2789 | (void) serialize_bool(f, "main-pid-known", s->main_pid_known); |
2790 | (void) serialize_bool(f, "bus-name-good", s->bus_name_good); | |
2791 | (void) serialize_bool(f, "bus-name-owner", s->bus_name_owner); | |
a16e1123 | 2792 | |
d68c645b LP |
2793 | (void) serialize_item_format(f, "n-restarts", "%u", s->n_restarts); |
2794 | (void) serialize_bool(f, "flush-n-restarts", s->flush_n_restarts); | |
7a0019d3 | 2795 | |
d68c645b | 2796 | r = serialize_item_escaped(f, "status-text", s->status_text); |
a34ceba6 LP |
2797 | if (r < 0) |
2798 | return r; | |
3a2776bc | 2799 | |
e266c068 MS |
2800 | service_serialize_exec_command(u, f, s->control_command); |
2801 | service_serialize_exec_command(u, f, s->main_command); | |
a16e1123 | 2802 | |
d68c645b | 2803 | r = serialize_fd(f, fds, "stdin-fd", s->stdin_fd); |
a34ceba6 LP |
2804 | if (r < 0) |
2805 | return r; | |
d68c645b | 2806 | r = serialize_fd(f, fds, "stdout-fd", s->stdout_fd); |
a34ceba6 LP |
2807 | if (r < 0) |
2808 | return r; | |
d68c645b | 2809 | r = serialize_fd(f, fds, "stderr-fd", s->stderr_fd); |
a34ceba6 LP |
2810 | if (r < 0) |
2811 | return r; | |
e44da745 | 2812 | |
5686391b | 2813 | if (s->exec_fd_event_source) { |
d68c645b | 2814 | r = serialize_fd(f, fds, "exec-fd", sd_event_source_get_io_fd(s->exec_fd_event_source)); |
5686391b LP |
2815 | if (r < 0) |
2816 | return r; | |
d68c645b LP |
2817 | |
2818 | (void) serialize_bool(f, "exec-fd-hot", s->exec_fd_hot); | |
5686391b LP |
2819 | } |
2820 | ||
9dfb64f8 | 2821 | if (UNIT_ISSET(s->accept_socket)) { |
d68c645b | 2822 | r = serialize_item(f, "accept-socket", UNIT_DEREF(s->accept_socket)->id); |
9dfb64f8 ZJS |
2823 | if (r < 0) |
2824 | return r; | |
2825 | } | |
2826 | ||
d68c645b | 2827 | r = serialize_fd(f, fds, "socket-fd", s->socket_fd); |
a34ceba6 LP |
2828 | if (r < 0) |
2829 | return r; | |
e44da745 | 2830 | |
2339fc93 | 2831 | LIST_FOREACH(fd_store, fs, s->fd_store) { |
8dd4c05b | 2832 | _cleanup_free_ char *c = NULL; |
2339fc93 LP |
2833 | int copy; |
2834 | ||
2835 | copy = fdset_put_dup(fds, fs->fd); | |
2836 | if (copy < 0) | |
d68c645b | 2837 | return log_error_errno(copy, "Failed to copy file descriptor for serialization: %m"); |
2339fc93 | 2838 | |
8dd4c05b | 2839 | c = cescape(fs->fdname); |
d68c645b LP |
2840 | if (!c) |
2841 | return log_oom(); | |
8dd4c05b | 2842 | |
30520492 | 2843 | (void) serialize_item_format(f, "fd-store-fd", "%i \"%s\" %i", copy, c, fs->do_poll); |
2339fc93 LP |
2844 | } |
2845 | ||
ecdbca40 | 2846 | if (s->main_exec_status.pid > 0) { |
d68c645b LP |
2847 | (void) serialize_item_format(f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid); |
2848 | (void) serialize_dual_timestamp(f, "main-exec-status-start", &s->main_exec_status.start_timestamp); | |
2849 | (void) serialize_dual_timestamp(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp); | |
ecdbca40 | 2850 | |
799fd0fd | 2851 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { |
d68c645b LP |
2852 | (void) serialize_item_format(f, "main-exec-status-code", "%i", s->main_exec_status.code); |
2853 | (void) serialize_item_format(f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
ecdbca40 LP |
2854 | } |
2855 | } | |
f06db334 | 2856 | |
d68c645b LP |
2857 | (void) serialize_dual_timestamp(f, "watchdog-timestamp", &s->watchdog_timestamp); |
2858 | (void) serialize_bool(f, "forbid-restart", s->forbid_restart); | |
6aca9a58 | 2859 | |
2787d83c | 2860 | if (s->watchdog_override_enable) |
d68c645b | 2861 | (void) serialize_item_format(f, "watchdog-override-usec", USEC_FMT, s->watchdog_override_usec); |
2787d83c | 2862 | |
aa8c4bbf LP |
2863 | if (s->watchdog_original_usec != USEC_INFINITY) |
2864 | (void) serialize_item_format(f, "watchdog-original-usec", USEC_FMT, s->watchdog_original_usec); | |
2865 | ||
3bd28bf7 LP |
2866 | if (s->reload_begin_usec != USEC_INFINITY) |
2867 | (void) serialize_item_format(f, "reload-begin-usec", USEC_FMT, s->reload_begin_usec); | |
2868 | ||
a16e1123 LP |
2869 | return 0; |
2870 | } | |
2871 | ||
35243b77 | 2872 | int service_deserialize_exec_command( |
5b99bd5f LP |
2873 | Unit *u, |
2874 | const char *key, | |
2875 | const char *value) { | |
2876 | ||
e266c068 MS |
2877 | Service *s = SERVICE(u); |
2878 | int r; | |
2879 | unsigned idx = 0, i; | |
2880 | bool control, found = false; | |
2881 | ServiceExecCommand id = _SERVICE_EXEC_COMMAND_INVALID; | |
2882 | ExecCommand *command = NULL; | |
6eeec374 | 2883 | _cleanup_free_ char *path = NULL; |
e266c068 MS |
2884 | _cleanup_strv_free_ char **argv = NULL; |
2885 | ||
2886 | enum ExecCommandState { | |
2887 | STATE_EXEC_COMMAND_TYPE, | |
2888 | STATE_EXEC_COMMAND_INDEX, | |
2889 | STATE_EXEC_COMMAND_PATH, | |
2890 | STATE_EXEC_COMMAND_ARGS, | |
2891 | _STATE_EXEC_COMMAND_MAX, | |
2d93c20e | 2892 | _STATE_EXEC_COMMAND_INVALID = -EINVAL, |
e266c068 MS |
2893 | } state; |
2894 | ||
2895 | assert(s); | |
2896 | assert(key); | |
2897 | assert(value); | |
2898 | ||
2899 | control = streq(key, "control-command"); | |
2900 | ||
2901 | state = STATE_EXEC_COMMAND_TYPE; | |
2902 | ||
2903 | for (;;) { | |
2904 | _cleanup_free_ char *arg = NULL; | |
2905 | ||
334c0979 | 2906 | r = extract_first_word(&value, &arg, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE); |
efa3f34e LP |
2907 | if (r < 0) |
2908 | return r; | |
e266c068 MS |
2909 | if (r == 0) |
2910 | break; | |
e266c068 MS |
2911 | |
2912 | switch (state) { | |
2913 | case STATE_EXEC_COMMAND_TYPE: | |
2914 | id = service_exec_command_from_string(arg); | |
2915 | if (id < 0) | |
7211c853 | 2916 | return id; |
e266c068 MS |
2917 | |
2918 | state = STATE_EXEC_COMMAND_INDEX; | |
2919 | break; | |
2920 | case STATE_EXEC_COMMAND_INDEX: | |
2921 | r = safe_atou(arg, &idx); | |
2922 | if (r < 0) | |
7211c853 | 2923 | return r; |
e266c068 MS |
2924 | |
2925 | state = STATE_EXEC_COMMAND_PATH; | |
2926 | break; | |
2927 | case STATE_EXEC_COMMAND_PATH: | |
ae2a15bc | 2928 | path = TAKE_PTR(arg); |
e266c068 | 2929 | state = STATE_EXEC_COMMAND_ARGS; |
e266c068 MS |
2930 | break; |
2931 | case STATE_EXEC_COMMAND_ARGS: | |
2932 | r = strv_extend(&argv, arg); | |
2933 | if (r < 0) | |
2934 | return -ENOMEM; | |
2935 | break; | |
2936 | default: | |
04499a70 | 2937 | assert_not_reached(); |
e266c068 MS |
2938 | } |
2939 | } | |
2940 | ||
2941 | if (state != STATE_EXEC_COMMAND_ARGS) | |
2942 | return -EINVAL; | |
90204792 ZJS |
2943 | if (strv_isempty(argv)) |
2944 | return -EINVAL; /* At least argv[0] must be always present. */ | |
e266c068 MS |
2945 | |
2946 | /* Let's check whether exec command on given offset matches data that we just deserialized */ | |
2947 | for (command = s->exec_command[id], i = 0; command; command = command->command_next, i++) { | |
2948 | if (i != idx) | |
2949 | continue; | |
2950 | ||
2951 | found = strv_equal(argv, command->argv) && streq(command->path, path); | |
2952 | break; | |
2953 | } | |
2954 | ||
2955 | if (!found) { | |
2956 | /* Command at the index we serialized is different, let's look for command that exactly | |
2957 | * matches but is on different index. If there is no such command we will not resume execution. */ | |
2958 | for (command = s->exec_command[id]; command; command = command->command_next) | |
2959 | if (strv_equal(command->argv, argv) && streq(command->path, path)) | |
2960 | break; | |
2961 | } | |
2962 | ||
e9da62b1 | 2963 | if (command && control) { |
e266c068 | 2964 | s->control_command = command; |
e9da62b1 LP |
2965 | s->control_command_id = id; |
2966 | } else if (command) | |
e266c068 MS |
2967 | s->main_command = command; |
2968 | else | |
2969 | log_unit_warning(u, "Current command vanished from the unit file, execution of the command list won't be resumed."); | |
2970 | ||
2971 | return 0; | |
2972 | } | |
2973 | ||
a16e1123 LP |
2974 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { |
2975 | Service *s = SERVICE(u); | |
2339fc93 | 2976 | int r; |
a16e1123 LP |
2977 | |
2978 | assert(u); | |
2979 | assert(key); | |
2980 | assert(value); | |
2981 | assert(fds); | |
2982 | ||
2983 | if (streq(key, "state")) { | |
2984 | ServiceState state; | |
2985 | ||
117dcc57 ZJS |
2986 | state = service_state_from_string(value); |
2987 | if (state < 0) | |
f2341e0a | 2988 | log_unit_debug(u, "Failed to parse state value: %s", value); |
a16e1123 LP |
2989 | else |
2990 | s->deserialized_state = state; | |
f42806df LP |
2991 | } else if (streq(key, "result")) { |
2992 | ServiceResult f; | |
2993 | ||
2994 | f = service_result_from_string(value); | |
2995 | if (f < 0) | |
f2341e0a | 2996 | log_unit_debug(u, "Failed to parse result value: %s", value); |
f42806df LP |
2997 | else if (f != SERVICE_SUCCESS) |
2998 | s->result = f; | |
2999 | ||
3000 | } else if (streq(key, "reload-result")) { | |
3001 | ServiceResult f; | |
3002 | ||
3003 | f = service_result_from_string(value); | |
3004 | if (f < 0) | |
f2341e0a | 3005 | log_unit_debug(u, "Failed to parse reload result value: %s", value); |
f42806df LP |
3006 | else if (f != SERVICE_SUCCESS) |
3007 | s->reload_result = f; | |
a16e1123 | 3008 | |
a16e1123 | 3009 | } else if (streq(key, "control-pid")) { |
5925dd3c | 3010 | pid_t pid; |
a16e1123 | 3011 | |
e364ad06 | 3012 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 3013 | log_unit_debug(u, "Failed to parse control-pid value: %s", value); |
a16e1123 | 3014 | else |
e55224ca | 3015 | s->control_pid = pid; |
a16e1123 | 3016 | } else if (streq(key, "main-pid")) { |
5925dd3c | 3017 | pid_t pid; |
a16e1123 | 3018 | |
e364ad06 | 3019 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 3020 | log_unit_debug(u, "Failed to parse main-pid value: %s", value); |
eabd3e56 LP |
3021 | else |
3022 | (void) service_set_main_pid(s, pid); | |
a16e1123 LP |
3023 | } else if (streq(key, "main-pid-known")) { |
3024 | int b; | |
3025 | ||
117dcc57 ZJS |
3026 | b = parse_boolean(value); |
3027 | if (b < 0) | |
f2341e0a | 3028 | log_unit_debug(u, "Failed to parse main-pid-known value: %s", value); |
a16e1123 LP |
3029 | else |
3030 | s->main_pid_known = b; | |
de1d4f9b WF |
3031 | } else if (streq(key, "bus-name-good")) { |
3032 | int b; | |
3033 | ||
3034 | b = parse_boolean(value); | |
3035 | if (b < 0) | |
3036 | log_unit_debug(u, "Failed to parse bus-name-good value: %s", value); | |
3037 | else | |
3038 | s->bus_name_good = b; | |
d8ccf5fd DM |
3039 | } else if (streq(key, "bus-name-owner")) { |
3040 | r = free_and_strdup(&s->bus_name_owner, value); | |
3041 | if (r < 0) | |
3042 | log_unit_error_errno(u, r, "Unable to deserialize current bus owner %s: %m", value); | |
3a2776bc LP |
3043 | } else if (streq(key, "status-text")) { |
3044 | char *t; | |
e437538f | 3045 | ssize_t l; |
3a2776bc | 3046 | |
e437538f ZJS |
3047 | l = cunescape(value, 0, &t); |
3048 | if (l < 0) | |
3049 | log_unit_debug_errno(u, l, "Failed to unescape status text '%s': %m", value); | |
efa3f34e LP |
3050 | else |
3051 | free_and_replace(s->status_text, t); | |
3a2776bc | 3052 | |
9dfb64f8 ZJS |
3053 | } else if (streq(key, "accept-socket")) { |
3054 | Unit *socket; | |
3055 | ||
3056 | r = manager_load_unit(u->manager, value, NULL, NULL, &socket); | |
3057 | if (r < 0) | |
5e1ee764 | 3058 | log_unit_debug_errno(u, r, "Failed to load accept-socket unit '%s': %m", value); |
9dfb64f8 | 3059 | else { |
7f7d01ed | 3060 | unit_ref_set(&s->accept_socket, u, socket); |
9dfb64f8 ZJS |
3061 | SOCKET(socket)->n_connections++; |
3062 | } | |
3063 | ||
a16e1123 LP |
3064 | } else if (streq(key, "socket-fd")) { |
3065 | int fd; | |
3066 | ||
3067 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 3068 | log_unit_debug(u, "Failed to parse socket-fd value: %s", value); |
a16e1123 | 3069 | else { |
574634bc | 3070 | asynchronous_close(s->socket_fd); |
a16e1123 LP |
3071 | s->socket_fd = fdset_remove(fds, fd); |
3072 | } | |
2339fc93 | 3073 | } else if (streq(key, "fd-store-fd")) { |
30520492 | 3074 | _cleanup_free_ char *fdv = NULL, *fdn = NULL, *fdp = NULL; |
2339fc93 | 3075 | int fd; |
30520492 | 3076 | int do_poll; |
2339fc93 | 3077 | |
30520492 KL |
3078 | r = extract_first_word(&value, &fdv, NULL, 0); |
3079 | if (r <= 0 || safe_atoi(fdv, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) { | |
f2341e0a | 3080 | log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); |
30520492 KL |
3081 | return 0; |
3082 | } | |
8dd4c05b | 3083 | |
30520492 KL |
3084 | r = extract_first_word(&value, &fdn, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE); |
3085 | if (r <= 0) { | |
95599cac | 3086 | log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); |
30520492 KL |
3087 | return 0; |
3088 | } | |
8dd4c05b | 3089 | |
30520492 KL |
3090 | r = extract_first_word(&value, &fdp, NULL, 0); |
3091 | if (r == 0) { | |
3092 | /* If the value is not present, we assume the default */ | |
3093 | do_poll = 1; | |
3094 | } else if (r < 0 || safe_atoi(fdp, &do_poll) < 0) { | |
3095 | log_unit_debug_errno(u, r, "Failed to parse fd-store-fd value \"%s\": %m", value); | |
3096 | return 0; | |
2339fc93 LP |
3097 | } |
3098 | ||
30520492 KL |
3099 | r = service_add_fd_store(s, fd, fdn, do_poll); |
3100 | if (r < 0) | |
3101 | log_unit_error_errno(u, r, "Failed to add fd to store: %m"); | |
3102 | else | |
3103 | fdset_remove(fds, fd); | |
ecdbca40 LP |
3104 | } else if (streq(key, "main-exec-status-pid")) { |
3105 | pid_t pid; | |
3106 | ||
e364ad06 | 3107 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 3108 | log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value); |
ecdbca40 LP |
3109 | else |
3110 | s->main_exec_status.pid = pid; | |
3111 | } else if (streq(key, "main-exec-status-code")) { | |
3112 | int i; | |
3113 | ||
e364ad06 | 3114 | if (safe_atoi(value, &i) < 0) |
f2341e0a | 3115 | log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value); |
ecdbca40 LP |
3116 | else |
3117 | s->main_exec_status.code = i; | |
3118 | } else if (streq(key, "main-exec-status-status")) { | |
3119 | int i; | |
3120 | ||
e364ad06 | 3121 | if (safe_atoi(value, &i) < 0) |
f2341e0a | 3122 | log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value); |
ecdbca40 LP |
3123 | else |
3124 | s->main_exec_status.status = i; | |
799fd0fd | 3125 | } else if (streq(key, "main-exec-status-start")) |
d68c645b | 3126 | deserialize_dual_timestamp(value, &s->main_exec_status.start_timestamp); |
799fd0fd | 3127 | else if (streq(key, "main-exec-status-exit")) |
d68c645b | 3128 | deserialize_dual_timestamp(value, &s->main_exec_status.exit_timestamp); |
a6927d7f | 3129 | else if (streq(key, "watchdog-timestamp")) |
d68c645b | 3130 | deserialize_dual_timestamp(value, &s->watchdog_timestamp); |
613b411c | 3131 | else if (streq(key, "forbid-restart")) { |
6aca9a58 SE |
3132 | int b; |
3133 | ||
3134 | b = parse_boolean(value); | |
3135 | if (b < 0) | |
f2341e0a | 3136 | log_unit_debug(u, "Failed to parse forbid-restart value: %s", value); |
6aca9a58 SE |
3137 | else |
3138 | s->forbid_restart = b; | |
a34ceba6 LP |
3139 | } else if (streq(key, "stdin-fd")) { |
3140 | int fd; | |
3141 | ||
3142 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3143 | log_unit_debug(u, "Failed to parse stdin-fd value: %s", value); | |
3144 | else { | |
3145 | asynchronous_close(s->stdin_fd); | |
3146 | s->stdin_fd = fdset_remove(fds, fd); | |
1e22b5cd | 3147 | s->exec_context.stdio_as_fds = true; |
a34ceba6 LP |
3148 | } |
3149 | } else if (streq(key, "stdout-fd")) { | |
3150 | int fd; | |
3151 | ||
3152 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3153 | log_unit_debug(u, "Failed to parse stdout-fd value: %s", value); | |
3154 | else { | |
3155 | asynchronous_close(s->stdout_fd); | |
3156 | s->stdout_fd = fdset_remove(fds, fd); | |
1e22b5cd | 3157 | s->exec_context.stdio_as_fds = true; |
a34ceba6 LP |
3158 | } |
3159 | } else if (streq(key, "stderr-fd")) { | |
3160 | int fd; | |
3161 | ||
3162 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3163 | log_unit_debug(u, "Failed to parse stderr-fd value: %s", value); | |
3164 | else { | |
3165 | asynchronous_close(s->stderr_fd); | |
3166 | s->stderr_fd = fdset_remove(fds, fd); | |
1e22b5cd | 3167 | s->exec_context.stdio_as_fds = true; |
a34ceba6 | 3168 | } |
5686391b LP |
3169 | } else if (streq(key, "exec-fd")) { |
3170 | int fd; | |
3171 | ||
3172 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3173 | log_unit_debug(u, "Failed to parse exec-fd value: %s", value); | |
3174 | else { | |
5dcadb4c | 3175 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
5686391b LP |
3176 | |
3177 | fd = fdset_remove(fds, fd); | |
3178 | if (service_allocate_exec_fd_event_source(s, fd, &s->exec_fd_event_source) < 0) | |
3179 | safe_close(fd); | |
3180 | } | |
2787d83c | 3181 | } else if (streq(key, "watchdog-override-usec")) { |
d68c645b | 3182 | if (deserialize_usec(value, &s->watchdog_override_usec) < 0) |
2787d83c | 3183 | log_unit_debug(u, "Failed to parse watchdog_override_usec value: %s", value); |
d68c645b | 3184 | else |
2787d83c | 3185 | s->watchdog_override_enable = true; |
d68c645b | 3186 | |
aa8c4bbf LP |
3187 | } else if (streq(key, "watchdog-original-usec")) { |
3188 | if (deserialize_usec(value, &s->watchdog_original_usec) < 0) | |
3189 | log_unit_debug(u, "Failed to parse watchdog_original_usec value: %s", value); | |
3190 | ||
e266c068 MS |
3191 | } else if (STR_IN_SET(key, "main-command", "control-command")) { |
3192 | r = service_deserialize_exec_command(u, key, value); | |
3193 | if (r < 0) | |
3194 | log_unit_debug_errno(u, r, "Failed to parse serialized command \"%s\": %m", value); | |
7a0019d3 LP |
3195 | |
3196 | } else if (streq(key, "n-restarts")) { | |
3197 | r = safe_atou(value, &s->n_restarts); | |
3198 | if (r < 0) | |
3199 | log_unit_debug_errno(u, r, "Failed to parse serialized restart counter '%s': %m", value); | |
3200 | ||
3201 | } else if (streq(key, "flush-n-restarts")) { | |
3202 | r = parse_boolean(value); | |
3203 | if (r < 0) | |
3204 | log_unit_debug_errno(u, r, "Failed to parse serialized flush restart counter setting '%s': %m", value); | |
3205 | else | |
3206 | s->flush_n_restarts = r; | |
3bd28bf7 LP |
3207 | } else if (streq(key, "reload-begin-usec")) { |
3208 | r = deserialize_usec(value, &s->reload_begin_usec); | |
3209 | if (r < 0) | |
3210 | log_unit_debug_errno(u, r, "Failed to parse serialized reload begin timestamp '%s', ignoring: %m", value); | |
c17ec25e | 3211 | } else |
f2341e0a | 3212 | log_unit_debug(u, "Unknown serialization key: %s", key); |
a16e1123 LP |
3213 | |
3214 | return 0; | |
3215 | } | |
3216 | ||
44a6b1b6 | 3217 | _pure_ static UnitActiveState service_active_state(Unit *u) { |
e056b01d LP |
3218 | const UnitActiveState *table; |
3219 | ||
87f0e418 | 3220 | assert(u); |
5cb5a6ff | 3221 | |
e056b01d LP |
3222 | table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
3223 | ||
3224 | return table[SERVICE(u)->state]; | |
034c6ed7 LP |
3225 | } |
3226 | ||
10a94420 LP |
3227 | static const char *service_sub_state_to_string(Unit *u) { |
3228 | assert(u); | |
3229 | ||
3230 | return service_state_to_string(SERVICE(u)->state); | |
3231 | } | |
3232 | ||
f2f725e5 | 3233 | static bool service_may_gc(Unit *u) { |
701cc384 LP |
3234 | Service *s = SERVICE(u); |
3235 | ||
3236 | assert(s); | |
3237 | ||
e98b2fbb | 3238 | /* Never clean up services that still have a process around, even if the service is formally dead. Note that |
f2f725e5 | 3239 | * unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they |
e98b2fbb LP |
3240 | * have moved outside of the cgroup. */ |
3241 | ||
3242 | if (main_pid_good(s) > 0 || | |
6d55002a | 3243 | control_pid_good(s) > 0) |
f2f725e5 | 3244 | return false; |
6d55002a | 3245 | |
f2f725e5 | 3246 | return true; |
6d55002a LP |
3247 | } |
3248 | ||
3a111838 MS |
3249 | static int service_retry_pid_file(Service *s) { |
3250 | int r; | |
3251 | ||
3252 | assert(s->pid_file); | |
3742095b | 3253 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
3a111838 MS |
3254 | |
3255 | r = service_load_pid_file(s, false); | |
3256 | if (r < 0) | |
3257 | return r; | |
3258 | ||
3259 | service_unwatch_pid_file(s); | |
3260 | ||
f42806df | 3261 | service_enter_running(s, SERVICE_SUCCESS); |
3a111838 MS |
3262 | return 0; |
3263 | } | |
3264 | ||
3265 | static int service_watch_pid_file(Service *s) { | |
3266 | int r; | |
3267 | ||
f2341e0a | 3268 | log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path); |
8bb2d17d | 3269 | |
5686391b | 3270 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_inotify_io); |
3a111838 MS |
3271 | if (r < 0) |
3272 | goto fail; | |
3273 | ||
3274 | /* the pidfile might have appeared just before we set the watch */ | |
f2341e0a | 3275 | log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path); |
3a111838 MS |
3276 | service_retry_pid_file(s); |
3277 | ||
3278 | return 0; | |
3279 | fail: | |
f2341e0a | 3280 | log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path); |
3a111838 MS |
3281 | service_unwatch_pid_file(s); |
3282 | return r; | |
3283 | } | |
3284 | ||
3285 | static int service_demand_pid_file(Service *s) { | |
3286 | PathSpec *ps; | |
3287 | ||
3288 | assert(s->pid_file); | |
3289 | assert(!s->pid_file_pathspec); | |
3290 | ||
3291 | ps = new0(PathSpec, 1); | |
3292 | if (!ps) | |
3293 | return -ENOMEM; | |
3294 | ||
718db961 | 3295 | ps->unit = UNIT(s); |
3a111838 MS |
3296 | ps->path = strdup(s->pid_file); |
3297 | if (!ps->path) { | |
3298 | free(ps); | |
3299 | return -ENOMEM; | |
3300 | } | |
3301 | ||
4ff361cc | 3302 | path_simplify(ps->path); |
3a111838 MS |
3303 | |
3304 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that | |
3305 | * keep their PID file open all the time. */ | |
3306 | ps->type = PATH_MODIFIED; | |
254d1313 | 3307 | ps->inotify_fd = -EBADF; |
3a111838 MS |
3308 | |
3309 | s->pid_file_pathspec = ps; | |
3310 | ||
3311 | return service_watch_pid_file(s); | |
3312 | } | |
3313 | ||
5686391b | 3314 | static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
99534007 | 3315 | PathSpec *p = ASSERT_PTR(userdata); |
e14c2802 LP |
3316 | Service *s; |
3317 | ||
e14c2802 | 3318 | s = SERVICE(p->unit); |
3a111838 MS |
3319 | |
3320 | assert(s); | |
3321 | assert(fd >= 0); | |
3742095b | 3322 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
3a111838 | 3323 | assert(s->pid_file_pathspec); |
57020a3a | 3324 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); |
3a111838 | 3325 | |
f2341e0a | 3326 | log_unit_debug(UNIT(s), "inotify event"); |
3a111838 | 3327 | |
e14c2802 | 3328 | if (path_spec_fd_event(p, events) < 0) |
3a111838 MS |
3329 | goto fail; |
3330 | ||
3331 | if (service_retry_pid_file(s) == 0) | |
718db961 | 3332 | return 0; |
3a111838 MS |
3333 | |
3334 | if (service_watch_pid_file(s) < 0) | |
3335 | goto fail; | |
3336 | ||
718db961 LP |
3337 | return 0; |
3338 | ||
3a111838 MS |
3339 | fail: |
3340 | service_unwatch_pid_file(s); | |
f42806df | 3341 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
718db961 | 3342 | return 0; |
3a111838 MS |
3343 | } |
3344 | ||
5686391b LP |
3345 | static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
3346 | Service *s = SERVICE(userdata); | |
3347 | ||
3348 | assert(s); | |
3349 | ||
3350 | log_unit_debug(UNIT(s), "got exec-fd event"); | |
3351 | ||
3352 | /* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve() | |
3353 | * successfully for it. We implement this through a pipe() towards the child, which the kernel automatically | |
3354 | * closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on the pipe in the | |
3355 | * parent. We need to be careful however, as there are other reasons that we might cause the child's side of | |
3356 | * the pipe to be closed (for example, a simple exit()). To deal with that we'll ignore EOFs on the pipe unless | |
3357 | * the child signalled us first that it is about to call the execve(). It does so by sending us a simple | |
3358 | * non-zero byte via the pipe. We also provide the child with a way to inform us in case execve() failed: if it | |
3359 | * sends a zero byte we'll ignore POLLHUP on the fd again. */ | |
3360 | ||
3361 | for (;;) { | |
3362 | uint8_t x; | |
3363 | ssize_t n; | |
3364 | ||
3365 | n = read(fd, &x, sizeof(x)); | |
3366 | if (n < 0) { | |
3367 | if (errno == EAGAIN) /* O_NONBLOCK in effect → everything queued has now been processed. */ | |
3368 | return 0; | |
3369 | ||
3370 | return log_unit_error_errno(UNIT(s), errno, "Failed to read from exec_fd: %m"); | |
3371 | } | |
3372 | if (n == 0) { /* EOF → the event we are waiting for */ | |
3373 | ||
5dcadb4c | 3374 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
5686391b LP |
3375 | |
3376 | if (s->exec_fd_hot) { /* Did the child tell us to expect EOF now? */ | |
3377 | log_unit_debug(UNIT(s), "Got EOF on exec-fd"); | |
3378 | ||
3379 | s->exec_fd_hot = false; | |
3380 | ||
3381 | /* Nice! This is what we have been waiting for. Transition to next state. */ | |
3382 | if (s->type == SERVICE_EXEC && s->state == SERVICE_START) | |
3383 | service_enter_start_post(s); | |
3384 | } else | |
3385 | log_unit_debug(UNIT(s), "Got EOF on exec-fd while it was disabled, ignoring."); | |
3386 | ||
3387 | return 0; | |
3388 | } | |
3389 | ||
3390 | /* A byte was read → this turns on/off the exec fd logic */ | |
3391 | assert(n == sizeof(x)); | |
3392 | s->exec_fd_hot = x; | |
3393 | } | |
3394 | ||
3395 | return 0; | |
3396 | } | |
3397 | ||
a911bb9a LP |
3398 | static void service_notify_cgroup_empty_event(Unit *u) { |
3399 | Service *s = SERVICE(u); | |
3400 | ||
3401 | assert(u); | |
3402 | ||
a5b5aece | 3403 | log_unit_debug(u, "Control group is empty."); |
a911bb9a LP |
3404 | |
3405 | switch (s->state) { | |
3406 | ||
3407 | /* Waiting for SIGCHLD is usually more interesting, | |
3408 | * because it includes return codes/signals. Which is | |
3409 | * why we ignore the cgroup events for most cases, | |
3410 | * except when we don't know pid which to expect the | |
3411 | * SIGCHLD for. */ | |
3412 | ||
3413 | case SERVICE_START: | |
3bd28bf7 | 3414 | if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) && |
3c751b1b LP |
3415 | main_pid_good(s) == 0 && |
3416 | control_pid_good(s) == 0) { | |
3d474ef7 | 3417 | /* No chance of getting a ready notification anymore */ |
c3fda31d | 3418 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
71e529fc JW |
3419 | break; |
3420 | } | |
3421 | ||
596e4470 HC |
3422 | if (s->exit_type == SERVICE_EXIT_CGROUP && main_pid_good(s) <= 0) |
3423 | service_enter_start_post(s); | |
3424 | ||
4831981d | 3425 | _fallthrough_; |
71e529fc | 3426 | case SERVICE_START_POST: |
3c751b1b LP |
3427 | if (s->pid_file_pathspec && |
3428 | main_pid_good(s) == 0 && | |
3429 | control_pid_good(s) == 0) { | |
3430 | ||
3d474ef7 | 3431 | /* Give up hoping for the daemon to write its PID file */ |
f2341e0a | 3432 | log_unit_warning(u, "Daemon never wrote its PID file. Failing."); |
8bb2d17d | 3433 | |
a911bb9a LP |
3434 | service_unwatch_pid_file(s); |
3435 | if (s->state == SERVICE_START) | |
c3fda31d | 3436 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
a911bb9a | 3437 | else |
c35755fb | 3438 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
a911bb9a LP |
3439 | } |
3440 | break; | |
3441 | ||
3442 | case SERVICE_RUNNING: | |
3443 | /* service_enter_running() will figure out what to do */ | |
3444 | service_enter_running(s, SERVICE_SUCCESS); | |
3445 | break; | |
3446 | ||
c87700a1 | 3447 | case SERVICE_STOP_WATCHDOG: |
a911bb9a LP |
3448 | case SERVICE_STOP_SIGTERM: |
3449 | case SERVICE_STOP_SIGKILL: | |
3450 | ||
b13ddbbc | 3451 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
a911bb9a LP |
3452 | service_enter_stop_post(s, SERVICE_SUCCESS); |
3453 | ||
3454 | break; | |
3455 | ||
3456 | case SERVICE_STOP_POST: | |
bf760801 | 3457 | case SERVICE_FINAL_WATCHDOG: |
a911bb9a LP |
3458 | case SERVICE_FINAL_SIGTERM: |
3459 | case SERVICE_FINAL_SIGKILL: | |
b13ddbbc | 3460 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
a911bb9a LP |
3461 | service_enter_dead(s, SERVICE_SUCCESS, true); |
3462 | ||
3463 | break; | |
3464 | ||
e08dabfe AZ |
3465 | /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean |
3466 | * up the cgroup earlier and should do it now. */ | |
3467 | case SERVICE_DEAD: | |
3468 | case SERVICE_FAILED: | |
3469 | unit_prune_cgroup(u); | |
3470 | break; | |
3471 | ||
a911bb9a LP |
3472 | default: |
3473 | ; | |
3474 | } | |
3475 | } | |
3476 | ||
38c41427 | 3477 | static void service_notify_cgroup_oom_event(Unit *u, bool managed_oom) { |
afcfaa69 LP |
3478 | Service *s = SERVICE(u); |
3479 | ||
38c41427 NK |
3480 | if (managed_oom) |
3481 | log_unit_debug(u, "Process(es) of control group were killed by systemd-oomd."); | |
3482 | else | |
3483 | log_unit_debug(u, "Process of control group was killed by the OOM killer."); | |
afcfaa69 LP |
3484 | |
3485 | if (s->oom_policy == OOM_CONTINUE) | |
3486 | return; | |
3487 | ||
3488 | switch (s->state) { | |
3489 | ||
31cd5f63 | 3490 | case SERVICE_CONDITION: |
afcfaa69 LP |
3491 | case SERVICE_START_PRE: |
3492 | case SERVICE_START: | |
3493 | case SERVICE_START_POST: | |
3494 | case SERVICE_STOP: | |
3495 | if (s->oom_policy == OOM_STOP) | |
3496 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_OOM_KILL); | |
3497 | else if (s->oom_policy == OOM_KILL) | |
3498 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3499 | ||
3500 | break; | |
3501 | ||
3502 | case SERVICE_EXITED: | |
3503 | case SERVICE_RUNNING: | |
3504 | if (s->oom_policy == OOM_STOP) | |
3505 | service_enter_stop(s, SERVICE_FAILURE_OOM_KILL); | |
3506 | else if (s->oom_policy == OOM_KILL) | |
3507 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3508 | ||
3509 | break; | |
3510 | ||
3511 | case SERVICE_STOP_WATCHDOG: | |
3512 | case SERVICE_STOP_SIGTERM: | |
3513 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3514 | break; | |
3515 | ||
3516 | case SERVICE_STOP_SIGKILL: | |
3517 | case SERVICE_FINAL_SIGKILL: | |
3518 | if (s->result == SERVICE_SUCCESS) | |
3519 | s->result = SERVICE_FAILURE_OOM_KILL; | |
3520 | break; | |
3521 | ||
3522 | case SERVICE_STOP_POST: | |
3523 | case SERVICE_FINAL_SIGTERM: | |
3524 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3525 | break; | |
3526 | ||
3527 | default: | |
3528 | ; | |
3529 | } | |
3530 | } | |
3531 | ||
87f0e418 | 3532 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
5cdabc8d | 3533 | bool notify_dbus = true; |
87f0e418 | 3534 | Service *s = SERVICE(u); |
f42806df | 3535 | ServiceResult f; |
e5123725 | 3536 | ExitClean clean_mode; |
5cb5a6ff LP |
3537 | |
3538 | assert(s); | |
034c6ed7 LP |
3539 | assert(pid >= 0); |
3540 | ||
e5123725 AZ |
3541 | /* Oneshot services and non-SERVICE_EXEC_START commands should not be |
3542 | * considered daemons as they are typically not long running. */ | |
3543 | if (s->type == SERVICE_ONESHOT || (s->control_pid == pid && s->control_command_id != SERVICE_EXEC_START)) | |
3544 | clean_mode = EXIT_CLEAN_COMMAND; | |
3545 | else | |
3546 | clean_mode = EXIT_CLEAN_DAEMON; | |
3547 | ||
3548 | if (is_clean_exit(code, status, clean_mode, &s->success_status)) | |
f42806df LP |
3549 | f = SERVICE_SUCCESS; |
3550 | else if (code == CLD_EXITED) | |
3551 | f = SERVICE_FAILURE_EXIT_CODE; | |
3552 | else if (code == CLD_KILLED) | |
3553 | f = SERVICE_FAILURE_SIGNAL; | |
3554 | else if (code == CLD_DUMPED) | |
3555 | f = SERVICE_FAILURE_CORE_DUMP; | |
d06dacd0 | 3556 | else |
04499a70 | 3557 | assert_not_reached(); |
034c6ed7 | 3558 | |
abaf5edd | 3559 | if (s->main_pid == pid) { |
13bb1ffb ZJS |
3560 | /* Clean up the exec_fd event source. We want to do this here, not later in |
3561 | * service_set_state(), because service_enter_stop_post() calls service_spawn(). | |
3562 | * The source owns its end of the pipe, so this will close that too. */ | |
3563 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
3564 | ||
db01f8b3 MS |
3565 | /* Forking services may occasionally move to a new PID. |
3566 | * As long as they update the PID file before exiting the old | |
3567 | * PID, they're fine. */ | |
db256aab | 3568 | if (service_load_pid_file(s, false) > 0) |
db01f8b3 | 3569 | return; |
034c6ed7 | 3570 | |
034c6ed7 | 3571 | s->main_pid = 0; |
6ea832a2 | 3572 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); |
034c6ed7 | 3573 | |
867b3b7d | 3574 | if (s->main_command) { |
fbeefb45 LP |
3575 | /* If this is not a forking service than the |
3576 | * main process got started and hence we copy | |
3577 | * the exit status so that it is recorded both | |
3578 | * as main and as control process exit | |
3579 | * status */ | |
3580 | ||
867b3b7d | 3581 | s->main_command->exec_status = s->main_exec_status; |
b708e7ce | 3582 | |
3ed0cd26 | 3583 | if (s->main_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
f42806df | 3584 | f = SERVICE_SUCCESS; |
fbeefb45 LP |
3585 | } else if (s->exec_command[SERVICE_EXEC_START]) { |
3586 | ||
3587 | /* If this is a forked process, then we should | |
3588 | * ignore the return value if this was | |
3589 | * configured for the starter process */ | |
3590 | ||
3ed0cd26 | 3591 | if (s->exec_command[SERVICE_EXEC_START]->flags & EXEC_COMMAND_IGNORE_FAILURE) |
fbeefb45 | 3592 | f = SERVICE_SUCCESS; |
034c6ed7 LP |
3593 | } |
3594 | ||
91bbd9b7 | 3595 | unit_log_process_exit( |
5cc2cd1c | 3596 | u, |
abaf5edd | 3597 | "Main process", |
91bbd9b7 | 3598 | service_exec_command_to_string(SERVICE_EXEC_START), |
5cc2cd1c | 3599 | f == SERVICE_SUCCESS, |
91bbd9b7 | 3600 | code, status); |
f42806df | 3601 | |
a0fef983 | 3602 | if (s->result == SERVICE_SUCCESS) |
f42806df | 3603 | s->result = f; |
034c6ed7 | 3604 | |
867b3b7d LP |
3605 | if (s->main_command && |
3606 | s->main_command->command_next && | |
b58aeb70 | 3607 | s->type == SERVICE_ONESHOT && |
f42806df | 3608 | f == SERVICE_SUCCESS) { |
034c6ed7 | 3609 | |
e78695d4 | 3610 | /* There is another command to execute, so let's do that. */ |
034c6ed7 | 3611 | |
f2341e0a | 3612 | log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state)); |
f42806df | 3613 | service_run_next_main(s); |
034c6ed7 | 3614 | |
34e9ba66 | 3615 | } else { |
867b3b7d | 3616 | s->main_command = NULL; |
34e9ba66 | 3617 | |
3bd28bf7 LP |
3618 | /* Services with ExitType=cgroup do not act on main PID exiting, unless the cgroup is |
3619 | * already empty */ | |
596e4470 HC |
3620 | if (s->exit_type == SERVICE_EXIT_MAIN || cgroup_good(s) <= 0) { |
3621 | /* The service exited, so the service is officially gone. */ | |
3622 | switch (s->state) { | |
3623 | ||
3624 | case SERVICE_START_POST: | |
3625 | case SERVICE_RELOAD: | |
3bd28bf7 LP |
3626 | case SERVICE_RELOAD_SIGNAL: |
3627 | case SERVICE_RELOAD_NOTIFY: | |
3628 | /* If neither main nor control processes are running then the current | |
3629 | * state can never exit cleanly, hence immediately terminate the | |
3630 | * service. */ | |
596e4470 HC |
3631 | if (control_pid_good(s) <= 0) |
3632 | service_enter_stop(s, f); | |
3633 | ||
3634 | /* Otherwise need to wait until the operation is done. */ | |
3635 | break; | |
bbe19f68 | 3636 | |
596e4470 HC |
3637 | case SERVICE_STOP: |
3638 | /* Need to wait until the operation is done. */ | |
3639 | break; | |
bbe19f68 | 3640 | |
596e4470 HC |
3641 | case SERVICE_START: |
3642 | if (s->type == SERVICE_ONESHOT) { | |
3643 | /* This was our main goal, so let's go on */ | |
3644 | if (f == SERVICE_SUCCESS) | |
3645 | service_enter_start_post(s); | |
3646 | else | |
3647 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3648 | break; | |
3bd28bf7 | 3649 | } else if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD)) { |
596e4470 HC |
3650 | /* Only enter running through a notification, so that the |
3651 | * SERVICE_START state signifies that no ready notification | |
3652 | * has been received */ | |
3653 | if (f != SERVICE_SUCCESS) | |
3654 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3655 | else if (!s->remain_after_exit || s->notify_access == NOTIFY_MAIN) | |
3656 | /* The service has never been and will never be active */ | |
3657 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); | |
3658 | break; | |
3659 | } | |
7d55e835 | 3660 | |
596e4470 HC |
3661 | _fallthrough_; |
3662 | case SERVICE_RUNNING: | |
3663 | service_enter_running(s, f); | |
3d474ef7 | 3664 | break; |
034c6ed7 | 3665 | |
596e4470 HC |
3666 | case SERVICE_STOP_WATCHDOG: |
3667 | case SERVICE_STOP_SIGTERM: | |
3668 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 3669 | |
596e4470 HC |
3670 | if (control_pid_good(s) <= 0) |
3671 | service_enter_stop_post(s, f); | |
5cb5a6ff | 3672 | |
596e4470 HC |
3673 | /* If there is still a control process, wait for that first */ |
3674 | break; | |
34e9ba66 | 3675 | |
596e4470 | 3676 | case SERVICE_STOP_POST: |
c1566ef0 | 3677 | |
596e4470 HC |
3678 | if (control_pid_good(s) <= 0) |
3679 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
c1566ef0 | 3680 | |
596e4470 | 3681 | break; |
c1566ef0 | 3682 | |
596e4470 HC |
3683 | case SERVICE_FINAL_WATCHDOG: |
3684 | case SERVICE_FINAL_SIGTERM: | |
3685 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 | 3686 | |
596e4470 HC |
3687 | if (control_pid_good(s) <= 0) |
3688 | service_enter_dead(s, f, true); | |
3689 | break; | |
bf108e55 | 3690 | |
596e4470 HC |
3691 | default: |
3692 | assert_not_reached(); | |
3693 | } | |
34e9ba66 | 3694 | } |
034c6ed7 | 3695 | } |
5cb5a6ff | 3696 | |
034c6ed7 | 3697 | } else if (s->control_pid == pid) { |
58441bc1 ZJS |
3698 | const char *kind; |
3699 | bool success; | |
3700 | ||
34e9ba66 LP |
3701 | s->control_pid = 0; |
3702 | ||
b708e7ce | 3703 | if (s->control_command) { |
8bb2d17d | 3704 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 3705 | |
3ed0cd26 | 3706 | if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
f42806df | 3707 | f = SERVICE_SUCCESS; |
b708e7ce LP |
3708 | } |
3709 | ||
42e6f549 AZ |
3710 | /* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */ |
3711 | if (s->state == SERVICE_CONDITION) { | |
3712 | if (f == SERVICE_FAILURE_EXIT_CODE && status < 255) { | |
3713 | UNIT(s)->condition_result = false; | |
3714 | f = SERVICE_SKIP_CONDITION; | |
58441bc1 ZJS |
3715 | success = true; |
3716 | } else if (f == SERVICE_SUCCESS) { | |
42e6f549 | 3717 | UNIT(s)->condition_result = true; |
58441bc1 ZJS |
3718 | success = true; |
3719 | } else | |
3720 | success = false; | |
3721 | ||
3722 | kind = "Condition check process"; | |
3723 | } else { | |
3724 | kind = "Control process"; | |
3725 | success = f == SERVICE_SUCCESS; | |
42e6f549 AZ |
3726 | } |
3727 | ||
91bbd9b7 | 3728 | unit_log_process_exit( |
5cc2cd1c | 3729 | u, |
58441bc1 | 3730 | kind, |
91bbd9b7 | 3731 | service_exec_command_to_string(s->control_command_id), |
58441bc1 | 3732 | success, |
91bbd9b7 | 3733 | code, status); |
f42806df | 3734 | |
d611cfa7 | 3735 | if (s->state != SERVICE_RELOAD && s->result == SERVICE_SUCCESS) |
f42806df | 3736 | s->result = f; |
034c6ed7 | 3737 | |
34e9ba66 LP |
3738 | if (s->control_command && |
3739 | s->control_command->command_next && | |
f42806df | 3740 | f == SERVICE_SUCCESS) { |
034c6ed7 | 3741 | |
3bd28bf7 | 3742 | /* There is another command to * execute, so let's do that. */ |
034c6ed7 | 3743 | |
f2341e0a | 3744 | log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state)); |
f42806df | 3745 | service_run_next_control(s); |
034c6ed7 | 3746 | |
80876c20 | 3747 | } else { |
3bd28bf7 | 3748 | /* No further commands for this step, so let's figure out what to do next */ |
034c6ed7 | 3749 | |
a16e1123 LP |
3750 | s->control_command = NULL; |
3751 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
3752 | ||
f2341e0a | 3753 | log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state)); |
bd982a8b | 3754 | |
034c6ed7 LP |
3755 | switch (s->state) { |
3756 | ||
31cd5f63 AZ |
3757 | case SERVICE_CONDITION: |
3758 | if (f == SERVICE_SUCCESS) | |
3759 | service_enter_start_pre(s); | |
3760 | else | |
3761 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3762 | break; | |
3763 | ||
034c6ed7 | 3764 | case SERVICE_START_PRE: |
f42806df | 3765 | if (f == SERVICE_SUCCESS) |
034c6ed7 LP |
3766 | service_enter_start(s); |
3767 | else | |
c3fda31d | 3768 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
3769 | break; |
3770 | ||
3771 | case SERVICE_START: | |
bfba3256 LP |
3772 | if (s->type != SERVICE_FORKING) |
3773 | /* Maybe spurious event due to a reload that changed the type? */ | |
3774 | break; | |
034c6ed7 | 3775 | |
f42806df | 3776 | if (f != SERVICE_SUCCESS) { |
c3fda31d | 3777 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
3a111838 MS |
3778 | break; |
3779 | } | |
034c6ed7 | 3780 | |
3a111838 | 3781 | if (s->pid_file) { |
f42806df LP |
3782 | bool has_start_post; |
3783 | int r; | |
3784 | ||
3a111838 MS |
3785 | /* Let's try to load the pid file here if we can. |
3786 | * The PID file might actually be created by a START_POST | |
3787 | * script. In that case don't worry if the loading fails. */ | |
f42806df | 3788 | |
5d904a6a | 3789 | has_start_post = s->exec_command[SERVICE_EXEC_START_POST]; |
f42806df | 3790 | r = service_load_pid_file(s, !has_start_post); |
3a111838 MS |
3791 | if (!has_start_post && r < 0) { |
3792 | r = service_demand_pid_file(s); | |
b13ddbbc | 3793 | if (r < 0 || cgroup_good(s) == 0) |
c3fda31d | 3794 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); |
3a111838 MS |
3795 | break; |
3796 | } | |
034c6ed7 | 3797 | } else |
783e05d6 | 3798 | service_search_main_pid(s); |
034c6ed7 | 3799 | |
3a111838 | 3800 | service_enter_start_post(s); |
034c6ed7 LP |
3801 | break; |
3802 | ||
3803 | case SERVICE_START_POST: | |
f42806df | 3804 | if (f != SERVICE_SUCCESS) { |
ce359e98 | 3805 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
2096e009 | 3806 | break; |
034c6ed7 LP |
3807 | } |
3808 | ||
2096e009 | 3809 | if (s->pid_file) { |
f42806df LP |
3810 | int r; |
3811 | ||
3812 | r = service_load_pid_file(s, true); | |
2096e009 MS |
3813 | if (r < 0) { |
3814 | r = service_demand_pid_file(s); | |
b13ddbbc | 3815 | if (r < 0 || cgroup_good(s) == 0) |
c35755fb | 3816 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
2096e009 MS |
3817 | break; |
3818 | } | |
3819 | } else | |
783e05d6 | 3820 | service_search_main_pid(s); |
2096e009 | 3821 | |
f42806df | 3822 | service_enter_running(s, SERVICE_SUCCESS); |
3185a36b | 3823 | break; |
034c6ed7 LP |
3824 | |
3825 | case SERVICE_RELOAD: | |
3bd28bf7 LP |
3826 | case SERVICE_RELOAD_SIGNAL: |
3827 | case SERVICE_RELOAD_NOTIFY: | |
7236ce6e ZJS |
3828 | if (f == SERVICE_SUCCESS) |
3829 | if (service_load_pid_file(s, true) < 0) | |
3830 | service_search_main_pid(s); | |
3185a36b | 3831 | |
f42806df | 3832 | s->reload_result = f; |
3bd28bf7 | 3833 | |
d09df6b9 | 3834 | /* If the last notification we received from the service process indicates |
3bd28bf7 LP |
3835 | * we are still reloading, then don't leave reloading state just yet, just |
3836 | * transition into SERVICE_RELOAD_NOTIFY, to wait for the READY=1 coming, | |
3837 | * too. */ | |
3838 | if (s->notify_state == NOTIFY_RELOADING) | |
3839 | service_set_state(s, SERVICE_RELOAD_NOTIFY); | |
3840 | else | |
3841 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
3842 | break; |
3843 | ||
3844 | case SERVICE_STOP: | |
f42806df | 3845 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
3846 | break; |
3847 | ||
c87700a1 | 3848 | case SERVICE_STOP_WATCHDOG: |
034c6ed7 LP |
3849 | case SERVICE_STOP_SIGTERM: |
3850 | case SERVICE_STOP_SIGKILL: | |
3851 | if (main_pid_good(s) <= 0) | |
f42806df | 3852 | service_enter_stop_post(s, f); |
034c6ed7 | 3853 | |
846a07b5 | 3854 | /* If there is still a service process around, wait until |
034c6ed7 LP |
3855 | * that one quit, too */ |
3856 | break; | |
3857 | ||
3858 | case SERVICE_STOP_POST: | |
c1566ef0 AZ |
3859 | if (main_pid_good(s) <= 0) |
3860 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
3861 | break; | |
3862 | ||
bf760801 | 3863 | case SERVICE_FINAL_WATCHDOG: |
034c6ed7 LP |
3864 | case SERVICE_FINAL_SIGTERM: |
3865 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 LP |
3866 | if (main_pid_good(s) <= 0) |
3867 | service_enter_dead(s, f, true); | |
034c6ed7 LP |
3868 | break; |
3869 | ||
4c2f5842 LP |
3870 | case SERVICE_CLEANING: |
3871 | ||
3872 | if (s->clean_result == SERVICE_SUCCESS) | |
3873 | s->clean_result = f; | |
3874 | ||
3875 | service_enter_dead(s, SERVICE_SUCCESS, false); | |
3876 | break; | |
3877 | ||
034c6ed7 | 3878 | default: |
04499a70 | 3879 | assert_not_reached(); |
034c6ed7 LP |
3880 | } |
3881 | } | |
5cdabc8d LP |
3882 | } else /* Neither control nor main PID? If so, don't notify about anything */ |
3883 | notify_dbus = false; | |
c4e2ceae LP |
3884 | |
3885 | /* Notify clients about changed exit status */ | |
5cdabc8d LP |
3886 | if (notify_dbus) |
3887 | unit_add_to_dbus_queue(u); | |
a911bb9a | 3888 | |
846a07b5 FB |
3889 | /* We watch the main/control process otherwise we can't retrieve the unit they |
3890 | * belong to with cgroupv1. But if they are not our direct child, we won't get a | |
3891 | * SIGCHLD for them. Therefore we need to look for others to watch so we can | |
3892 | * detect when the cgroup becomes empty. Note that the control process is always | |
3893 | * our child so it's pointless to watch all other processes. */ | |
3894 | if (!control_pid_good(s)) | |
3895 | if (!s->main_pid_known || s->main_pid_alien) | |
3896 | (void) unit_enqueue_rewatch_pids(u); | |
034c6ed7 LP |
3897 | } |
3898 | ||
718db961 LP |
3899 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
3900 | Service *s = SERVICE(userdata); | |
034c6ed7 LP |
3901 | |
3902 | assert(s); | |
718db961 | 3903 | assert(source == s->timer_event_source); |
034c6ed7 LP |
3904 | |
3905 | switch (s->state) { | |
3906 | ||
31cd5f63 | 3907 | case SERVICE_CONDITION: |
034c6ed7 LP |
3908 | case SERVICE_START_PRE: |
3909 | case SERVICE_START: | |
3910 | case SERVICE_START_POST: | |
bf760801 JK |
3911 | switch (s->timeout_start_failure_mode) { |
3912 | ||
3913 | case SERVICE_TIMEOUT_TERMINATE: | |
3914 | log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", service_state_to_string(s->state)); | |
3915 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
3916 | break; | |
3917 | ||
3918 | case SERVICE_TIMEOUT_ABORT: | |
3919 | log_unit_warning(UNIT(s), "%s operation timed out. Aborting.", service_state_to_string(s->state)); | |
3920 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3921 | break; | |
3922 | ||
3923 | case SERVICE_TIMEOUT_KILL: | |
3924 | if (s->kill_context.send_sigkill) { | |
3925 | log_unit_warning(UNIT(s), "%s operation timed out. Killing.", service_state_to_string(s->state)); | |
3926 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3927 | } else { | |
3928 | log_unit_warning(UNIT(s), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s->state)); | |
3929 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3930 | } | |
3931 | break; | |
3932 | ||
3933 | default: | |
04499a70 | 3934 | assert_not_reached(); |
bf760801 | 3935 | } |
034c6ed7 LP |
3936 | break; |
3937 | ||
36c16a7c LP |
3938 | case SERVICE_RUNNING: |
3939 | log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping."); | |
3940 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); | |
3941 | break; | |
3942 | ||
e2f3b44c | 3943 | case SERVICE_RELOAD: |
3bd28bf7 LP |
3944 | case SERVICE_RELOAD_SIGNAL: |
3945 | case SERVICE_RELOAD_NOTIFY: | |
089b64d5 | 3946 | log_unit_warning(UNIT(s), "Reload operation timed out. Killing reload process."); |
e9a4f676 | 3947 | service_kill_control_process(s); |
f42806df LP |
3948 | s->reload_result = SERVICE_FAILURE_TIMEOUT; |
3949 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c LP |
3950 | break; |
3951 | ||
034c6ed7 | 3952 | case SERVICE_STOP: |
bf760801 JK |
3953 | switch (s->timeout_stop_failure_mode) { |
3954 | ||
3955 | case SERVICE_TIMEOUT_TERMINATE: | |
3956 | log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); | |
3957 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
3958 | break; | |
3959 | ||
3960 | case SERVICE_TIMEOUT_ABORT: | |
3961 | log_unit_warning(UNIT(s), "Stopping timed out. Aborting."); | |
3962 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3963 | break; | |
3964 | ||
3965 | case SERVICE_TIMEOUT_KILL: | |
3966 | if (s->kill_context.send_sigkill) { | |
3967 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); | |
3968 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3969 | } else { | |
3970 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); | |
3971 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3972 | } | |
3973 | break; | |
3974 | ||
3975 | default: | |
04499a70 | 3976 | assert_not_reached(); |
bf760801 | 3977 | } |
034c6ed7 LP |
3978 | break; |
3979 | ||
c87700a1 | 3980 | case SERVICE_STOP_WATCHDOG: |
bf760801 JK |
3981 | if (s->kill_context.send_sigkill) { |
3982 | log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Killing."); | |
3983 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3984 | } else { | |
3985 | log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Skipping SIGKILL."); | |
3986 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3987 | } | |
db2cb23b UTL |
3988 | break; |
3989 | ||
034c6ed7 | 3990 | case SERVICE_STOP_SIGTERM: |
bf760801 JK |
3991 | if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { |
3992 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Aborting."); | |
3993 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3994 | } else if (s->kill_context.send_sigkill) { | |
f2341e0a | 3995 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing."); |
f42806df | 3996 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 3997 | } else { |
f2341e0a | 3998 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL."); |
f42806df | 3999 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
ba035df2 LP |
4000 | } |
4001 | ||
034c6ed7 LP |
4002 | break; |
4003 | ||
4004 | case SERVICE_STOP_SIGKILL: | |
35b8ca3a | 4005 | /* Uh, we sent a SIGKILL and it is still not gone? |
034c6ed7 LP |
4006 | * Must be something we cannot kill, so let's just be |
4007 | * weirded out and continue */ | |
4008 | ||
f2341e0a | 4009 | log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); |
f42806df | 4010 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
4011 | break; |
4012 | ||
4013 | case SERVICE_STOP_POST: | |
bf760801 JK |
4014 | switch (s->timeout_stop_failure_mode) { |
4015 | ||
4016 | case SERVICE_TIMEOUT_TERMINATE: | |
4017 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating."); | |
4018 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
4019 | break; | |
4020 | ||
4021 | case SERVICE_TIMEOUT_ABORT: | |
4022 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Aborting."); | |
4023 | service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4024 | break; | |
4025 | ||
4026 | case SERVICE_TIMEOUT_KILL: | |
4027 | if (s->kill_context.send_sigkill) { | |
4028 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Killing."); | |
4029 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4030 | } else { | |
4031 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode."); | |
4032 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
4033 | } | |
4034 | break; | |
4035 | ||
4036 | default: | |
04499a70 | 4037 | assert_not_reached(); |
bf760801 | 4038 | } |
034c6ed7 LP |
4039 | break; |
4040 | ||
bf760801 | 4041 | case SERVICE_FINAL_WATCHDOG: |
4819ff03 | 4042 | if (s->kill_context.send_sigkill) { |
bf760801 | 4043 | log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Killing."); |
f42806df | 4044 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 4045 | } else { |
bf760801 JK |
4046 | log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode."); |
4047 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
4048 | } | |
4049 | break; | |
4050 | ||
4051 | case SERVICE_FINAL_SIGTERM: | |
4052 | if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { | |
4053 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Aborting."); | |
4054 | service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4055 | } else if (s->kill_context.send_sigkill) { | |
4056 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Killing."); | |
4057 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4058 | } else { | |
4059 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode."); | |
f42806df | 4060 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
ba035df2 LP |
4061 | } |
4062 | ||
034c6ed7 LP |
4063 | break; |
4064 | ||
4065 | case SERVICE_FINAL_SIGKILL: | |
f2341e0a | 4066 | log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode."); |
f42806df | 4067 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
034c6ed7 LP |
4068 | break; |
4069 | ||
4070 | case SERVICE_AUTO_RESTART: | |
5291f26d | 4071 | if (s->restart_usec > 0) |
868f7d36 ZJS |
4072 | log_unit_debug(UNIT(s), |
4073 | "Service RestartSec=%s expired, scheduling restart.", | |
5291f26d ZJS |
4074 | FORMAT_TIMESPAN(s->restart_usec, USEC_PER_SEC)); |
4075 | else | |
868f7d36 ZJS |
4076 | log_unit_debug(UNIT(s), |
4077 | "Service has no hold-off time (RestartSec=0), scheduling restart."); | |
5ce6e7f5 | 4078 | |
034c6ed7 LP |
4079 | service_enter_restart(s); |
4080 | break; | |
4081 | ||
4c2f5842 LP |
4082 | case SERVICE_CLEANING: |
4083 | log_unit_warning(UNIT(s), "Cleaning timed out. killing."); | |
4084 | ||
4085 | if (s->clean_result == SERVICE_SUCCESS) | |
4086 | s->clean_result = SERVICE_FAILURE_TIMEOUT; | |
4087 | ||
4088 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, 0); | |
4089 | break; | |
4090 | ||
034c6ed7 | 4091 | default: |
04499a70 | 4092 | assert_not_reached(); |
034c6ed7 | 4093 | } |
718db961 LP |
4094 | |
4095 | return 0; | |
4096 | } | |
4097 | ||
4098 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { | |
4099 | Service *s = SERVICE(userdata); | |
2787d83c | 4100 | usec_t watchdog_usec; |
718db961 LP |
4101 | |
4102 | assert(s); | |
4103 | assert(source == s->watchdog_event_source); | |
4104 | ||
2787d83c M |
4105 | watchdog_usec = service_get_watchdog_usec(s); |
4106 | ||
2a12e32e JK |
4107 | if (UNIT(s)->manager->service_watchdogs) { |
4108 | log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!", | |
5291f26d | 4109 | FORMAT_TIMESPAN(watchdog_usec, 1)); |
8bb2d17d | 4110 | |
c87700a1 | 4111 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); |
2a12e32e JK |
4112 | } else |
4113 | log_unit_warning(UNIT(s), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!", | |
5291f26d | 4114 | FORMAT_TIMESPAN(watchdog_usec, 1)); |
842129f5 | 4115 | |
718db961 | 4116 | return 0; |
5cb5a6ff LP |
4117 | } |
4118 | ||
fcee2755 | 4119 | static bool service_notify_message_authorized(Service *s, pid_t pid, FDSet *fds) { |
e3285237 | 4120 | assert(s); |
8c47c732 | 4121 | |
c952c6ec | 4122 | if (s->notify_access == NOTIFY_NONE) { |
e3285237 LP |
4123 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception is disabled.", pid); |
4124 | return false; | |
4125 | } | |
4126 | ||
4127 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { | |
336c6e46 | 4128 | if (s->main_pid != 0) |
e3285237 | 4129 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid, s->main_pid); |
336c6e46 | 4130 | else |
e3285237 LP |
4131 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", pid); |
4132 | ||
4133 | return false; | |
4134 | } | |
4135 | ||
4136 | if (s->notify_access == NOTIFY_EXEC && pid != s->main_pid && pid != s->control_pid) { | |
6375bd20 | 4137 | if (s->main_pid != 0 && s->control_pid != 0) |
e3285237 | 4138 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT" and control PID "PID_FMT, |
6375bd20 JW |
4139 | pid, s->main_pid, s->control_pid); |
4140 | else if (s->main_pid != 0) | |
e3285237 | 4141 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid, s->main_pid); |
6375bd20 | 4142 | else if (s->control_pid != 0) |
e3285237 | 4143 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for control PID "PID_FMT, pid, s->control_pid); |
6375bd20 | 4144 | else |
e3285237 LP |
4145 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception only permitted for main PID and control PID which are currently not known", pid); |
4146 | ||
4147 | return false; | |
9711848f LP |
4148 | } |
4149 | ||
e3285237 LP |
4150 | return true; |
4151 | } | |
4152 | ||
99b43caf JK |
4153 | static void service_force_watchdog(Service *s) { |
4154 | if (!UNIT(s)->manager->service_watchdogs) | |
4155 | return; | |
4156 | ||
4157 | log_unit_error(UNIT(s), "Watchdog request (last status: %s)!", | |
4158 | s->status_text ? s->status_text : "<unset>"); | |
4159 | ||
4160 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); | |
4161 | } | |
4162 | ||
db256aab LP |
4163 | static void service_notify_message( |
4164 | Unit *u, | |
4165 | const struct ucred *ucred, | |
fcee2755 | 4166 | char * const *tags, |
db256aab LP |
4167 | FDSet *fds) { |
4168 | ||
e3285237 LP |
4169 | Service *s = SERVICE(u); |
4170 | bool notify_dbus = false; | |
3bd28bf7 | 4171 | usec_t monotonic_usec = USEC_INFINITY; |
e3285237 | 4172 | const char *e; |
db256aab | 4173 | int r; |
e3285237 LP |
4174 | |
4175 | assert(u); | |
db256aab | 4176 | assert(ucred); |
e3285237 | 4177 | |
fcee2755 | 4178 | if (!service_notify_message_authorized(SERVICE(u), ucred->pid, fds)) |
e3285237 LP |
4179 | return; |
4180 | ||
f1d34068 | 4181 | if (DEBUG_LOGGING) { |
9711848f LP |
4182 | _cleanup_free_ char *cc = NULL; |
4183 | ||
4184 | cc = strv_join(tags, ", "); | |
db256aab | 4185 | log_unit_debug(u, "Got notification message from PID "PID_FMT" (%s)", ucred->pid, isempty(cc) ? "n/a" : cc); |
9711848f | 4186 | } |
c952c6ec | 4187 | |
8c47c732 | 4188 | /* Interpret MAINPID= */ |
28849dba | 4189 | e = strv_find_startswith(tags, "MAINPID="); |
3bd28bf7 | 4190 | if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY)) { |
db256aab LP |
4191 | pid_t new_main_pid; |
4192 | ||
4193 | if (parse_pid(e, &new_main_pid) < 0) | |
4194 | log_unit_warning(u, "Failed to parse MAINPID= field in notification message, ignoring: %s", e); | |
4195 | else if (!s->main_pid_known || new_main_pid != s->main_pid) { | |
4196 | ||
4197 | r = service_is_suitable_main_pid(s, new_main_pid, LOG_WARNING); | |
4198 | if (r == 0) { | |
5238e957 | 4199 | /* The new main PID is a bit suspicious, which is OK if the sender is privileged. */ |
db256aab LP |
4200 | |
4201 | if (ucred->uid == 0) { | |
4202 | log_unit_debug(u, "New main PID "PID_FMT" does not belong to service, but we'll accept it as the request to change it came from a privileged process.", new_main_pid); | |
4203 | r = 1; | |
4204 | } else | |
4205 | log_unit_debug(u, "New main PID "PID_FMT" does not belong to service, refusing.", new_main_pid); | |
4206 | } | |
4207 | if (r > 0) { | |
5c95eb28 | 4208 | (void) service_set_main_pid(s, new_main_pid); |
cdc2af3e | 4209 | |
f75f613d | 4210 | r = unit_watch_pid(UNIT(s), new_main_pid, false); |
cdc2af3e LP |
4211 | if (r < 0) |
4212 | log_unit_warning_errno(UNIT(s), r, "Failed to watch new main PID "PID_FMT" for service: %m", new_main_pid); | |
4213 | ||
db256aab LP |
4214 | notify_dbus = true; |
4215 | } | |
8c47c732 LP |
4216 | } |
4217 | } | |
4218 | ||
3bd28bf7 LP |
4219 | /* Parse MONOTONIC_USEC= */ |
4220 | e = strv_find_startswith(tags, "MONOTONIC_USEC="); | |
4221 | if (e) { | |
4222 | r = safe_atou64(e, &monotonic_usec); | |
4223 | if (r < 0) | |
4224 | log_unit_warning_errno(u, r, "Failed to parse MONOTONIC_USEC= field in notification message, ignoring: %s", e); | |
4225 | } | |
308d72dc | 4226 | |
3bd28bf7 LP |
4227 | /* Interpret READY=/STOPPING=/RELOADING=. STOPPING= wins over the others, and READY= over RELOADING= */ |
4228 | if (strv_contains(tags, "STOPPING=1")) { | |
4229 | s->notify_state = NOTIFY_STOPPING; | |
308d72dc | 4230 | |
3bd28bf7 LP |
4231 | if (IN_SET(s->state, SERVICE_RUNNING, SERVICE_RELOAD_SIGNAL, SERVICE_RELOAD_NOTIFY)) |
4232 | service_enter_stop_by_notify(s); | |
308d72dc | 4233 | |
3bd28bf7 | 4234 | notify_dbus = true; |
308d72dc | 4235 | |
3bd28bf7 | 4236 | } else if (strv_contains(tags, "READY=1")) { |
308d72dc | 4237 | |
3bd28bf7 | 4238 | s->notify_state = NOTIFY_READY; |
308d72dc | 4239 | |
3bd28bf7 LP |
4240 | /* Type=notify services inform us about completed initialization with READY=1 */ |
4241 | if (IN_SET(s->type, SERVICE_NOTIFY, SERVICE_NOTIFY_RELOAD) && | |
4242 | s->state == SERVICE_START) | |
4243 | service_enter_start_post(s); | |
308d72dc | 4244 | |
3bd28bf7 LP |
4245 | /* Sending READY=1 while we are reloading informs us that the reloading is complete. */ |
4246 | if (s->state == SERVICE_RELOAD_NOTIFY) | |
4247 | service_enter_running(s, SERVICE_SUCCESS); | |
308d72dc | 4248 | |
3bd28bf7 LP |
4249 | /* Combined RELOADING=1 and READY=1? Then this is indication that the service started and |
4250 | * immediately finished reloading. */ | |
4251 | if (s->state == SERVICE_RELOAD_SIGNAL && | |
4252 | strv_contains(tags, "RELOADING=1") && | |
4253 | monotonic_usec != USEC_INFINITY && | |
4254 | monotonic_usec >= s->reload_begin_usec) { | |
4255 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; | |
308d72dc | 4256 | |
3bd28bf7 LP |
4257 | /* Propagate a reload explicitly */ |
4258 | r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); | |
4259 | if (r < 0) | |
4260 | log_unit_warning(UNIT(s), "Failed to schedule propagation of reload, ignoring: %s", bus_error_message(&error, r)); | |
308d72dc | 4261 | |
3bd28bf7 | 4262 | service_enter_running(s, SERVICE_SUCCESS); |
cc2b7b11 | 4263 | } |
3bd28bf7 LP |
4264 | |
4265 | notify_dbus = true; | |
4266 | ||
4267 | } else if (strv_contains(tags, "RELOADING=1")) { | |
4268 | ||
4269 | s->notify_state = NOTIFY_RELOADING; | |
4270 | ||
4271 | /* Sending RELOADING=1 after we send SIGHUP to request a reload will transition | |
4272 | * things to "reload-notify" state, where we'll wait for READY=1 to let us know the | |
4273 | * reload is done. Note that we insist on a timestamp being sent along here, so that | |
4274 | * we know for sure this is a reload cycle initiated *after* we sent the signal */ | |
4275 | if (s->state == SERVICE_RELOAD_SIGNAL && | |
4276 | monotonic_usec != USEC_INFINITY && | |
4277 | monotonic_usec >= s->reload_begin_usec) | |
4278 | /* Note, we don't call service_enter_reload_by_notify() here, because we | |
4279 | * don't need reload propagation nor do we want to restart the time-out. */ | |
4280 | service_set_state(s, SERVICE_RELOAD_NOTIFY); | |
4281 | ||
4282 | if (s->state == SERVICE_RUNNING) | |
4283 | service_enter_reload_by_notify(s); | |
4284 | ||
4285 | notify_dbus = true; | |
8c47c732 LP |
4286 | } |
4287 | ||
4288 | /* Interpret STATUS= */ | |
28849dba | 4289 | e = strv_find_startswith(tags, "STATUS="); |
7f110ff9 | 4290 | if (e) { |
28849dba | 4291 | _cleanup_free_ char *t = NULL; |
8c47c732 | 4292 | |
28849dba | 4293 | if (!isempty(e)) { |
3eac1bca LP |
4294 | /* Note that this size limit check is mostly paranoia: since the datagram size we are willing |
4295 | * to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */ | |
4296 | if (strlen(e) > STATUS_TEXT_MAX) | |
4297 | log_unit_warning(u, "Status message overly long (%zu > %u), ignoring.", strlen(e), STATUS_TEXT_MAX); | |
4298 | else if (!utf8_is_valid(e)) | |
4299 | log_unit_warning(u, "Status message in notification message is not UTF-8 clean, ignoring."); | |
28849dba | 4300 | else { |
28849dba LP |
4301 | t = strdup(e); |
4302 | if (!t) | |
4303 | log_oom(); | |
3a2776bc | 4304 | } |
28849dba | 4305 | } |
8c47c732 | 4306 | |
30b5275a | 4307 | if (!streq_ptr(s->status_text, t)) { |
3b319885 | 4308 | free_and_replace(s->status_text, t); |
30b5275a | 4309 | notify_dbus = true; |
28849dba | 4310 | } |
8c47c732 | 4311 | } |
842129f5 | 4312 | |
4774e357 | 4313 | /* Interpret ERRNO= */ |
28849dba | 4314 | e = strv_find_startswith(tags, "ERRNO="); |
4774e357 MAA |
4315 | if (e) { |
4316 | int status_errno; | |
4317 | ||
2fa40742 LP |
4318 | status_errno = parse_errno(e); |
4319 | if (status_errno < 0) | |
4320 | log_unit_warning_errno(u, status_errno, | |
5e1ee764 | 4321 | "Failed to parse ERRNO= field value '%s' in notification message: %m", e); |
2fa40742 LP |
4322 | else if (s->status_errno != status_errno) { |
4323 | s->status_errno = status_errno; | |
4324 | notify_dbus = true; | |
4774e357 MAA |
4325 | } |
4326 | } | |
4327 | ||
a327431b DB |
4328 | /* Interpret EXTEND_TIMEOUT= */ |
4329 | e = strv_find_startswith(tags, "EXTEND_TIMEOUT_USEC="); | |
4330 | if (e) { | |
4331 | usec_t extend_timeout_usec; | |
4332 | if (safe_atou64(e, &extend_timeout_usec) < 0) | |
4333 | log_unit_warning(u, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e); | |
4334 | else | |
4335 | service_extend_timeout(s, extend_timeout_usec); | |
4336 | } | |
4337 | ||
6f285378 | 4338 | /* Interpret WATCHDOG= */ |
99b43caf JK |
4339 | e = strv_find_startswith(tags, "WATCHDOG="); |
4340 | if (e) { | |
4341 | if (streq(e, "1")) | |
4342 | service_reset_watchdog(s); | |
4343 | else if (streq(e, "trigger")) | |
4344 | service_force_watchdog(s); | |
4345 | else | |
4346 | log_unit_warning(u, "Passed WATCHDOG= field is invalid, ignoring."); | |
4347 | } | |
c4e2ceae | 4348 | |
c45d11cb LP |
4349 | e = strv_find_startswith(tags, "WATCHDOG_USEC="); |
4350 | if (e) { | |
4351 | usec_t watchdog_override_usec; | |
4352 | if (safe_atou64(e, &watchdog_override_usec) < 0) | |
4353 | log_unit_warning(u, "Failed to parse WATCHDOG_USEC=%s", e); | |
4354 | else | |
95d0d8ed | 4355 | service_override_watchdog_timeout(s, watchdog_override_usec); |
c45d11cb LP |
4356 | } |
4357 | ||
e78ee06d LP |
4358 | /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases, |
4359 | * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing | |
4360 | * fds, but optional when pushing in new fds, for compatibility reasons. */ | |
d29cc4d6 | 4361 | if (strv_contains(tags, "FDSTOREREMOVE=1")) { |
e78ee06d LP |
4362 | const char *name; |
4363 | ||
4364 | name = strv_find_startswith(tags, "FDNAME="); | |
4365 | if (!name || !fdname_is_valid(name)) | |
4366 | log_unit_warning(u, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring."); | |
4367 | else | |
4368 | service_remove_fd_store(s, name); | |
4369 | ||
d29cc4d6 | 4370 | } else if (strv_contains(tags, "FDSTORE=1")) { |
8dd4c05b LP |
4371 | const char *name; |
4372 | ||
4373 | name = strv_find_startswith(tags, "FDNAME="); | |
4374 | if (name && !fdname_is_valid(name)) { | |
4375 | log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring."); | |
4376 | name = NULL; | |
4377 | } | |
4378 | ||
cb5a46b8 | 4379 | (void) service_add_fd_store_set(s, fds, name, !strv_contains(tags, "FDPOLL=0")); |
8dd4c05b | 4380 | } |
a354329f | 4381 | |
c4e2ceae | 4382 | /* Notify clients about changed status or main pid */ |
30b5275a LP |
4383 | if (notify_dbus) |
4384 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
4385 | } |
4386 | ||
7a7821c8 | 4387 | static int service_get_timeout(Unit *u, usec_t *timeout) { |
68db7a3b | 4388 | Service *s = SERVICE(u); |
7a7821c8 | 4389 | uint64_t t; |
68db7a3b ZJS |
4390 | int r; |
4391 | ||
4392 | if (!s->timer_event_source) | |
4393 | return 0; | |
4394 | ||
7a7821c8 | 4395 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
4396 | if (r < 0) |
4397 | return r; | |
7a7821c8 LP |
4398 | if (t == USEC_INFINITY) |
4399 | return 0; | |
68db7a3b | 4400 | |
7a7821c8 | 4401 | *timeout = t; |
68db7a3b ZJS |
4402 | return 1; |
4403 | } | |
4404 | ||
e39eb045 LP |
4405 | static bool pick_up_pid_from_bus_name(Service *s) { |
4406 | assert(s); | |
4407 | ||
4408 | /* If the service is running but we have no main PID yet, get it from the owner of the D-Bus name */ | |
4409 | ||
4410 | return !pid_is_valid(s->main_pid) && | |
4411 | IN_SET(s->state, | |
4412 | SERVICE_START, | |
4413 | SERVICE_START_POST, | |
4414 | SERVICE_RUNNING, | |
3bd28bf7 LP |
4415 | SERVICE_RELOAD, |
4416 | SERVICE_RELOAD_SIGNAL, | |
4417 | SERVICE_RELOAD_NOTIFY); | |
e39eb045 LP |
4418 | } |
4419 | ||
4420 | static int bus_name_pid_lookup_callback(sd_bus_message *reply, void *userdata, sd_bus_error *ret_error) { | |
4421 | const sd_bus_error *e; | |
99534007 | 4422 | Unit *u = ASSERT_PTR(userdata); |
e39eb045 LP |
4423 | uint32_t pid; |
4424 | Service *s; | |
4425 | int r; | |
4426 | ||
4427 | assert(reply); | |
e39eb045 LP |
4428 | |
4429 | s = SERVICE(u); | |
4430 | s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); | |
4431 | ||
4432 | if (!s->bus_name || !pick_up_pid_from_bus_name(s)) | |
4433 | return 1; | |
4434 | ||
4435 | e = sd_bus_message_get_error(reply); | |
4436 | if (e) { | |
4437 | r = sd_bus_error_get_errno(e); | |
4438 | log_warning_errno(r, "GetConnectionUnixProcessID() failed: %s", bus_error_message(e, r)); | |
4439 | return 1; | |
4440 | } | |
4441 | ||
4442 | r = sd_bus_message_read(reply, "u", &pid); | |
4443 | if (r < 0) { | |
4444 | bus_log_parse_error(r); | |
4445 | return 1; | |
4446 | } | |
4447 | ||
4448 | if (!pid_is_valid(pid)) { | |
4449 | log_debug_errno(SYNTHETIC_ERRNO(EINVAL), "GetConnectionUnixProcessID() returned invalid PID"); | |
4450 | return 1; | |
4451 | } | |
4452 | ||
4453 | log_unit_debug(u, "D-Bus name %s is now owned by process " PID_FMT, s->bus_name, (pid_t) pid); | |
4454 | ||
5c95eb28 SS |
4455 | (void) service_set_main_pid(s, pid); |
4456 | (void) unit_watch_pid(UNIT(s), pid, false); | |
e39eb045 LP |
4457 | return 1; |
4458 | } | |
4459 | ||
fc67a943 | 4460 | static void service_bus_name_owner_change(Unit *u, const char *new_owner) { |
05e343b7 LP |
4461 | |
4462 | Service *s = SERVICE(u); | |
718db961 | 4463 | int r; |
05e343b7 LP |
4464 | |
4465 | assert(s); | |
05e343b7 | 4466 | |
fc67a943 LP |
4467 | if (new_owner) |
4468 | log_unit_debug(u, "D-Bus name %s now owned by %s", s->bus_name, new_owner); | |
05e343b7 | 4469 | else |
fc67a943 | 4470 | log_unit_debug(u, "D-Bus name %s now not owned by anyone.", s->bus_name); |
05e343b7 | 4471 | |
d7a0f1f4 | 4472 | s->bus_name_good = new_owner; |
05e343b7 | 4473 | |
d8ccf5fd DM |
4474 | /* Track the current owner, so we can reconstruct changes after a daemon reload */ |
4475 | r = free_and_strdup(&s->bus_name_owner, new_owner); | |
4476 | if (r < 0) { | |
4477 | log_unit_error_errno(u, r, "Unable to set new bus name owner %s: %m", new_owner); | |
4478 | return; | |
4479 | } | |
4480 | ||
05e343b7 LP |
4481 | if (s->type == SERVICE_DBUS) { |
4482 | ||
4483 | /* service_enter_running() will figure out what to | |
4484 | * do */ | |
4485 | if (s->state == SERVICE_RUNNING) | |
f42806df | 4486 | service_enter_running(s, SERVICE_SUCCESS); |
05e343b7 LP |
4487 | else if (s->state == SERVICE_START && new_owner) |
4488 | service_enter_start_post(s); | |
4489 | ||
e39eb045 | 4490 | } else if (new_owner && pick_up_pid_from_bus_name(s)) { |
05e343b7 | 4491 | |
718db961 | 4492 | /* Try to acquire PID from bus service */ |
05e343b7 | 4493 | |
e39eb045 LP |
4494 | s->bus_name_pid_lookup_slot = sd_bus_slot_unref(s->bus_name_pid_lookup_slot); |
4495 | ||
4496 | r = sd_bus_call_method_async( | |
4497 | u->manager->api_bus, | |
4498 | &s->bus_name_pid_lookup_slot, | |
4499 | "org.freedesktop.DBus", | |
4500 | "/org/freedesktop/DBus", | |
4501 | "org.freedesktop.DBus", | |
4502 | "GetConnectionUnixProcessID", | |
4503 | bus_name_pid_lookup_callback, | |
4504 | s, | |
4505 | "s", | |
4506 | s->bus_name); | |
4507 | if (r < 0) | |
4508 | log_debug_errno(r, "Failed to request owner PID of service name, ignoring: %m"); | |
7400b9d2 | 4509 | } |
05e343b7 LP |
4510 | } |
4511 | ||
3fabebf4 LP |
4512 | int service_set_socket_fd( |
4513 | Service *s, | |
4514 | int fd, | |
4515 | Socket *sock, | |
4516 | SocketPeer *peer, | |
4517 | bool selinux_context_net) { | |
4518 | ||
4519 | _cleanup_free_ char *peer_text = NULL; | |
79a98c60 | 4520 | int r; |
57020a3a | 4521 | |
4f2d528d LP |
4522 | assert(s); |
4523 | assert(fd >= 0); | |
4524 | ||
7f2fbbff LP |
4525 | /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs |
4526 | * to be configured. We take ownership of the passed fd on success. */ | |
4f2d528d | 4527 | |
1124fe6f | 4528 | if (UNIT(s)->load_state != UNIT_LOADED) |
4f2d528d LP |
4529 | return -EINVAL; |
4530 | ||
4531 | if (s->socket_fd >= 0) | |
4532 | return -EBUSY; | |
4533 | ||
3fabebf4 LP |
4534 | assert(!s->socket_peer); |
4535 | ||
4f2d528d LP |
4536 | if (s->state != SERVICE_DEAD) |
4537 | return -EAGAIN; | |
4538 | ||
3fabebf4 | 4539 | if (getpeername_pretty(fd, true, &peer_text) >= 0) { |
79a98c60 LP |
4540 | |
4541 | if (UNIT(s)->description) { | |
c2b2df60 | 4542 | _cleanup_free_ char *a = NULL; |
79a98c60 | 4543 | |
3fabebf4 | 4544 | a = strjoin(UNIT(s)->description, " (", peer_text, ")"); |
79a98c60 LP |
4545 | if (!a) |
4546 | return -ENOMEM; | |
4547 | ||
4548 | r = unit_set_description(UNIT(s), a); | |
4549 | } else | |
3fabebf4 | 4550 | r = unit_set_description(UNIT(s), peer_text); |
79a98c60 LP |
4551 | if (r < 0) |
4552 | return r; | |
4553 | } | |
4554 | ||
eef85c4a | 4555 | r = unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false, UNIT_DEPENDENCY_IMPLICIT); |
7f2fbbff LP |
4556 | if (r < 0) |
4557 | return r; | |
4558 | ||
4f2d528d | 4559 | s->socket_fd = fd; |
3fabebf4 | 4560 | s->socket_peer = socket_peer_ref(peer); |
16115b0a | 4561 | s->socket_fd_selinux_context_net = selinux_context_net; |
6cf6bbc2 | 4562 | |
7f7d01ed | 4563 | unit_ref_set(&s->accept_socket, UNIT(s), UNIT(sock)); |
7f2fbbff | 4564 | return 0; |
4f2d528d LP |
4565 | } |
4566 | ||
fdf20a31 | 4567 | static void service_reset_failed(Unit *u) { |
5632e374 LP |
4568 | Service *s = SERVICE(u); |
4569 | ||
4570 | assert(s); | |
4571 | ||
fdf20a31 | 4572 | if (s->state == SERVICE_FAILED) |
5632e374 LP |
4573 | service_set_state(s, SERVICE_DEAD); |
4574 | ||
f42806df LP |
4575 | s->result = SERVICE_SUCCESS; |
4576 | s->reload_result = SERVICE_SUCCESS; | |
4c2f5842 | 4577 | s->clean_result = SERVICE_SUCCESS; |
7a0019d3 LP |
4578 | s->n_restarts = 0; |
4579 | s->flush_n_restarts = false; | |
5632e374 LP |
4580 | } |
4581 | ||
718db961 | 4582 | static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
8a0867d6 | 4583 | Service *s = SERVICE(u); |
41efeaec | 4584 | |
a6951a50 LP |
4585 | assert(s); |
4586 | ||
814cc562 | 4587 | return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error); |
8a0867d6 LP |
4588 | } |
4589 | ||
291d565a LP |
4590 | static int service_main_pid(Unit *u) { |
4591 | Service *s = SERVICE(u); | |
4592 | ||
4593 | assert(s); | |
4594 | ||
4595 | return s->main_pid; | |
4596 | } | |
4597 | ||
4598 | static int service_control_pid(Unit *u) { | |
4599 | Service *s = SERVICE(u); | |
4600 | ||
4601 | assert(s); | |
4602 | ||
4603 | return s->control_pid; | |
4604 | } | |
4605 | ||
bb2c7685 LP |
4606 | static bool service_needs_console(Unit *u) { |
4607 | Service *s = SERVICE(u); | |
4608 | ||
4609 | assert(s); | |
4610 | ||
4611 | /* We provide our own implementation of this here, instead of relying of the generic implementation | |
4612 | * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */ | |
4613 | ||
4614 | if (!exec_context_may_touch_console(&s->exec_context)) | |
4615 | return false; | |
4616 | ||
4617 | return IN_SET(s->state, | |
31cd5f63 | 4618 | SERVICE_CONDITION, |
bb2c7685 LP |
4619 | SERVICE_START_PRE, |
4620 | SERVICE_START, | |
4621 | SERVICE_START_POST, | |
4622 | SERVICE_RUNNING, | |
4623 | SERVICE_RELOAD, | |
3bd28bf7 LP |
4624 | SERVICE_RELOAD_SIGNAL, |
4625 | SERVICE_RELOAD_NOTIFY, | |
bb2c7685 | 4626 | SERVICE_STOP, |
c87700a1 | 4627 | SERVICE_STOP_WATCHDOG, |
bb2c7685 LP |
4628 | SERVICE_STOP_SIGTERM, |
4629 | SERVICE_STOP_SIGKILL, | |
4630 | SERVICE_STOP_POST, | |
bf760801 | 4631 | SERVICE_FINAL_WATCHDOG, |
bb2c7685 LP |
4632 | SERVICE_FINAL_SIGTERM, |
4633 | SERVICE_FINAL_SIGKILL); | |
4634 | } | |
4635 | ||
7af67e9a LP |
4636 | static int service_exit_status(Unit *u) { |
4637 | Service *s = SERVICE(u); | |
4638 | ||
4639 | assert(u); | |
4640 | ||
4641 | if (s->main_exec_status.pid <= 0 || | |
4642 | !dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) | |
4643 | return -ENODATA; | |
4644 | ||
4645 | if (s->main_exec_status.code != CLD_EXITED) | |
4646 | return -EBADE; | |
4647 | ||
4648 | return s->main_exec_status.status; | |
4649 | } | |
4650 | ||
5e1669ff ZJS |
4651 | static const char* service_status_text(Unit *u) { |
4652 | Service *s = SERVICE(u); | |
4653 | ||
4654 | assert(s); | |
4655 | ||
4656 | return s->status_text; | |
4657 | } | |
4658 | ||
4c2f5842 LP |
4659 | static int service_clean(Unit *u, ExecCleanMask mask) { |
4660 | _cleanup_strv_free_ char **l = NULL; | |
4661 | Service *s = SERVICE(u); | |
4c2f5842 LP |
4662 | int r; |
4663 | ||
4664 | assert(s); | |
4665 | assert(mask != 0); | |
4666 | ||
4667 | if (s->state != SERVICE_DEAD) | |
4668 | return -EBUSY; | |
4669 | ||
4670 | r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l); | |
4671 | if (r < 0) | |
4672 | return r; | |
4673 | ||
4674 | if (strv_isempty(l)) | |
4675 | return -EUNATCH; | |
4676 | ||
4677 | service_unwatch_control_pid(s); | |
4678 | s->clean_result = SERVICE_SUCCESS; | |
4679 | s->control_command = NULL; | |
4680 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
4681 | ||
e5d6dcce | 4682 | r = service_arm_timer(s, /* relative= */ true, s->exec_context.timeout_clean_usec); |
4c2f5842 LP |
4683 | if (r < 0) |
4684 | goto fail; | |
4685 | ||
810ef318 | 4686 | r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); |
4c2f5842 LP |
4687 | if (r < 0) |
4688 | goto fail; | |
4689 | ||
4c2f5842 LP |
4690 | service_set_state(s, SERVICE_CLEANING); |
4691 | ||
4692 | return 0; | |
4693 | ||
4694 | fail: | |
810ef318 | 4695 | log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m"); |
4c2f5842 | 4696 | s->clean_result = SERVICE_FAILURE_RESOURCES; |
5dcadb4c | 4697 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
4c2f5842 LP |
4698 | return r; |
4699 | } | |
4700 | ||
4701 | static int service_can_clean(Unit *u, ExecCleanMask *ret) { | |
4702 | Service *s = SERVICE(u); | |
4703 | ||
4704 | assert(s); | |
4705 | ||
4706 | return exec_context_get_clean_mask(&s->exec_context, ret); | |
4707 | } | |
4708 | ||
eda0cbf0 | 4709 | static const char *service_finished_job(Unit *u, JobType t, JobResult result) { |
04d232d8 ZJS |
4710 | if (t == JOB_START && |
4711 | result == JOB_DONE && | |
4712 | SERVICE(u)->type == SERVICE_ONESHOT) | |
4713 | return "Finished %s."; | |
eda0cbf0 ZJS |
4714 | |
4715 | /* Fall back to generic */ | |
4716 | return NULL; | |
4717 | } | |
4718 | ||
705578c3 | 4719 | static int service_can_start(Unit *u) { |
9727f242 DDM |
4720 | Service *s = SERVICE(u); |
4721 | int r; | |
4722 | ||
4723 | assert(s); | |
4724 | ||
4725 | /* Make sure we don't enter a busy loop of some kind. */ | |
4726 | r = unit_test_start_limit(u); | |
4727 | if (r < 0) { | |
4728 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false); | |
4729 | return r; | |
4730 | } | |
4731 | ||
705578c3 | 4732 | return 1; |
9727f242 DDM |
4733 | } |
4734 | ||
94f04347 | 4735 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { |
48d83e33 ZJS |
4736 | [SERVICE_RESTART_NO] = "no", |
4737 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
4738 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", | |
6cfe2fde | 4739 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
dc99a976 | 4740 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
48d83e33 ZJS |
4741 | [SERVICE_RESTART_ON_ABORT] = "on-abort", |
4742 | [SERVICE_RESTART_ALWAYS] = "always", | |
94f04347 LP |
4743 | }; |
4744 | ||
4745 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
4746 | ||
4747 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
3bd28bf7 LP |
4748 | [SERVICE_SIMPLE] = "simple", |
4749 | [SERVICE_FORKING] = "forking", | |
4750 | [SERVICE_ONESHOT] = "oneshot", | |
4751 | [SERVICE_DBUS] = "dbus", | |
4752 | [SERVICE_NOTIFY] = "notify", | |
4753 | [SERVICE_NOTIFY_RELOAD] = "notify-reload", | |
4754 | [SERVICE_IDLE] = "idle", | |
4755 | [SERVICE_EXEC] = "exec", | |
94f04347 LP |
4756 | }; |
4757 | ||
4758 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
4759 | ||
596e4470 | 4760 | static const char* const service_exit_type_table[_SERVICE_EXIT_TYPE_MAX] = { |
fb138166 | 4761 | [SERVICE_EXIT_MAIN] = "main", |
596e4470 HC |
4762 | [SERVICE_EXIT_CGROUP] = "cgroup", |
4763 | }; | |
4764 | ||
4765 | DEFINE_STRING_TABLE_LOOKUP(service_exit_type, ServiceExitType); | |
4766 | ||
e537352b | 4767 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
48d83e33 ZJS |
4768 | [SERVICE_EXEC_CONDITION] = "ExecCondition", |
4769 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", | |
4770 | [SERVICE_EXEC_START] = "ExecStart", | |
94f04347 | 4771 | [SERVICE_EXEC_START_POST] = "ExecStartPost", |
48d83e33 ZJS |
4772 | [SERVICE_EXEC_RELOAD] = "ExecReload", |
4773 | [SERVICE_EXEC_STOP] = "ExecStop", | |
4774 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
94f04347 LP |
4775 | }; |
4776 | ||
4777 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
4778 | ||
b3d59367 | 4779 | static const char* const service_exec_ex_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
48d83e33 ZJS |
4780 | [SERVICE_EXEC_CONDITION] = "ExecConditionEx", |
4781 | [SERVICE_EXEC_START_PRE] = "ExecStartPreEx", | |
4782 | [SERVICE_EXEC_START] = "ExecStartEx", | |
b3d59367 | 4783 | [SERVICE_EXEC_START_POST] = "ExecStartPostEx", |
48d83e33 ZJS |
4784 | [SERVICE_EXEC_RELOAD] = "ExecReloadEx", |
4785 | [SERVICE_EXEC_STOP] = "ExecStopEx", | |
4786 | [SERVICE_EXEC_STOP_POST] = "ExecStopPostEx", | |
b3d59367 AZ |
4787 | }; |
4788 | ||
4789 | DEFINE_STRING_TABLE_LOOKUP(service_exec_ex_command, ServiceExecCommand); | |
4790 | ||
308d72dc | 4791 | static const char* const notify_state_table[_NOTIFY_STATE_MAX] = { |
48d83e33 ZJS |
4792 | [NOTIFY_UNKNOWN] = "unknown", |
4793 | [NOTIFY_READY] = "ready", | |
308d72dc | 4794 | [NOTIFY_RELOADING] = "reloading", |
48d83e33 | 4795 | [NOTIFY_STOPPING] = "stopping", |
308d72dc LP |
4796 | }; |
4797 | ||
4798 | DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); | |
4799 | ||
f42806df | 4800 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
48d83e33 ZJS |
4801 | [SERVICE_SUCCESS] = "success", |
4802 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
4803 | [SERVICE_FAILURE_PROTOCOL] = "protocol", | |
4804 | [SERVICE_FAILURE_TIMEOUT] = "timeout", | |
4805 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
4806 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
4807 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", | |
4808 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", | |
07299350 | 4809 | [SERVICE_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
48d83e33 ZJS |
4810 | [SERVICE_FAILURE_OOM_KILL] = "oom-kill", |
4811 | [SERVICE_SKIP_CONDITION] = "exec-condition", | |
f42806df LP |
4812 | }; |
4813 | ||
4814 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
4815 | ||
bf760801 JK |
4816 | static const char* const service_timeout_failure_mode_table[_SERVICE_TIMEOUT_FAILURE_MODE_MAX] = { |
4817 | [SERVICE_TIMEOUT_TERMINATE] = "terminate", | |
48d83e33 ZJS |
4818 | [SERVICE_TIMEOUT_ABORT] = "abort", |
4819 | [SERVICE_TIMEOUT_KILL] = "kill", | |
bf760801 JK |
4820 | }; |
4821 | ||
4822 | DEFINE_STRING_TABLE_LOOKUP(service_timeout_failure_mode, ServiceTimeoutFailureMode); | |
4823 | ||
87f0e418 | 4824 | const UnitVTable service_vtable = { |
7d17cfbc | 4825 | .object_size = sizeof(Service), |
718db961 LP |
4826 | .exec_context_offset = offsetof(Service, exec_context), |
4827 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
4828 | .kill_context_offset = offsetof(Service, kill_context), | |
613b411c | 4829 | .exec_runtime_offset = offsetof(Service, exec_runtime), |
29206d46 | 4830 | .dynamic_creds_offset = offsetof(Service, dynamic_creds), |
3ef63c31 | 4831 | |
f975e971 LP |
4832 | .sections = |
4833 | "Unit\0" | |
4834 | "Service\0" | |
4835 | "Install\0", | |
4ad49000 | 4836 | .private_section = "Service", |
71645aca | 4837 | |
1d9cc876 LP |
4838 | .can_transient = true, |
4839 | .can_delegate = true, | |
c80a9a33 | 4840 | .can_fail = true, |
4d824a4e | 4841 | .can_set_managed_oom = true, |
1d9cc876 | 4842 | |
034c6ed7 LP |
4843 | .init = service_init, |
4844 | .done = service_done, | |
a16e1123 | 4845 | .load = service_load, |
a354329f | 4846 | .release_resources = service_release_resources, |
a16e1123 LP |
4847 | |
4848 | .coldplug = service_coldplug, | |
034c6ed7 | 4849 | |
5cb5a6ff LP |
4850 | .dump = service_dump, |
4851 | ||
4852 | .start = service_start, | |
4853 | .stop = service_stop, | |
4854 | .reload = service_reload, | |
4855 | ||
034c6ed7 LP |
4856 | .can_reload = service_can_reload, |
4857 | ||
8a0867d6 | 4858 | .kill = service_kill, |
4c2f5842 LP |
4859 | .clean = service_clean, |
4860 | .can_clean = service_can_clean, | |
8a0867d6 | 4861 | |
d9e45bc3 MS |
4862 | .freeze = unit_freeze_vtable_common, |
4863 | .thaw = unit_thaw_vtable_common, | |
4864 | ||
a16e1123 LP |
4865 | .serialize = service_serialize, |
4866 | .deserialize_item = service_deserialize_item, | |
4867 | ||
5cb5a6ff | 4868 | .active_state = service_active_state, |
10a94420 | 4869 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 4870 | |
deb4e708 MK |
4871 | .will_restart = service_will_restart, |
4872 | ||
f2f725e5 | 4873 | .may_gc = service_may_gc, |
701cc384 | 4874 | |
034c6ed7 | 4875 | .sigchld_event = service_sigchld_event, |
2c4104f0 | 4876 | |
fdf20a31 | 4877 | .reset_failed = service_reset_failed, |
5632e374 | 4878 | |
4ad49000 | 4879 | .notify_cgroup_empty = service_notify_cgroup_empty_event, |
afcfaa69 | 4880 | .notify_cgroup_oom = service_notify_cgroup_oom_event, |
8c47c732 | 4881 | .notify_message = service_notify_message, |
8e274523 | 4882 | |
291d565a LP |
4883 | .main_pid = service_main_pid, |
4884 | .control_pid = service_control_pid, | |
4885 | ||
05e343b7 | 4886 | .bus_name_owner_change = service_bus_name_owner_change, |
05e343b7 | 4887 | |
74c964d3 LP |
4888 | .bus_set_property = bus_service_set_property, |
4889 | .bus_commit_properties = bus_service_commit_properties, | |
4139c1b2 | 4890 | |
68db7a3b | 4891 | .get_timeout = service_get_timeout, |
bb2c7685 | 4892 | .needs_console = service_needs_console, |
7af67e9a | 4893 | .exit_status = service_exit_status, |
5e1669ff | 4894 | .status_text = service_status_text, |
718db961 | 4895 | |
c6918296 | 4896 | .status_message_formats = { |
c6918296 | 4897 | .finished_start_job = { |
c6918296 | 4898 | [JOB_FAILED] = "Failed to start %s.", |
c6918296 MS |
4899 | }, |
4900 | .finished_stop_job = { | |
4901 | [JOB_DONE] = "Stopped %s.", | |
4902 | [JOB_FAILED] = "Stopped (with error) %s.", | |
c6918296 | 4903 | }, |
eda0cbf0 | 4904 | .finished_job = service_finished_job, |
c6918296 | 4905 | }, |
9727f242 | 4906 | |
705578c3 | 4907 | .can_start = service_can_start, |
5cb5a6ff | 4908 | }; |