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