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