]>
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 | ||
cdebedb4 PM |
1443 | static int service_create_monitor_md_env(Job *j, char **ret) { |
1444 | _cleanup_free_ char *var = NULL; | |
1445 | const char *list_delim = ";"; | |
1446 | bool first = true; | |
1447 | Unit *tu; | |
1448 | ||
1449 | assert(j); | |
1450 | assert(ret); | |
1451 | ||
1452 | /* Create an environment variable 'MONITOR_METADATA', if creation is successful | |
1453 | * a pointer to it is returned via ret. | |
1454 | * | |
1455 | * This variable contains a space separated set of fields which relate to | |
1456 | * the service(s) which triggered job 'j'. Job 'j' is the JOB_START job for | |
1457 | * an OnFailure= or OnSuccess= dependency. Format of the MONITOR_METADATA | |
1458 | * variable is as follows: | |
1459 | * | |
1460 | * MONITOR_METADATA="SERVICE_RESULT=<result-string0>,EXIT_CODE=<exit-code0>,EXIT_STATUS=<exit-status0>, | |
1461 | * INVOCATION_ID=<id>,UNIT=<triggering-unit0.service>; | |
1462 | * SERVICE_RESULT=<result-stringN>,EXIT_CODE=<exit-codeN>,EXIT_STATUS=<exit-statusN>, | |
1463 | * INVOCATION_ID=<id>,UNIT=<triggering-unitN.service>" | |
1464 | * | |
1465 | * Multiple results may be passed as in the above example if jobs are merged, i.e. | |
1466 | * some services a and b contain an OnFailure= or OnSuccess= dependency on the same | |
1467 | * service. | |
1468 | * | |
1469 | * For example: | |
1470 | * | |
3e7bf853 YW |
1471 | * MONITOR_METADATA="SERVICE_RESULT=exit-code,EXIT_CODE=exited,EXIT_STATUS=1,INVOCATION_ID=02dd868af2f344b18edaf74b618b2f90,UNIT=failure.service; |
1472 | * SERVICE_RESULT=exit-code,EXIT_CODE=exited,EXIT_STATUS=1,INVOCATION_ID=80cb228bd7344f77a090eda603a3cfe2,UNIT=failure2.service" | |
cdebedb4 PM |
1473 | */ |
1474 | ||
1475 | LIST_FOREACH(triggered_by, tu, j->triggered_by) { | |
1476 | Service *env_source = SERVICE(tu); | |
1477 | int r; | |
1478 | ||
1479 | if (!env_source) | |
1480 | continue; | |
1481 | ||
1482 | if (first) { | |
1483 | /* Add the environment variable name first. */ | |
1484 | r = strextendf(&var, "MONITOR_METADATA="); | |
1485 | if (r < 0) | |
1486 | return r; | |
1487 | ||
1488 | } | |
1489 | ||
1490 | r = strextendf(&var, "%sSERVICE_RESULT=%s", | |
1491 | !first ? list_delim : "", service_result_to_string(env_source->result)); | |
1492 | if (r < 0) | |
1493 | return r; | |
1494 | ||
1495 | first = false; | |
1496 | ||
1497 | if (env_source->main_exec_status.pid > 0 && | |
1498 | dual_timestamp_is_set(&env_source->main_exec_status.exit_timestamp)) { | |
1499 | r = strextendf(&var, ",EXIT_CODE=%s", | |
1500 | sigchld_code_to_string(env_source->main_exec_status.code)); | |
1501 | if (r < 0) | |
1502 | return r; | |
1503 | ||
1504 | if (env_source->main_exec_status.code == CLD_EXITED) | |
1505 | r = strextendf(&var, ",EXIT_STATUS=%i", | |
1506 | env_source->main_exec_status.status); | |
1507 | else | |
1508 | r = strextendf(&var, ",EXIT_STATUS=%s", | |
1509 | signal_to_string(env_source->main_exec_status.status)); | |
1510 | if (r < 0) | |
1511 | return r; | |
1512 | } | |
1513 | ||
1514 | if (!sd_id128_is_null(UNIT(env_source)->invocation_id)) { | |
1515 | r = strextendf(&var, ",INVOCATION_ID=" SD_ID128_FORMAT_STR, | |
1516 | SD_ID128_FORMAT_VAL(UNIT(env_source)->invocation_id)); | |
1517 | if (r < 0) | |
1518 | return r; | |
1519 | } | |
1520 | ||
1521 | r = strextendf(&var, ",UNIT=%s", UNIT(env_source)->id); | |
1522 | if (r < 0) | |
1523 | return r; | |
1524 | } | |
1525 | ||
1526 | *ret = TAKE_PTR(var); | |
1527 | return 0; | |
1528 | } | |
1529 | ||
81a2b7ce LP |
1530 | static int service_spawn( |
1531 | Service *s, | |
1532 | ExecCommand *c, | |
21b2ce39 | 1533 | usec_t timeout, |
c39f1ce2 | 1534 | ExecFlags flags, |
e78695d4 | 1535 | pid_t *ret_pid) { |
81a2b7ce | 1536 | |
b9c04eaf | 1537 | _cleanup_(exec_params_clear) ExecParameters exec_params = { |
c3f8a065 LP |
1538 | .flags = flags, |
1539 | .stdin_fd = -1, | |
1540 | .stdout_fd = -1, | |
1541 | .stderr_fd = -1, | |
1542 | .exec_fd = -1, | |
9fa95f85 | 1543 | }; |
5686391b | 1544 | _cleanup_(sd_event_source_unrefp) sd_event_source *exec_fd_source = NULL; |
c3f8a065 LP |
1545 | _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL; |
1546 | size_t n_env = 0; | |
3c7416b6 | 1547 | pid_t pid; |
8dd4c05b LP |
1548 | int r; |
1549 | ||
034c6ed7 LP |
1550 | assert(s); |
1551 | assert(c); | |
e78695d4 | 1552 | assert(ret_pid); |
034c6ed7 | 1553 | |
78f93209 | 1554 | r = unit_prepare_exec(UNIT(s)); /* This realizes the cgroup, among other things */ |
3c7416b6 LP |
1555 | if (r < 0) |
1556 | return r; | |
1557 | ||
bc989831 ZJS |
1558 | assert(!s->exec_fd_event_source); |
1559 | ||
9c1a61ad LP |
1560 | if (flags & EXEC_IS_CONTROL) { |
1561 | /* If this is a control process, mask the permissions/chroot application if this is requested. */ | |
1562 | if (s->permissions_start_only) | |
1703fa41 | 1563 | exec_params.flags &= ~EXEC_APPLY_SANDBOXING; |
9c1a61ad LP |
1564 | if (s->root_directory_start_only) |
1565 | exec_params.flags &= ~EXEC_APPLY_CHROOT; | |
1566 | } | |
1567 | ||
c39f1ce2 | 1568 | if ((flags & EXEC_PASS_FDS) || |
6cf6bbc2 LP |
1569 | s->exec_context.std_input == EXEC_INPUT_SOCKET || |
1570 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1571 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1572 | ||
c3f8a065 LP |
1573 | r = service_collect_fds(s, |
1574 | &exec_params.fds, | |
1575 | &exec_params.fd_names, | |
1576 | &exec_params.n_socket_fds, | |
1577 | &exec_params.n_storage_fds); | |
8dd4c05b | 1578 | if (r < 0) |
36c16a7c | 1579 | return r; |
6cf6bbc2 | 1580 | |
c3f8a065 | 1581 | log_unit_debug(UNIT(s), "Passing %zu fds to service", exec_params.n_socket_fds + exec_params.n_storage_fds); |
4f2d528d | 1582 | } |
44d8db9e | 1583 | |
5686391b | 1584 | if (!FLAGS_SET(flags, EXEC_IS_CONTROL) && s->type == SERVICE_EXEC) { |
c3f8a065 | 1585 | r = service_allocate_exec_fd(s, &exec_fd_source, &exec_params.exec_fd); |
5686391b LP |
1586 | if (r < 0) |
1587 | return r; | |
1588 | } | |
1589 | ||
36c16a7c LP |
1590 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), timeout)); |
1591 | if (r < 0) | |
1592 | return r; | |
034c6ed7 | 1593 | |
dcf3c3c3 | 1594 | our_env = new0(char*, 10); |
36c16a7c LP |
1595 | if (!our_env) |
1596 | return -ENOMEM; | |
c952c6ec | 1597 | |
3bdc25a4 | 1598 | if (service_exec_needs_notify_socket(s, flags)) { |
36c16a7c LP |
1599 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) |
1600 | return -ENOMEM; | |
c952c6ec | 1601 | |
3bdc25a4 LP |
1602 | exec_params.notify_socket = UNIT(s)->manager->notify_socket; |
1603 | } | |
1604 | ||
2105e76a | 1605 | if (s->main_pid > 0) |
36c16a7c LP |
1606 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) |
1607 | return -ENOMEM; | |
2105e76a | 1608 | |
c39f1ce2 | 1609 | if (MANAGER_IS_USER(UNIT(s)->manager)) |
df0ff127 | 1610 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid_cached()) < 0) |
36c16a7c | 1611 | return -ENOMEM; |
97ae63e2 | 1612 | |
dcf3c3c3 LP |
1613 | if (s->pid_file) |
1614 | if (asprintf(our_env + n_env++, "PIDFILE=%s", s->pid_file) < 0) | |
1615 | return -ENOMEM; | |
1616 | ||
8dd4c05b | 1617 | if (s->socket_fd >= 0) { |
3b1c5241 SL |
1618 | union sockaddr_union sa; |
1619 | socklen_t salen = sizeof(sa); | |
1620 | ||
f56e7bfe LP |
1621 | /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something |
1622 | * useful. Note that we do this only when we are still connected at this point in time, which we might | |
1623 | * very well not be. Hence we ignore all errors when retrieving peer information (as that might result | |
1624 | * in ENOTCONN), and just use whate we can use. */ | |
f2dbd059 | 1625 | |
f56e7bfe LP |
1626 | if (getpeername(s->socket_fd, &sa.sa, &salen) >= 0 && |
1627 | IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { | |
3b1c5241 SL |
1628 | _cleanup_free_ char *addr = NULL; |
1629 | char *t; | |
882ac6e7 | 1630 | unsigned port; |
3b1c5241 SL |
1631 | |
1632 | r = sockaddr_pretty(&sa.sa, salen, true, false, &addr); | |
1633 | if (r < 0) | |
36c16a7c | 1634 | return r; |
3b1c5241 | 1635 | |
b910cc72 | 1636 | t = strjoin("REMOTE_ADDR=", addr); |
36c16a7c LP |
1637 | if (!t) |
1638 | return -ENOMEM; | |
3b1c5241 SL |
1639 | our_env[n_env++] = t; |
1640 | ||
882ac6e7 SH |
1641 | r = sockaddr_port(&sa.sa, &port); |
1642 | if (r < 0) | |
1643 | return r; | |
3b1c5241 | 1644 | |
36c16a7c LP |
1645 | if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) |
1646 | return -ENOMEM; | |
3b1c5241 SL |
1647 | our_env[n_env++] = t; |
1648 | } | |
1649 | } | |
1650 | ||
136dc4c4 LP |
1651 | if (flags & EXEC_SETENV_RESULT) { |
1652 | if (asprintf(our_env + n_env++, "SERVICE_RESULT=%s", service_result_to_string(s->result)) < 0) | |
1653 | return -ENOMEM; | |
1654 | ||
1655 | if (s->main_exec_status.pid > 0 && | |
1656 | dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { | |
1657 | if (asprintf(our_env + n_env++, "EXIT_CODE=%s", sigchld_code_to_string(s->main_exec_status.code)) < 0) | |
1658 | return -ENOMEM; | |
1659 | ||
1660 | if (s->main_exec_status.code == CLD_EXITED) | |
1661 | r = asprintf(our_env + n_env++, "EXIT_STATUS=%i", s->main_exec_status.status); | |
1662 | else | |
1663 | r = asprintf(our_env + n_env++, "EXIT_STATUS=%s", signal_to_string(s->main_exec_status.status)); | |
cdebedb4 | 1664 | |
136dc4c4 LP |
1665 | if (r < 0) |
1666 | return -ENOMEM; | |
1667 | } | |
cdebedb4 PM |
1668 | |
1669 | } else if (flags & EXEC_SETENV_MONITOR_RESULT) { | |
1670 | Job *j = UNIT(s)->job; | |
1671 | if (j) { | |
1672 | r = service_create_monitor_md_env(j, our_env + n_env++); | |
1673 | if (r < 0) | |
1674 | return r; | |
1675 | } | |
136dc4c4 LP |
1676 | } |
1677 | ||
1ad6e8b3 LP |
1678 | r = unit_set_exec_params(UNIT(s), &exec_params); |
1679 | if (r < 0) | |
1680 | return r; | |
3536f49e | 1681 | |
4ab3d29f | 1682 | final_env = strv_env_merge(exec_params.environment, our_env); |
36c16a7c LP |
1683 | if (!final_env) |
1684 | return -ENOMEM; | |
c952c6ec | 1685 | |
ac647978 LP |
1686 | /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */ |
1687 | SET_FLAG(exec_params.flags, EXEC_NSS_BYPASS_BUS, | |
1688 | MANAGER_IS_SYSTEM(UNIT(s)->manager) && unit_has_name(UNIT(s), SPECIAL_DBUS_SERVICE)); | |
4ad49000 | 1689 | |
b9c04eaf | 1690 | strv_free_and_replace(exec_params.environment, final_env); |
34b3f625 | 1691 | exec_params.watchdog_usec = service_get_watchdog_usec(s); |
a34ceba6 | 1692 | exec_params.selinux_context_net = s->socket_fd_selinux_context_net; |
9fa95f85 DM |
1693 | if (s->type == SERVICE_IDLE) |
1694 | exec_params.idle_pipe = UNIT(s)->manager->idle_pipe; | |
a34ceba6 LP |
1695 | exec_params.stdin_fd = s->stdin_fd; |
1696 | exec_params.stdout_fd = s->stdout_fd; | |
1697 | exec_params.stderr_fd = s->stderr_fd; | |
9fa95f85 | 1698 | |
f2341e0a LP |
1699 | r = exec_spawn(UNIT(s), |
1700 | c, | |
9e2f7c11 | 1701 | &s->exec_context, |
9fa95f85 | 1702 | &exec_params, |
613b411c | 1703 | s->exec_runtime, |
29206d46 | 1704 | &s->dynamic_creds, |
9e2f7c11 | 1705 | &pid); |
9e2f7c11 | 1706 | if (r < 0) |
36c16a7c | 1707 | return r; |
034c6ed7 | 1708 | |
5686391b LP |
1709 | s->exec_fd_event_source = TAKE_PTR(exec_fd_source); |
1710 | s->exec_fd_hot = false; | |
1711 | ||
f75f613d FB |
1712 | r = unit_watch_pid(UNIT(s), pid, true); |
1713 | if (r < 0) | |
36c16a7c | 1714 | return r; |
034c6ed7 | 1715 | |
e78695d4 | 1716 | *ret_pid = pid; |
034c6ed7 | 1717 | |
5cb5a6ff | 1718 | return 0; |
034c6ed7 LP |
1719 | } |
1720 | ||
80876c20 LP |
1721 | static int main_pid_good(Service *s) { |
1722 | assert(s); | |
1723 | ||
51894d70 | 1724 | /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */ |
80876c20 | 1725 | |
fc08079e | 1726 | /* If we know the pid file, then let's just check if it is |
80876c20 | 1727 | * still valid */ |
6dfa5494 LP |
1728 | if (s->main_pid_known) { |
1729 | ||
1730 | /* If it's an alien child let's check if it is still | |
1731 | * alive ... */ | |
62220cf7 | 1732 | if (s->main_pid_alien && s->main_pid > 0) |
9f5650ae | 1733 | return pid_is_alive(s->main_pid); |
6dfa5494 LP |
1734 | |
1735 | /* .. otherwise assume we'll get a SIGCHLD for it, | |
1736 | * which we really should wait for to collect exit | |
1737 | * status and code */ | |
80876c20 | 1738 | return s->main_pid > 0; |
6dfa5494 | 1739 | } |
80876c20 LP |
1740 | |
1741 | /* We don't know the pid */ | |
1742 | return -EAGAIN; | |
1743 | } | |
1744 | ||
019be286 | 1745 | static int control_pid_good(Service *s) { |
80876c20 LP |
1746 | assert(s); |
1747 | ||
07697d7e LP |
1748 | /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to |
1749 | * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also | |
1750 | * means: we can't figure it out. */ | |
1751 | ||
80876c20 LP |
1752 | return s->control_pid > 0; |
1753 | } | |
1754 | ||
cb0e818f HC |
1755 | static int cgroup_good(Service *s) { |
1756 | int r; | |
1757 | ||
abaf5edd ZJS |
1758 | assert(s); |
1759 | ||
cb0e818f HC |
1760 | /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't |
1761 | * figure it out */ | |
1762 | ||
abaf5edd ZJS |
1763 | if (!UNIT(s)->cgroup_path) |
1764 | return 0; | |
1765 | ||
1766 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path); | |
117dcc57 | 1767 | if (r < 0) |
80876c20 LP |
1768 | return r; |
1769 | ||
b13ddbbc | 1770 | return r == 0; |
80876c20 LP |
1771 | } |
1772 | ||
ebc57b89 | 1773 | static bool service_shall_restart(Service *s, const char **reason) { |
a509f0e6 LP |
1774 | assert(s); |
1775 | ||
1776 | /* Don't restart after manual stops */ | |
ebc57b89 ZJS |
1777 | if (s->forbid_restart) { |
1778 | *reason = "manual stop"; | |
a509f0e6 | 1779 | return false; |
ebc57b89 | 1780 | } |
a509f0e6 LP |
1781 | |
1782 | /* Never restart if this is configured as special exception */ | |
ebc57b89 ZJS |
1783 | if (exit_status_set_test(&s->restart_prevent_status, s->main_exec_status.code, s->main_exec_status.status)) { |
1784 | *reason = "prevented by exit status"; | |
a509f0e6 | 1785 | return false; |
ebc57b89 | 1786 | } |
a509f0e6 LP |
1787 | |
1788 | /* Restart if the exit code/status are configured as restart triggers */ | |
ebc57b89 ZJS |
1789 | if (exit_status_set_test(&s->restart_force_status, s->main_exec_status.code, s->main_exec_status.status)) { |
1790 | *reason = "forced by exit status"; | |
a509f0e6 | 1791 | return true; |
ebc57b89 | 1792 | } |
a509f0e6 | 1793 | |
ebc57b89 | 1794 | *reason = "restart setting"; |
a509f0e6 LP |
1795 | switch (s->restart) { |
1796 | ||
1797 | case SERVICE_RESTART_NO: | |
1798 | return false; | |
1799 | ||
1800 | case SERVICE_RESTART_ALWAYS: | |
abb99360 | 1801 | return s->result != SERVICE_SKIP_CONDITION; |
a509f0e6 LP |
1802 | |
1803 | case SERVICE_RESTART_ON_SUCCESS: | |
1804 | return s->result == SERVICE_SUCCESS; | |
1805 | ||
1806 | case SERVICE_RESTART_ON_FAILURE: | |
bb924478 | 1807 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_SKIP_CONDITION); |
a509f0e6 LP |
1808 | |
1809 | case SERVICE_RESTART_ON_ABNORMAL: | |
bb924478 | 1810 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE, SERVICE_SKIP_CONDITION); |
a509f0e6 LP |
1811 | |
1812 | case SERVICE_RESTART_ON_WATCHDOG: | |
1813 | return s->result == SERVICE_FAILURE_WATCHDOG; | |
1814 | ||
1815 | case SERVICE_RESTART_ON_ABORT: | |
1816 | return IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP); | |
1817 | ||
1818 | default: | |
04499a70 | 1819 | assert_not_reached(); |
a509f0e6 LP |
1820 | } |
1821 | } | |
1822 | ||
deb4e708 MK |
1823 | static bool service_will_restart(Unit *u) { |
1824 | Service *s = SERVICE(u); | |
1825 | ||
53f47dfc YW |
1826 | assert(s); |
1827 | ||
deb4e708 MK |
1828 | if (s->will_auto_restart) |
1829 | return true; | |
53f47dfc YW |
1830 | if (s->state == SERVICE_AUTO_RESTART) |
1831 | return true; | |
2ad2e41a | 1832 | |
52a12341 | 1833 | return unit_will_restart_default(u); |
53f47dfc YW |
1834 | } |
1835 | ||
f42806df | 1836 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
31cd5f63 | 1837 | ServiceState end_state; |
034c6ed7 | 1838 | int r; |
0f52f8e5 | 1839 | |
034c6ed7 LP |
1840 | assert(s); |
1841 | ||
0f52f8e5 LP |
1842 | /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not |
1843 | * undo what has already been enqueued. */ | |
1844 | if (unit_stop_pending(UNIT(s))) | |
1845 | allow_restart = false; | |
1846 | ||
a0fef983 | 1847 | if (s->result == SERVICE_SUCCESS) |
f42806df | 1848 | s->result = f; |
034c6ed7 | 1849 | |
31cd5f63 AZ |
1850 | if (s->result == SERVICE_SUCCESS) { |
1851 | unit_log_success(UNIT(s)); | |
1852 | end_state = SERVICE_DEAD; | |
1853 | } else if (s->result == SERVICE_SKIP_CONDITION) { | |
1854 | unit_log_skip(UNIT(s), service_result_to_string(s->result)); | |
1855 | end_state = SERVICE_DEAD; | |
1856 | } else { | |
1857 | unit_log_failure(UNIT(s), service_result_to_string(s->result)); | |
1858 | end_state = SERVICE_FAILED; | |
1859 | } | |
4c425434 | 1860 | unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_stop); |
ed77d407 | 1861 | |
ebc57b89 ZJS |
1862 | if (!allow_restart) |
1863 | log_unit_debug(UNIT(s), "Service restart not allowed."); | |
1864 | else { | |
1865 | const char *reason; | |
1866 | bool shall_restart; | |
1867 | ||
1868 | shall_restart = service_shall_restart(s, &reason); | |
1869 | log_unit_debug(UNIT(s), "Service will %srestart (%s)", | |
1870 | shall_restart ? "" : "not ", | |
1871 | reason); | |
1872 | if (shall_restart) | |
1873 | s->will_auto_restart = true; | |
1874 | } | |
deb4e708 | 1875 | |
7eb2a8a1 LP |
1876 | /* Make sure service_release_resources() doesn't destroy our FD store, while we are changing through |
1877 | * SERVICE_FAILED/SERVICE_DEAD before entering into SERVICE_AUTO_RESTART. */ | |
1878 | s->n_keep_fd_store ++; | |
1879 | ||
31cd5f63 | 1880 | service_set_state(s, end_state); |
0c7f15b3 | 1881 | |
deb4e708 MK |
1882 | if (s->will_auto_restart) { |
1883 | s->will_auto_restart = false; | |
034c6ed7 | 1884 | |
36c16a7c | 1885 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec)); |
7eb2a8a1 LP |
1886 | if (r < 0) { |
1887 | s->n_keep_fd_store--; | |
034c6ed7 | 1888 | goto fail; |
7eb2a8a1 | 1889 | } |
034c6ed7 LP |
1890 | |
1891 | service_set_state(s, SERVICE_AUTO_RESTART); | |
7a0019d3 LP |
1892 | } else |
1893 | /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the | |
1894 | * user can still introspect the counter. Do so on the next start. */ | |
1895 | s->flush_n_restarts = true; | |
034c6ed7 | 1896 | |
5238e957 | 1897 | /* 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 |
1898 | * queue, so that the fd store is possibly gc'ed again */ |
1899 | s->n_keep_fd_store--; | |
1900 | unit_add_to_gc_queue(UNIT(s)); | |
1901 | ||
f2341e0a | 1902 | /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */ |
47342320 LP |
1903 | s->forbid_restart = false; |
1904 | ||
e66cf1a3 | 1905 | /* We want fresh tmpdirs in case service is started again immediately */ |
e8a565cb | 1906 | s->exec_runtime = exec_runtime_unref(s->exec_runtime, true); |
c17ec25e | 1907 | |
95939aed | 1908 | /* Also, remove the runtime directory */ |
bb0c0d6f | 1909 | unit_destroy_runtime_data(UNIT(s), &s->exec_context); |
e66cf1a3 | 1910 | |
00d9ef85 LP |
1911 | /* Get rid of the IPC bits of the user */ |
1912 | unit_unref_uid_gid(UNIT(s), true); | |
1913 | ||
29206d46 LP |
1914 | /* Release the user, and destroy it if we are the only remaining owner */ |
1915 | dynamic_creds_destroy(&s->dynamic_creds); | |
1916 | ||
9285c9ff LN |
1917 | /* Try to delete the pid file. At this point it will be |
1918 | * out-of-date, and some software might be confused by it, so | |
1919 | * let's remove it. */ | |
1920 | if (s->pid_file) | |
fabab190 | 1921 | (void) unlink(s->pid_file); |
9285c9ff | 1922 | |
6f765baf LP |
1923 | /* Reset TTY ownership if necessary */ |
1924 | exec_context_revert_tty(&s->exec_context); | |
1925 | ||
034c6ed7 LP |
1926 | return; |
1927 | ||
1928 | fail: | |
f2341e0a | 1929 | log_unit_warning_errno(UNIT(s), r, "Failed to run install restart timer: %m"); |
f42806df | 1930 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1931 | } |
1932 | ||
f42806df | 1933 | static void service_enter_stop_post(Service *s, ServiceResult f) { |
034c6ed7 LP |
1934 | int r; |
1935 | assert(s); | |
1936 | ||
a0fef983 | 1937 | if (s->result == SERVICE_SUCCESS) |
f42806df | 1938 | s->result = f; |
034c6ed7 | 1939 | |
5e94833f | 1940 | service_unwatch_control_pid(s); |
50be4f4a | 1941 | (void) unit_enqueue_rewatch_pids(UNIT(s)); |
5e94833f | 1942 | |
117dcc57 ZJS |
1943 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; |
1944 | if (s->control_command) { | |
867b3b7d LP |
1945 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
1946 | ||
ecedd90f LP |
1947 | r = service_spawn(s, |
1948 | s->control_command, | |
21b2ce39 | 1949 | s->timeout_stop_usec, |
78f93209 | 1950 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_IS_CONTROL|EXEC_SETENV_RESULT|EXEC_CONTROL_CGROUP, |
ecedd90f LP |
1951 | &s->control_pid); |
1952 | if (r < 0) | |
034c6ed7 LP |
1953 | goto fail; |
1954 | ||
80876c20 LP |
1955 | service_set_state(s, SERVICE_STOP_POST); |
1956 | } else | |
ac84d1fb | 1957 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1958 | |
1959 | return; | |
1960 | ||
1961 | fail: | |
f2341e0a | 1962 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m"); |
f42806df | 1963 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1964 | } |
1965 | ||
a232ebcc | 1966 | static int state_to_kill_operation(Service *s, ServiceState state) { |
4940c0b0 LP |
1967 | switch (state) { |
1968 | ||
c87700a1 | 1969 | case SERVICE_STOP_WATCHDOG: |
bf760801 | 1970 | case SERVICE_FINAL_WATCHDOG: |
c87700a1 | 1971 | return KILL_WATCHDOG; |
4940c0b0 LP |
1972 | |
1973 | case SERVICE_STOP_SIGTERM: | |
a232ebcc ZJS |
1974 | if (unit_has_job_type(UNIT(s), JOB_RESTART)) |
1975 | return KILL_RESTART; | |
1976 | _fallthrough_; | |
1977 | ||
4940c0b0 LP |
1978 | case SERVICE_FINAL_SIGTERM: |
1979 | return KILL_TERMINATE; | |
1980 | ||
1981 | case SERVICE_STOP_SIGKILL: | |
1982 | case SERVICE_FINAL_SIGKILL: | |
1983 | return KILL_KILL; | |
1984 | ||
1985 | default: | |
1986 | return _KILL_OPERATION_INVALID; | |
1987 | } | |
1988 | } | |
1989 | ||
f42806df | 1990 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
bf760801 | 1991 | int kill_operation, r; |
034c6ed7 LP |
1992 | |
1993 | assert(s); | |
1994 | ||
a0fef983 | 1995 | if (s->result == SERVICE_SUCCESS) |
f42806df | 1996 | s->result = f; |
034c6ed7 | 1997 | |
50be4f4a LP |
1998 | /* Before sending any signal, make sure we track all members of this cgroup */ |
1999 | (void) unit_watch_all_pids(UNIT(s)); | |
2000 | ||
2001 | /* Also, enqueue a job that we recheck all our PIDs a bit later, given that it's likely some processes have | |
2002 | * died now */ | |
2003 | (void) unit_enqueue_rewatch_pids(UNIT(s)); | |
a911bb9a | 2004 | |
bf760801 | 2005 | kill_operation = state_to_kill_operation(s, state); |
cd2086fe LP |
2006 | r = unit_kill_context( |
2007 | UNIT(s), | |
2008 | &s->kill_context, | |
bf760801 | 2009 | kill_operation, |
cd2086fe LP |
2010 | s->main_pid, |
2011 | s->control_pid, | |
2012 | s->main_pid_alien); | |
2013 | if (r < 0) | |
2014 | goto fail; | |
034c6ed7 | 2015 | |
cd2086fe | 2016 | if (r > 0) { |
dc653bf4 | 2017 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), |
bf760801 | 2018 | kill_operation == KILL_WATCHDOG ? service_timeout_abort_usec(s) : s->timeout_stop_usec)); |
36c16a7c LP |
2019 | if (r < 0) |
2020 | goto fail; | |
d6ea93e3 | 2021 | |
80876c20 | 2022 | service_set_state(s, state); |
c87700a1 | 2023 | } else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM) && s->kill_context.send_sigkill) |
ac84d1fb | 2024 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); |
c87700a1 | 2025 | else if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 2026 | service_enter_stop_post(s, SERVICE_SUCCESS); |
bf760801 | 2027 | else if (IN_SET(state, SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM) && s->kill_context.send_sigkill) |
ac84d1fb | 2028 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); |
80876c20 | 2029 | else |
f42806df | 2030 | service_enter_dead(s, SERVICE_SUCCESS, true); |
034c6ed7 LP |
2031 | |
2032 | return; | |
2033 | ||
2034 | fail: | |
f2341e0a | 2035 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
034c6ed7 | 2036 | |
c87700a1 | 2037 | if (IN_SET(state, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 2038 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 2039 | else |
f42806df | 2040 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
2041 | } |
2042 | ||
308d72dc LP |
2043 | static void service_enter_stop_by_notify(Service *s) { |
2044 | assert(s); | |
2045 | ||
50be4f4a | 2046 | (void) unit_enqueue_rewatch_pids(UNIT(s)); |
308d72dc | 2047 | |
36c16a7c | 2048 | service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec)); |
308d72dc | 2049 | |
6041a7ee MS |
2050 | /* The service told us it's stopping, so it's as if we SIGTERM'd it. */ |
2051 | service_set_state(s, SERVICE_STOP_SIGTERM); | |
308d72dc LP |
2052 | } |
2053 | ||
f42806df | 2054 | static void service_enter_stop(Service *s, ServiceResult f) { |
034c6ed7 | 2055 | int r; |
5925dd3c | 2056 | |
034c6ed7 LP |
2057 | assert(s); |
2058 | ||
a0fef983 | 2059 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2060 | s->result = f; |
034c6ed7 | 2061 | |
5e94833f | 2062 | service_unwatch_control_pid(s); |
50be4f4a | 2063 | (void) unit_enqueue_rewatch_pids(UNIT(s)); |
5e94833f | 2064 | |
117dcc57 ZJS |
2065 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; |
2066 | if (s->control_command) { | |
867b3b7d LP |
2067 | s->control_command_id = SERVICE_EXEC_STOP; |
2068 | ||
ecedd90f LP |
2069 | r = service_spawn(s, |
2070 | s->control_command, | |
21b2ce39 | 2071 | s->timeout_stop_usec, |
78f93209 | 2072 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_SETENV_RESULT|EXEC_CONTROL_CGROUP, |
ecedd90f LP |
2073 | &s->control_pid); |
2074 | if (r < 0) | |
034c6ed7 LP |
2075 | goto fail; |
2076 | ||
80876c20 LP |
2077 | service_set_state(s, SERVICE_STOP); |
2078 | } else | |
f42806df | 2079 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
2080 | |
2081 | return; | |
2082 | ||
2083 | fail: | |
f2341e0a | 2084 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop' task: %m"); |
f42806df | 2085 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
2086 | } |
2087 | ||
957c3cf9 LP |
2088 | static bool service_good(Service *s) { |
2089 | int main_pid_ok; | |
2090 | assert(s); | |
2091 | ||
2092 | if (s->type == SERVICE_DBUS && !s->bus_name_good) | |
2093 | return false; | |
2094 | ||
2095 | main_pid_ok = main_pid_good(s); | |
2096 | if (main_pid_ok > 0) /* It's alive */ | |
2097 | return true; | |
2098 | if (main_pid_ok == 0) /* It's dead */ | |
2099 | return false; | |
2100 | ||
2101 | /* OK, we don't know anything about the main PID, maybe | |
2102 | * because there is none. Let's check the control group | |
2103 | * instead. */ | |
2104 | ||
2105 | return cgroup_good(s) != 0; | |
2106 | } | |
2107 | ||
f42806df | 2108 | static void service_enter_running(Service *s, ServiceResult f) { |
80876c20 LP |
2109 | assert(s); |
2110 | ||
a0fef983 | 2111 | if (s->result == SERVICE_SUCCESS) |
f42806df | 2112 | s->result = f; |
80876c20 | 2113 | |
089b64d5 LP |
2114 | service_unwatch_control_pid(s); |
2115 | ||
ec5b1452 LP |
2116 | if (s->result != SERVICE_SUCCESS) |
2117 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
2118 | else if (service_good(s)) { | |
308d72dc | 2119 | |
ec5b1452 | 2120 | /* If there are any queued up sd_notify() notifications, process them now */ |
308d72dc LP |
2121 | if (s->notify_state == NOTIFY_RELOADING) |
2122 | service_enter_reload_by_notify(s); | |
2123 | else if (s->notify_state == NOTIFY_STOPPING) | |
2124 | service_enter_stop_by_notify(s); | |
36c16a7c | 2125 | else { |
308d72dc | 2126 | service_set_state(s, SERVICE_RUNNING); |
5918a933 | 2127 | service_arm_timer(s, service_running_timeout(s)); |
36c16a7c | 2128 | } |
308d72dc | 2129 | |
ec5b1452 | 2130 | } else if (s->remain_after_exit) |
80876c20 LP |
2131 | service_set_state(s, SERVICE_EXITED); |
2132 | else | |
f42806df | 2133 | service_enter_stop(s, SERVICE_SUCCESS); |
80876c20 LP |
2134 | } |
2135 | ||
034c6ed7 LP |
2136 | static void service_enter_start_post(Service *s) { |
2137 | int r; | |
2138 | assert(s); | |
2139 | ||
5e94833f | 2140 | service_unwatch_control_pid(s); |
842129f5 | 2141 | service_reset_watchdog(s); |
bb242b7b | 2142 | |
117dcc57 ZJS |
2143 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; |
2144 | if (s->control_command) { | |
867b3b7d LP |
2145 | s->control_command_id = SERVICE_EXEC_START_POST; |
2146 | ||
ecedd90f LP |
2147 | r = service_spawn(s, |
2148 | s->control_command, | |
21b2ce39 | 2149 | s->timeout_start_usec, |
78f93209 | 2150 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_CONTROL_CGROUP, |
ecedd90f LP |
2151 | &s->control_pid); |
2152 | if (r < 0) | |
034c6ed7 LP |
2153 | goto fail; |
2154 | ||
80876c20 LP |
2155 | service_set_state(s, SERVICE_START_POST); |
2156 | } else | |
f42806df | 2157 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
2158 | |
2159 | return; | |
2160 | ||
2161 | fail: | |
f2341e0a | 2162 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m"); |
f42806df | 2163 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
2164 | } |
2165 | ||
e9a4f676 | 2166 | static void service_kill_control_process(Service *s) { |
a6951a50 | 2167 | int r; |
4ad49000 | 2168 | |
a6951a50 LP |
2169 | assert(s); |
2170 | ||
e9a4f676 LP |
2171 | if (s->control_pid <= 0) |
2172 | return; | |
4ad49000 | 2173 | |
e9a4f676 LP |
2174 | r = kill_and_sigcont(s->control_pid, SIGKILL); |
2175 | if (r < 0) { | |
2176 | _cleanup_free_ char *comm = NULL; | |
a6951a50 | 2177 | |
e9a4f676 | 2178 | (void) get_process_comm(s->control_pid, &comm); |
a6951a50 | 2179 | |
e9a4f676 LP |
2180 | log_unit_debug_errno(UNIT(s), r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", |
2181 | s->control_pid, strna(comm)); | |
a6951a50 | 2182 | } |
4ad49000 LP |
2183 | } |
2184 | ||
c53d2d54 DB |
2185 | static int service_adverse_to_leftover_processes(Service *s) { |
2186 | assert(s); | |
2187 | ||
2188 | /* KillMode=mixed and control group are used to indicate that all process should be killed off. | |
4c425434 LP |
2189 | * SendSIGKILL= is used for services that require a clean shutdown. These are typically database |
2190 | * service where a SigKilled process would result in a lengthy recovery and who's shutdown or startup | |
2191 | * time is quite variable (so Timeout settings aren't of use). | |
c53d2d54 DB |
2192 | * |
2193 | * Here we take these two factors and refuse to start a service if there are existing processes | |
2194 | * within a control group. Databases, while generally having some protection against multiple | |
4c425434 | 2195 | * instances running, lets not stress the rigor of these. Also ExecStartPre= parts of the service |
c53d2d54 | 2196 | * aren't as rigoriously written to protect aganst against multiple use. */ |
4c425434 LP |
2197 | |
2198 | if (unit_warn_leftover_processes(UNIT(s), unit_log_leftover_process_start) > 0 && | |
c53d2d54 | 2199 | IN_SET(s->kill_context.kill_mode, KILL_MIXED, KILL_CONTROL_GROUP) && |
569554d9 ZJS |
2200 | !s->kill_context.send_sigkill) |
2201 | return log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(EBUSY), | |
2202 | "Will not start SendSIGKILL=no service of type KillMode=control-group or mixed while processes exist"); | |
2203 | ||
c53d2d54 DB |
2204 | return 0; |
2205 | } | |
2206 | ||
034c6ed7 | 2207 | static void service_enter_start(Service *s) { |
4ad49000 | 2208 | ExecCommand *c; |
36c16a7c | 2209 | usec_t timeout; |
034c6ed7 LP |
2210 | pid_t pid; |
2211 | int r; | |
2212 | ||
2213 | assert(s); | |
2214 | ||
41efeaec LP |
2215 | service_unwatch_control_pid(s); |
2216 | service_unwatch_main_pid(s); | |
80876c20 | 2217 | |
c53d2d54 DB |
2218 | r = service_adverse_to_leftover_processes(s); |
2219 | if (r < 0) | |
2220 | goto fail; | |
a4634b21 | 2221 | |
867b3b7d LP |
2222 | if (s->type == SERVICE_FORKING) { |
2223 | s->control_command_id = SERVICE_EXEC_START; | |
2224 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
2225 | ||
2226 | s->main_command = NULL; | |
2227 | } else { | |
2228 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2229 | s->control_command = NULL; | |
2230 | ||
2231 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
2232 | } | |
34e9ba66 | 2233 | |
96fb8242 | 2234 | if (!c) { |
47fffb35 | 2235 | if (s->type != SERVICE_ONESHOT) { |
cbc056c8 ZJS |
2236 | /* There's no command line configured for the main command? Hmm, that is strange. |
2237 | * This can only happen if the configuration changes at runtime. In this case, | |
2238 | * let's enter a failure state. */ | |
d85ff944 | 2239 | r = log_unit_error_errno(UNIT(s), SYNTHETIC_ERRNO(ENXIO), "There's no 'start' task anymore we could start."); |
47fffb35 LP |
2240 | goto fail; |
2241 | } | |
2242 | ||
ef5ae8e7 ZJS |
2243 | /* We force a fake state transition here. Otherwise, the unit would go directly from |
2244 | * SERVICE_DEAD to SERVICE_DEAD without SERVICE_ACTIVATING or SERVICE_ACTIVE | |
5238e957 | 2245 | * in between. This way we can later trigger actions that depend on the state |
ef5ae8e7 ZJS |
2246 | * transition, including SuccessAction=. */ |
2247 | service_set_state(s, SERVICE_START); | |
2248 | ||
96fb8242 LP |
2249 | service_enter_start_post(s); |
2250 | return; | |
2251 | } | |
2252 | ||
36c16a7c LP |
2253 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) |
2254 | /* For simple + idle this is the main process. We don't apply any timeout here, but | |
2255 | * service_enter_running() will later apply the .runtime_max_usec timeout. */ | |
2256 | timeout = USEC_INFINITY; | |
2257 | else | |
2258 | timeout = s->timeout_start_usec; | |
2259 | ||
ecedd90f LP |
2260 | r = service_spawn(s, |
2261 | c, | |
36c16a7c | 2262 | timeout, |
cdebedb4 | 2263 | EXEC_PASS_FDS|EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_SET_WATCHDOG|EXEC_WRITE_CREDENTIALS|EXEC_SETENV_MONITOR_RESULT, |
ecedd90f LP |
2264 | &pid); |
2265 | if (r < 0) | |
034c6ed7 LP |
2266 | goto fail; |
2267 | ||
36c16a7c | 2268 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) { |
034c6ed7 LP |
2269 | /* For simple services we immediately start |
2270 | * the START_POST binaries. */ | |
2271 | ||
5925dd3c | 2272 | service_set_main_pid(s, pid); |
034c6ed7 LP |
2273 | service_enter_start_post(s); |
2274 | ||
2275 | } else if (s->type == SERVICE_FORKING) { | |
2276 | ||
2277 | /* For forking services we wait until the start | |
2278 | * process exited. */ | |
2279 | ||
e55224ca | 2280 | s->control_pid = pid; |
80876c20 LP |
2281 | service_set_state(s, SERVICE_START); |
2282 | ||
5686391b | 2283 | } else if (IN_SET(s->type, SERVICE_ONESHOT, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_EXEC)) { |
7d55e835 | 2284 | |
5686391b | 2285 | /* For oneshot services we wait until the start process exited, too, but it is our main process. */ |
7d55e835 | 2286 | |
5686391b LP |
2287 | /* For D-Bus services we know the main pid right away, but wait for the bus name to appear on the |
2288 | * bus. 'notify' and 'exec' services are similar. */ | |
05e343b7 | 2289 | |
5925dd3c | 2290 | service_set_main_pid(s, pid); |
80876c20 | 2291 | service_set_state(s, SERVICE_START); |
034c6ed7 | 2292 | } else |
04499a70 | 2293 | assert_not_reached(); |
034c6ed7 LP |
2294 | |
2295 | return; | |
2296 | ||
2297 | fail: | |
f2341e0a | 2298 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start' task: %m"); |
c3fda31d | 2299 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
2300 | } |
2301 | ||
2302 | static void service_enter_start_pre(Service *s) { | |
2303 | int r; | |
2304 | ||
2305 | assert(s); | |
2306 | ||
5e94833f LP |
2307 | service_unwatch_control_pid(s); |
2308 | ||
117dcc57 ZJS |
2309 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; |
2310 | if (s->control_command) { | |
8f53a7b8 | 2311 | |
c53d2d54 DB |
2312 | r = service_adverse_to_leftover_processes(s); |
2313 | if (r < 0) | |
2314 | goto fail; | |
a4634b21 | 2315 | |
867b3b7d LP |
2316 | s->control_command_id = SERVICE_EXEC_START_PRE; |
2317 | ||
ecedd90f LP |
2318 | r = service_spawn(s, |
2319 | s->control_command, | |
21b2ce39 | 2320 | s->timeout_start_usec, |
cdebedb4 | 2321 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_APPLY_TTY_STDIN|EXEC_SETENV_MONITOR_RESULT, |
ecedd90f LP |
2322 | &s->control_pid); |
2323 | if (r < 0) | |
034c6ed7 LP |
2324 | goto fail; |
2325 | ||
80876c20 LP |
2326 | service_set_state(s, SERVICE_START_PRE); |
2327 | } else | |
034c6ed7 LP |
2328 | service_enter_start(s); |
2329 | ||
2330 | return; | |
2331 | ||
2332 | fail: | |
f2341e0a | 2333 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m"); |
f42806df | 2334 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
2335 | } |
2336 | ||
31cd5f63 AZ |
2337 | static void service_enter_condition(Service *s) { |
2338 | int r; | |
2339 | ||
2340 | assert(s); | |
2341 | ||
2342 | service_unwatch_control_pid(s); | |
2343 | ||
2344 | s->control_command = s->exec_command[SERVICE_EXEC_CONDITION]; | |
2345 | if (s->control_command) { | |
2346 | ||
2347 | r = service_adverse_to_leftover_processes(s); | |
2348 | if (r < 0) | |
2349 | goto fail; | |
2350 | ||
2351 | s->control_command_id = SERVICE_EXEC_CONDITION; | |
2352 | ||
2353 | r = service_spawn(s, | |
2354 | s->control_command, | |
2355 | s->timeout_start_usec, | |
2356 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_APPLY_TTY_STDIN, | |
2357 | &s->control_pid); | |
2358 | ||
2359 | if (r < 0) | |
2360 | goto fail; | |
2361 | ||
2362 | service_set_state(s, SERVICE_CONDITION); | |
2363 | } else | |
2364 | service_enter_start_pre(s); | |
2365 | ||
2366 | return; | |
2367 | ||
2368 | fail: | |
2369 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'exec-condition' task: %m"); | |
2370 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); | |
2371 | } | |
2372 | ||
034c6ed7 | 2373 | static void service_enter_restart(Service *s) { |
4afd3348 | 2374 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 | 2375 | int r; |
398ef8ba | 2376 | |
034c6ed7 LP |
2377 | assert(s); |
2378 | ||
28a2dfe8 | 2379 | if (unit_has_job_type(UNIT(s), JOB_STOP)) { |
a8bb2e65 | 2380 | /* Don't restart things if we are going down anyway */ |
f2341e0a | 2381 | log_unit_info(UNIT(s), "Stop job pending for unit, delaying automatic restart."); |
2edfa366 | 2382 | |
36c16a7c | 2383 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec)); |
a8bb2e65 | 2384 | if (r < 0) |
2edfa366 | 2385 | goto fail; |
feae8adb DW |
2386 | |
2387 | return; | |
2edfa366 LP |
2388 | } |
2389 | ||
48bb5876 DW |
2390 | /* Any units that are bound to this service must also be |
2391 | * restarted. We use JOB_RESTART (instead of the more obvious | |
2392 | * JOB_START) here so that those dependency jobs will be added | |
2393 | * as well. */ | |
50cbaba4 | 2394 | r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_REPLACE, NULL, &error, NULL); |
48bb5876 | 2395 | if (r < 0) |
034c6ed7 LP |
2396 | goto fail; |
2397 | ||
7a0019d3 | 2398 | /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't fully |
f0c7e594 | 2399 | * stopped, i.e. as long as it remains up or remains in auto-start states. The user can reset the counter |
7a0019d3 LP |
2400 | * explicitly however via the usual "systemctl reset-failure" logic. */ |
2401 | s->n_restarts ++; | |
2402 | s->flush_n_restarts = false; | |
2403 | ||
c2503e35 RH |
2404 | log_unit_struct(UNIT(s), LOG_INFO, |
2405 | "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR, | |
2406 | LOG_UNIT_INVOCATION_ID(UNIT(s)), | |
2407 | LOG_UNIT_MESSAGE(UNIT(s), "Scheduled restart job, restart counter is at %u.", s->n_restarts), | |
2408 | "N_RESTARTS=%u", s->n_restarts); | |
7a0019d3 LP |
2409 | |
2410 | /* Notify clients about changed restart counter */ | |
2411 | unit_add_to_dbus_queue(UNIT(s)); | |
2412 | ||
a8bb2e65 LP |
2413 | /* Note that we stay in the SERVICE_AUTO_RESTART state here, |
2414 | * it will be canceled as part of the service_stop() call that | |
2415 | * is executed as part of JOB_RESTART. */ | |
2416 | ||
034c6ed7 LP |
2417 | return; |
2418 | ||
2419 | fail: | |
2a03b9ed | 2420 | log_unit_warning(UNIT(s), "Failed to schedule restart job: %s", bus_error_message(&error, r)); |
f42806df | 2421 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
2422 | } |
2423 | ||
308d72dc | 2424 | static void service_enter_reload_by_notify(Service *s) { |
15d167f8 JW |
2425 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2426 | int r; | |
2427 | ||
308d72dc LP |
2428 | assert(s); |
2429 | ||
36c16a7c | 2430 | service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_start_usec)); |
308d72dc | 2431 | service_set_state(s, SERVICE_RELOAD); |
15d167f8 JW |
2432 | |
2433 | /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */ | |
2434 | r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); | |
2435 | if (r < 0) | |
2a03b9ed | 2436 | log_unit_warning(UNIT(s), "Failed to schedule propagation of reload: %s", bus_error_message(&error, r)); |
308d72dc LP |
2437 | } |
2438 | ||
034c6ed7 LP |
2439 | static void service_enter_reload(Service *s) { |
2440 | int r; | |
2441 | ||
2442 | assert(s); | |
2443 | ||
5e94833f | 2444 | service_unwatch_control_pid(s); |
95c906ae | 2445 | s->reload_result = SERVICE_SUCCESS; |
5e94833f | 2446 | |
117dcc57 ZJS |
2447 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; |
2448 | if (s->control_command) { | |
867b3b7d LP |
2449 | s->control_command_id = SERVICE_EXEC_RELOAD; |
2450 | ||
ecedd90f LP |
2451 | r = service_spawn(s, |
2452 | s->control_command, | |
21b2ce39 | 2453 | s->timeout_start_usec, |
78f93209 | 2454 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_CONTROL_CGROUP, |
ecedd90f LP |
2455 | &s->control_pid); |
2456 | if (r < 0) | |
034c6ed7 LP |
2457 | goto fail; |
2458 | ||
80876c20 LP |
2459 | service_set_state(s, SERVICE_RELOAD); |
2460 | } else | |
f42806df | 2461 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
2462 | |
2463 | return; | |
2464 | ||
2465 | fail: | |
f2341e0a | 2466 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'reload' task: %m"); |
f42806df LP |
2467 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
2468 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
2469 | } |
2470 | ||
f42806df | 2471 | static void service_run_next_control(Service *s) { |
36c16a7c | 2472 | usec_t timeout; |
034c6ed7 LP |
2473 | int r; |
2474 | ||
2475 | assert(s); | |
2476 | assert(s->control_command); | |
2477 | assert(s->control_command->command_next); | |
2478 | ||
34e9ba66 | 2479 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 2480 | |
34e9ba66 | 2481 | s->control_command = s->control_command->command_next; |
5e94833f LP |
2482 | service_unwatch_control_pid(s); |
2483 | ||
31cd5f63 | 2484 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
36c16a7c LP |
2485 | timeout = s->timeout_start_usec; |
2486 | else | |
2487 | timeout = s->timeout_stop_usec; | |
2488 | ||
ecedd90f LP |
2489 | r = service_spawn(s, |
2490 | s->control_command, | |
36c16a7c | 2491 | timeout, |
1703fa41 | 2492 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL| |
31cd5f63 | 2493 | (IN_SET(s->control_command_id, SERVICE_EXEC_CONDITION, SERVICE_EXEC_START_PRE, SERVICE_EXEC_STOP_POST) ? EXEC_APPLY_TTY_STDIN : 0)| |
78f93209 LP |
2494 | (IN_SET(s->control_command_id, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST) ? EXEC_SETENV_RESULT : 0)| |
2495 | (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 |
2496 | &s->control_pid); |
2497 | if (r < 0) | |
034c6ed7 LP |
2498 | goto fail; |
2499 | ||
2500 | return; | |
2501 | ||
2502 | fail: | |
f2341e0a | 2503 | log_unit_warning_errno(UNIT(s), r, "Failed to run next control task: %m"); |
034c6ed7 | 2504 | |
31cd5f63 | 2505 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START_POST, SERVICE_STOP)) |
f42806df | 2506 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 2507 | else if (s->state == SERVICE_STOP_POST) |
f42806df | 2508 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
e2f3b44c | 2509 | else if (s->state == SERVICE_RELOAD) { |
f42806df LP |
2510 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
2511 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c | 2512 | } else |
f42806df | 2513 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
5cb5a6ff LP |
2514 | } |
2515 | ||
f42806df | 2516 | static void service_run_next_main(Service *s) { |
34e9ba66 LP |
2517 | pid_t pid; |
2518 | int r; | |
2519 | ||
2520 | assert(s); | |
867b3b7d LP |
2521 | assert(s->main_command); |
2522 | assert(s->main_command->command_next); | |
2523 | assert(s->type == SERVICE_ONESHOT); | |
34e9ba66 | 2524 | |
867b3b7d | 2525 | s->main_command = s->main_command->command_next; |
34e9ba66 LP |
2526 | service_unwatch_main_pid(s); |
2527 | ||
ecedd90f LP |
2528 | r = service_spawn(s, |
2529 | s->main_command, | |
21b2ce39 | 2530 | s->timeout_start_usec, |
1703fa41 | 2531 | EXEC_PASS_FDS|EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_SET_WATCHDOG, |
ecedd90f LP |
2532 | &pid); |
2533 | if (r < 0) | |
34e9ba66 LP |
2534 | goto fail; |
2535 | ||
2536 | service_set_main_pid(s, pid); | |
2537 | ||
2538 | return; | |
2539 | ||
2540 | fail: | |
f2341e0a | 2541 | log_unit_warning_errno(UNIT(s), r, "Failed to run next main task: %m"); |
f42806df | 2542 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
34e9ba66 LP |
2543 | } |
2544 | ||
87f0e418 LP |
2545 | static int service_start(Unit *u) { |
2546 | Service *s = SERVICE(u); | |
07299350 | 2547 | int r; |
5cb5a6ff LP |
2548 | |
2549 | assert(s); | |
2550 | ||
034c6ed7 LP |
2551 | /* We cannot fulfill this request right now, try again later |
2552 | * please! */ | |
a00973af | 2553 | if (IN_SET(s->state, |
c87700a1 | 2554 | SERVICE_STOP, SERVICE_STOP_WATCHDOG, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 2555 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, SERVICE_CLEANING)) |
5cb5a6ff LP |
2556 | return -EAGAIN; |
2557 | ||
034c6ed7 | 2558 | /* Already on it! */ |
31cd5f63 | 2559 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST)) |
034c6ed7 LP |
2560 | return 0; |
2561 | ||
2e9d6c12 | 2562 | /* A service that will be restarted must be stopped first to |
7f2cddae | 2563 | * trigger BindsTo and/or OnFailure dependencies. If a user |
2e9d6c12 | 2564 | * does not want to wait for the holdoff time to elapse, the |
d4943dc7 LP |
2565 | * service should be manually restarted, not started. We |
2566 | * simply return EAGAIN here, so that any start jobs stay | |
2567 | * queued, and assume that the auto restart timer will | |
2568 | * eventually trigger the restart. */ | |
2569 | if (s->state == SERVICE_AUTO_RESTART) | |
a8bb2e65 | 2570 | return -EAGAIN; |
2e9d6c12 | 2571 | |
a00973af | 2572 | assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED)); |
5cb5a6ff | 2573 | |
4b58153d LP |
2574 | r = unit_acquire_invocation_id(u); |
2575 | if (r < 0) | |
2576 | return r; | |
2577 | ||
f42806df LP |
2578 | s->result = SERVICE_SUCCESS; |
2579 | s->reload_result = SERVICE_SUCCESS; | |
034c6ed7 | 2580 | s->main_pid_known = false; |
6dfa5494 | 2581 | s->main_pid_alien = false; |
47342320 | 2582 | s->forbid_restart = false; |
3c7416b6 | 2583 | |
a1e58e8e | 2584 | s->status_text = mfree(s->status_text); |
8cfdb077 LP |
2585 | s->status_errno = 0; |
2586 | ||
308d72dc LP |
2587 | s->notify_state = NOTIFY_UNKNOWN; |
2588 | ||
aa8c4bbf | 2589 | s->watchdog_original_usec = s->watchdog_usec; |
2787d83c | 2590 | s->watchdog_override_enable = false; |
aa8c4bbf | 2591 | s->watchdog_override_usec = USEC_INFINITY; |
2787d83c | 2592 | |
6a1d4d9f LP |
2593 | exec_command_reset_status_list_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
2594 | exec_status_reset(&s->main_exec_status); | |
2595 | ||
7a0019d3 LP |
2596 | /* This is not an automatic restart? Flush the restart counter then */ |
2597 | if (s->flush_n_restarts) { | |
2598 | s->n_restarts = 0; | |
2599 | s->flush_n_restarts = false; | |
2600 | } | |
2601 | ||
6a1d4d9f LP |
2602 | u->reset_accounting = true; |
2603 | ||
31cd5f63 | 2604 | service_enter_condition(s); |
82a2b6bb | 2605 | return 1; |
5cb5a6ff LP |
2606 | } |
2607 | ||
87f0e418 LP |
2608 | static int service_stop(Unit *u) { |
2609 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
2610 | |
2611 | assert(s); | |
2612 | ||
a509f0e6 | 2613 | /* Don't create restart jobs from manual stops. */ |
47342320 | 2614 | s->forbid_restart = true; |
034c6ed7 | 2615 | |
e537352b | 2616 | /* Already on it */ |
a00973af | 2617 | if (IN_SET(s->state, |
dc653bf4 | 2618 | SERVICE_STOP, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf760801 | 2619 | SERVICE_FINAL_WATCHDOG, SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) |
e537352b LP |
2620 | return 0; |
2621 | ||
f0c7b229 | 2622 | /* A restart will be scheduled or is in progress. */ |
034c6ed7 | 2623 | if (s->state == SERVICE_AUTO_RESTART) { |
0c7f15b3 | 2624 | service_set_state(s, SERVICE_DEAD); |
034c6ed7 LP |
2625 | return 0; |
2626 | } | |
2627 | ||
3f6c78dc LP |
2628 | /* If there's already something running we go directly into |
2629 | * kill mode. */ | |
31cd5f63 | 2630 | if (IN_SET(s->state, SERVICE_CONDITION, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD, SERVICE_STOP_WATCHDOG)) { |
f42806df | 2631 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
3f6c78dc LP |
2632 | return 0; |
2633 | } | |
5cb5a6ff | 2634 | |
4c2f5842 LP |
2635 | /* If we are currently cleaning, then abort it, brutally. */ |
2636 | if (s->state == SERVICE_CLEANING) { | |
2637 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); | |
2638 | return 0; | |
2639 | } | |
2640 | ||
a00973af | 2641 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
3a762661 | 2642 | |
f42806df | 2643 | service_enter_stop(s, SERVICE_SUCCESS); |
82a2b6bb | 2644 | return 1; |
5cb5a6ff LP |
2645 | } |
2646 | ||
87f0e418 LP |
2647 | static int service_reload(Unit *u) { |
2648 | Service *s = SERVICE(u); | |
034c6ed7 LP |
2649 | |
2650 | assert(s); | |
2651 | ||
3742095b | 2652 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
034c6ed7 LP |
2653 | |
2654 | service_enter_reload(s); | |
2d018ae2 | 2655 | return 1; |
5cb5a6ff LP |
2656 | } |
2657 | ||
44a6b1b6 | 2658 | _pure_ static bool service_can_reload(Unit *u) { |
87f0e418 | 2659 | Service *s = SERVICE(u); |
034c6ed7 LP |
2660 | |
2661 | assert(s); | |
2662 | ||
2663 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
2664 | } | |
2665 | ||
e266c068 MS |
2666 | static unsigned service_exec_command_index(Unit *u, ServiceExecCommand id, ExecCommand *current) { |
2667 | Service *s = SERVICE(u); | |
2668 | unsigned idx = 0; | |
2669 | ExecCommand *first, *c; | |
2670 | ||
2671 | assert(s); | |
5b99bd5f LP |
2672 | assert(id >= 0); |
2673 | assert(id < _SERVICE_EXEC_COMMAND_MAX); | |
e266c068 MS |
2674 | |
2675 | first = s->exec_command[id]; | |
2676 | ||
2677 | /* Figure out where we are in the list by walking back to the beginning */ | |
2678 | for (c = current; c != first; c = c->command_prev) | |
2679 | idx++; | |
2680 | ||
2681 | return idx; | |
2682 | } | |
2683 | ||
2684 | static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command) { | |
d68c645b | 2685 | _cleanup_free_ char *args = NULL, *p = NULL; |
e266c068 | 2686 | Service *s = SERVICE(u); |
d68c645b | 2687 | const char *type, *key; |
e266c068 | 2688 | ServiceExecCommand id; |
319a4f4b | 2689 | size_t length = 0; |
e266c068 | 2690 | unsigned idx; |
e266c068 | 2691 | char **arg; |
e266c068 MS |
2692 | |
2693 | assert(s); | |
2694 | assert(f); | |
2695 | ||
2696 | if (!command) | |
2697 | return 0; | |
2698 | ||
2699 | if (command == s->control_command) { | |
2700 | type = "control"; | |
2701 | id = s->control_command_id; | |
2702 | } else { | |
2703 | type = "main"; | |
2704 | id = SERVICE_EXEC_START; | |
2705 | } | |
2706 | ||
2707 | idx = service_exec_command_index(u, id, command); | |
2708 | ||
2709 | STRV_FOREACH(arg, command->argv) { | |
e266c068 | 2710 | _cleanup_free_ char *e = NULL; |
d68c645b | 2711 | size_t n; |
e266c068 | 2712 | |
d68c645b | 2713 | e = cescape(*arg); |
e266c068 | 2714 | if (!e) |
d68c645b | 2715 | return log_oom(); |
e266c068 MS |
2716 | |
2717 | n = strlen(e); | |
319a4f4b | 2718 | if (!GREEDY_REALLOC(args, length + 2 + n + 2)) |
d68c645b | 2719 | return log_oom(); |
e266c068 MS |
2720 | |
2721 | if (length > 0) | |
2722 | args[length++] = ' '; | |
2723 | ||
334c0979 | 2724 | args[length++] = '"'; |
e266c068 MS |
2725 | memcpy(args + length, e, n); |
2726 | length += n; | |
334c0979 | 2727 | args[length++] = '"'; |
e266c068 MS |
2728 | } |
2729 | ||
319a4f4b | 2730 | if (!GREEDY_REALLOC(args, length + 1)) |
d68c645b LP |
2731 | return log_oom(); |
2732 | ||
e266c068 MS |
2733 | args[length++] = 0; |
2734 | ||
d68c645b | 2735 | p = cescape(command->path); |
e266c068 | 2736 | if (!p) |
5b99bd5f | 2737 | return log_oom(); |
e266c068 | 2738 | |
d68c645b | 2739 | key = strjoina(type, "-command"); |
5b99bd5f LP |
2740 | (void) serialize_item_format(f, key, "%s %u %s %s", service_exec_command_to_string(id), idx, p, args); |
2741 | ||
2742 | return 0; | |
e266c068 MS |
2743 | } |
2744 | ||
a16e1123 LP |
2745 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
2746 | Service *s = SERVICE(u); | |
2339fc93 | 2747 | ServiceFDStore *fs; |
a34ceba6 | 2748 | int r; |
a16e1123 LP |
2749 | |
2750 | assert(u); | |
2751 | assert(f); | |
2752 | assert(fds); | |
2753 | ||
d68c645b LP |
2754 | (void) serialize_item(f, "state", service_state_to_string(s->state)); |
2755 | (void) serialize_item(f, "result", service_result_to_string(s->result)); | |
2756 | (void) serialize_item(f, "reload-result", service_result_to_string(s->reload_result)); | |
a16e1123 LP |
2757 | |
2758 | if (s->control_pid > 0) | |
d68c645b | 2759 | (void) serialize_item_format(f, "control-pid", PID_FMT, s->control_pid); |
a16e1123 | 2760 | |
5925dd3c | 2761 | if (s->main_pid_known && s->main_pid > 0) |
d68c645b | 2762 | (void) serialize_item_format(f, "main-pid", PID_FMT, s->main_pid); |
a16e1123 | 2763 | |
d68c645b LP |
2764 | (void) serialize_bool(f, "main-pid-known", s->main_pid_known); |
2765 | (void) serialize_bool(f, "bus-name-good", s->bus_name_good); | |
2766 | (void) serialize_bool(f, "bus-name-owner", s->bus_name_owner); | |
a16e1123 | 2767 | |
d68c645b LP |
2768 | (void) serialize_item_format(f, "n-restarts", "%u", s->n_restarts); |
2769 | (void) serialize_bool(f, "flush-n-restarts", s->flush_n_restarts); | |
7a0019d3 | 2770 | |
d68c645b | 2771 | r = serialize_item_escaped(f, "status-text", s->status_text); |
a34ceba6 LP |
2772 | if (r < 0) |
2773 | return r; | |
3a2776bc | 2774 | |
e266c068 MS |
2775 | service_serialize_exec_command(u, f, s->control_command); |
2776 | service_serialize_exec_command(u, f, s->main_command); | |
a16e1123 | 2777 | |
d68c645b | 2778 | r = serialize_fd(f, fds, "stdin-fd", s->stdin_fd); |
a34ceba6 LP |
2779 | if (r < 0) |
2780 | return r; | |
d68c645b | 2781 | r = serialize_fd(f, fds, "stdout-fd", s->stdout_fd); |
a34ceba6 LP |
2782 | if (r < 0) |
2783 | return r; | |
d68c645b | 2784 | r = serialize_fd(f, fds, "stderr-fd", s->stderr_fd); |
a34ceba6 LP |
2785 | if (r < 0) |
2786 | return r; | |
e44da745 | 2787 | |
5686391b | 2788 | if (s->exec_fd_event_source) { |
d68c645b | 2789 | r = serialize_fd(f, fds, "exec-fd", sd_event_source_get_io_fd(s->exec_fd_event_source)); |
5686391b LP |
2790 | if (r < 0) |
2791 | return r; | |
d68c645b LP |
2792 | |
2793 | (void) serialize_bool(f, "exec-fd-hot", s->exec_fd_hot); | |
5686391b LP |
2794 | } |
2795 | ||
9dfb64f8 | 2796 | if (UNIT_ISSET(s->accept_socket)) { |
d68c645b | 2797 | r = serialize_item(f, "accept-socket", UNIT_DEREF(s->accept_socket)->id); |
9dfb64f8 ZJS |
2798 | if (r < 0) |
2799 | return r; | |
2800 | } | |
2801 | ||
d68c645b | 2802 | r = serialize_fd(f, fds, "socket-fd", s->socket_fd); |
a34ceba6 LP |
2803 | if (r < 0) |
2804 | return r; | |
e44da745 | 2805 | |
2339fc93 | 2806 | LIST_FOREACH(fd_store, fs, s->fd_store) { |
8dd4c05b | 2807 | _cleanup_free_ char *c = NULL; |
2339fc93 LP |
2808 | int copy; |
2809 | ||
2810 | copy = fdset_put_dup(fds, fs->fd); | |
2811 | if (copy < 0) | |
d68c645b | 2812 | return log_error_errno(copy, "Failed to copy file descriptor for serialization: %m"); |
2339fc93 | 2813 | |
8dd4c05b | 2814 | c = cescape(fs->fdname); |
d68c645b LP |
2815 | if (!c) |
2816 | return log_oom(); | |
8dd4c05b | 2817 | |
30520492 | 2818 | (void) serialize_item_format(f, "fd-store-fd", "%i \"%s\" %i", copy, c, fs->do_poll); |
2339fc93 LP |
2819 | } |
2820 | ||
ecdbca40 | 2821 | if (s->main_exec_status.pid > 0) { |
d68c645b LP |
2822 | (void) serialize_item_format(f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid); |
2823 | (void) serialize_dual_timestamp(f, "main-exec-status-start", &s->main_exec_status.start_timestamp); | |
2824 | (void) serialize_dual_timestamp(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp); | |
ecdbca40 | 2825 | |
799fd0fd | 2826 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { |
d68c645b LP |
2827 | (void) serialize_item_format(f, "main-exec-status-code", "%i", s->main_exec_status.code); |
2828 | (void) serialize_item_format(f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
ecdbca40 LP |
2829 | } |
2830 | } | |
f06db334 | 2831 | |
d68c645b LP |
2832 | (void) serialize_dual_timestamp(f, "watchdog-timestamp", &s->watchdog_timestamp); |
2833 | (void) serialize_bool(f, "forbid-restart", s->forbid_restart); | |
6aca9a58 | 2834 | |
2787d83c | 2835 | if (s->watchdog_override_enable) |
d68c645b | 2836 | (void) serialize_item_format(f, "watchdog-override-usec", USEC_FMT, s->watchdog_override_usec); |
2787d83c | 2837 | |
aa8c4bbf LP |
2838 | if (s->watchdog_original_usec != USEC_INFINITY) |
2839 | (void) serialize_item_format(f, "watchdog-original-usec", USEC_FMT, s->watchdog_original_usec); | |
2840 | ||
a16e1123 LP |
2841 | return 0; |
2842 | } | |
2843 | ||
35243b77 | 2844 | int service_deserialize_exec_command( |
5b99bd5f LP |
2845 | Unit *u, |
2846 | const char *key, | |
2847 | const char *value) { | |
2848 | ||
e266c068 MS |
2849 | Service *s = SERVICE(u); |
2850 | int r; | |
2851 | unsigned idx = 0, i; | |
2852 | bool control, found = false; | |
2853 | ServiceExecCommand id = _SERVICE_EXEC_COMMAND_INVALID; | |
2854 | ExecCommand *command = NULL; | |
6eeec374 | 2855 | _cleanup_free_ char *path = NULL; |
e266c068 MS |
2856 | _cleanup_strv_free_ char **argv = NULL; |
2857 | ||
2858 | enum ExecCommandState { | |
2859 | STATE_EXEC_COMMAND_TYPE, | |
2860 | STATE_EXEC_COMMAND_INDEX, | |
2861 | STATE_EXEC_COMMAND_PATH, | |
2862 | STATE_EXEC_COMMAND_ARGS, | |
2863 | _STATE_EXEC_COMMAND_MAX, | |
2d93c20e | 2864 | _STATE_EXEC_COMMAND_INVALID = -EINVAL, |
e266c068 MS |
2865 | } state; |
2866 | ||
2867 | assert(s); | |
2868 | assert(key); | |
2869 | assert(value); | |
2870 | ||
2871 | control = streq(key, "control-command"); | |
2872 | ||
2873 | state = STATE_EXEC_COMMAND_TYPE; | |
2874 | ||
2875 | for (;;) { | |
2876 | _cleanup_free_ char *arg = NULL; | |
2877 | ||
334c0979 | 2878 | r = extract_first_word(&value, &arg, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE); |
efa3f34e LP |
2879 | if (r < 0) |
2880 | return r; | |
e266c068 MS |
2881 | if (r == 0) |
2882 | break; | |
e266c068 MS |
2883 | |
2884 | switch (state) { | |
2885 | case STATE_EXEC_COMMAND_TYPE: | |
2886 | id = service_exec_command_from_string(arg); | |
2887 | if (id < 0) | |
7211c853 | 2888 | return id; |
e266c068 MS |
2889 | |
2890 | state = STATE_EXEC_COMMAND_INDEX; | |
2891 | break; | |
2892 | case STATE_EXEC_COMMAND_INDEX: | |
2893 | r = safe_atou(arg, &idx); | |
2894 | if (r < 0) | |
7211c853 | 2895 | return r; |
e266c068 MS |
2896 | |
2897 | state = STATE_EXEC_COMMAND_PATH; | |
2898 | break; | |
2899 | case STATE_EXEC_COMMAND_PATH: | |
ae2a15bc | 2900 | path = TAKE_PTR(arg); |
e266c068 | 2901 | state = STATE_EXEC_COMMAND_ARGS; |
e266c068 MS |
2902 | break; |
2903 | case STATE_EXEC_COMMAND_ARGS: | |
2904 | r = strv_extend(&argv, arg); | |
2905 | if (r < 0) | |
2906 | return -ENOMEM; | |
2907 | break; | |
2908 | default: | |
04499a70 | 2909 | assert_not_reached(); |
e266c068 MS |
2910 | } |
2911 | } | |
2912 | ||
2913 | if (state != STATE_EXEC_COMMAND_ARGS) | |
2914 | return -EINVAL; | |
90204792 ZJS |
2915 | if (strv_isempty(argv)) |
2916 | return -EINVAL; /* At least argv[0] must be always present. */ | |
e266c068 MS |
2917 | |
2918 | /* Let's check whether exec command on given offset matches data that we just deserialized */ | |
2919 | for (command = s->exec_command[id], i = 0; command; command = command->command_next, i++) { | |
2920 | if (i != idx) | |
2921 | continue; | |
2922 | ||
2923 | found = strv_equal(argv, command->argv) && streq(command->path, path); | |
2924 | break; | |
2925 | } | |
2926 | ||
2927 | if (!found) { | |
2928 | /* Command at the index we serialized is different, let's look for command that exactly | |
2929 | * matches but is on different index. If there is no such command we will not resume execution. */ | |
2930 | for (command = s->exec_command[id]; command; command = command->command_next) | |
2931 | if (strv_equal(command->argv, argv) && streq(command->path, path)) | |
2932 | break; | |
2933 | } | |
2934 | ||
e9da62b1 | 2935 | if (command && control) { |
e266c068 | 2936 | s->control_command = command; |
e9da62b1 LP |
2937 | s->control_command_id = id; |
2938 | } else if (command) | |
e266c068 MS |
2939 | s->main_command = command; |
2940 | else | |
2941 | log_unit_warning(u, "Current command vanished from the unit file, execution of the command list won't be resumed."); | |
2942 | ||
2943 | return 0; | |
2944 | } | |
2945 | ||
a16e1123 LP |
2946 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { |
2947 | Service *s = SERVICE(u); | |
2339fc93 | 2948 | int r; |
a16e1123 LP |
2949 | |
2950 | assert(u); | |
2951 | assert(key); | |
2952 | assert(value); | |
2953 | assert(fds); | |
2954 | ||
2955 | if (streq(key, "state")) { | |
2956 | ServiceState state; | |
2957 | ||
117dcc57 ZJS |
2958 | state = service_state_from_string(value); |
2959 | if (state < 0) | |
f2341e0a | 2960 | log_unit_debug(u, "Failed to parse state value: %s", value); |
a16e1123 LP |
2961 | else |
2962 | s->deserialized_state = state; | |
f42806df LP |
2963 | } else if (streq(key, "result")) { |
2964 | ServiceResult f; | |
2965 | ||
2966 | f = service_result_from_string(value); | |
2967 | if (f < 0) | |
f2341e0a | 2968 | log_unit_debug(u, "Failed to parse result value: %s", value); |
f42806df LP |
2969 | else if (f != SERVICE_SUCCESS) |
2970 | s->result = f; | |
2971 | ||
2972 | } else if (streq(key, "reload-result")) { | |
2973 | ServiceResult f; | |
2974 | ||
2975 | f = service_result_from_string(value); | |
2976 | if (f < 0) | |
f2341e0a | 2977 | log_unit_debug(u, "Failed to parse reload result value: %s", value); |
f42806df LP |
2978 | else if (f != SERVICE_SUCCESS) |
2979 | s->reload_result = f; | |
a16e1123 | 2980 | |
a16e1123 | 2981 | } else if (streq(key, "control-pid")) { |
5925dd3c | 2982 | pid_t pid; |
a16e1123 | 2983 | |
e364ad06 | 2984 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 2985 | log_unit_debug(u, "Failed to parse control-pid value: %s", value); |
a16e1123 | 2986 | else |
e55224ca | 2987 | s->control_pid = pid; |
a16e1123 | 2988 | } else if (streq(key, "main-pid")) { |
5925dd3c | 2989 | pid_t pid; |
a16e1123 | 2990 | |
e364ad06 | 2991 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 2992 | log_unit_debug(u, "Failed to parse main-pid value: %s", value); |
eabd3e56 LP |
2993 | else |
2994 | (void) service_set_main_pid(s, pid); | |
a16e1123 LP |
2995 | } else if (streq(key, "main-pid-known")) { |
2996 | int b; | |
2997 | ||
117dcc57 ZJS |
2998 | b = parse_boolean(value); |
2999 | if (b < 0) | |
f2341e0a | 3000 | log_unit_debug(u, "Failed to parse main-pid-known value: %s", value); |
a16e1123 LP |
3001 | else |
3002 | s->main_pid_known = b; | |
de1d4f9b WF |
3003 | } else if (streq(key, "bus-name-good")) { |
3004 | int b; | |
3005 | ||
3006 | b = parse_boolean(value); | |
3007 | if (b < 0) | |
3008 | log_unit_debug(u, "Failed to parse bus-name-good value: %s", value); | |
3009 | else | |
3010 | s->bus_name_good = b; | |
d8ccf5fd DM |
3011 | } else if (streq(key, "bus-name-owner")) { |
3012 | r = free_and_strdup(&s->bus_name_owner, value); | |
3013 | if (r < 0) | |
3014 | log_unit_error_errno(u, r, "Unable to deserialize current bus owner %s: %m", value); | |
3a2776bc LP |
3015 | } else if (streq(key, "status-text")) { |
3016 | char *t; | |
e437538f | 3017 | ssize_t l; |
3a2776bc | 3018 | |
e437538f ZJS |
3019 | l = cunescape(value, 0, &t); |
3020 | if (l < 0) | |
3021 | log_unit_debug_errno(u, l, "Failed to unescape status text '%s': %m", value); | |
efa3f34e LP |
3022 | else |
3023 | free_and_replace(s->status_text, t); | |
3a2776bc | 3024 | |
9dfb64f8 ZJS |
3025 | } else if (streq(key, "accept-socket")) { |
3026 | Unit *socket; | |
3027 | ||
3028 | r = manager_load_unit(u->manager, value, NULL, NULL, &socket); | |
3029 | if (r < 0) | |
5e1ee764 | 3030 | log_unit_debug_errno(u, r, "Failed to load accept-socket unit '%s': %m", value); |
9dfb64f8 | 3031 | else { |
7f7d01ed | 3032 | unit_ref_set(&s->accept_socket, u, socket); |
9dfb64f8 ZJS |
3033 | SOCKET(socket)->n_connections++; |
3034 | } | |
3035 | ||
a16e1123 LP |
3036 | } else if (streq(key, "socket-fd")) { |
3037 | int fd; | |
3038 | ||
3039 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 3040 | log_unit_debug(u, "Failed to parse socket-fd value: %s", value); |
a16e1123 | 3041 | else { |
574634bc | 3042 | asynchronous_close(s->socket_fd); |
a16e1123 LP |
3043 | s->socket_fd = fdset_remove(fds, fd); |
3044 | } | |
2339fc93 | 3045 | } else if (streq(key, "fd-store-fd")) { |
30520492 | 3046 | _cleanup_free_ char *fdv = NULL, *fdn = NULL, *fdp = NULL; |
2339fc93 | 3047 | int fd; |
30520492 | 3048 | int do_poll; |
2339fc93 | 3049 | |
30520492 KL |
3050 | r = extract_first_word(&value, &fdv, NULL, 0); |
3051 | if (r <= 0 || safe_atoi(fdv, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) { | |
f2341e0a | 3052 | log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); |
30520492 KL |
3053 | return 0; |
3054 | } | |
8dd4c05b | 3055 | |
30520492 KL |
3056 | r = extract_first_word(&value, &fdn, NULL, EXTRACT_CUNESCAPE | EXTRACT_UNQUOTE); |
3057 | if (r <= 0) { | |
95599cac | 3058 | log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); |
30520492 KL |
3059 | return 0; |
3060 | } | |
8dd4c05b | 3061 | |
30520492 KL |
3062 | r = extract_first_word(&value, &fdp, NULL, 0); |
3063 | if (r == 0) { | |
3064 | /* If the value is not present, we assume the default */ | |
3065 | do_poll = 1; | |
3066 | } else if (r < 0 || safe_atoi(fdp, &do_poll) < 0) { | |
3067 | log_unit_debug_errno(u, r, "Failed to parse fd-store-fd value \"%s\": %m", value); | |
3068 | return 0; | |
2339fc93 LP |
3069 | } |
3070 | ||
30520492 KL |
3071 | r = service_add_fd_store(s, fd, fdn, do_poll); |
3072 | if (r < 0) | |
3073 | log_unit_error_errno(u, r, "Failed to add fd to store: %m"); | |
3074 | else | |
3075 | fdset_remove(fds, fd); | |
ecdbca40 LP |
3076 | } else if (streq(key, "main-exec-status-pid")) { |
3077 | pid_t pid; | |
3078 | ||
e364ad06 | 3079 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 3080 | log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value); |
ecdbca40 LP |
3081 | else |
3082 | s->main_exec_status.pid = pid; | |
3083 | } else if (streq(key, "main-exec-status-code")) { | |
3084 | int i; | |
3085 | ||
e364ad06 | 3086 | if (safe_atoi(value, &i) < 0) |
f2341e0a | 3087 | log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value); |
ecdbca40 LP |
3088 | else |
3089 | s->main_exec_status.code = i; | |
3090 | } else if (streq(key, "main-exec-status-status")) { | |
3091 | int i; | |
3092 | ||
e364ad06 | 3093 | if (safe_atoi(value, &i) < 0) |
f2341e0a | 3094 | log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value); |
ecdbca40 LP |
3095 | else |
3096 | s->main_exec_status.status = i; | |
799fd0fd | 3097 | } else if (streq(key, "main-exec-status-start")) |
d68c645b | 3098 | deserialize_dual_timestamp(value, &s->main_exec_status.start_timestamp); |
799fd0fd | 3099 | else if (streq(key, "main-exec-status-exit")) |
d68c645b | 3100 | deserialize_dual_timestamp(value, &s->main_exec_status.exit_timestamp); |
a6927d7f | 3101 | else if (streq(key, "watchdog-timestamp")) |
d68c645b | 3102 | deserialize_dual_timestamp(value, &s->watchdog_timestamp); |
613b411c | 3103 | else if (streq(key, "forbid-restart")) { |
6aca9a58 SE |
3104 | int b; |
3105 | ||
3106 | b = parse_boolean(value); | |
3107 | if (b < 0) | |
f2341e0a | 3108 | log_unit_debug(u, "Failed to parse forbid-restart value: %s", value); |
6aca9a58 SE |
3109 | else |
3110 | s->forbid_restart = b; | |
a34ceba6 LP |
3111 | } else if (streq(key, "stdin-fd")) { |
3112 | int fd; | |
3113 | ||
3114 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3115 | log_unit_debug(u, "Failed to parse stdin-fd value: %s", value); | |
3116 | else { | |
3117 | asynchronous_close(s->stdin_fd); | |
3118 | s->stdin_fd = fdset_remove(fds, fd); | |
1e22b5cd | 3119 | s->exec_context.stdio_as_fds = true; |
a34ceba6 LP |
3120 | } |
3121 | } else if (streq(key, "stdout-fd")) { | |
3122 | int fd; | |
3123 | ||
3124 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3125 | log_unit_debug(u, "Failed to parse stdout-fd value: %s", value); | |
3126 | else { | |
3127 | asynchronous_close(s->stdout_fd); | |
3128 | s->stdout_fd = fdset_remove(fds, fd); | |
1e22b5cd | 3129 | s->exec_context.stdio_as_fds = true; |
a34ceba6 LP |
3130 | } |
3131 | } else if (streq(key, "stderr-fd")) { | |
3132 | int fd; | |
3133 | ||
3134 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3135 | log_unit_debug(u, "Failed to parse stderr-fd value: %s", value); | |
3136 | else { | |
3137 | asynchronous_close(s->stderr_fd); | |
3138 | s->stderr_fd = fdset_remove(fds, fd); | |
1e22b5cd | 3139 | s->exec_context.stdio_as_fds = true; |
a34ceba6 | 3140 | } |
5686391b LP |
3141 | } else if (streq(key, "exec-fd")) { |
3142 | int fd; | |
3143 | ||
3144 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
3145 | log_unit_debug(u, "Failed to parse exec-fd value: %s", value); | |
3146 | else { | |
5dcadb4c | 3147 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
5686391b LP |
3148 | |
3149 | fd = fdset_remove(fds, fd); | |
3150 | if (service_allocate_exec_fd_event_source(s, fd, &s->exec_fd_event_source) < 0) | |
3151 | safe_close(fd); | |
3152 | } | |
2787d83c | 3153 | } else if (streq(key, "watchdog-override-usec")) { |
d68c645b | 3154 | if (deserialize_usec(value, &s->watchdog_override_usec) < 0) |
2787d83c | 3155 | log_unit_debug(u, "Failed to parse watchdog_override_usec value: %s", value); |
d68c645b | 3156 | else |
2787d83c | 3157 | s->watchdog_override_enable = true; |
d68c645b | 3158 | |
aa8c4bbf LP |
3159 | } else if (streq(key, "watchdog-original-usec")) { |
3160 | if (deserialize_usec(value, &s->watchdog_original_usec) < 0) | |
3161 | log_unit_debug(u, "Failed to parse watchdog_original_usec value: %s", value); | |
3162 | ||
e266c068 MS |
3163 | } else if (STR_IN_SET(key, "main-command", "control-command")) { |
3164 | r = service_deserialize_exec_command(u, key, value); | |
3165 | if (r < 0) | |
3166 | log_unit_debug_errno(u, r, "Failed to parse serialized command \"%s\": %m", value); | |
7a0019d3 LP |
3167 | |
3168 | } else if (streq(key, "n-restarts")) { | |
3169 | r = safe_atou(value, &s->n_restarts); | |
3170 | if (r < 0) | |
3171 | log_unit_debug_errno(u, r, "Failed to parse serialized restart counter '%s': %m", value); | |
3172 | ||
3173 | } else if (streq(key, "flush-n-restarts")) { | |
3174 | r = parse_boolean(value); | |
3175 | if (r < 0) | |
3176 | log_unit_debug_errno(u, r, "Failed to parse serialized flush restart counter setting '%s': %m", value); | |
3177 | else | |
3178 | s->flush_n_restarts = r; | |
c17ec25e | 3179 | } else |
f2341e0a | 3180 | log_unit_debug(u, "Unknown serialization key: %s", key); |
a16e1123 LP |
3181 | |
3182 | return 0; | |
3183 | } | |
3184 | ||
44a6b1b6 | 3185 | _pure_ static UnitActiveState service_active_state(Unit *u) { |
e056b01d LP |
3186 | const UnitActiveState *table; |
3187 | ||
87f0e418 | 3188 | assert(u); |
5cb5a6ff | 3189 | |
e056b01d LP |
3190 | table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
3191 | ||
3192 | return table[SERVICE(u)->state]; | |
034c6ed7 LP |
3193 | } |
3194 | ||
10a94420 LP |
3195 | static const char *service_sub_state_to_string(Unit *u) { |
3196 | assert(u); | |
3197 | ||
3198 | return service_state_to_string(SERVICE(u)->state); | |
3199 | } | |
3200 | ||
f2f725e5 | 3201 | static bool service_may_gc(Unit *u) { |
701cc384 LP |
3202 | Service *s = SERVICE(u); |
3203 | ||
3204 | assert(s); | |
3205 | ||
e98b2fbb | 3206 | /* Never clean up services that still have a process around, even if the service is formally dead. Note that |
f2f725e5 | 3207 | * unit_may_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they |
e98b2fbb LP |
3208 | * have moved outside of the cgroup. */ |
3209 | ||
3210 | if (main_pid_good(s) > 0 || | |
6d55002a | 3211 | control_pid_good(s) > 0) |
f2f725e5 | 3212 | return false; |
6d55002a | 3213 | |
f2f725e5 | 3214 | return true; |
6d55002a LP |
3215 | } |
3216 | ||
3a111838 MS |
3217 | static int service_retry_pid_file(Service *s) { |
3218 | int r; | |
3219 | ||
3220 | assert(s->pid_file); | |
3742095b | 3221 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
3a111838 MS |
3222 | |
3223 | r = service_load_pid_file(s, false); | |
3224 | if (r < 0) | |
3225 | return r; | |
3226 | ||
3227 | service_unwatch_pid_file(s); | |
3228 | ||
f42806df | 3229 | service_enter_running(s, SERVICE_SUCCESS); |
3a111838 MS |
3230 | return 0; |
3231 | } | |
3232 | ||
3233 | static int service_watch_pid_file(Service *s) { | |
3234 | int r; | |
3235 | ||
f2341e0a | 3236 | log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path); |
8bb2d17d | 3237 | |
5686391b | 3238 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_inotify_io); |
3a111838 MS |
3239 | if (r < 0) |
3240 | goto fail; | |
3241 | ||
3242 | /* the pidfile might have appeared just before we set the watch */ | |
f2341e0a | 3243 | log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path); |
3a111838 MS |
3244 | service_retry_pid_file(s); |
3245 | ||
3246 | return 0; | |
3247 | fail: | |
f2341e0a | 3248 | log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path); |
3a111838 MS |
3249 | service_unwatch_pid_file(s); |
3250 | return r; | |
3251 | } | |
3252 | ||
3253 | static int service_demand_pid_file(Service *s) { | |
3254 | PathSpec *ps; | |
3255 | ||
3256 | assert(s->pid_file); | |
3257 | assert(!s->pid_file_pathspec); | |
3258 | ||
3259 | ps = new0(PathSpec, 1); | |
3260 | if (!ps) | |
3261 | return -ENOMEM; | |
3262 | ||
718db961 | 3263 | ps->unit = UNIT(s); |
3a111838 MS |
3264 | ps->path = strdup(s->pid_file); |
3265 | if (!ps->path) { | |
3266 | free(ps); | |
3267 | return -ENOMEM; | |
3268 | } | |
3269 | ||
4ff361cc | 3270 | path_simplify(ps->path); |
3a111838 MS |
3271 | |
3272 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that | |
3273 | * keep their PID file open all the time. */ | |
3274 | ps->type = PATH_MODIFIED; | |
3275 | ps->inotify_fd = -1; | |
3276 | ||
3277 | s->pid_file_pathspec = ps; | |
3278 | ||
3279 | return service_watch_pid_file(s); | |
3280 | } | |
3281 | ||
5686391b | 3282 | static int service_dispatch_inotify_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
e14c2802 LP |
3283 | PathSpec *p = userdata; |
3284 | Service *s; | |
3285 | ||
3286 | assert(p); | |
3287 | ||
3288 | s = SERVICE(p->unit); | |
3a111838 MS |
3289 | |
3290 | assert(s); | |
3291 | assert(fd >= 0); | |
3742095b | 3292 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
3a111838 | 3293 | assert(s->pid_file_pathspec); |
57020a3a | 3294 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); |
3a111838 | 3295 | |
f2341e0a | 3296 | log_unit_debug(UNIT(s), "inotify event"); |
3a111838 | 3297 | |
e14c2802 | 3298 | if (path_spec_fd_event(p, events) < 0) |
3a111838 MS |
3299 | goto fail; |
3300 | ||
3301 | if (service_retry_pid_file(s) == 0) | |
718db961 | 3302 | return 0; |
3a111838 MS |
3303 | |
3304 | if (service_watch_pid_file(s) < 0) | |
3305 | goto fail; | |
3306 | ||
718db961 LP |
3307 | return 0; |
3308 | ||
3a111838 MS |
3309 | fail: |
3310 | service_unwatch_pid_file(s); | |
f42806df | 3311 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
718db961 | 3312 | return 0; |
3a111838 MS |
3313 | } |
3314 | ||
5686391b LP |
3315 | static int service_dispatch_exec_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
3316 | Service *s = SERVICE(userdata); | |
3317 | ||
3318 | assert(s); | |
3319 | ||
3320 | log_unit_debug(UNIT(s), "got exec-fd event"); | |
3321 | ||
3322 | /* If Type=exec is set, we'll consider a service started successfully the instant we invoked execve() | |
3323 | * successfully for it. We implement this through a pipe() towards the child, which the kernel automatically | |
3324 | * closes for us due to O_CLOEXEC on execve() in the child, which then triggers EOF on the pipe in the | |
3325 | * parent. We need to be careful however, as there are other reasons that we might cause the child's side of | |
3326 | * the pipe to be closed (for example, a simple exit()). To deal with that we'll ignore EOFs on the pipe unless | |
3327 | * the child signalled us first that it is about to call the execve(). It does so by sending us a simple | |
3328 | * non-zero byte via the pipe. We also provide the child with a way to inform us in case execve() failed: if it | |
3329 | * sends a zero byte we'll ignore POLLHUP on the fd again. */ | |
3330 | ||
3331 | for (;;) { | |
3332 | uint8_t x; | |
3333 | ssize_t n; | |
3334 | ||
3335 | n = read(fd, &x, sizeof(x)); | |
3336 | if (n < 0) { | |
3337 | if (errno == EAGAIN) /* O_NONBLOCK in effect → everything queued has now been processed. */ | |
3338 | return 0; | |
3339 | ||
3340 | return log_unit_error_errno(UNIT(s), errno, "Failed to read from exec_fd: %m"); | |
3341 | } | |
3342 | if (n == 0) { /* EOF → the event we are waiting for */ | |
3343 | ||
5dcadb4c | 3344 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); |
5686391b LP |
3345 | |
3346 | if (s->exec_fd_hot) { /* Did the child tell us to expect EOF now? */ | |
3347 | log_unit_debug(UNIT(s), "Got EOF on exec-fd"); | |
3348 | ||
3349 | s->exec_fd_hot = false; | |
3350 | ||
3351 | /* Nice! This is what we have been waiting for. Transition to next state. */ | |
3352 | if (s->type == SERVICE_EXEC && s->state == SERVICE_START) | |
3353 | service_enter_start_post(s); | |
3354 | } else | |
3355 | log_unit_debug(UNIT(s), "Got EOF on exec-fd while it was disabled, ignoring."); | |
3356 | ||
3357 | return 0; | |
3358 | } | |
3359 | ||
3360 | /* A byte was read → this turns on/off the exec fd logic */ | |
3361 | assert(n == sizeof(x)); | |
3362 | s->exec_fd_hot = x; | |
3363 | } | |
3364 | ||
3365 | return 0; | |
3366 | } | |
3367 | ||
a911bb9a LP |
3368 | static void service_notify_cgroup_empty_event(Unit *u) { |
3369 | Service *s = SERVICE(u); | |
3370 | ||
3371 | assert(u); | |
3372 | ||
a5b5aece | 3373 | log_unit_debug(u, "Control group is empty."); |
a911bb9a LP |
3374 | |
3375 | switch (s->state) { | |
3376 | ||
3377 | /* Waiting for SIGCHLD is usually more interesting, | |
3378 | * because it includes return codes/signals. Which is | |
3379 | * why we ignore the cgroup events for most cases, | |
3380 | * except when we don't know pid which to expect the | |
3381 | * SIGCHLD for. */ | |
3382 | ||
3383 | case SERVICE_START: | |
3c751b1b LP |
3384 | if (s->type == SERVICE_NOTIFY && |
3385 | main_pid_good(s) == 0 && | |
3386 | control_pid_good(s) == 0) { | |
3d474ef7 | 3387 | /* No chance of getting a ready notification anymore */ |
c3fda31d | 3388 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
71e529fc JW |
3389 | break; |
3390 | } | |
3391 | ||
596e4470 HC |
3392 | if (s->exit_type == SERVICE_EXIT_CGROUP && main_pid_good(s) <= 0) |
3393 | service_enter_start_post(s); | |
3394 | ||
4831981d | 3395 | _fallthrough_; |
71e529fc | 3396 | case SERVICE_START_POST: |
3c751b1b LP |
3397 | if (s->pid_file_pathspec && |
3398 | main_pid_good(s) == 0 && | |
3399 | control_pid_good(s) == 0) { | |
3400 | ||
3d474ef7 | 3401 | /* Give up hoping for the daemon to write its PID file */ |
f2341e0a | 3402 | log_unit_warning(u, "Daemon never wrote its PID file. Failing."); |
8bb2d17d | 3403 | |
a911bb9a LP |
3404 | service_unwatch_pid_file(s); |
3405 | if (s->state == SERVICE_START) | |
c3fda31d | 3406 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
a911bb9a | 3407 | else |
c35755fb | 3408 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
a911bb9a LP |
3409 | } |
3410 | break; | |
3411 | ||
3412 | case SERVICE_RUNNING: | |
3413 | /* service_enter_running() will figure out what to do */ | |
3414 | service_enter_running(s, SERVICE_SUCCESS); | |
3415 | break; | |
3416 | ||
c87700a1 | 3417 | case SERVICE_STOP_WATCHDOG: |
a911bb9a LP |
3418 | case SERVICE_STOP_SIGTERM: |
3419 | case SERVICE_STOP_SIGKILL: | |
3420 | ||
b13ddbbc | 3421 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
a911bb9a LP |
3422 | service_enter_stop_post(s, SERVICE_SUCCESS); |
3423 | ||
3424 | break; | |
3425 | ||
3426 | case SERVICE_STOP_POST: | |
bf760801 | 3427 | case SERVICE_FINAL_WATCHDOG: |
a911bb9a LP |
3428 | case SERVICE_FINAL_SIGTERM: |
3429 | case SERVICE_FINAL_SIGKILL: | |
b13ddbbc | 3430 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
a911bb9a LP |
3431 | service_enter_dead(s, SERVICE_SUCCESS, true); |
3432 | ||
3433 | break; | |
3434 | ||
e08dabfe AZ |
3435 | /* If the cgroup empty notification comes when the unit is not active, we must have failed to clean |
3436 | * up the cgroup earlier and should do it now. */ | |
3437 | case SERVICE_DEAD: | |
3438 | case SERVICE_FAILED: | |
3439 | unit_prune_cgroup(u); | |
3440 | break; | |
3441 | ||
a911bb9a LP |
3442 | default: |
3443 | ; | |
3444 | } | |
3445 | } | |
3446 | ||
afcfaa69 LP |
3447 | static void service_notify_cgroup_oom_event(Unit *u) { |
3448 | Service *s = SERVICE(u); | |
3449 | ||
3450 | log_unit_debug(u, "Process of control group was killed by the OOM killer."); | |
3451 | ||
3452 | if (s->oom_policy == OOM_CONTINUE) | |
3453 | return; | |
3454 | ||
3455 | switch (s->state) { | |
3456 | ||
31cd5f63 | 3457 | case SERVICE_CONDITION: |
afcfaa69 LP |
3458 | case SERVICE_START_PRE: |
3459 | case SERVICE_START: | |
3460 | case SERVICE_START_POST: | |
3461 | case SERVICE_STOP: | |
3462 | if (s->oom_policy == OOM_STOP) | |
3463 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_OOM_KILL); | |
3464 | else if (s->oom_policy == OOM_KILL) | |
3465 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3466 | ||
3467 | break; | |
3468 | ||
3469 | case SERVICE_EXITED: | |
3470 | case SERVICE_RUNNING: | |
3471 | if (s->oom_policy == OOM_STOP) | |
3472 | service_enter_stop(s, SERVICE_FAILURE_OOM_KILL); | |
3473 | else if (s->oom_policy == OOM_KILL) | |
3474 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3475 | ||
3476 | break; | |
3477 | ||
3478 | case SERVICE_STOP_WATCHDOG: | |
3479 | case SERVICE_STOP_SIGTERM: | |
3480 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3481 | break; | |
3482 | ||
3483 | case SERVICE_STOP_SIGKILL: | |
3484 | case SERVICE_FINAL_SIGKILL: | |
3485 | if (s->result == SERVICE_SUCCESS) | |
3486 | s->result = SERVICE_FAILURE_OOM_KILL; | |
3487 | break; | |
3488 | ||
3489 | case SERVICE_STOP_POST: | |
3490 | case SERVICE_FINAL_SIGTERM: | |
3491 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_OOM_KILL); | |
3492 | break; | |
3493 | ||
3494 | default: | |
3495 | ; | |
3496 | } | |
3497 | } | |
3498 | ||
87f0e418 | 3499 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
5cdabc8d | 3500 | bool notify_dbus = true; |
87f0e418 | 3501 | Service *s = SERVICE(u); |
f42806df | 3502 | ServiceResult f; |
e5123725 | 3503 | ExitClean clean_mode; |
5cb5a6ff LP |
3504 | |
3505 | assert(s); | |
034c6ed7 LP |
3506 | assert(pid >= 0); |
3507 | ||
e5123725 AZ |
3508 | /* Oneshot services and non-SERVICE_EXEC_START commands should not be |
3509 | * considered daemons as they are typically not long running. */ | |
3510 | if (s->type == SERVICE_ONESHOT || (s->control_pid == pid && s->control_command_id != SERVICE_EXEC_START)) | |
3511 | clean_mode = EXIT_CLEAN_COMMAND; | |
3512 | else | |
3513 | clean_mode = EXIT_CLEAN_DAEMON; | |
3514 | ||
3515 | if (is_clean_exit(code, status, clean_mode, &s->success_status)) | |
f42806df LP |
3516 | f = SERVICE_SUCCESS; |
3517 | else if (code == CLD_EXITED) | |
3518 | f = SERVICE_FAILURE_EXIT_CODE; | |
3519 | else if (code == CLD_KILLED) | |
3520 | f = SERVICE_FAILURE_SIGNAL; | |
3521 | else if (code == CLD_DUMPED) | |
3522 | f = SERVICE_FAILURE_CORE_DUMP; | |
d06dacd0 | 3523 | else |
04499a70 | 3524 | assert_not_reached(); |
034c6ed7 | 3525 | |
abaf5edd | 3526 | if (s->main_pid == pid) { |
13bb1ffb ZJS |
3527 | /* Clean up the exec_fd event source. We want to do this here, not later in |
3528 | * service_set_state(), because service_enter_stop_post() calls service_spawn(). | |
3529 | * The source owns its end of the pipe, so this will close that too. */ | |
3530 | s->exec_fd_event_source = sd_event_source_disable_unref(s->exec_fd_event_source); | |
3531 | ||
db01f8b3 MS |
3532 | /* Forking services may occasionally move to a new PID. |
3533 | * As long as they update the PID file before exiting the old | |
3534 | * PID, they're fine. */ | |
db256aab | 3535 | if (service_load_pid_file(s, false) > 0) |
db01f8b3 | 3536 | return; |
034c6ed7 | 3537 | |
034c6ed7 | 3538 | s->main_pid = 0; |
6ea832a2 | 3539 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); |
034c6ed7 | 3540 | |
867b3b7d | 3541 | if (s->main_command) { |
fbeefb45 LP |
3542 | /* If this is not a forking service than the |
3543 | * main process got started and hence we copy | |
3544 | * the exit status so that it is recorded both | |
3545 | * as main and as control process exit | |
3546 | * status */ | |
3547 | ||
867b3b7d | 3548 | s->main_command->exec_status = s->main_exec_status; |
b708e7ce | 3549 | |
3ed0cd26 | 3550 | if (s->main_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
f42806df | 3551 | f = SERVICE_SUCCESS; |
fbeefb45 LP |
3552 | } else if (s->exec_command[SERVICE_EXEC_START]) { |
3553 | ||
3554 | /* If this is a forked process, then we should | |
3555 | * ignore the return value if this was | |
3556 | * configured for the starter process */ | |
3557 | ||
3ed0cd26 | 3558 | if (s->exec_command[SERVICE_EXEC_START]->flags & EXEC_COMMAND_IGNORE_FAILURE) |
fbeefb45 | 3559 | f = SERVICE_SUCCESS; |
034c6ed7 LP |
3560 | } |
3561 | ||
91bbd9b7 | 3562 | unit_log_process_exit( |
5cc2cd1c | 3563 | u, |
abaf5edd | 3564 | "Main process", |
91bbd9b7 | 3565 | service_exec_command_to_string(SERVICE_EXEC_START), |
5cc2cd1c | 3566 | f == SERVICE_SUCCESS, |
91bbd9b7 | 3567 | code, status); |
f42806df | 3568 | |
a0fef983 | 3569 | if (s->result == SERVICE_SUCCESS) |
f42806df | 3570 | s->result = f; |
034c6ed7 | 3571 | |
867b3b7d LP |
3572 | if (s->main_command && |
3573 | s->main_command->command_next && | |
b58aeb70 | 3574 | s->type == SERVICE_ONESHOT && |
f42806df | 3575 | f == SERVICE_SUCCESS) { |
034c6ed7 | 3576 | |
e78695d4 | 3577 | /* There is another command to execute, so let's do that. */ |
034c6ed7 | 3578 | |
f2341e0a | 3579 | log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state)); |
f42806df | 3580 | service_run_next_main(s); |
034c6ed7 | 3581 | |
34e9ba66 | 3582 | } else { |
867b3b7d | 3583 | s->main_command = NULL; |
34e9ba66 | 3584 | |
596e4470 HC |
3585 | /* Services with ExitType=cgroup do not act on main PID exiting, |
3586 | * unless the cgroup is already empty */ | |
3587 | if (s->exit_type == SERVICE_EXIT_MAIN || cgroup_good(s) <= 0) { | |
3588 | /* The service exited, so the service is officially gone. */ | |
3589 | switch (s->state) { | |
3590 | ||
3591 | case SERVICE_START_POST: | |
3592 | case SERVICE_RELOAD: | |
3593 | /* If neither main nor control processes are running then | |
3594 | * the current state can never exit cleanly, hence immediately | |
3595 | * terminate the service. */ | |
3596 | if (control_pid_good(s) <= 0) | |
3597 | service_enter_stop(s, f); | |
3598 | ||
3599 | /* Otherwise need to wait until the operation is done. */ | |
3600 | break; | |
bbe19f68 | 3601 | |
596e4470 HC |
3602 | case SERVICE_STOP: |
3603 | /* Need to wait until the operation is done. */ | |
3604 | break; | |
bbe19f68 | 3605 | |
596e4470 HC |
3606 | case SERVICE_START: |
3607 | if (s->type == SERVICE_ONESHOT) { | |
3608 | /* This was our main goal, so let's go on */ | |
3609 | if (f == SERVICE_SUCCESS) | |
3610 | service_enter_start_post(s); | |
3611 | else | |
3612 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3613 | break; | |
3614 | } else if (s->type == SERVICE_NOTIFY) { | |
3615 | /* Only enter running through a notification, so that the | |
3616 | * SERVICE_START state signifies that no ready notification | |
3617 | * has been received */ | |
3618 | if (f != SERVICE_SUCCESS) | |
3619 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3620 | else if (!s->remain_after_exit || s->notify_access == NOTIFY_MAIN) | |
3621 | /* The service has never been and will never be active */ | |
3622 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); | |
3623 | break; | |
3624 | } | |
7d55e835 | 3625 | |
596e4470 HC |
3626 | _fallthrough_; |
3627 | case SERVICE_RUNNING: | |
3628 | service_enter_running(s, f); | |
3d474ef7 | 3629 | break; |
034c6ed7 | 3630 | |
596e4470 HC |
3631 | case SERVICE_STOP_WATCHDOG: |
3632 | case SERVICE_STOP_SIGTERM: | |
3633 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 3634 | |
596e4470 HC |
3635 | if (control_pid_good(s) <= 0) |
3636 | service_enter_stop_post(s, f); | |
5cb5a6ff | 3637 | |
596e4470 HC |
3638 | /* If there is still a control process, wait for that first */ |
3639 | break; | |
34e9ba66 | 3640 | |
596e4470 | 3641 | case SERVICE_STOP_POST: |
c1566ef0 | 3642 | |
596e4470 HC |
3643 | if (control_pid_good(s) <= 0) |
3644 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
c1566ef0 | 3645 | |
596e4470 | 3646 | break; |
c1566ef0 | 3647 | |
596e4470 HC |
3648 | case SERVICE_FINAL_WATCHDOG: |
3649 | case SERVICE_FINAL_SIGTERM: | |
3650 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 | 3651 | |
596e4470 HC |
3652 | if (control_pid_good(s) <= 0) |
3653 | service_enter_dead(s, f, true); | |
3654 | break; | |
bf108e55 | 3655 | |
596e4470 HC |
3656 | default: |
3657 | assert_not_reached(); | |
3658 | } | |
34e9ba66 | 3659 | } |
034c6ed7 | 3660 | } |
5cb5a6ff | 3661 | |
034c6ed7 | 3662 | } else if (s->control_pid == pid) { |
58441bc1 ZJS |
3663 | const char *kind; |
3664 | bool success; | |
3665 | ||
34e9ba66 LP |
3666 | s->control_pid = 0; |
3667 | ||
b708e7ce | 3668 | if (s->control_command) { |
8bb2d17d | 3669 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 3670 | |
3ed0cd26 | 3671 | if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
f42806df | 3672 | f = SERVICE_SUCCESS; |
b708e7ce LP |
3673 | } |
3674 | ||
42e6f549 AZ |
3675 | /* ExecCondition= calls that exit with (0, 254] should invoke skip-like behavior instead of failing */ |
3676 | if (s->state == SERVICE_CONDITION) { | |
3677 | if (f == SERVICE_FAILURE_EXIT_CODE && status < 255) { | |
3678 | UNIT(s)->condition_result = false; | |
3679 | f = SERVICE_SKIP_CONDITION; | |
58441bc1 ZJS |
3680 | success = true; |
3681 | } else if (f == SERVICE_SUCCESS) { | |
42e6f549 | 3682 | UNIT(s)->condition_result = true; |
58441bc1 ZJS |
3683 | success = true; |
3684 | } else | |
3685 | success = false; | |
3686 | ||
3687 | kind = "Condition check process"; | |
3688 | } else { | |
3689 | kind = "Control process"; | |
3690 | success = f == SERVICE_SUCCESS; | |
42e6f549 AZ |
3691 | } |
3692 | ||
91bbd9b7 | 3693 | unit_log_process_exit( |
5cc2cd1c | 3694 | u, |
58441bc1 | 3695 | kind, |
91bbd9b7 | 3696 | service_exec_command_to_string(s->control_command_id), |
58441bc1 | 3697 | success, |
91bbd9b7 | 3698 | code, status); |
f42806df | 3699 | |
d611cfa7 | 3700 | if (s->state != SERVICE_RELOAD && s->result == SERVICE_SUCCESS) |
f42806df | 3701 | s->result = f; |
034c6ed7 | 3702 | |
34e9ba66 LP |
3703 | if (s->control_command && |
3704 | s->control_command->command_next && | |
f42806df | 3705 | f == SERVICE_SUCCESS) { |
034c6ed7 LP |
3706 | |
3707 | /* There is another command to * | |
3708 | * execute, so let's do that. */ | |
3709 | ||
f2341e0a | 3710 | log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state)); |
f42806df | 3711 | service_run_next_control(s); |
034c6ed7 | 3712 | |
80876c20 | 3713 | } else { |
034c6ed7 LP |
3714 | /* No further commands for this step, so let's |
3715 | * figure out what to do next */ | |
3716 | ||
a16e1123 LP |
3717 | s->control_command = NULL; |
3718 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
3719 | ||
f2341e0a | 3720 | log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state)); |
bd982a8b | 3721 | |
034c6ed7 LP |
3722 | switch (s->state) { |
3723 | ||
31cd5f63 AZ |
3724 | case SERVICE_CONDITION: |
3725 | if (f == SERVICE_SUCCESS) | |
3726 | service_enter_start_pre(s); | |
3727 | else | |
3728 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3729 | break; | |
3730 | ||
034c6ed7 | 3731 | case SERVICE_START_PRE: |
f42806df | 3732 | if (f == SERVICE_SUCCESS) |
034c6ed7 LP |
3733 | service_enter_start(s); |
3734 | else | |
c3fda31d | 3735 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
3736 | break; |
3737 | ||
3738 | case SERVICE_START: | |
bfba3256 LP |
3739 | if (s->type != SERVICE_FORKING) |
3740 | /* Maybe spurious event due to a reload that changed the type? */ | |
3741 | break; | |
034c6ed7 | 3742 | |
f42806df | 3743 | if (f != SERVICE_SUCCESS) { |
c3fda31d | 3744 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
3a111838 MS |
3745 | break; |
3746 | } | |
034c6ed7 | 3747 | |
3a111838 | 3748 | if (s->pid_file) { |
f42806df LP |
3749 | bool has_start_post; |
3750 | int r; | |
3751 | ||
3a111838 MS |
3752 | /* Let's try to load the pid file here if we can. |
3753 | * The PID file might actually be created by a START_POST | |
3754 | * script. In that case don't worry if the loading fails. */ | |
f42806df | 3755 | |
5d904a6a | 3756 | has_start_post = s->exec_command[SERVICE_EXEC_START_POST]; |
f42806df | 3757 | r = service_load_pid_file(s, !has_start_post); |
3a111838 MS |
3758 | if (!has_start_post && r < 0) { |
3759 | r = service_demand_pid_file(s); | |
b13ddbbc | 3760 | if (r < 0 || cgroup_good(s) == 0) |
c3fda31d | 3761 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); |
3a111838 MS |
3762 | break; |
3763 | } | |
034c6ed7 | 3764 | } else |
783e05d6 | 3765 | service_search_main_pid(s); |
034c6ed7 | 3766 | |
3a111838 | 3767 | service_enter_start_post(s); |
034c6ed7 LP |
3768 | break; |
3769 | ||
3770 | case SERVICE_START_POST: | |
f42806df | 3771 | if (f != SERVICE_SUCCESS) { |
ce359e98 | 3772 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
2096e009 | 3773 | break; |
034c6ed7 LP |
3774 | } |
3775 | ||
2096e009 | 3776 | if (s->pid_file) { |
f42806df LP |
3777 | int r; |
3778 | ||
3779 | r = service_load_pid_file(s, true); | |
2096e009 MS |
3780 | if (r < 0) { |
3781 | r = service_demand_pid_file(s); | |
b13ddbbc | 3782 | if (r < 0 || cgroup_good(s) == 0) |
c35755fb | 3783 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
2096e009 MS |
3784 | break; |
3785 | } | |
3786 | } else | |
783e05d6 | 3787 | service_search_main_pid(s); |
2096e009 | 3788 | |
f42806df | 3789 | service_enter_running(s, SERVICE_SUCCESS); |
3185a36b | 3790 | break; |
034c6ed7 LP |
3791 | |
3792 | case SERVICE_RELOAD: | |
7236ce6e ZJS |
3793 | if (f == SERVICE_SUCCESS) |
3794 | if (service_load_pid_file(s, true) < 0) | |
3795 | service_search_main_pid(s); | |
3185a36b | 3796 | |
f42806df LP |
3797 | s->reload_result = f; |
3798 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
3799 | break; |
3800 | ||
3801 | case SERVICE_STOP: | |
f42806df | 3802 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
3803 | break; |
3804 | ||
c87700a1 | 3805 | case SERVICE_STOP_WATCHDOG: |
034c6ed7 LP |
3806 | case SERVICE_STOP_SIGTERM: |
3807 | case SERVICE_STOP_SIGKILL: | |
3808 | if (main_pid_good(s) <= 0) | |
f42806df | 3809 | service_enter_stop_post(s, f); |
034c6ed7 | 3810 | |
846a07b5 | 3811 | /* If there is still a service process around, wait until |
034c6ed7 LP |
3812 | * that one quit, too */ |
3813 | break; | |
3814 | ||
3815 | case SERVICE_STOP_POST: | |
c1566ef0 AZ |
3816 | if (main_pid_good(s) <= 0) |
3817 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
3818 | break; | |
3819 | ||
bf760801 | 3820 | case SERVICE_FINAL_WATCHDOG: |
034c6ed7 LP |
3821 | case SERVICE_FINAL_SIGTERM: |
3822 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 LP |
3823 | if (main_pid_good(s) <= 0) |
3824 | service_enter_dead(s, f, true); | |
034c6ed7 LP |
3825 | break; |
3826 | ||
4c2f5842 LP |
3827 | case SERVICE_CLEANING: |
3828 | ||
3829 | if (s->clean_result == SERVICE_SUCCESS) | |
3830 | s->clean_result = f; | |
3831 | ||
3832 | service_enter_dead(s, SERVICE_SUCCESS, false); | |
3833 | break; | |
3834 | ||
034c6ed7 | 3835 | default: |
04499a70 | 3836 | assert_not_reached(); |
034c6ed7 LP |
3837 | } |
3838 | } | |
5cdabc8d LP |
3839 | } else /* Neither control nor main PID? If so, don't notify about anything */ |
3840 | notify_dbus = false; | |
c4e2ceae LP |
3841 | |
3842 | /* Notify clients about changed exit status */ | |
5cdabc8d LP |
3843 | if (notify_dbus) |
3844 | unit_add_to_dbus_queue(u); | |
a911bb9a | 3845 | |
846a07b5 FB |
3846 | /* We watch the main/control process otherwise we can't retrieve the unit they |
3847 | * belong to with cgroupv1. But if they are not our direct child, we won't get a | |
3848 | * SIGCHLD for them. Therefore we need to look for others to watch so we can | |
3849 | * detect when the cgroup becomes empty. Note that the control process is always | |
3850 | * our child so it's pointless to watch all other processes. */ | |
3851 | if (!control_pid_good(s)) | |
3852 | if (!s->main_pid_known || s->main_pid_alien) | |
3853 | (void) unit_enqueue_rewatch_pids(u); | |
034c6ed7 LP |
3854 | } |
3855 | ||
718db961 LP |
3856 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
3857 | Service *s = SERVICE(userdata); | |
034c6ed7 LP |
3858 | |
3859 | assert(s); | |
718db961 | 3860 | assert(source == s->timer_event_source); |
034c6ed7 LP |
3861 | |
3862 | switch (s->state) { | |
3863 | ||
31cd5f63 | 3864 | case SERVICE_CONDITION: |
034c6ed7 LP |
3865 | case SERVICE_START_PRE: |
3866 | case SERVICE_START: | |
3867 | case SERVICE_START_POST: | |
bf760801 JK |
3868 | switch (s->timeout_start_failure_mode) { |
3869 | ||
3870 | case SERVICE_TIMEOUT_TERMINATE: | |
3871 | log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", service_state_to_string(s->state)); | |
3872 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
3873 | break; | |
3874 | ||
3875 | case SERVICE_TIMEOUT_ABORT: | |
3876 | log_unit_warning(UNIT(s), "%s operation timed out. Aborting.", service_state_to_string(s->state)); | |
3877 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3878 | break; | |
3879 | ||
3880 | case SERVICE_TIMEOUT_KILL: | |
3881 | if (s->kill_context.send_sigkill) { | |
3882 | log_unit_warning(UNIT(s), "%s operation timed out. Killing.", service_state_to_string(s->state)); | |
3883 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3884 | } else { | |
3885 | log_unit_warning(UNIT(s), "%s operation timed out. Skipping SIGKILL.", service_state_to_string(s->state)); | |
3886 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3887 | } | |
3888 | break; | |
3889 | ||
3890 | default: | |
04499a70 | 3891 | assert_not_reached(); |
bf760801 | 3892 | } |
034c6ed7 LP |
3893 | break; |
3894 | ||
36c16a7c LP |
3895 | case SERVICE_RUNNING: |
3896 | log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping."); | |
3897 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); | |
3898 | break; | |
3899 | ||
e2f3b44c | 3900 | case SERVICE_RELOAD: |
089b64d5 | 3901 | log_unit_warning(UNIT(s), "Reload operation timed out. Killing reload process."); |
e9a4f676 | 3902 | service_kill_control_process(s); |
f42806df LP |
3903 | s->reload_result = SERVICE_FAILURE_TIMEOUT; |
3904 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c LP |
3905 | break; |
3906 | ||
034c6ed7 | 3907 | case SERVICE_STOP: |
bf760801 JK |
3908 | switch (s->timeout_stop_failure_mode) { |
3909 | ||
3910 | case SERVICE_TIMEOUT_TERMINATE: | |
3911 | log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); | |
3912 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
3913 | break; | |
3914 | ||
3915 | case SERVICE_TIMEOUT_ABORT: | |
3916 | log_unit_warning(UNIT(s), "Stopping timed out. Aborting."); | |
3917 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3918 | break; | |
3919 | ||
3920 | case SERVICE_TIMEOUT_KILL: | |
3921 | if (s->kill_context.send_sigkill) { | |
3922 | log_unit_warning(UNIT(s), "Stopping timed out. Killing."); | |
3923 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3924 | } else { | |
3925 | log_unit_warning(UNIT(s), "Stopping timed out. Skipping SIGKILL."); | |
3926 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3927 | } | |
3928 | break; | |
3929 | ||
3930 | default: | |
04499a70 | 3931 | assert_not_reached(); |
bf760801 | 3932 | } |
034c6ed7 LP |
3933 | break; |
3934 | ||
c87700a1 | 3935 | case SERVICE_STOP_WATCHDOG: |
bf760801 JK |
3936 | if (s->kill_context.send_sigkill) { |
3937 | log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Killing."); | |
3938 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3939 | } else { | |
3940 | log_unit_warning(UNIT(s), "State 'stop-watchdog' timed out. Skipping SIGKILL."); | |
3941 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); | |
3942 | } | |
db2cb23b UTL |
3943 | break; |
3944 | ||
034c6ed7 | 3945 | case SERVICE_STOP_SIGTERM: |
bf760801 JK |
3946 | if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { |
3947 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Aborting."); | |
3948 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3949 | } else if (s->kill_context.send_sigkill) { | |
f2341e0a | 3950 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing."); |
f42806df | 3951 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 3952 | } else { |
f2341e0a | 3953 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL."); |
f42806df | 3954 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
ba035df2 LP |
3955 | } |
3956 | ||
034c6ed7 LP |
3957 | break; |
3958 | ||
3959 | case SERVICE_STOP_SIGKILL: | |
35b8ca3a | 3960 | /* Uh, we sent a SIGKILL and it is still not gone? |
034c6ed7 LP |
3961 | * Must be something we cannot kill, so let's just be |
3962 | * weirded out and continue */ | |
3963 | ||
f2341e0a | 3964 | log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); |
f42806df | 3965 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
3966 | break; |
3967 | ||
3968 | case SERVICE_STOP_POST: | |
bf760801 JK |
3969 | switch (s->timeout_stop_failure_mode) { |
3970 | ||
3971 | case SERVICE_TIMEOUT_TERMINATE: | |
3972 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating."); | |
3973 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
3974 | break; | |
3975 | ||
3976 | case SERVICE_TIMEOUT_ABORT: | |
3977 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Aborting."); | |
3978 | service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
3979 | break; | |
3980 | ||
3981 | case SERVICE_TIMEOUT_KILL: | |
3982 | if (s->kill_context.send_sigkill) { | |
3983 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Killing."); | |
3984 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
3985 | } else { | |
3986 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Skipping SIGKILL. Entering failed mode."); | |
3987 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
3988 | } | |
3989 | break; | |
3990 | ||
3991 | default: | |
04499a70 | 3992 | assert_not_reached(); |
bf760801 | 3993 | } |
034c6ed7 LP |
3994 | break; |
3995 | ||
bf760801 | 3996 | case SERVICE_FINAL_WATCHDOG: |
4819ff03 | 3997 | if (s->kill_context.send_sigkill) { |
bf760801 | 3998 | log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Killing."); |
f42806df | 3999 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 4000 | } else { |
bf760801 JK |
4001 | log_unit_warning(UNIT(s), "State 'final-watchdog' timed out. Skipping SIGKILL. Entering failed mode."); |
4002 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); | |
4003 | } | |
4004 | break; | |
4005 | ||
4006 | case SERVICE_FINAL_SIGTERM: | |
4007 | if (s->timeout_stop_failure_mode == SERVICE_TIMEOUT_ABORT) { | |
4008 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Aborting."); | |
4009 | service_enter_signal(s, SERVICE_FINAL_WATCHDOG, SERVICE_FAILURE_TIMEOUT); | |
4010 | } else if (s->kill_context.send_sigkill) { | |
4011 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Killing."); | |
4012 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); | |
4013 | } else { | |
4014 | log_unit_warning(UNIT(s), "State 'final-sigterm' timed out. Skipping SIGKILL. Entering failed mode."); | |
f42806df | 4015 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
ba035df2 LP |
4016 | } |
4017 | ||
034c6ed7 LP |
4018 | break; |
4019 | ||
4020 | case SERVICE_FINAL_SIGKILL: | |
f2341e0a | 4021 | log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode."); |
f42806df | 4022 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
034c6ed7 LP |
4023 | break; |
4024 | ||
4025 | case SERVICE_AUTO_RESTART: | |
5291f26d | 4026 | if (s->restart_usec > 0) |
868f7d36 ZJS |
4027 | log_unit_debug(UNIT(s), |
4028 | "Service RestartSec=%s expired, scheduling restart.", | |
5291f26d ZJS |
4029 | FORMAT_TIMESPAN(s->restart_usec, USEC_PER_SEC)); |
4030 | else | |
868f7d36 ZJS |
4031 | log_unit_debug(UNIT(s), |
4032 | "Service has no hold-off time (RestartSec=0), scheduling restart."); | |
5ce6e7f5 | 4033 | |
034c6ed7 LP |
4034 | service_enter_restart(s); |
4035 | break; | |
4036 | ||
4c2f5842 LP |
4037 | case SERVICE_CLEANING: |
4038 | log_unit_warning(UNIT(s), "Cleaning timed out. killing."); | |
4039 | ||
4040 | if (s->clean_result == SERVICE_SUCCESS) | |
4041 | s->clean_result = SERVICE_FAILURE_TIMEOUT; | |
4042 | ||
4043 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, 0); | |
4044 | break; | |
4045 | ||
034c6ed7 | 4046 | default: |
04499a70 | 4047 | assert_not_reached(); |
034c6ed7 | 4048 | } |
718db961 LP |
4049 | |
4050 | return 0; | |
4051 | } | |
4052 | ||
4053 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { | |
4054 | Service *s = SERVICE(userdata); | |
2787d83c | 4055 | usec_t watchdog_usec; |
718db961 LP |
4056 | |
4057 | assert(s); | |
4058 | assert(source == s->watchdog_event_source); | |
4059 | ||
2787d83c M |
4060 | watchdog_usec = service_get_watchdog_usec(s); |
4061 | ||
2a12e32e JK |
4062 | if (UNIT(s)->manager->service_watchdogs) { |
4063 | log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!", | |
5291f26d | 4064 | FORMAT_TIMESPAN(watchdog_usec, 1)); |
8bb2d17d | 4065 | |
c87700a1 | 4066 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); |
2a12e32e JK |
4067 | } else |
4068 | log_unit_warning(UNIT(s), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!", | |
5291f26d | 4069 | FORMAT_TIMESPAN(watchdog_usec, 1)); |
842129f5 | 4070 | |
718db961 | 4071 | return 0; |
5cb5a6ff LP |
4072 | } |
4073 | ||
fcee2755 | 4074 | static bool service_notify_message_authorized(Service *s, pid_t pid, FDSet *fds) { |
e3285237 | 4075 | assert(s); |
8c47c732 | 4076 | |
c952c6ec | 4077 | if (s->notify_access == NOTIFY_NONE) { |
e3285237 LP |
4078 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception is disabled.", pid); |
4079 | return false; | |
4080 | } | |
4081 | ||
4082 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { | |
336c6e46 | 4083 | if (s->main_pid != 0) |
e3285237 | 4084 | 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 | 4085 | else |
e3285237 LP |
4086 | 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); |
4087 | ||
4088 | return false; | |
4089 | } | |
4090 | ||
4091 | if (s->notify_access == NOTIFY_EXEC && pid != s->main_pid && pid != s->control_pid) { | |
6375bd20 | 4092 | if (s->main_pid != 0 && s->control_pid != 0) |
e3285237 | 4093 | 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 |
4094 | pid, s->main_pid, s->control_pid); |
4095 | else if (s->main_pid != 0) | |
e3285237 | 4096 | 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 | 4097 | else if (s->control_pid != 0) |
e3285237 | 4098 | 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 | 4099 | else |
e3285237 LP |
4100 | 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); |
4101 | ||
4102 | return false; | |
9711848f LP |
4103 | } |
4104 | ||
e3285237 LP |
4105 | return true; |
4106 | } | |
4107 | ||
99b43caf JK |
4108 | static void service_force_watchdog(Service *s) { |
4109 | if (!UNIT(s)->manager->service_watchdogs) | |
4110 | return; | |
4111 | ||
4112 | log_unit_error(UNIT(s), "Watchdog request (last status: %s)!", | |
4113 | s->status_text ? s->status_text : "<unset>"); | |
4114 | ||
4115 | service_enter_signal(s, SERVICE_STOP_WATCHDOG, SERVICE_FAILURE_WATCHDOG); | |
4116 | } | |
4117 | ||
db256aab LP |
4118 | static void service_notify_message( |
4119 | Unit *u, | |
4120 | const struct ucred *ucred, | |
fcee2755 | 4121 | char * const *tags, |
db256aab LP |
4122 | FDSet *fds) { |
4123 | ||
e3285237 LP |
4124 | Service *s = SERVICE(u); |
4125 | bool notify_dbus = false; | |
4126 | const char *e; | |
fcee2755 | 4127 | char * const *i; |
db256aab | 4128 | int r; |
e3285237 LP |
4129 | |
4130 | assert(u); | |
db256aab | 4131 | assert(ucred); |
e3285237 | 4132 | |
fcee2755 | 4133 | if (!service_notify_message_authorized(SERVICE(u), ucred->pid, fds)) |
e3285237 LP |
4134 | return; |
4135 | ||
f1d34068 | 4136 | if (DEBUG_LOGGING) { |
9711848f LP |
4137 | _cleanup_free_ char *cc = NULL; |
4138 | ||
4139 | cc = strv_join(tags, ", "); | |
db256aab | 4140 | log_unit_debug(u, "Got notification message from PID "PID_FMT" (%s)", ucred->pid, isempty(cc) ? "n/a" : cc); |
9711848f | 4141 | } |
c952c6ec | 4142 | |
8c47c732 | 4143 | /* Interpret MAINPID= */ |
28849dba | 4144 | e = strv_find_startswith(tags, "MAINPID="); |
5e56b378 | 4145 | if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) { |
db256aab LP |
4146 | pid_t new_main_pid; |
4147 | ||
4148 | if (parse_pid(e, &new_main_pid) < 0) | |
4149 | log_unit_warning(u, "Failed to parse MAINPID= field in notification message, ignoring: %s", e); | |
4150 | else if (!s->main_pid_known || new_main_pid != s->main_pid) { | |
4151 | ||
4152 | r = service_is_suitable_main_pid(s, new_main_pid, LOG_WARNING); | |
4153 | if (r == 0) { | |
5238e957 | 4154 | /* The new main PID is a bit suspicious, which is OK if the sender is privileged. */ |
db256aab LP |
4155 | |
4156 | if (ucred->uid == 0) { | |
4157 | 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); | |
4158 | r = 1; | |
4159 | } else | |
4160 | log_unit_debug(u, "New main PID "PID_FMT" does not belong to service, refusing.", new_main_pid); | |
4161 | } | |
4162 | if (r > 0) { | |
4163 | service_set_main_pid(s, new_main_pid); | |
cdc2af3e | 4164 | |
f75f613d | 4165 | r = unit_watch_pid(UNIT(s), new_main_pid, false); |
cdc2af3e LP |
4166 | if (r < 0) |
4167 | log_unit_warning_errno(UNIT(s), r, "Failed to watch new main PID "PID_FMT" for service: %m", new_main_pid); | |
4168 | ||
db256aab LP |
4169 | notify_dbus = true; |
4170 | } | |
8c47c732 LP |
4171 | } |
4172 | } | |
4173 | ||
cc2b7b11 LP |
4174 | /* Interpret READY=/STOPPING=/RELOADING=. Last one wins. */ |
4175 | STRV_FOREACH_BACKWARDS(i, tags) { | |
308d72dc | 4176 | |
cc2b7b11 LP |
4177 | if (streq(*i, "READY=1")) { |
4178 | s->notify_state = NOTIFY_READY; | |
308d72dc | 4179 | |
cc2b7b11 LP |
4180 | /* Type=notify services inform us about completed |
4181 | * initialization with READY=1 */ | |
4182 | if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START) | |
4183 | service_enter_start_post(s); | |
308d72dc | 4184 | |
cc2b7b11 LP |
4185 | /* Sending READY=1 while we are reloading informs us |
4186 | * that the reloading is complete */ | |
4187 | if (s->state == SERVICE_RELOAD && s->control_pid == 0) | |
4188 | service_enter_running(s, SERVICE_SUCCESS); | |
308d72dc | 4189 | |
cc2b7b11 LP |
4190 | notify_dbus = true; |
4191 | break; | |
308d72dc | 4192 | |
cc2b7b11 LP |
4193 | } else if (streq(*i, "RELOADING=1")) { |
4194 | s->notify_state = NOTIFY_RELOADING; | |
308d72dc | 4195 | |
cc2b7b11 LP |
4196 | if (s->state == SERVICE_RUNNING) |
4197 | service_enter_reload_by_notify(s); | |
308d72dc | 4198 | |
cc2b7b11 LP |
4199 | notify_dbus = true; |
4200 | break; | |
308d72dc | 4201 | |
cc2b7b11 LP |
4202 | } else if (streq(*i, "STOPPING=1")) { |
4203 | s->notify_state = NOTIFY_STOPPING; | |
308d72dc | 4204 | |
cc2b7b11 LP |
4205 | if (s->state == SERVICE_RUNNING) |
4206 | service_enter_stop_by_notify(s); | |
308d72dc | 4207 | |
cc2b7b11 LP |
4208 | notify_dbus = true; |
4209 | break; | |
4210 | } | |
8c47c732 LP |
4211 | } |
4212 | ||
4213 | /* Interpret STATUS= */ | |
28849dba | 4214 | e = strv_find_startswith(tags, "STATUS="); |
7f110ff9 | 4215 | if (e) { |
28849dba | 4216 | _cleanup_free_ char *t = NULL; |
8c47c732 | 4217 | |
28849dba | 4218 | if (!isempty(e)) { |
3eac1bca LP |
4219 | /* Note that this size limit check is mostly paranoia: since the datagram size we are willing |
4220 | * to process is already limited to NOTIFY_BUFFER_MAX, this limit here should never be hit. */ | |
4221 | if (strlen(e) > STATUS_TEXT_MAX) | |
4222 | log_unit_warning(u, "Status message overly long (%zu > %u), ignoring.", strlen(e), STATUS_TEXT_MAX); | |
4223 | else if (!utf8_is_valid(e)) | |
4224 | log_unit_warning(u, "Status message in notification message is not UTF-8 clean, ignoring."); | |
28849dba | 4225 | else { |
28849dba LP |
4226 | t = strdup(e); |
4227 | if (!t) | |
4228 | log_oom(); | |
3a2776bc | 4229 | } |
28849dba | 4230 | } |
8c47c732 | 4231 | |
30b5275a | 4232 | if (!streq_ptr(s->status_text, t)) { |
3b319885 | 4233 | free_and_replace(s->status_text, t); |
30b5275a | 4234 | notify_dbus = true; |
28849dba | 4235 | } |
8c47c732 | 4236 | } |
842129f5 | 4237 | |
4774e357 | 4238 | /* Interpret ERRNO= */ |
28849dba | 4239 | e = strv_find_startswith(tags, "ERRNO="); |
4774e357 MAA |
4240 | if (e) { |
4241 | int status_errno; | |
4242 | ||
2fa40742 LP |
4243 | status_errno = parse_errno(e); |
4244 | if (status_errno < 0) | |
4245 | log_unit_warning_errno(u, status_errno, | |
5e1ee764 | 4246 | "Failed to parse ERRNO= field value '%s' in notification message: %m", e); |
2fa40742 LP |
4247 | else if (s->status_errno != status_errno) { |
4248 | s->status_errno = status_errno; | |
4249 | notify_dbus = true; | |
4774e357 MAA |
4250 | } |
4251 | } | |
4252 | ||
a327431b DB |
4253 | /* Interpret EXTEND_TIMEOUT= */ |
4254 | e = strv_find_startswith(tags, "EXTEND_TIMEOUT_USEC="); | |
4255 | if (e) { | |
4256 | usec_t extend_timeout_usec; | |
4257 | if (safe_atou64(e, &extend_timeout_usec) < 0) | |
4258 | log_unit_warning(u, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e); | |
4259 | else | |
4260 | service_extend_timeout(s, extend_timeout_usec); | |
4261 | } | |
4262 | ||
6f285378 | 4263 | /* Interpret WATCHDOG= */ |
99b43caf JK |
4264 | e = strv_find_startswith(tags, "WATCHDOG="); |
4265 | if (e) { | |
4266 | if (streq(e, "1")) | |
4267 | service_reset_watchdog(s); | |
4268 | else if (streq(e, "trigger")) | |
4269 | service_force_watchdog(s); | |
4270 | else | |
4271 | log_unit_warning(u, "Passed WATCHDOG= field is invalid, ignoring."); | |
4272 | } | |
c4e2ceae | 4273 | |
c45d11cb LP |
4274 | e = strv_find_startswith(tags, "WATCHDOG_USEC="); |
4275 | if (e) { | |
4276 | usec_t watchdog_override_usec; | |
4277 | if (safe_atou64(e, &watchdog_override_usec) < 0) | |
4278 | log_unit_warning(u, "Failed to parse WATCHDOG_USEC=%s", e); | |
4279 | else | |
95d0d8ed | 4280 | service_override_watchdog_timeout(s, watchdog_override_usec); |
c45d11cb LP |
4281 | } |
4282 | ||
e78ee06d LP |
4283 | /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases, |
4284 | * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing | |
4285 | * fds, but optional when pushing in new fds, for compatibility reasons. */ | |
4286 | if (strv_find(tags, "FDSTOREREMOVE=1")) { | |
4287 | const char *name; | |
4288 | ||
4289 | name = strv_find_startswith(tags, "FDNAME="); | |
4290 | if (!name || !fdname_is_valid(name)) | |
4291 | log_unit_warning(u, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring."); | |
4292 | else | |
4293 | service_remove_fd_store(s, name); | |
4294 | ||
4295 | } else if (strv_find(tags, "FDSTORE=1")) { | |
8dd4c05b LP |
4296 | const char *name; |
4297 | ||
4298 | name = strv_find_startswith(tags, "FDNAME="); | |
4299 | if (name && !fdname_is_valid(name)) { | |
4300 | log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring."); | |
4301 | name = NULL; | |
4302 | } | |
4303 | ||
cb5a46b8 | 4304 | (void) service_add_fd_store_set(s, fds, name, !strv_contains(tags, "FDPOLL=0")); |
8dd4c05b | 4305 | } |
a354329f | 4306 | |
c4e2ceae | 4307 | /* Notify clients about changed status or main pid */ |
30b5275a LP |
4308 | if (notify_dbus) |
4309 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
4310 | } |
4311 | ||
7a7821c8 | 4312 | static int service_get_timeout(Unit *u, usec_t *timeout) { |
68db7a3b | 4313 | Service *s = SERVICE(u); |
7a7821c8 | 4314 | uint64_t t; |
68db7a3b ZJS |
4315 | int r; |
4316 | ||
4317 | if (!s->timer_event_source) | |
4318 | return 0; | |
4319 | ||
7a7821c8 | 4320 | r = sd_event_source_get_time(s->timer_event_source, &t); |
68db7a3b ZJS |
4321 | if (r < 0) |
4322 | return r; | |
7a7821c8 LP |
4323 | if (t == USEC_INFINITY) |
4324 | return 0; | |
68db7a3b | 4325 | |
7a7821c8 | 4326 | *timeout = t; |
68db7a3b ZJS |
4327 | return 1; |
4328 | } | |
4329 | ||
fc67a943 | 4330 | static void service_bus_name_owner_change(Unit *u, const char *new_owner) { |
05e343b7 LP |
4331 | |
4332 | Service *s = SERVICE(u); | |
718db961 | 4333 | int r; |
05e343b7 LP |
4334 | |
4335 | assert(s); | |
05e343b7 | 4336 | |
fc67a943 LP |
4337 | if (new_owner) |
4338 | log_unit_debug(u, "D-Bus name %s now owned by %s", s->bus_name, new_owner); | |
05e343b7 | 4339 | else |
fc67a943 | 4340 | log_unit_debug(u, "D-Bus name %s now not owned by anyone.", s->bus_name); |
05e343b7 | 4341 | |
d7a0f1f4 | 4342 | s->bus_name_good = new_owner; |
05e343b7 | 4343 | |
d8ccf5fd DM |
4344 | /* Track the current owner, so we can reconstruct changes after a daemon reload */ |
4345 | r = free_and_strdup(&s->bus_name_owner, new_owner); | |
4346 | if (r < 0) { | |
4347 | log_unit_error_errno(u, r, "Unable to set new bus name owner %s: %m", new_owner); | |
4348 | return; | |
4349 | } | |
4350 | ||
05e343b7 LP |
4351 | if (s->type == SERVICE_DBUS) { |
4352 | ||
4353 | /* service_enter_running() will figure out what to | |
4354 | * do */ | |
4355 | if (s->state == SERVICE_RUNNING) | |
f42806df | 4356 | service_enter_running(s, SERVICE_SUCCESS); |
05e343b7 LP |
4357 | else if (s->state == SERVICE_START && new_owner) |
4358 | service_enter_start_post(s); | |
4359 | ||
4360 | } else if (new_owner && | |
4361 | s->main_pid <= 0 && | |
a6951a50 LP |
4362 | IN_SET(s->state, |
4363 | SERVICE_START, | |
4364 | SERVICE_START_POST, | |
4365 | SERVICE_RUNNING, | |
4366 | SERVICE_RELOAD)) { | |
05e343b7 | 4367 | |
4afd3348 | 4368 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
718db961 | 4369 | pid_t pid; |
05e343b7 | 4370 | |
718db961 | 4371 | /* Try to acquire PID from bus service */ |
05e343b7 | 4372 | |
a5a8776a | 4373 | r = sd_bus_get_name_creds(u->manager->api_bus, s->bus_name, SD_BUS_CREDS_PID, &creds); |
5b12334d LP |
4374 | if (r >= 0) |
4375 | r = sd_bus_creds_get_pid(creds, &pid); | |
718db961 | 4376 | if (r >= 0) { |
a5a8776a | 4377 | log_unit_debug(u, "D-Bus name %s is now owned by process " PID_FMT, s->bus_name, pid); |
05e343b7 | 4378 | |
718db961 | 4379 | service_set_main_pid(s, pid); |
f75f613d | 4380 | unit_watch_pid(UNIT(s), pid, false); |
718db961 | 4381 | } |
7400b9d2 | 4382 | } |
05e343b7 LP |
4383 | } |
4384 | ||
3fabebf4 LP |
4385 | int service_set_socket_fd( |
4386 | Service *s, | |
4387 | int fd, | |
4388 | Socket *sock, | |
4389 | SocketPeer *peer, | |
4390 | bool selinux_context_net) { | |
4391 | ||
4392 | _cleanup_free_ char *peer_text = NULL; | |
79a98c60 | 4393 | int r; |
57020a3a | 4394 | |
4f2d528d LP |
4395 | assert(s); |
4396 | assert(fd >= 0); | |
4397 | ||
7f2fbbff LP |
4398 | /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs |
4399 | * to be configured. We take ownership of the passed fd on success. */ | |
4f2d528d | 4400 | |
1124fe6f | 4401 | if (UNIT(s)->load_state != UNIT_LOADED) |
4f2d528d LP |
4402 | return -EINVAL; |
4403 | ||
4404 | if (s->socket_fd >= 0) | |
4405 | return -EBUSY; | |
4406 | ||
3fabebf4 LP |
4407 | assert(!s->socket_peer); |
4408 | ||
4f2d528d LP |
4409 | if (s->state != SERVICE_DEAD) |
4410 | return -EAGAIN; | |
4411 | ||
3fabebf4 | 4412 | if (getpeername_pretty(fd, true, &peer_text) >= 0) { |
79a98c60 LP |
4413 | |
4414 | if (UNIT(s)->description) { | |
c2b2df60 | 4415 | _cleanup_free_ char *a = NULL; |
79a98c60 | 4416 | |
3fabebf4 | 4417 | a = strjoin(UNIT(s)->description, " (", peer_text, ")"); |
79a98c60 LP |
4418 | if (!a) |
4419 | return -ENOMEM; | |
4420 | ||
4421 | r = unit_set_description(UNIT(s), a); | |
4422 | } else | |
3fabebf4 | 4423 | r = unit_set_description(UNIT(s), peer_text); |
79a98c60 LP |
4424 | if (r < 0) |
4425 | return r; | |
4426 | } | |
4427 | ||
eef85c4a | 4428 | r = unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false, UNIT_DEPENDENCY_IMPLICIT); |
7f2fbbff LP |
4429 | if (r < 0) |
4430 | return r; | |
4431 | ||
4f2d528d | 4432 | s->socket_fd = fd; |
3fabebf4 | 4433 | s->socket_peer = socket_peer_ref(peer); |
16115b0a | 4434 | s->socket_fd_selinux_context_net = selinux_context_net; |
6cf6bbc2 | 4435 | |
7f7d01ed | 4436 | unit_ref_set(&s->accept_socket, UNIT(s), UNIT(sock)); |
7f2fbbff | 4437 | return 0; |
4f2d528d LP |
4438 | } |
4439 | ||
fdf20a31 | 4440 | static void service_reset_failed(Unit *u) { |
5632e374 LP |
4441 | Service *s = SERVICE(u); |
4442 | ||
4443 | assert(s); | |
4444 | ||
fdf20a31 | 4445 | if (s->state == SERVICE_FAILED) |
5632e374 LP |
4446 | service_set_state(s, SERVICE_DEAD); |
4447 | ||
f42806df LP |
4448 | s->result = SERVICE_SUCCESS; |
4449 | s->reload_result = SERVICE_SUCCESS; | |
4c2f5842 | 4450 | s->clean_result = SERVICE_SUCCESS; |
7a0019d3 LP |
4451 | s->n_restarts = 0; |
4452 | s->flush_n_restarts = false; | |
5632e374 LP |
4453 | } |
4454 | ||
718db961 | 4455 | static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
8a0867d6 | 4456 | Service *s = SERVICE(u); |
41efeaec | 4457 | |
a6951a50 LP |
4458 | assert(s); |
4459 | ||
814cc562 | 4460 | return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error); |
8a0867d6 LP |
4461 | } |
4462 | ||
291d565a LP |
4463 | static int service_main_pid(Unit *u) { |
4464 | Service *s = SERVICE(u); | |
4465 | ||
4466 | assert(s); | |
4467 | ||
4468 | return s->main_pid; | |
4469 | } | |
4470 | ||
4471 | static int service_control_pid(Unit *u) { | |
4472 | Service *s = SERVICE(u); | |
4473 | ||
4474 | assert(s); | |
4475 | ||
4476 | return s->control_pid; | |
4477 | } | |
4478 | ||
bb2c7685 LP |
4479 | static bool service_needs_console(Unit *u) { |
4480 | Service *s = SERVICE(u); | |
4481 | ||
4482 | assert(s); | |
4483 | ||
4484 | /* We provide our own implementation of this here, instead of relying of the generic implementation | |
4485 | * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */ | |
4486 | ||
4487 | if (!exec_context_may_touch_console(&s->exec_context)) | |
4488 | return false; | |
4489 | ||
4490 | return IN_SET(s->state, | |
31cd5f63 | 4491 | SERVICE_CONDITION, |
bb2c7685 LP |
4492 | SERVICE_START_PRE, |
4493 | SERVICE_START, | |
4494 | SERVICE_START_POST, | |
4495 | SERVICE_RUNNING, | |
4496 | SERVICE_RELOAD, | |
4497 | SERVICE_STOP, | |
c87700a1 | 4498 | SERVICE_STOP_WATCHDOG, |
bb2c7685 LP |
4499 | SERVICE_STOP_SIGTERM, |
4500 | SERVICE_STOP_SIGKILL, | |
4501 | SERVICE_STOP_POST, | |
bf760801 | 4502 | SERVICE_FINAL_WATCHDOG, |
bb2c7685 LP |
4503 | SERVICE_FINAL_SIGTERM, |
4504 | SERVICE_FINAL_SIGKILL); | |
4505 | } | |
4506 | ||
7af67e9a LP |
4507 | static int service_exit_status(Unit *u) { |
4508 | Service *s = SERVICE(u); | |
4509 | ||
4510 | assert(u); | |
4511 | ||
4512 | if (s->main_exec_status.pid <= 0 || | |
4513 | !dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) | |
4514 | return -ENODATA; | |
4515 | ||
4516 | if (s->main_exec_status.code != CLD_EXITED) | |
4517 | return -EBADE; | |
4518 | ||
4519 | return s->main_exec_status.status; | |
4520 | } | |
4521 | ||
5e1669ff ZJS |
4522 | static const char* service_status_text(Unit *u) { |
4523 | Service *s = SERVICE(u); | |
4524 | ||
4525 | assert(s); | |
4526 | ||
4527 | return s->status_text; | |
4528 | } | |
4529 | ||
4c2f5842 LP |
4530 | static int service_clean(Unit *u, ExecCleanMask mask) { |
4531 | _cleanup_strv_free_ char **l = NULL; | |
4532 | Service *s = SERVICE(u); | |
4c2f5842 LP |
4533 | int r; |
4534 | ||
4535 | assert(s); | |
4536 | assert(mask != 0); | |
4537 | ||
4538 | if (s->state != SERVICE_DEAD) | |
4539 | return -EBUSY; | |
4540 | ||
4541 | r = exec_context_get_clean_directories(&s->exec_context, u->manager->prefix, mask, &l); | |
4542 | if (r < 0) | |
4543 | return r; | |
4544 | ||
4545 | if (strv_isempty(l)) | |
4546 | return -EUNATCH; | |
4547 | ||
4548 | service_unwatch_control_pid(s); | |
4549 | s->clean_result = SERVICE_SUCCESS; | |
4550 | s->control_command = NULL; | |
4551 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
4552 | ||
12213aed | 4553 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->exec_context.timeout_clean_usec)); |
4c2f5842 LP |
4554 | if (r < 0) |
4555 | goto fail; | |
4556 | ||
810ef318 | 4557 | r = unit_fork_and_watch_rm_rf(u, l, &s->control_pid); |
4c2f5842 LP |
4558 | if (r < 0) |
4559 | goto fail; | |
4560 | ||
4c2f5842 LP |
4561 | service_set_state(s, SERVICE_CLEANING); |
4562 | ||
4563 | return 0; | |
4564 | ||
4565 | fail: | |
810ef318 | 4566 | log_unit_warning_errno(u, r, "Failed to initiate cleaning: %m"); |
4c2f5842 | 4567 | s->clean_result = SERVICE_FAILURE_RESOURCES; |
5dcadb4c | 4568 | s->timer_event_source = sd_event_source_disable_unref(s->timer_event_source); |
4c2f5842 LP |
4569 | return r; |
4570 | } | |
4571 | ||
4572 | static int service_can_clean(Unit *u, ExecCleanMask *ret) { | |
4573 | Service *s = SERVICE(u); | |
4574 | ||
4575 | assert(s); | |
4576 | ||
4577 | return exec_context_get_clean_mask(&s->exec_context, ret); | |
4578 | } | |
4579 | ||
eda0cbf0 | 4580 | static const char *service_finished_job(Unit *u, JobType t, JobResult result) { |
04d232d8 ZJS |
4581 | if (t == JOB_START && |
4582 | result == JOB_DONE && | |
4583 | SERVICE(u)->type == SERVICE_ONESHOT) | |
4584 | return "Finished %s."; | |
eda0cbf0 ZJS |
4585 | |
4586 | /* Fall back to generic */ | |
4587 | return NULL; | |
4588 | } | |
4589 | ||
705578c3 | 4590 | static int service_can_start(Unit *u) { |
9727f242 DDM |
4591 | Service *s = SERVICE(u); |
4592 | int r; | |
4593 | ||
4594 | assert(s); | |
4595 | ||
4596 | /* Make sure we don't enter a busy loop of some kind. */ | |
4597 | r = unit_test_start_limit(u); | |
4598 | if (r < 0) { | |
4599 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false); | |
4600 | return r; | |
4601 | } | |
4602 | ||
705578c3 | 4603 | return 1; |
9727f242 DDM |
4604 | } |
4605 | ||
94f04347 | 4606 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { |
48d83e33 ZJS |
4607 | [SERVICE_RESTART_NO] = "no", |
4608 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
4609 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", | |
6cfe2fde | 4610 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
dc99a976 | 4611 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
48d83e33 ZJS |
4612 | [SERVICE_RESTART_ON_ABORT] = "on-abort", |
4613 | [SERVICE_RESTART_ALWAYS] = "always", | |
94f04347 LP |
4614 | }; |
4615 | ||
4616 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
4617 | ||
4618 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
48d83e33 | 4619 | [SERVICE_SIMPLE] = "simple", |
0d624a78 | 4620 | [SERVICE_FORKING] = "forking", |
34e9ba66 | 4621 | [SERVICE_ONESHOT] = "oneshot", |
48d83e33 ZJS |
4622 | [SERVICE_DBUS] = "dbus", |
4623 | [SERVICE_NOTIFY] = "notify", | |
4624 | [SERVICE_IDLE] = "idle", | |
4625 | [SERVICE_EXEC] = "exec", | |
94f04347 LP |
4626 | }; |
4627 | ||
4628 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
4629 | ||
596e4470 HC |
4630 | static const char* const service_exit_type_table[_SERVICE_EXIT_TYPE_MAX] = { |
4631 | [SERVICE_EXIT_MAIN] = "main", | |
4632 | [SERVICE_EXIT_CGROUP] = "cgroup", | |
4633 | }; | |
4634 | ||
4635 | DEFINE_STRING_TABLE_LOOKUP(service_exit_type, ServiceExitType); | |
4636 | ||
e537352b | 4637 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
48d83e33 ZJS |
4638 | [SERVICE_EXEC_CONDITION] = "ExecCondition", |
4639 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", | |
4640 | [SERVICE_EXEC_START] = "ExecStart", | |
94f04347 | 4641 | [SERVICE_EXEC_START_POST] = "ExecStartPost", |
48d83e33 ZJS |
4642 | [SERVICE_EXEC_RELOAD] = "ExecReload", |
4643 | [SERVICE_EXEC_STOP] = "ExecStop", | |
4644 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
94f04347 LP |
4645 | }; |
4646 | ||
4647 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
4648 | ||
b3d59367 | 4649 | static const char* const service_exec_ex_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
48d83e33 ZJS |
4650 | [SERVICE_EXEC_CONDITION] = "ExecConditionEx", |
4651 | [SERVICE_EXEC_START_PRE] = "ExecStartPreEx", | |
4652 | [SERVICE_EXEC_START] = "ExecStartEx", | |
b3d59367 | 4653 | [SERVICE_EXEC_START_POST] = "ExecStartPostEx", |
48d83e33 ZJS |
4654 | [SERVICE_EXEC_RELOAD] = "ExecReloadEx", |
4655 | [SERVICE_EXEC_STOP] = "ExecStopEx", | |
4656 | [SERVICE_EXEC_STOP_POST] = "ExecStopPostEx", | |
b3d59367 AZ |
4657 | }; |
4658 | ||
4659 | DEFINE_STRING_TABLE_LOOKUP(service_exec_ex_command, ServiceExecCommand); | |
4660 | ||
308d72dc | 4661 | static const char* const notify_state_table[_NOTIFY_STATE_MAX] = { |
48d83e33 ZJS |
4662 | [NOTIFY_UNKNOWN] = "unknown", |
4663 | [NOTIFY_READY] = "ready", | |
308d72dc | 4664 | [NOTIFY_RELOADING] = "reloading", |
48d83e33 | 4665 | [NOTIFY_STOPPING] = "stopping", |
308d72dc LP |
4666 | }; |
4667 | ||
4668 | DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); | |
4669 | ||
f42806df | 4670 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
48d83e33 ZJS |
4671 | [SERVICE_SUCCESS] = "success", |
4672 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
4673 | [SERVICE_FAILURE_PROTOCOL] = "protocol", | |
4674 | [SERVICE_FAILURE_TIMEOUT] = "timeout", | |
4675 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
4676 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
4677 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", | |
4678 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", | |
07299350 | 4679 | [SERVICE_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
48d83e33 ZJS |
4680 | [SERVICE_FAILURE_OOM_KILL] = "oom-kill", |
4681 | [SERVICE_SKIP_CONDITION] = "exec-condition", | |
f42806df LP |
4682 | }; |
4683 | ||
4684 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
4685 | ||
bf760801 JK |
4686 | static const char* const service_timeout_failure_mode_table[_SERVICE_TIMEOUT_FAILURE_MODE_MAX] = { |
4687 | [SERVICE_TIMEOUT_TERMINATE] = "terminate", | |
48d83e33 ZJS |
4688 | [SERVICE_TIMEOUT_ABORT] = "abort", |
4689 | [SERVICE_TIMEOUT_KILL] = "kill", | |
bf760801 JK |
4690 | }; |
4691 | ||
4692 | DEFINE_STRING_TABLE_LOOKUP(service_timeout_failure_mode, ServiceTimeoutFailureMode); | |
4693 | ||
87f0e418 | 4694 | const UnitVTable service_vtable = { |
7d17cfbc | 4695 | .object_size = sizeof(Service), |
718db961 LP |
4696 | .exec_context_offset = offsetof(Service, exec_context), |
4697 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
4698 | .kill_context_offset = offsetof(Service, kill_context), | |
613b411c | 4699 | .exec_runtime_offset = offsetof(Service, exec_runtime), |
29206d46 | 4700 | .dynamic_creds_offset = offsetof(Service, dynamic_creds), |
3ef63c31 | 4701 | |
f975e971 LP |
4702 | .sections = |
4703 | "Unit\0" | |
4704 | "Service\0" | |
4705 | "Install\0", | |
4ad49000 | 4706 | .private_section = "Service", |
71645aca | 4707 | |
1d9cc876 LP |
4708 | .can_transient = true, |
4709 | .can_delegate = true, | |
c80a9a33 | 4710 | .can_fail = true, |
4d824a4e | 4711 | .can_set_managed_oom = true, |
1d9cc876 | 4712 | |
034c6ed7 LP |
4713 | .init = service_init, |
4714 | .done = service_done, | |
a16e1123 | 4715 | .load = service_load, |
a354329f | 4716 | .release_resources = service_release_resources, |
a16e1123 LP |
4717 | |
4718 | .coldplug = service_coldplug, | |
034c6ed7 | 4719 | |
5cb5a6ff LP |
4720 | .dump = service_dump, |
4721 | ||
4722 | .start = service_start, | |
4723 | .stop = service_stop, | |
4724 | .reload = service_reload, | |
4725 | ||
034c6ed7 LP |
4726 | .can_reload = service_can_reload, |
4727 | ||
8a0867d6 | 4728 | .kill = service_kill, |
4c2f5842 LP |
4729 | .clean = service_clean, |
4730 | .can_clean = service_can_clean, | |
8a0867d6 | 4731 | |
d9e45bc3 MS |
4732 | .freeze = unit_freeze_vtable_common, |
4733 | .thaw = unit_thaw_vtable_common, | |
4734 | ||
a16e1123 LP |
4735 | .serialize = service_serialize, |
4736 | .deserialize_item = service_deserialize_item, | |
4737 | ||
5cb5a6ff | 4738 | .active_state = service_active_state, |
10a94420 | 4739 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 4740 | |
deb4e708 MK |
4741 | .will_restart = service_will_restart, |
4742 | ||
f2f725e5 | 4743 | .may_gc = service_may_gc, |
701cc384 | 4744 | |
034c6ed7 | 4745 | .sigchld_event = service_sigchld_event, |
2c4104f0 | 4746 | |
fdf20a31 | 4747 | .reset_failed = service_reset_failed, |
5632e374 | 4748 | |
4ad49000 | 4749 | .notify_cgroup_empty = service_notify_cgroup_empty_event, |
afcfaa69 | 4750 | .notify_cgroup_oom = service_notify_cgroup_oom_event, |
8c47c732 | 4751 | .notify_message = service_notify_message, |
8e274523 | 4752 | |
291d565a LP |
4753 | .main_pid = service_main_pid, |
4754 | .control_pid = service_control_pid, | |
4755 | ||
05e343b7 | 4756 | .bus_name_owner_change = service_bus_name_owner_change, |
05e343b7 | 4757 | |
74c964d3 LP |
4758 | .bus_set_property = bus_service_set_property, |
4759 | .bus_commit_properties = bus_service_commit_properties, | |
4139c1b2 | 4760 | |
68db7a3b | 4761 | .get_timeout = service_get_timeout, |
bb2c7685 | 4762 | .needs_console = service_needs_console, |
7af67e9a | 4763 | .exit_status = service_exit_status, |
5e1669ff | 4764 | .status_text = service_status_text, |
718db961 | 4765 | |
c6918296 | 4766 | .status_message_formats = { |
c6918296 | 4767 | .finished_start_job = { |
c6918296 | 4768 | [JOB_FAILED] = "Failed to start %s.", |
c6918296 MS |
4769 | }, |
4770 | .finished_stop_job = { | |
4771 | [JOB_DONE] = "Stopped %s.", | |
4772 | [JOB_FAILED] = "Stopped (with error) %s.", | |
c6918296 | 4773 | }, |
eda0cbf0 | 4774 | .finished_job = service_finished_job, |
c6918296 | 4775 | }, |
9727f242 | 4776 | |
705578c3 | 4777 | .can_start = service_can_start, |
5cb5a6ff | 4778 | }; |