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