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