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