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