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