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