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