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