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