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