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