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