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