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