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