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