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