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