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