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