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