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