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