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