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