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