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