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