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