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