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