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