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