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