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