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