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