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