]>
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 | 24 | #include <unistd.h> |
5cb5a6ff | 25 | |
574634bc | 26 | #include "async.h" |
4f5dd394 LP |
27 | #include "bus-error.h" |
28 | #include "bus-kernel.h" | |
29 | #include "bus-util.h" | |
4139c1b2 | 30 | #include "dbus-service.h" |
f6a6225e | 31 | #include "def.h" |
4d1a6904 | 32 | #include "env-util.h" |
4f5dd394 LP |
33 | #include "escape.h" |
34 | #include "exit-status.h" | |
3ffd4af2 | 35 | #include "fd-util.h" |
a5c32cff | 36 | #include "fileio.h" |
6482f626 | 37 | #include "formats-util.h" |
f4f15635 | 38 | #include "fs-util.h" |
4f5dd394 LP |
39 | #include "load-dropin.h" |
40 | #include "load-fragment.h" | |
41 | #include "log.h" | |
42 | #include "manager.h" | |
6bedfcbb | 43 | #include "parse-util.h" |
4f5dd394 | 44 | #include "path-util.h" |
0b452006 | 45 | #include "process-util.h" |
3ffd4af2 | 46 | #include "service.h" |
24882e06 | 47 | #include "signal-util.h" |
4f5dd394 | 48 | #include "special.h" |
07630cea | 49 | #include "string-util.h" |
4f5dd394 LP |
50 | #include "strv.h" |
51 | #include "unit-name.h" | |
52 | #include "unit-printf.h" | |
53 | #include "unit.h" | |
54 | #include "utf8.h" | |
55 | #include "util.h" | |
034c6ed7 | 56 | |
acbb0225 | 57 | static const UnitActiveState state_translation_table[_SERVICE_STATE_MAX] = { |
87f0e418 LP |
58 | [SERVICE_DEAD] = UNIT_INACTIVE, |
59 | [SERVICE_START_PRE] = UNIT_ACTIVATING, | |
60 | [SERVICE_START] = UNIT_ACTIVATING, | |
61 | [SERVICE_START_POST] = UNIT_ACTIVATING, | |
62 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
80876c20 | 63 | [SERVICE_EXITED] = UNIT_ACTIVE, |
032ff4af | 64 | [SERVICE_RELOAD] = UNIT_RELOADING, |
87f0e418 | 65 | [SERVICE_STOP] = UNIT_DEACTIVATING, |
db2cb23b | 66 | [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING, |
87f0e418 LP |
67 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
68 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
69 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
70 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
71 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
fdf20a31 | 72 | [SERVICE_FAILED] = UNIT_FAILED, |
6124958c | 73 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING |
034c6ed7 | 74 | }; |
5cb5a6ff | 75 | |
e056b01d LP |
76 | /* For Type=idle we never want to delay any other jobs, hence we |
77 | * consider idle jobs active as soon as we start working on them */ | |
78 | static const UnitActiveState state_translation_table_idle[_SERVICE_STATE_MAX] = { | |
79 | [SERVICE_DEAD] = UNIT_INACTIVE, | |
80 | [SERVICE_START_PRE] = UNIT_ACTIVE, | |
81 | [SERVICE_START] = UNIT_ACTIVE, | |
82 | [SERVICE_START_POST] = UNIT_ACTIVE, | |
83 | [SERVICE_RUNNING] = UNIT_ACTIVE, | |
84 | [SERVICE_EXITED] = UNIT_ACTIVE, | |
85 | [SERVICE_RELOAD] = UNIT_RELOADING, | |
86 | [SERVICE_STOP] = UNIT_DEACTIVATING, | |
db2cb23b | 87 | [SERVICE_STOP_SIGABRT] = UNIT_DEACTIVATING, |
e056b01d LP |
88 | [SERVICE_STOP_SIGTERM] = UNIT_DEACTIVATING, |
89 | [SERVICE_STOP_SIGKILL] = UNIT_DEACTIVATING, | |
90 | [SERVICE_STOP_POST] = UNIT_DEACTIVATING, | |
91 | [SERVICE_FINAL_SIGTERM] = UNIT_DEACTIVATING, | |
92 | [SERVICE_FINAL_SIGKILL] = UNIT_DEACTIVATING, | |
93 | [SERVICE_FAILED] = UNIT_FAILED, | |
94 | [SERVICE_AUTO_RESTART] = UNIT_ACTIVATING | |
95 | }; | |
96 | ||
718db961 LP |
97 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata); |
98 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata); | |
99 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata); | |
100 | ||
842129f5 | 101 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f); |
308d72dc | 102 | static void service_enter_reload_by_notify(Service *s); |
842129f5 | 103 | |
a16e1123 LP |
104 | static void service_init(Unit *u) { |
105 | Service *s = SERVICE(u); | |
106 | ||
107 | assert(u); | |
ac155bb8 | 108 | assert(u->load_state == UNIT_STUB); |
a16e1123 | 109 | |
1f19a534 OS |
110 | s->timeout_start_usec = u->manager->default_timeout_start_usec; |
111 | s->timeout_stop_usec = u->manager->default_timeout_stop_usec; | |
112 | s->restart_usec = u->manager->default_restart_usec; | |
0b86feac | 113 | s->type = _SERVICE_TYPE_INVALID; |
a16e1123 | 114 | s->socket_fd = -1; |
e44da745 | 115 | s->bus_endpoint_fd = -1; |
a34ceba6 | 116 | s->stdin_fd = s->stdout_fd = s->stderr_fd = -1; |
3185a36b | 117 | s->guess_main_pid = true; |
a16e1123 | 118 | |
085afe36 | 119 | RATELIMIT_INIT(s->start_limit, u->manager->default_start_limit_interval, u->manager->default_start_limit_burst); |
a16e1123 LP |
120 | |
121 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
122 | } | |
123 | ||
5e94833f LP |
124 | static void service_unwatch_control_pid(Service *s) { |
125 | assert(s); | |
126 | ||
127 | if (s->control_pid <= 0) | |
128 | return; | |
129 | ||
130 | unit_unwatch_pid(UNIT(s), s->control_pid); | |
131 | s->control_pid = 0; | |
132 | } | |
133 | ||
134 | static void service_unwatch_main_pid(Service *s) { | |
135 | assert(s); | |
136 | ||
137 | if (s->main_pid <= 0) | |
138 | return; | |
139 | ||
140 | unit_unwatch_pid(UNIT(s), s->main_pid); | |
141 | s->main_pid = 0; | |
142 | } | |
143 | ||
3e52541e MS |
144 | static void service_unwatch_pid_file(Service *s) { |
145 | if (!s->pid_file_pathspec) | |
146 | return; | |
147 | ||
f2341e0a | 148 | log_unit_debug(UNIT(s), "Stopping watch for PID file %s", s->pid_file_pathspec->path); |
718db961 | 149 | path_spec_unwatch(s->pid_file_pathspec); |
3e52541e | 150 | path_spec_done(s->pid_file_pathspec); |
a1e58e8e | 151 | s->pid_file_pathspec = mfree(s->pid_file_pathspec); |
3e52541e MS |
152 | } |
153 | ||
5925dd3c | 154 | static int service_set_main_pid(Service *s, pid_t pid) { |
e55224ca LP |
155 | pid_t ppid; |
156 | ||
5925dd3c LP |
157 | assert(s); |
158 | ||
159 | if (pid <= 1) | |
160 | return -EINVAL; | |
161 | ||
162 | if (pid == getpid()) | |
163 | return -EINVAL; | |
164 | ||
7400b9d2 LP |
165 | if (s->main_pid == pid && s->main_pid_known) |
166 | return 0; | |
167 | ||
168 | if (s->main_pid != pid) { | |
169 | service_unwatch_main_pid(s); | |
170 | exec_status_start(&s->main_exec_status, pid); | |
171 | } | |
41efeaec | 172 | |
6dfa5494 LP |
173 | s->main_pid = pid; |
174 | s->main_pid_known = true; | |
175 | ||
176 | if (get_parent_of_pid(pid, &ppid) >= 0 && ppid != getpid()) { | |
f2341e0a | 177 | log_unit_warning(UNIT(s), "Supervising process "PID_FMT" which is not our child. We'll most likely not notice when it exits.", pid); |
6dfa5494 LP |
178 | s->main_pid_alien = true; |
179 | } else | |
180 | s->main_pid_alien = false; | |
5925dd3c LP |
181 | |
182 | return 0; | |
183 | } | |
184 | ||
4f2d528d LP |
185 | static void service_close_socket_fd(Service *s) { |
186 | assert(s); | |
187 | ||
574634bc | 188 | s->socket_fd = asynchronous_close(s->socket_fd); |
4f2d528d LP |
189 | } |
190 | ||
6cf6bbc2 LP |
191 | static void service_connection_unref(Service *s) { |
192 | assert(s); | |
193 | ||
9444b1f2 | 194 | if (!UNIT_ISSET(s->accept_socket)) |
6cf6bbc2 LP |
195 | return; |
196 | ||
57020a3a LP |
197 | socket_connection_unref(SOCKET(UNIT_DEREF(s->accept_socket))); |
198 | unit_ref_unset(&s->accept_socket); | |
6cf6bbc2 LP |
199 | } |
200 | ||
a6927d7f MO |
201 | static void service_stop_watchdog(Service *s) { |
202 | assert(s); | |
203 | ||
718db961 | 204 | s->watchdog_event_source = sd_event_source_unref(s->watchdog_event_source); |
842129f5 | 205 | s->watchdog_timestamp = DUAL_TIMESTAMP_NULL; |
a6927d7f MO |
206 | } |
207 | ||
842129f5 | 208 | static void service_start_watchdog(Service *s) { |
bb242b7b MO |
209 | int r; |
210 | ||
211 | assert(s); | |
212 | ||
842129f5 | 213 | if (s->watchdog_usec <= 0) |
bb242b7b MO |
214 | return; |
215 | ||
718db961 LP |
216 | if (s->watchdog_event_source) { |
217 | r = sd_event_source_set_time(s->watchdog_event_source, s->watchdog_timestamp.monotonic + s->watchdog_usec); | |
218 | if (r < 0) { | |
f2341e0a | 219 | log_unit_warning_errno(UNIT(s), r, "Failed to reset watchdog timer: %m"); |
718db961 LP |
220 | return; |
221 | } | |
222 | ||
842129f5 | 223 | r = sd_event_source_set_enabled(s->watchdog_event_source, SD_EVENT_ONESHOT); |
c4ef3317 | 224 | } else { |
6a0f1f6d LP |
225 | r = sd_event_add_time( |
226 | UNIT(s)->manager->event, | |
227 | &s->watchdog_event_source, | |
228 | CLOCK_MONOTONIC, | |
229 | s->watchdog_timestamp.monotonic + s->watchdog_usec, 0, | |
230 | service_dispatch_watchdog, s); | |
c4ef3317 | 231 | if (r < 0) { |
f2341e0a | 232 | log_unit_warning_errno(UNIT(s), r, "Failed to add watchdog timer: %m"); |
c4ef3317 LP |
233 | return; |
234 | } | |
235 | ||
7dfbe2e3 TG |
236 | (void) sd_event_source_set_description(s->watchdog_event_source, "service-watchdog"); |
237 | ||
c4ef3317 LP |
238 | /* Let's process everything else which might be a sign |
239 | * of living before we consider a service died. */ | |
240 | r = sd_event_source_set_priority(s->watchdog_event_source, SD_EVENT_PRIORITY_IDLE); | |
241 | } | |
718db961 | 242 | |
bb242b7b | 243 | if (r < 0) |
f2341e0a | 244 | log_unit_warning_errno(UNIT(s), r, "Failed to install watchdog timer: %m"); |
bb242b7b MO |
245 | } |
246 | ||
a6927d7f MO |
247 | static void service_reset_watchdog(Service *s) { |
248 | assert(s); | |
249 | ||
250 | dual_timestamp_get(&s->watchdog_timestamp); | |
842129f5 | 251 | service_start_watchdog(s); |
a6927d7f MO |
252 | } |
253 | ||
a354329f LP |
254 | static void service_fd_store_unlink(ServiceFDStore *fs) { |
255 | ||
256 | if (!fs) | |
257 | return; | |
258 | ||
259 | if (fs->service) { | |
260 | assert(fs->service->n_fd_store > 0); | |
261 | LIST_REMOVE(fd_store, fs->service->fd_store, fs); | |
262 | fs->service->n_fd_store--; | |
263 | } | |
264 | ||
265 | if (fs->event_source) { | |
266 | sd_event_source_set_enabled(fs->event_source, SD_EVENT_OFF); | |
267 | sd_event_source_unref(fs->event_source); | |
268 | } | |
269 | ||
8dd4c05b | 270 | free(fs->fdname); |
a354329f LP |
271 | safe_close(fs->fd); |
272 | free(fs); | |
273 | } | |
274 | ||
275 | static void service_release_resources(Unit *u) { | |
276 | Service *s = SERVICE(u); | |
277 | ||
278 | assert(s); | |
279 | ||
a34ceba6 | 280 | if (!s->fd_store && s->stdin_fd < 0 && s->stdout_fd < 0 && s->stderr_fd < 0) |
a354329f LP |
281 | return; |
282 | ||
f2341e0a | 283 | log_unit_debug(u, "Releasing all resources."); |
a354329f | 284 | |
a34ceba6 LP |
285 | s->stdin_fd = safe_close(s->stdin_fd); |
286 | s->stdout_fd = safe_close(s->stdout_fd); | |
287 | s->stderr_fd = safe_close(s->stderr_fd); | |
288 | ||
a354329f LP |
289 | while (s->fd_store) |
290 | service_fd_store_unlink(s->fd_store); | |
291 | ||
292 | assert(s->n_fd_store == 0); | |
293 | } | |
294 | ||
87f0e418 LP |
295 | static void service_done(Unit *u) { |
296 | Service *s = SERVICE(u); | |
44d8db9e LP |
297 | |
298 | assert(s); | |
299 | ||
a1e58e8e LP |
300 | s->pid_file = mfree(s->pid_file); |
301 | s->status_text = mfree(s->status_text); | |
302 | s->reboot_arg = mfree(s->reboot_arg); | |
efe6e7d3 | 303 | |
613b411c | 304 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); |
e537352b | 305 | exec_command_free_array(s->exec_command, _SERVICE_EXEC_COMMAND_MAX); |
44d8db9e | 306 | s->control_command = NULL; |
867b3b7d | 307 | s->main_command = NULL; |
44d8db9e | 308 | |
37520c1b LP |
309 | exit_status_set_free(&s->restart_prevent_status); |
310 | exit_status_set_free(&s->restart_force_status); | |
311 | exit_status_set_free(&s->success_status); | |
96342de6 | 312 | |
44d8db9e LP |
313 | /* This will leak a process, but at least no memory or any of |
314 | * our resources */ | |
5e94833f LP |
315 | service_unwatch_main_pid(s); |
316 | service_unwatch_control_pid(s); | |
3e52541e | 317 | service_unwatch_pid_file(s); |
44d8db9e | 318 | |
05e343b7 | 319 | if (s->bus_name) { |
ac155bb8 | 320 | unit_unwatch_bus_name(u, s->bus_name); |
a1e58e8e | 321 | s->bus_name = mfree(s->bus_name); |
05e343b7 LP |
322 | } |
323 | ||
e44da745 | 324 | s->bus_endpoint_fd = safe_close(s->bus_endpoint_fd); |
4f2d528d | 325 | service_close_socket_fd(s); |
6cf6bbc2 | 326 | service_connection_unref(s); |
4f2d528d | 327 | |
57020a3a | 328 | unit_ref_unset(&s->accept_socket); |
f976f3f6 | 329 | |
bb242b7b MO |
330 | service_stop_watchdog(s); |
331 | ||
718db961 | 332 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
a354329f LP |
333 | |
334 | service_release_resources(u); | |
718db961 LP |
335 | } |
336 | ||
2339fc93 LP |
337 | static int on_fd_store_io(sd_event_source *e, int fd, uint32_t revents, void *userdata) { |
338 | ServiceFDStore *fs = userdata; | |
339 | ||
340 | assert(e); | |
341 | assert(fs); | |
342 | ||
343 | /* If we get either EPOLLHUP or EPOLLERR, it's time to remove this entry from the fd store */ | |
344 | service_fd_store_unlink(fs); | |
345 | return 0; | |
346 | } | |
347 | ||
8dd4c05b | 348 | static int service_add_fd_store(Service *s, int fd, const char *name) { |
2339fc93 LP |
349 | ServiceFDStore *fs; |
350 | int r; | |
351 | ||
352 | assert(s); | |
353 | assert(fd >= 0); | |
354 | ||
355 | if (s->n_fd_store >= s->n_fd_store_max) | |
356 | return 0; | |
357 | ||
358 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
359 | r = same_fd(fs->fd, fd); | |
360 | if (r < 0) | |
361 | return r; | |
362 | if (r > 0) { | |
363 | /* Already included */ | |
364 | safe_close(fd); | |
365 | return 1; | |
366 | } | |
367 | } | |
368 | ||
369 | fs = new0(ServiceFDStore, 1); | |
370 | if (!fs) | |
371 | return -ENOMEM; | |
372 | ||
373 | fs->fd = fd; | |
374 | fs->service = s; | |
8dd4c05b | 375 | fs->fdname = strdup(name ?: "stored"); |
17dec0f7 | 376 | if (!fs->fdname) { |
377 | free(fs); | |
8dd4c05b | 378 | return -ENOMEM; |
17dec0f7 | 379 | } |
2339fc93 LP |
380 | |
381 | r = sd_event_add_io(UNIT(s)->manager->event, &fs->event_source, fd, 0, on_fd_store_io, fs); | |
382 | if (r < 0) { | |
8dd4c05b | 383 | free(fs->fdname); |
2339fc93 LP |
384 | free(fs); |
385 | return r; | |
386 | } | |
387 | ||
7dfbe2e3 TG |
388 | (void) sd_event_source_set_description(fs->event_source, "service-fd-store"); |
389 | ||
2339fc93 LP |
390 | LIST_PREPEND(fd_store, s->fd_store, fs); |
391 | s->n_fd_store++; | |
392 | ||
393 | return 1; | |
394 | } | |
395 | ||
8dd4c05b | 396 | static int service_add_fd_store_set(Service *s, FDSet *fds, const char *name) { |
2339fc93 LP |
397 | int r; |
398 | ||
399 | assert(s); | |
400 | ||
401 | if (fdset_size(fds) <= 0) | |
402 | return 0; | |
403 | ||
404 | while (s->n_fd_store < s->n_fd_store_max) { | |
405 | _cleanup_close_ int fd = -1; | |
406 | ||
407 | fd = fdset_steal_first(fds); | |
408 | if (fd < 0) | |
409 | break; | |
410 | ||
8dd4c05b | 411 | r = service_add_fd_store(s, fd, name); |
2339fc93 | 412 | if (r < 0) |
f2341e0a | 413 | return log_unit_error_errno(UNIT(s), r, "Couldn't add fd to fd store: %m"); |
2339fc93 | 414 | if (r > 0) { |
f2341e0a | 415 | log_unit_debug(UNIT(s), "Added fd to fd store."); |
2339fc93 LP |
416 | fd = -1; |
417 | } | |
418 | } | |
419 | ||
420 | if (fdset_size(fds) > 0) | |
f2341e0a | 421 | log_unit_warning(UNIT(s), "Tried to store more fds than FDStoreMax=%u allows, closing remaining.", s->n_fd_store_max); |
2339fc93 LP |
422 | |
423 | return 0; | |
424 | } | |
425 | ||
718db961 LP |
426 | static int service_arm_timer(Service *s, usec_t usec) { |
427 | int r; | |
428 | ||
429 | assert(s); | |
430 | ||
718db961 LP |
431 | if (s->timer_event_source) { |
432 | r = sd_event_source_set_time(s->timer_event_source, now(CLOCK_MONOTONIC) + usec); | |
433 | if (r < 0) | |
434 | return r; | |
435 | ||
436 | return sd_event_source_set_enabled(s->timer_event_source, SD_EVENT_ONESHOT); | |
437 | } | |
438 | ||
7dfbe2e3 | 439 | r = sd_event_add_time( |
6a0f1f6d LP |
440 | UNIT(s)->manager->event, |
441 | &s->timer_event_source, | |
442 | CLOCK_MONOTONIC, | |
443 | now(CLOCK_MONOTONIC) + usec, 0, | |
444 | service_dispatch_timer, s); | |
7dfbe2e3 TG |
445 | if (r < 0) |
446 | return r; | |
447 | ||
448 | (void) sd_event_source_set_description(s->timer_event_source, "service-timer"); | |
449 | ||
450 | return 0; | |
44d8db9e LP |
451 | } |
452 | ||
243b1432 LP |
453 | static int service_verify(Service *s) { |
454 | assert(s); | |
455 | ||
1124fe6f | 456 | if (UNIT(s)->load_state != UNIT_LOADED) |
243b1432 LP |
457 | return 0; |
458 | ||
96fb8242 | 459 | if (!s->exec_command[SERVICE_EXEC_START] && !s->exec_command[SERVICE_EXEC_STOP]) { |
f2341e0a | 460 | log_unit_error(UNIT(s), "Service lacks both ExecStart= and ExecStop= setting. Refusing."); |
243b1432 LP |
461 | return -EINVAL; |
462 | } | |
463 | ||
96fb8242 | 464 | if (s->type != SERVICE_ONESHOT && !s->exec_command[SERVICE_EXEC_START]) { |
f2341e0a | 465 | log_unit_error(UNIT(s), "Service has no ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); |
96fb8242 LP |
466 | return -EINVAL; |
467 | } | |
468 | ||
469 | if (!s->remain_after_exit && !s->exec_command[SERVICE_EXEC_START]) { | |
f2341e0a | 470 | log_unit_error(UNIT(s), "Service has no ExecStart= setting, which is only allowed for RemainAfterExit=yes services. Refusing."); |
96fb8242 LP |
471 | return -EINVAL; |
472 | } | |
473 | ||
474 | if (s->type != SERVICE_ONESHOT && s->exec_command[SERVICE_EXEC_START]->command_next) { | |
f2341e0a | 475 | log_unit_error(UNIT(s), "Service has more than one ExecStart= setting, which is only allowed for Type=oneshot services. Refusing."); |
6cf6bbc2 LP |
476 | return -EINVAL; |
477 | } | |
478 | ||
b0693d30 | 479 | if (s->type == SERVICE_ONESHOT && s->restart != SERVICE_RESTART_NO) { |
f2341e0a | 480 | log_unit_error(UNIT(s), "Service has Restart= setting other than no, which isn't allowed for Type=oneshot services. Refusing."); |
37520c1b LP |
481 | return -EINVAL; |
482 | } | |
483 | ||
55ebf98c | 484 | if (s->type == SERVICE_ONESHOT && !exit_status_set_is_empty(&s->restart_force_status)) { |
f2341e0a | 485 | log_unit_error(UNIT(s), "Service has RestartForceStatus= set, which isn't allowed for Type=oneshot services. Refusing."); |
b0693d30 MW |
486 | return -EINVAL; |
487 | } | |
488 | ||
05e343b7 | 489 | if (s->type == SERVICE_DBUS && !s->bus_name) { |
f2341e0a | 490 | log_unit_error(UNIT(s), "Service is of type D-Bus but no D-Bus service name has been specified. Refusing."); |
4d0e5dbd LP |
491 | return -EINVAL; |
492 | } | |
493 | ||
7e2668c6 | 494 | if (s->bus_name && s->type != SERVICE_DBUS) |
f2341e0a | 495 | log_unit_warning(UNIT(s), "Service has a D-Bus service name specified, but is not of type dbus. Ignoring."); |
7e2668c6 | 496 | |
ebc2259d | 497 | if (s->exec_context.pam_name && !(s->kill_context.kill_mode == KILL_CONTROL_GROUP || s->kill_context.kill_mode == KILL_MIXED)) { |
f2341e0a | 498 | log_unit_error(UNIT(s), "Service has PAM enabled. Kill mode must be set to 'control-group' or 'mixed'. Refusing."); |
05e343b7 LP |
499 | return -EINVAL; |
500 | } | |
501 | ||
6b7e5923 PS |
502 | if (s->usb_function_descriptors && !s->usb_function_strings) |
503 | log_unit_warning(UNIT(s), "Service has USBFunctionDescriptors= setting, but no USBFunctionStrings=. Ignoring."); | |
504 | ||
505 | if (!s->usb_function_descriptors && s->usb_function_strings) | |
506 | log_unit_warning(UNIT(s), "Service has USBFunctionStrings= setting, but no USBFunctionDescriptors=. Ignoring."); | |
507 | ||
243b1432 LP |
508 | return 0; |
509 | } | |
510 | ||
a40eb732 LP |
511 | static int service_add_default_dependencies(Service *s) { |
512 | int r; | |
513 | ||
514 | assert(s); | |
515 | ||
516 | /* Add a number of automatic dependencies useful for the | |
517 | * majority of services. */ | |
518 | ||
519 | /* First, pull in base system */ | |
8bb2d17d | 520 | r = unit_add_two_dependencies_by_name(UNIT(s), UNIT_AFTER, UNIT_REQUIRES, SPECIAL_BASIC_TARGET, NULL, true); |
fccd44ec KS |
521 | if (r < 0) |
522 | return r; | |
a40eb732 LP |
523 | |
524 | /* Second, activate normal shutdown */ | |
8545f7ce | 525 | return unit_add_two_dependencies_by_name(UNIT(s), UNIT_BEFORE, UNIT_CONFLICTS, SPECIAL_SHUTDOWN_TARGET, NULL, true); |
a40eb732 LP |
526 | } |
527 | ||
4dfc092a LP |
528 | static void service_fix_output(Service *s) { |
529 | assert(s); | |
530 | ||
531 | /* If nothing has been explicitly configured, patch default | |
532 | * output in. If input is socket/tty we avoid this however, | |
533 | * since in that case we want output to default to the same | |
534 | * place as we read input from. */ | |
535 | ||
536 | if (s->exec_context.std_error == EXEC_OUTPUT_INHERIT && | |
537 | s->exec_context.std_output == EXEC_OUTPUT_INHERIT && | |
538 | s->exec_context.std_input == EXEC_INPUT_NULL) | |
1124fe6f | 539 | s->exec_context.std_error = UNIT(s)->manager->default_std_error; |
4dfc092a LP |
540 | |
541 | if (s->exec_context.std_output == EXEC_OUTPUT_INHERIT && | |
542 | s->exec_context.std_input == EXEC_INPUT_NULL) | |
1124fe6f | 543 | s->exec_context.std_output = UNIT(s)->manager->default_std_output; |
4dfc092a LP |
544 | } |
545 | ||
8545f7ce LP |
546 | static int service_add_extras(Service *s) { |
547 | int r; | |
548 | ||
549 | assert(s); | |
550 | ||
551 | if (s->type == _SERVICE_TYPE_INVALID) { | |
552 | /* Figure out a type automatically */ | |
553 | if (s->bus_name) | |
554 | s->type = SERVICE_DBUS; | |
555 | else if (s->exec_command[SERVICE_EXEC_START]) | |
556 | s->type = SERVICE_SIMPLE; | |
557 | else | |
558 | s->type = SERVICE_ONESHOT; | |
559 | } | |
560 | ||
561 | /* Oneshot services have disabled start timeout by default */ | |
562 | if (s->type == SERVICE_ONESHOT && !s->start_timeout_defined) | |
563 | s->timeout_start_usec = 0; | |
564 | ||
565 | service_fix_output(s); | |
566 | ||
567 | r = unit_patch_contexts(UNIT(s)); | |
568 | if (r < 0) | |
569 | return r; | |
570 | ||
571 | r = unit_add_exec_dependencies(UNIT(s), &s->exec_context); | |
572 | if (r < 0) | |
573 | return r; | |
574 | ||
d79200e2 | 575 | r = unit_set_default_slice(UNIT(s)); |
8545f7ce LP |
576 | if (r < 0) |
577 | return r; | |
578 | ||
579 | if (s->type == SERVICE_NOTIFY && s->notify_access == NOTIFY_NONE) | |
580 | s->notify_access = NOTIFY_MAIN; | |
581 | ||
582 | if (s->watchdog_usec > 0 && s->notify_access == NOTIFY_NONE) | |
583 | s->notify_access = NOTIFY_MAIN; | |
584 | ||
585 | if (s->bus_name) { | |
586 | const char *n; | |
587 | ||
6962fd3b MB |
588 | n = strjoina(s->bus_name, ".busname"); |
589 | r = unit_add_dependency_by_name(UNIT(s), UNIT_AFTER, n, NULL, true); | |
8545f7ce LP |
590 | if (r < 0) |
591 | return r; | |
592 | ||
6962fd3b | 593 | r = unit_watch_bus_name(UNIT(s), s->bus_name); |
fc1ba5d1 LP |
594 | if (r == -EEXIST) |
595 | return log_unit_error_errno(UNIT(s), r, "Two services allocated for the same bus name %s, refusing operation.", s->bus_name); | |
8545f7ce | 596 | if (r < 0) |
fc1ba5d1 | 597 | return log_unit_error_errno(UNIT(s), r, "Cannot watch bus name %s: %m", s->bus_name); |
8545f7ce LP |
598 | } |
599 | ||
600 | if (UNIT(s)->default_dependencies) { | |
601 | r = service_add_default_dependencies(s); | |
602 | if (r < 0) | |
603 | return r; | |
604 | } | |
605 | ||
606 | return 0; | |
607 | } | |
608 | ||
e537352b | 609 | static int service_load(Unit *u) { |
e537352b | 610 | Service *s = SERVICE(u); |
8bb2d17d | 611 | int r; |
e537352b LP |
612 | |
613 | assert(s); | |
1e2e8133 | 614 | |
5cb5a6ff | 615 | /* Load a .service file */ |
c2756a68 LP |
616 | r = unit_load_fragment(u); |
617 | if (r < 0) | |
5cb5a6ff LP |
618 | return r; |
619 | ||
23a177ef | 620 | /* Still nothing found? Then let's give up */ |
ac155bb8 | 621 | if (u->load_state == UNIT_STUB) |
23a177ef | 622 | return -ENOENT; |
034c6ed7 | 623 | |
23a177ef | 624 | /* This is a new unit? Then let's add in some extras */ |
ac155bb8 | 625 | if (u->load_state == UNIT_LOADED) { |
c2756a68 LP |
626 | |
627 | /* We were able to load something, then let's add in | |
628 | * the dropin directories. */ | |
629 | r = unit_load_dropin(u); | |
630 | if (r < 0) | |
631 | return r; | |
632 | ||
8545f7ce LP |
633 | /* This is a new unit? Then let's add in some |
634 | * extras */ | |
635 | r = service_add_extras(s); | |
598459ce LP |
636 | if (r < 0) |
637 | return r; | |
8e274523 LP |
638 | } |
639 | ||
243b1432 | 640 | return service_verify(s); |
034c6ed7 LP |
641 | } |
642 | ||
87f0e418 | 643 | static void service_dump(Unit *u, FILE *f, const char *prefix) { |
5cb5a6ff | 644 | ServiceExecCommand c; |
87f0e418 | 645 | Service *s = SERVICE(u); |
47be870b | 646 | const char *prefix2; |
5cb5a6ff LP |
647 | |
648 | assert(s); | |
649 | ||
4c940960 | 650 | prefix = strempty(prefix); |
63c372cb | 651 | prefix2 = strjoina(prefix, "\t"); |
44d8db9e | 652 | |
5cb5a6ff | 653 | fprintf(f, |
81a2b7ce | 654 | "%sService State: %s\n" |
f42806df LP |
655 | "%sResult: %s\n" |
656 | "%sReload Result: %s\n" | |
81a2b7ce | 657 | "%sPermissionsStartOnly: %s\n" |
8e274523 | 658 | "%sRootDirectoryStartOnly: %s\n" |
02ee865a | 659 | "%sRemainAfterExit: %s\n" |
3185a36b | 660 | "%sGuessMainPID: %s\n" |
c952c6ec | 661 | "%sType: %s\n" |
2cf3143a | 662 | "%sRestart: %s\n" |
308d72dc LP |
663 | "%sNotifyAccess: %s\n" |
664 | "%sNotifyState: %s\n", | |
81a2b7ce | 665 | prefix, service_state_to_string(s->state), |
f42806df LP |
666 | prefix, service_result_to_string(s->result), |
667 | prefix, service_result_to_string(s->reload_result), | |
81a2b7ce | 668 | prefix, yes_no(s->permissions_start_only), |
8e274523 | 669 | prefix, yes_no(s->root_directory_start_only), |
02ee865a | 670 | prefix, yes_no(s->remain_after_exit), |
3185a36b | 671 | prefix, yes_no(s->guess_main_pid), |
c952c6ec | 672 | prefix, service_type_to_string(s->type), |
2cf3143a | 673 | prefix, service_restart_to_string(s->restart), |
308d72dc LP |
674 | prefix, notify_access_to_string(s->notify_access), |
675 | prefix, notify_state_to_string(s->notify_state)); | |
5cb5a6ff | 676 | |
70123e68 LP |
677 | if (s->control_pid > 0) |
678 | fprintf(f, | |
ccd06097 ZJS |
679 | "%sControl PID: "PID_FMT"\n", |
680 | prefix, s->control_pid); | |
70123e68 LP |
681 | |
682 | if (s->main_pid > 0) | |
683 | fprintf(f, | |
ccd06097 | 684 | "%sMain PID: "PID_FMT"\n" |
6dfa5494 LP |
685 | "%sMain PID Known: %s\n" |
686 | "%sMain PID Alien: %s\n", | |
ccd06097 | 687 | prefix, s->main_pid, |
6dfa5494 LP |
688 | prefix, yes_no(s->main_pid_known), |
689 | prefix, yes_no(s->main_pid_alien)); | |
70123e68 | 690 | |
034c6ed7 LP |
691 | if (s->pid_file) |
692 | fprintf(f, | |
693 | "%sPIDFile: %s\n", | |
694 | prefix, s->pid_file); | |
695 | ||
05e343b7 LP |
696 | if (s->bus_name) |
697 | fprintf(f, | |
698 | "%sBusName: %s\n" | |
699 | "%sBus Name Good: %s\n", | |
700 | prefix, s->bus_name, | |
701 | prefix, yes_no(s->bus_name_good)); | |
702 | ||
4819ff03 | 703 | kill_context_dump(&s->kill_context, f, prefix); |
5cb5a6ff LP |
704 | exec_context_dump(&s->exec_context, f, prefix); |
705 | ||
e537352b | 706 | for (c = 0; c < _SERVICE_EXEC_COMMAND_MAX; c++) { |
5cb5a6ff | 707 | |
44d8db9e LP |
708 | if (!s->exec_command[c]) |
709 | continue; | |
710 | ||
40d50879 | 711 | fprintf(f, "%s-> %s:\n", |
94f04347 | 712 | prefix, service_exec_command_to_string(c)); |
44d8db9e LP |
713 | |
714 | exec_command_dump_list(s->exec_command[c], f, prefix2); | |
5cb5a6ff | 715 | } |
44d8db9e | 716 | |
8c47c732 LP |
717 | if (s->status_text) |
718 | fprintf(f, "%sStatus Text: %s\n", | |
719 | prefix, s->status_text); | |
a354329f | 720 | |
ece174c5 | 721 | if (s->n_fd_store_max > 0) |
a354329f LP |
722 | fprintf(f, |
723 | "%sFile Descriptor Store Max: %u\n" | |
724 | "%sFile Descriptor Store Current: %u\n", | |
725 | prefix, s->n_fd_store_max, | |
726 | prefix, s->n_fd_store); | |
5cb5a6ff LP |
727 | } |
728 | ||
c5419d42 | 729 | static int service_load_pid_file(Service *s, bool may_warn) { |
7fd1b19b | 730 | _cleanup_free_ char *k = NULL; |
034c6ed7 | 731 | int r; |
5925dd3c | 732 | pid_t pid; |
034c6ed7 LP |
733 | |
734 | assert(s); | |
735 | ||
034c6ed7 | 736 | if (!s->pid_file) |
13230d5d | 737 | return -ENOENT; |
034c6ed7 | 738 | |
117dcc57 ZJS |
739 | r = read_one_line_file(s->pid_file, &k); |
740 | if (r < 0) { | |
c5419d42 | 741 | if (may_warn) |
f2341e0a | 742 | log_unit_info_errno(UNIT(s), r, "PID file %s not readable (yet?) after %s: %m", s->pid_file, service_state_to_string(s->state)); |
034c6ed7 | 743 | return r; |
5375410b | 744 | } |
034c6ed7 | 745 | |
5925dd3c | 746 | r = parse_pid(k, &pid); |
bc41f93e ZJS |
747 | if (r < 0) { |
748 | if (may_warn) | |
f2341e0a | 749 | log_unit_info_errno(UNIT(s), r, "Failed to read PID from file %s: %m", s->pid_file); |
5925dd3c | 750 | return r; |
bc41f93e | 751 | } |
406eaf93 | 752 | |
9f5650ae | 753 | if (!pid_is_alive(pid)) { |
c5419d42 | 754 | if (may_warn) |
f2341e0a | 755 | log_unit_info(UNIT(s), "PID "PID_FMT" read from file %s does not exist or is a zombie.", pid, s->pid_file); |
e10c9985 YS |
756 | return -ESRCH; |
757 | } | |
758 | ||
db01f8b3 MS |
759 | if (s->main_pid_known) { |
760 | if (pid == s->main_pid) | |
761 | return 0; | |
762 | ||
f2341e0a | 763 | log_unit_debug(UNIT(s), "Main PID changing: "PID_FMT" -> "PID_FMT, s->main_pid, pid); |
8bb2d17d | 764 | |
db01f8b3 MS |
765 | service_unwatch_main_pid(s); |
766 | s->main_pid_known = false; | |
3a111838 | 767 | } else |
f2341e0a | 768 | log_unit_debug(UNIT(s), "Main PID loaded: "PID_FMT, pid); |
db01f8b3 | 769 | |
117dcc57 ZJS |
770 | r = service_set_main_pid(s, pid); |
771 | if (r < 0) | |
16f6025e LP |
772 | return r; |
773 | ||
117dcc57 | 774 | r = unit_watch_pid(UNIT(s), pid); |
bc41f93e | 775 | if (r < 0) { |
5925dd3c | 776 | /* FIXME: we need to do something here */ |
f2341e0a | 777 | log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" for service: %m", pid); |
5925dd3c | 778 | return r; |
bc41f93e | 779 | } |
034c6ed7 LP |
780 | |
781 | return 0; | |
782 | } | |
783 | ||
4fbf50b3 | 784 | static int service_search_main_pid(Service *s) { |
efdb0237 | 785 | pid_t pid = 0; |
4fbf50b3 LP |
786 | int r; |
787 | ||
788 | assert(s); | |
789 | ||
3185a36b LP |
790 | /* If we know it anyway, don't ever fallback to unreliable |
791 | * heuristics */ | |
4fbf50b3 LP |
792 | if (s->main_pid_known) |
793 | return 0; | |
794 | ||
3185a36b LP |
795 | if (!s->guess_main_pid) |
796 | return 0; | |
797 | ||
4fbf50b3 LP |
798 | assert(s->main_pid <= 0); |
799 | ||
efdb0237 LP |
800 | r = unit_search_main_pid(UNIT(s), &pid); |
801 | if (r < 0) | |
802 | return r; | |
4fbf50b3 | 803 | |
f2341e0a | 804 | log_unit_debug(UNIT(s), "Main PID guessed: "PID_FMT, pid); |
117dcc57 ZJS |
805 | r = service_set_main_pid(s, pid); |
806 | if (r < 0) | |
4fbf50b3 LP |
807 | return r; |
808 | ||
117dcc57 | 809 | r = unit_watch_pid(UNIT(s), pid); |
8bb2d17d | 810 | if (r < 0) { |
4fbf50b3 | 811 | /* FIXME: we need to do something here */ |
f2341e0a | 812 | log_unit_warning_errno(UNIT(s), r, "Failed to watch PID "PID_FMT" from: %m", pid); |
8bb2d17d LP |
813 | return r; |
814 | } | |
815 | ||
816 | return 0; | |
4fbf50b3 LP |
817 | } |
818 | ||
034c6ed7 LP |
819 | static void service_set_state(Service *s, ServiceState state) { |
820 | ServiceState old_state; | |
e056b01d | 821 | const UnitActiveState *table; |
842129f5 | 822 | |
5cb5a6ff LP |
823 | assert(s); |
824 | ||
e056b01d LP |
825 | table = s->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
826 | ||
034c6ed7 | 827 | old_state = s->state; |
5cb5a6ff | 828 | s->state = state; |
034c6ed7 | 829 | |
3a111838 MS |
830 | service_unwatch_pid_file(s); |
831 | ||
842129f5 LP |
832 | if (!IN_SET(state, |
833 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
834 | SERVICE_RELOAD, | |
57614eb1 | 835 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
842129f5 LP |
836 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL, |
837 | SERVICE_AUTO_RESTART)) | |
718db961 | 838 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
034c6ed7 | 839 | |
842129f5 LP |
840 | if (!IN_SET(state, |
841 | SERVICE_START, SERVICE_START_POST, | |
842 | SERVICE_RUNNING, SERVICE_RELOAD, | |
57614eb1 | 843 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf108e55 | 844 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
5e94833f | 845 | service_unwatch_main_pid(s); |
867b3b7d LP |
846 | s->main_command = NULL; |
847 | } | |
034c6ed7 | 848 | |
842129f5 LP |
849 | if (!IN_SET(state, |
850 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
851 | SERVICE_RELOAD, | |
57614eb1 | 852 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
842129f5 | 853 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
5e94833f | 854 | service_unwatch_control_pid(s); |
034c6ed7 | 855 | s->control_command = NULL; |
a16e1123 | 856 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; |
e537352b | 857 | } |
034c6ed7 | 858 | |
a911bb9a LP |
859 | if (IN_SET(state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
860 | unit_unwatch_all_pids(UNIT(s)); | |
861 | ||
842129f5 LP |
862 | if (!IN_SET(state, |
863 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
864 | SERVICE_RUNNING, SERVICE_RELOAD, | |
57614eb1 LP |
865 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
866 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL) && | |
1124fe6f | 867 | !(state == SERVICE_DEAD && UNIT(s)->job)) { |
4f2d528d | 868 | service_close_socket_fd(s); |
6cf6bbc2 LP |
869 | service_connection_unref(s); |
870 | } | |
4f2d528d | 871 | |
7596e9e1 | 872 | if (!IN_SET(state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
a6927d7f MO |
873 | service_stop_watchdog(s); |
874 | ||
f6023656 LP |
875 | /* For the inactive states unit_notify() will trim the cgroup, |
876 | * but for exit we have to do that ourselves... */ | |
1124fe6f | 877 | if (state == SERVICE_EXITED && UNIT(s)->manager->n_reloading <= 0) |
efdb0237 | 878 | unit_prune_cgroup(UNIT(s)); |
f6023656 | 879 | |
9cd86184 OB |
880 | /* For remain_after_exit services, let's see if we can "release" the |
881 | * hold on the console, since unit_notify() only does that in case of | |
882 | * change of state */ | |
f49650ce LP |
883 | if (state == SERVICE_EXITED && |
884 | s->remain_after_exit && | |
9cd86184 | 885 | UNIT(s)->manager->n_on_console > 0) { |
f49650ce LP |
886 | |
887 | ExecContext *ec; | |
888 | ||
889 | ec = unit_get_exec_context(UNIT(s)); | |
9cd86184 OB |
890 | if (ec && exec_context_may_touch_console(ec)) { |
891 | Manager *m = UNIT(s)->manager; | |
892 | ||
893 | m->n_on_console --; | |
894 | if (m->n_on_console == 0) | |
895 | /* unset no_console_output flag, since the console is free */ | |
896 | m->no_console_output = false; | |
897 | } | |
898 | } | |
899 | ||
e537352b | 900 | if (old_state != state) |
f2341e0a | 901 | log_unit_debug(UNIT(s), "Changed %s -> %s", service_state_to_string(old_state), service_state_to_string(state)); |
acbb0225 | 902 | |
e056b01d | 903 | unit_notify(UNIT(s), table[old_state], table[state], s->reload_result == SERVICE_SUCCESS); |
034c6ed7 LP |
904 | } |
905 | ||
be847e82 | 906 | static int service_coldplug(Unit *u) { |
a16e1123 LP |
907 | Service *s = SERVICE(u); |
908 | int r; | |
909 | ||
910 | assert(s); | |
911 | assert(s->state == SERVICE_DEAD); | |
912 | ||
913 | if (s->deserialized_state != s->state) { | |
914 | ||
bf108e55 LP |
915 | if (IN_SET(s->deserialized_state, |
916 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
917 | SERVICE_RELOAD, | |
57614eb1 | 918 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf108e55 | 919 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
92c1622e LP |
920 | |
921 | usec_t k; | |
6c12b52e | 922 | |
bf108e55 | 923 | k = IN_SET(s->deserialized_state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec; |
e558336f | 924 | |
92c1622e LP |
925 | /* For the start/stop timeouts 0 means off */ |
926 | if (k > 0) { | |
927 | r = service_arm_timer(s, k); | |
36697dc0 | 928 | if (r < 0) |
e558336f LP |
929 | return r; |
930 | } | |
931 | } | |
a16e1123 | 932 | |
92c1622e LP |
933 | if (s->deserialized_state == SERVICE_AUTO_RESTART) { |
934 | ||
935 | /* The restart timeouts 0 means immediately */ | |
936 | r = service_arm_timer(s, s->restart_usec); | |
937 | if (r < 0) | |
938 | return r; | |
939 | } | |
940 | ||
9f5650ae | 941 | if (pid_is_unwaited(s->main_pid) && |
bf108e55 LP |
942 | ((s->deserialized_state == SERVICE_START && IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_ONESHOT, SERVICE_NOTIFY)) || |
943 | IN_SET(s->deserialized_state, | |
944 | SERVICE_START, SERVICE_START_POST, | |
945 | SERVICE_RUNNING, SERVICE_RELOAD, | |
57614eb1 | 946 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf108e55 LP |
947 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL))) { |
948 | r = unit_watch_pid(UNIT(s), s->main_pid); | |
949 | if (r < 0) | |
950 | return r; | |
951 | } | |
a16e1123 | 952 | |
9f5650ae | 953 | if (pid_is_unwaited(s->control_pid) && |
bf108e55 LP |
954 | IN_SET(s->deserialized_state, |
955 | SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, | |
956 | SERVICE_RELOAD, | |
57614eb1 | 957 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, |
bf108e55 LP |
958 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) { |
959 | r = unit_watch_pid(UNIT(s), s->control_pid); | |
960 | if (r < 0) | |
961 | return r; | |
962 | } | |
a16e1123 | 963 | |
a911bb9a LP |
964 | if (!IN_SET(s->deserialized_state, SERVICE_DEAD, SERVICE_FAILED, SERVICE_AUTO_RESTART)) |
965 | unit_watch_all_pids(UNIT(s)); | |
966 | ||
842129f5 LP |
967 | if (IN_SET(s->deserialized_state, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) |
968 | service_start_watchdog(s); | |
bb242b7b | 969 | |
a16e1123 LP |
970 | service_set_state(s, s->deserialized_state); |
971 | } | |
92c1622e | 972 | |
a16e1123 LP |
973 | return 0; |
974 | } | |
975 | ||
8dd4c05b LP |
976 | static int service_collect_fds(Service *s, int **fds, char ***fd_names) { |
977 | _cleanup_strv_free_ char **rfd_names = NULL; | |
a354329f | 978 | _cleanup_free_ int *rfds = NULL; |
8dd4c05b | 979 | int rn_fds = 0, r; |
44d8db9e LP |
980 | |
981 | assert(s); | |
982 | assert(fds); | |
8dd4c05b | 983 | assert(fd_names); |
44d8db9e | 984 | |
8dd4c05b | 985 | if (s->socket_fd >= 0) { |
6cf6bbc2 | 986 | |
8dd4c05b | 987 | /* Pass the per-connection socket */ |
57020a3a | 988 | |
8dd4c05b LP |
989 | rfds = new(int, 1); |
990 | if (!rfds) | |
991 | return -ENOMEM; | |
992 | rfds[0] = s->socket_fd; | |
57020a3a | 993 | |
8dd4c05b LP |
994 | rfd_names = strv_new("connection", NULL); |
995 | if (!rfd_names) | |
996 | return -ENOMEM; | |
44d8db9e | 997 | |
8dd4c05b LP |
998 | rn_fds = 1; |
999 | } else { | |
1000 | Iterator i; | |
1001 | Unit *u; | |
44d8db9e | 1002 | |
8dd4c05b | 1003 | /* Pass all our configured sockets for singleton services */ |
44d8db9e | 1004 | |
8dd4c05b LP |
1005 | SET_FOREACH(u, UNIT(s)->dependencies[UNIT_TRIGGERED_BY], i) { |
1006 | _cleanup_free_ int *cfds = NULL; | |
1007 | Socket *sock; | |
1008 | int cn_fds; | |
44d8db9e | 1009 | |
8dd4c05b LP |
1010 | if (u->type != UNIT_SOCKET) |
1011 | continue; | |
44d8db9e | 1012 | |
8dd4c05b | 1013 | sock = SOCKET(u); |
a354329f | 1014 | |
8dd4c05b LP |
1015 | cn_fds = socket_collect_fds(sock, &cfds); |
1016 | if (cn_fds < 0) | |
1017 | return cn_fds; | |
44d8db9e | 1018 | |
8dd4c05b LP |
1019 | if (cn_fds <= 0) |
1020 | continue; | |
79c7626d | 1021 | |
8dd4c05b LP |
1022 | if (!rfds) { |
1023 | rfds = cfds; | |
1024 | rn_fds = cn_fds; | |
1025 | ||
1026 | cfds = NULL; | |
1027 | } else { | |
1028 | int *t; | |
1029 | ||
1030 | t = realloc(rfds, (rn_fds + cn_fds) * sizeof(int)); | |
1031 | if (!t) | |
1032 | return -ENOMEM; | |
1033 | ||
1034 | memcpy(t + rn_fds, cfds, cn_fds * sizeof(int)); | |
1035 | ||
1036 | rfds = t; | |
1037 | rn_fds += cn_fds; | |
1038 | } | |
1039 | ||
1040 | r = strv_extend_n(&rfd_names, socket_fdname(sock), cn_fds); | |
1041 | if (r < 0) | |
1042 | return r; | |
44d8db9e LP |
1043 | } |
1044 | } | |
1045 | ||
a354329f LP |
1046 | if (s->n_fd_store > 0) { |
1047 | ServiceFDStore *fs; | |
8dd4c05b | 1048 | char **nl; |
a354329f LP |
1049 | int *t; |
1050 | ||
1051 | t = realloc(rfds, (rn_fds + s->n_fd_store) * sizeof(int)); | |
1052 | if (!t) | |
1053 | return -ENOMEM; | |
1054 | ||
1055 | rfds = t; | |
8dd4c05b LP |
1056 | |
1057 | nl = realloc(rfd_names, (rn_fds + s->n_fd_store + 1) * sizeof(char*)); | |
1058 | if (!nl) | |
1059 | return -ENOMEM; | |
1060 | ||
1061 | rfd_names = nl; | |
1062 | ||
1063 | LIST_FOREACH(fd_store, fs, s->fd_store) { | |
1064 | rfds[rn_fds] = fs->fd; | |
1065 | rfd_names[rn_fds] = strdup(strempty(fs->fdname)); | |
1066 | if (!rfd_names[rn_fds]) | |
1067 | return -ENOMEM; | |
1068 | ||
1069 | rn_fds++; | |
1070 | } | |
1071 | ||
1072 | rfd_names[rn_fds] = NULL; | |
a354329f LP |
1073 | } |
1074 | ||
44d8db9e | 1075 | *fds = rfds; |
8dd4c05b | 1076 | *fd_names = rfd_names; |
3e33402a | 1077 | |
a354329f | 1078 | rfds = NULL; |
8dd4c05b | 1079 | rfd_names = NULL; |
79c7626d LP |
1080 | |
1081 | return rn_fds; | |
44d8db9e LP |
1082 | } |
1083 | ||
81a2b7ce LP |
1084 | static int service_spawn( |
1085 | Service *s, | |
1086 | ExecCommand *c, | |
21b2ce39 | 1087 | usec_t timeout, |
81a2b7ce LP |
1088 | bool pass_fds, |
1089 | bool apply_permissions, | |
1090 | bool apply_chroot, | |
1e3ad081 | 1091 | bool apply_tty_stdin, |
ecedd90f | 1092 | bool is_control, |
81a2b7ce LP |
1093 | pid_t *_pid) { |
1094 | ||
8dd4c05b | 1095 | _cleanup_strv_free_ char **argv = NULL, **final_env = NULL, **our_env = NULL, **fd_names = NULL; |
e44da745 | 1096 | _cleanup_free_ char *bus_endpoint_path = NULL; |
8dd4c05b LP |
1097 | _cleanup_free_ int *fds = NULL; |
1098 | unsigned n_fds = 0, n_env = 0; | |
4ad49000 | 1099 | const char *path; |
8dd4c05b LP |
1100 | pid_t pid; |
1101 | ||
9fa95f85 | 1102 | ExecParameters exec_params = { |
a34ceba6 LP |
1103 | .apply_permissions = apply_permissions, |
1104 | .apply_chroot = apply_chroot, | |
1105 | .apply_tty_stdin = apply_tty_stdin, | |
1106 | .bus_endpoint_fd = -1, | |
1107 | .stdin_fd = -1, | |
1108 | .stdout_fd = -1, | |
1109 | .stderr_fd = -1, | |
9fa95f85 | 1110 | }; |
034c6ed7 | 1111 | |
8dd4c05b LP |
1112 | int r; |
1113 | ||
034c6ed7 LP |
1114 | assert(s); |
1115 | assert(c); | |
1116 | assert(_pid); | |
1117 | ||
5ad096b3 LP |
1118 | (void) unit_realize_cgroup(UNIT(s)); |
1119 | if (s->reset_cpu_usage) { | |
1120 | (void) unit_reset_cpu_usage(UNIT(s)); | |
1121 | s->reset_cpu_usage = false; | |
1122 | } | |
4ad49000 | 1123 | |
613b411c LP |
1124 | r = unit_setup_exec_runtime(UNIT(s)); |
1125 | if (r < 0) | |
1126 | goto fail; | |
1127 | ||
6cf6bbc2 LP |
1128 | if (pass_fds || |
1129 | s->exec_context.std_input == EXEC_INPUT_SOCKET || | |
1130 | s->exec_context.std_output == EXEC_OUTPUT_SOCKET || | |
1131 | s->exec_context.std_error == EXEC_OUTPUT_SOCKET) { | |
1132 | ||
8dd4c05b LP |
1133 | r = service_collect_fds(s, &fds, &fd_names); |
1134 | if (r < 0) | |
1135 | goto fail; | |
6cf6bbc2 | 1136 | |
8dd4c05b | 1137 | n_fds = r; |
4f2d528d | 1138 | } |
44d8db9e | 1139 | |
21b2ce39 LP |
1140 | if (timeout > 0) { |
1141 | r = service_arm_timer(s, timeout); | |
92c1622e LP |
1142 | if (r < 0) |
1143 | goto fail; | |
1144 | } else | |
1145 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); | |
034c6ed7 | 1146 | |
19f6d710 LP |
1147 | r = unit_full_printf_strv(UNIT(s), c->argv, &argv); |
1148 | if (r < 0) | |
9e2f7c11 | 1149 | goto fail; |
9e2f7c11 | 1150 | |
3b1c5241 | 1151 | our_env = new0(char*, 6); |
97ae63e2 | 1152 | if (!our_env) { |
2105e76a LP |
1153 | r = -ENOMEM; |
1154 | goto fail; | |
1155 | } | |
c952c6ec | 1156 | |
a158dbf1 | 1157 | if (is_control ? s->notify_access == NOTIFY_ALL : s->notify_access != NOTIFY_NONE) |
1124fe6f | 1158 | if (asprintf(our_env + n_env++, "NOTIFY_SOCKET=%s", UNIT(s)->manager->notify_socket) < 0) { |
c952c6ec LP |
1159 | r = -ENOMEM; |
1160 | goto fail; | |
1161 | } | |
1162 | ||
2105e76a | 1163 | if (s->main_pid > 0) |
ccd06097 | 1164 | if (asprintf(our_env + n_env++, "MAINPID="PID_FMT, s->main_pid) < 0) { |
c952c6ec LP |
1165 | r = -ENOMEM; |
1166 | goto fail; | |
1167 | } | |
2105e76a | 1168 | |
b2c23da8 | 1169 | if (UNIT(s)->manager->running_as != MANAGER_SYSTEM) |
ccd06097 | 1170 | if (asprintf(our_env + n_env++, "MANAGERPID="PID_FMT, getpid()) < 0) { |
97ae63e2 LP |
1171 | r = -ENOMEM; |
1172 | goto fail; | |
1173 | } | |
1174 | ||
8dd4c05b | 1175 | if (s->socket_fd >= 0) { |
3b1c5241 SL |
1176 | union sockaddr_union sa; |
1177 | socklen_t salen = sizeof(sa); | |
1178 | ||
1179 | r = getpeername(s->socket_fd, &sa.sa, &salen); | |
1180 | if (r < 0) { | |
1181 | r = -errno; | |
1182 | goto fail; | |
1183 | } | |
1184 | ||
1185 | if (IN_SET(sa.sa.sa_family, AF_INET, AF_INET6)) { | |
1186 | _cleanup_free_ char *addr = NULL; | |
1187 | char *t; | |
1188 | int port; | |
1189 | ||
1190 | r = sockaddr_pretty(&sa.sa, salen, true, false, &addr); | |
1191 | if (r < 0) | |
1192 | goto fail; | |
1193 | ||
1194 | t = strappend("REMOTE_ADDR=", addr); | |
1195 | if (!t) { | |
1196 | r = -ENOMEM; | |
1197 | goto fail; | |
1198 | } | |
1199 | our_env[n_env++] = t; | |
1200 | ||
1201 | port = sockaddr_port(&sa.sa); | |
1202 | if (port < 0) { | |
1203 | r = port; | |
1204 | goto fail; | |
1205 | } | |
1206 | ||
1207 | if (asprintf(&t, "REMOTE_PORT=%u", port) < 0) { | |
1208 | r = -ENOMEM; | |
1209 | goto fail; | |
1210 | } | |
1211 | our_env[n_env++] = t; | |
1212 | } | |
1213 | } | |
1214 | ||
97ae63e2 LP |
1215 | final_env = strv_env_merge(2, UNIT(s)->manager->environment, our_env, NULL); |
1216 | if (!final_env) { | |
2105e76a LP |
1217 | r = -ENOMEM; |
1218 | goto fail; | |
1219 | } | |
c952c6ec | 1220 | |
4ad49000 | 1221 | if (is_control && UNIT(s)->cgroup_path) { |
63c372cb | 1222 | path = strjoina(UNIT(s)->cgroup_path, "/control"); |
ac5b0c13 | 1223 | (void) cg_create(SYSTEMD_CGROUP_CONTROLLER, path); |
4ad49000 LP |
1224 | } else |
1225 | path = UNIT(s)->cgroup_path; | |
1226 | ||
e44da745 | 1227 | if (s->exec_context.bus_endpoint) { |
b2c23da8 | 1228 | r = bus_kernel_create_endpoint(UNIT(s)->manager->running_as == MANAGER_SYSTEM ? "system" : "user", |
e44da745 DM |
1229 | UNIT(s)->id, &bus_endpoint_path); |
1230 | if (r < 0) | |
1231 | goto fail; | |
1232 | ||
1233 | /* Pass the fd to the exec_params so that the child process can upload the policy. | |
1234 | * Keep a reference to the fd in the service, so the endpoint is kept alive as long | |
1235 | * as the service is running. */ | |
1236 | exec_params.bus_endpoint_fd = s->bus_endpoint_fd = r; | |
1237 | } | |
e44da745 | 1238 | |
9fa95f85 DM |
1239 | exec_params.argv = argv; |
1240 | exec_params.fds = fds; | |
8dd4c05b | 1241 | exec_params.fd_names = fd_names; |
9fa95f85 DM |
1242 | exec_params.n_fds = n_fds; |
1243 | exec_params.environment = final_env; | |
1244 | exec_params.confirm_spawn = UNIT(s)->manager->confirm_spawn; | |
1245 | exec_params.cgroup_supported = UNIT(s)->manager->cgroup_supported; | |
1246 | exec_params.cgroup_path = path; | |
a931ad47 | 1247 | exec_params.cgroup_delegate = s->cgroup_context.delegate; |
9fa95f85 | 1248 | exec_params.runtime_prefix = manager_get_runtime_prefix(UNIT(s)->manager); |
9fa95f85 | 1249 | exec_params.watchdog_usec = s->watchdog_usec; |
e44da745 | 1250 | exec_params.bus_endpoint_path = bus_endpoint_path; |
a34ceba6 | 1251 | exec_params.selinux_context_net = s->socket_fd_selinux_context_net; |
9fa95f85 DM |
1252 | if (s->type == SERVICE_IDLE) |
1253 | exec_params.idle_pipe = UNIT(s)->manager->idle_pipe; | |
a34ceba6 LP |
1254 | exec_params.stdin_fd = s->stdin_fd; |
1255 | exec_params.stdout_fd = s->stdout_fd; | |
1256 | exec_params.stderr_fd = s->stderr_fd; | |
9fa95f85 | 1257 | |
f2341e0a LP |
1258 | r = exec_spawn(UNIT(s), |
1259 | c, | |
9e2f7c11 | 1260 | &s->exec_context, |
9fa95f85 | 1261 | &exec_params, |
613b411c | 1262 | s->exec_runtime, |
9e2f7c11 | 1263 | &pid); |
9e2f7c11 | 1264 | if (r < 0) |
034c6ed7 LP |
1265 | goto fail; |
1266 | ||
117dcc57 ZJS |
1267 | r = unit_watch_pid(UNIT(s), pid); |
1268 | if (r < 0) | |
034c6ed7 LP |
1269 | /* FIXME: we need to do something here */ |
1270 | goto fail; | |
1271 | ||
1272 | *_pid = pid; | |
1273 | ||
5cb5a6ff | 1274 | return 0; |
034c6ed7 LP |
1275 | |
1276 | fail: | |
1277 | if (timeout) | |
718db961 | 1278 | s->timer_event_source = sd_event_source_unref(s->timer_event_source); |
034c6ed7 LP |
1279 | |
1280 | return r; | |
1281 | } | |
1282 | ||
80876c20 LP |
1283 | static int main_pid_good(Service *s) { |
1284 | assert(s); | |
1285 | ||
1286 | /* Returns 0 if the pid is dead, 1 if it is good, -1 if we | |
1287 | * don't know */ | |
1288 | ||
fc08079e | 1289 | /* If we know the pid file, then let's just check if it is |
80876c20 | 1290 | * still valid */ |
6dfa5494 LP |
1291 | if (s->main_pid_known) { |
1292 | ||
1293 | /* If it's an alien child let's check if it is still | |
1294 | * alive ... */ | |
62220cf7 | 1295 | if (s->main_pid_alien && s->main_pid > 0) |
9f5650ae | 1296 | return pid_is_alive(s->main_pid); |
6dfa5494 LP |
1297 | |
1298 | /* .. otherwise assume we'll get a SIGCHLD for it, | |
1299 | * which we really should wait for to collect exit | |
1300 | * status and code */ | |
80876c20 | 1301 | return s->main_pid > 0; |
6dfa5494 | 1302 | } |
80876c20 LP |
1303 | |
1304 | /* We don't know the pid */ | |
1305 | return -EAGAIN; | |
1306 | } | |
1307 | ||
44a6b1b6 | 1308 | _pure_ static int control_pid_good(Service *s) { |
80876c20 LP |
1309 | assert(s); |
1310 | ||
1311 | return s->control_pid > 0; | |
1312 | } | |
1313 | ||
1314 | static int cgroup_good(Service *s) { | |
1315 | int r; | |
1316 | ||
1317 | assert(s); | |
1318 | ||
4ad49000 LP |
1319 | if (!UNIT(s)->cgroup_path) |
1320 | return 0; | |
1321 | ||
6f883237 | 1322 | r = cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, UNIT(s)->cgroup_path); |
117dcc57 | 1323 | if (r < 0) |
80876c20 LP |
1324 | return r; |
1325 | ||
1326 | return !r; | |
1327 | } | |
1328 | ||
a509f0e6 LP |
1329 | static bool service_shall_restart(Service *s) { |
1330 | assert(s); | |
1331 | ||
1332 | /* Don't restart after manual stops */ | |
1333 | if (s->forbid_restart) | |
1334 | return false; | |
1335 | ||
1336 | /* Never restart if this is configured as special exception */ | |
1337 | if (exit_status_set_test(&s->restart_prevent_status, s->main_exec_status.code, s->main_exec_status.status)) | |
1338 | return false; | |
1339 | ||
1340 | /* Restart if the exit code/status are configured as restart triggers */ | |
1341 | if (exit_status_set_test(&s->restart_force_status, s->main_exec_status.code, s->main_exec_status.status)) | |
1342 | return true; | |
1343 | ||
1344 | switch (s->restart) { | |
1345 | ||
1346 | case SERVICE_RESTART_NO: | |
1347 | return false; | |
1348 | ||
1349 | case SERVICE_RESTART_ALWAYS: | |
1350 | return true; | |
1351 | ||
1352 | case SERVICE_RESTART_ON_SUCCESS: | |
1353 | return s->result == SERVICE_SUCCESS; | |
1354 | ||
1355 | case SERVICE_RESTART_ON_FAILURE: | |
1356 | return s->result != SERVICE_SUCCESS; | |
1357 | ||
1358 | case SERVICE_RESTART_ON_ABNORMAL: | |
1359 | return !IN_SET(s->result, SERVICE_SUCCESS, SERVICE_FAILURE_EXIT_CODE); | |
1360 | ||
1361 | case SERVICE_RESTART_ON_WATCHDOG: | |
1362 | return s->result == SERVICE_FAILURE_WATCHDOG; | |
1363 | ||
1364 | case SERVICE_RESTART_ON_ABORT: | |
1365 | return IN_SET(s->result, SERVICE_FAILURE_SIGNAL, SERVICE_FAILURE_CORE_DUMP); | |
1366 | ||
1367 | default: | |
1368 | assert_not_reached("unknown restart setting"); | |
1369 | } | |
1370 | } | |
1371 | ||
f42806df | 1372 | static void service_enter_dead(Service *s, ServiceResult f, bool allow_restart) { |
034c6ed7 LP |
1373 | int r; |
1374 | assert(s); | |
1375 | ||
f42806df LP |
1376 | if (f != SERVICE_SUCCESS) |
1377 | s->result = f; | |
034c6ed7 | 1378 | |
0c7f15b3 MS |
1379 | service_set_state(s, s->result != SERVICE_SUCCESS ? SERVICE_FAILED : SERVICE_DEAD); |
1380 | ||
2928b0a8 | 1381 | if (s->result != SERVICE_SUCCESS) { |
f2341e0a | 1382 | log_unit_warning(UNIT(s), "Failed with result '%s'.", service_result_to_string(s->result)); |
2928b0a8 LP |
1383 | failure_action(UNIT(s)->manager, s->failure_action, s->reboot_arg); |
1384 | } | |
93ae25e6 | 1385 | |
a509f0e6 | 1386 | if (allow_restart && service_shall_restart(s)) { |
034c6ed7 | 1387 | |
718db961 | 1388 | r = service_arm_timer(s, s->restart_usec); |
f42806df | 1389 | if (r < 0) |
034c6ed7 LP |
1390 | goto fail; |
1391 | ||
1392 | service_set_state(s, SERVICE_AUTO_RESTART); | |
0c7f15b3 | 1393 | } |
034c6ed7 | 1394 | |
f2341e0a | 1395 | /* The next restart might not be a manual stop, hence reset the flag indicating manual stops */ |
47342320 LP |
1396 | s->forbid_restart = false; |
1397 | ||
e66cf1a3 | 1398 | /* We want fresh tmpdirs in case service is started again immediately */ |
613b411c LP |
1399 | exec_runtime_destroy(s->exec_runtime); |
1400 | s->exec_runtime = exec_runtime_unref(s->exec_runtime); | |
c17ec25e | 1401 | |
e66cf1a3 LP |
1402 | /* Also, remove the runtime directory in */ |
1403 | exec_context_destroy_runtime_directory(&s->exec_context, manager_get_runtime_prefix(UNIT(s)->manager)); | |
1404 | ||
9285c9ff LN |
1405 | /* Try to delete the pid file. At this point it will be |
1406 | * out-of-date, and some software might be confused by it, so | |
1407 | * let's remove it. */ | |
1408 | if (s->pid_file) | |
1409 | unlink_noerrno(s->pid_file); | |
1410 | ||
034c6ed7 LP |
1411 | return; |
1412 | ||
1413 | fail: | |
f2341e0a | 1414 | log_unit_warning_errno(UNIT(s), r, "Failed to run install restart timer: %m"); |
f42806df | 1415 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1416 | } |
1417 | ||
f42806df | 1418 | static void service_enter_stop_post(Service *s, ServiceResult f) { |
034c6ed7 LP |
1419 | int r; |
1420 | assert(s); | |
1421 | ||
f42806df LP |
1422 | if (f != SERVICE_SUCCESS) |
1423 | s->result = f; | |
034c6ed7 | 1424 | |
5e94833f | 1425 | service_unwatch_control_pid(s); |
a911bb9a | 1426 | unit_watch_all_pids(UNIT(s)); |
5e94833f | 1427 | |
117dcc57 ZJS |
1428 | s->control_command = s->exec_command[SERVICE_EXEC_STOP_POST]; |
1429 | if (s->control_command) { | |
867b3b7d LP |
1430 | s->control_command_id = SERVICE_EXEC_STOP_POST; |
1431 | ||
ecedd90f LP |
1432 | r = service_spawn(s, |
1433 | s->control_command, | |
21b2ce39 | 1434 | s->timeout_stop_usec, |
ecedd90f LP |
1435 | false, |
1436 | !s->permissions_start_only, | |
1437 | !s->root_directory_start_only, | |
1438 | true, | |
ecedd90f LP |
1439 | true, |
1440 | &s->control_pid); | |
1441 | if (r < 0) | |
034c6ed7 LP |
1442 | goto fail; |
1443 | ||
80876c20 LP |
1444 | service_set_state(s, SERVICE_STOP_POST); |
1445 | } else | |
ac84d1fb | 1446 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1447 | |
1448 | return; | |
1449 | ||
1450 | fail: | |
f2341e0a | 1451 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop-post' task: %m"); |
f42806df | 1452 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1453 | } |
1454 | ||
4940c0b0 LP |
1455 | static int state_to_kill_operation(ServiceState state) { |
1456 | switch (state) { | |
1457 | ||
1458 | case SERVICE_STOP_SIGABRT: | |
1459 | return KILL_ABORT; | |
1460 | ||
1461 | case SERVICE_STOP_SIGTERM: | |
1462 | case SERVICE_FINAL_SIGTERM: | |
1463 | return KILL_TERMINATE; | |
1464 | ||
1465 | case SERVICE_STOP_SIGKILL: | |
1466 | case SERVICE_FINAL_SIGKILL: | |
1467 | return KILL_KILL; | |
1468 | ||
1469 | default: | |
1470 | return _KILL_OPERATION_INVALID; | |
1471 | } | |
1472 | } | |
1473 | ||
f42806df | 1474 | static void service_enter_signal(Service *s, ServiceState state, ServiceResult f) { |
034c6ed7 | 1475 | int r; |
034c6ed7 LP |
1476 | |
1477 | assert(s); | |
1478 | ||
f42806df LP |
1479 | if (f != SERVICE_SUCCESS) |
1480 | s->result = f; | |
034c6ed7 | 1481 | |
a911bb9a LP |
1482 | unit_watch_all_pids(UNIT(s)); |
1483 | ||
cd2086fe LP |
1484 | r = unit_kill_context( |
1485 | UNIT(s), | |
1486 | &s->kill_context, | |
4940c0b0 | 1487 | state_to_kill_operation(state), |
cd2086fe LP |
1488 | s->main_pid, |
1489 | s->control_pid, | |
1490 | s->main_pid_alien); | |
ac84d1fb | 1491 | |
cd2086fe LP |
1492 | if (r < 0) |
1493 | goto fail; | |
034c6ed7 | 1494 | |
cd2086fe | 1495 | if (r > 0) { |
d568a335 | 1496 | if (s->timeout_stop_usec > 0) { |
718db961 | 1497 | r = service_arm_timer(s, s->timeout_stop_usec); |
d568a335 | 1498 | if (r < 0) |
e558336f | 1499 | goto fail; |
d568a335 | 1500 | } |
d6ea93e3 | 1501 | |
80876c20 | 1502 | service_set_state(s, state); |
1db0db4b | 1503 | } else if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM) && s->kill_context.send_sigkill) |
ac84d1fb | 1504 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_SUCCESS); |
1db0db4b | 1505 | else if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 1506 | service_enter_stop_post(s, SERVICE_SUCCESS); |
1db0db4b | 1507 | else if (state == SERVICE_FINAL_SIGTERM && s->kill_context.send_sigkill) |
ac84d1fb | 1508 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_SUCCESS); |
80876c20 | 1509 | else |
f42806df | 1510 | service_enter_dead(s, SERVICE_SUCCESS, true); |
034c6ed7 LP |
1511 | |
1512 | return; | |
1513 | ||
1514 | fail: | |
f2341e0a | 1515 | log_unit_warning_errno(UNIT(s), r, "Failed to kill processes: %m"); |
034c6ed7 | 1516 | |
a00973af | 1517 | if (IN_SET(state, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL)) |
f42806df | 1518 | service_enter_stop_post(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 1519 | else |
f42806df | 1520 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
1521 | } |
1522 | ||
308d72dc LP |
1523 | static void service_enter_stop_by_notify(Service *s) { |
1524 | assert(s); | |
1525 | ||
1526 | unit_watch_all_pids(UNIT(s)); | |
1527 | ||
1528 | if (s->timeout_stop_usec > 0) | |
1529 | service_arm_timer(s, s->timeout_stop_usec); | |
1530 | ||
6041a7ee MS |
1531 | /* The service told us it's stopping, so it's as if we SIGTERM'd it. */ |
1532 | service_set_state(s, SERVICE_STOP_SIGTERM); | |
308d72dc LP |
1533 | } |
1534 | ||
f42806df | 1535 | static void service_enter_stop(Service *s, ServiceResult f) { |
034c6ed7 | 1536 | int r; |
5925dd3c | 1537 | |
034c6ed7 LP |
1538 | assert(s); |
1539 | ||
f42806df LP |
1540 | if (f != SERVICE_SUCCESS) |
1541 | s->result = f; | |
034c6ed7 | 1542 | |
5e94833f | 1543 | service_unwatch_control_pid(s); |
a911bb9a | 1544 | unit_watch_all_pids(UNIT(s)); |
5e94833f | 1545 | |
117dcc57 ZJS |
1546 | s->control_command = s->exec_command[SERVICE_EXEC_STOP]; |
1547 | if (s->control_command) { | |
867b3b7d LP |
1548 | s->control_command_id = SERVICE_EXEC_STOP; |
1549 | ||
ecedd90f LP |
1550 | r = service_spawn(s, |
1551 | s->control_command, | |
21b2ce39 | 1552 | s->timeout_stop_usec, |
ecedd90f LP |
1553 | false, |
1554 | !s->permissions_start_only, | |
1555 | !s->root_directory_start_only, | |
1556 | false, | |
ecedd90f LP |
1557 | true, |
1558 | &s->control_pid); | |
1559 | if (r < 0) | |
034c6ed7 LP |
1560 | goto fail; |
1561 | ||
80876c20 LP |
1562 | service_set_state(s, SERVICE_STOP); |
1563 | } else | |
f42806df | 1564 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
034c6ed7 LP |
1565 | |
1566 | return; | |
1567 | ||
1568 | fail: | |
f2341e0a | 1569 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'stop' task: %m"); |
f42806df | 1570 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1571 | } |
1572 | ||
957c3cf9 LP |
1573 | static bool service_good(Service *s) { |
1574 | int main_pid_ok; | |
1575 | assert(s); | |
1576 | ||
1577 | if (s->type == SERVICE_DBUS && !s->bus_name_good) | |
1578 | return false; | |
1579 | ||
1580 | main_pid_ok = main_pid_good(s); | |
1581 | if (main_pid_ok > 0) /* It's alive */ | |
1582 | return true; | |
1583 | if (main_pid_ok == 0) /* It's dead */ | |
1584 | return false; | |
1585 | ||
1586 | /* OK, we don't know anything about the main PID, maybe | |
1587 | * because there is none. Let's check the control group | |
1588 | * instead. */ | |
1589 | ||
1590 | return cgroup_good(s) != 0; | |
1591 | } | |
1592 | ||
f42806df | 1593 | static void service_enter_running(Service *s, ServiceResult f) { |
80876c20 LP |
1594 | assert(s); |
1595 | ||
f42806df LP |
1596 | if (f != SERVICE_SUCCESS) |
1597 | s->result = f; | |
80876c20 | 1598 | |
957c3cf9 | 1599 | if (service_good(s)) { |
308d72dc LP |
1600 | |
1601 | /* If there are any queued up sd_notify() | |
1602 | * notifications, process them now */ | |
1603 | if (s->notify_state == NOTIFY_RELOADING) | |
1604 | service_enter_reload_by_notify(s); | |
1605 | else if (s->notify_state == NOTIFY_STOPPING) | |
1606 | service_enter_stop_by_notify(s); | |
1607 | else | |
1608 | service_set_state(s, SERVICE_RUNNING); | |
1609 | ||
1610 | } else if (s->remain_after_exit) | |
80876c20 LP |
1611 | service_set_state(s, SERVICE_EXITED); |
1612 | else | |
f42806df | 1613 | service_enter_stop(s, SERVICE_SUCCESS); |
80876c20 LP |
1614 | } |
1615 | ||
034c6ed7 LP |
1616 | static void service_enter_start_post(Service *s) { |
1617 | int r; | |
1618 | assert(s); | |
1619 | ||
5e94833f | 1620 | service_unwatch_control_pid(s); |
842129f5 | 1621 | service_reset_watchdog(s); |
bb242b7b | 1622 | |
117dcc57 ZJS |
1623 | s->control_command = s->exec_command[SERVICE_EXEC_START_POST]; |
1624 | if (s->control_command) { | |
867b3b7d LP |
1625 | s->control_command_id = SERVICE_EXEC_START_POST; |
1626 | ||
ecedd90f LP |
1627 | r = service_spawn(s, |
1628 | s->control_command, | |
21b2ce39 | 1629 | s->timeout_start_usec, |
ecedd90f LP |
1630 | false, |
1631 | !s->permissions_start_only, | |
1632 | !s->root_directory_start_only, | |
1633 | false, | |
ecedd90f LP |
1634 | true, |
1635 | &s->control_pid); | |
1636 | if (r < 0) | |
034c6ed7 LP |
1637 | goto fail; |
1638 | ||
80876c20 LP |
1639 | service_set_state(s, SERVICE_START_POST); |
1640 | } else | |
f42806df | 1641 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
1642 | |
1643 | return; | |
1644 | ||
1645 | fail: | |
f2341e0a | 1646 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-post' task: %m"); |
f42806df | 1647 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1648 | } |
1649 | ||
4ad49000 LP |
1650 | static void service_kill_control_processes(Service *s) { |
1651 | char *p; | |
1652 | ||
1653 | if (!UNIT(s)->cgroup_path) | |
1654 | return; | |
1655 | ||
63c372cb | 1656 | p = strjoina(UNIT(s)->cgroup_path, "/control"); |
4ad49000 LP |
1657 | cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, p, SIGKILL, true, true, true, NULL); |
1658 | } | |
1659 | ||
034c6ed7 | 1660 | static void service_enter_start(Service *s) { |
4ad49000 | 1661 | ExecCommand *c; |
034c6ed7 LP |
1662 | pid_t pid; |
1663 | int r; | |
1664 | ||
1665 | assert(s); | |
1666 | ||
41efeaec LP |
1667 | service_unwatch_control_pid(s); |
1668 | service_unwatch_main_pid(s); | |
80876c20 | 1669 | |
8f53a7b8 LP |
1670 | /* We want to ensure that nobody leaks processes from |
1671 | * START_PRE here, so let's go on a killing spree, People | |
1672 | * should not spawn long running processes from START_PRE. */ | |
4ad49000 | 1673 | service_kill_control_processes(s); |
8f53a7b8 | 1674 | |
867b3b7d LP |
1675 | if (s->type == SERVICE_FORKING) { |
1676 | s->control_command_id = SERVICE_EXEC_START; | |
1677 | c = s->control_command = s->exec_command[SERVICE_EXEC_START]; | |
1678 | ||
1679 | s->main_command = NULL; | |
1680 | } else { | |
1681 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
1682 | s->control_command = NULL; | |
1683 | ||
1684 | c = s->main_command = s->exec_command[SERVICE_EXEC_START]; | |
1685 | } | |
34e9ba66 | 1686 | |
96fb8242 LP |
1687 | if (!c) { |
1688 | assert(s->type == SERVICE_ONESHOT); | |
1689 | service_enter_start_post(s); | |
1690 | return; | |
1691 | } | |
1692 | ||
ecedd90f LP |
1693 | r = service_spawn(s, |
1694 | c, | |
21b2ce39 | 1695 | IN_SET(s->type, SERVICE_FORKING, SERVICE_DBUS, SERVICE_NOTIFY, SERVICE_ONESHOT) ? s->timeout_start_usec : 0, |
ecedd90f LP |
1696 | true, |
1697 | true, | |
1698 | true, | |
1699 | true, | |
ecedd90f LP |
1700 | false, |
1701 | &pid); | |
1702 | if (r < 0) | |
034c6ed7 LP |
1703 | goto fail; |
1704 | ||
f2b68789 | 1705 | if (s->type == SERVICE_SIMPLE || s->type == SERVICE_IDLE) { |
034c6ed7 LP |
1706 | /* For simple services we immediately start |
1707 | * the START_POST binaries. */ | |
1708 | ||
5925dd3c | 1709 | service_set_main_pid(s, pid); |
034c6ed7 LP |
1710 | service_enter_start_post(s); |
1711 | ||
1712 | } else if (s->type == SERVICE_FORKING) { | |
1713 | ||
1714 | /* For forking services we wait until the start | |
1715 | * process exited. */ | |
1716 | ||
e55224ca | 1717 | s->control_pid = pid; |
80876c20 LP |
1718 | service_set_state(s, SERVICE_START); |
1719 | ||
34e9ba66 | 1720 | } else if (s->type == SERVICE_ONESHOT || |
8c47c732 LP |
1721 | s->type == SERVICE_DBUS || |
1722 | s->type == SERVICE_NOTIFY) { | |
7d55e835 | 1723 | |
34e9ba66 | 1724 | /* For oneshot services we wait until the start |
7d55e835 LP |
1725 | * process exited, too, but it is our main process. */ |
1726 | ||
05e343b7 | 1727 | /* For D-Bus services we know the main pid right away, |
8c47c732 LP |
1728 | * but wait for the bus name to appear on the |
1729 | * bus. Notify services are similar. */ | |
05e343b7 | 1730 | |
5925dd3c | 1731 | service_set_main_pid(s, pid); |
80876c20 | 1732 | service_set_state(s, SERVICE_START); |
034c6ed7 LP |
1733 | } else |
1734 | assert_not_reached("Unknown service type"); | |
1735 | ||
1736 | return; | |
1737 | ||
1738 | fail: | |
f2341e0a | 1739 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start' task: %m"); |
f42806df | 1740 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 LP |
1741 | } |
1742 | ||
1743 | static void service_enter_start_pre(Service *s) { | |
1744 | int r; | |
1745 | ||
1746 | assert(s); | |
1747 | ||
5e94833f LP |
1748 | service_unwatch_control_pid(s); |
1749 | ||
117dcc57 ZJS |
1750 | s->control_command = s->exec_command[SERVICE_EXEC_START_PRE]; |
1751 | if (s->control_command) { | |
8f53a7b8 LP |
1752 | /* Before we start anything, let's clear up what might |
1753 | * be left from previous runs. */ | |
4ad49000 | 1754 | service_kill_control_processes(s); |
8f53a7b8 | 1755 | |
867b3b7d LP |
1756 | s->control_command_id = SERVICE_EXEC_START_PRE; |
1757 | ||
ecedd90f LP |
1758 | r = service_spawn(s, |
1759 | s->control_command, | |
21b2ce39 | 1760 | s->timeout_start_usec, |
ecedd90f LP |
1761 | false, |
1762 | !s->permissions_start_only, | |
1763 | !s->root_directory_start_only, | |
1764 | true, | |
ecedd90f LP |
1765 | true, |
1766 | &s->control_pid); | |
1767 | if (r < 0) | |
034c6ed7 LP |
1768 | goto fail; |
1769 | ||
80876c20 LP |
1770 | service_set_state(s, SERVICE_START_PRE); |
1771 | } else | |
034c6ed7 LP |
1772 | service_enter_start(s); |
1773 | ||
1774 | return; | |
1775 | ||
1776 | fail: | |
f2341e0a | 1777 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'start-pre' task: %m"); |
f42806df | 1778 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
034c6ed7 LP |
1779 | } |
1780 | ||
1781 | static void service_enter_restart(Service *s) { | |
718db961 | 1782 | _cleanup_bus_error_free_ sd_bus_error error = SD_BUS_ERROR_NULL; |
034c6ed7 | 1783 | int r; |
398ef8ba | 1784 | |
034c6ed7 LP |
1785 | assert(s); |
1786 | ||
a8bb2e65 LP |
1787 | if (UNIT(s)->job && UNIT(s)->job->type == JOB_STOP) { |
1788 | /* Don't restart things if we are going down anyway */ | |
f2341e0a | 1789 | log_unit_info(UNIT(s), "Stop job pending for unit, delaying automatic restart."); |
2edfa366 | 1790 | |
718db961 | 1791 | r = service_arm_timer(s, s->restart_usec); |
a8bb2e65 | 1792 | if (r < 0) |
2edfa366 | 1793 | goto fail; |
feae8adb DW |
1794 | |
1795 | return; | |
2edfa366 LP |
1796 | } |
1797 | ||
48bb5876 DW |
1798 | /* Any units that are bound to this service must also be |
1799 | * restarted. We use JOB_RESTART (instead of the more obvious | |
1800 | * JOB_START) here so that those dependency jobs will be added | |
1801 | * as well. */ | |
1802 | r = manager_add_job(UNIT(s)->manager, JOB_RESTART, UNIT(s), JOB_FAIL, false, &error, NULL); | |
1803 | if (r < 0) | |
034c6ed7 LP |
1804 | goto fail; |
1805 | ||
a8bb2e65 LP |
1806 | /* Note that we stay in the SERVICE_AUTO_RESTART state here, |
1807 | * it will be canceled as part of the service_stop() call that | |
1808 | * is executed as part of JOB_RESTART. */ | |
1809 | ||
f2341e0a | 1810 | log_unit_debug(UNIT(s), "Scheduled restart job."); |
034c6ed7 LP |
1811 | return; |
1812 | ||
1813 | fail: | |
f2341e0a | 1814 | log_unit_warning(UNIT(s), "Failed to schedule restart job: %s", bus_error_message(&error, -r)); |
f42806df | 1815 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, false); |
034c6ed7 LP |
1816 | } |
1817 | ||
308d72dc LP |
1818 | static void service_enter_reload_by_notify(Service *s) { |
1819 | assert(s); | |
1820 | ||
1821 | if (s->timeout_start_usec > 0) | |
1822 | service_arm_timer(s, s->timeout_start_usec); | |
1823 | ||
1824 | service_set_state(s, SERVICE_RELOAD); | |
1825 | } | |
1826 | ||
034c6ed7 LP |
1827 | static void service_enter_reload(Service *s) { |
1828 | int r; | |
1829 | ||
1830 | assert(s); | |
1831 | ||
5e94833f | 1832 | service_unwatch_control_pid(s); |
95c906ae | 1833 | s->reload_result = SERVICE_SUCCESS; |
5e94833f | 1834 | |
117dcc57 ZJS |
1835 | s->control_command = s->exec_command[SERVICE_EXEC_RELOAD]; |
1836 | if (s->control_command) { | |
867b3b7d LP |
1837 | s->control_command_id = SERVICE_EXEC_RELOAD; |
1838 | ||
ecedd90f LP |
1839 | r = service_spawn(s, |
1840 | s->control_command, | |
21b2ce39 | 1841 | s->timeout_start_usec, |
ecedd90f LP |
1842 | false, |
1843 | !s->permissions_start_only, | |
1844 | !s->root_directory_start_only, | |
1845 | false, | |
ecedd90f LP |
1846 | true, |
1847 | &s->control_pid); | |
1848 | if (r < 0) | |
034c6ed7 LP |
1849 | goto fail; |
1850 | ||
80876c20 LP |
1851 | service_set_state(s, SERVICE_RELOAD); |
1852 | } else | |
f42806df | 1853 | service_enter_running(s, SERVICE_SUCCESS); |
034c6ed7 LP |
1854 | |
1855 | return; | |
1856 | ||
1857 | fail: | |
f2341e0a | 1858 | log_unit_warning_errno(UNIT(s), r, "Failed to run 'reload' task: %m"); |
f42806df LP |
1859 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
1860 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
1861 | } |
1862 | ||
f42806df | 1863 | static void service_run_next_control(Service *s) { |
034c6ed7 LP |
1864 | int r; |
1865 | ||
1866 | assert(s); | |
1867 | assert(s->control_command); | |
1868 | assert(s->control_command->command_next); | |
1869 | ||
34e9ba66 | 1870 | assert(s->control_command_id != SERVICE_EXEC_START); |
034c6ed7 | 1871 | |
34e9ba66 | 1872 | s->control_command = s->control_command->command_next; |
5e94833f LP |
1873 | service_unwatch_control_pid(s); |
1874 | ||
ecedd90f LP |
1875 | r = service_spawn(s, |
1876 | s->control_command, | |
21b2ce39 | 1877 | IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD) ? s->timeout_start_usec : s->timeout_stop_usec, |
ecedd90f LP |
1878 | false, |
1879 | !s->permissions_start_only, | |
1880 | !s->root_directory_start_only, | |
1881 | s->control_command_id == SERVICE_EXEC_START_PRE || | |
1882 | s->control_command_id == SERVICE_EXEC_STOP_POST, | |
ecedd90f LP |
1883 | true, |
1884 | &s->control_pid); | |
1885 | if (r < 0) | |
034c6ed7 LP |
1886 | goto fail; |
1887 | ||
1888 | return; | |
1889 | ||
1890 | fail: | |
f2341e0a | 1891 | log_unit_warning_errno(UNIT(s), r, "Failed to run next control task: %m"); |
034c6ed7 | 1892 | |
80876c20 | 1893 | if (s->state == SERVICE_START_PRE) |
f42806df | 1894 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
80876c20 | 1895 | else if (s->state == SERVICE_STOP) |
f42806df | 1896 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
034c6ed7 | 1897 | else if (s->state == SERVICE_STOP_POST) |
f42806df | 1898 | service_enter_dead(s, SERVICE_FAILURE_RESOURCES, true); |
e2f3b44c | 1899 | else if (s->state == SERVICE_RELOAD) { |
f42806df LP |
1900 | s->reload_result = SERVICE_FAILURE_RESOURCES; |
1901 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c | 1902 | } else |
f42806df | 1903 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
5cb5a6ff LP |
1904 | } |
1905 | ||
f42806df | 1906 | static void service_run_next_main(Service *s) { |
34e9ba66 LP |
1907 | pid_t pid; |
1908 | int r; | |
1909 | ||
1910 | assert(s); | |
867b3b7d LP |
1911 | assert(s->main_command); |
1912 | assert(s->main_command->command_next); | |
1913 | assert(s->type == SERVICE_ONESHOT); | |
34e9ba66 | 1914 | |
867b3b7d | 1915 | s->main_command = s->main_command->command_next; |
34e9ba66 LP |
1916 | service_unwatch_main_pid(s); |
1917 | ||
ecedd90f LP |
1918 | r = service_spawn(s, |
1919 | s->main_command, | |
21b2ce39 | 1920 | s->timeout_start_usec, |
ecedd90f LP |
1921 | true, |
1922 | true, | |
1923 | true, | |
1924 | true, | |
ecedd90f LP |
1925 | false, |
1926 | &pid); | |
1927 | if (r < 0) | |
34e9ba66 LP |
1928 | goto fail; |
1929 | ||
1930 | service_set_main_pid(s, pid); | |
1931 | ||
1932 | return; | |
1933 | ||
1934 | fail: | |
f2341e0a | 1935 | log_unit_warning_errno(UNIT(s), r, "Failed to run next main task: %m"); |
f42806df | 1936 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
34e9ba66 LP |
1937 | } |
1938 | ||
93ae25e6 MO |
1939 | static int service_start_limit_test(Service *s) { |
1940 | assert(s); | |
1941 | ||
1942 | if (ratelimit_test(&s->start_limit)) | |
1943 | return 0; | |
1944 | ||
f2341e0a | 1945 | log_unit_warning(UNIT(s), "Start request repeated too quickly."); |
2928b0a8 LP |
1946 | |
1947 | return failure_action(UNIT(s)->manager, s->start_limit_action, s->reboot_arg); | |
93ae25e6 MO |
1948 | } |
1949 | ||
87f0e418 LP |
1950 | static int service_start(Unit *u) { |
1951 | Service *s = SERVICE(u); | |
4b939747 | 1952 | int r; |
5cb5a6ff LP |
1953 | |
1954 | assert(s); | |
1955 | ||
034c6ed7 LP |
1956 | /* We cannot fulfill this request right now, try again later |
1957 | * please! */ | |
a00973af LP |
1958 | if (IN_SET(s->state, |
1959 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
1960 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) | |
5cb5a6ff LP |
1961 | return -EAGAIN; |
1962 | ||
034c6ed7 | 1963 | /* Already on it! */ |
a00973af | 1964 | if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST)) |
034c6ed7 LP |
1965 | return 0; |
1966 | ||
2e9d6c12 | 1967 | /* A service that will be restarted must be stopped first to |
7f2cddae | 1968 | * trigger BindsTo and/or OnFailure dependencies. If a user |
2e9d6c12 | 1969 | * does not want to wait for the holdoff time to elapse, the |
d4943dc7 LP |
1970 | * service should be manually restarted, not started. We |
1971 | * simply return EAGAIN here, so that any start jobs stay | |
1972 | * queued, and assume that the auto restart timer will | |
1973 | * eventually trigger the restart. */ | |
1974 | if (s->state == SERVICE_AUTO_RESTART) | |
a8bb2e65 | 1975 | return -EAGAIN; |
2e9d6c12 | 1976 | |
a00973af | 1977 | assert(IN_SET(s->state, SERVICE_DEAD, SERVICE_FAILED)); |
5cb5a6ff | 1978 | |
1e2e8133 | 1979 | /* Make sure we don't enter a busy loop of some kind. */ |
4b939747 | 1980 | r = service_start_limit_test(s); |
c2f34808 | 1981 | if (r < 0) { |
8d1b002a | 1982 | service_enter_dead(s, SERVICE_FAILURE_START_LIMIT, false); |
4b939747 | 1983 | return r; |
c2f34808 | 1984 | } |
1e2e8133 | 1985 | |
f42806df LP |
1986 | s->result = SERVICE_SUCCESS; |
1987 | s->reload_result = SERVICE_SUCCESS; | |
034c6ed7 | 1988 | s->main_pid_known = false; |
6dfa5494 | 1989 | s->main_pid_alien = false; |
47342320 | 1990 | s->forbid_restart = false; |
5ad096b3 | 1991 | s->reset_cpu_usage = true; |
034c6ed7 | 1992 | |
a1e58e8e | 1993 | s->status_text = mfree(s->status_text); |
8cfdb077 LP |
1994 | s->status_errno = 0; |
1995 | ||
308d72dc LP |
1996 | s->notify_state = NOTIFY_UNKNOWN; |
1997 | ||
034c6ed7 | 1998 | service_enter_start_pre(s); |
82a2b6bb | 1999 | return 1; |
5cb5a6ff LP |
2000 | } |
2001 | ||
87f0e418 LP |
2002 | static int service_stop(Unit *u) { |
2003 | Service *s = SERVICE(u); | |
5cb5a6ff LP |
2004 | |
2005 | assert(s); | |
2006 | ||
a509f0e6 | 2007 | /* Don't create restart jobs from manual stops. */ |
47342320 | 2008 | s->forbid_restart = true; |
034c6ed7 | 2009 | |
e537352b | 2010 | /* Already on it */ |
a00973af LP |
2011 | if (IN_SET(s->state, |
2012 | SERVICE_STOP, SERVICE_STOP_SIGABRT, SERVICE_STOP_SIGTERM, SERVICE_STOP_SIGKILL, SERVICE_STOP_POST, | |
2013 | SERVICE_FINAL_SIGTERM, SERVICE_FINAL_SIGKILL)) | |
e537352b LP |
2014 | return 0; |
2015 | ||
f0c7b229 | 2016 | /* A restart will be scheduled or is in progress. */ |
034c6ed7 | 2017 | if (s->state == SERVICE_AUTO_RESTART) { |
0c7f15b3 | 2018 | service_set_state(s, SERVICE_DEAD); |
034c6ed7 LP |
2019 | return 0; |
2020 | } | |
2021 | ||
3f6c78dc LP |
2022 | /* If there's already something running we go directly into |
2023 | * kill mode. */ | |
a00973af | 2024 | if (IN_SET(s->state, SERVICE_START_PRE, SERVICE_START, SERVICE_START_POST, SERVICE_RELOAD)) { |
f42806df | 2025 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_SUCCESS); |
3f6c78dc LP |
2026 | return 0; |
2027 | } | |
5cb5a6ff | 2028 | |
a00973af | 2029 | assert(IN_SET(s->state, SERVICE_RUNNING, SERVICE_EXITED)); |
3a762661 | 2030 | |
f42806df | 2031 | service_enter_stop(s, SERVICE_SUCCESS); |
82a2b6bb | 2032 | return 1; |
5cb5a6ff LP |
2033 | } |
2034 | ||
87f0e418 LP |
2035 | static int service_reload(Unit *u) { |
2036 | Service *s = SERVICE(u); | |
034c6ed7 LP |
2037 | |
2038 | assert(s); | |
2039 | ||
80876c20 | 2040 | assert(s->state == SERVICE_RUNNING || s->state == SERVICE_EXITED); |
034c6ed7 LP |
2041 | |
2042 | service_enter_reload(s); | |
2d018ae2 | 2043 | return 1; |
5cb5a6ff LP |
2044 | } |
2045 | ||
44a6b1b6 | 2046 | _pure_ static bool service_can_reload(Unit *u) { |
87f0e418 | 2047 | Service *s = SERVICE(u); |
034c6ed7 LP |
2048 | |
2049 | assert(s); | |
2050 | ||
2051 | return !!s->exec_command[SERVICE_EXEC_RELOAD]; | |
2052 | } | |
2053 | ||
a16e1123 LP |
2054 | static int service_serialize(Unit *u, FILE *f, FDSet *fds) { |
2055 | Service *s = SERVICE(u); | |
2339fc93 | 2056 | ServiceFDStore *fs; |
a34ceba6 | 2057 | int r; |
a16e1123 LP |
2058 | |
2059 | assert(u); | |
2060 | assert(f); | |
2061 | assert(fds); | |
2062 | ||
2063 | unit_serialize_item(u, f, "state", service_state_to_string(s->state)); | |
f42806df LP |
2064 | unit_serialize_item(u, f, "result", service_result_to_string(s->result)); |
2065 | unit_serialize_item(u, f, "reload-result", service_result_to_string(s->reload_result)); | |
a16e1123 LP |
2066 | |
2067 | if (s->control_pid > 0) | |
f06db334 | 2068 | unit_serialize_item_format(u, f, "control-pid", PID_FMT, s->control_pid); |
a16e1123 | 2069 | |
5925dd3c | 2070 | if (s->main_pid_known && s->main_pid > 0) |
ccd06097 | 2071 | unit_serialize_item_format(u, f, "main-pid", PID_FMT, s->main_pid); |
a16e1123 LP |
2072 | |
2073 | unit_serialize_item(u, f, "main-pid-known", yes_no(s->main_pid_known)); | |
de1d4f9b | 2074 | unit_serialize_item(u, f, "bus-name-good", yes_no(s->bus_name_good)); |
a16e1123 | 2075 | |
a34ceba6 LP |
2076 | r = unit_serialize_item_escaped(u, f, "status-text", s->status_text); |
2077 | if (r < 0) | |
2078 | return r; | |
3a2776bc | 2079 | |
cfc4eb4c LP |
2080 | /* FIXME: There's a minor uncleanliness here: if there are |
2081 | * multiple commands attached here, we will start from the | |
2082 | * first one again */ | |
a16e1123 | 2083 | if (s->control_command_id >= 0) |
f06db334 | 2084 | unit_serialize_item(u, f, "control-command", service_exec_command_to_string(s->control_command_id)); |
a16e1123 | 2085 | |
a34ceba6 LP |
2086 | r = unit_serialize_item_fd(u, f, fds, "stdin-fd", s->stdin_fd); |
2087 | if (r < 0) | |
2088 | return r; | |
2089 | r = unit_serialize_item_fd(u, f, fds, "stdout-fd", s->stdout_fd); | |
2090 | if (r < 0) | |
2091 | return r; | |
2092 | r = unit_serialize_item_fd(u, f, fds, "stderr-fd", s->stderr_fd); | |
2093 | if (r < 0) | |
2094 | return r; | |
e44da745 | 2095 | |
a34ceba6 LP |
2096 | r = unit_serialize_item_fd(u, f, fds, "socket-fd", s->socket_fd); |
2097 | if (r < 0) | |
2098 | return r; | |
2099 | r = unit_serialize_item_fd(u, f, fds, "endpoint-fd", s->bus_endpoint_fd); | |
2100 | if (r < 0) | |
2101 | return r; | |
e44da745 | 2102 | |
2339fc93 | 2103 | LIST_FOREACH(fd_store, fs, s->fd_store) { |
8dd4c05b | 2104 | _cleanup_free_ char *c = NULL; |
2339fc93 LP |
2105 | int copy; |
2106 | ||
2107 | copy = fdset_put_dup(fds, fs->fd); | |
2108 | if (copy < 0) | |
2109 | return copy; | |
2110 | ||
8dd4c05b LP |
2111 | c = cescape(fs->fdname); |
2112 | ||
2113 | unit_serialize_item_format(u, f, "fd-store-fd", "%i %s", copy, strempty(c)); | |
2339fc93 LP |
2114 | } |
2115 | ||
ecdbca40 | 2116 | if (s->main_exec_status.pid > 0) { |
f06db334 LP |
2117 | unit_serialize_item_format(u, f, "main-exec-status-pid", PID_FMT, s->main_exec_status.pid); |
2118 | dual_timestamp_serialize(f, "main-exec-status-start", &s->main_exec_status.start_timestamp); | |
2119 | dual_timestamp_serialize(f, "main-exec-status-exit", &s->main_exec_status.exit_timestamp); | |
ecdbca40 | 2120 | |
799fd0fd | 2121 | if (dual_timestamp_is_set(&s->main_exec_status.exit_timestamp)) { |
f06db334 LP |
2122 | unit_serialize_item_format(u, f, "main-exec-status-code", "%i", s->main_exec_status.code); |
2123 | unit_serialize_item_format(u, f, "main-exec-status-status", "%i", s->main_exec_status.status); | |
ecdbca40 LP |
2124 | } |
2125 | } | |
f06db334 | 2126 | |
a6927d7f | 2127 | if (dual_timestamp_is_set(&s->watchdog_timestamp)) |
842129f5 | 2128 | dual_timestamp_serialize(f, "watchdog-timestamp", &s->watchdog_timestamp); |
ecdbca40 | 2129 | |
a34ceba6 | 2130 | unit_serialize_item(u, f, "forbid-restart", yes_no(s->forbid_restart)); |
6aca9a58 | 2131 | |
a16e1123 LP |
2132 | return 0; |
2133 | } | |
2134 | ||
2135 | static int service_deserialize_item(Unit *u, const char *key, const char *value, FDSet *fds) { | |
2136 | Service *s = SERVICE(u); | |
2339fc93 | 2137 | int r; |
a16e1123 LP |
2138 | |
2139 | assert(u); | |
2140 | assert(key); | |
2141 | assert(value); | |
2142 | assert(fds); | |
2143 | ||
2144 | if (streq(key, "state")) { | |
2145 | ServiceState state; | |
2146 | ||
117dcc57 ZJS |
2147 | state = service_state_from_string(value); |
2148 | if (state < 0) | |
f2341e0a | 2149 | log_unit_debug(u, "Failed to parse state value: %s", value); |
a16e1123 LP |
2150 | else |
2151 | s->deserialized_state = state; | |
f42806df LP |
2152 | } else if (streq(key, "result")) { |
2153 | ServiceResult f; | |
2154 | ||
2155 | f = service_result_from_string(value); | |
2156 | if (f < 0) | |
f2341e0a | 2157 | log_unit_debug(u, "Failed to parse result value: %s", value); |
f42806df LP |
2158 | else if (f != SERVICE_SUCCESS) |
2159 | s->result = f; | |
2160 | ||
2161 | } else if (streq(key, "reload-result")) { | |
2162 | ServiceResult f; | |
2163 | ||
2164 | f = service_result_from_string(value); | |
2165 | if (f < 0) | |
f2341e0a | 2166 | log_unit_debug(u, "Failed to parse reload result value: %s", value); |
f42806df LP |
2167 | else if (f != SERVICE_SUCCESS) |
2168 | s->reload_result = f; | |
a16e1123 | 2169 | |
a16e1123 | 2170 | } else if (streq(key, "control-pid")) { |
5925dd3c | 2171 | pid_t pid; |
a16e1123 | 2172 | |
e364ad06 | 2173 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 2174 | log_unit_debug(u, "Failed to parse control-pid value: %s", value); |
a16e1123 | 2175 | else |
e55224ca | 2176 | s->control_pid = pid; |
a16e1123 | 2177 | } else if (streq(key, "main-pid")) { |
5925dd3c | 2178 | pid_t pid; |
a16e1123 | 2179 | |
e364ad06 | 2180 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 2181 | log_unit_debug(u, "Failed to parse main-pid value: %s", value); |
7400b9d2 LP |
2182 | else { |
2183 | service_set_main_pid(s, pid); | |
2184 | unit_watch_pid(UNIT(s), pid); | |
2185 | } | |
a16e1123 LP |
2186 | } else if (streq(key, "main-pid-known")) { |
2187 | int b; | |
2188 | ||
117dcc57 ZJS |
2189 | b = parse_boolean(value); |
2190 | if (b < 0) | |
f2341e0a | 2191 | log_unit_debug(u, "Failed to parse main-pid-known value: %s", value); |
a16e1123 LP |
2192 | else |
2193 | s->main_pid_known = b; | |
de1d4f9b WF |
2194 | } else if (streq(key, "bus-name-good")) { |
2195 | int b; | |
2196 | ||
2197 | b = parse_boolean(value); | |
2198 | if (b < 0) | |
2199 | log_unit_debug(u, "Failed to parse bus-name-good value: %s", value); | |
2200 | else | |
2201 | s->bus_name_good = b; | |
3a2776bc LP |
2202 | } else if (streq(key, "status-text")) { |
2203 | char *t; | |
2204 | ||
f2341e0a LP |
2205 | r = cunescape(value, 0, &t); |
2206 | if (r < 0) | |
2207 | log_unit_debug_errno(u, r, "Failed to unescape status text: %s", value); | |
117dcc57 | 2208 | else { |
3a2776bc LP |
2209 | free(s->status_text); |
2210 | s->status_text = t; | |
2211 | } | |
2212 | ||
a16e1123 LP |
2213 | } else if (streq(key, "control-command")) { |
2214 | ServiceExecCommand id; | |
2215 | ||
117dcc57 ZJS |
2216 | id = service_exec_command_from_string(value); |
2217 | if (id < 0) | |
f2341e0a | 2218 | log_unit_debug(u, "Failed to parse exec-command value: %s", value); |
a16e1123 LP |
2219 | else { |
2220 | s->control_command_id = id; | |
2221 | s->control_command = s->exec_command[id]; | |
2222 | } | |
2223 | } else if (streq(key, "socket-fd")) { | |
2224 | int fd; | |
2225 | ||
2226 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2227 | log_unit_debug(u, "Failed to parse socket-fd value: %s", value); |
a16e1123 | 2228 | else { |
574634bc | 2229 | asynchronous_close(s->socket_fd); |
a16e1123 LP |
2230 | s->socket_fd = fdset_remove(fds, fd); |
2231 | } | |
e44da745 DM |
2232 | } else if (streq(key, "endpoint-fd")) { |
2233 | int fd; | |
2234 | ||
2235 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2236 | log_unit_debug(u, "Failed to parse endpoint-fd value: %s", value); |
e44da745 DM |
2237 | else { |
2238 | safe_close(s->bus_endpoint_fd); | |
2239 | s->bus_endpoint_fd = fdset_remove(fds, fd); | |
2240 | } | |
2339fc93 | 2241 | } else if (streq(key, "fd-store-fd")) { |
8dd4c05b LP |
2242 | const char *fdv; |
2243 | size_t pf; | |
2339fc93 LP |
2244 | int fd; |
2245 | ||
8dd4c05b LP |
2246 | pf = strcspn(value, WHITESPACE); |
2247 | fdv = strndupa(value, pf); | |
2248 | ||
2249 | if (safe_atoi(fdv, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
f2341e0a | 2250 | log_unit_debug(u, "Failed to parse fd-store-fd value: %s", value); |
2339fc93 | 2251 | else { |
8dd4c05b LP |
2252 | _cleanup_free_ char *t = NULL; |
2253 | const char *fdn; | |
2254 | ||
2255 | fdn = value + pf; | |
2256 | fdn += strspn(fdn, WHITESPACE); | |
2257 | (void) cunescape(fdn, 0, &t); | |
2258 | ||
2259 | r = service_add_fd_store(s, fd, t); | |
2339fc93 | 2260 | if (r < 0) |
f2341e0a | 2261 | log_unit_error_errno(u, r, "Failed to add fd to store: %m"); |
2339fc93 LP |
2262 | else if (r > 0) |
2263 | fdset_remove(fds, fd); | |
2264 | } | |
2265 | ||
ecdbca40 LP |
2266 | } else if (streq(key, "main-exec-status-pid")) { |
2267 | pid_t pid; | |
2268 | ||
e364ad06 | 2269 | if (parse_pid(value, &pid) < 0) |
f2341e0a | 2270 | log_unit_debug(u, "Failed to parse main-exec-status-pid value: %s", value); |
ecdbca40 LP |
2271 | else |
2272 | s->main_exec_status.pid = pid; | |
2273 | } else if (streq(key, "main-exec-status-code")) { | |
2274 | int i; | |
2275 | ||
e364ad06 | 2276 | if (safe_atoi(value, &i) < 0) |
f2341e0a | 2277 | log_unit_debug(u, "Failed to parse main-exec-status-code value: %s", value); |
ecdbca40 LP |
2278 | else |
2279 | s->main_exec_status.code = i; | |
2280 | } else if (streq(key, "main-exec-status-status")) { | |
2281 | int i; | |
2282 | ||
e364ad06 | 2283 | if (safe_atoi(value, &i) < 0) |
f2341e0a | 2284 | log_unit_debug(u, "Failed to parse main-exec-status-status value: %s", value); |
ecdbca40 LP |
2285 | else |
2286 | s->main_exec_status.status = i; | |
799fd0fd LP |
2287 | } else if (streq(key, "main-exec-status-start")) |
2288 | dual_timestamp_deserialize(value, &s->main_exec_status.start_timestamp); | |
2289 | else if (streq(key, "main-exec-status-exit")) | |
2290 | dual_timestamp_deserialize(value, &s->main_exec_status.exit_timestamp); | |
a6927d7f MO |
2291 | else if (streq(key, "watchdog-timestamp")) |
2292 | dual_timestamp_deserialize(value, &s->watchdog_timestamp); | |
613b411c | 2293 | else if (streq(key, "forbid-restart")) { |
6aca9a58 SE |
2294 | int b; |
2295 | ||
2296 | b = parse_boolean(value); | |
2297 | if (b < 0) | |
f2341e0a | 2298 | log_unit_debug(u, "Failed to parse forbid-restart value: %s", value); |
6aca9a58 SE |
2299 | else |
2300 | s->forbid_restart = b; | |
a34ceba6 LP |
2301 | } else if (streq(key, "stdin-fd")) { |
2302 | int fd; | |
2303 | ||
2304 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2305 | log_unit_debug(u, "Failed to parse stdin-fd value: %s", value); | |
2306 | else { | |
2307 | asynchronous_close(s->stdin_fd); | |
2308 | s->stdin_fd = fdset_remove(fds, fd); | |
2309 | } | |
2310 | } else if (streq(key, "stdout-fd")) { | |
2311 | int fd; | |
2312 | ||
2313 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2314 | log_unit_debug(u, "Failed to parse stdout-fd value: %s", value); | |
2315 | else { | |
2316 | asynchronous_close(s->stdout_fd); | |
2317 | s->stdout_fd = fdset_remove(fds, fd); | |
2318 | } | |
2319 | } else if (streq(key, "stderr-fd")) { | |
2320 | int fd; | |
2321 | ||
2322 | if (safe_atoi(value, &fd) < 0 || fd < 0 || !fdset_contains(fds, fd)) | |
2323 | log_unit_debug(u, "Failed to parse stderr-fd value: %s", value); | |
2324 | else { | |
2325 | asynchronous_close(s->stderr_fd); | |
2326 | s->stderr_fd = fdset_remove(fds, fd); | |
2327 | } | |
c17ec25e | 2328 | } else |
f2341e0a | 2329 | log_unit_debug(u, "Unknown serialization key: %s", key); |
a16e1123 LP |
2330 | |
2331 | return 0; | |
2332 | } | |
2333 | ||
44a6b1b6 | 2334 | _pure_ static UnitActiveState service_active_state(Unit *u) { |
e056b01d LP |
2335 | const UnitActiveState *table; |
2336 | ||
87f0e418 | 2337 | assert(u); |
5cb5a6ff | 2338 | |
e056b01d LP |
2339 | table = SERVICE(u)->type == SERVICE_IDLE ? state_translation_table_idle : state_translation_table; |
2340 | ||
2341 | return table[SERVICE(u)->state]; | |
034c6ed7 LP |
2342 | } |
2343 | ||
10a94420 LP |
2344 | static const char *service_sub_state_to_string(Unit *u) { |
2345 | assert(u); | |
2346 | ||
2347 | return service_state_to_string(SERVICE(u)->state); | |
2348 | } | |
2349 | ||
701cc384 LP |
2350 | static bool service_check_gc(Unit *u) { |
2351 | Service *s = SERVICE(u); | |
2352 | ||
2353 | assert(s); | |
2354 | ||
6d55002a LP |
2355 | /* Never clean up services that still have a process around, |
2356 | * even if the service is formally dead. */ | |
2357 | if (cgroup_good(s) > 0 || | |
2358 | main_pid_good(s) > 0 || | |
2359 | control_pid_good(s) > 0) | |
2360 | return true; | |
2361 | ||
6d55002a LP |
2362 | return false; |
2363 | } | |
2364 | ||
44a6b1b6 | 2365 | _pure_ static bool service_check_snapshot(Unit *u) { |
701cc384 LP |
2366 | Service *s = SERVICE(u); |
2367 | ||
2368 | assert(s); | |
2369 | ||
8bb2d17d | 2370 | return s->socket_fd < 0; |
701cc384 LP |
2371 | } |
2372 | ||
3a111838 MS |
2373 | static int service_retry_pid_file(Service *s) { |
2374 | int r; | |
2375 | ||
2376 | assert(s->pid_file); | |
2377 | assert(s->state == SERVICE_START || s->state == SERVICE_START_POST); | |
2378 | ||
2379 | r = service_load_pid_file(s, false); | |
2380 | if (r < 0) | |
2381 | return r; | |
2382 | ||
2383 | service_unwatch_pid_file(s); | |
2384 | ||
f42806df | 2385 | service_enter_running(s, SERVICE_SUCCESS); |
3a111838 MS |
2386 | return 0; |
2387 | } | |
2388 | ||
2389 | static int service_watch_pid_file(Service *s) { | |
2390 | int r; | |
2391 | ||
f2341e0a | 2392 | log_unit_debug(UNIT(s), "Setting watch for PID file %s", s->pid_file_pathspec->path); |
8bb2d17d | 2393 | |
718db961 | 2394 | r = path_spec_watch(s->pid_file_pathspec, service_dispatch_io); |
3a111838 MS |
2395 | if (r < 0) |
2396 | goto fail; | |
2397 | ||
2398 | /* the pidfile might have appeared just before we set the watch */ | |
f2341e0a | 2399 | log_unit_debug(UNIT(s), "Trying to read PID file %s in case it changed", s->pid_file_pathspec->path); |
3a111838 MS |
2400 | service_retry_pid_file(s); |
2401 | ||
2402 | return 0; | |
2403 | fail: | |
f2341e0a | 2404 | log_unit_error_errno(UNIT(s), r, "Failed to set a watch for PID file %s: %m", s->pid_file_pathspec->path); |
3a111838 MS |
2405 | service_unwatch_pid_file(s); |
2406 | return r; | |
2407 | } | |
2408 | ||
2409 | static int service_demand_pid_file(Service *s) { | |
2410 | PathSpec *ps; | |
2411 | ||
2412 | assert(s->pid_file); | |
2413 | assert(!s->pid_file_pathspec); | |
2414 | ||
2415 | ps = new0(PathSpec, 1); | |
2416 | if (!ps) | |
2417 | return -ENOMEM; | |
2418 | ||
718db961 | 2419 | ps->unit = UNIT(s); |
3a111838 MS |
2420 | ps->path = strdup(s->pid_file); |
2421 | if (!ps->path) { | |
2422 | free(ps); | |
2423 | return -ENOMEM; | |
2424 | } | |
2425 | ||
2426 | path_kill_slashes(ps->path); | |
2427 | ||
2428 | /* PATH_CHANGED would not be enough. There are daemons (sendmail) that | |
2429 | * keep their PID file open all the time. */ | |
2430 | ps->type = PATH_MODIFIED; | |
2431 | ps->inotify_fd = -1; | |
2432 | ||
2433 | s->pid_file_pathspec = ps; | |
2434 | ||
2435 | return service_watch_pid_file(s); | |
2436 | } | |
2437 | ||
718db961 | 2438 | static int service_dispatch_io(sd_event_source *source, int fd, uint32_t events, void *userdata) { |
e14c2802 LP |
2439 | PathSpec *p = userdata; |
2440 | Service *s; | |
2441 | ||
2442 | assert(p); | |
2443 | ||
2444 | s = SERVICE(p->unit); | |
3a111838 MS |
2445 | |
2446 | assert(s); | |
2447 | assert(fd >= 0); | |
2448 | assert(s->state == SERVICE_START || s->state == SERVICE_START_POST); | |
2449 | assert(s->pid_file_pathspec); | |
57020a3a | 2450 | assert(path_spec_owns_inotify_fd(s->pid_file_pathspec, fd)); |
3a111838 | 2451 | |
f2341e0a | 2452 | log_unit_debug(UNIT(s), "inotify event"); |
3a111838 | 2453 | |
e14c2802 | 2454 | if (path_spec_fd_event(p, events) < 0) |
3a111838 MS |
2455 | goto fail; |
2456 | ||
2457 | if (service_retry_pid_file(s) == 0) | |
718db961 | 2458 | return 0; |
3a111838 MS |
2459 | |
2460 | if (service_watch_pid_file(s) < 0) | |
2461 | goto fail; | |
2462 | ||
718db961 LP |
2463 | return 0; |
2464 | ||
3a111838 MS |
2465 | fail: |
2466 | service_unwatch_pid_file(s); | |
f42806df | 2467 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_RESOURCES); |
718db961 | 2468 | return 0; |
3a111838 MS |
2469 | } |
2470 | ||
a911bb9a LP |
2471 | static void service_notify_cgroup_empty_event(Unit *u) { |
2472 | Service *s = SERVICE(u); | |
2473 | ||
2474 | assert(u); | |
2475 | ||
f2341e0a | 2476 | log_unit_debug(u, "cgroup is empty"); |
a911bb9a LP |
2477 | |
2478 | switch (s->state) { | |
2479 | ||
2480 | /* Waiting for SIGCHLD is usually more interesting, | |
2481 | * because it includes return codes/signals. Which is | |
2482 | * why we ignore the cgroup events for most cases, | |
2483 | * except when we don't know pid which to expect the | |
2484 | * SIGCHLD for. */ | |
2485 | ||
2486 | case SERVICE_START: | |
2487 | case SERVICE_START_POST: | |
2488 | /* If we were hoping for the daemon to write its PID file, | |
2489 | * we can give up now. */ | |
2490 | if (s->pid_file_pathspec) { | |
f2341e0a | 2491 | log_unit_warning(u, "Daemon never wrote its PID file. Failing."); |
8bb2d17d | 2492 | |
a911bb9a LP |
2493 | service_unwatch_pid_file(s); |
2494 | if (s->state == SERVICE_START) | |
2495 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); | |
2496 | else | |
2497 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); | |
2498 | } | |
2499 | break; | |
2500 | ||
2501 | case SERVICE_RUNNING: | |
2502 | /* service_enter_running() will figure out what to do */ | |
2503 | service_enter_running(s, SERVICE_SUCCESS); | |
2504 | break; | |
2505 | ||
db2cb23b | 2506 | case SERVICE_STOP_SIGABRT: |
a911bb9a LP |
2507 | case SERVICE_STOP_SIGTERM: |
2508 | case SERVICE_STOP_SIGKILL: | |
2509 | ||
2510 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2511 | service_enter_stop_post(s, SERVICE_SUCCESS); | |
2512 | ||
2513 | break; | |
2514 | ||
2515 | case SERVICE_STOP_POST: | |
2516 | case SERVICE_FINAL_SIGTERM: | |
2517 | case SERVICE_FINAL_SIGKILL: | |
2518 | if (main_pid_good(s) <= 0 && !control_pid_good(s)) | |
2519 | service_enter_dead(s, SERVICE_SUCCESS, true); | |
2520 | ||
2521 | break; | |
2522 | ||
2523 | default: | |
2524 | ; | |
2525 | } | |
2526 | } | |
2527 | ||
87f0e418 LP |
2528 | static void service_sigchld_event(Unit *u, pid_t pid, int code, int status) { |
2529 | Service *s = SERVICE(u); | |
f42806df | 2530 | ServiceResult f; |
5cb5a6ff LP |
2531 | |
2532 | assert(s); | |
034c6ed7 LP |
2533 | assert(pid >= 0); |
2534 | ||
96342de6 LN |
2535 | if (UNIT(s)->fragment_path ? is_clean_exit(code, status, &s->success_status) : |
2536 | is_clean_exit_lsb(code, status, &s->success_status)) | |
f42806df LP |
2537 | f = SERVICE_SUCCESS; |
2538 | else if (code == CLD_EXITED) | |
2539 | f = SERVICE_FAILURE_EXIT_CODE; | |
2540 | else if (code == CLD_KILLED) | |
2541 | f = SERVICE_FAILURE_SIGNAL; | |
2542 | else if (code == CLD_DUMPED) | |
2543 | f = SERVICE_FAILURE_CORE_DUMP; | |
d06dacd0 | 2544 | else |
cfc4eb4c | 2545 | assert_not_reached("Unknown code"); |
034c6ed7 LP |
2546 | |
2547 | if (s->main_pid == pid) { | |
db01f8b3 MS |
2548 | /* Forking services may occasionally move to a new PID. |
2549 | * As long as they update the PID file before exiting the old | |
2550 | * PID, they're fine. */ | |
5375410b | 2551 | if (service_load_pid_file(s, false) == 0) |
db01f8b3 | 2552 | return; |
034c6ed7 | 2553 | |
034c6ed7 | 2554 | s->main_pid = 0; |
6ea832a2 | 2555 | exec_status_exit(&s->main_exec_status, &s->exec_context, pid, code, status); |
034c6ed7 | 2556 | |
867b3b7d | 2557 | if (s->main_command) { |
fbeefb45 LP |
2558 | /* If this is not a forking service than the |
2559 | * main process got started and hence we copy | |
2560 | * the exit status so that it is recorded both | |
2561 | * as main and as control process exit | |
2562 | * status */ | |
2563 | ||
867b3b7d | 2564 | s->main_command->exec_status = s->main_exec_status; |
b708e7ce | 2565 | |
867b3b7d | 2566 | if (s->main_command->ignore) |
f42806df | 2567 | f = SERVICE_SUCCESS; |
fbeefb45 LP |
2568 | } else if (s->exec_command[SERVICE_EXEC_START]) { |
2569 | ||
2570 | /* If this is a forked process, then we should | |
2571 | * ignore the return value if this was | |
2572 | * configured for the starter process */ | |
2573 | ||
2574 | if (s->exec_command[SERVICE_EXEC_START]->ignore) | |
2575 | f = SERVICE_SUCCESS; | |
034c6ed7 LP |
2576 | } |
2577 | ||
f2341e0a LP |
2578 | log_struct(f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, |
2579 | LOG_UNIT_ID(u), | |
2580 | LOG_UNIT_MESSAGE(u, "Main process exited, code=%s, status=%i/%s", | |
2581 | sigchld_code_to_string(code), status, | |
e2cc6eca LP |
2582 | strna(code == CLD_EXITED |
2583 | ? exit_status_to_string(status, EXIT_STATUS_FULL) | |
2584 | : signal_to_string(status))), | |
f2341e0a LP |
2585 | "EXIT_CODE=%s", sigchld_code_to_string(code), |
2586 | "EXIT_STATUS=%i", status, | |
2587 | NULL); | |
f42806df LP |
2588 | |
2589 | if (f != SERVICE_SUCCESS) | |
2590 | s->result = f; | |
034c6ed7 | 2591 | |
867b3b7d LP |
2592 | if (s->main_command && |
2593 | s->main_command->command_next && | |
f42806df | 2594 | f == SERVICE_SUCCESS) { |
034c6ed7 | 2595 | |
34e9ba66 LP |
2596 | /* There is another command to * |
2597 | * execute, so let's do that. */ | |
034c6ed7 | 2598 | |
f2341e0a | 2599 | log_unit_debug(u, "Running next main command for state %s.", service_state_to_string(s->state)); |
f42806df | 2600 | service_run_next_main(s); |
034c6ed7 | 2601 | |
34e9ba66 LP |
2602 | } else { |
2603 | ||
2604 | /* The service exited, so the service is officially | |
2605 | * gone. */ | |
867b3b7d | 2606 | s->main_command = NULL; |
34e9ba66 LP |
2607 | |
2608 | switch (s->state) { | |
2609 | ||
2610 | case SERVICE_START_POST: | |
2611 | case SERVICE_RELOAD: | |
2612 | case SERVICE_STOP: | |
2613 | /* Need to wait until the operation is | |
2614 | * done */ | |
c4653a4d | 2615 | break; |
7d55e835 | 2616 | |
34e9ba66 LP |
2617 | case SERVICE_START: |
2618 | if (s->type == SERVICE_ONESHOT) { | |
2619 | /* This was our main goal, so let's go on */ | |
f42806df | 2620 | if (f == SERVICE_SUCCESS) |
34e9ba66 LP |
2621 | service_enter_start_post(s); |
2622 | else | |
f42806df | 2623 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
34e9ba66 | 2624 | break; |
34e9ba66 | 2625 | } |
034c6ed7 | 2626 | |
bfba3256 LP |
2627 | /* Fall through */ |
2628 | ||
34e9ba66 | 2629 | case SERVICE_RUNNING: |
f42806df | 2630 | service_enter_running(s, f); |
34e9ba66 | 2631 | break; |
034c6ed7 | 2632 | |
db2cb23b | 2633 | case SERVICE_STOP_SIGABRT: |
34e9ba66 LP |
2634 | case SERVICE_STOP_SIGTERM: |
2635 | case SERVICE_STOP_SIGKILL: | |
5cb5a6ff | 2636 | |
34e9ba66 | 2637 | if (!control_pid_good(s)) |
f42806df | 2638 | service_enter_stop_post(s, f); |
5cb5a6ff | 2639 | |
34e9ba66 LP |
2640 | /* If there is still a control process, wait for that first */ |
2641 | break; | |
2642 | ||
bf108e55 LP |
2643 | case SERVICE_STOP_POST: |
2644 | case SERVICE_FINAL_SIGTERM: | |
2645 | case SERVICE_FINAL_SIGKILL: | |
2646 | ||
2647 | if (!control_pid_good(s)) | |
2648 | service_enter_dead(s, f, true); | |
2649 | break; | |
2650 | ||
34e9ba66 LP |
2651 | default: |
2652 | assert_not_reached("Uh, main process died at wrong time."); | |
2653 | } | |
034c6ed7 | 2654 | } |
5cb5a6ff | 2655 | |
034c6ed7 | 2656 | } else if (s->control_pid == pid) { |
34e9ba66 LP |
2657 | s->control_pid = 0; |
2658 | ||
b708e7ce | 2659 | if (s->control_command) { |
8bb2d17d | 2660 | exec_status_exit(&s->control_command->exec_status, &s->exec_context, pid, code, status); |
a16e1123 | 2661 | |
b708e7ce | 2662 | if (s->control_command->ignore) |
f42806df | 2663 | f = SERVICE_SUCCESS; |
b708e7ce LP |
2664 | } |
2665 | ||
f2341e0a LP |
2666 | log_unit_full(u, f == SERVICE_SUCCESS ? LOG_DEBUG : LOG_NOTICE, 0, |
2667 | "Control process exited, code=%s status=%i", | |
2668 | sigchld_code_to_string(code), status); | |
f42806df LP |
2669 | |
2670 | if (f != SERVICE_SUCCESS) | |
2671 | s->result = f; | |
034c6ed7 | 2672 | |
88f3e0c9 LP |
2673 | /* Immediately get rid of the cgroup, so that the |
2674 | * kernel doesn't delay the cgroup empty messages for | |
2675 | * the service cgroup any longer than necessary */ | |
4ad49000 | 2676 | service_kill_control_processes(s); |
88f3e0c9 | 2677 | |
34e9ba66 LP |
2678 | if (s->control_command && |
2679 | s->control_command->command_next && | |
f42806df | 2680 | f == SERVICE_SUCCESS) { |
034c6ed7 LP |
2681 | |
2682 | /* There is another command to * | |
2683 | * execute, so let's do that. */ | |
2684 | ||
f2341e0a | 2685 | log_unit_debug(u, "Running next control command for state %s.", service_state_to_string(s->state)); |
f42806df | 2686 | service_run_next_control(s); |
034c6ed7 | 2687 | |
80876c20 | 2688 | } else { |
034c6ed7 LP |
2689 | /* No further commands for this step, so let's |
2690 | * figure out what to do next */ | |
2691 | ||
a16e1123 LP |
2692 | s->control_command = NULL; |
2693 | s->control_command_id = _SERVICE_EXEC_COMMAND_INVALID; | |
2694 | ||
f2341e0a | 2695 | log_unit_debug(u, "Got final SIGCHLD for state %s.", service_state_to_string(s->state)); |
bd982a8b | 2696 | |
034c6ed7 LP |
2697 | switch (s->state) { |
2698 | ||
2699 | case SERVICE_START_PRE: | |
f42806df | 2700 | if (f == SERVICE_SUCCESS) |
034c6ed7 LP |
2701 | service_enter_start(s); |
2702 | else | |
f42806df | 2703 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); |
034c6ed7 LP |
2704 | break; |
2705 | ||
2706 | case SERVICE_START: | |
bfba3256 LP |
2707 | if (s->type != SERVICE_FORKING) |
2708 | /* Maybe spurious event due to a reload that changed the type? */ | |
2709 | break; | |
034c6ed7 | 2710 | |
f42806df LP |
2711 | if (f != SERVICE_SUCCESS) { |
2712 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, f); | |
3a111838 MS |
2713 | break; |
2714 | } | |
034c6ed7 | 2715 | |
3a111838 | 2716 | if (s->pid_file) { |
f42806df LP |
2717 | bool has_start_post; |
2718 | int r; | |
2719 | ||
3a111838 MS |
2720 | /* Let's try to load the pid file here if we can. |
2721 | * The PID file might actually be created by a START_POST | |
2722 | * script. In that case don't worry if the loading fails. */ | |
f42806df LP |
2723 | |
2724 | has_start_post = !!s->exec_command[SERVICE_EXEC_START_POST]; | |
2725 | r = service_load_pid_file(s, !has_start_post); | |
3a111838 MS |
2726 | if (!has_start_post && r < 0) { |
2727 | r = service_demand_pid_file(s); | |
2728 | if (r < 0 || !cgroup_good(s)) | |
f42806df | 2729 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_RESOURCES); |
3a111838 MS |
2730 | break; |
2731 | } | |
034c6ed7 | 2732 | } else |
efdb0237 | 2733 | (void) service_search_main_pid(s); |
034c6ed7 | 2734 | |
3a111838 | 2735 | service_enter_start_post(s); |
034c6ed7 LP |
2736 | break; |
2737 | ||
2738 | case SERVICE_START_POST: | |
f42806df LP |
2739 | if (f != SERVICE_SUCCESS) { |
2740 | service_enter_stop(s, f); | |
2096e009 | 2741 | break; |
034c6ed7 LP |
2742 | } |
2743 | ||
2096e009 | 2744 | if (s->pid_file) { |
f42806df LP |
2745 | int r; |
2746 | ||
2747 | r = service_load_pid_file(s, true); | |
2096e009 MS |
2748 | if (r < 0) { |
2749 | r = service_demand_pid_file(s); | |
2750 | if (r < 0 || !cgroup_good(s)) | |
f42806df | 2751 | service_enter_stop(s, SERVICE_FAILURE_RESOURCES); |
2096e009 MS |
2752 | break; |
2753 | } | |
2754 | } else | |
efdb0237 | 2755 | (void) service_search_main_pid(s); |
2096e009 | 2756 | |
f42806df | 2757 | service_enter_running(s, SERVICE_SUCCESS); |
3185a36b | 2758 | break; |
034c6ed7 LP |
2759 | |
2760 | case SERVICE_RELOAD: | |
f42806df | 2761 | if (f == SERVICE_SUCCESS) { |
5375410b | 2762 | service_load_pid_file(s, true); |
efdb0237 | 2763 | (void) service_search_main_pid(s); |
3185a36b LP |
2764 | } |
2765 | ||
f42806df LP |
2766 | s->reload_result = f; |
2767 | service_enter_running(s, SERVICE_SUCCESS); | |
034c6ed7 LP |
2768 | break; |
2769 | ||
2770 | case SERVICE_STOP: | |
f42806df | 2771 | service_enter_signal(s, SERVICE_STOP_SIGTERM, f); |
034c6ed7 LP |
2772 | break; |
2773 | ||
db2cb23b | 2774 | case SERVICE_STOP_SIGABRT: |
034c6ed7 LP |
2775 | case SERVICE_STOP_SIGTERM: |
2776 | case SERVICE_STOP_SIGKILL: | |
2777 | if (main_pid_good(s) <= 0) | |
f42806df | 2778 | service_enter_stop_post(s, f); |
034c6ed7 LP |
2779 | |
2780 | /* If there is still a service | |
2781 | * process around, wait until | |
2782 | * that one quit, too */ | |
2783 | break; | |
2784 | ||
2785 | case SERVICE_STOP_POST: | |
2786 | case SERVICE_FINAL_SIGTERM: | |
2787 | case SERVICE_FINAL_SIGKILL: | |
bf108e55 LP |
2788 | if (main_pid_good(s) <= 0) |
2789 | service_enter_dead(s, f, true); | |
034c6ed7 LP |
2790 | break; |
2791 | ||
2792 | default: | |
2793 | assert_not_reached("Uh, control process died at wrong time."); | |
2794 | } | |
2795 | } | |
8c47c732 | 2796 | } |
c4e2ceae LP |
2797 | |
2798 | /* Notify clients about changed exit status */ | |
2799 | unit_add_to_dbus_queue(u); | |
a911bb9a LP |
2800 | |
2801 | /* We got one SIGCHLD for the service, let's watch all | |
2802 | * processes that are now running of the service, and watch | |
2803 | * that. Among the PIDs we then watch will be children | |
2804 | * reassigned to us, which hopefully allows us to identify | |
2805 | * when all children are gone */ | |
2806 | unit_tidy_watch_pids(u, s->main_pid, s->control_pid); | |
2807 | unit_watch_all_pids(u); | |
2808 | ||
2809 | /* If the PID set is empty now, then let's finish this off */ | |
2810 | if (set_isempty(u->pids)) | |
2811 | service_notify_cgroup_empty_event(u); | |
034c6ed7 LP |
2812 | } |
2813 | ||
718db961 LP |
2814 | static int service_dispatch_timer(sd_event_source *source, usec_t usec, void *userdata) { |
2815 | Service *s = SERVICE(userdata); | |
034c6ed7 LP |
2816 | |
2817 | assert(s); | |
718db961 | 2818 | assert(source == s->timer_event_source); |
034c6ed7 LP |
2819 | |
2820 | switch (s->state) { | |
2821 | ||
2822 | case SERVICE_START_PRE: | |
2823 | case SERVICE_START: | |
f2341e0a | 2824 | log_unit_warning(UNIT(s), "%s operation timed out. Terminating.", s->state == SERVICE_START ? "Start" : "Start-pre"); |
f42806df | 2825 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
80876c20 LP |
2826 | break; |
2827 | ||
034c6ed7 | 2828 | case SERVICE_START_POST: |
f2341e0a | 2829 | log_unit_warning(UNIT(s), "Start-post operation timed out. Stopping."); |
f42806df | 2830 | service_enter_stop(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2831 | break; |
2832 | ||
e2f3b44c | 2833 | case SERVICE_RELOAD: |
f2341e0a | 2834 | log_unit_warning(UNIT(s), "Reload operation timed out. Stopping."); |
478b6192 EV |
2835 | service_unwatch_control_pid(s); |
2836 | service_kill_control_processes(s); | |
f42806df LP |
2837 | s->reload_result = SERVICE_FAILURE_TIMEOUT; |
2838 | service_enter_running(s, SERVICE_SUCCESS); | |
e2f3b44c LP |
2839 | break; |
2840 | ||
034c6ed7 | 2841 | case SERVICE_STOP: |
f2341e0a | 2842 | log_unit_warning(UNIT(s), "Stopping timed out. Terminating."); |
f42806df | 2843 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2844 | break; |
2845 | ||
db2cb23b | 2846 | case SERVICE_STOP_SIGABRT: |
f2341e0a | 2847 | log_unit_warning(UNIT(s), "State 'stop-sigabrt' timed out. Terminating."); |
2ab2ab7b | 2848 | service_enter_signal(s, SERVICE_STOP_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
db2cb23b UTL |
2849 | break; |
2850 | ||
034c6ed7 | 2851 | case SERVICE_STOP_SIGTERM: |
4819ff03 | 2852 | if (s->kill_context.send_sigkill) { |
f2341e0a | 2853 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Killing."); |
f42806df | 2854 | service_enter_signal(s, SERVICE_STOP_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 2855 | } else { |
f2341e0a | 2856 | log_unit_warning(UNIT(s), "State 'stop-sigterm' timed out. Skipping SIGKILL."); |
f42806df | 2857 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
ba035df2 LP |
2858 | } |
2859 | ||
034c6ed7 LP |
2860 | break; |
2861 | ||
2862 | case SERVICE_STOP_SIGKILL: | |
35b8ca3a | 2863 | /* Uh, we sent a SIGKILL and it is still not gone? |
034c6ed7 LP |
2864 | * Must be something we cannot kill, so let's just be |
2865 | * weirded out and continue */ | |
2866 | ||
f2341e0a | 2867 | log_unit_warning(UNIT(s), "Processes still around after SIGKILL. Ignoring."); |
f42806df | 2868 | service_enter_stop_post(s, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2869 | break; |
2870 | ||
2871 | case SERVICE_STOP_POST: | |
f2341e0a | 2872 | log_unit_warning(UNIT(s), "State 'stop-post' timed out. Terminating."); |
f42806df | 2873 | service_enter_signal(s, SERVICE_FINAL_SIGTERM, SERVICE_FAILURE_TIMEOUT); |
034c6ed7 LP |
2874 | break; |
2875 | ||
2876 | case SERVICE_FINAL_SIGTERM: | |
4819ff03 | 2877 | if (s->kill_context.send_sigkill) { |
f2341e0a | 2878 | log_unit_warning(UNIT(s), "State 'stop-final-sigterm' timed out. Killing."); |
f42806df | 2879 | service_enter_signal(s, SERVICE_FINAL_SIGKILL, SERVICE_FAILURE_TIMEOUT); |
ba035df2 | 2880 | } else { |
f2341e0a | 2881 | log_unit_warning(UNIT(s), "State 'stop-final-sigterm' timed out. Skipping SIGKILL. Entering failed mode."); |
f42806df | 2882 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, false); |
ba035df2 LP |
2883 | } |
2884 | ||
034c6ed7 LP |
2885 | break; |
2886 | ||
2887 | case SERVICE_FINAL_SIGKILL: | |
f2341e0a | 2888 | log_unit_warning(UNIT(s), "Processes still around after final SIGKILL. Entering failed mode."); |
f42806df | 2889 | service_enter_dead(s, SERVICE_FAILURE_TIMEOUT, true); |
034c6ed7 LP |
2890 | break; |
2891 | ||
2892 | case SERVICE_AUTO_RESTART: | |
f2341e0a | 2893 | log_unit_info(UNIT(s), |
ef417cfd | 2894 | s->restart_usec > 0 ? |
f2341e0a LP |
2895 | "Service hold-off time over, scheduling restart." : |
2896 | "Service has no hold-off time, scheduling restart."); | |
034c6ed7 LP |
2897 | service_enter_restart(s); |
2898 | break; | |
2899 | ||
2900 | default: | |
2901 | assert_not_reached("Timeout at wrong time."); | |
2902 | } | |
718db961 LP |
2903 | |
2904 | return 0; | |
2905 | } | |
2906 | ||
2907 | static int service_dispatch_watchdog(sd_event_source *source, usec_t usec, void *userdata) { | |
2908 | Service *s = SERVICE(userdata); | |
a7850c7d | 2909 | char t[FORMAT_TIMESPAN_MAX]; |
718db961 LP |
2910 | |
2911 | assert(s); | |
2912 | assert(source == s->watchdog_event_source); | |
2913 | ||
f2341e0a | 2914 | log_unit_error(UNIT(s), "Watchdog timeout (limit %s)!", |
a7850c7d | 2915 | format_timespan(t, sizeof(t), s->watchdog_usec, 1)); |
8bb2d17d | 2916 | |
db2cb23b | 2917 | service_enter_signal(s, SERVICE_STOP_SIGABRT, SERVICE_FAILURE_WATCHDOG); |
842129f5 | 2918 | |
718db961 | 2919 | return 0; |
5cb5a6ff LP |
2920 | } |
2921 | ||
a354329f | 2922 | static void service_notify_message(Unit *u, pid_t pid, char **tags, FDSet *fds) { |
8c47c732 | 2923 | Service *s = SERVICE(u); |
308d72dc | 2924 | _cleanup_free_ char *cc = NULL; |
30b5275a | 2925 | bool notify_dbus = false; |
308d72dc | 2926 | const char *e; |
8c47c732 LP |
2927 | |
2928 | assert(u); | |
2929 | ||
308d72dc | 2930 | cc = strv_join(tags, ", "); |
da13d4d2 | 2931 | |
c952c6ec | 2932 | if (s->notify_access == NOTIFY_NONE) { |
f2341e0a | 2933 | log_unit_warning(u, "Got notification message from PID "PID_FMT", but reception is disabled.", pid); |
c952c6ec | 2934 | return; |
34959677 | 2935 | } else if (s->notify_access == NOTIFY_MAIN && pid != s->main_pid) { |
336c6e46 | 2936 | if (s->main_pid != 0) |
f2341e0a | 2937 | log_unit_warning(u, "Got notification message from PID "PID_FMT", but reception only permitted for main PID "PID_FMT, pid, s->main_pid); |
336c6e46 | 2938 | else |
f2341e0a | 2939 | log_unit_debug(u, "Got notification message from PID "PID_FMT", but reception only permitted for main PID which is currently not known", pid); |
c952c6ec | 2940 | return; |
34959677 TG |
2941 | } else |
2942 | log_unit_debug(u, "Got notification message from PID "PID_FMT" (%s)", pid, isempty(cc) ? "n/a" : cc); | |
c952c6ec | 2943 | |
8c47c732 | 2944 | /* Interpret MAINPID= */ |
28849dba | 2945 | e = strv_find_startswith(tags, "MAINPID="); |
5e56b378 | 2946 | if (e && IN_SET(s->state, SERVICE_START, SERVICE_START_POST, SERVICE_RUNNING, SERVICE_RELOAD)) { |
28849dba | 2947 | if (parse_pid(e, &pid) < 0) |
f2341e0a | 2948 | log_unit_warning(u, "Failed to parse MAINPID= field in notification message: %s", e); |
8c47c732 | 2949 | else { |
5925dd3c | 2950 | service_set_main_pid(s, pid); |
7400b9d2 | 2951 | unit_watch_pid(UNIT(s), pid); |
30b5275a | 2952 | notify_dbus = true; |
8c47c732 LP |
2953 | } |
2954 | } | |
2955 | ||
308d72dc LP |
2956 | /* Interpret RELOADING= */ |
2957 | if (strv_find(tags, "RELOADING=1")) { | |
2958 | ||
308d72dc LP |
2959 | s->notify_state = NOTIFY_RELOADING; |
2960 | ||
2961 | if (s->state == SERVICE_RUNNING) | |
2962 | service_enter_reload_by_notify(s); | |
2963 | ||
2964 | notify_dbus = true; | |
2965 | } | |
2966 | ||
8c47c732 | 2967 | /* Interpret READY= */ |
308d72dc LP |
2968 | if (strv_find(tags, "READY=1")) { |
2969 | ||
308d72dc LP |
2970 | s->notify_state = NOTIFY_READY; |
2971 | ||
2972 | /* Type=notify services inform us about completed | |
2973 | * initialization with READY=1 */ | |
2974 | if (s->type == SERVICE_NOTIFY && s->state == SERVICE_START) | |
2975 | service_enter_start_post(s); | |
2976 | ||
2977 | /* Sending READY=1 while we are reloading informs us | |
2978 | * that the reloading is complete */ | |
2979 | if (s->state == SERVICE_RELOAD && s->control_pid == 0) | |
2980 | service_enter_running(s, SERVICE_SUCCESS); | |
2981 | ||
2982 | notify_dbus = true; | |
2983 | } | |
2984 | ||
2985 | /* Interpret STOPPING= */ | |
2986 | if (strv_find(tags, "STOPPING=1")) { | |
2987 | ||
308d72dc LP |
2988 | s->notify_state = NOTIFY_STOPPING; |
2989 | ||
2990 | if (s->state == SERVICE_RUNNING) | |
2991 | service_enter_stop_by_notify(s); | |
2992 | ||
30b5275a | 2993 | notify_dbus = true; |
8c47c732 LP |
2994 | } |
2995 | ||
2996 | /* Interpret STATUS= */ | |
28849dba | 2997 | e = strv_find_startswith(tags, "STATUS="); |
7f110ff9 | 2998 | if (e) { |
28849dba | 2999 | _cleanup_free_ char *t = NULL; |
8c47c732 | 3000 | |
28849dba LP |
3001 | if (!isempty(e)) { |
3002 | if (!utf8_is_valid(e)) | |
f2341e0a | 3003 | log_unit_warning(u, "Status message in notification message is not UTF-8 clean."); |
28849dba | 3004 | else { |
28849dba LP |
3005 | t = strdup(e); |
3006 | if (!t) | |
3007 | log_oom(); | |
3a2776bc | 3008 | } |
28849dba | 3009 | } |
8c47c732 | 3010 | |
30b5275a | 3011 | if (!streq_ptr(s->status_text, t)) { |
28849dba | 3012 | |
3a2776bc LP |
3013 | free(s->status_text); |
3014 | s->status_text = t; | |
28849dba LP |
3015 | t = NULL; |
3016 | ||
30b5275a | 3017 | notify_dbus = true; |
28849dba | 3018 | } |
8c47c732 | 3019 | } |
842129f5 | 3020 | |
4774e357 | 3021 | /* Interpret ERRNO= */ |
28849dba | 3022 | e = strv_find_startswith(tags, "ERRNO="); |
4774e357 MAA |
3023 | if (e) { |
3024 | int status_errno; | |
3025 | ||
28849dba | 3026 | if (safe_atoi(e, &status_errno) < 0 || status_errno < 0) |
f2341e0a | 3027 | log_unit_warning(u, "Failed to parse ERRNO= field in notification message: %s", e); |
4774e357 | 3028 | else { |
4774e357 MAA |
3029 | if (s->status_errno != status_errno) { |
3030 | s->status_errno = status_errno; | |
3031 | notify_dbus = true; | |
3032 | } | |
3033 | } | |
3034 | } | |
3035 | ||
6f285378 | 3036 | /* Interpret WATCHDOG= */ |
1f6b4113 | 3037 | if (strv_find(tags, "WATCHDOG=1")) |
842129f5 | 3038 | service_reset_watchdog(s); |
c4e2ceae | 3039 | |
8dd4c05b LP |
3040 | if (strv_find(tags, "FDSTORE=1")) { |
3041 | const char *name; | |
3042 | ||
3043 | name = strv_find_startswith(tags, "FDNAME="); | |
3044 | if (name && !fdname_is_valid(name)) { | |
3045 | log_unit_warning(u, "Passed FDNAME= name is invalid, ignoring."); | |
3046 | name = NULL; | |
3047 | } | |
3048 | ||
3049 | service_add_fd_store_set(s, fds, name); | |
3050 | } | |
a354329f | 3051 | |
c4e2ceae | 3052 | /* Notify clients about changed status or main pid */ |
30b5275a LP |
3053 | if (notify_dbus) |
3054 | unit_add_to_dbus_queue(u); | |
8c47c732 LP |
3055 | } |
3056 | ||
68db7a3b ZJS |
3057 | static int service_get_timeout(Unit *u, uint64_t *timeout) { |
3058 | Service *s = SERVICE(u); | |
3059 | int r; | |
3060 | ||
3061 | if (!s->timer_event_source) | |
3062 | return 0; | |
3063 | ||
3064 | r = sd_event_source_get_time(s->timer_event_source, timeout); | |
3065 | if (r < 0) | |
3066 | return r; | |
3067 | ||
3068 | return 1; | |
3069 | } | |
3070 | ||
05e343b7 LP |
3071 | static void service_bus_name_owner_change( |
3072 | Unit *u, | |
3073 | const char *name, | |
3074 | const char *old_owner, | |
3075 | const char *new_owner) { | |
3076 | ||
3077 | Service *s = SERVICE(u); | |
718db961 | 3078 | int r; |
05e343b7 LP |
3079 | |
3080 | assert(s); | |
3081 | assert(name); | |
3082 | ||
3083 | assert(streq(s->bus_name, name)); | |
3084 | assert(old_owner || new_owner); | |
3085 | ||
3086 | if (old_owner && new_owner) | |
f2341e0a | 3087 | log_unit_debug(u, "D-Bus name %s changed owner from %s to %s", name, old_owner, new_owner); |
05e343b7 | 3088 | else if (old_owner) |
f2341e0a | 3089 | log_unit_debug(u, "D-Bus name %s no longer registered by %s", name, old_owner); |
05e343b7 | 3090 | else |
f2341e0a | 3091 | log_unit_debug(u, "D-Bus name %s now registered by %s", name, new_owner); |
05e343b7 LP |
3092 | |
3093 | s->bus_name_good = !!new_owner; | |
3094 | ||
3095 | if (s->type == SERVICE_DBUS) { | |
3096 | ||
3097 | /* service_enter_running() will figure out what to | |
3098 | * do */ | |
3099 | if (s->state == SERVICE_RUNNING) | |
f42806df | 3100 | service_enter_running(s, SERVICE_SUCCESS); |
05e343b7 LP |
3101 | else if (s->state == SERVICE_START && new_owner) |
3102 | service_enter_start_post(s); | |
3103 | ||
3104 | } else if (new_owner && | |
3105 | s->main_pid <= 0 && | |
3106 | (s->state == SERVICE_START || | |
3107 | s->state == SERVICE_START_POST || | |
3108 | s->state == SERVICE_RUNNING || | |
3109 | s->state == SERVICE_RELOAD)) { | |
3110 | ||
5b12334d | 3111 | _cleanup_bus_creds_unref_ sd_bus_creds *creds = NULL; |
718db961 | 3112 | pid_t pid; |
05e343b7 | 3113 | |
718db961 | 3114 | /* Try to acquire PID from bus service */ |
05e343b7 | 3115 | |
056f95d0 | 3116 | r = sd_bus_get_name_creds(u->manager->api_bus, name, SD_BUS_CREDS_PID, &creds); |
5b12334d LP |
3117 | if (r >= 0) |
3118 | r = sd_bus_creds_get_pid(creds, &pid); | |
718db961 | 3119 | if (r >= 0) { |
f2341e0a | 3120 | log_unit_debug(u, "D-Bus name %s is now owned by process %u", name, (unsigned) pid); |
05e343b7 | 3121 | |
718db961 LP |
3122 | service_set_main_pid(s, pid); |
3123 | unit_watch_pid(UNIT(s), pid); | |
3124 | } | |
7400b9d2 | 3125 | } |
05e343b7 LP |
3126 | } |
3127 | ||
16115b0a | 3128 | int service_set_socket_fd(Service *s, int fd, Socket *sock, bool selinux_context_net) { |
79a98c60 LP |
3129 | _cleanup_free_ char *peer = NULL; |
3130 | int r; | |
57020a3a | 3131 | |
4f2d528d LP |
3132 | assert(s); |
3133 | assert(fd >= 0); | |
3134 | ||
3135 | /* This is called by the socket code when instantiating a new | |
3136 | * service for a stream socket and the socket needs to be | |
3137 | * configured. */ | |
3138 | ||
1124fe6f | 3139 | if (UNIT(s)->load_state != UNIT_LOADED) |
4f2d528d LP |
3140 | return -EINVAL; |
3141 | ||
3142 | if (s->socket_fd >= 0) | |
3143 | return -EBUSY; | |
3144 | ||
3145 | if (s->state != SERVICE_DEAD) | |
3146 | return -EAGAIN; | |
3147 | ||
79a98c60 LP |
3148 | if (getpeername_pretty(fd, &peer) >= 0) { |
3149 | ||
3150 | if (UNIT(s)->description) { | |
3151 | _cleanup_free_ char *a; | |
3152 | ||
3153 | a = strjoin(UNIT(s)->description, " (", peer, ")", NULL); | |
3154 | if (!a) | |
3155 | return -ENOMEM; | |
3156 | ||
3157 | r = unit_set_description(UNIT(s), a); | |
3158 | } else | |
3159 | r = unit_set_description(UNIT(s), peer); | |
3160 | ||
3161 | if (r < 0) | |
3162 | return r; | |
3163 | } | |
3164 | ||
4f2d528d | 3165 | s->socket_fd = fd; |
16115b0a | 3166 | s->socket_fd_selinux_context_net = selinux_context_net; |
6cf6bbc2 | 3167 | |
57020a3a LP |
3168 | unit_ref_set(&s->accept_socket, UNIT(sock)); |
3169 | ||
3170 | return unit_add_two_dependencies(UNIT(sock), UNIT_BEFORE, UNIT_TRIGGERS, UNIT(s), false); | |
4f2d528d LP |
3171 | } |
3172 | ||
fdf20a31 | 3173 | static void service_reset_failed(Unit *u) { |
5632e374 LP |
3174 | Service *s = SERVICE(u); |
3175 | ||
3176 | assert(s); | |
3177 | ||
fdf20a31 | 3178 | if (s->state == SERVICE_FAILED) |
5632e374 LP |
3179 | service_set_state(s, SERVICE_DEAD); |
3180 | ||
f42806df LP |
3181 | s->result = SERVICE_SUCCESS; |
3182 | s->reload_result = SERVICE_SUCCESS; | |
451b34cc LP |
3183 | |
3184 | RATELIMIT_RESET(s->start_limit); | |
5632e374 LP |
3185 | } |
3186 | ||
718db961 | 3187 | static int service_kill(Unit *u, KillWho who, int signo, sd_bus_error *error) { |
8a0867d6 | 3188 | Service *s = SERVICE(u); |
41efeaec | 3189 | |
814cc562 | 3190 | return unit_kill_common(u, who, signo, s->main_pid, s->control_pid, error); |
8a0867d6 LP |
3191 | } |
3192 | ||
94f04347 | 3193 | static const char* const service_restart_table[_SERVICE_RESTART_MAX] = { |
525ee6f4 LP |
3194 | [SERVICE_RESTART_NO] = "no", |
3195 | [SERVICE_RESTART_ON_SUCCESS] = "on-success", | |
50caaedb | 3196 | [SERVICE_RESTART_ON_FAILURE] = "on-failure", |
6cfe2fde | 3197 | [SERVICE_RESTART_ON_ABNORMAL] = "on-abnormal", |
dc99a976 | 3198 | [SERVICE_RESTART_ON_WATCHDOG] = "on-watchdog", |
50caaedb | 3199 | [SERVICE_RESTART_ON_ABORT] = "on-abort", |
6cfe2fde | 3200 | [SERVICE_RESTART_ALWAYS] = "always", |
94f04347 LP |
3201 | }; |
3202 | ||
3203 | DEFINE_STRING_TABLE_LOOKUP(service_restart, ServiceRestart); | |
3204 | ||
3205 | static const char* const service_type_table[_SERVICE_TYPE_MAX] = { | |
94f04347 | 3206 | [SERVICE_SIMPLE] = "simple", |
0d624a78 | 3207 | [SERVICE_FORKING] = "forking", |
34e9ba66 | 3208 | [SERVICE_ONESHOT] = "oneshot", |
8c47c732 | 3209 | [SERVICE_DBUS] = "dbus", |
f2b68789 LP |
3210 | [SERVICE_NOTIFY] = "notify", |
3211 | [SERVICE_IDLE] = "idle" | |
94f04347 LP |
3212 | }; |
3213 | ||
3214 | DEFINE_STRING_TABLE_LOOKUP(service_type, ServiceType); | |
3215 | ||
e537352b | 3216 | static const char* const service_exec_command_table[_SERVICE_EXEC_COMMAND_MAX] = { |
94f04347 LP |
3217 | [SERVICE_EXEC_START_PRE] = "ExecStartPre", |
3218 | [SERVICE_EXEC_START] = "ExecStart", | |
3219 | [SERVICE_EXEC_START_POST] = "ExecStartPost", | |
3220 | [SERVICE_EXEC_RELOAD] = "ExecReload", | |
3221 | [SERVICE_EXEC_STOP] = "ExecStop", | |
3222 | [SERVICE_EXEC_STOP_POST] = "ExecStopPost", | |
3223 | }; | |
3224 | ||
3225 | DEFINE_STRING_TABLE_LOOKUP(service_exec_command, ServiceExecCommand); | |
3226 | ||
c952c6ec LP |
3227 | static const char* const notify_access_table[_NOTIFY_ACCESS_MAX] = { |
3228 | [NOTIFY_NONE] = "none", | |
3229 | [NOTIFY_MAIN] = "main", | |
3230 | [NOTIFY_ALL] = "all" | |
3231 | }; | |
3232 | ||
3233 | DEFINE_STRING_TABLE_LOOKUP(notify_access, NotifyAccess); | |
3234 | ||
308d72dc LP |
3235 | static const char* const notify_state_table[_NOTIFY_STATE_MAX] = { |
3236 | [NOTIFY_UNKNOWN] = "unknown", | |
3237 | [NOTIFY_READY] = "ready", | |
3238 | [NOTIFY_RELOADING] = "reloading", | |
3239 | [NOTIFY_STOPPING] = "stopping", | |
3240 | }; | |
3241 | ||
3242 | DEFINE_STRING_TABLE_LOOKUP(notify_state, NotifyState); | |
3243 | ||
f42806df LP |
3244 | static const char* const service_result_table[_SERVICE_RESULT_MAX] = { |
3245 | [SERVICE_SUCCESS] = "success", | |
3246 | [SERVICE_FAILURE_RESOURCES] = "resources", | |
3247 | [SERVICE_FAILURE_TIMEOUT] = "timeout", | |
3248 | [SERVICE_FAILURE_EXIT_CODE] = "exit-code", | |
3249 | [SERVICE_FAILURE_SIGNAL] = "signal", | |
bb242b7b | 3250 | [SERVICE_FAILURE_CORE_DUMP] = "core-dump", |
8d1b002a LP |
3251 | [SERVICE_FAILURE_WATCHDOG] = "watchdog", |
3252 | [SERVICE_FAILURE_START_LIMIT] = "start-limit" | |
f42806df LP |
3253 | }; |
3254 | ||
3255 | DEFINE_STRING_TABLE_LOOKUP(service_result, ServiceResult); | |
3256 | ||
87f0e418 | 3257 | const UnitVTable service_vtable = { |
7d17cfbc | 3258 | .object_size = sizeof(Service), |
718db961 LP |
3259 | .exec_context_offset = offsetof(Service, exec_context), |
3260 | .cgroup_context_offset = offsetof(Service, cgroup_context), | |
3261 | .kill_context_offset = offsetof(Service, kill_context), | |
613b411c | 3262 | .exec_runtime_offset = offsetof(Service, exec_runtime), |
3ef63c31 | 3263 | |
f975e971 LP |
3264 | .sections = |
3265 | "Unit\0" | |
3266 | "Service\0" | |
3267 | "Install\0", | |
4ad49000 | 3268 | .private_section = "Service", |
71645aca | 3269 | |
034c6ed7 LP |
3270 | .init = service_init, |
3271 | .done = service_done, | |
a16e1123 | 3272 | .load = service_load, |
a354329f | 3273 | .release_resources = service_release_resources, |
a16e1123 LP |
3274 | |
3275 | .coldplug = service_coldplug, | |
034c6ed7 | 3276 | |
5cb5a6ff LP |
3277 | .dump = service_dump, |
3278 | ||
3279 | .start = service_start, | |
3280 | .stop = service_stop, | |
3281 | .reload = service_reload, | |
3282 | ||
034c6ed7 LP |
3283 | .can_reload = service_can_reload, |
3284 | ||
8a0867d6 LP |
3285 | .kill = service_kill, |
3286 | ||
a16e1123 LP |
3287 | .serialize = service_serialize, |
3288 | .deserialize_item = service_deserialize_item, | |
3289 | ||
5cb5a6ff | 3290 | .active_state = service_active_state, |
10a94420 | 3291 | .sub_state_to_string = service_sub_state_to_string, |
5cb5a6ff | 3292 | |
701cc384 LP |
3293 | .check_gc = service_check_gc, |
3294 | .check_snapshot = service_check_snapshot, | |
3295 | ||
034c6ed7 | 3296 | .sigchld_event = service_sigchld_event, |
2c4104f0 | 3297 | |
fdf20a31 | 3298 | .reset_failed = service_reset_failed, |
5632e374 | 3299 | |
4ad49000 | 3300 | .notify_cgroup_empty = service_notify_cgroup_empty_event, |
8c47c732 | 3301 | .notify_message = service_notify_message, |
8e274523 | 3302 | |
05e343b7 | 3303 | .bus_name_owner_change = service_bus_name_owner_change, |
05e343b7 | 3304 | |
718db961 | 3305 | .bus_vtable = bus_service_vtable, |
74c964d3 LP |
3306 | .bus_set_property = bus_service_set_property, |
3307 | .bus_commit_properties = bus_service_commit_properties, | |
4139c1b2 | 3308 | |
68db7a3b | 3309 | .get_timeout = service_get_timeout, |
718db961 LP |
3310 | .can_transient = true, |
3311 | ||
c6918296 MS |
3312 | .status_message_formats = { |
3313 | .starting_stopping = { | |
3314 | [0] = "Starting %s...", | |
3315 | [1] = "Stopping %s...", | |
3316 | }, | |
3317 | .finished_start_job = { | |
3318 | [JOB_DONE] = "Started %s.", | |
3319 | [JOB_FAILED] = "Failed to start %s.", | |
c6918296 MS |
3320 | }, |
3321 | .finished_stop_job = { | |
3322 | [JOB_DONE] = "Stopped %s.", | |
3323 | [JOB_FAILED] = "Stopped (with error) %s.", | |
c6918296 MS |
3324 | }, |
3325 | }, | |
5cb5a6ff | 3326 | }; |