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