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