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