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