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