]> git.ipfire.org Git - people/ms/systemd.git/blame - service.c
service: never do automatic restarts for user requested stops
[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 32#include "strv.h"
9e2f7c11 33#include "unit-name.h"
4139c1b2 34#include "dbus-service.h"
2c4104f0
LP
35
36#define COMMENTS "#;\n"
37#define NEWLINES "\n\r"
38#define LINE_MAX 4096
034c6ed7 39
23a177ef 40static const char * const rcnd_table[] = {
adcbe07c
LP
41 "/rc0.d", SPECIAL_RUNLEVEL0_TARGET,
42 "/rc1.d", SPECIAL_RUNLEVEL1_TARGET,
43 "/rc2.d", SPECIAL_RUNLEVEL2_TARGET,
44 "/rc3.d", SPECIAL_RUNLEVEL3_TARGET,
45 "/rc4.d", SPECIAL_RUNLEVEL4_TARGET,
46 "/rc5.d", SPECIAL_RUNLEVEL5_TARGET,
47 "/rc6.d", SPECIAL_RUNLEVEL6_TARGET,
48 "/boot.d", SPECIAL_BASIC_TARGET
23a177ef
LP
49};
50
acbb0225 51static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87f0e418
LP
52 [SERVICE_DEAD] = UNIT_INACTIVE,
53 [SERVICE_START_PRE] = UNIT_ACTIVATING,
54 [SERVICE_START] = UNIT_ACTIVATING,
55 [SERVICE_START_POST] = UNIT_ACTIVATING,
56 [SERVICE_RUNNING] = UNIT_ACTIVE,
80876c20 57 [SERVICE_EXITED] = UNIT_ACTIVE,
87f0e418
LP
58 [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING,
59 [SERVICE_STOP] = UNIT_DEACTIVATING,
60 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
61 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
62 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
63 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
64 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
65 [SERVICE_MAINTAINANCE] = UNIT_INACTIVE,
66 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING,
034c6ed7 67};
5cb5a6ff 68
a16e1123
LP
69static void service_init(Unit *u) {
70 Service *s = SERVICE(u);
71
72 assert(u);
73 assert(u->meta.load_state == UNIT_STUB);
74
75 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
76 s->restart_usec = DEFAULT_RESTART_USEC;
77 s->timer_watch.type = WATCH_INVALID;
78 s->sysv_start_priority = -1;
79 s->socket_fd = -1;
80
81 exec_context_init(&s->exec_context);
82
83 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
84
85 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
86}
87
5e94833f
LP
88static void service_unwatch_control_pid(Service *s) {
89 assert(s);
90
91 if (s->control_pid <= 0)
92 return;
93
94 unit_unwatch_pid(UNIT(s), s->control_pid);
95 s->control_pid = 0;
96}
97
98static void service_unwatch_main_pid(Service *s) {
99 assert(s);
100
101 if (s->main_pid <= 0)
102 return;
103
104 unit_unwatch_pid(UNIT(s), s->main_pid);
105 s->main_pid = 0;
106}
107
4f2d528d
LP
108static void service_close_socket_fd(Service *s) {
109 assert(s);
110
111 if (s->socket_fd < 0)
112 return;
113
114 close_nointr_nofail(s->socket_fd);
115 s->socket_fd = -1;
116}
117
87f0e418
LP
118static void service_done(Unit *u) {
119 Service *s = SERVICE(u);
44d8db9e
LP
120
121 assert(s);
122
123 free(s->pid_file);
124 s->pid_file = NULL;
125
2c4104f0
LP
126 free(s->sysv_path);
127 s->sysv_path = NULL;
128
8309400a
LP
129 free(s->sysv_runlevels);
130 s->sysv_runlevels = NULL;
131
44d8db9e 132 exec_context_done(&s->exec_context);
e537352b 133 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
44d8db9e
LP
134 s->control_command = NULL;
135
136 /* This will leak a process, but at least no memory or any of
137 * our resources */
5e94833f
LP
138 service_unwatch_main_pid(s);
139 service_unwatch_control_pid(s);
44d8db9e 140
05e343b7
LP
141 if (s->bus_name) {
142 unit_unwatch_bus_name(UNIT(u), s->bus_name);
143 free(s->bus_name);
144 s->bus_name = NULL;
145 }
146
4f2d528d
LP
147 service_close_socket_fd(s);
148
acbb0225 149 unit_unwatch_timer(u, &s->timer_watch);
44d8db9e
LP
150}
151
2c4104f0
LP
152static int sysv_translate_name(const char *name, char **_r) {
153
154 static const char * const table[] = {
155 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
156 "$network", SPECIAL_NETWORK_TARGET,
157 "$named", SPECIAL_NSS_LOOKUP_TARGET,
158 "$portmap", SPECIAL_RPCBIND_TARGET,
159 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
160 "$syslog", SPECIAL_SYSLOG_TARGET,
161 "$time", SPECIAL_RTC_SET_TARGET
162 };
163
164 unsigned i;
165 char *r;
166
167 for (i = 0; i < ELEMENTSOF(table); i += 2)
168 if (streq(table[i], name)) {
169 if (!(r = strdup(table[i+1])))
170 return -ENOMEM;
171
172 goto finish;
173 }
174
175 if (*name == '$')
176 return 0;
177
178 if (asprintf(&r, "%s.service", name) < 0)
179 return -ENOMEM;
180
181finish:
182
183 if (_r)
184 *_r = r;
185
186 return 1;
187}
188
189static int sysv_chkconfig_order(Service *s) {
190 Meta *other;
191 int r;
192
193 assert(s);
194
195 if (s->sysv_start_priority < 0)
196 return 0;
197
23a177ef
LP
198 /* For each pair of services where at least one lacks a LSB
199 * header, we use the start priority value to order things. */
2c4104f0
LP
200
201 LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) {
202 Service *t;
203 UnitDependency d;
204
205 t = (Service*) other;
206
207 if (s == t)
208 continue;
209
210 if (t->sysv_start_priority < 0)
211 continue;
212
51a1a79d
LP
213 /* If both units have modern headers we don't care
214 * about the priorities */
215 if ((!s->sysv_path || s->sysv_has_lsb) &&
216 (!t->sysv_path || t->sysv_has_lsb))
23a177ef
LP
217 continue;
218
2c4104f0
LP
219 if (t->sysv_start_priority < s->sysv_start_priority)
220 d = UNIT_AFTER;
221 else if (t->sysv_start_priority > s->sysv_start_priority)
222 d = UNIT_BEFORE;
223 else
224 continue;
225
226 /* FIXME: Maybe we should compare the name here lexicographically? */
227
701cc384 228 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
2c4104f0
LP
229 return r;
230 }
231
232 return 0;
233}
234
235static ExecCommand *exec_command_new(const char *path, const char *arg1) {
236 ExecCommand *c;
237
238 if (!(c = new0(ExecCommand, 1)))
239 return NULL;
240
241 if (!(c->path = strdup(path))) {
242 free(c);
243 return NULL;
244 }
245
246 if (!(c->argv = strv_new(path, arg1, NULL))) {
247 free(c->path);
248 free(c);
249 return NULL;
250 }
251
252 return c;
253}
254
255static int sysv_exec_commands(Service *s) {
256 ExecCommand *c;
257
258 assert(s);
259 assert(s->sysv_path);
260
261 if (!(c = exec_command_new(s->sysv_path, "start")))
262 return -ENOMEM;
263 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
264
265 if (!(c = exec_command_new(s->sysv_path, "stop")))
266 return -ENOMEM;
267 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
268
269 if (!(c = exec_command_new(s->sysv_path, "reload")))
270 return -ENOMEM;
271 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
272
273 return 0;
274}
275
23a177ef
LP
276static int priority_from_rcd(Service *s, const char *init_script) {
277 char **p;
278 unsigned i;
279
061978fa 280 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvrcnd_path)
23a177ef
LP
281 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
282 char *path;
283 DIR *d;
284 struct dirent *de;
285
286 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0)
287 return -ENOMEM;
288
289 d = opendir(path);
290 free(path);
291
292 if (!d) {
293 if (errno != ENOENT)
294 log_warning("opendir() failed on %s: %s", path, strerror(errno));
295
296 continue;
297 }
298
299 while ((de = readdir(d))) {
300 int a, b;
301
302 if (ignore_file(de->d_name))
303 continue;
304
305 if (de->d_name[0] != 'S')
306 continue;
307
308 if (strlen(de->d_name) < 4)
309 continue;
310
311 if (!streq(de->d_name + 3, init_script))
312 continue;
313
314 /* Yay, we found it! Now decode the priority */
315
316 a = undecchar(de->d_name[1]);
317 b = undecchar(de->d_name[2]);
318
319 if (a < 0 || b < 0)
320 continue;
321
322 s->sysv_start_priority = a*10 + b;
323
9e2f7c11 324 log_debug("Determined priority %i from link farm for %s", s->sysv_start_priority, UNIT(s)->meta.id);
23a177ef
LP
325
326 closedir(d);
327 return 0;
328 }
329
330 closedir(d);
331 }
332
333 return 0;
334}
335
e537352b 336static int service_load_sysv_path(Service *s, const char *path) {
2c4104f0
LP
337 FILE *f;
338 Unit *u;
339 unsigned line = 0;
340 int r;
341 enum {
342 NORMAL,
343 DESCRIPTION,
344 LSB,
345 LSB_DESCRIPTION
346 } state = NORMAL;
23a177ef
LP
347
348 assert(s);
349 assert(path);
2c4104f0
LP
350
351 u = UNIT(s);
352
353 if (!(f = fopen(path, "re"))) {
354 r = errno == ENOENT ? 0 : -errno;
355 goto finish;
356 }
357
358 s->type = SERVICE_FORKING;
359 s->restart = SERVICE_ONCE;
360
361 free(s->sysv_path);
362 if (!(s->sysv_path = strdup(path))) {
363 r = -ENOMEM;
364 goto finish;
365 }
366
367 while (!feof(f)) {
368 char l[LINE_MAX], *t;
369
370 if (!fgets(l, sizeof(l), f)) {
371 if (feof(f))
372 break;
373
374 r = -errno;
375 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
376 goto finish;
377 }
378
379 line++;
380
381 t = strstrip(l);
382 if (*t != '#')
383 continue;
384
385 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
386 state = LSB;
23a177ef 387 s->sysv_has_lsb = true;
2c4104f0
LP
388 continue;
389 }
390
391 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
392 state = NORMAL;
393 continue;
394 }
395
396 t++;
397 t += strspn(t, WHITESPACE);
398
399 if (state == NORMAL) {
400
401 /* Try to parse Red Hat style chkconfig headers */
402
403 if (startswith(t, "chkconfig:")) {
404 int start_priority;
8309400a 405 char runlevels[16], *k;
2c4104f0
LP
406
407 state = NORMAL;
408
8309400a
LP
409 if (sscanf(t+10, "%15s %i %*i",
410 runlevels,
411 &start_priority) != 2) {
2c4104f0
LP
412
413 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
414 continue;
415 }
416
8309400a 417 if (start_priority < 0 || start_priority > 99)
2c4104f0 418 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
8309400a
LP
419 else
420 s->sysv_start_priority = start_priority;
421
422 char_array_0(runlevels);
423 k = delete_chars(runlevels, WHITESPACE "-");
424
425 if (k[0]) {
426 char *d;
427
428 if (!(d = strdup(k))) {
429 r = -ENOMEM;
430 goto finish;
431 }
432
433 free(s->sysv_runlevels);
434 s->sysv_runlevels = d;
2c4104f0
LP
435 }
436
2c4104f0
LP
437
438 } else if (startswith(t, "description:")) {
439
440 size_t k = strlen(t);
441 char *d;
442
443 if (t[k-1] == '\\') {
444 state = DESCRIPTION;
445 t[k-1] = 0;
446 }
447
448 if (!(d = strdup(strstrip(t+12)))) {
449 r = -ENOMEM;
450 goto finish;
451 }
452
453 free(u->meta.description);
454 u->meta.description = d;
455
456 } else if (startswith(t, "pidfile:")) {
457
458 char *fn;
459
460 state = NORMAL;
461
462 fn = strstrip(t+8);
463 if (!path_is_absolute(fn)) {
464 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
465 continue;
466 }
467
468 if (!(fn = strdup(fn))) {
469 r = -ENOMEM;
470 goto finish;
471 }
472
473 free(s->pid_file);
474 s->pid_file = fn;
475 }
476
477 } else if (state == DESCRIPTION) {
478
479 /* Try to parse Red Hat style description
480 * continuation */
481
482 size_t k = strlen(t);
483 char *d;
484
485 if (t[k-1] == '\\')
486 t[k-1] = 0;
487 else
488 state = NORMAL;
489
490 assert(u->meta.description);
491 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
492 r = -ENOMEM;
493 goto finish;
494 }
495
496 free(u->meta.description);
497 u->meta.description = d;
498
499 } else if (state == LSB || state == LSB_DESCRIPTION) {
500
501 if (startswith(t, "Provides:")) {
502 char *i, *w;
503 size_t z;
504
505 state = LSB;
506
507 FOREACH_WORD(w, z, t+9, i) {
508 char *n, *m;
509
510 if (!(n = strndup(w, z))) {
511 r = -ENOMEM;
512 goto finish;
513 }
514
515 r = sysv_translate_name(n, &m);
516 free(n);
517
518 if (r < 0)
519 goto finish;
520
521 if (r == 0)
522 continue;
523
bd77d0fc
LP
524 if (unit_name_to_type(m) == UNIT_SERVICE)
525 r = unit_add_name(u, m);
526 else {
701cc384
LP
527 if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m, NULL, true)) >= 0)
528 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
bd77d0fc
LP
529 }
530
2c4104f0
LP
531 free(m);
532
533 if (r < 0)
534 goto finish;
535 }
536
537 } else if (startswith(t, "Required-Start:") ||
538 startswith(t, "Should-Start:")) {
539 char *i, *w;
540 size_t z;
541
542 state = LSB;
543
544 FOREACH_WORD(w, z, strchr(t, ':')+1, i) {
545 char *n, *m;
546
547 if (!(n = strndup(w, z))) {
548 r = -ENOMEM;
549 goto finish;
550 }
551
552 r = sysv_translate_name(n, &m);
553 free(n);
554
555 if (r < 0)
556 goto finish;
557
558 if (r == 0)
559 continue;
560
701cc384 561 r = unit_add_dependency_by_name(u, UNIT_AFTER, m, NULL, true);
2c4104f0
LP
562 free(m);
563
564 if (r < 0)
565 goto finish;
566 }
8309400a
LP
567 } else if (startswith(t, "Default-Start:")) {
568 char *k, *d;
569
570 state = LSB;
571
572 k = delete_chars(t+14, WHITESPACE "-");
573
574 if (k[0] != 0) {
575 if (!(d = strdup(k))) {
576 r = -ENOMEM;
577 goto finish;
578 }
579
580 free(s->sysv_runlevels);
581 s->sysv_runlevels = d;
582 }
2c4104f0
LP
583
584 } else if (startswith(t, "Description:")) {
585 char *d;
586
587 state = LSB_DESCRIPTION;
588
589 if (!(d = strdup(strstrip(t+12)))) {
590 r = -ENOMEM;
591 goto finish;
592 }
593
594 free(u->meta.description);
595 u->meta.description = d;
596
8309400a
LP
597 } else if (startswith(t, "Short-Description:") &&
598 !u->meta.description) {
2c4104f0
LP
599 char *d;
600
601 /* We use the short description only
602 * if no long description is set. */
603
604 state = LSB;
605
606 if (!(d = strdup(strstrip(t+18)))) {
607 r = -ENOMEM;
608 goto finish;
609 }
610
2c4104f0
LP
611 u->meta.description = d;
612
613 } else if (state == LSB_DESCRIPTION) {
614
615 if (startswith(l, "#\t") || startswith(l, "# ")) {
616 char *d;
617
618 assert(u->meta.description);
619 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
620 r = -ENOMEM;
621 goto finish;
622 }
623
624 free(u->meta.description);
625 u->meta.description = d;
626 } else
627 state = LSB;
628 }
629 }
630 }
631
23a177ef
LP
632 /* If init scripts have no LSB header, then we enforce the
633 * ordering via the chkconfig priorities. We try to determine
634 * a priority for *all* init scripts here, since they are
635 * needed as soon as at least one non-LSB script is used. */
2c4104f0 636
23a177ef 637 if (s->sysv_start_priority < 0) {
9e2f7c11 638 log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", u->meta.id);
23a177ef
LP
639
640 if ((r = priority_from_rcd(s, file_name_from_path(path))) < 0)
2c4104f0
LP
641 goto finish;
642
23a177ef 643 if (s->sysv_start_priority < 0)
9e2f7c11 644 log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", u->meta.id);
23a177ef
LP
645 }
646
2c4104f0
LP
647 if ((r = sysv_exec_commands(s)) < 0)
648 goto finish;
649
0bc824be
LP
650 if (!s->sysv_runlevels || chars_intersect("12345", s->sysv_runlevels)) {
651 /* If there a runlevels configured for this service
652 * but none of the standard ones, then we assume this
653 * is some special kind of service (which might be
654 * needed for early boot) and don't create any links
655 * to it. */
656
701cc384
LP
657 if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0 ||
658 (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
0bc824be
LP
659 goto finish;
660 }
0fd030be 661
80876c20
LP
662 /* Special setting for all SysV services */
663 s->valid_no_process = true;
664
90a13a44 665 /* Don't timeout special services during boot (like fsck) */
ba8c0bef 666 if (s->sysv_runlevels && !chars_intersect("12345", s->sysv_runlevels))
90a13a44
LP
667 s->timeout_usec = -1;
668
e537352b 669 u->meta.load_state = UNIT_LOADED;
23a177ef 670 r = 0;
2c4104f0
LP
671
672finish:
673
674 if (f)
675 fclose(f);
676
677 return r;
678}
679
e537352b 680static int service_load_sysv_name(Service *s, const char *name) {
2c4104f0
LP
681 char **p;
682
683 assert(s);
684 assert(name);
685
686 STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) {
687 char *path;
688 int r;
689
690 if (asprintf(&path, "%s/%s", *p, name) < 0)
691 return -ENOMEM;
692
693 assert(endswith(path, ".service"));
694 path[strlen(path)-8] = 0;
695
e537352b 696 r = service_load_sysv_path(s, path);
2c4104f0
LP
697 free(path);
698
23a177ef 699 if (r < 0)
2c4104f0 700 return r;
23a177ef 701
e537352b 702 if ((UNIT(s)->meta.load_state != UNIT_STUB))
23a177ef 703 break;
2c4104f0
LP
704 }
705
706 return 0;
707}
708
e537352b 709static int service_load_sysv(Service *s) {
2c4104f0
LP
710 const char *t;
711 Iterator i;
712 int r;
713
5cb5a6ff
LP
714 assert(s);
715
716 /* Load service data from SysV init scripts, preferably with
717 * LSB headers ... */
718
2c4104f0
LP
719 if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path))
720 return 0;
721
9e2f7c11 722 if ((t = UNIT(s)->meta.id))
e537352b 723 if ((r = service_load_sysv_name(s, t)) < 0)
2c4104f0
LP
724 return r;
725
e537352b 726 if (UNIT(s)->meta.load_state == UNIT_STUB)
23a177ef 727 SET_FOREACH(t, UNIT(s)->meta.names, i) {
9e2f7c11 728 if (t == UNIT(s)->meta.id)
e537352b
LP
729 continue;
730
731 if ((r == service_load_sysv_name(s, t)) < 0)
23a177ef
LP
732 return r;
733
e537352b 734 if (UNIT(s)->meta.load_state != UNIT_STUB)
23a177ef
LP
735 break;
736 }
2c4104f0
LP
737
738 return 0;
5cb5a6ff
LP
739}
740
05e343b7
LP
741static int service_add_bus_name(Service *s) {
742 char *n;
743 int r;
744
745 assert(s);
746 assert(s->bus_name);
747
748 if (asprintf(&n, "dbus-%s.service", s->bus_name) < 0)
749 return 0;
750
751 r = unit_merge_by_name(UNIT(s), n);
752 free(n);
753
754 return r;
755}
756
243b1432
LP
757static int service_verify(Service *s) {
758 assert(s);
759
760 if (UNIT(s)->meta.load_state != UNIT_LOADED)
761 return 0;
762
763 if (!s->exec_command[SERVICE_EXEC_START]) {
9e2f7c11 764 log_error("%s lacks ExecStart setting. Refusing.", UNIT(s)->meta.id);
243b1432
LP
765 return -EINVAL;
766 }
767
05e343b7
LP
768 if (s->type == SERVICE_DBUS && !s->bus_name) {
769 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", UNIT(s)->meta.id);
770 return -EINVAL;
771 }
772
243b1432
LP
773 return 0;
774}
775
e537352b
LP
776static int service_load(Unit *u) {
777 int r;
778 Service *s = SERVICE(u);
779
780 assert(s);
1e2e8133 781
5cb5a6ff 782 /* Load a .service file */
e537352b 783 if ((r = unit_load_fragment(u)) < 0)
5cb5a6ff
LP
784 return r;
785
bd77d0fc 786 /* Load a classic init script as a fallback, if we couldn't find anything */
e537352b
LP
787 if (u->meta.load_state == UNIT_STUB)
788 if ((r = service_load_sysv(s)) < 0)
23a177ef 789 return r;
d46de8a1 790
23a177ef 791 /* Still nothing found? Then let's give up */
e537352b 792 if (u->meta.load_state == UNIT_STUB)
23a177ef 793 return -ENOENT;
034c6ed7 794
23a177ef
LP
795 /* We were able to load something, then let's add in the
796 * dropin directories. */
797 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
8e274523 798 return r;
23a177ef
LP
799
800 /* This is a new unit? Then let's add in some extras */
e537352b 801 if (u->meta.load_state == UNIT_LOADED) {
23a177ef
LP
802 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
803 return r;
804
805 if ((r = unit_add_default_cgroup(u)) < 0)
806 return r;
807
808 if ((r = sysv_chkconfig_order(s)) < 0)
809 return r;
05e343b7
LP
810
811 if (s->bus_name) {
812 if ((r = service_add_bus_name(s)) < 0)
813 return r;
814
815 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
816 return r;
817 }
8e274523
LP
818 }
819
243b1432 820 return service_verify(s);
034c6ed7
LP
821}
822
87f0e418 823static void service_dump(Unit *u, FILE *f, const char *prefix) {
5cb5a6ff 824
5cb5a6ff 825 ServiceExecCommand c;
87f0e418 826 Service *s = SERVICE(u);
47be870b
LP
827 const char *prefix2;
828 char *p2;
5cb5a6ff
LP
829
830 assert(s);
831
47be870b
LP
832 p2 = strappend(prefix, "\t");
833 prefix2 = p2 ? p2 : prefix;
44d8db9e 834
5cb5a6ff 835 fprintf(f,
81a2b7ce
LP
836 "%sService State: %s\n"
837 "%sPermissionsStartOnly: %s\n"
8e274523 838 "%sRootDirectoryStartOnly: %s\n"
03d6ab85 839 "%sValidNoProcess: %s\n"
50159e6a 840 "%sKillMode: %s\n"
03d6ab85 841 "%sType: %s\n",
81a2b7ce
LP
842 prefix, service_state_to_string(s->state),
843 prefix, yes_no(s->permissions_start_only),
8e274523 844 prefix, yes_no(s->root_directory_start_only),
03d6ab85 845 prefix, yes_no(s->valid_no_process),
50159e6a 846 prefix, kill_mode_to_string(s->kill_mode),
03d6ab85 847 prefix, service_type_to_string(s->type));
5cb5a6ff 848
70123e68
LP
849 if (s->control_pid > 0)
850 fprintf(f,
851 "%sControl PID: %llu\n",
852 prefix, (unsigned long long) s->control_pid);
853
854 if (s->main_pid > 0)
855 fprintf(f,
856 "%sMain PID: %llu\n",
857 prefix, (unsigned long long) s->main_pid);
858
034c6ed7
LP
859 if (s->pid_file)
860 fprintf(f,
861 "%sPIDFile: %s\n",
862 prefix, s->pid_file);
863
05e343b7
LP
864 if (s->bus_name)
865 fprintf(f,
866 "%sBusName: %s\n"
867 "%sBus Name Good: %s\n",
868 prefix, s->bus_name,
869 prefix, yes_no(s->bus_name_good));
870
5cb5a6ff
LP
871 exec_context_dump(&s->exec_context, f, prefix);
872
e537352b 873 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
5cb5a6ff 874
44d8db9e
LP
875 if (!s->exec_command[c])
876 continue;
877
40d50879 878 fprintf(f, "%s-> %s:\n",
94f04347 879 prefix, service_exec_command_to_string(c));
44d8db9e
LP
880
881 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff 882 }
44d8db9e 883
2c4104f0
LP
884 if (s->sysv_path)
885 fprintf(f,
23a177ef
LP
886 "%sSysV Init Script Path: %s\n"
887 "%sSysV Init Script has LSB Header: %s\n",
888 prefix, s->sysv_path,
889 prefix, yes_no(s->sysv_has_lsb));
2c4104f0
LP
890
891 if (s->sysv_start_priority >= 0)
892 fprintf(f,
23a177ef 893 "%sSysVStartPriority: %i\n",
2c4104f0
LP
894 prefix, s->sysv_start_priority);
895
8309400a
LP
896 if (s->sysv_runlevels)
897 fprintf(f, "%sSysVRunLevels: %s\n",
898 prefix, s->sysv_runlevels);
23a177ef 899
47be870b 900 free(p2);
5cb5a6ff
LP
901}
902
034c6ed7
LP
903static int service_load_pid_file(Service *s) {
904 char *k;
905 unsigned long p;
906 int r;
907
908 assert(s);
909
910 if (s->main_pid_known)
911 return 0;
912
5e94833f
LP
913 assert(s->main_pid <= 0);
914
034c6ed7
LP
915 if (!s->pid_file)
916 return -ENOENT;
917
918 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
919 return r;
920
921 if ((r = safe_atolu(k, &p)) < 0) {
922 free(k);
923 return r;
924 }
925
926 if ((unsigned long) (pid_t) p != p)
927 return -ERANGE;
928
b8c597d5
LP
929 if (kill((pid_t) p, 0) < 0 && errno != EPERM) {
930 log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.",
931 (unsigned long long) p, s->pid_file);
932 return -ESRCH;
933 }
934
16f6025e
LP
935 if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0)
936 /* FIXME: we need to do something here */
937 return r;
938
b8c597d5 939 s->main_pid = (pid_t) p;
034c6ed7
LP
940 s->main_pid_known = true;
941
942 return 0;
943}
944
3e33402a
LP
945static int service_get_sockets(Service *s, Set **_set) {
946 Set *set;
ceee3d82
LP
947 Iterator i;
948 char *t;
3e33402a 949 int r;
ceee3d82
LP
950
951 assert(s);
3e33402a
LP
952 assert(_set);
953
954 /* Collects all Socket objects that belong to this
955 * service. Note that a service might have multiple sockets
956 * via multiple names. */
957
958 if (!(set = set_new(NULL, NULL)))
959 return -ENOMEM;
ceee3d82
LP
960
961 SET_FOREACH(t, UNIT(s)->meta.names, i) {
962 char *k;
963 Unit *p;
964
965 /* Look for all socket objects that go by any of our
966 * units and collect their fds */
967
3e33402a
LP
968 if (!(k = unit_name_change_suffix(t, ".socket"))) {
969 r = -ENOMEM;
970 goto fail;
971 }
ceee3d82
LP
972
973 p = manager_get_unit(UNIT(s)->meta.manager, k);
974 free(k);
975
8d567588
LP
976 if (!p)
977 continue;
ceee3d82 978
3e33402a
LP
979 if ((r = set_put(set, p)) < 0)
980 goto fail;
ceee3d82
LP
981 }
982
3e33402a
LP
983 *_set = set;
984 return 0;
985
986fail:
987 set_free(set);
988 return r;
989}
990
e537352b 991static int service_notify_sockets_dead(Service *s) {
3e33402a
LP
992 Iterator i;
993 Set *set;
47be870b 994 Socket *sock;
3e33402a
LP
995 int r;
996
997 assert(s);
998
999 /* Notifies all our sockets when we die */
3e33402a
LP
1000 if ((r = service_get_sockets(s, &set)) < 0)
1001 return r;
1002
47be870b
LP
1003 SET_FOREACH(sock, set, i)
1004 socket_notify_service_dead(sock);
3e33402a
LP
1005
1006 set_free(set);
1007
ceee3d82
LP
1008 return 0;
1009}
1010
034c6ed7
LP
1011static void service_set_state(Service *s, ServiceState state) {
1012 ServiceState old_state;
5cb5a6ff
LP
1013 assert(s);
1014
034c6ed7 1015 old_state = s->state;
5cb5a6ff 1016 s->state = state;
034c6ed7
LP
1017
1018 if (state != SERVICE_START_PRE &&
1019 state != SERVICE_START &&
1020 state != SERVICE_START_POST &&
1021 state != SERVICE_RELOAD &&
1022 state != SERVICE_STOP &&
1023 state != SERVICE_STOP_SIGTERM &&
1024 state != SERVICE_STOP_SIGKILL &&
1025 state != SERVICE_STOP_POST &&
1026 state != SERVICE_FINAL_SIGTERM &&
1027 state != SERVICE_FINAL_SIGKILL &&
1028 state != SERVICE_AUTO_RESTART)
acbb0225 1029 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7 1030
7d55e835
LP
1031 if (state != SERVICE_START &&
1032 state != SERVICE_START_POST &&
034c6ed7
LP
1033 state != SERVICE_RUNNING &&
1034 state != SERVICE_RELOAD &&
1035 state != SERVICE_STOP &&
1036 state != SERVICE_STOP_SIGTERM &&
1037 state != SERVICE_STOP_SIGKILL)
5e94833f 1038 service_unwatch_main_pid(s);
034c6ed7
LP
1039
1040 if (state != SERVICE_START_PRE &&
1041 state != SERVICE_START &&
1042 state != SERVICE_START_POST &&
1043 state != SERVICE_RELOAD &&
1044 state != SERVICE_STOP &&
1045 state != SERVICE_STOP_SIGTERM &&
1046 state != SERVICE_STOP_SIGKILL &&
1047 state != SERVICE_STOP_POST &&
1048 state != SERVICE_FINAL_SIGTERM &&
e537352b 1049 state != SERVICE_FINAL_SIGKILL) {
5e94833f 1050 service_unwatch_control_pid(s);
034c6ed7 1051 s->control_command = NULL;
a16e1123 1052 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
e537352b 1053 }
034c6ed7 1054
ceee3d82
LP
1055 if (state == SERVICE_DEAD ||
1056 state == SERVICE_STOP ||
1057 state == SERVICE_STOP_SIGTERM ||
1058 state == SERVICE_STOP_SIGKILL ||
1059 state == SERVICE_STOP_POST ||
1060 state == SERVICE_FINAL_SIGTERM ||
1061 state == SERVICE_FINAL_SIGKILL ||
1062 state == SERVICE_MAINTAINANCE ||
1063 state == SERVICE_AUTO_RESTART)
e537352b 1064 service_notify_sockets_dead(s);
ceee3d82 1065
4f2d528d
LP
1066 if (state != SERVICE_START_PRE &&
1067 state != SERVICE_START &&
1068 !(state == SERVICE_DEAD && UNIT(s)->meta.job))
1069 service_close_socket_fd(s);
1070
e537352b 1071 if (old_state != state)
40d50879 1072 log_debug("%s changed %s -> %s", UNIT(s)->meta.id, service_state_to_string(old_state), service_state_to_string(state));
acbb0225
LP
1073
1074 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
034c6ed7
LP
1075}
1076
a16e1123
LP
1077static int service_coldplug(Unit *u) {
1078 Service *s = SERVICE(u);
1079 int r;
1080
1081 assert(s);
1082 assert(s->state == SERVICE_DEAD);
1083
1084 if (s->deserialized_state != s->state) {
1085
1086 if (s->deserialized_state == SERVICE_START_PRE ||
1087 s->deserialized_state == SERVICE_START ||
1088 s->deserialized_state == SERVICE_START_POST ||
1089 s->deserialized_state == SERVICE_RELOAD ||
1090 s->deserialized_state == SERVICE_STOP ||
1091 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1092 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1093 s->deserialized_state == SERVICE_STOP_POST ||
1094 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1095 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
e558336f
LP
1096 s->deserialized_state == SERVICE_AUTO_RESTART) {
1097
1098 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1099 usec_t k;
1100
1101 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1102
1103 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1104 return r;
1105 }
1106 }
a16e1123
LP
1107
1108 if ((s->deserialized_state == SERVICE_START &&
1109 (s->type == SERVICE_FORKING ||
1110 s->type == SERVICE_DBUS)) ||
1111 s->deserialized_state == SERVICE_START_POST ||
1112 s->deserialized_state == SERVICE_RUNNING ||
1113 s->deserialized_state == SERVICE_RELOAD ||
1114 s->deserialized_state == SERVICE_STOP ||
1115 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1116 s->deserialized_state == SERVICE_STOP_SIGKILL)
1117 if (s->main_pid > 0)
1118 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1119 return r;
1120
1121 if (s->deserialized_state == SERVICE_START_PRE ||
1122 s->deserialized_state == SERVICE_START ||
1123 s->deserialized_state == SERVICE_START_POST ||
1124 s->deserialized_state == SERVICE_RELOAD ||
1125 s->deserialized_state == SERVICE_STOP ||
1126 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1127 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1128 s->deserialized_state == SERVICE_STOP_POST ||
1129 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1130 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1131 if (s->control_pid > 0)
1132 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1133 return r;
1134
1135 service_set_state(s, s->deserialized_state);
1136 }
1137
1138 return 0;
1139}
1140
44d8db9e
LP
1141static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1142 Iterator i;
1143 int r;
1144 int *rfds = NULL;
1145 unsigned rn_fds = 0;
3e33402a 1146 Set *set;
47be870b 1147 Socket *sock;
44d8db9e
LP
1148
1149 assert(s);
1150 assert(fds);
1151 assert(n_fds);
1152
3e33402a
LP
1153 if ((r = service_get_sockets(s, &set)) < 0)
1154 return r;
1155
47be870b 1156 SET_FOREACH(sock, set, i) {
44d8db9e
LP
1157 int *cfds;
1158 unsigned cn_fds;
1159
47be870b 1160 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
44d8db9e
LP
1161 goto fail;
1162
1163 if (!cfds)
1164 continue;
1165
1166 if (!rfds) {
1167 rfds = cfds;
1168 rn_fds = cn_fds;
1169 } else {
1170 int *t;
1171
1172 if (!(t = new(int, rn_fds+cn_fds))) {
1173 free(cfds);
1174 r = -ENOMEM;
1175 goto fail;
1176 }
1177
1178 memcpy(t, rfds, rn_fds);
1179 memcpy(t+rn_fds, cfds, cn_fds);
1180 free(rfds);
1181 free(cfds);
1182
1183 rfds = t;
1184 rn_fds = rn_fds+cn_fds;
1185 }
1186 }
1187
1188 *fds = rfds;
1189 *n_fds = rn_fds;
3e33402a
LP
1190
1191 set_free(set);
1192
44d8db9e
LP
1193 return 0;
1194
1195fail:
3e33402a 1196 set_free(set);
44d8db9e 1197 free(rfds);
3e33402a 1198
44d8db9e
LP
1199 return r;
1200}
1201
81a2b7ce
LP
1202static int service_spawn(
1203 Service *s,
1204 ExecCommand *c,
1205 bool timeout,
1206 bool pass_fds,
1207 bool apply_permissions,
1208 bool apply_chroot,
1209 pid_t *_pid) {
1210
034c6ed7
LP
1211 pid_t pid;
1212 int r;
44d8db9e
LP
1213 int *fds = NULL;
1214 unsigned n_fds = 0;
9e2f7c11 1215 char **argv;
034c6ed7
LP
1216
1217 assert(s);
1218 assert(c);
1219 assert(_pid);
1220
4f2d528d
LP
1221 if (pass_fds) {
1222 if (s->socket_fd >= 0) {
1223 fds = &s->socket_fd;
1224 n_fds = 1;
1225 } else if ((r = service_collect_fds(s, &fds, &n_fds)) < 0)
44d8db9e 1226 goto fail;
4f2d528d 1227 }
44d8db9e 1228
e558336f 1229 if (timeout && s->timeout_usec) {
acbb0225 1230 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
034c6ed7
LP
1231 goto fail;
1232 } else
acbb0225 1233 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7 1234
9e2f7c11
LP
1235 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1236 r = -ENOMEM;
1237 goto fail;
1238 }
1239
1240 r = exec_spawn(c,
1241 argv,
1242 &s->exec_context,
1243 fds, n_fds,
1244 apply_permissions,
1245 apply_chroot,
1246 UNIT(s)->meta.manager->confirm_spawn,
1247 UNIT(s)->meta.cgroup_bondings,
1248 &pid);
1249
1250 strv_free(argv);
1251 if (r < 0)
034c6ed7
LP
1252 goto fail;
1253
4f2d528d
LP
1254 if (fds) {
1255 if (s->socket_fd >= 0)
1256 service_close_socket_fd(s);
1257 else
1258 free(fds);
1259 }
1260
87f0e418 1261 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
034c6ed7
LP
1262 /* FIXME: we need to do something here */
1263 goto fail;
1264
1265 *_pid = pid;
1266
5cb5a6ff 1267 return 0;
034c6ed7
LP
1268
1269fail:
44d8db9e
LP
1270 free(fds);
1271
034c6ed7 1272 if (timeout)
acbb0225 1273 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7
LP
1274
1275 return r;
1276}
1277
80876c20
LP
1278static int main_pid_good(Service *s) {
1279 assert(s);
1280
1281 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1282 * don't know */
1283
1284 /* If we know the pid file, then lets just check if it is
1285 * still valid */
1286 if (s->main_pid_known)
1287 return s->main_pid > 0;
1288
1289 /* We don't know the pid */
1290 return -EAGAIN;
1291}
1292
1293static int control_pid_good(Service *s) {
1294 assert(s);
1295
1296 return s->control_pid > 0;
1297}
1298
1299static int cgroup_good(Service *s) {
1300 int r;
1301
1302 assert(s);
1303
1304 if (s->valid_no_process)
1305 return -EAGAIN;
1306
1307 if ((r = cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings)) < 0)
1308 return r;
1309
1310 return !r;
1311}
1312
034c6ed7
LP
1313static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1314 int r;
1315 assert(s);
1316
1317 if (!success)
1318 s->failure = true;
1319
1320 if (allow_restart &&
3a762661 1321 s->allow_restart &&
034c6ed7
LP
1322 (s->restart == SERVICE_RESTART_ALWAYS ||
1323 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1324
acbb0225 1325 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
034c6ed7
LP
1326 goto fail;
1327
1328 service_set_state(s, SERVICE_AUTO_RESTART);
1329 } else
1330 service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD);
1331
1332 return;
1333
1334fail:
9e2f7c11 1335 log_warning("%s failed to run install restart timer: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1336 service_enter_dead(s, false, false);
1337}
1338
1339static void service_enter_signal(Service *s, ServiceState state, bool success);
1340
1341static void service_enter_stop_post(Service *s, bool success) {
1342 int r;
1343 assert(s);
1344
1345 if (!success)
1346 s->failure = true;
1347
5e94833f
LP
1348 service_unwatch_control_pid(s);
1349
a16e1123 1350 s->control_command_id = SERVICE_EXEC_STOP_POST;
80876c20 1351 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
81a2b7ce
LP
1352 if ((r = service_spawn(s,
1353 s->control_command,
1354 true,
1355 false,
1356 !s->permissions_start_only,
1357 !s->root_directory_start_only,
1358 &s->control_pid)) < 0)
034c6ed7
LP
1359 goto fail;
1360
d6ea93e3 1361
80876c20
LP
1362 service_set_state(s, SERVICE_STOP_POST);
1363 } else
1364 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
034c6ed7
LP
1365
1366 return;
1367
1368fail:
9e2f7c11 1369 log_warning("%s failed to run stop-post executable: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1370 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1371}
1372
1373static void service_enter_signal(Service *s, ServiceState state, bool success) {
1374 int r;
1375 bool sent = false;
1376
1377 assert(s);
1378
1379 if (!success)
1380 s->failure = true;
1381
80876c20
LP
1382 if (s->kill_mode != KILL_NONE) {
1383 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL;
034c6ed7 1384
50159e6a 1385 if (s->kill_mode == KILL_CONTROL_GROUP) {
034c6ed7 1386
50159e6a
LP
1387 if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) {
1388 if (r != -EAGAIN && r != -ESRCH)
1389 goto fail;
1390 } else
034c6ed7
LP
1391 sent = true;
1392 }
1393
50159e6a
LP
1394 if (!sent) {
1395 r = 0;
80876c20 1396
50159e6a
LP
1397 if (s->main_pid > 0) {
1398 if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH)
1399 r = -errno;
1400 else
1401 sent = true;
1402 }
1403
1404 if (s->control_pid > 0) {
1405 if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH)
1406 r = -errno;
1407 else
1408 sent = true;
1409 }
1410
1411 if (r < 0)
1412 goto fail;
1413 }
d6ea93e3 1414 }
034c6ed7 1415
80876c20 1416 if (sent) {
e558336f
LP
1417 if (s->timeout_usec > 0)
1418 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1419 goto fail;
d6ea93e3 1420
80876c20
LP
1421 service_set_state(s, state);
1422 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1423 service_enter_stop_post(s, true);
1424 else
034c6ed7
LP
1425 service_enter_dead(s, true, true);
1426
1427 return;
1428
1429fail:
9e2f7c11 1430 log_warning("%s failed to kill processes: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7 1431
80876c20 1432 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
034c6ed7
LP
1433 service_enter_stop_post(s, false);
1434 else
1435 service_enter_dead(s, false, true);
1436}
1437
1438static void service_enter_stop(Service *s, bool success) {
1439 int r;
1440 assert(s);
1441
1442 if (!success)
1443 s->failure = true;
1444
5e94833f
LP
1445 service_unwatch_control_pid(s);
1446
a16e1123 1447 s->control_command_id = SERVICE_EXEC_STOP;
80876c20 1448 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
81a2b7ce
LP
1449 if ((r = service_spawn(s,
1450 s->control_command,
1451 true,
1452 false,
1453 !s->permissions_start_only,
1454 !s->root_directory_start_only,
1455 &s->control_pid)) < 0)
034c6ed7
LP
1456 goto fail;
1457
80876c20
LP
1458 service_set_state(s, SERVICE_STOP);
1459 } else
034c6ed7
LP
1460 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1461
1462 return;
1463
1464fail:
9e2f7c11 1465 log_warning("%s failed to run stop executable: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1466 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1467}
1468
80876c20
LP
1469static void service_enter_running(Service *s, bool success) {
1470 assert(s);
1471
1472 if (!success)
1473 s->failure = true;
1474
05e343b7
LP
1475 if (main_pid_good(s) != 0 &&
1476 cgroup_good(s) != 0 &&
1477 (s->bus_name_good || s->type != SERVICE_DBUS))
80876c20
LP
1478 service_set_state(s, SERVICE_RUNNING);
1479 else if (s->valid_no_process)
1480 service_set_state(s, SERVICE_EXITED);
1481 else
1482 service_enter_stop(s, true);
1483}
1484
034c6ed7
LP
1485static void service_enter_start_post(Service *s) {
1486 int r;
1487 assert(s);
1488
5e94833f
LP
1489 service_unwatch_control_pid(s);
1490
a16e1123 1491 s->control_command_id = SERVICE_EXEC_START_POST;
80876c20 1492 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
81a2b7ce
LP
1493 if ((r = service_spawn(s,
1494 s->control_command,
1495 true,
1496 false,
1497 !s->permissions_start_only,
1498 !s->root_directory_start_only,
1499 &s->control_pid)) < 0)
034c6ed7
LP
1500 goto fail;
1501
d6ea93e3 1502
80876c20
LP
1503 service_set_state(s, SERVICE_START_POST);
1504 } else
1505 service_enter_running(s, true);
034c6ed7
LP
1506
1507 return;
1508
1509fail:
9e2f7c11 1510 log_warning("%s failed to run start-post executable: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1511 service_enter_stop(s, false);
1512}
1513
1514static void service_enter_start(Service *s) {
1515 pid_t pid;
1516 int r;
1517
1518 assert(s);
1519
1520 assert(s->exec_command[SERVICE_EXEC_START]);
1521 assert(!s->exec_command[SERVICE_EXEC_START]->command_next);
1522
80876c20
LP
1523 if (s->type == SERVICE_FORKING)
1524 service_unwatch_control_pid(s);
1525 else
1526 service_unwatch_main_pid(s);
1527
81a2b7ce
LP
1528 if ((r = service_spawn(s,
1529 s->exec_command[SERVICE_EXEC_START],
05e343b7 1530 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS,
81a2b7ce
LP
1531 true,
1532 true,
1533 true,
1534 &pid)) < 0)
034c6ed7
LP
1535 goto fail;
1536
1537 if (s->type == SERVICE_SIMPLE) {
1538 /* For simple services we immediately start
1539 * the START_POST binaries. */
1540
1541 s->main_pid = pid;
1542 s->main_pid_known = true;
80876c20 1543
034c6ed7
LP
1544 service_enter_start_post(s);
1545
1546 } else if (s->type == SERVICE_FORKING) {
1547
1548 /* For forking services we wait until the start
1549 * process exited. */
1550
1551 s->control_pid = pid;
80876c20 1552
a16e1123 1553 s->control_command_id = SERVICE_EXEC_START;
034c6ed7 1554 s->control_command = s->exec_command[SERVICE_EXEC_START];
80876c20
LP
1555 service_set_state(s, SERVICE_START);
1556
05e343b7
LP
1557 } else if (s->type == SERVICE_FINISH ||
1558 s->type == SERVICE_DBUS) {
7d55e835
LP
1559
1560 /* For finishing services we wait until the start
1561 * process exited, too, but it is our main process. */
1562
05e343b7
LP
1563 /* For D-Bus services we know the main pid right away,
1564 * but wait for the bus name to appear on the bus. */
1565
7d55e835 1566 s->main_pid = pid;
5e94833f 1567 s->main_pid_known = true;
80876c20 1568
80876c20 1569 service_set_state(s, SERVICE_START);
034c6ed7
LP
1570 } else
1571 assert_not_reached("Unknown service type");
1572
1573 return;
1574
1575fail:
9e2f7c11 1576 log_warning("%s failed to run start exectuable: %s", UNIT(s)->meta.id, strerror(-r));
80876c20 1577 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
1578}
1579
1580static void service_enter_start_pre(Service *s) {
1581 int r;
1582
1583 assert(s);
1584
5e94833f
LP
1585 service_unwatch_control_pid(s);
1586
a16e1123 1587 s->control_command_id = SERVICE_EXEC_START_PRE;
80876c20 1588 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
81a2b7ce
LP
1589 if ((r = service_spawn(s,
1590 s->control_command,
1591 true,
1592 false,
1593 !s->permissions_start_only,
1594 !s->root_directory_start_only,
1595 &s->control_pid)) < 0)
034c6ed7
LP
1596 goto fail;
1597
80876c20
LP
1598 service_set_state(s, SERVICE_START_PRE);
1599 } else
034c6ed7
LP
1600 service_enter_start(s);
1601
1602 return;
1603
1604fail:
9e2f7c11 1605 log_warning("%s failed to run start-pre executable: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1606 service_enter_dead(s, false, true);
1607}
1608
1609static void service_enter_restart(Service *s) {
1610 int r;
1611 assert(s);
1612
9ea9a0c8
LP
1613 service_enter_dead(s, true, false);
1614
87f0e418 1615 if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0)
034c6ed7
LP
1616 goto fail;
1617
9e2f7c11 1618 log_debug("%s scheduled restart job.", UNIT(s)->meta.id);
034c6ed7
LP
1619 return;
1620
1621fail:
1622
9e2f7c11 1623 log_warning("%s failed to schedule restart job: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1624 service_enter_dead(s, false, false);
1625}
1626
1627static void service_enter_reload(Service *s) {
1628 int r;
1629
1630 assert(s);
1631
5e94833f
LP
1632 service_unwatch_control_pid(s);
1633
a16e1123 1634 s->control_command_id = SERVICE_EXEC_RELOAD;
80876c20 1635 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
81a2b7ce
LP
1636 if ((r = service_spawn(s,
1637 s->control_command,
1638 true,
1639 false,
1640 !s->permissions_start_only,
1641 !s->root_directory_start_only,
1642 &s->control_pid)) < 0)
034c6ed7
LP
1643 goto fail;
1644
80876c20
LP
1645 service_set_state(s, SERVICE_RELOAD);
1646 } else
1647 service_enter_running(s, true);
034c6ed7
LP
1648
1649 return;
1650
1651fail:
9e2f7c11 1652 log_warning("%s failed to run reload executable: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7
LP
1653 service_enter_stop(s, false);
1654}
1655
1656static void service_run_next(Service *s, bool success) {
1657 int r;
1658
1659 assert(s);
1660 assert(s->control_command);
1661 assert(s->control_command->command_next);
1662
1663 if (!success)
1664 s->failure = true;
1665
1666 s->control_command = s->control_command->command_next;
1667
5e94833f
LP
1668 service_unwatch_control_pid(s);
1669
81a2b7ce
LP
1670 if ((r = service_spawn(s,
1671 s->control_command,
1672 true,
1673 false,
1674 !s->permissions_start_only,
1675 !s->root_directory_start_only,
1676 &s->control_pid)) < 0)
034c6ed7
LP
1677 goto fail;
1678
1679 return;
1680
1681fail:
9e2f7c11 1682 log_warning("%s failed to run spawn next executable: %s", UNIT(s)->meta.id, strerror(-r));
034c6ed7 1683
80876c20
LP
1684 if (s->state == SERVICE_START_PRE)
1685 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1686 else if (s->state == SERVICE_STOP)
1687 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
034c6ed7
LP
1688 else if (s->state == SERVICE_STOP_POST)
1689 service_enter_dead(s, false, true);
1690 else
1691 service_enter_stop(s, false);
5cb5a6ff
LP
1692}
1693
87f0e418
LP
1694static int service_start(Unit *u) {
1695 Service *s = SERVICE(u);
5cb5a6ff
LP
1696
1697 assert(s);
1698
034c6ed7
LP
1699 /* We cannot fulfill this request right now, try again later
1700 * please! */
1701 if (s->state == SERVICE_STOP ||
1702 s->state == SERVICE_STOP_SIGTERM ||
1703 s->state == SERVICE_STOP_SIGKILL ||
1704 s->state == SERVICE_STOP_POST ||
1705 s->state == SERVICE_FINAL_SIGTERM ||
1706 s->state == SERVICE_FINAL_SIGKILL)
5cb5a6ff
LP
1707 return -EAGAIN;
1708
034c6ed7
LP
1709 /* Already on it! */
1710 if (s->state == SERVICE_START_PRE ||
1711 s->state == SERVICE_START ||
1712 s->state == SERVICE_START_POST)
1713 return 0;
1714
1715 assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART);
5cb5a6ff 1716
1e2e8133
LP
1717 /* Make sure we don't enter a busy loop of some kind. */
1718 if (!ratelimit_test(&s->ratelimit)) {
9e2f7c11 1719 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1e2e8133
LP
1720 return -EAGAIN;
1721 }
1722
034c6ed7
LP
1723 s->failure = false;
1724 s->main_pid_known = false;
3a762661 1725 s->allow_restart = true;
034c6ed7
LP
1726
1727 service_enter_start_pre(s);
1728 return 0;
5cb5a6ff
LP
1729}
1730
87f0e418
LP
1731static int service_stop(Unit *u) {
1732 Service *s = SERVICE(u);
5cb5a6ff
LP
1733
1734 assert(s);
1735
e537352b 1736 /* Cannot do this now */
034c6ed7
LP
1737 if (s->state == SERVICE_START_PRE ||
1738 s->state == SERVICE_START ||
1739 s->state == SERVICE_START_POST ||
1740 s->state == SERVICE_RELOAD)
1741 return -EAGAIN;
1742
e537352b
LP
1743 /* Already on it */
1744 if (s->state == SERVICE_STOP ||
1745 s->state == SERVICE_STOP_SIGTERM ||
1746 s->state == SERVICE_STOP_SIGKILL ||
1747 s->state == SERVICE_STOP_POST ||
1748 s->state == SERVICE_FINAL_SIGTERM ||
1749 s->state == SERVICE_FINAL_SIGKILL)
1750 return 0;
1751
034c6ed7
LP
1752 if (s->state == SERVICE_AUTO_RESTART) {
1753 service_set_state(s, SERVICE_DEAD);
1754 return 0;
1755 }
1756
80876c20 1757 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
5cb5a6ff 1758
3a762661
LP
1759 /* This is a user request, so don't do restarts on this
1760 * shutdown. */
1761 s->allow_restart = false;
1762
034c6ed7 1763 service_enter_stop(s, true);
5cb5a6ff
LP
1764 return 0;
1765}
1766
87f0e418
LP
1767static int service_reload(Unit *u) {
1768 Service *s = SERVICE(u);
034c6ed7
LP
1769
1770 assert(s);
1771
80876c20 1772 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
034c6ed7
LP
1773
1774 service_enter_reload(s);
5cb5a6ff
LP
1775 return 0;
1776}
1777
87f0e418
LP
1778static bool service_can_reload(Unit *u) {
1779 Service *s = SERVICE(u);
034c6ed7
LP
1780
1781 assert(s);
1782
1783 return !!s->exec_command[SERVICE_EXEC_RELOAD];
1784}
1785
a16e1123
LP
1786static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
1787 Service *s = SERVICE(u);
1788
1789 assert(u);
1790 assert(f);
1791 assert(fds);
1792
1793 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
1794 unit_serialize_item(u, f, "failure", yes_no(s->failure));
1795
1796 if (s->control_pid > 0)
1797 unit_serialize_item_format(u, f, "control-pid", "%u", (unsigned) (s->control_pid));
1798
1799 if (s->main_pid > 0)
1800 unit_serialize_item_format(u, f, "main-pid", "%u", (unsigned) (s->main_pid));
1801
1802 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
1803
1804 /* There's a minor uncleanliness here: if there are multiple
1805 * commands attached here, we will start from the first one
1806 * again */
1807 if (s->control_command_id >= 0)
1808 unit_serialize_item(u, f, "control-command", mount_exec_command_to_string(s->control_command_id));
1809
1810 if (s->socket_fd >= 0) {
1811 int copy;
1812
1813 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
1814 return copy;
1815
1816 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
1817 }
1818
1819 return 0;
1820}
1821
1822static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
1823 Service *s = SERVICE(u);
1824 int r;
1825
1826 assert(u);
1827 assert(key);
1828 assert(value);
1829 assert(fds);
1830
1831 if (streq(key, "state")) {
1832 ServiceState state;
1833
1834 if ((state = service_state_from_string(value)) < 0)
1835 log_debug("Failed to parse state value %s", value);
1836 else
1837 s->deserialized_state = state;
1838 } else if (streq(key, "failure")) {
1839 int b;
1840
1841 if ((b = parse_boolean(value)) < 0)
1842 log_debug("Failed to parse failure value %s", value);
1843 else
1844 s->failure = b || s->failure;
1845 } else if (streq(key, "control-pid")) {
1846 unsigned pid;
1847
1848 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1849 log_debug("Failed to parse control-pid value %s", value);
1850 else
1851 s->control_pid = (pid_t) pid;
1852 } else if (streq(key, "main-pid")) {
1853 unsigned pid;
1854
1855 if ((r = safe_atou(value, &pid)) < 0 || pid <= 0)
1856 log_debug("Failed to parse main-pid value %s", value);
1857 else
1858 s->main_pid = (pid_t) pid;
1859 } else if (streq(key, "main-pid-known")) {
1860 int b;
1861
1862 if ((b = parse_boolean(value)) < 0)
1863 log_debug("Failed to parse main-pid-known value %s", value);
1864 else
1865 s->main_pid_known = b;
1866 } else if (streq(key, "control-command")) {
1867 ServiceExecCommand id;
1868
1869 if ((id = service_exec_command_from_string(value)) < 0)
1870 log_debug("Failed to parse exec-command value %s", value);
1871 else {
1872 s->control_command_id = id;
1873 s->control_command = s->exec_command[id];
1874 }
1875 } else if (streq(key, "socket-fd")) {
1876 int fd;
1877
1878 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
1879 log_debug("Failed to parse socket-fd value %s", value);
1880 else {
1881
1882 if (s->socket_fd >= 0)
1883 close_nointr_nofail(s->socket_fd);
1884 s->socket_fd = fdset_remove(fds, fd);
1885 }
1886 } else
1887 log_debug("Unknown serialization key '%s'", key);
1888
1889 return 0;
1890}
1891
87f0e418
LP
1892static UnitActiveState service_active_state(Unit *u) {
1893 assert(u);
5cb5a6ff 1894
acbb0225 1895 return state_translation_table[SERVICE(u)->state];
034c6ed7
LP
1896}
1897
10a94420
LP
1898static const char *service_sub_state_to_string(Unit *u) {
1899 assert(u);
1900
1901 return service_state_to_string(SERVICE(u)->state);
1902}
1903
701cc384
LP
1904static bool service_check_gc(Unit *u) {
1905 Service *s = SERVICE(u);
1906
1907 assert(s);
1908
1909 return !!s->sysv_path;
1910}
1911
1912static bool service_check_snapshot(Unit *u) {
1913 Service *s = SERVICE(u);
1914
1915 assert(s);
1916
1917 return !s->got_socket_fd;
1918}
1919
87f0e418
LP
1920static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
1921 Service *s = SERVICE(u);
034c6ed7 1922 bool success;
5cb5a6ff
LP
1923
1924 assert(s);
034c6ed7
LP
1925 assert(pid >= 0);
1926
bd982a8b 1927 success = code == CLD_EXITED && status == 0;
034c6ed7
LP
1928 s->failure = s->failure || !success;
1929
1930 if (s->main_pid == pid) {
1931
1932 exec_status_fill(&s->main_exec_status, pid, code, status);
1933 s->main_pid = 0;
1934
7d55e835 1935 if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) {
034c6ed7
LP
1936 assert(s->exec_command[SERVICE_EXEC_START]);
1937 s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status;
1938 }
1939
9e2f7c11 1940 log_debug("%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
034c6ed7
LP
1941
1942 /* The service exited, so the service is officially
1943 * gone. */
1944
1945 switch (s->state) {
1946
1947 case SERVICE_START_POST:
1948 case SERVICE_RELOAD:
1949 case SERVICE_STOP:
1950 /* Need to wait until the operation is
1951 * done */
1952 break;
1953
7d55e835
LP
1954 case SERVICE_START:
1955 assert(s->type == SERVICE_FINISH);
1956
1957 /* This was our main goal, so let's go on */
1958 if (success)
1959 service_enter_start_post(s);
1960 else
80876c20 1961 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
7d55e835
LP
1962 break;
1963
034c6ed7 1964 case SERVICE_RUNNING:
80876c20 1965 service_enter_running(s, success);
034c6ed7
LP
1966 break;
1967
1968 case SERVICE_STOP_SIGTERM:
1969 case SERVICE_STOP_SIGKILL:
1970
1971 if (!control_pid_good(s))
1972 service_enter_stop_post(s, success);
5cb5a6ff 1973
034c6ed7
LP
1974 /* If there is still a control process, wait for that first */
1975 break;
5cb5a6ff 1976
034c6ed7
LP
1977 default:
1978 assert_not_reached("Uh, main process died at wrong time.");
1979 }
5cb5a6ff 1980
034c6ed7 1981 } else if (s->control_pid == pid) {
034c6ed7 1982
a16e1123
LP
1983 if (s->control_command)
1984 exec_status_fill(&s->control_command->exec_status, pid, code, status);
1985
034c6ed7
LP
1986 s->control_pid = 0;
1987
9e2f7c11 1988 log_debug("%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
034c6ed7
LP
1989
1990 /* If we are shutting things down anyway we
1991 * don't care about failing commands. */
1992
a16e1123 1993 if (s->control_command && s->control_command->command_next && success) {
034c6ed7
LP
1994
1995 /* There is another command to *
1996 * execute, so let's do that. */
1997
9e2f7c11 1998 log_debug("%s running next command for state %s", u->meta.id, service_state_to_string(s->state));
034c6ed7
LP
1999 service_run_next(s, success);
2000
80876c20 2001 } else {
034c6ed7
LP
2002 /* No further commands for this step, so let's
2003 * figure out what to do next */
2004
a16e1123
LP
2005 s->control_command = NULL;
2006 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2007
9e2f7c11 2008 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
bd982a8b 2009
034c6ed7
LP
2010 switch (s->state) {
2011
2012 case SERVICE_START_PRE:
2013 if (success)
2014 service_enter_start(s);
2015 else
80876c20 2016 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
2017 break;
2018
2019 case SERVICE_START:
2020 assert(s->type == SERVICE_FORKING);
2021
2022 /* Let's try to load the pid
2023 * file here if we can. We
2024 * ignore the return value,
2025 * since the PID file might
2026 * actually be created by a
2027 * START_POST script */
2028
2029 if (success) {
2030 if (s->pid_file)
2031 service_load_pid_file(s);
2032
2033 service_enter_start_post(s);
2034 } else
80876c20 2035 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
2036
2037 break;
2038
2039 case SERVICE_START_POST:
2040 if (success && s->pid_file && !s->main_pid_known) {
2041 int r;
2042
2043 /* Hmm, let's see if we can
2044 * load the pid now after the
2045 * start-post scripts got
2046 * executed. */
2047
2048 if ((r = service_load_pid_file(s)) < 0)
9e2f7c11 2049 log_warning("%s: failed to load PID file %s: %s", UNIT(s)->meta.id, s->pid_file, strerror(-r));
034c6ed7
LP
2050 }
2051
2052 /* Fall through */
2053
2054 case SERVICE_RELOAD:
80876c20
LP
2055 if (success)
2056 service_enter_running(s, true);
2057 else
034c6ed7
LP
2058 service_enter_stop(s, false);
2059
2060 break;
2061
2062 case SERVICE_STOP:
80876c20 2063 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
034c6ed7
LP
2064 break;
2065
2066 case SERVICE_STOP_SIGTERM:
2067 case SERVICE_STOP_SIGKILL:
2068 if (main_pid_good(s) <= 0)
2069 service_enter_stop_post(s, success);
2070
2071 /* If there is still a service
2072 * process around, wait until
2073 * that one quit, too */
2074 break;
2075
2076 case SERVICE_STOP_POST:
2077 case SERVICE_FINAL_SIGTERM:
2078 case SERVICE_FINAL_SIGKILL:
2079 service_enter_dead(s, success, true);
2080 break;
2081
2082 default:
2083 assert_not_reached("Uh, control process died at wrong time.");
2084 }
2085 }
2086 } else
2087 assert_not_reached("Got SIGCHLD for unkown PID");
2088}
2089
acbb0225 2090static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
87f0e418 2091 Service *s = SERVICE(u);
034c6ed7
LP
2092
2093 assert(s);
2094 assert(elapsed == 1);
2095
acbb0225 2096 assert(w == &s->timer_watch);
034c6ed7
LP
2097
2098 switch (s->state) {
2099
2100 case SERVICE_START_PRE:
2101 case SERVICE_START:
9e2f7c11 2102 log_warning("%s operation timed out. Terminating.", u->meta.id);
80876c20
LP
2103 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2104 break;
2105
034c6ed7
LP
2106 case SERVICE_START_POST:
2107 case SERVICE_RELOAD:
9e2f7c11 2108 log_warning("%s operation timed out. Stopping.", u->meta.id);
034c6ed7
LP
2109 service_enter_stop(s, false);
2110 break;
2111
2112 case SERVICE_STOP:
9e2f7c11 2113 log_warning("%s stopping timed out. Terminating.", u->meta.id);
034c6ed7
LP
2114 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2115 break;
2116
2117 case SERVICE_STOP_SIGTERM:
9e2f7c11 2118 log_warning("%s stopping timed out. Killing.", u->meta.id);
034c6ed7
LP
2119 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2120 break;
2121
2122 case SERVICE_STOP_SIGKILL:
2123 /* Uh, wie sent a SIGKILL and it is still not gone?
2124 * Must be something we cannot kill, so let's just be
2125 * weirded out and continue */
2126
9e2f7c11 2127 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
034c6ed7
LP
2128 service_enter_stop_post(s, false);
2129 break;
2130
2131 case SERVICE_STOP_POST:
9e2f7c11 2132 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
034c6ed7
LP
2133 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2134 break;
2135
2136 case SERVICE_FINAL_SIGTERM:
9e2f7c11 2137 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
034c6ed7
LP
2138 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2139 break;
2140
2141 case SERVICE_FINAL_SIGKILL:
9e2f7c11 2142 log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", u->meta.id);
034c6ed7
LP
2143 service_enter_dead(s, false, true);
2144 break;
2145
2146 case SERVICE_AUTO_RESTART:
9e2f7c11 2147 log_debug("%s holdoff time over, scheduling restart.", u->meta.id);
034c6ed7
LP
2148 service_enter_restart(s);
2149 break;
2150
2151 default:
2152 assert_not_reached("Timeout at wrong time.");
2153 }
5cb5a6ff
LP
2154}
2155
8e274523
LP
2156static void service_cgroup_notify_event(Unit *u) {
2157 Service *s = SERVICE(u);
2158
2159 assert(u);
2160
9e2f7c11 2161 log_debug("%s: cgroup is empty", u->meta.id);
8e274523
LP
2162
2163 switch (s->state) {
2164
2165 /* Waiting for SIGCHLD is usually more interesting,
2166 * because it includes return codes/signals. Which is
2167 * why we ignore the cgroup events for most cases,
2168 * except when we don't know pid which to expect the
2169 * SIGCHLD for. */
2170
2171 case SERVICE_RUNNING:
80876c20 2172 service_enter_running(s, true);
8e274523
LP
2173 break;
2174
2175 default:
2176 ;
2177 }
2178}
2179
2c4104f0 2180static int service_enumerate(Manager *m) {
2c4104f0
LP
2181 char **p;
2182 unsigned i;
2183 DIR *d = NULL;
2184 char *path = NULL, *fpath = NULL, *name = NULL;
2185 int r;
2186
2187 assert(m);
2188
5e177ece 2189 STRV_FOREACH(p, m->sysvrcnd_path)
23a177ef 2190 for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) {
2c4104f0
LP
2191 struct dirent *de;
2192
2193 free(path);
2194 path = NULL;
23a177ef 2195 if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) {
2c4104f0
LP
2196 r = -ENOMEM;
2197 goto finish;
2198 }
2199
2200 if (d)
2201 closedir(d);
2202
2203 if (!(d = opendir(path))) {
2204 if (errno != ENOENT)
2205 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2206
2207 continue;
2208 }
2209
2210 while ((de = readdir(d))) {
6542952f 2211 Unit *service;
2c4104f0
LP
2212
2213 if (ignore_file(de->d_name))
2214 continue;
2215
2216 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2217 continue;
2218
2219 if (strlen(de->d_name) < 4)
2220 continue;
2221
2222 free(fpath);
2223 fpath = NULL;
23a177ef 2224 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) {
2c4104f0
LP
2225 r = -ENOMEM;
2226 goto finish;
2227 }
2228
2229 if (access(fpath, X_OK) < 0) {
2230
2231 if (errno != ENOENT)
2232 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2233
2234 continue;
2235 }
2236
2237 free(name);
2238 name = NULL;
2239 if (asprintf(&name, "%s.service", de->d_name+3) < 0) {
2240 r = -ENOMEM;
2241 goto finish;
2242 }
2243
9e2f7c11 2244 if ((r = manager_load_unit(m, name, NULL, &service)) < 0)
2c4104f0
LP
2245 goto finish;
2246
2c4104f0 2247 if (de->d_name[0] == 'S') {
6542952f
LP
2248 Unit *runlevel_target;
2249
2250 if ((r = manager_load_unit(m, rcnd_table[i+1], NULL, &runlevel_target)) < 0)
2251 goto finish;
2252
2253 if ((r = unit_add_dependency(runlevel_target, UNIT_WANTS, service, true)) < 0)
2c4104f0
LP
2254 goto finish;
2255
6542952f 2256 if ((r = unit_add_dependency(runlevel_target, UNIT_AFTER, service, true)) < 0)
2c4104f0 2257 goto finish;
23a177ef
LP
2258
2259 } else if (de->d_name[0] == 'K' &&
2260 (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) ||
2261 streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) {
2262
6542952f
LP
2263 Unit *shutdown_target;
2264
23a177ef
LP
2265 /* We honour K links only for
2266 * halt/reboot. For the normal
2267 * runlevels we assume the
2268 * stop jobs will be
2269 * implicitly added by the
6542952f
LP
2270 * core logic. Also, we don't
2271 * really distuingish here
2272 * between the runlevels 0 and
2273 * 6 and just add them to the
2274 * special shutdown target. */
2275
2276 if ((r = manager_load_unit(m, SPECIAL_SHUTDOWN_TARGET, NULL, &shutdown_target)) < 0)
2277 goto finish;
23a177ef 2278
6542952f 2279 if ((r = unit_add_dependency(shutdown_target, UNIT_CONFLICTS, service, true)) < 0)
2c4104f0
LP
2280 goto finish;
2281
6542952f 2282 if ((r = unit_add_dependency(shutdown_target, UNIT_BEFORE, service, true)) < 0)
2c4104f0
LP
2283 goto finish;
2284 }
2285 }
2286 }
2287
2288 r = 0;
2289
2290finish:
2291 free(path);
2292 free(fpath);
2293 free(name);
2294 closedir(d);
2295
2296 return r;
2297}
2298
05e343b7
LP
2299static void service_bus_name_owner_change(
2300 Unit *u,
2301 const char *name,
2302 const char *old_owner,
2303 const char *new_owner) {
2304
2305 Service *s = SERVICE(u);
2306
2307 assert(s);
2308 assert(name);
2309
2310 assert(streq(s->bus_name, name));
2311 assert(old_owner || new_owner);
2312
2313 if (old_owner && new_owner)
2314 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2315 else if (old_owner)
2316 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2317 else
2318 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2319
2320 s->bus_name_good = !!new_owner;
2321
2322 if (s->type == SERVICE_DBUS) {
2323
2324 /* service_enter_running() will figure out what to
2325 * do */
2326 if (s->state == SERVICE_RUNNING)
2327 service_enter_running(s, true);
2328 else if (s->state == SERVICE_START && new_owner)
2329 service_enter_start_post(s);
2330
2331 } else if (new_owner &&
2332 s->main_pid <= 0 &&
2333 (s->state == SERVICE_START ||
2334 s->state == SERVICE_START_POST ||
2335 s->state == SERVICE_RUNNING ||
2336 s->state == SERVICE_RELOAD)) {
2337
2338 /* Try to acquire PID from bus service */
2339 log_debug("Trying to acquire PID from D-Bus name...");
2340
2341 bus_query_pid(u->meta.manager, name);
2342 }
2343}
2344
2345static void service_bus_query_pid_done(
2346 Unit *u,
2347 const char *name,
2348 pid_t pid) {
2349
2350 Service *s = SERVICE(u);
2351
2352 assert(s);
2353 assert(name);
2354
2355 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2356
2357 if (s->main_pid <= 0 &&
2358 (s->state == SERVICE_START ||
2359 s->state == SERVICE_START_POST ||
2360 s->state == SERVICE_RUNNING ||
2361 s->state == SERVICE_RELOAD))
2362 s->main_pid = pid;
2363}
2364
4f2d528d
LP
2365int service_set_socket_fd(Service *s, int fd) {
2366 assert(s);
2367 assert(fd >= 0);
2368
2369 /* This is called by the socket code when instantiating a new
2370 * service for a stream socket and the socket needs to be
2371 * configured. */
2372
2373 if (UNIT(s)->meta.load_state != UNIT_LOADED)
2374 return -EINVAL;
2375
2376 if (s->socket_fd >= 0)
2377 return -EBUSY;
2378
2379 if (s->state != SERVICE_DEAD)
2380 return -EAGAIN;
2381
2382 s->socket_fd = fd;
701cc384 2383 s->got_socket_fd = true;
4f2d528d
LP
2384 return 0;
2385}
2386
94f04347
LP
2387static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2388 [SERVICE_DEAD] = "dead",
2389 [SERVICE_START_PRE] = "start-pre",
2390 [SERVICE_START] = "start",
2391 [SERVICE_START_POST] = "start-post",
2392 [SERVICE_RUNNING] = "running",
80876c20 2393 [SERVICE_EXITED] = "exited",
94f04347
LP
2394 [SERVICE_RELOAD] = "reload",
2395 [SERVICE_STOP] = "stop",
2396 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2397 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2398 [SERVICE_STOP_POST] = "stop-post",
2399 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2400 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2401 [SERVICE_MAINTAINANCE] = "maintainance",
2402 [SERVICE_AUTO_RESTART] = "auto-restart",
2403};
2404
2405DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2406
2407static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2408 [SERVICE_ONCE] = "once",
2409 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
40531a55 2410 [SERVICE_RESTART_ALWAYS] = "restart-always",
94f04347
LP
2411};
2412
2413DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2414
2415static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2416 [SERVICE_FORKING] = "forking",
2417 [SERVICE_SIMPLE] = "simple",
05e343b7
LP
2418 [SERVICE_FINISH] = "finish",
2419 [SERVICE_DBUS] = "dbus"
94f04347
LP
2420};
2421
2422DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2423
e537352b 2424static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
94f04347
LP
2425 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2426 [SERVICE_EXEC_START] = "ExecStart",
2427 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2428 [SERVICE_EXEC_RELOAD] = "ExecReload",
2429 [SERVICE_EXEC_STOP] = "ExecStop",
2430 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2431};
2432
2433DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2434
87f0e418 2435const UnitVTable service_vtable = {
5cb5a6ff
LP
2436 .suffix = ".service",
2437
034c6ed7
LP
2438 .init = service_init,
2439 .done = service_done,
a16e1123
LP
2440 .load = service_load,
2441
2442 .coldplug = service_coldplug,
034c6ed7 2443
5cb5a6ff
LP
2444 .dump = service_dump,
2445
2446 .start = service_start,
2447 .stop = service_stop,
2448 .reload = service_reload,
2449
034c6ed7
LP
2450 .can_reload = service_can_reload,
2451
a16e1123
LP
2452 .serialize = service_serialize,
2453 .deserialize_item = service_deserialize_item,
2454
5cb5a6ff 2455 .active_state = service_active_state,
10a94420 2456 .sub_state_to_string = service_sub_state_to_string,
5cb5a6ff 2457
701cc384
LP
2458 .check_gc = service_check_gc,
2459 .check_snapshot = service_check_snapshot,
2460
034c6ed7
LP
2461 .sigchld_event = service_sigchld_event,
2462 .timer_event = service_timer_event,
2c4104f0 2463
8e274523
LP
2464 .cgroup_notify_empty = service_cgroup_notify_event,
2465
05e343b7
LP
2466 .bus_name_owner_change = service_bus_name_owner_change,
2467 .bus_query_pid_done = service_bus_query_pid_done,
2468
4139c1b2
LP
2469 .bus_message_handler = bus_service_message_handler,
2470
2c4104f0 2471 .enumerate = service_enumerate
5cb5a6ff 2472};