]> git.ipfire.org Git - people/ms/systemd.git/blob - service.c
execute: make kill mode configurable
[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 (!s->sysv_runlevels || chars_intersect("12345", s->sysv_runlevels)) {
596 /* If there a runlevels configured for this service
597 * but none of the standard ones, then we assume this
598 * is some special kind of service (which might be
599 * needed for early boot) and don't create any links
600 * to it. */
601
602 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET)) < 0 ||
603 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET)) < 0)
604 goto finish;
605 }
606
607 *new_state = UNIT_LOADED;
608 r = 0;
609
610 finish:
611
612 if (f)
613 fclose(f);
614
615 return r;
616 }
617
618 static int service_load_sysv_name(Service *s, const char *name, UnitLoadState *new_state) {
619 char **p;
620
621 assert(s);
622 assert(name);
623
624 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
625 char *path;
626 int r;
627
628 if (asprintf(&path, "%s/%s", *p, name) < 0)
629 return -ENOMEM;
630
631 assert(endswith(path, ".service"));
632 path[strlen(path)-8] = 0;
633
634 r = service_load_sysv_path(s, path, new_state);
635 free(path);
636
637 if (r < 0)
638 return r;
639
640 if (*new_state != UNIT_STUB)
641 break;
642 }
643
644 return 0;
645 }
646
647 static int service_load_sysv(Service *s, UnitLoadState *new_state) {
648 const char *t;
649 Iterator i;
650 int r;
651
652 assert(s);
653 assert(new_state);
654
655 /* Load service data from SysV init scripts, preferably with
656 * LSB headers ... */
657
658 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
659 return 0;
660
661 if ((t = unit_id(UNIT(s))))
662 if ((r = service_load_sysv_name(s, t, new_state)) < 0)
663 return r;
664
665 if (*new_state == UNIT_STUB)
666 SET_FOREACH(t, UNIT(s)->meta.names, i) {
667 if ((r == service_load_sysv_name(s, t, new_state)) < 0)
668 return r;
669
670 if (*new_state != UNIT_STUB)
671 break;
672 }
673
674 return 0;
675 }
676
677 static int service_init(Unit *u, UnitLoadState *new_state) {
678 int r;
679 Service *s = SERVICE(u);
680
681 assert(s);
682 assert(new_state);
683 assert(*new_state == UNIT_STUB);
684
685 /* First, reset everything to the defaults, in case this is a
686 * reload */
687
688 s->type = 0;
689 s->restart = 0;
690
691 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
692 s->restart_usec = DEFAULT_RESTART_USEC;
693
694 exec_context_init(&s->exec_context);
695
696 s->timer_watch.type = WATCH_INVALID;
697
698 s->state = SERVICE_DEAD;
699
700 s->sysv_start_priority = -1;
701 s->permissions_start_only = false;
702 s->root_directory_start_only = false;
703 s->valid_no_process = false;
704 s->kill_mode = 0;
705 s->sysv_has_lsb = false;
706 s->main_pid = s->control_pid = 0;
707 s->main_pid_known = false;
708 s->failure = false;
709
710 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
711
712 /* Load a .service file */
713 if ((r = unit_load_fragment(u, new_state)) < 0)
714 return r;
715
716 /* Load a classic init script as a fallback, if we couldn't find anything */
717 if (*new_state == UNIT_STUB)
718 if ((r = service_load_sysv(s, new_state)) < 0)
719 return r;
720
721 /* Still nothing found? Then let's give up */
722 if (*new_state == UNIT_STUB)
723 return -ENOENT;
724
725 /* We were able to load something, then let's add in the
726 * dropin directories. */
727 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
728 return r;
729
730 /* This is a new unit? Then let's add in some extras */
731 if (*new_state == UNIT_LOADED) {
732 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
733 return r;
734
735 if ((r = unit_add_default_cgroup(u)) < 0)
736 return r;
737
738 if ((r = sysv_chkconfig_order(s)) < 0)
739 return r;
740 }
741
742 return 0;
743 }
744
745 static void service_dump(Unit *u, FILE *f, const char *prefix) {
746
747 ServiceExecCommand c;
748 Service *s = SERVICE(u);
749 const char *prefix2;
750 char *p2;
751
752 assert(s);
753
754 p2 = strappend(prefix, "\t");
755 prefix2 = p2 ? p2 : prefix;
756
757 fprintf(f,
758 "%sService State: %s\n"
759 "%sPermissionsStartOnly: %s\n"
760 "%sRootDirectoryStartOnly: %s\n"
761 "%sValidNoProcess: %s\n"
762 "%sKillMode: %s\n"
763 "%sType: %s\n",
764 prefix, service_state_to_string(s->state),
765 prefix, yes_no(s->permissions_start_only),
766 prefix, yes_no(s->root_directory_start_only),
767 prefix, yes_no(s->valid_no_process),
768 prefix, kill_mode_to_string(s->kill_mode),
769 prefix, service_type_to_string(s->type));
770
771 if (s->pid_file)
772 fprintf(f,
773 "%sPIDFile: %s\n",
774 prefix, s->pid_file);
775
776 exec_context_dump(&s->exec_context, f, prefix);
777
778 for (c = 0; c < _SERVICE_EXEC_MAX; c++) {
779
780 if (!s->exec_command[c])
781 continue;
782
783 fprintf(f, "%s→ %s:\n",
784 prefix, service_exec_command_to_string(c));
785
786 exec_command_dump_list(s->exec_command[c], f, prefix2);
787 }
788
789 if (s->sysv_path)
790 fprintf(f,
791 "%sSysV Init Script Path: %s\n"
792 "%sSysV Init Script has LSB Header: %s\n",
793 prefix, s->sysv_path,
794 prefix, yes_no(s->sysv_has_lsb));
795
796 if (s->sysv_start_priority >= 0)
797 fprintf(f,
798 "%sSysVStartPriority: %i\n",
799 prefix, s->sysv_start_priority);
800
801 if (s->sysv_runlevels)
802 fprintf(f, "%sSysVRunLevels: %s\n",
803 prefix, s->sysv_runlevels);
804
805 free(p2);
806 }
807
808 static int service_load_pid_file(Service *s) {
809 char *k;
810 unsigned long p;
811 int r;
812
813 assert(s);
814
815 if (s->main_pid_known)
816 return 0;
817
818 if (!s->pid_file)
819 return -ENOENT;
820
821 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
822 return r;
823
824 if ((r = safe_atolu(k, &p)) < 0) {
825 free(k);
826 return r;
827 }
828
829 if ((unsigned long) (pid_t) p != p)
830 return -ERANGE;
831
832 s->main_pid = p;
833 s->main_pid_known = true;
834
835 return 0;
836 }
837
838 static int service_get_sockets(Service *s, Set **_set) {
839 Set *set;
840 Iterator i;
841 char *t;
842 int r;
843
844 assert(s);
845 assert(_set);
846
847 /* Collects all Socket objects that belong to this
848 * service. Note that a service might have multiple sockets
849 * via multiple names. */
850
851 if (!(set = set_new(NULL, NULL)))
852 return -ENOMEM;
853
854 SET_FOREACH(t, UNIT(s)->meta.names, i) {
855 char *k;
856 Unit *p;
857
858 /* Look for all socket objects that go by any of our
859 * units and collect their fds */
860
861 if (!(k = unit_name_change_suffix(t, ".socket"))) {
862 r = -ENOMEM;
863 goto fail;
864 }
865
866 p = manager_get_unit(UNIT(s)->meta.manager, k);
867 free(k);
868
869 if (!p) continue;
870
871 if ((r = set_put(set, p)) < 0)
872 goto fail;
873 }
874
875 *_set = set;
876 return 0;
877
878 fail:
879 set_free(set);
880 return r;
881 }
882
883
884 static int service_notify_sockets(Service *s) {
885 Iterator i;
886 Set *set;
887 Socket *sock;
888 int r;
889
890 assert(s);
891
892 /* Notifies all our sockets when we die */
893
894 if ((r = service_get_sockets(s, &set)) < 0)
895 return r;
896
897 SET_FOREACH(sock, set, i)
898 socket_notify_service_dead(sock);
899
900 set_free(set);
901
902 return 0;
903 }
904
905 static void service_set_state(Service *s, ServiceState state) {
906 ServiceState old_state;
907 assert(s);
908
909 old_state = s->state;
910 s->state = state;
911
912 if (state != SERVICE_START_PRE &&
913 state != SERVICE_START &&
914 state != SERVICE_START_POST &&
915 state != SERVICE_RELOAD &&
916 state != SERVICE_STOP &&
917 state != SERVICE_STOP_SIGTERM &&
918 state != SERVICE_STOP_SIGKILL &&
919 state != SERVICE_STOP_POST &&
920 state != SERVICE_FINAL_SIGTERM &&
921 state != SERVICE_FINAL_SIGKILL &&
922 state != SERVICE_AUTO_RESTART)
923 unit_unwatch_timer(UNIT(s), &s->timer_watch);
924
925 if (state != SERVICE_START &&
926 state != SERVICE_START_POST &&
927 state != SERVICE_RUNNING &&
928 state != SERVICE_RELOAD &&
929 state != SERVICE_STOP &&
930 state != SERVICE_STOP_SIGTERM &&
931 state != SERVICE_STOP_SIGKILL)
932 if (s->main_pid > 0) {
933 unit_unwatch_pid(UNIT(s), s->main_pid);
934 s->main_pid = 0;
935 }
936
937 if (state != SERVICE_START_PRE &&
938 state != SERVICE_START &&
939 state != SERVICE_START_POST &&
940 state != SERVICE_RELOAD &&
941 state != SERVICE_STOP &&
942 state != SERVICE_STOP_SIGTERM &&
943 state != SERVICE_STOP_SIGKILL &&
944 state != SERVICE_STOP_POST &&
945 state != SERVICE_FINAL_SIGTERM &&
946 state != SERVICE_FINAL_SIGKILL)
947 if (s->control_pid > 0) {
948 unit_unwatch_pid(UNIT(s), s->control_pid);
949 s->control_pid = 0;
950 }
951
952 if (state != SERVICE_START_PRE &&
953 state != SERVICE_START &&
954 state != SERVICE_START_POST &&
955 state != SERVICE_RELOAD &&
956 state != SERVICE_STOP &&
957 state != SERVICE_STOP_POST)
958 s->control_command = NULL;
959
960 if (state == SERVICE_DEAD ||
961 state == SERVICE_STOP ||
962 state == SERVICE_STOP_SIGTERM ||
963 state == SERVICE_STOP_SIGKILL ||
964 state == SERVICE_STOP_POST ||
965 state == SERVICE_FINAL_SIGTERM ||
966 state == SERVICE_FINAL_SIGKILL ||
967 state == SERVICE_MAINTAINANCE ||
968 state == SERVICE_AUTO_RESTART)
969 service_notify_sockets(s);
970
971 log_debug("%s changed %s → %s", unit_id(UNIT(s)), service_state_to_string(old_state), service_state_to_string(state));
972
973 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
974 }
975
976 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
977 Iterator i;
978 int r;
979 int *rfds = NULL;
980 unsigned rn_fds = 0;
981 Set *set;
982 Socket *sock;
983
984 assert(s);
985 assert(fds);
986 assert(n_fds);
987
988 if ((r = service_get_sockets(s, &set)) < 0)
989 return r;
990
991 SET_FOREACH(sock, set, i) {
992 int *cfds;
993 unsigned cn_fds;
994
995 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
996 goto fail;
997
998 if (!cfds)
999 continue;
1000
1001 if (!rfds) {
1002 rfds = cfds;
1003 rn_fds = cn_fds;
1004 } else {
1005 int *t;
1006
1007 if (!(t = new(int, rn_fds+cn_fds))) {
1008 free(cfds);
1009 r = -ENOMEM;
1010 goto fail;
1011 }
1012
1013 memcpy(t, rfds, rn_fds);
1014 memcpy(t+rn_fds, cfds, cn_fds);
1015 free(rfds);
1016 free(cfds);
1017
1018 rfds = t;
1019 rn_fds = rn_fds+cn_fds;
1020 }
1021 }
1022
1023 *fds = rfds;
1024 *n_fds = rn_fds;
1025
1026 set_free(set);
1027
1028 return 0;
1029
1030 fail:
1031 set_free(set);
1032 free(rfds);
1033
1034 return r;
1035 }
1036
1037 static int service_spawn(
1038 Service *s,
1039 ExecCommand *c,
1040 bool timeout,
1041 bool pass_fds,
1042 bool apply_permissions,
1043 bool apply_chroot,
1044 pid_t *_pid) {
1045
1046 pid_t pid;
1047 int r;
1048 int *fds = NULL;
1049 unsigned n_fds = 0;
1050
1051 assert(s);
1052 assert(c);
1053 assert(_pid);
1054
1055 if (pass_fds)
1056 if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
1057 goto fail;
1058
1059 if (timeout) {
1060 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1061 goto fail;
1062 } else
1063 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1064
1065 if ((r = exec_spawn(c,
1066 &s->exec_context,
1067 fds, n_fds,
1068 apply_permissions,
1069 apply_chroot,
1070 UNIT(s)->meta.cgroup_bondings,
1071 &pid)) < 0)
1072 goto fail;
1073
1074 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1075 /* FIXME: we need to do something here */
1076 goto fail;
1077
1078 free(fds);
1079 *_pid = pid;
1080
1081 return 0;
1082
1083 fail:
1084 free(fds);
1085
1086 if (timeout)
1087 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1088
1089 return r;
1090 }
1091
1092 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1093 int r;
1094 assert(s);
1095
1096 if (!success)
1097 s->failure = true;
1098
1099 if (allow_restart &&
1100 (s->restart == SERVICE_RESTART_ALWAYS ||
1101 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1102
1103 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1104 goto fail;
1105
1106 service_set_state(s, SERVICE_AUTO_RESTART);
1107 } else
1108 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1109
1110 return;
1111
1112 fail:
1113 log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r));
1114 service_enter_dead(s, false, false);
1115 }
1116
1117 static void service_enter_signal(Service *s, ServiceState state, bool success);
1118
1119 static void service_enter_stop_post(Service *s, bool success) {
1120 int r;
1121 assert(s);
1122
1123 if (!success)
1124 s->failure = true;
1125
1126 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]))
1127 if ((r = service_spawn(s,
1128 s->control_command,
1129 true,
1130 false,
1131 !s->permissions_start_only,
1132 !s->root_directory_start_only,
1133 &s->control_pid)) < 0)
1134 goto fail;
1135
1136
1137 service_set_state(s, SERVICE_STOP_POST);
1138
1139 if (!s->control_command)
1140 service_enter_dead(s, true, true);
1141
1142 return;
1143
1144 fail:
1145 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1146 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1147 }
1148
1149 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1150 int r;
1151 bool sent = false;
1152
1153 assert(s);
1154
1155 if (!success)
1156 s->failure = true;
1157
1158 if (s->main_pid > 0 || s->control_pid > 0) {
1159 int sig;
1160
1161 sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
1162
1163 if (s->kill_mode == KILL_CONTROL_GROUP) {
1164
1165 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1166 if (r != -EAGAIN && r != -ESRCH)
1167 goto fail;
1168 } else
1169 sent = true;
1170 }
1171
1172 if (!sent) {
1173 r = 0;
1174 if (s->main_pid > 0) {
1175 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1176 r = -errno;
1177 else
1178 sent = true;
1179 }
1180
1181 if (s->control_pid > 0) {
1182 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1183 r = -errno;
1184 else
1185 sent = true;
1186 }
1187
1188 if (r < 0)
1189 goto fail;
1190 }
1191 }
1192
1193 service_set_state(s, state);
1194
1195 if (s->main_pid <= 0 && s->control_pid <= 0)
1196 service_enter_dead(s, true, true);
1197
1198 return;
1199
1200 fail:
1201 log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r));
1202
1203 if (sent) {
1204 s->failure = true;
1205 service_set_state(s, state);
1206 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1207 service_enter_stop_post(s, false);
1208 else
1209 service_enter_dead(s, false, true);
1210 }
1211
1212 static void service_enter_stop(Service *s, bool success) {
1213 int r;
1214 assert(s);
1215
1216 if (!success)
1217 s->failure = true;
1218
1219 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP]))
1220 if ((r = service_spawn(s,
1221 s->control_command,
1222 true,
1223 false,
1224 !s->permissions_start_only,
1225 !s->root_directory_start_only,
1226 &s->control_pid)) < 0)
1227 goto fail;
1228
1229 service_set_state(s, SERVICE_STOP);
1230
1231 if (!s->control_command)
1232 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1233
1234 return;
1235
1236 fail:
1237 log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r));
1238 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1239 }
1240
1241 static void service_enter_start_post(Service *s) {
1242 int r;
1243 assert(s);
1244
1245 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST]))
1246 if ((r = service_spawn(s,
1247 s->control_command,
1248 true,
1249 false,
1250 !s->permissions_start_only,
1251 !s->root_directory_start_only,
1252 &s->control_pid)) < 0)
1253 goto fail;
1254
1255
1256 service_set_state(s, SERVICE_START_POST);
1257
1258 if (!s->control_command)
1259 service_set_state(s, SERVICE_RUNNING);
1260
1261 return;
1262
1263 fail:
1264 log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r));
1265 service_enter_stop(s, false);
1266 }
1267
1268 static void service_enter_start(Service *s) {
1269 pid_t pid;
1270 int r;
1271
1272 assert(s);
1273
1274 assert(s->exec_command[SERVICE_EXEC_START]);
1275 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1276
1277 if ((r = service_spawn(s,
1278 s->exec_command[SERVICE_EXEC_START],
1279 s->type == SERVICE_FORKING,
1280 true,
1281 true,
1282 true,
1283 &pid)) < 0)
1284 goto fail;
1285
1286 service_set_state(s, SERVICE_START);
1287
1288 if (s->type == SERVICE_SIMPLE) {
1289 /* For simple services we immediately start
1290 * the START_POST binaries. */
1291
1292 s->main_pid = pid;
1293 s->main_pid_known = true;
1294 service_enter_start_post(s);
1295
1296 } else if (s->type == SERVICE_FORKING) {
1297
1298 /* For forking services we wait until the start
1299 * process exited. */
1300
1301 s->control_pid = pid;
1302 s->control_command = s->exec_command[SERVICE_EXEC_START];
1303 } else if (s->type == SERVICE_FINISH) {
1304
1305 /* For finishing services we wait until the start
1306 * process exited, too, but it is our main process. */
1307
1308 s->main_pid = pid;
1309 s->control_command = s->exec_command[SERVICE_EXEC_START];
1310 } else
1311 assert_not_reached("Unknown service type");
1312
1313 return;
1314
1315 fail:
1316 log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r));
1317 service_enter_stop(s, false);
1318 }
1319
1320 static void service_enter_start_pre(Service *s) {
1321 int r;
1322
1323 assert(s);
1324
1325 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]))
1326 if ((r = service_spawn(s,
1327 s->control_command,
1328 true,
1329 false,
1330 !s->permissions_start_only,
1331 !s->root_directory_start_only,
1332 &s->control_pid)) < 0)
1333 goto fail;
1334
1335 service_set_state(s, SERVICE_START_PRE);
1336
1337 if (!s->control_command)
1338 service_enter_start(s);
1339
1340 return;
1341
1342 fail:
1343 log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r));
1344 service_enter_dead(s, false, true);
1345 }
1346
1347 static void service_enter_restart(Service *s) {
1348 int r;
1349 assert(s);
1350
1351 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
1352 goto fail;
1353
1354 log_debug("%s scheduled restart job.", unit_id(UNIT(s)));
1355 service_enter_dead(s, true, false);
1356 return;
1357
1358 fail:
1359
1360 log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r));
1361 service_enter_dead(s, false, false);
1362 }
1363
1364 static void service_enter_reload(Service *s) {
1365 int r;
1366
1367 assert(s);
1368
1369 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]))
1370 if ((r = service_spawn(s,
1371 s->control_command,
1372 true,
1373 false,
1374 !s->permissions_start_only,
1375 !s->root_directory_start_only,
1376 &s->control_pid)) < 0)
1377 goto fail;
1378
1379 service_set_state(s, SERVICE_RELOAD);
1380
1381 if (!s->control_command)
1382 service_set_state(s, SERVICE_RUNNING);
1383
1384 return;
1385
1386 fail:
1387 log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r));
1388 service_enter_stop(s, false);
1389 }
1390
1391 static void service_run_next(Service *s, bool success) {
1392 int r;
1393
1394 assert(s);
1395 assert(s->control_command);
1396 assert(s->control_command->command_next);
1397
1398 if (!success)
1399 s->failure = true;
1400
1401 s->control_command = s->control_command->command_next;
1402
1403 if ((r = service_spawn(s,
1404 s->control_command,
1405 true,
1406 false,
1407 !s->permissions_start_only,
1408 !s->root_directory_start_only,
1409 &s->control_pid)) < 0)
1410 goto fail;
1411
1412 return;
1413
1414 fail:
1415 log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r));
1416
1417 if (s->state == SERVICE_STOP)
1418 service_enter_stop_post(s, false);
1419 else if (s->state == SERVICE_STOP_POST)
1420 service_enter_dead(s, false, true);
1421 else
1422 service_enter_stop(s, false);
1423 }
1424
1425 static int service_start(Unit *u) {
1426 Service *s = SERVICE(u);
1427
1428 assert(s);
1429
1430 /* We cannot fulfill this request right now, try again later
1431 * please! */
1432 if (s->state == SERVICE_STOP ||
1433 s->state == SERVICE_STOP_SIGTERM ||
1434 s->state == SERVICE_STOP_SIGKILL ||
1435 s->state == SERVICE_STOP_POST ||
1436 s->state == SERVICE_FINAL_SIGTERM ||
1437 s->state == SERVICE_FINAL_SIGKILL)
1438 return -EAGAIN;
1439
1440 /* Already on it! */
1441 if (s->state == SERVICE_START_PRE ||
1442 s->state == SERVICE_START ||
1443 s->state == SERVICE_START_POST)
1444 return 0;
1445
1446 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
1447
1448 /* Make sure we don't enter a busy loop of some kind. */
1449 if (!ratelimit_test(&s->ratelimit)) {
1450 log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u));
1451 return -EAGAIN;
1452 }
1453
1454 s->failure = false;
1455 s->main_pid_known = false;
1456
1457 service_enter_start_pre(s);
1458 return 0;
1459 }
1460
1461 static int service_stop(Unit *u) {
1462 Service *s = SERVICE(u);
1463
1464 assert(s);
1465
1466 if (s->state == SERVICE_START_PRE ||
1467 s->state == SERVICE_START ||
1468 s->state == SERVICE_START_POST ||
1469 s->state == SERVICE_RELOAD)
1470 return -EAGAIN;
1471
1472 if (s->state == SERVICE_AUTO_RESTART) {
1473 service_set_state(s, SERVICE_DEAD);
1474 return 0;
1475 }
1476
1477 assert(s->state == SERVICE_RUNNING);
1478
1479 service_enter_stop(s, true);
1480 return 0;
1481 }
1482
1483 static int service_reload(Unit *u) {
1484 Service *s = SERVICE(u);
1485
1486 assert(s);
1487
1488 assert(s->state == SERVICE_RUNNING);
1489
1490 service_enter_reload(s);
1491 return 0;
1492 }
1493
1494 static bool service_can_reload(Unit *u) {
1495 Service *s = SERVICE(u);
1496
1497 assert(s);
1498
1499 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1500 }
1501
1502 static UnitActiveState service_active_state(Unit *u) {
1503 assert(u);
1504
1505 return state_translation_table[SERVICE(u)->state];
1506 }
1507
1508 static int main_pid_good(Service *s) {
1509 assert(s);
1510
1511 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1512 * don't know */
1513
1514 /* If we know the pid file, then lets just check if it is
1515 * still valid */
1516 if (s->main_pid_known)
1517 return s->main_pid > 0;
1518
1519 /* We don't know the pid */
1520 return -EAGAIN;
1521 }
1522
1523 static bool control_pid_good(Service *s) {
1524 assert(s);
1525
1526 return s->control_pid > 0;
1527 }
1528
1529 static int cgroup_good(Service *s) {
1530 assert(s);
1531
1532 if (s->valid_no_process)
1533 return -EAGAIN;
1534
1535 return cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings);
1536 }
1537
1538 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1539 Service *s = SERVICE(u);
1540 bool success;
1541
1542 assert(s);
1543 assert(pid >= 0);
1544
1545 success = code == CLD_EXITED && status == 0;
1546 s->failure = s->failure || !success;
1547
1548 if (s->main_pid == pid) {
1549
1550 exec_status_fill(&s->main_exec_status, pid, code, status);
1551 s->main_pid = 0;
1552
1553 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
1554 assert(s->exec_command[SERVICE_EXEC_START]);
1555 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1556 }
1557
1558 log_debug("%s: main process exited, code=%s, status=%i", unit_id(u), sigchld_code_to_string(code), status);
1559
1560 /* The service exited, so the service is officially
1561 * gone. */
1562
1563 switch (s->state) {
1564
1565 case SERVICE_START_POST:
1566 case SERVICE_RELOAD:
1567 case SERVICE_STOP:
1568 /* Need to wait until the operation is
1569 * done */
1570 break;
1571
1572 case SERVICE_START:
1573 assert(s->type == SERVICE_FINISH);
1574
1575 /* This was our main goal, so let's go on */
1576 if (success)
1577 service_enter_start_post(s);
1578 else
1579 service_enter_stop(s, false);
1580 break;
1581
1582 case SERVICE_RUNNING:
1583 service_enter_stop(s, success);
1584 break;
1585
1586 case SERVICE_STOP_SIGTERM:
1587 case SERVICE_STOP_SIGKILL:
1588
1589 if (!control_pid_good(s))
1590 service_enter_stop_post(s, success);
1591
1592 /* If there is still a control process, wait for that first */
1593 break;
1594
1595 default:
1596 assert_not_reached("Uh, main process died at wrong time.");
1597 }
1598
1599 } else if (s->control_pid == pid) {
1600 assert(s->control_command);
1601
1602 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1603 s->control_pid = 0;
1604
1605 log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status);
1606
1607 /* If we are shutting things down anyway we
1608 * don't care about failing commands. */
1609
1610 if (s->control_command->command_next &&
1611 (success || (s->state == SERVICE_STOP || s->state == SERVICE_STOP_POST)))
1612
1613 /* There is another command to *
1614 * execute, so let's do that. */
1615
1616 service_run_next(s, success);
1617
1618 else {
1619 /* No further commands for this step, so let's
1620 * figure out what to do next */
1621
1622 log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state));
1623
1624 switch (s->state) {
1625
1626 case SERVICE_START_PRE:
1627 if (success)
1628 service_enter_start(s);
1629 else
1630 service_enter_stop(s, false);
1631 break;
1632
1633 case SERVICE_START:
1634 assert(s->type == SERVICE_FORKING);
1635
1636 /* Let's try to load the pid
1637 * file here if we can. We
1638 * ignore the return value,
1639 * since the PID file might
1640 * actually be created by a
1641 * START_POST script */
1642
1643 if (success) {
1644 if (s->pid_file)
1645 service_load_pid_file(s);
1646
1647 service_enter_start_post(s);
1648 } else
1649 service_enter_stop(s, false);
1650
1651 break;
1652
1653 case SERVICE_START_POST:
1654 if (success && s->pid_file && !s->main_pid_known) {
1655 int r;
1656
1657 /* Hmm, let's see if we can
1658 * load the pid now after the
1659 * start-post scripts got
1660 * executed. */
1661
1662 if ((r = service_load_pid_file(s)) < 0)
1663 log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r));
1664 }
1665
1666 /* Fall through */
1667
1668 case SERVICE_RELOAD:
1669 if (success) {
1670 if (main_pid_good(s) != 0 && cgroup_good(s) != 0)
1671 service_set_state(s, SERVICE_RUNNING);
1672 else
1673 service_enter_stop(s, true);
1674 } else
1675 service_enter_stop(s, false);
1676
1677 break;
1678
1679 case SERVICE_STOP:
1680 if (main_pid_good(s) > 0)
1681 /* Still not dead and we know the PID? Let's go hunting. */
1682 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
1683 else
1684 service_enter_stop_post(s, success);
1685 break;
1686
1687 case SERVICE_STOP_SIGTERM:
1688 case SERVICE_STOP_SIGKILL:
1689 if (main_pid_good(s) <= 0)
1690 service_enter_stop_post(s, success);
1691
1692 /* If there is still a service
1693 * process around, wait until
1694 * that one quit, too */
1695 break;
1696
1697 case SERVICE_STOP_POST:
1698 case SERVICE_FINAL_SIGTERM:
1699 case SERVICE_FINAL_SIGKILL:
1700 service_enter_dead(s, success, true);
1701 break;
1702
1703 default:
1704 assert_not_reached("Uh, control process died at wrong time.");
1705 }
1706 }
1707 } else
1708 assert_not_reached("Got SIGCHLD for unkown PID");
1709 }
1710
1711 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
1712 Service *s = SERVICE(u);
1713
1714 assert(s);
1715 assert(elapsed == 1);
1716
1717 assert(w == &s->timer_watch);
1718
1719 switch (s->state) {
1720
1721 case SERVICE_START_PRE:
1722 case SERVICE_START:
1723 case SERVICE_START_POST:
1724 case SERVICE_RELOAD:
1725 log_warning("%s operation timed out. Stopping.", unit_id(u));
1726 service_enter_stop(s, false);
1727 break;
1728
1729 case SERVICE_STOP:
1730 log_warning("%s stopping timed out. Terminating.", unit_id(u));
1731 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1732 break;
1733
1734 case SERVICE_STOP_SIGTERM:
1735 log_warning("%s stopping timed out. Killing.", unit_id(u));
1736 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
1737 break;
1738
1739 case SERVICE_STOP_SIGKILL:
1740 /* Uh, wie sent a SIGKILL and it is still not gone?
1741 * Must be something we cannot kill, so let's just be
1742 * weirded out and continue */
1743
1744 log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u));
1745 service_enter_stop_post(s, false);
1746 break;
1747
1748 case SERVICE_STOP_POST:
1749 log_warning("%s stopping timed out (2). Terminating.", unit_id(u));
1750 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1751 break;
1752
1753 case SERVICE_FINAL_SIGTERM:
1754 log_warning("%s stopping timed out (2). Killing.", unit_id(u));
1755 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
1756 break;
1757
1758 case SERVICE_FINAL_SIGKILL:
1759 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u));
1760 service_enter_dead(s, false, true);
1761 break;
1762
1763 case SERVICE_AUTO_RESTART:
1764 log_debug("%s holdoff time over, scheduling restart.", unit_id(u));
1765 service_enter_restart(s);
1766 break;
1767
1768 default:
1769 assert_not_reached("Timeout at wrong time.");
1770 }
1771 }
1772
1773 static void service_cgroup_notify_event(Unit *u) {
1774 Service *s = SERVICE(u);
1775
1776 assert(u);
1777
1778 log_debug("%s: cgroup is empty", unit_id(u));
1779
1780 switch (s->state) {
1781
1782 /* Waiting for SIGCHLD is usually more interesting,
1783 * because it includes return codes/signals. Which is
1784 * why we ignore the cgroup events for most cases,
1785 * except when we don't know pid which to expect the
1786 * SIGCHLD for. */
1787
1788 case SERVICE_RUNNING:
1789
1790 if (!s->valid_no_process && main_pid_good(s) <= 0)
1791 service_enter_stop(s, true);
1792
1793 break;
1794
1795 default:
1796 ;
1797 }
1798 }
1799
1800 static int service_enumerate(Manager *m) {
1801 char **p;
1802 unsigned i;
1803 DIR *d = NULL;
1804 char *path = NULL, *fpath = NULL, *name = NULL;
1805 int r;
1806
1807 assert(m);
1808
1809 STRV_FOREACH(p, m->sysvrcnd_path)
1810 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
1811 struct dirent *de;
1812
1813 free(path);
1814 path = NULL;
1815 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
1816 r = -ENOMEM;
1817 goto finish;
1818 }
1819
1820 if (d)
1821 closedir(d);
1822
1823 if (!(d = opendir(path))) {
1824 if (errno != ENOENT)
1825 log_warning("opendir() failed on %s: %s", path, strerror(errno));
1826
1827 continue;
1828 }
1829
1830 while ((de = readdir(d))) {
1831 Unit *runlevel, *service;
1832
1833 if (ignore_file(de->d_name))
1834 continue;
1835
1836 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
1837 continue;
1838
1839 if (strlen(de->d_name) < 4)
1840 continue;
1841
1842 free(fpath);
1843 fpath = NULL;
1844 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
1845 r = -ENOMEM;
1846 goto finish;
1847 }
1848
1849 if (access(fpath, X_OK) < 0) {
1850
1851 if (errno != ENOENT)
1852 log_warning("access() failed on %s: %s", fpath, strerror(errno));
1853
1854 continue;
1855 }
1856
1857 free(name);
1858 name = NULL;
1859 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
1860 r = -ENOMEM;
1861 goto finish;
1862 }
1863
1864 if ((r = manager_load_unit(m, name, &service)) < 0)
1865 goto finish;
1866
1867 if ((r = manager_load_unit(m, rcnd_table[i+1], &runlevel)) < 0)
1868 goto finish;
1869
1870 if (de->d_name[0] == 'S') {
1871 if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0)
1872 goto finish;
1873
1874 if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0)
1875 goto finish;
1876
1877 } else if (de->d_name[0] == 'K' &&
1878 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
1879 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
1880
1881 /* We honour K links only for
1882 * halt/reboot. For the normal
1883 * runlevels we assume the
1884 * stop jobs will be
1885 * implicitly added by the
1886 * core logic. */
1887
1888 if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0)
1889 goto finish;
1890
1891 if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0)
1892 goto finish;
1893 }
1894 }
1895 }
1896
1897 r = 0;
1898
1899 finish:
1900 free(path);
1901 free(fpath);
1902 free(name);
1903 closedir(d);
1904
1905 return r;
1906 }
1907
1908 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
1909 [SERVICE_DEAD] = "dead",
1910 [SERVICE_START_PRE] = "start-pre",
1911 [SERVICE_START] = "start",
1912 [SERVICE_START_POST] = "start-post",
1913 [SERVICE_RUNNING] = "running",
1914 [SERVICE_RELOAD] = "reload",
1915 [SERVICE_STOP] = "stop",
1916 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
1917 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
1918 [SERVICE_STOP_POST] = "stop-post",
1919 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
1920 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
1921 [SERVICE_MAINTAINANCE] = "maintainance",
1922 [SERVICE_AUTO_RESTART] = "auto-restart",
1923 };
1924
1925 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
1926
1927 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
1928 [SERVICE_ONCE] = "once",
1929 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
1930 [SERVICE_RESTART_ALWAYS] = "restart-on-failure",
1931 };
1932
1933 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
1934
1935 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
1936 [SERVICE_FORKING] = "forking",
1937 [SERVICE_SIMPLE] = "simple",
1938 [SERVICE_FINISH] = "finish"
1939 };
1940
1941 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
1942
1943 static const char* const service_exec_command_table[_SERVICE_EXEC_MAX] = {
1944 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
1945 [SERVICE_EXEC_START] = "ExecStart",
1946 [SERVICE_EXEC_START_POST] = "ExecStartPost",
1947 [SERVICE_EXEC_RELOAD] = "ExecReload",
1948 [SERVICE_EXEC_STOP] = "ExecStop",
1949 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
1950 };
1951
1952 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
1953
1954 const UnitVTable service_vtable = {
1955 .suffix = ".service",
1956
1957 .init = service_init,
1958 .done = service_done,
1959
1960 .dump = service_dump,
1961
1962 .start = service_start,
1963 .stop = service_stop,
1964 .reload = service_reload,
1965
1966 .can_reload = service_can_reload,
1967
1968 .active_state = service_active_state,
1969
1970 .sigchld_event = service_sigchld_event,
1971 .timer_event = service_timer_event,
1972
1973 .cgroup_notify_empty = service_cgroup_notify_event,
1974
1975 .enumerate = service_enumerate
1976 };