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