]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/service.c
modules-load: use /etc/modules-load.d/?*.conf
[thirdparty/systemd.git] / src / service.c
CommitLineData
d6c9574f 1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
5cb5a6ff 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"
514f4ef5 35#include "special.h"
398ef8ba 36#include "bus-errors.h"
2c4104f0
LP
37
38#define COMMENTS "#;\n"
39#define NEWLINES "\n\r"
034c6ed7 40
07459bb6 41#ifdef HAVE_SYSV_COMPAT
09cd1ab1
LP
42typedef enum RunlevelType {
43 RUNLEVEL_UP,
44 RUNLEVEL_DOWN,
fc5df99e 45 RUNLEVEL_SYSINIT
09cd1ab1
LP
46} RunlevelType;
47
48static const struct {
49 const char *path;
50 const char *target;
51 const RunlevelType type;
52} rcnd_table[] = {
9d25f5ed 53 /* Standard SysV runlevels for start-up */
514f4ef5 54 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
fbe9f3a9
LP
55 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
56 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
57 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
58 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
fbe9f3a9 59
d4054675 60#ifdef TARGET_SUSE
cfe243e3 61 /* SUSE style boot.d */
fc5df99e 62 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
d4054675 63#endif
fbe9f3a9 64
d4054675 65#ifdef TARGET_DEBIAN
fbe9f3a9 66 /* Debian style rcS.d */
fc5df99e 67 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
d4054675 68#endif
9d25f5ed
LP
69
70 /* Standard SysV runlevels for shutdown */
71 { "rc0.d", SPECIAL_POWEROFF_TARGET, RUNLEVEL_DOWN },
72 { "rc6.d", SPECIAL_REBOOT_TARGET, RUNLEVEL_DOWN }
73
74 /* Note that the order here matters, as we read the
75 directories in this order, and we want to make sure that
76 sysv_start_priority is known when we first load the
77 unit. And that value we only know from S links. Hence
78 UP/SYSINIT must be read before DOWN */
23a177ef
LP
79};
80
09cd1ab1 81#define RUNLEVELS_UP "12345"
fbe9f3a9
LP
82/* #define RUNLEVELS_DOWN "06" */
83/* #define RUNLEVELS_BOOT "bBsS" */
07459bb6 84#endif
09cd1ab1 85
acbb0225 86static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
87f0e418
LP
87 [SERVICE_DEAD] = UNIT_INACTIVE,
88 [SERVICE_START_PRE] = UNIT_ACTIVATING,
89 [SERVICE_START] = UNIT_ACTIVATING,
90 [SERVICE_START_POST] = UNIT_ACTIVATING,
91 [SERVICE_RUNNING] = UNIT_ACTIVE,
80876c20 92 [SERVICE_EXITED] = UNIT_ACTIVE,
032ff4af 93 [SERVICE_RELOAD] = UNIT_RELOADING,
87f0e418
LP
94 [SERVICE_STOP] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
96 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
97 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
98 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
99 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
fdf20a31 100 [SERVICE_FAILED] = UNIT_FAILED,
6124958c 101 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
034c6ed7 102};
5cb5a6ff 103
a16e1123
LP
104static void service_init(Unit *u) {
105 Service *s = SERVICE(u);
106
107 assert(u);
108 assert(u->meta.load_state == UNIT_STUB);
109
110 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
111 s->restart_usec = DEFAULT_RESTART_USEC;
112 s->timer_watch.type = WATCH_INVALID;
07459bb6 113#ifdef HAVE_SYSV_COMPAT
a16e1123 114 s->sysv_start_priority = -1;
07459bb6 115#endif
a16e1123
LP
116 s->socket_fd = -1;
117
118 exec_context_init(&s->exec_context);
119
120 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
121
122 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
123}
124
5e94833f
LP
125static void service_unwatch_control_pid(Service *s) {
126 assert(s);
127
128 if (s->control_pid <= 0)
129 return;
130
131 unit_unwatch_pid(UNIT(s), s->control_pid);
132 s->control_pid = 0;
133}
134
135static void service_unwatch_main_pid(Service *s) {
136 assert(s);
137
138 if (s->main_pid <= 0)
139 return;
140
141 unit_unwatch_pid(UNIT(s), s->main_pid);
142 s->main_pid = 0;
143}
144
5925dd3c 145static int service_set_main_pid(Service *s, pid_t pid) {
e55224ca
LP
146 pid_t ppid;
147
5925dd3c
LP
148 assert(s);
149
150 if (pid <= 1)
151 return -EINVAL;
152
153 if (pid == getpid())
154 return -EINVAL;
155
e55224ca
LP
156 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
157 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
158 s->meta.id, (unsigned long) pid);
159
5925dd3c
LP
160 s->main_pid = pid;
161 s->main_pid_known = true;
162
b58b4116
LP
163 exec_status_start(&s->main_exec_status, pid);
164
5925dd3c
LP
165 return 0;
166}
167
4f2d528d
LP
168static void service_close_socket_fd(Service *s) {
169 assert(s);
170
171 if (s->socket_fd < 0)
172 return;
173
174 close_nointr_nofail(s->socket_fd);
175 s->socket_fd = -1;
176}
177
6cf6bbc2
LP
178static void service_connection_unref(Service *s) {
179 assert(s);
180
181 if (!s->socket)
182 return;
183
184 socket_connection_unref(s->socket);
185 s->socket = NULL;
186}
187
87f0e418
LP
188static void service_done(Unit *u) {
189 Service *s = SERVICE(u);
44d8db9e
LP
190
191 assert(s);
192
193 free(s->pid_file);
194 s->pid_file = NULL;
195
07459bb6 196#ifdef HAVE_SYSV_COMPAT
2c4104f0
LP
197 free(s->sysv_path);
198 s->sysv_path = NULL;
199
8309400a
LP
200 free(s->sysv_runlevels);
201 s->sysv_runlevels = NULL;
07459bb6 202#endif
8309400a 203
8c47c732
LP
204 free(s->status_text);
205 s->status_text = NULL;
206
44d8db9e 207 exec_context_done(&s->exec_context);
e537352b 208 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
44d8db9e
LP
209 s->control_command = NULL;
210
211 /* This will leak a process, but at least no memory or any of
212 * our resources */
5e94833f
LP
213 service_unwatch_main_pid(s);
214 service_unwatch_control_pid(s);
44d8db9e 215
05e343b7
LP
216 if (s->bus_name) {
217 unit_unwatch_bus_name(UNIT(u), s->bus_name);
218 free(s->bus_name);
219 s->bus_name = NULL;
220 }
221
4f2d528d 222 service_close_socket_fd(s);
6cf6bbc2 223 service_connection_unref(s);
4f2d528d 224
acbb0225 225 unit_unwatch_timer(u, &s->timer_watch);
44d8db9e
LP
226}
227
07459bb6 228#ifdef HAVE_SYSV_COMPAT
b7ccee3c
LP
229static char *sysv_translate_name(const char *name) {
230 char *r;
231
232 if (!(r = new(char, strlen(name) + sizeof(".service"))))
233 return NULL;
234
235 if (startswith(name, "boot."))
236 /* Drop SuSE-style boot. prefix */
237 strcpy(stpcpy(r, name + 5), ".service");
238 else if (endswith(name, ".sh"))
239 /* Drop Debian-style .sh suffix */
240 strcpy(stpcpy(r, name) - 3, ".service");
980900c1 241#ifdef TARGET_ARCH
9f151f29
LP
242 else if (startswith(name, "@"))
243 /* Drop Arch-style background prefix */
244 strcpy(stpcpy(r, name + 1), ".service");
980900c1 245#endif
b7ccee3c
LP
246 else
247 /* Normal init scripts */
248 strcpy(stpcpy(r, name), ".service");
249
250 return r;
251}
252
253static int sysv_translate_facility(const char *name, char **_r) {
2c4104f0
LP
254
255 static const char * const table[] = {
6464aa08 256 /* LSB defined facilities */
2c4104f0
LP
257 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
258 "$network", SPECIAL_NETWORK_TARGET,
259 "$named", SPECIAL_NSS_LOOKUP_TARGET,
260 "$portmap", SPECIAL_RPCBIND_TARGET,
261 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
262 "$syslog", SPECIAL_SYSLOG_TARGET,
6464aa08
LP
263 "$time", SPECIAL_RTC_SET_TARGET,
264
265 /* Debian extensions */
3177a49c 266#ifdef TARGET_DEBIAN
6464aa08 267 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
3177a49c 268#endif
6464aa08 269 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
3177a49c
LP
270 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
271
272#ifdef TARGET_FEDORA
273 /* Fedora extensions, lacking the $ prefix */
274 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
4f7dcad3
LP
275 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
276 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
3177a49c 277#endif
2c4104f0
LP
278 };
279
280 unsigned i;
281 char *r;
282
cb33e400
LP
283 /* SuSE insserv extension */
284 if (streq(name, "$null"))
285 return 0;
286
2c4104f0
LP
287 for (i = 0; i < ELEMENTSOF(table); i += 2)
288 if (streq(table[i], name)) {
289 if (!(r = strdup(table[i+1])))
290 return -ENOMEM;
291
292 goto finish;
293 }
294
295 if (*name == '$')
caca31b5 296 /* This is a heuristic. */
32159d3a
LP
297 r = unit_name_build(name+1, NULL, ".target");
298 else
299 r = sysv_translate_name(name);
2c4104f0 300
32159d3a 301 if (!r)
2c4104f0
LP
302 return -ENOMEM;
303
304finish:
305
306 if (_r)
307 *_r = r;
308
309 return 1;
310}
311
56d748b4 312static int sysv_fix_order(Service *s) {
2c4104f0
LP
313 Meta *other;
314 int r;
315
316 assert(s);
317
318 if (s->sysv_start_priority < 0)
319 return 0;
320
23a177ef
LP
321 /* For each pair of services where at least one lacks a LSB
322 * header, we use the start priority value to order things. */
2c4104f0 323
4cd1fbcc 324 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
2c4104f0
LP
325 Service *t;
326 UnitDependency d;
eeaafddc 327 bool special_s, special_t;
2c4104f0
LP
328
329 t = (Service*) other;
330
331 if (s == t)
332 continue;
9f151f29
LP
333
334 if (t->meta.load_state != UNIT_LOADED)
335 continue;
2c4104f0
LP
336
337 if (t->sysv_start_priority < 0)
338 continue;
339
51a1a79d
LP
340 /* If both units have modern headers we don't care
341 * about the priorities */
f21781d5
LP
342 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
343 (t->meta.fragment_path || t->sysv_has_lsb))
23a177ef
LP
344 continue;
345
eeaafddc
LP
346 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
347 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
348
349 if (special_t && !special_s)
350 d = UNIT_AFTER;
351 else if (special_s && !special_t)
352 d = UNIT_BEFORE;
353 else if (t->sysv_start_priority < s->sysv_start_priority)
2c4104f0
LP
354 d = UNIT_AFTER;
355 else if (t->sysv_start_priority > s->sysv_start_priority)
356 d = UNIT_BEFORE;
357 else
358 continue;
359
360 /* FIXME: Maybe we should compare the name here lexicographically? */
361
701cc384 362 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
2c4104f0
LP
363 return r;
364 }
365
366 return 0;
367}
368
369static ExecCommand *exec_command_new(const char *path, const char *arg1) {
370 ExecCommand *c;
371
372 if (!(c = new0(ExecCommand, 1)))
373 return NULL;
374
375 if (!(c->path = strdup(path))) {
376 free(c);
377 return NULL;
378 }
379
380 if (!(c->argv = strv_new(path, arg1, NULL))) {
381 free(c->path);
382 free(c);
383 return NULL;
384 }
385
386 return c;
387}
388
389static int sysv_exec_commands(Service *s) {
390 ExecCommand *c;
391
392 assert(s);
393 assert(s->sysv_path);
394
395 if (!(c = exec_command_new(s->sysv_path, "start")))
396 return -ENOMEM;
397 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
398
399 if (!(c = exec_command_new(s->sysv_path, "stop")))
400 return -ENOMEM;
401 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
402
403 if (!(c = exec_command_new(s->sysv_path, "reload")))
404 return -ENOMEM;
405 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
406
407 return 0;
408}
409
e537352b 410static int service_load_sysv_path(Service *s, const char *path) {
2c4104f0
LP
411 FILE *f;
412 Unit *u;
413 unsigned line = 0;
414 int r;
415 enum {
416 NORMAL,
417 DESCRIPTION,
418 LSB,
419 LSB_DESCRIPTION
420 } state = NORMAL;
23a177ef
LP
421
422 assert(s);
423 assert(path);
2c4104f0
LP
424
425 u = UNIT(s);
426
427 if (!(f = fopen(path, "re"))) {
428 r = errno == ENOENT ? 0 : -errno;
429 goto finish;
430 }
431
2c4104f0
LP
432 free(s->sysv_path);
433 if (!(s->sysv_path = strdup(path))) {
434 r = -ENOMEM;
435 goto finish;
436 }
437
438 while (!feof(f)) {
439 char l[LINE_MAX], *t;
440
441 if (!fgets(l, sizeof(l), f)) {
442 if (feof(f))
443 break;
444
445 r = -errno;
446 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
447 goto finish;
448 }
449
450 line++;
451
452 t = strstrip(l);
453 if (*t != '#')
454 continue;
455
456 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
457 state = LSB;
23a177ef 458 s->sysv_has_lsb = true;
2c4104f0
LP
459 continue;
460 }
461
462 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
463 state = NORMAL;
464 continue;
465 }
466
467 t++;
468 t += strspn(t, WHITESPACE);
469
470 if (state == NORMAL) {
471
472 /* Try to parse Red Hat style chkconfig headers */
473
c2b35af6 474 if (startswith_no_case(t, "chkconfig:")) {
2c4104f0 475 int start_priority;
8309400a 476 char runlevels[16], *k;
2c4104f0
LP
477
478 state = NORMAL;
479
8309400a
LP
480 if (sscanf(t+10, "%15s %i %*i",
481 runlevels,
482 &start_priority) != 2) {
2c4104f0
LP
483
484 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
485 continue;
486 }
487
fbe9f3a9
LP
488 /* A start priority gathered from the
489 * symlink farms is preferred over the
490 * data from the LSB header. */
8309400a 491 if (start_priority < 0 || start_priority > 99)
2c4104f0 492 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
db06e3b6 493 else if (s->sysv_start_priority < 0)
8309400a
LP
494 s->sysv_start_priority = start_priority;
495
496 char_array_0(runlevels);
497 k = delete_chars(runlevels, WHITESPACE "-");
498
499 if (k[0]) {
500 char *d;
501
502 if (!(d = strdup(k))) {
503 r = -ENOMEM;
504 goto finish;
505 }
506
507 free(s->sysv_runlevels);
508 s->sysv_runlevels = d;
2c4104f0
LP
509 }
510
ed4c1cc6
LP
511 } else if (startswith_no_case(t, "description:") &&
512 !u->meta.description) {
2c4104f0
LP
513
514 size_t k = strlen(t);
515 char *d;
516
517 if (t[k-1] == '\\') {
518 state = DESCRIPTION;
519 t[k-1] = 0;
520 }
521
46948365 522 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
2c4104f0
LP
523 r = -ENOMEM;
524 goto finish;
525 }
526
527 free(u->meta.description);
528 u->meta.description = d;
529
c2b35af6 530 } else if (startswith_no_case(t, "pidfile:")) {
2c4104f0
LP
531
532 char *fn;
533
534 state = NORMAL;
535
536 fn = strstrip(t+8);
537 if (!path_is_absolute(fn)) {
538 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
539 continue;
540 }
541
542 if (!(fn = strdup(fn))) {
543 r = -ENOMEM;
544 goto finish;
545 }
546
547 free(s->pid_file);
548 s->pid_file = fn;
549 }
550
551 } else if (state == DESCRIPTION) {
552
553 /* Try to parse Red Hat style description
554 * continuation */
555
556 size_t k = strlen(t);
557 char *d;
558
559 if (t[k-1] == '\\')
560 t[k-1] = 0;
561 else
562 state = NORMAL;
563
564 assert(u->meta.description);
565 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
566 r = -ENOMEM;
567 goto finish;
568 }
569
570 free(u->meta.description);
571 u->meta.description = d;
572
573 } else if (state == LSB || state == LSB_DESCRIPTION) {
574
c2b35af6 575 if (startswith_no_case(t, "Provides:")) {
2c4104f0
LP
576 char *i, *w;
577 size_t z;
578
579 state = LSB;
580
f60f22df 581 FOREACH_WORD_QUOTED(w, z, t+9, i) {
2c4104f0
LP
582 char *n, *m;
583
584 if (!(n = strndup(w, z))) {
585 r = -ENOMEM;
586 goto finish;
587 }
588
caca31b5
LP
589 if (streq(n, file_name_from_path(path))) {
590 free(n);
591 continue;
592 }
593
b7ccee3c 594 r = sysv_translate_facility(n, &m);
2c4104f0
LP
595 free(n);
596
597 if (r < 0)
598 goto finish;
599
600 if (r == 0)
601 continue;
602
bd77d0fc
LP
603 if (unit_name_to_type(m) == UNIT_SERVICE)
604 r = unit_add_name(u, m);
c68364b7
LP
605 else {
606 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
607
608 if (s->sysv_enabled) {
609 int k;
610
611 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
612 r = k;
613 }
614 }
bd77d0fc 615
2c4104f0 616 if (r < 0)
43a37549 617 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
42a097a2
LP
618
619 free(m);
2c4104f0
LP
620 }
621
c2b35af6
LP
622 } else if (startswith_no_case(t, "Required-Start:") ||
623 startswith_no_case(t, "Should-Start:") ||
624 startswith_no_case(t, "X-Start-Before:") ||
625 startswith_no_case(t, "X-Start-After:")) {
2c4104f0
LP
626 char *i, *w;
627 size_t z;
628
629 state = LSB;
630
f60f22df 631 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
2c4104f0
LP
632 char *n, *m;
633
634 if (!(n = strndup(w, z))) {
635 r = -ENOMEM;
636 goto finish;
637 }
638
caca31b5
LP
639 if (streq(n, file_name_from_path(path))) {
640 free(n);
641 continue;
642 }
643
b7ccee3c 644 r = sysv_translate_facility(n, &m);
2c4104f0
LP
645 free(n);
646
647 if (r < 0)
648 goto finish;
649
650 if (r == 0)
651 continue;
652
c2b35af6 653 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
2c4104f0
LP
654
655 if (r < 0)
43a37549 656 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
42a097a2
LP
657
658 free(m);
2c4104f0 659 }
c2b35af6 660 } else if (startswith_no_case(t, "Default-Start:")) {
8309400a
LP
661 char *k, *d;
662
663 state = LSB;
664
665 k = delete_chars(t+14, WHITESPACE "-");
666
667 if (k[0] != 0) {
668 if (!(d = strdup(k))) {
669 r = -ENOMEM;
670 goto finish;
671 }
672
673 free(s->sysv_runlevels);
674 s->sysv_runlevels = d;
675 }
2c4104f0 676
ed4c1cc6
LP
677 } else if (startswith_no_case(t, "Description:") &&
678 !u->meta.description) {
2c4104f0
LP
679 char *d;
680
ed4c1cc6
LP
681 /* We use the long description only if
682 * no short description is set. */
683
2c4104f0
LP
684 state = LSB_DESCRIPTION;
685
46948365 686 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
2c4104f0
LP
687 r = -ENOMEM;
688 goto finish;
689 }
690
691 free(u->meta.description);
692 u->meta.description = d;
693
ed4c1cc6 694 } else if (startswith_no_case(t, "Short-Description:")) {
2c4104f0
LP
695 char *d;
696
2c4104f0
LP
697 state = LSB;
698
46948365 699 if (!(d = strappend("LSB: ", strstrip(t+18)))) {
2c4104f0
LP
700 r = -ENOMEM;
701 goto finish;
702 }
703
7740296d 704 free(u->meta.description);
2c4104f0
LP
705 u->meta.description = d;
706
723c83fd
LP
707 } else if (startswith_no_case(t, "X-Interactive:")) {
708 int b;
709
710 if ((b = parse_boolean(strstrip(t+14))) < 0) {
711 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
712 continue;
713 }
714
715 if (b)
716 s->exec_context.std_input = EXEC_INPUT_TTY;
717 else
718 s->exec_context.std_input = EXEC_INPUT_NULL;
719
2c4104f0
LP
720 } else if (state == LSB_DESCRIPTION) {
721
722 if (startswith(l, "#\t") || startswith(l, "# ")) {
723 char *d;
724
725 assert(u->meta.description);
726 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
727 r = -ENOMEM;
728 goto finish;
729 }
730
731 free(u->meta.description);
732 u->meta.description = d;
733 } else
734 state = LSB;
735 }
736 }
737 }
738
2c4104f0
LP
739 if ((r = sysv_exec_commands(s)) < 0)
740 goto finish;
741
a40eb732 742 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
0bc824be
LP
743 /* If there a runlevels configured for this service
744 * but none of the standard ones, then we assume this
745 * is some special kind of service (which might be
746 * needed for early boot) and don't create any links
747 * to it. */
748
a40eb732 749 s->meta.default_dependencies = false;
09cd1ab1 750
09cd1ab1
LP
751 /* Don't timeout special services during boot (like fsck) */
752 s->timeout_usec = 0;
a40eb732 753 }
0fd030be 754
80876c20 755 /* Special setting for all SysV services */
1f48cf56 756 s->type = SERVICE_FORKING;
02ee865a 757 s->remain_after_exit = true;
1f48cf56 758 s->restart = SERVICE_ONCE;
8a2b3c09
LP
759 s->exec_context.std_output =
760 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
761 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
2e22afe9 762 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
80876c20 763
e537352b 764 u->meta.load_state = UNIT_LOADED;
23a177ef 765 r = 0;
2c4104f0
LP
766
767finish:
768
769 if (f)
770 fclose(f);
771
772 return r;
773}
774
e537352b 775static int service_load_sysv_name(Service *s, const char *name) {
2c4104f0
LP
776 char **p;
777
778 assert(s);
779 assert(name);
780
d017c6ca
LP
781 /* For SysV services we strip the boot. or .sh
782 * prefixes/suffixes. */
783 if (startswith(name, "boot.") ||
784 endswith(name, ".sh.service"))
785 return -ENOENT;
786
4cd1fbcc 787 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
2c4104f0
LP
788 char *path;
789 int r;
790
791 if (asprintf(&path, "%s/%s", *p, name) < 0)
792 return -ENOMEM;
793
794 assert(endswith(path, ".service"));
795 path[strlen(path)-8] = 0;
796
e537352b 797 r = service_load_sysv_path(s, path);
fbe9f3a9 798
4cd1fbcc 799 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
d017c6ca 800 /* Try Debian style xxx.sh source'able init scripts */
fbe9f3a9
LP
801 strcat(path, ".sh");
802 r = service_load_sysv_path(s, path);
803 }
804
2c4104f0
LP
805 free(path);
806
4cd1fbcc 807 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
cfe243e3 808 /* Try SUSE style boot.xxx init scripts */
fbe9f3a9
LP
809
810 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
811 return -ENOMEM;
812
813 path[strlen(path)-8] = 0;
814 r = service_load_sysv_path(s, path);
815 free(path);
816 }
817
23a177ef 818 if (r < 0)
2c4104f0 819 return r;
23a177ef 820
4cd1fbcc 821 if ((s->meta.load_state != UNIT_STUB))
23a177ef 822 break;
2c4104f0
LP
823 }
824
825 return 0;
826}
827
e537352b 828static int service_load_sysv(Service *s) {
2c4104f0
LP
829 const char *t;
830 Iterator i;
831 int r;
832
5cb5a6ff
LP
833 assert(s);
834
835 /* Load service data from SysV init scripts, preferably with
836 * LSB headers ... */
837
4cd1fbcc 838 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
2c4104f0
LP
839 return 0;
840
4cd1fbcc 841 if ((t = s->meta.id))
e537352b 842 if ((r = service_load_sysv_name(s, t)) < 0)
2c4104f0
LP
843 return r;
844
4cd1fbcc
LP
845 if (s->meta.load_state == UNIT_STUB)
846 SET_FOREACH(t, s->meta.names, i) {
847 if (t == s->meta.id)
e537352b
LP
848 continue;
849
e364ad06 850 if ((r = service_load_sysv_name(s, t)) < 0)
23a177ef
LP
851 return r;
852
4cd1fbcc 853 if (s->meta.load_state != UNIT_STUB)
23a177ef
LP
854 break;
855 }
2c4104f0
LP
856
857 return 0;
5cb5a6ff 858}
07459bb6 859#endif
5cb5a6ff 860
243b1432
LP
861static int service_verify(Service *s) {
862 assert(s);
863
4cd1fbcc 864 if (s->meta.load_state != UNIT_LOADED)
243b1432
LP
865 return 0;
866
867 if (!s->exec_command[SERVICE_EXEC_START]) {
4cd1fbcc 868 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
243b1432
LP
869 return -EINVAL;
870 }
871
34e9ba66
LP
872 if (s->type != SERVICE_ONESHOT &&
873 s->exec_command[SERVICE_EXEC_START]->command_next) {
874 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
6cf6bbc2
LP
875 return -EINVAL;
876 }
877
05e343b7 878 if (s->type == SERVICE_DBUS && !s->bus_name) {
4d0e5dbd
LP
879 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
880 return -EINVAL;
881 }
882
2e22afe9 883 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
4d0e5dbd 884 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
05e343b7
LP
885 return -EINVAL;
886 }
887
243b1432
LP
888 return 0;
889}
890
a40eb732
LP
891static int service_add_default_dependencies(Service *s) {
892 int r;
893
894 assert(s);
895
896 /* Add a number of automatic dependencies useful for the
897 * majority of services. */
898
899 /* First, pull in base system */
900 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
901
902 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
903 return r;
904
905 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
906
907 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
908 return r;
909 }
910
911 /* Second, activate normal shutdown */
69dd2852 912 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
a40eb732
LP
913}
914
e537352b
LP
915static int service_load(Unit *u) {
916 int r;
917 Service *s = SERVICE(u);
918
919 assert(s);
1e2e8133 920
5cb5a6ff 921 /* Load a .service file */
e537352b 922 if ((r = unit_load_fragment(u)) < 0)
5cb5a6ff
LP
923 return r;
924
07459bb6 925#ifdef HAVE_SYSV_COMPAT
bd77d0fc 926 /* Load a classic init script as a fallback, if we couldn't find anything */
e537352b
LP
927 if (u->meta.load_state == UNIT_STUB)
928 if ((r = service_load_sysv(s)) < 0)
23a177ef 929 return r;
07459bb6 930#endif
d46de8a1 931
23a177ef 932 /* Still nothing found? Then let's give up */
e537352b 933 if (u->meta.load_state == UNIT_STUB)
23a177ef 934 return -ENOENT;
034c6ed7 935
23a177ef
LP
936 /* We were able to load something, then let's add in the
937 * dropin directories. */
938 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
8e274523 939 return r;
23a177ef
LP
940
941 /* This is a new unit? Then let's add in some extras */
e537352b 942 if (u->meta.load_state == UNIT_LOADED) {
23a177ef
LP
943 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
944 return r;
945
946 if ((r = unit_add_default_cgroup(u)) < 0)
947 return r;
948
07459bb6 949#ifdef HAVE_SYSV_COMPAT
56d748b4 950 if ((r = sysv_fix_order(s)) < 0)
23a177ef 951 return r;
07459bb6 952#endif
05e343b7 953
ee0dd802 954 if (s->bus_name)
05e343b7 955 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
a40eb732 956 return r;
c952c6ec
LP
957
958 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
959 s->notify_access = NOTIFY_MAIN;
a40eb732
LP
960
961 if (s->type == SERVICE_DBUS || s->bus_name)
962 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
963 return r;
964
965 if (s->meta.default_dependencies)
966 if ((r = service_add_default_dependencies(s)) < 0)
967 return r;
8e274523
LP
968 }
969
243b1432 970 return service_verify(s);
034c6ed7
LP
971}
972
87f0e418 973static void service_dump(Unit *u, FILE *f, const char *prefix) {
5cb5a6ff 974
5cb5a6ff 975 ServiceExecCommand c;
87f0e418 976 Service *s = SERVICE(u);
47be870b
LP
977 const char *prefix2;
978 char *p2;
5cb5a6ff
LP
979
980 assert(s);
981
47be870b
LP
982 p2 = strappend(prefix, "\t");
983 prefix2 = p2 ? p2 : prefix;
44d8db9e 984
5cb5a6ff 985 fprintf(f,
81a2b7ce
LP
986 "%sService State: %s\n"
987 "%sPermissionsStartOnly: %s\n"
8e274523 988 "%sRootDirectoryStartOnly: %s\n"
02ee865a 989 "%sRemainAfterExit: %s\n"
c952c6ec 990 "%sType: %s\n"
2cf3143a 991 "%sRestart: %s\n"
c952c6ec 992 "%sNotifyAccess: %s\n",
81a2b7ce
LP
993 prefix, service_state_to_string(s->state),
994 prefix, yes_no(s->permissions_start_only),
8e274523 995 prefix, yes_no(s->root_directory_start_only),
02ee865a 996 prefix, yes_no(s->remain_after_exit),
c952c6ec 997 prefix, service_type_to_string(s->type),
2cf3143a 998 prefix, service_restart_to_string(s->restart),
c952c6ec 999 prefix, notify_access_to_string(s->notify_access));
5cb5a6ff 1000
70123e68
LP
1001 if (s->control_pid > 0)
1002 fprintf(f,
bb00e604
LP
1003 "%sControl PID: %lu\n",
1004 prefix, (unsigned long) s->control_pid);
70123e68
LP
1005
1006 if (s->main_pid > 0)
1007 fprintf(f,
bb00e604
LP
1008 "%sMain PID: %lu\n",
1009 prefix, (unsigned long) s->main_pid);
70123e68 1010
034c6ed7
LP
1011 if (s->pid_file)
1012 fprintf(f,
1013 "%sPIDFile: %s\n",
1014 prefix, s->pid_file);
1015
05e343b7
LP
1016 if (s->bus_name)
1017 fprintf(f,
1018 "%sBusName: %s\n"
1019 "%sBus Name Good: %s\n",
1020 prefix, s->bus_name,
1021 prefix, yes_no(s->bus_name_good));
1022
5cb5a6ff
LP
1023 exec_context_dump(&s->exec_context, f, prefix);
1024
e537352b 1025 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
5cb5a6ff 1026
44d8db9e
LP
1027 if (!s->exec_command[c])
1028 continue;
1029
40d50879 1030 fprintf(f, "%s-> %s:\n",
94f04347 1031 prefix, service_exec_command_to_string(c));
44d8db9e
LP
1032
1033 exec_command_dump_list(s->exec_command[c], f, prefix2);
5cb5a6ff 1034 }
44d8db9e 1035
07459bb6 1036#ifdef HAVE_SYSV_COMPAT
2c4104f0
LP
1037 if (s->sysv_path)
1038 fprintf(f,
23a177ef
LP
1039 "%sSysV Init Script Path: %s\n"
1040 "%sSysV Init Script has LSB Header: %s\n",
1041 prefix, s->sysv_path,
1042 prefix, yes_no(s->sysv_has_lsb));
2c4104f0
LP
1043
1044 if (s->sysv_start_priority >= 0)
1045 fprintf(f,
5de6b302
LP
1046 "%sSysVStartPriority: %i\n"
1047 "%sSysVEnabled: %s\n",
1048 prefix, s->sysv_start_priority,
1049 prefix, yes_no(s->sysv_enabled));
2c4104f0 1050
8309400a
LP
1051 if (s->sysv_runlevels)
1052 fprintf(f, "%sSysVRunLevels: %s\n",
1053 prefix, s->sysv_runlevels);
07459bb6 1054#endif
23a177ef 1055
8c47c732
LP
1056 if (s->status_text)
1057 fprintf(f, "%sStatus Text: %s\n",
1058 prefix, s->status_text);
1059
47be870b 1060 free(p2);
5cb5a6ff
LP
1061}
1062
034c6ed7
LP
1063static int service_load_pid_file(Service *s) {
1064 char *k;
034c6ed7 1065 int r;
5925dd3c 1066 pid_t pid;
034c6ed7
LP
1067
1068 assert(s);
1069
1070 if (s->main_pid_known)
1071 return 0;
1072
5e94833f
LP
1073 assert(s->main_pid <= 0);
1074
034c6ed7
LP
1075 if (!s->pid_file)
1076 return -ENOENT;
1077
1078 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1079 return r;
1080
5925dd3c
LP
1081 r = parse_pid(k, &pid);
1082 free(k);
034c6ed7 1083
5925dd3c
LP
1084 if (r < 0)
1085 return r;
406eaf93 1086
5925dd3c
LP
1087 if (kill(pid, 0) < 0 && errno != EPERM) {
1088 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1089 (unsigned long) pid, s->pid_file);
b8c597d5
LP
1090 return -ESRCH;
1091 }
1092
5925dd3c 1093 if ((r = service_set_main_pid(s, pid)) < 0)
16f6025e
LP
1094 return r;
1095
5925dd3c
LP
1096 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1097 /* FIXME: we need to do something here */
1098 return r;
034c6ed7
LP
1099
1100 return 0;
1101}
1102
3e33402a
LP
1103static int service_get_sockets(Service *s, Set **_set) {
1104 Set *set;
ceee3d82
LP
1105 Iterator i;
1106 char *t;
3e33402a 1107 int r;
ceee3d82
LP
1108
1109 assert(s);
3e33402a
LP
1110 assert(_set);
1111
6cf6bbc2
LP
1112 if (s->socket_fd >= 0)
1113 return 0;
1114
3e33402a
LP
1115 /* Collects all Socket objects that belong to this
1116 * service. Note that a service might have multiple sockets
1117 * via multiple names. */
1118
1119 if (!(set = set_new(NULL, NULL)))
1120 return -ENOMEM;
ceee3d82 1121
4cd1fbcc 1122 SET_FOREACH(t, s->meta.names, i) {
ceee3d82
LP
1123 char *k;
1124 Unit *p;
1125
1126 /* Look for all socket objects that go by any of our
1127 * units and collect their fds */
1128
3e33402a
LP
1129 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1130 r = -ENOMEM;
1131 goto fail;
1132 }
ceee3d82 1133
4cd1fbcc 1134 p = manager_get_unit(s->meta.manager, k);
ceee3d82
LP
1135 free(k);
1136
8d567588
LP
1137 if (!p)
1138 continue;
ceee3d82 1139
3e33402a
LP
1140 if ((r = set_put(set, p)) < 0)
1141 goto fail;
ceee3d82
LP
1142 }
1143
3e33402a
LP
1144 *_set = set;
1145 return 0;
1146
1147fail:
1148 set_free(set);
1149 return r;
1150}
1151
e537352b 1152static int service_notify_sockets_dead(Service *s) {
3e33402a
LP
1153 Iterator i;
1154 Set *set;
47be870b 1155 Socket *sock;
3e33402a
LP
1156 int r;
1157
1158 assert(s);
1159
6cf6bbc2
LP
1160 if (s->socket_fd >= 0)
1161 return 0;
1162
3e33402a 1163 /* Notifies all our sockets when we die */
3e33402a
LP
1164 if ((r = service_get_sockets(s, &set)) < 0)
1165 return r;
1166
47be870b
LP
1167 SET_FOREACH(sock, set, i)
1168 socket_notify_service_dead(sock);
3e33402a
LP
1169
1170 set_free(set);
1171
ceee3d82
LP
1172 return 0;
1173}
1174
034c6ed7
LP
1175static void service_set_state(Service *s, ServiceState state) {
1176 ServiceState old_state;
5cb5a6ff
LP
1177 assert(s);
1178
034c6ed7 1179 old_state = s->state;
5cb5a6ff 1180 s->state = state;
034c6ed7
LP
1181
1182 if (state != SERVICE_START_PRE &&
1183 state != SERVICE_START &&
1184 state != SERVICE_START_POST &&
1185 state != SERVICE_RELOAD &&
1186 state != SERVICE_STOP &&
1187 state != SERVICE_STOP_SIGTERM &&
1188 state != SERVICE_STOP_SIGKILL &&
1189 state != SERVICE_STOP_POST &&
1190 state != SERVICE_FINAL_SIGTERM &&
1191 state != SERVICE_FINAL_SIGKILL &&
1192 state != SERVICE_AUTO_RESTART)
acbb0225 1193 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7 1194
7d55e835
LP
1195 if (state != SERVICE_START &&
1196 state != SERVICE_START_POST &&
034c6ed7
LP
1197 state != SERVICE_RUNNING &&
1198 state != SERVICE_RELOAD &&
1199 state != SERVICE_STOP &&
1200 state != SERVICE_STOP_SIGTERM &&
1201 state != SERVICE_STOP_SIGKILL)
5e94833f 1202 service_unwatch_main_pid(s);
034c6ed7
LP
1203
1204 if (state != SERVICE_START_PRE &&
1205 state != SERVICE_START &&
1206 state != SERVICE_START_POST &&
1207 state != SERVICE_RELOAD &&
1208 state != SERVICE_STOP &&
1209 state != SERVICE_STOP_SIGTERM &&
1210 state != SERVICE_STOP_SIGKILL &&
1211 state != SERVICE_STOP_POST &&
1212 state != SERVICE_FINAL_SIGTERM &&
e537352b 1213 state != SERVICE_FINAL_SIGKILL) {
5e94833f 1214 service_unwatch_control_pid(s);
034c6ed7 1215 s->control_command = NULL;
a16e1123 1216 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
e537352b 1217 }
034c6ed7 1218
ceee3d82
LP
1219 if (state == SERVICE_DEAD ||
1220 state == SERVICE_STOP ||
1221 state == SERVICE_STOP_SIGTERM ||
1222 state == SERVICE_STOP_SIGKILL ||
1223 state == SERVICE_STOP_POST ||
1224 state == SERVICE_FINAL_SIGTERM ||
1225 state == SERVICE_FINAL_SIGKILL ||
fdf20a31 1226 state == SERVICE_FAILED ||
ceee3d82 1227 state == SERVICE_AUTO_RESTART)
e537352b 1228 service_notify_sockets_dead(s);
ceee3d82 1229
4f2d528d
LP
1230 if (state != SERVICE_START_PRE &&
1231 state != SERVICE_START &&
6cf6bbc2
LP
1232 state != SERVICE_START_POST &&
1233 state != SERVICE_RUNNING &&
1234 state != SERVICE_RELOAD &&
1235 state != SERVICE_STOP &&
1236 state != SERVICE_STOP_SIGTERM &&
1237 state != SERVICE_STOP_SIGKILL &&
1238 state != SERVICE_STOP_POST &&
1239 state != SERVICE_FINAL_SIGTERM &&
1240 state != SERVICE_FINAL_SIGKILL &&
4cd1fbcc 1241 !(state == SERVICE_DEAD && s->meta.job)) {
4f2d528d 1242 service_close_socket_fd(s);
6cf6bbc2
LP
1243 service_connection_unref(s);
1244 }
4f2d528d 1245
f6023656
LP
1246 /* For the inactive states unit_notify() will trim the cgroup,
1247 * but for exit we have to do that ourselves... */
1248 if (state == SERVICE_EXITED)
1249 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1250
e537352b 1251 if (old_state != state)
4cd1fbcc 1252 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
acbb0225
LP
1253
1254 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
034c6ed7
LP
1255}
1256
a16e1123
LP
1257static int service_coldplug(Unit *u) {
1258 Service *s = SERVICE(u);
1259 int r;
1260
1261 assert(s);
1262 assert(s->state == SERVICE_DEAD);
1263
1264 if (s->deserialized_state != s->state) {
1265
1266 if (s->deserialized_state == SERVICE_START_PRE ||
1267 s->deserialized_state == SERVICE_START ||
1268 s->deserialized_state == SERVICE_START_POST ||
1269 s->deserialized_state == SERVICE_RELOAD ||
1270 s->deserialized_state == SERVICE_STOP ||
1271 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1272 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1273 s->deserialized_state == SERVICE_STOP_POST ||
1274 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1275 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
e558336f
LP
1276 s->deserialized_state == SERVICE_AUTO_RESTART) {
1277
1278 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1279 usec_t k;
1280
1281 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1282
1283 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1284 return r;
1285 }
1286 }
a16e1123
LP
1287
1288 if ((s->deserialized_state == SERVICE_START &&
1289 (s->type == SERVICE_FORKING ||
8c47c732 1290 s->type == SERVICE_DBUS ||
34e9ba66 1291 s->type == SERVICE_ONESHOT ||
8c47c732 1292 s->type == SERVICE_NOTIFY)) ||
a16e1123
LP
1293 s->deserialized_state == SERVICE_START_POST ||
1294 s->deserialized_state == SERVICE_RUNNING ||
1295 s->deserialized_state == SERVICE_RELOAD ||
1296 s->deserialized_state == SERVICE_STOP ||
1297 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1298 s->deserialized_state == SERVICE_STOP_SIGKILL)
1299 if (s->main_pid > 0)
1300 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1301 return r;
1302
1303 if (s->deserialized_state == SERVICE_START_PRE ||
1304 s->deserialized_state == SERVICE_START ||
1305 s->deserialized_state == SERVICE_START_POST ||
1306 s->deserialized_state == SERVICE_RELOAD ||
1307 s->deserialized_state == SERVICE_STOP ||
1308 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1309 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1310 s->deserialized_state == SERVICE_STOP_POST ||
1311 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1312 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1313 if (s->control_pid > 0)
1314 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1315 return r;
1316
1317 service_set_state(s, s->deserialized_state);
1318 }
1319
1320 return 0;
1321}
1322
44d8db9e
LP
1323static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1324 Iterator i;
1325 int r;
1326 int *rfds = NULL;
1327 unsigned rn_fds = 0;
3e33402a 1328 Set *set;
47be870b 1329 Socket *sock;
44d8db9e
LP
1330
1331 assert(s);
1332 assert(fds);
1333 assert(n_fds);
1334
6cf6bbc2
LP
1335 if (s->socket_fd >= 0)
1336 return 0;
1337
3e33402a
LP
1338 if ((r = service_get_sockets(s, &set)) < 0)
1339 return r;
1340
47be870b 1341 SET_FOREACH(sock, set, i) {
44d8db9e
LP
1342 int *cfds;
1343 unsigned cn_fds;
1344
47be870b 1345 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
44d8db9e
LP
1346 goto fail;
1347
1348 if (!cfds)
1349 continue;
1350
1351 if (!rfds) {
1352 rfds = cfds;
1353 rn_fds = cn_fds;
1354 } else {
1355 int *t;
1356
1357 if (!(t = new(int, rn_fds+cn_fds))) {
1358 free(cfds);
1359 r = -ENOMEM;
1360 goto fail;
1361 }
1362
1363 memcpy(t, rfds, rn_fds);
1364 memcpy(t+rn_fds, cfds, cn_fds);
1365 free(rfds);
1366 free(cfds);
1367
1368 rfds = t;
1369 rn_fds = rn_fds+cn_fds;
1370 }
1371 }
1372
1373 *fds = rfds;
1374 *n_fds = rn_fds;
3e33402a
LP
1375
1376 set_free(set);
1377
44d8db9e
LP
1378 return 0;
1379
1380fail:
3e33402a 1381 set_free(set);
44d8db9e 1382 free(rfds);
3e33402a 1383
44d8db9e
LP
1384 return r;
1385}
1386
81a2b7ce
LP
1387static int service_spawn(
1388 Service *s,
1389 ExecCommand *c,
1390 bool timeout,
1391 bool pass_fds,
1392 bool apply_permissions,
1393 bool apply_chroot,
1e3ad081 1394 bool apply_tty_stdin,
c952c6ec 1395 bool set_notify_socket,
81a2b7ce
LP
1396 pid_t *_pid) {
1397
034c6ed7
LP
1398 pid_t pid;
1399 int r;
6cf6bbc2 1400 int *fds = NULL, *fdsbuf = NULL;
2105e76a
LP
1401 unsigned n_fds = 0, n_env = 0;
1402 char **argv = NULL, **final_env = NULL, **our_env = NULL;
034c6ed7
LP
1403
1404 assert(s);
1405 assert(c);
1406 assert(_pid);
1407
6cf6bbc2
LP
1408 if (pass_fds ||
1409 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1410 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1411 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1412
4f2d528d
LP
1413 if (s->socket_fd >= 0) {
1414 fds = &s->socket_fd;
1415 n_fds = 1;
6cf6bbc2
LP
1416 } else {
1417 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1418 goto fail;
1419
1420 fds = fdsbuf;
1421 }
4f2d528d 1422 }
44d8db9e 1423
e558336f 1424 if (timeout && s->timeout_usec) {
acbb0225 1425 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
034c6ed7
LP
1426 goto fail;
1427 } else
acbb0225 1428 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7 1429
9e2f7c11
LP
1430 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1431 r = -ENOMEM;
1432 goto fail;
1433 }
1434
2105e76a
LP
1435 if (!(our_env = new0(char*, 3))) {
1436 r = -ENOMEM;
1437 goto fail;
1438 }
c952c6ec 1439
2105e76a
LP
1440 if (set_notify_socket)
1441 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
c952c6ec
LP
1442 r = -ENOMEM;
1443 goto fail;
1444 }
1445
2105e76a
LP
1446 if (s->main_pid > 0)
1447 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
c952c6ec
LP
1448 r = -ENOMEM;
1449 goto fail;
1450 }
2105e76a
LP
1451
1452 if (!(final_env = strv_env_merge(2,
1453 s->meta.manager->environment,
1454 our_env,
1455 NULL))) {
1456 r = -ENOMEM;
1457 goto fail;
1458 }
c952c6ec 1459
9e2f7c11
LP
1460 r = exec_spawn(c,
1461 argv,
1462 &s->exec_context,
1463 fds, n_fds,
2105e76a 1464 final_env,
9e2f7c11
LP
1465 apply_permissions,
1466 apply_chroot,
1e3ad081 1467 apply_tty_stdin,
4cd1fbcc
LP
1468 s->meta.manager->confirm_spawn,
1469 s->meta.cgroup_bondings,
9e2f7c11
LP
1470 &pid);
1471
9e2f7c11 1472 if (r < 0)
034c6ed7
LP
1473 goto fail;
1474
4f2d528d 1475
87f0e418 1476 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
034c6ed7
LP
1477 /* FIXME: we need to do something here */
1478 goto fail;
1479
2105e76a
LP
1480 free(fdsbuf);
1481 strv_free(argv);
1482 strv_free(our_env);
1483 strv_free(final_env);
1484
034c6ed7
LP
1485 *_pid = pid;
1486
5cb5a6ff 1487 return 0;
034c6ed7
LP
1488
1489fail:
2105e76a 1490 free(fdsbuf);
c952c6ec 1491 strv_free(argv);
2105e76a
LP
1492 strv_free(our_env);
1493 strv_free(final_env);
c952c6ec 1494
034c6ed7 1495 if (timeout)
acbb0225 1496 unit_unwatch_timer(UNIT(s), &s->timer_watch);
034c6ed7
LP
1497
1498 return r;
1499}
1500
80876c20
LP
1501static int main_pid_good(Service *s) {
1502 assert(s);
1503
1504 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1505 * don't know */
1506
1507 /* If we know the pid file, then lets just check if it is
1508 * still valid */
1509 if (s->main_pid_known)
1510 return s->main_pid > 0;
1511
1512 /* We don't know the pid */
1513 return -EAGAIN;
1514}
1515
1516static int control_pid_good(Service *s) {
1517 assert(s);
1518
1519 return s->control_pid > 0;
1520}
1521
1522static int cgroup_good(Service *s) {
1523 int r;
1524
1525 assert(s);
1526
4cd1fbcc 1527 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
80876c20
LP
1528 return r;
1529
1530 return !r;
1531}
1532
034c6ed7
LP
1533static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1534 int r;
1535 assert(s);
1536
1537 if (!success)
1538 s->failure = true;
1539
1540 if (allow_restart &&
47342320 1541 !s->forbid_restart &&
034c6ed7
LP
1542 (s->restart == SERVICE_RESTART_ALWAYS ||
1543 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1544
acbb0225 1545 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
034c6ed7
LP
1546 goto fail;
1547
1548 service_set_state(s, SERVICE_AUTO_RESTART);
1549 } else
fdf20a31 1550 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
034c6ed7 1551
47342320
LP
1552 s->forbid_restart = false;
1553
034c6ed7
LP
1554 return;
1555
1556fail:
4cd1fbcc 1557 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
034c6ed7
LP
1558 service_enter_dead(s, false, false);
1559}
1560
1561static void service_enter_signal(Service *s, ServiceState state, bool success);
1562
1563static void service_enter_stop_post(Service *s, bool success) {
1564 int r;
1565 assert(s);
1566
1567 if (!success)
1568 s->failure = true;
1569
5e94833f
LP
1570 service_unwatch_control_pid(s);
1571
a16e1123 1572 s->control_command_id = SERVICE_EXEC_STOP_POST;
80876c20 1573 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
81a2b7ce
LP
1574 if ((r = service_spawn(s,
1575 s->control_command,
1576 true,
1577 false,
1578 !s->permissions_start_only,
1579 !s->root_directory_start_only,
1e3ad081 1580 true,
c952c6ec 1581 false,
81a2b7ce 1582 &s->control_pid)) < 0)
034c6ed7
LP
1583 goto fail;
1584
d6ea93e3 1585
80876c20
LP
1586 service_set_state(s, SERVICE_STOP_POST);
1587 } else
1588 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
034c6ed7
LP
1589
1590 return;
1591
1592fail:
4cd1fbcc 1593 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
034c6ed7
LP
1594 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1595}
1596
1597static void service_enter_signal(Service *s, ServiceState state, bool success) {
1598 int r;
ca949c9d
LP
1599 Set *pid_set = NULL;
1600 bool wait_for_exit = false;
034c6ed7
LP
1601
1602 assert(s);
1603
1604 if (!success)
1605 s->failure = true;
1606
2e22afe9
LP
1607 if (s->exec_context.kill_mode != KILL_NONE) {
1608 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
034c6ed7 1609
ca949c9d
LP
1610 if (s->main_pid > 0) {
1611 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1612 -s->main_pid :
1613 s->main_pid, sig) < 0 && errno != ESRCH)
034c6ed7 1614
ca949c9d
LP
1615 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1616 else
1617 wait_for_exit = true;
034c6ed7
LP
1618 }
1619
ca949c9d
LP
1620 if (s->control_pid > 0) {
1621 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1622 -s->control_pid :
1623 s->control_pid, sig) < 0 && errno != ESRCH)
80876c20 1624
ca949c9d
LP
1625 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1626 else
1627 wait_for_exit = true;
1628 }
50159e6a 1629
ca949c9d 1630 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
50159e6a 1631
ca949c9d
LP
1632 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1633 r = -ENOMEM;
50159e6a 1634 goto fail;
ca949c9d
LP
1635 }
1636
1637 /* Exclude the main/control pids from being killed via the cgroup */
1638 if (s->main_pid > 0)
1639 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1640 goto fail;
1641
1642 if (s->control_pid > 0)
1643 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1644 goto fail;
1645
1646 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1647 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1648 log_warning("Failed to kill control group: %s", strerror(-r));
f5a50114 1649 } else if (r > 0)
ca949c9d
LP
1650 wait_for_exit = true;
1651
1652 set_free(pid_set);
50159e6a 1653 }
d6ea93e3 1654 }
034c6ed7 1655
ca949c9d 1656 if (wait_for_exit) {
e558336f
LP
1657 if (s->timeout_usec > 0)
1658 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1659 goto fail;
d6ea93e3 1660
80876c20
LP
1661 service_set_state(s, state);
1662 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1663 service_enter_stop_post(s, true);
1664 else
034c6ed7
LP
1665 service_enter_dead(s, true, true);
1666
1667 return;
1668
1669fail:
4cd1fbcc 1670 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
034c6ed7 1671
80876c20 1672 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
034c6ed7
LP
1673 service_enter_stop_post(s, false);
1674 else
1675 service_enter_dead(s, false, true);
ca949c9d
LP
1676
1677 if (pid_set)
1678 set_free(pid_set);
034c6ed7
LP
1679}
1680
1681static void service_enter_stop(Service *s, bool success) {
1682 int r;
5925dd3c 1683
034c6ed7
LP
1684 assert(s);
1685
1686 if (!success)
1687 s->failure = true;
1688
5e94833f
LP
1689 service_unwatch_control_pid(s);
1690
a16e1123 1691 s->control_command_id = SERVICE_EXEC_STOP;
80876c20 1692 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
81a2b7ce
LP
1693 if ((r = service_spawn(s,
1694 s->control_command,
1695 true,
1696 false,
1697 !s->permissions_start_only,
1698 !s->root_directory_start_only,
c952c6ec 1699 false,
1e3ad081 1700 false,
e55224ca 1701 &s->control_pid)) < 0)
034c6ed7
LP
1702 goto fail;
1703
80876c20
LP
1704 service_set_state(s, SERVICE_STOP);
1705 } else
034c6ed7
LP
1706 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1707
1708 return;
1709
1710fail:
4cd1fbcc 1711 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
034c6ed7
LP
1712 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1713}
1714
80876c20 1715static void service_enter_running(Service *s, bool success) {
4eab639f 1716 int main_pid_ok, cgroup_ok;
80876c20
LP
1717 assert(s);
1718
1719 if (!success)
1720 s->failure = true;
1721
4eab639f
LP
1722 main_pid_ok = main_pid_good(s);
1723 cgroup_ok = cgroup_good(s);
1724
1725 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
05e343b7 1726 (s->bus_name_good || s->type != SERVICE_DBUS))
80876c20 1727 service_set_state(s, SERVICE_RUNNING);
02ee865a 1728 else if (s->remain_after_exit)
80876c20
LP
1729 service_set_state(s, SERVICE_EXITED);
1730 else
1731 service_enter_stop(s, true);
1732}
1733
034c6ed7
LP
1734static void service_enter_start_post(Service *s) {
1735 int r;
1736 assert(s);
1737
5e94833f
LP
1738 service_unwatch_control_pid(s);
1739
a16e1123 1740 s->control_command_id = SERVICE_EXEC_START_POST;
80876c20 1741 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
81a2b7ce
LP
1742 if ((r = service_spawn(s,
1743 s->control_command,
1744 true,
1745 false,
1746 !s->permissions_start_only,
1747 !s->root_directory_start_only,
c952c6ec 1748 false,
1e3ad081 1749 false,
e55224ca 1750 &s->control_pid)) < 0)
034c6ed7
LP
1751 goto fail;
1752
80876c20
LP
1753 service_set_state(s, SERVICE_START_POST);
1754 } else
1755 service_enter_running(s, true);
034c6ed7
LP
1756
1757 return;
1758
1759fail:
4cd1fbcc 1760 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
034c6ed7
LP
1761 service_enter_stop(s, false);
1762}
1763
1764static void service_enter_start(Service *s) {
1765 pid_t pid;
1766 int r;
1767
1768 assert(s);
1769
1770 assert(s->exec_command[SERVICE_EXEC_START]);
34e9ba66 1771 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
034c6ed7 1772
80876c20
LP
1773 if (s->type == SERVICE_FORKING)
1774 service_unwatch_control_pid(s);
1775 else
1776 service_unwatch_main_pid(s);
1777
34e9ba66
LP
1778 s->control_command_id = SERVICE_EXEC_START;
1779 s->control_command = s->exec_command[SERVICE_EXEC_START];
1780
81a2b7ce 1781 if ((r = service_spawn(s,
34e9ba66 1782 s->control_command,
8c47c732 1783 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
81a2b7ce
LP
1784 true,
1785 true,
1786 true,
1e3ad081 1787 true,
c952c6ec 1788 s->notify_access != NOTIFY_NONE,
81a2b7ce 1789 &pid)) < 0)
034c6ed7
LP
1790 goto fail;
1791
1792 if (s->type == SERVICE_SIMPLE) {
1793 /* For simple services we immediately start
1794 * the START_POST binaries. */
1795
5925dd3c 1796 service_set_main_pid(s, pid);
034c6ed7
LP
1797 service_enter_start_post(s);
1798
1799 } else if (s->type == SERVICE_FORKING) {
1800
1801 /* For forking services we wait until the start
1802 * process exited. */
1803
e55224ca 1804 s->control_pid = pid;
80876c20
LP
1805 service_set_state(s, SERVICE_START);
1806
34e9ba66 1807 } else if (s->type == SERVICE_ONESHOT ||
8c47c732
LP
1808 s->type == SERVICE_DBUS ||
1809 s->type == SERVICE_NOTIFY) {
7d55e835 1810
34e9ba66 1811 /* For oneshot services we wait until the start
7d55e835
LP
1812 * process exited, too, but it is our main process. */
1813
05e343b7 1814 /* For D-Bus services we know the main pid right away,
8c47c732
LP
1815 * but wait for the bus name to appear on the
1816 * bus. Notify services are similar. */
05e343b7 1817
5925dd3c 1818 service_set_main_pid(s, pid);
80876c20 1819 service_set_state(s, SERVICE_START);
034c6ed7
LP
1820 } else
1821 assert_not_reached("Unknown service type");
1822
1823 return;
1824
1825fail:
4cd1fbcc 1826 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
80876c20 1827 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
1828}
1829
1830static void service_enter_start_pre(Service *s) {
1831 int r;
1832
1833 assert(s);
1834
5e94833f
LP
1835 service_unwatch_control_pid(s);
1836
a16e1123 1837 s->control_command_id = SERVICE_EXEC_START_PRE;
80876c20 1838 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
81a2b7ce
LP
1839 if ((r = service_spawn(s,
1840 s->control_command,
1841 true,
1842 false,
1843 !s->permissions_start_only,
1844 !s->root_directory_start_only,
1e3ad081 1845 true,
c952c6ec 1846 false,
e55224ca 1847 &s->control_pid)) < 0)
034c6ed7
LP
1848 goto fail;
1849
80876c20
LP
1850 service_set_state(s, SERVICE_START_PRE);
1851 } else
034c6ed7
LP
1852 service_enter_start(s);
1853
1854 return;
1855
1856fail:
4cd1fbcc 1857 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
034c6ed7
LP
1858 service_enter_dead(s, false, true);
1859}
1860
1861static void service_enter_restart(Service *s) {
1862 int r;
398ef8ba
LP
1863 DBusError error;
1864
034c6ed7 1865 assert(s);
398ef8ba 1866 dbus_error_init(&error);
034c6ed7 1867
9ea9a0c8
LP
1868 service_enter_dead(s, true, false);
1869
7b2603e6 1870 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
034c6ed7
LP
1871 goto fail;
1872
4cd1fbcc 1873 log_debug("%s scheduled restart job.", s->meta.id);
034c6ed7
LP
1874 return;
1875
1876fail:
398ef8ba 1877 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
034c6ed7 1878 service_enter_dead(s, false, false);
398ef8ba
LP
1879
1880 dbus_error_free(&error);
034c6ed7
LP
1881}
1882
1883static void service_enter_reload(Service *s) {
1884 int r;
1885
1886 assert(s);
1887
5e94833f
LP
1888 service_unwatch_control_pid(s);
1889
a16e1123 1890 s->control_command_id = SERVICE_EXEC_RELOAD;
80876c20 1891 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
81a2b7ce
LP
1892 if ((r = service_spawn(s,
1893 s->control_command,
1894 true,
1895 false,
1896 !s->permissions_start_only,
1897 !s->root_directory_start_only,
c952c6ec 1898 false,
1e3ad081 1899 false,
e55224ca 1900 &s->control_pid)) < 0)
034c6ed7
LP
1901 goto fail;
1902
80876c20
LP
1903 service_set_state(s, SERVICE_RELOAD);
1904 } else
1905 service_enter_running(s, true);
034c6ed7
LP
1906
1907 return;
1908
1909fail:
4cd1fbcc 1910 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
034c6ed7
LP
1911 service_enter_stop(s, false);
1912}
1913
34e9ba66 1914static void service_run_next_control(Service *s, bool success) {
034c6ed7
LP
1915 int r;
1916
1917 assert(s);
1918 assert(s->control_command);
1919 assert(s->control_command->command_next);
1920
1921 if (!success)
1922 s->failure = true;
1923
34e9ba66 1924 assert(s->control_command_id != SERVICE_EXEC_START);
034c6ed7 1925
34e9ba66 1926 s->control_command = s->control_command->command_next;
5e94833f
LP
1927 service_unwatch_control_pid(s);
1928
81a2b7ce
LP
1929 if ((r = service_spawn(s,
1930 s->control_command,
1931 true,
1932 false,
1933 !s->permissions_start_only,
1934 !s->root_directory_start_only,
5830833f
LP
1935 s->control_command_id == SERVICE_EXEC_START_PRE ||
1936 s->control_command_id == SERVICE_EXEC_STOP_POST,
1e3ad081 1937 false,
e55224ca 1938 &s->control_pid)) < 0)
034c6ed7
LP
1939 goto fail;
1940
1941 return;
1942
1943fail:
34e9ba66 1944 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
034c6ed7 1945
80876c20
LP
1946 if (s->state == SERVICE_START_PRE)
1947 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1948 else if (s->state == SERVICE_STOP)
1949 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
034c6ed7
LP
1950 else if (s->state == SERVICE_STOP_POST)
1951 service_enter_dead(s, false, true);
1952 else
1953 service_enter_stop(s, false);
5cb5a6ff
LP
1954}
1955
34e9ba66
LP
1956static void service_run_next_main(Service *s, bool success) {
1957 pid_t pid;
1958 int r;
1959
1960 assert(s);
1961 assert(s->control_command);
1962 assert(s->control_command->command_next);
1963
1964 if (!success)
1965 s->failure = true;
1966
1967 assert(s->control_command_id == SERVICE_EXEC_START);
1968 assert(s->type == SERVICE_ONESHOT);
1969
1970 s->control_command = s->control_command->command_next;
1971 service_unwatch_main_pid(s);
1972
1973 if ((r = service_spawn(s,
1974 s->control_command,
1975 false,
1976 true,
1977 true,
1978 true,
1979 true,
1980 s->notify_access != NOTIFY_NONE,
1981 &pid)) < 0)
1982 goto fail;
1983
1984 service_set_main_pid(s, pid);
1985
1986 return;
1987
1988fail:
1989 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
1990 service_enter_stop(s, false);
1991}
1992
87f0e418
LP
1993static int service_start(Unit *u) {
1994 Service *s = SERVICE(u);
5cb5a6ff
LP
1995
1996 assert(s);
1997
034c6ed7
LP
1998 /* We cannot fulfill this request right now, try again later
1999 * please! */
2000 if (s->state == SERVICE_STOP ||
2001 s->state == SERVICE_STOP_SIGTERM ||
2002 s->state == SERVICE_STOP_SIGKILL ||
2003 s->state == SERVICE_STOP_POST ||
2004 s->state == SERVICE_FINAL_SIGTERM ||
2005 s->state == SERVICE_FINAL_SIGKILL)
5cb5a6ff
LP
2006 return -EAGAIN;
2007
034c6ed7
LP
2008 /* Already on it! */
2009 if (s->state == SERVICE_START_PRE ||
2010 s->state == SERVICE_START ||
2011 s->state == SERVICE_START_POST)
2012 return 0;
2013
fdf20a31 2014 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
5cb5a6ff 2015
1e2e8133
LP
2016 /* Make sure we don't enter a busy loop of some kind. */
2017 if (!ratelimit_test(&s->ratelimit)) {
9e2f7c11 2018 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
d5159713 2019 return -ECANCELED;
1e2e8133
LP
2020 }
2021
cac6f7c8
LP
2022 if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
2023 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
2024 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
2025 s->socket_fd < 0) {
2026 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
2027 return -EINVAL;
2028 }
2029
034c6ed7
LP
2030 s->failure = false;
2031 s->main_pid_known = false;
47342320 2032 s->forbid_restart = false;
034c6ed7
LP
2033
2034 service_enter_start_pre(s);
2035 return 0;
5cb5a6ff
LP
2036}
2037
87f0e418
LP
2038static int service_stop(Unit *u) {
2039 Service *s = SERVICE(u);
5cb5a6ff
LP
2040
2041 assert(s);
2042
3f6c78dc
LP
2043 /* This is a user request, so don't do restarts on this
2044 * shutdown. */
47342320 2045 s->forbid_restart = true;
034c6ed7 2046
e537352b
LP
2047 /* Already on it */
2048 if (s->state == SERVICE_STOP ||
2049 s->state == SERVICE_STOP_SIGTERM ||
2050 s->state == SERVICE_STOP_SIGKILL ||
2051 s->state == SERVICE_STOP_POST ||
2052 s->state == SERVICE_FINAL_SIGTERM ||
2053 s->state == SERVICE_FINAL_SIGKILL)
2054 return 0;
2055
3f6c78dc 2056 /* Don't allow a restart */
034c6ed7
LP
2057 if (s->state == SERVICE_AUTO_RESTART) {
2058 service_set_state(s, SERVICE_DEAD);
2059 return 0;
2060 }
2061
3f6c78dc
LP
2062 /* If there's already something running we go directly into
2063 * kill mode. */
2064 if (s->state == SERVICE_START_PRE ||
2065 s->state == SERVICE_START ||
2066 s->state == SERVICE_START_POST ||
2067 s->state == SERVICE_RELOAD) {
2068 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2069 return 0;
2070 }
5cb5a6ff 2071
3f6c78dc
LP
2072 assert(s->state == SERVICE_RUNNING ||
2073 s->state == SERVICE_EXITED);
3a762661 2074
034c6ed7 2075 service_enter_stop(s, true);
5cb5a6ff
LP
2076 return 0;
2077}
2078
87f0e418
LP
2079static int service_reload(Unit *u) {
2080 Service *s = SERVICE(u);
034c6ed7
LP
2081
2082 assert(s);
2083
80876c20 2084 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
034c6ed7
LP
2085
2086 service_enter_reload(s);
5cb5a6ff
LP
2087 return 0;
2088}
2089
87f0e418
LP
2090static bool service_can_reload(Unit *u) {
2091 Service *s = SERVICE(u);
034c6ed7
LP
2092
2093 assert(s);
2094
2095 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2096}
2097
a16e1123
LP
2098static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2099 Service *s = SERVICE(u);
2100
2101 assert(u);
2102 assert(f);
2103 assert(fds);
2104
2105 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2106 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2107
2108 if (s->control_pid > 0)
5925dd3c 2109 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
a16e1123 2110
5925dd3c
LP
2111 if (s->main_pid_known && s->main_pid > 0)
2112 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
a16e1123
LP
2113
2114 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2115
3a2776bc
LP
2116 if (s->status_text)
2117 unit_serialize_item(u, f, "status-text", s->status_text);
2118
a16e1123
LP
2119 /* There's a minor uncleanliness here: if there are multiple
2120 * commands attached here, we will start from the first one
2121 * again */
2122 if (s->control_command_id >= 0)
825636e5 2123 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
a16e1123
LP
2124
2125 if (s->socket_fd >= 0) {
2126 int copy;
2127
2128 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2129 return copy;
2130
2131 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2132 }
2133
ecdbca40
LP
2134 if (s->main_exec_status.pid > 0) {
2135 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2136
2137 if (s->main_exec_status.start_timestamp.realtime > 0) {
2138 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2139 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2140
2141 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2142 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2143 }
2144
2145 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2146 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2147 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2148 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2149 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2150
2151 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2152 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2153 }
2154 }
2155
a16e1123
LP
2156 return 0;
2157}
2158
2159static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2160 Service *s = SERVICE(u);
a16e1123
LP
2161
2162 assert(u);
2163 assert(key);
2164 assert(value);
2165 assert(fds);
2166
2167 if (streq(key, "state")) {
2168 ServiceState state;
2169
2170 if ((state = service_state_from_string(value)) < 0)
2171 log_debug("Failed to parse state value %s", value);
2172 else
2173 s->deserialized_state = state;
2174 } else if (streq(key, "failure")) {
2175 int b;
2176
2177 if ((b = parse_boolean(value)) < 0)
2178 log_debug("Failed to parse failure value %s", value);
2179 else
2180 s->failure = b || s->failure;
2181 } else if (streq(key, "control-pid")) {
5925dd3c 2182 pid_t pid;
a16e1123 2183
e364ad06 2184 if (parse_pid(value, &pid) < 0)
a16e1123
LP
2185 log_debug("Failed to parse control-pid value %s", value);
2186 else
e55224ca 2187 s->control_pid = pid;
a16e1123 2188 } else if (streq(key, "main-pid")) {
5925dd3c 2189 pid_t pid;
a16e1123 2190
e364ad06 2191 if (parse_pid(value, &pid) < 0)
a16e1123
LP
2192 log_debug("Failed to parse main-pid value %s", value);
2193 else
5925dd3c 2194 service_set_main_pid(s, (pid_t) pid);
a16e1123
LP
2195 } else if (streq(key, "main-pid-known")) {
2196 int b;
2197
2198 if ((b = parse_boolean(value)) < 0)
2199 log_debug("Failed to parse main-pid-known value %s", value);
2200 else
2201 s->main_pid_known = b;
3a2776bc
LP
2202 } else if (streq(key, "status-text")) {
2203 char *t;
2204
2205 if ((t = strdup(value))) {
2206 free(s->status_text);
2207 s->status_text = t;
2208 }
2209
a16e1123
LP
2210 } else if (streq(key, "control-command")) {
2211 ServiceExecCommand id;
2212
2213 if ((id = service_exec_command_from_string(value)) < 0)
2214 log_debug("Failed to parse exec-command value %s", value);
2215 else {
2216 s->control_command_id = id;
2217 s->control_command = s->exec_command[id];
2218 }
2219 } else if (streq(key, "socket-fd")) {
2220 int fd;
2221
2222 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2223 log_debug("Failed to parse socket-fd value %s", value);
2224 else {
2225
2226 if (s->socket_fd >= 0)
2227 close_nointr_nofail(s->socket_fd);
2228 s->socket_fd = fdset_remove(fds, fd);
2229 }
ecdbca40
LP
2230 } else if (streq(key, "main-exec-status-pid")) {
2231 pid_t pid;
2232
e364ad06 2233 if (parse_pid(value, &pid) < 0)
ecdbca40
LP
2234 log_debug("Failed to parse main-exec-status-pid value %s", value);
2235 else
2236 s->main_exec_status.pid = pid;
2237 } else if (streq(key, "main-exec-status-code")) {
2238 int i;
2239
e364ad06 2240 if (safe_atoi(value, &i) < 0)
ecdbca40
LP
2241 log_debug("Failed to parse main-exec-status-code value %s", value);
2242 else
2243 s->main_exec_status.code = i;
2244 } else if (streq(key, "main-exec-status-status")) {
2245 int i;
2246
e364ad06 2247 if (safe_atoi(value, &i) < 0)
ecdbca40
LP
2248 log_debug("Failed to parse main-exec-status-status value %s", value);
2249 else
2250 s->main_exec_status.status = i;
2251 } else if (streq(key, "main-exec-status-start-realtime")) {
2252 uint64_t k;
2253
e364ad06 2254 if (safe_atou64(value, &k) < 0)
ecdbca40
LP
2255 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2256 else
2257 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2258 } else if (streq(key, "main-exec-status-start-monotonic")) {
2259 uint64_t k;
2260
e364ad06 2261 if (safe_atou64(value, &k) < 0)
ecdbca40
LP
2262 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2263 else
2264 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2265 } else if (streq(key, "main-exec-status-exit-realtime")) {
2266 uint64_t k;
2267
e364ad06 2268 if (safe_atou64(value, &k) < 0)
ecdbca40
LP
2269 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2270 else
2271 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2272 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2273 uint64_t k;
2274
e364ad06 2275 if (safe_atou64(value, &k) < 0)
ecdbca40
LP
2276 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2277 else
2278 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
a16e1123
LP
2279 } else
2280 log_debug("Unknown serialization key '%s'", key);
2281
2282 return 0;
2283}
2284
87f0e418
LP
2285static UnitActiveState service_active_state(Unit *u) {
2286 assert(u);
5cb5a6ff 2287
acbb0225 2288 return state_translation_table[SERVICE(u)->state];
034c6ed7
LP
2289}
2290
10a94420
LP
2291static const char *service_sub_state_to_string(Unit *u) {
2292 assert(u);
2293
2294 return service_state_to_string(SERVICE(u)->state);
2295}
2296
07459bb6 2297#ifdef HAVE_SYSV_COMPAT
701cc384
LP
2298static bool service_check_gc(Unit *u) {
2299 Service *s = SERVICE(u);
2300
2301 assert(s);
2302
2303 return !!s->sysv_path;
2304}
07459bb6 2305#endif
701cc384
LP
2306
2307static bool service_check_snapshot(Unit *u) {
2308 Service *s = SERVICE(u);
2309
2310 assert(s);
2311
2312 return !s->got_socket_fd;
2313}
2314
87f0e418
LP
2315static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2316 Service *s = SERVICE(u);
034c6ed7 2317 bool success;
5cb5a6ff
LP
2318
2319 assert(s);
034c6ed7
LP
2320 assert(pid >= 0);
2321
f21781d5 2322 if (!s->meta.fragment_path)
d06dacd0
LP
2323 success = is_clean_exit_lsb(code, status);
2324 else
2325 success = is_clean_exit(code, status);
034c6ed7
LP
2326
2327 if (s->main_pid == pid) {
2328
034c6ed7 2329 s->main_pid = 0;
34e9ba66 2330 exec_status_exit(&s->main_exec_status, pid, code, status);
034c6ed7 2331
34e9ba66
LP
2332 if (s->type != SERVICE_FORKING && s->control_command) {
2333 s->control_command->exec_status = s->main_exec_status;
b708e7ce 2334
34e9ba66 2335 if (s->control_command->ignore)
b708e7ce 2336 success = true;
034c6ed7
LP
2337 }
2338
92abbefb
LP
2339 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2340 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
b708e7ce 2341 s->failure = s->failure || !success;
034c6ed7 2342
34e9ba66
LP
2343 if (s->control_command &&
2344 s->control_command->command_next &&
2345 success) {
034c6ed7 2346
34e9ba66
LP
2347 /* There is another command to *
2348 * execute, so let's do that. */
034c6ed7 2349
34e9ba66
LP
2350 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2351 service_run_next_main(s, success);
034c6ed7 2352
34e9ba66
LP
2353 } else {
2354
2355 /* The service exited, so the service is officially
2356 * gone. */
2357
2358 s->control_command = NULL;
2359 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2360
2361 switch (s->state) {
2362
2363 case SERVICE_START_POST:
2364 case SERVICE_RELOAD:
2365 case SERVICE_STOP:
2366 /* Need to wait until the operation is
2367 * done */
c4653a4d 2368 break;
7d55e835 2369
34e9ba66
LP
2370 case SERVICE_START:
2371 if (s->type == SERVICE_ONESHOT) {
2372 /* This was our main goal, so let's go on */
2373 if (success)
2374 service_enter_start_post(s);
2375 else
2376 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2377 break;
2378 } else {
2379 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
7d55e835 2380
34e9ba66
LP
2381 /* Fall through */
2382 }
034c6ed7 2383
34e9ba66
LP
2384 case SERVICE_RUNNING:
2385 service_enter_running(s, success);
2386 break;
034c6ed7 2387
34e9ba66
LP
2388 case SERVICE_STOP_SIGTERM:
2389 case SERVICE_STOP_SIGKILL:
5cb5a6ff 2390
34e9ba66
LP
2391 if (!control_pid_good(s))
2392 service_enter_stop_post(s, success);
5cb5a6ff 2393
34e9ba66
LP
2394 /* If there is still a control process, wait for that first */
2395 break;
2396
2397 default:
2398 assert_not_reached("Uh, main process died at wrong time.");
2399 }
034c6ed7 2400 }
5cb5a6ff 2401
034c6ed7 2402 } else if (s->control_pid == pid) {
034c6ed7 2403
34e9ba66
LP
2404 s->control_pid = 0;
2405
b708e7ce 2406 if (s->control_command) {
b58b4116 2407 exec_status_exit(&s->control_command->exec_status, pid, code, status);
a16e1123 2408
b708e7ce
LP
2409 if (s->control_command->ignore)
2410 success = true;
2411 }
2412
34e9ba66 2413 log_full(success ? LOG_DEBUG : LOG_NOTICE,
92abbefb 2414 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
b708e7ce 2415 s->failure = s->failure || !success;
034c6ed7 2416
34e9ba66
LP
2417 if (s->control_command &&
2418 s->control_command->command_next &&
2419 success) {
034c6ed7
LP
2420
2421 /* There is another command to *
2422 * execute, so let's do that. */
2423
34e9ba66
LP
2424 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2425 service_run_next_control(s, success);
034c6ed7 2426
80876c20 2427 } else {
034c6ed7
LP
2428 /* No further commands for this step, so let's
2429 * figure out what to do next */
2430
a16e1123
LP
2431 s->control_command = NULL;
2432 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2433
9e2f7c11 2434 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
bd982a8b 2435
034c6ed7
LP
2436 switch (s->state) {
2437
2438 case SERVICE_START_PRE:
2439 if (success)
2440 service_enter_start(s);
2441 else
80876c20 2442 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
2443 break;
2444
2445 case SERVICE_START:
2446 assert(s->type == SERVICE_FORKING);
2447
2448 /* Let's try to load the pid
2449 * file here if we can. We
2450 * ignore the return value,
2451 * since the PID file might
2452 * actually be created by a
2453 * START_POST script */
2454
2455 if (success) {
2456 if (s->pid_file)
2457 service_load_pid_file(s);
2458
2459 service_enter_start_post(s);
2460 } else
80876c20 2461 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
034c6ed7
LP
2462
2463 break;
2464
2465 case SERVICE_START_POST:
2466 if (success && s->pid_file && !s->main_pid_known) {
2467 int r;
2468
2469 /* Hmm, let's see if we can
2470 * load the pid now after the
2471 * start-post scripts got
2472 * executed. */
2473
2474 if ((r = service_load_pid_file(s)) < 0)
4cd1fbcc 2475 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
034c6ed7
LP
2476 }
2477
2478 /* Fall through */
2479
2480 case SERVICE_RELOAD:
80876c20
LP
2481 if (success)
2482 service_enter_running(s, true);
2483 else
034c6ed7
LP
2484 service_enter_stop(s, false);
2485
2486 break;
2487
2488 case SERVICE_STOP:
80876c20 2489 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
034c6ed7
LP
2490 break;
2491
2492 case SERVICE_STOP_SIGTERM:
2493 case SERVICE_STOP_SIGKILL:
2494 if (main_pid_good(s) <= 0)
2495 service_enter_stop_post(s, success);
2496
2497 /* If there is still a service
2498 * process around, wait until
2499 * that one quit, too */
2500 break;
2501
2502 case SERVICE_STOP_POST:
2503 case SERVICE_FINAL_SIGTERM:
2504 case SERVICE_FINAL_SIGKILL:
2505 service_enter_dead(s, success, true);
2506 break;
2507
2508 default:
2509 assert_not_reached("Uh, control process died at wrong time.");
2510 }
2511 }
8c47c732 2512 }
c4e2ceae
LP
2513
2514 /* Notify clients about changed exit status */
2515 unit_add_to_dbus_queue(u);
034c6ed7
LP
2516}
2517
acbb0225 2518static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
87f0e418 2519 Service *s = SERVICE(u);
034c6ed7
LP
2520
2521 assert(s);
2522 assert(elapsed == 1);
2523
acbb0225 2524 assert(w == &s->timer_watch);
034c6ed7
LP
2525
2526 switch (s->state) {
2527
2528 case SERVICE_START_PRE:
2529 case SERVICE_START:
9e2f7c11 2530 log_warning("%s operation timed out. Terminating.", u->meta.id);
80876c20
LP
2531 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2532 break;
2533
034c6ed7
LP
2534 case SERVICE_START_POST:
2535 case SERVICE_RELOAD:
9e2f7c11 2536 log_warning("%s operation timed out. Stopping.", u->meta.id);
034c6ed7
LP
2537 service_enter_stop(s, false);
2538 break;
2539
2540 case SERVICE_STOP:
9e2f7c11 2541 log_warning("%s stopping timed out. Terminating.", u->meta.id);
034c6ed7
LP
2542 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2543 break;
2544
2545 case SERVICE_STOP_SIGTERM:
9e2f7c11 2546 log_warning("%s stopping timed out. Killing.", u->meta.id);
034c6ed7
LP
2547 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2548 break;
2549
2550 case SERVICE_STOP_SIGKILL:
2551 /* Uh, wie sent a SIGKILL and it is still not gone?
2552 * Must be something we cannot kill, so let's just be
2553 * weirded out and continue */
2554
9e2f7c11 2555 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
034c6ed7
LP
2556 service_enter_stop_post(s, false);
2557 break;
2558
2559 case SERVICE_STOP_POST:
9e2f7c11 2560 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
034c6ed7
LP
2561 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2562 break;
2563
2564 case SERVICE_FINAL_SIGTERM:
9e2f7c11 2565 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
034c6ed7
LP
2566 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2567 break;
2568
2569 case SERVICE_FINAL_SIGKILL:
fdf20a31 2570 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
034c6ed7
LP
2571 service_enter_dead(s, false, true);
2572 break;
2573
2574 case SERVICE_AUTO_RESTART:
54165a39 2575 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
034c6ed7
LP
2576 service_enter_restart(s);
2577 break;
2578
2579 default:
2580 assert_not_reached("Timeout at wrong time.");
2581 }
5cb5a6ff
LP
2582}
2583
8e274523
LP
2584static void service_cgroup_notify_event(Unit *u) {
2585 Service *s = SERVICE(u);
2586
2587 assert(u);
2588
9e2f7c11 2589 log_debug("%s: cgroup is empty", u->meta.id);
8e274523
LP
2590
2591 switch (s->state) {
2592
2593 /* Waiting for SIGCHLD is usually more interesting,
2594 * because it includes return codes/signals. Which is
2595 * why we ignore the cgroup events for most cases,
2596 * except when we don't know pid which to expect the
2597 * SIGCHLD for. */
2598
2599 case SERVICE_RUNNING:
80876c20 2600 service_enter_running(s, true);
8e274523
LP
2601 break;
2602
28708d8a
LP
2603 case SERVICE_STOP_SIGTERM:
2604 case SERVICE_STOP_SIGKILL:
2605 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2606 service_enter_stop_post(s, true);
2607
2608 break;
2609
7f97f0fe
LP
2610 case SERVICE_FINAL_SIGTERM:
2611 case SERVICE_FINAL_SIGKILL:
2612 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2613 service_enter_dead(s, true, true);
2614
2615 break;
2616
8e274523
LP
2617 default:
2618 ;
2619 }
2620}
2621
c952c6ec 2622static void service_notify_message(Unit *u, pid_t pid, char **tags) {
8c47c732
LP
2623 Service *s = SERVICE(u);
2624 const char *e;
2625
2626 assert(u);
2627
c952c6ec
LP
2628 if (s->notify_access == NOTIFY_NONE) {
2629 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2630 u->meta.id, (unsigned long) pid);
2631 return;
2632 }
2633
2634 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2635 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2636 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2637 return;
2638 }
2639
8c47c732
LP
2640 log_debug("%s: Got message", u->meta.id);
2641
2642 /* Interpret MAINPID= */
2643 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2644 (s->state == SERVICE_START ||
2645 s->state == SERVICE_START_POST ||
2646 s->state == SERVICE_RUNNING ||
2647 s->state == SERVICE_RELOAD)) {
8c47c732 2648
5925dd3c 2649 if (parse_pid(e + 8, &pid) < 0)
92abbefb 2650 log_warning("Failed to parse notification message %s", e);
8c47c732
LP
2651 else {
2652 log_debug("%s: got %s", u->meta.id, e);
5925dd3c 2653 service_set_main_pid(s, pid);
8c47c732
LP
2654 }
2655 }
2656
2657 /* Interpret READY= */
2658 if (s->type == SERVICE_NOTIFY &&
2659 s->state == SERVICE_START &&
2660 strv_find(tags, "READY=1")) {
2661 log_debug("%s: got READY=1", u->meta.id);
2662
2663 service_enter_start_post(s);
2664 }
2665
2666 /* Interpret STATUS= */
2667 if ((e = strv_find_prefix(tags, "STATUS="))) {
2668 char *t;
2669
3a2776bc
LP
2670 if (e[7]) {
2671 if (!(t = strdup(e+7))) {
2672 log_error("Failed to allocate string.");
2673 return;
2674 }
2675
2676 log_debug("%s: got %s", u->meta.id, e);
8c47c732 2677
3a2776bc
LP
2678 free(s->status_text);
2679 s->status_text = t;
2680 } else {
2681 free(s->status_text);
2682 s->status_text = NULL;
2683 }
8c47c732 2684
8c47c732 2685 }
c4e2ceae
LP
2686
2687 /* Notify clients about changed status or main pid */
2688 unit_add_to_dbus_queue(u);
8c47c732
LP
2689}
2690
07459bb6 2691#ifdef HAVE_SYSV_COMPAT
2c4104f0 2692static int service_enumerate(Manager *m) {
2c4104f0
LP
2693 char **p;
2694 unsigned i;
2695 DIR *d = NULL;
2696 char *path = NULL, *fpath = NULL, *name = NULL;
c68364b7
LP
2697 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2698 Unit *service;
2699 Iterator j;
2c4104f0 2700 int r;
980900c1
TG
2701#ifdef TARGET_ARCH
2702 Unit *previous = NULL;
2703 char *arch_daemons = NULL;
2704 char *arch_daemons_stripped = NULL;
2705 char **arch_daemons_split = NULL;
2706#endif
2c4104f0
LP
2707
2708 assert(m);
2709
980900c1
TG
2710#ifdef TARGET_ARCH
2711 if ((r = parse_env_file("/etc/rc.conf", NEWLINE,
2712 "DAEMONS", &arch_daemons,
2713 NULL)) < 0) {
2714
2715 if (r != -ENOENT)
2716 log_warning("Failed to read /etc/rc.conf: %s", strerror(-r));
2717
2718 } else if (arch_daemons) {
2719 if (!(arch_daemons_stripped = strchr(arch_daemons, '(')))
2720 arch_daemons_stripped = arch_daemons;
2721 else
2722 arch_daemons_stripped++; /* strip start paren */
2723
2724 arch_daemons_stripped[strcspn(arch_daemons_stripped, ")")] = 0; /* strip end paren */
2725
2726 if (!(arch_daemons_split = strv_split_quoted(arch_daemons_stripped))) {
2727 r = -ENOMEM;
2728 goto finish;
2729 }
2730
2731 STRV_FOREACH(p, arch_daemons_split) {
2732
2733 free(name);
2734
2735 if (**p == '!') /* daemons prefixed with ! are disabled, so ignore them */
2736 continue;
2737
2738 if (!(name = sysv_translate_name(*p))) {
2739 r = -ENOMEM;
2740 goto finish;
2741 }
2742
2743 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2744 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2745 continue;
2746 }
2747
2748 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, "multi-user.target", NULL, true)) < 0)
2749 goto finish;
2750
2751 if (previous)
2752 if ((r = unit_add_dependency(service, UNIT_AFTER, previous, true)) < 0)
2753 goto finish;
2754
2755 if (**p != '@') /* daemons prefixed with @ can be started in the background */
2756 previous = service;
2757 }
2758 }
2759#endif
2760
c68364b7
LP
2761 zero(runlevel_services);
2762
84e3543e 2763 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
09cd1ab1 2764 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2c4104f0
LP
2765 struct dirent *de;
2766
2767 free(path);
2768 path = NULL;
09cd1ab1 2769 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2c4104f0
LP
2770 r = -ENOMEM;
2771 goto finish;
2772 }
2773
2774 if (d)
2775 closedir(d);
2776
2777 if (!(d = opendir(path))) {
2778 if (errno != ENOENT)
2779 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2780
2781 continue;
2782 }
2783
2784 while ((de = readdir(d))) {
db06e3b6 2785 int a, b;
2c4104f0
LP
2786
2787 if (ignore_file(de->d_name))
2788 continue;
2789
2790 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2791 continue;
2792
2793 if (strlen(de->d_name) < 4)
2794 continue;
2795
db06e3b6
LP
2796 a = undecchar(de->d_name[1]);
2797 b = undecchar(de->d_name[2]);
2798
2799 if (a < 0 || b < 0)
2800 continue;
2801
2c4104f0
LP
2802 free(fpath);
2803 fpath = NULL;
09cd1ab1 2804 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2c4104f0
LP
2805 r = -ENOMEM;
2806 goto finish;
2807 }
2808
2809 if (access(fpath, X_OK) < 0) {
2810
2811 if (errno != ENOENT)
2812 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2813
2814 continue;
2815 }
2816
2817 free(name);
b7ccee3c 2818 if (!(name = sysv_translate_name(de->d_name + 3))) {
2c4104f0
LP
2819 r = -ENOMEM;
2820 goto finish;
2821 }
2822
398ef8ba 2823 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
fbe9f3a9
LP
2824 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2825 continue;
2826 }
2c4104f0 2827
c68364b7
LP
2828 if (de->d_name[0] == 'S') {
2829
f73d93a4
LP
2830 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2831 SERVICE(service)->sysv_start_priority =
2832 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
db06e3b6 2833
c68364b7 2834 SERVICE(service)->sysv_enabled = true;
f73d93a4 2835 }
db06e3b6 2836
c68364b7
LP
2837 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2838 goto finish;
2c4104f0 2839
c68364b7 2840 if ((r = set_put(runlevel_services[i], service)) < 0)
2c4104f0 2841 goto finish;
23a177ef 2842
fc5df99e
LP
2843 } else if (de->d_name[0] == 'K' &&
2844 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2845 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
6542952f 2846
c68364b7
LP
2847 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2848 goto finish;
2849
2850 if ((r = set_put(shutdown_services, service)) < 0)
2c4104f0
LP
2851 goto finish;
2852 }
2853 }
2854 }
2855
c68364b7
LP
2856 /* Now we loaded all stubs and are aware of the lowest
2857 start-up priority for all services, not let's actually load
2858 the services, this will also tell us which services are
2859 actually native now */
2860 manager_dispatch_load_queue(m);
2861
2862 /* If this is a native service, rely on native ways to pull in
2863 * a service, don't pull it in via sysv rcN.d links. */
2864 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2865 SET_FOREACH(service, runlevel_services[i], j) {
2866 service = unit_follow_merge(service);
2867
2868 if (service->meta.fragment_path)
2869 continue;
2870
2871 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2872 goto finish;
2873 }
2874
2875 /* We honour K links only for halt/reboot. For the normal
2876 * runlevels we assume the stop jobs will be implicitly added
2877 * by the core logic. Also, we don't really distuingish here
2878 * between the runlevels 0 and 6 and just add them to the
2879 * special shutdown target. On SUSE the boot.d/ runlevel is
2880 * also used for shutdown, so we add links for that too to the
2881 * shutdown target.*/
2882 SET_FOREACH(service, shutdown_services, j) {
2883 service = unit_follow_merge(service);
2884
2885 if (service->meta.fragment_path)
2886 continue;
2887
2888 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2889 goto finish;
2890 }
2891
2c4104f0
LP
2892 r = 0;
2893
2894finish:
2895 free(path);
2896 free(fpath);
2897 free(name);
980900c1
TG
2898#ifdef TARGET_ARCH
2899 free(arch_daemons);
2900 free(arch_daemons_split);
2901#endif
fbe9f3a9 2902
c68364b7
LP
2903 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2904 set_free(runlevel_services[i]);
2905 set_free(shutdown_services);
2906
fbe9f3a9
LP
2907 if (d)
2908 closedir(d);
2c4104f0
LP
2909
2910 return r;
2911}
07459bb6 2912#endif
2c4104f0 2913
05e343b7
LP
2914static void service_bus_name_owner_change(
2915 Unit *u,
2916 const char *name,
2917 const char *old_owner,
2918 const char *new_owner) {
2919
2920 Service *s = SERVICE(u);
2921
2922 assert(s);
2923 assert(name);
2924
2925 assert(streq(s->bus_name, name));
2926 assert(old_owner || new_owner);
2927
2928 if (old_owner && new_owner)
2929 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2930 else if (old_owner)
2931 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2932 else
2933 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2934
2935 s->bus_name_good = !!new_owner;
2936
2937 if (s->type == SERVICE_DBUS) {
2938
2939 /* service_enter_running() will figure out what to
2940 * do */
2941 if (s->state == SERVICE_RUNNING)
2942 service_enter_running(s, true);
2943 else if (s->state == SERVICE_START && new_owner)
2944 service_enter_start_post(s);
2945
2946 } else if (new_owner &&
2947 s->main_pid <= 0 &&
2948 (s->state == SERVICE_START ||
2949 s->state == SERVICE_START_POST ||
2950 s->state == SERVICE_RUNNING ||
2951 s->state == SERVICE_RELOAD)) {
2952
2953 /* Try to acquire PID from bus service */
2954 log_debug("Trying to acquire PID from D-Bus name...");
2955
2956 bus_query_pid(u->meta.manager, name);
2957 }
2958}
2959
2960static void service_bus_query_pid_done(
2961 Unit *u,
2962 const char *name,
2963 pid_t pid) {
2964
2965 Service *s = SERVICE(u);
2966
2967 assert(s);
2968 assert(name);
2969
2970 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2971
2972 if (s->main_pid <= 0 &&
2973 (s->state == SERVICE_START ||
2974 s->state == SERVICE_START_POST ||
2975 s->state == SERVICE_RUNNING ||
2976 s->state == SERVICE_RELOAD))
5925dd3c 2977 service_set_main_pid(s, pid);
05e343b7
LP
2978}
2979
6cf6bbc2 2980int service_set_socket_fd(Service *s, int fd, Socket *sock) {
4f2d528d
LP
2981 assert(s);
2982 assert(fd >= 0);
2983
2984 /* This is called by the socket code when instantiating a new
2985 * service for a stream socket and the socket needs to be
2986 * configured. */
2987
4cd1fbcc 2988 if (s->meta.load_state != UNIT_LOADED)
4f2d528d
LP
2989 return -EINVAL;
2990
2991 if (s->socket_fd >= 0)
2992 return -EBUSY;
2993
2994 if (s->state != SERVICE_DEAD)
2995 return -EAGAIN;
2996
2997 s->socket_fd = fd;
701cc384 2998 s->got_socket_fd = true;
6cf6bbc2
LP
2999 s->socket = sock;
3000
4f2d528d
LP
3001 return 0;
3002}
3003
fdf20a31 3004static void service_reset_failed(Unit *u) {
5632e374
LP
3005 Service *s = SERVICE(u);
3006
3007 assert(s);
3008
fdf20a31 3009 if (s->state == SERVICE_FAILED)
5632e374
LP
3010 service_set_state(s, SERVICE_DEAD);
3011
3012 s->failure = false;
3013}
3014
94f04347
LP
3015static const char* const service_state_table[_SERVICE_STATE_MAX] = {
3016 [SERVICE_DEAD] = "dead",
3017 [SERVICE_START_PRE] = "start-pre",
3018 [SERVICE_START] = "start",
3019 [SERVICE_START_POST] = "start-post",
3020 [SERVICE_RUNNING] = "running",
80876c20 3021 [SERVICE_EXITED] = "exited",
94f04347
LP
3022 [SERVICE_RELOAD] = "reload",
3023 [SERVICE_STOP] = "stop",
3024 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
3025 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
3026 [SERVICE_STOP_POST] = "stop-post",
3027 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
3028 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
fdf20a31 3029 [SERVICE_FAILED] = "failed",
94f04347
LP
3030 [SERVICE_AUTO_RESTART] = "auto-restart",
3031};
3032
3033DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
3034
3035static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
3036 [SERVICE_ONCE] = "once",
3037 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
40531a55 3038 [SERVICE_RESTART_ALWAYS] = "restart-always",
94f04347
LP
3039};
3040
3041DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
3042
3043static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
94f04347 3044 [SERVICE_SIMPLE] = "simple",
0d624a78 3045 [SERVICE_FORKING] = "forking",
34e9ba66 3046 [SERVICE_ONESHOT] = "oneshot",
8c47c732
LP
3047 [SERVICE_DBUS] = "dbus",
3048 [SERVICE_NOTIFY] = "notify"
94f04347
LP
3049};
3050
3051DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
3052
e537352b 3053static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
94f04347
LP
3054 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
3055 [SERVICE_EXEC_START] = "ExecStart",
3056 [SERVICE_EXEC_START_POST] = "ExecStartPost",
3057 [SERVICE_EXEC_RELOAD] = "ExecReload",
3058 [SERVICE_EXEC_STOP] = "ExecStop",
3059 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
3060};
3061
3062DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
3063
c952c6ec
LP
3064static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
3065 [NOTIFY_NONE] = "none",
3066 [NOTIFY_MAIN] = "main",
3067 [NOTIFY_ALL] = "all"
3068};
3069
3070DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
3071
87f0e418 3072const UnitVTable service_vtable = {
5cb5a6ff 3073 .suffix = ".service",
9e58ff9c 3074 .show_status = true,
5cb5a6ff 3075
034c6ed7
LP
3076 .init = service_init,
3077 .done = service_done,
a16e1123
LP
3078 .load = service_load,
3079
3080 .coldplug = service_coldplug,
034c6ed7 3081
5cb5a6ff
LP
3082 .dump = service_dump,
3083
3084 .start = service_start,
3085 .stop = service_stop,
3086 .reload = service_reload,
3087
034c6ed7
LP
3088 .can_reload = service_can_reload,
3089
a16e1123
LP
3090 .serialize = service_serialize,
3091 .deserialize_item = service_deserialize_item,
3092
5cb5a6ff 3093 .active_state = service_active_state,
10a94420 3094 .sub_state_to_string = service_sub_state_to_string,
5cb5a6ff 3095
07459bb6 3096#ifdef HAVE_SYSV_COMPAT
701cc384 3097 .check_gc = service_check_gc,
07459bb6 3098#endif
701cc384
LP
3099 .check_snapshot = service_check_snapshot,
3100
034c6ed7
LP
3101 .sigchld_event = service_sigchld_event,
3102 .timer_event = service_timer_event,
2c4104f0 3103
fdf20a31 3104 .reset_failed = service_reset_failed,
5632e374 3105
8e274523 3106 .cgroup_notify_empty = service_cgroup_notify_event,
8c47c732 3107 .notify_message = service_notify_message,
8e274523 3108
05e343b7
LP
3109 .bus_name_owner_change = service_bus_name_owner_change,
3110 .bus_query_pid_done = service_bus_query_pid_done,
3111
c4e2ceae 3112 .bus_interface = "org.freedesktop.systemd1.Service",
4139c1b2 3113 .bus_message_handler = bus_service_message_handler,
c4e2ceae 3114 .bus_invalidating_properties = bus_service_invalidating_properties,
4139c1b2 3115
07459bb6 3116#ifdef HAVE_SYSV_COMPAT
2c4104f0 3117 .enumerate = service_enumerate
07459bb6 3118#endif
5cb5a6ff 3119};