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