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