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