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