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