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