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