X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=manager.c;h=688d9fa65b28e831217faee879cf4977a241006f;hp=5cb5c3d2d2f3a8008453407c22f1dd6ada4134d3;hb=949061f0d6f5ae8aecb5e1fc711f336e4ba48ca2;hpb=c9c0cadbf8d8e4c1b99c35a8150c08558f361ebd diff --git a/manager.c b/manager.c index 5cb5c3d2d..688d9fa65 100644 --- a/manager.c +++ b/manager.c @@ -51,12 +51,13 @@ #include "unit-name.h" #include "dbus-unit.h" #include "dbus-job.h" +#include "missing.h" /* As soon as 16 units are in our GC queue, make sure to run a gc sweep */ #define GC_QUEUE_ENTRIES_MAX 16 /* As soon as 5s passed since a unit was added to our GC queue, make sure to run a gc sweep */ -#define GC_QUEUE_USEC_MAX (5*USEC_PER_SEC) +#define GC_QUEUE_USEC_MAX (10*USEC_PER_SEC) static int enable_special_signals(Manager *m) { char fd; @@ -67,7 +68,7 @@ static int enable_special_signals(Manager *m) { if (reboot(RB_DISABLE_CAD) < 0) log_warning("Failed to enable ctrl-alt-del handling: %m"); - if ((fd = open_terminal("/dev/tty0", O_RDWR)) < 0) + if ((fd = open_terminal("/dev/tty0", O_RDWR|O_NOCTTY)) < 0) log_warning("Failed to open /dev/tty0: %m"); else { /* Enable that we get SIGWINCH on kbrequest */ @@ -148,12 +149,12 @@ static char** session_dirs(void) { } if ((e = getenv("XDG_CONFIG_DIRS"))) - config_dirs = strv_split(e, ":"); - else - config_dirs = strv_new("/etc/xdg", NULL); + if (!(config_dirs = strv_split(e, ":"))) + goto fail; - if (!config_dirs) - goto fail; + /* We don't treat /etc/xdg/systemd here as the spec + * suggests because we assume that that is a link to + * /etc/systemd/ anyway. */ if ((e = getenv("XDG_DATA_HOME"))) { if (asprintf(&data_home, "%s/systemd/session", e) < 0) @@ -253,7 +254,7 @@ static int manager_find_paths(Manager *m) { } if (m->running_as == MANAGER_INIT) { - /* /etc/init.d/ compativility does not matter to users */ + /* /etc/init.d/ compatibility does not matter to users */ if ((e = getenv("SYSTEMD_SYSVINIT_PATH"))) if (!(m->sysvinit_path = split_path_and_make_absolute(e))) @@ -336,6 +337,9 @@ int manager_new(ManagerRunningAs running_as, bool confirm_spawn, Manager **_m) { m->signal_watch.fd = m->mount_watch.fd = m->udev_watch.fd = m->epoll_fd = m->dev_autofs_fd = -1; m->current_job_id = 1; /* start as id #1, so that we can leave #0 around as "null-like" value */ + if (!(m->environment = strv_copy(environ))) + goto fail; + if (!(m->units = hashmap_new(string_hash_func, string_compare_func))) goto fail; @@ -395,14 +399,24 @@ static unsigned manager_dispatch_cleanup_queue(Manager *m) { return n; } -static void unit_gc_sweep(Unit *u, int gc_marker) { +enum { + GC_OFFSET_IN_PATH, /* This one is on the path we were travelling */ + GC_OFFSET_UNSURE, /* No clue */ + GC_OFFSET_GOOD, /* We still need this unit */ + GC_OFFSET_BAD, /* We don't need this unit anymore */ + _GC_OFFSET_MAX +}; + +static void unit_gc_sweep(Unit *u, unsigned gc_marker) { Iterator i; Unit *other; + bool is_bad; assert(u); - if (u->meta.gc_marker == gc_marker || - u->meta.gc_marker == -gc_marker) + if (u->meta.gc_marker == gc_marker + GC_OFFSET_GOOD || + u->meta.gc_marker == gc_marker + GC_OFFSET_BAD || + u->meta.gc_marker == gc_marker + GC_OFFSET_IN_PATH) return; if (u->meta.in_cleanup_queue) @@ -411,26 +425,44 @@ static void unit_gc_sweep(Unit *u, int gc_marker) { if (unit_check_gc(u)) goto good; + u->meta.gc_marker = gc_marker + GC_OFFSET_IN_PATH; + + is_bad = true; + SET_FOREACH(other, u->meta.dependencies[UNIT_REFERENCED_BY], i) { unit_gc_sweep(other, gc_marker); - if (other->meta.gc_marker == gc_marker) + if (other->meta.gc_marker == gc_marker + GC_OFFSET_GOOD) goto good; + + if (other->meta.gc_marker != gc_marker + GC_OFFSET_BAD) + is_bad = false; } + if (is_bad) + goto bad; + + /* We were unable to find anything out about this entry, so + * let's investigate it later */ + u->meta.gc_marker = gc_marker + GC_OFFSET_UNSURE; + unit_add_to_gc_queue(u); + return; + bad: - /* So there is no reason to keep this unit around, hence let's get rid of it */ - u->meta.gc_marker = -gc_marker; + /* We definitely know that this one is not useful anymore, so + * let's mark it for deletion */ + u->meta.gc_marker = gc_marker + GC_OFFSET_BAD; + unit_add_to_cleanup_queue(u); return; good: - u->meta.gc_marker = gc_marker; + u->meta.gc_marker = gc_marker + GC_OFFSET_GOOD; } static unsigned manager_dispatch_gc_queue(Manager *m) { Meta *meta; unsigned n = 0; - int gc_marker; + unsigned gc_marker; assert(m); @@ -441,23 +473,26 @@ static unsigned manager_dispatch_gc_queue(Manager *m) { log_debug("Running GC..."); - gc_marker = ++m->gc_marker; - - if (m->gc_marker < 0) + m->gc_marker += _GC_OFFSET_MAX; + if (m->gc_marker + _GC_OFFSET_MAX <= _GC_OFFSET_MAX) m->gc_marker = 1; + gc_marker = m->gc_marker; + while ((meta = m->gc_queue)) { assert(meta->in_gc_queue); + unit_gc_sweep(UNIT(meta), gc_marker); + LIST_REMOVE(Meta, gc_queue, m->gc_queue, meta); meta->in_gc_queue = false; n++; - unit_gc_sweep(UNIT(meta), gc_marker); - - if (meta->gc_marker == -gc_marker) { + if (meta->gc_marker == gc_marker + GC_OFFSET_BAD || + meta->gc_marker == gc_marker + GC_OFFSET_UNSURE) { log_debug("Collecting %s", meta->id); + meta->gc_marker = gc_marker + GC_OFFSET_BAD; unit_add_to_cleanup_queue(UNIT(meta)); } } @@ -486,6 +521,7 @@ void manager_free(Manager *m) { assert(m); + manager_dispatch_cleanup_queue(m); manager_clear_jobs_and_units(m); for (c = 0; c < _UNIT_TYPE_MAX; c++) @@ -513,6 +549,7 @@ void manager_free(Manager *m) { strv_free(m->unit_path); strv_free(m->sysvinit_path); strv_free(m->sysvrcnd_path); + strv_free(m->environment); free(m->cgroup_controller); free(m->cgroup_hierarchy); @@ -1004,7 +1041,7 @@ static void transaction_collect_garbage(Manager *m) { } while (again); } -static int transaction_is_destructive(Manager *m, JobMode mode) { +static int transaction_is_destructive(Manager *m) { Iterator i; Job *j; @@ -1083,7 +1120,7 @@ static void transaction_minimize_impact(Manager *m) { } while (again); } -static int transaction_apply(Manager *m, JobMode mode) { +static int transaction_apply(Manager *m) { Iterator i; Job *j; int r; @@ -1203,13 +1240,13 @@ static int transaction_activate(Manager *m, JobMode mode) { /* Ninth step: check whether we can actually apply this */ if (mode == JOB_FAIL) - if ((r = transaction_is_destructive(m, mode)) < 0) { + if ((r = transaction_is_destructive(m)) < 0) { log_debug("Requested transaction contradicts existing jobs: %s", strerror(-r)); goto rollback; } /* Tenth step: apply changes */ - if ((r = transaction_apply(m, mode)) < 0) { + if ((r = transaction_apply(m)) < 0) { log_debug("Failed to apply transaction: %s", strerror(-r)); goto rollback; } @@ -1383,6 +1420,38 @@ fail: return r; } +static int transaction_add_isolate_jobs(Manager *m) { + Iterator i; + Unit *u; + char *k; + int r; + + assert(m); + + HASHMAP_FOREACH_KEY(u, k, m->units, i) { + + /* ignore aliases */ + if (u->meta.id != k) + continue; + + if (UNIT_VTABLE(u)->no_isolate) + continue; + + /* No need to stop inactive jobs */ + if (unit_active_state(u) == UNIT_INACTIVE) + continue; + + /* Is there already something listed for this? */ + if (hashmap_get(m->transaction_jobs, u)) + continue; + + if ((r = transaction_add_job_and_dependencies(m, JOB_STOP, u, NULL, true, false, NULL)) < 0) + log_warning("Cannot add isolate job for unit %s, ignoring: %s", u->meta.id, strerror(-r)); + } + + return 0; +} + int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool override, Job **_ret) { int r; Job *ret; @@ -1392,6 +1461,9 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove assert(unit); assert(mode < _JOB_MODE_MAX); + if (mode == JOB_ISOLATE && type != JOB_START) + return -EINVAL; + log_debug("Trying to enqueue job %s/%s", unit->meta.id, job_type_to_string(type)); if ((r = transaction_add_job_and_dependencies(m, type, unit, NULL, true, override, &ret)) < 0) { @@ -1399,6 +1471,12 @@ int manager_add_job(Manager *m, JobType type, Unit *unit, JobMode mode, bool ove return r; } + if (mode == JOB_ISOLATE) + if ((r = transaction_add_isolate_jobs(m)) < 0) { + transaction_abort(m); + return r; + } + if ((r = transaction_activate(m, mode)) < 0) return r; @@ -1464,15 +1542,15 @@ unsigned manager_dispatch_load_queue(Manager *m) { return n; } -int manager_load_unit(Manager *m, const char *name, const char *path, Unit **_ret) { +int manager_load_unit_prepare(Manager *m, const char *name, const char *path, Unit **_ret) { Unit *ret; int r; assert(m); assert(name || path); - /* This will load the service information files, but not actually - * start any services or anything. */ + /* This will prepare the unit for loading, but not actually + * load anything from disk. */ if (path && !is_path(path)) return -EINVAL; @@ -1485,7 +1563,7 @@ int manager_load_unit(Manager *m, const char *name, const char *path, Unit **_re if ((ret = manager_get_unit(m, name))) { *_ret = ret; - return 0; + return 1; } if (!(ret = unit_new(m))) @@ -1504,11 +1582,29 @@ int manager_load_unit(Manager *m, const char *name, const char *path, Unit **_re unit_add_to_load_queue(ret); unit_add_to_dbus_queue(ret); + unit_add_to_gc_queue(ret); + + if (_ret) + *_ret = ret; + + return 0; +} + +int manager_load_unit(Manager *m, const char *name, const char *path, Unit **_ret) { + int r; + + assert(m); + + /* This will load the service information files, but not actually + * start any services or anything. */ + + if ((r = manager_load_unit_prepare(m, name, path, _ret)) != 0) + return r; manager_dispatch_load_queue(m); if (_ret) - *_ret = unit_follow_merge(ret); + *_ret = unit_follow_merge(*_ret); return 0; } @@ -1693,9 +1789,17 @@ static int manager_process_signal_fd(Manager *m) { sigchld = true; break; - case SIGINT: case SIGTERM: + if (m->running_as == MANAGER_INIT) + /* This is for compatibility with the + * original sysvinit */ + m->exit_code = MANAGER_REEXECUTE; + else + m->exit_code = MANAGER_EXIT; + return 0; + + case SIGINT: if (m->running_as == MANAGER_INIT) { manager_start_target(m, SPECIAL_CTRL_ALT_DEL_TARGET); break; @@ -1705,7 +1809,6 @@ static int manager_process_signal_fd(Manager *m) { return 0; case SIGWINCH: - if (m->running_as == MANAGER_INIT) manager_start_target(m, SPECIAL_KBREQUEST_TARGET); @@ -1719,12 +1822,7 @@ static int manager_process_signal_fd(Manager *m) { /* This is a nop on non-init */ break; - case SIGUSR1: - manager_dump_units(m, stdout, "\t"); - manager_dump_jobs(m, stdout, "\t"); - break; - - case SIGUSR2: { + case SIGUSR1: { Unit *u; u = manager_get_unit(m, SPECIAL_DBUS_SERVICE); @@ -1743,6 +1841,11 @@ static int manager_process_signal_fd(Manager *m) { break; } + case SIGUSR2: + manager_dump_units(m, stdout, "\t"); + manager_dump_jobs(m, stdout, "\t"); + break; + case SIGHUP: m->exit_code = MANAGER_RELOAD; break; @@ -1846,19 +1949,19 @@ int manager_loop(Manager *m) { sleep(1); } - if (manager_dispatch_cleanup_queue(m) > 0) + if (manager_dispatch_load_queue(m) > 0) continue; - if (manager_dispatch_gc_queue(m) > 0) + if (manager_dispatch_run_queue(m) > 0) continue; - if (manager_dispatch_load_queue(m) > 0) + if (bus_dispatch(m) > 0) continue; - if (manager_dispatch_run_queue(m) > 0) + if (manager_dispatch_cleanup_queue(m) > 0) continue; - if (bus_dispatch(m) > 0) + if (manager_dispatch_gc_queue(m) > 0) continue; if (manager_dispatch_dbus_queue(m) > 0) @@ -1866,7 +1969,7 @@ int manager_loop(Manager *m) { if ((n = epoll_wait(m->epoll_fd, &event, 1, -1)) < 0) { - if (errno == -EINTR) + if (errno == EINTR) continue; return -errno;