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