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