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