]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/service.c
service: fix check for non-LSB files
[thirdparty/systemd.git] / src / 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 General Public License as published by
10 the Free Software Foundation; either version 2 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 General Public License for more details.
17
18 You should have received a copy of the GNU 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
27 #include "unit.h"
28 #include "service.h"
29 #include "load-fragment.h"
30 #include "load-dropin.h"
31 #include "log.h"
32 #include "strv.h"
33 #include "unit-name.h"
34 #include "dbus-service.h"
35 #include "special.h"
36 #include "bus-errors.h"
37
38 #define COMMENTS "#;\n"
39 #define NEWLINES "\n\r"
40
41 typedef enum RunlevelType {
42 RUNLEVEL_UP,
43 RUNLEVEL_DOWN,
44 RUNLEVEL_SYSINIT
45 } RunlevelType;
46
47 static const struct {
48 const char *path;
49 const char *target;
50 const RunlevelType type;
51 } rcnd_table[] = {
52 /* Standard SysV runlevels for start-up */
53 { "rc1.d", SPECIAL_RESCUE_TARGET, RUNLEVEL_UP },
54 { "rc2.d", SPECIAL_RUNLEVEL2_TARGET, RUNLEVEL_UP },
55 { "rc3.d", SPECIAL_RUNLEVEL3_TARGET, RUNLEVEL_UP },
56 { "rc4.d", SPECIAL_RUNLEVEL4_TARGET, RUNLEVEL_UP },
57 { "rc5.d", SPECIAL_RUNLEVEL5_TARGET, RUNLEVEL_UP },
58
59 #ifdef TARGET_SUSE
60 /* SUSE style boot.d */
61 { "boot.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
62 #endif
63
64 #ifdef TARGET_DEBIAN
65 /* Debian style rcS.d */
66 { "rcS.d", SPECIAL_SYSINIT_TARGET, RUNLEVEL_SYSINIT },
67 #endif
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/SYSINIT must be read before DOWN */
78 };
79
80 #define RUNLEVELS_UP "12345"
81 /* #define RUNLEVELS_DOWN "06" */
82 /* #define RUNLEVELS_BOOT "bBsS" */
83
84 static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = {
85 [SERVICE_DEAD] = UNIT_INACTIVE,
86 [SERVICE_START_PRE] = UNIT_ACTIVATING,
87 [SERVICE_START] = UNIT_ACTIVATING,
88 [SERVICE_START_POST] = UNIT_ACTIVATING,
89 [SERVICE_RUNNING] = UNIT_ACTIVE,
90 [SERVICE_EXITED] = UNIT_ACTIVE,
91 [SERVICE_RELOAD] = UNIT_RELOADING,
92 [SERVICE_STOP] = UNIT_DEACTIVATING,
93 [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING,
94 [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING,
95 [SERVICE_STOP_POST] = UNIT_DEACTIVATING,
96 [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING,
97 [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING,
98 [SERVICE_FAILED] = UNIT_FAILED,
99 [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING
100 };
101
102 static void service_init(Unit *u) {
103 Service *s = SERVICE(u);
104
105 assert(u);
106 assert(u->meta.load_state == UNIT_STUB);
107
108 s->timeout_usec = DEFAULT_TIMEOUT_USEC;
109 s->restart_usec = DEFAULT_RESTART_USEC;
110 s->timer_watch.type = WATCH_INVALID;
111 s->sysv_start_priority = -1;
112 s->socket_fd = -1;
113
114 exec_context_init(&s->exec_context);
115
116 RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5);
117
118 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
119 }
120
121 static void service_unwatch_control_pid(Service *s) {
122 assert(s);
123
124 if (s->control_pid <= 0)
125 return;
126
127 unit_unwatch_pid(UNIT(s), s->control_pid);
128 s->control_pid = 0;
129 }
130
131 static void service_unwatch_main_pid(Service *s) {
132 assert(s);
133
134 if (s->main_pid <= 0)
135 return;
136
137 unit_unwatch_pid(UNIT(s), s->main_pid);
138 s->main_pid = 0;
139 }
140
141 static int service_set_main_pid(Service *s, pid_t pid) {
142 pid_t ppid;
143
144 assert(s);
145
146 if (pid <= 1)
147 return -EINVAL;
148
149 if (pid == getpid())
150 return -EINVAL;
151
152 if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid())
153 log_warning("%s: Supervising process %lu which is not our child. We'll most likely not notice when it exits.",
154 s->meta.id, (unsigned long) pid);
155
156 s->main_pid = pid;
157 s->main_pid_known = true;
158
159 exec_status_start(&s->main_exec_status, pid);
160
161 return 0;
162 }
163
164 static void service_close_socket_fd(Service *s) {
165 assert(s);
166
167 if (s->socket_fd < 0)
168 return;
169
170 close_nointr_nofail(s->socket_fd);
171 s->socket_fd = -1;
172 }
173
174 static void service_connection_unref(Service *s) {
175 assert(s);
176
177 if (!s->socket)
178 return;
179
180 socket_connection_unref(s->socket);
181 s->socket = NULL;
182 }
183
184 static void service_done(Unit *u) {
185 Service *s = SERVICE(u);
186
187 assert(s);
188
189 free(s->pid_file);
190 s->pid_file = NULL;
191
192 free(s->sysv_path);
193 s->sysv_path = NULL;
194
195 free(s->sysv_runlevels);
196 s->sysv_runlevels = NULL;
197
198 free(s->status_text);
199 s->status_text = NULL;
200
201 exec_context_done(&s->exec_context);
202 exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX);
203 s->control_command = NULL;
204
205 /* This will leak a process, but at least no memory or any of
206 * our resources */
207 service_unwatch_main_pid(s);
208 service_unwatch_control_pid(s);
209
210 if (s->bus_name) {
211 unit_unwatch_bus_name(UNIT(u), s->bus_name);
212 free(s->bus_name);
213 s->bus_name = NULL;
214 }
215
216 service_close_socket_fd(s);
217 service_connection_unref(s);
218
219 unit_unwatch_timer(u, &s->timer_watch);
220 }
221
222 static char *sysv_translate_name(const char *name) {
223 char *r;
224
225 if (!(r = new(char, strlen(name) + sizeof(".service"))))
226 return NULL;
227
228 if (startswith(name, "boot."))
229 /* Drop SuSE-style boot. prefix */
230 strcpy(stpcpy(r, name + 5), ".service");
231 else if (endswith(name, ".sh"))
232 /* Drop Debian-style .sh suffix */
233 strcpy(stpcpy(r, name) - 3, ".service");
234 else
235 /* Normal init scripts */
236 strcpy(stpcpy(r, name), ".service");
237
238 return r;
239 }
240
241 static int sysv_translate_facility(const char *name, char **_r) {
242
243 static const char * const table[] = {
244 /* LSB defined facilities */
245 "$local_fs", SPECIAL_LOCAL_FS_TARGET,
246 "$network", SPECIAL_NETWORK_TARGET,
247 "$named", SPECIAL_NSS_LOOKUP_TARGET,
248 "$portmap", SPECIAL_RPCBIND_TARGET,
249 "$remote_fs", SPECIAL_REMOTE_FS_TARGET,
250 "$syslog", SPECIAL_SYSLOG_TARGET,
251 "$time", SPECIAL_RTC_SET_TARGET,
252
253 /* Debian extensions */
254 #ifdef TARGET_DEBIAN
255 "$mail-transport-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
256 #endif
257 "$mail-transfer-agent", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
258 "$x-display-manager", SPECIAL_DISPLAY_MANAGER_SERVICE,
259
260 #ifdef TARGET_FEDORA
261 /* Fedora extensions, lacking the $ prefix */
262 "MTA", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
263 "smtpdaemon", SPECIAL_MAIL_TRANSFER_AGENT_TARGET,
264 "httpd", SPECIAL_HTTP_DAEMON_TARGET,
265 #endif
266 };
267
268 unsigned i;
269 char *r;
270
271 for (i = 0; i < ELEMENTSOF(table); i += 2)
272 if (streq(table[i], name)) {
273 if (!(r = strdup(table[i+1])))
274 return -ENOMEM;
275
276 goto finish;
277 }
278
279 if (*name == '$')
280 r = unit_name_build(name+1, NULL, ".target");
281 else
282 r = sysv_translate_name(name);
283
284 if (!r)
285 return -ENOMEM;
286
287 finish:
288
289 if (_r)
290 *_r = r;
291
292 return 1;
293 }
294
295 static int sysv_fix_order(Service *s) {
296 Meta *other;
297 int r;
298
299 assert(s);
300
301 if (s->sysv_start_priority < 0)
302 return 0;
303
304 /* For each pair of services where at least one lacks a LSB
305 * header, we use the start priority value to order things. */
306
307 LIST_FOREACH(units_per_type, other, s->meta.manager->units_per_type[UNIT_SERVICE]) {
308 Service *t;
309 UnitDependency d;
310 bool special_s, special_t;
311
312 t = (Service*) other;
313
314 if (s == t)
315 continue;
316
317 if (t->sysv_start_priority < 0)
318 continue;
319
320 /* If both units have modern headers we don't care
321 * about the priorities */
322 if ((s->meta.fragment_path || s->sysv_has_lsb) &&
323 (t->meta.fragment_path || t->sysv_has_lsb))
324 continue;
325
326 special_s = s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels);
327 special_t = t->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, t->sysv_runlevels);
328
329 if (special_t && !special_s)
330 d = UNIT_AFTER;
331 else if (special_s && !special_t)
332 d = UNIT_BEFORE;
333 else if (t->sysv_start_priority < s->sysv_start_priority)
334 d = UNIT_AFTER;
335 else if (t->sysv_start_priority > s->sysv_start_priority)
336 d = UNIT_BEFORE;
337 else
338 continue;
339
340 /* FIXME: Maybe we should compare the name here lexicographically? */
341
342 if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t), true)) < 0)
343 return r;
344 }
345
346 return 0;
347 }
348
349 static ExecCommand *exec_command_new(const char *path, const char *arg1) {
350 ExecCommand *c;
351
352 if (!(c = new0(ExecCommand, 1)))
353 return NULL;
354
355 if (!(c->path = strdup(path))) {
356 free(c);
357 return NULL;
358 }
359
360 if (!(c->argv = strv_new(path, arg1, NULL))) {
361 free(c->path);
362 free(c);
363 return NULL;
364 }
365
366 return c;
367 }
368
369 static int sysv_exec_commands(Service *s) {
370 ExecCommand *c;
371
372 assert(s);
373 assert(s->sysv_path);
374
375 if (!(c = exec_command_new(s->sysv_path, "start")))
376 return -ENOMEM;
377 exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c);
378
379 if (!(c = exec_command_new(s->sysv_path, "stop")))
380 return -ENOMEM;
381 exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c);
382
383 if (!(c = exec_command_new(s->sysv_path, "reload")))
384 return -ENOMEM;
385 exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c);
386
387 return 0;
388 }
389
390 static int service_load_sysv_path(Service *s, const char *path) {
391 FILE *f;
392 Unit *u;
393 unsigned line = 0;
394 int r;
395 enum {
396 NORMAL,
397 DESCRIPTION,
398 LSB,
399 LSB_DESCRIPTION
400 } state = NORMAL;
401
402 assert(s);
403 assert(path);
404
405 u = UNIT(s);
406
407 if (!(f = fopen(path, "re"))) {
408 r = errno == ENOENT ? 0 : -errno;
409 goto finish;
410 }
411
412 free(s->sysv_path);
413 if (!(s->sysv_path = strdup(path))) {
414 r = -ENOMEM;
415 goto finish;
416 }
417
418 while (!feof(f)) {
419 char l[LINE_MAX], *t;
420
421 if (!fgets(l, sizeof(l), f)) {
422 if (feof(f))
423 break;
424
425 r = -errno;
426 log_error("Failed to read configuration file '%s': %s", path, strerror(-r));
427 goto finish;
428 }
429
430 line++;
431
432 t = strstrip(l);
433 if (*t != '#')
434 continue;
435
436 if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) {
437 state = LSB;
438 s->sysv_has_lsb = true;
439 continue;
440 }
441
442 if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) {
443 state = NORMAL;
444 continue;
445 }
446
447 t++;
448 t += strspn(t, WHITESPACE);
449
450 if (state == NORMAL) {
451
452 /* Try to parse Red Hat style chkconfig headers */
453
454 if (startswith_no_case(t, "chkconfig:")) {
455 int start_priority;
456 char runlevels[16], *k;
457
458 state = NORMAL;
459
460 if (sscanf(t+10, "%15s %i %*i",
461 runlevels,
462 &start_priority) != 2) {
463
464 log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line);
465 continue;
466 }
467
468 /* A start priority gathered from the
469 * symlink farms is preferred over the
470 * data from the LSB header. */
471 if (start_priority < 0 || start_priority > 99)
472 log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line);
473 else if (s->sysv_start_priority < 0)
474 s->sysv_start_priority = start_priority;
475
476 char_array_0(runlevels);
477 k = delete_chars(runlevels, WHITESPACE "-");
478
479 if (k[0]) {
480 char *d;
481
482 if (!(d = strdup(k))) {
483 r = -ENOMEM;
484 goto finish;
485 }
486
487 free(s->sysv_runlevels);
488 s->sysv_runlevels = d;
489 }
490
491 } else if (startswith_no_case(t, "description:") &&
492 !u->meta.description) {
493
494 size_t k = strlen(t);
495 char *d;
496
497 if (t[k-1] == '\\') {
498 state = DESCRIPTION;
499 t[k-1] = 0;
500 }
501
502 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
503 r = -ENOMEM;
504 goto finish;
505 }
506
507 free(u->meta.description);
508 u->meta.description = d;
509
510 } else if (startswith_no_case(t, "pidfile:")) {
511
512 char *fn;
513
514 state = NORMAL;
515
516 fn = strstrip(t+8);
517 if (!path_is_absolute(fn)) {
518 log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line);
519 continue;
520 }
521
522 if (!(fn = strdup(fn))) {
523 r = -ENOMEM;
524 goto finish;
525 }
526
527 free(s->pid_file);
528 s->pid_file = fn;
529 }
530
531 } else if (state == DESCRIPTION) {
532
533 /* Try to parse Red Hat style description
534 * continuation */
535
536 size_t k = strlen(t);
537 char *d;
538
539 if (t[k-1] == '\\')
540 t[k-1] = 0;
541 else
542 state = NORMAL;
543
544 assert(u->meta.description);
545 if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) {
546 r = -ENOMEM;
547 goto finish;
548 }
549
550 free(u->meta.description);
551 u->meta.description = d;
552
553 } else if (state == LSB || state == LSB_DESCRIPTION) {
554
555 if (startswith_no_case(t, "Provides:")) {
556 char *i, *w;
557 size_t z;
558
559 state = LSB;
560
561 FOREACH_WORD_QUOTED(w, z, t+9, i) {
562 char *n, *m;
563
564 if (!(n = strndup(w, z))) {
565 r = -ENOMEM;
566 goto finish;
567 }
568
569 r = sysv_translate_facility(n, &m);
570 free(n);
571
572 if (r < 0)
573 goto finish;
574
575 if (r == 0)
576 continue;
577
578 if (unit_name_to_type(m) == UNIT_SERVICE)
579 r = unit_add_name(u, m);
580 else {
581 r = unit_add_dependency_by_name(u, UNIT_BEFORE, m, NULL, true);
582
583 if (s->sysv_enabled) {
584 int k;
585
586 if ((k = unit_add_dependency_by_name_inverse(u, UNIT_WANTS, m, NULL, true)) < 0)
587 r = k;
588 }
589 }
590
591 if (r < 0)
592 log_error("[%s:%u] Failed to add LSB Provides name %s, ignoring: %s", path, line, m, strerror(-r));
593
594 free(m);
595 }
596
597 } else if (startswith_no_case(t, "Required-Start:") ||
598 startswith_no_case(t, "Should-Start:") ||
599 startswith_no_case(t, "X-Start-Before:") ||
600 startswith_no_case(t, "X-Start-After:")) {
601 char *i, *w;
602 size_t z;
603
604 state = LSB;
605
606 FOREACH_WORD_QUOTED(w, z, strchr(t, ':')+1, i) {
607 char *n, *m;
608
609 if (!(n = strndup(w, z))) {
610 r = -ENOMEM;
611 goto finish;
612 }
613
614 r = sysv_translate_facility(n, &m);
615 free(n);
616
617 if (r < 0)
618 goto finish;
619
620 if (r == 0)
621 continue;
622
623 r = unit_add_dependency_by_name(u, startswith_no_case(t, "X-Start-Before:") ? UNIT_BEFORE : UNIT_AFTER, m, NULL, true);
624
625 if (r < 0)
626 log_error("[%s:%u] Failed to add dependency on %s, ignoring: %s", path, line, m, strerror(-r));
627
628 free(m);
629 }
630 } else if (startswith_no_case(t, "Default-Start:")) {
631 char *k, *d;
632
633 state = LSB;
634
635 k = delete_chars(t+14, WHITESPACE "-");
636
637 if (k[0] != 0) {
638 if (!(d = strdup(k))) {
639 r = -ENOMEM;
640 goto finish;
641 }
642
643 free(s->sysv_runlevels);
644 s->sysv_runlevels = d;
645 }
646
647 } else if (startswith_no_case(t, "Description:") &&
648 !u->meta.description) {
649 char *d;
650
651 /* We use the long description only if
652 * no short description is set. */
653
654 state = LSB_DESCRIPTION;
655
656 if (!(d = strappend("LSB: ", strstrip(t+12)))) {
657 r = -ENOMEM;
658 goto finish;
659 }
660
661 free(u->meta.description);
662 u->meta.description = d;
663
664 } else if (startswith_no_case(t, "Short-Description:")) {
665 char *d;
666
667 state = LSB;
668
669 if (!(d = strappend("LSB: ", strstrip(t+18)))) {
670 r = -ENOMEM;
671 goto finish;
672 }
673
674 free(u->meta.description);
675 u->meta.description = d;
676
677 } else if (startswith_no_case(t, "X-Interactive:")) {
678 int b;
679
680 if ((b = parse_boolean(strstrip(t+14))) < 0) {
681 log_warning("[%s:%u] Couldn't parse interactive flag. Ignoring.", path, line);
682 continue;
683 }
684
685 if (b)
686 s->exec_context.std_input = EXEC_INPUT_TTY;
687 else
688 s->exec_context.std_input = EXEC_INPUT_NULL;
689
690 } else if (state == LSB_DESCRIPTION) {
691
692 if (startswith(l, "#\t") || startswith(l, "# ")) {
693 char *d;
694
695 assert(u->meta.description);
696 if (asprintf(&d, "%s %s", u->meta.description, t) < 0) {
697 r = -ENOMEM;
698 goto finish;
699 }
700
701 free(u->meta.description);
702 u->meta.description = d;
703 } else
704 state = LSB;
705 }
706 }
707 }
708
709 if ((r = sysv_exec_commands(s)) < 0)
710 goto finish;
711
712 if (s->sysv_runlevels && !chars_intersect(RUNLEVELS_UP, s->sysv_runlevels)) {
713 /* If there a runlevels configured for this service
714 * but none of the standard ones, then we assume this
715 * is some special kind of service (which might be
716 * needed for early boot) and don't create any links
717 * to it. */
718
719 s->meta.default_dependencies = false;
720
721 /* Don't timeout special services during boot (like fsck) */
722 s->timeout_usec = 0;
723 }
724
725 /* Special setting for all SysV services */
726 s->type = SERVICE_FORKING;
727 s->remain_after_exit = true;
728 s->restart = SERVICE_ONCE;
729 s->exec_context.std_output =
730 (s->meta.manager->sysv_console || s->exec_context.std_input == EXEC_INPUT_TTY)
731 ? EXEC_OUTPUT_TTY : EXEC_OUTPUT_NULL;
732 s->exec_context.kill_mode = KILL_PROCESS_GROUP;
733
734 u->meta.load_state = UNIT_LOADED;
735 r = 0;
736
737 finish:
738
739 if (f)
740 fclose(f);
741
742 return r;
743 }
744
745 static int service_load_sysv_name(Service *s, const char *name) {
746 char **p;
747
748 assert(s);
749 assert(name);
750
751 /* For SysV services we strip the boot. or .sh
752 * prefixes/suffixes. */
753 if (startswith(name, "boot.") ||
754 endswith(name, ".sh.service"))
755 return -ENOENT;
756
757 STRV_FOREACH(p, s->meta.manager->lookup_paths.sysvinit_path) {
758 char *path;
759 int r;
760
761 if (asprintf(&path, "%s/%s", *p, name) < 0)
762 return -ENOMEM;
763
764 assert(endswith(path, ".service"));
765 path[strlen(path)-8] = 0;
766
767 r = service_load_sysv_path(s, path);
768
769 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
770 /* Try Debian style xxx.sh source'able init scripts */
771 strcat(path, ".sh");
772 r = service_load_sysv_path(s, path);
773 }
774
775 free(path);
776
777 if (r >= 0 && s->meta.load_state == UNIT_STUB) {
778 /* Try SUSE style boot.xxx init scripts */
779
780 if (asprintf(&path, "%s/boot.%s", *p, name) < 0)
781 return -ENOMEM;
782
783 path[strlen(path)-8] = 0;
784 r = service_load_sysv_path(s, path);
785 free(path);
786 }
787
788 if (r < 0)
789 return r;
790
791 if ((s->meta.load_state != UNIT_STUB))
792 break;
793 }
794
795 return 0;
796 }
797
798 static int service_load_sysv(Service *s) {
799 const char *t;
800 Iterator i;
801 int r;
802
803 assert(s);
804
805 /* Load service data from SysV init scripts, preferably with
806 * LSB headers ... */
807
808 if (strv_isempty(s->meta.manager->lookup_paths.sysvinit_path))
809 return 0;
810
811 if ((t = s->meta.id))
812 if ((r = service_load_sysv_name(s, t)) < 0)
813 return r;
814
815 if (s->meta.load_state == UNIT_STUB)
816 SET_FOREACH(t, s->meta.names, i) {
817 if (t == s->meta.id)
818 continue;
819
820 if ((r = service_load_sysv_name(s, t)) < 0)
821 return r;
822
823 if (s->meta.load_state != UNIT_STUB)
824 break;
825 }
826
827 return 0;
828 }
829
830 static int service_verify(Service *s) {
831 assert(s);
832
833 if (s->meta.load_state != UNIT_LOADED)
834 return 0;
835
836 if (!s->exec_command[SERVICE_EXEC_START]) {
837 log_error("%s lacks ExecStart setting. Refusing.", s->meta.id);
838 return -EINVAL;
839 }
840
841 if (s->type != SERVICE_ONESHOT &&
842 s->exec_command[SERVICE_EXEC_START]->command_next) {
843 log_error("%s has more than one ExecStart setting, which is only allowed for Type=oneshot services. Refusing.", s->meta.id);
844 return -EINVAL;
845 }
846
847 if (s->type == SERVICE_DBUS && !s->bus_name) {
848 log_error("%s is of type D-Bus but no D-Bus service name has been specified. Refusing.", s->meta.id);
849 return -EINVAL;
850 }
851
852 if (s->exec_context.pam_name && s->exec_context.kill_mode != KILL_CONTROL_GROUP) {
853 log_error("%s has PAM enabled. Kill mode must be set to 'control-group'. Refusing.", s->meta.id);
854 return -EINVAL;
855 }
856
857 return 0;
858 }
859
860 static int service_add_default_dependencies(Service *s) {
861 int r;
862
863 assert(s);
864
865 /* Add a number of automatic dependencies useful for the
866 * majority of services. */
867
868 /* First, pull in base system */
869 if (s->meta.manager->running_as == MANAGER_SYSTEM) {
870
871 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true)) < 0)
872 return r;
873
874 } else if (s->meta.manager->running_as == MANAGER_SESSION) {
875
876 if ((r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_SOCKETS_TARGET, NULL, true)) < 0)
877 return r;
878 }
879
880 /* Second, activate normal shutdown */
881 return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTED_BY, SPECIAL_SHUTDOWN_TARGET, NULL, true);
882 }
883
884 static int service_load(Unit *u) {
885 int r;
886 Service *s = SERVICE(u);
887
888 assert(s);
889
890 /* Load a .service file */
891 if ((r = unit_load_fragment(u)) < 0)
892 return r;
893
894 /* Load a classic init script as a fallback, if we couldn't find anything */
895 if (u->meta.load_state == UNIT_STUB)
896 if ((r = service_load_sysv(s)) < 0)
897 return r;
898
899 /* Still nothing found? Then let's give up */
900 if (u->meta.load_state == UNIT_STUB)
901 return -ENOENT;
902
903 /* We were able to load something, then let's add in the
904 * dropin directories. */
905 if ((r = unit_load_dropin(unit_follow_merge(u))) < 0)
906 return r;
907
908 /* This is a new unit? Then let's add in some extras */
909 if (u->meta.load_state == UNIT_LOADED) {
910 if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0)
911 return r;
912
913 if ((r = unit_add_default_cgroup(u)) < 0)
914 return r;
915
916 if ((r = sysv_fix_order(s)) < 0)
917 return r;
918
919 if (s->bus_name)
920 if ((r = unit_watch_bus_name(u, s->bus_name)) < 0)
921 return r;
922
923 if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE)
924 s->notify_access = NOTIFY_MAIN;
925
926 if (s->type == SERVICE_DBUS || s->bus_name)
927 if ((r = unit_add_two_dependencies_by_name(u, UNIT_AFTER, UNIT_REQUIRES, SPECIAL_DBUS_TARGET, NULL, true)) < 0)
928 return r;
929
930 if (s->meta.default_dependencies)
931 if ((r = service_add_default_dependencies(s)) < 0)
932 return r;
933 }
934
935 return service_verify(s);
936 }
937
938 static void service_dump(Unit *u, FILE *f, const char *prefix) {
939
940 ServiceExecCommand c;
941 Service *s = SERVICE(u);
942 const char *prefix2;
943 char *p2;
944
945 assert(s);
946
947 p2 = strappend(prefix, "\t");
948 prefix2 = p2 ? p2 : prefix;
949
950 fprintf(f,
951 "%sService State: %s\n"
952 "%sPermissionsStartOnly: %s\n"
953 "%sRootDirectoryStartOnly: %s\n"
954 "%sRemainAfterExit: %s\n"
955 "%sType: %s\n"
956 "%sRestart: %s\n"
957 "%sNotifyAccess: %s\n",
958 prefix, service_state_to_string(s->state),
959 prefix, yes_no(s->permissions_start_only),
960 prefix, yes_no(s->root_directory_start_only),
961 prefix, yes_no(s->remain_after_exit),
962 prefix, service_type_to_string(s->type),
963 prefix, service_restart_to_string(s->restart),
964 prefix, notify_access_to_string(s->notify_access));
965
966 if (s->control_pid > 0)
967 fprintf(f,
968 "%sControl PID: %lu\n",
969 prefix, (unsigned long) s->control_pid);
970
971 if (s->main_pid > 0)
972 fprintf(f,
973 "%sMain PID: %lu\n",
974 prefix, (unsigned long) s->main_pid);
975
976 if (s->pid_file)
977 fprintf(f,
978 "%sPIDFile: %s\n",
979 prefix, s->pid_file);
980
981 if (s->bus_name)
982 fprintf(f,
983 "%sBusName: %s\n"
984 "%sBus Name Good: %s\n",
985 prefix, s->bus_name,
986 prefix, yes_no(s->bus_name_good));
987
988 exec_context_dump(&s->exec_context, f, prefix);
989
990 for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) {
991
992 if (!s->exec_command[c])
993 continue;
994
995 fprintf(f, "%s-> %s:\n",
996 prefix, service_exec_command_to_string(c));
997
998 exec_command_dump_list(s->exec_command[c], f, prefix2);
999 }
1000
1001 if (s->sysv_path)
1002 fprintf(f,
1003 "%sSysV Init Script Path: %s\n"
1004 "%sSysV Init Script has LSB Header: %s\n",
1005 prefix, s->sysv_path,
1006 prefix, yes_no(s->sysv_has_lsb));
1007
1008 if (s->sysv_start_priority >= 0)
1009 fprintf(f,
1010 "%sSysVStartPriority: %i\n"
1011 "%sSysVEnabled: %s\n",
1012 prefix, s->sysv_start_priority,
1013 prefix, yes_no(s->sysv_enabled));
1014
1015 if (s->sysv_runlevels)
1016 fprintf(f, "%sSysVRunLevels: %s\n",
1017 prefix, s->sysv_runlevels);
1018
1019 if (s->status_text)
1020 fprintf(f, "%sStatus Text: %s\n",
1021 prefix, s->status_text);
1022
1023 free(p2);
1024 }
1025
1026 static int service_load_pid_file(Service *s) {
1027 char *k;
1028 int r;
1029 pid_t pid;
1030
1031 assert(s);
1032
1033 if (s->main_pid_known)
1034 return 0;
1035
1036 assert(s->main_pid <= 0);
1037
1038 if (!s->pid_file)
1039 return -ENOENT;
1040
1041 if ((r = read_one_line_file(s->pid_file, &k)) < 0)
1042 return r;
1043
1044 r = parse_pid(k, &pid);
1045 free(k);
1046
1047 if (r < 0)
1048 return r;
1049
1050 if (kill(pid, 0) < 0 && errno != EPERM) {
1051 log_warning("PID %lu read from file %s does not exist. Your service or init script might be broken.",
1052 (unsigned long) pid, s->pid_file);
1053 return -ESRCH;
1054 }
1055
1056 if ((r = service_set_main_pid(s, pid)) < 0)
1057 return r;
1058
1059 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1060 /* FIXME: we need to do something here */
1061 return r;
1062
1063 return 0;
1064 }
1065
1066 static int service_get_sockets(Service *s, Set **_set) {
1067 Set *set;
1068 Iterator i;
1069 char *t;
1070 int r;
1071
1072 assert(s);
1073 assert(_set);
1074
1075 if (s->socket_fd >= 0)
1076 return 0;
1077
1078 /* Collects all Socket objects that belong to this
1079 * service. Note that a service might have multiple sockets
1080 * via multiple names. */
1081
1082 if (!(set = set_new(NULL, NULL)))
1083 return -ENOMEM;
1084
1085 SET_FOREACH(t, s->meta.names, i) {
1086 char *k;
1087 Unit *p;
1088
1089 /* Look for all socket objects that go by any of our
1090 * units and collect their fds */
1091
1092 if (!(k = unit_name_change_suffix(t, ".socket"))) {
1093 r = -ENOMEM;
1094 goto fail;
1095 }
1096
1097 p = manager_get_unit(s->meta.manager, k);
1098 free(k);
1099
1100 if (!p)
1101 continue;
1102
1103 if ((r = set_put(set, p)) < 0)
1104 goto fail;
1105 }
1106
1107 *_set = set;
1108 return 0;
1109
1110 fail:
1111 set_free(set);
1112 return r;
1113 }
1114
1115 static int service_notify_sockets_dead(Service *s) {
1116 Iterator i;
1117 Set *set;
1118 Socket *sock;
1119 int r;
1120
1121 assert(s);
1122
1123 if (s->socket_fd >= 0)
1124 return 0;
1125
1126 /* Notifies all our sockets when we die */
1127 if ((r = service_get_sockets(s, &set)) < 0)
1128 return r;
1129
1130 SET_FOREACH(sock, set, i)
1131 socket_notify_service_dead(sock);
1132
1133 set_free(set);
1134
1135 return 0;
1136 }
1137
1138 static void service_set_state(Service *s, ServiceState state) {
1139 ServiceState old_state;
1140 assert(s);
1141
1142 old_state = s->state;
1143 s->state = state;
1144
1145 if (state != SERVICE_START_PRE &&
1146 state != SERVICE_START &&
1147 state != SERVICE_START_POST &&
1148 state != SERVICE_RELOAD &&
1149 state != SERVICE_STOP &&
1150 state != SERVICE_STOP_SIGTERM &&
1151 state != SERVICE_STOP_SIGKILL &&
1152 state != SERVICE_STOP_POST &&
1153 state != SERVICE_FINAL_SIGTERM &&
1154 state != SERVICE_FINAL_SIGKILL &&
1155 state != SERVICE_AUTO_RESTART)
1156 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1157
1158 if (state != SERVICE_START &&
1159 state != SERVICE_START_POST &&
1160 state != SERVICE_RUNNING &&
1161 state != SERVICE_RELOAD &&
1162 state != SERVICE_STOP &&
1163 state != SERVICE_STOP_SIGTERM &&
1164 state != SERVICE_STOP_SIGKILL)
1165 service_unwatch_main_pid(s);
1166
1167 if (state != SERVICE_START_PRE &&
1168 state != SERVICE_START &&
1169 state != SERVICE_START_POST &&
1170 state != SERVICE_RELOAD &&
1171 state != SERVICE_STOP &&
1172 state != SERVICE_STOP_SIGTERM &&
1173 state != SERVICE_STOP_SIGKILL &&
1174 state != SERVICE_STOP_POST &&
1175 state != SERVICE_FINAL_SIGTERM &&
1176 state != SERVICE_FINAL_SIGKILL) {
1177 service_unwatch_control_pid(s);
1178 s->control_command = NULL;
1179 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
1180 }
1181
1182 if (state == SERVICE_DEAD ||
1183 state == SERVICE_STOP ||
1184 state == SERVICE_STOP_SIGTERM ||
1185 state == SERVICE_STOP_SIGKILL ||
1186 state == SERVICE_STOP_POST ||
1187 state == SERVICE_FINAL_SIGTERM ||
1188 state == SERVICE_FINAL_SIGKILL ||
1189 state == SERVICE_FAILED ||
1190 state == SERVICE_AUTO_RESTART)
1191 service_notify_sockets_dead(s);
1192
1193 if (state != SERVICE_START_PRE &&
1194 state != SERVICE_START &&
1195 state != SERVICE_START_POST &&
1196 state != SERVICE_RUNNING &&
1197 state != SERVICE_RELOAD &&
1198 state != SERVICE_STOP &&
1199 state != SERVICE_STOP_SIGTERM &&
1200 state != SERVICE_STOP_SIGKILL &&
1201 state != SERVICE_STOP_POST &&
1202 state != SERVICE_FINAL_SIGTERM &&
1203 state != SERVICE_FINAL_SIGKILL &&
1204 !(state == SERVICE_DEAD && s->meta.job)) {
1205 service_close_socket_fd(s);
1206 service_connection_unref(s);
1207 }
1208
1209 /* For the inactive states unit_notify() will trim the cgroup,
1210 * but for exit we have to do that ourselves... */
1211 if (state == SERVICE_EXITED)
1212 cgroup_bonding_trim_list(s->meta.cgroup_bondings, true);
1213
1214 if (old_state != state)
1215 log_debug("%s changed %s -> %s", s->meta.id, service_state_to_string(old_state), service_state_to_string(state));
1216
1217 unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]);
1218 }
1219
1220 static int service_coldplug(Unit *u) {
1221 Service *s = SERVICE(u);
1222 int r;
1223
1224 assert(s);
1225 assert(s->state == SERVICE_DEAD);
1226
1227 if (s->deserialized_state != s->state) {
1228
1229 if (s->deserialized_state == SERVICE_START_PRE ||
1230 s->deserialized_state == SERVICE_START ||
1231 s->deserialized_state == SERVICE_START_POST ||
1232 s->deserialized_state == SERVICE_RELOAD ||
1233 s->deserialized_state == SERVICE_STOP ||
1234 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1235 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1236 s->deserialized_state == SERVICE_STOP_POST ||
1237 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1238 s->deserialized_state == SERVICE_FINAL_SIGKILL ||
1239 s->deserialized_state == SERVICE_AUTO_RESTART) {
1240
1241 if (s->deserialized_state == SERVICE_AUTO_RESTART || s->timeout_usec > 0) {
1242 usec_t k;
1243
1244 k = s->deserialized_state == SERVICE_AUTO_RESTART ? s->restart_usec : s->timeout_usec;
1245
1246 if ((r = unit_watch_timer(UNIT(s), k, &s->timer_watch)) < 0)
1247 return r;
1248 }
1249 }
1250
1251 if ((s->deserialized_state == SERVICE_START &&
1252 (s->type == SERVICE_FORKING ||
1253 s->type == SERVICE_DBUS ||
1254 s->type == SERVICE_ONESHOT ||
1255 s->type == SERVICE_NOTIFY)) ||
1256 s->deserialized_state == SERVICE_START_POST ||
1257 s->deserialized_state == SERVICE_RUNNING ||
1258 s->deserialized_state == SERVICE_RELOAD ||
1259 s->deserialized_state == SERVICE_STOP ||
1260 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1261 s->deserialized_state == SERVICE_STOP_SIGKILL)
1262 if (s->main_pid > 0)
1263 if ((r = unit_watch_pid(UNIT(s), s->main_pid)) < 0)
1264 return r;
1265
1266 if (s->deserialized_state == SERVICE_START_PRE ||
1267 s->deserialized_state == SERVICE_START ||
1268 s->deserialized_state == SERVICE_START_POST ||
1269 s->deserialized_state == SERVICE_RELOAD ||
1270 s->deserialized_state == SERVICE_STOP ||
1271 s->deserialized_state == SERVICE_STOP_SIGTERM ||
1272 s->deserialized_state == SERVICE_STOP_SIGKILL ||
1273 s->deserialized_state == SERVICE_STOP_POST ||
1274 s->deserialized_state == SERVICE_FINAL_SIGTERM ||
1275 s->deserialized_state == SERVICE_FINAL_SIGKILL)
1276 if (s->control_pid > 0)
1277 if ((r = unit_watch_pid(UNIT(s), s->control_pid)) < 0)
1278 return r;
1279
1280 service_set_state(s, s->deserialized_state);
1281 }
1282
1283 return 0;
1284 }
1285
1286 static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) {
1287 Iterator i;
1288 int r;
1289 int *rfds = NULL;
1290 unsigned rn_fds = 0;
1291 Set *set;
1292 Socket *sock;
1293
1294 assert(s);
1295 assert(fds);
1296 assert(n_fds);
1297
1298 if (s->socket_fd >= 0)
1299 return 0;
1300
1301 if ((r = service_get_sockets(s, &set)) < 0)
1302 return r;
1303
1304 SET_FOREACH(sock, set, i) {
1305 int *cfds;
1306 unsigned cn_fds;
1307
1308 if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0)
1309 goto fail;
1310
1311 if (!cfds)
1312 continue;
1313
1314 if (!rfds) {
1315 rfds = cfds;
1316 rn_fds = cn_fds;
1317 } else {
1318 int *t;
1319
1320 if (!(t = new(int, rn_fds+cn_fds))) {
1321 free(cfds);
1322 r = -ENOMEM;
1323 goto fail;
1324 }
1325
1326 memcpy(t, rfds, rn_fds);
1327 memcpy(t+rn_fds, cfds, cn_fds);
1328 free(rfds);
1329 free(cfds);
1330
1331 rfds = t;
1332 rn_fds = rn_fds+cn_fds;
1333 }
1334 }
1335
1336 *fds = rfds;
1337 *n_fds = rn_fds;
1338
1339 set_free(set);
1340
1341 return 0;
1342
1343 fail:
1344 set_free(set);
1345 free(rfds);
1346
1347 return r;
1348 }
1349
1350 static int service_spawn(
1351 Service *s,
1352 ExecCommand *c,
1353 bool timeout,
1354 bool pass_fds,
1355 bool apply_permissions,
1356 bool apply_chroot,
1357 bool apply_tty_stdin,
1358 bool set_notify_socket,
1359 pid_t *_pid) {
1360
1361 pid_t pid;
1362 int r;
1363 int *fds = NULL, *fdsbuf = NULL;
1364 unsigned n_fds = 0, n_env = 0;
1365 char **argv = NULL, **final_env = NULL, **our_env = NULL;
1366
1367 assert(s);
1368 assert(c);
1369 assert(_pid);
1370
1371 if (pass_fds ||
1372 s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1373 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1374 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) {
1375
1376 if (s->socket_fd >= 0) {
1377 fds = &s->socket_fd;
1378 n_fds = 1;
1379 } else {
1380 if ((r = service_collect_fds(s, &fdsbuf, &n_fds)) < 0)
1381 goto fail;
1382
1383 fds = fdsbuf;
1384 }
1385 }
1386
1387 if (timeout && s->timeout_usec) {
1388 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1389 goto fail;
1390 } else
1391 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1392
1393 if (!(argv = unit_full_printf_strv(UNIT(s), c->argv))) {
1394 r = -ENOMEM;
1395 goto fail;
1396 }
1397
1398 if (!(our_env = new0(char*, 3))) {
1399 r = -ENOMEM;
1400 goto fail;
1401 }
1402
1403 if (set_notify_socket)
1404 if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=@%s", s->meta.manager->notify_socket) < 0) {
1405 r = -ENOMEM;
1406 goto fail;
1407 }
1408
1409 if (s->main_pid > 0)
1410 if (asprintf(our_env + n_env++, "MAINPID=%lu", (unsigned long) s->main_pid) < 0) {
1411 r = -ENOMEM;
1412 goto fail;
1413 }
1414
1415 if (!(final_env = strv_env_merge(2,
1416 s->meta.manager->environment,
1417 our_env,
1418 NULL))) {
1419 r = -ENOMEM;
1420 goto fail;
1421 }
1422
1423 r = exec_spawn(c,
1424 argv,
1425 &s->exec_context,
1426 fds, n_fds,
1427 final_env,
1428 apply_permissions,
1429 apply_chroot,
1430 apply_tty_stdin,
1431 s->meta.manager->confirm_spawn,
1432 s->meta.cgroup_bondings,
1433 &pid);
1434
1435 if (r < 0)
1436 goto fail;
1437
1438
1439 if ((r = unit_watch_pid(UNIT(s), pid)) < 0)
1440 /* FIXME: we need to do something here */
1441 goto fail;
1442
1443 free(fdsbuf);
1444 strv_free(argv);
1445 strv_free(our_env);
1446 strv_free(final_env);
1447
1448 *_pid = pid;
1449
1450 return 0;
1451
1452 fail:
1453 free(fdsbuf);
1454 strv_free(argv);
1455 strv_free(our_env);
1456 strv_free(final_env);
1457
1458 if (timeout)
1459 unit_unwatch_timer(UNIT(s), &s->timer_watch);
1460
1461 return r;
1462 }
1463
1464 static int main_pid_good(Service *s) {
1465 assert(s);
1466
1467 /* Returns 0 if the pid is dead, 1 if it is good, -1 if we
1468 * don't know */
1469
1470 /* If we know the pid file, then lets just check if it is
1471 * still valid */
1472 if (s->main_pid_known)
1473 return s->main_pid > 0;
1474
1475 /* We don't know the pid */
1476 return -EAGAIN;
1477 }
1478
1479 static int control_pid_good(Service *s) {
1480 assert(s);
1481
1482 return s->control_pid > 0;
1483 }
1484
1485 static int cgroup_good(Service *s) {
1486 int r;
1487
1488 assert(s);
1489
1490 if ((r = cgroup_bonding_is_empty_list(s->meta.cgroup_bondings)) < 0)
1491 return r;
1492
1493 return !r;
1494 }
1495
1496 static void service_enter_dead(Service *s, bool success, bool allow_restart) {
1497 int r;
1498 assert(s);
1499
1500 if (!success)
1501 s->failure = true;
1502
1503 if (allow_restart &&
1504 !s->forbid_restart &&
1505 (s->restart == SERVICE_RESTART_ALWAYS ||
1506 (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) {
1507
1508 if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0)
1509 goto fail;
1510
1511 service_set_state(s, SERVICE_AUTO_RESTART);
1512 } else
1513 service_set_state(s, s->failure ? SERVICE_FAILED : SERVICE_DEAD);
1514
1515 s->forbid_restart = false;
1516
1517 return;
1518
1519 fail:
1520 log_warning("%s failed to run install restart timer: %s", s->meta.id, strerror(-r));
1521 service_enter_dead(s, false, false);
1522 }
1523
1524 static void service_enter_signal(Service *s, ServiceState state, bool success);
1525
1526 static void service_enter_stop_post(Service *s, bool success) {
1527 int r;
1528 assert(s);
1529
1530 if (!success)
1531 s->failure = true;
1532
1533 service_unwatch_control_pid(s);
1534
1535 s->control_command_id = SERVICE_EXEC_STOP_POST;
1536 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) {
1537 if ((r = service_spawn(s,
1538 s->control_command,
1539 true,
1540 false,
1541 !s->permissions_start_only,
1542 !s->root_directory_start_only,
1543 true,
1544 false,
1545 &s->control_pid)) < 0)
1546 goto fail;
1547
1548
1549 service_set_state(s, SERVICE_STOP_POST);
1550 } else
1551 service_enter_signal(s, SERVICE_FINAL_SIGTERM, true);
1552
1553 return;
1554
1555 fail:
1556 log_warning("%s failed to run 'stop-post' task: %s", s->meta.id, strerror(-r));
1557 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1558 }
1559
1560 static void service_enter_signal(Service *s, ServiceState state, bool success) {
1561 int r;
1562 Set *pid_set = NULL;
1563 bool wait_for_exit = false;
1564
1565 assert(s);
1566
1567 if (!success)
1568 s->failure = true;
1569
1570 if (s->exec_context.kill_mode != KILL_NONE) {
1571 int sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? s->exec_context.kill_signal : SIGKILL;
1572
1573 if (s->main_pid > 0) {
1574 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1575 -s->main_pid :
1576 s->main_pid, sig) < 0 && errno != ESRCH)
1577
1578 log_warning("Failed to kill main process %li: %m", (long) s->main_pid);
1579 else
1580 wait_for_exit = true;
1581 }
1582
1583 if (s->control_pid > 0) {
1584 if (kill(s->exec_context.kill_mode == KILL_PROCESS_GROUP ?
1585 -s->control_pid :
1586 s->control_pid, sig) < 0 && errno != ESRCH)
1587
1588 log_warning("Failed to kill control process %li: %m", (long) s->control_pid);
1589 else
1590 wait_for_exit = true;
1591 }
1592
1593 if (s->exec_context.kill_mode == KILL_CONTROL_GROUP) {
1594
1595 if (!(pid_set = set_new(trivial_hash_func, trivial_compare_func))) {
1596 r = -ENOMEM;
1597 goto fail;
1598 }
1599
1600 /* Exclude the main/control pids from being killed via the cgroup */
1601 if (s->main_pid > 0)
1602 if ((r = set_put(pid_set, LONG_TO_PTR(s->main_pid))) < 0)
1603 goto fail;
1604
1605 if (s->control_pid > 0)
1606 if ((r = set_put(pid_set, LONG_TO_PTR(s->control_pid))) < 0)
1607 goto fail;
1608
1609 if ((r = cgroup_bonding_kill_list(s->meta.cgroup_bondings, sig, pid_set)) < 0) {
1610 if (r != -EAGAIN && r != -ESRCH && r != -ENOENT)
1611 log_warning("Failed to kill control group: %s", strerror(-r));
1612 } else if (r > 0)
1613 wait_for_exit = true;
1614
1615 set_free(pid_set);
1616 }
1617 }
1618
1619 if (wait_for_exit) {
1620 if (s->timeout_usec > 0)
1621 if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0)
1622 goto fail;
1623
1624 service_set_state(s, state);
1625 } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1626 service_enter_stop_post(s, true);
1627 else
1628 service_enter_dead(s, true, true);
1629
1630 return;
1631
1632 fail:
1633 log_warning("%s failed to kill processes: %s", s->meta.id, strerror(-r));
1634
1635 if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL)
1636 service_enter_stop_post(s, false);
1637 else
1638 service_enter_dead(s, false, true);
1639
1640 if (pid_set)
1641 set_free(pid_set);
1642 }
1643
1644 static void service_enter_stop(Service *s, bool success) {
1645 int r;
1646
1647 assert(s);
1648
1649 if (!success)
1650 s->failure = true;
1651
1652 service_unwatch_control_pid(s);
1653
1654 s->control_command_id = SERVICE_EXEC_STOP;
1655 if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) {
1656 if ((r = service_spawn(s,
1657 s->control_command,
1658 true,
1659 false,
1660 !s->permissions_start_only,
1661 !s->root_directory_start_only,
1662 false,
1663 false,
1664 &s->control_pid)) < 0)
1665 goto fail;
1666
1667 service_set_state(s, SERVICE_STOP);
1668 } else
1669 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
1670
1671 return;
1672
1673 fail:
1674 log_warning("%s failed to run 'stop' task: %s", s->meta.id, strerror(-r));
1675 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1676 }
1677
1678 static void service_enter_running(Service *s, bool success) {
1679 int main_pid_ok, cgroup_ok;
1680 assert(s);
1681
1682 if (!success)
1683 s->failure = true;
1684
1685 main_pid_ok = main_pid_good(s);
1686 cgroup_ok = cgroup_good(s);
1687
1688 if ((main_pid_ok > 0 || (main_pid_ok < 0 && cgroup_ok != 0)) &&
1689 (s->bus_name_good || s->type != SERVICE_DBUS))
1690 service_set_state(s, SERVICE_RUNNING);
1691 else if (s->remain_after_exit)
1692 service_set_state(s, SERVICE_EXITED);
1693 else
1694 service_enter_stop(s, true);
1695 }
1696
1697 static void service_enter_start_post(Service *s) {
1698 int r;
1699 assert(s);
1700
1701 service_unwatch_control_pid(s);
1702
1703 s->control_command_id = SERVICE_EXEC_START_POST;
1704 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) {
1705 if ((r = service_spawn(s,
1706 s->control_command,
1707 true,
1708 false,
1709 !s->permissions_start_only,
1710 !s->root_directory_start_only,
1711 false,
1712 false,
1713 &s->control_pid)) < 0)
1714 goto fail;
1715
1716 service_set_state(s, SERVICE_START_POST);
1717 } else
1718 service_enter_running(s, true);
1719
1720 return;
1721
1722 fail:
1723 log_warning("%s failed to run 'start-post' task: %s", s->meta.id, strerror(-r));
1724 service_enter_stop(s, false);
1725 }
1726
1727 static void service_enter_start(Service *s) {
1728 pid_t pid;
1729 int r;
1730
1731 assert(s);
1732
1733 assert(s->exec_command[SERVICE_EXEC_START]);
1734 assert(!s->exec_command[SERVICE_EXEC_START]->command_next || s->type == SERVICE_ONESHOT);
1735
1736 if (s->type == SERVICE_FORKING)
1737 service_unwatch_control_pid(s);
1738 else
1739 service_unwatch_main_pid(s);
1740
1741 s->control_command_id = SERVICE_EXEC_START;
1742 s->control_command = s->exec_command[SERVICE_EXEC_START];
1743
1744 if ((r = service_spawn(s,
1745 s->control_command,
1746 s->type == SERVICE_FORKING || s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY,
1747 true,
1748 true,
1749 true,
1750 true,
1751 s->notify_access != NOTIFY_NONE,
1752 &pid)) < 0)
1753 goto fail;
1754
1755 if (s->type == SERVICE_SIMPLE) {
1756 /* For simple services we immediately start
1757 * the START_POST binaries. */
1758
1759 service_set_main_pid(s, pid);
1760 service_enter_start_post(s);
1761
1762 } else if (s->type == SERVICE_FORKING) {
1763
1764 /* For forking services we wait until the start
1765 * process exited. */
1766
1767 s->control_pid = pid;
1768 service_set_state(s, SERVICE_START);
1769
1770 } else if (s->type == SERVICE_ONESHOT ||
1771 s->type == SERVICE_DBUS ||
1772 s->type == SERVICE_NOTIFY) {
1773
1774 /* For oneshot services we wait until the start
1775 * process exited, too, but it is our main process. */
1776
1777 /* For D-Bus services we know the main pid right away,
1778 * but wait for the bus name to appear on the
1779 * bus. Notify services are similar. */
1780
1781 service_set_main_pid(s, pid);
1782 service_set_state(s, SERVICE_START);
1783 } else
1784 assert_not_reached("Unknown service type");
1785
1786 return;
1787
1788 fail:
1789 log_warning("%s failed to run 'start' task: %s", s->meta.id, strerror(-r));
1790 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1791 }
1792
1793 static void service_enter_start_pre(Service *s) {
1794 int r;
1795
1796 assert(s);
1797
1798 service_unwatch_control_pid(s);
1799
1800 s->control_command_id = SERVICE_EXEC_START_PRE;
1801 if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) {
1802 if ((r = service_spawn(s,
1803 s->control_command,
1804 true,
1805 false,
1806 !s->permissions_start_only,
1807 !s->root_directory_start_only,
1808 true,
1809 false,
1810 &s->control_pid)) < 0)
1811 goto fail;
1812
1813 service_set_state(s, SERVICE_START_PRE);
1814 } else
1815 service_enter_start(s);
1816
1817 return;
1818
1819 fail:
1820 log_warning("%s failed to run 'start-pre' task: %s", s->meta.id, strerror(-r));
1821 service_enter_dead(s, false, true);
1822 }
1823
1824 static void service_enter_restart(Service *s) {
1825 int r;
1826 DBusError error;
1827
1828 assert(s);
1829 dbus_error_init(&error);
1830
1831 service_enter_dead(s, true, false);
1832
1833 if ((r = manager_add_job(s->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, &error, NULL)) < 0)
1834 goto fail;
1835
1836 log_debug("%s scheduled restart job.", s->meta.id);
1837 return;
1838
1839 fail:
1840 log_warning("%s failed to schedule restart job: %s", s->meta.id, bus_error(&error, -r));
1841 service_enter_dead(s, false, false);
1842
1843 dbus_error_free(&error);
1844 }
1845
1846 static void service_enter_reload(Service *s) {
1847 int r;
1848
1849 assert(s);
1850
1851 service_unwatch_control_pid(s);
1852
1853 s->control_command_id = SERVICE_EXEC_RELOAD;
1854 if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) {
1855 if ((r = service_spawn(s,
1856 s->control_command,
1857 true,
1858 false,
1859 !s->permissions_start_only,
1860 !s->root_directory_start_only,
1861 false,
1862 false,
1863 &s->control_pid)) < 0)
1864 goto fail;
1865
1866 service_set_state(s, SERVICE_RELOAD);
1867 } else
1868 service_enter_running(s, true);
1869
1870 return;
1871
1872 fail:
1873 log_warning("%s failed to run 'reload' task: %s", s->meta.id, strerror(-r));
1874 service_enter_stop(s, false);
1875 }
1876
1877 static void service_run_next_control(Service *s, bool success) {
1878 int r;
1879
1880 assert(s);
1881 assert(s->control_command);
1882 assert(s->control_command->command_next);
1883
1884 if (!success)
1885 s->failure = true;
1886
1887 assert(s->control_command_id != SERVICE_EXEC_START);
1888
1889 s->control_command = s->control_command->command_next;
1890 service_unwatch_control_pid(s);
1891
1892 if ((r = service_spawn(s,
1893 s->control_command,
1894 true,
1895 false,
1896 !s->permissions_start_only,
1897 !s->root_directory_start_only,
1898 s->control_command_id == SERVICE_EXEC_START_PRE ||
1899 s->control_command_id == SERVICE_EXEC_STOP_POST,
1900 false,
1901 &s->control_pid)) < 0)
1902 goto fail;
1903
1904 return;
1905
1906 fail:
1907 log_warning("%s failed to run next control task: %s", s->meta.id, strerror(-r));
1908
1909 if (s->state == SERVICE_START_PRE)
1910 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
1911 else if (s->state == SERVICE_STOP)
1912 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
1913 else if (s->state == SERVICE_STOP_POST)
1914 service_enter_dead(s, false, true);
1915 else
1916 service_enter_stop(s, false);
1917 }
1918
1919 static void service_run_next_main(Service *s, bool success) {
1920 pid_t pid;
1921 int r;
1922
1923 assert(s);
1924 assert(s->control_command);
1925 assert(s->control_command->command_next);
1926
1927 if (!success)
1928 s->failure = true;
1929
1930 assert(s->control_command_id == SERVICE_EXEC_START);
1931 assert(s->type == SERVICE_ONESHOT);
1932
1933 s->control_command = s->control_command->command_next;
1934 service_unwatch_main_pid(s);
1935
1936 if ((r = service_spawn(s,
1937 s->control_command,
1938 false,
1939 true,
1940 true,
1941 true,
1942 true,
1943 s->notify_access != NOTIFY_NONE,
1944 &pid)) < 0)
1945 goto fail;
1946
1947 service_set_main_pid(s, pid);
1948
1949 return;
1950
1951 fail:
1952 log_warning("%s failed to run next main task: %s", s->meta.id, strerror(-r));
1953 service_enter_stop(s, false);
1954 }
1955
1956 static int service_start(Unit *u) {
1957 Service *s = SERVICE(u);
1958
1959 assert(s);
1960
1961 /* We cannot fulfill this request right now, try again later
1962 * please! */
1963 if (s->state == SERVICE_STOP ||
1964 s->state == SERVICE_STOP_SIGTERM ||
1965 s->state == SERVICE_STOP_SIGKILL ||
1966 s->state == SERVICE_STOP_POST ||
1967 s->state == SERVICE_FINAL_SIGTERM ||
1968 s->state == SERVICE_FINAL_SIGKILL)
1969 return -EAGAIN;
1970
1971 /* Already on it! */
1972 if (s->state == SERVICE_START_PRE ||
1973 s->state == SERVICE_START ||
1974 s->state == SERVICE_START_POST)
1975 return 0;
1976
1977 assert(s->state == SERVICE_DEAD || s->state == SERVICE_FAILED || s->state == SERVICE_AUTO_RESTART);
1978
1979 /* Make sure we don't enter a busy loop of some kind. */
1980 if (!ratelimit_test(&s->ratelimit)) {
1981 log_warning("%s start request repeated too quickly, refusing to start.", u->meta.id);
1982 return -ECANCELED;
1983 }
1984
1985 if ((s->exec_context.std_input == EXEC_INPUT_SOCKET ||
1986 s->exec_context.std_output == EXEC_OUTPUT_SOCKET ||
1987 s->exec_context.std_error == EXEC_OUTPUT_SOCKET) &&
1988 s->socket_fd < 0) {
1989 log_warning("%s can only be started with a per-connection socket.", u->meta.id);
1990 return -EINVAL;
1991 }
1992
1993 s->failure = false;
1994 s->main_pid_known = false;
1995 s->forbid_restart = false;
1996
1997 service_enter_start_pre(s);
1998 return 0;
1999 }
2000
2001 static int service_stop(Unit *u) {
2002 Service *s = SERVICE(u);
2003
2004 assert(s);
2005
2006 /* This is a user request, so don't do restarts on this
2007 * shutdown. */
2008 s->forbid_restart = true;
2009
2010 /* Already on it */
2011 if (s->state == SERVICE_STOP ||
2012 s->state == SERVICE_STOP_SIGTERM ||
2013 s->state == SERVICE_STOP_SIGKILL ||
2014 s->state == SERVICE_STOP_POST ||
2015 s->state == SERVICE_FINAL_SIGTERM ||
2016 s->state == SERVICE_FINAL_SIGKILL)
2017 return 0;
2018
2019 /* Don't allow a restart */
2020 if (s->state == SERVICE_AUTO_RESTART) {
2021 service_set_state(s, SERVICE_DEAD);
2022 return 0;
2023 }
2024
2025 /* If there's already something running we go directly into
2026 * kill mode. */
2027 if (s->state == SERVICE_START_PRE ||
2028 s->state == SERVICE_START ||
2029 s->state == SERVICE_START_POST ||
2030 s->state == SERVICE_RELOAD) {
2031 service_enter_signal(s, SERVICE_STOP_SIGTERM, true);
2032 return 0;
2033 }
2034
2035 assert(s->state == SERVICE_RUNNING ||
2036 s->state == SERVICE_EXITED);
2037
2038 service_enter_stop(s, true);
2039 return 0;
2040 }
2041
2042 static int service_reload(Unit *u) {
2043 Service *s = SERVICE(u);
2044
2045 assert(s);
2046
2047 assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED);
2048
2049 service_enter_reload(s);
2050 return 0;
2051 }
2052
2053 static bool service_can_reload(Unit *u) {
2054 Service *s = SERVICE(u);
2055
2056 assert(s);
2057
2058 return !!s->exec_command[SERVICE_EXEC_RELOAD];
2059 }
2060
2061 static int service_serialize(Unit *u, FILE *f, FDSet *fds) {
2062 Service *s = SERVICE(u);
2063
2064 assert(u);
2065 assert(f);
2066 assert(fds);
2067
2068 unit_serialize_item(u, f, "state", service_state_to_string(s->state));
2069 unit_serialize_item(u, f, "failure", yes_no(s->failure));
2070
2071 if (s->control_pid > 0)
2072 unit_serialize_item_format(u, f, "control-pid", "%lu", (unsigned long) s->control_pid);
2073
2074 if (s->main_pid_known && s->main_pid > 0)
2075 unit_serialize_item_format(u, f, "main-pid", "%lu", (unsigned long) s->main_pid);
2076
2077 unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known));
2078
2079 if (s->status_text)
2080 unit_serialize_item(u, f, "status-text", s->status_text);
2081
2082 /* There's a minor uncleanliness here: if there are multiple
2083 * commands attached here, we will start from the first one
2084 * again */
2085 if (s->control_command_id >= 0)
2086 unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id));
2087
2088 if (s->socket_fd >= 0) {
2089 int copy;
2090
2091 if ((copy = fdset_put_dup(fds, s->socket_fd)) < 0)
2092 return copy;
2093
2094 unit_serialize_item_format(u, f, "socket-fd", "%i", copy);
2095 }
2096
2097 if (s->main_exec_status.pid > 0) {
2098 unit_serialize_item_format(u, f, "main-exec-status-pid", "%lu", (unsigned long) s->main_exec_status.pid);
2099
2100 if (s->main_exec_status.start_timestamp.realtime > 0) {
2101 unit_serialize_item_format(u, f, "main-exec-status-start-realtime",
2102 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.realtime);
2103
2104 unit_serialize_item_format(u, f, "main-exec-status-start-monotonic",
2105 "%llu", (unsigned long long) s->main_exec_status.start_timestamp.monotonic);
2106 }
2107
2108 if (s->main_exec_status.exit_timestamp.realtime > 0) {
2109 unit_serialize_item_format(u, f, "main-exec-status-exit-realtime",
2110 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.realtime);
2111 unit_serialize_item_format(u, f, "main-exec-status-exit-monotonic",
2112 "%llu", (unsigned long long) s->main_exec_status.exit_timestamp.monotonic);
2113
2114 unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code);
2115 unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status);
2116 }
2117 }
2118
2119 return 0;
2120 }
2121
2122 static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) {
2123 Service *s = SERVICE(u);
2124
2125 assert(u);
2126 assert(key);
2127 assert(value);
2128 assert(fds);
2129
2130 if (streq(key, "state")) {
2131 ServiceState state;
2132
2133 if ((state = service_state_from_string(value)) < 0)
2134 log_debug("Failed to parse state value %s", value);
2135 else
2136 s->deserialized_state = state;
2137 } else if (streq(key, "failure")) {
2138 int b;
2139
2140 if ((b = parse_boolean(value)) < 0)
2141 log_debug("Failed to parse failure value %s", value);
2142 else
2143 s->failure = b || s->failure;
2144 } else if (streq(key, "control-pid")) {
2145 pid_t pid;
2146
2147 if (parse_pid(value, &pid) < 0)
2148 log_debug("Failed to parse control-pid value %s", value);
2149 else
2150 s->control_pid = pid;
2151 } else if (streq(key, "main-pid")) {
2152 pid_t pid;
2153
2154 if (parse_pid(value, &pid) < 0)
2155 log_debug("Failed to parse main-pid value %s", value);
2156 else
2157 service_set_main_pid(s, (pid_t) pid);
2158 } else if (streq(key, "main-pid-known")) {
2159 int b;
2160
2161 if ((b = parse_boolean(value)) < 0)
2162 log_debug("Failed to parse main-pid-known value %s", value);
2163 else
2164 s->main_pid_known = b;
2165 } else if (streq(key, "status-text")) {
2166 char *t;
2167
2168 if ((t = strdup(value))) {
2169 free(s->status_text);
2170 s->status_text = t;
2171 }
2172
2173 } else if (streq(key, "control-command")) {
2174 ServiceExecCommand id;
2175
2176 if ((id = service_exec_command_from_string(value)) < 0)
2177 log_debug("Failed to parse exec-command value %s", value);
2178 else {
2179 s->control_command_id = id;
2180 s->control_command = s->exec_command[id];
2181 }
2182 } else if (streq(key, "socket-fd")) {
2183 int fd;
2184
2185 if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd))
2186 log_debug("Failed to parse socket-fd value %s", value);
2187 else {
2188
2189 if (s->socket_fd >= 0)
2190 close_nointr_nofail(s->socket_fd);
2191 s->socket_fd = fdset_remove(fds, fd);
2192 }
2193 } else if (streq(key, "main-exec-status-pid")) {
2194 pid_t pid;
2195
2196 if (parse_pid(value, &pid) < 0)
2197 log_debug("Failed to parse main-exec-status-pid value %s", value);
2198 else
2199 s->main_exec_status.pid = pid;
2200 } else if (streq(key, "main-exec-status-code")) {
2201 int i;
2202
2203 if (safe_atoi(value, &i) < 0)
2204 log_debug("Failed to parse main-exec-status-code value %s", value);
2205 else
2206 s->main_exec_status.code = i;
2207 } else if (streq(key, "main-exec-status-status")) {
2208 int i;
2209
2210 if (safe_atoi(value, &i) < 0)
2211 log_debug("Failed to parse main-exec-status-status value %s", value);
2212 else
2213 s->main_exec_status.status = i;
2214 } else if (streq(key, "main-exec-status-start-realtime")) {
2215 uint64_t k;
2216
2217 if (safe_atou64(value, &k) < 0)
2218 log_debug("Failed to parse main-exec-status-start-realtime value %s", value);
2219 else
2220 s->main_exec_status.start_timestamp.realtime = (usec_t) k;
2221 } else if (streq(key, "main-exec-status-start-monotonic")) {
2222 uint64_t k;
2223
2224 if (safe_atou64(value, &k) < 0)
2225 log_debug("Failed to parse main-exec-status-start-monotonic value %s", value);
2226 else
2227 s->main_exec_status.start_timestamp.monotonic = (usec_t) k;
2228 } else if (streq(key, "main-exec-status-exit-realtime")) {
2229 uint64_t k;
2230
2231 if (safe_atou64(value, &k) < 0)
2232 log_debug("Failed to parse main-exec-status-exit-realtime value %s", value);
2233 else
2234 s->main_exec_status.exit_timestamp.realtime = (usec_t) k;
2235 } else if (streq(key, "main-exec-status-exit-monotonic")) {
2236 uint64_t k;
2237
2238 if (safe_atou64(value, &k) < 0)
2239 log_debug("Failed to parse main-exec-status-exit-monotonic value %s", value);
2240 else
2241 s->main_exec_status.exit_timestamp.monotonic = (usec_t) k;
2242 } else
2243 log_debug("Unknown serialization key '%s'", key);
2244
2245 return 0;
2246 }
2247
2248 static UnitActiveState service_active_state(Unit *u) {
2249 assert(u);
2250
2251 return state_translation_table[SERVICE(u)->state];
2252 }
2253
2254 static const char *service_sub_state_to_string(Unit *u) {
2255 assert(u);
2256
2257 return service_state_to_string(SERVICE(u)->state);
2258 }
2259
2260 static bool service_check_gc(Unit *u) {
2261 Service *s = SERVICE(u);
2262
2263 assert(s);
2264
2265 return !!s->sysv_path;
2266 }
2267
2268 static bool service_check_snapshot(Unit *u) {
2269 Service *s = SERVICE(u);
2270
2271 assert(s);
2272
2273 return !s->got_socket_fd;
2274 }
2275
2276 static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) {
2277 Service *s = SERVICE(u);
2278 bool success;
2279
2280 assert(s);
2281 assert(pid >= 0);
2282
2283 if (!s->meta.fragment_path)
2284 success = is_clean_exit_lsb(code, status);
2285 else
2286 success = is_clean_exit(code, status);
2287
2288 if (s->main_pid == pid) {
2289
2290 s->main_pid = 0;
2291 exec_status_exit(&s->main_exec_status, pid, code, status);
2292
2293 if (s->type != SERVICE_FORKING && s->control_command) {
2294 s->control_command->exec_status = s->main_exec_status;
2295
2296 if (s->control_command->ignore)
2297 success = true;
2298 }
2299
2300 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2301 "%s: main process exited, code=%s, status=%i", u->meta.id, sigchld_code_to_string(code), status);
2302 s->failure = s->failure || !success;
2303
2304 if (s->control_command &&
2305 s->control_command->command_next &&
2306 success) {
2307
2308 /* There is another command to *
2309 * execute, so let's do that. */
2310
2311 log_debug("%s running next main command for state %s", u->meta.id, service_state_to_string(s->state));
2312 service_run_next_main(s, success);
2313
2314 } else {
2315
2316 /* The service exited, so the service is officially
2317 * gone. */
2318
2319 s->control_command = NULL;
2320 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2321
2322 switch (s->state) {
2323
2324 case SERVICE_START_POST:
2325 case SERVICE_RELOAD:
2326 case SERVICE_STOP:
2327 /* Need to wait until the operation is
2328 * done */
2329 break;
2330
2331 case SERVICE_START:
2332 if (s->type == SERVICE_ONESHOT) {
2333 /* This was our main goal, so let's go on */
2334 if (success)
2335 service_enter_start_post(s);
2336 else
2337 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2338 break;
2339 } else {
2340 assert(s->type == SERVICE_DBUS || s->type == SERVICE_NOTIFY);
2341
2342 /* Fall through */
2343 }
2344
2345 case SERVICE_RUNNING:
2346 service_enter_running(s, success);
2347 break;
2348
2349 case SERVICE_STOP_SIGTERM:
2350 case SERVICE_STOP_SIGKILL:
2351
2352 if (!control_pid_good(s))
2353 service_enter_stop_post(s, success);
2354
2355 /* If there is still a control process, wait for that first */
2356 break;
2357
2358 default:
2359 assert_not_reached("Uh, main process died at wrong time.");
2360 }
2361 }
2362
2363 } else if (s->control_pid == pid) {
2364
2365 s->control_pid = 0;
2366
2367 if (s->control_command) {
2368 exec_status_exit(&s->control_command->exec_status, pid, code, status);
2369
2370 if (s->control_command->ignore)
2371 success = true;
2372 }
2373
2374 log_full(success ? LOG_DEBUG : LOG_NOTICE,
2375 "%s: control process exited, code=%s status=%i", u->meta.id, sigchld_code_to_string(code), status);
2376 s->failure = s->failure || !success;
2377
2378 if (s->control_command &&
2379 s->control_command->command_next &&
2380 success) {
2381
2382 /* There is another command to *
2383 * execute, so let's do that. */
2384
2385 log_debug("%s running next control command for state %s", u->meta.id, service_state_to_string(s->state));
2386 service_run_next_control(s, success);
2387
2388 } else {
2389 /* No further commands for this step, so let's
2390 * figure out what to do next */
2391
2392 s->control_command = NULL;
2393 s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID;
2394
2395 log_debug("%s got final SIGCHLD for state %s", u->meta.id, service_state_to_string(s->state));
2396
2397 switch (s->state) {
2398
2399 case SERVICE_START_PRE:
2400 if (success)
2401 service_enter_start(s);
2402 else
2403 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2404 break;
2405
2406 case SERVICE_START:
2407 assert(s->type == SERVICE_FORKING);
2408
2409 /* Let's try to load the pid
2410 * file here if we can. We
2411 * ignore the return value,
2412 * since the PID file might
2413 * actually be created by a
2414 * START_POST script */
2415
2416 if (success) {
2417 if (s->pid_file)
2418 service_load_pid_file(s);
2419
2420 service_enter_start_post(s);
2421 } else
2422 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2423
2424 break;
2425
2426 case SERVICE_START_POST:
2427 if (success && s->pid_file && !s->main_pid_known) {
2428 int r;
2429
2430 /* Hmm, let's see if we can
2431 * load the pid now after the
2432 * start-post scripts got
2433 * executed. */
2434
2435 if ((r = service_load_pid_file(s)) < 0)
2436 log_warning("%s: failed to load PID file %s: %s", s->meta.id, s->pid_file, strerror(-r));
2437 }
2438
2439 /* Fall through */
2440
2441 case SERVICE_RELOAD:
2442 if (success)
2443 service_enter_running(s, true);
2444 else
2445 service_enter_stop(s, false);
2446
2447 break;
2448
2449 case SERVICE_STOP:
2450 service_enter_signal(s, SERVICE_STOP_SIGTERM, success);
2451 break;
2452
2453 case SERVICE_STOP_SIGTERM:
2454 case SERVICE_STOP_SIGKILL:
2455 if (main_pid_good(s) <= 0)
2456 service_enter_stop_post(s, success);
2457
2458 /* If there is still a service
2459 * process around, wait until
2460 * that one quit, too */
2461 break;
2462
2463 case SERVICE_STOP_POST:
2464 case SERVICE_FINAL_SIGTERM:
2465 case SERVICE_FINAL_SIGKILL:
2466 service_enter_dead(s, success, true);
2467 break;
2468
2469 default:
2470 assert_not_reached("Uh, control process died at wrong time.");
2471 }
2472 }
2473 }
2474
2475 /* Notify clients about changed exit status */
2476 unit_add_to_dbus_queue(u);
2477 }
2478
2479 static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) {
2480 Service *s = SERVICE(u);
2481
2482 assert(s);
2483 assert(elapsed == 1);
2484
2485 assert(w == &s->timer_watch);
2486
2487 switch (s->state) {
2488
2489 case SERVICE_START_PRE:
2490 case SERVICE_START:
2491 log_warning("%s operation timed out. Terminating.", u->meta.id);
2492 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2493 break;
2494
2495 case SERVICE_START_POST:
2496 case SERVICE_RELOAD:
2497 log_warning("%s operation timed out. Stopping.", u->meta.id);
2498 service_enter_stop(s, false);
2499 break;
2500
2501 case SERVICE_STOP:
2502 log_warning("%s stopping timed out. Terminating.", u->meta.id);
2503 service_enter_signal(s, SERVICE_STOP_SIGTERM, false);
2504 break;
2505
2506 case SERVICE_STOP_SIGTERM:
2507 log_warning("%s stopping timed out. Killing.", u->meta.id);
2508 service_enter_signal(s, SERVICE_STOP_SIGKILL, false);
2509 break;
2510
2511 case SERVICE_STOP_SIGKILL:
2512 /* Uh, wie sent a SIGKILL and it is still not gone?
2513 * Must be something we cannot kill, so let's just be
2514 * weirded out and continue */
2515
2516 log_warning("%s still around after SIGKILL. Ignoring.", u->meta.id);
2517 service_enter_stop_post(s, false);
2518 break;
2519
2520 case SERVICE_STOP_POST:
2521 log_warning("%s stopping timed out (2). Terminating.", u->meta.id);
2522 service_enter_signal(s, SERVICE_FINAL_SIGTERM, false);
2523 break;
2524
2525 case SERVICE_FINAL_SIGTERM:
2526 log_warning("%s stopping timed out (2). Killing.", u->meta.id);
2527 service_enter_signal(s, SERVICE_FINAL_SIGKILL, false);
2528 break;
2529
2530 case SERVICE_FINAL_SIGKILL:
2531 log_warning("%s still around after SIGKILL (2). Entering failed mode.", u->meta.id);
2532 service_enter_dead(s, false, true);
2533 break;
2534
2535 case SERVICE_AUTO_RESTART:
2536 log_info("%s holdoff time over, scheduling restart.", u->meta.id);
2537 service_enter_restart(s);
2538 break;
2539
2540 default:
2541 assert_not_reached("Timeout at wrong time.");
2542 }
2543 }
2544
2545 static void service_cgroup_notify_event(Unit *u) {
2546 Service *s = SERVICE(u);
2547
2548 assert(u);
2549
2550 log_debug("%s: cgroup is empty", u->meta.id);
2551
2552 switch (s->state) {
2553
2554 /* Waiting for SIGCHLD is usually more interesting,
2555 * because it includes return codes/signals. Which is
2556 * why we ignore the cgroup events for most cases,
2557 * except when we don't know pid which to expect the
2558 * SIGCHLD for. */
2559
2560 case SERVICE_RUNNING:
2561 service_enter_running(s, true);
2562 break;
2563
2564 case SERVICE_STOP_SIGTERM:
2565 case SERVICE_STOP_SIGKILL:
2566 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2567 service_enter_stop_post(s, true);
2568
2569 break;
2570
2571 case SERVICE_FINAL_SIGTERM:
2572 case SERVICE_FINAL_SIGKILL:
2573 if (main_pid_good(s) <= 0 && !control_pid_good(s))
2574 service_enter_dead(s, true, true);
2575
2576 break;
2577
2578 default:
2579 ;
2580 }
2581 }
2582
2583 static void service_notify_message(Unit *u, pid_t pid, char **tags) {
2584 Service *s = SERVICE(u);
2585 const char *e;
2586
2587 assert(u);
2588
2589 if (s->notify_access == NOTIFY_NONE) {
2590 log_warning("%s: Got notification message from PID %lu, but reception is disabled.",
2591 u->meta.id, (unsigned long) pid);
2592 return;
2593 }
2594
2595 if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) {
2596 log_warning("%s: Got notification message from PID %lu, but reception only permitted for PID %lu",
2597 u->meta.id, (unsigned long) pid, (unsigned long) s->main_pid);
2598 return;
2599 }
2600
2601 log_debug("%s: Got message", u->meta.id);
2602
2603 /* Interpret MAINPID= */
2604 if ((e = strv_find_prefix(tags, "MAINPID=")) &&
2605 (s->state == SERVICE_START ||
2606 s->state == SERVICE_START_POST ||
2607 s->state == SERVICE_RUNNING ||
2608 s->state == SERVICE_RELOAD)) {
2609
2610 if (parse_pid(e + 8, &pid) < 0)
2611 log_warning("Failed to parse notification message %s", e);
2612 else {
2613 log_debug("%s: got %s", u->meta.id, e);
2614 service_set_main_pid(s, pid);
2615 }
2616 }
2617
2618 /* Interpret READY= */
2619 if (s->type == SERVICE_NOTIFY &&
2620 s->state == SERVICE_START &&
2621 strv_find(tags, "READY=1")) {
2622 log_debug("%s: got READY=1", u->meta.id);
2623
2624 service_enter_start_post(s);
2625 }
2626
2627 /* Interpret STATUS= */
2628 if ((e = strv_find_prefix(tags, "STATUS="))) {
2629 char *t;
2630
2631 if (e[7]) {
2632 if (!(t = strdup(e+7))) {
2633 log_error("Failed to allocate string.");
2634 return;
2635 }
2636
2637 log_debug("%s: got %s", u->meta.id, e);
2638
2639 free(s->status_text);
2640 s->status_text = t;
2641 } else {
2642 free(s->status_text);
2643 s->status_text = NULL;
2644 }
2645
2646 }
2647
2648 /* Notify clients about changed status or main pid */
2649 unit_add_to_dbus_queue(u);
2650 }
2651
2652 static int service_enumerate(Manager *m) {
2653 char **p;
2654 unsigned i;
2655 DIR *d = NULL;
2656 char *path = NULL, *fpath = NULL, *name = NULL;
2657 Set *runlevel_services[ELEMENTSOF(rcnd_table)], *shutdown_services = NULL;
2658 Unit *service;
2659 Iterator j;
2660 int r;
2661
2662 assert(m);
2663
2664 zero(runlevel_services);
2665
2666 STRV_FOREACH(p, m->lookup_paths.sysvrcnd_path)
2667 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++) {
2668 struct dirent *de;
2669
2670 free(path);
2671 path = NULL;
2672 if (asprintf(&path, "%s/%s", *p, rcnd_table[i].path) < 0) {
2673 r = -ENOMEM;
2674 goto finish;
2675 }
2676
2677 if (d)
2678 closedir(d);
2679
2680 if (!(d = opendir(path))) {
2681 if (errno != ENOENT)
2682 log_warning("opendir() failed on %s: %s", path, strerror(errno));
2683
2684 continue;
2685 }
2686
2687 while ((de = readdir(d))) {
2688 int a, b;
2689
2690 if (ignore_file(de->d_name))
2691 continue;
2692
2693 if (de->d_name[0] != 'S' && de->d_name[0] != 'K')
2694 continue;
2695
2696 if (strlen(de->d_name) < 4)
2697 continue;
2698
2699 a = undecchar(de->d_name[1]);
2700 b = undecchar(de->d_name[2]);
2701
2702 if (a < 0 || b < 0)
2703 continue;
2704
2705 free(fpath);
2706 fpath = NULL;
2707 if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i].path, de->d_name) < 0) {
2708 r = -ENOMEM;
2709 goto finish;
2710 }
2711
2712 if (access(fpath, X_OK) < 0) {
2713
2714 if (errno != ENOENT)
2715 log_warning("access() failed on %s: %s", fpath, strerror(errno));
2716
2717 continue;
2718 }
2719
2720 free(name);
2721 if (!(name = sysv_translate_name(de->d_name + 3))) {
2722 r = -ENOMEM;
2723 goto finish;
2724 }
2725
2726 if ((r = manager_load_unit_prepare(m, name, NULL, NULL, &service)) < 0) {
2727 log_warning("Failed to prepare unit %s: %s", name, strerror(-r));
2728 continue;
2729 }
2730
2731 if (de->d_name[0] == 'S') {
2732
2733 if (rcnd_table[i].type == RUNLEVEL_UP || rcnd_table[i].type == RUNLEVEL_SYSINIT) {
2734 SERVICE(service)->sysv_start_priority =
2735 MAX(a*10 + b, SERVICE(service)->sysv_start_priority);
2736
2737 SERVICE(service)->sysv_enabled = true;
2738 }
2739
2740 if ((r = set_ensure_allocated(&runlevel_services[i], trivial_hash_func, trivial_compare_func)) < 0)
2741 goto finish;
2742
2743 if ((r = set_put(runlevel_services[i], service)) < 0)
2744 goto finish;
2745
2746 } else if (de->d_name[0] == 'K' &&
2747 (rcnd_table[i].type == RUNLEVEL_DOWN ||
2748 rcnd_table[i].type == RUNLEVEL_SYSINIT)) {
2749
2750 if ((r = set_ensure_allocated(&shutdown_services, trivial_hash_func, trivial_compare_func)) < 0)
2751 goto finish;
2752
2753 if ((r = set_put(shutdown_services, service)) < 0)
2754 goto finish;
2755 }
2756 }
2757 }
2758
2759 /* Now we loaded all stubs and are aware of the lowest
2760 start-up priority for all services, not let's actually load
2761 the services, this will also tell us which services are
2762 actually native now */
2763 manager_dispatch_load_queue(m);
2764
2765 /* If this is a native service, rely on native ways to pull in
2766 * a service, don't pull it in via sysv rcN.d links. */
2767 for (i = 0; i < ELEMENTSOF(rcnd_table); i ++)
2768 SET_FOREACH(service, runlevel_services[i], j) {
2769 service = unit_follow_merge(service);
2770
2771 if (service->meta.fragment_path)
2772 continue;
2773
2774 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_WANTS, rcnd_table[i].target, NULL, true)) < 0)
2775 goto finish;
2776 }
2777
2778 /* We honour K links only for halt/reboot. For the normal
2779 * runlevels we assume the stop jobs will be implicitly added
2780 * by the core logic. Also, we don't really distuingish here
2781 * between the runlevels 0 and 6 and just add them to the
2782 * special shutdown target. On SUSE the boot.d/ runlevel is
2783 * also used for shutdown, so we add links for that too to the
2784 * shutdown target.*/
2785 SET_FOREACH(service, shutdown_services, j) {
2786 service = unit_follow_merge(service);
2787
2788 if (service->meta.fragment_path)
2789 continue;
2790
2791 if ((r = unit_add_two_dependencies_by_name_inverse(service, UNIT_AFTER, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true)) < 0)
2792 goto finish;
2793 }
2794
2795 r = 0;
2796
2797 finish:
2798 free(path);
2799 free(fpath);
2800 free(name);
2801
2802 for (i = 0; i < ELEMENTSOF(rcnd_table); i++)
2803 set_free(runlevel_services[i]);
2804 set_free(shutdown_services);
2805
2806 if (d)
2807 closedir(d);
2808
2809 return r;
2810 }
2811
2812 static void service_bus_name_owner_change(
2813 Unit *u,
2814 const char *name,
2815 const char *old_owner,
2816 const char *new_owner) {
2817
2818 Service *s = SERVICE(u);
2819
2820 assert(s);
2821 assert(name);
2822
2823 assert(streq(s->bus_name, name));
2824 assert(old_owner || new_owner);
2825
2826 if (old_owner && new_owner)
2827 log_debug("%s's D-Bus name %s changed owner from %s to %s", u->meta.id, name, old_owner, new_owner);
2828 else if (old_owner)
2829 log_debug("%s's D-Bus name %s no longer registered by %s", u->meta.id, name, old_owner);
2830 else
2831 log_debug("%s's D-Bus name %s now registered by %s", u->meta.id, name, new_owner);
2832
2833 s->bus_name_good = !!new_owner;
2834
2835 if (s->type == SERVICE_DBUS) {
2836
2837 /* service_enter_running() will figure out what to
2838 * do */
2839 if (s->state == SERVICE_RUNNING)
2840 service_enter_running(s, true);
2841 else if (s->state == SERVICE_START && new_owner)
2842 service_enter_start_post(s);
2843
2844 } else if (new_owner &&
2845 s->main_pid <= 0 &&
2846 (s->state == SERVICE_START ||
2847 s->state == SERVICE_START_POST ||
2848 s->state == SERVICE_RUNNING ||
2849 s->state == SERVICE_RELOAD)) {
2850
2851 /* Try to acquire PID from bus service */
2852 log_debug("Trying to acquire PID from D-Bus name...");
2853
2854 bus_query_pid(u->meta.manager, name);
2855 }
2856 }
2857
2858 static void service_bus_query_pid_done(
2859 Unit *u,
2860 const char *name,
2861 pid_t pid) {
2862
2863 Service *s = SERVICE(u);
2864
2865 assert(s);
2866 assert(name);
2867
2868 log_debug("%s's D-Bus name %s is now owned by process %u", u->meta.id, name, (unsigned) pid);
2869
2870 if (s->main_pid <= 0 &&
2871 (s->state == SERVICE_START ||
2872 s->state == SERVICE_START_POST ||
2873 s->state == SERVICE_RUNNING ||
2874 s->state == SERVICE_RELOAD))
2875 service_set_main_pid(s, pid);
2876 }
2877
2878 int service_set_socket_fd(Service *s, int fd, Socket *sock) {
2879 assert(s);
2880 assert(fd >= 0);
2881
2882 /* This is called by the socket code when instantiating a new
2883 * service for a stream socket and the socket needs to be
2884 * configured. */
2885
2886 if (s->meta.load_state != UNIT_LOADED)
2887 return -EINVAL;
2888
2889 if (s->socket_fd >= 0)
2890 return -EBUSY;
2891
2892 if (s->state != SERVICE_DEAD)
2893 return -EAGAIN;
2894
2895 s->socket_fd = fd;
2896 s->got_socket_fd = true;
2897 s->socket = sock;
2898
2899 return 0;
2900 }
2901
2902 static void service_reset_failed(Unit *u) {
2903 Service *s = SERVICE(u);
2904
2905 assert(s);
2906
2907 if (s->state == SERVICE_FAILED)
2908 service_set_state(s, SERVICE_DEAD);
2909
2910 s->failure = false;
2911 }
2912
2913 static const char* const service_state_table[_SERVICE_STATE_MAX] = {
2914 [SERVICE_DEAD] = "dead",
2915 [SERVICE_START_PRE] = "start-pre",
2916 [SERVICE_START] = "start",
2917 [SERVICE_START_POST] = "start-post",
2918 [SERVICE_RUNNING] = "running",
2919 [SERVICE_EXITED] = "exited",
2920 [SERVICE_RELOAD] = "reload",
2921 [SERVICE_STOP] = "stop",
2922 [SERVICE_STOP_SIGTERM] = "stop-sigterm",
2923 [SERVICE_STOP_SIGKILL] = "stop-sigkill",
2924 [SERVICE_STOP_POST] = "stop-post",
2925 [SERVICE_FINAL_SIGTERM] = "final-sigterm",
2926 [SERVICE_FINAL_SIGKILL] = "final-sigkill",
2927 [SERVICE_FAILED] = "failed",
2928 [SERVICE_AUTO_RESTART] = "auto-restart",
2929 };
2930
2931 DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState);
2932
2933 static const char* const service_restart_table[_SERVICE_RESTART_MAX] = {
2934 [SERVICE_ONCE] = "once",
2935 [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success",
2936 [SERVICE_RESTART_ALWAYS] = "restart-always",
2937 };
2938
2939 DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart);
2940
2941 static const char* const service_type_table[_SERVICE_TYPE_MAX] = {
2942 [SERVICE_SIMPLE] = "simple",
2943 [SERVICE_FORKING] = "forking",
2944 [SERVICE_ONESHOT] = "oneshot",
2945 [SERVICE_DBUS] = "dbus",
2946 [SERVICE_NOTIFY] = "notify"
2947 };
2948
2949 DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType);
2950
2951 static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = {
2952 [SERVICE_EXEC_START_PRE] = "ExecStartPre",
2953 [SERVICE_EXEC_START] = "ExecStart",
2954 [SERVICE_EXEC_START_POST] = "ExecStartPost",
2955 [SERVICE_EXEC_RELOAD] = "ExecReload",
2956 [SERVICE_EXEC_STOP] = "ExecStop",
2957 [SERVICE_EXEC_STOP_POST] = "ExecStopPost",
2958 };
2959
2960 DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand);
2961
2962 static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = {
2963 [NOTIFY_NONE] = "none",
2964 [NOTIFY_MAIN] = "main",
2965 [NOTIFY_ALL] = "all"
2966 };
2967
2968 DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess);
2969
2970 const UnitVTable service_vtable = {
2971 .suffix = ".service",
2972 .show_status = true,
2973
2974 .init = service_init,
2975 .done = service_done,
2976 .load = service_load,
2977
2978 .coldplug = service_coldplug,
2979
2980 .dump = service_dump,
2981
2982 .start = service_start,
2983 .stop = service_stop,
2984 .reload = service_reload,
2985
2986 .can_reload = service_can_reload,
2987
2988 .serialize = service_serialize,
2989 .deserialize_item = service_deserialize_item,
2990
2991 .active_state = service_active_state,
2992 .sub_state_to_string = service_sub_state_to_string,
2993
2994 .check_gc = service_check_gc,
2995 .check_snapshot = service_check_snapshot,
2996
2997 .sigchld_event = service_sigchld_event,
2998 .timer_event = service_timer_event,
2999
3000 .reset_failed = service_reset_failed,
3001
3002 .cgroup_notify_empty = service_cgroup_notify_event,
3003 .notify_message = service_notify_message,
3004
3005 .bus_name_owner_change = service_bus_name_owner_change,
3006 .bus_query_pid_done = service_bus_query_pid_done,
3007
3008 .bus_interface = "org.freedesktop.systemd1.Service",
3009 .bus_message_handler = bus_service_message_handler,
3010 .bus_invalidating_properties = bus_service_invalidating_properties,
3011
3012 .enumerate = service_enumerate
3013 };