]>
Commit | Line | Data |
---|---|---|
52ad194e | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
663996b3 MS |
2 | /*** |
3 | This file is part of systemd. | |
4 | ||
5 | Copyright 2010 Lennart Poettering | |
6 | ||
7 | systemd is free software; you can redistribute it and/or modify it | |
8 | under the terms of the GNU Lesser General Public License as published by | |
9 | the Free Software Foundation; either version 2.1 of the License, or | |
10 | (at your option) any later version. | |
11 | ||
12 | systemd is distributed in the hope that it will be useful, but | |
13 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
15 | Lesser General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU Lesser General Public License | |
18 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
19 | ***/ | |
20 | ||
21 | #include <errno.h> | |
22 | #include <signal.h> | |
663996b3 | 23 | #include <unistd.h> |
663996b3 | 24 | |
f5e65279 MB |
25 | #include "sd-messages.h" |
26 | ||
db2df898 | 27 | #include "alloc-util.h" |
60f067b4 | 28 | #include "async.h" |
db2df898 MP |
29 | #include "bus-error.h" |
30 | #include "bus-kernel.h" | |
31 | #include "bus-util.h" | |
663996b3 | 32 | #include "dbus-service.h" |
663996b3 | 33 | #include "def.h" |
663996b3 | 34 | #include "env-util.h" |
db2df898 MP |
35 | #include "escape.h" |
36 | #include "exit-status.h" | |
37 | #include "fd-util.h" | |
663996b3 | 38 | #include "fileio.h" |
2897b343 | 39 | #include "format-util.h" |
db2df898 MP |
40 | #include "fs-util.h" |
41 | #include "load-dropin.h" | |
42 | #include "load-fragment.h" | |
43 | #include "log.h" | |
44 | #include "manager.h" | |
45 | #include "parse-util.h" | |
46 | #include "path-util.h" | |
e3bff60a | 47 | #include "process-util.h" |
db2df898 | 48 | #include "service.h" |
86f210e9 | 49 | #include "signal-util.h" |
db2df898 | 50 | #include "special.h" |
81c58355 | 51 | #include "stdio-util.h" |
db2df898 MP |
52 | #include "string-table.h" |
53 | #include "string-util.h" | |
54 | #include "strv.h" | |
55 | #include "unit-name.h" | |
db2df898 MP |
56 | #include "unit.h" |
57 | #include "utf8.h" | |
58 | #include "util.h" | |
663996b3 MS |
59 | |
60 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { | |
61 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
62 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
63 | [SERVICE_START] = UNIT_ACTIVATING, | |
64 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
65 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
66 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
67 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
68 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
5eef597e | 69 | [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING, |
663996b3 MS |
70 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
71 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
72 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
73 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
74 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
75 | [SERVICE_FAILED] = UNIT_FAILED, | |
76 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING | |
77 | }; | |
78 | ||
79 | /* For Type=idle we never want to delay any other jobs, hence we | |
80 | * consider idle jobs active as soon as we start working on them */ | |
81 | static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = { | |
82 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
83 | [SERVICE_START_PRE] = UNIT_ACTIVE, | |
84 | [SERVICE_START] = UNIT_ACTIVE, | |
85 | [SERVICE_START_POST] = UNIT_ACTIVE, | |
86 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
87 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
88 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
89 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
5eef597e | 90 | [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING, |
663996b3 MS |
91 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
92 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
93 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
94 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
95 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
96 | [SERVICE_FAILED] = UNIT_FAILED, | |
97 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING | |
98 | }; | |
99 | ||
60f067b4 JS |
100 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
101 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); | |
102 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata); | |
103 | ||
104 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f); | |
5eef597e | 105 | static void service_enter_reload_by_notify(Service *s); |
60f067b4 | 106 | |
663996b3 MS |
107 | static void service_init(Unit *u) { |
108 | Service *s = SERVICE(u); | |
109 | ||
110 | assert(u); | |
111 | assert(u->load_state == UNIT_STUB); | |
112 | ||
60f067b4 JS |
113 | s->timeout_start_usec = u->manager->default_timeout_start_usec; |
114 | s->timeout_stop_usec = u->manager->default_timeout_stop_usec; | |
115 | s->restart_usec = u->manager->default_restart_usec; | |
4c89c718 | 116 | s->runtime_max_usec = USEC_INFINITY; |
663996b3 | 117 | s->type = _SERVICE_TYPE_INVALID; |
663996b3 | 118 | s->socket_fd = -1; |
db2df898 | 119 | s->stdin_fd = s->stdout_fd = s->stderr_fd = -1; |
663996b3 MS |
120 | s->guess_main_pid = true; |
121 | ||
663996b3 MS |
122 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
123 | } | |
124 | ||
125 | static void service_unwatch_control_pid(Service *s) { | |
126 | assert(s); | |
127 | ||
128 | if (s->control_pid <= 0) | |
129 | return; | |
130 | ||
131 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
132 | s->control_pid = 0; | |
133 | } | |
134 | ||
135 | static void service_unwatch_main_pid(Service *s) { | |
136 | assert(s); | |
137 | ||
138 | if (s->main_pid <= 0) | |
139 | return; | |
140 | ||
141 | unit_unwatch_pid(UNIT(s), s->main_pid); | |
142 | s->main_pid = 0; | |
143 | } | |
144 | ||
145 | static void service_unwatch_pid_file(Service *s) { | |
146 | if (!s->pid_file_pathspec) | |
147 | return; | |
148 | ||
e3bff60a | 149 | log_unit_debug(UNIT(s), "Stopping watch for PID file %s", s->pid_file_pathspec->path); |
60f067b4 | 150 | path_spec_unwatch(s->pid_file_pathspec); |
663996b3 | 151 | path_spec_done(s->pid_file_pathspec); |
6300502b | 152 | s->pid_file_pathspec = mfree(s->pid_file_pathspec); |
663996b3 MS |
153 | } |
154 | ||
155 | static int service_set_main_pid(Service *s, pid_t pid) { | |
156 | pid_t ppid; | |
157 | ||
158 | assert(s); | |
159 | ||
160 | if (pid <= 1) | |
161 | return -EINVAL; | |
162 | ||
f5e65279 | 163 | if (pid == getpid_cached()) |
663996b3 MS |
164 | return -EINVAL; |
165 | ||
14228c0d MB |
166 | if (s->main_pid == pid && s->main_pid_known) |
167 | return 0; | |
168 | ||
169 | if (s->main_pid != pid) { | |
170 | service_unwatch_main_pid(s); | |
171 | exec_status_start(&s->main_exec_status, pid); | |
172 | } | |
173 | ||
663996b3 MS |
174 | s->main_pid = pid; |
175 | s->main_pid_known = true; | |
176 | ||
f5e65279 | 177 | if (get_process_ppid(pid, &ppid) >= 0 && ppid != getpid_cached()) { |
e3bff60a | 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); |
663996b3 MS |
179 | s->main_pid_alien = true; |
180 | } else | |
181 | s->main_pid_alien = false; | |
182 | ||
663996b3 MS |
183 | return 0; |
184 | } | |
185 | ||
aa27b158 | 186 | void service_close_socket_fd(Service *s) { |
663996b3 MS |
187 | assert(s); |
188 | ||
aa27b158 | 189 | /* Undo the effect of service_set_socket_fd(). */ |
663996b3 | 190 | |
aa27b158 | 191 | s->socket_fd = asynchronous_close(s->socket_fd); |
663996b3 | 192 | |
aa27b158 MP |
193 | if (UNIT_ISSET(s->accept_socket)) { |
194 | socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket))); | |
195 | unit_ref_unset(&s->accept_socket); | |
196 | } | |
663996b3 MS |
197 | } |
198 | ||
199 | static void service_stop_watchdog(Service *s) { | |
200 | assert(s); | |
201 | ||
60f067b4 JS |
202 | s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source); |
203 | s->watchdog_timestamp = DUAL_TIMESTAMP_NULL; | |
663996b3 MS |
204 | } |
205 | ||
5a920b42 MP |
206 | static usec_t service_get_watchdog_usec(Service *s) { |
207 | assert(s); | |
208 | ||
209 | if (s->watchdog_override_enable) | |
210 | return s->watchdog_override_usec; | |
211 | else | |
212 | return s->watchdog_usec; | |
213 | } | |
214 | ||
60f067b4 | 215 | static void service_start_watchdog(Service *s) { |
663996b3 | 216 | int r; |
5a920b42 | 217 | usec_t watchdog_usec; |
663996b3 MS |
218 | |
219 | assert(s); | |
220 | ||
5a920b42 | 221 | watchdog_usec = service_get_watchdog_usec(s); |
f5e65279 | 222 | if (IN_SET(watchdog_usec, 0, USEC_INFINITY)) |
663996b3 MS |
223 | return; |
224 | ||
60f067b4 | 225 | if (s->watchdog_event_source) { |
5a920b42 | 226 | r = sd_event_source_set_time(s->watchdog_event_source, usec_add(s->watchdog_timestamp.monotonic, watchdog_usec)); |
60f067b4 | 227 | if (r < 0) { |
e3bff60a | 228 | log_unit_warning_errno(UNIT(s), r, "Failed to reset watchdog timer: %m"); |
60f067b4 JS |
229 | return; |
230 | } | |
231 | ||
232 | r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT); | |
233 | } else { | |
234 | r = sd_event_add_time( | |
235 | UNIT(s)->manager->event, | |
236 | &s->watchdog_event_source, | |
237 | CLOCK_MONOTONIC, | |
5a920b42 | 238 | usec_add(s->watchdog_timestamp.monotonic, watchdog_usec), 0, |
60f067b4 JS |
239 | service_dispatch_watchdog, s); |
240 | if (r < 0) { | |
e3bff60a | 241 | log_unit_warning_errno(UNIT(s), r, "Failed to add watchdog timer: %m"); |
60f067b4 JS |
242 | return; |
243 | } | |
244 | ||
e3bff60a MP |
245 | (void) sd_event_source_set_description(s->watchdog_event_source, "service-watchdog"); |
246 | ||
60f067b4 JS |
247 | /* Let's process everything else which might be a sign |
248 | * of living before we consider a service died. */ | |
249 | r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE); | |
663996b3 MS |
250 | } |
251 | ||
663996b3 | 252 | if (r < 0) |
e3bff60a | 253 | log_unit_warning_errno(UNIT(s), r, "Failed to install watchdog timer: %m"); |
663996b3 MS |
254 | } |
255 | ||
52ad194e MB |
256 | static void service_extend_timeout(Service *s, usec_t extend_timeout_usec) { |
257 | assert(s); | |
258 | ||
259 | if (s->timer_event_source) { | |
260 | uint64_t current = 0, extended = 0; | |
261 | int r; | |
262 | ||
263 | if (IN_SET(extend_timeout_usec, 0, USEC_INFINITY)) | |
264 | return; | |
265 | ||
266 | extended = usec_add(now(CLOCK_MONOTONIC), extend_timeout_usec); | |
267 | ||
268 | r = sd_event_source_get_time(s->timer_event_source, ¤t); | |
269 | if (r < 0) | |
270 | log_unit_error_errno(UNIT(s), r, "Failed to retrieve timeout timer: %m"); | |
271 | else if (extended > current) { | |
272 | r = sd_event_source_set_time(s->timer_event_source, extended); | |
273 | if (r < 0) | |
274 | log_unit_warning_errno(UNIT(s), r, "Failed to set timeout timer: %m"); | |
275 | } | |
276 | ||
277 | if (s->watchdog_event_source) { | |
278 | /* extend watchdog if necessary. We've asked for an extended timeout so we | |
279 | * shouldn't expect a watchdog timeout in the interval in between */ | |
280 | r = sd_event_source_get_time(s->watchdog_event_source, ¤t); | |
281 | if (r < 0) { | |
282 | log_unit_error_errno(UNIT(s), r, "Failed to retrieve watchdog timer: %m"); | |
283 | return; | |
284 | } | |
285 | ||
286 | if (extended > current) { | |
287 | r = sd_event_source_set_time(s->watchdog_event_source, extended); | |
288 | if (r < 0) | |
289 | log_unit_warning_errno(UNIT(s), r, "Failed to set watchdog timer: %m"); | |
290 | } | |
291 | } | |
292 | } | |
293 | } | |
294 | ||
663996b3 MS |
295 | static void service_reset_watchdog(Service *s) { |
296 | assert(s); | |
297 | ||
298 | dual_timestamp_get(&s->watchdog_timestamp); | |
60f067b4 | 299 | service_start_watchdog(s); |
663996b3 MS |
300 | } |
301 | ||
5a920b42 MP |
302 | static void service_reset_watchdog_timeout(Service *s, usec_t watchdog_override_usec) { |
303 | assert(s); | |
304 | ||
305 | s->watchdog_override_enable = true; | |
306 | s->watchdog_override_usec = watchdog_override_usec; | |
307 | service_reset_watchdog(s); | |
308 | ||
309 | log_unit_debug(UNIT(s), "watchdog_usec="USEC_FMT, s->watchdog_usec); | |
310 | log_unit_debug(UNIT(s), "watchdog_override_usec="USEC_FMT, s->watchdog_override_usec); | |
311 | } | |
312 | ||
e735f4d4 MP |
313 | static void service_fd_store_unlink(ServiceFDStore *fs) { |
314 | ||
315 | if (!fs) | |
316 | return; | |
317 | ||
318 | if (fs->service) { | |
319 | assert(fs->service->n_fd_store > 0); | |
320 | LIST_REMOVE(fd_store, fs->service->fd_store, fs); | |
321 | fs->service->n_fd_store--; | |
322 | } | |
323 | ||
324 | if (fs->event_source) { | |
325 | sd_event_source_set_enabled(fs->event_source, SD_EVENT_OFF); | |
326 | sd_event_source_unref(fs->event_source); | |
327 | } | |
328 | ||
6300502b | 329 | free(fs->fdname); |
e735f4d4 MP |
330 | safe_close(fs->fd); |
331 | free(fs); | |
332 | } | |
333 | ||
8a584da2 MP |
334 | static void service_release_fd_store(Service *s) { |
335 | assert(s); | |
336 | ||
52ad194e MB |
337 | if (s->n_keep_fd_store > 0) |
338 | return; | |
339 | ||
8a584da2 MP |
340 | log_unit_debug(UNIT(s), "Releasing all stored fds"); |
341 | while (s->fd_store) | |
342 | service_fd_store_unlink(s->fd_store); | |
343 | ||
344 | assert(s->n_fd_store == 0); | |
345 | } | |
346 | ||
52ad194e | 347 | static void service_release_resources(Unit *u) { |
e735f4d4 MP |
348 | Service *s = SERVICE(u); |
349 | ||
350 | assert(s); | |
351 | ||
db2df898 | 352 | if (!s->fd_store && s->stdin_fd < 0 && s->stdout_fd < 0 && s->stderr_fd < 0) |
e735f4d4 MP |
353 | return; |
354 | ||
8a584da2 | 355 | log_unit_debug(u, "Releasing resources."); |
e735f4d4 | 356 | |
db2df898 MP |
357 | s->stdin_fd = safe_close(s->stdin_fd); |
358 | s->stdout_fd = safe_close(s->stdout_fd); | |
359 | s->stderr_fd = safe_close(s->stderr_fd); | |
360 | ||
52ad194e | 361 | service_release_fd_store(s); |
e735f4d4 MP |
362 | } |
363 | ||
663996b3 MS |
364 | static void service_done(Unit *u) { |
365 | Service *s = SERVICE(u); | |
366 | ||
367 | assert(s); | |
368 | ||
6300502b MP |
369 | s->pid_file = mfree(s->pid_file); |
370 | s->status_text = mfree(s->status_text); | |
60f067b4 JS |
371 | |
372 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); | |
663996b3 MS |
373 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
374 | s->control_command = NULL; | |
375 | s->main_command = NULL; | |
376 | ||
8a584da2 MP |
377 | dynamic_creds_unref(&s->dynamic_creds); |
378 | ||
e842803a MB |
379 | exit_status_set_free(&s->restart_prevent_status); |
380 | exit_status_set_free(&s->restart_force_status); | |
381 | exit_status_set_free(&s->success_status); | |
663996b3 MS |
382 | |
383 | /* This will leak a process, but at least no memory or any of | |
384 | * our resources */ | |
385 | service_unwatch_main_pid(s); | |
386 | service_unwatch_control_pid(s); | |
387 | service_unwatch_pid_file(s); | |
388 | ||
389 | if (s->bus_name) { | |
390 | unit_unwatch_bus_name(u, s->bus_name); | |
6300502b | 391 | s->bus_name = mfree(s->bus_name); |
663996b3 MS |
392 | } |
393 | ||
4c89c718 MP |
394 | s->bus_name_owner = mfree(s->bus_name_owner); |
395 | ||
663996b3 | 396 | service_close_socket_fd(s); |
8a584da2 | 397 | s->peer = socket_peer_unref(s->peer); |
663996b3 MS |
398 | |
399 | unit_ref_unset(&s->accept_socket); | |
400 | ||
401 | service_stop_watchdog(s); | |
402 | ||
60f067b4 | 403 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
e735f4d4 | 404 | |
52ad194e | 405 | service_release_resources(u); |
e735f4d4 MP |
406 | } |
407 | ||
408 | static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) { | |
409 | ServiceFDStore *fs = userdata; | |
410 | ||
411 | assert(e); | |
412 | assert(fs); | |
413 | ||
414 | /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */ | |
8a584da2 MP |
415 | log_unit_debug(UNIT(fs->service), |
416 | "Received %s on stored fd %d (%s), closing.", | |
417 | revents & EPOLLERR ? "EPOLLERR" : "EPOLLHUP", | |
418 | fs->fd, strna(fs->fdname)); | |
e735f4d4 MP |
419 | service_fd_store_unlink(fs); |
420 | return 0; | |
421 | } | |
422 | ||
6300502b | 423 | static int service_add_fd_store(Service *s, int fd, const char *name) { |
e735f4d4 MP |
424 | ServiceFDStore *fs; |
425 | int r; | |
426 | ||
8a584da2 MP |
427 | /* fd is always consumed if we return >= 0 */ |
428 | ||
e735f4d4 MP |
429 | assert(s); |
430 | assert(fd >= 0); | |
431 | ||
432 | if (s->n_fd_store >= s->n_fd_store_max) | |
8a584da2 MP |
433 | return -EXFULL; /* Our store is full. |
434 | * Use this errno rather than E[NM]FILE to distinguish from | |
435 | * the case where systemd itself hits the file limit. */ | |
e735f4d4 MP |
436 | |
437 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
438 | r = same_fd(fs->fd, fd); | |
439 | if (r < 0) | |
440 | return r; | |
441 | if (r > 0) { | |
e735f4d4 | 442 | safe_close(fd); |
8a584da2 | 443 | return 0; /* fd already included */ |
e735f4d4 MP |
444 | } |
445 | } | |
446 | ||
447 | fs = new0(ServiceFDStore, 1); | |
448 | if (!fs) | |
449 | return -ENOMEM; | |
450 | ||
451 | fs->fd = fd; | |
452 | fs->service = s; | |
6300502b MP |
453 | fs->fdname = strdup(name ?: "stored"); |
454 | if (!fs->fdname) { | |
455 | free(fs); | |
456 | return -ENOMEM; | |
457 | } | |
e735f4d4 MP |
458 | |
459 | r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fd, 0, on_fd_store_io, fs); | |
81c58355 | 460 | if (r < 0 && r != -EPERM) { /* EPERM indicates fds that aren't pollable, which is OK */ |
6300502b | 461 | free(fs->fdname); |
e735f4d4 MP |
462 | free(fs); |
463 | return r; | |
81c58355 MB |
464 | } else if (r >= 0) |
465 | (void) sd_event_source_set_description(fs->event_source, "service-fd-store"); | |
e3bff60a | 466 | |
e735f4d4 MP |
467 | LIST_PREPEND(fd_store, s->fd_store, fs); |
468 | s->n_fd_store++; | |
469 | ||
8a584da2 | 470 | return 1; /* fd newly stored */ |
e735f4d4 MP |
471 | } |
472 | ||
6300502b | 473 | static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name) { |
e735f4d4 MP |
474 | int r; |
475 | ||
476 | assert(s); | |
477 | ||
8a584da2 | 478 | while (fdset_size(fds) > 0) { |
e735f4d4 MP |
479 | _cleanup_close_ int fd = -1; |
480 | ||
481 | fd = fdset_steal_first(fds); | |
482 | if (fd < 0) | |
483 | break; | |
484 | ||
6300502b | 485 | r = service_add_fd_store(s, fd, name); |
8a584da2 MP |
486 | if (r == -EXFULL) |
487 | return log_unit_warning_errno(UNIT(s), r, | |
488 | "Cannot store more fds than FileDescriptorStoreMax=%u, closing remaining.", | |
489 | s->n_fd_store_max); | |
e735f4d4 | 490 | if (r < 0) |
8a584da2 MP |
491 | return log_unit_error_errno(UNIT(s), r, "Failed to add fd to store: %m"); |
492 | if (r > 0) | |
493 | log_unit_debug(UNIT(s), "Added fd %u (%s) to fd store.", fd, strna(name)); | |
494 | fd = -1; | |
e735f4d4 MP |
495 | } |
496 | ||
e735f4d4 | 497 | return 0; |
663996b3 MS |
498 | } |
499 | ||
52ad194e MB |
500 | static void service_remove_fd_store(Service *s, const char *name) { |
501 | ServiceFDStore *fs, *n; | |
502 | ||
503 | assert(s); | |
504 | assert(name); | |
505 | ||
506 | LIST_FOREACH_SAFE(fd_store, fs, n, s->fd_store) { | |
507 | if (!streq(fs->fdname, name)) | |
508 | continue; | |
509 | ||
510 | log_unit_debug(UNIT(s), "Got explicit request to remove fd %i (%s), closing.", fs->fd, name); | |
511 | service_fd_store_unlink(fs); | |
512 | } | |
513 | } | |
514 | ||
60f067b4 | 515 | static int service_arm_timer(Service *s, usec_t usec) { |
663996b3 MS |
516 | int r; |
517 | ||
518 | assert(s); | |
519 | ||
60f067b4 | 520 | if (s->timer_event_source) { |
4c89c718 | 521 | r = sd_event_source_set_time(s->timer_event_source, usec); |
663996b3 MS |
522 | if (r < 0) |
523 | return r; | |
524 | ||
60f067b4 | 525 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); |
663996b3 MS |
526 | } |
527 | ||
4c89c718 MP |
528 | if (usec == USEC_INFINITY) |
529 | return 0; | |
530 | ||
e3bff60a | 531 | r = sd_event_add_time( |
60f067b4 JS |
532 | UNIT(s)->manager->event, |
533 | &s->timer_event_source, | |
534 | CLOCK_MONOTONIC, | |
4c89c718 | 535 | usec, 0, |
60f067b4 | 536 | service_dispatch_timer, s); |
e3bff60a MP |
537 | if (r < 0) |
538 | return r; | |
539 | ||
540 | (void) sd_event_source_set_description(s->timer_event_source, "service-timer"); | |
541 | ||
542 | return 0; | |
663996b3 MS |
543 | } |
544 | ||
545 | static int service_verify(Service *s) { | |
546 | assert(s); | |
547 | ||
548 | if (UNIT(s)->load_state != UNIT_LOADED) | |
549 | return 0; | |
550 | ||
5eef597e | 551 | if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]) { |
e3bff60a | 552 | log_unit_error(UNIT(s), "Service lacks both ExecStart= and ExecStop= setting. Refusing."); |
663996b3 MS |
553 | return -EINVAL; |
554 | } | |
555 | ||
5eef597e | 556 | if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) { |
e3bff60a | 557 | log_unit_error(UNIT(s), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); |
5eef597e MP |
558 | return -EINVAL; |
559 | } | |
560 | ||
561 | if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START]) { | |
e3bff60a | 562 | log_unit_error(UNIT(s), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing."); |
5eef597e MP |
563 | return -EINVAL; |
564 | } | |
565 | ||
566 | if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) { | |
e3bff60a | 567 | log_unit_error(UNIT(s), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); |
663996b3 MS |
568 | return -EINVAL; |
569 | } | |
570 | ||
14228c0d | 571 | if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) { |
e3bff60a | 572 | log_unit_error(UNIT(s), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing."); |
e842803a MB |
573 | return -EINVAL; |
574 | } | |
575 | ||
576 | if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) { | |
e3bff60a | 577 | log_unit_error(UNIT(s), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing."); |
14228c0d MB |
578 | return -EINVAL; |
579 | } | |
580 | ||
663996b3 | 581 | if (s->type == SERVICE_DBUS && !s->bus_name) { |
e3bff60a | 582 | log_unit_error(UNIT(s), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing."); |
663996b3 MS |
583 | return -EINVAL; |
584 | } | |
585 | ||
586 | if (s->bus_name && s->type != SERVICE_DBUS) | |
e3bff60a | 587 | log_unit_warning(UNIT(s), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring."); |
663996b3 | 588 | |
f5e65279 | 589 | if (s->exec_context.pam_name && !IN_SET(s->kill_context.kill_mode, KILL_CONTROL_GROUP, KILL_MIXED)) { |
e3bff60a | 590 | log_unit_error(UNIT(s), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing."); |
663996b3 MS |
591 | return -EINVAL; |
592 | } | |
593 | ||
6300502b MP |
594 | if (s->usb_function_descriptors && !s->usb_function_strings) |
595 | log_unit_warning(UNIT(s), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring."); | |
596 | ||
597 | if (!s->usb_function_descriptors && s->usb_function_strings) | |
598 | log_unit_warning(UNIT(s), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring."); | |
599 | ||
4c89c718 MP |
600 | if (s->runtime_max_usec != USEC_INFINITY && s->type == SERVICE_ONESHOT) |
601 | log_unit_warning(UNIT(s), "MaxRuntimeSec= has no effect in combination with Type=oneshot. Ignoring."); | |
602 | ||
663996b3 MS |
603 | return 0; |
604 | } | |
605 | ||
606 | static int service_add_default_dependencies(Service *s) { | |
607 | int r; | |
608 | ||
609 | assert(s); | |
610 | ||
db2df898 MP |
611 | if (!UNIT(s)->default_dependencies) |
612 | return 0; | |
613 | ||
663996b3 MS |
614 | /* Add a number of automatic dependencies useful for the |
615 | * majority of services. */ | |
616 | ||
aa27b158 | 617 | if (MANAGER_IS_SYSTEM(UNIT(s)->manager)) { |
db2df898 MP |
618 | /* First, pull in the really early boot stuff, and |
619 | * require it, so that we fail if we can't acquire | |
620 | * it. */ | |
621 | ||
52ad194e | 622 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SYSINIT_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT); |
db2df898 MP |
623 | if (r < 0) |
624 | return r; | |
625 | } else { | |
626 | ||
627 | /* In the --user instance there's no sysinit.target, | |
628 | * in that case require basic.target instead. */ | |
629 | ||
52ad194e | 630 | r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT); |
db2df898 MP |
631 | if (r < 0) |
632 | return r; | |
633 | } | |
634 | ||
635 | /* Second, if the rest of the base system is in the same | |
636 | * transaction, order us after it, but do not pull it in or | |
637 | * even require it. */ | |
52ad194e | 638 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT); |
60f067b4 JS |
639 | if (r < 0) |
640 | return r; | |
663996b3 | 641 | |
db2df898 | 642 | /* Third, add us in for normal shutdown. */ |
52ad194e | 643 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true, UNIT_DEPENDENCY_DEFAULT); |
663996b3 MS |
644 | } |
645 | ||
646 | static void service_fix_output(Service *s) { | |
647 | assert(s); | |
648 | ||
52ad194e MB |
649 | /* If nothing has been explicitly configured, patch default output in. If input is socket/tty we avoid this |
650 | * however, since in that case we want output to default to the same place as we read input from. */ | |
663996b3 MS |
651 | |
652 | if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT && | |
653 | s->exec_context.std_output == EXEC_OUTPUT_INHERIT && | |
654 | s->exec_context.std_input == EXEC_INPUT_NULL) | |
655 | s->exec_context.std_error = UNIT(s)->manager->default_std_error; | |
656 | ||
657 | if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT && | |
658 | s->exec_context.std_input == EXEC_INPUT_NULL) | |
659 | s->exec_context.std_output = UNIT(s)->manager->default_std_output; | |
52ad194e MB |
660 | |
661 | if (s->exec_context.std_input == EXEC_INPUT_NULL && | |
662 | s->exec_context.stdin_data_size > 0) | |
663 | s->exec_context.std_input = EXEC_INPUT_DATA; | |
663996b3 MS |
664 | } |
665 | ||
db2df898 MP |
666 | static int service_setup_bus_name(Service *s) { |
667 | int r; | |
668 | ||
669 | assert(s); | |
670 | ||
671 | if (!s->bus_name) | |
672 | return 0; | |
673 | ||
52ad194e | 674 | r = unit_add_dependency_by_name(UNIT(s), UNIT_REQUIRES, SPECIAL_DBUS_SOCKET, NULL, true, UNIT_DEPENDENCY_FILE); |
5a920b42 MP |
675 | if (r < 0) |
676 | return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m"); | |
db2df898 | 677 | |
f5e65279 | 678 | /* We always want to be ordered against dbus.socket if both are in the transaction. */ |
52ad194e | 679 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, SPECIAL_DBUS_SOCKET, NULL, true, UNIT_DEPENDENCY_FILE); |
db2df898 MP |
680 | if (r < 0) |
681 | return log_unit_error_errno(UNIT(s), r, "Failed to add dependency on " SPECIAL_DBUS_SOCKET ": %m"); | |
682 | ||
683 | r = unit_watch_bus_name(UNIT(s), s->bus_name); | |
684 | if (r == -EEXIST) | |
685 | return log_unit_error_errno(UNIT(s), r, "Two services allocated for the same bus name %s, refusing operation.", s->bus_name); | |
686 | if (r < 0) | |
687 | return log_unit_error_errno(UNIT(s), r, "Cannot watch bus name %s: %m", s->bus_name); | |
688 | ||
689 | return 0; | |
690 | } | |
691 | ||
e735f4d4 MP |
692 | static int service_add_extras(Service *s) { |
693 | int r; | |
694 | ||
695 | assert(s); | |
696 | ||
697 | if (s->type == _SERVICE_TYPE_INVALID) { | |
698 | /* Figure out a type automatically */ | |
699 | if (s->bus_name) | |
700 | s->type = SERVICE_DBUS; | |
701 | else if (s->exec_command[SERVICE_EXEC_START]) | |
702 | s->type = SERVICE_SIMPLE; | |
703 | else | |
704 | s->type = SERVICE_ONESHOT; | |
705 | } | |
706 | ||
707 | /* Oneshot services have disabled start timeout by default */ | |
708 | if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined) | |
4c89c718 | 709 | s->timeout_start_usec = USEC_INFINITY; |
e735f4d4 MP |
710 | |
711 | service_fix_output(s); | |
712 | ||
713 | r = unit_patch_contexts(UNIT(s)); | |
714 | if (r < 0) | |
715 | return r; | |
716 | ||
717 | r = unit_add_exec_dependencies(UNIT(s), &s->exec_context); | |
718 | if (r < 0) | |
719 | return r; | |
720 | ||
d9dfd233 | 721 | r = unit_set_default_slice(UNIT(s)); |
e735f4d4 MP |
722 | if (r < 0) |
723 | return r; | |
724 | ||
725 | if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE) | |
726 | s->notify_access = NOTIFY_MAIN; | |
727 | ||
728 | if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE) | |
729 | s->notify_access = NOTIFY_MAIN; | |
730 | ||
db2df898 MP |
731 | r = service_add_default_dependencies(s); |
732 | if (r < 0) | |
733 | return r; | |
e735f4d4 | 734 | |
db2df898 MP |
735 | r = service_setup_bus_name(s); |
736 | if (r < 0) | |
737 | return r; | |
e735f4d4 MP |
738 | |
739 | return 0; | |
740 | } | |
741 | ||
663996b3 | 742 | static int service_load(Unit *u) { |
663996b3 | 743 | Service *s = SERVICE(u); |
5eef597e | 744 | int r; |
663996b3 MS |
745 | |
746 | assert(s); | |
747 | ||
748 | /* Load a .service file */ | |
14228c0d MB |
749 | r = unit_load_fragment(u); |
750 | if (r < 0) | |
663996b3 MS |
751 | return r; |
752 | ||
663996b3 MS |
753 | /* Still nothing found? Then let's give up */ |
754 | if (u->load_state == UNIT_STUB) | |
755 | return -ENOENT; | |
756 | ||
663996b3 MS |
757 | /* This is a new unit? Then let's add in some extras */ |
758 | if (u->load_state == UNIT_LOADED) { | |
14228c0d MB |
759 | |
760 | /* We were able to load something, then let's add in | |
761 | * the dropin directories. */ | |
762 | r = unit_load_dropin(u); | |
763 | if (r < 0) | |
764 | return r; | |
765 | ||
e735f4d4 MP |
766 | /* This is a new unit? Then let's add in some |
767 | * extras */ | |
768 | r = service_add_extras(s); | |
663996b3 MS |
769 | if (r < 0) |
770 | return r; | |
663996b3 MS |
771 | } |
772 | ||
773 | return service_verify(s); | |
774 | } | |
775 | ||
776 | static void service_dump(Unit *u, FILE *f, const char *prefix) { | |
663996b3 MS |
777 | ServiceExecCommand c; |
778 | Service *s = SERVICE(u); | |
779 | const char *prefix2; | |
663996b3 MS |
780 | |
781 | assert(s); | |
782 | ||
5eef597e | 783 | prefix = strempty(prefix); |
e735f4d4 | 784 | prefix2 = strjoina(prefix, "\t"); |
663996b3 MS |
785 | |
786 | fprintf(f, | |
787 | "%sService State: %s\n" | |
788 | "%sResult: %s\n" | |
789 | "%sReload Result: %s\n" | |
790 | "%sPermissionsStartOnly: %s\n" | |
791 | "%sRootDirectoryStartOnly: %s\n" | |
792 | "%sRemainAfterExit: %s\n" | |
793 | "%sGuessMainPID: %s\n" | |
794 | "%sType: %s\n" | |
795 | "%sRestart: %s\n" | |
5eef597e MP |
796 | "%sNotifyAccess: %s\n" |
797 | "%sNotifyState: %s\n", | |
663996b3 MS |
798 | prefix, service_state_to_string(s->state), |
799 | prefix, service_result_to_string(s->result), | |
800 | prefix, service_result_to_string(s->reload_result), | |
801 | prefix, yes_no(s->permissions_start_only), | |
802 | prefix, yes_no(s->root_directory_start_only), | |
803 | prefix, yes_no(s->remain_after_exit), | |
804 | prefix, yes_no(s->guess_main_pid), | |
805 | prefix, service_type_to_string(s->type), | |
806 | prefix, service_restart_to_string(s->restart), | |
5eef597e MP |
807 | prefix, notify_access_to_string(s->notify_access), |
808 | prefix, notify_state_to_string(s->notify_state)); | |
663996b3 MS |
809 | |
810 | if (s->control_pid > 0) | |
811 | fprintf(f, | |
60f067b4 JS |
812 | "%sControl PID: "PID_FMT"\n", |
813 | prefix, s->control_pid); | |
663996b3 MS |
814 | |
815 | if (s->main_pid > 0) | |
816 | fprintf(f, | |
60f067b4 | 817 | "%sMain PID: "PID_FMT"\n" |
663996b3 MS |
818 | "%sMain PID Known: %s\n" |
819 | "%sMain PID Alien: %s\n", | |
60f067b4 | 820 | prefix, s->main_pid, |
663996b3 MS |
821 | prefix, yes_no(s->main_pid_known), |
822 | prefix, yes_no(s->main_pid_alien)); | |
823 | ||
824 | if (s->pid_file) | |
825 | fprintf(f, | |
826 | "%sPIDFile: %s\n", | |
827 | prefix, s->pid_file); | |
828 | ||
829 | if (s->bus_name) | |
830 | fprintf(f, | |
831 | "%sBusName: %s\n" | |
832 | "%sBus Name Good: %s\n", | |
833 | prefix, s->bus_name, | |
834 | prefix, yes_no(s->bus_name_good)); | |
835 | ||
8a584da2 MP |
836 | if (UNIT_ISSET(s->accept_socket)) |
837 | fprintf(f, | |
838 | "%sAccept Socket: %s\n", | |
839 | prefix, UNIT_DEREF(s->accept_socket)->id); | |
840 | ||
663996b3 MS |
841 | kill_context_dump(&s->kill_context, f, prefix); |
842 | exec_context_dump(&s->exec_context, f, prefix); | |
843 | ||
844 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { | |
845 | ||
846 | if (!s->exec_command[c]) | |
847 | continue; | |
848 | ||
849 | fprintf(f, "%s-> %s:\n", | |
850 | prefix, service_exec_command_to_string(c)); | |
851 | ||
852 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
853 | } | |
854 | ||
663996b3 MS |
855 | if (s->status_text) |
856 | fprintf(f, "%sStatus Text: %s\n", | |
857 | prefix, s->status_text); | |
e735f4d4 | 858 | |
6300502b | 859 | if (s->n_fd_store_max > 0) |
e735f4d4 MP |
860 | fprintf(f, |
861 | "%sFile Descriptor Store Max: %u\n" | |
862 | "%sFile Descriptor Store Current: %u\n", | |
863 | prefix, s->n_fd_store_max, | |
864 | prefix, s->n_fd_store); | |
f5e65279 MB |
865 | |
866 | cgroup_context_dump(&s->cgroup_context, f, prefix); | |
663996b3 MS |
867 | } |
868 | ||
1d42b86d MB |
869 | static int service_is_suitable_main_pid(Service *s, pid_t pid, int prio) { |
870 | Unit *owner; | |
871 | ||
872 | assert(s); | |
873 | assert(pid_is_valid(pid)); | |
874 | ||
875 | /* Checks whether the specified PID is suitable as main PID for this service. returns negative if not, 0 if the | |
876 | * PID is questionnable but should be accepted if the source of configuration is trusted. > 0 if the PID is | |
877 | * good */ | |
878 | ||
879 | if (pid == getpid_cached() || pid == 1) { | |
880 | log_unit_full(UNIT(s), prio, 0, "New main PID "PID_FMT" is the manager, refusing.", pid); | |
881 | return -EPERM; | |
882 | } | |
883 | ||
884 | if (pid == s->control_pid) { | |
885 | log_unit_full(UNIT(s), prio, 0, "New main PID "PID_FMT" is the control process, refusing.", pid); | |
886 | return -EPERM; | |
887 | } | |
888 | ||
889 | if (!pid_is_alive(pid)) { | |
890 | log_unit_full(UNIT(s), prio, 0, "New main PID "PID_FMT" does not exist or is a zombie.", pid); | |
891 | return -ESRCH; | |
892 | } | |
893 | ||
894 | owner = manager_get_unit_by_pid(UNIT(s)->manager, pid); | |
895 | if (owner == UNIT(s)) { | |
896 | log_unit_debug(UNIT(s), "New main PID "PID_FMT" belongs to service, we are happy.", pid); | |
897 | return 1; /* Yay, it's definitely a good PID */ | |
898 | } | |
899 | ||
900 | return 0; /* Hmm it's a suspicious PID, let's accept it if configuration source is trusted */ | |
901 | } | |
902 | ||
663996b3 | 903 | static int service_load_pid_file(Service *s, bool may_warn) { |
1d42b86d | 904 | char procfs[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)]; |
663996b3 | 905 | _cleanup_free_ char *k = NULL; |
1d42b86d MB |
906 | _cleanup_close_ int fd = -1; |
907 | int r, prio; | |
663996b3 MS |
908 | pid_t pid; |
909 | ||
910 | assert(s); | |
911 | ||
912 | if (!s->pid_file) | |
913 | return -ENOENT; | |
914 | ||
1d42b86d MB |
915 | prio = may_warn ? LOG_INFO : LOG_DEBUG; |
916 | ||
917 | fd = chase_symlinks(s->pid_file, NULL, CHASE_OPEN|CHASE_SAFE, NULL); | |
918 | if (fd == -EPERM) | |
919 | return log_unit_full(UNIT(s), prio, fd, "Permission denied while opening PID file or unsafe symlink chain: %s", s->pid_file); | |
920 | if (fd < 0) | |
921 | 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)); | |
922 | ||
923 | /* 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. */ | |
924 | xsprintf(procfs, "/proc/self/fd/%i", fd); | |
925 | r = read_one_line_file(procfs, &k); | |
926 | if (r < 0) | |
927 | 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); | |
663996b3 MS |
928 | |
929 | r = parse_pid(k, &pid); | |
1d42b86d MB |
930 | if (r < 0) |
931 | return log_unit_full(UNIT(s), prio, r, "Failed to parse PID from file %s: %m", s->pid_file); | |
932 | ||
933 | if (s->main_pid_known && pid == s->main_pid) | |
934 | return 0; | |
935 | ||
936 | r = service_is_suitable_main_pid(s, pid, prio); | |
937 | if (r < 0) | |
663996b3 | 938 | return r; |
1d42b86d MB |
939 | if (r == 0) { |
940 | struct stat st; | |
663996b3 | 941 | |
1d42b86d MB |
942 | /* Hmm, it's not clear if the new main PID is safe. Let's allow this if the PID file is owned by root */ |
943 | ||
944 | if (fstat(fd, &st) < 0) | |
945 | return log_unit_error_errno(UNIT(s), errno, "Failed to fstat() PID file O_PATH fd: %m"); | |
946 | ||
947 | if (st.st_uid != 0) { | |
948 | 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); | |
949 | return -EPERM; | |
950 | } | |
951 | ||
952 | 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); | |
663996b3 MS |
953 | } |
954 | ||
955 | if (s->main_pid_known) { | |
e3bff60a | 956 | log_unit_debug(UNIT(s), "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid); |
5eef597e | 957 | |
663996b3 MS |
958 | service_unwatch_main_pid(s); |
959 | s->main_pid_known = false; | |
960 | } else | |
e3bff60a | 961 | log_unit_debug(UNIT(s), "Main PID loaded: "PID_FMT, pid); |
663996b3 MS |
962 | |
963 | r = service_set_main_pid(s, pid); | |
964 | if (r < 0) | |
965 | return r; | |
966 | ||
967 | r = unit_watch_pid(UNIT(s), pid); | |
2897b343 MP |
968 | if (r < 0) /* FIXME: we need to do something here */ |
969 | return log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" for service: %m", pid); | |
663996b3 | 970 | |
1d42b86d | 971 | return 1; |
663996b3 MS |
972 | } |
973 | ||
aa27b158 | 974 | static void service_search_main_pid(Service *s) { |
d9dfd233 | 975 | pid_t pid = 0; |
663996b3 MS |
976 | int r; |
977 | ||
978 | assert(s); | |
979 | ||
980 | /* If we know it anyway, don't ever fallback to unreliable | |
981 | * heuristics */ | |
982 | if (s->main_pid_known) | |
aa27b158 | 983 | return; |
663996b3 MS |
984 | |
985 | if (!s->guess_main_pid) | |
aa27b158 | 986 | return; |
663996b3 MS |
987 | |
988 | assert(s->main_pid <= 0); | |
989 | ||
aa27b158 MP |
990 | if (unit_search_main_pid(UNIT(s), &pid) < 0) |
991 | return; | |
663996b3 | 992 | |
e3bff60a | 993 | log_unit_debug(UNIT(s), "Main PID guessed: "PID_FMT, pid); |
aa27b158 MP |
994 | if (service_set_main_pid(s, pid) < 0) |
995 | return; | |
663996b3 MS |
996 | |
997 | r = unit_watch_pid(UNIT(s), pid); | |
aa27b158 | 998 | if (r < 0) |
663996b3 | 999 | /* FIXME: we need to do something here */ |
e3bff60a | 1000 | log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" from: %m", pid); |
663996b3 MS |
1001 | } |
1002 | ||
663996b3 MS |
1003 | static void service_set_state(Service *s, ServiceState state) { |
1004 | ServiceState old_state; | |
1005 | const UnitActiveState *table; | |
60f067b4 | 1006 | |
663996b3 MS |
1007 | assert(s); |
1008 | ||
1009 | table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; | |
1010 | ||
1011 | old_state = s->state; | |
1012 | s->state = state; | |
1013 | ||
1014 | service_unwatch_pid_file(s); | |
1015 | ||
60f067b4 JS |
1016 | if (!IN_SET(state, |
1017 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
4c89c718 | 1018 | SERVICE_RUNNING, |
60f067b4 | 1019 | SERVICE_RELOAD, |
e3bff60a | 1020 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
60f067b4 JS |
1021 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
1022 | SERVICE_AUTO_RESTART)) | |
1023 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
1024 | ||
1025 | if (!IN_SET(state, | |
1026 | SERVICE_START, SERVICE_START_POST, | |
1027 | SERVICE_RUNNING, SERVICE_RELOAD, | |
e3bff60a | 1028 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
60f067b4 | 1029 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
663996b3 MS |
1030 | service_unwatch_main_pid(s); |
1031 | s->main_command = NULL; | |
1032 | } | |
1033 | ||
60f067b4 JS |
1034 | if (!IN_SET(state, |
1035 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
1036 | SERVICE_RELOAD, | |
e3bff60a | 1037 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
60f067b4 | 1038 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
663996b3 MS |
1039 | service_unwatch_control_pid(s); |
1040 | s->control_command = NULL; | |
1041 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
1042 | } | |
1043 | ||
60f067b4 JS |
1044 | if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
1045 | unit_unwatch_all_pids(UNIT(s)); | |
1046 | ||
1047 | if (!IN_SET(state, | |
1048 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
1049 | SERVICE_RUNNING, SERVICE_RELOAD, | |
e3bff60a MP |
1050 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
1051 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) && | |
aa27b158 | 1052 | !(state == SERVICE_DEAD && UNIT(s)->job)) |
663996b3 | 1053 | service_close_socket_fd(s); |
663996b3 | 1054 | |
60f067b4 | 1055 | if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
663996b3 MS |
1056 | service_stop_watchdog(s); |
1057 | ||
1058 | /* For the inactive states unit_notify() will trim the cgroup, | |
1059 | * but for exit we have to do that ourselves... */ | |
aa27b158 | 1060 | if (state == SERVICE_EXITED && !MANAGER_IS_RELOADING(UNIT(s)->manager)) |
d9dfd233 | 1061 | unit_prune_cgroup(UNIT(s)); |
663996b3 MS |
1062 | |
1063 | if (old_state != state) | |
e3bff60a | 1064 | log_unit_debug(UNIT(s), "Changed %s -> %s", service_state_to_string(old_state), service_state_to_string(state)); |
663996b3 MS |
1065 | |
1066 | unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS); | |
663996b3 MS |
1067 | } |
1068 | ||
4c89c718 MP |
1069 | static usec_t service_coldplug_timeout(Service *s) { |
1070 | assert(s); | |
1071 | ||
1072 | switch (s->deserialized_state) { | |
1073 | ||
1074 | case SERVICE_START_PRE: | |
1075 | case SERVICE_START: | |
1076 | case SERVICE_START_POST: | |
1077 | case SERVICE_RELOAD: | |
1078 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_start_usec); | |
1079 | ||
1080 | case SERVICE_RUNNING: | |
1081 | return usec_add(UNIT(s)->active_enter_timestamp.monotonic, s->runtime_max_usec); | |
1082 | ||
1083 | case SERVICE_STOP: | |
1084 | case SERVICE_STOP_SIGABRT: | |
1085 | case SERVICE_STOP_SIGTERM: | |
1086 | case SERVICE_STOP_SIGKILL: | |
1087 | case SERVICE_STOP_POST: | |
1088 | case SERVICE_FINAL_SIGTERM: | |
1089 | case SERVICE_FINAL_SIGKILL: | |
1090 | return usec_add(UNIT(s)->state_change_timestamp.monotonic, s->timeout_stop_usec); | |
1091 | ||
1092 | case SERVICE_AUTO_RESTART: | |
1093 | return usec_add(UNIT(s)->inactive_enter_timestamp.monotonic, s->restart_usec); | |
1094 | ||
1095 | default: | |
1096 | return USEC_INFINITY; | |
1097 | } | |
1098 | } | |
1099 | ||
663996b3 MS |
1100 | static int service_coldplug(Unit *u) { |
1101 | Service *s = SERVICE(u); | |
1102 | int r; | |
1103 | ||
1104 | assert(s); | |
1105 | assert(s->state == SERVICE_DEAD); | |
1106 | ||
db2df898 MP |
1107 | if (s->deserialized_state == s->state) |
1108 | return 0; | |
663996b3 | 1109 | |
4c89c718 MP |
1110 | r = service_arm_timer(s, service_coldplug_timeout(s)); |
1111 | if (r < 0) | |
1112 | return r; | |
60f067b4 | 1113 | |
db2df898 MP |
1114 | if (s->main_pid > 0 && |
1115 | pid_is_unwaited(s->main_pid) && | |
1d42b86d | 1116 | (IN_SET(s->deserialized_state, |
db2df898 MP |
1117 | SERVICE_START, SERVICE_START_POST, |
1118 | SERVICE_RUNNING, SERVICE_RELOAD, | |
1119 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1120 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) { | |
1121 | r = unit_watch_pid(UNIT(s), s->main_pid); | |
1122 | if (r < 0) | |
1123 | return r; | |
1124 | } | |
663996b3 | 1125 | |
db2df898 MP |
1126 | if (s->control_pid > 0 && |
1127 | pid_is_unwaited(s->control_pid) && | |
1128 | IN_SET(s->deserialized_state, | |
1129 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
1130 | SERVICE_RELOAD, | |
1131 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1132 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { | |
1133 | r = unit_watch_pid(UNIT(s), s->control_pid); | |
1134 | if (r < 0) | |
1135 | return r; | |
663996b3 | 1136 | } |
60f067b4 | 1137 | |
db2df898 MP |
1138 | if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
1139 | unit_watch_all_pids(UNIT(s)); | |
1140 | ||
1141 | if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) | |
1142 | service_start_watchdog(s); | |
1143 | ||
8a584da2 MP |
1144 | if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
1145 | (void) unit_setup_dynamic_creds(u); | |
1146 | ||
1147 | if (UNIT_ISSET(s->accept_socket)) { | |
1148 | Socket* socket = SOCKET(UNIT_DEREF(s->accept_socket)); | |
1149 | ||
1150 | if (socket->max_connections_per_source > 0) { | |
1151 | SocketPeer *peer; | |
1152 | ||
1153 | /* Make a best-effort attempt at bumping the connection count */ | |
1154 | if (socket_acquire_peer(socket, s->socket_fd, &peer) > 0) { | |
1155 | socket_peer_unref(s->peer); | |
1156 | s->peer = peer; | |
1157 | } | |
1158 | } | |
1159 | } | |
1160 | ||
db2df898 | 1161 | service_set_state(s, s->deserialized_state); |
663996b3 MS |
1162 | return 0; |
1163 | } | |
1164 | ||
81c58355 MB |
1165 | static int service_collect_fds(Service *s, |
1166 | int **fds, | |
1167 | char ***fd_names, | |
1168 | unsigned *n_storage_fds, | |
1169 | unsigned *n_socket_fds) { | |
1170 | ||
6300502b | 1171 | _cleanup_strv_free_ char **rfd_names = NULL; |
e735f4d4 | 1172 | _cleanup_free_ int *rfds = NULL; |
81c58355 MB |
1173 | unsigned rn_socket_fds = 0, rn_storage_fds = 0; |
1174 | int r; | |
663996b3 MS |
1175 | |
1176 | assert(s); | |
1177 | assert(fds); | |
6300502b | 1178 | assert(fd_names); |
81c58355 | 1179 | assert(n_socket_fds); |
663996b3 | 1180 | |
6300502b | 1181 | if (s->socket_fd >= 0) { |
663996b3 | 1182 | |
6300502b | 1183 | /* Pass the per-connection socket */ |
663996b3 | 1184 | |
6300502b MP |
1185 | rfds = new(int, 1); |
1186 | if (!rfds) | |
1187 | return -ENOMEM; | |
1188 | rfds[0] = s->socket_fd; | |
663996b3 | 1189 | |
6300502b MP |
1190 | rfd_names = strv_new("connection", NULL); |
1191 | if (!rfd_names) | |
1192 | return -ENOMEM; | |
663996b3 | 1193 | |
81c58355 | 1194 | rn_socket_fds = 1; |
6300502b MP |
1195 | } else { |
1196 | Iterator i; | |
52ad194e | 1197 | void *v; |
6300502b | 1198 | Unit *u; |
663996b3 | 1199 | |
6300502b | 1200 | /* Pass all our configured sockets for singleton services */ |
663996b3 | 1201 | |
52ad194e | 1202 | HASHMAP_FOREACH_KEY(v, u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) { |
6300502b MP |
1203 | _cleanup_free_ int *cfds = NULL; |
1204 | Socket *sock; | |
1205 | int cn_fds; | |
663996b3 | 1206 | |
6300502b MP |
1207 | if (u->type != UNIT_SOCKET) |
1208 | continue; | |
1209 | ||
1210 | sock = SOCKET(u); | |
1211 | ||
1212 | cn_fds = socket_collect_fds(sock, &cfds); | |
1213 | if (cn_fds < 0) | |
1214 | return cn_fds; | |
663996b3 | 1215 | |
6300502b MP |
1216 | if (cn_fds <= 0) |
1217 | continue; | |
e735f4d4 | 1218 | |
6300502b MP |
1219 | if (!rfds) { |
1220 | rfds = cfds; | |
81c58355 | 1221 | rn_socket_fds = cn_fds; |
663996b3 | 1222 | |
6300502b MP |
1223 | cfds = NULL; |
1224 | } else { | |
1225 | int *t; | |
1226 | ||
81c58355 | 1227 | t = realloc(rfds, (rn_socket_fds + cn_fds) * sizeof(int)); |
6300502b MP |
1228 | if (!t) |
1229 | return -ENOMEM; | |
1230 | ||
81c58355 | 1231 | memcpy(t + rn_socket_fds, cfds, cn_fds * sizeof(int)); |
6300502b MP |
1232 | |
1233 | rfds = t; | |
81c58355 | 1234 | rn_socket_fds += cn_fds; |
6300502b MP |
1235 | } |
1236 | ||
1237 | r = strv_extend_n(&rfd_names, socket_fdname(sock), cn_fds); | |
1238 | if (r < 0) | |
1239 | return r; | |
663996b3 MS |
1240 | } |
1241 | } | |
1242 | ||
e735f4d4 MP |
1243 | if (s->n_fd_store > 0) { |
1244 | ServiceFDStore *fs; | |
81c58355 | 1245 | unsigned n_fds; |
6300502b | 1246 | char **nl; |
e735f4d4 MP |
1247 | int *t; |
1248 | ||
81c58355 | 1249 | t = realloc(rfds, (rn_socket_fds + s->n_fd_store) * sizeof(int)); |
e735f4d4 MP |
1250 | if (!t) |
1251 | return -ENOMEM; | |
1252 | ||
1253 | rfds = t; | |
6300502b | 1254 | |
81c58355 | 1255 | nl = realloc(rfd_names, (rn_socket_fds + s->n_fd_store + 1) * sizeof(char*)); |
6300502b MP |
1256 | if (!nl) |
1257 | return -ENOMEM; | |
1258 | ||
1259 | rfd_names = nl; | |
81c58355 | 1260 | n_fds = rn_socket_fds; |
6300502b MP |
1261 | |
1262 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
81c58355 MB |
1263 | rfds[n_fds] = fs->fd; |
1264 | rfd_names[n_fds] = strdup(strempty(fs->fdname)); | |
1265 | if (!rfd_names[n_fds]) | |
6300502b MP |
1266 | return -ENOMEM; |
1267 | ||
81c58355 MB |
1268 | rn_storage_fds++; |
1269 | n_fds++; | |
6300502b MP |
1270 | } |
1271 | ||
81c58355 | 1272 | rfd_names[n_fds] = NULL; |
e735f4d4 MP |
1273 | } |
1274 | ||
663996b3 | 1275 | *fds = rfds; |
6300502b | 1276 | *fd_names = rfd_names; |
81c58355 MB |
1277 | *n_socket_fds = rn_socket_fds; |
1278 | *n_storage_fds = rn_storage_fds; | |
663996b3 | 1279 | |
e735f4d4 | 1280 | rfds = NULL; |
6300502b MP |
1281 | rfd_names = NULL; |
1282 | ||
81c58355 | 1283 | return 0; |
663996b3 MS |
1284 | } |
1285 | ||
2897b343 MP |
1286 | static bool service_exec_needs_notify_socket(Service *s, ExecFlags flags) { |
1287 | assert(s); | |
1288 | ||
1289 | /* Notifications are accepted depending on the process and | |
1290 | * the access setting of the service: | |
1291 | * process: \ access: NONE MAIN EXEC ALL | |
1292 | * main no yes yes yes | |
1293 | * control no no yes yes | |
1294 | * other (forked) no no no yes */ | |
1295 | ||
1296 | if (flags & EXEC_IS_CONTROL) | |
1297 | /* A control process */ | |
1298 | return IN_SET(s->notify_access, NOTIFY_EXEC, NOTIFY_ALL); | |
1299 | ||
1300 | /* We only spawn main processes and control processes, so any | |
1301 | * process that is not a control process is a main process */ | |
1302 | return s->notify_access != NOTIFY_NONE; | |
1303 | } | |
1304 | ||
663996b3 MS |
1305 | static int service_spawn( |
1306 | Service *s, | |
1307 | ExecCommand *c, | |
5eef597e | 1308 | usec_t timeout, |
8a584da2 | 1309 | ExecFlags flags, |
663996b3 MS |
1310 | pid_t *_pid) { |
1311 | ||
5eef597e | 1312 | ExecParameters exec_params = { |
8a584da2 MP |
1313 | .flags = flags, |
1314 | .stdin_fd = -1, | |
1315 | .stdout_fd = -1, | |
1316 | .stderr_fd = -1, | |
5eef597e | 1317 | }; |
52ad194e MB |
1318 | _cleanup_strv_free_ char **final_env = NULL, **our_env = NULL, **fd_names = NULL; |
1319 | unsigned n_storage_fds = 0, n_socket_fds = 0, n_env = 0; | |
1320 | _cleanup_free_ int *fds = NULL; | |
1321 | pid_t pid; | |
6300502b MP |
1322 | int r; |
1323 | ||
663996b3 MS |
1324 | assert(s); |
1325 | assert(c); | |
1326 | assert(_pid); | |
1327 | ||
52ad194e MB |
1328 | r = unit_prepare_exec(UNIT(s)); |
1329 | if (r < 0) | |
1330 | return r; | |
1331 | ||
8a584da2 MP |
1332 | if (flags & EXEC_IS_CONTROL) { |
1333 | /* If this is a control process, mask the permissions/chroot application if this is requested. */ | |
1334 | if (s->permissions_start_only) | |
f5e65279 | 1335 | exec_params.flags &= ~EXEC_APPLY_SANDBOXING; |
8a584da2 MP |
1336 | if (s->root_directory_start_only) |
1337 | exec_params.flags &= ~EXEC_APPLY_CHROOT; | |
1338 | } | |
1339 | ||
8a584da2 | 1340 | if ((flags & EXEC_PASS_FDS) || |
663996b3 MS |
1341 | s->exec_context.std_input == EXEC_INPUT_SOCKET || |
1342 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1343 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1344 | ||
81c58355 | 1345 | r = service_collect_fds(s, &fds, &fd_names, &n_storage_fds, &n_socket_fds); |
6300502b | 1346 | if (r < 0) |
4c89c718 | 1347 | return r; |
663996b3 | 1348 | |
81c58355 | 1349 | log_unit_debug(UNIT(s), "Passing %i fds to service", n_storage_fds + n_socket_fds); |
663996b3 MS |
1350 | } |
1351 | ||
4c89c718 MP |
1352 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), timeout)); |
1353 | if (r < 0) | |
1354 | return r; | |
663996b3 | 1355 | |
8a584da2 | 1356 | our_env = new0(char*, 9); |
4c89c718 MP |
1357 | if (!our_env) |
1358 | return -ENOMEM; | |
663996b3 | 1359 | |
2897b343 | 1360 | if (service_exec_needs_notify_socket(s, flags)) |
4c89c718 MP |
1361 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) |
1362 | return -ENOMEM; | |
663996b3 MS |
1363 | |
1364 | if (s->main_pid > 0) | |
4c89c718 MP |
1365 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) |
1366 | return -ENOMEM; | |
663996b3 | 1367 | |
8a584da2 | 1368 | if (MANAGER_IS_USER(UNIT(s)->manager)) |
f5e65279 | 1369 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid_cached()) < 0) |
4c89c718 | 1370 | return -ENOMEM; |
663996b3 | 1371 | |
6300502b | 1372 | if (s->socket_fd >= 0) { |
e3bff60a MP |
1373 | union sockaddr_union sa; |
1374 | socklen_t salen = sizeof(sa); | |
1375 | ||
52ad194e MB |
1376 | /* If this is a per-connection service instance, let's set $REMOTE_ADDR and $REMOTE_PORT to something |
1377 | * useful. Note that we do this only when we are still connected at this point in time, which we might | |
1378 | * very well not be. Hence we ignore all errors when retrieving peer information (as that might result | |
1379 | * in ENOTCONN), and just use whate we can use. */ | |
e3bff60a | 1380 | |
52ad194e MB |
1381 | if (getpeername(s->socket_fd, &sa.sa, &salen) >= 0 && |
1382 | IN_SET(sa.sa.sa_family, AF_INET, AF_INET6, AF_VSOCK)) { | |
8a584da2 | 1383 | |
e3bff60a MP |
1384 | _cleanup_free_ char *addr = NULL; |
1385 | char *t; | |
2897b343 | 1386 | unsigned port; |
e3bff60a MP |
1387 | |
1388 | r = sockaddr_pretty(&sa.sa, salen, true, false, &addr); | |
1389 | if (r < 0) | |
4c89c718 | 1390 | return r; |
e3bff60a MP |
1391 | |
1392 | t = strappend("REMOTE_ADDR=", addr); | |
4c89c718 MP |
1393 | if (!t) |
1394 | return -ENOMEM; | |
e3bff60a MP |
1395 | our_env[n_env++] = t; |
1396 | ||
2897b343 MP |
1397 | r = sockaddr_port(&sa.sa, &port); |
1398 | if (r < 0) | |
1399 | return r; | |
e3bff60a | 1400 | |
4c89c718 MP |
1401 | if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) |
1402 | return -ENOMEM; | |
e3bff60a MP |
1403 | our_env[n_env++] = t; |
1404 | } | |
1405 | } | |
1406 | ||
8a584da2 MP |
1407 | if (flags & EXEC_SETENV_RESULT) { |
1408 | if (asprintf(our_env + n_env++, "SERVICE_RESULT=%s", service_result_to_string(s->result)) < 0) | |
1409 | return -ENOMEM; | |
1410 | ||
1411 | if (s->main_exec_status.pid > 0 && | |
1412 | dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { | |
1413 | if (asprintf(our_env + n_env++, "EXIT_CODE=%s", sigchld_code_to_string(s->main_exec_status.code)) < 0) | |
1414 | return -ENOMEM; | |
1415 | ||
1416 | if (s->main_exec_status.code == CLD_EXITED) | |
1417 | r = asprintf(our_env + n_env++, "EXIT_STATUS=%i", s->main_exec_status.status); | |
1418 | else | |
1419 | r = asprintf(our_env + n_env++, "EXIT_STATUS=%s", signal_to_string(s->main_exec_status.status)); | |
1420 | if (r < 0) | |
1421 | return -ENOMEM; | |
1422 | } | |
1423 | } | |
1424 | ||
f5e65279 MB |
1425 | manager_set_exec_params(UNIT(s)->manager, &exec_params); |
1426 | unit_set_exec_params(UNIT(s), &exec_params); | |
1427 | ||
1428 | final_env = strv_env_merge(2, exec_params.environment, our_env, NULL); | |
4c89c718 MP |
1429 | if (!final_env) |
1430 | return -ENOMEM; | |
663996b3 | 1431 | |
f5e65279 MB |
1432 | /* System services should get a new keyring by default. */ |
1433 | SET_FLAG(exec_params.flags, EXEC_NEW_KEYRING, MANAGER_IS_SYSTEM(UNIT(s)->manager)); | |
1434 | ||
1435 | /* System D-Bus needs nss-systemd disabled, so that we don't deadlock */ | |
1436 | SET_FLAG(exec_params.flags, EXEC_NSS_BYPASS_BUS, | |
1437 | MANAGER_IS_SYSTEM(UNIT(s)->manager) && unit_has_name(UNIT(s), SPECIAL_DBUS_SERVICE)); | |
14228c0d | 1438 | |
2897b343 | 1439 | exec_params.argv = c->argv; |
8a584da2 | 1440 | exec_params.environment = final_env; |
5eef597e | 1441 | exec_params.fds = fds; |
6300502b | 1442 | exec_params.fd_names = fd_names; |
81c58355 MB |
1443 | exec_params.n_storage_fds = n_storage_fds; |
1444 | exec_params.n_socket_fds = n_socket_fds; | |
5eef597e | 1445 | exec_params.watchdog_usec = s->watchdog_usec; |
db2df898 | 1446 | exec_params.selinux_context_net = s->socket_fd_selinux_context_net; |
5eef597e MP |
1447 | if (s->type == SERVICE_IDLE) |
1448 | exec_params.idle_pipe = UNIT(s)->manager->idle_pipe; | |
db2df898 MP |
1449 | exec_params.stdin_fd = s->stdin_fd; |
1450 | exec_params.stdout_fd = s->stdout_fd; | |
1451 | exec_params.stderr_fd = s->stderr_fd; | |
5eef597e | 1452 | |
e3bff60a MP |
1453 | r = exec_spawn(UNIT(s), |
1454 | c, | |
663996b3 | 1455 | &s->exec_context, |
5eef597e | 1456 | &exec_params, |
60f067b4 | 1457 | s->exec_runtime, |
8a584da2 | 1458 | &s->dynamic_creds, |
663996b3 MS |
1459 | &pid); |
1460 | if (r < 0) | |
4c89c718 | 1461 | return r; |
663996b3 MS |
1462 | |
1463 | r = unit_watch_pid(UNIT(s), pid); | |
2897b343 | 1464 | if (r < 0) /* FIXME: we need to do something here */ |
4c89c718 | 1465 | return r; |
663996b3 MS |
1466 | |
1467 | *_pid = pid; | |
1468 | ||
1469 | return 0; | |
663996b3 MS |
1470 | } |
1471 | ||
1472 | static int main_pid_good(Service *s) { | |
1473 | assert(s); | |
1474 | ||
f5e65279 | 1475 | /* Returns 0 if the pid is dead, > 0 if it is good, < 0 if we don't know */ |
663996b3 | 1476 | |
fb183854 | 1477 | /* If we know the pid file, then let's just check if it is |
663996b3 MS |
1478 | * still valid */ |
1479 | if (s->main_pid_known) { | |
1480 | ||
1481 | /* If it's an alien child let's check if it is still | |
1482 | * alive ... */ | |
14228c0d | 1483 | if (s->main_pid_alien && s->main_pid > 0) |
60f067b4 | 1484 | return pid_is_alive(s->main_pid); |
663996b3 MS |
1485 | |
1486 | /* .. otherwise assume we'll get a SIGCHLD for it, | |
1487 | * which we really should wait for to collect exit | |
1488 | * status and code */ | |
1489 | return s->main_pid > 0; | |
1490 | } | |
1491 | ||
1492 | /* We don't know the pid */ | |
1493 | return -EAGAIN; | |
1494 | } | |
1495 | ||
f5e65279 | 1496 | static int control_pid_good(Service *s) { |
663996b3 MS |
1497 | assert(s); |
1498 | ||
f5e65279 MB |
1499 | /* Returns 0 if the control PID is dead, > 0 if it is good. We never actually return < 0 here, but in order to |
1500 | * make this function as similar as possible to main_pid_good() and cgroup_good(), we pretend that < 0 also | |
1501 | * means: we can't figure it out. */ | |
1502 | ||
663996b3 MS |
1503 | return s->control_pid > 0; |
1504 | } | |
1505 | ||
1506 | static int cgroup_good(Service *s) { | |
1507 | int r; | |
1508 | ||
1509 | assert(s); | |
1510 | ||
f5e65279 MB |
1511 | /* Returns 0 if the cgroup is empty or doesn't exist, > 0 if it is exists and is populated, < 0 if we can't |
1512 | * figure it out */ | |
1513 | ||
14228c0d MB |
1514 | if (!UNIT(s)->cgroup_path) |
1515 | return 0; | |
1516 | ||
d9dfd233 | 1517 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path); |
663996b3 MS |
1518 | if (r < 0) |
1519 | return r; | |
1520 | ||
f5e65279 | 1521 | return r == 0; |
663996b3 MS |
1522 | } |
1523 | ||
e3bff60a MP |
1524 | static bool service_shall_restart(Service *s) { |
1525 | assert(s); | |
1526 | ||
1527 | /* Don't restart after manual stops */ | |
1528 | if (s->forbid_restart) | |
1529 | return false; | |
1530 | ||
1531 | /* Never restart if this is configured as special exception */ | |
1532 | if (exit_status_set_test(&s->restart_prevent_status, s->main_exec_status.code, s->main_exec_status.status)) | |
1533 | return false; | |
1534 | ||
1535 | /* Restart if the exit code/status are configured as restart triggers */ | |
1536 | if (exit_status_set_test(&s->restart_force_status, s->main_exec_status.code, s->main_exec_status.status)) | |
1537 | return true; | |
1538 | ||
1539 | switch (s->restart) { | |
1540 | ||
1541 | case SERVICE_RESTART_NO: | |
1542 | return false; | |
1543 | ||
1544 | case SERVICE_RESTART_ALWAYS: | |
1545 | return true; | |
1546 | ||
1547 | case SERVICE_RESTART_ON_SUCCESS: | |
1548 | return s->result == SERVICE_SUCCESS; | |
1549 | ||
1550 | case SERVICE_RESTART_ON_FAILURE: | |
1551 | return s->result != SERVICE_SUCCESS; | |
1552 | ||
1553 | case SERVICE_RESTART_ON_ABNORMAL: | |
1554 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE); | |
1555 | ||
1556 | case SERVICE_RESTART_ON_WATCHDOG: | |
1557 | return s->result == SERVICE_FAILURE_WATCHDOG; | |
1558 | ||
1559 | case SERVICE_RESTART_ON_ABORT: | |
1560 | return IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP); | |
1561 | ||
1562 | default: | |
1563 | assert_not_reached("unknown restart setting"); | |
1564 | } | |
1565 | } | |
1566 | ||
52ad194e MB |
1567 | static bool service_will_restart(Unit *u) { |
1568 | Service *s = SERVICE(u); | |
1569 | ||
f5e65279 MB |
1570 | assert(s); |
1571 | ||
52ad194e MB |
1572 | if (s->will_auto_restart) |
1573 | return true; | |
f5e65279 MB |
1574 | if (s->state == SERVICE_AUTO_RESTART) |
1575 | return true; | |
1576 | if (!UNIT(s)->job) | |
1577 | return false; | |
1578 | if (UNIT(s)->job->type == JOB_START) | |
1579 | return true; | |
1580 | return false; | |
1581 | } | |
1582 | ||
663996b3 MS |
1583 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
1584 | int r; | |
f5e65279 | 1585 | |
663996b3 MS |
1586 | assert(s); |
1587 | ||
f5e65279 MB |
1588 | /* If there's a stop job queued before we enter the DEAD state, we shouldn't act on Restart=, in order to not |
1589 | * undo what has already been enqueued. */ | |
1590 | if (unit_stop_pending(UNIT(s))) | |
1591 | allow_restart = false; | |
1592 | ||
8a584da2 | 1593 | if (s->result == SERVICE_SUCCESS) |
663996b3 MS |
1594 | s->result = f; |
1595 | ||
f5e65279 MB |
1596 | if (s->result != SERVICE_SUCCESS) |
1597 | log_unit_warning(UNIT(s), "Failed with result '%s'.", service_result_to_string(s->result)); | |
1598 | ||
52ad194e MB |
1599 | if (allow_restart && service_shall_restart(s)) |
1600 | s->will_auto_restart = true; | |
663996b3 | 1601 | |
52ad194e MB |
1602 | /* Make sure service_release_resources() doesn't destroy our FD store, while we are changing through |
1603 | * SERVICE_FAILED/SERVICE_DEAD before entering into SERVICE_AUTO_RESTART. */ | |
1604 | s->n_keep_fd_store ++; | |
60f067b4 | 1605 | |
52ad194e MB |
1606 | service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD); |
1607 | ||
1608 | if (s->will_auto_restart) { | |
1609 | s->will_auto_restart = false; | |
663996b3 | 1610 | |
4c89c718 | 1611 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec)); |
52ad194e MB |
1612 | if (r < 0) { |
1613 | s->n_keep_fd_store--; | |
663996b3 | 1614 | goto fail; |
52ad194e | 1615 | } |
663996b3 MS |
1616 | |
1617 | service_set_state(s, SERVICE_AUTO_RESTART); | |
f5e65279 MB |
1618 | } else |
1619 | /* If we shan't restart, then flush out the restart counter. But don't do that immediately, so that the | |
1620 | * user can still introspect the counter. Do so on the next start. */ | |
1621 | s->flush_n_restarts = true; | |
663996b3 | 1622 | |
52ad194e MB |
1623 | /* The new state is in effect, let's decrease the fd store ref counter again. Let's also readd us to the GC |
1624 | * queue, so that the fd store is possibly gc'ed again */ | |
1625 | s->n_keep_fd_store--; | |
1626 | unit_add_to_gc_queue(UNIT(s)); | |
1627 | ||
e3bff60a | 1628 | /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */ |
663996b3 MS |
1629 | s->forbid_restart = false; |
1630 | ||
60f067b4 JS |
1631 | /* We want fresh tmpdirs in case service is started again immediately */ |
1632 | exec_runtime_destroy(s->exec_runtime); | |
1633 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); | |
1634 | ||
f5e65279 | 1635 | if (s->exec_context.runtime_directory_preserve_mode == EXEC_PRESERVE_NO || |
52ad194e | 1636 | (s->exec_context.runtime_directory_preserve_mode == EXEC_PRESERVE_RESTART && !service_will_restart(UNIT(s)))) |
f5e65279 MB |
1637 | /* Also, remove the runtime directory */ |
1638 | exec_context_destroy_runtime_directory(&s->exec_context, UNIT(s)->manager->prefix[EXEC_DIRECTORY_RUNTIME]); | |
663996b3 | 1639 | |
8a584da2 MP |
1640 | /* Get rid of the IPC bits of the user */ |
1641 | unit_unref_uid_gid(UNIT(s), true); | |
1642 | ||
1643 | /* Release the user, and destroy it if we are the only remaining owner */ | |
1644 | dynamic_creds_destroy(&s->dynamic_creds); | |
1645 | ||
14228c0d MB |
1646 | /* Try to delete the pid file. At this point it will be |
1647 | * out-of-date, and some software might be confused by it, so | |
1648 | * let's remove it. */ | |
1649 | if (s->pid_file) | |
4c89c718 | 1650 | (void) unlink(s->pid_file); |
14228c0d | 1651 | |
663996b3 MS |
1652 | return; |
1653 | ||
1654 | fail: | |
e3bff60a | 1655 | log_unit_warning_errno(UNIT(s), r, "Failed to run install restart timer: %m"); |
663996b3 MS |
1656 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
1657 | } | |
1658 | ||
663996b3 MS |
1659 | static void service_enter_stop_post(Service *s, ServiceResult f) { |
1660 | int r; | |
1661 | assert(s); | |
1662 | ||
8a584da2 | 1663 | if (s->result == SERVICE_SUCCESS) |
663996b3 MS |
1664 | s->result = f; |
1665 | ||
1666 | service_unwatch_control_pid(s); | |
60f067b4 | 1667 | unit_watch_all_pids(UNIT(s)); |
663996b3 MS |
1668 | |
1669 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; | |
1670 | if (s->control_command) { | |
1671 | s->control_command_id = SERVICE_EXEC_STOP_POST; | |
1672 | ||
1673 | r = service_spawn(s, | |
1674 | s->control_command, | |
5eef597e | 1675 | s->timeout_stop_usec, |
f5e65279 | 1676 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_IS_CONTROL|EXEC_SETENV_RESULT, |
663996b3 MS |
1677 | &s->control_pid); |
1678 | if (r < 0) | |
1679 | goto fail; | |
1680 | ||
663996b3 MS |
1681 | service_set_state(s, SERVICE_STOP_POST); |
1682 | } else | |
60f067b4 | 1683 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
663996b3 MS |
1684 | |
1685 | return; | |
1686 | ||
1687 | fail: | |
e3bff60a | 1688 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m"); |
663996b3 MS |
1689 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
1690 | } | |
1691 | ||
e3bff60a MP |
1692 | static int state_to_kill_operation(ServiceState state) { |
1693 | switch (state) { | |
1694 | ||
1695 | case SERVICE_STOP_SIGABRT: | |
1696 | return KILL_ABORT; | |
1697 | ||
1698 | case SERVICE_STOP_SIGTERM: | |
1699 | case SERVICE_FINAL_SIGTERM: | |
1700 | return KILL_TERMINATE; | |
1701 | ||
1702 | case SERVICE_STOP_SIGKILL: | |
1703 | case SERVICE_FINAL_SIGKILL: | |
1704 | return KILL_KILL; | |
1705 | ||
1706 | default: | |
1707 | return _KILL_OPERATION_INVALID; | |
1708 | } | |
1709 | } | |
1710 | ||
663996b3 MS |
1711 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
1712 | int r; | |
1713 | ||
1714 | assert(s); | |
1715 | ||
8a584da2 | 1716 | if (s->result == SERVICE_SUCCESS) |
663996b3 MS |
1717 | s->result = f; |
1718 | ||
60f067b4 JS |
1719 | unit_watch_all_pids(UNIT(s)); |
1720 | ||
663996b3 MS |
1721 | r = unit_kill_context( |
1722 | UNIT(s), | |
1723 | &s->kill_context, | |
e3bff60a | 1724 | state_to_kill_operation(state), |
663996b3 MS |
1725 | s->main_pid, |
1726 | s->control_pid, | |
1727 | s->main_pid_alien); | |
1728 | if (r < 0) | |
1729 | goto fail; | |
1730 | ||
1731 | if (r > 0) { | |
4c89c718 MP |
1732 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec)); |
1733 | if (r < 0) | |
1734 | goto fail; | |
663996b3 MS |
1735 | |
1736 | service_set_state(s, state); | |
e3bff60a | 1737 | } else if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM) && s->kill_context.send_sigkill) |
60f067b4 | 1738 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); |
e3bff60a | 1739 | else if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
663996b3 | 1740 | service_enter_stop_post(s, SERVICE_SUCCESS); |
e3bff60a | 1741 | else if (state == SERVICE_FINAL_SIGTERM && s->kill_context.send_sigkill) |
60f067b4 | 1742 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); |
663996b3 MS |
1743 | else |
1744 | service_enter_dead(s, SERVICE_SUCCESS, true); | |
1745 | ||
1746 | return; | |
1747 | ||
1748 | fail: | |
e3bff60a | 1749 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
663996b3 | 1750 | |
e3bff60a | 1751 | if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
663996b3 MS |
1752 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
1753 | else | |
1754 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); | |
1755 | } | |
1756 | ||
5eef597e MP |
1757 | static void service_enter_stop_by_notify(Service *s) { |
1758 | assert(s); | |
1759 | ||
1760 | unit_watch_all_pids(UNIT(s)); | |
1761 | ||
4c89c718 | 1762 | service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_stop_usec)); |
5eef597e MP |
1763 | |
1764 | /* The service told us it's stopping, so it's as if we SIGTERM'd it. */ | |
1765 | service_set_state(s, SERVICE_STOP_SIGTERM); | |
1766 | } | |
1767 | ||
663996b3 MS |
1768 | static void service_enter_stop(Service *s, ServiceResult f) { |
1769 | int r; | |
1770 | ||
1771 | assert(s); | |
1772 | ||
8a584da2 | 1773 | if (s->result == SERVICE_SUCCESS) |
663996b3 MS |
1774 | s->result = f; |
1775 | ||
1776 | service_unwatch_control_pid(s); | |
60f067b4 | 1777 | unit_watch_all_pids(UNIT(s)); |
663996b3 MS |
1778 | |
1779 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; | |
1780 | if (s->control_command) { | |
1781 | s->control_command_id = SERVICE_EXEC_STOP; | |
1782 | ||
1783 | r = service_spawn(s, | |
1784 | s->control_command, | |
5eef597e | 1785 | s->timeout_stop_usec, |
f5e65279 | 1786 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_SETENV_RESULT, |
663996b3 MS |
1787 | &s->control_pid); |
1788 | if (r < 0) | |
1789 | goto fail; | |
1790 | ||
1791 | service_set_state(s, SERVICE_STOP); | |
1792 | } else | |
1793 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); | |
1794 | ||
1795 | return; | |
1796 | ||
1797 | fail: | |
e3bff60a | 1798 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop' task: %m"); |
663996b3 MS |
1799 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
1800 | } | |
1801 | ||
d9dfd233 MP |
1802 | static bool service_good(Service *s) { |
1803 | int main_pid_ok; | |
1804 | assert(s); | |
1805 | ||
1806 | if (s->type == SERVICE_DBUS && !s->bus_name_good) | |
1807 | return false; | |
1808 | ||
1809 | main_pid_ok = main_pid_good(s); | |
1810 | if (main_pid_ok > 0) /* It's alive */ | |
1811 | return true; | |
1812 | if (main_pid_ok == 0) /* It's dead */ | |
1813 | return false; | |
1814 | ||
1815 | /* OK, we don't know anything about the main PID, maybe | |
1816 | * because there is none. Let's check the control group | |
1817 | * instead. */ | |
1818 | ||
1819 | return cgroup_good(s) != 0; | |
1820 | } | |
1821 | ||
663996b3 | 1822 | static void service_enter_running(Service *s, ServiceResult f) { |
663996b3 MS |
1823 | assert(s); |
1824 | ||
8a584da2 | 1825 | if (s->result == SERVICE_SUCCESS) |
663996b3 MS |
1826 | s->result = f; |
1827 | ||
4c89c718 MP |
1828 | service_unwatch_control_pid(s); |
1829 | ||
d9dfd233 | 1830 | if (service_good(s)) { |
5eef597e MP |
1831 | |
1832 | /* If there are any queued up sd_notify() | |
1833 | * notifications, process them now */ | |
1834 | if (s->notify_state == NOTIFY_RELOADING) | |
1835 | service_enter_reload_by_notify(s); | |
1836 | else if (s->notify_state == NOTIFY_STOPPING) | |
1837 | service_enter_stop_by_notify(s); | |
4c89c718 | 1838 | else { |
5eef597e | 1839 | service_set_state(s, SERVICE_RUNNING); |
4c89c718 MP |
1840 | service_arm_timer(s, usec_add(UNIT(s)->active_enter_timestamp.monotonic, s->runtime_max_usec)); |
1841 | } | |
5eef597e | 1842 | |
2897b343 MP |
1843 | } else if (f != SERVICE_SUCCESS) |
1844 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
1845 | else if (s->remain_after_exit) | |
663996b3 MS |
1846 | service_set_state(s, SERVICE_EXITED); |
1847 | else | |
1848 | service_enter_stop(s, SERVICE_SUCCESS); | |
1849 | } | |
1850 | ||
1851 | static void service_enter_start_post(Service *s) { | |
1852 | int r; | |
1853 | assert(s); | |
1854 | ||
1855 | service_unwatch_control_pid(s); | |
60f067b4 | 1856 | service_reset_watchdog(s); |
663996b3 MS |
1857 | |
1858 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; | |
1859 | if (s->control_command) { | |
1860 | s->control_command_id = SERVICE_EXEC_START_POST; | |
1861 | ||
1862 | r = service_spawn(s, | |
1863 | s->control_command, | |
5eef597e | 1864 | s->timeout_start_usec, |
f5e65279 | 1865 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL, |
663996b3 MS |
1866 | &s->control_pid); |
1867 | if (r < 0) | |
1868 | goto fail; | |
1869 | ||
1870 | service_set_state(s, SERVICE_START_POST); | |
1871 | } else | |
1872 | service_enter_running(s, SERVICE_SUCCESS); | |
1873 | ||
1874 | return; | |
1875 | ||
1876 | fail: | |
e3bff60a | 1877 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m"); |
663996b3 MS |
1878 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
1879 | } | |
1880 | ||
52ad194e | 1881 | static void service_kill_control_process(Service *s) { |
f5e65279 | 1882 | int r; |
14228c0d | 1883 | |
f5e65279 MB |
1884 | assert(s); |
1885 | ||
52ad194e MB |
1886 | if (s->control_pid <= 0) |
1887 | return; | |
f5e65279 | 1888 | |
52ad194e MB |
1889 | r = kill_and_sigcont(s->control_pid, SIGKILL); |
1890 | if (r < 0) { | |
1891 | _cleanup_free_ char *comm = NULL; | |
f5e65279 | 1892 | |
52ad194e | 1893 | (void) get_process_comm(s->control_pid, &comm); |
f5e65279 | 1894 | |
52ad194e MB |
1895 | log_unit_debug_errno(UNIT(s), r, "Failed to kill control process " PID_FMT " (%s), ignoring: %m", |
1896 | s->control_pid, strna(comm)); | |
f5e65279 | 1897 | } |
14228c0d MB |
1898 | } |
1899 | ||
663996b3 | 1900 | static void service_enter_start(Service *s) { |
14228c0d | 1901 | ExecCommand *c; |
4c89c718 | 1902 | usec_t timeout; |
663996b3 MS |
1903 | pid_t pid; |
1904 | int r; | |
663996b3 MS |
1905 | |
1906 | assert(s); | |
1907 | ||
14228c0d MB |
1908 | service_unwatch_control_pid(s); |
1909 | service_unwatch_main_pid(s); | |
663996b3 | 1910 | |
52ad194e | 1911 | unit_warn_leftover_processes(UNIT(s)); |
663996b3 MS |
1912 | |
1913 | if (s->type == SERVICE_FORKING) { | |
1914 | s->control_command_id = SERVICE_EXEC_START; | |
1915 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
1916 | ||
1917 | s->main_command = NULL; | |
1918 | } else { | |
1919 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
1920 | s->control_command = NULL; | |
1921 | ||
1922 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
1923 | } | |
1924 | ||
5eef597e | 1925 | if (!c) { |
8a584da2 MP |
1926 | if (s->type != SERVICE_ONESHOT) { |
1927 | /* There's no command line configured for the main command? Hmm, that is strange. This can only | |
1928 | * happen if the configuration changes at runtime. In this case, let's enter a failure | |
1929 | * state. */ | |
1930 | log_unit_error(UNIT(s), "There's no 'start' task anymore we could start: %m"); | |
1931 | r = -ENXIO; | |
1932 | goto fail; | |
1933 | } | |
1934 | ||
5eef597e MP |
1935 | service_enter_start_post(s); |
1936 | return; | |
1937 | } | |
1938 | ||
4c89c718 MP |
1939 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) |
1940 | /* For simple + idle this is the main process. We don't apply any timeout here, but | |
1941 | * service_enter_running() will later apply the .runtime_max_usec timeout. */ | |
1942 | timeout = USEC_INFINITY; | |
1943 | else | |
1944 | timeout = s->timeout_start_usec; | |
1945 | ||
663996b3 MS |
1946 | r = service_spawn(s, |
1947 | c, | |
4c89c718 | 1948 | timeout, |
f5e65279 | 1949 | EXEC_PASS_FDS|EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_SET_WATCHDOG, |
663996b3 MS |
1950 | &pid); |
1951 | if (r < 0) | |
1952 | goto fail; | |
1953 | ||
4c89c718 | 1954 | if (IN_SET(s->type, SERVICE_SIMPLE, SERVICE_IDLE)) { |
663996b3 MS |
1955 | /* For simple services we immediately start |
1956 | * the START_POST binaries. */ | |
1957 | ||
1958 | service_set_main_pid(s, pid); | |
1959 | service_enter_start_post(s); | |
1960 | ||
1961 | } else if (s->type == SERVICE_FORKING) { | |
1962 | ||
1963 | /* For forking services we wait until the start | |
1964 | * process exited. */ | |
1965 | ||
1966 | s->control_pid = pid; | |
1967 | service_set_state(s, SERVICE_START); | |
1968 | ||
4c89c718 | 1969 | } else if (IN_SET(s->type, SERVICE_ONESHOT, SERVICE_DBUS, SERVICE_NOTIFY)) { |
663996b3 MS |
1970 | |
1971 | /* For oneshot services we wait until the start | |
1972 | * process exited, too, but it is our main process. */ | |
1973 | ||
1974 | /* For D-Bus services we know the main pid right away, | |
1975 | * but wait for the bus name to appear on the | |
1976 | * bus. Notify services are similar. */ | |
1977 | ||
1978 | service_set_main_pid(s, pid); | |
1979 | service_set_state(s, SERVICE_START); | |
1980 | } else | |
1981 | assert_not_reached("Unknown service type"); | |
1982 | ||
1983 | return; | |
1984 | ||
1985 | fail: | |
e3bff60a | 1986 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start' task: %m"); |
2897b343 | 1987 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
663996b3 MS |
1988 | } |
1989 | ||
1990 | static void service_enter_start_pre(Service *s) { | |
1991 | int r; | |
1992 | ||
1993 | assert(s); | |
1994 | ||
1995 | service_unwatch_control_pid(s); | |
1996 | ||
1997 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; | |
1998 | if (s->control_command) { | |
52ad194e MB |
1999 | |
2000 | unit_warn_leftover_processes(UNIT(s)); | |
663996b3 MS |
2001 | |
2002 | s->control_command_id = SERVICE_EXEC_START_PRE; | |
2003 | ||
2004 | r = service_spawn(s, | |
2005 | s->control_command, | |
5eef597e | 2006 | s->timeout_start_usec, |
f5e65279 | 2007 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL|EXEC_APPLY_TTY_STDIN, |
663996b3 MS |
2008 | &s->control_pid); |
2009 | if (r < 0) | |
2010 | goto fail; | |
2011 | ||
2012 | service_set_state(s, SERVICE_START_PRE); | |
2013 | } else | |
2014 | service_enter_start(s); | |
2015 | ||
2016 | return; | |
2017 | ||
2018 | fail: | |
e3bff60a | 2019 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m"); |
663996b3 MS |
2020 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
2021 | } | |
2022 | ||
2023 | static void service_enter_restart(Service *s) { | |
4c89c718 | 2024 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
663996b3 | 2025 | int r; |
663996b3 MS |
2026 | |
2027 | assert(s); | |
663996b3 MS |
2028 | |
2029 | if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) { | |
2030 | /* Don't restart things if we are going down anyway */ | |
e3bff60a | 2031 | log_unit_info(UNIT(s), "Stop job pending for unit, delaying automatic restart."); |
663996b3 | 2032 | |
4c89c718 | 2033 | r = service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->restart_usec)); |
663996b3 MS |
2034 | if (r < 0) |
2035 | goto fail; | |
2036 | ||
2037 | return; | |
2038 | } | |
2039 | ||
2040 | /* Any units that are bound to this service must also be | |
2041 | * restarted. We use JOB_RESTART (instead of the more obvious | |
2042 | * JOB_START) here so that those dependency jobs will be added | |
2043 | * as well. */ | |
db2df898 | 2044 | r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, &error, NULL); |
663996b3 MS |
2045 | if (r < 0) |
2046 | goto fail; | |
2047 | ||
f5e65279 MB |
2048 | /* Count the jobs we enqueue for restarting. This counter is maintained as long as the unit isn't fully |
2049 | * stopped, i.e. as long as it remains up or remains in auto-start states. The use can reset the counter | |
2050 | * explicitly however via the usual "systemctl reset-failure" logic. */ | |
2051 | s->n_restarts ++; | |
2052 | s->flush_n_restarts = false; | |
2053 | ||
2054 | log_struct(LOG_INFO, | |
2055 | "MESSAGE_ID=" SD_MESSAGE_UNIT_RESTART_SCHEDULED_STR, | |
2056 | LOG_UNIT_ID(UNIT(s)), | |
2057 | LOG_UNIT_INVOCATION_ID(UNIT(s)), | |
2058 | LOG_UNIT_MESSAGE(UNIT(s), "Scheduled restart job, restart counter is at %u.", s->n_restarts), | |
2059 | "N_RESTARTS=%u", s->n_restarts, | |
2060 | NULL); | |
2061 | ||
2062 | /* Notify clients about changed restart counter */ | |
2063 | unit_add_to_dbus_queue(UNIT(s)); | |
2064 | ||
663996b3 MS |
2065 | /* Note that we stay in the SERVICE_AUTO_RESTART state here, |
2066 | * it will be canceled as part of the service_stop() call that | |
2067 | * is executed as part of JOB_RESTART. */ | |
2068 | ||
663996b3 MS |
2069 | return; |
2070 | ||
2071 | fail: | |
e3bff60a | 2072 | log_unit_warning(UNIT(s), "Failed to schedule restart job: %s", bus_error_message(&error, -r)); |
663996b3 | 2073 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
663996b3 MS |
2074 | } |
2075 | ||
5eef597e | 2076 | static void service_enter_reload_by_notify(Service *s) { |
f5e65279 MB |
2077 | _cleanup_(sd_bus_error_free) sd_bus_error error = SD_BUS_ERROR_NULL; |
2078 | int r; | |
2079 | ||
5eef597e MP |
2080 | assert(s); |
2081 | ||
4c89c718 | 2082 | service_arm_timer(s, usec_add(now(CLOCK_MONOTONIC), s->timeout_start_usec)); |
5eef597e | 2083 | service_set_state(s, SERVICE_RELOAD); |
f5e65279 MB |
2084 | |
2085 | /* service_enter_reload_by_notify is never called during a reload, thus no loops are possible. */ | |
2086 | r = manager_propagate_reload(UNIT(s)->manager, UNIT(s), JOB_FAIL, &error); | |
2087 | if (r < 0) | |
2088 | log_unit_warning(UNIT(s), "Failed to schedule propagation of reload: %s", bus_error_message(&error, -r)); | |
5eef597e MP |
2089 | } |
2090 | ||
663996b3 MS |
2091 | static void service_enter_reload(Service *s) { |
2092 | int r; | |
2093 | ||
2094 | assert(s); | |
2095 | ||
2096 | service_unwatch_control_pid(s); | |
6300502b | 2097 | s->reload_result = SERVICE_SUCCESS; |
663996b3 MS |
2098 | |
2099 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; | |
2100 | if (s->control_command) { | |
2101 | s->control_command_id = SERVICE_EXEC_RELOAD; | |
2102 | ||
2103 | r = service_spawn(s, | |
2104 | s->control_command, | |
5eef597e | 2105 | s->timeout_start_usec, |
f5e65279 | 2106 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL, |
663996b3 MS |
2107 | &s->control_pid); |
2108 | if (r < 0) | |
2109 | goto fail; | |
2110 | ||
2111 | service_set_state(s, SERVICE_RELOAD); | |
2112 | } else | |
2113 | service_enter_running(s, SERVICE_SUCCESS); | |
2114 | ||
2115 | return; | |
2116 | ||
2117 | fail: | |
e3bff60a | 2118 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'reload' task: %m"); |
663996b3 MS |
2119 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
2120 | service_enter_running(s, SERVICE_SUCCESS); | |
2121 | } | |
2122 | ||
2123 | static void service_run_next_control(Service *s) { | |
4c89c718 | 2124 | usec_t timeout; |
663996b3 MS |
2125 | int r; |
2126 | ||
2127 | assert(s); | |
2128 | assert(s->control_command); | |
2129 | assert(s->control_command->command_next); | |
2130 | ||
2131 | assert(s->control_command_id != SERVICE_EXEC_START); | |
2132 | ||
2133 | s->control_command = s->control_command->command_next; | |
2134 | service_unwatch_control_pid(s); | |
2135 | ||
4c89c718 MP |
2136 | if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
2137 | timeout = s->timeout_start_usec; | |
2138 | else | |
2139 | timeout = s->timeout_stop_usec; | |
2140 | ||
663996b3 MS |
2141 | r = service_spawn(s, |
2142 | s->control_command, | |
4c89c718 | 2143 | timeout, |
f5e65279 | 2144 | EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_IS_CONTROL| |
8a584da2 MP |
2145 | (IN_SET(s->control_command_id, SERVICE_EXEC_START_PRE, SERVICE_EXEC_STOP_POST) ? EXEC_APPLY_TTY_STDIN : 0)| |
2146 | (IN_SET(s->control_command_id, SERVICE_EXEC_STOP, SERVICE_EXEC_STOP_POST) ? EXEC_SETENV_RESULT : 0), | |
663996b3 MS |
2147 | &s->control_pid); |
2148 | if (r < 0) | |
2149 | goto fail; | |
2150 | ||
2151 | return; | |
2152 | ||
2153 | fail: | |
e3bff60a | 2154 | log_unit_warning_errno(UNIT(s), r, "Failed to run next control task: %m"); |
663996b3 | 2155 | |
52ad194e | 2156 | if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START_POST, SERVICE_STOP)) |
663996b3 MS |
2157 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
2158 | else if (s->state == SERVICE_STOP_POST) | |
2159 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); | |
2160 | else if (s->state == SERVICE_RELOAD) { | |
2161 | s->reload_result = SERVICE_FAILURE_RESOURCES; | |
2162 | service_enter_running(s, SERVICE_SUCCESS); | |
2163 | } else | |
2164 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2165 | } | |
2166 | ||
2167 | static void service_run_next_main(Service *s) { | |
2168 | pid_t pid; | |
2169 | int r; | |
2170 | ||
2171 | assert(s); | |
2172 | assert(s->main_command); | |
2173 | assert(s->main_command->command_next); | |
2174 | assert(s->type == SERVICE_ONESHOT); | |
2175 | ||
2176 | s->main_command = s->main_command->command_next; | |
2177 | service_unwatch_main_pid(s); | |
2178 | ||
2179 | r = service_spawn(s, | |
2180 | s->main_command, | |
5eef597e | 2181 | s->timeout_start_usec, |
f5e65279 | 2182 | EXEC_PASS_FDS|EXEC_APPLY_SANDBOXING|EXEC_APPLY_CHROOT|EXEC_APPLY_TTY_STDIN|EXEC_SET_WATCHDOG, |
663996b3 MS |
2183 | &pid); |
2184 | if (r < 0) | |
2185 | goto fail; | |
2186 | ||
2187 | service_set_main_pid(s, pid); | |
2188 | ||
2189 | return; | |
2190 | ||
2191 | fail: | |
e3bff60a | 2192 | log_unit_warning_errno(UNIT(s), r, "Failed to run next main task: %m"); |
663996b3 MS |
2193 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
2194 | } | |
2195 | ||
663996b3 MS |
2196 | static int service_start(Unit *u) { |
2197 | Service *s = SERVICE(u); | |
aa27b158 | 2198 | int r; |
663996b3 MS |
2199 | |
2200 | assert(s); | |
2201 | ||
2202 | /* We cannot fulfill this request right now, try again later | |
2203 | * please! */ | |
e3bff60a MP |
2204 | if (IN_SET(s->state, |
2205 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
2206 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) | |
663996b3 MS |
2207 | return -EAGAIN; |
2208 | ||
2209 | /* Already on it! */ | |
e3bff60a | 2210 | if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST)) |
663996b3 MS |
2211 | return 0; |
2212 | ||
2213 | /* A service that will be restarted must be stopped first to | |
2214 | * trigger BindsTo and/or OnFailure dependencies. If a user | |
2215 | * does not want to wait for the holdoff time to elapse, the | |
2216 | * service should be manually restarted, not started. We | |
2217 | * simply return EAGAIN here, so that any start jobs stay | |
2218 | * queued, and assume that the auto restart timer will | |
2219 | * eventually trigger the restart. */ | |
2220 | if (s->state == SERVICE_AUTO_RESTART) | |
2221 | return -EAGAIN; | |
2222 | ||
e3bff60a | 2223 | assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED)); |
663996b3 | 2224 | |
aa27b158 MP |
2225 | /* Make sure we don't enter a busy loop of some kind. */ |
2226 | r = unit_start_limit_test(u); | |
2227 | if (r < 0) { | |
2228 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT_HIT, false); | |
2229 | return r; | |
2230 | } | |
2231 | ||
8a584da2 MP |
2232 | r = unit_acquire_invocation_id(u); |
2233 | if (r < 0) | |
2234 | return r; | |
2235 | ||
663996b3 MS |
2236 | s->result = SERVICE_SUCCESS; |
2237 | s->reload_result = SERVICE_SUCCESS; | |
2238 | s->main_pid_known = false; | |
2239 | s->main_pid_alien = false; | |
2240 | s->forbid_restart = false; | |
52ad194e MB |
2241 | |
2242 | u->reset_accounting = true; | |
663996b3 | 2243 | |
6300502b | 2244 | s->status_text = mfree(s->status_text); |
5eef597e MP |
2245 | s->status_errno = 0; |
2246 | ||
2247 | s->notify_state = NOTIFY_UNKNOWN; | |
2248 | ||
5a920b42 MP |
2249 | s->watchdog_override_enable = false; |
2250 | s->watchdog_override_usec = 0; | |
2251 | ||
f5e65279 MB |
2252 | /* This is not an automatic restart? Flush the restart counter then */ |
2253 | if (s->flush_n_restarts) { | |
2254 | s->n_restarts = 0; | |
2255 | s->flush_n_restarts = false; | |
2256 | } | |
2257 | ||
663996b3 | 2258 | service_enter_start_pre(s); |
e735f4d4 | 2259 | return 1; |
663996b3 MS |
2260 | } |
2261 | ||
2262 | static int service_stop(Unit *u) { | |
2263 | Service *s = SERVICE(u); | |
2264 | ||
2265 | assert(s); | |
2266 | ||
e3bff60a | 2267 | /* Don't create restart jobs from manual stops. */ |
663996b3 MS |
2268 | s->forbid_restart = true; |
2269 | ||
2270 | /* Already on it */ | |
e3bff60a MP |
2271 | if (IN_SET(s->state, |
2272 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
2273 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) | |
663996b3 MS |
2274 | return 0; |
2275 | ||
2276 | /* A restart will be scheduled or is in progress. */ | |
2277 | if (s->state == SERVICE_AUTO_RESTART) { | |
2278 | service_set_state(s, SERVICE_DEAD); | |
2279 | return 0; | |
2280 | } | |
2281 | ||
2282 | /* If there's already something running we go directly into | |
2283 | * kill mode. */ | |
e3bff60a | 2284 | if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD)) { |
663996b3 MS |
2285 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
2286 | return 0; | |
2287 | } | |
2288 | ||
e3bff60a | 2289 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
663996b3 MS |
2290 | |
2291 | service_enter_stop(s, SERVICE_SUCCESS); | |
e735f4d4 | 2292 | return 1; |
663996b3 MS |
2293 | } |
2294 | ||
2295 | static int service_reload(Unit *u) { | |
2296 | Service *s = SERVICE(u); | |
2297 | ||
2298 | assert(s); | |
2299 | ||
f5e65279 | 2300 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
663996b3 MS |
2301 | |
2302 | service_enter_reload(s); | |
7035cd9e | 2303 | return 1; |
663996b3 MS |
2304 | } |
2305 | ||
2306 | _pure_ static bool service_can_reload(Unit *u) { | |
2307 | Service *s = SERVICE(u); | |
2308 | ||
2309 | assert(s); | |
2310 | ||
2311 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
2312 | } | |
2313 | ||
81c58355 MB |
2314 | static unsigned service_exec_command_index(Unit *u, ServiceExecCommand id, ExecCommand *current) { |
2315 | Service *s = SERVICE(u); | |
2316 | unsigned idx = 0; | |
2317 | ExecCommand *first, *c; | |
2318 | ||
2319 | assert(s); | |
2320 | ||
2321 | first = s->exec_command[id]; | |
2322 | ||
2323 | /* Figure out where we are in the list by walking back to the beginning */ | |
2324 | for (c = current; c != first; c = c->command_prev) | |
2325 | idx++; | |
2326 | ||
2327 | return idx; | |
2328 | } | |
2329 | ||
2330 | static int service_serialize_exec_command(Unit *u, FILE *f, ExecCommand *command) { | |
2331 | Service *s = SERVICE(u); | |
2332 | ServiceExecCommand id; | |
2333 | unsigned idx; | |
2334 | const char *type; | |
2335 | char **arg; | |
2336 | _cleanup_free_ char *args = NULL, *p = NULL; | |
2337 | size_t allocated = 0, length = 0; | |
2338 | ||
2339 | assert(s); | |
2340 | assert(f); | |
2341 | ||
2342 | if (!command) | |
2343 | return 0; | |
2344 | ||
2345 | if (command == s->control_command) { | |
2346 | type = "control"; | |
2347 | id = s->control_command_id; | |
2348 | } else { | |
2349 | type = "main"; | |
2350 | id = SERVICE_EXEC_START; | |
2351 | } | |
2352 | ||
2353 | idx = service_exec_command_index(u, id, command); | |
2354 | ||
2355 | STRV_FOREACH(arg, command->argv) { | |
2356 | size_t n; | |
2357 | _cleanup_free_ char *e = NULL; | |
2358 | ||
2359 | e = xescape(*arg, WHITESPACE); | |
2360 | if (!e) | |
2361 | return -ENOMEM; | |
2362 | ||
2363 | n = strlen(e); | |
2364 | if (!GREEDY_REALLOC(args, allocated, length + 1 + n + 1)) | |
2365 | return -ENOMEM; | |
2366 | ||
2367 | if (length > 0) | |
2368 | args[length++] = ' '; | |
2369 | ||
2370 | memcpy(args + length, e, n); | |
2371 | length += n; | |
2372 | } | |
2373 | ||
2374 | if (!GREEDY_REALLOC(args, allocated, length + 1)) | |
2375 | return -ENOMEM; | |
2376 | args[length++] = 0; | |
2377 | ||
2378 | p = xescape(command->path, WHITESPACE); | |
2379 | if (!p) | |
2380 | return -ENOMEM; | |
2381 | ||
2382 | fprintf(f, "%s-command=%s %u %s %s\n", type, service_exec_command_to_string(id), idx, p, args); | |
2383 | ||
2384 | return 0; | |
2385 | } | |
2386 | ||
663996b3 MS |
2387 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
2388 | Service *s = SERVICE(u); | |
e735f4d4 | 2389 | ServiceFDStore *fs; |
db2df898 | 2390 | int r; |
663996b3 MS |
2391 | |
2392 | assert(u); | |
2393 | assert(f); | |
2394 | assert(fds); | |
2395 | ||
2396 | unit_serialize_item(u, f, "state", service_state_to_string(s->state)); | |
2397 | unit_serialize_item(u, f, "result", service_result_to_string(s->result)); | |
2398 | unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result)); | |
2399 | ||
2400 | if (s->control_pid > 0) | |
e3bff60a | 2401 | unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid); |
663996b3 MS |
2402 | |
2403 | if (s->main_pid_known && s->main_pid > 0) | |
60f067b4 | 2404 | unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid); |
663996b3 MS |
2405 | |
2406 | unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known)); | |
86f210e9 | 2407 | unit_serialize_item(u, f, "bus-name-good", yes_no(s->bus_name_good)); |
4c89c718 | 2408 | unit_serialize_item(u, f, "bus-name-owner", s->bus_name_owner); |
663996b3 | 2409 | |
f5e65279 MB |
2410 | unit_serialize_item_format(u, f, "n-restarts", "%u", s->n_restarts); |
2411 | unit_serialize_item(u, f, "flush-n-restarts", yes_no(s->flush_n_restarts)); | |
2412 | ||
db2df898 MP |
2413 | r = unit_serialize_item_escaped(u, f, "status-text", s->status_text); |
2414 | if (r < 0) | |
2415 | return r; | |
663996b3 | 2416 | |
81c58355 MB |
2417 | service_serialize_exec_command(u, f, s->control_command); |
2418 | service_serialize_exec_command(u, f, s->main_command); | |
663996b3 | 2419 | |
db2df898 MP |
2420 | r = unit_serialize_item_fd(u, f, fds, "stdin-fd", s->stdin_fd); |
2421 | if (r < 0) | |
2422 | return r; | |
2423 | r = unit_serialize_item_fd(u, f, fds, "stdout-fd", s->stdout_fd); | |
2424 | if (r < 0) | |
2425 | return r; | |
2426 | r = unit_serialize_item_fd(u, f, fds, "stderr-fd", s->stderr_fd); | |
2427 | if (r < 0) | |
2428 | return r; | |
5eef597e | 2429 | |
8a584da2 MP |
2430 | if (UNIT_ISSET(s->accept_socket)) { |
2431 | r = unit_serialize_item(u, f, "accept-socket", UNIT_DEREF(s->accept_socket)->id); | |
2432 | if (r < 0) | |
2433 | return r; | |
2434 | } | |
2435 | ||
db2df898 | 2436 | r = unit_serialize_item_fd(u, f, fds, "socket-fd", s->socket_fd); |
db2df898 MP |
2437 | if (r < 0) |
2438 | return r; | |
5eef597e | 2439 | |
e735f4d4 | 2440 | LIST_FOREACH(fd_store, fs, s->fd_store) { |
6300502b | 2441 | _cleanup_free_ char *c = NULL; |
e735f4d4 MP |
2442 | int copy; |
2443 | ||
2444 | copy = fdset_put_dup(fds, fs->fd); | |
2445 | if (copy < 0) | |
2446 | return copy; | |
2447 | ||
6300502b MP |
2448 | c = cescape(fs->fdname); |
2449 | ||
2450 | unit_serialize_item_format(u, f, "fd-store-fd", "%i %s", copy, strempty(c)); | |
e735f4d4 MP |
2451 | } |
2452 | ||
663996b3 | 2453 | if (s->main_exec_status.pid > 0) { |
e3bff60a MP |
2454 | unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid); |
2455 | dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp); | |
2456 | dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp); | |
663996b3 MS |
2457 | |
2458 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { | |
e3bff60a MP |
2459 | unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code); |
2460 | unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
663996b3 MS |
2461 | } |
2462 | } | |
e3bff60a | 2463 | |
aa27b158 | 2464 | dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp); |
663996b3 | 2465 | |
db2df898 | 2466 | unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart)); |
663996b3 | 2467 | |
5a920b42 MP |
2468 | if (s->watchdog_override_enable) |
2469 | unit_serialize_item_format(u, f, "watchdog-override-usec", USEC_FMT, s->watchdog_override_usec); | |
2470 | ||
663996b3 MS |
2471 | return 0; |
2472 | } | |
2473 | ||
81c58355 MB |
2474 | static int service_deserialize_exec_command(Unit *u, const char *key, const char *value) { |
2475 | Service *s = SERVICE(u); | |
2476 | int r; | |
2477 | unsigned idx = 0, i; | |
2478 | bool control, found = false; | |
2479 | ServiceExecCommand id = _SERVICE_EXEC_COMMAND_INVALID; | |
2480 | ExecCommand *command = NULL; | |
2481 | _cleanup_free_ char *path = NULL; | |
2482 | _cleanup_strv_free_ char **argv = NULL; | |
2483 | ||
2484 | enum ExecCommandState { | |
2485 | STATE_EXEC_COMMAND_TYPE, | |
2486 | STATE_EXEC_COMMAND_INDEX, | |
2487 | STATE_EXEC_COMMAND_PATH, | |
2488 | STATE_EXEC_COMMAND_ARGS, | |
2489 | _STATE_EXEC_COMMAND_MAX, | |
2490 | _STATE_EXEC_COMMAND_INVALID = -1, | |
2491 | } state; | |
2492 | ||
2493 | assert(s); | |
2494 | assert(key); | |
2495 | assert(value); | |
2496 | ||
2497 | control = streq(key, "control-command"); | |
2498 | ||
2499 | state = STATE_EXEC_COMMAND_TYPE; | |
2500 | ||
2501 | for (;;) { | |
2502 | _cleanup_free_ char *arg = NULL; | |
2503 | ||
2504 | r = extract_first_word(&value, &arg, NULL, EXTRACT_CUNESCAPE); | |
2505 | if (r == 0) | |
2506 | break; | |
2507 | else if (r < 0) | |
2508 | return r; | |
2509 | ||
2510 | switch (state) { | |
2511 | case STATE_EXEC_COMMAND_TYPE: | |
2512 | id = service_exec_command_from_string(arg); | |
2513 | if (id < 0) | |
2514 | return -EINVAL; | |
2515 | ||
2516 | state = STATE_EXEC_COMMAND_INDEX; | |
2517 | break; | |
2518 | case STATE_EXEC_COMMAND_INDEX: | |
2519 | r = safe_atou(arg, &idx); | |
2520 | if (r < 0) | |
2521 | return -EINVAL; | |
2522 | ||
2523 | state = STATE_EXEC_COMMAND_PATH; | |
2524 | break; | |
2525 | case STATE_EXEC_COMMAND_PATH: | |
2526 | path = arg; | |
2527 | arg = NULL; | |
2528 | state = STATE_EXEC_COMMAND_ARGS; | |
2529 | ||
2530 | if (!path_is_absolute(path)) | |
2531 | return -EINVAL; | |
2532 | break; | |
2533 | case STATE_EXEC_COMMAND_ARGS: | |
2534 | r = strv_extend(&argv, arg); | |
2535 | if (r < 0) | |
2536 | return -ENOMEM; | |
2537 | break; | |
2538 | default: | |
2539 | assert_not_reached("Unknown error at deserialization of exec command"); | |
2540 | break; | |
2541 | } | |
2542 | } | |
2543 | ||
2544 | if (state != STATE_EXEC_COMMAND_ARGS) | |
2545 | return -EINVAL; | |
2546 | ||
2547 | /* Let's check whether exec command on given offset matches data that we just deserialized */ | |
2548 | for (command = s->exec_command[id], i = 0; command; command = command->command_next, i++) { | |
2549 | if (i != idx) | |
2550 | continue; | |
2551 | ||
2552 | found = strv_equal(argv, command->argv) && streq(command->path, path); | |
2553 | break; | |
2554 | } | |
2555 | ||
2556 | if (!found) { | |
2557 | /* Command at the index we serialized is different, let's look for command that exactly | |
2558 | * matches but is on different index. If there is no such command we will not resume execution. */ | |
2559 | for (command = s->exec_command[id]; command; command = command->command_next) | |
2560 | if (strv_equal(command->argv, argv) && streq(command->path, path)) | |
2561 | break; | |
2562 | } | |
2563 | ||
2564 | if (command && control) | |
2565 | s->control_command = command; | |
2566 | else if (command) | |
2567 | s->main_command = command; | |
2568 | else | |
2569 | log_unit_warning(u, "Current command vanished from the unit file, execution of the command list won't be resumed."); | |
2570 | ||
2571 | return 0; | |
2572 | } | |
2573 | ||
663996b3 MS |
2574 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { |
2575 | Service *s = SERVICE(u); | |
e735f4d4 | 2576 | int r; |
663996b3 MS |
2577 | |
2578 | assert(u); | |
2579 | assert(key); | |
2580 | assert(value); | |
2581 | assert(fds); | |
2582 | ||
2583 | if (streq(key, "state")) { | |
2584 | ServiceState state; | |
2585 | ||
2586 | state = service_state_from_string(value); | |
2587 | if (state < 0) | |
e3bff60a | 2588 | log_unit_debug(u, "Failed to parse state value: %s", value); |
663996b3 MS |
2589 | else |
2590 | s->deserialized_state = state; | |
2591 | } else if (streq(key, "result")) { | |
2592 | ServiceResult f; | |
2593 | ||
2594 | f = service_result_from_string(value); | |
2595 | if (f < 0) | |
e3bff60a | 2596 | log_unit_debug(u, "Failed to parse result value: %s", value); |
663996b3 MS |
2597 | else if (f != SERVICE_SUCCESS) |
2598 | s->result = f; | |
2599 | ||
2600 | } else if (streq(key, "reload-result")) { | |
2601 | ServiceResult f; | |
2602 | ||
2603 | f = service_result_from_string(value); | |
2604 | if (f < 0) | |
e3bff60a | 2605 | log_unit_debug(u, "Failed to parse reload result value: %s", value); |
663996b3 MS |
2606 | else if (f != SERVICE_SUCCESS) |
2607 | s->reload_result = f; | |
2608 | ||
2609 | } else if (streq(key, "control-pid")) { | |
2610 | pid_t pid; | |
2611 | ||
2612 | if (parse_pid(value, &pid) < 0) | |
e3bff60a | 2613 | log_unit_debug(u, "Failed to parse control-pid value: %s", value); |
663996b3 MS |
2614 | else |
2615 | s->control_pid = pid; | |
2616 | } else if (streq(key, "main-pid")) { | |
2617 | pid_t pid; | |
2618 | ||
2619 | if (parse_pid(value, &pid) < 0) | |
e3bff60a | 2620 | log_unit_debug(u, "Failed to parse main-pid value: %s", value); |
1d42b86d MB |
2621 | else |
2622 | (void) service_set_main_pid(s, pid); | |
663996b3 MS |
2623 | } else if (streq(key, "main-pid-known")) { |
2624 | int b; | |
2625 | ||
2626 | b = parse_boolean(value); | |
2627 | if (b < 0) | |
e3bff60a | 2628 | log_unit_debug(u, "Failed to parse main-pid-known value: %s", value); |
663996b3 MS |
2629 | else |
2630 | s->main_pid_known = b; | |
86f210e9 MP |
2631 | } else if (streq(key, "bus-name-good")) { |
2632 | int b; | |
2633 | ||
2634 | b = parse_boolean(value); | |
2635 | if (b < 0) | |
2636 | log_unit_debug(u, "Failed to parse bus-name-good value: %s", value); | |
2637 | else | |
2638 | s->bus_name_good = b; | |
4c89c718 MP |
2639 | } else if (streq(key, "bus-name-owner")) { |
2640 | r = free_and_strdup(&s->bus_name_owner, value); | |
2641 | if (r < 0) | |
2642 | log_unit_error_errno(u, r, "Unable to deserialize current bus owner %s: %m", value); | |
663996b3 MS |
2643 | } else if (streq(key, "status-text")) { |
2644 | char *t; | |
2645 | ||
e3bff60a MP |
2646 | r = cunescape(value, 0, &t); |
2647 | if (r < 0) | |
2648 | log_unit_debug_errno(u, r, "Failed to unescape status text: %s", value); | |
663996b3 MS |
2649 | else { |
2650 | free(s->status_text); | |
2651 | s->status_text = t; | |
2652 | } | |
2653 | ||
8a584da2 MP |
2654 | } else if (streq(key, "accept-socket")) { |
2655 | Unit *socket; | |
2656 | ||
2657 | r = manager_load_unit(u->manager, value, NULL, NULL, &socket); | |
2658 | if (r < 0) | |
2659 | log_unit_debug_errno(u, r, "Failed to load accept-socket unit: %s", value); | |
2660 | else { | |
2661 | unit_ref_set(&s->accept_socket, socket); | |
2662 | SOCKET(socket)->n_connections++; | |
2663 | } | |
2664 | ||
663996b3 MS |
2665 | } else if (streq(key, "socket-fd")) { |
2666 | int fd; | |
2667 | ||
2668 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
e3bff60a | 2669 | log_unit_debug(u, "Failed to parse socket-fd value: %s", value); |
663996b3 | 2670 | else { |
60f067b4 | 2671 | asynchronous_close(s->socket_fd); |
663996b3 MS |
2672 | s->socket_fd = fdset_remove(fds, fd); |
2673 | } | |
e735f4d4 | 2674 | } else if (streq(key, "fd-store-fd")) { |
6300502b MP |
2675 | const char *fdv; |
2676 | size_t pf; | |
e735f4d4 MP |
2677 | int fd; |
2678 | ||
6300502b MP |
2679 | pf = strcspn(value, WHITESPACE); |
2680 | fdv = strndupa(value, pf); | |
2681 | ||
2682 | if (safe_atoi(fdv, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
e3bff60a | 2683 | log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); |
e735f4d4 | 2684 | else { |
6300502b MP |
2685 | _cleanup_free_ char *t = NULL; |
2686 | const char *fdn; | |
2687 | ||
2688 | fdn = value + pf; | |
2689 | fdn += strspn(fdn, WHITESPACE); | |
2690 | (void) cunescape(fdn, 0, &t); | |
2691 | ||
2692 | r = service_add_fd_store(s, fd, t); | |
e735f4d4 | 2693 | if (r < 0) |
e3bff60a | 2694 | log_unit_error_errno(u, r, "Failed to add fd to store: %m"); |
8a584da2 | 2695 | else |
e735f4d4 MP |
2696 | fdset_remove(fds, fd); |
2697 | } | |
2698 | ||
663996b3 MS |
2699 | } else if (streq(key, "main-exec-status-pid")) { |
2700 | pid_t pid; | |
2701 | ||
2702 | if (parse_pid(value, &pid) < 0) | |
e3bff60a | 2703 | log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value); |
663996b3 MS |
2704 | else |
2705 | s->main_exec_status.pid = pid; | |
2706 | } else if (streq(key, "main-exec-status-code")) { | |
2707 | int i; | |
2708 | ||
2709 | if (safe_atoi(value, &i) < 0) | |
e3bff60a | 2710 | log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value); |
663996b3 MS |
2711 | else |
2712 | s->main_exec_status.code = i; | |
2713 | } else if (streq(key, "main-exec-status-status")) { | |
2714 | int i; | |
2715 | ||
2716 | if (safe_atoi(value, &i) < 0) | |
e3bff60a | 2717 | log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value); |
663996b3 MS |
2718 | else |
2719 | s->main_exec_status.status = i; | |
2720 | } else if (streq(key, "main-exec-status-start")) | |
2721 | dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp); | |
2722 | else if (streq(key, "main-exec-status-exit")) | |
2723 | dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp); | |
2724 | else if (streq(key, "watchdog-timestamp")) | |
2725 | dual_timestamp_deserialize(value, &s->watchdog_timestamp); | |
60f067b4 JS |
2726 | else if (streq(key, "forbid-restart")) { |
2727 | int b; | |
663996b3 | 2728 | |
60f067b4 JS |
2729 | b = parse_boolean(value); |
2730 | if (b < 0) | |
e3bff60a | 2731 | log_unit_debug(u, "Failed to parse forbid-restart value: %s", value); |
60f067b4 JS |
2732 | else |
2733 | s->forbid_restart = b; | |
db2df898 MP |
2734 | } else if (streq(key, "stdin-fd")) { |
2735 | int fd; | |
2736 | ||
2737 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2738 | log_unit_debug(u, "Failed to parse stdin-fd value: %s", value); | |
2739 | else { | |
2740 | asynchronous_close(s->stdin_fd); | |
2741 | s->stdin_fd = fdset_remove(fds, fd); | |
4c89c718 | 2742 | s->exec_context.stdio_as_fds = true; |
db2df898 MP |
2743 | } |
2744 | } else if (streq(key, "stdout-fd")) { | |
2745 | int fd; | |
2746 | ||
2747 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2748 | log_unit_debug(u, "Failed to parse stdout-fd value: %s", value); | |
2749 | else { | |
2750 | asynchronous_close(s->stdout_fd); | |
2751 | s->stdout_fd = fdset_remove(fds, fd); | |
4c89c718 | 2752 | s->exec_context.stdio_as_fds = true; |
db2df898 MP |
2753 | } |
2754 | } else if (streq(key, "stderr-fd")) { | |
2755 | int fd; | |
2756 | ||
2757 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2758 | log_unit_debug(u, "Failed to parse stderr-fd value: %s", value); | |
2759 | else { | |
2760 | asynchronous_close(s->stderr_fd); | |
2761 | s->stderr_fd = fdset_remove(fds, fd); | |
4c89c718 | 2762 | s->exec_context.stdio_as_fds = true; |
db2df898 | 2763 | } |
5a920b42 MP |
2764 | } else if (streq(key, "watchdog-override-usec")) { |
2765 | usec_t watchdog_override_usec; | |
2766 | if (timestamp_deserialize(value, &watchdog_override_usec) < 0) | |
2767 | log_unit_debug(u, "Failed to parse watchdog_override_usec value: %s", value); | |
2768 | else { | |
2769 | s->watchdog_override_enable = true; | |
2770 | s->watchdog_override_usec = watchdog_override_usec; | |
2771 | } | |
81c58355 MB |
2772 | } else if (STR_IN_SET(key, "main-command", "control-command")) { |
2773 | r = service_deserialize_exec_command(u, key, value); | |
2774 | if (r < 0) | |
2775 | log_unit_debug_errno(u, r, "Failed to parse serialized command \"%s\": %m", value); | |
f5e65279 MB |
2776 | |
2777 | } else if (streq(key, "n-restarts")) { | |
2778 | r = safe_atou(value, &s->n_restarts); | |
2779 | if (r < 0) | |
2780 | log_unit_debug_errno(u, r, "Failed to parse serialized restart counter '%s': %m", value); | |
2781 | ||
2782 | } else if (streq(key, "flush-n-restarts")) { | |
2783 | r = parse_boolean(value); | |
2784 | if (r < 0) | |
2785 | log_unit_debug_errno(u, r, "Failed to parse serialized flush restart counter setting '%s': %m", value); | |
2786 | else | |
2787 | s->flush_n_restarts = r; | |
663996b3 | 2788 | } else |
e3bff60a | 2789 | log_unit_debug(u, "Unknown serialization key: %s", key); |
663996b3 MS |
2790 | |
2791 | return 0; | |
2792 | } | |
2793 | ||
2794 | _pure_ static UnitActiveState service_active_state(Unit *u) { | |
2795 | const UnitActiveState *table; | |
2796 | ||
2797 | assert(u); | |
2798 | ||
2799 | table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; | |
2800 | ||
2801 | return table[SERVICE(u)->state]; | |
2802 | } | |
2803 | ||
2804 | static const char *service_sub_state_to_string(Unit *u) { | |
2805 | assert(u); | |
2806 | ||
2807 | return service_state_to_string(SERVICE(u)->state); | |
2808 | } | |
2809 | ||
2810 | static bool service_check_gc(Unit *u) { | |
2811 | Service *s = SERVICE(u); | |
2812 | ||
2813 | assert(s); | |
2814 | ||
52ad194e MB |
2815 | /* Never clean up services that still have a process around, even if the service is formally dead. Note that |
2816 | * unit_check_gc() already checked our cgroup for us, we just check our two additional PIDs, too, in case they | |
2817 | * have moved outside of the cgroup. */ | |
2818 | ||
2819 | if (main_pid_good(s) > 0 || | |
663996b3 MS |
2820 | control_pid_good(s) > 0) |
2821 | return true; | |
2822 | ||
663996b3 MS |
2823 | return false; |
2824 | } | |
2825 | ||
663996b3 MS |
2826 | static int service_retry_pid_file(Service *s) { |
2827 | int r; | |
2828 | ||
2829 | assert(s->pid_file); | |
f5e65279 | 2830 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
663996b3 MS |
2831 | |
2832 | r = service_load_pid_file(s, false); | |
2833 | if (r < 0) | |
2834 | return r; | |
2835 | ||
2836 | service_unwatch_pid_file(s); | |
2837 | ||
2838 | service_enter_running(s, SERVICE_SUCCESS); | |
2839 | return 0; | |
2840 | } | |
2841 | ||
2842 | static int service_watch_pid_file(Service *s) { | |
2843 | int r; | |
2844 | ||
e3bff60a | 2845 | log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path); |
5eef597e | 2846 | |
60f067b4 | 2847 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io); |
663996b3 MS |
2848 | if (r < 0) |
2849 | goto fail; | |
2850 | ||
2851 | /* the pidfile might have appeared just before we set the watch */ | |
e3bff60a | 2852 | log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path); |
663996b3 MS |
2853 | service_retry_pid_file(s); |
2854 | ||
2855 | return 0; | |
2856 | fail: | |
e3bff60a | 2857 | log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path); |
663996b3 MS |
2858 | service_unwatch_pid_file(s); |
2859 | return r; | |
2860 | } | |
2861 | ||
2862 | static int service_demand_pid_file(Service *s) { | |
2863 | PathSpec *ps; | |
2864 | ||
2865 | assert(s->pid_file); | |
2866 | assert(!s->pid_file_pathspec); | |
2867 | ||
2868 | ps = new0(PathSpec, 1); | |
2869 | if (!ps) | |
2870 | return -ENOMEM; | |
2871 | ||
60f067b4 | 2872 | ps->unit = UNIT(s); |
663996b3 MS |
2873 | ps->path = strdup(s->pid_file); |
2874 | if (!ps->path) { | |
2875 | free(ps); | |
2876 | return -ENOMEM; | |
2877 | } | |
2878 | ||
2879 | path_kill_slashes(ps->path); | |
2880 | ||
2881 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that | |
2882 | * keep their PID file open all the time. */ | |
2883 | ps->type = PATH_MODIFIED; | |
2884 | ps->inotify_fd = -1; | |
2885 | ||
2886 | s->pid_file_pathspec = ps; | |
2887 | ||
60f067b4 JS |
2888 | return service_watch_pid_file(s); |
2889 | } | |
2890 | ||
2891 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { | |
2892 | PathSpec *p = userdata; | |
2893 | Service *s; | |
2894 | ||
2895 | assert(p); | |
2896 | ||
2897 | s = SERVICE(p->unit); | |
2898 | ||
2899 | assert(s); | |
2900 | assert(fd >= 0); | |
f5e65279 | 2901 | assert(IN_SET(s->state, SERVICE_START, SERVICE_START_POST)); |
60f067b4 JS |
2902 | assert(s->pid_file_pathspec); |
2903 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); | |
2904 | ||
e3bff60a | 2905 | log_unit_debug(UNIT(s), "inotify event"); |
60f067b4 JS |
2906 | |
2907 | if (path_spec_fd_event(p, events) < 0) | |
2908 | goto fail; | |
2909 | ||
2910 | if (service_retry_pid_file(s) == 0) | |
2911 | return 0; | |
2912 | ||
2913 | if (service_watch_pid_file(s) < 0) | |
2914 | goto fail; | |
2915 | ||
2916 | return 0; | |
2917 | ||
2918 | fail: | |
2919 | service_unwatch_pid_file(s); | |
2920 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2921 | return 0; | |
2922 | } | |
2923 | ||
2924 | static void service_notify_cgroup_empty_event(Unit *u) { | |
2925 | Service *s = SERVICE(u); | |
2926 | ||
2927 | assert(u); | |
2928 | ||
e3bff60a | 2929 | log_unit_debug(u, "cgroup is empty"); |
60f067b4 JS |
2930 | |
2931 | switch (s->state) { | |
2932 | ||
2933 | /* Waiting for SIGCHLD is usually more interesting, | |
2934 | * because it includes return codes/signals. Which is | |
2935 | * why we ignore the cgroup events for most cases, | |
2936 | * except when we don't know pid which to expect the | |
2937 | * SIGCHLD for. */ | |
2938 | ||
2939 | case SERVICE_START: | |
f5e65279 MB |
2940 | if (s->type == SERVICE_NOTIFY && |
2941 | main_pid_good(s) == 0 && | |
2942 | control_pid_good(s) == 0) { | |
2897b343 MP |
2943 | /* No chance of getting a ready notification anymore */ |
2944 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); | |
2945 | break; | |
2946 | } | |
2947 | ||
52ad194e | 2948 | _fallthrough_; |
60f067b4 | 2949 | case SERVICE_START_POST: |
f5e65279 MB |
2950 | if (s->pid_file_pathspec && |
2951 | main_pid_good(s) == 0 && | |
2952 | control_pid_good(s) == 0) { | |
2953 | ||
2897b343 | 2954 | /* Give up hoping for the daemon to write its PID file */ |
e3bff60a | 2955 | log_unit_warning(u, "Daemon never wrote its PID file. Failing."); |
5eef597e | 2956 | |
60f067b4 JS |
2957 | service_unwatch_pid_file(s); |
2958 | if (s->state == SERVICE_START) | |
2897b343 | 2959 | service_enter_stop_post(s, SERVICE_FAILURE_PROTOCOL); |
60f067b4 | 2960 | else |
2897b343 | 2961 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
60f067b4 JS |
2962 | } |
2963 | break; | |
663996b3 | 2964 | |
60f067b4 JS |
2965 | case SERVICE_RUNNING: |
2966 | /* service_enter_running() will figure out what to do */ | |
2967 | service_enter_running(s, SERVICE_SUCCESS); | |
2968 | break; | |
663996b3 | 2969 | |
5eef597e | 2970 | case SERVICE_STOP_SIGABRT: |
60f067b4 JS |
2971 | case SERVICE_STOP_SIGTERM: |
2972 | case SERVICE_STOP_SIGKILL: | |
663996b3 | 2973 | |
f5e65279 | 2974 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
60f067b4 | 2975 | service_enter_stop_post(s, SERVICE_SUCCESS); |
663996b3 | 2976 | |
60f067b4 | 2977 | break; |
663996b3 | 2978 | |
60f067b4 JS |
2979 | case SERVICE_STOP_POST: |
2980 | case SERVICE_FINAL_SIGTERM: | |
2981 | case SERVICE_FINAL_SIGKILL: | |
f5e65279 | 2982 | if (main_pid_good(s) <= 0 && control_pid_good(s) <= 0) |
60f067b4 | 2983 | service_enter_dead(s, SERVICE_SUCCESS, true); |
663996b3 | 2984 | |
60f067b4 | 2985 | break; |
663996b3 | 2986 | |
60f067b4 JS |
2987 | default: |
2988 | ; | |
2989 | } | |
663996b3 MS |
2990 | } |
2991 | ||
2992 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { | |
1d42b86d | 2993 | bool notify_dbus = true; |
663996b3 MS |
2994 | Service *s = SERVICE(u); |
2995 | ServiceResult f; | |
2996 | ||
2997 | assert(s); | |
2998 | assert(pid >= 0); | |
2999 | ||
8a584da2 | 3000 | if (is_clean_exit(code, status, s->type == SERVICE_ONESHOT ? EXIT_CLEAN_COMMAND : EXIT_CLEAN_DAEMON, &s->success_status)) |
663996b3 MS |
3001 | f = SERVICE_SUCCESS; |
3002 | else if (code == CLD_EXITED) | |
3003 | f = SERVICE_FAILURE_EXIT_CODE; | |
3004 | else if (code == CLD_KILLED) | |
3005 | f = SERVICE_FAILURE_SIGNAL; | |
3006 | else if (code == CLD_DUMPED) | |
3007 | f = SERVICE_FAILURE_CORE_DUMP; | |
3008 | else | |
3009 | assert_not_reached("Unknown code"); | |
3010 | ||
3011 | if (s->main_pid == pid) { | |
3012 | /* Forking services may occasionally move to a new PID. | |
3013 | * As long as they update the PID file before exiting the old | |
3014 | * PID, they're fine. */ | |
1d42b86d | 3015 | if (service_load_pid_file(s, false) > 0) |
663996b3 MS |
3016 | return; |
3017 | ||
3018 | s->main_pid = 0; | |
3019 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); | |
3020 | ||
3021 | if (s->main_command) { | |
3022 | /* If this is not a forking service than the | |
3023 | * main process got started and hence we copy | |
3024 | * the exit status so that it is recorded both | |
3025 | * as main and as control process exit | |
3026 | * status */ | |
3027 | ||
3028 | s->main_command->exec_status = s->main_exec_status; | |
3029 | ||
f5e65279 | 3030 | if (s->main_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
663996b3 MS |
3031 | f = SERVICE_SUCCESS; |
3032 | } else if (s->exec_command[SERVICE_EXEC_START]) { | |
3033 | ||
3034 | /* If this is a forked process, then we should | |
3035 | * ignore the return value if this was | |
3036 | * configured for the starter process */ | |
3037 | ||
f5e65279 | 3038 | if (s->exec_command[SERVICE_EXEC_START]->flags & EXEC_COMMAND_IGNORE_FAILURE) |
663996b3 MS |
3039 | f = SERVICE_SUCCESS; |
3040 | } | |
3041 | ||
8a584da2 MP |
3042 | /* When this is a successful exit, let's log about the exit code on DEBUG level. If this is a failure |
3043 | * and the process exited on its own via exit(), then let's make this a NOTICE, under the assumption | |
3044 | * that the service already logged the reason at a higher log level on its own. However, if the service | |
3045 | * died due to a signal, then it most likely didn't say anything about any reason, hence let's raise | |
3046 | * our log level to WARNING then. */ | |
3047 | ||
3048 | log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : | |
3049 | (code == CLD_EXITED ? LOG_NOTICE : LOG_WARNING), | |
e3bff60a MP |
3050 | LOG_UNIT_MESSAGE(u, "Main process exited, code=%s, status=%i/%s", |
3051 | sigchld_code_to_string(code), status, | |
f47781d8 MP |
3052 | strna(code == CLD_EXITED |
3053 | ? exit_status_to_string(status, EXIT_STATUS_FULL) | |
3054 | : signal_to_string(status))), | |
e3bff60a MP |
3055 | "EXIT_CODE=%s", sigchld_code_to_string(code), |
3056 | "EXIT_STATUS=%i", status, | |
81c58355 | 3057 | LOG_UNIT_ID(u), |
f5e65279 | 3058 | LOG_UNIT_INVOCATION_ID(u), |
e3bff60a | 3059 | NULL); |
663996b3 | 3060 | |
8a584da2 | 3061 | if (s->result == SERVICE_SUCCESS) |
663996b3 MS |
3062 | s->result = f; |
3063 | ||
3064 | if (s->main_command && | |
3065 | s->main_command->command_next && | |
f5e65279 | 3066 | s->type == SERVICE_ONESHOT && |
663996b3 MS |
3067 | f == SERVICE_SUCCESS) { |
3068 | ||
3069 | /* There is another command to * | |
3070 | * execute, so let's do that. */ | |
3071 | ||
e3bff60a | 3072 | log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state)); |
663996b3 MS |
3073 | service_run_next_main(s); |
3074 | ||
3075 | } else { | |
3076 | ||
3077 | /* The service exited, so the service is officially | |
3078 | * gone. */ | |
3079 | s->main_command = NULL; | |
3080 | ||
3081 | switch (s->state) { | |
3082 | ||
3083 | case SERVICE_START_POST: | |
3084 | case SERVICE_RELOAD: | |
3085 | case SERVICE_STOP: | |
3086 | /* Need to wait until the operation is | |
3087 | * done */ | |
3088 | break; | |
3089 | ||
3090 | case SERVICE_START: | |
3091 | if (s->type == SERVICE_ONESHOT) { | |
3092 | /* This was our main goal, so let's go on */ | |
3093 | if (f == SERVICE_SUCCESS) | |
3094 | service_enter_start_post(s); | |
3095 | else | |
2897b343 MP |
3096 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
3097 | break; | |
3098 | } else if (s->type == SERVICE_NOTIFY) { | |
3099 | /* Only enter running through a notification, so that the | |
3100 | * SERVICE_START state signifies that no ready notification | |
3101 | * has been received */ | |
3102 | if (f != SERVICE_SUCCESS) | |
3103 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
f5e65279 MB |
3104 | else if (!s->remain_after_exit || s->notify_access == NOTIFY_MAIN) |
3105 | /* The service has never been and will never be active */ | |
2897b343 | 3106 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); |
663996b3 MS |
3107 | break; |
3108 | } | |
3109 | ||
52ad194e | 3110 | _fallthrough_; |
663996b3 MS |
3111 | case SERVICE_RUNNING: |
3112 | service_enter_running(s, f); | |
3113 | break; | |
3114 | ||
5eef597e | 3115 | case SERVICE_STOP_SIGABRT: |
663996b3 MS |
3116 | case SERVICE_STOP_SIGTERM: |
3117 | case SERVICE_STOP_SIGKILL: | |
3118 | ||
f5e65279 | 3119 | if (control_pid_good(s) <= 0) |
663996b3 MS |
3120 | service_enter_stop_post(s, f); |
3121 | ||
3122 | /* If there is still a control process, wait for that first */ | |
3123 | break; | |
3124 | ||
60f067b4 JS |
3125 | case SERVICE_STOP_POST: |
3126 | case SERVICE_FINAL_SIGTERM: | |
3127 | case SERVICE_FINAL_SIGKILL: | |
3128 | ||
f5e65279 | 3129 | if (control_pid_good(s) <= 0) |
60f067b4 JS |
3130 | service_enter_dead(s, f, true); |
3131 | break; | |
3132 | ||
663996b3 MS |
3133 | default: |
3134 | assert_not_reached("Uh, main process died at wrong time."); | |
3135 | } | |
3136 | } | |
3137 | ||
3138 | } else if (s->control_pid == pid) { | |
663996b3 MS |
3139 | s->control_pid = 0; |
3140 | ||
3141 | if (s->control_command) { | |
5eef597e | 3142 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
663996b3 | 3143 | |
f5e65279 | 3144 | if (s->control_command->flags & EXEC_COMMAND_IGNORE_FAILURE) |
663996b3 MS |
3145 | f = SERVICE_SUCCESS; |
3146 | } | |
3147 | ||
e3bff60a MP |
3148 | log_unit_full(u, f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0, |
3149 | "Control process exited, code=%s status=%i", | |
3150 | sigchld_code_to_string(code), status); | |
663996b3 | 3151 | |
8a584da2 | 3152 | if (s->result == SERVICE_SUCCESS) |
663996b3 MS |
3153 | s->result = f; |
3154 | ||
663996b3 MS |
3155 | if (s->control_command && |
3156 | s->control_command->command_next && | |
3157 | f == SERVICE_SUCCESS) { | |
3158 | ||
3159 | /* There is another command to * | |
3160 | * execute, so let's do that. */ | |
3161 | ||
e3bff60a | 3162 | log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state)); |
663996b3 MS |
3163 | service_run_next_control(s); |
3164 | ||
3165 | } else { | |
3166 | /* No further commands for this step, so let's | |
3167 | * figure out what to do next */ | |
3168 | ||
3169 | s->control_command = NULL; | |
3170 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
3171 | ||
e3bff60a | 3172 | log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state)); |
663996b3 MS |
3173 | |
3174 | switch (s->state) { | |
3175 | ||
3176 | case SERVICE_START_PRE: | |
3177 | if (f == SERVICE_SUCCESS) | |
3178 | service_enter_start(s); | |
3179 | else | |
2897b343 | 3180 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
663996b3 MS |
3181 | break; |
3182 | ||
3183 | case SERVICE_START: | |
3184 | if (s->type != SERVICE_FORKING) | |
3185 | /* Maybe spurious event due to a reload that changed the type? */ | |
3186 | break; | |
3187 | ||
3188 | if (f != SERVICE_SUCCESS) { | |
2897b343 | 3189 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
663996b3 MS |
3190 | break; |
3191 | } | |
3192 | ||
3193 | if (s->pid_file) { | |
3194 | bool has_start_post; | |
3195 | int r; | |
3196 | ||
3197 | /* Let's try to load the pid file here if we can. | |
3198 | * The PID file might actually be created by a START_POST | |
3199 | * script. In that case don't worry if the loading fails. */ | |
3200 | ||
3201 | has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST]; | |
3202 | r = service_load_pid_file(s, !has_start_post); | |
3203 | if (!has_start_post && r < 0) { | |
3204 | r = service_demand_pid_file(s); | |
f5e65279 | 3205 | if (r < 0 || cgroup_good(s) == 0) |
2897b343 | 3206 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_PROTOCOL); |
663996b3 MS |
3207 | break; |
3208 | } | |
3209 | } else | |
aa27b158 | 3210 | service_search_main_pid(s); |
663996b3 MS |
3211 | |
3212 | service_enter_start_post(s); | |
3213 | break; | |
3214 | ||
3215 | case SERVICE_START_POST: | |
3216 | if (f != SERVICE_SUCCESS) { | |
4c89c718 | 3217 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
663996b3 MS |
3218 | break; |
3219 | } | |
3220 | ||
3221 | if (s->pid_file) { | |
3222 | int r; | |
3223 | ||
3224 | r = service_load_pid_file(s, true); | |
3225 | if (r < 0) { | |
3226 | r = service_demand_pid_file(s); | |
f5e65279 | 3227 | if (r < 0 || cgroup_good(s) == 0) |
2897b343 | 3228 | service_enter_stop(s, SERVICE_FAILURE_PROTOCOL); |
663996b3 MS |
3229 | break; |
3230 | } | |
3231 | } else | |
aa27b158 | 3232 | service_search_main_pid(s); |
663996b3 MS |
3233 | |
3234 | service_enter_running(s, SERVICE_SUCCESS); | |
3235 | break; | |
3236 | ||
3237 | case SERVICE_RELOAD: | |
aa27b158 MP |
3238 | if (f == SERVICE_SUCCESS) |
3239 | if (service_load_pid_file(s, true) < 0) | |
3240 | service_search_main_pid(s); | |
663996b3 MS |
3241 | |
3242 | s->reload_result = f; | |
3243 | service_enter_running(s, SERVICE_SUCCESS); | |
3244 | break; | |
3245 | ||
3246 | case SERVICE_STOP: | |
3247 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); | |
3248 | break; | |
3249 | ||
5eef597e | 3250 | case SERVICE_STOP_SIGABRT: |
663996b3 MS |
3251 | case SERVICE_STOP_SIGTERM: |
3252 | case SERVICE_STOP_SIGKILL: | |
3253 | if (main_pid_good(s) <= 0) | |
3254 | service_enter_stop_post(s, f); | |
3255 | ||
3256 | /* If there is still a service | |
3257 | * process around, wait until | |
3258 | * that one quit, too */ | |
3259 | break; | |
3260 | ||
3261 | case SERVICE_STOP_POST: | |
3262 | case SERVICE_FINAL_SIGTERM: | |
3263 | case SERVICE_FINAL_SIGKILL: | |
60f067b4 JS |
3264 | if (main_pid_good(s) <= 0) |
3265 | service_enter_dead(s, f, true); | |
663996b3 MS |
3266 | break; |
3267 | ||
3268 | default: | |
3269 | assert_not_reached("Uh, control process died at wrong time."); | |
3270 | } | |
3271 | } | |
1d42b86d MB |
3272 | } else /* Neither control nor main PID? If so, don't notify about anything */ |
3273 | notify_dbus = false; | |
663996b3 MS |
3274 | |
3275 | /* Notify clients about changed exit status */ | |
1d42b86d MB |
3276 | if (notify_dbus) |
3277 | unit_add_to_dbus_queue(u); | |
663996b3 | 3278 | |
1d42b86d MB |
3279 | /* If we get a SIGCHLD event for one of the processes we were interested in, then we look for others to watch, |
3280 | * under the assumption that we'll sooner or later get a SIGCHLD for them, as the original process we watched | |
3281 | * was probably the parent of them, and they are hence now our children. */ | |
60f067b4 JS |
3282 | unit_tidy_watch_pids(u, s->main_pid, s->control_pid); |
3283 | unit_watch_all_pids(u); | |
663996b3 | 3284 | |
1d42b86d MB |
3285 | /* If the PID set is empty now, then let's check if the cgroup is empty too and finish off the unit. */ |
3286 | unit_synthesize_cgroup_empty_event(u); | |
60f067b4 | 3287 | } |
663996b3 | 3288 | |
60f067b4 JS |
3289 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
3290 | Service *s = SERVICE(userdata); | |
663996b3 | 3291 | |
60f067b4 JS |
3292 | assert(s); |
3293 | assert(source == s->timer_event_source); | |
663996b3 MS |
3294 | |
3295 | switch (s->state) { | |
3296 | ||
3297 | case SERVICE_START_PRE: | |
3298 | case SERVICE_START: | |
e3bff60a | 3299 | log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", s->state == SERVICE_START ? "Start" : "Start-pre"); |
2897b343 | 3300 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
663996b3 MS |
3301 | break; |
3302 | ||
3303 | case SERVICE_START_POST: | |
e3bff60a | 3304 | log_unit_warning(UNIT(s), "Start-post operation timed out. Stopping."); |
4c89c718 MP |
3305 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
3306 | break; | |
3307 | ||
3308 | case SERVICE_RUNNING: | |
3309 | log_unit_warning(UNIT(s), "Service reached runtime time limit. Stopping."); | |
663996b3 MS |
3310 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); |
3311 | break; | |
3312 | ||
3313 | case SERVICE_RELOAD: | |
4c89c718 | 3314 | log_unit_warning(UNIT(s), "Reload operation timed out. Killing reload process."); |
52ad194e | 3315 | service_kill_control_process(s); |
663996b3 MS |
3316 | s->reload_result = SERVICE_FAILURE_TIMEOUT; |
3317 | service_enter_running(s, SERVICE_SUCCESS); | |
3318 | break; | |
3319 | ||
3320 | case SERVICE_STOP: | |
e3bff60a | 3321 | log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); |
663996b3 MS |
3322 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
3323 | break; | |
3324 | ||
5eef597e | 3325 | case SERVICE_STOP_SIGABRT: |
e3bff60a MP |
3326 | log_unit_warning(UNIT(s), "State 'stop-sigabrt' timed out. Terminating."); |
3327 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); | |
5eef597e MP |
3328 | break; |
3329 | ||
663996b3 MS |
3330 | case SERVICE_STOP_SIGTERM: |
3331 | if (s->kill_context.send_sigkill) { | |
e3bff60a | 3332 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing."); |
663996b3 MS |
3333 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
3334 | } else { | |
e3bff60a | 3335 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL."); |
663996b3 MS |
3336 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
3337 | } | |
3338 | ||
3339 | break; | |
3340 | ||
3341 | case SERVICE_STOP_SIGKILL: | |
3342 | /* Uh, we sent a SIGKILL and it is still not gone? | |
3343 | * Must be something we cannot kill, so let's just be | |
3344 | * weirded out and continue */ | |
3345 | ||
e3bff60a | 3346 | log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); |
663996b3 MS |
3347 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
3348 | break; | |
3349 | ||
3350 | case SERVICE_STOP_POST: | |
e3bff60a | 3351 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating."); |
663996b3 MS |
3352 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
3353 | break; | |
3354 | ||
3355 | case SERVICE_FINAL_SIGTERM: | |
3356 | if (s->kill_context.send_sigkill) { | |
e3bff60a | 3357 | log_unit_warning(UNIT(s), "State 'stop-final-sigterm' timed out. Killing."); |
663996b3 MS |
3358 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
3359 | } else { | |
e3bff60a | 3360 | log_unit_warning(UNIT(s), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode."); |
663996b3 MS |
3361 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
3362 | } | |
3363 | ||
3364 | break; | |
3365 | ||
3366 | case SERVICE_FINAL_SIGKILL: | |
e3bff60a | 3367 | log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode."); |
663996b3 MS |
3368 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
3369 | break; | |
3370 | ||
3371 | case SERVICE_AUTO_RESTART: | |
e3bff60a | 3372 | log_unit_info(UNIT(s), |
60f067b4 | 3373 | s->restart_usec > 0 ? |
e3bff60a MP |
3374 | "Service hold-off time over, scheduling restart." : |
3375 | "Service has no hold-off time, scheduling restart."); | |
663996b3 MS |
3376 | service_enter_restart(s); |
3377 | break; | |
3378 | ||
3379 | default: | |
3380 | assert_not_reached("Timeout at wrong time."); | |
3381 | } | |
663996b3 | 3382 | |
60f067b4 JS |
3383 | return 0; |
3384 | } | |
663996b3 | 3385 | |
60f067b4 JS |
3386 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { |
3387 | Service *s = SERVICE(userdata); | |
5eef597e | 3388 | char t[FORMAT_TIMESPAN_MAX]; |
5a920b42 | 3389 | usec_t watchdog_usec; |
663996b3 | 3390 | |
60f067b4 JS |
3391 | assert(s); |
3392 | assert(source == s->watchdog_event_source); | |
663996b3 | 3393 | |
5a920b42 MP |
3394 | watchdog_usec = service_get_watchdog_usec(s); |
3395 | ||
1d42b86d MB |
3396 | if (UNIT(s)->manager->service_watchdogs) { |
3397 | log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!", | |
3398 | format_timespan(t, sizeof(t), watchdog_usec, 1)); | |
5eef597e | 3399 | |
1d42b86d MB |
3400 | service_enter_signal(s, SERVICE_STOP_SIGABRT, SERVICE_FAILURE_WATCHDOG); |
3401 | } else | |
3402 | log_unit_warning(UNIT(s), "Watchdog disabled! Ignoring watchdog timeout (limit %s)!", | |
3403 | format_timespan(t, sizeof(t), watchdog_usec, 1)); | |
663996b3 | 3404 | |
60f067b4 | 3405 | return 0; |
663996b3 MS |
3406 | } |
3407 | ||
52ad194e MB |
3408 | static bool service_notify_message_authorized(Service *s, pid_t pid, char **tags, FDSet *fds) { |
3409 | assert(s); | |
60f067b4 | 3410 | |
663996b3 | 3411 | if (s->notify_access == NOTIFY_NONE) { |
52ad194e MB |
3412 | log_unit_warning(UNIT(s), "Got notification message from PID "PID_FMT", but reception is disabled.", pid); |
3413 | return false; | |
3414 | } | |
3415 | ||
3416 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { | |
60f067b4 | 3417 | if (s->main_pid != 0) |
52ad194e | 3418 | 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); |
60f067b4 | 3419 | else |
52ad194e MB |
3420 | 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); |
3421 | ||
3422 | return false; | |
3423 | } | |
3424 | ||
3425 | if (s->notify_access == NOTIFY_EXEC && pid != s->main_pid && pid != s->control_pid) { | |
2897b343 | 3426 | if (s->main_pid != 0 && s->control_pid != 0) |
52ad194e | 3427 | 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, |
2897b343 MP |
3428 | pid, s->main_pid, s->control_pid); |
3429 | else if (s->main_pid != 0) | |
52ad194e | 3430 | 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); |
2897b343 | 3431 | else if (s->control_pid != 0) |
52ad194e | 3432 | 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); |
2897b343 | 3433 | else |
52ad194e MB |
3434 | 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); |
3435 | ||
3436 | return false; | |
3437 | } | |
3438 | ||
3439 | return true; | |
3440 | } | |
3441 | ||
1d42b86d MB |
3442 | static void service_notify_message( |
3443 | Unit *u, | |
3444 | const struct ucred *ucred, | |
3445 | char **tags, | |
3446 | FDSet *fds) { | |
3447 | ||
52ad194e MB |
3448 | Service *s = SERVICE(u); |
3449 | bool notify_dbus = false; | |
3450 | const char *e; | |
3451 | char **i; | |
1d42b86d | 3452 | int r; |
52ad194e MB |
3453 | |
3454 | assert(u); | |
1d42b86d | 3455 | assert(ucred); |
52ad194e | 3456 | |
1d42b86d | 3457 | if (!service_notify_message_authorized(SERVICE(u), ucred->pid, tags, fds)) |
663996b3 | 3458 | return; |
52ad194e | 3459 | |
1d42b86d | 3460 | if (DEBUG_LOGGING) { |
52ad194e MB |
3461 | _cleanup_free_ char *cc = NULL; |
3462 | ||
3463 | cc = strv_join(tags, ", "); | |
1d42b86d | 3464 | log_unit_debug(u, "Got notification message from PID "PID_FMT" (%s)", ucred->pid, isempty(cc) ? "n/a" : cc); |
52ad194e | 3465 | } |
663996b3 | 3466 | |
663996b3 | 3467 | /* Interpret MAINPID= */ |
5eef597e MP |
3468 | e = strv_find_startswith(tags, "MAINPID="); |
3469 | if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) { | |
1d42b86d MB |
3470 | pid_t new_main_pid; |
3471 | ||
3472 | if (parse_pid(e, &new_main_pid) < 0) | |
3473 | log_unit_warning(u, "Failed to parse MAINPID= field in notification message, ignoring: %s", e); | |
3474 | else if (!s->main_pid_known || new_main_pid != s->main_pid) { | |
3475 | ||
3476 | r = service_is_suitable_main_pid(s, new_main_pid, LOG_WARNING); | |
3477 | if (r == 0) { | |
3478 | /* The new main PID is a bit suspicous, which is OK if the sender is privileged. */ | |
3479 | ||
3480 | if (ucred->uid == 0) { | |
3481 | 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); | |
3482 | r = 1; | |
3483 | } else | |
3484 | log_unit_debug(u, "New main PID "PID_FMT" does not belong to service, refusing.", new_main_pid); | |
3485 | } | |
3486 | if (r > 0) { | |
3487 | service_set_main_pid(s, new_main_pid); | |
3488 | unit_watch_pid(UNIT(s), new_main_pid); | |
3489 | notify_dbus = true; | |
3490 | } | |
663996b3 MS |
3491 | } |
3492 | } | |
3493 | ||
52ad194e MB |
3494 | /* Interpret READY=/STOPPING=/RELOADING=. Last one wins. */ |
3495 | STRV_FOREACH_BACKWARDS(i, tags) { | |
5eef597e | 3496 | |
52ad194e MB |
3497 | if (streq(*i, "READY=1")) { |
3498 | s->notify_state = NOTIFY_READY; | |
5eef597e | 3499 | |
52ad194e MB |
3500 | /* Type=notify services inform us about completed |
3501 | * initialization with READY=1 */ | |
3502 | if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START) | |
3503 | service_enter_start_post(s); | |
5eef597e | 3504 | |
52ad194e MB |
3505 | /* Sending READY=1 while we are reloading informs us |
3506 | * that the reloading is complete */ | |
3507 | if (s->state == SERVICE_RELOAD && s->control_pid == 0) | |
3508 | service_enter_running(s, SERVICE_SUCCESS); | |
5eef597e | 3509 | |
52ad194e MB |
3510 | notify_dbus = true; |
3511 | break; | |
5eef597e | 3512 | |
52ad194e MB |
3513 | } else if (streq(*i, "RELOADING=1")) { |
3514 | s->notify_state = NOTIFY_RELOADING; | |
5eef597e | 3515 | |
52ad194e MB |
3516 | if (s->state == SERVICE_RUNNING) |
3517 | service_enter_reload_by_notify(s); | |
5eef597e | 3518 | |
52ad194e MB |
3519 | notify_dbus = true; |
3520 | break; | |
5eef597e | 3521 | |
52ad194e MB |
3522 | } else if (streq(*i, "STOPPING=1")) { |
3523 | s->notify_state = NOTIFY_STOPPING; | |
5eef597e | 3524 | |
52ad194e MB |
3525 | if (s->state == SERVICE_RUNNING) |
3526 | service_enter_stop_by_notify(s); | |
5eef597e | 3527 | |
52ad194e MB |
3528 | notify_dbus = true; |
3529 | break; | |
3530 | } | |
663996b3 MS |
3531 | } |
3532 | ||
3533 | /* Interpret STATUS= */ | |
5eef597e | 3534 | e = strv_find_startswith(tags, "STATUS="); |
663996b3 | 3535 | if (e) { |
5eef597e | 3536 | _cleanup_free_ char *t = NULL; |
663996b3 | 3537 | |
5eef597e MP |
3538 | if (!isempty(e)) { |
3539 | if (!utf8_is_valid(e)) | |
e3bff60a | 3540 | log_unit_warning(u, "Status message in notification message is not UTF-8 clean."); |
5eef597e | 3541 | else { |
5eef597e MP |
3542 | t = strdup(e); |
3543 | if (!t) | |
3544 | log_oom(); | |
663996b3 | 3545 | } |
5eef597e | 3546 | } |
663996b3 | 3547 | |
60f067b4 | 3548 | if (!streq_ptr(s->status_text, t)) { |
8a584da2 | 3549 | free_and_replace(s->status_text, t); |
60f067b4 | 3550 | notify_dbus = true; |
5eef597e MP |
3551 | } |
3552 | } | |
3553 | ||
3554 | /* Interpret ERRNO= */ | |
3555 | e = strv_find_startswith(tags, "ERRNO="); | |
3556 | if (e) { | |
3557 | int status_errno; | |
3558 | ||
52ad194e MB |
3559 | status_errno = parse_errno(e); |
3560 | if (status_errno < 0) | |
3561 | log_unit_warning_errno(u, status_errno, | |
3562 | "Failed to parse ERRNO= field in notification message: %s", e); | |
3563 | else if (s->status_errno != status_errno) { | |
3564 | s->status_errno = status_errno; | |
3565 | notify_dbus = true; | |
5eef597e | 3566 | } |
663996b3 | 3567 | } |
60f067b4 | 3568 | |
52ad194e MB |
3569 | /* Interpret EXTEND_TIMEOUT= */ |
3570 | e = strv_find_startswith(tags, "EXTEND_TIMEOUT_USEC="); | |
3571 | if (e) { | |
3572 | usec_t extend_timeout_usec; | |
3573 | if (safe_atou64(e, &extend_timeout_usec) < 0) | |
3574 | log_unit_warning(u, "Failed to parse EXTEND_TIMEOUT_USEC=%s", e); | |
3575 | else | |
3576 | service_extend_timeout(s, extend_timeout_usec); | |
3577 | } | |
3578 | ||
60f067b4 | 3579 | /* Interpret WATCHDOG= */ |
6300502b | 3580 | if (strv_find(tags, "WATCHDOG=1")) |
60f067b4 | 3581 | service_reset_watchdog(s); |
663996b3 | 3582 | |
52ad194e MB |
3583 | e = strv_find_startswith(tags, "WATCHDOG_USEC="); |
3584 | if (e) { | |
3585 | usec_t watchdog_override_usec; | |
3586 | if (safe_atou64(e, &watchdog_override_usec) < 0) | |
3587 | log_unit_warning(u, "Failed to parse WATCHDOG_USEC=%s", e); | |
3588 | else | |
3589 | service_reset_watchdog_timeout(s, watchdog_override_usec); | |
3590 | } | |
3591 | ||
3592 | /* Process FD store messages. Either FDSTOREREMOVE=1 for removal, or FDSTORE=1 for addition. In both cases, | |
3593 | * process FDNAME= for picking the file descriptor name to use. Note that FDNAME= is required when removing | |
3594 | * fds, but optional when pushing in new fds, for compatibility reasons. */ | |
3595 | if (strv_find(tags, "FDSTOREREMOVE=1")) { | |
3596 | const char *name; | |
3597 | ||
3598 | name = strv_find_startswith(tags, "FDNAME="); | |
3599 | if (!name || !fdname_is_valid(name)) | |
3600 | log_unit_warning(u, "FDSTOREREMOVE=1 requested, but no valid file descriptor name passed, ignoring."); | |
3601 | else | |
3602 | service_remove_fd_store(s, name); | |
3603 | ||
3604 | } else if (strv_find(tags, "FDSTORE=1")) { | |
6300502b MP |
3605 | const char *name; |
3606 | ||
3607 | name = strv_find_startswith(tags, "FDNAME="); | |
3608 | if (name && !fdname_is_valid(name)) { | |
3609 | log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring."); | |
3610 | name = NULL; | |
3611 | } | |
3612 | ||
52ad194e | 3613 | (void) service_add_fd_store_set(s, fds, name); |
5a920b42 MP |
3614 | } |
3615 | ||
663996b3 | 3616 | /* Notify clients about changed status or main pid */ |
60f067b4 JS |
3617 | if (notify_dbus) |
3618 | unit_add_to_dbus_queue(u); | |
663996b3 MS |
3619 | } |
3620 | ||
4c89c718 | 3621 | static int service_get_timeout(Unit *u, usec_t *timeout) { |
60f067b4 | 3622 | Service *s = SERVICE(u); |
4c89c718 | 3623 | uint64_t t; |
663996b3 MS |
3624 | int r; |
3625 | ||
60f067b4 | 3626 | if (!s->timer_event_source) |
663996b3 MS |
3627 | return 0; |
3628 | ||
4c89c718 | 3629 | r = sd_event_source_get_time(s->timer_event_source, &t); |
60f067b4 JS |
3630 | if (r < 0) |
3631 | return r; | |
4c89c718 MP |
3632 | if (t == USEC_INFINITY) |
3633 | return 0; | |
663996b3 | 3634 | |
4c89c718 | 3635 | *timeout = t; |
60f067b4 | 3636 | return 1; |
663996b3 | 3637 | } |
663996b3 MS |
3638 | |
3639 | static void service_bus_name_owner_change( | |
3640 | Unit *u, | |
3641 | const char *name, | |
3642 | const char *old_owner, | |
3643 | const char *new_owner) { | |
3644 | ||
3645 | Service *s = SERVICE(u); | |
60f067b4 | 3646 | int r; |
663996b3 MS |
3647 | |
3648 | assert(s); | |
3649 | assert(name); | |
3650 | ||
3651 | assert(streq(s->bus_name, name)); | |
3652 | assert(old_owner || new_owner); | |
3653 | ||
3654 | if (old_owner && new_owner) | |
e3bff60a | 3655 | log_unit_debug(u, "D-Bus name %s changed owner from %s to %s", name, old_owner, new_owner); |
663996b3 | 3656 | else if (old_owner) |
e3bff60a | 3657 | log_unit_debug(u, "D-Bus name %s no longer registered by %s", name, old_owner); |
663996b3 | 3658 | else |
e3bff60a | 3659 | log_unit_debug(u, "D-Bus name %s now registered by %s", name, new_owner); |
663996b3 MS |
3660 | |
3661 | s->bus_name_good = !!new_owner; | |
3662 | ||
4c89c718 MP |
3663 | /* Track the current owner, so we can reconstruct changes after a daemon reload */ |
3664 | r = free_and_strdup(&s->bus_name_owner, new_owner); | |
3665 | if (r < 0) { | |
3666 | log_unit_error_errno(u, r, "Unable to set new bus name owner %s: %m", new_owner); | |
3667 | return; | |
3668 | } | |
3669 | ||
663996b3 MS |
3670 | if (s->type == SERVICE_DBUS) { |
3671 | ||
3672 | /* service_enter_running() will figure out what to | |
3673 | * do */ | |
3674 | if (s->state == SERVICE_RUNNING) | |
3675 | service_enter_running(s, SERVICE_SUCCESS); | |
3676 | else if (s->state == SERVICE_START && new_owner) | |
3677 | service_enter_start_post(s); | |
3678 | ||
3679 | } else if (new_owner && | |
3680 | s->main_pid <= 0 && | |
f5e65279 MB |
3681 | IN_SET(s->state, |
3682 | SERVICE_START, | |
3683 | SERVICE_START_POST, | |
3684 | SERVICE_RUNNING, | |
3685 | SERVICE_RELOAD)) { | |
663996b3 | 3686 | |
4c89c718 | 3687 | _cleanup_(sd_bus_creds_unrefp) sd_bus_creds *creds = NULL; |
60f067b4 | 3688 | pid_t pid; |
663996b3 | 3689 | |
60f067b4 | 3690 | /* Try to acquire PID from bus service */ |
663996b3 | 3691 | |
5eef597e | 3692 | r = sd_bus_get_name_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds); |
60f067b4 JS |
3693 | if (r >= 0) |
3694 | r = sd_bus_creds_get_pid(creds, &pid); | |
3695 | if (r >= 0) { | |
2897b343 | 3696 | log_unit_debug(u, "D-Bus name %s is now owned by process " PID_FMT, name, pid); |
663996b3 | 3697 | |
60f067b4 JS |
3698 | service_set_main_pid(s, pid); |
3699 | unit_watch_pid(UNIT(s), pid); | |
3700 | } | |
14228c0d | 3701 | } |
663996b3 MS |
3702 | } |
3703 | ||
5eef597e | 3704 | int service_set_socket_fd(Service *s, int fd, Socket *sock, bool selinux_context_net) { |
60f067b4 JS |
3705 | _cleanup_free_ char *peer = NULL; |
3706 | int r; | |
663996b3 MS |
3707 | |
3708 | assert(s); | |
3709 | assert(fd >= 0); | |
3710 | ||
aa27b158 MP |
3711 | /* This is called by the socket code when instantiating a new service for a stream socket and the socket needs |
3712 | * to be configured. We take ownership of the passed fd on success. */ | |
663996b3 MS |
3713 | |
3714 | if (UNIT(s)->load_state != UNIT_LOADED) | |
3715 | return -EINVAL; | |
3716 | ||
3717 | if (s->socket_fd >= 0) | |
3718 | return -EBUSY; | |
3719 | ||
3720 | if (s->state != SERVICE_DEAD) | |
3721 | return -EAGAIN; | |
3722 | ||
4c89c718 | 3723 | if (getpeername_pretty(fd, true, &peer) >= 0) { |
60f067b4 JS |
3724 | |
3725 | if (UNIT(s)->description) { | |
3726 | _cleanup_free_ char *a; | |
3727 | ||
2897b343 | 3728 | a = strjoin(UNIT(s)->description, " (", peer, ")"); |
60f067b4 JS |
3729 | if (!a) |
3730 | return -ENOMEM; | |
3731 | ||
3732 | r = unit_set_description(UNIT(s), a); | |
3733 | } else | |
3734 | r = unit_set_description(UNIT(s), peer); | |
3735 | ||
3736 | if (r < 0) | |
3737 | return r; | |
3738 | } | |
3739 | ||
52ad194e | 3740 | r = unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false, UNIT_DEPENDENCY_IMPLICIT); |
aa27b158 MP |
3741 | if (r < 0) |
3742 | return r; | |
3743 | ||
663996b3 | 3744 | s->socket_fd = fd; |
5eef597e | 3745 | s->socket_fd_selinux_context_net = selinux_context_net; |
663996b3 MS |
3746 | |
3747 | unit_ref_set(&s->accept_socket, UNIT(sock)); | |
aa27b158 | 3748 | return 0; |
663996b3 MS |
3749 | } |
3750 | ||
3751 | static void service_reset_failed(Unit *u) { | |
3752 | Service *s = SERVICE(u); | |
3753 | ||
3754 | assert(s); | |
3755 | ||
3756 | if (s->state == SERVICE_FAILED) | |
3757 | service_set_state(s, SERVICE_DEAD); | |
3758 | ||
3759 | s->result = SERVICE_SUCCESS; | |
3760 | s->reload_result = SERVICE_SUCCESS; | |
f5e65279 MB |
3761 | s->n_restarts = 0; |
3762 | s->flush_n_restarts = false; | |
663996b3 MS |
3763 | } |
3764 | ||
60f067b4 | 3765 | static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
663996b3 | 3766 | Service *s = SERVICE(u); |
14228c0d | 3767 | |
f5e65279 MB |
3768 | assert(s); |
3769 | ||
663996b3 MS |
3770 | return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error); |
3771 | } | |
3772 | ||
aa27b158 MP |
3773 | static int service_main_pid(Unit *u) { |
3774 | Service *s = SERVICE(u); | |
3775 | ||
3776 | assert(s); | |
3777 | ||
3778 | return s->main_pid; | |
3779 | } | |
3780 | ||
3781 | static int service_control_pid(Unit *u) { | |
3782 | Service *s = SERVICE(u); | |
3783 | ||
3784 | assert(s); | |
3785 | ||
3786 | return s->control_pid; | |
3787 | } | |
3788 | ||
1d42b86d MB |
3789 | static bool service_needs_console(Unit *u) { |
3790 | Service *s = SERVICE(u); | |
3791 | ||
3792 | assert(s); | |
3793 | ||
3794 | /* We provide our own implementation of this here, instead of relying of the generic implementation | |
3795 | * unit_needs_console() provides, since we want to return false if we are in SERVICE_EXITED state. */ | |
3796 | ||
3797 | if (!exec_context_may_touch_console(&s->exec_context)) | |
3798 | return false; | |
3799 | ||
3800 | return IN_SET(s->state, | |
3801 | SERVICE_START_PRE, | |
3802 | SERVICE_START, | |
3803 | SERVICE_START_POST, | |
3804 | SERVICE_RUNNING, | |
3805 | SERVICE_RELOAD, | |
3806 | SERVICE_STOP, | |
3807 | SERVICE_STOP_SIGABRT, | |
3808 | SERVICE_STOP_SIGTERM, | |
3809 | SERVICE_STOP_SIGKILL, | |
3810 | SERVICE_STOP_POST, | |
3811 | SERVICE_FINAL_SIGTERM, | |
3812 | SERVICE_FINAL_SIGKILL); | |
3813 | } | |
3814 | ||
663996b3 MS |
3815 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { |
3816 | [SERVICE_RESTART_NO] = "no", | |
3817 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
3818 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", | |
60f067b4 | 3819 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
14228c0d | 3820 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
663996b3 | 3821 | [SERVICE_RESTART_ON_ABORT] = "on-abort", |
60f067b4 | 3822 | [SERVICE_RESTART_ALWAYS] = "always", |
663996b3 MS |
3823 | }; |
3824 | ||
3825 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
3826 | ||
3827 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
3828 | [SERVICE_SIMPLE] = "simple", | |
3829 | [SERVICE_FORKING] = "forking", | |
3830 | [SERVICE_ONESHOT] = "oneshot", | |
3831 | [SERVICE_DBUS] = "dbus", | |
3832 | [SERVICE_NOTIFY] = "notify", | |
3833 | [SERVICE_IDLE] = "idle" | |
3834 | }; | |
3835 | ||
3836 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
3837 | ||
3838 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { | |
3839 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", | |
3840 | [SERVICE_EXEC_START] = "ExecStart", | |
3841 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
3842 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
3843 | [SERVICE_EXEC_STOP] = "ExecStop", | |
3844 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
3845 | }; | |
3846 | ||
3847 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
3848 | ||
5eef597e MP |
3849 | static const char* const notify_state_table[_NOTIFY_STATE_MAX] = { |
3850 | [NOTIFY_UNKNOWN] = "unknown", | |
3851 | [NOTIFY_READY] = "ready", | |
3852 | [NOTIFY_RELOADING] = "reloading", | |
3853 | [NOTIFY_STOPPING] = "stopping", | |
3854 | }; | |
3855 | ||
3856 | DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); | |
3857 | ||
663996b3 MS |
3858 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
3859 | [SERVICE_SUCCESS] = "success", | |
3860 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
2897b343 | 3861 | [SERVICE_FAILURE_PROTOCOL] = "protocol", |
663996b3 MS |
3862 | [SERVICE_FAILURE_TIMEOUT] = "timeout", |
3863 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
3864 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
3865 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", | |
3866 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", | |
aa27b158 | 3867 | [SERVICE_FAILURE_START_LIMIT_HIT] = "start-limit-hit", |
663996b3 MS |
3868 | }; |
3869 | ||
3870 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
3871 | ||
663996b3 MS |
3872 | const UnitVTable service_vtable = { |
3873 | .object_size = sizeof(Service), | |
60f067b4 JS |
3874 | .exec_context_offset = offsetof(Service, exec_context), |
3875 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
3876 | .kill_context_offset = offsetof(Service, kill_context), | |
3877 | .exec_runtime_offset = offsetof(Service, exec_runtime), | |
8a584da2 | 3878 | .dynamic_creds_offset = offsetof(Service, dynamic_creds), |
663996b3 MS |
3879 | |
3880 | .sections = | |
3881 | "Unit\0" | |
3882 | "Service\0" | |
3883 | "Install\0", | |
14228c0d | 3884 | .private_section = "Service", |
663996b3 MS |
3885 | |
3886 | .init = service_init, | |
3887 | .done = service_done, | |
3888 | .load = service_load, | |
e735f4d4 | 3889 | .release_resources = service_release_resources, |
663996b3 MS |
3890 | |
3891 | .coldplug = service_coldplug, | |
3892 | ||
3893 | .dump = service_dump, | |
3894 | ||
3895 | .start = service_start, | |
3896 | .stop = service_stop, | |
3897 | .reload = service_reload, | |
3898 | ||
3899 | .can_reload = service_can_reload, | |
3900 | ||
3901 | .kill = service_kill, | |
3902 | ||
3903 | .serialize = service_serialize, | |
3904 | .deserialize_item = service_deserialize_item, | |
3905 | ||
3906 | .active_state = service_active_state, | |
3907 | .sub_state_to_string = service_sub_state_to_string, | |
3908 | ||
52ad194e MB |
3909 | .will_restart = service_will_restart, |
3910 | ||
663996b3 | 3911 | .check_gc = service_check_gc, |
663996b3 MS |
3912 | |
3913 | .sigchld_event = service_sigchld_event, | |
663996b3 MS |
3914 | |
3915 | .reset_failed = service_reset_failed, | |
3916 | ||
14228c0d | 3917 | .notify_cgroup_empty = service_notify_cgroup_empty_event, |
663996b3 MS |
3918 | .notify_message = service_notify_message, |
3919 | ||
aa27b158 MP |
3920 | .main_pid = service_main_pid, |
3921 | .control_pid = service_control_pid, | |
3922 | ||
663996b3 | 3923 | .bus_name_owner_change = service_bus_name_owner_change, |
663996b3 | 3924 | |
60f067b4 | 3925 | .bus_vtable = bus_service_vtable, |
14228c0d MB |
3926 | .bus_set_property = bus_service_set_property, |
3927 | .bus_commit_properties = bus_service_commit_properties, | |
3928 | ||
60f067b4 | 3929 | .get_timeout = service_get_timeout, |
1d42b86d | 3930 | .needs_console = service_needs_console, |
14228c0d | 3931 | .can_transient = true, |
663996b3 | 3932 | |
663996b3 MS |
3933 | .status_message_formats = { |
3934 | .starting_stopping = { | |
3935 | [0] = "Starting %s...", | |
3936 | [1] = "Stopping %s...", | |
3937 | }, | |
3938 | .finished_start_job = { | |
3939 | [JOB_DONE] = "Started %s.", | |
3940 | [JOB_FAILED] = "Failed to start %s.", | |
663996b3 MS |
3941 | }, |
3942 | .finished_stop_job = { | |
3943 | [JOB_DONE] = "Stopped %s.", | |
3944 | [JOB_FAILED] = "Stopped (with error) %s.", | |
663996b3 MS |
3945 | }, |
3946 | }, | |
3947 | }; |