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