]>
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 General Public License as published by | |
10 | the Free Software Foundation; either version 2 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 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU 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 | ||
27 | #include "unit.h" | |
28 | #include "service.h" | |
29 | #include "load-fragment.h" | |
30 | #include "load-dropin.h" | |
31 | #include "log.h" | |
32 | #include "strv.h" | |
33 | #include "unit-name.h" | |
34 | #include "dbus-service.h" | |
35 | #include "special.h" | |
36 | #include "bus-errors.h" | |
37 | #include "exit-status.h" | |
38 | ||
39 | #define COMMENTS "#;\n" | |
40 | #define NEWLINES "\n\r" | |
41 | ||
42 | #ifdef HAVE_SYSV_COMPAT | |
43 | ||
44 | #define DEFAULT_SYSV_TIMEOUT_USEC (3*USEC_PER_MINUTE) | |
45 | ||
46 | typedef enum RunlevelType { | |
47 | RUNLEVEL_UP, | |
48 | RUNLEVEL_DOWN, | |
49 | RUNLEVEL_SYSINIT | |
50 | } RunlevelType; | |
51 | ||
52 | static const struct { | |
53 | const char *path; | |
54 | const char *target; | |
55 | const RunlevelType type; | |
56 | } rcnd_table[] = { | |
57 | /* Standard SysV runlevels for start-up */ | |
58 | { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP }, | |
59 | { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP }, | |
60 | { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP }, | |
61 | { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP }, | |
62 | { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP }, | |
63 | ||
64 | #ifdef TARGET_SUSE | |
65 | /* SUSE style boot.d */ | |
66 | { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT }, | |
67 | #endif | |
68 | ||
69 | #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) || defined(TARGET_FRUGALWARE) | |
70 | /* Debian style rcS.d */ | |
71 | { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT }, | |
72 | #endif | |
73 | ||
74 | /* Standard SysV runlevels for shutdown */ | |
75 | { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN }, | |
76 | { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN } | |
77 | ||
78 | /* Note that the order here matters, as we read the | |
79 | directories in this order, and we want to make sure that | |
80 | sysv_start_priority is known when we first load the | |
81 | unit. And that value we only know from S links. Hence | |
82 | UP/SYSINIT must be read before DOWN */ | |
83 | }; | |
84 | ||
85 | #define RUNLEVELS_UP "12345" | |
86 | /* #define RUNLEVELS_DOWN "06" */ | |
87 | /* #define RUNLEVELS_BOOT "bBsS" */ | |
88 | #endif | |
89 | ||
90 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { | |
91 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
92 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
93 | [SERVICE_START] = UNIT_ACTIVATING, | |
94 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
95 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
96 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
97 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
98 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
99 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
100 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
101 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
102 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
103 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
104 | [SERVICE_FAILED] = UNIT_FAILED, | |
105 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING | |
106 | }; | |
107 | ||
108 | static void service_init(Unit *u) { | |
109 | Service *s = SERVICE(u); | |
110 | ||
111 | assert(u); | |
112 | assert(u->meta.load_state == UNIT_STUB); | |
113 | ||
114 | s->timeout_usec = DEFAULT_TIMEOUT_USEC; | |
115 | s->restart_usec = DEFAULT_RESTART_USEC; | |
116 | s->timer_watch.type = WATCH_INVALID; | |
117 | #ifdef HAVE_SYSV_COMPAT | |
118 | s->sysv_start_priority = -1; | |
119 | #endif | |
120 | s->socket_fd = -1; | |
121 | ||
122 | exec_context_init(&s->exec_context); | |
123 | ||
124 | RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5); | |
125 | ||
126 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
127 | } | |
128 | ||
129 | static void service_unwatch_control_pid(Service *s) { | |
130 | assert(s); | |
131 | ||
132 | if (s->control_pid <= 0) | |
133 | return; | |
134 | ||
135 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
136 | s->control_pid = 0; | |
137 | } | |
138 | ||
139 | static void service_unwatch_main_pid(Service *s) { | |
140 | assert(s); | |
141 | ||
142 | if (s->main_pid <= 0) | |
143 | return; | |
144 | ||
145 | unit_unwatch_pid(UNIT(s), s->main_pid); | |
146 | s->main_pid = 0; | |
147 | } | |
148 | ||
149 | static int service_set_main_pid(Service *s, pid_t pid) { | |
150 | pid_t ppid; | |
151 | ||
152 | assert(s); | |
153 | ||
154 | if (pid <= 1) | |
155 | return -EINVAL; | |
156 | ||
157 | if (pid == getpid()) | |
158 | return -EINVAL; | |
159 | ||
160 | if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) | |
161 | log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.", | |
162 | s->meta.id, (unsigned long) pid); | |
163 | ||
164 | s->main_pid = pid; | |
165 | s->main_pid_known = true; | |
166 | ||
167 | exec_status_start(&s->main_exec_status, pid); | |
168 | ||
169 | return 0; | |
170 | } | |
171 | ||
172 | static void service_close_socket_fd(Service *s) { | |
173 | assert(s); | |
174 | ||
175 | if (s->socket_fd < 0) | |
176 | return; | |
177 | ||
178 | close_nointr_nofail(s->socket_fd); | |
179 | s->socket_fd = -1; | |
180 | } | |
181 | ||
182 | static void service_connection_unref(Service *s) { | |
183 | assert(s); | |
184 | ||
185 | if (!s->accept_socket) | |
186 | return; | |
187 | ||
188 | socket_connection_unref(s->accept_socket); | |
189 | s->accept_socket = NULL; | |
190 | } | |
191 | ||
192 | static void service_done(Unit *u) { | |
193 | Service *s = SERVICE(u); | |
194 | ||
195 | assert(s); | |
196 | ||
197 | free(s->pid_file); | |
198 | s->pid_file = NULL; | |
199 | ||
200 | #ifdef HAVE_SYSV_COMPAT | |
201 | free(s->sysv_path); | |
202 | s->sysv_path = NULL; | |
203 | ||
204 | free(s->sysv_runlevels); | |
205 | s->sysv_runlevels = NULL; | |
206 | #endif | |
207 | ||
208 | free(s->status_text); | |
209 | s->status_text = NULL; | |
210 | ||
211 | exec_context_done(&s->exec_context); | |
212 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); | |
213 | s->control_command = NULL; | |
214 | s->main_command = NULL; | |
215 | ||
216 | /* This will leak a process, but at least no memory or any of | |
217 | * our resources */ | |
218 | service_unwatch_main_pid(s); | |
219 | service_unwatch_control_pid(s); | |
220 | ||
221 | if (s->bus_name) { | |
222 | unit_unwatch_bus_name(UNIT(u), s->bus_name); | |
223 | free(s->bus_name); | |
224 | s->bus_name = NULL; | |
225 | } | |
226 | ||
227 | service_close_socket_fd(s); | |
228 | service_connection_unref(s); | |
229 | ||
230 | set_free(s->configured_sockets); | |
231 | ||
232 | unit_unwatch_timer(u, &s->timer_watch); | |
233 | } | |
234 | ||
235 | #ifdef HAVE_SYSV_COMPAT | |
236 | static char *sysv_translate_name(const char *name) { | |
237 | char *r; | |
238 | ||
239 | if (!(r = new(char, strlen(name) + sizeof(".service")))) | |
240 | return NULL; | |
241 | ||
242 | #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) | |
243 | if (endswith(name, ".sh")) | |
244 | /* Drop Debian-style .sh suffix */ | |
245 | strcpy(stpcpy(r, name) - 3, ".service"); | |
246 | #endif | |
247 | #ifdef TARGET_SUSE | |
248 | if (startswith(name, "boot.")) | |
249 | /* Drop SuSE-style boot. prefix */ | |
250 | strcpy(stpcpy(r, name + 5), ".service"); | |
251 | #endif | |
252 | #ifdef TARGET_FRUGALWARE | |
253 | if (startswith(name, "rc.")) | |
254 | /* Drop Frugalware-style rc. prefix */ | |
255 | strcpy(stpcpy(r, name + 3), ".service"); | |
256 | #endif | |
257 | else | |
258 | /* Normal init scripts */ | |
259 | strcpy(stpcpy(r, name), ".service"); | |
260 | ||
261 | return r; | |
262 | } | |
263 | ||
264 | static int sysv_translate_facility(const char *name, const char *filename, char **_r) { | |
265 | ||
266 | /* We silently ignore the $ prefix here. According to the LSB | |
267 | * spec it simply indicates whether something is a | |
268 | * standardized name or a distribution-specific one. Since we | |
269 | * just follow what already exists and do not introduce new | |
270 | * uses or names we don't care who introduced a new name. */ | |
271 | ||
272 | static const char * const table[] = { | |
273 | /* LSB defined facilities */ | |
274 | "local_fs", SPECIAL_LOCAL_FS_TARGET, | |
275 | "network", SPECIAL_NETWORK_TARGET, | |
276 | "named", SPECIAL_NSS_LOOKUP_TARGET, | |
277 | "portmap", SPECIAL_RPCBIND_TARGET, | |
278 | "remote_fs", SPECIAL_REMOTE_FS_TARGET, | |
279 | "syslog", SPECIAL_SYSLOG_TARGET, | |
280 | "time", SPECIAL_RTC_SET_TARGET, | |
281 | ||
282 | /* common extensions */ | |
283 | "mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, | |
284 | "x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE, | |
285 | "null", NULL, | |
286 | ||
287 | #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) | |
288 | "mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, | |
289 | #endif | |
290 | ||
291 | #ifdef TARGET_FEDORA | |
292 | "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, | |
293 | "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, | |
294 | "httpd", SPECIAL_HTTP_DAEMON_TARGET, | |
295 | #endif | |
296 | ||
297 | #ifdef TARGET_SUSE | |
298 | "smtp", SPECIAL_MAIL_TRANSFER_AGENT_TARGET, | |
299 | #endif | |
300 | }; | |
301 | ||
302 | unsigned i; | |
303 | char *r; | |
304 | const char *n; | |
305 | ||
306 | assert(name); | |
307 | assert(_r); | |
308 | ||
309 | n = *name == '$' ? name + 1 : name; | |
310 | ||
311 | for (i = 0; i < ELEMENTSOF(table); i += 2) { | |
312 | ||
313 | if (!streq(table[i], n)) | |
314 | continue; | |
315 | ||
316 | if (!table[i+1]) | |
317 | return 0; | |
318 | ||
319 | if (!(r = strdup(table[i+1]))) | |
320 | return -ENOMEM; | |
321 | ||
322 | goto finish; | |
323 | } | |
324 | ||
325 | /* If we don't know this name, fallback heuristics to figure | |
326 | * out whether something is a target or a service alias. */ | |
327 | ||
328 | if (*name == '$') { | |
329 | if (!unit_prefix_is_valid(n)) | |
330 | return -EINVAL; | |
331 | ||
332 | /* Facilities starting with $ are most likely targets */ | |
333 | r = unit_name_build(n, NULL, ".target"); | |
334 | } else if (filename && streq(name, filename)) | |
335 | /* Names equalling the file name of the services are redundant */ | |
336 | return 0; | |
337 | else | |
338 | /* Everything else we assume to be normal service names */ | |
339 | r = sysv_translate_name(n); | |
340 | ||
341 | if (!r) | |
342 | return -ENOMEM; | |
343 | ||
344 | finish: | |
345 | ||
346 | if (_r) | |
347 | *_r = r; | |
348 | ||
349 | return 1; | |
350 | } | |
351 | ||
352 | static int sysv_fix_order(Service *s) { | |
353 | Meta *other; | |
354 | int r; | |
355 | ||
356 | assert(s); | |
357 | ||
358 | if (s->sysv_start_priority < 0) | |
359 | return 0; | |
360 | ||
361 | /* For each pair of services where at least one lacks a LSB | |
362 | * header, we use the start priority value to order things. */ | |
363 | ||
364 | LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) { | |
365 | Service *t; | |
366 | UnitDependency d; | |
367 | bool special_s, special_t; | |
368 | ||
369 | t = (Service*) other; | |
370 | ||
371 | if (s == t) | |
372 | continue; | |
373 | ||
374 | if (t->meta.load_state != UNIT_LOADED) | |
375 | continue; | |
376 | ||
377 | if (t->sysv_start_priority < 0) | |
378 | continue; | |
379 | ||
380 | /* If both units have modern headers we don't care | |
381 | * about the priorities */ | |
382 | if ((s->meta.fragment_path || s->sysv_has_lsb) && | |
383 | (t->meta.fragment_path || t->sysv_has_lsb)) | |
384 | continue; | |
385 | ||
386 | special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels); | |
387 | special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels); | |
388 | ||
389 | if (special_t && !special_s) | |
390 | d = UNIT_AFTER; | |
391 | else if (special_s && !special_t) | |
392 | d = UNIT_BEFORE; | |
393 | else if (t->sysv_start_priority < s->sysv_start_priority) | |
394 | d = UNIT_AFTER; | |
395 | else if (t->sysv_start_priority > s->sysv_start_priority) | |
396 | d = UNIT_BEFORE; | |
397 | else | |
398 | continue; | |
399 | ||
400 | /* FIXME: Maybe we should compare the name here lexicographically? */ | |
401 | ||
402 | if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0) | |
403 | return r; | |
404 | } | |
405 | ||
406 | return 0; | |
407 | } | |
408 | ||
409 | static ExecCommand *exec_command_new(const char *path, const char *arg1) { | |
410 | ExecCommand *c; | |
411 | ||
412 | if (!(c = new0(ExecCommand, 1))) | |
413 | return NULL; | |
414 | ||
415 | if (!(c->path = strdup(path))) { | |
416 | free(c); | |
417 | return NULL; | |
418 | } | |
419 | ||
420 | if (!(c->argv = strv_new(path, arg1, NULL))) { | |
421 | free(c->path); | |
422 | free(c); | |
423 | return NULL; | |
424 | } | |
425 | ||
426 | return c; | |
427 | } | |
428 | ||
429 | static int sysv_exec_commands(Service *s) { | |
430 | ExecCommand *c; | |
431 | ||
432 | assert(s); | |
433 | assert(s->sysv_path); | |
434 | ||
435 | if (!(c = exec_command_new(s->sysv_path, "start"))) | |
436 | return -ENOMEM; | |
437 | exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c); | |
438 | ||
439 | if (!(c = exec_command_new(s->sysv_path, "stop"))) | |
440 | return -ENOMEM; | |
441 | exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c); | |
442 | ||
443 | if (!(c = exec_command_new(s->sysv_path, "reload"))) | |
444 | return -ENOMEM; | |
445 | exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c); | |
446 | ||
447 | return 0; | |
448 | } | |
449 | ||
450 | static int service_load_sysv_path(Service *s, const char *path) { | |
451 | FILE *f; | |
452 | Unit *u; | |
453 | unsigned line = 0; | |
454 | int r; | |
455 | enum { | |
456 | NORMAL, | |
457 | DESCRIPTION, | |
458 | LSB, | |
459 | LSB_DESCRIPTION | |
460 | } state = NORMAL; | |
461 | char *short_description = NULL, *long_description = NULL, *chkconfig_description = NULL, *description; | |
462 | ||
463 | assert(s); | |
464 | assert(path); | |
465 | ||
466 | u = UNIT(s); | |
467 | ||
468 | if (!(f = fopen(path, "re"))) { | |
469 | r = errno == ENOENT ? 0 : -errno; | |
470 | goto finish; | |
471 | } | |
472 | ||
473 | free(s->sysv_path); | |
474 | if (!(s->sysv_path = strdup(path))) { | |
475 | r = -ENOMEM; | |
476 | goto finish; | |
477 | } | |
478 | ||
479 | while (!feof(f)) { | |
480 | char l[LINE_MAX], *t; | |
481 | ||
482 | if (!fgets(l, sizeof(l), f)) { | |
483 | if (feof(f)) | |
484 | break; | |
485 | ||
486 | r = -errno; | |
487 | log_error("Failed to read configuration file '%s': %s", path, strerror(-r)); | |
488 | goto finish; | |
489 | } | |
490 | ||
491 | line++; | |
492 | ||
493 | t = strstrip(l); | |
494 | if (*t != '#') | |
495 | continue; | |
496 | ||
497 | if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) { | |
498 | state = LSB; | |
499 | s->sysv_has_lsb = true; | |
500 | continue; | |
501 | } | |
502 | ||
503 | if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) { | |
504 | state = NORMAL; | |
505 | continue; | |
506 | } | |
507 | ||
508 | t++; | |
509 | t += strspn(t, WHITESPACE); | |
510 | ||
511 | if (state == NORMAL) { | |
512 | ||
513 | /* Try to parse Red Hat style chkconfig headers */ | |
514 | ||
515 | if (startswith_no_case(t, "chkconfig:")) { | |
516 | int start_priority; | |
517 | char runlevels[16], *k; | |
518 | ||
519 | state = NORMAL; | |
520 | ||
521 | if (sscanf(t+10, "%15s %i %*i", | |
522 | runlevels, | |
523 | &start_priority) != 2) { | |
524 | ||
525 | log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line); | |
526 | continue; | |
527 | } | |
528 | ||
529 | /* A start priority gathered from the | |
530 | * symlink farms is preferred over the | |
531 | * data from the LSB header. */ | |
532 | if (start_priority < 0 || start_priority > 99) | |
533 | log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line); | |
534 | else if (s->sysv_start_priority < 0) | |
535 | s->sysv_start_priority = start_priority; | |
536 | ||
537 | char_array_0(runlevels); | |
538 | k = delete_chars(runlevels, WHITESPACE "-"); | |
539 | ||
540 | if (k[0]) { | |
541 | char *d; | |
542 | ||
543 | if (!(d = strdup(k))) { | |
544 | r = -ENOMEM; | |
545 | goto finish; | |
546 | } | |
547 | ||
548 | free(s->sysv_runlevels); | |
549 | s->sysv_runlevels = d; | |
550 | } | |
551 | ||
552 | } else if (startswith_no_case(t, "description:")) { | |
553 | ||
554 | size_t k = strlen(t); | |
555 | char *d; | |
556 | const char *j; | |
557 | ||
558 | if (t[k-1] == '\\') { | |
559 | state = DESCRIPTION; | |
560 | t[k-1] = 0; | |
561 | } | |
562 | ||
563 | if ((j = strstrip(t+12)) && *j) { | |
564 | if (!(d = strdup(j))) { | |
565 | r = -ENOMEM; | |
566 | goto finish; | |
567 | } | |
568 | } else | |
569 | d = NULL; | |
570 | ||
571 | free(chkconfig_description); | |
572 | chkconfig_description = d; | |
573 | ||
574 | } else if (startswith_no_case(t, "pidfile:")) { | |
575 | ||
576 | char *fn; | |
577 | ||
578 | state = NORMAL; | |
579 | ||
580 | fn = strstrip(t+8); | |
581 | if (!path_is_absolute(fn)) { | |
582 | log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line); | |
583 | continue; | |
584 | } | |
585 | ||
586 | if (!(fn = strdup(fn))) { | |
587 | r = -ENOMEM; | |
588 | goto finish; | |
589 | } | |
590 | ||
591 | free(s->pid_file); | |
592 | s->pid_file = fn; | |
593 | } | |
594 | ||
595 | } else if (state == DESCRIPTION) { | |
596 | ||
597 | /* Try to parse Red Hat style description | |
598 | * continuation */ | |
599 | ||
600 | size_t k = strlen(t); | |
601 | char *j; | |
602 | ||
603 | if (t[k-1] == '\\') | |
604 | t[k-1] = 0; | |
605 | else | |
606 | state = NORMAL; | |
607 | ||
608 | if ((j = strstrip(t)) && *j) { | |
609 | char *d = NULL; | |
610 | ||
611 | if (chkconfig_description) | |
612 | asprintf(&d, "%s %s", chkconfig_description, j); | |
613 | else | |
614 | d = strdup(j); | |
615 | ||
616 | if (!d) { | |
617 | r = -ENOMEM; | |
618 | goto finish; | |
619 | } | |
620 | ||
621 | free(chkconfig_description); | |
622 | chkconfig_description = d; | |
623 | } | |
624 | ||
625 | } else if (state == LSB || state == LSB_DESCRIPTION) { | |
626 | ||
627 | if (startswith_no_case(t, "Provides:")) { | |
628 | char *i, *w; | |
629 | size_t z; | |
630 | ||
631 | state = LSB; | |
632 | ||
633 | FOREACH_WORD_QUOTED(w, z, t+9, i) { | |
634 | char *n, *m; | |
635 | ||
636 | if (!(n = strndup(w, z))) { | |
637 | r = -ENOMEM; | |
638 | goto finish; | |
639 | } | |
640 | ||
641 | r = sysv_translate_facility(n, file_name_from_path(path), &m); | |
642 | free(n); | |
643 | ||
644 | if (r < 0) | |
645 | goto finish; | |
646 | ||
647 | if (r == 0) | |
648 | continue; | |
649 | ||
650 | if (unit_name_to_type(m) == UNIT_SERVICE) | |
651 | r = unit_add_name(u, m); | |
652 | else { | |
653 | r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true); | |
654 | ||
655 | if (s->sysv_enabled) { | |
656 | int k; | |
657 | ||
658 | if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0) | |
659 | r = k; | |
660 | } | |
661 | } | |
662 | ||
663 | if (r < 0) | |
664 | log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r)); | |
665 | ||
666 | free(m); | |
667 | } | |
668 | ||
669 | } else if (startswith_no_case(t, "Required-Start:") || | |
670 | startswith_no_case(t, "Should-Start:") || | |
671 | startswith_no_case(t, "X-Start-Before:") || | |
672 | startswith_no_case(t, "X-Start-After:")) { | |
673 | char *i, *w; | |
674 | size_t z; | |
675 | ||
676 | state = LSB; | |
677 | ||
678 | FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) { | |
679 | char *n, *m; | |
680 | ||
681 | if (!(n = strndup(w, z))) { | |
682 | r = -ENOMEM; | |
683 | goto finish; | |
684 | } | |
685 | ||
686 | r = sysv_translate_facility(n, file_name_from_path(path), &m); | |
687 | ||
688 | if (r < 0) { | |
689 | log_error("[%s:%u] Failed to translate LSB dependency %s, ignoring: %s", path, line, n, strerror(-r)); | |
690 | free(n); | |
691 | continue; | |
692 | } | |
693 | ||
694 | free(n); | |
695 | ||
696 | if (r == 0) | |
697 | continue; | |
698 | ||
699 | r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true); | |
700 | ||
701 | if (r < 0) | |
702 | log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r)); | |
703 | ||
704 | free(m); | |
705 | } | |
706 | } else if (startswith_no_case(t, "Default-Start:")) { | |
707 | char *k, *d; | |
708 | ||
709 | state = LSB; | |
710 | ||
711 | k = delete_chars(t+14, WHITESPACE "-"); | |
712 | ||
713 | if (k[0] != 0) { | |
714 | if (!(d = strdup(k))) { | |
715 | r = -ENOMEM; | |
716 | goto finish; | |
717 | } | |
718 | ||
719 | free(s->sysv_runlevels); | |
720 | s->sysv_runlevels = d; | |
721 | } | |
722 | ||
723 | } else if (startswith_no_case(t, "Description:")) { | |
724 | char *d, *j; | |
725 | ||
726 | state = LSB_DESCRIPTION; | |
727 | ||
728 | if ((j = strstrip(t+12)) && *j) { | |
729 | if (!(d = strdup(j))) { | |
730 | r = -ENOMEM; | |
731 | goto finish; | |
732 | } | |
733 | } else | |
734 | d = NULL; | |
735 | ||
736 | free(long_description); | |
737 | long_description = d; | |
738 | ||
739 | } else if (startswith_no_case(t, "Short-Description:")) { | |
740 | char *d, *j; | |
741 | ||
742 | state = LSB; | |
743 | ||
744 | if ((j = strstrip(t+18)) && *j) { | |
745 | if (!(d = strdup(j))) { | |
746 | r = -ENOMEM; | |
747 | goto finish; | |
748 | } | |
749 | } else | |
750 | d = NULL; | |
751 | ||
752 | free(short_description); | |
753 | short_description = d; | |
754 | ||
755 | } else if (startswith_no_case(t, "X-Interactive:")) { | |
756 | int b; | |
757 | ||
758 | if ((b = parse_boolean(strstrip(t+14))) < 0) { | |
759 | log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line); | |
760 | continue; | |
761 | } | |
762 | ||
763 | if (b) | |
764 | s->exec_context.std_input = EXEC_INPUT_TTY; | |
765 | else | |
766 | s->exec_context.std_input = EXEC_INPUT_NULL; | |
767 | ||
768 | } else if (state == LSB_DESCRIPTION) { | |
769 | ||
770 | if (startswith(l, "#\t") || startswith(l, "# ")) { | |
771 | char *j; | |
772 | ||
773 | if ((j = strstrip(t)) && *j) { | |
774 | char *d = NULL; | |
775 | ||
776 | if (long_description) | |
777 | asprintf(&d, "%s %s", long_description, t); | |
778 | else | |
779 | d = strdup(j); | |
780 | ||
781 | if (!d) { | |
782 | r = -ENOMEM; | |
783 | goto finish; | |
784 | } | |
785 | ||
786 | free(long_description); | |
787 | long_description = d; | |
788 | } | |
789 | ||
790 | } else | |
791 | state = LSB; | |
792 | } | |
793 | } | |
794 | } | |
795 | ||
796 | if ((r = sysv_exec_commands(s)) < 0) | |
797 | goto finish; | |
798 | ||
799 | if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) { | |
800 | /* If there a runlevels configured for this service | |
801 | * but none of the standard ones, then we assume this | |
802 | * is some special kind of service (which might be | |
803 | * needed for early boot) and don't create any links | |
804 | * to it. */ | |
805 | ||
806 | s->meta.default_dependencies = false; | |
807 | ||
808 | /* Don't timeout special services during boot (like fsck) */ | |
809 | s->timeout_usec = 0; | |
810 | } else | |
811 | s->timeout_usec = DEFAULT_SYSV_TIMEOUT_USEC; | |
812 | ||
813 | /* Special setting for all SysV services */ | |
814 | s->type = SERVICE_FORKING; | |
815 | s->remain_after_exit = true; | |
816 | s->restart = SERVICE_RESTART_NO; | |
817 | s->exec_context.std_output = | |
818 | (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY) | |
819 | ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL; | |
820 | s->exec_context.kill_mode = KILL_PROCESS_GROUP; | |
821 | ||
822 | /* We use the long description only if | |
823 | * no short description is set. */ | |
824 | ||
825 | if (short_description) | |
826 | description = short_description; | |
827 | else if (chkconfig_description) | |
828 | description = chkconfig_description; | |
829 | else if (long_description) | |
830 | description = long_description; | |
831 | else | |
832 | description = NULL; | |
833 | ||
834 | if (description) { | |
835 | char *d; | |
836 | ||
837 | if (!(d = strappend("LSB: ", description))) { | |
838 | r = -ENOMEM; | |
839 | goto finish; | |
840 | } | |
841 | ||
842 | u->meta.description = d; | |
843 | } | |
844 | ||
845 | u->meta.load_state = UNIT_LOADED; | |
846 | r = 0; | |
847 | ||
848 | finish: | |
849 | ||
850 | if (f) | |
851 | fclose(f); | |
852 | ||
853 | free(short_description); | |
854 | free(long_description); | |
855 | free(chkconfig_description); | |
856 | ||
857 | return r; | |
858 | } | |
859 | ||
860 | static int service_load_sysv_name(Service *s, const char *name) { | |
861 | char **p; | |
862 | ||
863 | assert(s); | |
864 | assert(name); | |
865 | ||
866 | /* For SysV services we strip the boot.*, rc.* and *.sh | |
867 | * prefixes/suffixes. */ | |
868 | #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) | |
869 | if (endswith(name, ".sh.service")) | |
870 | return -ENOENT; | |
871 | #endif | |
872 | ||
873 | #ifdef TARGET_SUSE | |
874 | if (startswith(name, "boot.")) | |
875 | return -ENOENT; | |
876 | #endif | |
877 | ||
878 | #ifdef TARGET_FRUGALWARE | |
879 | if (startswith(name, "rc.")) | |
880 | return -ENOENT; | |
881 | #endif | |
882 | ||
883 | STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) { | |
884 | char *path; | |
885 | int r; | |
886 | ||
887 | if (asprintf(&path, "%s/%s", *p, name) < 0) | |
888 | return -ENOMEM; | |
889 | ||
890 | assert(endswith(path, ".service")); | |
891 | path[strlen(path)-8] = 0; | |
892 | ||
893 | r = service_load_sysv_path(s, path); | |
894 | ||
895 | #if defined(TARGET_DEBIAN) || defined(TARGET_UBUNTU) | |
896 | if (r >= 0 && s->meta.load_state == UNIT_STUB) { | |
897 | /* Try Debian style *.sh source'able init scripts */ | |
898 | strcat(path, ".sh"); | |
899 | r = service_load_sysv_path(s, path); | |
900 | } | |
901 | #endif | |
902 | free(path); | |
903 | ||
904 | #ifdef TARGET_SUSE | |
905 | if (r >= 0 && s->meta.load_state == UNIT_STUB) { | |
906 | /* Try SUSE style boot.* init scripts */ | |
907 | ||
908 | if (asprintf(&path, "%s/boot.%s", *p, name) < 0) | |
909 | return -ENOMEM; | |
910 | ||
911 | /* Drop .service suffix */ | |
912 | path[strlen(path)-8] = 0; | |
913 | r = service_load_sysv_path(s, path); | |
914 | free(path); | |
915 | } | |
916 | #endif | |
917 | ||
918 | #ifdef TARGET_FRUGALWARE | |
919 | if (r >= 0 && s->meta.load_state == UNIT_STUB) { | |
920 | /* Try Frugalware style rc.* init scripts */ | |
921 | ||
922 | if (asprintf(&path, "%s/rc.%s", *p, name) < 0) | |
923 | return -ENOMEM; | |
924 | ||
925 | /* Drop .service suffix */ | |
926 | path[strlen(path)-8] = 0; | |
927 | r = service_load_sysv_path(s, path); | |
928 | free(path); | |
929 | } | |
930 | #endif | |
931 | ||
932 | if (r < 0) | |
933 | return r; | |
934 | ||
935 | if ((s->meta.load_state != UNIT_STUB)) | |
936 | break; | |
937 | } | |
938 | ||
939 | return 0; | |
940 | } | |
941 | ||
942 | static int service_load_sysv(Service *s) { | |
943 | const char *t; | |
944 | Iterator i; | |
945 | int r; | |
946 | ||
947 | assert(s); | |
948 | ||
949 | /* Load service data from SysV init scripts, preferably with | |
950 | * LSB headers ... */ | |
951 | ||
952 | if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path)) | |
953 | return 0; | |
954 | ||
955 | if ((t = s->meta.id)) | |
956 | if ((r = service_load_sysv_name(s, t)) < 0) | |
957 | return r; | |
958 | ||
959 | if (s->meta.load_state == UNIT_STUB) | |
960 | SET_FOREACH(t, s->meta.names, i) { | |
961 | if (t == s->meta.id) | |
962 | continue; | |
963 | ||
964 | if ((r = service_load_sysv_name(s, t)) < 0) | |
965 | return r; | |
966 | ||
967 | if (s->meta.load_state != UNIT_STUB) | |
968 | break; | |
969 | } | |
970 | ||
971 | return 0; | |
972 | } | |
973 | #endif | |
974 | ||
975 | static int fsck_fix_order(Service *s) { | |
976 | Meta *other; | |
977 | int r; | |
978 | ||
979 | assert(s); | |
980 | ||
981 | if (s->fsck_passno <= 0) | |
982 | return 0; | |
983 | ||
984 | /* For each pair of services where both have an fsck priority | |
985 | * we order things based on it. */ | |
986 | ||
987 | LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) { | |
988 | Service *t; | |
989 | UnitDependency d; | |
990 | ||
991 | t = (Service*) other; | |
992 | ||
993 | if (s == t) | |
994 | continue; | |
995 | ||
996 | if (t->meta.load_state != UNIT_LOADED) | |
997 | continue; | |
998 | ||
999 | if (t->fsck_passno <= 0) | |
1000 | continue; | |
1001 | ||
1002 | if (t->fsck_passno < s->fsck_passno) | |
1003 | d = UNIT_AFTER; | |
1004 | else if (t->fsck_passno > s->fsck_passno) | |
1005 | d = UNIT_BEFORE; | |
1006 | else | |
1007 | continue; | |
1008 | ||
1009 | if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0) | |
1010 | return r; | |
1011 | } | |
1012 | ||
1013 | return 0; | |
1014 | } | |
1015 | ||
1016 | static int service_verify(Service *s) { | |
1017 | assert(s); | |
1018 | ||
1019 | if (s->meta.load_state != UNIT_LOADED) | |
1020 | return 0; | |
1021 | ||
1022 | if (!s->exec_command[SERVICE_EXEC_START]) { | |
1023 | log_error("%s lacks ExecStart setting. Refusing.", s->meta.id); | |
1024 | return -EINVAL; | |
1025 | } | |
1026 | ||
1027 | if (s->type != SERVICE_ONESHOT && | |
1028 | s->exec_command[SERVICE_EXEC_START]->command_next) { | |
1029 | log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id); | |
1030 | return -EINVAL; | |
1031 | } | |
1032 | ||
1033 | if (s->type == SERVICE_ONESHOT && | |
1034 | s->exec_command[SERVICE_EXEC_RELOAD]) { | |
1035 | log_error("%s has an ExecReload setting, which is not allowed for Type=oneshot services. Refusing.", s->meta.id); | |
1036 | return -EINVAL; | |
1037 | } | |
1038 | ||
1039 | if (s->type == SERVICE_DBUS && !s->bus_name) { | |
1040 | log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id); | |
1041 | return -EINVAL; | |
1042 | } | |
1043 | ||
1044 | if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) { | |
1045 | log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id); | |
1046 | return -EINVAL; | |
1047 | } | |
1048 | ||
1049 | return 0; | |
1050 | } | |
1051 | ||
1052 | static int service_add_default_dependencies(Service *s) { | |
1053 | int r; | |
1054 | ||
1055 | assert(s); | |
1056 | ||
1057 | /* Add a number of automatic dependencies useful for the | |
1058 | * majority of services. */ | |
1059 | ||
1060 | /* First, pull in base system */ | |
1061 | if (s->meta.manager->running_as == MANAGER_SYSTEM) { | |
1062 | ||
1063 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0) | |
1064 | return r; | |
1065 | ||
1066 | } else if (s->meta.manager->running_as == MANAGER_USER) { | |
1067 | ||
1068 | if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0) | |
1069 | return r; | |
1070 | } | |
1071 | ||
1072 | /* Second, activate normal shutdown */ | |
1073 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); | |
1074 | } | |
1075 | ||
1076 | static int service_load(Unit *u) { | |
1077 | int r; | |
1078 | Service *s = SERVICE(u); | |
1079 | ||
1080 | assert(s); | |
1081 | ||
1082 | /* Load a .service file */ | |
1083 | if ((r = unit_load_fragment(u)) < 0) | |
1084 | return r; | |
1085 | ||
1086 | #ifdef HAVE_SYSV_COMPAT | |
1087 | /* Load a classic init script as a fallback, if we couldn't find anything */ | |
1088 | if (u->meta.load_state == UNIT_STUB) | |
1089 | if ((r = service_load_sysv(s)) < 0) | |
1090 | return r; | |
1091 | #endif | |
1092 | ||
1093 | /* Still nothing found? Then let's give up */ | |
1094 | if (u->meta.load_state == UNIT_STUB) | |
1095 | return -ENOENT; | |
1096 | ||
1097 | /* We were able to load something, then let's add in the | |
1098 | * dropin directories. */ | |
1099 | if ((r = unit_load_dropin(unit_follow_merge(u))) < 0) | |
1100 | return r; | |
1101 | ||
1102 | /* This is a new unit? Then let's add in some extras */ | |
1103 | if (u->meta.load_state == UNIT_LOADED) { | |
1104 | if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) | |
1105 | return r; | |
1106 | ||
1107 | if ((r = unit_add_default_cgroups(u)) < 0) | |
1108 | return r; | |
1109 | ||
1110 | #ifdef HAVE_SYSV_COMPAT | |
1111 | if ((r = sysv_fix_order(s)) < 0) | |
1112 | return r; | |
1113 | #endif | |
1114 | ||
1115 | if ((r = fsck_fix_order(s)) < 0) | |
1116 | return r; | |
1117 | ||
1118 | if (s->bus_name) | |
1119 | if ((r = unit_watch_bus_name(u, s->bus_name)) < 0) | |
1120 | return r; | |
1121 | ||
1122 | if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE) | |
1123 | s->notify_access = NOTIFY_MAIN; | |
1124 | ||
1125 | if (s->type == SERVICE_DBUS || s->bus_name) | |
1126 | if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0) | |
1127 | return r; | |
1128 | ||
1129 | if (s->meta.default_dependencies) | |
1130 | if ((r = service_add_default_dependencies(s)) < 0) | |
1131 | return r; | |
1132 | } | |
1133 | ||
1134 | return service_verify(s); | |
1135 | } | |
1136 | ||
1137 | static void service_dump(Unit *u, FILE *f, const char *prefix) { | |
1138 | ||
1139 | ServiceExecCommand c; | |
1140 | Service *s = SERVICE(u); | |
1141 | const char *prefix2; | |
1142 | char *p2; | |
1143 | ||
1144 | assert(s); | |
1145 | ||
1146 | p2 = strappend(prefix, "\t"); | |
1147 | prefix2 = p2 ? p2 : prefix; | |
1148 | ||
1149 | fprintf(f, | |
1150 | "%sService State: %s\n" | |
1151 | "%sPermissionsStartOnly: %s\n" | |
1152 | "%sRootDirectoryStartOnly: %s\n" | |
1153 | "%sRemainAfterExit: %s\n" | |
1154 | "%sType: %s\n" | |
1155 | "%sRestart: %s\n" | |
1156 | "%sNotifyAccess: %s\n", | |
1157 | prefix, service_state_to_string(s->state), | |
1158 | prefix, yes_no(s->permissions_start_only), | |
1159 | prefix, yes_no(s->root_directory_start_only), | |
1160 | prefix, yes_no(s->remain_after_exit), | |
1161 | prefix, service_type_to_string(s->type), | |
1162 | prefix, service_restart_to_string(s->restart), | |
1163 | prefix, notify_access_to_string(s->notify_access)); | |
1164 | ||
1165 | if (s->control_pid > 0) | |
1166 | fprintf(f, | |
1167 | "%sControl PID: %lu\n", | |
1168 | prefix, (unsigned long) s->control_pid); | |
1169 | ||
1170 | if (s->main_pid > 0) | |
1171 | fprintf(f, | |
1172 | "%sMain PID: %lu\n", | |
1173 | prefix, (unsigned long) s->main_pid); | |
1174 | ||
1175 | if (s->pid_file) | |
1176 | fprintf(f, | |
1177 | "%sPIDFile: %s\n", | |
1178 | prefix, s->pid_file); | |
1179 | ||
1180 | if (s->bus_name) | |
1181 | fprintf(f, | |
1182 | "%sBusName: %s\n" | |
1183 | "%sBus Name Good: %s\n", | |
1184 | prefix, s->bus_name, | |
1185 | prefix, yes_no(s->bus_name_good)); | |
1186 | ||
1187 | exec_context_dump(&s->exec_context, f, prefix); | |
1188 | ||
1189 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { | |
1190 | ||
1191 | if (!s->exec_command[c]) | |
1192 | continue; | |
1193 | ||
1194 | fprintf(f, "%s-> %s:\n", | |
1195 | prefix, service_exec_command_to_string(c)); | |
1196 | ||
1197 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
1198 | } | |
1199 | ||
1200 | #ifdef HAVE_SYSV_COMPAT | |
1201 | if (s->sysv_path) | |
1202 | fprintf(f, | |
1203 | "%sSysV Init Script Path: %s\n" | |
1204 | "%sSysV Init Script has LSB Header: %s\n" | |
1205 | "%sSysVEnabled: %s\n", | |
1206 | prefix, s->sysv_path, | |
1207 | prefix, yes_no(s->sysv_has_lsb), | |
1208 | prefix, yes_no(s->sysv_enabled)); | |
1209 | ||
1210 | if (s->sysv_start_priority >= 0) | |
1211 | fprintf(f, | |
1212 | "%sSysVStartPriority: %i\n", | |
1213 | prefix, s->sysv_start_priority); | |
1214 | ||
1215 | if (s->sysv_runlevels) | |
1216 | fprintf(f, "%sSysVRunLevels: %s\n", | |
1217 | prefix, s->sysv_runlevels); | |
1218 | #endif | |
1219 | ||
1220 | if (s->fsck_passno > 0) | |
1221 | fprintf(f, | |
1222 | "%sFsckPassNo: %i\n", | |
1223 | prefix, s->fsck_passno); | |
1224 | ||
1225 | if (s->status_text) | |
1226 | fprintf(f, "%sStatus Text: %s\n", | |
1227 | prefix, s->status_text); | |
1228 | ||
1229 | free(p2); | |
1230 | } | |
1231 | ||
1232 | static int service_load_pid_file(Service *s) { | |
1233 | char *k; | |
1234 | int r; | |
1235 | pid_t pid; | |
1236 | ||
1237 | assert(s); | |
1238 | ||
1239 | if (s->main_pid_known) | |
1240 | return 0; | |
1241 | ||
1242 | assert(s->main_pid <= 0); | |
1243 | ||
1244 | if (!s->pid_file) | |
1245 | return -ENOENT; | |
1246 | ||
1247 | if ((r = read_one_line_file(s->pid_file, &k)) < 0) | |
1248 | return r; | |
1249 | ||
1250 | r = parse_pid(k, &pid); | |
1251 | free(k); | |
1252 | ||
1253 | if (r < 0) | |
1254 | return r; | |
1255 | ||
1256 | if (kill(pid, 0) < 0 && errno != EPERM) { | |
1257 | log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.", | |
1258 | (unsigned long) pid, s->pid_file); | |
1259 | return -ESRCH; | |
1260 | } | |
1261 | ||
1262 | if ((r = service_set_main_pid(s, pid)) < 0) | |
1263 | return r; | |
1264 | ||
1265 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) | |
1266 | /* FIXME: we need to do something here */ | |
1267 | return r; | |
1268 | ||
1269 | return 0; | |
1270 | } | |
1271 | ||
1272 | static int service_search_main_pid(Service *s) { | |
1273 | pid_t pid; | |
1274 | int r; | |
1275 | ||
1276 | assert(s); | |
1277 | ||
1278 | if (s->main_pid_known) | |
1279 | return 0; | |
1280 | ||
1281 | assert(s->main_pid <= 0); | |
1282 | ||
1283 | if ((pid = cgroup_bonding_search_main_pid_list(s->meta.cgroup_bondings)) <= 0) | |
1284 | return -ENOENT; | |
1285 | ||
1286 | if ((r = service_set_main_pid(s, pid)) < 0) | |
1287 | return r; | |
1288 | ||
1289 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) | |
1290 | /* FIXME: we need to do something here */ | |
1291 | return r; | |
1292 | ||
1293 | return 0; | |
1294 | } | |
1295 | ||
1296 | static int service_get_sockets(Service *s, Set **_set) { | |
1297 | Set *set; | |
1298 | Iterator i; | |
1299 | char *t; | |
1300 | int r; | |
1301 | ||
1302 | assert(s); | |
1303 | assert(_set); | |
1304 | ||
1305 | if (s->socket_fd >= 0) | |
1306 | return 0; | |
1307 | ||
1308 | if (!set_isempty(s->configured_sockets)) | |
1309 | return 0; | |
1310 | ||
1311 | /* Collects all Socket objects that belong to this | |
1312 | * service. Note that a service might have multiple sockets | |
1313 | * via multiple names. */ | |
1314 | ||
1315 | if (!(set = set_new(NULL, NULL))) | |
1316 | return -ENOMEM; | |
1317 | ||
1318 | SET_FOREACH(t, s->meta.names, i) { | |
1319 | char *k; | |
1320 | Unit *p; | |
1321 | ||
1322 | /* Look for all socket objects that go by any of our | |
1323 | * units and collect their fds */ | |
1324 | ||
1325 | if (!(k = unit_name_change_suffix(t, ".socket"))) { | |
1326 | r = -ENOMEM; | |
1327 | goto fail; | |
1328 | } | |
1329 | ||
1330 | p = manager_get_unit(s->meta.manager, k); | |
1331 | free(k); | |
1332 | ||
1333 | if (!p) | |
1334 | continue; | |
1335 | ||
1336 | if ((r = set_put(set, p)) < 0) | |
1337 | goto fail; | |
1338 | } | |
1339 | ||
1340 | *_set = set; | |
1341 | return 0; | |
1342 | ||
1343 | fail: | |
1344 | set_free(set); | |
1345 | return r; | |
1346 | } | |
1347 | ||
1348 | static int service_notify_sockets_dead(Service *s) { | |
1349 | Iterator i; | |
1350 | Set *set, *free_set = NULL; | |
1351 | Socket *sock; | |
1352 | int r; | |
1353 | ||
1354 | assert(s); | |
1355 | ||
1356 | /* Notifies all our sockets when we die */ | |
1357 | ||
1358 | if (s->socket_fd >= 0) | |
1359 | return 0; | |
1360 | ||
1361 | if (!set_isempty(s->configured_sockets)) | |
1362 | set = s->configured_sockets; | |
1363 | else { | |
1364 | if ((r = service_get_sockets(s, &free_set)) < 0) | |
1365 | return r; | |
1366 | ||
1367 | set = free_set; | |
1368 | } | |
1369 | ||
1370 | SET_FOREACH(sock, set, i) | |
1371 | socket_notify_service_dead(sock); | |
1372 | ||
1373 | set_free(free_set); | |
1374 | ||
1375 | return 0; | |
1376 | } | |
1377 | ||
1378 | static void service_set_state(Service *s, ServiceState state) { | |
1379 | ServiceState old_state; | |
1380 | assert(s); | |
1381 | ||
1382 | old_state = s->state; | |
1383 | s->state = state; | |
1384 | ||
1385 | if (state != SERVICE_START_PRE && | |
1386 | state != SERVICE_START && | |
1387 | state != SERVICE_START_POST && | |
1388 | state != SERVICE_RELOAD && | |
1389 | state != SERVICE_STOP && | |
1390 | state != SERVICE_STOP_SIGTERM && | |
1391 | state != SERVICE_STOP_SIGKILL && | |
1392 | state != SERVICE_STOP_POST && | |
1393 | state != SERVICE_FINAL_SIGTERM && | |
1394 | state != SERVICE_FINAL_SIGKILL && | |
1395 | state != SERVICE_AUTO_RESTART) | |
1396 | unit_unwatch_timer(UNIT(s), &s->timer_watch); | |
1397 | ||
1398 | if (state != SERVICE_START && | |
1399 | state != SERVICE_START_POST && | |
1400 | state != SERVICE_RUNNING && | |
1401 | state != SERVICE_RELOAD && | |
1402 | state != SERVICE_STOP && | |
1403 | state != SERVICE_STOP_SIGTERM && | |
1404 | state != SERVICE_STOP_SIGKILL) { | |
1405 | service_unwatch_main_pid(s); | |
1406 | s->main_command = NULL; | |
1407 | } | |
1408 | ||
1409 | if (state != SERVICE_START_PRE && | |
1410 | state != SERVICE_START && | |
1411 | state != SERVICE_START_POST && | |
1412 | state != SERVICE_RELOAD && | |
1413 | state != SERVICE_STOP && | |
1414 | state != SERVICE_STOP_SIGTERM && | |
1415 | state != SERVICE_STOP_SIGKILL && | |
1416 | state != SERVICE_STOP_POST && | |
1417 | state != SERVICE_FINAL_SIGTERM && | |
1418 | state != SERVICE_FINAL_SIGKILL) { | |
1419 | service_unwatch_control_pid(s); | |
1420 | s->control_command = NULL; | |
1421 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
1422 | } | |
1423 | ||
1424 | if (state == SERVICE_DEAD || | |
1425 | state == SERVICE_STOP || | |
1426 | state == SERVICE_STOP_SIGTERM || | |
1427 | state == SERVICE_STOP_SIGKILL || | |
1428 | state == SERVICE_STOP_POST || | |
1429 | state == SERVICE_FINAL_SIGTERM || | |
1430 | state == SERVICE_FINAL_SIGKILL || | |
1431 | state == SERVICE_FAILED || | |
1432 | state == SERVICE_AUTO_RESTART) | |
1433 | service_notify_sockets_dead(s); | |
1434 | ||
1435 | if (state != SERVICE_START_PRE && | |
1436 | state != SERVICE_START && | |
1437 | state != SERVICE_START_POST && | |
1438 | state != SERVICE_RUNNING && | |
1439 | state != SERVICE_RELOAD && | |
1440 | state != SERVICE_STOP && | |
1441 | state != SERVICE_STOP_SIGTERM && | |
1442 | state != SERVICE_STOP_SIGKILL && | |
1443 | state != SERVICE_STOP_POST && | |
1444 | state != SERVICE_FINAL_SIGTERM && | |
1445 | state != SERVICE_FINAL_SIGKILL && | |
1446 | !(state == SERVICE_DEAD && s->meta.job)) { | |
1447 | service_close_socket_fd(s); | |
1448 | service_connection_unref(s); | |
1449 | } | |
1450 | ||
1451 | /* For the inactive states unit_notify() will trim the cgroup, | |
1452 | * but for exit we have to do that ourselves... */ | |
1453 | if (state == SERVICE_EXITED) | |
1454 | cgroup_bonding_trim_list(s->meta.cgroup_bondings, true); | |
1455 | ||
1456 | if (old_state != state) | |
1457 | log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state)); | |
1458 | ||
1459 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state], !s->reload_failure); | |
1460 | s->reload_failure = false; | |
1461 | } | |
1462 | ||
1463 | static int service_coldplug(Unit *u) { | |
1464 | Service *s = SERVICE(u); | |
1465 | int r; | |
1466 | ||
1467 | assert(s); | |
1468 | assert(s->state == SERVICE_DEAD); | |
1469 | ||
1470 | if (s->deserialized_state != s->state) { | |
1471 | ||
1472 | if (s->deserialized_state == SERVICE_START_PRE || | |
1473 | s->deserialized_state == SERVICE_START || | |
1474 | s->deserialized_state == SERVICE_START_POST || | |
1475 | s->deserialized_state == SERVICE_RELOAD || | |
1476 | s->deserialized_state == SERVICE_STOP || | |
1477 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1478 | s->deserialized_state == SERVICE_STOP_SIGKILL || | |
1479 | s->deserialized_state == SERVICE_STOP_POST || | |
1480 | s->deserialized_state == SERVICE_FINAL_SIGTERM || | |
1481 | s->deserialized_state == SERVICE_FINAL_SIGKILL || | |
1482 | s->deserialized_state == SERVICE_AUTO_RESTART) { | |
1483 | ||
1484 | if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) { | |
1485 | usec_t k; | |
1486 | ||
1487 | k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec; | |
1488 | ||
1489 | if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0) | |
1490 | return r; | |
1491 | } | |
1492 | } | |
1493 | ||
1494 | if ((s->deserialized_state == SERVICE_START && | |
1495 | (s->type == SERVICE_FORKING || | |
1496 | s->type == SERVICE_DBUS || | |
1497 | s->type == SERVICE_ONESHOT || | |
1498 | s->type == SERVICE_NOTIFY)) || | |
1499 | s->deserialized_state == SERVICE_START_POST || | |
1500 | s->deserialized_state == SERVICE_RUNNING || | |
1501 | s->deserialized_state == SERVICE_RELOAD || | |
1502 | s->deserialized_state == SERVICE_STOP || | |
1503 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1504 | s->deserialized_state == SERVICE_STOP_SIGKILL) | |
1505 | if (s->main_pid > 0) | |
1506 | if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0) | |
1507 | return r; | |
1508 | ||
1509 | if (s->deserialized_state == SERVICE_START_PRE || | |
1510 | s->deserialized_state == SERVICE_START || | |
1511 | s->deserialized_state == SERVICE_START_POST || | |
1512 | s->deserialized_state == SERVICE_RELOAD || | |
1513 | s->deserialized_state == SERVICE_STOP || | |
1514 | s->deserialized_state == SERVICE_STOP_SIGTERM || | |
1515 | s->deserialized_state == SERVICE_STOP_SIGKILL || | |
1516 | s->deserialized_state == SERVICE_STOP_POST || | |
1517 | s->deserialized_state == SERVICE_FINAL_SIGTERM || | |
1518 | s->deserialized_state == SERVICE_FINAL_SIGKILL) | |
1519 | if (s->control_pid > 0) | |
1520 | if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0) | |
1521 | return r; | |
1522 | ||
1523 | service_set_state(s, s->deserialized_state); | |
1524 | } | |
1525 | ||
1526 | return 0; | |
1527 | } | |
1528 | ||
1529 | static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { | |
1530 | Iterator i; | |
1531 | int r; | |
1532 | int *rfds = NULL; | |
1533 | unsigned rn_fds = 0; | |
1534 | Set *set, *free_set = NULL; | |
1535 | Socket *sock; | |
1536 | ||
1537 | assert(s); | |
1538 | assert(fds); | |
1539 | assert(n_fds); | |
1540 | ||
1541 | if (s->socket_fd >= 0) | |
1542 | return 0; | |
1543 | ||
1544 | if (!set_isempty(s->configured_sockets)) | |
1545 | set = s->configured_sockets; | |
1546 | else { | |
1547 | if ((r = service_get_sockets(s, &free_set)) < 0) | |
1548 | return r; | |
1549 | ||
1550 | set = free_set; | |
1551 | } | |
1552 | ||
1553 | SET_FOREACH(sock, set, i) { | |
1554 | int *cfds; | |
1555 | unsigned cn_fds; | |
1556 | ||
1557 | if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0) | |
1558 | goto fail; | |
1559 | ||
1560 | if (!cfds) | |
1561 | continue; | |
1562 | ||
1563 | if (!rfds) { | |
1564 | rfds = cfds; | |
1565 | rn_fds = cn_fds; | |
1566 | } else { | |
1567 | int *t; | |
1568 | ||
1569 | if (!(t = new(int, rn_fds+cn_fds))) { | |
1570 | free(cfds); | |
1571 | r = -ENOMEM; | |
1572 | goto fail; | |
1573 | } | |
1574 | ||
1575 | memcpy(t, rfds, rn_fds); | |
1576 | memcpy(t+rn_fds, cfds, cn_fds); | |
1577 | free(rfds); | |
1578 | free(cfds); | |
1579 | ||
1580 | rfds = t; | |
1581 | rn_fds = rn_fds+cn_fds; | |
1582 | } | |
1583 | } | |
1584 | ||
1585 | *fds = rfds; | |
1586 | *n_fds = rn_fds; | |
1587 | ||
1588 | set_free(free_set); | |
1589 | ||
1590 | return 0; | |
1591 | ||
1592 | fail: | |
1593 | set_free(set); | |
1594 | free(rfds); | |
1595 | ||
1596 | return r; | |
1597 | } | |
1598 | ||
1599 | static int service_spawn( | |
1600 | Service *s, | |
1601 | ExecCommand *c, | |
1602 | bool timeout, | |
1603 | bool pass_fds, | |
1604 | bool apply_permissions, | |
1605 | bool apply_chroot, | |
1606 | bool apply_tty_stdin, | |
1607 | bool set_notify_socket, | |
1608 | pid_t *_pid) { | |
1609 | ||
1610 | pid_t pid; | |
1611 | int r; | |
1612 | int *fds = NULL, *fdsbuf = NULL; | |
1613 | unsigned n_fds = 0, n_env = 0; | |
1614 | char **argv = NULL, **final_env = NULL, **our_env = NULL; | |
1615 | ||
1616 | assert(s); | |
1617 | assert(c); | |
1618 | assert(_pid); | |
1619 | ||
1620 | if (pass_fds || | |
1621 | s->exec_context.std_input == EXEC_INPUT_SOCKET || | |
1622 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1623 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1624 | ||
1625 | if (s->socket_fd >= 0) { | |
1626 | fds = &s->socket_fd; | |
1627 | n_fds = 1; | |
1628 | } else { | |
1629 | if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0) | |
1630 | goto fail; | |
1631 | ||
1632 | fds = fdsbuf; | |
1633 | } | |
1634 | } | |
1635 | ||
1636 | if (timeout && s->timeout_usec) { | |
1637 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
1638 | goto fail; | |
1639 | } else | |
1640 | unit_unwatch_timer(UNIT(s), &s->timer_watch); | |
1641 | ||
1642 | if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) { | |
1643 | r = -ENOMEM; | |
1644 | goto fail; | |
1645 | } | |
1646 | ||
1647 | if (!(our_env = new0(char*, 4))) { | |
1648 | r = -ENOMEM; | |
1649 | goto fail; | |
1650 | } | |
1651 | ||
1652 | if (set_notify_socket) | |
1653 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) { | |
1654 | r = -ENOMEM; | |
1655 | goto fail; | |
1656 | } | |
1657 | ||
1658 | if (s->main_pid > 0) | |
1659 | if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) { | |
1660 | r = -ENOMEM; | |
1661 | goto fail; | |
1662 | } | |
1663 | ||
1664 | if (!(final_env = strv_env_merge(2, | |
1665 | s->meta.manager->environment, | |
1666 | our_env, | |
1667 | NULL))) { | |
1668 | r = -ENOMEM; | |
1669 | goto fail; | |
1670 | } | |
1671 | ||
1672 | r = exec_spawn(c, | |
1673 | argv, | |
1674 | &s->exec_context, | |
1675 | fds, n_fds, | |
1676 | final_env, | |
1677 | apply_permissions, | |
1678 | apply_chroot, | |
1679 | apply_tty_stdin, | |
1680 | s->meta.manager->confirm_spawn, | |
1681 | s->meta.cgroup_bondings, | |
1682 | &pid); | |
1683 | ||
1684 | if (r < 0) | |
1685 | goto fail; | |
1686 | ||
1687 | ||
1688 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) | |
1689 | /* FIXME: we need to do something here */ | |
1690 | goto fail; | |
1691 | ||
1692 | free(fdsbuf); | |
1693 | strv_free(argv); | |
1694 | strv_free(our_env); | |
1695 | strv_free(final_env); | |
1696 | ||
1697 | *_pid = pid; | |
1698 | ||
1699 | return 0; | |
1700 | ||
1701 | fail: | |
1702 | free(fdsbuf); | |
1703 | strv_free(argv); | |
1704 | strv_free(our_env); | |
1705 | strv_free(final_env); | |
1706 | ||
1707 | if (timeout) | |
1708 | unit_unwatch_timer(UNIT(s), &s->timer_watch); | |
1709 | ||
1710 | return r; | |
1711 | } | |
1712 | ||
1713 | static int main_pid_good(Service *s) { | |
1714 | assert(s); | |
1715 | ||
1716 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
1717 | * don't know */ | |
1718 | ||
1719 | /* If we know the pid file, then lets just check if it is | |
1720 | * still valid */ | |
1721 | if (s->main_pid_known) | |
1722 | return s->main_pid > 0; | |
1723 | ||
1724 | /* We don't know the pid */ | |
1725 | return -EAGAIN; | |
1726 | } | |
1727 | ||
1728 | static int control_pid_good(Service *s) { | |
1729 | assert(s); | |
1730 | ||
1731 | return s->control_pid > 0; | |
1732 | } | |
1733 | ||
1734 | static int cgroup_good(Service *s) { | |
1735 | int r; | |
1736 | ||
1737 | assert(s); | |
1738 | ||
1739 | if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0) | |
1740 | return r; | |
1741 | ||
1742 | return !r; | |
1743 | } | |
1744 | ||
1745 | static void service_enter_dead(Service *s, bool success, bool allow_restart) { | |
1746 | int r; | |
1747 | assert(s); | |
1748 | ||
1749 | if (!success) | |
1750 | s->failure = true; | |
1751 | ||
1752 | if (allow_restart && | |
1753 | !s->forbid_restart && | |
1754 | (s->restart == SERVICE_RESTART_ALWAYS || | |
1755 | (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure) || | |
1756 | (s->restart == SERVICE_RESTART_ON_FAILURE && s->failure) || | |
1757 | (s->restart == SERVICE_RESTART_ON_ABORT && s->failure && | |
1758 | (s->main_exec_status.code == CLD_KILLED || | |
1759 | s->main_exec_status.code == CLD_DUMPED)))) { | |
1760 | ||
1761 | if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0) | |
1762 | goto fail; | |
1763 | ||
1764 | service_set_state(s, SERVICE_AUTO_RESTART); | |
1765 | } else | |
1766 | service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD); | |
1767 | ||
1768 | s->forbid_restart = false; | |
1769 | ||
1770 | return; | |
1771 | ||
1772 | fail: | |
1773 | log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r)); | |
1774 | service_enter_dead(s, false, false); | |
1775 | } | |
1776 | ||
1777 | static void service_enter_signal(Service *s, ServiceState state, bool success); | |
1778 | ||
1779 | static void service_enter_stop_post(Service *s, bool success) { | |
1780 | int r; | |
1781 | assert(s); | |
1782 | ||
1783 | if (!success) | |
1784 | s->failure = true; | |
1785 | ||
1786 | service_unwatch_control_pid(s); | |
1787 | ||
1788 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) { | |
1789 | s->control_command_id = SERVICE_EXEC_STOP_POST; | |
1790 | ||
1791 | if ((r = service_spawn(s, | |
1792 | s->control_command, | |
1793 | true, | |
1794 | false, | |
1795 | !s->permissions_start_only, | |
1796 | !s->root_directory_start_only, | |
1797 | true, | |
1798 | false, | |
1799 | &s->control_pid)) < 0) | |
1800 | goto fail; | |
1801 | ||
1802 | ||
1803 | service_set_state(s, SERVICE_STOP_POST); | |
1804 | } else | |
1805 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, true); | |
1806 | ||
1807 | return; | |
1808 | ||
1809 | fail: | |
1810 | log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r)); | |
1811 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
1812 | } | |
1813 | ||
1814 | static void service_enter_signal(Service *s, ServiceState state, bool success) { | |
1815 | int r; | |
1816 | Set *pid_set = NULL; | |
1817 | bool wait_for_exit = false; | |
1818 | ||
1819 | assert(s); | |
1820 | ||
1821 | if (!success) | |
1822 | s->failure = true; | |
1823 | ||
1824 | if (s->exec_context.kill_mode != KILL_NONE) { | |
1825 | int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL; | |
1826 | ||
1827 | if (s->main_pid > 0) { | |
1828 | if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ? | |
1829 | -s->main_pid : | |
1830 | s->main_pid, sig) < 0 && errno != ESRCH) | |
1831 | ||
1832 | log_warning("Failed to kill main process %li: %m", (long) s->main_pid); | |
1833 | else | |
1834 | wait_for_exit = true; | |
1835 | } | |
1836 | ||
1837 | if (s->control_pid > 0) { | |
1838 | if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ? | |
1839 | -s->control_pid : | |
1840 | s->control_pid, sig) < 0 && errno != ESRCH) | |
1841 | ||
1842 | log_warning("Failed to kill control process %li: %m", (long) s->control_pid); | |
1843 | else | |
1844 | wait_for_exit = true; | |
1845 | } | |
1846 | ||
1847 | if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) { | |
1848 | ||
1849 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) { | |
1850 | r = -ENOMEM; | |
1851 | goto fail; | |
1852 | } | |
1853 | ||
1854 | /* Exclude the main/control pids from being killed via the cgroup */ | |
1855 | if (s->main_pid > 0) | |
1856 | if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) | |
1857 | goto fail; | |
1858 | ||
1859 | if (s->control_pid > 0) | |
1860 | if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) | |
1861 | goto fail; | |
1862 | ||
1863 | if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) { | |
1864 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
1865 | log_warning("Failed to kill control group: %s", strerror(-r)); | |
1866 | } else if (r > 0) | |
1867 | wait_for_exit = true; | |
1868 | ||
1869 | set_free(pid_set); | |
1870 | } | |
1871 | } | |
1872 | ||
1873 | if (wait_for_exit) { | |
1874 | if (s->timeout_usec > 0) | |
1875 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) | |
1876 | goto fail; | |
1877 | ||
1878 | service_set_state(s, state); | |
1879 | } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) | |
1880 | service_enter_stop_post(s, true); | |
1881 | else | |
1882 | service_enter_dead(s, true, true); | |
1883 | ||
1884 | return; | |
1885 | ||
1886 | fail: | |
1887 | log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r)); | |
1888 | ||
1889 | if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) | |
1890 | service_enter_stop_post(s, false); | |
1891 | else | |
1892 | service_enter_dead(s, false, true); | |
1893 | ||
1894 | if (pid_set) | |
1895 | set_free(pid_set); | |
1896 | } | |
1897 | ||
1898 | static void service_enter_stop(Service *s, bool success) { | |
1899 | int r; | |
1900 | ||
1901 | assert(s); | |
1902 | ||
1903 | if (!success) | |
1904 | s->failure = true; | |
1905 | ||
1906 | service_unwatch_control_pid(s); | |
1907 | ||
1908 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) { | |
1909 | s->control_command_id = SERVICE_EXEC_STOP; | |
1910 | ||
1911 | if ((r = service_spawn(s, | |
1912 | s->control_command, | |
1913 | true, | |
1914 | false, | |
1915 | !s->permissions_start_only, | |
1916 | !s->root_directory_start_only, | |
1917 | false, | |
1918 | false, | |
1919 | &s->control_pid)) < 0) | |
1920 | goto fail; | |
1921 | ||
1922 | service_set_state(s, SERVICE_STOP); | |
1923 | } else | |
1924 | service_enter_signal(s, SERVICE_STOP_SIGTERM, true); | |
1925 | ||
1926 | return; | |
1927 | ||
1928 | fail: | |
1929 | log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r)); | |
1930 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); | |
1931 | } | |
1932 | ||
1933 | static void service_enter_running(Service *s, bool success) { | |
1934 | int main_pid_ok, cgroup_ok; | |
1935 | assert(s); | |
1936 | ||
1937 | if (!success) | |
1938 | s->failure = true; | |
1939 | ||
1940 | main_pid_ok = main_pid_good(s); | |
1941 | cgroup_ok = cgroup_good(s); | |
1942 | ||
1943 | if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) && | |
1944 | (s->bus_name_good || s->type != SERVICE_DBUS)) | |
1945 | service_set_state(s, SERVICE_RUNNING); | |
1946 | else if (s->remain_after_exit) | |
1947 | service_set_state(s, SERVICE_EXITED); | |
1948 | else | |
1949 | service_enter_stop(s, true); | |
1950 | } | |
1951 | ||
1952 | static void service_enter_start_post(Service *s) { | |
1953 | int r; | |
1954 | assert(s); | |
1955 | ||
1956 | service_unwatch_control_pid(s); | |
1957 | ||
1958 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) { | |
1959 | s->control_command_id = SERVICE_EXEC_START_POST; | |
1960 | ||
1961 | if ((r = service_spawn(s, | |
1962 | s->control_command, | |
1963 | true, | |
1964 | false, | |
1965 | !s->permissions_start_only, | |
1966 | !s->root_directory_start_only, | |
1967 | false, | |
1968 | false, | |
1969 | &s->control_pid)) < 0) | |
1970 | goto fail; | |
1971 | ||
1972 | service_set_state(s, SERVICE_START_POST); | |
1973 | } else | |
1974 | service_enter_running(s, true); | |
1975 | ||
1976 | return; | |
1977 | ||
1978 | fail: | |
1979 | log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r)); | |
1980 | service_enter_stop(s, false); | |
1981 | } | |
1982 | ||
1983 | static void service_enter_start(Service *s) { | |
1984 | pid_t pid; | |
1985 | int r; | |
1986 | ExecCommand *c; | |
1987 | ||
1988 | assert(s); | |
1989 | ||
1990 | assert(s->exec_command[SERVICE_EXEC_START]); | |
1991 | assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT); | |
1992 | ||
1993 | if (s->type == SERVICE_FORKING) | |
1994 | service_unwatch_control_pid(s); | |
1995 | else | |
1996 | service_unwatch_main_pid(s); | |
1997 | ||
1998 | if (s->type == SERVICE_FORKING) { | |
1999 | s->control_command_id = SERVICE_EXEC_START; | |
2000 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
2001 | ||
2002 | s->main_command = NULL; | |
2003 | } else { | |
2004 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2005 | s->control_command = NULL; | |
2006 | ||
2007 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
2008 | } | |
2009 | ||
2010 | if ((r = service_spawn(s, | |
2011 | c, | |
2012 | s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY, | |
2013 | true, | |
2014 | true, | |
2015 | true, | |
2016 | true, | |
2017 | s->notify_access != NOTIFY_NONE, | |
2018 | &pid)) < 0) | |
2019 | goto fail; | |
2020 | ||
2021 | if (s->type == SERVICE_SIMPLE) { | |
2022 | /* For simple services we immediately start | |
2023 | * the START_POST binaries. */ | |
2024 | ||
2025 | service_set_main_pid(s, pid); | |
2026 | service_enter_start_post(s); | |
2027 | ||
2028 | } else if (s->type == SERVICE_FORKING) { | |
2029 | ||
2030 | /* For forking services we wait until the start | |
2031 | * process exited. */ | |
2032 | ||
2033 | s->control_pid = pid; | |
2034 | service_set_state(s, SERVICE_START); | |
2035 | ||
2036 | } else if (s->type == SERVICE_ONESHOT || | |
2037 | s->type == SERVICE_DBUS || | |
2038 | s->type == SERVICE_NOTIFY) { | |
2039 | ||
2040 | /* For oneshot services we wait until the start | |
2041 | * process exited, too, but it is our main process. */ | |
2042 | ||
2043 | /* For D-Bus services we know the main pid right away, | |
2044 | * but wait for the bus name to appear on the | |
2045 | * bus. Notify services are similar. */ | |
2046 | ||
2047 | service_set_main_pid(s, pid); | |
2048 | service_set_state(s, SERVICE_START); | |
2049 | } else | |
2050 | assert_not_reached("Unknown service type"); | |
2051 | ||
2052 | return; | |
2053 | ||
2054 | fail: | |
2055 | log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r)); | |
2056 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2057 | } | |
2058 | ||
2059 | static void service_enter_start_pre(Service *s) { | |
2060 | int r; | |
2061 | ||
2062 | assert(s); | |
2063 | ||
2064 | service_unwatch_control_pid(s); | |
2065 | ||
2066 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) { | |
2067 | s->control_command_id = SERVICE_EXEC_START_PRE; | |
2068 | ||
2069 | if ((r = service_spawn(s, | |
2070 | s->control_command, | |
2071 | true, | |
2072 | false, | |
2073 | !s->permissions_start_only, | |
2074 | !s->root_directory_start_only, | |
2075 | true, | |
2076 | false, | |
2077 | &s->control_pid)) < 0) | |
2078 | goto fail; | |
2079 | ||
2080 | service_set_state(s, SERVICE_START_PRE); | |
2081 | } else | |
2082 | service_enter_start(s); | |
2083 | ||
2084 | return; | |
2085 | ||
2086 | fail: | |
2087 | log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r)); | |
2088 | service_enter_dead(s, false, true); | |
2089 | } | |
2090 | ||
2091 | static void service_enter_restart(Service *s) { | |
2092 | int r; | |
2093 | DBusError error; | |
2094 | ||
2095 | assert(s); | |
2096 | dbus_error_init(&error); | |
2097 | ||
2098 | if (s->meta.job) { | |
2099 | log_info("Job pending for unit, delaying automatic restart."); | |
2100 | ||
2101 | if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0) | |
2102 | goto fail; | |
2103 | } | |
2104 | ||
2105 | service_enter_dead(s, true, false); | |
2106 | ||
2107 | if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0) | |
2108 | goto fail; | |
2109 | ||
2110 | log_debug("%s scheduled restart job.", s->meta.id); | |
2111 | return; | |
2112 | ||
2113 | fail: | |
2114 | log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r)); | |
2115 | service_enter_dead(s, false, false); | |
2116 | ||
2117 | dbus_error_free(&error); | |
2118 | } | |
2119 | ||
2120 | static void service_enter_reload(Service *s) { | |
2121 | int r; | |
2122 | ||
2123 | assert(s); | |
2124 | ||
2125 | service_unwatch_control_pid(s); | |
2126 | ||
2127 | if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) { | |
2128 | s->control_command_id = SERVICE_EXEC_RELOAD; | |
2129 | ||
2130 | if ((r = service_spawn(s, | |
2131 | s->control_command, | |
2132 | true, | |
2133 | false, | |
2134 | !s->permissions_start_only, | |
2135 | !s->root_directory_start_only, | |
2136 | false, | |
2137 | false, | |
2138 | &s->control_pid)) < 0) | |
2139 | goto fail; | |
2140 | ||
2141 | service_set_state(s, SERVICE_RELOAD); | |
2142 | } else | |
2143 | service_enter_running(s, true); | |
2144 | ||
2145 | return; | |
2146 | ||
2147 | fail: | |
2148 | log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r)); | |
2149 | s->reload_failure = true; | |
2150 | service_enter_running(s, true); | |
2151 | } | |
2152 | ||
2153 | static void service_run_next_control(Service *s, bool success) { | |
2154 | int r; | |
2155 | ||
2156 | assert(s); | |
2157 | assert(s->control_command); | |
2158 | assert(s->control_command->command_next); | |
2159 | ||
2160 | if (!success) | |
2161 | s->failure = true; | |
2162 | ||
2163 | assert(s->control_command_id != SERVICE_EXEC_START); | |
2164 | ||
2165 | s->control_command = s->control_command->command_next; | |
2166 | service_unwatch_control_pid(s); | |
2167 | ||
2168 | if ((r = service_spawn(s, | |
2169 | s->control_command, | |
2170 | true, | |
2171 | false, | |
2172 | !s->permissions_start_only, | |
2173 | !s->root_directory_start_only, | |
2174 | s->control_command_id == SERVICE_EXEC_START_PRE || | |
2175 | s->control_command_id == SERVICE_EXEC_STOP_POST, | |
2176 | false, | |
2177 | &s->control_pid)) < 0) | |
2178 | goto fail; | |
2179 | ||
2180 | return; | |
2181 | ||
2182 | fail: | |
2183 | log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r)); | |
2184 | ||
2185 | if (s->state == SERVICE_START_PRE) | |
2186 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2187 | else if (s->state == SERVICE_STOP) | |
2188 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); | |
2189 | else if (s->state == SERVICE_STOP_POST) | |
2190 | service_enter_dead(s, false, true); | |
2191 | else if (s->state == SERVICE_RELOAD) { | |
2192 | s->reload_failure = true; | |
2193 | service_enter_running(s, true); | |
2194 | } else | |
2195 | service_enter_stop(s, false); | |
2196 | } | |
2197 | ||
2198 | static void service_run_next_main(Service *s, bool success) { | |
2199 | pid_t pid; | |
2200 | int r; | |
2201 | ||
2202 | assert(s); | |
2203 | assert(s->main_command); | |
2204 | assert(s->main_command->command_next); | |
2205 | assert(s->type == SERVICE_ONESHOT); | |
2206 | ||
2207 | if (!success) | |
2208 | s->failure = true; | |
2209 | ||
2210 | s->main_command = s->main_command->command_next; | |
2211 | service_unwatch_main_pid(s); | |
2212 | ||
2213 | if ((r = service_spawn(s, | |
2214 | s->main_command, | |
2215 | false, | |
2216 | true, | |
2217 | true, | |
2218 | true, | |
2219 | true, | |
2220 | s->notify_access != NOTIFY_NONE, | |
2221 | &pid)) < 0) | |
2222 | goto fail; | |
2223 | ||
2224 | service_set_main_pid(s, pid); | |
2225 | ||
2226 | return; | |
2227 | ||
2228 | fail: | |
2229 | log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r)); | |
2230 | service_enter_stop(s, false); | |
2231 | } | |
2232 | ||
2233 | static int service_start(Unit *u) { | |
2234 | Service *s = SERVICE(u); | |
2235 | ||
2236 | assert(s); | |
2237 | ||
2238 | /* We cannot fulfill this request right now, try again later | |
2239 | * please! */ | |
2240 | if (s->state == SERVICE_STOP || | |
2241 | s->state == SERVICE_STOP_SIGTERM || | |
2242 | s->state == SERVICE_STOP_SIGKILL || | |
2243 | s->state == SERVICE_STOP_POST || | |
2244 | s->state == SERVICE_FINAL_SIGTERM || | |
2245 | s->state == SERVICE_FINAL_SIGKILL) | |
2246 | return -EAGAIN; | |
2247 | ||
2248 | /* Already on it! */ | |
2249 | if (s->state == SERVICE_START_PRE || | |
2250 | s->state == SERVICE_START || | |
2251 | s->state == SERVICE_START_POST) | |
2252 | return 0; | |
2253 | ||
2254 | assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART); | |
2255 | ||
2256 | /* Make sure we don't enter a busy loop of some kind. */ | |
2257 | if (!ratelimit_test(&s->ratelimit)) { | |
2258 | log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id); | |
2259 | return -ECANCELED; | |
2260 | } | |
2261 | ||
2262 | s->failure = false; | |
2263 | s->main_pid_known = false; | |
2264 | s->forbid_restart = false; | |
2265 | ||
2266 | service_enter_start_pre(s); | |
2267 | return 0; | |
2268 | } | |
2269 | ||
2270 | static int service_stop(Unit *u) { | |
2271 | Service *s = SERVICE(u); | |
2272 | ||
2273 | assert(s); | |
2274 | ||
2275 | /* This is a user request, so don't do restarts on this | |
2276 | * shutdown. */ | |
2277 | s->forbid_restart = true; | |
2278 | ||
2279 | /* Already on it */ | |
2280 | if (s->state == SERVICE_STOP || | |
2281 | s->state == SERVICE_STOP_SIGTERM || | |
2282 | s->state == SERVICE_STOP_SIGKILL || | |
2283 | s->state == SERVICE_STOP_POST || | |
2284 | s->state == SERVICE_FINAL_SIGTERM || | |
2285 | s->state == SERVICE_FINAL_SIGKILL) | |
2286 | return 0; | |
2287 | ||
2288 | /* Don't allow a restart */ | |
2289 | if (s->state == SERVICE_AUTO_RESTART) { | |
2290 | service_set_state(s, SERVICE_DEAD); | |
2291 | return 0; | |
2292 | } | |
2293 | ||
2294 | /* If there's already something running we go directly into | |
2295 | * kill mode. */ | |
2296 | if (s->state == SERVICE_START_PRE || | |
2297 | s->state == SERVICE_START || | |
2298 | s->state == SERVICE_START_POST || | |
2299 | s->state == SERVICE_RELOAD) { | |
2300 | service_enter_signal(s, SERVICE_STOP_SIGTERM, true); | |
2301 | return 0; | |
2302 | } | |
2303 | ||
2304 | assert(s->state == SERVICE_RUNNING || | |
2305 | s->state == SERVICE_EXITED); | |
2306 | ||
2307 | service_enter_stop(s, true); | |
2308 | return 0; | |
2309 | } | |
2310 | ||
2311 | static int service_reload(Unit *u) { | |
2312 | Service *s = SERVICE(u); | |
2313 | ||
2314 | assert(s); | |
2315 | ||
2316 | assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED); | |
2317 | ||
2318 | service_enter_reload(s); | |
2319 | return 0; | |
2320 | } | |
2321 | ||
2322 | static bool service_can_reload(Unit *u) { | |
2323 | Service *s = SERVICE(u); | |
2324 | ||
2325 | assert(s); | |
2326 | ||
2327 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
2328 | } | |
2329 | ||
2330 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { | |
2331 | Service *s = SERVICE(u); | |
2332 | ||
2333 | assert(u); | |
2334 | assert(f); | |
2335 | assert(fds); | |
2336 | ||
2337 | unit_serialize_item(u, f, "state", service_state_to_string(s->state)); | |
2338 | unit_serialize_item(u, f, "failure", yes_no(s->failure)); | |
2339 | ||
2340 | if (s->control_pid > 0) | |
2341 | unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid); | |
2342 | ||
2343 | if (s->main_pid_known && s->main_pid > 0) | |
2344 | unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid); | |
2345 | ||
2346 | unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known)); | |
2347 | ||
2348 | if (s->status_text) | |
2349 | unit_serialize_item(u, f, "status-text", s->status_text); | |
2350 | ||
2351 | /* There's a minor uncleanliness here: if there are multiple | |
2352 | * commands attached here, we will start from the first one | |
2353 | * again */ | |
2354 | if (s->control_command_id >= 0) | |
2355 | unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id)); | |
2356 | ||
2357 | if (s->socket_fd >= 0) { | |
2358 | int copy; | |
2359 | ||
2360 | if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0) | |
2361 | return copy; | |
2362 | ||
2363 | unit_serialize_item_format(u, f, "socket-fd", "%i", copy); | |
2364 | } | |
2365 | ||
2366 | if (s->main_exec_status.pid > 0) { | |
2367 | unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid); | |
2368 | dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp); | |
2369 | dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp); | |
2370 | ||
2371 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { | |
2372 | unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code); | |
2373 | unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
2374 | } | |
2375 | } | |
2376 | ||
2377 | return 0; | |
2378 | } | |
2379 | ||
2380 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
2381 | Service *s = SERVICE(u); | |
2382 | ||
2383 | assert(u); | |
2384 | assert(key); | |
2385 | assert(value); | |
2386 | assert(fds); | |
2387 | ||
2388 | if (streq(key, "state")) { | |
2389 | ServiceState state; | |
2390 | ||
2391 | if ((state = service_state_from_string(value)) < 0) | |
2392 | log_debug("Failed to parse state value %s", value); | |
2393 | else | |
2394 | s->deserialized_state = state; | |
2395 | } else if (streq(key, "failure")) { | |
2396 | int b; | |
2397 | ||
2398 | if ((b = parse_boolean(value)) < 0) | |
2399 | log_debug("Failed to parse failure value %s", value); | |
2400 | else | |
2401 | s->failure = b || s->failure; | |
2402 | } else if (streq(key, "control-pid")) { | |
2403 | pid_t pid; | |
2404 | ||
2405 | if (parse_pid(value, &pid) < 0) | |
2406 | log_debug("Failed to parse control-pid value %s", value); | |
2407 | else | |
2408 | s->control_pid = pid; | |
2409 | } else if (streq(key, "main-pid")) { | |
2410 | pid_t pid; | |
2411 | ||
2412 | if (parse_pid(value, &pid) < 0) | |
2413 | log_debug("Failed to parse main-pid value %s", value); | |
2414 | else | |
2415 | service_set_main_pid(s, (pid_t) pid); | |
2416 | } else if (streq(key, "main-pid-known")) { | |
2417 | int b; | |
2418 | ||
2419 | if ((b = parse_boolean(value)) < 0) | |
2420 | log_debug("Failed to parse main-pid-known value %s", value); | |
2421 | else | |
2422 | s->main_pid_known = b; | |
2423 | } else if (streq(key, "status-text")) { | |
2424 | char *t; | |
2425 | ||
2426 | if ((t = strdup(value))) { | |
2427 | free(s->status_text); | |
2428 | s->status_text = t; | |
2429 | } | |
2430 | ||
2431 | } else if (streq(key, "control-command")) { | |
2432 | ServiceExecCommand id; | |
2433 | ||
2434 | if ((id = service_exec_command_from_string(value)) < 0) | |
2435 | log_debug("Failed to parse exec-command value %s", value); | |
2436 | else { | |
2437 | s->control_command_id = id; | |
2438 | s->control_command = s->exec_command[id]; | |
2439 | } | |
2440 | } else if (streq(key, "socket-fd")) { | |
2441 | int fd; | |
2442 | ||
2443 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2444 | log_debug("Failed to parse socket-fd value %s", value); | |
2445 | else { | |
2446 | ||
2447 | if (s->socket_fd >= 0) | |
2448 | close_nointr_nofail(s->socket_fd); | |
2449 | s->socket_fd = fdset_remove(fds, fd); | |
2450 | } | |
2451 | } else if (streq(key, "main-exec-status-pid")) { | |
2452 | pid_t pid; | |
2453 | ||
2454 | if (parse_pid(value, &pid) < 0) | |
2455 | log_debug("Failed to parse main-exec-status-pid value %s", value); | |
2456 | else | |
2457 | s->main_exec_status.pid = pid; | |
2458 | } else if (streq(key, "main-exec-status-code")) { | |
2459 | int i; | |
2460 | ||
2461 | if (safe_atoi(value, &i) < 0) | |
2462 | log_debug("Failed to parse main-exec-status-code value %s", value); | |
2463 | else | |
2464 | s->main_exec_status.code = i; | |
2465 | } else if (streq(key, "main-exec-status-status")) { | |
2466 | int i; | |
2467 | ||
2468 | if (safe_atoi(value, &i) < 0) | |
2469 | log_debug("Failed to parse main-exec-status-status value %s", value); | |
2470 | else | |
2471 | s->main_exec_status.status = i; | |
2472 | } else if (streq(key, "main-exec-status-start")) | |
2473 | dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp); | |
2474 | else if (streq(key, "main-exec-status-exit")) | |
2475 | dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp); | |
2476 | else | |
2477 | log_debug("Unknown serialization key '%s'", key); | |
2478 | ||
2479 | return 0; | |
2480 | } | |
2481 | ||
2482 | static UnitActiveState service_active_state(Unit *u) { | |
2483 | assert(u); | |
2484 | ||
2485 | return state_translation_table[SERVICE(u)->state]; | |
2486 | } | |
2487 | ||
2488 | static const char *service_sub_state_to_string(Unit *u) { | |
2489 | assert(u); | |
2490 | ||
2491 | return service_state_to_string(SERVICE(u)->state); | |
2492 | } | |
2493 | ||
2494 | #ifdef HAVE_SYSV_COMPAT | |
2495 | static bool service_check_gc(Unit *u) { | |
2496 | Service *s = SERVICE(u); | |
2497 | ||
2498 | assert(s); | |
2499 | ||
2500 | return !!s->sysv_path; | |
2501 | } | |
2502 | #endif | |
2503 | ||
2504 | static bool service_check_snapshot(Unit *u) { | |
2505 | Service *s = SERVICE(u); | |
2506 | ||
2507 | assert(s); | |
2508 | ||
2509 | return !s->got_socket_fd; | |
2510 | } | |
2511 | ||
2512 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { | |
2513 | Service *s = SERVICE(u); | |
2514 | bool success; | |
2515 | ||
2516 | assert(s); | |
2517 | assert(pid >= 0); | |
2518 | ||
2519 | if (!s->meta.fragment_path) | |
2520 | success = is_clean_exit_lsb(code, status); | |
2521 | else | |
2522 | success = is_clean_exit(code, status); | |
2523 | ||
2524 | if (s->main_pid == pid) { | |
2525 | ||
2526 | s->main_pid = 0; | |
2527 | exec_status_exit(&s->main_exec_status, pid, code, status, s->exec_context.utmp_id); | |
2528 | ||
2529 | /* If this is not a forking service than the main | |
2530 | * process got started and hence we copy the exit | |
2531 | * status so that it is recorded both as main and as | |
2532 | * control process exit status */ | |
2533 | if (s->main_command) { | |
2534 | s->main_command->exec_status = s->main_exec_status; | |
2535 | ||
2536 | if (s->main_command->ignore) | |
2537 | success = true; | |
2538 | } | |
2539 | ||
2540 | log_full(success ? LOG_DEBUG : LOG_NOTICE, | |
2541 | "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status); | |
2542 | s->failure = s->failure || !success; | |
2543 | ||
2544 | if (s->main_command && | |
2545 | s->main_command->command_next && | |
2546 | success) { | |
2547 | ||
2548 | /* There is another command to * | |
2549 | * execute, so let's do that. */ | |
2550 | ||
2551 | log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state)); | |
2552 | service_run_next_main(s, success); | |
2553 | ||
2554 | } else { | |
2555 | ||
2556 | /* The service exited, so the service is officially | |
2557 | * gone. */ | |
2558 | s->main_command = NULL; | |
2559 | ||
2560 | switch (s->state) { | |
2561 | ||
2562 | case SERVICE_START_POST: | |
2563 | case SERVICE_RELOAD: | |
2564 | case SERVICE_STOP: | |
2565 | /* Need to wait until the operation is | |
2566 | * done */ | |
2567 | break; | |
2568 | ||
2569 | case SERVICE_START: | |
2570 | if (s->type == SERVICE_ONESHOT) { | |
2571 | /* This was our main goal, so let's go on */ | |
2572 | if (success) | |
2573 | service_enter_start_post(s); | |
2574 | else | |
2575 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2576 | break; | |
2577 | } else { | |
2578 | assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY); | |
2579 | ||
2580 | /* Fall through */ | |
2581 | } | |
2582 | ||
2583 | case SERVICE_RUNNING: | |
2584 | service_enter_running(s, success); | |
2585 | break; | |
2586 | ||
2587 | case SERVICE_STOP_SIGTERM: | |
2588 | case SERVICE_STOP_SIGKILL: | |
2589 | ||
2590 | if (!control_pid_good(s)) | |
2591 | service_enter_stop_post(s, success); | |
2592 | ||
2593 | /* If there is still a control process, wait for that first */ | |
2594 | break; | |
2595 | ||
2596 | default: | |
2597 | assert_not_reached("Uh, main process died at wrong time."); | |
2598 | } | |
2599 | } | |
2600 | ||
2601 | } else if (s->control_pid == pid) { | |
2602 | ||
2603 | s->control_pid = 0; | |
2604 | ||
2605 | if (s->control_command) { | |
2606 | exec_status_exit(&s->control_command->exec_status, pid, code, status, s->exec_context.utmp_id); | |
2607 | ||
2608 | if (s->control_command->ignore) | |
2609 | success = true; | |
2610 | } | |
2611 | ||
2612 | log_full(success ? LOG_DEBUG : LOG_NOTICE, | |
2613 | "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status); | |
2614 | s->failure = s->failure || !success; | |
2615 | ||
2616 | if (s->control_command && | |
2617 | s->control_command->command_next && | |
2618 | success) { | |
2619 | ||
2620 | /* There is another command to * | |
2621 | * execute, so let's do that. */ | |
2622 | ||
2623 | log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state)); | |
2624 | service_run_next_control(s, success); | |
2625 | ||
2626 | } else { | |
2627 | /* No further commands for this step, so let's | |
2628 | * figure out what to do next */ | |
2629 | ||
2630 | s->control_command = NULL; | |
2631 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2632 | ||
2633 | log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state)); | |
2634 | ||
2635 | switch (s->state) { | |
2636 | ||
2637 | case SERVICE_START_PRE: | |
2638 | if (success) | |
2639 | service_enter_start(s); | |
2640 | else | |
2641 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2642 | break; | |
2643 | ||
2644 | case SERVICE_START: | |
2645 | assert(s->type == SERVICE_FORKING); | |
2646 | ||
2647 | /* Let's try to load the pid | |
2648 | * file here if we can. We | |
2649 | * ignore the return value, | |
2650 | * since the PID file might | |
2651 | * actually be created by a | |
2652 | * START_POST script */ | |
2653 | ||
2654 | if (success) { | |
2655 | service_load_pid_file(s); | |
2656 | service_search_main_pid(s); | |
2657 | ||
2658 | service_enter_start_post(s); | |
2659 | } else | |
2660 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2661 | ||
2662 | break; | |
2663 | ||
2664 | case SERVICE_START_POST: | |
2665 | if (success && s->pid_file && !s->main_pid_known) { | |
2666 | int r; | |
2667 | ||
2668 | /* Hmm, let's see if we can | |
2669 | * load the pid now after the | |
2670 | * start-post scripts got | |
2671 | * executed. */ | |
2672 | ||
2673 | if ((r = service_load_pid_file(s)) < 0) | |
2674 | log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r)); | |
2675 | } | |
2676 | ||
2677 | /* Fall through */ | |
2678 | ||
2679 | case SERVICE_RELOAD: | |
2680 | s->reload_failure = !success; | |
2681 | service_enter_running(s, true); | |
2682 | break; | |
2683 | ||
2684 | case SERVICE_STOP: | |
2685 | service_enter_signal(s, SERVICE_STOP_SIGTERM, success); | |
2686 | break; | |
2687 | ||
2688 | case SERVICE_STOP_SIGTERM: | |
2689 | case SERVICE_STOP_SIGKILL: | |
2690 | if (main_pid_good(s) <= 0) | |
2691 | service_enter_stop_post(s, success); | |
2692 | ||
2693 | /* If there is still a service | |
2694 | * process around, wait until | |
2695 | * that one quit, too */ | |
2696 | break; | |
2697 | ||
2698 | case SERVICE_STOP_POST: | |
2699 | case SERVICE_FINAL_SIGTERM: | |
2700 | case SERVICE_FINAL_SIGKILL: | |
2701 | service_enter_dead(s, success, true); | |
2702 | break; | |
2703 | ||
2704 | default: | |
2705 | assert_not_reached("Uh, control process died at wrong time."); | |
2706 | } | |
2707 | } | |
2708 | } | |
2709 | ||
2710 | /* Notify clients about changed exit status */ | |
2711 | unit_add_to_dbus_queue(u); | |
2712 | } | |
2713 | ||
2714 | static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) { | |
2715 | Service *s = SERVICE(u); | |
2716 | ||
2717 | assert(s); | |
2718 | assert(elapsed == 1); | |
2719 | ||
2720 | assert(w == &s->timer_watch); | |
2721 | ||
2722 | switch (s->state) { | |
2723 | ||
2724 | case SERVICE_START_PRE: | |
2725 | case SERVICE_START: | |
2726 | log_warning("%s operation timed out. Terminating.", u->meta.id); | |
2727 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2728 | break; | |
2729 | ||
2730 | case SERVICE_START_POST: | |
2731 | log_warning("%s operation timed out. Stopping.", u->meta.id); | |
2732 | service_enter_stop(s, false); | |
2733 | break; | |
2734 | ||
2735 | case SERVICE_RELOAD: | |
2736 | log_warning("%s operation timed out. Stopping.", u->meta.id); | |
2737 | s->reload_failure = true; | |
2738 | service_enter_running(s, true); | |
2739 | break; | |
2740 | ||
2741 | case SERVICE_STOP: | |
2742 | log_warning("%s stopping timed out. Terminating.", u->meta.id); | |
2743 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); | |
2744 | break; | |
2745 | ||
2746 | case SERVICE_STOP_SIGTERM: | |
2747 | if (s->exec_context.send_sigkill) { | |
2748 | log_warning("%s stopping timed out. Killing.", u->meta.id); | |
2749 | service_enter_signal(s, SERVICE_STOP_SIGKILL, false); | |
2750 | } else { | |
2751 | log_warning("%s stopping timed out. Skipping SIGKILL.", u->meta.id); | |
2752 | service_enter_stop_post(s, false); | |
2753 | } | |
2754 | ||
2755 | break; | |
2756 | ||
2757 | case SERVICE_STOP_SIGKILL: | |
2758 | /* Uh, wie sent a SIGKILL and it is still not gone? | |
2759 | * Must be something we cannot kill, so let's just be | |
2760 | * weirded out and continue */ | |
2761 | ||
2762 | log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id); | |
2763 | service_enter_stop_post(s, false); | |
2764 | break; | |
2765 | ||
2766 | case SERVICE_STOP_POST: | |
2767 | log_warning("%s stopping timed out (2). Terminating.", u->meta.id); | |
2768 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); | |
2769 | break; | |
2770 | ||
2771 | case SERVICE_FINAL_SIGTERM: | |
2772 | if (s->exec_context.send_sigkill) { | |
2773 | log_warning("%s stopping timed out (2). Killing.", u->meta.id); | |
2774 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, false); | |
2775 | } else { | |
2776 | log_warning("%s stopping timed out (2). Skipping SIGKILL. Entering failed mode.", u->meta.id); | |
2777 | service_enter_dead(s, false, true); | |
2778 | } | |
2779 | ||
2780 | break; | |
2781 | ||
2782 | case SERVICE_FINAL_SIGKILL: | |
2783 | log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id); | |
2784 | service_enter_dead(s, false, true); | |
2785 | break; | |
2786 | ||
2787 | case SERVICE_AUTO_RESTART: | |
2788 | log_info("%s holdoff time over, scheduling restart.", u->meta.id); | |
2789 | service_enter_restart(s); | |
2790 | break; | |
2791 | ||
2792 | default: | |
2793 | assert_not_reached("Timeout at wrong time."); | |
2794 | } | |
2795 | } | |
2796 | ||
2797 | static void service_cgroup_notify_event(Unit *u) { | |
2798 | Service *s = SERVICE(u); | |
2799 | ||
2800 | assert(u); | |
2801 | ||
2802 | log_debug("%s: cgroup is empty", u->meta.id); | |
2803 | ||
2804 | switch (s->state) { | |
2805 | ||
2806 | /* Waiting for SIGCHLD is usually more interesting, | |
2807 | * because it includes return codes/signals. Which is | |
2808 | * why we ignore the cgroup events for most cases, | |
2809 | * except when we don't know pid which to expect the | |
2810 | * SIGCHLD for. */ | |
2811 | ||
2812 | case SERVICE_RUNNING: | |
2813 | service_enter_running(s, true); | |
2814 | break; | |
2815 | ||
2816 | case SERVICE_STOP_SIGTERM: | |
2817 | case SERVICE_STOP_SIGKILL: | |
2818 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2819 | service_enter_stop_post(s, true); | |
2820 | ||
2821 | break; | |
2822 | ||
2823 | case SERVICE_FINAL_SIGTERM: | |
2824 | case SERVICE_FINAL_SIGKILL: | |
2825 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2826 | service_enter_dead(s, true, true); | |
2827 | ||
2828 | break; | |
2829 | ||
2830 | default: | |
2831 | ; | |
2832 | } | |
2833 | } | |
2834 | ||
2835 | static void service_notify_message(Unit *u, pid_t pid, char **tags) { | |
2836 | Service *s = SERVICE(u); | |
2837 | const char *e; | |
2838 | ||
2839 | assert(u); | |
2840 | ||
2841 | if (s->notify_access == NOTIFY_NONE) { | |
2842 | log_warning("%s: Got notification message from PID %lu, but reception is disabled.", | |
2843 | u->meta.id, (unsigned long) pid); | |
2844 | return; | |
2845 | } | |
2846 | ||
2847 | if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { | |
2848 | log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu", | |
2849 | u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid); | |
2850 | return; | |
2851 | } | |
2852 | ||
2853 | log_debug("%s: Got message", u->meta.id); | |
2854 | ||
2855 | /* Interpret MAINPID= */ | |
2856 | if ((e = strv_find_prefix(tags, "MAINPID=")) && | |
2857 | (s->state == SERVICE_START || | |
2858 | s->state == SERVICE_START_POST || | |
2859 | s->state == SERVICE_RUNNING || | |
2860 | s->state == SERVICE_RELOAD)) { | |
2861 | ||
2862 | if (parse_pid(e + 8, &pid) < 0) | |
2863 | log_warning("Failed to parse notification message %s", e); | |
2864 | else { | |
2865 | log_debug("%s: got %s", u->meta.id, e); | |
2866 | service_set_main_pid(s, pid); | |
2867 | } | |
2868 | } | |
2869 | ||
2870 | /* Interpret READY= */ | |
2871 | if (s->type == SERVICE_NOTIFY && | |
2872 | s->state == SERVICE_START && | |
2873 | strv_find(tags, "READY=1")) { | |
2874 | log_debug("%s: got READY=1", u->meta.id); | |
2875 | ||
2876 | service_enter_start_post(s); | |
2877 | } | |
2878 | ||
2879 | /* Interpret STATUS= */ | |
2880 | if ((e = strv_find_prefix(tags, "STATUS="))) { | |
2881 | char *t; | |
2882 | ||
2883 | if (e[7]) { | |
2884 | if (!(t = strdup(e+7))) { | |
2885 | log_error("Failed to allocate string."); | |
2886 | return; | |
2887 | } | |
2888 | ||
2889 | log_debug("%s: got %s", u->meta.id, e); | |
2890 | ||
2891 | free(s->status_text); | |
2892 | s->status_text = t; | |
2893 | } else { | |
2894 | free(s->status_text); | |
2895 | s->status_text = NULL; | |
2896 | } | |
2897 | ||
2898 | } | |
2899 | ||
2900 | /* Notify clients about changed status or main pid */ | |
2901 | unit_add_to_dbus_queue(u); | |
2902 | } | |
2903 | ||
2904 | #ifdef HAVE_SYSV_COMPAT | |
2905 | static int service_enumerate(Manager *m) { | |
2906 | char **p; | |
2907 | unsigned i; | |
2908 | DIR *d = NULL; | |
2909 | char *path = NULL, *fpath = NULL, *name = NULL; | |
2910 | Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL; | |
2911 | Unit *service; | |
2912 | Iterator j; | |
2913 | int r; | |
2914 | ||
2915 | assert(m); | |
2916 | ||
2917 | zero(runlevel_services); | |
2918 | ||
2919 | STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path) | |
2920 | for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) { | |
2921 | struct dirent *de; | |
2922 | ||
2923 | free(path); | |
2924 | path = NULL; | |
2925 | if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) { | |
2926 | r = -ENOMEM; | |
2927 | goto finish; | |
2928 | } | |
2929 | ||
2930 | if (d) | |
2931 | closedir(d); | |
2932 | ||
2933 | if (!(d = opendir(path))) { | |
2934 | if (errno != ENOENT) | |
2935 | log_warning("opendir() failed on %s: %s", path, strerror(errno)); | |
2936 | ||
2937 | continue; | |
2938 | } | |
2939 | ||
2940 | while ((de = readdir(d))) { | |
2941 | int a, b; | |
2942 | ||
2943 | if (ignore_file(de->d_name)) | |
2944 | continue; | |
2945 | ||
2946 | if (de->d_name[0] != 'S' && de->d_name[0] != 'K') | |
2947 | continue; | |
2948 | ||
2949 | if (strlen(de->d_name) < 4) | |
2950 | continue; | |
2951 | ||
2952 | a = undecchar(de->d_name[1]); | |
2953 | b = undecchar(de->d_name[2]); | |
2954 | ||
2955 | if (a < 0 || b < 0) | |
2956 | continue; | |
2957 | ||
2958 | free(fpath); | |
2959 | fpath = NULL; | |
2960 | if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) { | |
2961 | r = -ENOMEM; | |
2962 | goto finish; | |
2963 | } | |
2964 | ||
2965 | if (access(fpath, X_OK) < 0) { | |
2966 | ||
2967 | if (errno != ENOENT) | |
2968 | log_warning("access() failed on %s: %s", fpath, strerror(errno)); | |
2969 | ||
2970 | continue; | |
2971 | } | |
2972 | ||
2973 | free(name); | |
2974 | if (!(name = sysv_translate_name(de->d_name + 3))) { | |
2975 | r = -ENOMEM; | |
2976 | goto finish; | |
2977 | } | |
2978 | ||
2979 | if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) { | |
2980 | log_warning("Failed to prepare unit %s: %s", name, strerror(-r)); | |
2981 | continue; | |
2982 | } | |
2983 | ||
2984 | if (de->d_name[0] == 'S') { | |
2985 | ||
2986 | if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) { | |
2987 | SERVICE(service)->sysv_start_priority = | |
2988 | MAX(a*10 + b, SERVICE(service)->sysv_start_priority); | |
2989 | ||
2990 | SERVICE(service)->sysv_enabled = true; | |
2991 | } | |
2992 | ||
2993 | if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0) | |
2994 | goto finish; | |
2995 | ||
2996 | if ((r = set_put(runlevel_services[i], service)) < 0) | |
2997 | goto finish; | |
2998 | ||
2999 | } else if (de->d_name[0] == 'K' && | |
3000 | (rcnd_table[i].type == RUNLEVEL_DOWN || | |
3001 | rcnd_table[i].type == RUNLEVEL_SYSINIT)) { | |
3002 | ||
3003 | if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0) | |
3004 | goto finish; | |
3005 | ||
3006 | if ((r = set_put(shutdown_services, service)) < 0) | |
3007 | goto finish; | |
3008 | } | |
3009 | } | |
3010 | } | |
3011 | ||
3012 | /* Now we loaded all stubs and are aware of the lowest | |
3013 | start-up priority for all services, not let's actually load | |
3014 | the services, this will also tell us which services are | |
3015 | actually native now */ | |
3016 | manager_dispatch_load_queue(m); | |
3017 | ||
3018 | /* If this is a native service, rely on native ways to pull in | |
3019 | * a service, don't pull it in via sysv rcN.d links. */ | |
3020 | for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) | |
3021 | SET_FOREACH(service, runlevel_services[i], j) { | |
3022 | service = unit_follow_merge(service); | |
3023 | ||
3024 | if (service->meta.fragment_path) | |
3025 | continue; | |
3026 | ||
3027 | if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0) | |
3028 | goto finish; | |
3029 | } | |
3030 | ||
3031 | /* We honour K links only for halt/reboot. For the normal | |
3032 | * runlevels we assume the stop jobs will be implicitly added | |
3033 | * by the core logic. Also, we don't really distuingish here | |
3034 | * between the runlevels 0 and 6 and just add them to the | |
3035 | * special shutdown target. On SUSE the boot.d/ runlevel is | |
3036 | * also used for shutdown, so we add links for that too to the | |
3037 | * shutdown target.*/ | |
3038 | SET_FOREACH(service, shutdown_services, j) { | |
3039 | service = unit_follow_merge(service); | |
3040 | ||
3041 | if (service->meta.fragment_path) | |
3042 | continue; | |
3043 | ||
3044 | if ((r = unit_add_two_dependencies_by_name(service, UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0) | |
3045 | goto finish; | |
3046 | } | |
3047 | ||
3048 | r = 0; | |
3049 | ||
3050 | finish: | |
3051 | free(path); | |
3052 | free(fpath); | |
3053 | free(name); | |
3054 | ||
3055 | for (i = 0; i < ELEMENTSOF(rcnd_table); i++) | |
3056 | set_free(runlevel_services[i]); | |
3057 | set_free(shutdown_services); | |
3058 | ||
3059 | if (d) | |
3060 | closedir(d); | |
3061 | ||
3062 | return r; | |
3063 | } | |
3064 | #endif | |
3065 | ||
3066 | static void service_bus_name_owner_change( | |
3067 | Unit *u, | |
3068 | const char *name, | |
3069 | const char *old_owner, | |
3070 | const char *new_owner) { | |
3071 | ||
3072 | Service *s = SERVICE(u); | |
3073 | ||
3074 | assert(s); | |
3075 | assert(name); | |
3076 | ||
3077 | assert(streq(s->bus_name, name)); | |
3078 | assert(old_owner || new_owner); | |
3079 | ||
3080 | if (old_owner && new_owner) | |
3081 | log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner); | |
3082 | else if (old_owner) | |
3083 | log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner); | |
3084 | else | |
3085 | log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner); | |
3086 | ||
3087 | s->bus_name_good = !!new_owner; | |
3088 | ||
3089 | if (s->type == SERVICE_DBUS) { | |
3090 | ||
3091 | /* service_enter_running() will figure out what to | |
3092 | * do */ | |
3093 | if (s->state == SERVICE_RUNNING) | |
3094 | service_enter_running(s, true); | |
3095 | else if (s->state == SERVICE_START && new_owner) | |
3096 | service_enter_start_post(s); | |
3097 | ||
3098 | } else if (new_owner && | |
3099 | s->main_pid <= 0 && | |
3100 | (s->state == SERVICE_START || | |
3101 | s->state == SERVICE_START_POST || | |
3102 | s->state == SERVICE_RUNNING || | |
3103 | s->state == SERVICE_RELOAD)) { | |
3104 | ||
3105 | /* Try to acquire PID from bus service */ | |
3106 | log_debug("Trying to acquire PID from D-Bus name..."); | |
3107 | ||
3108 | bus_query_pid(u->meta.manager, name); | |
3109 | } | |
3110 | } | |
3111 | ||
3112 | static void service_bus_query_pid_done( | |
3113 | Unit *u, | |
3114 | const char *name, | |
3115 | pid_t pid) { | |
3116 | ||
3117 | Service *s = SERVICE(u); | |
3118 | ||
3119 | assert(s); | |
3120 | assert(name); | |
3121 | ||
3122 | log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid); | |
3123 | ||
3124 | if (s->main_pid <= 0 && | |
3125 | (s->state == SERVICE_START || | |
3126 | s->state == SERVICE_START_POST || | |
3127 | s->state == SERVICE_RUNNING || | |
3128 | s->state == SERVICE_RELOAD)) | |
3129 | service_set_main_pid(s, pid); | |
3130 | } | |
3131 | ||
3132 | int service_set_socket_fd(Service *s, int fd, Socket *sock) { | |
3133 | assert(s); | |
3134 | assert(fd >= 0); | |
3135 | ||
3136 | /* This is called by the socket code when instantiating a new | |
3137 | * service for a stream socket and the socket needs to be | |
3138 | * configured. */ | |
3139 | ||
3140 | if (s->meta.load_state != UNIT_LOADED) | |
3141 | return -EINVAL; | |
3142 | ||
3143 | if (s->socket_fd >= 0) | |
3144 | return -EBUSY; | |
3145 | ||
3146 | if (s->state != SERVICE_DEAD) | |
3147 | return -EAGAIN; | |
3148 | ||
3149 | s->socket_fd = fd; | |
3150 | s->got_socket_fd = true; | |
3151 | s->accept_socket = sock; | |
3152 | ||
3153 | return 0; | |
3154 | } | |
3155 | ||
3156 | static void service_reset_failed(Unit *u) { | |
3157 | Service *s = SERVICE(u); | |
3158 | ||
3159 | assert(s); | |
3160 | ||
3161 | if (s->state == SERVICE_FAILED) | |
3162 | service_set_state(s, SERVICE_DEAD); | |
3163 | ||
3164 | s->failure = false; | |
3165 | } | |
3166 | ||
3167 | static int service_kill(Unit *u, KillWho who, KillMode mode, int signo, DBusError *error) { | |
3168 | Service *s = SERVICE(u); | |
3169 | int r = 0; | |
3170 | Set *pid_set = NULL; | |
3171 | ||
3172 | assert(s); | |
3173 | ||
3174 | if (s->main_pid <= 0 && who == KILL_MAIN) { | |
3175 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No main process to kill"); | |
3176 | return -EINVAL; | |
3177 | } | |
3178 | ||
3179 | if (s->control_pid <= 0 && who == KILL_CONTROL) { | |
3180 | dbus_set_error(error, BUS_ERROR_NO_SUCH_PROCESS, "No control process to kill"); | |
3181 | return -ENOENT; | |
3182 | } | |
3183 | ||
3184 | if (s->control_pid > 0) | |
3185 | if (kill(mode == KILL_PROCESS_GROUP ? -s->control_pid : s->control_pid, signo) < 0) | |
3186 | r = -errno; | |
3187 | ||
3188 | if (s->main_pid > 0) | |
3189 | if (kill(mode == KILL_PROCESS_GROUP ? -s->main_pid : s->main_pid, signo) < 0) | |
3190 | r = -errno; | |
3191 | ||
3192 | if (mode == KILL_CONTROL_GROUP) { | |
3193 | int q; | |
3194 | ||
3195 | if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) | |
3196 | return -ENOMEM; | |
3197 | ||
3198 | /* Exclude the control/main pid from being killed via the cgroup */ | |
3199 | if (s->control_pid > 0) | |
3200 | if ((q = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0) { | |
3201 | r = q; | |
3202 | goto finish; | |
3203 | } | |
3204 | ||
3205 | if (s->main_pid > 0) | |
3206 | if ((q = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0) { | |
3207 | r = q; | |
3208 | goto finish; | |
3209 | } | |
3210 | ||
3211 | if ((q = cgroup_bonding_kill_list(s->meta.cgroup_bondings, signo, pid_set)) < 0) | |
3212 | if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) | |
3213 | r = q; | |
3214 | } | |
3215 | ||
3216 | finish: | |
3217 | if (pid_set) | |
3218 | set_free(pid_set); | |
3219 | ||
3220 | return r; | |
3221 | } | |
3222 | ||
3223 | static const char* const service_state_table[_SERVICE_STATE_MAX] = { | |
3224 | [SERVICE_DEAD] = "dead", | |
3225 | [SERVICE_START_PRE] = "start-pre", | |
3226 | [SERVICE_START] = "start", | |
3227 | [SERVICE_START_POST] = "start-post", | |
3228 | [SERVICE_RUNNING] = "running", | |
3229 | [SERVICE_EXITED] = "exited", | |
3230 | [SERVICE_RELOAD] = "reload", | |
3231 | [SERVICE_STOP] = "stop", | |
3232 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", | |
3233 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
3234 | [SERVICE_STOP_POST] = "stop-post", | |
3235 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
3236 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
3237 | [SERVICE_FAILED] = "failed", | |
3238 | [SERVICE_AUTO_RESTART] = "auto-restart", | |
3239 | }; | |
3240 | ||
3241 | DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); | |
3242 | ||
3243 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { | |
3244 | [SERVICE_RESTART_NO] = "no", | |
3245 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
3246 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", | |
3247 | [SERVICE_RESTART_ON_ABORT] = "on-abort", | |
3248 | [SERVICE_RESTART_ALWAYS] = "always" | |
3249 | }; | |
3250 | ||
3251 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
3252 | ||
3253 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
3254 | [SERVICE_SIMPLE] = "simple", | |
3255 | [SERVICE_FORKING] = "forking", | |
3256 | [SERVICE_ONESHOT] = "oneshot", | |
3257 | [SERVICE_DBUS] = "dbus", | |
3258 | [SERVICE_NOTIFY] = "notify" | |
3259 | }; | |
3260 | ||
3261 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
3262 | ||
3263 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { | |
3264 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", | |
3265 | [SERVICE_EXEC_START] = "ExecStart", | |
3266 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
3267 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
3268 | [SERVICE_EXEC_STOP] = "ExecStop", | |
3269 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
3270 | }; | |
3271 | ||
3272 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
3273 | ||
3274 | static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = { | |
3275 | [NOTIFY_NONE] = "none", | |
3276 | [NOTIFY_MAIN] = "main", | |
3277 | [NOTIFY_ALL] = "all" | |
3278 | }; | |
3279 | ||
3280 | DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess); | |
3281 | ||
3282 | const UnitVTable service_vtable = { | |
3283 | .suffix = ".service", | |
3284 | .show_status = true, | |
3285 | ||
3286 | .init = service_init, | |
3287 | .done = service_done, | |
3288 | .load = service_load, | |
3289 | ||
3290 | .coldplug = service_coldplug, | |
3291 | ||
3292 | .dump = service_dump, | |
3293 | ||
3294 | .start = service_start, | |
3295 | .stop = service_stop, | |
3296 | .reload = service_reload, | |
3297 | ||
3298 | .can_reload = service_can_reload, | |
3299 | ||
3300 | .kill = service_kill, | |
3301 | ||
3302 | .serialize = service_serialize, | |
3303 | .deserialize_item = service_deserialize_item, | |
3304 | ||
3305 | .active_state = service_active_state, | |
3306 | .sub_state_to_string = service_sub_state_to_string, | |
3307 | ||
3308 | #ifdef HAVE_SYSV_COMPAT | |
3309 | .check_gc = service_check_gc, | |
3310 | #endif | |
3311 | .check_snapshot = service_check_snapshot, | |
3312 | ||
3313 | .sigchld_event = service_sigchld_event, | |
3314 | .timer_event = service_timer_event, | |
3315 | ||
3316 | .reset_failed = service_reset_failed, | |
3317 | ||
3318 | .cgroup_notify_empty = service_cgroup_notify_event, | |
3319 | .notify_message = service_notify_message, | |
3320 | ||
3321 | .bus_name_owner_change = service_bus_name_owner_change, | |
3322 | .bus_query_pid_done = service_bus_query_pid_done, | |
3323 | ||
3324 | .bus_interface = "org.freedesktop.systemd1.Service", | |
3325 | .bus_message_handler = bus_service_message_handler, | |
3326 | .bus_invalidating_properties = bus_service_invalidating_properties, | |
3327 | ||
3328 | #ifdef HAVE_SYSV_COMPAT | |
3329 | .enumerate = service_enumerate | |
3330 | #endif | |
3331 | }; |