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