]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/service.c
service: add minimal access control logic for notifcation socket
[thirdparty/systemd.git] / src / service.c
1 /*-*- Mode: C; c-basic-offset: 8 -*-*/
2
3 /***
4 This file is part of systemd.
5
6 Copyright 2010 Lennart Poettering
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20 ***/
21
22 #include <errno.h>
23 #include <signal.h>
24 #include <dirent.h>
25 #include <unistd.h>
26
27 #include "unit.h"
28 #include "service.h"
29 #include "load-fragment.h"
30 #include "load-dropin.h"
31 #include "log.h"
32 #include "strv.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
35 #include "special.h"
36
37 #define COMMENTS "#;\n"
38 #define NEWLINES "\n\r"
39 #define LINE_MAX 4096
40
41 typedef enum RunlevelType {
42 RUNLEVEL_UP,
43 RUNLEVEL_DOWN,
44 RUNLEVEL_SYSINIT
45 } RunlevelType;
46
47 static const struct {
48 const char *path;
49 const char *target;
50 const RunlevelType type;
51 } rcnd_table[] = {
52 /* Standard SysV runlevels */
53 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
54 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
55 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
56 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
57 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
58 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
59 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN },
60
61 /* SUSE style boot.d */
62 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
63
64 /* Debian style rcS.d */
65 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
66 };
67
68 #define RUNLEVELS_UP "12345"
69 /* #define RUNLEVELS_DOWN "06" */
70 /* #define RUNLEVELS_BOOT "bBsS" */
71
72 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
73 [SERVICE_DEAD] = UNIT_INACTIVE,
74 [SERVICE_START_PRE] = UNIT_ACTIVATING,
75 [SERVICE_START] = UNIT_ACTIVATING,
76 [SERVICE_START_POST] = UNIT_ACTIVATING,
77 [SERVICE_RUNNING] = UNIT_ACTIVE,
78 [SERVICE_EXITED] = UNIT_ACTIVE,
79 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
80 [SERVICE_STOP] = UNIT_DEACTIVATING,
81 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
82 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
83 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
84 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
85 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
86 [SERVICE_MAINTENANCE] = UNIT_INACTIVE,
87 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
88 };
89
90 static void service_init(Unit *u) {
91 Service *s = SERVICE(u);
92
93 assert(u);
94 assert(u->meta.load_state == UNIT_STUB);
95
96 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
97 s->restart_usec = DEFAULT_RESTART_USEC;
98 s->timer_watch.type = WATCH_INVALID;
99 s->sysv_start_priority = -1;
100 s->socket_fd = -1;
101
102 exec_context_init(&s->exec_context);
103
104 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
105
106 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
107 }
108
109 static void service_unwatch_control_pid(Service *s) {
110 assert(s);
111
112 if (s->control_pid <= 0)
113 return;
114
115 unit_unwatch_pid(UNIT(s), s->control_pid);
116 s->control_pid = 0;
117 }
118
119 static void service_unwatch_main_pid(Service *s) {
120 assert(s);
121
122 if (s->main_pid <= 0)
123 return;
124
125 unit_unwatch_pid(UNIT(s), s->main_pid);
126 s->main_pid = 0;
127 }
128
129 static int service_set_main_pid(Service *s, pid_t pid) {
130 pid_t ppid;
131
132 assert(s);
133
134 if (pid <= 1)
135 return -EINVAL;
136
137 if (pid == getpid())
138 return -EINVAL;
139
140 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
141 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
142 s->meta.id, (unsigned long) pid);
143
144 s->main_pid = pid;
145 s->main_pid_known = true;
146
147 return 0;
148 }
149
150 static void service_close_socket_fd(Service *s) {
151 assert(s);
152
153 if (s->socket_fd < 0)
154 return;
155
156 close_nointr_nofail(s->socket_fd);
157 s->socket_fd = -1;
158 }
159
160 static void service_done(Unit *u) {
161 Service *s = SERVICE(u);
162
163 assert(s);
164
165 free(s->pid_file);
166 s->pid_file = NULL;
167
168 free(s->sysv_path);
169 s->sysv_path = NULL;
170
171 free(s->sysv_runlevels);
172 s->sysv_runlevels = NULL;
173
174 free(s->status_text);
175 s->status_text = NULL;
176
177 exec_context_done(&s->exec_context);
178 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
179 s->control_command = NULL;
180
181 /* This will leak a process, but at least no memory or any of
182 * our resources */
183 service_unwatch_main_pid(s);
184 service_unwatch_control_pid(s);
185
186 if (s->bus_name) {
187 unit_unwatch_bus_name(UNIT(u), s->bus_name);
188 free(s->bus_name);
189 s->bus_name = NULL;
190 }
191
192 service_close_socket_fd(s);
193
194 unit_unwatch_timer(u, &s->timer_watch);
195 }
196
197 static char *sysv_translate_name(const char *name) {
198 char *r;
199
200 if (!(r = new(char, strlen(name) + sizeof(".service"))))
201 return NULL;
202
203 if (startswith(name, "boot."))
204 /* Drop SuSE-style boot. prefix */
205 strcpy(stpcpy(r, name + 5), ".service");
206 else if (endswith(name, ".sh"))
207 /* Drop Debian-style .sh suffix */
208 strcpy(stpcpy(r, name) - 3, ".service");
209 else
210 /* Normal init scripts */
211 strcpy(stpcpy(r, name), ".service");
212
213 return r;
214 }
215
216 static int sysv_translate_facility(const char *name, char **_r) {
217
218 static const char * const table[] = {
219 /* LSB defined facilities */
220 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
221 "$network", SPECIAL_NETWORK_TARGET,
222 "$named", SPECIAL_NSS_LOOKUP_TARGET,
223 "$portmap", SPECIAL_RPCBIND_TARGET,
224 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
225 "$syslog", SPECIAL_SYSLOG_TARGET,
226 "$time", SPECIAL_RTC_SET_TARGET,
227
228 /* Debian extensions */
229 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
230 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
231 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE
232 };
233
234 unsigned i;
235 char *r;
236
237 for (i = 0; i < ELEMENTSOF(table); i += 2)
238 if (streq(table[i], name)) {
239 if (!(r = strdup(table[i+1])))
240 return -ENOMEM;
241
242 goto finish;
243 }
244
245 if (*name == '$')
246 return 0;
247
248 if (!(r = sysv_translate_name(name)))
249 return -ENOMEM;
250
251 finish:
252
253 if (_r)
254 *_r = r;
255
256 return 1;
257 }
258
259 static int sysv_fix_order(Service *s) {
260 Meta *other;
261 int r;
262
263 assert(s);
264
265 if (s->sysv_start_priority < 0)
266 return 0;
267
268 /* For each pair of services where at least one lacks a LSB
269 * header, we use the start priority value to order things. */
270
271 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
272 Service *t;
273 UnitDependency d;
274
275 t = (Service*) other;
276
277 if (s == t)
278 continue;
279
280 if (t->sysv_start_priority < 0)
281 continue;
282
283 /* If both units have modern headers we don't care
284 * about the priorities */
285 if ((!s->sysv_path || s->sysv_has_lsb) &&
286 (!t->sysv_path || t->sysv_has_lsb))
287 continue;
288
289 if (t->sysv_start_priority < s->sysv_start_priority)
290 d = UNIT_AFTER;
291 else if (t->sysv_start_priority > s->sysv_start_priority)
292 d = UNIT_BEFORE;
293 else
294 continue;
295
296 /* FIXME: Maybe we should compare the name here lexicographically? */
297
298 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
299 return r;
300 }
301
302 return 0;
303 }
304
305 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
306 ExecCommand *c;
307
308 if (!(c = new0(ExecCommand, 1)))
309 return NULL;
310
311 if (!(c->path = strdup(path))) {
312 free(c);
313 return NULL;
314 }
315
316 if (!(c->argv = strv_new(path, arg1, NULL))) {
317 free(c->path);
318 free(c);
319 return NULL;
320 }
321
322 return c;
323 }
324
325 static int sysv_exec_commands(Service *s) {
326 ExecCommand *c;
327
328 assert(s);
329 assert(s->sysv_path);
330
331 if (!(c = exec_command_new(s->sysv_path, "start")))
332 return -ENOMEM;
333 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
334
335 if (!(c = exec_command_new(s->sysv_path, "stop")))
336 return -ENOMEM;
337 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
338
339 if (!(c = exec_command_new(s->sysv_path, "reload")))
340 return -ENOMEM;
341 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
342
343 return 0;
344 }
345
346 static int service_load_sysv_path(Service *s, const char *path) {
347 FILE *f;
348 Unit *u;
349 unsigned line = 0;
350 int r;
351 enum {
352 NORMAL,
353 DESCRIPTION,
354 LSB,
355 LSB_DESCRIPTION
356 } state = NORMAL;
357
358 assert(s);
359 assert(path);
360
361 u = UNIT(s);
362
363 if (!(f = fopen(path, "re"))) {
364 r = errno == ENOENT ? 0 : -errno;
365 goto finish;
366 }
367
368 free(s->sysv_path);
369 if (!(s->sysv_path = strdup(path))) {
370 r = -ENOMEM;
371 goto finish;
372 }
373
374 while (!feof(f)) {
375 char l[LINE_MAX], *t;
376
377 if (!fgets(l, sizeof(l), f)) {
378 if (feof(f))
379 break;
380
381 r = -errno;
382 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
383 goto finish;
384 }
385
386 line++;
387
388 t = strstrip(l);
389 if (*t != '#')
390 continue;
391
392 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
393 state = LSB;
394 s->sysv_has_lsb = true;
395 continue;
396 }
397
398 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
399 state = NORMAL;
400 continue;
401 }
402
403 t++;
404 t += strspn(t, WHITESPACE);
405
406 if (state == NORMAL) {
407
408 /* Try to parse Red Hat style chkconfig headers */
409
410 if (startswith_no_case(t, "chkconfig:")) {
411 int start_priority;
412 char runlevels[16], *k;
413
414 state = NORMAL;
415
416 if (sscanf(t+10, "%15s %i %*i",
417 runlevels,
418 &start_priority) != 2) {
419
420 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
421 continue;
422 }
423
424 /* A start priority gathered from the
425 * symlink farms is preferred over the
426 * data from the LSB header. */
427 if (start_priority < 0 || start_priority > 99)
428 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
429 else if (s->sysv_start_priority < 0)
430 s->sysv_start_priority = start_priority;
431
432 char_array_0(runlevels);
433 k = delete_chars(runlevels, WHITESPACE "-");
434
435 if (k[0]) {
436 char *d;
437
438 if (!(d = strdup(k))) {
439 r = -ENOMEM;
440 goto finish;
441 }
442
443 free(s->sysv_runlevels);
444 s->sysv_runlevels = d;
445 }
446
447 } else if (startswith_no_case(t, "description:")) {
448
449 size_t k = strlen(t);
450 char *d;
451
452 if (t[k-1] == '\\') {
453 state = DESCRIPTION;
454 t[k-1] = 0;
455 }
456
457 if (!(d = strdup(strstrip(t+12)))) {
458 r = -ENOMEM;
459 goto finish;
460 }
461
462 free(u->meta.description);
463 u->meta.description = d;
464
465 } else if (startswith_no_case(t, "pidfile:")) {
466
467 char *fn;
468
469 state = NORMAL;
470
471 fn = strstrip(t+8);
472 if (!path_is_absolute(fn)) {
473 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
474 continue;
475 }
476
477 if (!(fn = strdup(fn))) {
478 r = -ENOMEM;
479 goto finish;
480 }
481
482 free(s->pid_file);
483 s->pid_file = fn;
484 }
485
486 } else if (state == DESCRIPTION) {
487
488 /* Try to parse Red Hat style description
489 * continuation */
490
491 size_t k = strlen(t);
492 char *d;
493
494 if (t[k-1] == '\\')
495 t[k-1] = 0;
496 else
497 state = NORMAL;
498
499 assert(u->meta.description);
500 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
501 r = -ENOMEM;
502 goto finish;
503 }
504
505 free(u->meta.description);
506 u->meta.description = d;
507
508 } else if (state == LSB || state == LSB_DESCRIPTION) {
509
510 if (startswith_no_case(t, "Provides:")) {
511 char *i, *w;
512 size_t z;
513
514 state = LSB;
515
516 FOREACH_WORD(w, z, t+9, i) {
517 char *n, *m;
518
519 if (!(n = strndup(w, z))) {
520 r = -ENOMEM;
521 goto finish;
522 }
523
524 r = sysv_translate_facility(n, &m);
525 free(n);
526
527 if (r < 0)
528 goto finish;
529
530 if (r == 0)
531 continue;
532
533 if (unit_name_to_type(m) == UNIT_SERVICE)
534 r = unit_add_name(u, m);
535 else {
536 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
537 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
538 }
539
540 free(m);
541
542 if (r < 0)
543 goto finish;
544 }
545
546 } else if (startswith_no_case(t, "Required-Start:") ||
547 startswith_no_case(t, "Should-Start:") ||
548 startswith_no_case(t, "X-Start-Before:") ||
549 startswith_no_case(t, "X-Start-After:")) {
550 char *i, *w;
551 size_t z;
552
553 state = LSB;
554
555 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
556 char *n, *m;
557
558 if (!(n = strndup(w, z))) {
559 r = -ENOMEM;
560 goto finish;
561 }
562
563 r = sysv_translate_facility(n, &m);
564 free(n);
565
566 if (r < 0)
567 goto finish;
568
569 if (r == 0)
570 continue;
571
572 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
573 free(m);
574
575 if (r < 0)
576 goto finish;
577 }
578 } else if (startswith_no_case(t, "Default-Start:")) {
579 char *k, *d;
580
581 state = LSB;
582
583 k = delete_chars(t+14, WHITESPACE "-");
584
585 if (k[0] != 0) {
586 if (!(d = strdup(k))) {
587 r = -ENOMEM;
588 goto finish;
589 }
590
591 free(s->sysv_runlevels);
592 s->sysv_runlevels = d;
593 }
594
595 } else if (startswith_no_case(t, "Description:")) {
596 char *d;
597
598 state = LSB_DESCRIPTION;
599
600 if (!(d = strdup(strstrip(t+12)))) {
601 r = -ENOMEM;
602 goto finish;
603 }
604
605 free(u->meta.description);
606 u->meta.description = d;
607
608 } else if (startswith_no_case(t, "Short-Description:") &&
609 !u->meta.description) {
610 char *d;
611
612 /* We use the short description only
613 * if no long description is set. */
614
615 state = LSB;
616
617 if (!(d = strdup(strstrip(t+18)))) {
618 r = -ENOMEM;
619 goto finish;
620 }
621
622 u->meta.description = d;
623
624 } else if (startswith_no_case(t, "X-Interactive:")) {
625 int b;
626
627 if ((b = parse_boolean(strstrip(t+14))) < 0) {
628 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
629 continue;
630 }
631
632 if (b)
633 s->exec_context.std_input = EXEC_INPUT_TTY;
634 else
635 s->exec_context.std_input = EXEC_INPUT_NULL;
636
637 } else if (state == LSB_DESCRIPTION) {
638
639 if (startswith(l, "#\t") || startswith(l, "# ")) {
640 char *d;
641
642 assert(u->meta.description);
643 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
644 r = -ENOMEM;
645 goto finish;
646 }
647
648 free(u->meta.description);
649 u->meta.description = d;
650 } else
651 state = LSB;
652 }
653 }
654 }
655
656 if ((r = sysv_exec_commands(s)) < 0)
657 goto finish;
658
659 if (!s->sysv_runlevels || chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
660 /* If there a runlevels configured for this service
661 * but none of the standard ones, then we assume this
662 * is some special kind of service (which might be
663 * needed for early boot) and don't create any links
664 * to it. */
665
666 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
667 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
668 goto finish;
669
670 } else
671 /* Don't timeout special services during boot (like fsck) */
672 s->timeout_usec = 0;
673
674 /* Special setting for all SysV services */
675 s->type = SERVICE_FORKING;
676 s->valid_no_process = true;
677 s->kill_mode = KILL_PROCESS_GROUP;
678 s->restart = SERVICE_ONCE;
679
680 u->meta.load_state = UNIT_LOADED;
681 r = 0;
682
683 finish:
684
685 if (f)
686 fclose(f);
687
688 return r;
689 }
690
691 static int service_load_sysv_name(Service *s, const char *name) {
692 char **p;
693
694 assert(s);
695 assert(name);
696
697 /* For SysV services we strip the boot. or .sh
698 * prefixes/suffixes. */
699 if (startswith(name, "boot.") ||
700 endswith(name, ".sh.service"))
701 return -ENOENT;
702
703 STRV_FOREACH(p, UNIT(s)->meta.manager->lookup_paths.sysvinit_path) {
704 char *path;
705 int r;
706
707 if (asprintf(&path, "%s/%s", *p, name) < 0)
708 return -ENOMEM;
709
710 assert(endswith(path, ".service"));
711 path[strlen(path)-8] = 0;
712
713 r = service_load_sysv_path(s, path);
714
715 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
716 /* Try Debian style xxx.sh source'able init scripts */
717 strcat(path, ".sh");
718 r = service_load_sysv_path(s, path);
719 }
720
721 free(path);
722
723 if (r >= 0 && UNIT(s)->meta.load_state == UNIT_STUB) {
724 /* Try SUSE style boot.xxx init scripts */
725
726 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
727 return -ENOMEM;
728
729 path[strlen(path)-8] = 0;
730 r = service_load_sysv_path(s, path);
731 free(path);
732 }
733
734 if (r < 0)
735 return r;
736
737 if ((UNIT(s)->meta.load_state != UNIT_STUB))
738 break;
739 }
740
741 return 0;
742 }
743
744 static int service_load_sysv(Service *s) {
745 const char *t;
746 Iterator i;
747 int r;
748
749 assert(s);
750
751 /* Load service data from SysV init scripts, preferably with
752 * LSB headers ... */
753
754 if (strv_isempty(UNIT(s)->meta.manager->lookup_paths.sysvinit_path))
755 return 0;
756
757 if ((t = UNIT(s)->meta.id))
758 if ((r = service_load_sysv_name(s, t)) < 0)
759 return r;
760
761 if (UNIT(s)->meta.load_state == UNIT_STUB)
762 SET_FOREACH(t, UNIT(s)->meta.names, i) {
763 if (t == UNIT(s)->meta.id)
764 continue;
765
766 if ((r == service_load_sysv_name(s, t)) < 0)
767 return r;
768
769 if (UNIT(s)->meta.load_state != UNIT_STUB)
770 break;
771 }
772
773 return 0;
774 }
775
776 static int service_add_bus_name(Service *s) {
777 char *n;
778 int r;
779
780 assert(s);
781 assert(s->bus_name);
782
783 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
784 return 0;
785
786 r = unit_merge_by_name(UNIT(s), n);
787 free(n);
788
789 return r;
790 }
791
792 static int service_verify(Service *s) {
793 assert(s);
794
795 if (UNIT(s)->meta.load_state != UNIT_LOADED)
796 return 0;
797
798 if (!s->exec_command[SERVICE_EXEC_START]) {
799 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
800 return -EINVAL;
801 }
802
803 if (s->type == SERVICE_DBUS && !s->bus_name) {
804 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
805 return -EINVAL;
806 }
807
808 return 0;
809 }
810
811 static int service_load(Unit *u) {
812 int r;
813 Service *s = SERVICE(u);
814
815 assert(s);
816
817 /* Load a .service file */
818 if ((r = unit_load_fragment(u)) < 0)
819 return r;
820
821 /* Load a classic init script as a fallback, if we couldn't find anything */
822 if (u->meta.load_state == UNIT_STUB)
823 if ((r = service_load_sysv(s)) < 0)
824 return r;
825
826 /* Still nothing found? Then let's give up */
827 if (u->meta.load_state == UNIT_STUB)
828 return -ENOENT;
829
830 /* We were able to load something, then let's add in the
831 * dropin directories. */
832 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
833 return r;
834
835 /* This is a new unit? Then let's add in some extras */
836 if (u->meta.load_state == UNIT_LOADED) {
837 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
838 return r;
839
840 if ((r = unit_add_default_cgroup(u)) < 0)
841 return r;
842
843 if ((r = sysv_fix_order(s)) < 0)
844 return r;
845
846 if (s->bus_name) {
847 if ((r = service_add_bus_name(s)) < 0)
848 return r;
849
850 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
851 return r;
852 }
853
854 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
855 s->notify_access = NOTIFY_MAIN;
856 }
857
858 return service_verify(s);
859 }
860
861 static void service_dump(Unit *u, FILE *f, const char *prefix) {
862
863 ServiceExecCommand c;
864 Service *s = SERVICE(u);
865 const char *prefix2;
866 char *p2;
867
868 assert(s);
869
870 p2 = strappend(prefix, "\t");
871 prefix2 = p2 ? p2 : prefix;
872
873 fprintf(f,
874 "%sService State: %s\n"
875 "%sPermissionsStartOnly: %s\n"
876 "%sRootDirectoryStartOnly: %s\n"
877 "%sValidNoProcess: %s\n"
878 "%sKillMode: %s\n"
879 "%sType: %s\n"
880 "%sNotifyAccess: %s\n",
881 prefix, service_state_to_string(s->state),
882 prefix, yes_no(s->permissions_start_only),
883 prefix, yes_no(s->root_directory_start_only),
884 prefix, yes_no(s->valid_no_process),
885 prefix, kill_mode_to_string(s->kill_mode),
886 prefix, service_type_to_string(s->type),
887 prefix, notify_access_to_string(s->notify_access));
888
889 if (s->control_pid > 0)
890 fprintf(f,
891 "%sControl PID: %llu\n",
892 prefix, (unsigned long long) s->control_pid);
893
894 if (s->main_pid > 0)
895 fprintf(f,
896 "%sMain PID: %llu\n",
897 prefix, (unsigned long long) s->main_pid);
898
899 if (s->pid_file)
900 fprintf(f,
901 "%sPIDFile: %s\n",
902 prefix, s->pid_file);
903
904 if (s->bus_name)
905 fprintf(f,
906 "%sBusName: %s\n"
907 "%sBus Name Good: %s\n",
908 prefix, s->bus_name,
909 prefix, yes_no(s->bus_name_good));
910
911 exec_context_dump(&s->exec_context, f, prefix);
912
913 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
914
915 if (!s->exec_command[c])
916 continue;
917
918 fprintf(f, "%s-> %s:\n",
919 prefix, service_exec_command_to_string(c));
920
921 exec_command_dump_list(s->exec_command[c], f, prefix2);
922 }
923
924 if (s->sysv_path)
925 fprintf(f,
926 "%sSysV Init Script Path: %s\n"
927 "%sSysV Init Script has LSB Header: %s\n",
928 prefix, s->sysv_path,
929 prefix, yes_no(s->sysv_has_lsb));
930
931 if (s->sysv_start_priority >= 0)
932 fprintf(f,
933 "%sSysVStartPriority: %i\n",
934 prefix, s->sysv_start_priority);
935
936 if (s->sysv_runlevels)
937 fprintf(f, "%sSysVRunLevels: %s\n",
938 prefix, s->sysv_runlevels);
939
940 if (s->status_text)
941 fprintf(f, "%sStatus Text: %s\n",
942 prefix, s->status_text);
943
944 free(p2);
945 }
946
947 static int service_load_pid_file(Service *s) {
948 char *k;
949 int r;
950 pid_t pid;
951
952 assert(s);
953
954 if (s->main_pid_known)
955 return 0;
956
957 assert(s->main_pid <= 0);
958
959 if (!s->pid_file)
960 return -ENOENT;
961
962 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
963 return r;
964
965 r = parse_pid(k, &pid);
966 free(k);
967
968 if (r < 0)
969 return r;
970
971 if (kill(pid, 0) < 0 && errno != EPERM) {
972 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
973 (unsigned long) pid, s->pid_file);
974 return -ESRCH;
975 }
976
977 if ((r = service_set_main_pid(s, pid)) < 0)
978 return r;
979
980 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
981 /* FIXME: we need to do something here */
982 return r;
983
984 return 0;
985 }
986
987 static int service_get_sockets(Service *s, Set **_set) {
988 Set *set;
989 Iterator i;
990 char *t;
991 int r;
992
993 assert(s);
994 assert(_set);
995
996 /* Collects all Socket objects that belong to this
997 * service. Note that a service might have multiple sockets
998 * via multiple names. */
999
1000 if (!(set = set_new(NULL, NULL)))
1001 return -ENOMEM;
1002
1003 SET_FOREACH(t, UNIT(s)->meta.names, i) {
1004 char *k;
1005 Unit *p;
1006
1007 /* Look for all socket objects that go by any of our
1008 * units and collect their fds */
1009
1010 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1011 r = -ENOMEM;
1012 goto fail;
1013 }
1014
1015 p = manager_get_unit(UNIT(s)->meta.manager, k);
1016 free(k);
1017
1018 if (!p)
1019 continue;
1020
1021 if ((r = set_put(set, p)) < 0)
1022 goto fail;
1023 }
1024
1025 *_set = set;
1026 return 0;
1027
1028 fail:
1029 set_free(set);
1030 return r;
1031 }
1032
1033 static int service_notify_sockets_dead(Service *s) {
1034 Iterator i;
1035 Set *set;
1036 Socket *sock;
1037 int r;
1038
1039 assert(s);
1040
1041 /* Notifies all our sockets when we die */
1042 if ((r = service_get_sockets(s, &set)) < 0)
1043 return r;
1044
1045 SET_FOREACH(sock, set, i)
1046 socket_notify_service_dead(sock);
1047
1048 set_free(set);
1049
1050 return 0;
1051 }
1052
1053 static void service_set_state(Service *s, ServiceState state) {
1054 ServiceState old_state;
1055 assert(s);
1056
1057 old_state = s->state;
1058 s->state = state;
1059
1060 if (state != SERVICE_START_PRE &&
1061 state != SERVICE_START &&
1062 state != SERVICE_START_POST &&
1063 state != SERVICE_RELOAD &&
1064 state != SERVICE_STOP &&
1065 state != SERVICE_STOP_SIGTERM &&
1066 state != SERVICE_STOP_SIGKILL &&
1067 state != SERVICE_STOP_POST &&
1068 state != SERVICE_FINAL_SIGTERM &&
1069 state != SERVICE_FINAL_SIGKILL &&
1070 state != SERVICE_AUTO_RESTART)
1071 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1072
1073 if (state != SERVICE_START &&
1074 state != SERVICE_START_POST &&
1075 state != SERVICE_RUNNING &&
1076 state != SERVICE_RELOAD &&
1077 state != SERVICE_STOP &&
1078 state != SERVICE_STOP_SIGTERM &&
1079 state != SERVICE_STOP_SIGKILL)
1080 service_unwatch_main_pid(s);
1081
1082 if (state != SERVICE_START_PRE &&
1083 state != SERVICE_START &&
1084 state != SERVICE_START_POST &&
1085 state != SERVICE_RELOAD &&
1086 state != SERVICE_STOP &&
1087 state != SERVICE_STOP_SIGTERM &&
1088 state != SERVICE_STOP_SIGKILL &&
1089 state != SERVICE_STOP_POST &&
1090 state != SERVICE_FINAL_SIGTERM &&
1091 state != SERVICE_FINAL_SIGKILL) {
1092 service_unwatch_control_pid(s);
1093 s->control_command = NULL;
1094 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1095 }
1096
1097 if (state == SERVICE_DEAD ||
1098 state == SERVICE_STOP ||
1099 state == SERVICE_STOP_SIGTERM ||
1100 state == SERVICE_STOP_SIGKILL ||
1101 state == SERVICE_STOP_POST ||
1102 state == SERVICE_FINAL_SIGTERM ||
1103 state == SERVICE_FINAL_SIGKILL ||
1104 state == SERVICE_MAINTENANCE ||
1105 state == SERVICE_AUTO_RESTART)
1106 service_notify_sockets_dead(s);
1107
1108 if (state != SERVICE_START_PRE &&
1109 state != SERVICE_START &&
1110 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1111 service_close_socket_fd(s);
1112
1113 if (old_state != state)
1114 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1115
1116 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1117 }
1118
1119 static int service_coldplug(Unit *u) {
1120 Service *s = SERVICE(u);
1121 int r;
1122
1123 assert(s);
1124 assert(s->state == SERVICE_DEAD);
1125
1126 if (s->deserialized_state != s->state) {
1127
1128 if (s->deserialized_state == SERVICE_START_PRE ||
1129 s->deserialized_state == SERVICE_START ||
1130 s->deserialized_state == SERVICE_START_POST ||
1131 s->deserialized_state == SERVICE_RELOAD ||
1132 s->deserialized_state == SERVICE_STOP ||
1133 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1134 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1135 s->deserialized_state == SERVICE_STOP_POST ||
1136 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1137 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1138 s->deserialized_state == SERVICE_AUTO_RESTART) {
1139
1140 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1141 usec_t k;
1142
1143 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1144
1145 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1146 return r;
1147 }
1148 }
1149
1150 if ((s->deserialized_state == SERVICE_START &&
1151 (s->type == SERVICE_FORKING ||
1152 s->type == SERVICE_DBUS ||
1153 s->type == SERVICE_FINISH ||
1154 s->type == SERVICE_NOTIFY)) ||
1155 s->deserialized_state == SERVICE_START_POST ||
1156 s->deserialized_state == SERVICE_RUNNING ||
1157 s->deserialized_state == SERVICE_RELOAD ||
1158 s->deserialized_state == SERVICE_STOP ||
1159 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1160 s->deserialized_state == SERVICE_STOP_SIGKILL)
1161 if (s->main_pid > 0)
1162 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1163 return r;
1164
1165 if (s->deserialized_state == SERVICE_START_PRE ||
1166 s->deserialized_state == SERVICE_START ||
1167 s->deserialized_state == SERVICE_START_POST ||
1168 s->deserialized_state == SERVICE_RELOAD ||
1169 s->deserialized_state == SERVICE_STOP ||
1170 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1171 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1172 s->deserialized_state == SERVICE_STOP_POST ||
1173 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1174 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1175 if (s->control_pid > 0)
1176 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1177 return r;
1178
1179 service_set_state(s, s->deserialized_state);
1180 }
1181
1182 return 0;
1183 }
1184
1185 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1186 Iterator i;
1187 int r;
1188 int *rfds = NULL;
1189 unsigned rn_fds = 0;
1190 Set *set;
1191 Socket *sock;
1192
1193 assert(s);
1194 assert(fds);
1195 assert(n_fds);
1196
1197 if ((r = service_get_sockets(s, &set)) < 0)
1198 return r;
1199
1200 SET_FOREACH(sock, set, i) {
1201 int *cfds;
1202 unsigned cn_fds;
1203
1204 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1205 goto fail;
1206
1207 if (!cfds)
1208 continue;
1209
1210 if (!rfds) {
1211 rfds = cfds;
1212 rn_fds = cn_fds;
1213 } else {
1214 int *t;
1215
1216 if (!(t = new(int, rn_fds+cn_fds))) {
1217 free(cfds);
1218 r = -ENOMEM;
1219 goto fail;
1220 }
1221
1222 memcpy(t, rfds, rn_fds);
1223 memcpy(t+rn_fds, cfds, cn_fds);
1224 free(rfds);
1225 free(cfds);
1226
1227 rfds = t;
1228 rn_fds = rn_fds+cn_fds;
1229 }
1230 }
1231
1232 *fds = rfds;
1233 *n_fds = rn_fds;
1234
1235 set_free(set);
1236
1237 return 0;
1238
1239 fail:
1240 set_free(set);
1241 free(rfds);
1242
1243 return r;
1244 }
1245
1246 static int service_spawn(
1247 Service *s,
1248 ExecCommand *c,
1249 bool timeout,
1250 bool pass_fds,
1251 bool apply_permissions,
1252 bool apply_chroot,
1253 bool set_notify_socket,
1254 pid_t *_pid) {
1255
1256 pid_t pid;
1257 int r;
1258 int *fds = NULL;
1259 unsigned n_fds = 0;
1260 char **argv = NULL, **env = NULL;
1261
1262 assert(s);
1263 assert(c);
1264 assert(_pid);
1265
1266 if (pass_fds) {
1267 if (s->socket_fd >= 0) {
1268 fds = &s->socket_fd;
1269 n_fds = 1;
1270 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1271 goto fail;
1272 }
1273
1274 if (timeout && s->timeout_usec) {
1275 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1276 goto fail;
1277 } else
1278 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1279
1280 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1281 r = -ENOMEM;
1282 goto fail;
1283 }
1284
1285 if (set_notify_socket) {
1286 char *t;
1287
1288 if (asprintf(&t, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1289 r = -ENOMEM;
1290 goto fail;
1291 }
1292
1293 env = strv_env_set(s->meta.manager->environment, t);
1294 free(t);
1295
1296 if (!env) {
1297 r = -ENOMEM;
1298 goto fail;
1299 }
1300 } else
1301 env = s->meta.manager->environment;
1302
1303 r = exec_spawn(c,
1304 argv,
1305 &s->exec_context,
1306 fds, n_fds,
1307 env,
1308 apply_permissions,
1309 apply_chroot,
1310 UNIT(s)->meta.manager->confirm_spawn,
1311 UNIT(s)->meta.cgroup_bondings,
1312 &pid);
1313
1314 strv_free(argv);
1315 argv = NULL;
1316
1317 if (set_notify_socket)
1318 strv_free(env);
1319 env = NULL;
1320
1321 if (r < 0)
1322 goto fail;
1323
1324 if (fds) {
1325 if (s->socket_fd >= 0)
1326 service_close_socket_fd(s);
1327 else
1328 free(fds);
1329 }
1330
1331 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1332 /* FIXME: we need to do something here */
1333 goto fail;
1334
1335 *_pid = pid;
1336
1337 return 0;
1338
1339 fail:
1340 free(fds);
1341
1342 strv_free(argv);
1343
1344 if (set_notify_socket)
1345 strv_free(env);
1346
1347 if (timeout)
1348 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1349
1350 return r;
1351 }
1352
1353 static int main_pid_good(Service *s) {
1354 assert(s);
1355
1356 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1357 * don't know */
1358
1359 /* If we know the pid file, then lets just check if it is
1360 * still valid */
1361 if (s->main_pid_known)
1362 return s->main_pid > 0;
1363
1364 /* We don't know the pid */
1365 return -EAGAIN;
1366 }
1367
1368 static int control_pid_good(Service *s) {
1369 assert(s);
1370
1371 return s->control_pid > 0;
1372 }
1373
1374 static int cgroup_good(Service *s) {
1375 int r;
1376
1377 assert(s);
1378
1379 if (s->valid_no_process)
1380 return -EAGAIN;
1381
1382 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1383 return r;
1384
1385 return !r;
1386 }
1387
1388 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1389 int r;
1390 assert(s);
1391
1392 if (!success)
1393 s->failure = true;
1394
1395 if (allow_restart &&
1396 s->allow_restart &&
1397 (s->restart == SERVICE_RESTART_ALWAYS ||
1398 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1399
1400 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1401 goto fail;
1402
1403 service_set_state(s, SERVICE_AUTO_RESTART);
1404 } else
1405 service_set_state(s, s->failure ? SERVICE_MAINTENANCE : SERVICE_DEAD);
1406
1407 return;
1408
1409 fail:
1410 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1411 service_enter_dead(s, false, false);
1412 }
1413
1414 static void service_enter_signal(Service *s, ServiceState state, bool success);
1415
1416 static void service_enter_stop_post(Service *s, bool success) {
1417 int r;
1418 assert(s);
1419
1420 if (!success)
1421 s->failure = true;
1422
1423 service_unwatch_control_pid(s);
1424
1425 s->control_command_id = SERVICE_EXEC_STOP_POST;
1426 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1427 if ((r = service_spawn(s,
1428 s->control_command,
1429 true,
1430 false,
1431 !s->permissions_start_only,
1432 !s->root_directory_start_only,
1433 false,
1434 &s->control_pid)) < 0)
1435 goto fail;
1436
1437
1438 service_set_state(s, SERVICE_STOP_POST);
1439 } else
1440 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1441
1442 return;
1443
1444 fail:
1445 log_warning("%s failed to run 'stop-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1446 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1447 }
1448
1449 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1450 int r;
1451 bool sent = false;
1452
1453 assert(s);
1454
1455 if (!success)
1456 s->failure = true;
1457
1458 if (s->kill_mode != KILL_NONE) {
1459 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1460
1461 if (s->kill_mode == KILL_CONTROL_GROUP) {
1462
1463 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1464 if (r != -EAGAIN && r != -ESRCH)
1465 goto fail;
1466 } else
1467 sent = true;
1468 }
1469
1470 if (!sent) {
1471 r = 0;
1472
1473 if (s->main_pid > 0) {
1474 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1475 r = -errno;
1476 else
1477 sent = true;
1478 }
1479
1480 if (s->control_pid > 0) {
1481 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1482 r = -errno;
1483 else
1484 sent = true;
1485 }
1486
1487 if (r < 0)
1488 goto fail;
1489 }
1490 }
1491
1492 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1493 if (s->timeout_usec > 0)
1494 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1495 goto fail;
1496
1497 service_set_state(s, state);
1498 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1499 service_enter_stop_post(s, true);
1500 else
1501 service_enter_dead(s, true, true);
1502
1503 return;
1504
1505 fail:
1506 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1507
1508 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1509 service_enter_stop_post(s, false);
1510 else
1511 service_enter_dead(s, false, true);
1512 }
1513
1514 static void service_enter_stop(Service *s, bool success) {
1515 int r;
1516
1517 assert(s);
1518
1519 if (!success)
1520 s->failure = true;
1521
1522 service_unwatch_control_pid(s);
1523
1524 s->control_command_id = SERVICE_EXEC_STOP;
1525 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1526 if ((r = service_spawn(s,
1527 s->control_command,
1528 true,
1529 false,
1530 !s->permissions_start_only,
1531 !s->root_directory_start_only,
1532 false,
1533 &s->control_pid)) < 0)
1534 goto fail;
1535
1536 service_set_state(s, SERVICE_STOP);
1537 } else
1538 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1539
1540 return;
1541
1542 fail:
1543 log_warning("%s failed to run 'stop' task: %s", UNIT(s)->meta.id, strerror(-r));
1544 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1545 }
1546
1547 static void service_enter_running(Service *s, bool success) {
1548 assert(s);
1549
1550 if (!success)
1551 s->failure = true;
1552
1553 if (main_pid_good(s) != 0 &&
1554 cgroup_good(s) != 0 &&
1555 (s->bus_name_good || s->type != SERVICE_DBUS))
1556 service_set_state(s, SERVICE_RUNNING);
1557 else if (s->valid_no_process)
1558 service_set_state(s, SERVICE_EXITED);
1559 else
1560 service_enter_stop(s, true);
1561 }
1562
1563 static void service_enter_start_post(Service *s) {
1564 int r;
1565 assert(s);
1566
1567 service_unwatch_control_pid(s);
1568
1569 s->control_command_id = SERVICE_EXEC_START_POST;
1570 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1571 if ((r = service_spawn(s,
1572 s->control_command,
1573 true,
1574 false,
1575 !s->permissions_start_only,
1576 !s->root_directory_start_only,
1577 false,
1578 &s->control_pid)) < 0)
1579 goto fail;
1580
1581 service_set_state(s, SERVICE_START_POST);
1582 } else
1583 service_enter_running(s, true);
1584
1585 return;
1586
1587 fail:
1588 log_warning("%s failed to run 'start-post' task: %s", UNIT(s)->meta.id, strerror(-r));
1589 service_enter_stop(s, false);
1590 }
1591
1592 static void service_enter_start(Service *s) {
1593 pid_t pid;
1594 int r;
1595
1596 assert(s);
1597
1598 assert(s->exec_command[SERVICE_EXEC_START]);
1599 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1600
1601 if (s->type == SERVICE_FORKING)
1602 service_unwatch_control_pid(s);
1603 else
1604 service_unwatch_main_pid(s);
1605
1606 if ((r = service_spawn(s,
1607 s->exec_command[SERVICE_EXEC_START],
1608 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1609 true,
1610 true,
1611 true,
1612 s->notify_access != NOTIFY_NONE,
1613 &pid)) < 0)
1614 goto fail;
1615
1616 if (s->type == SERVICE_SIMPLE) {
1617 /* For simple services we immediately start
1618 * the START_POST binaries. */
1619
1620 service_set_main_pid(s, pid);
1621 service_enter_start_post(s);
1622
1623 } else if (s->type == SERVICE_FORKING) {
1624
1625 /* For forking services we wait until the start
1626 * process exited. */
1627
1628 s->control_command_id = SERVICE_EXEC_START;
1629 s->control_command = s->exec_command[SERVICE_EXEC_START];
1630
1631 s->control_pid = pid;
1632 service_set_state(s, SERVICE_START);
1633
1634 } else if (s->type == SERVICE_FINISH ||
1635 s->type == SERVICE_DBUS ||
1636 s->type == SERVICE_NOTIFY) {
1637
1638 /* For finishing services we wait until the start
1639 * process exited, too, but it is our main process. */
1640
1641 /* For D-Bus services we know the main pid right away,
1642 * but wait for the bus name to appear on the
1643 * bus. Notify services are similar. */
1644
1645 service_set_main_pid(s, pid);
1646 service_set_state(s, SERVICE_START);
1647 } else
1648 assert_not_reached("Unknown service type");
1649
1650 return;
1651
1652 fail:
1653 log_warning("%s failed to run 'start' task: %s", UNIT(s)->meta.id, strerror(-r));
1654 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1655 }
1656
1657 static void service_enter_start_pre(Service *s) {
1658 int r;
1659
1660 assert(s);
1661
1662 service_unwatch_control_pid(s);
1663
1664 s->control_command_id = SERVICE_EXEC_START_PRE;
1665 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1666 if ((r = service_spawn(s,
1667 s->control_command,
1668 true,
1669 false,
1670 !s->permissions_start_only,
1671 !s->root_directory_start_only,
1672 false,
1673 &s->control_pid)) < 0)
1674 goto fail;
1675
1676 service_set_state(s, SERVICE_START_PRE);
1677 } else
1678 service_enter_start(s);
1679
1680 return;
1681
1682 fail:
1683 log_warning("%s failed to run 'start-pre' task: %s", UNIT(s)->meta.id, strerror(-r));
1684 service_enter_dead(s, false, true);
1685 }
1686
1687 static void service_enter_restart(Service *s) {
1688 int r;
1689 assert(s);
1690
1691 service_enter_dead(s, true, false);
1692
1693 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1694 goto fail;
1695
1696 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1697 return;
1698
1699 fail:
1700
1701 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1702 service_enter_dead(s, false, false);
1703 }
1704
1705 static void service_enter_reload(Service *s) {
1706 int r;
1707
1708 assert(s);
1709
1710 service_unwatch_control_pid(s);
1711
1712 s->control_command_id = SERVICE_EXEC_RELOAD;
1713 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1714 if ((r = service_spawn(s,
1715 s->control_command,
1716 true,
1717 false,
1718 !s->permissions_start_only,
1719 !s->root_directory_start_only,
1720 false,
1721 &s->control_pid)) < 0)
1722 goto fail;
1723
1724 service_set_state(s, SERVICE_RELOAD);
1725 } else
1726 service_enter_running(s, true);
1727
1728 return;
1729
1730 fail:
1731 log_warning("%s failed to run 'reload' task: %s", UNIT(s)->meta.id, strerror(-r));
1732 service_enter_stop(s, false);
1733 }
1734
1735 static void service_run_next(Service *s, bool success) {
1736 int r;
1737
1738 assert(s);
1739 assert(s->control_command);
1740 assert(s->control_command->command_next);
1741
1742 if (!success)
1743 s->failure = true;
1744
1745 s->control_command = s->control_command->command_next;
1746
1747 service_unwatch_control_pid(s);
1748
1749 if ((r = service_spawn(s,
1750 s->control_command,
1751 true,
1752 false,
1753 !s->permissions_start_only,
1754 !s->root_directory_start_only,
1755 false,
1756 &s->control_pid)) < 0)
1757 goto fail;
1758
1759 return;
1760
1761 fail:
1762 log_warning("%s failed to run spawn next task: %s", UNIT(s)->meta.id, strerror(-r));
1763
1764 if (s->state == SERVICE_START_PRE)
1765 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1766 else if (s->state == SERVICE_STOP)
1767 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1768 else if (s->state == SERVICE_STOP_POST)
1769 service_enter_dead(s, false, true);
1770 else
1771 service_enter_stop(s, false);
1772 }
1773
1774 static int service_start(Unit *u) {
1775 Service *s = SERVICE(u);
1776
1777 assert(s);
1778
1779 /* We cannot fulfill this request right now, try again later
1780 * please! */
1781 if (s->state == SERVICE_STOP ||
1782 s->state == SERVICE_STOP_SIGTERM ||
1783 s->state == SERVICE_STOP_SIGKILL ||
1784 s->state == SERVICE_STOP_POST ||
1785 s->state == SERVICE_FINAL_SIGTERM ||
1786 s->state == SERVICE_FINAL_SIGKILL)
1787 return -EAGAIN;
1788
1789 /* Already on it! */
1790 if (s->state == SERVICE_START_PRE ||
1791 s->state == SERVICE_START ||
1792 s->state == SERVICE_START_POST)
1793 return 0;
1794
1795 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTENANCE || s->state == SERVICE_AUTO_RESTART);
1796
1797 /* Make sure we don't enter a busy loop of some kind. */
1798 if (!ratelimit_test(&s->ratelimit)) {
1799 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1800 return -ECANCELED;
1801 }
1802
1803 s->failure = false;
1804 s->main_pid_known = false;
1805 s->allow_restart = true;
1806
1807 service_enter_start_pre(s);
1808 return 0;
1809 }
1810
1811 static int service_stop(Unit *u) {
1812 Service *s = SERVICE(u);
1813
1814 assert(s);
1815
1816 /* Cannot do this now */
1817 if (s->state == SERVICE_START_PRE ||
1818 s->state == SERVICE_START ||
1819 s->state == SERVICE_START_POST ||
1820 s->state == SERVICE_RELOAD)
1821 return -EAGAIN;
1822
1823 /* Already on it */
1824 if (s->state == SERVICE_STOP ||
1825 s->state == SERVICE_STOP_SIGTERM ||
1826 s->state == SERVICE_STOP_SIGKILL ||
1827 s->state == SERVICE_STOP_POST ||
1828 s->state == SERVICE_FINAL_SIGTERM ||
1829 s->state == SERVICE_FINAL_SIGKILL)
1830 return 0;
1831
1832 if (s->state == SERVICE_AUTO_RESTART) {
1833 service_set_state(s, SERVICE_DEAD);
1834 return 0;
1835 }
1836
1837 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1838
1839 /* This is a user request, so don't do restarts on this
1840 * shutdown. */
1841 s->allow_restart = false;
1842
1843 service_enter_stop(s, true);
1844 return 0;
1845 }
1846
1847 static int service_reload(Unit *u) {
1848 Service *s = SERVICE(u);
1849
1850 assert(s);
1851
1852 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1853
1854 service_enter_reload(s);
1855 return 0;
1856 }
1857
1858 static bool service_can_reload(Unit *u) {
1859 Service *s = SERVICE(u);
1860
1861 assert(s);
1862
1863 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1864 }
1865
1866 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1867 Service *s = SERVICE(u);
1868
1869 assert(u);
1870 assert(f);
1871 assert(fds);
1872
1873 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1874 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1875
1876 if (s->control_pid > 0)
1877 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
1878
1879 if (s->main_pid_known && s->main_pid > 0)
1880 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
1881
1882 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1883
1884 /* There's a minor uncleanliness here: if there are multiple
1885 * commands attached here, we will start from the first one
1886 * again */
1887 if (s->control_command_id >= 0)
1888 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1889
1890 if (s->socket_fd >= 0) {
1891 int copy;
1892
1893 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1894 return copy;
1895
1896 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1897 }
1898
1899 return 0;
1900 }
1901
1902 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1903 Service *s = SERVICE(u);
1904 int r;
1905
1906 assert(u);
1907 assert(key);
1908 assert(value);
1909 assert(fds);
1910
1911 if (streq(key, "state")) {
1912 ServiceState state;
1913
1914 if ((state = service_state_from_string(value)) < 0)
1915 log_debug("Failed to parse state value %s", value);
1916 else
1917 s->deserialized_state = state;
1918 } else if (streq(key, "failure")) {
1919 int b;
1920
1921 if ((b = parse_boolean(value)) < 0)
1922 log_debug("Failed to parse failure value %s", value);
1923 else
1924 s->failure = b || s->failure;
1925 } else if (streq(key, "control-pid")) {
1926 pid_t pid;
1927
1928 if ((r = parse_pid(value, &pid)) < 0)
1929 log_debug("Failed to parse control-pid value %s", value);
1930 else
1931 s->control_pid = pid;
1932 } else if (streq(key, "main-pid")) {
1933 pid_t pid;
1934
1935 if ((r = parse_pid(value, &pid)) < 0)
1936 log_debug("Failed to parse main-pid value %s", value);
1937 else
1938 service_set_main_pid(s, (pid_t) pid);
1939 } else if (streq(key, "main-pid-known")) {
1940 int b;
1941
1942 if ((b = parse_boolean(value)) < 0)
1943 log_debug("Failed to parse main-pid-known value %s", value);
1944 else
1945 s->main_pid_known = b;
1946 } else if (streq(key, "control-command")) {
1947 ServiceExecCommand id;
1948
1949 if ((id = service_exec_command_from_string(value)) < 0)
1950 log_debug("Failed to parse exec-command value %s", value);
1951 else {
1952 s->control_command_id = id;
1953 s->control_command = s->exec_command[id];
1954 }
1955 } else if (streq(key, "socket-fd")) {
1956 int fd;
1957
1958 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1959 log_debug("Failed to parse socket-fd value %s", value);
1960 else {
1961
1962 if (s->socket_fd >= 0)
1963 close_nointr_nofail(s->socket_fd);
1964 s->socket_fd = fdset_remove(fds, fd);
1965 }
1966 } else
1967 log_debug("Unknown serialization key '%s'", key);
1968
1969 return 0;
1970 }
1971
1972 static UnitActiveState service_active_state(Unit *u) {
1973 assert(u);
1974
1975 return state_translation_table[SERVICE(u)->state];
1976 }
1977
1978 static const char *service_sub_state_to_string(Unit *u) {
1979 assert(u);
1980
1981 return service_state_to_string(SERVICE(u)->state);
1982 }
1983
1984 static bool service_check_gc(Unit *u) {
1985 Service *s = SERVICE(u);
1986
1987 assert(s);
1988
1989 return !!s->sysv_path;
1990 }
1991
1992 static bool service_check_snapshot(Unit *u) {
1993 Service *s = SERVICE(u);
1994
1995 assert(s);
1996
1997 return !s->got_socket_fd;
1998 }
1999
2000 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2001 Service *s = SERVICE(u);
2002 bool success;
2003
2004 assert(s);
2005 assert(pid >= 0);
2006
2007 success = is_clean_exit(code, status);
2008 s->failure = s->failure || !success;
2009
2010 if (s->main_pid == pid) {
2011
2012 exec_status_fill(&s->main_exec_status, pid, code, status);
2013 s->main_pid = 0;
2014
2015 if (s->type != SERVICE_FORKING) {
2016 assert(s->exec_command[SERVICE_EXEC_START]);
2017 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
2018 }
2019
2020 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2021
2022 /* The service exited, so the service is officially
2023 * gone. */
2024
2025 switch (s->state) {
2026
2027 case SERVICE_START_POST:
2028 case SERVICE_RELOAD:
2029 case SERVICE_STOP:
2030 /* Need to wait until the operation is
2031 * done */
2032 break;
2033
2034 case SERVICE_START:
2035 if (s->type == SERVICE_FINISH) {
2036 /* This was our main goal, so let's go on */
2037 if (success)
2038 service_enter_start_post(s);
2039 else
2040 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2041 break;
2042 } else {
2043 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2044
2045 /* Fall through */
2046 }
2047
2048 case SERVICE_RUNNING:
2049 service_enter_running(s, success);
2050 break;
2051
2052 case SERVICE_STOP_SIGTERM:
2053 case SERVICE_STOP_SIGKILL:
2054
2055 if (!control_pid_good(s))
2056 service_enter_stop_post(s, success);
2057
2058 /* If there is still a control process, wait for that first */
2059 break;
2060
2061 default:
2062 assert_not_reached("Uh, main process died at wrong time.");
2063 }
2064
2065 } else if (s->control_pid == pid) {
2066
2067 if (s->control_command)
2068 exec_status_fill(&s->control_command->exec_status, pid, code, status);
2069
2070 s->control_pid = 0;
2071
2072 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2073
2074 /* If we are shutting things down anyway we
2075 * don't care about failing commands. */
2076
2077 if (s->control_command && s->control_command->command_next && success) {
2078
2079 /* There is another command to *
2080 * execute, so let's do that. */
2081
2082 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2083 service_run_next(s, success);
2084
2085 } else {
2086 /* No further commands for this step, so let's
2087 * figure out what to do next */
2088
2089 s->control_command = NULL;
2090 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2091
2092 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2093
2094 switch (s->state) {
2095
2096 case SERVICE_START_PRE:
2097 if (success)
2098 service_enter_start(s);
2099 else
2100 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2101 break;
2102
2103 case SERVICE_START:
2104 assert(s->type == SERVICE_FORKING);
2105
2106 /* Let's try to load the pid
2107 * file here if we can. We
2108 * ignore the return value,
2109 * since the PID file might
2110 * actually be created by a
2111 * START_POST script */
2112
2113 if (success) {
2114 if (s->pid_file)
2115 service_load_pid_file(s);
2116
2117 service_enter_start_post(s);
2118 } else
2119 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2120
2121 break;
2122
2123 case SERVICE_START_POST:
2124 if (success && s->pid_file && !s->main_pid_known) {
2125 int r;
2126
2127 /* Hmm, let's see if we can
2128 * load the pid now after the
2129 * start-post scripts got
2130 * executed. */
2131
2132 if ((r = service_load_pid_file(s)) < 0)
2133 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2134 }
2135
2136 /* Fall through */
2137
2138 case SERVICE_RELOAD:
2139 if (success)
2140 service_enter_running(s, true);
2141 else
2142 service_enter_stop(s, false);
2143
2144 break;
2145
2146 case SERVICE_STOP:
2147 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2148 break;
2149
2150 case SERVICE_STOP_SIGTERM:
2151 case SERVICE_STOP_SIGKILL:
2152 if (main_pid_good(s) <= 0)
2153 service_enter_stop_post(s, success);
2154
2155 /* If there is still a service
2156 * process around, wait until
2157 * that one quit, too */
2158 break;
2159
2160 case SERVICE_STOP_POST:
2161 case SERVICE_FINAL_SIGTERM:
2162 case SERVICE_FINAL_SIGKILL:
2163 service_enter_dead(s, success, true);
2164 break;
2165
2166 default:
2167 assert_not_reached("Uh, control process died at wrong time.");
2168 }
2169 }
2170 }
2171 }
2172
2173 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2174 Service *s = SERVICE(u);
2175
2176 assert(s);
2177 assert(elapsed == 1);
2178
2179 assert(w == &s->timer_watch);
2180
2181 switch (s->state) {
2182
2183 case SERVICE_START_PRE:
2184 case SERVICE_START:
2185 log_warning("%s operation timed out. Terminating.", u->meta.id);
2186 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2187 break;
2188
2189 case SERVICE_START_POST:
2190 case SERVICE_RELOAD:
2191 log_warning("%s operation timed out. Stopping.", u->meta.id);
2192 service_enter_stop(s, false);
2193 break;
2194
2195 case SERVICE_STOP:
2196 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2197 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2198 break;
2199
2200 case SERVICE_STOP_SIGTERM:
2201 log_warning("%s stopping timed out. Killing.", u->meta.id);
2202 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2203 break;
2204
2205 case SERVICE_STOP_SIGKILL:
2206 /* Uh, wie sent a SIGKILL and it is still not gone?
2207 * Must be something we cannot kill, so let's just be
2208 * weirded out and continue */
2209
2210 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2211 service_enter_stop_post(s, false);
2212 break;
2213
2214 case SERVICE_STOP_POST:
2215 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2216 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2217 break;
2218
2219 case SERVICE_FINAL_SIGTERM:
2220 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2221 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2222 break;
2223
2224 case SERVICE_FINAL_SIGKILL:
2225 log_warning("%s still around after SIGKILL (2). Entering maintenance mode.", u->meta.id);
2226 service_enter_dead(s, false, true);
2227 break;
2228
2229 case SERVICE_AUTO_RESTART:
2230 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2231 service_enter_restart(s);
2232 break;
2233
2234 default:
2235 assert_not_reached("Timeout at wrong time.");
2236 }
2237 }
2238
2239 static void service_cgroup_notify_event(Unit *u) {
2240 Service *s = SERVICE(u);
2241
2242 assert(u);
2243
2244 log_debug("%s: cgroup is empty", u->meta.id);
2245
2246 switch (s->state) {
2247
2248 /* Waiting for SIGCHLD is usually more interesting,
2249 * because it includes return codes/signals. Which is
2250 * why we ignore the cgroup events for most cases,
2251 * except when we don't know pid which to expect the
2252 * SIGCHLD for. */
2253
2254 case SERVICE_RUNNING:
2255 service_enter_running(s, true);
2256 break;
2257
2258 default:
2259 ;
2260 }
2261 }
2262
2263 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2264 Service *s = SERVICE(u);
2265 const char *e;
2266
2267 assert(u);
2268
2269 if (s->notify_access == NOTIFY_NONE) {
2270 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2271 u->meta.id, (unsigned long) pid);
2272 return;
2273 }
2274
2275 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2276 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2277 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2278 return;
2279 }
2280
2281 log_debug("%s: Got message", u->meta.id);
2282
2283 /* Interpret MAINPID= */
2284 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2285 (s->state == SERVICE_START ||
2286 s->state == SERVICE_START_POST ||
2287 s->state == SERVICE_RUNNING ||
2288 s->state == SERVICE_RELOAD)) {
2289
2290 if (parse_pid(e + 8, &pid) < 0)
2291 log_warning("Failed to parse %s", e);
2292 else {
2293 log_debug("%s: got %s", u->meta.id, e);
2294 service_set_main_pid(s, pid);
2295 }
2296 }
2297
2298 /* Interpret READY= */
2299 if (s->type == SERVICE_NOTIFY &&
2300 s->state == SERVICE_START &&
2301 strv_find(tags, "READY=1")) {
2302 log_debug("%s: got READY=1", u->meta.id);
2303
2304 service_enter_start_post(s);
2305 }
2306
2307 /* Interpret STATUS= */
2308 if ((e = strv_find_prefix(tags, "STATUS="))) {
2309 char *t;
2310
2311 if (!(t = strdup(e+7))) {
2312 log_error("Failed to allocate string.");
2313 return;
2314 }
2315
2316 log_debug("%s: got %s", u->meta.id, e);
2317
2318 free(s->status_text);
2319 s->status_text = t;
2320 }
2321 }
2322
2323 static int service_enumerate(Manager *m) {
2324 char **p;
2325 unsigned i;
2326 DIR *d = NULL;
2327 char *path = NULL, *fpath = NULL, *name = NULL;
2328 int r;
2329
2330 assert(m);
2331
2332 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2333 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2334 struct dirent *de;
2335
2336 free(path);
2337 path = NULL;
2338 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2339 r = -ENOMEM;
2340 goto finish;
2341 }
2342
2343 if (d)
2344 closedir(d);
2345
2346 if (!(d = opendir(path))) {
2347 if (errno != ENOENT)
2348 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2349
2350 continue;
2351 }
2352
2353 while ((de = readdir(d))) {
2354 Unit *service;
2355 int a, b;
2356
2357 if (ignore_file(de->d_name))
2358 continue;
2359
2360 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2361 continue;
2362
2363 if (strlen(de->d_name) < 4)
2364 continue;
2365
2366 a = undecchar(de->d_name[1]);
2367 b = undecchar(de->d_name[2]);
2368
2369 if (a < 0 || b < 0)
2370 continue;
2371
2372 free(fpath);
2373 fpath = NULL;
2374 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2375 r = -ENOMEM;
2376 goto finish;
2377 }
2378
2379 if (access(fpath, X_OK) < 0) {
2380
2381 if (errno != ENOENT)
2382 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2383
2384 continue;
2385 }
2386
2387 free(name);
2388 if (!(name = sysv_translate_name(de->d_name + 3))) {
2389 r = -ENOMEM;
2390 goto finish;
2391 }
2392
2393 if ((r = manager_load_unit_prepare(m, name, NULL, &service)) < 0) {
2394 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2395 continue;
2396 }
2397
2398 if (de->d_name[0] == 'S' &&
2399 (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT))
2400 SERVICE(service)->sysv_start_priority =
2401 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2402
2403 manager_dispatch_load_queue(m);
2404 service = unit_follow_merge(service);
2405
2406 if (de->d_name[0] == 'S') {
2407 Unit *runlevel_target;
2408
2409 if ((r = manager_load_unit(m, rcnd_table[i].target, NULL, &runlevel_target)) < 0)
2410 goto finish;
2411
2412 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2413 goto finish;
2414
2415 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2416 goto finish;
2417
2418 } else if (de->d_name[0] == 'K' &&
2419 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2420 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2421 Unit *shutdown_target;
2422
2423 /* We honour K links only for
2424 * halt/reboot. For the normal
2425 * runlevels we assume the
2426 * stop jobs will be
2427 * implicitly added by the
2428 * core logic. Also, we don't
2429 * really distuingish here
2430 * between the runlevels 0 and
2431 * 6 and just add them to the
2432 * special shutdown target. On
2433 * SUSE the boot.d/ runlevel
2434 * is also used for shutdown,
2435 * so we add links for that
2436 * too to the shutdown
2437 * target.*/
2438
2439 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2440 goto finish;
2441
2442 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2443 goto finish;
2444
2445 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2446 goto finish;
2447 }
2448 }
2449 }
2450
2451 r = 0;
2452
2453 finish:
2454 free(path);
2455 free(fpath);
2456 free(name);
2457
2458 if (d)
2459 closedir(d);
2460
2461 return r;
2462 }
2463
2464 static void service_bus_name_owner_change(
2465 Unit *u,
2466 const char *name,
2467 const char *old_owner,
2468 const char *new_owner) {
2469
2470 Service *s = SERVICE(u);
2471
2472 assert(s);
2473 assert(name);
2474
2475 assert(streq(s->bus_name, name));
2476 assert(old_owner || new_owner);
2477
2478 if (old_owner && new_owner)
2479 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2480 else if (old_owner)
2481 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2482 else
2483 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2484
2485 s->bus_name_good = !!new_owner;
2486
2487 if (s->type == SERVICE_DBUS) {
2488
2489 /* service_enter_running() will figure out what to
2490 * do */
2491 if (s->state == SERVICE_RUNNING)
2492 service_enter_running(s, true);
2493 else if (s->state == SERVICE_START && new_owner)
2494 service_enter_start_post(s);
2495
2496 } else if (new_owner &&
2497 s->main_pid <= 0 &&
2498 (s->state == SERVICE_START ||
2499 s->state == SERVICE_START_POST ||
2500 s->state == SERVICE_RUNNING ||
2501 s->state == SERVICE_RELOAD)) {
2502
2503 /* Try to acquire PID from bus service */
2504 log_debug("Trying to acquire PID from D-Bus name...");
2505
2506 bus_query_pid(u->meta.manager, name);
2507 }
2508 }
2509
2510 static void service_bus_query_pid_done(
2511 Unit *u,
2512 const char *name,
2513 pid_t pid) {
2514
2515 Service *s = SERVICE(u);
2516
2517 assert(s);
2518 assert(name);
2519
2520 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2521
2522 if (s->main_pid <= 0 &&
2523 (s->state == SERVICE_START ||
2524 s->state == SERVICE_START_POST ||
2525 s->state == SERVICE_RUNNING ||
2526 s->state == SERVICE_RELOAD))
2527 service_set_main_pid(s, pid);
2528 }
2529
2530 int service_set_socket_fd(Service *s, int fd) {
2531 assert(s);
2532 assert(fd >= 0);
2533
2534 /* This is called by the socket code when instantiating a new
2535 * service for a stream socket and the socket needs to be
2536 * configured. */
2537
2538 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2539 return -EINVAL;
2540
2541 if (s->socket_fd >= 0)
2542 return -EBUSY;
2543
2544 if (s->state != SERVICE_DEAD)
2545 return -EAGAIN;
2546
2547 s->socket_fd = fd;
2548 s->got_socket_fd = true;
2549 return 0;
2550 }
2551
2552 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2553 [SERVICE_DEAD] = "dead",
2554 [SERVICE_START_PRE] = "start-pre",
2555 [SERVICE_START] = "start",
2556 [SERVICE_START_POST] = "start-post",
2557 [SERVICE_RUNNING] = "running",
2558 [SERVICE_EXITED] = "exited",
2559 [SERVICE_RELOAD] = "reload",
2560 [SERVICE_STOP] = "stop",
2561 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2562 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2563 [SERVICE_STOP_POST] = "stop-post",
2564 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2565 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2566 [SERVICE_MAINTENANCE] = "maintenance",
2567 [SERVICE_AUTO_RESTART] = "auto-restart",
2568 };
2569
2570 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2571
2572 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2573 [SERVICE_ONCE] = "once",
2574 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2575 [SERVICE_RESTART_ALWAYS] = "restart-always",
2576 };
2577
2578 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2579
2580 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2581 [SERVICE_FORKING] = "forking",
2582 [SERVICE_SIMPLE] = "simple",
2583 [SERVICE_FINISH] = "finish",
2584 [SERVICE_DBUS] = "dbus",
2585 [SERVICE_NOTIFY] = "notify"
2586 };
2587
2588 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2589
2590 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2591 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2592 [SERVICE_EXEC_START] = "ExecStart",
2593 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2594 [SERVICE_EXEC_RELOAD] = "ExecReload",
2595 [SERVICE_EXEC_STOP] = "ExecStop",
2596 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2597 };
2598
2599 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2600
2601 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2602 [NOTIFY_NONE] = "none",
2603 [NOTIFY_MAIN] = "main",
2604 [NOTIFY_ALL] = "all"
2605 };
2606
2607 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2608
2609 const UnitVTable service_vtable = {
2610 .suffix = ".service",
2611
2612 .init = service_init,
2613 .done = service_done,
2614 .load = service_load,
2615
2616 .coldplug = service_coldplug,
2617
2618 .dump = service_dump,
2619
2620 .start = service_start,
2621 .stop = service_stop,
2622 .reload = service_reload,
2623
2624 .can_reload = service_can_reload,
2625
2626 .serialize = service_serialize,
2627 .deserialize_item = service_deserialize_item,
2628
2629 .active_state = service_active_state,
2630 .sub_state_to_string = service_sub_state_to_string,
2631
2632 .check_gc = service_check_gc,
2633 .check_snapshot = service_check_snapshot,
2634
2635 .sigchld_event = service_sigchld_event,
2636 .timer_event = service_timer_event,
2637
2638 .cgroup_notify_empty = service_cgroup_notify_event,
2639 .notify_message = service_notify_message,
2640
2641 .bus_name_owner_change = service_bus_name_owner_change,
2642 .bus_query_pid_done = service_bus_query_pid_done,
2643
2644 .bus_message_handler = bus_service_message_handler,
2645
2646 .enumerate = service_enumerate
2647 };