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