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