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