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