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