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