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