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