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