]>
Commit | Line | Data |
---|---|---|
d6c9574f | 1 | /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/ |
60918275 | 2 | |
a7334b09 LP |
3 | /*** |
4 | This file is part of systemd. | |
5 | ||
6 | Copyright 2010 Lennart Poettering | |
7 | ||
8 | systemd is free software; you can redistribute it and/or modify it | |
9 | under the terms of the GNU General Public License as published by | |
10 | the Free Software Foundation; either version 2 of the License, or | |
11 | (at your option) any later version. | |
12 | ||
13 | systemd is distributed in the hope that it will be useful, but | |
14 | WITHOUT ANY WARRANTY; without even the implied warranty of | |
15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
16 | General Public License for more details. | |
17 | ||
18 | You should have received a copy of the GNU General Public License | |
19 | along with systemd; If not, see <http://www.gnu.org/licenses/>. | |
20 | ***/ | |
21 | ||
60918275 LP |
22 | #include <assert.h> |
23 | #include <errno.h> | |
87d1515d | 24 | #include <string.h> |
9152c765 LP |
25 | #include <sys/epoll.h> |
26 | #include <signal.h> | |
27 | #include <sys/signalfd.h> | |
28 | #include <sys/wait.h> | |
29 | #include <unistd.h> | |
30 | #include <sys/poll.h> | |
e1414003 LP |
31 | #include <sys/reboot.h> |
32 | #include <sys/ioctl.h> | |
33 | #include <linux/kd.h> | |
80876c20 LP |
34 | #include <termios.h> |
35 | #include <fcntl.h> | |
a16e1123 LP |
36 | #include <sys/types.h> |
37 | #include <sys/stat.h> | |
fe51822e | 38 | #include <dirent.h> |
830f6caa LP |
39 | |
40 | #ifdef HAVE_AUDIT | |
4927fcae | 41 | #include <libaudit.h> |
830f6caa | 42 | #endif |
60918275 LP |
43 | |
44 | #include "manager.h" | |
45 | #include "hashmap.h" | |
46 | #include "macro.h" | |
47 | #include "strv.h" | |
16354eff | 48 | #include "log.h" |
2a987ee8 | 49 | #include "util.h" |
ea430986 | 50 | #include "ratelimit.h" |
8e274523 LP |
51 | #include "cgroup.h" |
52 | #include "mount-setup.h" | |
9e2f7c11 | 53 | #include "unit-name.h" |
4139c1b2 LP |
54 | #include "dbus-unit.h" |
55 | #include "dbus-job.h" | |
1137a57c | 56 | #include "missing.h" |
84e3543e | 57 | #include "path-lookup.h" |
514f4ef5 | 58 | #include "special.h" |
398ef8ba | 59 | #include "bus-errors.h" |
d06dacd0 | 60 | #include "exit-status.h" |
60918275 | 61 | |
701cc384 LP |
62 | /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */ |
63 | #define GC_QUEUE_ENTRIES_MAX 16 | |
64 | ||
65 | /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */ | |
94b6dfa2 | 66 | #define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC) |
701cc384 | 67 | |
8c47c732 | 68 | /* Where clients shall send notification messages to */ |
2b583ce6 | 69 | #define NOTIFY_SOCKET_SYSTEM "/run/systemd/notify" |
91b22f21 | 70 | #define NOTIFY_SOCKET_USER "@/org/freedesktop/systemd1/notify" |
8c47c732 LP |
71 | |
72 | static int manager_setup_notify(Manager *m) { | |
73 | union { | |
74 | struct sockaddr sa; | |
75 | struct sockaddr_un un; | |
76 | } sa; | |
77 | struct epoll_event ev; | |
8c47c732 LP |
78 | int one = 1; |
79 | ||
80 | assert(m); | |
81 | ||
82 | m->notify_watch.type = WATCH_NOTIFY; | |
83 | if ((m->notify_watch.fd = socket(AF_UNIX, SOCK_DGRAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) { | |
84 | log_error("Failed to allocate notification socket: %m"); | |
85 | return -errno; | |
86 | } | |
87 | ||
88 | zero(sa); | |
89 | sa.sa.sa_family = AF_UNIX; | |
90 | ||
a821caaa | 91 | if (getpid() != 1) |
91b22f21 | 92 | snprintf(sa.un.sun_path, sizeof(sa.un.sun_path), NOTIFY_SOCKET_USER "/%llu", random_ull()); |
6f79c579 LP |
93 | else { |
94 | unlink(NOTIFY_SOCKET_SYSTEM); | |
91b22f21 | 95 | strncpy(sa.un.sun_path, NOTIFY_SOCKET_SYSTEM, sizeof(sa.un.sun_path)); |
6f79c579 | 96 | } |
91b22f21 LP |
97 | |
98 | if (sa.un.sun_path[0] == '@') | |
99 | sa.un.sun_path[0] = 0; | |
8c47c732 | 100 | |
0e098b15 | 101 | if (bind(m->notify_watch.fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) { |
8c47c732 LP |
102 | log_error("bind() failed: %m"); |
103 | return -errno; | |
104 | } | |
105 | ||
106 | if (setsockopt(m->notify_watch.fd, SOL_SOCKET, SO_PASSCRED, &one, sizeof(one)) < 0) { | |
107 | log_error("SO_PASSCRED failed: %m"); | |
108 | return -errno; | |
109 | } | |
110 | ||
111 | zero(ev); | |
112 | ev.events = EPOLLIN; | |
113 | ev.data.ptr = &m->notify_watch; | |
114 | ||
115 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->notify_watch.fd, &ev) < 0) | |
116 | return -errno; | |
117 | ||
91b22f21 LP |
118 | if (sa.un.sun_path[0] == 0) |
119 | sa.un.sun_path[0] = '@'; | |
120 | ||
121 | if (!(m->notify_socket = strdup(sa.un.sun_path))) | |
8c47c732 LP |
122 | return -ENOMEM; |
123 | ||
b2bb3dbe LP |
124 | log_debug("Using notification socket %s", m->notify_socket); |
125 | ||
8c47c732 LP |
126 | return 0; |
127 | } | |
128 | ||
80876c20 | 129 | static int enable_special_signals(Manager *m) { |
4466ee6a | 130 | int fd; |
80876c20 LP |
131 | |
132 | assert(m); | |
133 | ||
134 | /* Enable that we get SIGINT on control-alt-del */ | |
135 | if (reboot(RB_DISABLE_CAD) < 0) | |
136 | log_warning("Failed to enable ctrl-alt-del handling: %m"); | |
137 | ||
affda787 | 138 | if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY)) < 0) |
80876c20 LP |
139 | log_warning("Failed to open /dev/tty0: %m"); |
140 | else { | |
141 | /* Enable that we get SIGWINCH on kbrequest */ | |
142 | if (ioctl(fd, KDSIGACCEPT, SIGWINCH) < 0) | |
143 | log_warning("Failed to enable kbrequest handling: %s", strerror(errno)); | |
144 | ||
145 | close_nointr_nofail(fd); | |
146 | } | |
147 | ||
148 | return 0; | |
149 | } | |
150 | ||
ce578209 | 151 | static int manager_setup_signals(Manager *m) { |
9152c765 LP |
152 | sigset_t mask; |
153 | struct epoll_event ev; | |
57c0c30e | 154 | struct sigaction sa; |
60918275 | 155 | |
ce578209 LP |
156 | assert(m); |
157 | ||
57c0c30e LP |
158 | /* We are not interested in SIGSTOP and friends. */ |
159 | zero(sa); | |
160 | sa.sa_handler = SIG_DFL; | |
161 | sa.sa_flags = SA_NOCLDSTOP|SA_RESTART; | |
162 | assert_se(sigaction(SIGCHLD, &sa, NULL) == 0); | |
163 | ||
ce578209 | 164 | assert_se(sigemptyset(&mask) == 0); |
7d793605 LP |
165 | |
166 | sigset_add_many(&mask, | |
167 | SIGCHLD, /* Child died */ | |
168 | SIGTERM, /* Reexecute daemon */ | |
169 | SIGHUP, /* Reload configuration */ | |
170 | SIGUSR1, /* systemd/upstart: reconnect to D-Bus */ | |
171 | SIGUSR2, /* systemd: dump status */ | |
172 | SIGINT, /* Kernel sends us this on control-alt-del */ | |
173 | SIGWINCH, /* Kernel sends us this on kbrequest (alt-arrowup) */ | |
174 | SIGPWR, /* Some kernel drivers and upsd send us this on power failure */ | |
175 | SIGRTMIN+0, /* systemd: start default.target */ | |
0003d1ab | 176 | SIGRTMIN+1, /* systemd: isolate rescue.target */ |
7d793605 LP |
177 | SIGRTMIN+2, /* systemd: isolate emergency.target */ |
178 | SIGRTMIN+3, /* systemd: start halt.target */ | |
179 | SIGRTMIN+4, /* systemd: start poweroff.target */ | |
180 | SIGRTMIN+5, /* systemd: start reboot.target */ | |
0003d1ab LP |
181 | SIGRTMIN+6, /* systemd: start kexec.target */ |
182 | SIGRTMIN+13, /* systemd: Immediate halt */ | |
183 | SIGRTMIN+14, /* systemd: Immediate poweroff */ | |
184 | SIGRTMIN+15, /* systemd: Immediate reboot */ | |
185 | SIGRTMIN+16, /* systemd: Immediate kexec */ | |
0658666b LP |
186 | SIGRTMIN+20, /* systemd: enable status messages */ |
187 | SIGRTMIN+21, /* systemd: disable status messages */ | |
7d793605 | 188 | -1); |
ce578209 LP |
189 | assert_se(sigprocmask(SIG_SETMASK, &mask, NULL) == 0); |
190 | ||
ef734fd6 | 191 | m->signal_watch.type = WATCH_SIGNAL; |
ce578209 LP |
192 | if ((m->signal_watch.fd = signalfd(-1, &mask, SFD_NONBLOCK|SFD_CLOEXEC)) < 0) |
193 | return -errno; | |
194 | ||
195 | zero(ev); | |
196 | ev.events = EPOLLIN; | |
197 | ev.data.ptr = &m->signal_watch; | |
198 | ||
199 | if (epoll_ctl(m->epoll_fd, EPOLL_CTL_ADD, m->signal_watch.fd, &ev) < 0) | |
200 | return -errno; | |
201 | ||
a3d4e06d | 202 | if (m->running_as == MANAGER_SYSTEM) |
80876c20 | 203 | return enable_special_signals(m); |
e1414003 | 204 | |
ce578209 LP |
205 | return 0; |
206 | } | |
207 | ||
9e58ff9c | 208 | int manager_new(ManagerRunningAs running_as, Manager **_m) { |
ce578209 | 209 | Manager *m; |
8e274523 LP |
210 | int r = -ENOMEM; |
211 | ||
212 | assert(_m); | |
a5dab5ce LP |
213 | assert(running_as >= 0); |
214 | assert(running_as < _MANAGER_RUNNING_AS_MAX); | |
ce578209 | 215 | |
60918275 | 216 | if (!(m = new0(Manager, 1))) |
8e274523 | 217 | return -ENOMEM; |
60918275 | 218 | |
63983207 | 219 | dual_timestamp_get(&m->startup_timestamp); |
e537352b | 220 | |
a5dab5ce | 221 | m->running_as = running_as; |
a567261a | 222 | m->name_data_slot = m->subscribed_data_slot = -1; |
a16e1123 | 223 | m->exit_code = _MANAGER_EXIT_CODE_INVALID; |
33be102a | 224 | m->pin_cgroupfs_fd = -1; |
80876c20 | 225 | |
4927fcae LP |
226 | #ifdef HAVE_AUDIT |
227 | m->audit_fd = -1; | |
228 | #endif | |
229 | ||
4e434314 | 230 | m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = m->swap_watch.fd = -1; |
ea430986 | 231 | m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */ |
9152c765 | 232 | |
1137a57c LP |
233 | if (!(m->environment = strv_copy(environ))) |
234 | goto fail; | |
235 | ||
06d4c99a LP |
236 | if (!(m->default_controllers = strv_new("cpu", NULL))) |
237 | goto fail; | |
238 | ||
87f0e418 | 239 | if (!(m->units = hashmap_new(string_hash_func, string_compare_func))) |
60918275 LP |
240 | goto fail; |
241 | ||
242 | if (!(m->jobs = hashmap_new(trivial_hash_func, trivial_compare_func))) | |
243 | goto fail; | |
244 | ||
e5b5ae50 | 245 | if (!(m->transaction_jobs = hashmap_new(trivial_hash_func, trivial_compare_func))) |
60918275 LP |
246 | goto fail; |
247 | ||
9152c765 LP |
248 | if (!(m->watch_pids = hashmap_new(trivial_hash_func, trivial_compare_func))) |
249 | goto fail; | |
250 | ||
8e274523 LP |
251 | if (!(m->cgroup_bondings = hashmap_new(string_hash_func, string_compare_func))) |
252 | goto fail; | |
253 | ||
05e343b7 LP |
254 | if (!(m->watch_bus = hashmap_new(string_hash_func, string_compare_func))) |
255 | goto fail; | |
256 | ||
9152c765 LP |
257 | if ((m->epoll_fd = epoll_create1(EPOLL_CLOEXEC)) < 0) |
258 | goto fail; | |
259 | ||
84e3543e | 260 | if ((r = lookup_paths_init(&m->lookup_paths, m->running_as)) < 0) |
e1414003 LP |
261 | goto fail; |
262 | ||
8e274523 LP |
263 | if ((r = manager_setup_signals(m)) < 0) |
264 | goto fail; | |
265 | ||
8e274523 | 266 | if ((r = manager_setup_cgroup(m)) < 0) |
9152c765 LP |
267 | goto fail; |
268 | ||
8c47c732 LP |
269 | if ((r = manager_setup_notify(m)) < 0) |
270 | goto fail; | |
271 | ||
f278026d | 272 | /* Try to connect to the busses, if possible. */ |
3996fbe2 | 273 | if ((r = bus_init(m, running_as != MANAGER_SYSTEM)) < 0) |
ea430986 LP |
274 | goto fail; |
275 | ||
e543deae | 276 | #ifdef HAVE_AUDIT |
4927fcae LP |
277 | if ((m->audit_fd = audit_open()) < 0) |
278 | log_error("Failed to connect to audit log: %m"); | |
e543deae | 279 | #endif |
4927fcae | 280 | |
72bc8d00 LP |
281 | m->taint_usr = dir_is_empty("/usr") > 0; |
282 | ||
8e274523 LP |
283 | *_m = m; |
284 | return 0; | |
60918275 LP |
285 | |
286 | fail: | |
287 | manager_free(m); | |
8e274523 | 288 | return r; |
60918275 LP |
289 | } |
290 | ||
23a177ef LP |
291 | static unsigned manager_dispatch_cleanup_queue(Manager *m) { |
292 | Meta *meta; | |
293 | unsigned n = 0; | |
294 | ||
295 | assert(m); | |
296 | ||
297 | while ((meta = m->cleanup_queue)) { | |
298 | assert(meta->in_cleanup_queue); | |
299 | ||
399ab2b1 | 300 | unit_free((Unit*) meta); |
23a177ef LP |
301 | n++; |
302 | } | |
303 | ||
304 | return n; | |
305 | } | |
306 | ||
eced69b3 | 307 | enum { |
35b8ca3a | 308 | GC_OFFSET_IN_PATH, /* This one is on the path we were traveling */ |
eced69b3 LP |
309 | GC_OFFSET_UNSURE, /* No clue */ |
310 | GC_OFFSET_GOOD, /* We still need this unit */ | |
311 | GC_OFFSET_BAD, /* We don't need this unit anymore */ | |
312 | _GC_OFFSET_MAX | |
313 | }; | |
314 | ||
315 | static void unit_gc_sweep(Unit *u, unsigned gc_marker) { | |
701cc384 LP |
316 | Iterator i; |
317 | Unit *other; | |
eced69b3 | 318 | bool is_bad; |
701cc384 LP |
319 | |
320 | assert(u); | |
321 | ||
eced69b3 LP |
322 | if (u->meta.gc_marker == gc_marker + GC_OFFSET_GOOD || |
323 | u->meta.gc_marker == gc_marker + GC_OFFSET_BAD || | |
324 | u->meta.gc_marker == gc_marker + GC_OFFSET_IN_PATH) | |
701cc384 LP |
325 | return; |
326 | ||
c9c0cadb | 327 | if (u->meta.in_cleanup_queue) |
701cc384 LP |
328 | goto bad; |
329 | ||
330 | if (unit_check_gc(u)) | |
331 | goto good; | |
332 | ||
eced69b3 LP |
333 | u->meta.gc_marker = gc_marker + GC_OFFSET_IN_PATH; |
334 | ||
335 | is_bad = true; | |
336 | ||
701cc384 LP |
337 | SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) { |
338 | unit_gc_sweep(other, gc_marker); | |
339 | ||
eced69b3 | 340 | if (other->meta.gc_marker == gc_marker + GC_OFFSET_GOOD) |
701cc384 | 341 | goto good; |
eced69b3 LP |
342 | |
343 | if (other->meta.gc_marker != gc_marker + GC_OFFSET_BAD) | |
344 | is_bad = false; | |
701cc384 LP |
345 | } |
346 | ||
eced69b3 LP |
347 | if (is_bad) |
348 | goto bad; | |
349 | ||
350 | /* We were unable to find anything out about this entry, so | |
351 | * let's investigate it later */ | |
352 | u->meta.gc_marker = gc_marker + GC_OFFSET_UNSURE; | |
353 | unit_add_to_gc_queue(u); | |
354 | return; | |
355 | ||
701cc384 | 356 | bad: |
eced69b3 LP |
357 | /* We definitely know that this one is not useful anymore, so |
358 | * let's mark it for deletion */ | |
359 | u->meta.gc_marker = gc_marker + GC_OFFSET_BAD; | |
360 | unit_add_to_cleanup_queue(u); | |
701cc384 LP |
361 | return; |
362 | ||
363 | good: | |
eced69b3 | 364 | u->meta.gc_marker = gc_marker + GC_OFFSET_GOOD; |
701cc384 LP |
365 | } |
366 | ||
367 | static unsigned manager_dispatch_gc_queue(Manager *m) { | |
368 | Meta *meta; | |
369 | unsigned n = 0; | |
eced69b3 | 370 | unsigned gc_marker; |
701cc384 LP |
371 | |
372 | assert(m); | |
373 | ||
374 | if ((m->n_in_gc_queue < GC_QUEUE_ENTRIES_MAX) && | |
375 | (m->gc_queue_timestamp <= 0 || | |
376 | (m->gc_queue_timestamp + GC_QUEUE_USEC_MAX) > now(CLOCK_MONOTONIC))) | |
377 | return 0; | |
378 | ||
379 | log_debug("Running GC..."); | |
380 | ||
eced69b3 LP |
381 | m->gc_marker += _GC_OFFSET_MAX; |
382 | if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX) | |
c9c0cadb | 383 | m->gc_marker = 1; |
701cc384 | 384 | |
eced69b3 LP |
385 | gc_marker = m->gc_marker; |
386 | ||
701cc384 LP |
387 | while ((meta = m->gc_queue)) { |
388 | assert(meta->in_gc_queue); | |
389 | ||
399ab2b1 | 390 | unit_gc_sweep((Unit*) meta, gc_marker); |
eced69b3 | 391 | |
701cc384 LP |
392 | LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta); |
393 | meta->in_gc_queue = false; | |
394 | ||
395 | n++; | |
396 | ||
eced69b3 LP |
397 | if (meta->gc_marker == gc_marker + GC_OFFSET_BAD || |
398 | meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) { | |
701cc384 | 399 | log_debug("Collecting %s", meta->id); |
eced69b3 | 400 | meta->gc_marker = gc_marker + GC_OFFSET_BAD; |
399ab2b1 | 401 | unit_add_to_cleanup_queue((Unit*) meta); |
701cc384 LP |
402 | } |
403 | } | |
404 | ||
405 | m->n_in_gc_queue = 0; | |
406 | m->gc_queue_timestamp = 0; | |
407 | ||
408 | return n; | |
409 | } | |
410 | ||
a16e1123 | 411 | static void manager_clear_jobs_and_units(Manager *m) { |
e5b5ae50 | 412 | Job *j; |
a16e1123 | 413 | Unit *u; |
60918275 LP |
414 | |
415 | assert(m); | |
416 | ||
87f0e418 | 417 | while ((j = hashmap_first(m->transaction_jobs))) |
e5b5ae50 LP |
418 | job_free(j); |
419 | ||
87f0e418 LP |
420 | while ((u = hashmap_first(m->units))) |
421 | unit_free(u); | |
964e0949 LP |
422 | |
423 | manager_dispatch_cleanup_queue(m); | |
424 | ||
425 | assert(!m->load_queue); | |
426 | assert(!m->run_queue); | |
427 | assert(!m->dbus_unit_queue); | |
428 | assert(!m->dbus_job_queue); | |
429 | assert(!m->cleanup_queue); | |
430 | assert(!m->gc_queue); | |
431 | ||
432 | assert(hashmap_isempty(m->transaction_jobs)); | |
433 | assert(hashmap_isempty(m->jobs)); | |
434 | assert(hashmap_isempty(m->units)); | |
a16e1123 LP |
435 | } |
436 | ||
437 | void manager_free(Manager *m) { | |
438 | UnitType c; | |
87f0e418 | 439 | |
a16e1123 LP |
440 | assert(m); |
441 | ||
442 | manager_clear_jobs_and_units(m); | |
23a177ef | 443 | |
7824bbeb LP |
444 | for (c = 0; c < _UNIT_TYPE_MAX; c++) |
445 | if (unit_vtable[c]->shutdown) | |
446 | unit_vtable[c]->shutdown(m); | |
447 | ||
a16e1123 LP |
448 | /* If we reexecute ourselves, we keep the root cgroup |
449 | * around */ | |
c6c18be3 | 450 | manager_shutdown_cgroup(m, m->exit_code != MANAGER_REEXECUTE); |
8e274523 | 451 | |
5a1e9937 LP |
452 | manager_undo_generators(m); |
453 | ||
5e8d1c9a | 454 | bus_done(m); |
ea430986 | 455 | |
87f0e418 | 456 | hashmap_free(m->units); |
60918275 | 457 | hashmap_free(m->jobs); |
e5b5ae50 | 458 | hashmap_free(m->transaction_jobs); |
9152c765 | 459 | hashmap_free(m->watch_pids); |
05e343b7 | 460 | hashmap_free(m->watch_bus); |
9152c765 LP |
461 | |
462 | if (m->epoll_fd >= 0) | |
a16e1123 | 463 | close_nointr_nofail(m->epoll_fd); |
acbb0225 | 464 | if (m->signal_watch.fd >= 0) |
a16e1123 | 465 | close_nointr_nofail(m->signal_watch.fd); |
8c47c732 LP |
466 | if (m->notify_watch.fd >= 0) |
467 | close_nointr_nofail(m->notify_watch.fd); | |
60918275 | 468 | |
4927fcae LP |
469 | #ifdef HAVE_AUDIT |
470 | if (m->audit_fd >= 0) | |
471 | audit_close(m->audit_fd); | |
472 | #endif | |
473 | ||
c952c6ec LP |
474 | free(m->notify_socket); |
475 | ||
84e3543e | 476 | lookup_paths_free(&m->lookup_paths); |
1137a57c | 477 | strv_free(m->environment); |
036643a2 | 478 | |
06d4c99a LP |
479 | strv_free(m->default_controllers); |
480 | ||
8e274523 | 481 | hashmap_free(m->cgroup_bondings); |
c6c18be3 | 482 | set_free_free(m->unit_path_cache); |
33be102a | 483 | |
60918275 LP |
484 | free(m); |
485 | } | |
486 | ||
a16e1123 LP |
487 | int manager_enumerate(Manager *m) { |
488 | int r = 0, q; | |
f50e0a01 | 489 | UnitType c; |
f50e0a01 LP |
490 | |
491 | assert(m); | |
492 | ||
a16e1123 LP |
493 | /* Let's ask every type to load all units from disk/kernel |
494 | * that it might know */ | |
f50e0a01 LP |
495 | for (c = 0; c < _UNIT_TYPE_MAX; c++) |
496 | if (unit_vtable[c]->enumerate) | |
a16e1123 LP |
497 | if ((q = unit_vtable[c]->enumerate(m)) < 0) |
498 | r = q; | |
f50e0a01 LP |
499 | |
500 | manager_dispatch_load_queue(m); | |
a16e1123 LP |
501 | return r; |
502 | } | |
503 | ||
504 | int manager_coldplug(Manager *m) { | |
505 | int r = 0, q; | |
506 | Iterator i; | |
507 | Unit *u; | |
508 | char *k; | |
509 | ||
510 | assert(m); | |
f50e0a01 LP |
511 | |
512 | /* Then, let's set up their initial state. */ | |
513 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
514 | ||
515 | /* ignore aliases */ | |
9e2f7c11 | 516 | if (u->meta.id != k) |
f50e0a01 LP |
517 | continue; |
518 | ||
cca098b0 LP |
519 | if ((q = unit_coldplug(u)) < 0) |
520 | r = q; | |
f50e0a01 LP |
521 | } |
522 | ||
a16e1123 LP |
523 | return r; |
524 | } | |
525 | ||
fe51822e LP |
526 | static void manager_build_unit_path_cache(Manager *m) { |
527 | char **i; | |
528 | DIR *d = NULL; | |
529 | int r; | |
530 | ||
531 | assert(m); | |
532 | ||
533 | set_free_free(m->unit_path_cache); | |
534 | ||
535 | if (!(m->unit_path_cache = set_new(string_hash_func, string_compare_func))) { | |
536 | log_error("Failed to allocate unit path cache."); | |
537 | return; | |
538 | } | |
539 | ||
540 | /* This simply builds a list of files we know exist, so that | |
541 | * we don't always have to go to disk */ | |
542 | ||
543 | STRV_FOREACH(i, m->lookup_paths.unit_path) { | |
544 | struct dirent *de; | |
545 | ||
546 | if (!(d = opendir(*i))) { | |
547 | log_error("Failed to open directory: %m"); | |
548 | continue; | |
549 | } | |
550 | ||
551 | while ((de = readdir(d))) { | |
552 | char *p; | |
553 | ||
554 | if (ignore_file(de->d_name)) | |
555 | continue; | |
556 | ||
557 | if (asprintf(&p, "%s/%s", streq(*i, "/") ? "" : *i, de->d_name) < 0) { | |
558 | r = -ENOMEM; | |
559 | goto fail; | |
560 | } | |
561 | ||
562 | if ((r = set_put(m->unit_path_cache, p)) < 0) { | |
563 | free(p); | |
564 | goto fail; | |
565 | } | |
566 | } | |
567 | ||
568 | closedir(d); | |
569 | d = NULL; | |
570 | } | |
571 | ||
572 | return; | |
573 | ||
574 | fail: | |
575 | log_error("Failed to build unit path cache: %s", strerror(-r)); | |
576 | ||
577 | set_free_free(m->unit_path_cache); | |
578 | m->unit_path_cache = NULL; | |
579 | ||
580 | if (d) | |
581 | closedir(d); | |
582 | } | |
583 | ||
a16e1123 LP |
584 | int manager_startup(Manager *m, FILE *serialization, FDSet *fds) { |
585 | int r, q; | |
586 | ||
587 | assert(m); | |
588 | ||
5a1e9937 LP |
589 | manager_run_generators(m); |
590 | ||
fe51822e LP |
591 | manager_build_unit_path_cache(m); |
592 | ||
9f611ad8 LP |
593 | /* If we will deserialize make sure that during enumeration |
594 | * this is already known, so we increase the counter here | |
595 | * already */ | |
596 | if (serialization) | |
597 | m->n_deserializing ++; | |
598 | ||
a16e1123 LP |
599 | /* First, enumerate what we can from all config files */ |
600 | r = manager_enumerate(m); | |
601 | ||
602 | /* Second, deserialize if there is something to deserialize */ | |
603 | if (serialization) | |
604 | if ((q = manager_deserialize(m, serialization, fds)) < 0) | |
605 | r = q; | |
606 | ||
607 | /* Third, fire things up! */ | |
608 | if ((q = manager_coldplug(m)) < 0) | |
609 | r = q; | |
610 | ||
9f611ad8 LP |
611 | if (serialization) { |
612 | assert(m->n_deserializing > 0); | |
613 | m->n_deserializing --; | |
614 | } | |
615 | ||
a16e1123 | 616 | return r; |
f50e0a01 LP |
617 | } |
618 | ||
23a177ef | 619 | static void transaction_delete_job(Manager *m, Job *j, bool delete_dependencies) { |
302d0040 LP |
620 | assert(m); |
621 | assert(j); | |
622 | ||
1ffba6fe LP |
623 | /* Deletes one job from the transaction */ |
624 | ||
23a177ef | 625 | manager_transaction_unlink_job(m, j, delete_dependencies); |
302d0040 | 626 | |
ac1135be | 627 | if (!j->installed) |
302d0040 LP |
628 | job_free(j); |
629 | } | |
630 | ||
87f0e418 | 631 | static void transaction_delete_unit(Manager *m, Unit *u) { |
1ffba6fe LP |
632 | Job *j; |
633 | ||
87f0e418 | 634 | /* Deletes all jobs associated with a certain unit from the |
1ffba6fe LP |
635 | * transaction */ |
636 | ||
87f0e418 | 637 | while ((j = hashmap_get(m->transaction_jobs, u))) |
23a177ef | 638 | transaction_delete_job(m, j, true); |
1ffba6fe LP |
639 | } |
640 | ||
f04fa1d5 LP |
641 | static void transaction_clean_dependencies(Manager *m) { |
642 | Iterator i; | |
643 | Job *j; | |
644 | ||
645 | assert(m); | |
646 | ||
647 | /* Drops all dependencies of all installed jobs */ | |
648 | ||
649 | HASHMAP_FOREACH(j, m->jobs, i) { | |
650 | while (j->subject_list) | |
651 | job_dependency_free(j->subject_list); | |
652 | while (j->object_list) | |
653 | job_dependency_free(j->object_list); | |
654 | } | |
655 | ||
656 | assert(!m->transaction_anchor); | |
657 | } | |
658 | ||
11dd41ce LP |
659 | static void transaction_abort(Manager *m) { |
660 | Job *j; | |
661 | ||
662 | assert(m); | |
11dd41ce | 663 | |
e5b5ae50 | 664 | while ((j = hashmap_first(m->transaction_jobs))) |
ac1135be | 665 | if (j->installed) |
23a177ef | 666 | transaction_delete_job(m, j, true); |
e5b5ae50 LP |
667 | else |
668 | job_free(j); | |
669 | ||
670 | assert(hashmap_isempty(m->transaction_jobs)); | |
f04fa1d5 LP |
671 | |
672 | transaction_clean_dependencies(m); | |
e5b5ae50 LP |
673 | } |
674 | ||
675 | static void transaction_find_jobs_that_matter_to_anchor(Manager *m, Job *j, unsigned generation) { | |
676 | JobDependency *l; | |
677 | ||
678 | assert(m); | |
679 | ||
87f0e418 | 680 | /* A recursive sweep through the graph that marks all units |
1ffba6fe LP |
681 | * that matter to the anchor job, i.e. are directly or |
682 | * indirectly a dependency of the anchor job via paths that | |
683 | * are fully marked as mattering. */ | |
684 | ||
44d8db9e LP |
685 | if (j) |
686 | l = j->subject_list; | |
687 | else | |
688 | l = m->transaction_anchor; | |
689 | ||
690 | LIST_FOREACH(subject, l, l) { | |
e5b5ae50 LP |
691 | |
692 | /* This link does not matter */ | |
693 | if (!l->matters) | |
694 | continue; | |
695 | ||
87f0e418 | 696 | /* This unit has already been marked */ |
e5b5ae50 LP |
697 | if (l->object->generation == generation) |
698 | continue; | |
699 | ||
700 | l->object->matters_to_anchor = true; | |
701 | l->object->generation = generation; | |
702 | ||
703 | transaction_find_jobs_that_matter_to_anchor(m, l->object, generation); | |
704 | } | |
705 | } | |
706 | ||
7fad411c | 707 | static void transaction_merge_and_delete_job(Manager *m, Job *j, Job *other, JobType t) { |
e5b5ae50 LP |
708 | JobDependency *l, *last; |
709 | ||
710 | assert(j); | |
711 | assert(other); | |
87f0e418 | 712 | assert(j->unit == other->unit); |
ac1135be | 713 | assert(!j->installed); |
e5b5ae50 | 714 | |
1ffba6fe LP |
715 | /* Merges 'other' into 'j' and then deletes j. */ |
716 | ||
e5b5ae50 LP |
717 | j->type = t; |
718 | j->state = JOB_WAITING; | |
9e2f7c11 | 719 | j->override = j->override || other->override; |
e5b5ae50 LP |
720 | |
721 | j->matters_to_anchor = j->matters_to_anchor || other->matters_to_anchor; | |
722 | ||
723 | /* Patch us in as new owner of the JobDependency objects */ | |
724 | last = NULL; | |
44d8db9e | 725 | LIST_FOREACH(subject, l, other->subject_list) { |
e5b5ae50 LP |
726 | assert(l->subject == other); |
727 | l->subject = j; | |
728 | last = l; | |
729 | } | |
730 | ||
731 | /* Merge both lists */ | |
732 | if (last) { | |
733 | last->subject_next = j->subject_list; | |
734 | if (j->subject_list) | |
735 | j->subject_list->subject_prev = last; | |
736 | j->subject_list = other->subject_list; | |
737 | } | |
738 | ||
739 | /* Patch us in as new owner of the JobDependency objects */ | |
740 | last = NULL; | |
44d8db9e | 741 | LIST_FOREACH(object, l, other->object_list) { |
e5b5ae50 LP |
742 | assert(l->object == other); |
743 | l->object = j; | |
744 | last = l; | |
745 | } | |
746 | ||
747 | /* Merge both lists */ | |
748 | if (last) { | |
749 | last->object_next = j->object_list; | |
750 | if (j->object_list) | |
751 | j->object_list->object_prev = last; | |
752 | j->object_list = other->object_list; | |
753 | } | |
754 | ||
e5b5ae50 LP |
755 | /* Kill the other job */ |
756 | other->subject_list = NULL; | |
757 | other->object_list = NULL; | |
23a177ef | 758 | transaction_delete_job(m, other, true); |
e5b5ae50 | 759 | } |
69dd2852 LP |
760 | static bool job_is_conflicted_by(Job *j) { |
761 | JobDependency *l; | |
762 | ||
763 | assert(j); | |
764 | ||
765 | /* Returns true if this job is pulled in by a least one | |
766 | * ConflictedBy dependency. */ | |
767 | ||
768 | LIST_FOREACH(object, l, j->object_list) | |
769 | if (l->conflicts) | |
770 | return true; | |
771 | ||
772 | return false; | |
773 | } | |
e5b5ae50 | 774 | |
5cb5a6ff | 775 | static int delete_one_unmergeable_job(Manager *m, Job *j) { |
1ffba6fe LP |
776 | Job *k; |
777 | ||
778 | assert(j); | |
779 | ||
780 | /* Tries to delete one item in the linked list | |
781 | * j->transaction_next->transaction_next->... that conflicts | |
35b8ca3a | 782 | * with another one, in an attempt to make an inconsistent |
1ffba6fe LP |
783 | * transaction work. */ |
784 | ||
785 | /* We rely here on the fact that if a merged with b does not | |
786 | * merge with c, either a or b merge with c neither */ | |
034c6ed7 LP |
787 | LIST_FOREACH(transaction, j, j) |
788 | LIST_FOREACH(transaction, k, j->transaction_next) { | |
1ffba6fe LP |
789 | Job *d; |
790 | ||
791 | /* Is this one mergeable? Then skip it */ | |
5cb5a6ff | 792 | if (job_type_is_mergeable(j->type, k->type)) |
1ffba6fe LP |
793 | continue; |
794 | ||
795 | /* Ok, we found two that conflict, let's see if we can | |
796 | * drop one of them */ | |
69dd2852 LP |
797 | if (!j->matters_to_anchor && !k->matters_to_anchor) { |
798 | ||
799 | /* Both jobs don't matter, so let's | |
800 | * find the one that is smarter to | |
801 | * remove. Let's think positive and | |
802 | * rather remove stops then starts -- | |
803 | * except if something is being | |
804 | * stopped because it is conflicted by | |
805 | * another unit in which case we | |
806 | * rather remove the start. */ | |
807 | ||
808 | log_debug("Looking at job %s/%s conflicted_by=%s", j->unit->meta.id, job_type_to_string(j->type), yes_no(j->type == JOB_STOP && job_is_conflicted_by(j))); | |
809 | log_debug("Looking at job %s/%s conflicted_by=%s", k->unit->meta.id, job_type_to_string(k->type), yes_no(k->type == JOB_STOP && job_is_conflicted_by(k))); | |
810 | ||
811 | if (j->type == JOB_STOP) { | |
812 | ||
813 | if (job_is_conflicted_by(j)) | |
814 | d = k; | |
815 | else | |
816 | d = j; | |
817 | ||
818 | } else if (k->type == JOB_STOP) { | |
819 | ||
820 | if (job_is_conflicted_by(k)) | |
821 | d = j; | |
822 | else | |
823 | d = k; | |
e364ad06 LP |
824 | } else |
825 | d = j; | |
69dd2852 LP |
826 | |
827 | } else if (!j->matters_to_anchor) | |
1ffba6fe LP |
828 | d = j; |
829 | else if (!k->matters_to_anchor) | |
830 | d = k; | |
831 | else | |
832 | return -ENOEXEC; | |
833 | ||
834 | /* Ok, we can drop one, so let's do so. */ | |
2e81c8a5 | 835 | log_debug("Fixing conflicting jobs by deleting job %s/%s", d->unit->meta.id, job_type_to_string(d->type)); |
23a177ef | 836 | transaction_delete_job(m, d, true); |
1ffba6fe LP |
837 | return 0; |
838 | } | |
839 | ||
840 | return -EINVAL; | |
841 | } | |
842 | ||
398ef8ba | 843 | static int transaction_merge_jobs(Manager *m, DBusError *e) { |
11dd41ce | 844 | Job *j; |
034c6ed7 | 845 | Iterator i; |
e5b5ae50 LP |
846 | int r; |
847 | ||
848 | assert(m); | |
849 | ||
1ffba6fe LP |
850 | /* First step, check whether any of the jobs for one specific |
851 | * task conflict. If so, try to drop one of them. */ | |
034c6ed7 | 852 | HASHMAP_FOREACH(j, m->transaction_jobs, i) { |
1ffba6fe LP |
853 | JobType t; |
854 | Job *k; | |
855 | ||
856 | t = j->type; | |
034c6ed7 | 857 | LIST_FOREACH(transaction, k, j->transaction_next) { |
e364ad06 | 858 | if (job_type_merge(&t, k->type) >= 0) |
1ffba6fe LP |
859 | continue; |
860 | ||
861 | /* OK, we could not merge all jobs for this | |
862 | * action. Let's see if we can get rid of one | |
863 | * of them */ | |
864 | ||
5cb5a6ff | 865 | if ((r = delete_one_unmergeable_job(m, j)) >= 0) |
1ffba6fe LP |
866 | /* Ok, we managed to drop one, now |
867 | * let's ask our callers to call us | |
868 | * again after garbage collecting */ | |
869 | return -EAGAIN; | |
870 | ||
871 | /* We couldn't merge anything. Failure */ | |
398ef8ba LP |
872 | dbus_set_error(e, BUS_ERROR_TRANSACTION_JOBS_CONFLICTING, "Transaction contains conflicting jobs '%s' and '%s' for %s. Probably contradicting requirement dependencies configured.", |
873 | job_type_to_string(t), job_type_to_string(k->type), k->unit->meta.id); | |
1ffba6fe LP |
874 | return r; |
875 | } | |
876 | } | |
877 | ||
878 | /* Second step, merge the jobs. */ | |
034c6ed7 | 879 | HASHMAP_FOREACH(j, m->transaction_jobs, i) { |
e5b5ae50 LP |
880 | JobType t = j->type; |
881 | Job *k; | |
882 | ||
e094e853 | 883 | /* Merge all transactions */ |
034c6ed7 | 884 | LIST_FOREACH(transaction, k, j->transaction_next) |
1ffba6fe | 885 | assert_se(job_type_merge(&t, k->type) == 0); |
e5b5ae50 | 886 | |
5cb5a6ff | 887 | /* If an active job is mergeable, merge it too */ |
87f0e418 LP |
888 | if (j->unit->meta.job) |
889 | job_type_merge(&t, j->unit->meta.job->type); /* Might fail. Which is OK */ | |
e094e853 | 890 | |
e5b5ae50 | 891 | while ((k = j->transaction_next)) { |
ac1135be | 892 | if (j->installed) { |
7fad411c | 893 | transaction_merge_and_delete_job(m, k, j, t); |
e5b5ae50 LP |
894 | j = k; |
895 | } else | |
7fad411c | 896 | transaction_merge_and_delete_job(m, j, k, t); |
e5b5ae50 LP |
897 | } |
898 | ||
899 | assert(!j->transaction_next); | |
900 | assert(!j->transaction_prev); | |
901 | } | |
902 | ||
7fad411c | 903 | return 0; |
e5b5ae50 LP |
904 | } |
905 | ||
23a177ef LP |
906 | static void transaction_drop_redundant(Manager *m) { |
907 | bool again; | |
908 | ||
909 | assert(m); | |
910 | ||
911 | /* Goes through the transaction and removes all jobs that are | |
912 | * a noop */ | |
913 | ||
914 | do { | |
915 | Job *j; | |
916 | Iterator i; | |
917 | ||
918 | again = false; | |
919 | ||
920 | HASHMAP_FOREACH(j, m->transaction_jobs, i) { | |
921 | bool changes_something = false; | |
922 | Job *k; | |
923 | ||
924 | LIST_FOREACH(transaction, k, j) { | |
925 | ||
926 | if (!job_is_anchor(k) && | |
3aea3b35 LP |
927 | (k->installed || job_type_is_redundant(k->type, unit_active_state(k->unit))) && |
928 | (!k->unit->meta.job || !job_type_is_conflicting(k->type, k->unit->meta.job->type))) | |
23a177ef LP |
929 | continue; |
930 | ||
931 | changes_something = true; | |
932 | break; | |
933 | } | |
934 | ||
935 | if (changes_something) | |
936 | continue; | |
937 | ||
9b3d9090 | 938 | /* log_debug("Found redundant job %s/%s, dropping.", j->unit->meta.id, job_type_to_string(j->type)); */ |
23a177ef LP |
939 | transaction_delete_job(m, j, false); |
940 | again = true; | |
941 | break; | |
942 | } | |
943 | ||
944 | } while (again); | |
945 | } | |
946 | ||
87f0e418 LP |
947 | static bool unit_matters_to_anchor(Unit *u, Job *j) { |
948 | assert(u); | |
1ffba6fe LP |
949 | assert(!j->transaction_prev); |
950 | ||
87f0e418 | 951 | /* Checks whether at least one of the jobs for this unit |
1ffba6fe LP |
952 | * matters to the anchor. */ |
953 | ||
034c6ed7 | 954 | LIST_FOREACH(transaction, j, j) |
1ffba6fe LP |
955 | if (j->matters_to_anchor) |
956 | return true; | |
957 | ||
958 | return false; | |
959 | } | |
960 | ||
398ef8ba | 961 | static int transaction_verify_order_one(Manager *m, Job *j, Job *from, unsigned generation, DBusError *e) { |
034c6ed7 | 962 | Iterator i; |
87f0e418 | 963 | Unit *u; |
11dd41ce | 964 | int r; |
e5b5ae50 LP |
965 | |
966 | assert(m); | |
967 | assert(j); | |
1ffba6fe LP |
968 | assert(!j->transaction_prev); |
969 | ||
970 | /* Does a recursive sweep through the ordering graph, looking | |
971 | * for a cycle. If we find cycle we try to break it. */ | |
e5b5ae50 | 972 | |
23e3c588 LP |
973 | /* Have we seen this before? */ |
974 | if (j->generation == generation) { | |
674a6e4d | 975 | Job *k, *delete; |
e5b5ae50 | 976 | |
23e3c588 LP |
977 | /* If the marker is NULL we have been here already and |
978 | * decided the job was loop-free from here. Hence | |
979 | * shortcut things and return right-away. */ | |
980 | if (!j->marker) | |
981 | return 0; | |
e5b5ae50 | 982 | |
23e3c588 LP |
983 | /* So, the marker is not NULL and we already have been |
984 | * here. We have a cycle. Let's try to break it. We go | |
985 | * backwards in our path and try to find a suitable | |
986 | * job to remove. We use the marker to find our way | |
987 | * back, since smart how we are we stored our way back | |
988 | * in there. */ | |
54165a39 | 989 | log_warning("Found ordering cycle on %s/%s", j->unit->meta.id, job_type_to_string(j->type)); |
9f04bd52 | 990 | |
674a6e4d | 991 | delete = NULL; |
23e3c588 | 992 | for (k = from; k; k = ((k->generation == generation && k->marker != k) ? k->marker : NULL)) { |
1ffba6fe | 993 | |
54165a39 | 994 | log_info("Walked on cycle path to %s/%s", k->unit->meta.id, job_type_to_string(k->type)); |
9f04bd52 | 995 | |
674a6e4d LP |
996 | if (!delete && |
997 | !k->installed && | |
87f0e418 | 998 | !unit_matters_to_anchor(k->unit, k)) { |
1ffba6fe LP |
999 | /* Ok, we can drop this one, so let's |
1000 | * do so. */ | |
674a6e4d | 1001 | delete = k; |
e5b5ae50 LP |
1002 | } |
1003 | ||
1004 | /* Check if this in fact was the beginning of | |
7fad411c | 1005 | * the cycle */ |
e5b5ae50 LP |
1006 | if (k == j) |
1007 | break; | |
1008 | } | |
1009 | ||
674a6e4d LP |
1010 | |
1011 | if (delete) { | |
54ec68b6 | 1012 | log_warning("Breaking ordering cycle by deleting job %s/%s", delete->unit->meta.id, job_type_to_string(delete->type)); |
674a6e4d LP |
1013 | transaction_delete_unit(m, delete->unit); |
1014 | return -EAGAIN; | |
1015 | } | |
1016 | ||
54165a39 | 1017 | log_error("Unable to break cycle"); |
9f04bd52 | 1018 | |
fe71c02c | 1019 | dbus_set_error(e, BUS_ERROR_TRANSACTION_ORDER_IS_CYCLIC, "Transaction order is cyclic. See system logs for details."); |
1ffba6fe | 1020 | return -ENOEXEC; |
e5b5ae50 LP |
1021 | } |
1022 | ||
1ffba6fe | 1023 | /* Make the marker point to where we come from, so that we can |
23e3c588 LP |
1024 | * find our way backwards if we want to break a cycle. We use |
1025 | * a special marker for the beginning: we point to | |
1026 | * ourselves. */ | |
1027 | j->marker = from ? from : j; | |
e5b5ae50 LP |
1028 | j->generation = generation; |
1029 | ||
1ffba6fe | 1030 | /* We assume that the the dependencies are bidirectional, and |
87f0e418 LP |
1031 | * hence can ignore UNIT_AFTER */ |
1032 | SET_FOREACH(u, j->unit->meta.dependencies[UNIT_BEFORE], i) { | |
e5b5ae50 LP |
1033 | Job *o; |
1034 | ||
87f0e418 LP |
1035 | /* Is there a job for this unit? */ |
1036 | if (!(o = hashmap_get(m->transaction_jobs, u))) | |
1ffba6fe LP |
1037 | |
1038 | /* Ok, there is no job for this in the | |
1039 | * transaction, but maybe there is already one | |
1040 | * running? */ | |
87f0e418 | 1041 | if (!(o = u->meta.job)) |
e5b5ae50 LP |
1042 | continue; |
1043 | ||
398ef8ba | 1044 | if ((r = transaction_verify_order_one(m, o, j, generation, e)) < 0) |
e5b5ae50 LP |
1045 | return r; |
1046 | } | |
1047 | ||
9f04bd52 LP |
1048 | /* Ok, let's backtrack, and remember that this entry is not on |
1049 | * our path anymore. */ | |
1050 | j->marker = NULL; | |
1051 | ||
e5b5ae50 LP |
1052 | return 0; |
1053 | } | |
1054 | ||
398ef8ba | 1055 | static int transaction_verify_order(Manager *m, unsigned *generation, DBusError *e) { |
1ffba6fe LP |
1056 | Job *j; |
1057 | int r; | |
034c6ed7 | 1058 | Iterator i; |
23e3c588 | 1059 | unsigned g; |
1ffba6fe | 1060 | |
e5b5ae50 LP |
1061 | assert(m); |
1062 | assert(generation); | |
1063 | ||
1ffba6fe LP |
1064 | /* Check if the ordering graph is cyclic. If it is, try to fix |
1065 | * that up by dropping one of the jobs. */ | |
e5b5ae50 | 1066 | |
23e3c588 LP |
1067 | g = (*generation)++; |
1068 | ||
034c6ed7 | 1069 | HASHMAP_FOREACH(j, m->transaction_jobs, i) |
398ef8ba | 1070 | if ((r = transaction_verify_order_one(m, j, NULL, g, e)) < 0) |
1ffba6fe | 1071 | return r; |
e5b5ae50 LP |
1072 | |
1073 | return 0; | |
1074 | } | |
1075 | ||
1076 | static void transaction_collect_garbage(Manager *m) { | |
1077 | bool again; | |
1078 | ||
1079 | assert(m); | |
1080 | ||
1ffba6fe LP |
1081 | /* Drop jobs that are not required by any other job */ |
1082 | ||
e5b5ae50 | 1083 | do { |
034c6ed7 | 1084 | Iterator i; |
e5b5ae50 LP |
1085 | Job *j; |
1086 | ||
1087 | again = false; | |
1088 | ||
034c6ed7 | 1089 | HASHMAP_FOREACH(j, m->transaction_jobs, i) { |
9b3d9090 LP |
1090 | if (j->object_list) { |
1091 | /* log_debug("Keeping job %s/%s because of %s/%s", */ | |
1092 | /* j->unit->meta.id, job_type_to_string(j->type), */ | |
1093 | /* j->object_list->subject ? j->object_list->subject->unit->meta.id : "root", */ | |
1094 | /* j->object_list->subject ? job_type_to_string(j->object_list->subject->type) : "root"); */ | |
e5b5ae50 | 1095 | continue; |
9b3d9090 | 1096 | } |
e5b5ae50 | 1097 | |
9b3d9090 | 1098 | /* log_debug("Garbage collecting job %s/%s", j->unit->meta.id, job_type_to_string(j->type)); */ |
23a177ef | 1099 | transaction_delete_job(m, j, true); |
e5b5ae50 LP |
1100 | again = true; |
1101 | break; | |
1102 | } | |
1103 | ||
1104 | } while (again); | |
1105 | } | |
1106 | ||
398ef8ba | 1107 | static int transaction_is_destructive(Manager *m, DBusError *e) { |
034c6ed7 | 1108 | Iterator i; |
e5b5ae50 | 1109 | Job *j; |
11dd41ce LP |
1110 | |
1111 | assert(m); | |
11dd41ce | 1112 | |
e5b5ae50 LP |
1113 | /* Checks whether applying this transaction means that |
1114 | * existing jobs would be replaced */ | |
11dd41ce | 1115 | |
034c6ed7 | 1116 | HASHMAP_FOREACH(j, m->transaction_jobs, i) { |
e094e853 LP |
1117 | |
1118 | /* Assume merged */ | |
1119 | assert(!j->transaction_prev); | |
1120 | assert(!j->transaction_next); | |
1121 | ||
87f0e418 LP |
1122 | if (j->unit->meta.job && |
1123 | j->unit->meta.job != j && | |
398ef8ba LP |
1124 | !job_type_is_superset(j->type, j->unit->meta.job->type)) { |
1125 | ||
1126 | dbus_set_error(e, BUS_ERROR_TRANSACTION_IS_DESTRUCTIVE, "Transaction is destructive."); | |
e5b5ae50 | 1127 | return -EEXIST; |
398ef8ba | 1128 | } |
e094e853 | 1129 | } |
11dd41ce | 1130 | |
e5b5ae50 LP |
1131 | return 0; |
1132 | } | |
1133 | ||
e094e853 LP |
1134 | static void transaction_minimize_impact(Manager *m) { |
1135 | bool again; | |
1136 | assert(m); | |
1137 | ||
1138 | /* Drops all unnecessary jobs that reverse already active jobs | |
1139 | * or that stop a running service. */ | |
1140 | ||
1141 | do { | |
1142 | Job *j; | |
034c6ed7 | 1143 | Iterator i; |
e094e853 LP |
1144 | |
1145 | again = false; | |
1146 | ||
034c6ed7 LP |
1147 | HASHMAP_FOREACH(j, m->transaction_jobs, i) { |
1148 | LIST_FOREACH(transaction, j, j) { | |
c20cae32 | 1149 | bool stops_running_service, changes_existing_job; |
e094e853 LP |
1150 | |
1151 | /* If it matters, we shouldn't drop it */ | |
1152 | if (j->matters_to_anchor) | |
1153 | continue; | |
1154 | ||
1155 | /* Would this stop a running service? | |
1156 | * Would this change an existing job? | |
1157 | * If so, let's drop this entry */ | |
c20cae32 LP |
1158 | |
1159 | stops_running_service = | |
1160 | j->type == JOB_STOP && UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(j->unit)); | |
1161 | ||
1162 | changes_existing_job = | |
143072ed LP |
1163 | j->unit->meta.job && |
1164 | job_type_is_conflicting(j->type, j->unit->meta.job->type); | |
c20cae32 LP |
1165 | |
1166 | if (!stops_running_service && !changes_existing_job) | |
e094e853 LP |
1167 | continue; |
1168 | ||
c20cae32 | 1169 | if (stops_running_service) |
54165a39 | 1170 | log_info("%s/%s would stop a running service.", j->unit->meta.id, job_type_to_string(j->type)); |
c20cae32 LP |
1171 | |
1172 | if (changes_existing_job) | |
54165a39 | 1173 | log_info("%s/%s would change existing job.", j->unit->meta.id, job_type_to_string(j->type)); |
c20cae32 | 1174 | |
e094e853 | 1175 | /* Ok, let's get rid of this */ |
54165a39 | 1176 | log_info("Deleting %s/%s to minimize impact.", j->unit->meta.id, job_type_to_string(j->type)); |
c20cae32 | 1177 | |
23a177ef | 1178 | transaction_delete_job(m, j, true); |
e094e853 LP |
1179 | again = true; |
1180 | break; | |
1181 | } | |
1182 | ||
1183 | if (again) | |
1184 | break; | |
1185 | } | |
1186 | ||
1187 | } while (again); | |
1188 | } | |
1189 | ||
c497c7a9 | 1190 | static int transaction_apply(Manager *m) { |
034c6ed7 | 1191 | Iterator i; |
e5b5ae50 LP |
1192 | Job *j; |
1193 | int r; | |
1194 | ||
1ffba6fe LP |
1195 | /* Moves the transaction jobs to the set of active jobs */ |
1196 | ||
034c6ed7 | 1197 | HASHMAP_FOREACH(j, m->transaction_jobs, i) { |
e094e853 LP |
1198 | /* Assume merged */ |
1199 | assert(!j->transaction_prev); | |
1200 | assert(!j->transaction_next); | |
1201 | ||
ac1135be | 1202 | if (j->installed) |
e5b5ae50 LP |
1203 | continue; |
1204 | ||
1205 | if ((r = hashmap_put(m->jobs, UINT32_TO_PTR(j->id), j)) < 0) | |
11dd41ce LP |
1206 | goto rollback; |
1207 | } | |
1208 | ||
e5b5ae50 | 1209 | while ((j = hashmap_steal_first(m->transaction_jobs))) { |
9b3d9090 LP |
1210 | if (j->installed) { |
1211 | /* log_debug("Skipping already installed job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id); */ | |
e5b5ae50 | 1212 | continue; |
9b3d9090 | 1213 | } |
e5b5ae50 | 1214 | |
87f0e418 LP |
1215 | if (j->unit->meta.job) |
1216 | job_free(j->unit->meta.job); | |
11dd41ce | 1217 | |
87f0e418 | 1218 | j->unit->meta.job = j; |
ac1135be | 1219 | j->installed = true; |
e409f875 | 1220 | m->n_installed_jobs ++; |
11dd41ce | 1221 | |
e5b5ae50 LP |
1222 | /* We're fully installed. Now let's free data we don't |
1223 | * need anymore. */ | |
1224 | ||
1225 | assert(!j->transaction_next); | |
1226 | assert(!j->transaction_prev); | |
1227 | ||
c1e1601e LP |
1228 | job_add_to_run_queue(j); |
1229 | job_add_to_dbus_queue(j); | |
312732cf | 1230 | job_start_timer(j); |
28c3247e LP |
1231 | |
1232 | log_debug("Installed new job %s/%s as %u", j->unit->meta.id, job_type_to_string(j->type), (unsigned) j->id); | |
01184e04 LP |
1233 | } |
1234 | ||
1235 | /* As last step, kill all remaining job dependencies. */ | |
f04fa1d5 | 1236 | transaction_clean_dependencies(m); |
1ffba6fe | 1237 | |
11dd41ce LP |
1238 | return 0; |
1239 | ||
1240 | rollback: | |
1241 | ||
034c6ed7 | 1242 | HASHMAP_FOREACH(j, m->transaction_jobs, i) { |
ac1135be | 1243 | if (j->installed) |
e5b5ae50 LP |
1244 | continue; |
1245 | ||
1246 | hashmap_remove(m->jobs, UINT32_TO_PTR(j->id)); | |
1247 | } | |
1248 | ||
1249 | return r; | |
1250 | } | |
1251 | ||
398ef8ba | 1252 | static int transaction_activate(Manager *m, JobMode mode, DBusError *e) { |
e5b5ae50 LP |
1253 | int r; |
1254 | unsigned generation = 1; | |
1255 | ||
1256 | assert(m); | |
1257 | ||
1258 | /* This applies the changes recorded in transaction_jobs to | |
1259 | * the actual list of jobs, if possible. */ | |
1260 | ||
1261 | /* First step: figure out which jobs matter */ | |
1262 | transaction_find_jobs_that_matter_to_anchor(m, NULL, generation++); | |
1263 | ||
e094e853 LP |
1264 | /* Second step: Try not to stop any running services if |
1265 | * we don't have to. Don't try to reverse running | |
1266 | * jobs if we don't have to. */ | |
143072ed | 1267 | if (mode == JOB_FAIL) |
c88e7f4e | 1268 | transaction_minimize_impact(m); |
e094e853 | 1269 | |
23a177ef LP |
1270 | /* Third step: Drop redundant jobs */ |
1271 | transaction_drop_redundant(m); | |
1272 | ||
1ffba6fe | 1273 | for (;;) { |
23a177ef | 1274 | /* Fourth step: Let's remove unneeded jobs that might |
1ffba6fe | 1275 | * be lurking. */ |
a8049b7a LP |
1276 | if (mode != JOB_ISOLATE) |
1277 | transaction_collect_garbage(m); | |
e5b5ae50 | 1278 | |
23a177ef | 1279 | /* Fifth step: verify order makes sense and correct |
1ffba6fe | 1280 | * cycles if necessary and possible */ |
398ef8ba | 1281 | if ((r = transaction_verify_order(m, &generation, e)) >= 0) |
1ffba6fe | 1282 | break; |
e5b5ae50 | 1283 | |
9f04bd52 | 1284 | if (r != -EAGAIN) { |
398ef8ba | 1285 | log_warning("Requested transaction contains an unfixable cyclic ordering dependency: %s", bus_error(e, r)); |
1ffba6fe | 1286 | goto rollback; |
9f04bd52 | 1287 | } |
e5b5ae50 | 1288 | |
1ffba6fe LP |
1289 | /* Let's see if the resulting transaction ordering |
1290 | * graph is still cyclic... */ | |
1291 | } | |
1292 | ||
1293 | for (;;) { | |
23a177ef | 1294 | /* Sixth step: let's drop unmergeable entries if |
1ffba6fe LP |
1295 | * necessary and possible, merge entries we can |
1296 | * merge */ | |
398ef8ba | 1297 | if ((r = transaction_merge_jobs(m, e)) >= 0) |
1ffba6fe LP |
1298 | break; |
1299 | ||
9f04bd52 | 1300 | if (r != -EAGAIN) { |
35b8ca3a | 1301 | log_warning("Requested transaction contains unmergeable jobs: %s", bus_error(e, r)); |
1ffba6fe | 1302 | goto rollback; |
9f04bd52 | 1303 | } |
1ffba6fe | 1304 | |
23a177ef | 1305 | /* Seventh step: an entry got dropped, let's garbage |
1ffba6fe | 1306 | * collect its dependencies. */ |
a8049b7a LP |
1307 | if (mode != JOB_ISOLATE) |
1308 | transaction_collect_garbage(m); | |
1ffba6fe LP |
1309 | |
1310 | /* Let's see if the resulting transaction still has | |
5cb5a6ff | 1311 | * unmergeable entries ... */ |
1ffba6fe LP |
1312 | } |
1313 | ||
23a177ef LP |
1314 | /* Eights step: Drop redundant jobs again, if the merging now allows us to drop more. */ |
1315 | transaction_drop_redundant(m); | |
1316 | ||
1317 | /* Ninth step: check whether we can actually apply this */ | |
e5b5ae50 | 1318 | if (mode == JOB_FAIL) |
398ef8ba LP |
1319 | if ((r = transaction_is_destructive(m, e)) < 0) { |
1320 | log_notice("Requested transaction contradicts existing jobs: %s", bus_error(e, r)); | |
e5b5ae50 | 1321 | goto rollback; |
9f04bd52 | 1322 | } |
e5b5ae50 | 1323 | |
23a177ef | 1324 | /* Tenth step: apply changes */ |
c497c7a9 | 1325 | if ((r = transaction_apply(m)) < 0) { |
54165a39 | 1326 | log_warning("Failed to apply transaction: %s", strerror(-r)); |
e5b5ae50 | 1327 | goto rollback; |
9f04bd52 | 1328 | } |
e5b5ae50 LP |
1329 | |
1330 | assert(hashmap_isempty(m->transaction_jobs)); | |
1331 | assert(!m->transaction_anchor); | |
1332 | ||
1333 | return 0; | |
11dd41ce | 1334 | |
e5b5ae50 | 1335 | rollback: |
11dd41ce LP |
1336 | transaction_abort(m); |
1337 | return r; | |
1338 | } | |
1339 | ||
9e2f7c11 | 1340 | static Job* transaction_add_one_job(Manager *m, JobType type, Unit *unit, bool override, bool *is_new) { |
e5b5ae50 | 1341 | Job *j, *f; |
60918275 LP |
1342 | |
1343 | assert(m); | |
87f0e418 | 1344 | assert(unit); |
60918275 | 1345 | |
35b8ca3a | 1346 | /* Looks for an existing prospective job and returns that. If |
e5b5ae50 LP |
1347 | * it doesn't exist it is created and added to the prospective |
1348 | * jobs list. */ | |
60918275 | 1349 | |
87f0e418 | 1350 | f = hashmap_get(m->transaction_jobs, unit); |
60918275 | 1351 | |
034c6ed7 | 1352 | LIST_FOREACH(transaction, j, f) { |
87f0e418 | 1353 | assert(j->unit == unit); |
60918275 | 1354 | |
e5b5ae50 LP |
1355 | if (j->type == type) { |
1356 | if (is_new) | |
1357 | *is_new = false; | |
1358 | return j; | |
1359 | } | |
1360 | } | |
60918275 | 1361 | |
87f0e418 LP |
1362 | if (unit->meta.job && unit->meta.job->type == type) |
1363 | j = unit->meta.job; | |
1364 | else if (!(j = job_new(m, type, unit))) | |
e5b5ae50 | 1365 | return NULL; |
60918275 | 1366 | |
e5b5ae50 LP |
1367 | j->generation = 0; |
1368 | j->marker = NULL; | |
1369 | j->matters_to_anchor = false; | |
9e2f7c11 | 1370 | j->override = override; |
60918275 | 1371 | |
034c6ed7 LP |
1372 | LIST_PREPEND(Job, transaction, f, j); |
1373 | ||
e364ad06 | 1374 | if (hashmap_replace(m->transaction_jobs, unit, f) < 0) { |
034c6ed7 LP |
1375 | job_free(j); |
1376 | return NULL; | |
1377 | } | |
1378 | ||
e5b5ae50 LP |
1379 | if (is_new) |
1380 | *is_new = true; | |
60918275 | 1381 | |
9b3d9090 | 1382 | /* log_debug("Added job %s/%s to transaction.", unit->meta.id, job_type_to_string(type)); */ |
23a177ef | 1383 | |
e5b5ae50 LP |
1384 | return j; |
1385 | } | |
11dd41ce | 1386 | |
23a177ef | 1387 | void manager_transaction_unlink_job(Manager *m, Job *j, bool delete_dependencies) { |
e5b5ae50 LP |
1388 | assert(m); |
1389 | assert(j); | |
11dd41ce | 1390 | |
e5b5ae50 LP |
1391 | if (j->transaction_prev) |
1392 | j->transaction_prev->transaction_next = j->transaction_next; | |
1393 | else if (j->transaction_next) | |
87f0e418 | 1394 | hashmap_replace(m->transaction_jobs, j->unit, j->transaction_next); |
e5b5ae50 | 1395 | else |
87f0e418 | 1396 | hashmap_remove_value(m->transaction_jobs, j->unit, j); |
e5b5ae50 LP |
1397 | |
1398 | if (j->transaction_next) | |
1399 | j->transaction_next->transaction_prev = j->transaction_prev; | |
1400 | ||
1401 | j->transaction_prev = j->transaction_next = NULL; | |
1402 | ||
1403 | while (j->subject_list) | |
1404 | job_dependency_free(j->subject_list); | |
1e198baf LP |
1405 | |
1406 | while (j->object_list) { | |
1407 | Job *other = j->object_list->matters ? j->object_list->subject : NULL; | |
1408 | ||
e5b5ae50 | 1409 | job_dependency_free(j->object_list); |
1e198baf | 1410 | |
23a177ef | 1411 | if (other && delete_dependencies) { |
2e81c8a5 | 1412 | log_debug("Deleting job %s/%s as dependency of job %s/%s", |
9e2f7c11 LP |
1413 | other->unit->meta.id, job_type_to_string(other->type), |
1414 | j->unit->meta.id, job_type_to_string(j->type)); | |
23a177ef | 1415 | transaction_delete_job(m, other, delete_dependencies); |
1e198baf LP |
1416 | } |
1417 | } | |
e5b5ae50 LP |
1418 | } |
1419 | ||
9e2f7c11 LP |
1420 | static int transaction_add_job_and_dependencies( |
1421 | Manager *m, | |
1422 | JobType type, | |
1423 | Unit *unit, | |
1424 | Job *by, | |
1425 | bool matters, | |
1426 | bool override, | |
69dd2852 | 1427 | bool conflicts, |
cebe0d41 LP |
1428 | bool ignore_requirements, |
1429 | bool ignore_order, | |
398ef8ba | 1430 | DBusError *e, |
9e2f7c11 | 1431 | Job **_ret) { |
e5b5ae50 | 1432 | Job *ret; |
034c6ed7 | 1433 | Iterator i; |
87f0e418 | 1434 | Unit *dep; |
e5b5ae50 LP |
1435 | int r; |
1436 | bool is_new; | |
1437 | ||
1438 | assert(m); | |
1439 | assert(type < _JOB_TYPE_MAX); | |
87f0e418 | 1440 | assert(unit); |
e5b5ae50 | 1441 | |
3aea3b35 LP |
1442 | /* log_debug("Pulling in %s/%s from %s/%s", */ |
1443 | /* unit->meta.id, job_type_to_string(type), */ | |
1444 | /* by ? by->unit->meta.id : "NA", */ | |
1445 | /* by ? job_type_to_string(by->type) : "NA"); */ | |
1446 | ||
00dc5d76 LP |
1447 | if (unit->meta.load_state != UNIT_LOADED && |
1448 | unit->meta.load_state != UNIT_ERROR && | |
6daf4f90 | 1449 | unit->meta.load_state != UNIT_MASKED) { |
8821a00f LP |
1450 | dbus_set_error(e, BUS_ERROR_LOAD_FAILED, "Unit %s is not loaded properly.", unit->meta.id); |
1451 | return -EINVAL; | |
1452 | } | |
1453 | ||
fdf20a31 | 1454 | if (type != JOB_STOP && unit->meta.load_state == UNIT_ERROR) { |
00dc5d76 LP |
1455 | dbus_set_error(e, BUS_ERROR_LOAD_FAILED, |
1456 | "Unit %s failed to load: %s. " | |
da8f9f8c | 1457 | "See system logs and 'systemctl status' for details.", |
8821a00f LP |
1458 | unit->meta.id, |
1459 | strerror(-unit->meta.load_error)); | |
21b293e8 | 1460 | return -EINVAL; |
398ef8ba | 1461 | } |
21b293e8 | 1462 | |
6daf4f90 LP |
1463 | if (type != JOB_STOP && unit->meta.load_state == UNIT_MASKED) { |
1464 | dbus_set_error(e, BUS_ERROR_MASKED, "Unit %s is masked.", unit->meta.id); | |
00dc5d76 LP |
1465 | return -EINVAL; |
1466 | } | |
1467 | ||
398ef8ba LP |
1468 | if (!unit_job_is_applicable(unit, type)) { |
1469 | dbus_set_error(e, BUS_ERROR_JOB_TYPE_NOT_APPLICABLE, "Job type %s is not applicable for unit %s.", job_type_to_string(type), unit->meta.id); | |
cd2dbd7d | 1470 | return -EBADR; |
398ef8ba | 1471 | } |
cd2dbd7d | 1472 | |
e5b5ae50 | 1473 | /* First add the job. */ |
9e2f7c11 | 1474 | if (!(ret = transaction_add_one_job(m, type, unit, override, &is_new))) |
e5b5ae50 LP |
1475 | return -ENOMEM; |
1476 | ||
cebe0d41 | 1477 | ret->ignore_order = ret->ignore_order || ignore_order; |
e67c3609 | 1478 | |
e5b5ae50 | 1479 | /* Then, add a link to the job. */ |
69dd2852 | 1480 | if (!job_dependency_new(by, ret, matters, conflicts)) |
e5b5ae50 LP |
1481 | return -ENOMEM; |
1482 | ||
cebe0d41 | 1483 | if (is_new && !ignore_requirements) { |
6210e7fc LP |
1484 | Set *following; |
1485 | ||
1486 | /* If we are following some other unit, make sure we | |
1487 | * add all dependencies of everybody following. */ | |
1488 | if (unit_following_set(ret->unit, &following) > 0) { | |
1489 | SET_FOREACH(dep, following, i) | |
cebe0d41 | 1490 | if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) { |
6210e7fc LP |
1491 | log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r)); |
1492 | ||
1493 | if (e) | |
1494 | dbus_error_free(e); | |
1495 | } | |
1496 | ||
1497 | set_free(following); | |
1498 | } | |
1499 | ||
e5b5ae50 LP |
1500 | /* Finally, recursively add in all dependencies. */ |
1501 | if (type == JOB_START || type == JOB_RELOAD_OR_START) { | |
87f0e418 | 1502 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES], i) |
cebe0d41 | 1503 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) { |
41242c42 LP |
1504 | if (r != -EBADR) |
1505 | goto fail; | |
1506 | ||
1507 | if (e) | |
1508 | dbus_error_free(e); | |
1509 | } | |
9e2f7c11 | 1510 | |
b81884e7 | 1511 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BIND_TO], i) |
cebe0d41 | 1512 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) { |
41242c42 LP |
1513 | |
1514 | if (r != -EBADR) | |
1515 | goto fail; | |
1516 | ||
1517 | if (e) | |
1518 | dbus_error_free(e); | |
1519 | } | |
b81884e7 | 1520 | |
9e2f7c11 | 1521 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRES_OVERRIDABLE], i) |
cebe0d41 | 1522 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) { |
65e92d67 | 1523 | log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r)); |
60dc72b5 LP |
1524 | |
1525 | if (e) | |
1526 | dbus_error_free(e); | |
65e92d67 | 1527 | } |
9e2f7c11 | 1528 | |
87f0e418 | 1529 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_WANTS], i) |
cebe0d41 | 1530 | if ((r = transaction_add_job_and_dependencies(m, JOB_START, dep, ret, false, false, false, false, ignore_order, e, NULL)) < 0) { |
65e92d67 | 1531 | log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r)); |
60dc72b5 LP |
1532 | |
1533 | if (e) | |
1534 | dbus_error_free(e); | |
65e92d67 | 1535 | } |
9e2f7c11 | 1536 | |
87f0e418 | 1537 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE], i) |
cebe0d41 | 1538 | if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) { |
41242c42 LP |
1539 | |
1540 | if (r != -EBADR) | |
1541 | goto fail; | |
1542 | ||
1543 | if (e) | |
1544 | dbus_error_free(e); | |
1545 | } | |
9e2f7c11 LP |
1546 | |
1547 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUISITE_OVERRIDABLE], i) | |
cebe0d41 | 1548 | if ((r = transaction_add_job_and_dependencies(m, JOB_VERIFY_ACTIVE, dep, ret, !override, override, false, false, ignore_order, e, NULL)) < 0) { |
65e92d67 | 1549 | log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r)); |
60dc72b5 LP |
1550 | |
1551 | if (e) | |
1552 | dbus_error_free(e); | |
65e92d67 | 1553 | } |
9e2f7c11 | 1554 | |
87f0e418 | 1555 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTS], i) |
cebe0d41 | 1556 | if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, true, override, true, false, ignore_order, e, NULL)) < 0) { |
41242c42 LP |
1557 | |
1558 | if (r != -EBADR) | |
1559 | goto fail; | |
1560 | ||
1561 | if (e) | |
1562 | dbus_error_free(e); | |
1563 | } | |
69dd2852 LP |
1564 | |
1565 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_CONFLICTED_BY], i) | |
cebe0d41 | 1566 | if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, dep, ret, false, override, false, false, ignore_order, e, NULL)) < 0) { |
41242c42 LP |
1567 | log_warning("Cannot add dependency job for unit %s, ignoring: %s", dep->meta.id, bus_error(e, r)); |
1568 | ||
1569 | if (e) | |
1570 | dbus_error_free(e); | |
1571 | } | |
e5b5ae50 LP |
1572 | |
1573 | } else if (type == JOB_STOP || type == JOB_RESTART || type == JOB_TRY_RESTART) { | |
1574 | ||
87f0e418 | 1575 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_REQUIRED_BY], i) |
cebe0d41 | 1576 | if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) { |
41242c42 LP |
1577 | |
1578 | if (r != -EBADR) | |
1579 | goto fail; | |
1580 | ||
1581 | if (e) | |
1582 | dbus_error_free(e); | |
1583 | } | |
b81884e7 LP |
1584 | |
1585 | SET_FOREACH(dep, ret->unit->meta.dependencies[UNIT_BOUND_BY], i) | |
cebe0d41 | 1586 | if ((r = transaction_add_job_and_dependencies(m, type, dep, ret, true, override, false, false, ignore_order, e, NULL)) < 0) { |
41242c42 LP |
1587 | |
1588 | if (r != -EBADR) | |
1589 | goto fail; | |
1590 | ||
1591 | if (e) | |
1592 | dbus_error_free(e); | |
1593 | } | |
e5b5ae50 LP |
1594 | } |
1595 | ||
1596 | /* JOB_VERIFY_STARTED, JOB_RELOAD require no dependency handling */ | |
1597 | } | |
60918275 | 1598 | |
c0dafa48 LP |
1599 | if (_ret) |
1600 | *_ret = ret; | |
1601 | ||
60918275 LP |
1602 | return 0; |
1603 | ||
1604 | fail: | |
e5b5ae50 LP |
1605 | return r; |
1606 | } | |
1607 | ||
c497c7a9 LP |
1608 | static int transaction_add_isolate_jobs(Manager *m) { |
1609 | Iterator i; | |
1610 | Unit *u; | |
1611 | char *k; | |
1612 | int r; | |
1613 | ||
1614 | assert(m); | |
1615 | ||
1616 | HASHMAP_FOREACH_KEY(u, k, m->units, i) { | |
1617 | ||
1618 | /* ignore aliases */ | |
1619 | if (u->meta.id != k) | |
1620 | continue; | |
1621 | ||
1622 | if (UNIT_VTABLE(u)->no_isolate) | |
1623 | continue; | |
1624 | ||
1625 | /* No need to stop inactive jobs */ | |
2ce5c8f9 | 1626 | if (UNIT_IS_INACTIVE_OR_FAILED(unit_active_state(u)) && !u->meta.job) |
c497c7a9 LP |
1627 | continue; |
1628 | ||
1629 | /* Is there already something listed for this? */ | |
1630 | if (hashmap_get(m->transaction_jobs, u)) | |
1631 | continue; | |
1632 | ||
cebe0d41 | 1633 | if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, false, false, false, NULL, NULL)) < 0) |
c497c7a9 LP |
1634 | log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r)); |
1635 | } | |
1636 | ||
1637 | return 0; | |
1638 | } | |
1639 | ||
398ef8ba | 1640 | int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, DBusError *e, Job **_ret) { |
e5b5ae50 LP |
1641 | int r; |
1642 | Job *ret; | |
1643 | ||
1644 | assert(m); | |
1645 | assert(type < _JOB_TYPE_MAX); | |
87f0e418 | 1646 | assert(unit); |
e5b5ae50 | 1647 | assert(mode < _JOB_MODE_MAX); |
60918275 | 1648 | |
398ef8ba LP |
1649 | if (mode == JOB_ISOLATE && type != JOB_START) { |
1650 | dbus_set_error(e, BUS_ERROR_INVALID_JOB_MODE, "Isolate is only valid for start."); | |
c497c7a9 | 1651 | return -EINVAL; |
398ef8ba | 1652 | } |
c497c7a9 | 1653 | |
2528a7a6 LP |
1654 | if (mode == JOB_ISOLATE && !unit->meta.allow_isolate) { |
1655 | dbus_set_error(e, BUS_ERROR_NO_ISOLATION, "Operation refused, unit may not be isolated."); | |
1656 | return -EPERM; | |
1657 | } | |
1658 | ||
e43ac878 | 1659 | log_debug("Trying to enqueue job %s/%s/%s", unit->meta.id, job_type_to_string(type), job_mode_to_string(mode)); |
9f04bd52 | 1660 | |
cebe0d41 LP |
1661 | if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, false, |
1662 | mode == JOB_IGNORE_DEPENDENCIES || mode == JOB_IGNORE_REQUIREMENTS, | |
1663 | mode == JOB_IGNORE_DEPENDENCIES, e, &ret)) < 0) { | |
11dd41ce | 1664 | transaction_abort(m); |
e5b5ae50 LP |
1665 | return r; |
1666 | } | |
11dd41ce | 1667 | |
c497c7a9 LP |
1668 | if (mode == JOB_ISOLATE) |
1669 | if ((r = transaction_add_isolate_jobs(m)) < 0) { | |
1670 | transaction_abort(m); | |
1671 | return r; | |
1672 | } | |
1673 | ||
398ef8ba | 1674 | if ((r = transaction_activate(m, mode, e)) < 0) |
e5b5ae50 LP |
1675 | return r; |
1676 | ||
9e2f7c11 | 1677 | log_debug("Enqueued job %s/%s as %u", unit->meta.id, job_type_to_string(type), (unsigned) ret->id); |
f50e0a01 | 1678 | |
e5b5ae50 LP |
1679 | if (_ret) |
1680 | *_ret = ret; | |
60918275 | 1681 | |
e5b5ae50 LP |
1682 | return 0; |
1683 | } | |
60918275 | 1684 | |
398ef8ba | 1685 | int manager_add_job_by_name(Manager *m, JobType type, const char *name, JobMode mode, bool override, DBusError *e, Job **_ret) { |
28247076 LP |
1686 | Unit *unit; |
1687 | int r; | |
1688 | ||
1689 | assert(m); | |
1690 | assert(type < _JOB_TYPE_MAX); | |
1691 | assert(name); | |
1692 | assert(mode < _JOB_MODE_MAX); | |
1693 | ||
398ef8ba | 1694 | if ((r = manager_load_unit(m, name, NULL, NULL, &unit)) < 0) |
28247076 LP |
1695 | return r; |
1696 | ||
398ef8ba | 1697 | return manager_add_job(m, type, unit, mode, override, e, _ret); |
28247076 LP |
1698 | } |
1699 | ||
60918275 LP |
1700 | Job *manager_get_job(Manager *m, uint32_t id) { |
1701 | assert(m); | |
1702 | ||
1703 | return hashmap_get(m->jobs, UINT32_TO_PTR(id)); | |
1704 | } | |
1705 | ||
87f0e418 | 1706 | Unit *manager_get_unit(Manager *m, const char *name) { |
60918275 LP |
1707 | assert(m); |
1708 | assert(name); | |
1709 | ||
87f0e418 | 1710 | return hashmap_get(m->units, name); |
60918275 LP |
1711 | } |
1712 | ||
c1e1601e | 1713 | unsigned manager_dispatch_load_queue(Manager *m) { |
60918275 | 1714 | Meta *meta; |
c1e1601e | 1715 | unsigned n = 0; |
60918275 LP |
1716 | |
1717 | assert(m); | |
1718 | ||
223dabab LP |
1719 | /* Make sure we are not run recursively */ |
1720 | if (m->dispatching_load_queue) | |
c1e1601e | 1721 | return 0; |
223dabab LP |
1722 | |
1723 | m->dispatching_load_queue = true; | |
1724 | ||
87f0e418 | 1725 | /* Dispatches the load queue. Takes a unit from the queue and |
60918275 LP |
1726 | * tries to load its data until the queue is empty */ |
1727 | ||
1728 | while ((meta = m->load_queue)) { | |
034c6ed7 LP |
1729 | assert(meta->in_load_queue); |
1730 | ||
399ab2b1 | 1731 | unit_load((Unit*) meta); |
c1e1601e | 1732 | n++; |
60918275 LP |
1733 | } |
1734 | ||
223dabab | 1735 | m->dispatching_load_queue = false; |
c1e1601e | 1736 | return n; |
60918275 LP |
1737 | } |
1738 | ||
398ef8ba | 1739 | int manager_load_unit_prepare(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) { |
87f0e418 | 1740 | Unit *ret; |
60918275 LP |
1741 | int r; |
1742 | ||
1743 | assert(m); | |
9e2f7c11 | 1744 | assert(name || path); |
60918275 | 1745 | |
db06e3b6 LP |
1746 | /* This will prepare the unit for loading, but not actually |
1747 | * load anything from disk. */ | |
0301abf4 | 1748 | |
398ef8ba LP |
1749 | if (path && !is_path(path)) { |
1750 | dbus_set_error(e, BUS_ERROR_INVALID_PATH, "Path %s is not absolute.", path); | |
9e2f7c11 | 1751 | return -EINVAL; |
398ef8ba | 1752 | } |
9e2f7c11 LP |
1753 | |
1754 | if (!name) | |
1755 | name = file_name_from_path(path); | |
1756 | ||
b9c0d441 | 1757 | if (!unit_name_is_valid(name, false)) { |
398ef8ba | 1758 | dbus_set_error(e, BUS_ERROR_INVALID_NAME, "Unit name %s is not valid.", name); |
9e2f7c11 | 1759 | return -EINVAL; |
398ef8ba | 1760 | } |
60918275 | 1761 | |
87f0e418 | 1762 | if ((ret = manager_get_unit(m, name))) { |
034c6ed7 | 1763 | *_ret = ret; |
413d6313 | 1764 | return 1; |
034c6ed7 | 1765 | } |
60918275 | 1766 | |
87f0e418 | 1767 | if (!(ret = unit_new(m))) |
60918275 LP |
1768 | return -ENOMEM; |
1769 | ||
9e2f7c11 | 1770 | if (path) |
6be1e7d5 | 1771 | if (!(ret->meta.fragment_path = strdup(path))) { |
0301abf4 LP |
1772 | unit_free(ret); |
1773 | return -ENOMEM; | |
1774 | } | |
0301abf4 | 1775 | |
87f0e418 LP |
1776 | if ((r = unit_add_name(ret, name)) < 0) { |
1777 | unit_free(ret); | |
1ffba6fe | 1778 | return r; |
60918275 LP |
1779 | } |
1780 | ||
87f0e418 | 1781 | unit_add_to_load_queue(ret); |
c1e1601e | 1782 | unit_add_to_dbus_queue(ret); |
949061f0 | 1783 | unit_add_to_gc_queue(ret); |
c1e1601e | 1784 | |
db06e3b6 LP |
1785 | if (_ret) |
1786 | *_ret = ret; | |
1787 | ||
1788 | return 0; | |
1789 | } | |
1790 | ||
398ef8ba | 1791 | int manager_load_unit(Manager *m, const char *name, const char *path, DBusError *e, Unit **_ret) { |
db06e3b6 LP |
1792 | int r; |
1793 | ||
1794 | assert(m); | |
1795 | ||
1796 | /* This will load the service information files, but not actually | |
1797 | * start any services or anything. */ | |
1798 | ||
398ef8ba | 1799 | if ((r = manager_load_unit_prepare(m, name, path, e, _ret)) != 0) |
db06e3b6 LP |
1800 | return r; |
1801 | ||
f50e0a01 | 1802 | manager_dispatch_load_queue(m); |
60918275 | 1803 | |
9e2f7c11 | 1804 | if (_ret) |
413d6313 | 1805 | *_ret = unit_follow_merge(*_ret); |
9e2f7c11 | 1806 | |
60918275 LP |
1807 | return 0; |
1808 | } | |
a66d02c3 | 1809 | |
cea8e32e | 1810 | void manager_dump_jobs(Manager *s, FILE *f, const char *prefix) { |
034c6ed7 | 1811 | Iterator i; |
a66d02c3 LP |
1812 | Job *j; |
1813 | ||
1814 | assert(s); | |
1815 | assert(f); | |
1816 | ||
034c6ed7 | 1817 | HASHMAP_FOREACH(j, s->jobs, i) |
cea8e32e | 1818 | job_dump(j, f, prefix); |
a66d02c3 LP |
1819 | } |
1820 | ||
87f0e418 | 1821 | void manager_dump_units(Manager *s, FILE *f, const char *prefix) { |
034c6ed7 | 1822 | Iterator i; |
87f0e418 | 1823 | Unit *u; |
11dd41ce | 1824 | const char *t; |
a66d02c3 LP |
1825 | |
1826 | assert(s); | |
1827 | assert(f); | |
1828 | ||
87f0e418 | 1829 | HASHMAP_FOREACH_KEY(u, t, s->units, i) |
9e2f7c11 | 1830 | if (u->meta.id == t) |
87f0e418 | 1831 | unit_dump(u, f, prefix); |
a66d02c3 | 1832 | } |
7fad411c LP |
1833 | |
1834 | void manager_clear_jobs(Manager *m) { | |
1835 | Job *j; | |
1836 | ||
1837 | assert(m); | |
1838 | ||
1839 | transaction_abort(m); | |
1840 | ||
1841 | while ((j = hashmap_first(m->jobs))) | |
c77bc38d | 1842 | job_finish_and_invalidate(j, JOB_CANCELED); |
7fad411c | 1843 | } |
83c60c9f | 1844 | |
c1e1601e | 1845 | unsigned manager_dispatch_run_queue(Manager *m) { |
83c60c9f | 1846 | Job *j; |
c1e1601e | 1847 | unsigned n = 0; |
83c60c9f | 1848 | |
034c6ed7 | 1849 | if (m->dispatching_run_queue) |
c1e1601e | 1850 | return 0; |
034c6ed7 LP |
1851 | |
1852 | m->dispatching_run_queue = true; | |
9152c765 | 1853 | |
034c6ed7 | 1854 | while ((j = m->run_queue)) { |
ac1135be | 1855 | assert(j->installed); |
034c6ed7 LP |
1856 | assert(j->in_run_queue); |
1857 | ||
1858 | job_run_and_invalidate(j); | |
c1e1601e | 1859 | n++; |
9152c765 | 1860 | } |
034c6ed7 LP |
1861 | |
1862 | m->dispatching_run_queue = false; | |
c1e1601e LP |
1863 | return n; |
1864 | } | |
1865 | ||
1866 | unsigned manager_dispatch_dbus_queue(Manager *m) { | |
1867 | Job *j; | |
1868 | Meta *meta; | |
1869 | unsigned n = 0; | |
1870 | ||
1871 | assert(m); | |
1872 | ||
1873 | if (m->dispatching_dbus_queue) | |
1874 | return 0; | |
1875 | ||
1876 | m->dispatching_dbus_queue = true; | |
1877 | ||
1878 | while ((meta = m->dbus_unit_queue)) { | |
23a177ef | 1879 | assert(meta->in_dbus_queue); |
c1e1601e | 1880 | |
399ab2b1 | 1881 | bus_unit_send_change_signal((Unit*) meta); |
c1e1601e LP |
1882 | n++; |
1883 | } | |
1884 | ||
1885 | while ((j = m->dbus_job_queue)) { | |
1886 | assert(j->in_dbus_queue); | |
1887 | ||
1888 | bus_job_send_change_signal(j); | |
1889 | n++; | |
1890 | } | |
1891 | ||
1892 | m->dispatching_dbus_queue = false; | |
1893 | return n; | |
9152c765 LP |
1894 | } |
1895 | ||
8c47c732 LP |
1896 | static int manager_process_notify_fd(Manager *m) { |
1897 | ssize_t n; | |
1898 | ||
1899 | assert(m); | |
1900 | ||
1901 | for (;;) { | |
1902 | char buf[4096]; | |
1903 | struct msghdr msghdr; | |
1904 | struct iovec iovec; | |
1905 | struct ucred *ucred; | |
1906 | union { | |
1907 | struct cmsghdr cmsghdr; | |
1908 | uint8_t buf[CMSG_SPACE(sizeof(struct ucred))]; | |
1909 | } control; | |
1910 | Unit *u; | |
1911 | char **tags; | |
1912 | ||
1913 | zero(iovec); | |
1914 | iovec.iov_base = buf; | |
1915 | iovec.iov_len = sizeof(buf)-1; | |
1916 | ||
1917 | zero(control); | |
1918 | zero(msghdr); | |
1919 | msghdr.msg_iov = &iovec; | |
1920 | msghdr.msg_iovlen = 1; | |
1921 | msghdr.msg_control = &control; | |
1922 | msghdr.msg_controllen = sizeof(control); | |
1923 | ||
1924 | if ((n = recvmsg(m->notify_watch.fd, &msghdr, MSG_DONTWAIT)) <= 0) { | |
1925 | if (n >= 0) | |
1926 | return -EIO; | |
1927 | ||
f6144808 | 1928 | if (errno == EAGAIN || errno == EINTR) |
8c47c732 LP |
1929 | break; |
1930 | ||
1931 | return -errno; | |
1932 | } | |
1933 | ||
1934 | if (msghdr.msg_controllen < CMSG_LEN(sizeof(struct ucred)) || | |
1935 | control.cmsghdr.cmsg_level != SOL_SOCKET || | |
1936 | control.cmsghdr.cmsg_type != SCM_CREDENTIALS || | |
1937 | control.cmsghdr.cmsg_len != CMSG_LEN(sizeof(struct ucred))) { | |
1938 | log_warning("Received notify message without credentials. Ignoring."); | |
1939 | continue; | |
1940 | } | |
1941 | ||
1942 | ucred = (struct ucred*) CMSG_DATA(&control.cmsghdr); | |
1943 | ||
c6c18be3 | 1944 | if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(ucred->pid)))) |
8c47c732 LP |
1945 | if (!(u = cgroup_unit_by_pid(m, ucred->pid))) { |
1946 | log_warning("Cannot find unit for notify message of PID %lu.", (unsigned long) ucred->pid); | |
1947 | continue; | |
1948 | } | |
1949 | ||
8c40acf7 LP |
1950 | assert((size_t) n < sizeof(buf)); |
1951 | buf[n] = 0; | |
8c47c732 LP |
1952 | if (!(tags = strv_split(buf, "\n\r"))) |
1953 | return -ENOMEM; | |
1954 | ||
1955 | log_debug("Got notification message for unit %s", u->meta.id); | |
1956 | ||
1957 | if (UNIT_VTABLE(u)->notify_message) | |
c952c6ec | 1958 | UNIT_VTABLE(u)->notify_message(u, ucred->pid, tags); |
8c47c732 LP |
1959 | |
1960 | strv_free(tags); | |
1961 | } | |
1962 | ||
1963 | return 0; | |
1964 | } | |
1965 | ||
034c6ed7 | 1966 | static int manager_dispatch_sigchld(Manager *m) { |
9152c765 LP |
1967 | assert(m); |
1968 | ||
1969 | for (;;) { | |
1970 | siginfo_t si; | |
87f0e418 | 1971 | Unit *u; |
8c47c732 | 1972 | int r; |
9152c765 LP |
1973 | |
1974 | zero(si); | |
4112df16 LP |
1975 | |
1976 | /* First we call waitd() for a PID and do not reap the | |
1977 | * zombie. That way we can still access /proc/$PID for | |
1978 | * it while it is a zombie. */ | |
1979 | if (waitid(P_ALL, 0, &si, WEXITED|WNOHANG|WNOWAIT) < 0) { | |
acbb0225 LP |
1980 | |
1981 | if (errno == ECHILD) | |
1982 | break; | |
1983 | ||
4112df16 LP |
1984 | if (errno == EINTR) |
1985 | continue; | |
1986 | ||
9152c765 | 1987 | return -errno; |
acbb0225 | 1988 | } |
9152c765 | 1989 | |
4112df16 | 1990 | if (si.si_pid <= 0) |
9152c765 LP |
1991 | break; |
1992 | ||
15d5d9d9 | 1993 | if (si.si_code == CLD_EXITED || si.si_code == CLD_KILLED || si.si_code == CLD_DUMPED) { |
4112df16 LP |
1994 | char *name = NULL; |
1995 | ||
1996 | get_process_name(si.si_pid, &name); | |
bb00e604 | 1997 | log_debug("Got SIGCHLD for process %lu (%s)", (unsigned long) si.si_pid, strna(name)); |
4112df16 LP |
1998 | free(name); |
1999 | } | |
2000 | ||
8c47c732 LP |
2001 | /* Let's flush any message the dying child might still |
2002 | * have queued for us. This ensures that the process | |
2003 | * still exists in /proc so that we can figure out | |
2004 | * which cgroup and hence unit it belongs to. */ | |
2005 | if ((r = manager_process_notify_fd(m)) < 0) | |
2006 | return r; | |
2007 | ||
2008 | /* And now figure out the unit this belongs to */ | |
c6c18be3 | 2009 | if (!(u = hashmap_get(m->watch_pids, LONG_TO_PTR(si.si_pid)))) |
8c47c732 LP |
2010 | u = cgroup_unit_by_pid(m, si.si_pid); |
2011 | ||
4112df16 LP |
2012 | /* And now, we actually reap the zombie. */ |
2013 | if (waitid(P_PID, si.si_pid, &si, WEXITED) < 0) { | |
2014 | if (errno == EINTR) | |
2015 | continue; | |
2016 | ||
2017 | return -errno; | |
2018 | } | |
2019 | ||
034c6ed7 LP |
2020 | if (si.si_code != CLD_EXITED && si.si_code != CLD_KILLED && si.si_code != CLD_DUMPED) |
2021 | continue; | |
2022 | ||
bb00e604 LP |
2023 | log_debug("Child %lu died (code=%s, status=%i/%s)", |
2024 | (long unsigned) si.si_pid, | |
4112df16 LP |
2025 | sigchld_code_to_string(si.si_code), |
2026 | si.si_status, | |
d06dacd0 LP |
2027 | strna(si.si_code == CLD_EXITED |
2028 | ? exit_status_to_string(si.si_status, EXIT_STATUS_FULL) | |
2029 | : signal_to_string(si.si_status))); | |
acbb0225 | 2030 | |
8c47c732 | 2031 | if (!u) |
9152c765 LP |
2032 | continue; |
2033 | ||
c6c18be3 | 2034 | log_debug("Child %lu belongs to %s", (long unsigned) si.si_pid, u->meta.id); |
6c1a0478 | 2035 | |
c6c18be3 | 2036 | hashmap_remove(m->watch_pids, LONG_TO_PTR(si.si_pid)); |
87f0e418 | 2037 | UNIT_VTABLE(u)->sigchld_event(u, si.si_pid, si.si_code, si.si_status); |
9152c765 LP |
2038 | } |
2039 | ||
2040 | return 0; | |
2041 | } | |
2042 | ||
7d793605 | 2043 | static int manager_start_target(Manager *m, const char *name, JobMode mode) { |
28247076 | 2044 | int r; |
398ef8ba LP |
2045 | DBusError error; |
2046 | ||
2047 | dbus_error_init(&error); | |
2048 | ||
af2d49f7 | 2049 | log_debug("Activating special unit %s", name); |
1e001f52 | 2050 | |
398ef8ba LP |
2051 | if ((r = manager_add_job_by_name(m, JOB_START, name, mode, true, &error, NULL)) < 0) |
2052 | log_error("Failed to enqueue %s job: %s", name, bus_error(&error, r)); | |
28247076 | 2053 | |
398ef8ba | 2054 | dbus_error_free(&error); |
a1b256b0 LP |
2055 | |
2056 | return r; | |
28247076 LP |
2057 | } |
2058 | ||
a16e1123 | 2059 | static int manager_process_signal_fd(Manager *m) { |
9152c765 LP |
2060 | ssize_t n; |
2061 | struct signalfd_siginfo sfsi; | |
2062 | bool sigchld = false; | |
2063 | ||
2064 | assert(m); | |
2065 | ||
2066 | for (;;) { | |
acbb0225 | 2067 | if ((n = read(m->signal_watch.fd, &sfsi, sizeof(sfsi))) != sizeof(sfsi)) { |
9152c765 LP |
2068 | |
2069 | if (n >= 0) | |
2070 | return -EIO; | |
2071 | ||
63090775 | 2072 | if (errno == EINTR || errno == EAGAIN) |
acbb0225 | 2073 | break; |
9152c765 LP |
2074 | |
2075 | return -errno; | |
2076 | } | |
2077 | ||
67370238 LP |
2078 | if (sfsi.ssi_pid > 0) { |
2079 | char *p = NULL; | |
2080 | ||
dfa7f7e1 LP |
2081 | get_process_name(sfsi.ssi_pid, &p); |
2082 | ||
67370238 LP |
2083 | log_debug("Received SIG%s from PID %lu (%s).", |
2084 | strna(signal_to_string(sfsi.ssi_signo)), | |
2085 | (unsigned long) sfsi.ssi_pid, strna(p)); | |
2086 | free(p); | |
2087 | } else | |
2088 | log_debug("Received SIG%s.", strna(signal_to_string(sfsi.ssi_signo))); | |
1e001f52 | 2089 | |
b9cd2ec1 LP |
2090 | switch (sfsi.ssi_signo) { |
2091 | ||
4112df16 | 2092 | case SIGCHLD: |
9152c765 | 2093 | sigchld = true; |
b9cd2ec1 LP |
2094 | break; |
2095 | ||
6632c602 | 2096 | case SIGTERM: |
a3d4e06d | 2097 | if (m->running_as == MANAGER_SYSTEM) { |
db06e3b6 LP |
2098 | /* This is for compatibility with the |
2099 | * original sysvinit */ | |
e11dc4a2 | 2100 | m->exit_code = MANAGER_REEXECUTE; |
a1b256b0 LP |
2101 | break; |
2102 | } | |
84e9af1e | 2103 | |
a1b256b0 | 2104 | /* Fall through */ |
e11dc4a2 LP |
2105 | |
2106 | case SIGINT: | |
a3d4e06d | 2107 | if (m->running_as == MANAGER_SYSTEM) { |
7d793605 | 2108 | manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET, JOB_REPLACE); |
84e9af1e LP |
2109 | break; |
2110 | } | |
2111 | ||
a1b256b0 | 2112 | /* Run the exit target if there is one, if not, just exit. */ |
0003d1ab | 2113 | if (manager_start_target(m, SPECIAL_EXIT_TARGET, JOB_REPLACE) < 0) { |
a1b256b0 LP |
2114 | m->exit_code = MANAGER_EXIT; |
2115 | return 0; | |
2116 | } | |
2117 | ||
2118 | break; | |
84e9af1e | 2119 | |
28247076 | 2120 | case SIGWINCH: |
a3d4e06d | 2121 | if (m->running_as == MANAGER_SYSTEM) |
7d793605 | 2122 | manager_start_target(m, SPECIAL_KBREQUEST_TARGET, JOB_REPLACE); |
84e9af1e | 2123 | |
28247076 LP |
2124 | /* This is a nop on non-init */ |
2125 | break; | |
84e9af1e | 2126 | |
28247076 | 2127 | case SIGPWR: |
a3d4e06d | 2128 | if (m->running_as == MANAGER_SYSTEM) |
7d793605 | 2129 | manager_start_target(m, SPECIAL_SIGPWR_TARGET, JOB_REPLACE); |
84e9af1e | 2130 | |
28247076 | 2131 | /* This is a nop on non-init */ |
84e9af1e | 2132 | break; |
6632c602 | 2133 | |
1005d14f | 2134 | case SIGUSR1: { |
57ee42ce LP |
2135 | Unit *u; |
2136 | ||
2137 | u = manager_get_unit(m, SPECIAL_DBUS_SERVICE); | |
2138 | ||
2139 | if (!u || UNIT_IS_ACTIVE_OR_RELOADING(unit_active_state(u))) { | |
2140 | log_info("Trying to reconnect to bus..."); | |
3996fbe2 | 2141 | bus_init(m, true); |
57ee42ce LP |
2142 | } |
2143 | ||
2144 | if (!u || !UNIT_IS_ACTIVE_OR_ACTIVATING(unit_active_state(u))) { | |
2145 | log_info("Loading D-Bus service..."); | |
7d793605 | 2146 | manager_start_target(m, SPECIAL_DBUS_SERVICE, JOB_REPLACE); |
57ee42ce LP |
2147 | } |
2148 | ||
2149 | break; | |
2150 | } | |
2151 | ||
2149e37c LP |
2152 | case SIGUSR2: { |
2153 | FILE *f; | |
2154 | char *dump = NULL; | |
2155 | size_t size; | |
2156 | ||
2157 | if (!(f = open_memstream(&dump, &size))) { | |
2158 | log_warning("Failed to allocate memory stream."); | |
2159 | break; | |
2160 | } | |
2161 | ||
2162 | manager_dump_units(m, f, "\t"); | |
2163 | manager_dump_jobs(m, f, "\t"); | |
2164 | ||
2165 | if (ferror(f)) { | |
2166 | fclose(f); | |
2167 | free(dump); | |
2168 | log_warning("Failed to write status stream"); | |
2169 | break; | |
2170 | } | |
2171 | ||
2172 | fclose(f); | |
2173 | log_dump(LOG_INFO, dump); | |
2174 | free(dump); | |
2175 | ||
1005d14f | 2176 | break; |
2149e37c | 2177 | } |
1005d14f | 2178 | |
a16e1123 LP |
2179 | case SIGHUP: |
2180 | m->exit_code = MANAGER_RELOAD; | |
2181 | break; | |
2182 | ||
7d793605 | 2183 | default: { |
0003d1ab LP |
2184 | /* Starting SIGRTMIN+0 */ |
2185 | static const char * const target_table[] = { | |
7d793605 LP |
2186 | [0] = SPECIAL_DEFAULT_TARGET, |
2187 | [1] = SPECIAL_RESCUE_TARGET, | |
f057408c | 2188 | [2] = SPECIAL_EMERGENCY_TARGET, |
7d793605 LP |
2189 | [3] = SPECIAL_HALT_TARGET, |
2190 | [4] = SPECIAL_POWEROFF_TARGET, | |
0003d1ab LP |
2191 | [5] = SPECIAL_REBOOT_TARGET, |
2192 | [6] = SPECIAL_KEXEC_TARGET | |
2193 | }; | |
2194 | ||
2195 | /* Starting SIGRTMIN+13, so that target halt and system halt are 10 apart */ | |
2196 | static const ManagerExitCode code_table[] = { | |
2197 | [0] = MANAGER_HALT, | |
2198 | [1] = MANAGER_POWEROFF, | |
2199 | [2] = MANAGER_REBOOT, | |
2200 | [3] = MANAGER_KEXEC | |
7d793605 LP |
2201 | }; |
2202 | ||
2203 | if ((int) sfsi.ssi_signo >= SIGRTMIN+0 && | |
0003d1ab LP |
2204 | (int) sfsi.ssi_signo < SIGRTMIN+(int) ELEMENTSOF(target_table)) { |
2205 | manager_start_target(m, target_table[sfsi.ssi_signo - SIGRTMIN], | |
e6b3f00f | 2206 | (sfsi.ssi_signo == 1 || sfsi.ssi_signo == 2) ? JOB_ISOLATE : JOB_REPLACE); |
7d793605 LP |
2207 | break; |
2208 | } | |
2209 | ||
0003d1ab LP |
2210 | if ((int) sfsi.ssi_signo >= SIGRTMIN+13 && |
2211 | (int) sfsi.ssi_signo < SIGRTMIN+13+(int) ELEMENTSOF(code_table)) { | |
2212 | m->exit_code = code_table[sfsi.ssi_signo - SIGRTMIN - 13]; | |
2213 | break; | |
2214 | } | |
2215 | ||
0658666b LP |
2216 | switch (sfsi.ssi_signo - SIGRTMIN) { |
2217 | ||
2218 | case 20: | |
2219 | log_debug("Enabling showing of status."); | |
2220 | m->show_status = true; | |
2221 | break; | |
2222 | ||
2223 | case 21: | |
2224 | log_debug("Disabling showing of status."); | |
2225 | m->show_status = false; | |
2226 | break; | |
2227 | ||
2228 | default: | |
2229 | log_warning("Got unhandled signal <%s>.", strna(signal_to_string(sfsi.ssi_signo))); | |
2230 | } | |
b9cd2ec1 | 2231 | } |
7d793605 | 2232 | } |
9152c765 LP |
2233 | } |
2234 | ||
2235 | if (sigchld) | |
034c6ed7 LP |
2236 | return manager_dispatch_sigchld(m); |
2237 | ||
2238 | return 0; | |
2239 | } | |
2240 | ||
a16e1123 | 2241 | static int process_event(Manager *m, struct epoll_event *ev) { |
034c6ed7 | 2242 | int r; |
acbb0225 | 2243 | Watch *w; |
034c6ed7 LP |
2244 | |
2245 | assert(m); | |
2246 | assert(ev); | |
2247 | ||
acbb0225 | 2248 | assert(w = ev->data.ptr); |
034c6ed7 | 2249 | |
40dde66f LP |
2250 | if (w->type == WATCH_INVALID) |
2251 | return 0; | |
2252 | ||
acbb0225 | 2253 | switch (w->type) { |
034c6ed7 | 2254 | |
ef734fd6 | 2255 | case WATCH_SIGNAL: |
034c6ed7 | 2256 | |
acbb0225 | 2257 | /* An incoming signal? */ |
f94ea366 | 2258 | if (ev->events != EPOLLIN) |
acbb0225 | 2259 | return -EINVAL; |
034c6ed7 | 2260 | |
a16e1123 | 2261 | if ((r = manager_process_signal_fd(m)) < 0) |
acbb0225 | 2262 | return r; |
034c6ed7 | 2263 | |
acbb0225 | 2264 | break; |
034c6ed7 | 2265 | |
8c47c732 LP |
2266 | case WATCH_NOTIFY: |
2267 | ||
2268 | /* An incoming daemon notification event? */ | |
2269 | if (ev->events != EPOLLIN) | |
2270 | return -EINVAL; | |
2271 | ||
2272 | if ((r = manager_process_notify_fd(m)) < 0) | |
2273 | return r; | |
2274 | ||
2275 | break; | |
2276 | ||
acbb0225 | 2277 | case WATCH_FD: |
034c6ed7 | 2278 | |
acbb0225 | 2279 | /* Some fd event, to be dispatched to the units */ |
ea430986 | 2280 | UNIT_VTABLE(w->data.unit)->fd_event(w->data.unit, w->fd, ev->events, w); |
acbb0225 | 2281 | break; |
034c6ed7 | 2282 | |
faf919f1 LP |
2283 | case WATCH_UNIT_TIMER: |
2284 | case WATCH_JOB_TIMER: { | |
acbb0225 LP |
2285 | uint64_t v; |
2286 | ssize_t k; | |
034c6ed7 | 2287 | |
acbb0225 | 2288 | /* Some timer event, to be dispatched to the units */ |
be888ebb | 2289 | if ((k = read(w->fd, &v, sizeof(v))) != sizeof(v)) { |
034c6ed7 | 2290 | |
acbb0225 LP |
2291 | if (k < 0 && (errno == EINTR || errno == EAGAIN)) |
2292 | break; | |
034c6ed7 | 2293 | |
acbb0225 | 2294 | return k < 0 ? -errno : -EIO; |
034c6ed7 LP |
2295 | } |
2296 | ||
faf919f1 LP |
2297 | if (w->type == WATCH_UNIT_TIMER) |
2298 | UNIT_VTABLE(w->data.unit)->timer_event(w->data.unit, v, w); | |
2299 | else | |
2300 | job_timer_event(w->data.job, v, w); | |
acbb0225 LP |
2301 | break; |
2302 | } | |
2303 | ||
ef734fd6 LP |
2304 | case WATCH_MOUNT: |
2305 | /* Some mount table change, intended for the mount subsystem */ | |
2306 | mount_fd_event(m, ev->events); | |
2307 | break; | |
2308 | ||
4e434314 LP |
2309 | case WATCH_SWAP: |
2310 | /* Some swap table change, intended for the swap subsystem */ | |
2311 | swap_fd_event(m, ev->events); | |
2312 | break; | |
2313 | ||
f94ea366 LP |
2314 | case WATCH_UDEV: |
2315 | /* Some notification from udev, intended for the device subsystem */ | |
2316 | device_fd_event(m, ev->events); | |
2317 | break; | |
2318 | ||
ea430986 LP |
2319 | case WATCH_DBUS_WATCH: |
2320 | bus_watch_event(m, w, ev->events); | |
2321 | break; | |
2322 | ||
2323 | case WATCH_DBUS_TIMEOUT: | |
2324 | bus_timeout_event(m, w, ev->events); | |
2325 | break; | |
2326 | ||
acbb0225 | 2327 | default: |
c5fd1e57 | 2328 | log_error("event type=%i", w->type); |
acbb0225 | 2329 | assert_not_reached("Unknown epoll event type."); |
034c6ed7 | 2330 | } |
9152c765 LP |
2331 | |
2332 | return 0; | |
2333 | } | |
2334 | ||
2335 | int manager_loop(Manager *m) { | |
2336 | int r; | |
9152c765 | 2337 | |
fac9f8df | 2338 | RATELIMIT_DEFINE(rl, 1*USEC_PER_SEC, 50000); |
ea430986 | 2339 | |
9152c765 | 2340 | assert(m); |
a16e1123 | 2341 | m->exit_code = MANAGER_RUNNING; |
9152c765 | 2342 | |
fe51822e LP |
2343 | /* Release the path cache */ |
2344 | set_free_free(m->unit_path_cache); | |
2345 | m->unit_path_cache = NULL; | |
2346 | ||
b0c918b9 LP |
2347 | manager_check_finished(m); |
2348 | ||
a4312405 LP |
2349 | /* There might still be some zombies hanging around from |
2350 | * before we were exec()'ed. Leat's reap them */ | |
2351 | if ((r = manager_dispatch_sigchld(m)) < 0) | |
2352 | return r; | |
2353 | ||
a16e1123 | 2354 | while (m->exit_code == MANAGER_RUNNING) { |
957ca890 LP |
2355 | struct epoll_event event; |
2356 | int n; | |
9152c765 | 2357 | |
ea430986 LP |
2358 | if (!ratelimit_test(&rl)) { |
2359 | /* Yay, something is going seriously wrong, pause a little */ | |
2360 | log_warning("Looping too fast. Throttling execution a little."); | |
2361 | sleep(1); | |
2362 | } | |
2363 | ||
37a8e683 | 2364 | if (manager_dispatch_load_queue(m) > 0) |
23a177ef LP |
2365 | continue; |
2366 | ||
37a8e683 | 2367 | if (manager_dispatch_run_queue(m) > 0) |
701cc384 LP |
2368 | continue; |
2369 | ||
37a8e683 | 2370 | if (bus_dispatch(m) > 0) |
c1e1601e | 2371 | continue; |
034c6ed7 | 2372 | |
37a8e683 | 2373 | if (manager_dispatch_cleanup_queue(m) > 0) |
c1e1601e LP |
2374 | continue; |
2375 | ||
37a8e683 | 2376 | if (manager_dispatch_gc_queue(m) > 0) |
c1e1601e LP |
2377 | continue; |
2378 | ||
2379 | if (manager_dispatch_dbus_queue(m) > 0) | |
ea430986 | 2380 | continue; |
ea430986 | 2381 | |
e04aad61 LP |
2382 | if (swap_dispatch_reload(m) > 0) |
2383 | continue; | |
2384 | ||
957ca890 | 2385 | if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) { |
9152c765 | 2386 | |
6089f4a9 | 2387 | if (errno == EINTR) |
9152c765 LP |
2388 | continue; |
2389 | ||
2390 | return -errno; | |
2391 | } | |
2392 | ||
957ca890 | 2393 | assert(n == 1); |
b9cd2ec1 | 2394 | |
a16e1123 | 2395 | if ((r = process_event(m, &event)) < 0) |
957ca890 | 2396 | return r; |
a16e1123 | 2397 | } |
957ca890 | 2398 | |
a16e1123 | 2399 | return m->exit_code; |
83c60c9f | 2400 | } |
ea430986 LP |
2401 | |
2402 | int manager_get_unit_from_dbus_path(Manager *m, const char *s, Unit **_u) { | |
2403 | char *n; | |
2404 | Unit *u; | |
2405 | ||
2406 | assert(m); | |
2407 | assert(s); | |
2408 | assert(_u); | |
2409 | ||
2410 | if (!startswith(s, "/org/freedesktop/systemd1/unit/")) | |
2411 | return -EINVAL; | |
2412 | ||
2413 | if (!(n = bus_path_unescape(s+31))) | |
2414 | return -ENOMEM; | |
2415 | ||
2416 | u = manager_get_unit(m, n); | |
2417 | free(n); | |
2418 | ||
2419 | if (!u) | |
2420 | return -ENOENT; | |
2421 | ||
2422 | *_u = u; | |
2423 | ||
2424 | return 0; | |
2425 | } | |
86fbf370 LP |
2426 | |
2427 | int manager_get_job_from_dbus_path(Manager *m, const char *s, Job **_j) { | |
2428 | Job *j; | |
2429 | unsigned id; | |
2430 | int r; | |
2431 | ||
2432 | assert(m); | |
2433 | assert(s); | |
2434 | assert(_j); | |
2435 | ||
2436 | if (!startswith(s, "/org/freedesktop/systemd1/job/")) | |
2437 | return -EINVAL; | |
2438 | ||
2439 | if ((r = safe_atou(s + 30, &id)) < 0) | |
2440 | return r; | |
2441 | ||
2442 | if (!(j = manager_get_job(m, id))) | |
2443 | return -ENOENT; | |
2444 | ||
2445 | *_j = j; | |
2446 | ||
2447 | return 0; | |
2448 | } | |
dfcd764e | 2449 | |
4927fcae | 2450 | void manager_send_unit_audit(Manager *m, Unit *u, int type, bool success) { |
e537352b | 2451 | |
4927fcae LP |
2452 | #ifdef HAVE_AUDIT |
2453 | char *p; | |
e537352b | 2454 | |
4927fcae | 2455 | if (m->audit_fd < 0) |
e537352b LP |
2456 | return; |
2457 | ||
bbd3a7ba LP |
2458 | /* Don't generate audit events if the service was already |
2459 | * started and we're just deserializing */ | |
2460 | if (m->n_deserializing > 0) | |
2461 | return; | |
2462 | ||
f1dd0c3f LP |
2463 | if (m->running_as != MANAGER_SYSTEM) |
2464 | return; | |
2465 | ||
2466 | if (u->meta.type != UNIT_SERVICE) | |
2467 | return; | |
2468 | ||
4927fcae LP |
2469 | if (!(p = unit_name_to_prefix_and_instance(u->meta.id))) { |
2470 | log_error("Failed to allocate unit name for audit message: %s", strerror(ENOMEM)); | |
e537352b LP |
2471 | return; |
2472 | } | |
2473 | ||
391ade86 LP |
2474 | if (audit_log_user_comm_message(m->audit_fd, type, "", p, NULL, NULL, NULL, success) < 0) { |
2475 | log_warning("Failed to send audit message: %m"); | |
2476 | ||
2477 | if (errno == EPERM) { | |
2478 | /* We aren't allowed to send audit messages? | |
2479 | * Then let's not retry again, to avoid | |
2480 | * spamming the user with the same and same | |
2481 | * messages over and over. */ | |
2482 | ||
2483 | audit_close(m->audit_fd); | |
2484 | m->audit_fd = -1; | |
2485 | } | |
2486 | } | |
e537352b | 2487 | |
4927fcae LP |
2488 | free(p); |
2489 | #endif | |
e537352b | 2490 | |
e537352b LP |
2491 | } |
2492 | ||
e983b760 LP |
2493 | void manager_send_unit_plymouth(Manager *m, Unit *u) { |
2494 | int fd = -1; | |
2495 | union sockaddr_union sa; | |
2496 | int n = 0; | |
2497 | char *message = NULL; | |
e983b760 LP |
2498 | |
2499 | /* Don't generate plymouth events if the service was already | |
2500 | * started and we're just deserializing */ | |
2501 | if (m->n_deserializing > 0) | |
2502 | return; | |
2503 | ||
2504 | if (m->running_as != MANAGER_SYSTEM) | |
2505 | return; | |
2506 | ||
2507 | if (u->meta.type != UNIT_SERVICE && | |
2508 | u->meta.type != UNIT_MOUNT && | |
2509 | u->meta.type != UNIT_SWAP) | |
2510 | return; | |
2511 | ||
2512 | /* We set SOCK_NONBLOCK here so that we rather drop the | |
2513 | * message then wait for plymouth */ | |
2514 | if ((fd = socket(AF_UNIX, SOCK_STREAM|SOCK_CLOEXEC|SOCK_NONBLOCK, 0)) < 0) { | |
2515 | log_error("socket() failed: %m"); | |
2516 | return; | |
2517 | } | |
2518 | ||
2519 | zero(sa); | |
2520 | sa.sa.sa_family = AF_UNIX; | |
96707269 LP |
2521 | strncpy(sa.un.sun_path+1, "/org/freedesktop/plymouthd", sizeof(sa.un.sun_path)-1); |
2522 | if (connect(fd, &sa.sa, offsetof(struct sockaddr_un, sun_path) + 1 + strlen(sa.un.sun_path+1)) < 0) { | |
e983b760 LP |
2523 | |
2524 | if (errno != EPIPE && | |
2525 | errno != EAGAIN && | |
2526 | errno != ENOENT && | |
2527 | errno != ECONNREFUSED && | |
2528 | errno != ECONNRESET && | |
2529 | errno != ECONNABORTED) | |
2530 | log_error("connect() failed: %m"); | |
2531 | ||
2532 | goto finish; | |
2533 | } | |
2534 | ||
2535 | if (asprintf(&message, "U\002%c%s%n", (int) (strlen(u->meta.id) + 1), u->meta.id, &n) < 0) { | |
2536 | log_error("Out of memory"); | |
2537 | goto finish; | |
2538 | } | |
2539 | ||
2540 | errno = 0; | |
bd40a2d8 | 2541 | if (write(fd, message, n + 1) != n + 1) { |
e983b760 LP |
2542 | |
2543 | if (errno != EPIPE && | |
2544 | errno != EAGAIN && | |
2545 | errno != ENOENT && | |
2546 | errno != ECONNREFUSED && | |
2547 | errno != ECONNRESET && | |
2548 | errno != ECONNABORTED) | |
2549 | log_error("Failed to write Plymouth message: %m"); | |
2550 | ||
2551 | goto finish; | |
2552 | } | |
2553 | ||
2554 | finish: | |
2555 | if (fd >= 0) | |
2556 | close_nointr_nofail(fd); | |
2557 | ||
2558 | free(message); | |
2559 | } | |
2560 | ||
05e343b7 LP |
2561 | void manager_dispatch_bus_name_owner_changed( |
2562 | Manager *m, | |
2563 | const char *name, | |
2564 | const char* old_owner, | |
2565 | const char *new_owner) { | |
2566 | ||
2567 | Unit *u; | |
2568 | ||
2569 | assert(m); | |
2570 | assert(name); | |
2571 | ||
2572 | if (!(u = hashmap_get(m->watch_bus, name))) | |
2573 | return; | |
2574 | ||
2575 | UNIT_VTABLE(u)->bus_name_owner_change(u, name, old_owner, new_owner); | |
2576 | } | |
2577 | ||
2578 | void manager_dispatch_bus_query_pid_done( | |
2579 | Manager *m, | |
2580 | const char *name, | |
2581 | pid_t pid) { | |
2582 | ||
2583 | Unit *u; | |
2584 | ||
2585 | assert(m); | |
2586 | assert(name); | |
2587 | assert(pid >= 1); | |
2588 | ||
2589 | if (!(u = hashmap_get(m->watch_bus, name))) | |
2590 | return; | |
2591 | ||
2592 | UNIT_VTABLE(u)->bus_query_pid_done(u, name, pid); | |
2593 | } | |
2594 | ||
d8d5ab98 | 2595 | int manager_open_serialization(Manager *m, FILE **_f) { |
b925e726 | 2596 | char *path = NULL; |
a16e1123 LP |
2597 | mode_t saved_umask; |
2598 | int fd; | |
2599 | FILE *f; | |
2600 | ||
2601 | assert(_f); | |
2602 | ||
b925e726 | 2603 | if (m->running_as == MANAGER_SYSTEM) |
2b583ce6 | 2604 | asprintf(&path, "/run/systemd/dump-%lu-XXXXXX", (unsigned long) getpid()); |
b925e726 LP |
2605 | else |
2606 | asprintf(&path, "/tmp/systemd-dump-%lu-XXXXXX", (unsigned long) getpid()); | |
d8d5ab98 | 2607 | |
b925e726 LP |
2608 | if (!path) |
2609 | return -ENOMEM; | |
a16e1123 LP |
2610 | |
2611 | saved_umask = umask(0077); | |
2612 | fd = mkostemp(path, O_RDWR|O_CLOEXEC); | |
2613 | umask(saved_umask); | |
2614 | ||
2615 | if (fd < 0) { | |
2616 | free(path); | |
2617 | return -errno; | |
2618 | } | |
2619 | ||
2620 | unlink(path); | |
2621 | ||
2622 | log_debug("Serializing state to %s", path); | |
2623 | free(path); | |
2624 | ||
da19d5c1 | 2625 | if (!(f = fdopen(fd, "w+"))) |
a16e1123 LP |
2626 | return -errno; |
2627 | ||
2628 | *_f = f; | |
2629 | ||
2630 | return 0; | |
2631 | } | |
2632 | ||
2633 | int manager_serialize(Manager *m, FILE *f, FDSet *fds) { | |
2634 | Iterator i; | |
2635 | Unit *u; | |
2636 | const char *t; | |
2637 | int r; | |
2638 | ||
2639 | assert(m); | |
2640 | assert(f); | |
2641 | assert(fds); | |
2642 | ||
01d67b43 LP |
2643 | fprintf(f, "current-job-id=%i\n", m->current_job_id); |
2644 | fprintf(f, "taint-usr=%s\n", yes_no(m->taint_usr)); | |
2645 | ||
e9ddabc2 | 2646 | dual_timestamp_serialize(f, "initrd-timestamp", &m->initrd_timestamp); |
10717a1a LP |
2647 | dual_timestamp_serialize(f, "startup-timestamp", &m->startup_timestamp); |
2648 | dual_timestamp_serialize(f, "finish-timestamp", &m->finish_timestamp); | |
47a483a1 | 2649 | |
f2382a94 LP |
2650 | fputc('\n', f); |
2651 | ||
a16e1123 LP |
2652 | HASHMAP_FOREACH_KEY(u, t, m->units, i) { |
2653 | if (u->meta.id != t) | |
2654 | continue; | |
2655 | ||
2656 | if (!unit_can_serialize(u)) | |
2657 | continue; | |
2658 | ||
2659 | /* Start marker */ | |
2660 | fputs(u->meta.id, f); | |
2661 | fputc('\n', f); | |
2662 | ||
2663 | if ((r = unit_serialize(u, f, fds)) < 0) | |
2664 | return r; | |
2665 | } | |
2666 | ||
2667 | if (ferror(f)) | |
2668 | return -EIO; | |
2669 | ||
2670 | return 0; | |
2671 | } | |
2672 | ||
2673 | int manager_deserialize(Manager *m, FILE *f, FDSet *fds) { | |
2674 | int r = 0; | |
2675 | ||
2676 | assert(m); | |
2677 | assert(f); | |
2678 | ||
2679 | log_debug("Deserializing state..."); | |
2680 | ||
9f611ad8 | 2681 | m->n_deserializing ++; |
82c64bf5 | 2682 | |
10f8e83c | 2683 | for (;;) { |
20c03b7b | 2684 | char line[LINE_MAX], *l; |
10f8e83c LP |
2685 | |
2686 | if (!fgets(line, sizeof(line), f)) { | |
2687 | if (feof(f)) | |
2688 | r = 0; | |
2689 | else | |
2690 | r = -errno; | |
2691 | ||
2692 | goto finish; | |
2693 | } | |
2694 | ||
2695 | char_array_0(line); | |
2696 | l = strstrip(line); | |
2697 | ||
2698 | if (l[0] == 0) | |
2699 | break; | |
2700 | ||
01d67b43 LP |
2701 | if (startswith(l, "current-job-id=")) { |
2702 | uint32_t id; | |
2703 | ||
2704 | if (safe_atou32(l+15, &id) < 0) | |
2705 | log_debug("Failed to parse current job id value %s", l+15); | |
2706 | else | |
2707 | m->current_job_id = MAX(m->current_job_id, id); | |
2708 | } else if (startswith(l, "taint-usr=")) { | |
2709 | int b; | |
2710 | ||
2711 | if ((b = parse_boolean(l+10)) < 0) | |
2712 | log_debug("Failed to parse taint /usr flag %s", l+10); | |
2713 | else | |
2714 | m->taint_usr = m->taint_usr || b; | |
2715 | } else if (startswith(l, "initrd-timestamp=")) | |
e9ddabc2 LP |
2716 | dual_timestamp_deserialize(l+17, &m->initrd_timestamp); |
2717 | else if (startswith(l, "startup-timestamp=")) | |
799fd0fd | 2718 | dual_timestamp_deserialize(l+18, &m->startup_timestamp); |
10717a1a | 2719 | else if (startswith(l, "finish-timestamp=")) |
799fd0fd | 2720 | dual_timestamp_deserialize(l+17, &m->finish_timestamp); |
10717a1a | 2721 | else |
10f8e83c LP |
2722 | log_debug("Unknown serialization item '%s'", l); |
2723 | } | |
2724 | ||
a16e1123 LP |
2725 | for (;;) { |
2726 | Unit *u; | |
2727 | char name[UNIT_NAME_MAX+2]; | |
2728 | ||
2729 | /* Start marker */ | |
2730 | if (!fgets(name, sizeof(name), f)) { | |
2731 | if (feof(f)) | |
10f8e83c LP |
2732 | r = 0; |
2733 | else | |
2734 | r = -errno; | |
a16e1123 | 2735 | |
82c64bf5 | 2736 | goto finish; |
a16e1123 LP |
2737 | } |
2738 | ||
2739 | char_array_0(name); | |
2740 | ||
398ef8ba | 2741 | if ((r = manager_load_unit(m, strstrip(name), NULL, NULL, &u)) < 0) |
82c64bf5 | 2742 | goto finish; |
a16e1123 LP |
2743 | |
2744 | if ((r = unit_deserialize(u, f, fds)) < 0) | |
82c64bf5 | 2745 | goto finish; |
a16e1123 LP |
2746 | } |
2747 | ||
10f8e83c | 2748 | finish: |
82c64bf5 LP |
2749 | if (ferror(f)) { |
2750 | r = -EIO; | |
2751 | goto finish; | |
2752 | } | |
a16e1123 | 2753 | |
9f611ad8 LP |
2754 | assert(m->n_deserializing > 0); |
2755 | m->n_deserializing --; | |
82c64bf5 LP |
2756 | |
2757 | return r; | |
a16e1123 LP |
2758 | } |
2759 | ||
2760 | int manager_reload(Manager *m) { | |
2761 | int r, q; | |
2762 | FILE *f; | |
2763 | FDSet *fds; | |
2764 | ||
2765 | assert(m); | |
2766 | ||
d8d5ab98 | 2767 | if ((r = manager_open_serialization(m, &f)) < 0) |
a16e1123 LP |
2768 | return r; |
2769 | ||
2770 | if (!(fds = fdset_new())) { | |
2771 | r = -ENOMEM; | |
2772 | goto finish; | |
2773 | } | |
2774 | ||
2775 | if ((r = manager_serialize(m, f, fds)) < 0) | |
2776 | goto finish; | |
2777 | ||
2778 | if (fseeko(f, 0, SEEK_SET) < 0) { | |
2779 | r = -errno; | |
2780 | goto finish; | |
2781 | } | |
2782 | ||
2783 | /* From here on there is no way back. */ | |
2784 | manager_clear_jobs_and_units(m); | |
5a1e9937 | 2785 | manager_undo_generators(m); |
a16e1123 | 2786 | |
2ded0c04 | 2787 | /* Find new unit paths */ |
84e3543e LP |
2788 | lookup_paths_free(&m->lookup_paths); |
2789 | if ((q = lookup_paths_init(&m->lookup_paths, m->running_as)) < 0) | |
2ded0c04 LP |
2790 | r = q; |
2791 | ||
5a1e9937 LP |
2792 | manager_run_generators(m); |
2793 | ||
2794 | manager_build_unit_path_cache(m); | |
2795 | ||
9f611ad8 LP |
2796 | m->n_deserializing ++; |
2797 | ||
a16e1123 LP |
2798 | /* First, enumerate what we can from all config files */ |
2799 | if ((q = manager_enumerate(m)) < 0) | |
2800 | r = q; | |
2801 | ||
2802 | /* Second, deserialize our stored data */ | |
2803 | if ((q = manager_deserialize(m, f, fds)) < 0) | |
2804 | r = q; | |
2805 | ||
2806 | fclose(f); | |
2807 | f = NULL; | |
2808 | ||
2809 | /* Third, fire things up! */ | |
2810 | if ((q = manager_coldplug(m)) < 0) | |
2811 | r = q; | |
2812 | ||
9f611ad8 LP |
2813 | assert(m->n_deserializing > 0); |
2814 | m->n_deserializing ++; | |
2815 | ||
a16e1123 LP |
2816 | finish: |
2817 | if (f) | |
2818 | fclose(f); | |
2819 | ||
2820 | if (fds) | |
2821 | fdset_free(fds); | |
2822 | ||
2823 | return r; | |
2824 | } | |
2825 | ||
9e58ff9c LP |
2826 | bool manager_is_booting_or_shutting_down(Manager *m) { |
2827 | Unit *u; | |
2828 | ||
2829 | assert(m); | |
2830 | ||
2831 | /* Is the initial job still around? */ | |
2832 | if (manager_get_job(m, 1)) | |
2833 | return true; | |
2834 | ||
2835 | /* Is there a job for the shutdown target? */ | |
2836 | if (((u = manager_get_unit(m, SPECIAL_SHUTDOWN_TARGET)))) | |
2837 | return !!u->meta.job; | |
2838 | ||
2839 | return false; | |
2840 | } | |
2841 | ||
fdf20a31 | 2842 | void manager_reset_failed(Manager *m) { |
5632e374 LP |
2843 | Unit *u; |
2844 | Iterator i; | |
2845 | ||
2846 | assert(m); | |
2847 | ||
2848 | HASHMAP_FOREACH(u, m->units, i) | |
fdf20a31 | 2849 | unit_reset_failed(u); |
5632e374 LP |
2850 | } |
2851 | ||
8f6df3fa LP |
2852 | bool manager_unit_pending_inactive(Manager *m, const char *name) { |
2853 | Unit *u; | |
2854 | ||
2855 | assert(m); | |
2856 | assert(name); | |
2857 | ||
2858 | /* Returns true if the unit is inactive or going down */ | |
8f6df3fa LP |
2859 | if (!(u = manager_get_unit(m, name))) |
2860 | return true; | |
2861 | ||
18ffdfda | 2862 | return unit_pending_inactive(u); |
8f6df3fa LP |
2863 | } |
2864 | ||
b0c918b9 | 2865 | void manager_check_finished(Manager *m) { |
e9ddabc2 | 2866 | char userspace[FORMAT_TIMESPAN_MAX], initrd[FORMAT_TIMESPAN_MAX], kernel[FORMAT_TIMESPAN_MAX], sum[FORMAT_TIMESPAN_MAX]; |
b0c918b9 LP |
2867 | |
2868 | assert(m); | |
2869 | ||
2870 | if (dual_timestamp_is_set(&m->finish_timestamp)) | |
2871 | return; | |
2872 | ||
2873 | if (hashmap_size(m->jobs) > 0) | |
2874 | return; | |
2875 | ||
2876 | dual_timestamp_get(&m->finish_timestamp); | |
2877 | ||
e03ae661 LP |
2878 | if (m->running_as == MANAGER_SYSTEM && detect_container(NULL) <= 0) { |
2879 | ||
e9ddabc2 LP |
2880 | if (dual_timestamp_is_set(&m->initrd_timestamp)) { |
2881 | log_info("Startup finished in %s (kernel) + %s (initrd) + %s (userspace) = %s.", | |
2882 | format_timespan(kernel, sizeof(kernel), | |
2883 | m->initrd_timestamp.monotonic), | |
2884 | format_timespan(initrd, sizeof(initrd), | |
2885 | m->startup_timestamp.monotonic - m->initrd_timestamp.monotonic), | |
2886 | format_timespan(userspace, sizeof(userspace), | |
2887 | m->finish_timestamp.monotonic - m->startup_timestamp.monotonic), | |
2888 | format_timespan(sum, sizeof(sum), | |
2889 | m->finish_timestamp.monotonic)); | |
2890 | } else | |
2891 | log_info("Startup finished in %s (kernel) + %s (userspace) = %s.", | |
2892 | format_timespan(kernel, sizeof(kernel), | |
2893 | m->startup_timestamp.monotonic), | |
2894 | format_timespan(userspace, sizeof(userspace), | |
2895 | m->finish_timestamp.monotonic - m->startup_timestamp.monotonic), | |
2896 | format_timespan(sum, sizeof(sum), | |
2897 | m->finish_timestamp.monotonic)); | |
2898 | } else | |
b0c918b9 LP |
2899 | log_debug("Startup finished in %s.", |
2900 | format_timespan(userspace, sizeof(userspace), | |
2901 | m->finish_timestamp.monotonic - m->startup_timestamp.monotonic)); | |
2902 | ||
2903 | } | |
2904 | ||
5a1e9937 LP |
2905 | void manager_run_generators(Manager *m) { |
2906 | DIR *d = NULL; | |
5a1e9937 | 2907 | const char *generator_path; |
83cc030f | 2908 | const char *argv[3]; |
5a1e9937 LP |
2909 | |
2910 | assert(m); | |
2911 | ||
af2d49f7 | 2912 | generator_path = m->running_as == MANAGER_SYSTEM ? SYSTEM_GENERATOR_PATH : USER_GENERATOR_PATH; |
5a1e9937 LP |
2913 | if (!(d = opendir(generator_path))) { |
2914 | ||
2915 | if (errno == ENOENT) | |
2916 | return; | |
2917 | ||
2918 | log_error("Failed to enumerate generator directory: %m"); | |
2919 | return; | |
2920 | } | |
2921 | ||
2922 | if (!m->generator_unit_path) { | |
2923 | char *p; | |
2b583ce6 | 2924 | char system_path[] = "/run/systemd/generator-XXXXXX", |
af2d49f7 | 2925 | user_path[] = "/tmp/systemd-generator-XXXXXX"; |
5a1e9937 | 2926 | |
af2d49f7 | 2927 | if (!(p = mkdtemp(m->running_as == MANAGER_SYSTEM ? system_path : user_path))) { |
5a1e9937 LP |
2928 | log_error("Failed to generate generator directory: %m"); |
2929 | goto finish; | |
2930 | } | |
2931 | ||
2932 | if (!(m->generator_unit_path = strdup(p))) { | |
2933 | log_error("Failed to allocate generator unit path."); | |
2934 | goto finish; | |
2935 | } | |
2936 | } | |
2937 | ||
83cc030f LP |
2938 | argv[0] = NULL; /* Leave this empty, execute_directory() will fill something in */ |
2939 | argv[1] = m->generator_unit_path; | |
2940 | argv[2] = NULL; | |
5a1e9937 | 2941 | |
83cc030f | 2942 | execute_directory(generator_path, d, (char**) argv); |
5a1e9937 LP |
2943 | |
2944 | if (rmdir(m->generator_unit_path) >= 0) { | |
2945 | /* Uh? we were able to remove this dir? I guess that | |
2946 | * means the directory was empty, hence let's shortcut | |
2947 | * this */ | |
2948 | ||
2949 | free(m->generator_unit_path); | |
2950 | m->generator_unit_path = NULL; | |
2951 | goto finish; | |
2952 | } | |
2953 | ||
2954 | if (!strv_find(m->lookup_paths.unit_path, m->generator_unit_path)) { | |
2955 | char **l; | |
2956 | ||
2957 | if (!(l = strv_append(m->lookup_paths.unit_path, m->generator_unit_path))) { | |
2958 | log_error("Failed to add generator directory to unit search path: %m"); | |
2959 | goto finish; | |
2960 | } | |
2961 | ||
2962 | strv_free(m->lookup_paths.unit_path); | |
2963 | m->lookup_paths.unit_path = l; | |
7f4e0805 LP |
2964 | |
2965 | log_debug("Added generator unit path %s to search path.", m->generator_unit_path); | |
5a1e9937 LP |
2966 | } |
2967 | ||
2968 | finish: | |
2969 | if (d) | |
2970 | closedir(d); | |
5a1e9937 LP |
2971 | } |
2972 | ||
2973 | void manager_undo_generators(Manager *m) { | |
2974 | assert(m); | |
2975 | ||
2976 | if (!m->generator_unit_path) | |
2977 | return; | |
2978 | ||
2979 | strv_remove(m->lookup_paths.unit_path, m->generator_unit_path); | |
2980 | rm_rf(m->generator_unit_path, false, true); | |
2981 | ||
2982 | free(m->generator_unit_path); | |
2983 | m->generator_unit_path = NULL; | |
2984 | } | |
2985 | ||
06d4c99a LP |
2986 | int manager_set_default_controllers(Manager *m, char **controllers) { |
2987 | char **l; | |
2988 | ||
2989 | assert(m); | |
2990 | ||
2991 | if (!(l = strv_copy(controllers))) | |
2992 | return -ENOMEM; | |
2993 | ||
2994 | strv_free(m->default_controllers); | |
2995 | m->default_controllers = l; | |
2996 | ||
2997 | return 0; | |
2998 | } | |
2999 | ||
f1dd0c3f LP |
3000 | void manager_recheck_syslog(Manager *m) { |
3001 | Unit *u; | |
3002 | ||
3003 | assert(m); | |
3004 | ||
3005 | if (m->running_as != MANAGER_SYSTEM) | |
3006 | return; | |
3007 | ||
3008 | if ((u = manager_get_unit(m, SPECIAL_SYSLOG_SOCKET))) { | |
3009 | SocketState state; | |
3010 | ||
3011 | state = SOCKET(u)->state; | |
3012 | ||
3013 | if (state != SOCKET_DEAD && | |
3014 | state != SOCKET_FAILED && | |
3015 | state != SOCKET_RUNNING) { | |
3016 | ||
3017 | /* Hmm, the socket is not set up, or is still | |
3018 | * listening, let's better not try to use | |
3019 | * it. Note that we have no problem if the | |
3020 | * socket is completely down, since there | |
3021 | * might be a foreign /dev/log socket around | |
3022 | * and we want to make use of that. | |
3023 | */ | |
3024 | ||
3025 | log_close_syslog(); | |
3026 | return; | |
3027 | } | |
3028 | } | |
3029 | ||
3030 | if ((u = manager_get_unit(m, SPECIAL_SYSLOG_TARGET))) | |
3031 | if (TARGET(u)->state != TARGET_ACTIVE) { | |
3032 | log_close_syslog(); | |
3033 | return; | |
3034 | } | |
3035 | ||
3036 | /* Hmm, OK, so the socket is either fully up, or fully down, | |
3037 | * and the target is up, then let's make use of the socket */ | |
3038 | log_open(); | |
3039 | } | |
3040 | ||
dfcd764e | 3041 | static const char* const manager_running_as_table[_MANAGER_RUNNING_AS_MAX] = { |
dfcd764e | 3042 | [MANAGER_SYSTEM] = "system", |
af2d49f7 | 3043 | [MANAGER_USER] = "user" |
dfcd764e LP |
3044 | }; |
3045 | ||
3046 | DEFINE_STRING_TABLE_LOOKUP(manager_running_as, ManagerRunningAs); |