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