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