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