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