]> git.ipfire.org Git - people/ms/systemd.git/blob - service.c
load-fragment: replace % specifiers in descriptions
[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 s->kill_mode = KILL_PROCESS_GROUP;
665
666 /* Don't timeout special services during boot (like fsck) */
667 if (s->sysv_runlevels && !chars_intersect("12345", s->sysv_runlevels))
668 s->timeout_usec = -1;
669
670 u->meta.load_state = UNIT_LOADED;
671 r = 0;
672
673 finish:
674
675 if (f)
676 fclose(f);
677
678 return r;
679 }
680
681 static int service_load_sysv_name(Service *s, const char *name) {
682 char **p;
683
684 assert(s);
685 assert(name);
686
687 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
688 char *path;
689 int r;
690
691 if (asprintf(&path, "%s/%s", *p, name) < 0)
692 return -ENOMEM;
693
694 assert(endswith(path, ".service"));
695 path[strlen(path)-8] = 0;
696
697 r = service_load_sysv_path(s, path);
698 free(path);
699
700 if (r < 0)
701 return r;
702
703 if ((UNIT(s)->meta.load_state != UNIT_STUB))
704 break;
705 }
706
707 return 0;
708 }
709
710 static int service_load_sysv(Service *s) {
711 const char *t;
712 Iterator i;
713 int r;
714
715 assert(s);
716
717 /* Load service data from SysV init scripts, preferably with
718 * LSB headers ... */
719
720 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
721 return 0;
722
723 if ((t = UNIT(s)->meta.id))
724 if ((r = service_load_sysv_name(s, t)) < 0)
725 return r;
726
727 if (UNIT(s)->meta.load_state == UNIT_STUB)
728 SET_FOREACH(t, UNIT(s)->meta.names, i) {
729 if (t == UNIT(s)->meta.id)
730 continue;
731
732 if ((r == service_load_sysv_name(s, t)) < 0)
733 return r;
734
735 if (UNIT(s)->meta.load_state != UNIT_STUB)
736 break;
737 }
738
739 return 0;
740 }
741
742 static int service_add_bus_name(Service *s) {
743 char *n;
744 int r;
745
746 assert(s);
747 assert(s->bus_name);
748
749 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
750 return 0;
751
752 r = unit_merge_by_name(UNIT(s), n);
753 free(n);
754
755 return r;
756 }
757
758 static int service_verify(Service *s) {
759 assert(s);
760
761 if (UNIT(s)->meta.load_state != UNIT_LOADED)
762 return 0;
763
764 if (!s->exec_command[SERVICE_EXEC_START]) {
765 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
766 return -EINVAL;
767 }
768
769 if (s->type == SERVICE_DBUS && !s->bus_name) {
770 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
771 return -EINVAL;
772 }
773
774 return 0;
775 }
776
777 static int service_load(Unit *u) {
778 int r;
779 Service *s = SERVICE(u);
780
781 assert(s);
782
783 /* Load a .service file */
784 if ((r = unit_load_fragment(u)) < 0)
785 return r;
786
787 /* Load a classic init script as a fallback, if we couldn't find anything */
788 if (u->meta.load_state == UNIT_STUB)
789 if ((r = service_load_sysv(s)) < 0)
790 return r;
791
792 /* Still nothing found? Then let's give up */
793 if (u->meta.load_state == UNIT_STUB)
794 return -ENOENT;
795
796 /* We were able to load something, then let's add in the
797 * dropin directories. */
798 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
799 return r;
800
801 /* This is a new unit? Then let's add in some extras */
802 if (u->meta.load_state == UNIT_LOADED) {
803 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
804 return r;
805
806 if ((r = unit_add_default_cgroup(u)) < 0)
807 return r;
808
809 if ((r = sysv_chkconfig_order(s)) < 0)
810 return r;
811
812 if (s->bus_name) {
813 if ((r = service_add_bus_name(s)) < 0)
814 return r;
815
816 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
817 return r;
818 }
819 }
820
821 return service_verify(s);
822 }
823
824 static void service_dump(Unit *u, FILE *f, const char *prefix) {
825
826 ServiceExecCommand c;
827 Service *s = SERVICE(u);
828 const char *prefix2;
829 char *p2;
830
831 assert(s);
832
833 p2 = strappend(prefix, "\t");
834 prefix2 = p2 ? p2 : prefix;
835
836 fprintf(f,
837 "%sService State: %s\n"
838 "%sPermissionsStartOnly: %s\n"
839 "%sRootDirectoryStartOnly: %s\n"
840 "%sValidNoProcess: %s\n"
841 "%sKillMode: %s\n"
842 "%sType: %s\n",
843 prefix, service_state_to_string(s->state),
844 prefix, yes_no(s->permissions_start_only),
845 prefix, yes_no(s->root_directory_start_only),
846 prefix, yes_no(s->valid_no_process),
847 prefix, kill_mode_to_string(s->kill_mode),
848 prefix, service_type_to_string(s->type));
849
850 if (s->control_pid > 0)
851 fprintf(f,
852 "%sControl PID: %llu\n",
853 prefix, (unsigned long long) s->control_pid);
854
855 if (s->main_pid > 0)
856 fprintf(f,
857 "%sMain PID: %llu\n",
858 prefix, (unsigned long long) s->main_pid);
859
860 if (s->pid_file)
861 fprintf(f,
862 "%sPIDFile: %s\n",
863 prefix, s->pid_file);
864
865 if (s->bus_name)
866 fprintf(f,
867 "%sBusName: %s\n"
868 "%sBus Name Good: %s\n",
869 prefix, s->bus_name,
870 prefix, yes_no(s->bus_name_good));
871
872 exec_context_dump(&s->exec_context, f, prefix);
873
874 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
875
876 if (!s->exec_command[c])
877 continue;
878
879 fprintf(f, "%s-> %s:\n",
880 prefix, service_exec_command_to_string(c));
881
882 exec_command_dump_list(s->exec_command[c], f, prefix2);
883 }
884
885 if (s->sysv_path)
886 fprintf(f,
887 "%sSysV Init Script Path: %s\n"
888 "%sSysV Init Script has LSB Header: %s\n",
889 prefix, s->sysv_path,
890 prefix, yes_no(s->sysv_has_lsb));
891
892 if (s->sysv_start_priority >= 0)
893 fprintf(f,
894 "%sSysVStartPriority: %i\n",
895 prefix, s->sysv_start_priority);
896
897 if (s->sysv_runlevels)
898 fprintf(f, "%sSysVRunLevels: %s\n",
899 prefix, s->sysv_runlevels);
900
901 free(p2);
902 }
903
904 static int service_load_pid_file(Service *s) {
905 char *k;
906 unsigned long p;
907 int r;
908
909 assert(s);
910
911 if (s->main_pid_known)
912 return 0;
913
914 assert(s->main_pid <= 0);
915
916 if (!s->pid_file)
917 return -ENOENT;
918
919 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
920 return r;
921
922 if ((r = safe_atolu(k, &p)) < 0) {
923 free(k);
924 return r;
925 }
926
927 if ((unsigned long) (pid_t) p != p)
928 return -ERANGE;
929
930 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
931 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
932 (unsigned long long) p, s->pid_file);
933 return -ESRCH;
934 }
935
936 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
937 /* FIXME: we need to do something here */
938 return r;
939
940 s->main_pid = (pid_t) p;
941 s->main_pid_known = true;
942
943 return 0;
944 }
945
946 static int service_get_sockets(Service *s, Set **_set) {
947 Set *set;
948 Iterator i;
949 char *t;
950 int r;
951
952 assert(s);
953 assert(_set);
954
955 /* Collects all Socket objects that belong to this
956 * service. Note that a service might have multiple sockets
957 * via multiple names. */
958
959 if (!(set = set_new(NULL, NULL)))
960 return -ENOMEM;
961
962 SET_FOREACH(t, UNIT(s)->meta.names, i) {
963 char *k;
964 Unit *p;
965
966 /* Look for all socket objects that go by any of our
967 * units and collect their fds */
968
969 if (!(k = unit_name_change_suffix(t, ".socket"))) {
970 r = -ENOMEM;
971 goto fail;
972 }
973
974 p = manager_get_unit(UNIT(s)->meta.manager, k);
975 free(k);
976
977 if (!p)
978 continue;
979
980 if ((r = set_put(set, p)) < 0)
981 goto fail;
982 }
983
984 *_set = set;
985 return 0;
986
987 fail:
988 set_free(set);
989 return r;
990 }
991
992 static int service_notify_sockets_dead(Service *s) {
993 Iterator i;
994 Set *set;
995 Socket *sock;
996 int r;
997
998 assert(s);
999
1000 /* Notifies all our sockets when we die */
1001 if ((r = service_get_sockets(s, &set)) < 0)
1002 return r;
1003
1004 SET_FOREACH(sock, set, i)
1005 socket_notify_service_dead(sock);
1006
1007 set_free(set);
1008
1009 return 0;
1010 }
1011
1012 static void service_set_state(Service *s, ServiceState state) {
1013 ServiceState old_state;
1014 assert(s);
1015
1016 old_state = s->state;
1017 s->state = state;
1018
1019 if (state != SERVICE_START_PRE &&
1020 state != SERVICE_START &&
1021 state != SERVICE_START_POST &&
1022 state != SERVICE_RELOAD &&
1023 state != SERVICE_STOP &&
1024 state != SERVICE_STOP_SIGTERM &&
1025 state != SERVICE_STOP_SIGKILL &&
1026 state != SERVICE_STOP_POST &&
1027 state != SERVICE_FINAL_SIGTERM &&
1028 state != SERVICE_FINAL_SIGKILL &&
1029 state != SERVICE_AUTO_RESTART)
1030 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1031
1032 if (state != SERVICE_START &&
1033 state != SERVICE_START_POST &&
1034 state != SERVICE_RUNNING &&
1035 state != SERVICE_RELOAD &&
1036 state != SERVICE_STOP &&
1037 state != SERVICE_STOP_SIGTERM &&
1038 state != SERVICE_STOP_SIGKILL)
1039 service_unwatch_main_pid(s);
1040
1041 if (state != SERVICE_START_PRE &&
1042 state != SERVICE_START &&
1043 state != SERVICE_START_POST &&
1044 state != SERVICE_RELOAD &&
1045 state != SERVICE_STOP &&
1046 state != SERVICE_STOP_SIGTERM &&
1047 state != SERVICE_STOP_SIGKILL &&
1048 state != SERVICE_STOP_POST &&
1049 state != SERVICE_FINAL_SIGTERM &&
1050 state != SERVICE_FINAL_SIGKILL) {
1051 service_unwatch_control_pid(s);
1052 s->control_command = NULL;
1053 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1054 }
1055
1056 if (state == SERVICE_DEAD ||
1057 state == SERVICE_STOP ||
1058 state == SERVICE_STOP_SIGTERM ||
1059 state == SERVICE_STOP_SIGKILL ||
1060 state == SERVICE_STOP_POST ||
1061 state == SERVICE_FINAL_SIGTERM ||
1062 state == SERVICE_FINAL_SIGKILL ||
1063 state == SERVICE_MAINTAINANCE ||
1064 state == SERVICE_AUTO_RESTART)
1065 service_notify_sockets_dead(s);
1066
1067 if (state != SERVICE_START_PRE &&
1068 state != SERVICE_START &&
1069 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1070 service_close_socket_fd(s);
1071
1072 if (old_state != state)
1073 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1074
1075 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1076 }
1077
1078 static int service_coldplug(Unit *u) {
1079 Service *s = SERVICE(u);
1080 int r;
1081
1082 assert(s);
1083 assert(s->state == SERVICE_DEAD);
1084
1085 if (s->deserialized_state != s->state) {
1086
1087 if (s->deserialized_state == SERVICE_START_PRE ||
1088 s->deserialized_state == SERVICE_START ||
1089 s->deserialized_state == SERVICE_START_POST ||
1090 s->deserialized_state == SERVICE_RELOAD ||
1091 s->deserialized_state == SERVICE_STOP ||
1092 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1093 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1094 s->deserialized_state == SERVICE_STOP_POST ||
1095 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1096 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1097 s->deserialized_state == SERVICE_AUTO_RESTART) {
1098
1099 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1100 usec_t k;
1101
1102 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1103
1104 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1105 return r;
1106 }
1107 }
1108
1109 if ((s->deserialized_state == SERVICE_START &&
1110 (s->type == SERVICE_FORKING ||
1111 s->type == SERVICE_DBUS)) ||
1112 s->deserialized_state == SERVICE_START_POST ||
1113 s->deserialized_state == SERVICE_RUNNING ||
1114 s->deserialized_state == SERVICE_RELOAD ||
1115 s->deserialized_state == SERVICE_STOP ||
1116 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1117 s->deserialized_state == SERVICE_STOP_SIGKILL)
1118 if (s->main_pid > 0)
1119 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1120 return r;
1121
1122 if (s->deserialized_state == SERVICE_START_PRE ||
1123 s->deserialized_state == SERVICE_START ||
1124 s->deserialized_state == SERVICE_START_POST ||
1125 s->deserialized_state == SERVICE_RELOAD ||
1126 s->deserialized_state == SERVICE_STOP ||
1127 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1128 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1129 s->deserialized_state == SERVICE_STOP_POST ||
1130 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1131 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1132 if (s->control_pid > 0)
1133 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1134 return r;
1135
1136 service_set_state(s, s->deserialized_state);
1137 }
1138
1139 return 0;
1140 }
1141
1142 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1143 Iterator i;
1144 int r;
1145 int *rfds = NULL;
1146 unsigned rn_fds = 0;
1147 Set *set;
1148 Socket *sock;
1149
1150 assert(s);
1151 assert(fds);
1152 assert(n_fds);
1153
1154 if ((r = service_get_sockets(s, &set)) < 0)
1155 return r;
1156
1157 SET_FOREACH(sock, set, i) {
1158 int *cfds;
1159 unsigned cn_fds;
1160
1161 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1162 goto fail;
1163
1164 if (!cfds)
1165 continue;
1166
1167 if (!rfds) {
1168 rfds = cfds;
1169 rn_fds = cn_fds;
1170 } else {
1171 int *t;
1172
1173 if (!(t = new(int, rn_fds+cn_fds))) {
1174 free(cfds);
1175 r = -ENOMEM;
1176 goto fail;
1177 }
1178
1179 memcpy(t, rfds, rn_fds);
1180 memcpy(t+rn_fds, cfds, cn_fds);
1181 free(rfds);
1182 free(cfds);
1183
1184 rfds = t;
1185 rn_fds = rn_fds+cn_fds;
1186 }
1187 }
1188
1189 *fds = rfds;
1190 *n_fds = rn_fds;
1191
1192 set_free(set);
1193
1194 return 0;
1195
1196 fail:
1197 set_free(set);
1198 free(rfds);
1199
1200 return r;
1201 }
1202
1203 static int service_spawn(
1204 Service *s,
1205 ExecCommand *c,
1206 bool timeout,
1207 bool pass_fds,
1208 bool apply_permissions,
1209 bool apply_chroot,
1210 pid_t *_pid) {
1211
1212 pid_t pid;
1213 int r;
1214 int *fds = NULL;
1215 unsigned n_fds = 0;
1216 char **argv;
1217
1218 assert(s);
1219 assert(c);
1220 assert(_pid);
1221
1222 if (pass_fds) {
1223 if (s->socket_fd >= 0) {
1224 fds = &s->socket_fd;
1225 n_fds = 1;
1226 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1227 goto fail;
1228 }
1229
1230 if (timeout && s->timeout_usec) {
1231 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1232 goto fail;
1233 } else
1234 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1235
1236 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1237 r = -ENOMEM;
1238 goto fail;
1239 }
1240
1241 r = exec_spawn(c,
1242 argv,
1243 &s->exec_context,
1244 fds, n_fds,
1245 apply_permissions,
1246 apply_chroot,
1247 UNIT(s)->meta.manager->confirm_spawn,
1248 UNIT(s)->meta.cgroup_bondings,
1249 &pid);
1250
1251 strv_free(argv);
1252 if (r < 0)
1253 goto fail;
1254
1255 if (fds) {
1256 if (s->socket_fd >= 0)
1257 service_close_socket_fd(s);
1258 else
1259 free(fds);
1260 }
1261
1262 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1263 /* FIXME: we need to do something here */
1264 goto fail;
1265
1266 *_pid = pid;
1267
1268 return 0;
1269
1270 fail:
1271 free(fds);
1272
1273 if (timeout)
1274 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1275
1276 return r;
1277 }
1278
1279 static int main_pid_good(Service *s) {
1280 assert(s);
1281
1282 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1283 * don't know */
1284
1285 /* If we know the pid file, then lets just check if it is
1286 * still valid */
1287 if (s->main_pid_known)
1288 return s->main_pid > 0;
1289
1290 /* We don't know the pid */
1291 return -EAGAIN;
1292 }
1293
1294 static int control_pid_good(Service *s) {
1295 assert(s);
1296
1297 return s->control_pid > 0;
1298 }
1299
1300 static int cgroup_good(Service *s) {
1301 int r;
1302
1303 assert(s);
1304
1305 if (s->valid_no_process)
1306 return -EAGAIN;
1307
1308 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1309 return r;
1310
1311 return !r;
1312 }
1313
1314 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1315 int r;
1316 assert(s);
1317
1318 if (!success)
1319 s->failure = true;
1320
1321 if (allow_restart &&
1322 s->allow_restart &&
1323 (s->restart == SERVICE_RESTART_ALWAYS ||
1324 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1325
1326 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1327 goto fail;
1328
1329 service_set_state(s, SERVICE_AUTO_RESTART);
1330 } else
1331 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1332
1333 return;
1334
1335 fail:
1336 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
1337 service_enter_dead(s, false, false);
1338 }
1339
1340 static void service_enter_signal(Service *s, ServiceState state, bool success);
1341
1342 static void service_enter_stop_post(Service *s, bool success) {
1343 int r;
1344 assert(s);
1345
1346 if (!success)
1347 s->failure = true;
1348
1349 service_unwatch_control_pid(s);
1350
1351 s->control_command_id = SERVICE_EXEC_STOP_POST;
1352 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1353 if ((r = service_spawn(s,
1354 s->control_command,
1355 true,
1356 false,
1357 !s->permissions_start_only,
1358 !s->root_directory_start_only,
1359 &s->control_pid)) < 0)
1360 goto fail;
1361
1362
1363 service_set_state(s, SERVICE_STOP_POST);
1364 } else
1365 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1366
1367 return;
1368
1369 fail:
1370 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1371 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1372 }
1373
1374 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1375 int r;
1376 bool sent = false;
1377
1378 assert(s);
1379
1380 if (!success)
1381 s->failure = true;
1382
1383 if (s->kill_mode != KILL_NONE) {
1384 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1385
1386 if (s->kill_mode == KILL_CONTROL_GROUP) {
1387
1388 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1389 if (r != -EAGAIN && r != -ESRCH)
1390 goto fail;
1391 } else
1392 sent = true;
1393 }
1394
1395 if (!sent) {
1396 r = 0;
1397
1398 if (s->main_pid > 0) {
1399 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1400 r = -errno;
1401 else
1402 sent = true;
1403 }
1404
1405 if (s->control_pid > 0) {
1406 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1407 r = -errno;
1408 else
1409 sent = true;
1410 }
1411
1412 if (r < 0)
1413 goto fail;
1414 }
1415 }
1416
1417 if (sent && (s->main_pid > 0 || s->control_pid > 0)) {
1418 if (s->timeout_usec > 0)
1419 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1420 goto fail;
1421
1422 service_set_state(s, state);
1423 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1424 service_enter_stop_post(s, true);
1425 else
1426 service_enter_dead(s, true, true);
1427
1428 return;
1429
1430 fail:
1431 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
1432
1433 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1434 service_enter_stop_post(s, false);
1435 else
1436 service_enter_dead(s, false, true);
1437 }
1438
1439 static void service_enter_stop(Service *s, bool success) {
1440 int r;
1441 assert(s);
1442
1443 if (!success)
1444 s->failure = true;
1445
1446 service_unwatch_control_pid(s);
1447
1448 s->control_command_id = SERVICE_EXEC_STOP;
1449 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1450 if ((r = service_spawn(s,
1451 s->control_command,
1452 true,
1453 false,
1454 !s->permissions_start_only,
1455 !s->root_directory_start_only,
1456 &s->control_pid)) < 0)
1457 goto fail;
1458
1459 service_set_state(s, SERVICE_STOP);
1460 } else
1461 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1462
1463 return;
1464
1465 fail:
1466 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
1467 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1468 }
1469
1470 static void service_enter_running(Service *s, bool success) {
1471 assert(s);
1472
1473 if (!success)
1474 s->failure = true;
1475
1476 if (main_pid_good(s) != 0 &&
1477 cgroup_good(s) != 0 &&
1478 (s->bus_name_good || s->type != SERVICE_DBUS))
1479 service_set_state(s, SERVICE_RUNNING);
1480 else if (s->valid_no_process)
1481 service_set_state(s, SERVICE_EXITED);
1482 else
1483 service_enter_stop(s, true);
1484 }
1485
1486 static void service_enter_start_post(Service *s) {
1487 int r;
1488 assert(s);
1489
1490 service_unwatch_control_pid(s);
1491
1492 s->control_command_id = SERVICE_EXEC_START_POST;
1493 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1494 if ((r = service_spawn(s,
1495 s->control_command,
1496 true,
1497 false,
1498 !s->permissions_start_only,
1499 !s->root_directory_start_only,
1500 &s->control_pid)) < 0)
1501 goto fail;
1502
1503
1504 service_set_state(s, SERVICE_START_POST);
1505 } else
1506 service_enter_running(s, true);
1507
1508 return;
1509
1510 fail:
1511 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
1512 service_enter_stop(s, false);
1513 }
1514
1515 static void service_enter_start(Service *s) {
1516 pid_t pid;
1517 int r;
1518
1519 assert(s);
1520
1521 assert(s->exec_command[SERVICE_EXEC_START]);
1522 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1523
1524 if (s->type == SERVICE_FORKING)
1525 service_unwatch_control_pid(s);
1526 else
1527 service_unwatch_main_pid(s);
1528
1529 if ((r = service_spawn(s,
1530 s->exec_command[SERVICE_EXEC_START],
1531 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
1532 true,
1533 true,
1534 true,
1535 &pid)) < 0)
1536 goto fail;
1537
1538 if (s->type == SERVICE_SIMPLE) {
1539 /* For simple services we immediately start
1540 * the START_POST binaries. */
1541
1542 s->main_pid = pid;
1543 s->main_pid_known = true;
1544
1545 service_enter_start_post(s);
1546
1547 } else if (s->type == SERVICE_FORKING) {
1548
1549 /* For forking services we wait until the start
1550 * process exited. */
1551
1552 s->control_pid = pid;
1553
1554 s->control_command_id = SERVICE_EXEC_START;
1555 s->control_command = s->exec_command[SERVICE_EXEC_START];
1556 service_set_state(s, SERVICE_START);
1557
1558 } else if (s->type == SERVICE_FINISH ||
1559 s->type == SERVICE_DBUS) {
1560
1561 /* For finishing services we wait until the start
1562 * process exited, too, but it is our main process. */
1563
1564 /* For D-Bus services we know the main pid right away,
1565 * but wait for the bus name to appear on the bus. */
1566
1567 s->main_pid = pid;
1568 s->main_pid_known = true;
1569
1570 service_set_state(s, SERVICE_START);
1571 } else
1572 assert_not_reached("Unknown service type");
1573
1574 return;
1575
1576 fail:
1577 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
1578 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1579 }
1580
1581 static void service_enter_start_pre(Service *s) {
1582 int r;
1583
1584 assert(s);
1585
1586 service_unwatch_control_pid(s);
1587
1588 s->control_command_id = SERVICE_EXEC_START_PRE;
1589 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1590 if ((r = service_spawn(s,
1591 s->control_command,
1592 true,
1593 false,
1594 !s->permissions_start_only,
1595 !s->root_directory_start_only,
1596 &s->control_pid)) < 0)
1597 goto fail;
1598
1599 service_set_state(s, SERVICE_START_PRE);
1600 } else
1601 service_enter_start(s);
1602
1603 return;
1604
1605 fail:
1606 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
1607 service_enter_dead(s, false, true);
1608 }
1609
1610 static void service_enter_restart(Service *s) {
1611 int r;
1612 assert(s);
1613
1614 service_enter_dead(s, true, false);
1615
1616 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1617 goto fail;
1618
1619 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
1620 return;
1621
1622 fail:
1623
1624 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
1625 service_enter_dead(s, false, false);
1626 }
1627
1628 static void service_enter_reload(Service *s) {
1629 int r;
1630
1631 assert(s);
1632
1633 service_unwatch_control_pid(s);
1634
1635 s->control_command_id = SERVICE_EXEC_RELOAD;
1636 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1637 if ((r = service_spawn(s,
1638 s->control_command,
1639 true,
1640 false,
1641 !s->permissions_start_only,
1642 !s->root_directory_start_only,
1643 &s->control_pid)) < 0)
1644 goto fail;
1645
1646 service_set_state(s, SERVICE_RELOAD);
1647 } else
1648 service_enter_running(s, true);
1649
1650 return;
1651
1652 fail:
1653 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
1654 service_enter_stop(s, false);
1655 }
1656
1657 static void service_run_next(Service *s, bool success) {
1658 int r;
1659
1660 assert(s);
1661 assert(s->control_command);
1662 assert(s->control_command->command_next);
1663
1664 if (!success)
1665 s->failure = true;
1666
1667 s->control_command = s->control_command->command_next;
1668
1669 service_unwatch_control_pid(s);
1670
1671 if ((r = service_spawn(s,
1672 s->control_command,
1673 true,
1674 false,
1675 !s->permissions_start_only,
1676 !s->root_directory_start_only,
1677 &s->control_pid)) < 0)
1678 goto fail;
1679
1680 return;
1681
1682 fail:
1683 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
1684
1685 if (s->state == SERVICE_START_PRE)
1686 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1687 else if (s->state == SERVICE_STOP)
1688 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1689 else if (s->state == SERVICE_STOP_POST)
1690 service_enter_dead(s, false, true);
1691 else
1692 service_enter_stop(s, false);
1693 }
1694
1695 static int service_start(Unit *u) {
1696 Service *s = SERVICE(u);
1697
1698 assert(s);
1699
1700 /* We cannot fulfill this request right now, try again later
1701 * please! */
1702 if (s->state == SERVICE_STOP ||
1703 s->state == SERVICE_STOP_SIGTERM ||
1704 s->state == SERVICE_STOP_SIGKILL ||
1705 s->state == SERVICE_STOP_POST ||
1706 s->state == SERVICE_FINAL_SIGTERM ||
1707 s->state == SERVICE_FINAL_SIGKILL)
1708 return -EAGAIN;
1709
1710 /* Already on it! */
1711 if (s->state == SERVICE_START_PRE ||
1712 s->state == SERVICE_START ||
1713 s->state == SERVICE_START_POST)
1714 return 0;
1715
1716 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1717
1718 /* Make sure we don't enter a busy loop of some kind. */
1719 if (!ratelimit_test(&s->ratelimit)) {
1720 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1721 return -EAGAIN;
1722 }
1723
1724 s->failure = false;
1725 s->main_pid_known = false;
1726 s->allow_restart = true;
1727
1728 service_enter_start_pre(s);
1729 return 0;
1730 }
1731
1732 static int service_stop(Unit *u) {
1733 Service *s = SERVICE(u);
1734
1735 assert(s);
1736
1737 /* Cannot do this now */
1738 if (s->state == SERVICE_START_PRE ||
1739 s->state == SERVICE_START ||
1740 s->state == SERVICE_START_POST ||
1741 s->state == SERVICE_RELOAD)
1742 return -EAGAIN;
1743
1744 /* Already on it */
1745 if (s->state == SERVICE_STOP ||
1746 s->state == SERVICE_STOP_SIGTERM ||
1747 s->state == SERVICE_STOP_SIGKILL ||
1748 s->state == SERVICE_STOP_POST ||
1749 s->state == SERVICE_FINAL_SIGTERM ||
1750 s->state == SERVICE_FINAL_SIGKILL)
1751 return 0;
1752
1753 if (s->state == SERVICE_AUTO_RESTART) {
1754 service_set_state(s, SERVICE_DEAD);
1755 return 0;
1756 }
1757
1758 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1759
1760 /* This is a user request, so don't do restarts on this
1761 * shutdown. */
1762 s->allow_restart = false;
1763
1764 service_enter_stop(s, true);
1765 return 0;
1766 }
1767
1768 static int service_reload(Unit *u) {
1769 Service *s = SERVICE(u);
1770
1771 assert(s);
1772
1773 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
1774
1775 service_enter_reload(s);
1776 return 0;
1777 }
1778
1779 static bool service_can_reload(Unit *u) {
1780 Service *s = SERVICE(u);
1781
1782 assert(s);
1783
1784 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1785 }
1786
1787 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1788 Service *s = SERVICE(u);
1789
1790 assert(u);
1791 assert(f);
1792 assert(fds);
1793
1794 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1795 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1796
1797 if (s->control_pid > 0)
1798 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1799
1800 if (s->main_pid > 0)
1801 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1802
1803 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1804
1805 /* There's a minor uncleanliness here: if there are multiple
1806 * commands attached here, we will start from the first one
1807 * again */
1808 if (s->control_command_id >= 0)
1809 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
1810
1811 if (s->socket_fd >= 0) {
1812 int copy;
1813
1814 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1815 return copy;
1816
1817 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1818 }
1819
1820 return 0;
1821 }
1822
1823 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1824 Service *s = SERVICE(u);
1825 int r;
1826
1827 assert(u);
1828 assert(key);
1829 assert(value);
1830 assert(fds);
1831
1832 if (streq(key, "state")) {
1833 ServiceState state;
1834
1835 if ((state = service_state_from_string(value)) < 0)
1836 log_debug("Failed to parse state value %s", value);
1837 else
1838 s->deserialized_state = state;
1839 } else if (streq(key, "failure")) {
1840 int b;
1841
1842 if ((b = parse_boolean(value)) < 0)
1843 log_debug("Failed to parse failure value %s", value);
1844 else
1845 s->failure = b || s->failure;
1846 } else if (streq(key, "control-pid")) {
1847 unsigned pid;
1848
1849 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1850 log_debug("Failed to parse control-pid value %s", value);
1851 else
1852 s->control_pid = (pid_t) pid;
1853 } else if (streq(key, "main-pid")) {
1854 unsigned pid;
1855
1856 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1857 log_debug("Failed to parse main-pid value %s", value);
1858 else
1859 s->main_pid = (pid_t) pid;
1860 } else if (streq(key, "main-pid-known")) {
1861 int b;
1862
1863 if ((b = parse_boolean(value)) < 0)
1864 log_debug("Failed to parse main-pid-known value %s", value);
1865 else
1866 s->main_pid_known = b;
1867 } else if (streq(key, "control-command")) {
1868 ServiceExecCommand id;
1869
1870 if ((id = service_exec_command_from_string(value)) < 0)
1871 log_debug("Failed to parse exec-command value %s", value);
1872 else {
1873 s->control_command_id = id;
1874 s->control_command = s->exec_command[id];
1875 }
1876 } else if (streq(key, "socket-fd")) {
1877 int fd;
1878
1879 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1880 log_debug("Failed to parse socket-fd value %s", value);
1881 else {
1882
1883 if (s->socket_fd >= 0)
1884 close_nointr_nofail(s->socket_fd);
1885 s->socket_fd = fdset_remove(fds, fd);
1886 }
1887 } else
1888 log_debug("Unknown serialization key '%s'", key);
1889
1890 return 0;
1891 }
1892
1893 static UnitActiveState service_active_state(Unit *u) {
1894 assert(u);
1895
1896 return state_translation_table[SERVICE(u)->state];
1897 }
1898
1899 static const char *service_sub_state_to_string(Unit *u) {
1900 assert(u);
1901
1902 return service_state_to_string(SERVICE(u)->state);
1903 }
1904
1905 static bool service_check_gc(Unit *u) {
1906 Service *s = SERVICE(u);
1907
1908 assert(s);
1909
1910 return !!s->sysv_path;
1911 }
1912
1913 static bool service_check_snapshot(Unit *u) {
1914 Service *s = SERVICE(u);
1915
1916 assert(s);
1917
1918 return !s->got_socket_fd;
1919 }
1920
1921 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1922 Service *s = SERVICE(u);
1923 bool success;
1924
1925 assert(s);
1926 assert(pid >= 0);
1927
1928 success = code == CLD_EXITED && status == 0;
1929 s->failure = s->failure || !success;
1930
1931 if (s->main_pid == pid) {
1932
1933 exec_status_fill(&s->main_exec_status, pid, code, status);
1934 s->main_pid = 0;
1935
1936 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1937 assert(s->exec_command[SERVICE_EXEC_START]);
1938 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1939 }
1940
1941 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
1942
1943 /* The service exited, so the service is officially
1944 * gone. */
1945
1946 switch (s->state) {
1947
1948 case SERVICE_START_POST:
1949 case SERVICE_RELOAD:
1950 case SERVICE_STOP:
1951 /* Need to wait until the operation is
1952 * done */
1953 break;
1954
1955 case SERVICE_START:
1956 assert(s->type == SERVICE_FINISH);
1957
1958 /* This was our main goal, so let's go on */
1959 if (success)
1960 service_enter_start_post(s);
1961 else
1962 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1963 break;
1964
1965 case SERVICE_RUNNING:
1966 service_enter_running(s, success);
1967 break;
1968
1969 case SERVICE_STOP_SIGTERM:
1970 case SERVICE_STOP_SIGKILL:
1971
1972 if (!control_pid_good(s))
1973 service_enter_stop_post(s, success);
1974
1975 /* If there is still a control process, wait for that first */
1976 break;
1977
1978 default:
1979 assert_not_reached("Uh, main process died at wrong time.");
1980 }
1981
1982 } else if (s->control_pid == pid) {
1983
1984 if (s->control_command)
1985 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1986
1987 s->control_pid = 0;
1988
1989 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
1990
1991 /* If we are shutting things down anyway we
1992 * don't care about failing commands. */
1993
1994 if (s->control_command && s->control_command->command_next && success) {
1995
1996 /* There is another command to *
1997 * execute, so let's do that. */
1998
1999 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
2000 service_run_next(s, success);
2001
2002 } else {
2003 /* No further commands for this step, so let's
2004 * figure out what to do next */
2005
2006 s->control_command = NULL;
2007 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2008
2009 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2010
2011 switch (s->state) {
2012
2013 case SERVICE_START_PRE:
2014 if (success)
2015 service_enter_start(s);
2016 else
2017 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2018 break;
2019
2020 case SERVICE_START:
2021 assert(s->type == SERVICE_FORKING);
2022
2023 /* Let's try to load the pid
2024 * file here if we can. We
2025 * ignore the return value,
2026 * since the PID file might
2027 * actually be created by a
2028 * START_POST script */
2029
2030 if (success) {
2031 if (s->pid_file)
2032 service_load_pid_file(s);
2033
2034 service_enter_start_post(s);
2035 } else
2036 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2037
2038 break;
2039
2040 case SERVICE_START_POST:
2041 if (success && s->pid_file && !s->main_pid_known) {
2042 int r;
2043
2044 /* Hmm, let's see if we can
2045 * load the pid now after the
2046 * start-post scripts got
2047 * executed. */
2048
2049 if ((r = service_load_pid_file(s)) < 0)
2050 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
2051 }
2052
2053 /* Fall through */
2054
2055 case SERVICE_RELOAD:
2056 if (success)
2057 service_enter_running(s, true);
2058 else
2059 service_enter_stop(s, false);
2060
2061 break;
2062
2063 case SERVICE_STOP:
2064 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2065 break;
2066
2067 case SERVICE_STOP_SIGTERM:
2068 case SERVICE_STOP_SIGKILL:
2069 if (main_pid_good(s) <= 0)
2070 service_enter_stop_post(s, success);
2071
2072 /* If there is still a service
2073 * process around, wait until
2074 * that one quit, too */
2075 break;
2076
2077 case SERVICE_STOP_POST:
2078 case SERVICE_FINAL_SIGTERM:
2079 case SERVICE_FINAL_SIGKILL:
2080 service_enter_dead(s, success, true);
2081 break;
2082
2083 default:
2084 assert_not_reached("Uh, control process died at wrong time.");
2085 }
2086 }
2087 } else
2088 assert_not_reached("Got SIGCHLD for unkown PID");
2089 }
2090
2091 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2092 Service *s = SERVICE(u);
2093
2094 assert(s);
2095 assert(elapsed == 1);
2096
2097 assert(w == &s->timer_watch);
2098
2099 switch (s->state) {
2100
2101 case SERVICE_START_PRE:
2102 case SERVICE_START:
2103 log_warning("%s operation timed out. Terminating.", u->meta.id);
2104 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2105 break;
2106
2107 case SERVICE_START_POST:
2108 case SERVICE_RELOAD:
2109 log_warning("%s operation timed out. Stopping.", u->meta.id);
2110 service_enter_stop(s, false);
2111 break;
2112
2113 case SERVICE_STOP:
2114 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2115 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2116 break;
2117
2118 case SERVICE_STOP_SIGTERM:
2119 log_warning("%s stopping timed out. Killing.", u->meta.id);
2120 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2121 break;
2122
2123 case SERVICE_STOP_SIGKILL:
2124 /* Uh, wie sent a SIGKILL and it is still not gone?
2125 * Must be something we cannot kill, so let's just be
2126 * weirded out and continue */
2127
2128 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2129 service_enter_stop_post(s, false);
2130 break;
2131
2132 case SERVICE_STOP_POST:
2133 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2134 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2135 break;
2136
2137 case SERVICE_FINAL_SIGTERM:
2138 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2139 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2140 break;
2141
2142 case SERVICE_FINAL_SIGKILL:
2143 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
2144 service_enter_dead(s, false, true);
2145 break;
2146
2147 case SERVICE_AUTO_RESTART:
2148 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
2149 service_enter_restart(s);
2150 break;
2151
2152 default:
2153 assert_not_reached("Timeout at wrong time.");
2154 }
2155 }
2156
2157 static void service_cgroup_notify_event(Unit *u) {
2158 Service *s = SERVICE(u);
2159
2160 assert(u);
2161
2162 log_debug("%s: cgroup is empty", u->meta.id);
2163
2164 switch (s->state) {
2165
2166 /* Waiting for SIGCHLD is usually more interesting,
2167 * because it includes return codes/signals. Which is
2168 * why we ignore the cgroup events for most cases,
2169 * except when we don't know pid which to expect the
2170 * SIGCHLD for. */
2171
2172 case SERVICE_RUNNING:
2173 service_enter_running(s, true);
2174 break;
2175
2176 default:
2177 ;
2178 }
2179 }
2180
2181 static int service_enumerate(Manager *m) {
2182 char **p;
2183 unsigned i;
2184 DIR *d = NULL;
2185 char *path = NULL, *fpath = NULL, *name = NULL;
2186 int r;
2187
2188 assert(m);
2189
2190 STRV_FOREACH(p, m->sysvrcnd_path)
2191 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
2192 struct dirent *de;
2193
2194 free(path);
2195 path = NULL;
2196 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
2197 r = -ENOMEM;
2198 goto finish;
2199 }
2200
2201 if (d)
2202 closedir(d);
2203
2204 if (!(d = opendir(path))) {
2205 if (errno != ENOENT)
2206 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2207
2208 continue;
2209 }
2210
2211 while ((de = readdir(d))) {
2212 Unit *service;
2213
2214 if (ignore_file(de->d_name))
2215 continue;
2216
2217 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2218 continue;
2219
2220 if (strlen(de->d_name) < 4)
2221 continue;
2222
2223 free(fpath);
2224 fpath = NULL;
2225 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
2226 r = -ENOMEM;
2227 goto finish;
2228 }
2229
2230 if (access(fpath, X_OK) < 0) {
2231
2232 if (errno != ENOENT)
2233 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2234
2235 continue;
2236 }
2237
2238 free(name);
2239 name = NULL;
2240 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
2241 r = -ENOMEM;
2242 goto finish;
2243 }
2244
2245 if ((r = manager_load_unit(m, name, NULL, &service)) < 0)
2246 goto finish;
2247
2248 if (de->d_name[0] == 'S') {
2249 Unit *runlevel_target;
2250
2251 if ((r = manager_load_unit(m, rcnd_table[i+1], NULL, &runlevel_target)) < 0)
2252 goto finish;
2253
2254 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2255 goto finish;
2256
2257 if ((r = unit_add_dependency(service, UNIT_BEFORE, runlevel_target, true)) < 0)
2258 goto finish;
2259
2260 } else if (de->d_name[0] == 'K' &&
2261 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
2262 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
2263
2264 Unit *shutdown_target;
2265
2266 /* We honour K links only for
2267 * halt/reboot. For the normal
2268 * runlevels we assume the
2269 * stop jobs will be
2270 * implicitly added by the
2271 * core logic. Also, we don't
2272 * really distuingish here
2273 * between the runlevels 0 and
2274 * 6 and just add them to the
2275 * special shutdown target. */
2276
2277 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2278 goto finish;
2279
2280 if ((r = unit_add_dependency(service, UNIT_CONFLICTS, shutdown_target, true)) < 0)
2281 goto finish;
2282
2283 if ((r = unit_add_dependency(service, UNIT_BEFORE, shutdown_target, true)) < 0)
2284 goto finish;
2285 }
2286 }
2287 }
2288
2289 r = 0;
2290
2291 finish:
2292 free(path);
2293 free(fpath);
2294 free(name);
2295 closedir(d);
2296
2297 return r;
2298 }
2299
2300 static void service_bus_name_owner_change(
2301 Unit *u,
2302 const char *name,
2303 const char *old_owner,
2304 const char *new_owner) {
2305
2306 Service *s = SERVICE(u);
2307
2308 assert(s);
2309 assert(name);
2310
2311 assert(streq(s->bus_name, name));
2312 assert(old_owner || new_owner);
2313
2314 if (old_owner && new_owner)
2315 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2316 else if (old_owner)
2317 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2318 else
2319 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2320
2321 s->bus_name_good = !!new_owner;
2322
2323 if (s->type == SERVICE_DBUS) {
2324
2325 /* service_enter_running() will figure out what to
2326 * do */
2327 if (s->state == SERVICE_RUNNING)
2328 service_enter_running(s, true);
2329 else if (s->state == SERVICE_START && new_owner)
2330 service_enter_start_post(s);
2331
2332 } else if (new_owner &&
2333 s->main_pid <= 0 &&
2334 (s->state == SERVICE_START ||
2335 s->state == SERVICE_START_POST ||
2336 s->state == SERVICE_RUNNING ||
2337 s->state == SERVICE_RELOAD)) {
2338
2339 /* Try to acquire PID from bus service */
2340 log_debug("Trying to acquire PID from D-Bus name...");
2341
2342 bus_query_pid(u->meta.manager, name);
2343 }
2344 }
2345
2346 static void service_bus_query_pid_done(
2347 Unit *u,
2348 const char *name,
2349 pid_t pid) {
2350
2351 Service *s = SERVICE(u);
2352
2353 assert(s);
2354 assert(name);
2355
2356 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2357
2358 if (s->main_pid <= 0 &&
2359 (s->state == SERVICE_START ||
2360 s->state == SERVICE_START_POST ||
2361 s->state == SERVICE_RUNNING ||
2362 s->state == SERVICE_RELOAD))
2363 s->main_pid = pid;
2364 }
2365
2366 int service_set_socket_fd(Service *s, int fd) {
2367 assert(s);
2368 assert(fd >= 0);
2369
2370 /* This is called by the socket code when instantiating a new
2371 * service for a stream socket and the socket needs to be
2372 * configured. */
2373
2374 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2375 return -EINVAL;
2376
2377 if (s->socket_fd >= 0)
2378 return -EBUSY;
2379
2380 if (s->state != SERVICE_DEAD)
2381 return -EAGAIN;
2382
2383 s->socket_fd = fd;
2384 s->got_socket_fd = true;
2385 return 0;
2386 }
2387
2388 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2389 [SERVICE_DEAD] = "dead",
2390 [SERVICE_START_PRE] = "start-pre",
2391 [SERVICE_START] = "start",
2392 [SERVICE_START_POST] = "start-post",
2393 [SERVICE_RUNNING] = "running",
2394 [SERVICE_EXITED] = "exited",
2395 [SERVICE_RELOAD] = "reload",
2396 [SERVICE_STOP] = "stop",
2397 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2398 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2399 [SERVICE_STOP_POST] = "stop-post",
2400 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2401 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2402 [SERVICE_MAINTAINANCE] = "maintainance",
2403 [SERVICE_AUTO_RESTART] = "auto-restart",
2404 };
2405
2406 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2407
2408 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2409 [SERVICE_ONCE] = "once",
2410 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2411 [SERVICE_RESTART_ALWAYS] = "restart-always",
2412 };
2413
2414 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2415
2416 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2417 [SERVICE_FORKING] = "forking",
2418 [SERVICE_SIMPLE] = "simple",
2419 [SERVICE_FINISH] = "finish",
2420 [SERVICE_DBUS] = "dbus"
2421 };
2422
2423 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2424
2425 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2426 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2427 [SERVICE_EXEC_START] = "ExecStart",
2428 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2429 [SERVICE_EXEC_RELOAD] = "ExecReload",
2430 [SERVICE_EXEC_STOP] = "ExecStop",
2431 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2432 };
2433
2434 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2435
2436 const UnitVTable service_vtable = {
2437 .suffix = ".service",
2438
2439 .init = service_init,
2440 .done = service_done,
2441 .load = service_load,
2442
2443 .coldplug = service_coldplug,
2444
2445 .dump = service_dump,
2446
2447 .start = service_start,
2448 .stop = service_stop,
2449 .reload = service_reload,
2450
2451 .can_reload = service_can_reload,
2452
2453 .serialize = service_serialize,
2454 .deserialize_item = service_deserialize_item,
2455
2456 .active_state = service_active_state,
2457 .sub_state_to_string = service_sub_state_to_string,
2458
2459 .check_gc = service_check_gc,
2460 .check_snapshot = service_check_snapshot,
2461
2462 .sigchld_event = service_sigchld_event,
2463 .timer_event = service_timer_event,
2464
2465 .cgroup_notify_empty = service_cgroup_notify_event,
2466
2467 .bus_name_owner_change = service_bus_name_owner_change,
2468 .bus_query_pid_done = service_bus_query_pid_done,
2469
2470 .bus_message_handler = bus_service_message_handler,
2471
2472 .enumerate = service_enumerate
2473 };