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