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