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