]>
Commit | Line | Data |
---|---|---|
5cb5a6ff LP |
1 | /*-*- Mode: C; c-basic-offset: 8 -*-*/ |
2 | ||
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
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 | ||
5cb5a6ff | 22 | #include <errno.h> |
034c6ed7 | 23 | #include <signal.h> |
2c4104f0 LP |
24 | #include <dirent.h> |
25 | #include <unistd.h> | |
5cb5a6ff | 26 | |
87f0e418 | 27 | #include "unit.h" |
5cb5a6ff LP |
28 | #include "service.h" |
29 | #include "load-fragment.h" | |
30 | #include "load-dropin.h" | |
034c6ed7 | 31 | #include "log.h" |
2c4104f0 LP |
32 | #include "strv.h" |
33 | ||
34 | #define COMMENTS "#;\n" | |
35 | #define NEWLINES "\n\r" | |
36 | #define LINE_MAX 4096 | |
034c6ed7 | 37 | |
23a177ef | 38 | static const char * const rcnd_table[] = { |
adcbe07c LP |
39 | "/rc0.d", SPECIAL_RUNLEVEL0_TARGET, |
40 | "/rc1.d", SPECIAL_RUNLEVEL1_TARGET, | |
41 | "/rc2.d", SPECIAL_RUNLEVEL2_TARGET, | |
42 | "/rc3.d", SPECIAL_RUNLEVEL3_TARGET, | |
43 | "/rc4.d", SPECIAL_RUNLEVEL4_TARGET, | |
44 | "/rc5.d", SPECIAL_RUNLEVEL5_TARGET, | |
45 | "/rc6.d", SPECIAL_RUNLEVEL6_TARGET, | |
46 | "/boot.d", SPECIAL_BASIC_TARGET | |
23a177ef LP |
47 | }; |
48 | ||
acbb0225 | 49 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
87f0e418 LP |
50 | [SERVICE_DEAD] = UNIT_INACTIVE, |
51 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
52 | [SERVICE_START] = UNIT_ACTIVATING, | |
53 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
54 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
55 | [SERVICE_RELOAD] = UNIT_ACTIVE_RELOADING, | |
56 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
57 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, | |
58 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
59 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
60 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
61 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
62 | [SERVICE_MAINTAINANCE] = UNIT_INACTIVE, | |
63 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING, | |
034c6ed7 | 64 | }; |
5cb5a6ff | 65 | |
87f0e418 LP |
66 | static void service_done(Unit *u) { |
67 | Service *s = SERVICE(u); | |
44d8db9e LP |
68 | |
69 | assert(s); | |
70 | ||
71 | free(s->pid_file); | |
72 | s->pid_file = NULL; | |
73 | ||
2c4104f0 LP |
74 | free(s->sysv_path); |
75 | s->sysv_path = NULL; | |
76 | ||
8309400a LP |
77 | free(s->sysv_runlevels); |
78 | s->sysv_runlevels = NULL; | |
79 | ||
44d8db9e | 80 | exec_context_done(&s->exec_context); |
e537352b | 81 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
44d8db9e LP |
82 | s->control_command = NULL; |
83 | ||
84 | /* This will leak a process, but at least no memory or any of | |
85 | * our resources */ | |
86 | if (s->main_pid > 0) { | |
87f0e418 | 87 | unit_unwatch_pid(u, s->main_pid); |
44d8db9e LP |
88 | s->main_pid = 0; |
89 | } | |
90 | ||
91 | if (s->control_pid > 0) { | |
87f0e418 | 92 | unit_unwatch_pid(u, s->control_pid); |
44d8db9e LP |
93 | s->control_pid = 0; |
94 | } | |
95 | ||
acbb0225 | 96 | unit_unwatch_timer(u, &s->timer_watch); |
44d8db9e LP |
97 | } |
98 | ||
2c4104f0 LP |
99 | static int sysv_translate_name(const char *name, char **_r) { |
100 | ||
101 | static const char * const table[] = { | |
102 | "$local_fs", SPECIAL_LOCAL_FS_TARGET, | |
103 | "$network", SPECIAL_NETWORK_TARGET, | |
104 | "$named", SPECIAL_NSS_LOOKUP_TARGET, | |
105 | "$portmap", SPECIAL_RPCBIND_TARGET, | |
106 | "$remote_fs", SPECIAL_REMOTE_FS_TARGET, | |
107 | "$syslog", SPECIAL_SYSLOG_TARGET, | |
108 | "$time", SPECIAL_RTC_SET_TARGET | |
109 | }; | |
110 | ||
111 | unsigned i; | |
112 | char *r; | |
113 | ||
114 | for (i = 0; i < ELEMENTSOF(table); i += 2) | |
115 | if (streq(table[i], name)) { | |
116 | if (!(r = strdup(table[i+1]))) | |
117 | return -ENOMEM; | |
118 | ||
119 | goto finish; | |
120 | } | |
121 | ||
122 | if (*name == '$') | |
123 | return 0; | |
124 | ||
125 | if (asprintf(&r, "%s.service", name) < 0) | |
126 | return -ENOMEM; | |
127 | ||
128 | finish: | |
129 | ||
130 | if (_r) | |
131 | *_r = r; | |
132 | ||
133 | return 1; | |
134 | } | |
135 | ||
136 | static int sysv_chkconfig_order(Service *s) { | |
137 | Meta *other; | |
138 | int r; | |
139 | ||
140 | assert(s); | |
141 | ||
142 | if (s->sysv_start_priority < 0) | |
143 | return 0; | |
144 | ||
23a177ef LP |
145 | /* For each pair of services where at least one lacks a LSB |
146 | * header, we use the start priority value to order things. */ | |
2c4104f0 LP |
147 | |
148 | LIST_FOREACH(units_per_type, other, UNIT(s)->meta.manager->units_per_type[UNIT_SERVICE]) { | |
149 | Service *t; | |
150 | UnitDependency d; | |
151 | ||
152 | t = (Service*) other; | |
153 | ||
154 | if (s == t) | |
155 | continue; | |
156 | ||
157 | if (t->sysv_start_priority < 0) | |
158 | continue; | |
159 | ||
51a1a79d LP |
160 | /* If both units have modern headers we don't care |
161 | * about the priorities */ | |
162 | if ((!s->sysv_path || s->sysv_has_lsb) && | |
163 | (!t->sysv_path || t->sysv_has_lsb)) | |
23a177ef LP |
164 | continue; |
165 | ||
2c4104f0 LP |
166 | if (t->sysv_start_priority < s->sysv_start_priority) |
167 | d = UNIT_AFTER; | |
168 | else if (t->sysv_start_priority > s->sysv_start_priority) | |
169 | d = UNIT_BEFORE; | |
170 | else | |
171 | continue; | |
172 | ||
173 | /* FIXME: Maybe we should compare the name here lexicographically? */ | |
174 | ||
175 | if (!(r = unit_add_dependency(UNIT(s), d, UNIT(t))) < 0) | |
176 | return r; | |
177 | } | |
178 | ||
179 | return 0; | |
180 | } | |
181 | ||
182 | static ExecCommand *exec_command_new(const char *path, const char *arg1) { | |
183 | ExecCommand *c; | |
184 | ||
185 | if (!(c = new0(ExecCommand, 1))) | |
186 | return NULL; | |
187 | ||
188 | if (!(c->path = strdup(path))) { | |
189 | free(c); | |
190 | return NULL; | |
191 | } | |
192 | ||
193 | if (!(c->argv = strv_new(path, arg1, NULL))) { | |
194 | free(c->path); | |
195 | free(c); | |
196 | return NULL; | |
197 | } | |
198 | ||
199 | return c; | |
200 | } | |
201 | ||
202 | static int sysv_exec_commands(Service *s) { | |
203 | ExecCommand *c; | |
204 | ||
205 | assert(s); | |
206 | assert(s->sysv_path); | |
207 | ||
208 | if (!(c = exec_command_new(s->sysv_path, "start"))) | |
209 | return -ENOMEM; | |
210 | exec_command_append_list(s->exec_command+SERVICE_EXEC_START, c); | |
211 | ||
212 | if (!(c = exec_command_new(s->sysv_path, "stop"))) | |
213 | return -ENOMEM; | |
214 | exec_command_append_list(s->exec_command+SERVICE_EXEC_STOP, c); | |
215 | ||
216 | if (!(c = exec_command_new(s->sysv_path, "reload"))) | |
217 | return -ENOMEM; | |
218 | exec_command_append_list(s->exec_command+SERVICE_EXEC_RELOAD, c); | |
219 | ||
220 | return 0; | |
221 | } | |
222 | ||
23a177ef LP |
223 | static int priority_from_rcd(Service *s, const char *init_script) { |
224 | char **p; | |
225 | unsigned i; | |
226 | ||
061978fa | 227 | STRV_FOREACH(p, UNIT(s)->meta.manager->sysvrcnd_path) |
23a177ef LP |
228 | for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) { |
229 | char *path; | |
230 | DIR *d; | |
231 | struct dirent *de; | |
232 | ||
233 | if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) | |
234 | return -ENOMEM; | |
235 | ||
236 | d = opendir(path); | |
237 | free(path); | |
238 | ||
239 | if (!d) { | |
240 | if (errno != ENOENT) | |
241 | log_warning("opendir() failed on %s: %s", path, strerror(errno)); | |
242 | ||
243 | continue; | |
244 | } | |
245 | ||
246 | while ((de = readdir(d))) { | |
247 | int a, b; | |
248 | ||
249 | if (ignore_file(de->d_name)) | |
250 | continue; | |
251 | ||
252 | if (de->d_name[0] != 'S') | |
253 | continue; | |
254 | ||
255 | if (strlen(de->d_name) < 4) | |
256 | continue; | |
257 | ||
258 | if (!streq(de->d_name + 3, init_script)) | |
259 | continue; | |
260 | ||
261 | /* Yay, we found it! Now decode the priority */ | |
262 | ||
263 | a = undecchar(de->d_name[1]); | |
264 | b = undecchar(de->d_name[2]); | |
265 | ||
266 | if (a < 0 || b < 0) | |
267 | continue; | |
268 | ||
269 | s->sysv_start_priority = a*10 + b; | |
270 | ||
271 | log_debug("Determined priority %i from link farm for %s", s->sysv_start_priority, unit_id(UNIT(s))); | |
272 | ||
273 | closedir(d); | |
274 | return 0; | |
275 | } | |
276 | ||
277 | closedir(d); | |
278 | } | |
279 | ||
280 | return 0; | |
281 | } | |
282 | ||
e537352b | 283 | static int service_load_sysv_path(Service *s, const char *path) { |
2c4104f0 LP |
284 | FILE *f; |
285 | Unit *u; | |
286 | unsigned line = 0; | |
287 | int r; | |
288 | enum { | |
289 | NORMAL, | |
290 | DESCRIPTION, | |
291 | LSB, | |
292 | LSB_DESCRIPTION | |
293 | } state = NORMAL; | |
23a177ef LP |
294 | |
295 | assert(s); | |
296 | assert(path); | |
2c4104f0 LP |
297 | |
298 | u = UNIT(s); | |
299 | ||
300 | if (!(f = fopen(path, "re"))) { | |
301 | r = errno == ENOENT ? 0 : -errno; | |
302 | goto finish; | |
303 | } | |
304 | ||
305 | s->type = SERVICE_FORKING; | |
306 | s->restart = SERVICE_ONCE; | |
307 | ||
308 | free(s->sysv_path); | |
309 | if (!(s->sysv_path = strdup(path))) { | |
310 | r = -ENOMEM; | |
311 | goto finish; | |
312 | } | |
313 | ||
314 | while (!feof(f)) { | |
315 | char l[LINE_MAX], *t; | |
316 | ||
317 | if (!fgets(l, sizeof(l), f)) { | |
318 | if (feof(f)) | |
319 | break; | |
320 | ||
321 | r = -errno; | |
322 | log_error("Failed to read configuration file '%s': %s", path, strerror(-r)); | |
323 | goto finish; | |
324 | } | |
325 | ||
326 | line++; | |
327 | ||
328 | t = strstrip(l); | |
329 | if (*t != '#') | |
330 | continue; | |
331 | ||
332 | if (state == NORMAL && streq(t, "### BEGIN INIT INFO")) { | |
333 | state = LSB; | |
23a177ef | 334 | s->sysv_has_lsb = true; |
2c4104f0 LP |
335 | continue; |
336 | } | |
337 | ||
338 | if ((state == LSB_DESCRIPTION || state == LSB) && streq(t, "### END INIT INFO")) { | |
339 | state = NORMAL; | |
340 | continue; | |
341 | } | |
342 | ||
343 | t++; | |
344 | t += strspn(t, WHITESPACE); | |
345 | ||
346 | if (state == NORMAL) { | |
347 | ||
348 | /* Try to parse Red Hat style chkconfig headers */ | |
349 | ||
350 | if (startswith(t, "chkconfig:")) { | |
351 | int start_priority; | |
8309400a | 352 | char runlevels[16], *k; |
2c4104f0 LP |
353 | |
354 | state = NORMAL; | |
355 | ||
8309400a LP |
356 | if (sscanf(t+10, "%15s %i %*i", |
357 | runlevels, | |
358 | &start_priority) != 2) { | |
2c4104f0 LP |
359 | |
360 | log_warning("[%s:%u] Failed to parse chkconfig line. Ignoring.", path, line); | |
361 | continue; | |
362 | } | |
363 | ||
8309400a | 364 | if (start_priority < 0 || start_priority > 99) |
2c4104f0 | 365 | log_warning("[%s:%u] Start priority out of range. Ignoring.", path, line); |
8309400a LP |
366 | else |
367 | s->sysv_start_priority = start_priority; | |
368 | ||
369 | char_array_0(runlevels); | |
370 | k = delete_chars(runlevels, WHITESPACE "-"); | |
371 | ||
372 | if (k[0]) { | |
373 | char *d; | |
374 | ||
375 | if (!(d = strdup(k))) { | |
376 | r = -ENOMEM; | |
377 | goto finish; | |
378 | } | |
379 | ||
380 | free(s->sysv_runlevels); | |
381 | s->sysv_runlevels = d; | |
2c4104f0 LP |
382 | } |
383 | ||
2c4104f0 LP |
384 | |
385 | } else if (startswith(t, "description:")) { | |
386 | ||
387 | size_t k = strlen(t); | |
388 | char *d; | |
389 | ||
390 | if (t[k-1] == '\\') { | |
391 | state = DESCRIPTION; | |
392 | t[k-1] = 0; | |
393 | } | |
394 | ||
395 | if (!(d = strdup(strstrip(t+12)))) { | |
396 | r = -ENOMEM; | |
397 | goto finish; | |
398 | } | |
399 | ||
400 | free(u->meta.description); | |
401 | u->meta.description = d; | |
402 | ||
403 | } else if (startswith(t, "pidfile:")) { | |
404 | ||
405 | char *fn; | |
406 | ||
407 | state = NORMAL; | |
408 | ||
409 | fn = strstrip(t+8); | |
410 | if (!path_is_absolute(fn)) { | |
411 | log_warning("[%s:%u] PID file not absolute. Ignoring.", path, line); | |
412 | continue; | |
413 | } | |
414 | ||
415 | if (!(fn = strdup(fn))) { | |
416 | r = -ENOMEM; | |
417 | goto finish; | |
418 | } | |
419 | ||
420 | free(s->pid_file); | |
421 | s->pid_file = fn; | |
422 | } | |
423 | ||
424 | } else if (state == DESCRIPTION) { | |
425 | ||
426 | /* Try to parse Red Hat style description | |
427 | * continuation */ | |
428 | ||
429 | size_t k = strlen(t); | |
430 | char *d; | |
431 | ||
432 | if (t[k-1] == '\\') | |
433 | t[k-1] = 0; | |
434 | else | |
435 | state = NORMAL; | |
436 | ||
437 | assert(u->meta.description); | |
438 | if (asprintf(&d, "%s %s", u->meta.description, strstrip(t)) < 0) { | |
439 | r = -ENOMEM; | |
440 | goto finish; | |
441 | } | |
442 | ||
443 | free(u->meta.description); | |
444 | u->meta.description = d; | |
445 | ||
446 | } else if (state == LSB || state == LSB_DESCRIPTION) { | |
447 | ||
448 | if (startswith(t, "Provides:")) { | |
449 | char *i, *w; | |
450 | size_t z; | |
451 | ||
452 | state = LSB; | |
453 | ||
454 | FOREACH_WORD(w, z, t+9, i) { | |
455 | char *n, *m; | |
456 | ||
457 | if (!(n = strndup(w, z))) { | |
458 | r = -ENOMEM; | |
459 | goto finish; | |
460 | } | |
461 | ||
462 | r = sysv_translate_name(n, &m); | |
463 | free(n); | |
464 | ||
465 | if (r < 0) | |
466 | goto finish; | |
467 | ||
468 | if (r == 0) | |
469 | continue; | |
470 | ||
bd77d0fc LP |
471 | if (unit_name_to_type(m) == UNIT_SERVICE) |
472 | r = unit_add_name(u, m); | |
473 | else { | |
474 | if ((r = unit_add_dependency_by_name_inverse(u, UNIT_REQUIRES, m)) >= 0) | |
475 | r = unit_add_dependency_by_name(u, UNIT_BEFORE, m); | |
476 | } | |
477 | ||
2c4104f0 LP |
478 | free(m); |
479 | ||
480 | if (r < 0) | |
481 | goto finish; | |
482 | } | |
483 | ||
484 | } else if (startswith(t, "Required-Start:") || | |
485 | startswith(t, "Should-Start:")) { | |
486 | char *i, *w; | |
487 | size_t z; | |
488 | ||
489 | state = LSB; | |
490 | ||
491 | FOREACH_WORD(w, z, strchr(t, ':')+1, i) { | |
492 | char *n, *m; | |
493 | ||
494 | if (!(n = strndup(w, z))) { | |
495 | r = -ENOMEM; | |
496 | goto finish; | |
497 | } | |
498 | ||
499 | r = sysv_translate_name(n, &m); | |
500 | free(n); | |
501 | ||
502 | if (r < 0) | |
503 | goto finish; | |
504 | ||
505 | if (r == 0) | |
506 | continue; | |
507 | ||
fc22fc6e | 508 | r = unit_add_dependency_by_name(u, UNIT_AFTER, m); |
2c4104f0 LP |
509 | free(m); |
510 | ||
511 | if (r < 0) | |
512 | goto finish; | |
513 | } | |
8309400a LP |
514 | } else if (startswith(t, "Default-Start:")) { |
515 | char *k, *d; | |
516 | ||
517 | state = LSB; | |
518 | ||
519 | k = delete_chars(t+14, WHITESPACE "-"); | |
520 | ||
521 | if (k[0] != 0) { | |
522 | if (!(d = strdup(k))) { | |
523 | r = -ENOMEM; | |
524 | goto finish; | |
525 | } | |
526 | ||
527 | free(s->sysv_runlevels); | |
528 | s->sysv_runlevels = d; | |
529 | } | |
2c4104f0 LP |
530 | |
531 | } else if (startswith(t, "Description:")) { | |
532 | char *d; | |
533 | ||
534 | state = LSB_DESCRIPTION; | |
535 | ||
536 | if (!(d = strdup(strstrip(t+12)))) { | |
537 | r = -ENOMEM; | |
538 | goto finish; | |
539 | } | |
540 | ||
541 | free(u->meta.description); | |
542 | u->meta.description = d; | |
543 | ||
8309400a LP |
544 | } else if (startswith(t, "Short-Description:") && |
545 | !u->meta.description) { | |
2c4104f0 LP |
546 | char *d; |
547 | ||
548 | /* We use the short description only | |
549 | * if no long description is set. */ | |
550 | ||
551 | state = LSB; | |
552 | ||
553 | if (!(d = strdup(strstrip(t+18)))) { | |
554 | r = -ENOMEM; | |
555 | goto finish; | |
556 | } | |
557 | ||
2c4104f0 LP |
558 | u->meta.description = d; |
559 | ||
560 | } else if (state == LSB_DESCRIPTION) { | |
561 | ||
562 | if (startswith(l, "#\t") || startswith(l, "# ")) { | |
563 | char *d; | |
564 | ||
565 | assert(u->meta.description); | |
566 | if (asprintf(&d, "%s %s", u->meta.description, t) < 0) { | |
567 | r = -ENOMEM; | |
568 | goto finish; | |
569 | } | |
570 | ||
571 | free(u->meta.description); | |
572 | u->meta.description = d; | |
573 | } else | |
574 | state = LSB; | |
575 | } | |
576 | } | |
577 | } | |
578 | ||
23a177ef LP |
579 | /* If init scripts have no LSB header, then we enforce the |
580 | * ordering via the chkconfig priorities. We try to determine | |
581 | * a priority for *all* init scripts here, since they are | |
582 | * needed as soon as at least one non-LSB script is used. */ | |
2c4104f0 | 583 | |
23a177ef LP |
584 | if (s->sysv_start_priority < 0) { |
585 | log_debug("%s has no chkconfig header, trying to determine SysV priority from link farm.", unit_id(u)); | |
586 | ||
587 | if ((r = priority_from_rcd(s, file_name_from_path(path))) < 0) | |
2c4104f0 LP |
588 | goto finish; |
589 | ||
23a177ef LP |
590 | if (s->sysv_start_priority < 0) |
591 | log_warning("%s has neither a chkconfig header nor a directory link, cannot order unit!", unit_id(u)); | |
592 | } | |
593 | ||
2c4104f0 LP |
594 | if ((r = sysv_exec_commands(s)) < 0) |
595 | goto finish; | |
596 | ||
0bc824be LP |
597 | if (!s->sysv_runlevels || chars_intersect("12345", s->sysv_runlevels)) { |
598 | /* If there a runlevels configured for this service | |
599 | * but none of the standard ones, then we assume this | |
600 | * is some special kind of service (which might be | |
601 | * needed for early boot) and don't create any links | |
602 | * to it. */ | |
603 | ||
604 | if ((r = unit_add_dependency_by_name(u, UNIT_REQUIRES, SPECIAL_BASIC_TARGET)) < 0 || | |
605 | (r = unit_add_dependency_by_name(u, UNIT_AFTER, SPECIAL_BASIC_TARGET)) < 0) | |
606 | goto finish; | |
607 | } | |
0fd030be | 608 | |
e537352b | 609 | u->meta.load_state = UNIT_LOADED; |
23a177ef | 610 | r = 0; |
2c4104f0 LP |
611 | |
612 | finish: | |
613 | ||
614 | if (f) | |
615 | fclose(f); | |
616 | ||
617 | return r; | |
618 | } | |
619 | ||
e537352b | 620 | static int service_load_sysv_name(Service *s, const char *name) { |
2c4104f0 LP |
621 | char **p; |
622 | ||
623 | assert(s); | |
624 | assert(name); | |
625 | ||
626 | STRV_FOREACH(p, UNIT(s)->meta.manager->sysvinit_path) { | |
627 | char *path; | |
628 | int r; | |
629 | ||
630 | if (asprintf(&path, "%s/%s", *p, name) < 0) | |
631 | return -ENOMEM; | |
632 | ||
633 | assert(endswith(path, ".service")); | |
634 | path[strlen(path)-8] = 0; | |
635 | ||
e537352b | 636 | r = service_load_sysv_path(s, path); |
2c4104f0 LP |
637 | free(path); |
638 | ||
23a177ef | 639 | if (r < 0) |
2c4104f0 | 640 | return r; |
23a177ef | 641 | |
e537352b | 642 | if ((UNIT(s)->meta.load_state != UNIT_STUB)) |
23a177ef | 643 | break; |
2c4104f0 LP |
644 | } |
645 | ||
646 | return 0; | |
647 | } | |
648 | ||
e537352b | 649 | static int service_load_sysv(Service *s) { |
2c4104f0 LP |
650 | const char *t; |
651 | Iterator i; | |
652 | int r; | |
653 | ||
5cb5a6ff LP |
654 | assert(s); |
655 | ||
656 | /* Load service data from SysV init scripts, preferably with | |
657 | * LSB headers ... */ | |
658 | ||
2c4104f0 LP |
659 | if (strv_isempty(UNIT(s)->meta.manager->sysvinit_path)) |
660 | return 0; | |
661 | ||
662 | if ((t = unit_id(UNIT(s)))) | |
e537352b | 663 | if ((r = service_load_sysv_name(s, t)) < 0) |
2c4104f0 LP |
664 | return r; |
665 | ||
e537352b | 666 | if (UNIT(s)->meta.load_state == UNIT_STUB) |
23a177ef | 667 | SET_FOREACH(t, UNIT(s)->meta.names, i) { |
e537352b LP |
668 | if (t == unit_id(UNIT(s))) |
669 | continue; | |
670 | ||
671 | if ((r == service_load_sysv_name(s, t)) < 0) | |
23a177ef LP |
672 | return r; |
673 | ||
e537352b | 674 | if (UNIT(s)->meta.load_state != UNIT_STUB) |
23a177ef LP |
675 | break; |
676 | } | |
2c4104f0 LP |
677 | |
678 | return 0; | |
5cb5a6ff LP |
679 | } |
680 | ||
e537352b | 681 | static void service_init(Unit *u) { |
87f0e418 | 682 | Service *s = SERVICE(u); |
5cb5a6ff | 683 | |
e537352b LP |
684 | assert(u); |
685 | assert(u->meta.load_state == UNIT_STUB); | |
034c6ed7 LP |
686 | |
687 | s->type = 0; | |
688 | s->restart = 0; | |
689 | ||
690 | s->timeout_usec = DEFAULT_TIMEOUT_USEC; | |
691 | s->restart_usec = DEFAULT_RESTART_USEC; | |
692 | ||
693 | exec_context_init(&s->exec_context); | |
694 | ||
acbb0225 | 695 | s->timer_watch.type = WATCH_INVALID; |
034c6ed7 LP |
696 | |
697 | s->state = SERVICE_DEAD; | |
5cb5a6ff | 698 | |
2c4104f0 | 699 | s->sysv_start_priority = -1; |
81a2b7ce LP |
700 | s->permissions_start_only = false; |
701 | s->root_directory_start_only = false; | |
50159e6a LP |
702 | s->valid_no_process = false; |
703 | s->kill_mode = 0; | |
23a177ef | 704 | s->sysv_has_lsb = false; |
50159e6a LP |
705 | s->main_pid = s->control_pid = 0; |
706 | s->main_pid_known = false; | |
707 | s->failure = false; | |
23a177ef | 708 | |
1e2e8133 | 709 | RATELIMIT_INIT(s->ratelimit, 10*USEC_PER_SEC, 5); |
e537352b LP |
710 | } |
711 | ||
712 | static int service_load(Unit *u) { | |
713 | int r; | |
714 | Service *s = SERVICE(u); | |
715 | ||
716 | assert(s); | |
1e2e8133 | 717 | |
5cb5a6ff | 718 | /* Load a .service file */ |
e537352b | 719 | if ((r = unit_load_fragment(u)) < 0) |
5cb5a6ff LP |
720 | return r; |
721 | ||
bd77d0fc | 722 | /* Load a classic init script as a fallback, if we couldn't find anything */ |
e537352b LP |
723 | if (u->meta.load_state == UNIT_STUB) |
724 | if ((r = service_load_sysv(s)) < 0) | |
23a177ef | 725 | return r; |
d46de8a1 | 726 | |
23a177ef | 727 | /* Still nothing found? Then let's give up */ |
e537352b | 728 | if (u->meta.load_state == UNIT_STUB) |
23a177ef | 729 | return -ENOENT; |
034c6ed7 | 730 | |
23a177ef LP |
731 | /* We were able to load something, then let's add in the |
732 | * dropin directories. */ | |
733 | if ((r = unit_load_dropin(unit_follow_merge(u))) < 0) | |
8e274523 | 734 | return r; |
23a177ef LP |
735 | |
736 | /* This is a new unit? Then let's add in some extras */ | |
e537352b | 737 | if (u->meta.load_state == UNIT_LOADED) { |
23a177ef LP |
738 | if ((r = unit_add_exec_dependencies(u, &s->exec_context)) < 0) |
739 | return r; | |
740 | ||
741 | if ((r = unit_add_default_cgroup(u)) < 0) | |
742 | return r; | |
743 | ||
744 | if ((r = sysv_chkconfig_order(s)) < 0) | |
745 | return r; | |
8e274523 LP |
746 | } |
747 | ||
44d8db9e | 748 | return 0; |
034c6ed7 LP |
749 | } |
750 | ||
87f0e418 | 751 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 752 | |
5cb5a6ff | 753 | ServiceExecCommand c; |
87f0e418 | 754 | Service *s = SERVICE(u); |
47be870b LP |
755 | const char *prefix2; |
756 | char *p2; | |
5cb5a6ff LP |
757 | |
758 | assert(s); | |
759 | ||
47be870b LP |
760 | p2 = strappend(prefix, "\t"); |
761 | prefix2 = p2 ? p2 : prefix; | |
44d8db9e | 762 | |
5cb5a6ff | 763 | fprintf(f, |
81a2b7ce LP |
764 | "%sService State: %s\n" |
765 | "%sPermissionsStartOnly: %s\n" | |
8e274523 | 766 | "%sRootDirectoryStartOnly: %s\n" |
03d6ab85 | 767 | "%sValidNoProcess: %s\n" |
50159e6a | 768 | "%sKillMode: %s\n" |
03d6ab85 | 769 | "%sType: %s\n", |
81a2b7ce LP |
770 | prefix, service_state_to_string(s->state), |
771 | prefix, yes_no(s->permissions_start_only), | |
8e274523 | 772 | prefix, yes_no(s->root_directory_start_only), |
03d6ab85 | 773 | prefix, yes_no(s->valid_no_process), |
50159e6a | 774 | prefix, kill_mode_to_string(s->kill_mode), |
03d6ab85 | 775 | prefix, service_type_to_string(s->type)); |
5cb5a6ff | 776 | |
70123e68 LP |
777 | if (s->control_pid > 0) |
778 | fprintf(f, | |
779 | "%sControl PID: %llu\n", | |
780 | prefix, (unsigned long long) s->control_pid); | |
781 | ||
782 | if (s->main_pid > 0) | |
783 | fprintf(f, | |
784 | "%sMain PID: %llu\n", | |
785 | prefix, (unsigned long long) s->main_pid); | |
786 | ||
034c6ed7 LP |
787 | if (s->pid_file) |
788 | fprintf(f, | |
789 | "%sPIDFile: %s\n", | |
790 | prefix, s->pid_file); | |
791 | ||
5cb5a6ff LP |
792 | exec_context_dump(&s->exec_context, f, prefix); |
793 | ||
e537352b | 794 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
5cb5a6ff | 795 | |
44d8db9e LP |
796 | if (!s->exec_command[c]) |
797 | continue; | |
798 | ||
799 | fprintf(f, "%s→ %s:\n", | |
94f04347 | 800 | prefix, service_exec_command_to_string(c)); |
44d8db9e LP |
801 | |
802 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 803 | } |
44d8db9e | 804 | |
2c4104f0 LP |
805 | if (s->sysv_path) |
806 | fprintf(f, | |
23a177ef LP |
807 | "%sSysV Init Script Path: %s\n" |
808 | "%sSysV Init Script has LSB Header: %s\n", | |
809 | prefix, s->sysv_path, | |
810 | prefix, yes_no(s->sysv_has_lsb)); | |
2c4104f0 LP |
811 | |
812 | if (s->sysv_start_priority >= 0) | |
813 | fprintf(f, | |
23a177ef | 814 | "%sSysVStartPriority: %i\n", |
2c4104f0 LP |
815 | prefix, s->sysv_start_priority); |
816 | ||
8309400a LP |
817 | if (s->sysv_runlevels) |
818 | fprintf(f, "%sSysVRunLevels: %s\n", | |
819 | prefix, s->sysv_runlevels); | |
23a177ef | 820 | |
47be870b | 821 | free(p2); |
5cb5a6ff LP |
822 | } |
823 | ||
034c6ed7 LP |
824 | static int service_load_pid_file(Service *s) { |
825 | char *k; | |
826 | unsigned long p; | |
827 | int r; | |
828 | ||
829 | assert(s); | |
830 | ||
831 | if (s->main_pid_known) | |
832 | return 0; | |
833 | ||
834 | if (!s->pid_file) | |
835 | return -ENOENT; | |
836 | ||
837 | if ((r = read_one_line_file(s->pid_file, &k)) < 0) | |
838 | return r; | |
839 | ||
840 | if ((r = safe_atolu(k, &p)) < 0) { | |
841 | free(k); | |
842 | return r; | |
843 | } | |
844 | ||
845 | if ((unsigned long) (pid_t) p != p) | |
846 | return -ERANGE; | |
847 | ||
b8c597d5 LP |
848 | if (kill((pid_t) p, 0) < 0 && errno != EPERM) { |
849 | log_warning("PID %llu read from file %s does not exist. Your service or init script might be broken.", | |
850 | (unsigned long long) p, s->pid_file); | |
851 | return -ESRCH; | |
852 | } | |
853 | ||
16f6025e LP |
854 | if ((r = unit_watch_pid(UNIT(s), (pid_t) p)) < 0) |
855 | /* FIXME: we need to do something here */ | |
856 | return r; | |
857 | ||
b8c597d5 | 858 | s->main_pid = (pid_t) p; |
034c6ed7 LP |
859 | s->main_pid_known = true; |
860 | ||
861 | return 0; | |
862 | } | |
863 | ||
3e33402a LP |
864 | static int service_get_sockets(Service *s, Set **_set) { |
865 | Set *set; | |
ceee3d82 LP |
866 | Iterator i; |
867 | char *t; | |
3e33402a | 868 | int r; |
ceee3d82 LP |
869 | |
870 | assert(s); | |
3e33402a LP |
871 | assert(_set); |
872 | ||
873 | /* Collects all Socket objects that belong to this | |
874 | * service. Note that a service might have multiple sockets | |
875 | * via multiple names. */ | |
876 | ||
877 | if (!(set = set_new(NULL, NULL))) | |
878 | return -ENOMEM; | |
ceee3d82 LP |
879 | |
880 | SET_FOREACH(t, UNIT(s)->meta.names, i) { | |
881 | char *k; | |
882 | Unit *p; | |
883 | ||
884 | /* Look for all socket objects that go by any of our | |
885 | * units and collect their fds */ | |
886 | ||
3e33402a LP |
887 | if (!(k = unit_name_change_suffix(t, ".socket"))) { |
888 | r = -ENOMEM; | |
889 | goto fail; | |
890 | } | |
ceee3d82 LP |
891 | |
892 | p = manager_get_unit(UNIT(s)->meta.manager, k); | |
893 | free(k); | |
894 | ||
3e33402a | 895 | if (!p) continue; |
ceee3d82 | 896 | |
3e33402a LP |
897 | if ((r = set_put(set, p)) < 0) |
898 | goto fail; | |
ceee3d82 LP |
899 | } |
900 | ||
3e33402a LP |
901 | *_set = set; |
902 | return 0; | |
903 | ||
904 | fail: | |
905 | set_free(set); | |
906 | return r; | |
907 | } | |
908 | ||
909 | ||
e537352b | 910 | static int service_notify_sockets_dead(Service *s) { |
3e33402a LP |
911 | Iterator i; |
912 | Set *set; | |
47be870b | 913 | Socket *sock; |
3e33402a LP |
914 | int r; |
915 | ||
916 | assert(s); | |
917 | ||
918 | /* Notifies all our sockets when we die */ | |
919 | ||
920 | if ((r = service_get_sockets(s, &set)) < 0) | |
921 | return r; | |
922 | ||
47be870b LP |
923 | SET_FOREACH(sock, set, i) |
924 | socket_notify_service_dead(sock); | |
3e33402a LP |
925 | |
926 | set_free(set); | |
927 | ||
ceee3d82 LP |
928 | return 0; |
929 | } | |
930 | ||
034c6ed7 LP |
931 | static void service_set_state(Service *s, ServiceState state) { |
932 | ServiceState old_state; | |
5cb5a6ff LP |
933 | assert(s); |
934 | ||
034c6ed7 | 935 | old_state = s->state; |
5cb5a6ff | 936 | s->state = state; |
034c6ed7 LP |
937 | |
938 | if (state != SERVICE_START_PRE && | |
939 | state != SERVICE_START && | |
940 | state != SERVICE_START_POST && | |
941 | state != SERVICE_RELOAD && | |
942 | state != SERVICE_STOP && | |
943 | state != SERVICE_STOP_SIGTERM && | |
944 | state != SERVICE_STOP_SIGKILL && | |
945 | state != SERVICE_STOP_POST && | |
946 | state != SERVICE_FINAL_SIGTERM && | |
947 | state != SERVICE_FINAL_SIGKILL && | |
948 | state != SERVICE_AUTO_RESTART) | |
acbb0225 | 949 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 | 950 | |
7d55e835 LP |
951 | if (state != SERVICE_START && |
952 | state != SERVICE_START_POST && | |
034c6ed7 LP |
953 | state != SERVICE_RUNNING && |
954 | state != SERVICE_RELOAD && | |
955 | state != SERVICE_STOP && | |
956 | state != SERVICE_STOP_SIGTERM && | |
957 | state != SERVICE_STOP_SIGKILL) | |
acbb0225 | 958 | if (s->main_pid > 0) { |
87f0e418 | 959 | unit_unwatch_pid(UNIT(s), s->main_pid); |
034c6ed7 LP |
960 | s->main_pid = 0; |
961 | } | |
962 | ||
963 | if (state != SERVICE_START_PRE && | |
964 | state != SERVICE_START && | |
965 | state != SERVICE_START_POST && | |
966 | state != SERVICE_RELOAD && | |
967 | state != SERVICE_STOP && | |
968 | state != SERVICE_STOP_SIGTERM && | |
969 | state != SERVICE_STOP_SIGKILL && | |
970 | state != SERVICE_STOP_POST && | |
971 | state != SERVICE_FINAL_SIGTERM && | |
e537352b | 972 | state != SERVICE_FINAL_SIGKILL) { |
acbb0225 | 973 | if (s->control_pid > 0) { |
87f0e418 | 974 | unit_unwatch_pid(UNIT(s), s->control_pid); |
034c6ed7 LP |
975 | s->control_pid = 0; |
976 | } | |
977 | ||
034c6ed7 | 978 | s->control_command = NULL; |
e537352b | 979 | } |
034c6ed7 | 980 | |
ceee3d82 LP |
981 | if (state == SERVICE_DEAD || |
982 | state == SERVICE_STOP || | |
983 | state == SERVICE_STOP_SIGTERM || | |
984 | state == SERVICE_STOP_SIGKILL || | |
985 | state == SERVICE_STOP_POST || | |
986 | state == SERVICE_FINAL_SIGTERM || | |
987 | state == SERVICE_FINAL_SIGKILL || | |
988 | state == SERVICE_MAINTAINANCE || | |
989 | state == SERVICE_AUTO_RESTART) | |
e537352b | 990 | service_notify_sockets_dead(s); |
ceee3d82 | 991 | |
e537352b LP |
992 | if (old_state != state) |
993 | log_debug("%s changed %s → %s", unit_id(UNIT(s)), service_state_to_string(old_state), service_state_to_string(state)); | |
acbb0225 LP |
994 | |
995 | unit_notify(UNIT(s), state_translation_table[old_state], state_translation_table[state]); | |
034c6ed7 LP |
996 | } |
997 | ||
44d8db9e LP |
998 | static int service_collect_fds(Service *s, int **fds, unsigned *n_fds) { |
999 | Iterator i; | |
1000 | int r; | |
1001 | int *rfds = NULL; | |
1002 | unsigned rn_fds = 0; | |
3e33402a | 1003 | Set *set; |
47be870b | 1004 | Socket *sock; |
44d8db9e LP |
1005 | |
1006 | assert(s); | |
1007 | assert(fds); | |
1008 | assert(n_fds); | |
1009 | ||
3e33402a LP |
1010 | if ((r = service_get_sockets(s, &set)) < 0) |
1011 | return r; | |
1012 | ||
47be870b | 1013 | SET_FOREACH(sock, set, i) { |
44d8db9e LP |
1014 | int *cfds; |
1015 | unsigned cn_fds; | |
1016 | ||
47be870b | 1017 | if ((r = socket_collect_fds(sock, &cfds, &cn_fds)) < 0) |
44d8db9e LP |
1018 | goto fail; |
1019 | ||
1020 | if (!cfds) | |
1021 | continue; | |
1022 | ||
1023 | if (!rfds) { | |
1024 | rfds = cfds; | |
1025 | rn_fds = cn_fds; | |
1026 | } else { | |
1027 | int *t; | |
1028 | ||
1029 | if (!(t = new(int, rn_fds+cn_fds))) { | |
1030 | free(cfds); | |
1031 | r = -ENOMEM; | |
1032 | goto fail; | |
1033 | } | |
1034 | ||
1035 | memcpy(t, rfds, rn_fds); | |
1036 | memcpy(t+rn_fds, cfds, cn_fds); | |
1037 | free(rfds); | |
1038 | free(cfds); | |
1039 | ||
1040 | rfds = t; | |
1041 | rn_fds = rn_fds+cn_fds; | |
1042 | } | |
1043 | } | |
1044 | ||
1045 | *fds = rfds; | |
1046 | *n_fds = rn_fds; | |
3e33402a LP |
1047 | |
1048 | set_free(set); | |
1049 | ||
44d8db9e LP |
1050 | return 0; |
1051 | ||
1052 | fail: | |
3e33402a | 1053 | set_free(set); |
44d8db9e | 1054 | free(rfds); |
3e33402a | 1055 | |
44d8db9e LP |
1056 | return r; |
1057 | } | |
1058 | ||
81a2b7ce LP |
1059 | static int service_spawn( |
1060 | Service *s, | |
1061 | ExecCommand *c, | |
1062 | bool timeout, | |
1063 | bool pass_fds, | |
1064 | bool apply_permissions, | |
1065 | bool apply_chroot, | |
1066 | pid_t *_pid) { | |
1067 | ||
034c6ed7 LP |
1068 | pid_t pid; |
1069 | int r; | |
44d8db9e LP |
1070 | int *fds = NULL; |
1071 | unsigned n_fds = 0; | |
034c6ed7 LP |
1072 | |
1073 | assert(s); | |
1074 | assert(c); | |
1075 | assert(_pid); | |
1076 | ||
44d8db9e LP |
1077 | if (pass_fds) |
1078 | if ((r = service_collect_fds(s, &fds, &n_fds)) < 0) | |
1079 | goto fail; | |
1080 | ||
034c6ed7 | 1081 | if (timeout) { |
acbb0225 | 1082 | if ((r = unit_watch_timer(UNIT(s), s->timeout_usec, &s->timer_watch)) < 0) |
034c6ed7 LP |
1083 | goto fail; |
1084 | } else | |
acbb0225 | 1085 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 | 1086 | |
81a2b7ce LP |
1087 | if ((r = exec_spawn(c, |
1088 | &s->exec_context, | |
1089 | fds, n_fds, | |
1090 | apply_permissions, | |
1091 | apply_chroot, | |
8e274523 | 1092 | UNIT(s)->meta.cgroup_bondings, |
81a2b7ce | 1093 | &pid)) < 0) |
034c6ed7 LP |
1094 | goto fail; |
1095 | ||
87f0e418 | 1096 | if ((r = unit_watch_pid(UNIT(s), pid)) < 0) |
034c6ed7 LP |
1097 | /* FIXME: we need to do something here */ |
1098 | goto fail; | |
1099 | ||
44d8db9e | 1100 | free(fds); |
034c6ed7 LP |
1101 | *_pid = pid; |
1102 | ||
5cb5a6ff | 1103 | return 0; |
034c6ed7 LP |
1104 | |
1105 | fail: | |
44d8db9e LP |
1106 | free(fds); |
1107 | ||
034c6ed7 | 1108 | if (timeout) |
acbb0225 | 1109 | unit_unwatch_timer(UNIT(s), &s->timer_watch); |
034c6ed7 LP |
1110 | |
1111 | return r; | |
1112 | } | |
1113 | ||
1114 | static void service_enter_dead(Service *s, bool success, bool allow_restart) { | |
1115 | int r; | |
1116 | assert(s); | |
1117 | ||
1118 | if (!success) | |
1119 | s->failure = true; | |
1120 | ||
1121 | if (allow_restart && | |
1122 | (s->restart == SERVICE_RESTART_ALWAYS || | |
1123 | (s->restart == SERVICE_RESTART_ON_SUCCESS && !s->failure))) { | |
1124 | ||
acbb0225 | 1125 | if ((r = unit_watch_timer(UNIT(s), s->restart_usec, &s->timer_watch)) < 0) |
034c6ed7 LP |
1126 | goto fail; |
1127 | ||
1128 | service_set_state(s, SERVICE_AUTO_RESTART); | |
1129 | } else | |
1130 | service_set_state(s, s->failure ? SERVICE_MAINTAINANCE : SERVICE_DEAD); | |
1131 | ||
1132 | return; | |
1133 | ||
1134 | fail: | |
87f0e418 | 1135 | log_warning("%s failed to run install restart timer: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1136 | service_enter_dead(s, false, false); |
1137 | } | |
1138 | ||
1139 | static void service_enter_signal(Service *s, ServiceState state, bool success); | |
1140 | ||
1141 | static void service_enter_stop_post(Service *s, bool success) { | |
1142 | int r; | |
1143 | assert(s); | |
1144 | ||
1145 | if (!success) | |
1146 | s->failure = true; | |
1147 | ||
d6ea93e3 | 1148 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST])) |
81a2b7ce LP |
1149 | if ((r = service_spawn(s, |
1150 | s->control_command, | |
1151 | true, | |
1152 | false, | |
1153 | !s->permissions_start_only, | |
1154 | !s->root_directory_start_only, | |
1155 | &s->control_pid)) < 0) | |
034c6ed7 LP |
1156 | goto fail; |
1157 | ||
d6ea93e3 LP |
1158 | |
1159 | service_set_state(s, SERVICE_STOP_POST); | |
1160 | ||
1161 | if (!s->control_command) | |
034c6ed7 LP |
1162 | service_enter_dead(s, true, true); |
1163 | ||
1164 | return; | |
1165 | ||
1166 | fail: | |
87f0e418 | 1167 | log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1168 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
1169 | } | |
1170 | ||
1171 | static void service_enter_signal(Service *s, ServiceState state, bool success) { | |
1172 | int r; | |
1173 | bool sent = false; | |
1174 | ||
1175 | assert(s); | |
1176 | ||
1177 | if (!success) | |
1178 | s->failure = true; | |
1179 | ||
1180 | if (s->main_pid > 0 || s->control_pid > 0) { | |
1181 | int sig; | |
1182 | ||
1183 | sig = (state == SERVICE_STOP_SIGTERM || state == SERVICE_FINAL_SIGTERM) ? SIGTERM : SIGKILL; | |
1184 | ||
50159e6a | 1185 | if (s->kill_mode == KILL_CONTROL_GROUP) { |
034c6ed7 | 1186 | |
50159e6a LP |
1187 | if ((r = cgroup_bonding_kill_list(UNIT(s)->meta.cgroup_bondings, sig)) < 0) { |
1188 | if (r != -EAGAIN && r != -ESRCH) | |
1189 | goto fail; | |
1190 | } else | |
034c6ed7 LP |
1191 | sent = true; |
1192 | } | |
1193 | ||
50159e6a LP |
1194 | if (!sent) { |
1195 | r = 0; | |
1196 | if (s->main_pid > 0) { | |
1197 | if (kill(s->kill_mode == KILL_PROCESS ? s->main_pid : -s->main_pid, sig) < 0 && errno != ESRCH) | |
1198 | r = -errno; | |
1199 | else | |
1200 | sent = true; | |
1201 | } | |
1202 | ||
1203 | if (s->control_pid > 0) { | |
1204 | if (kill(s->kill_mode == KILL_PROCESS ? s->control_pid : -s->control_pid, sig) < 0 && errno != ESRCH) | |
1205 | r = -errno; | |
1206 | else | |
1207 | sent = true; | |
1208 | } | |
1209 | ||
1210 | if (r < 0) | |
1211 | goto fail; | |
1212 | } | |
d6ea93e3 | 1213 | } |
034c6ed7 | 1214 | |
d6ea93e3 LP |
1215 | service_set_state(s, state); |
1216 | ||
1217 | if (s->main_pid <= 0 && s->control_pid <= 0) | |
034c6ed7 LP |
1218 | service_enter_dead(s, true, true); |
1219 | ||
1220 | return; | |
1221 | ||
1222 | fail: | |
87f0e418 | 1223 | log_warning("%s failed to kill processes: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1224 | |
1225 | if (sent) { | |
1226 | s->failure = true; | |
1227 | service_set_state(s, state); | |
1228 | } else if (state == SERVICE_STOP_SIGTERM || state == SERVICE_STOP_SIGKILL) | |
1229 | service_enter_stop_post(s, false); | |
1230 | else | |
1231 | service_enter_dead(s, false, true); | |
1232 | } | |
1233 | ||
1234 | static void service_enter_stop(Service *s, bool success) { | |
1235 | int r; | |
1236 | assert(s); | |
1237 | ||
1238 | if (!success) | |
1239 | s->failure = true; | |
1240 | ||
d6ea93e3 | 1241 | if ((s->control_command = s->exec_command[SERVICE_EXEC_STOP])) |
81a2b7ce LP |
1242 | if ((r = service_spawn(s, |
1243 | s->control_command, | |
1244 | true, | |
1245 | false, | |
1246 | !s->permissions_start_only, | |
1247 | !s->root_directory_start_only, | |
1248 | &s->control_pid)) < 0) | |
034c6ed7 LP |
1249 | goto fail; |
1250 | ||
d6ea93e3 LP |
1251 | service_set_state(s, SERVICE_STOP); |
1252 | ||
1253 | if (!s->control_command) | |
034c6ed7 LP |
1254 | service_enter_signal(s, SERVICE_STOP_SIGTERM, true); |
1255 | ||
1256 | return; | |
1257 | ||
1258 | fail: | |
87f0e418 | 1259 | log_warning("%s failed to run stop executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1260 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); |
1261 | } | |
1262 | ||
1263 | static void service_enter_start_post(Service *s) { | |
1264 | int r; | |
1265 | assert(s); | |
1266 | ||
d6ea93e3 | 1267 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_POST])) |
81a2b7ce LP |
1268 | if ((r = service_spawn(s, |
1269 | s->control_command, | |
1270 | true, | |
1271 | false, | |
1272 | !s->permissions_start_only, | |
1273 | !s->root_directory_start_only, | |
1274 | &s->control_pid)) < 0) | |
034c6ed7 LP |
1275 | goto fail; |
1276 | ||
d6ea93e3 LP |
1277 | |
1278 | service_set_state(s, SERVICE_START_POST); | |
1279 | ||
1280 | if (!s->control_command) | |
034c6ed7 LP |
1281 | service_set_state(s, SERVICE_RUNNING); |
1282 | ||
1283 | return; | |
1284 | ||
1285 | fail: | |
87f0e418 | 1286 | log_warning("%s failed to run start-post executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1287 | service_enter_stop(s, false); |
1288 | } | |
1289 | ||
1290 | static void service_enter_start(Service *s) { | |
1291 | pid_t pid; | |
1292 | int r; | |
1293 | ||
1294 | assert(s); | |
1295 | ||
1296 | assert(s->exec_command[SERVICE_EXEC_START]); | |
1297 | assert(!s->exec_command[SERVICE_EXEC_START]->command_next); | |
1298 | ||
81a2b7ce LP |
1299 | if ((r = service_spawn(s, |
1300 | s->exec_command[SERVICE_EXEC_START], | |
1301 | s->type == SERVICE_FORKING, | |
1302 | true, | |
1303 | true, | |
1304 | true, | |
1305 | &pid)) < 0) | |
034c6ed7 LP |
1306 | goto fail; |
1307 | ||
d6ea93e3 LP |
1308 | service_set_state(s, SERVICE_START); |
1309 | ||
034c6ed7 LP |
1310 | if (s->type == SERVICE_SIMPLE) { |
1311 | /* For simple services we immediately start | |
1312 | * the START_POST binaries. */ | |
1313 | ||
1314 | s->main_pid = pid; | |
1315 | s->main_pid_known = true; | |
1316 | service_enter_start_post(s); | |
1317 | ||
1318 | } else if (s->type == SERVICE_FORKING) { | |
1319 | ||
1320 | /* For forking services we wait until the start | |
1321 | * process exited. */ | |
1322 | ||
1323 | s->control_pid = pid; | |
1324 | s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
7d55e835 LP |
1325 | } else if (s->type == SERVICE_FINISH) { |
1326 | ||
1327 | /* For finishing services we wait until the start | |
1328 | * process exited, too, but it is our main process. */ | |
1329 | ||
1330 | s->main_pid = pid; | |
1331 | s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
034c6ed7 LP |
1332 | } else |
1333 | assert_not_reached("Unknown service type"); | |
1334 | ||
1335 | return; | |
1336 | ||
1337 | fail: | |
87f0e418 | 1338 | log_warning("%s failed to run start exectuable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1339 | service_enter_stop(s, false); |
1340 | } | |
1341 | ||
1342 | static void service_enter_start_pre(Service *s) { | |
1343 | int r; | |
1344 | ||
1345 | assert(s); | |
1346 | ||
d6ea93e3 | 1347 | if ((s->control_command = s->exec_command[SERVICE_EXEC_START_PRE])) |
81a2b7ce LP |
1348 | if ((r = service_spawn(s, |
1349 | s->control_command, | |
1350 | true, | |
1351 | false, | |
1352 | !s->permissions_start_only, | |
1353 | !s->root_directory_start_only, | |
1354 | &s->control_pid)) < 0) | |
034c6ed7 LP |
1355 | goto fail; |
1356 | ||
d6ea93e3 LP |
1357 | service_set_state(s, SERVICE_START_PRE); |
1358 | ||
1359 | if (!s->control_command) | |
034c6ed7 LP |
1360 | service_enter_start(s); |
1361 | ||
1362 | return; | |
1363 | ||
1364 | fail: | |
87f0e418 | 1365 | log_warning("%s failed to run start-pre executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1366 | service_enter_dead(s, false, true); |
1367 | } | |
1368 | ||
1369 | static void service_enter_restart(Service *s) { | |
1370 | int r; | |
1371 | assert(s); | |
1372 | ||
87f0e418 | 1373 | if ((r = manager_add_job(UNIT(s)->meta.manager, JOB_START, UNIT(s), JOB_FAIL, false, NULL)) < 0) |
034c6ed7 LP |
1374 | goto fail; |
1375 | ||
87f0e418 | 1376 | log_debug("%s scheduled restart job.", unit_id(UNIT(s))); |
034c6ed7 LP |
1377 | service_enter_dead(s, true, false); |
1378 | return; | |
1379 | ||
1380 | fail: | |
1381 | ||
87f0e418 | 1382 | log_warning("%s failed to schedule restart job: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1383 | service_enter_dead(s, false, false); |
1384 | } | |
1385 | ||
1386 | static void service_enter_reload(Service *s) { | |
1387 | int r; | |
1388 | ||
1389 | assert(s); | |
1390 | ||
d6ea93e3 | 1391 | if ((s->control_command = s->exec_command[SERVICE_EXEC_RELOAD])) |
81a2b7ce LP |
1392 | if ((r = service_spawn(s, |
1393 | s->control_command, | |
1394 | true, | |
1395 | false, | |
1396 | !s->permissions_start_only, | |
1397 | !s->root_directory_start_only, | |
1398 | &s->control_pid)) < 0) | |
034c6ed7 LP |
1399 | goto fail; |
1400 | ||
d6ea93e3 LP |
1401 | service_set_state(s, SERVICE_RELOAD); |
1402 | ||
1403 | if (!s->control_command) | |
034c6ed7 LP |
1404 | service_set_state(s, SERVICE_RUNNING); |
1405 | ||
1406 | return; | |
1407 | ||
1408 | fail: | |
87f0e418 | 1409 | log_warning("%s failed to run reload executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1410 | service_enter_stop(s, false); |
1411 | } | |
1412 | ||
1413 | static void service_run_next(Service *s, bool success) { | |
1414 | int r; | |
1415 | ||
1416 | assert(s); | |
1417 | assert(s->control_command); | |
1418 | assert(s->control_command->command_next); | |
1419 | ||
1420 | if (!success) | |
1421 | s->failure = true; | |
1422 | ||
1423 | s->control_command = s->control_command->command_next; | |
1424 | ||
81a2b7ce LP |
1425 | if ((r = service_spawn(s, |
1426 | s->control_command, | |
1427 | true, | |
1428 | false, | |
1429 | !s->permissions_start_only, | |
1430 | !s->root_directory_start_only, | |
1431 | &s->control_pid)) < 0) | |
034c6ed7 LP |
1432 | goto fail; |
1433 | ||
1434 | return; | |
1435 | ||
1436 | fail: | |
87f0e418 | 1437 | log_warning("%s failed to run spawn next executable: %s", unit_id(UNIT(s)), strerror(-r)); |
034c6ed7 LP |
1438 | |
1439 | if (s->state == SERVICE_STOP) | |
1440 | service_enter_stop_post(s, false); | |
1441 | else if (s->state == SERVICE_STOP_POST) | |
1442 | service_enter_dead(s, false, true); | |
1443 | else | |
1444 | service_enter_stop(s, false); | |
5cb5a6ff LP |
1445 | } |
1446 | ||
87f0e418 LP |
1447 | static int service_start(Unit *u) { |
1448 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
1449 | |
1450 | assert(s); | |
1451 | ||
034c6ed7 LP |
1452 | /* We cannot fulfill this request right now, try again later |
1453 | * please! */ | |
1454 | if (s->state == SERVICE_STOP || | |
1455 | s->state == SERVICE_STOP_SIGTERM || | |
1456 | s->state == SERVICE_STOP_SIGKILL || | |
1457 | s->state == SERVICE_STOP_POST || | |
1458 | s->state == SERVICE_FINAL_SIGTERM || | |
1459 | s->state == SERVICE_FINAL_SIGKILL) | |
5cb5a6ff LP |
1460 | return -EAGAIN; |
1461 | ||
034c6ed7 LP |
1462 | /* Already on it! */ |
1463 | if (s->state == SERVICE_START_PRE || | |
1464 | s->state == SERVICE_START || | |
1465 | s->state == SERVICE_START_POST) | |
1466 | return 0; | |
1467 | ||
1468 | assert(s->state == SERVICE_DEAD || s->state == SERVICE_MAINTAINANCE || s->state == SERVICE_AUTO_RESTART); | |
5cb5a6ff | 1469 | |
1e2e8133 LP |
1470 | /* Make sure we don't enter a busy loop of some kind. */ |
1471 | if (!ratelimit_test(&s->ratelimit)) { | |
1472 | log_warning("%s start request repeated too quickly, refusing to start.", unit_id(u)); | |
1473 | return -EAGAIN; | |
1474 | } | |
1475 | ||
034c6ed7 LP |
1476 | s->failure = false; |
1477 | s->main_pid_known = false; | |
1478 | ||
1479 | service_enter_start_pre(s); | |
1480 | return 0; | |
5cb5a6ff LP |
1481 | } |
1482 | ||
87f0e418 LP |
1483 | static int service_stop(Unit *u) { |
1484 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
1485 | |
1486 | assert(s); | |
1487 | ||
e537352b | 1488 | /* Cannot do this now */ |
034c6ed7 LP |
1489 | if (s->state == SERVICE_START_PRE || |
1490 | s->state == SERVICE_START || | |
1491 | s->state == SERVICE_START_POST || | |
1492 | s->state == SERVICE_RELOAD) | |
1493 | return -EAGAIN; | |
1494 | ||
e537352b LP |
1495 | /* Already on it */ |
1496 | if (s->state == SERVICE_STOP || | |
1497 | s->state == SERVICE_STOP_SIGTERM || | |
1498 | s->state == SERVICE_STOP_SIGKILL || | |
1499 | s->state == SERVICE_STOP_POST || | |
1500 | s->state == SERVICE_FINAL_SIGTERM || | |
1501 | s->state == SERVICE_FINAL_SIGKILL) | |
1502 | return 0; | |
1503 | ||
034c6ed7 LP |
1504 | if (s->state == SERVICE_AUTO_RESTART) { |
1505 | service_set_state(s, SERVICE_DEAD); | |
1506 | return 0; | |
1507 | } | |
1508 | ||
1509 | assert(s->state == SERVICE_RUNNING); | |
5cb5a6ff | 1510 | |
034c6ed7 | 1511 | service_enter_stop(s, true); |
5cb5a6ff LP |
1512 | return 0; |
1513 | } | |
1514 | ||
87f0e418 LP |
1515 | static int service_reload(Unit *u) { |
1516 | Service *s = SERVICE(u); | |
034c6ed7 LP |
1517 | |
1518 | assert(s); | |
1519 | ||
1520 | assert(s->state == SERVICE_RUNNING); | |
1521 | ||
1522 | service_enter_reload(s); | |
5cb5a6ff LP |
1523 | return 0; |
1524 | } | |
1525 | ||
87f0e418 LP |
1526 | static bool service_can_reload(Unit *u) { |
1527 | Service *s = SERVICE(u); | |
034c6ed7 LP |
1528 | |
1529 | assert(s); | |
1530 | ||
1531 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
1532 | } | |
1533 | ||
87f0e418 LP |
1534 | static UnitActiveState service_active_state(Unit *u) { |
1535 | assert(u); | |
5cb5a6ff | 1536 | |
acbb0225 | 1537 | return state_translation_table[SERVICE(u)->state]; |
034c6ed7 LP |
1538 | } |
1539 | ||
1540 | static int main_pid_good(Service *s) { | |
1541 | assert(s); | |
1542 | ||
1543 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
1544 | * don't know */ | |
1545 | ||
1546 | /* If we know the pid file, then lets just check if it is | |
1547 | * still valid */ | |
1548 | if (s->main_pid_known) | |
1549 | return s->main_pid > 0; | |
1550 | ||
1551 | /* We don't know the pid */ | |
8e274523 | 1552 | return -EAGAIN; |
034c6ed7 LP |
1553 | } |
1554 | ||
1555 | static bool control_pid_good(Service *s) { | |
1556 | assert(s); | |
5cb5a6ff | 1557 | |
034c6ed7 | 1558 | return s->control_pid > 0; |
5cb5a6ff LP |
1559 | } |
1560 | ||
8e274523 LP |
1561 | static int cgroup_good(Service *s) { |
1562 | assert(s); | |
1563 | ||
1564 | if (s->valid_no_process) | |
1565 | return -EAGAIN; | |
1566 | ||
1567 | return cgroup_bonding_is_empty_list(UNIT(s)->meta.cgroup_bondings); | |
1568 | } | |
1569 | ||
87f0e418 LP |
1570 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
1571 | Service *s = SERVICE(u); | |
034c6ed7 | 1572 | bool success; |
5cb5a6ff LP |
1573 | |
1574 | assert(s); | |
034c6ed7 LP |
1575 | assert(pid >= 0); |
1576 | ||
bd982a8b | 1577 | success = code == CLD_EXITED && status == 0; |
034c6ed7 LP |
1578 | s->failure = s->failure || !success; |
1579 | ||
1580 | if (s->main_pid == pid) { | |
1581 | ||
1582 | exec_status_fill(&s->main_exec_status, pid, code, status); | |
1583 | s->main_pid = 0; | |
1584 | ||
7d55e835 | 1585 | if (s->type == SERVICE_SIMPLE || s->type == SERVICE_FINISH) { |
034c6ed7 LP |
1586 | assert(s->exec_command[SERVICE_EXEC_START]); |
1587 | s->exec_command[SERVICE_EXEC_START]->exec_status = s->main_exec_status; | |
1588 | } | |
1589 | ||
50159e6a | 1590 | log_debug("%s: main process exited, code=%s, status=%i", unit_id(u), sigchld_code_to_string(code), status); |
034c6ed7 LP |
1591 | |
1592 | /* The service exited, so the service is officially | |
1593 | * gone. */ | |
1594 | ||
1595 | switch (s->state) { | |
1596 | ||
1597 | case SERVICE_START_POST: | |
1598 | case SERVICE_RELOAD: | |
1599 | case SERVICE_STOP: | |
1600 | /* Need to wait until the operation is | |
1601 | * done */ | |
1602 | break; | |
1603 | ||
7d55e835 LP |
1604 | case SERVICE_START: |
1605 | assert(s->type == SERVICE_FINISH); | |
1606 | ||
1607 | /* This was our main goal, so let's go on */ | |
1608 | if (success) | |
1609 | service_enter_start_post(s); | |
1610 | else | |
1611 | service_enter_stop(s, false); | |
1612 | break; | |
1613 | ||
034c6ed7 LP |
1614 | case SERVICE_RUNNING: |
1615 | service_enter_stop(s, success); | |
1616 | break; | |
1617 | ||
1618 | case SERVICE_STOP_SIGTERM: | |
1619 | case SERVICE_STOP_SIGKILL: | |
1620 | ||
1621 | if (!control_pid_good(s)) | |
1622 | service_enter_stop_post(s, success); | |
5cb5a6ff | 1623 | |
034c6ed7 LP |
1624 | /* If there is still a control process, wait for that first */ |
1625 | break; | |
5cb5a6ff | 1626 | |
034c6ed7 LP |
1627 | default: |
1628 | assert_not_reached("Uh, main process died at wrong time."); | |
1629 | } | |
5cb5a6ff | 1630 | |
034c6ed7 LP |
1631 | } else if (s->control_pid == pid) { |
1632 | assert(s->control_command); | |
1633 | ||
1634 | exec_status_fill(&s->control_command->exec_status, pid, code, status); | |
1635 | s->control_pid = 0; | |
1636 | ||
94f04347 | 1637 | log_debug("%s: control process exited, code=%s status=%i", unit_id(u), sigchld_code_to_string(code), status); |
034c6ed7 LP |
1638 | |
1639 | /* If we are shutting things down anyway we | |
1640 | * don't care about failing commands. */ | |
1641 | ||
1642 | if (s->control_command->command_next && | |
b866264a | 1643 | (success || (s->state == SERVICE_STOP || s->state == SERVICE_STOP_POST))) |
034c6ed7 LP |
1644 | |
1645 | /* There is another command to * | |
1646 | * execute, so let's do that. */ | |
1647 | ||
1648 | service_run_next(s, success); | |
1649 | ||
1650 | else { | |
1651 | /* No further commands for this step, so let's | |
1652 | * figure out what to do next */ | |
1653 | ||
94f04347 | 1654 | log_debug("%s got final SIGCHLD for state %s", unit_id(u), service_state_to_string(s->state)); |
bd982a8b | 1655 | |
034c6ed7 LP |
1656 | switch (s->state) { |
1657 | ||
1658 | case SERVICE_START_PRE: | |
1659 | if (success) | |
1660 | service_enter_start(s); | |
1661 | else | |
1662 | service_enter_stop(s, false); | |
1663 | break; | |
1664 | ||
1665 | case SERVICE_START: | |
1666 | assert(s->type == SERVICE_FORKING); | |
1667 | ||
1668 | /* Let's try to load the pid | |
1669 | * file here if we can. We | |
1670 | * ignore the return value, | |
1671 | * since the PID file might | |
1672 | * actually be created by a | |
1673 | * START_POST script */ | |
1674 | ||
1675 | if (success) { | |
1676 | if (s->pid_file) | |
1677 | service_load_pid_file(s); | |
1678 | ||
1679 | service_enter_start_post(s); | |
1680 | } else | |
1681 | service_enter_stop(s, false); | |
1682 | ||
1683 | break; | |
1684 | ||
1685 | case SERVICE_START_POST: | |
1686 | if (success && s->pid_file && !s->main_pid_known) { | |
1687 | int r; | |
1688 | ||
1689 | /* Hmm, let's see if we can | |
1690 | * load the pid now after the | |
1691 | * start-post scripts got | |
1692 | * executed. */ | |
1693 | ||
1694 | if ((r = service_load_pid_file(s)) < 0) | |
87f0e418 | 1695 | log_warning("%s: failed to load PID file %s: %s", unit_id(UNIT(s)), s->pid_file, strerror(-r)); |
034c6ed7 LP |
1696 | } |
1697 | ||
1698 | /* Fall through */ | |
1699 | ||
1700 | case SERVICE_RELOAD: | |
1701 | if (success) { | |
8e274523 | 1702 | if (main_pid_good(s) != 0 && cgroup_good(s) != 0) |
034c6ed7 LP |
1703 | service_set_state(s, SERVICE_RUNNING); |
1704 | else | |
1705 | service_enter_stop(s, true); | |
1706 | } else | |
1707 | service_enter_stop(s, false); | |
1708 | ||
1709 | break; | |
1710 | ||
1711 | case SERVICE_STOP: | |
1712 | if (main_pid_good(s) > 0) | |
1713 | /* Still not dead and we know the PID? Let's go hunting. */ | |
1714 | service_enter_signal(s, SERVICE_STOP_SIGTERM, success); | |
1715 | else | |
1716 | service_enter_stop_post(s, success); | |
1717 | break; | |
1718 | ||
1719 | case SERVICE_STOP_SIGTERM: | |
1720 | case SERVICE_STOP_SIGKILL: | |
1721 | if (main_pid_good(s) <= 0) | |
1722 | service_enter_stop_post(s, success); | |
1723 | ||
1724 | /* If there is still a service | |
1725 | * process around, wait until | |
1726 | * that one quit, too */ | |
1727 | break; | |
1728 | ||
1729 | case SERVICE_STOP_POST: | |
1730 | case SERVICE_FINAL_SIGTERM: | |
1731 | case SERVICE_FINAL_SIGKILL: | |
1732 | service_enter_dead(s, success, true); | |
1733 | break; | |
1734 | ||
1735 | default: | |
1736 | assert_not_reached("Uh, control process died at wrong time."); | |
1737 | } | |
1738 | } | |
1739 | } else | |
1740 | assert_not_reached("Got SIGCHLD for unkown PID"); | |
1741 | } | |
1742 | ||
acbb0225 | 1743 | static void service_timer_event(Unit *u, uint64_t elapsed, Watch* w) { |
87f0e418 | 1744 | Service *s = SERVICE(u); |
034c6ed7 LP |
1745 | |
1746 | assert(s); | |
1747 | assert(elapsed == 1); | |
1748 | ||
acbb0225 | 1749 | assert(w == &s->timer_watch); |
034c6ed7 LP |
1750 | |
1751 | switch (s->state) { | |
1752 | ||
1753 | case SERVICE_START_PRE: | |
1754 | case SERVICE_START: | |
1755 | case SERVICE_START_POST: | |
1756 | case SERVICE_RELOAD: | |
87f0e418 | 1757 | log_warning("%s operation timed out. Stopping.", unit_id(u)); |
034c6ed7 LP |
1758 | service_enter_stop(s, false); |
1759 | break; | |
1760 | ||
1761 | case SERVICE_STOP: | |
87f0e418 | 1762 | log_warning("%s stopping timed out. Terminating.", unit_id(u)); |
034c6ed7 LP |
1763 | service_enter_signal(s, SERVICE_STOP_SIGTERM, false); |
1764 | break; | |
1765 | ||
1766 | case SERVICE_STOP_SIGTERM: | |
87f0e418 | 1767 | log_warning("%s stopping timed out. Killing.", unit_id(u)); |
034c6ed7 LP |
1768 | service_enter_signal(s, SERVICE_STOP_SIGKILL, false); |
1769 | break; | |
1770 | ||
1771 | case SERVICE_STOP_SIGKILL: | |
1772 | /* Uh, wie sent a SIGKILL and it is still not gone? | |
1773 | * Must be something we cannot kill, so let's just be | |
1774 | * weirded out and continue */ | |
1775 | ||
87f0e418 | 1776 | log_warning("%s still around after SIGKILL. Ignoring.", unit_id(u)); |
034c6ed7 LP |
1777 | service_enter_stop_post(s, false); |
1778 | break; | |
1779 | ||
1780 | case SERVICE_STOP_POST: | |
87f0e418 | 1781 | log_warning("%s stopping timed out (2). Terminating.", unit_id(u)); |
034c6ed7 LP |
1782 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, false); |
1783 | break; | |
1784 | ||
1785 | case SERVICE_FINAL_SIGTERM: | |
87f0e418 | 1786 | log_warning("%s stopping timed out (2). Killing.", unit_id(u)); |
034c6ed7 LP |
1787 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, false); |
1788 | break; | |
1789 | ||
1790 | case SERVICE_FINAL_SIGKILL: | |
87f0e418 | 1791 | log_warning("%s still around after SIGKILL (2). Entering maintainance mode.", unit_id(u)); |
034c6ed7 LP |
1792 | service_enter_dead(s, false, true); |
1793 | break; | |
1794 | ||
1795 | case SERVICE_AUTO_RESTART: | |
87f0e418 | 1796 | log_debug("%s holdoff time over, scheduling restart.", unit_id(u)); |
034c6ed7 LP |
1797 | service_enter_restart(s); |
1798 | break; | |
1799 | ||
1800 | default: | |
1801 | assert_not_reached("Timeout at wrong time."); | |
1802 | } | |
5cb5a6ff LP |
1803 | } |
1804 | ||
8e274523 LP |
1805 | static void service_cgroup_notify_event(Unit *u) { |
1806 | Service *s = SERVICE(u); | |
1807 | ||
1808 | assert(u); | |
1809 | ||
1810 | log_debug("%s: cgroup is empty", unit_id(u)); | |
1811 | ||
1812 | switch (s->state) { | |
1813 | ||
1814 | /* Waiting for SIGCHLD is usually more interesting, | |
1815 | * because it includes return codes/signals. Which is | |
1816 | * why we ignore the cgroup events for most cases, | |
1817 | * except when we don't know pid which to expect the | |
1818 | * SIGCHLD for. */ | |
1819 | ||
1820 | case SERVICE_RUNNING: | |
1821 | ||
1822 | if (!s->valid_no_process && main_pid_good(s) <= 0) | |
1823 | service_enter_stop(s, true); | |
1824 | ||
1825 | break; | |
1826 | ||
1827 | default: | |
1828 | ; | |
1829 | } | |
1830 | } | |
1831 | ||
2c4104f0 | 1832 | static int service_enumerate(Manager *m) { |
2c4104f0 LP |
1833 | char **p; |
1834 | unsigned i; | |
1835 | DIR *d = NULL; | |
1836 | char *path = NULL, *fpath = NULL, *name = NULL; | |
1837 | int r; | |
1838 | ||
1839 | assert(m); | |
1840 | ||
5e177ece | 1841 | STRV_FOREACH(p, m->sysvrcnd_path) |
23a177ef | 1842 | for (i = 0; i < ELEMENTSOF(rcnd_table); i += 2) { |
2c4104f0 LP |
1843 | struct dirent *de; |
1844 | ||
1845 | free(path); | |
1846 | path = NULL; | |
23a177ef | 1847 | if (asprintf(&path, "%s/%s", *p, rcnd_table[i]) < 0) { |
2c4104f0 LP |
1848 | r = -ENOMEM; |
1849 | goto finish; | |
1850 | } | |
1851 | ||
1852 | if (d) | |
1853 | closedir(d); | |
1854 | ||
1855 | if (!(d = opendir(path))) { | |
1856 | if (errno != ENOENT) | |
1857 | log_warning("opendir() failed on %s: %s", path, strerror(errno)); | |
1858 | ||
1859 | continue; | |
1860 | } | |
1861 | ||
1862 | while ((de = readdir(d))) { | |
1863 | Unit *runlevel, *service; | |
1864 | ||
1865 | if (ignore_file(de->d_name)) | |
1866 | continue; | |
1867 | ||
1868 | if (de->d_name[0] != 'S' && de->d_name[0] != 'K') | |
1869 | continue; | |
1870 | ||
1871 | if (strlen(de->d_name) < 4) | |
1872 | continue; | |
1873 | ||
1874 | free(fpath); | |
1875 | fpath = NULL; | |
23a177ef | 1876 | if (asprintf(&fpath, "%s/%s/%s", *p, rcnd_table[i], de->d_name) < 0) { |
2c4104f0 LP |
1877 | r = -ENOMEM; |
1878 | goto finish; | |
1879 | } | |
1880 | ||
1881 | if (access(fpath, X_OK) < 0) { | |
1882 | ||
1883 | if (errno != ENOENT) | |
1884 | log_warning("access() failed on %s: %s", fpath, strerror(errno)); | |
1885 | ||
1886 | continue; | |
1887 | } | |
1888 | ||
1889 | free(name); | |
1890 | name = NULL; | |
1891 | if (asprintf(&name, "%s.service", de->d_name+3) < 0) { | |
1892 | r = -ENOMEM; | |
1893 | goto finish; | |
1894 | } | |
1895 | ||
1896 | if ((r = manager_load_unit(m, name, &service)) < 0) | |
1897 | goto finish; | |
1898 | ||
23a177ef | 1899 | if ((r = manager_load_unit(m, rcnd_table[i+1], &runlevel)) < 0) |
2c4104f0 LP |
1900 | goto finish; |
1901 | ||
1902 | if (de->d_name[0] == 'S') { | |
1903 | if ((r = unit_add_dependency(runlevel, UNIT_WANTS, service)) < 0) | |
1904 | goto finish; | |
1905 | ||
1906 | if ((r = unit_add_dependency(runlevel, UNIT_AFTER, service)) < 0) | |
1907 | goto finish; | |
23a177ef LP |
1908 | |
1909 | } else if (de->d_name[0] == 'K' && | |
1910 | (streq(rcnd_table[i+1], SPECIAL_RUNLEVEL0_TARGET) || | |
1911 | streq(rcnd_table[i+1], SPECIAL_RUNLEVEL6_TARGET))) { | |
1912 | ||
1913 | /* We honour K links only for | |
1914 | * halt/reboot. For the normal | |
1915 | * runlevels we assume the | |
1916 | * stop jobs will be | |
1917 | * implicitly added by the | |
1918 | * core logic. */ | |
1919 | ||
2c4104f0 LP |
1920 | if ((r = unit_add_dependency(runlevel, UNIT_CONFLICTS, service)) < 0) |
1921 | goto finish; | |
1922 | ||
1923 | if ((r = unit_add_dependency(runlevel, UNIT_BEFORE, service)) < 0) | |
1924 | goto finish; | |
1925 | } | |
1926 | } | |
1927 | } | |
1928 | ||
1929 | r = 0; | |
1930 | ||
1931 | finish: | |
1932 | free(path); | |
1933 | free(fpath); | |
1934 | free(name); | |
1935 | closedir(d); | |
1936 | ||
1937 | return r; | |
1938 | } | |
1939 | ||
94f04347 LP |
1940 | static const char* const service_state_table[_SERVICE_STATE_MAX] = { |
1941 | [SERVICE_DEAD] = "dead", | |
1942 | [SERVICE_START_PRE] = "start-pre", | |
1943 | [SERVICE_START] = "start", | |
1944 | [SERVICE_START_POST] = "start-post", | |
1945 | [SERVICE_RUNNING] = "running", | |
1946 | [SERVICE_RELOAD] = "reload", | |
1947 | [SERVICE_STOP] = "stop", | |
1948 | [SERVICE_STOP_SIGTERM] = "stop-sigterm", | |
1949 | [SERVICE_STOP_SIGKILL] = "stop-sigkill", | |
1950 | [SERVICE_STOP_POST] = "stop-post", | |
1951 | [SERVICE_FINAL_SIGTERM] = "final-sigterm", | |
1952 | [SERVICE_FINAL_SIGKILL] = "final-sigkill", | |
1953 | [SERVICE_MAINTAINANCE] = "maintainance", | |
1954 | [SERVICE_AUTO_RESTART] = "auto-restart", | |
1955 | }; | |
1956 | ||
1957 | DEFINE_STRING_TABLE_LOOKUP(service_state, ServiceState); | |
1958 | ||
1959 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { | |
1960 | [SERVICE_ONCE] = "once", | |
1961 | [SERVICE_RESTART_ON_SUCCESS] = "restart-on-success", | |
1962 | [SERVICE_RESTART_ALWAYS] = "restart-on-failure", | |
1963 | }; | |
1964 | ||
1965 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
1966 | ||
1967 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
1968 | [SERVICE_FORKING] = "forking", | |
1969 | [SERVICE_SIMPLE] = "simple", | |
1970 | [SERVICE_FINISH] = "finish" | |
1971 | }; | |
1972 | ||
1973 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
1974 | ||
e537352b | 1975 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
94f04347 LP |
1976 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", |
1977 | [SERVICE_EXEC_START] = "ExecStart", | |
1978 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
1979 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
1980 | [SERVICE_EXEC_STOP] = "ExecStop", | |
1981 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
1982 | }; | |
1983 | ||
1984 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
1985 | ||
87f0e418 | 1986 | const UnitVTable service_vtable = { |
5cb5a6ff LP |
1987 | .suffix = ".service", |
1988 | ||
034c6ed7 | 1989 | .init = service_init, |
e537352b | 1990 | .load = service_load, |
034c6ed7 LP |
1991 | .done = service_done, |
1992 | ||
5cb5a6ff LP |
1993 | .dump = service_dump, |
1994 | ||
1995 | .start = service_start, | |
1996 | .stop = service_stop, | |
1997 | .reload = service_reload, | |
1998 | ||
034c6ed7 LP |
1999 | .can_reload = service_can_reload, |
2000 | ||
5cb5a6ff LP |
2001 | .active_state = service_active_state, |
2002 | ||
034c6ed7 LP |
2003 | .sigchld_event = service_sigchld_event, |
2004 | .timer_event = service_timer_event, | |
2c4104f0 | 2005 | |
8e274523 LP |
2006 | .cgroup_notify_empty = service_cgroup_notify_event, |
2007 | ||
2c4104f0 | 2008 | .enumerate = service_enumerate |
5cb5a6ff | 2009 | }; |