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