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