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