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