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