]> git.ipfire.org Git - people/ms/systemd.git/blob - service.c
a693c3339249ed6bd09d23988bdcba0ae433fc93
[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
34 #define COMMENTS "#;\n"
35 #define NEWLINES "\n\r"
36 #define LINE_MAX 4096
37
38 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
39 [SERVICE_DEAD] = UNIT_INACTIVE,
40 [SERVICE_START_PRE] = UNIT_ACTIVATING,
41 [SERVICE_START] = UNIT_ACTIVATING,
42 [SERVICE_START_POST] = UNIT_ACTIVATING,
43 [SERVICE_RUNNING] = UNIT_ACTIVE,
44 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
45 [SERVICE_STOP] = UNIT_DEACTIVATING,
46 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
47 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
48 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
49 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
50 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
51 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
52 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
53 };
54
55 static void service_done(Unit *u) {
56 Service *s = SERVICE(u);
57
58 assert(s);
59
60 free(s->pid_file);
61 s->pid_file = NULL;
62
63 free(s->sysv_path);
64 s->sysv_path = NULL;
65
66 exec_context_done(&s->exec_context);
67 exec_command_free_array(s->exec_command, _SERVICE_EXEC_MAX);
68 s->control_command = NULL;
69
70 /* This will leak a process, but at least no memory or any of
71 * our resources */
72 if (s->main_pid > 0) {
73 unit_unwatch_pid(u, s->main_pid);
74 s->main_pid = 0;
75 }
76
77 if (s->control_pid > 0) {
78 unit_unwatch_pid(u, s->control_pid);
79 s->control_pid = 0;
80 }
81
82 unit_unwatch_timer(u, &s->timer_watch);
83 }
84
85 static int sysv_translate_name(const char *name, char **_r) {
86
87 static const char * const table[] = {
88 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
89 "$network", SPECIAL_NETWORK_TARGET,
90 "$named", SPECIAL_NSS_LOOKUP_TARGET,
91 "$portmap", SPECIAL_RPCBIND_TARGET,
92 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
93 "$syslog", SPECIAL_SYSLOG_TARGET,
94 "$time", SPECIAL_RTC_SET_TARGET
95 };
96
97 unsigned i;
98 char *r;
99
100 for (i = 0; i < ELEMENTSOF(table); i += 2)
101 if (streq(table[i], name)) {
102 if (!(r = strdup(table[i+1])))
103 return -ENOMEM;
104
105 goto finish;
106 }
107
108 if (*name == '$')
109 return 0;
110
111 if (asprintf(&r, "%s.service", name) < 0)
112 return -ENOMEM;
113
114 finish:
115
116 if (_r)
117 *_r = r;
118
119 return 1;
120 }
121
122 static int sysv_chkconfig_order(Service *s) {
123 Meta *other;
124 int r;
125
126 assert(s);
127
128 if (s->sysv_start_priority < 0)
129 return 0;
130
131 /* If no LSB header is found we try to order init scripts via
132 * the start priority of the chkconfig header. */
133
134 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
135 Service *t;
136 UnitDependency d;
137
138 t = (Service*) other;
139
140 if (s == t)
141 continue;
142
143 if (t->sysv_start_priority < 0)
144 continue;
145
146 if (t->sysv_start_priority < s->sysv_start_priority)
147 d = UNIT_AFTER;
148 else if (t->sysv_start_priority > s->sysv_start_priority)
149 d = UNIT_BEFORE;
150 else
151 continue;
152
153 /* FIXME: Maybe we should compare the name here lexicographically? */
154
155 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t))) < 0)
156 return r;
157 }
158
159 return 0;
160 }
161
162 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
163 ExecCommand *c;
164
165 if (!(c = new0(ExecCommand, 1)))
166 return NULL;
167
168 if (!(c->path = strdup(path))) {
169 free(c);
170 return NULL;
171 }
172
173 if (!(c->argv = strv_new(path, arg1, NULL))) {
174 free(c->path);
175 free(c);
176 return NULL;
177 }
178
179 return c;
180 }
181
182 static int sysv_exec_commands(Service *s) {
183 ExecCommand *c;
184
185 assert(s);
186 assert(s->sysv_path);
187
188 if (!(c = exec_command_new(s->sysv_path, "start")))
189 return -ENOMEM;
190 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
191
192 if (!(c = exec_command_new(s->sysv_path, "stop")))
193 return -ENOMEM;
194 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
195
196 if (!(c = exec_command_new(s->sysv_path, "reload")))
197 return -ENOMEM;
198 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
199
200 return 0;
201 }
202
203 static int service_load_sysv_path(Service *s, const char *path) {
204 FILE *f;
205 Unit *u;
206 unsigned line = 0;
207 int r;
208 enum {
209 NORMAL,
210 DESCRIPTION,
211 LSB,
212 LSB_DESCRIPTION
213 } state = NORMAL;
214 bool has_lsb = false;
215
216 u = UNIT(s);
217
218 if (!(f = fopen(path, "re"))) {
219 r = errno == ENOENT ? 0 : -errno;
220 goto finish;
221 }
222
223 s->type = SERVICE_FORKING;
224 s->restart = SERVICE_ONCE;
225
226 free(s->sysv_path);
227 if (!(s->sysv_path = strdup(path))) {
228 r = -ENOMEM;
229 goto finish;
230 }
231
232 while (!feof(f)) {
233 char l[LINE_MAX], *t;
234
235 if (!fgets(l, sizeof(l), f)) {
236 if (feof(f))
237 break;
238
239 r = -errno;
240 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
241 goto finish;
242 }
243
244 line++;
245
246 t = strstrip(l);
247 if (*t != '#')
248 continue;
249
250 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
251 state = LSB;
252 has_lsb = true;
253 continue;
254 }
255
256 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
257 state = NORMAL;
258 continue;
259 }
260
261 t++;
262 t += strspn(t, WHITESPACE);
263
264 if (state == NORMAL) {
265
266 /* Try to parse Red Hat style chkconfig headers */
267
268 if (startswith(t, "chkconfig:")) {
269 int start_priority;
270
271 state = NORMAL;
272
273 if (sscanf(t+10, "%*15s %i %*i",
274 &start_priority) != 1) {
275
276 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
277 continue;
278 }
279
280 if (start_priority < 0 || start_priority > 99) {
281 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
282 continue;
283 }
284
285 s->sysv_start_priority = start_priority;
286
287 } else if (startswith(t, "description:")) {
288
289 size_t k = strlen(t);
290 char *d;
291
292 if (t[k-1] == '\\') {
293 state = DESCRIPTION;
294 t[k-1] = 0;
295 }
296
297 if (!(d = strdup(strstrip(t+12)))) {
298 r = -ENOMEM;
299 goto finish;
300 }
301
302 free(u->meta.description);
303 u->meta.description = d;
304
305 } else if (startswith(t, "pidfile:")) {
306
307 char *fn;
308
309 state = NORMAL;
310
311 fn = strstrip(t+8);
312 if (!path_is_absolute(fn)) {
313 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
314 continue;
315 }
316
317 if (!(fn = strdup(fn))) {
318 r = -ENOMEM;
319 goto finish;
320 }
321
322 free(s->pid_file);
323 s->pid_file = fn;
324 }
325
326 } else if (state == DESCRIPTION) {
327
328 /* Try to parse Red Hat style description
329 * continuation */
330
331 size_t k = strlen(t);
332 char *d;
333
334 if (t[k-1] == '\\')
335 t[k-1] = 0;
336 else
337 state = NORMAL;
338
339 assert(u->meta.description);
340 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
341 r = -ENOMEM;
342 goto finish;
343 }
344
345 free(u->meta.description);
346 u->meta.description = d;
347
348 } else if (state == LSB || state == LSB_DESCRIPTION) {
349
350 if (startswith(t, "Provides:")) {
351 char *i, *w;
352 size_t z;
353
354 state = LSB;
355
356 FOREACH_WORD(w, z, t+9, i) {
357 char *n, *m;
358
359 if (!(n = strndup(w, z))) {
360 r = -ENOMEM;
361 goto finish;
362 }
363
364 r = sysv_translate_name(n, &m);
365 free(n);
366
367 if (r < 0)
368 goto finish;
369
370 if (r == 0)
371 continue;
372
373 r = unit_add_name(u, m);
374 free(m);
375
376 if (r < 0)
377 goto finish;
378 }
379
380 } else if (startswith(t, "Required-Start:") ||
381 startswith(t, "Should-Start:")) {
382 char *i, *w;
383 size_t z;
384
385 state = LSB;
386
387 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
388 char *n, *m;
389
390 if (!(n = strndup(w, z))) {
391 r = -ENOMEM;
392 goto finish;
393 }
394
395 r = sysv_translate_name(n, &m);
396 free(n);
397
398 if (r < 0)
399 goto finish;
400
401 if (r == 0)
402 continue;
403
404 if (!(r = unit_add_dependency_by_name(u, UNIT_AFTER, m)) < 0) {
405 free(m);
406 goto finish;
407 }
408
409 r = unit_add_dependency_by_name(
410 u,
411 startswith(t, "Required-Start:") ? UNIT_REQUIRES : UNIT_WANTS,
412 m);
413 free(m);
414
415 if (r < 0)
416 goto finish;
417 }
418
419 } else if (startswith(t, "Description:")) {
420 char *d;
421
422 state = LSB_DESCRIPTION;
423
424 if (!(d = strdup(strstrip(t+12)))) {
425 r = -ENOMEM;
426 goto finish;
427 }
428
429 free(u->meta.description);
430 u->meta.description = d;
431
432 } else if (startswith(t, "Short-Description:") && !u->meta.description) {
433 char *d;
434
435 /* We use the short description only
436 * if no long description is set. */
437
438 state = LSB;
439
440 if (!(d = strdup(strstrip(t+18)))) {
441 r = -ENOMEM;
442 goto finish;
443 }
444
445 free(u->meta.description);
446 u->meta.description = d;
447
448 } else if (state == LSB_DESCRIPTION) {
449
450 if (startswith(l, "#\t") || startswith(l, "# ")) {
451 char *d;
452
453 assert(u->meta.description);
454 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
455 r = -ENOMEM;
456 goto finish;
457 }
458
459 free(u->meta.description);
460 u->meta.description = d;
461 } else
462 state = LSB;
463 }
464 }
465 }
466
467 /* If the init script has no LSB header, then let's
468 * enforce the ordering via the chkconfig
469 * priorities */
470
471 if (!has_lsb)
472 if ((r = sysv_chkconfig_order(s)) < 0)
473 goto finish;
474
475 if ((r = sysv_exec_commands(s)) < 0)
476 goto finish;
477
478 r = 1;
479
480 finish:
481
482 if (f)
483 fclose(f);
484
485 return r;
486 }
487
488 static int service_load_sysv_name(Service *s, const char *name) {
489 char **p;
490
491 assert(s);
492 assert(name);
493
494 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
495 char *path;
496 int r;
497
498 if (asprintf(&path, "%s/%s", *p, name) < 0)
499 return -ENOMEM;
500
501 assert(endswith(path, ".service"));
502 path[strlen(path)-8] = 0;
503
504 r = service_load_sysv_path(s, path);
505 free(path);
506
507 if (r >= 0)
508 return r;
509 }
510
511 return 0;
512 }
513
514 static int service_load_sysv(Service *s) {
515 const char *t;
516 Iterator i;
517 int r;
518
519 assert(s);
520
521 /* Load service data from SysV init scripts, preferably with
522 * LSB headers ... */
523
524 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
525 return 0;
526
527 if ((t = unit_id(UNIT(s))))
528 if ((r = service_load_sysv_name(s, t)) != 0)
529 return r;
530
531 SET_FOREACH(t, UNIT(s)->meta.names, i)
532 if ((r == service_load_sysv_name(s, t)) != 0)
533 return r;
534
535 return 0;
536 }
537
538 static int service_init(Unit *u) {
539 int r;
540 Service *s = SERVICE(u);
541
542 assert(s);
543
544 /* First, reset everything to the defaults, in case this is a
545 * reload */
546
547 s->type = 0;
548 s->restart = 0;
549
550 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
551 s->restart_usec = DEFAULT_RESTART_USEC;
552
553 exec_context_init(&s->exec_context);
554
555 s->timer_watch.type = WATCH_INVALID;
556
557 s->state = SERVICE_DEAD;
558
559 s->sysv_start_priority = -1;
560 s->permissions_start_only = false;
561 s->root_directory_start_only = false;
562
563 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
564
565 /* Load a .service file */
566 if ((r = unit_load_fragment(u)) < 0) {
567 service_done(u);
568 return r;
569 }
570
571 /* Load a classic init script as a fallback, if we couldn*t find anything */
572 if (r == 0)
573 if ((r = service_load_sysv(s)) <= 0) {
574 service_done(u);
575 return r < 0 ? r : -ENOENT;
576 }
577
578 /* Load dropin directory data */
579 if ((r = unit_load_dropin(u)) < 0) {
580 service_done(u);
581 return r;
582 }
583
584 return 0;
585 }
586
587 static void service_dump(Unit *u, FILE *f, const char *prefix) {
588
589 ServiceExecCommand c;
590 Service *s = SERVICE(u);
591 const char *prefix2;
592 char *p2;
593
594 assert(s);
595
596 p2 = strappend(prefix, "\t");
597 prefix2 = p2 ? p2 : prefix;
598
599 fprintf(f,
600 "%sService State: %s\n"
601 "%sPermissionsStartOnly: %s\n"
602 "%sRootDirectoryStartOnly: %s\n",
603 prefix, service_state_to_string(s->state),
604 prefix, yes_no(s->permissions_start_only),
605 prefix, yes_no(s->root_directory_start_only));
606
607 if (s->pid_file)
608 fprintf(f,
609 "%sPIDFile: %s\n",
610 prefix, s->pid_file);
611
612
613 exec_context_dump(&s->exec_context, f, prefix);
614
615 for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
616
617 if (!s->exec_command[c])
618 continue;
619
620 fprintf(f, "%s→ %s:\n",
621 prefix, service_exec_command_to_string(c));
622
623 exec_command_dump_list(s->exec_command[c], f, prefix2);
624 }
625
626 if (s->sysv_path)
627 fprintf(f,
628 "%sSysV Init Script Path: %s\n",
629 prefix, s->sysv_path);
630
631 if (s->sysv_start_priority >= 0)
632 fprintf(f,
633 "%sSysV Start Priority: %i\n",
634 prefix, s->sysv_start_priority);
635
636 free(p2);
637 }
638
639 static int service_load_pid_file(Service *s) {
640 char *k;
641 unsigned long p;
642 int r;
643
644 assert(s);
645
646 if (s->main_pid_known)
647 return 0;
648
649 if (!s->pid_file)
650 return -ENOENT;
651
652 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
653 return r;
654
655 if ((r = safe_atolu(k, &p)) < 0) {
656 free(k);
657 return r;
658 }
659
660 if ((unsigned long) (pid_t) p != p)
661 return -ERANGE;
662
663 s->main_pid = p;
664 s->main_pid_known = true;
665
666 return 0;
667 }
668
669 static int service_get_sockets(Service *s, Set **_set) {
670 Set *set;
671 Iterator i;
672 char *t;
673 int r;
674
675 assert(s);
676 assert(_set);
677
678 /* Collects all Socket objects that belong to this
679 * service. Note that a service might have multiple sockets
680 * via multiple names. */
681
682 if (!(set = set_new(NULL, NULL)))
683 return -ENOMEM;
684
685 SET_FOREACH(t, UNIT(s)->meta.names, i) {
686 char *k;
687 Unit *p;
688
689 /* Look for all socket objects that go by any of our
690 * units and collect their fds */
691
692 if (!(k = unit_name_change_suffix(t, ".socket"))) {
693 r = -ENOMEM;
694 goto fail;
695 }
696
697 p = manager_get_unit(UNIT(s)->meta.manager, k);
698 free(k);
699
700 if (!p) continue;
701
702 if ((r = set_put(set, p)) < 0)
703 goto fail;
704 }
705
706 *_set = set;
707 return 0;
708
709 fail:
710 set_free(set);
711 return r;
712 }
713
714
715 static int service_notify_sockets(Service *s) {
716 Iterator i;
717 Set *set;
718 Socket *sock;
719 int r;
720
721 assert(s);
722
723 /* Notifies all our sockets when we die */
724
725 if ((r = service_get_sockets(s, &set)) < 0)
726 return r;
727
728 SET_FOREACH(sock, set, i)
729 socket_notify_service_dead(sock);
730
731 set_free(set);
732
733 return 0;
734 }
735
736 static void service_set_state(Service *s, ServiceState state) {
737 ServiceState old_state;
738 assert(s);
739
740 old_state = s->state;
741 s->state = state;
742
743 if (state != SERVICE_START_PRE &&
744 state != SERVICE_START &&
745 state != SERVICE_START_POST &&
746 state != SERVICE_RELOAD &&
747 state != SERVICE_STOP &&
748 state != SERVICE_STOP_SIGTERM &&
749 state != SERVICE_STOP_SIGKILL &&
750 state != SERVICE_STOP_POST &&
751 state != SERVICE_FINAL_SIGTERM &&
752 state != SERVICE_FINAL_SIGKILL &&
753 state != SERVICE_AUTO_RESTART)
754 unit_unwatch_timer(UNIT(s), &s->timer_watch);
755
756 if (state != SERVICE_START &&
757 state != SERVICE_START_POST &&
758 state != SERVICE_RUNNING &&
759 state != SERVICE_RELOAD &&
760 state != SERVICE_STOP &&
761 state != SERVICE_STOP_SIGTERM &&
762 state != SERVICE_STOP_SIGKILL)
763 if (s->main_pid > 0) {
764 unit_unwatch_pid(UNIT(s), s->main_pid);
765 s->main_pid = 0;
766 }
767
768 if (state != SERVICE_START_PRE &&
769 state != SERVICE_START &&
770 state != SERVICE_START_POST &&
771 state != SERVICE_RELOAD &&
772 state != SERVICE_STOP &&
773 state != SERVICE_STOP_SIGTERM &&
774 state != SERVICE_STOP_SIGKILL &&
775 state != SERVICE_STOP_POST &&
776 state != SERVICE_FINAL_SIGTERM &&
777 state != SERVICE_FINAL_SIGKILL)
778 if (s->control_pid > 0) {
779 unit_unwatch_pid(UNIT(s), s->control_pid);
780 s->control_pid = 0;
781 }
782
783 if (state != SERVICE_START_PRE &&
784 state != SERVICE_START &&
785 state != SERVICE_START_POST &&
786 state != SERVICE_RELOAD &&
787 state != SERVICE_STOP &&
788 state != SERVICE_STOP_POST)
789 s->control_command = NULL;
790
791 if (state == SERVICE_DEAD ||
792 state == SERVICE_STOP ||
793 state == SERVICE_STOP_SIGTERM ||
794 state == SERVICE_STOP_SIGKILL ||
795 state == SERVICE_STOP_POST ||
796 state == SERVICE_FINAL_SIGTERM ||
797 state == SERVICE_FINAL_SIGKILL ||
798 state == SERVICE_MAINTAINANCE ||
799 state == SERVICE_AUTO_RESTART)
800 service_notify_sockets(s);
801
802 log_debug("%s changed %s → %s", unit_id(UNIT(s)), service_state_to_string(old_state), service_state_to_string(state));
803
804 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
805 }
806
807 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
808 Iterator i;
809 int r;
810 int *rfds = NULL;
811 unsigned rn_fds = 0;
812 Set *set;
813 Socket *sock;
814
815 assert(s);
816 assert(fds);
817 assert(n_fds);
818
819 if ((r = service_get_sockets(s, &set)) < 0)
820 return r;
821
822 SET_FOREACH(sock, set, i) {
823 int *cfds;
824 unsigned cn_fds;
825
826 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
827 goto fail;
828
829 if (!cfds)
830 continue;
831
832 if (!rfds) {
833 rfds = cfds;
834 rn_fds = cn_fds;
835 } else {
836 int *t;
837
838 if (!(t = new(int, rn_fds+cn_fds))) {
839 free(cfds);
840 r = -ENOMEM;
841 goto fail;
842 }
843
844 memcpy(t, rfds, rn_fds);
845 memcpy(t+rn_fds, cfds, cn_fds);
846 free(rfds);
847 free(cfds);
848
849 rfds = t;
850 rn_fds = rn_fds+cn_fds;
851 }
852 }
853
854 *fds = rfds;
855 *n_fds = rn_fds;
856
857 set_free(set);
858
859 return 0;
860
861 fail:
862 set_free(set);
863 free(rfds);
864
865 return r;
866 }
867
868 static int service_spawn(
869 Service *s,
870 ExecCommand *c,
871 bool timeout,
872 bool pass_fds,
873 bool apply_permissions,
874 bool apply_chroot,
875 pid_t *_pid) {
876
877 pid_t pid;
878 int r;
879 int *fds = NULL;
880 unsigned n_fds = 0;
881
882 assert(s);
883 assert(c);
884 assert(_pid);
885
886 if (pass_fds)
887 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
888 goto fail;
889
890 if (timeout) {
891 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
892 goto fail;
893 } else
894 unit_unwatch_timer(UNIT(s), &s->timer_watch);
895
896 if ((r = exec_spawn(c,
897 &s->exec_context,
898 fds, n_fds,
899 apply_permissions,
900 apply_chroot,
901 &pid)) < 0)
902 goto fail;
903
904 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
905 /* FIXME: we need to do something here */
906 goto fail;
907
908 free(fds);
909 *_pid = pid;
910
911 return 0;
912
913 fail:
914 free(fds);
915
916 if (timeout)
917 unit_unwatch_timer(UNIT(s), &s->timer_watch);
918
919 return r;
920 }
921
922 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
923 int r;
924 assert(s);
925
926 if (!success)
927 s->failure = true;
928
929 if (allow_restart &&
930 (s->restart == SERVICE_RESTART_ALWAYS ||
931 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
932
933 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
934 goto fail;
935
936 service_set_state(s, SERVICE_AUTO_RESTART);
937 } else
938 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
939
940 return;
941
942 fail:
943 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
944 service_enter_dead(s, false, false);
945 }
946
947 static void service_enter_signal(Service *s, ServiceState state, bool success);
948
949 static void service_enter_stop_post(Service *s, bool success) {
950 int r;
951 assert(s);
952
953 if (!success)
954 s->failure = true;
955
956 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
957 if ((r = service_spawn(s,
958 s->control_command,
959 true,
960 false,
961 !s->permissions_start_only,
962 !s->root_directory_start_only,
963 &s->control_pid)) < 0)
964 goto fail;
965
966
967 service_set_state(s, SERVICE_STOP_POST);
968
969 if (!s->control_command)
970 service_enter_dead(s, true, true);
971
972 return;
973
974 fail:
975 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
976 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
977 }
978
979 static void service_enter_signal(Service *s, ServiceState state, bool success) {
980 int r;
981 bool sent = false;
982
983 assert(s);
984
985 if (!success)
986 s->failure = true;
987
988 if (s->main_pid > 0 || s->control_pid > 0) {
989 int sig;
990
991 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
992
993 r = 0;
994 if (s->main_pid > 0) {
995 if (kill(s->main_pid, sig) < 0 && errno != ESRCH)
996 r = -errno;
997 else
998 sent = true;
999 }
1000
1001 if (s->control_pid > 0) {
1002 if (kill(s->control_pid, sig) < 0 && errno != ESRCH)
1003 r = -errno;
1004 else
1005 sent = true;
1006 }
1007
1008 if (r < 0)
1009 goto fail;
1010 }
1011
1012 service_set_state(s, state);
1013
1014 if (s->main_pid <= 0 && s->control_pid <= 0)
1015 service_enter_dead(s, true, true);
1016
1017 return;
1018
1019 fail:
1020 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
1021
1022 if (sent) {
1023 s->failure = true;
1024 service_set_state(s, state);
1025 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1026 service_enter_stop_post(s, false);
1027 else
1028 service_enter_dead(s, false, true);
1029 }
1030
1031 static void service_enter_stop(Service *s, bool success) {
1032 int r;
1033 assert(s);
1034
1035 if (!success)
1036 s->failure = true;
1037
1038 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
1039 if ((r = service_spawn(s,
1040 s->control_command,
1041 true,
1042 false,
1043 !s->permissions_start_only,
1044 !s->root_directory_start_only,
1045 &s->control_pid)) < 0)
1046 goto fail;
1047
1048 service_set_state(s, SERVICE_STOP);
1049
1050 if (!s->control_command)
1051 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1052
1053 return;
1054
1055 fail:
1056 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1057 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1058 }
1059
1060 static void service_enter_start_post(Service *s) {
1061 int r;
1062 assert(s);
1063
1064 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
1065 if ((r = service_spawn(s,
1066 s->control_command,
1067 true,
1068 false,
1069 !s->permissions_start_only,
1070 !s->root_directory_start_only,
1071 &s->control_pid)) < 0)
1072 goto fail;
1073
1074
1075 service_set_state(s, SERVICE_START_POST);
1076
1077 if (!s->control_command)
1078 service_set_state(s, SERVICE_RUNNING);
1079
1080 return;
1081
1082 fail:
1083 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1084 service_enter_stop(s, false);
1085 }
1086
1087 static void service_enter_start(Service *s) {
1088 pid_t pid;
1089 int r;
1090
1091 assert(s);
1092
1093 assert(s->exec_command[SERVICE_EXEC_START]);
1094 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1095
1096 if ((r = service_spawn(s,
1097 s->exec_command[SERVICE_EXEC_START],
1098 s->type == SERVICE_FORKING,
1099 true,
1100 true,
1101 true,
1102 &pid)) < 0)
1103 goto fail;
1104
1105 service_set_state(s, SERVICE_START);
1106
1107 if (s->type == SERVICE_SIMPLE) {
1108 /* For simple services we immediately start
1109 * the START_POST binaries. */
1110
1111 s->main_pid = pid;
1112 s->main_pid_known = true;
1113 service_enter_start_post(s);
1114
1115 } else if (s->type == SERVICE_FORKING) {
1116
1117 /* For forking services we wait until the start
1118 * process exited. */
1119
1120 s->control_pid = pid;
1121 s->control_command = s->exec_command[SERVICE_EXEC_START];
1122 } else if (s->type == SERVICE_FINISH) {
1123
1124 /* For finishing services we wait until the start
1125 * process exited, too, but it is our main process. */
1126
1127 s->main_pid = pid;
1128 s->control_command = s->exec_command[SERVICE_EXEC_START];
1129 } else
1130 assert_not_reached("Unknown service type");
1131
1132 return;
1133
1134 fail:
1135 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
1136 service_enter_stop(s, false);
1137 }
1138
1139 static void service_enter_start_pre(Service *s) {
1140 int r;
1141
1142 assert(s);
1143
1144 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
1145 if ((r = service_spawn(s,
1146 s->control_command,
1147 true,
1148 false,
1149 !s->permissions_start_only,
1150 !s->root_directory_start_only,
1151 &s->control_pid)) < 0)
1152 goto fail;
1153
1154 service_set_state(s, SERVICE_START_PRE);
1155
1156 if (!s->control_command)
1157 service_enter_start(s);
1158
1159 return;
1160
1161 fail:
1162 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
1163 service_enter_dead(s, false, true);
1164 }
1165
1166 static void service_enter_restart(Service *s) {
1167 int r;
1168 assert(s);
1169
1170 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1171 goto fail;
1172
1173 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
1174 service_enter_dead(s, true, false);
1175 return;
1176
1177 fail:
1178
1179 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
1180 service_enter_dead(s, false, false);
1181 }
1182
1183 static void service_enter_reload(Service *s) {
1184 int r;
1185
1186 assert(s);
1187
1188 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
1189 if ((r = service_spawn(s,
1190 s->control_command,
1191 true,
1192 false,
1193 !s->permissions_start_only,
1194 !s->root_directory_start_only,
1195 &s->control_pid)) < 0)
1196 goto fail;
1197
1198 service_set_state(s, SERVICE_RELOAD);
1199
1200 if (!s->control_command)
1201 service_set_state(s, SERVICE_RUNNING);
1202
1203 return;
1204
1205 fail:
1206 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
1207 service_enter_stop(s, false);
1208 }
1209
1210 static void service_run_next(Service *s, bool success) {
1211 int r;
1212
1213 assert(s);
1214 assert(s->control_command);
1215 assert(s->control_command->command_next);
1216
1217 if (!success)
1218 s->failure = true;
1219
1220 s->control_command = s->control_command->command_next;
1221
1222 if ((r = service_spawn(s,
1223 s->control_command,
1224 true,
1225 false,
1226 !s->permissions_start_only,
1227 !s->root_directory_start_only,
1228 &s->control_pid)) < 0)
1229 goto fail;
1230
1231 return;
1232
1233 fail:
1234 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
1235
1236 if (s->state == SERVICE_STOP)
1237 service_enter_stop_post(s, false);
1238 else if (s->state == SERVICE_STOP_POST)
1239 service_enter_dead(s, false, true);
1240 else
1241 service_enter_stop(s, false);
1242 }
1243
1244 static int service_start(Unit *u) {
1245 Service *s = SERVICE(u);
1246
1247 assert(s);
1248
1249 /* We cannot fulfill this request right now, try again later
1250 * please! */
1251 if (s->state == SERVICE_STOP ||
1252 s->state == SERVICE_STOP_SIGTERM ||
1253 s->state == SERVICE_STOP_SIGKILL ||
1254 s->state == SERVICE_STOP_POST ||
1255 s->state == SERVICE_FINAL_SIGTERM ||
1256 s->state == SERVICE_FINAL_SIGKILL)
1257 return -EAGAIN;
1258
1259 /* Already on it! */
1260 if (s->state == SERVICE_START_PRE ||
1261 s->state == SERVICE_START ||
1262 s->state == SERVICE_START_POST)
1263 return 0;
1264
1265 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1266
1267 /* Make sure we don't enter a busy loop of some kind. */
1268 if (!ratelimit_test(&s->ratelimit)) {
1269 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
1270 return -EAGAIN;
1271 }
1272
1273 s->failure = false;
1274 s->main_pid_known = false;
1275
1276 service_enter_start_pre(s);
1277 return 0;
1278 }
1279
1280 static int service_stop(Unit *u) {
1281 Service *s = SERVICE(u);
1282
1283 assert(s);
1284
1285 if (s->state == SERVICE_START_PRE ||
1286 s->state == SERVICE_START ||
1287 s->state == SERVICE_START_POST ||
1288 s->state == SERVICE_RELOAD)
1289 return -EAGAIN;
1290
1291 if (s->state == SERVICE_AUTO_RESTART) {
1292 service_set_state(s, SERVICE_DEAD);
1293 return 0;
1294 }
1295
1296 assert(s->state == SERVICE_RUNNING);
1297
1298 service_enter_stop(s, true);
1299 return 0;
1300 }
1301
1302 static int service_reload(Unit *u) {
1303 Service *s = SERVICE(u);
1304
1305 assert(s);
1306
1307 assert(s->state == SERVICE_RUNNING);
1308
1309 service_enter_reload(s);
1310 return 0;
1311 }
1312
1313 static bool service_can_reload(Unit *u) {
1314 Service *s = SERVICE(u);
1315
1316 assert(s);
1317
1318 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1319 }
1320
1321 static UnitActiveState service_active_state(Unit *u) {
1322 assert(u);
1323
1324 return state_translation_table[SERVICE(u)->state];
1325 }
1326
1327 static int main_pid_good(Service *s) {
1328 assert(s);
1329
1330 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1331 * don't know */
1332
1333 /* If we know the pid file, then lets just check if it is
1334 * still valid */
1335 if (s->main_pid_known)
1336 return s->main_pid > 0;
1337
1338 /* We don't know the pid */
1339 return -1;
1340 }
1341
1342 static bool control_pid_good(Service *s) {
1343 assert(s);
1344
1345 return s->control_pid > 0;
1346 }
1347
1348 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1349 Service *s = SERVICE(u);
1350 bool success;
1351
1352 assert(s);
1353 assert(pid >= 0);
1354
1355 success = code == CLD_EXITED && status == 0;
1356 s->failure = s->failure || !success;
1357
1358 if (s->main_pid == pid) {
1359
1360 exec_status_fill(&s->main_exec_status, pid, code, status);
1361 s->main_pid = 0;
1362
1363 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1364 assert(s->exec_command[SERVICE_EXEC_START]);
1365 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1366 }
1367
1368 log_debug("%s: main process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1369
1370 /* The service exited, so the service is officially
1371 * gone. */
1372
1373 switch (s->state) {
1374
1375 case SERVICE_START_POST:
1376 case SERVICE_RELOAD:
1377 case SERVICE_STOP:
1378 /* Need to wait until the operation is
1379 * done */
1380 break;
1381
1382 case SERVICE_START:
1383 assert(s->type == SERVICE_FINISH);
1384
1385 /* This was our main goal, so let's go on */
1386 if (success)
1387 service_enter_start_post(s);
1388 else
1389 service_enter_stop(s, false);
1390 break;
1391
1392 case SERVICE_RUNNING:
1393 service_enter_stop(s, success);
1394 break;
1395
1396 case SERVICE_STOP_SIGTERM:
1397 case SERVICE_STOP_SIGKILL:
1398
1399 if (!control_pid_good(s))
1400 service_enter_stop_post(s, success);
1401
1402 /* If there is still a control process, wait for that first */
1403 break;
1404
1405 default:
1406 assert_not_reached("Uh, main process died at wrong time.");
1407 }
1408
1409 } else if (s->control_pid == pid) {
1410 assert(s->control_command);
1411
1412 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1413 s->control_pid = 0;
1414
1415 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1416
1417 /* If we are shutting things down anyway we
1418 * don't care about failing commands. */
1419
1420 if (s->control_command->command_next &&
1421 (success || (s->state == SERVICE_EXEC_STOP || s->state == SERVICE_EXEC_STOP_POST)))
1422
1423 /* There is another command to *
1424 * execute, so let's do that. */
1425
1426 service_run_next(s, success);
1427
1428 else {
1429 /* No further commands for this step, so let's
1430 * figure out what to do next */
1431
1432 log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state));
1433
1434 switch (s->state) {
1435
1436 case SERVICE_START_PRE:
1437 if (success)
1438 service_enter_start(s);
1439 else
1440 service_enter_stop(s, false);
1441 break;
1442
1443 case SERVICE_START:
1444 assert(s->type == SERVICE_FORKING);
1445
1446 /* Let's try to load the pid
1447 * file here if we can. We
1448 * ignore the return value,
1449 * since the PID file might
1450 * actually be created by a
1451 * START_POST script */
1452
1453 if (success) {
1454 if (s->pid_file)
1455 service_load_pid_file(s);
1456
1457 service_enter_start_post(s);
1458 } else
1459 service_enter_stop(s, false);
1460
1461 break;
1462
1463 case SERVICE_START_POST:
1464 if (success && s->pid_file && !s->main_pid_known) {
1465 int r;
1466
1467 /* Hmm, let's see if we can
1468 * load the pid now after the
1469 * start-post scripts got
1470 * executed. */
1471
1472 if ((r = service_load_pid_file(s)) < 0)
1473 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
1474 }
1475
1476 /* Fall through */
1477
1478 case SERVICE_RELOAD:
1479 if (success) {
1480 if (main_pid_good(s) != 0)
1481 service_set_state(s, SERVICE_RUNNING);
1482 else
1483 service_enter_stop(s, true);
1484 } else
1485 service_enter_stop(s, false);
1486
1487 break;
1488
1489 case SERVICE_STOP:
1490 if (main_pid_good(s) > 0)
1491 /* Still not dead and we know the PID? Let's go hunting. */
1492 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1493 else
1494 service_enter_stop_post(s, success);
1495 break;
1496
1497 case SERVICE_STOP_SIGTERM:
1498 case SERVICE_STOP_SIGKILL:
1499 if (main_pid_good(s) <= 0)
1500 service_enter_stop_post(s, success);
1501
1502 /* If there is still a service
1503 * process around, wait until
1504 * that one quit, too */
1505 break;
1506
1507 case SERVICE_STOP_POST:
1508 case SERVICE_FINAL_SIGTERM:
1509 case SERVICE_FINAL_SIGKILL:
1510 service_enter_dead(s, success, true);
1511 break;
1512
1513 default:
1514 assert_not_reached("Uh, control process died at wrong time.");
1515 }
1516 }
1517 } else
1518 assert_not_reached("Got SIGCHLD for unkown PID");
1519 }
1520
1521 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1522 Service *s = SERVICE(u);
1523
1524 assert(s);
1525 assert(elapsed == 1);
1526
1527 assert(w == &s->timer_watch);
1528
1529 switch (s->state) {
1530
1531 case SERVICE_START_PRE:
1532 case SERVICE_START:
1533 case SERVICE_START_POST:
1534 case SERVICE_RELOAD:
1535 log_warning("%s operation timed out. Stopping.", unit_id(u));
1536 service_enter_stop(s, false);
1537 break;
1538
1539 case SERVICE_STOP:
1540 log_warning("%s stopping timed out. Terminating.", unit_id(u));
1541 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1542 break;
1543
1544 case SERVICE_STOP_SIGTERM:
1545 log_warning("%s stopping timed out. Killing.", unit_id(u));
1546 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1547 break;
1548
1549 case SERVICE_STOP_SIGKILL:
1550 /* Uh, wie sent a SIGKILL and it is still not gone?
1551 * Must be something we cannot kill, so let's just be
1552 * weirded out and continue */
1553
1554 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1555 service_enter_stop_post(s, false);
1556 break;
1557
1558 case SERVICE_STOP_POST:
1559 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1560 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1561 break;
1562
1563 case SERVICE_FINAL_SIGTERM:
1564 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1565 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1566 break;
1567
1568 case SERVICE_FINAL_SIGKILL:
1569 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1570 service_enter_dead(s, false, true);
1571 break;
1572
1573 case SERVICE_AUTO_RESTART:
1574 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1575 service_enter_restart(s);
1576 break;
1577
1578 default:
1579 assert_not_reached("Timeout at wrong time.");
1580 }
1581 }
1582
1583 static int service_enumerate(Manager *m) {
1584
1585 static const char * const rcnd[] = {
1586 "../rc0.d", SPECIAL_RUNLEVEL0_TARGET,
1587 "../rc1.d", SPECIAL_RUNLEVEL1_TARGET,
1588 "../rc2.d", SPECIAL_RUNLEVEL2_TARGET,
1589 "../rc3.d", SPECIAL_RUNLEVEL3_TARGET,
1590 "../rc4.d", SPECIAL_RUNLEVEL4_TARGET,
1591 "../rc5.d", SPECIAL_RUNLEVEL5_TARGET,
1592 "../rc6.d", SPECIAL_RUNLEVEL6_TARGET
1593 };
1594
1595 char **p;
1596 unsigned i;
1597 DIR *d = NULL;
1598 char *path = NULL, *fpath = NULL, *name = NULL;
1599 int r;
1600
1601 assert(m);
1602
1603 STRV_FOREACH(p, m->sysvinit_path)
1604 for (i = 0; i < ELEMENTSOF(rcnd); i += 2) {
1605 struct dirent *de;
1606
1607 free(path);
1608 path = NULL;
1609 if (asprintf(&path, "%s/%s", *p, rcnd[i]) < 0) {
1610 r = -ENOMEM;
1611 goto finish;
1612 }
1613
1614 if (d)
1615 closedir(d);
1616
1617 if (!(d = opendir(path))) {
1618 if (errno != ENOENT)
1619 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1620
1621 continue;
1622 }
1623
1624 while ((de = readdir(d))) {
1625 Unit *runlevel, *service;
1626
1627 if (ignore_file(de->d_name))
1628 continue;
1629
1630 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1631 continue;
1632
1633 if (strlen(de->d_name) < 4)
1634 continue;
1635
1636 free(fpath);
1637 fpath = NULL;
1638 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd[i], de->d_name) < 0) {
1639 r = -ENOMEM;
1640 goto finish;
1641 }
1642
1643 if (access(fpath, X_OK) < 0) {
1644
1645 if (errno != ENOENT)
1646 log_warning("access() failed on %s: %s", fpath, strerror(errno));
1647
1648 continue;
1649 }
1650
1651 free(name);
1652 name = NULL;
1653 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
1654 r = -ENOMEM;
1655 goto finish;
1656 }
1657
1658 if ((r = manager_load_unit(m, name, &service)) < 0)
1659 goto finish;
1660
1661 if ((r = manager_load_unit(m, rcnd[i+1], &runlevel)) < 0)
1662 goto finish;
1663
1664 if (de->d_name[0] == 'S') {
1665 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
1666 goto finish;
1667
1668 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
1669 goto finish;
1670 } else {
1671 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
1672 goto finish;
1673
1674 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
1675 goto finish;
1676 }
1677 }
1678 }
1679
1680 r = 0;
1681
1682 finish:
1683 free(path);
1684 free(fpath);
1685 free(name);
1686 closedir(d);
1687
1688 return r;
1689 }
1690
1691 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
1692 [SERVICE_DEAD] = "dead",
1693 [SERVICE_START_PRE] = "start-pre",
1694 [SERVICE_START] = "start",
1695 [SERVICE_START_POST] = "start-post",
1696 [SERVICE_RUNNING] = "running",
1697 [SERVICE_RELOAD] = "reload",
1698 [SERVICE_STOP] = "stop",
1699 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
1700 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
1701 [SERVICE_STOP_POST] = "stop-post",
1702 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
1703 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
1704 [SERVICE_MAINTAINANCE] = "maintainance",
1705 [SERVICE_AUTO_RESTART] = "auto-restart",
1706 };
1707
1708 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
1709
1710 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
1711 [SERVICE_ONCE] = "once",
1712 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
1713 [SERVICE_RESTART_ALWAYS] = "restart-on-failure",
1714 };
1715
1716 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
1717
1718 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
1719 [SERVICE_FORKING] = "forking",
1720 [SERVICE_SIMPLE] = "simple",
1721 [SERVICE_FINISH] = "finish"
1722 };
1723
1724 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
1725
1726 static const char* const service_exec_command_table[_SERVICE_EXEC_MAX] = {
1727 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
1728 [SERVICE_EXEC_START] = "ExecStart",
1729 [SERVICE_EXEC_START_POST] = "ExecStartPost",
1730 [SERVICE_EXEC_RELOAD] = "ExecReload",
1731 [SERVICE_EXEC_STOP] = "ExecStop",
1732 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
1733 };
1734
1735 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
1736
1737 const UnitVTable service_vtable = {
1738 .suffix = ".service",
1739
1740 .init = service_init,
1741 .done = service_done,
1742
1743 .dump = service_dump,
1744
1745 .start = service_start,
1746 .stop = service_stop,
1747 .reload = service_reload,
1748
1749 .can_reload = service_can_reload,
1750
1751 .active_state = service_active_state,
1752
1753 .sigchld_event = service_sigchld_event,
1754 .timer_event = service_timer_event,
1755
1756 .enumerate = service_enumerate
1757 };