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