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