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