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