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