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