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