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