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