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