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