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