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