X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Funit.c;h=ab313b9b91b6faa4e8bfdd53a82c9c3e331d2e23;hb=112a7f4696ebb96abdb42df62e1e794e903f66b3;hp=bfde08d68cf429ece1686ad5db35c750d915cbb5;hpb=a8833944647bfd10e43569646be954db5cbac54e;p=elogind.git diff --git a/src/core/unit.c b/src/core/unit.c index bfde08d68..ab313b9b9 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -189,7 +189,8 @@ fail: } int unit_choose_id(Unit *u, const char *name) { - char *s, *t = NULL, *i; + char *s, *i; + _cleanup_free_ char *t = NULL; int r; assert(u); @@ -208,7 +209,6 @@ int unit_choose_id(Unit *u, const char *name) { /* Selects one of the names of this unit as the id */ s = set_get(u->names, (char*) name); - free(t); if (!s) return -ENOENT; @@ -598,7 +598,7 @@ int unit_merge(Unit *u, Unit *other) { int unit_merge_by_name(Unit *u, const char *name) { Unit *other; int r; - char *s = NULL; + _cleanup_free_ char *s = NULL; assert(u); assert(name); @@ -619,7 +619,6 @@ int unit_merge_by_name(Unit *u, const char *name) { else r = unit_merge(u, other); - free(s); return r; } @@ -677,7 +676,7 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) { char *t, **j; UnitDependency d; Iterator i; - char *p2; + _cleanup_free_ char *p2 = NULL; const char *prefix2; char timestamp1[FORMAT_TIMESTAMP_MAX], @@ -811,7 +810,6 @@ void unit_dump(Unit *u, FILE *f, const char *prefix) { if (u->nop_job) job_dump(u->nop_job, f, prefix2); - free(p2); } /* Common implementation for multiple backends */ @@ -996,7 +994,7 @@ bool unit_condition_test(Unit *u) { assert(u); dual_timestamp_get(&u->condition_timestamp); - u->condition_result = condition_test_list(u->conditions); + u->condition_result = condition_test_list(u->id, u->conditions); return u->condition_result; } @@ -1125,7 +1123,8 @@ int unit_start(Unit *u) { } /* Forward to the main object, if we aren't it. */ - if ((following = unit_following(u))) { + following = unit_following(u); + if (following) { log_debug_unit(u->id, "Redirecting start request from %s to %s.", u->id, following->id); return unit_start(following); @@ -1425,10 +1424,14 @@ void unit_notify(Unit *u, UnitActiveState os, UnitActiveState ns, bool reload_su if (UNIT_IS_INACTIVE_OR_FAILED(os) != UNIT_IS_INACTIVE_OR_FAILED(ns)) { ExecContext *ec = unit_get_exec_context(u); if (ec && exec_context_may_touch_console(ec)) { - if (UNIT_IS_INACTIVE_OR_FAILED(ns)) - m->n_on_console--; - else - m->n_on_console++; + if (UNIT_IS_INACTIVE_OR_FAILED(ns)) { + m->n_on_console --; + + if (m->n_on_console == 0) + /* unset no_console_output flag, since the console is free */ + m->no_console_output = 0; + } else + m->n_on_console ++; } } @@ -1909,7 +1912,7 @@ int unit_add_dependency_by_name(Unit *u, UnitDependency d, const char *name, con int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { Unit *other; int r; - char *s; + _cleanup_free_ char *s = NULL; assert(u); assert(name || path); @@ -1918,19 +1921,17 @@ int unit_add_two_dependencies_by_name(Unit *u, UnitDependency d, UnitDependency return -ENOMEM; if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0) - goto finish; + return r; r = unit_add_two_dependencies(u, d, e, other, add_reference); -finish: - free(s); return r; } int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *name, const char *path, bool add_reference) { Unit *other; int r; - char *s; + _cleanup_free_ char *s = NULL; assert(u); assert(name || path); @@ -1939,19 +1940,17 @@ int unit_add_dependency_by_name_inverse(Unit *u, UnitDependency d, const char *n return -ENOMEM; if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0) - goto finish; + return r; r = unit_add_dependency(other, d, u, add_reference); -finish: - free(s); return r; } int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDependency e, const char *name, const char *path, bool add_reference) { Unit *other; int r; - char *s; + _cleanup_free_ char *s = NULL; assert(u); assert(name || path); @@ -1960,13 +1959,11 @@ int unit_add_two_dependencies_by_name_inverse(Unit *u, UnitDependency d, UnitDep return -ENOMEM; if ((r = manager_load_unit(u->manager, name, path, NULL, &other)) < 0) - goto finish; + return r; if ((r = unit_add_two_dependencies(other, d, e, u, add_reference)) < 0) - goto finish; + return r; -finish: - free(s); return r; } @@ -1991,7 +1988,7 @@ char *unit_dbus_path(Unit *u) { } char *unit_default_cgroup_path(Unit *u) { - _cleanup_free_ char *escaped_instance = NULL, *slice = NULL; + _cleanup_free_ char *escaped = NULL, *slice = NULL; int r; assert(u); @@ -2005,28 +2002,14 @@ char *unit_default_cgroup_path(Unit *u) { return NULL; } - escaped_instance = cg_escape(u->id); - if (!escaped_instance) + escaped = cg_escape(u->id); + if (!escaped) return NULL; - if (u->instance) { - _cleanup_free_ char *t = NULL, *escaped_template = NULL; - - t = unit_name_template(u->id); - if (!t) - return NULL; - - escaped_template = cg_escape(t); - if (!escaped_template) - return NULL; - - return strjoin(u->manager->cgroup_root, "/", - slice ? slice : "", slice ? "/" : "", - escaped_template, "/", escaped_instance, NULL); - } else - return strjoin(u->manager->cgroup_root, "/", - slice ? slice : "", slice ? "/" : "", - escaped_instance, NULL); + if (slice) + return strjoin(u->manager->cgroup_root, "/", slice, "/", escaped, NULL); + else + return strjoin(u->manager->cgroup_root, "/", escaped, NULL); } int unit_add_default_slice(Unit *u) { @@ -2045,7 +2028,7 @@ int unit_add_default_slice(Unit *u) { if (u->instance) { _cleanup_free_ char *prefix = NULL, *escaped = NULL; - ; + /* Implicitly place all instantiated units in their * own per-template slice */ @@ -2340,13 +2323,13 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { char *s; s = strdup(v); - if (!v) + if (!s) return -ENOMEM; free(u->cgroup_path); u->cgroup_path = s; - hashmap_put(u->manager->cgroup_unit, s, u); + assert(hashmap_put(u->manager->cgroup_unit, s, u) == 1); continue; } @@ -2358,7 +2341,7 @@ int unit_deserialize(Unit *u, FILE *f, FDSet *fds) { int unit_add_node_link(Unit *u, const char *what, bool wants) { Unit *device; - char *e; + _cleanup_free_ char *e = NULL; int r; assert(u); @@ -2376,7 +2359,7 @@ int unit_add_node_link(Unit *u, const char *what, bool wants) { return -ENOMEM; r = manager_load_unit(u->manager, e, NULL, NULL, &device); - free(e); + if (r < 0) return r; @@ -2551,6 +2534,34 @@ int unit_kill(Unit *u, KillWho w, int signo, DBusError *error) { return UNIT_VTABLE(u)->kill(u, w, signo, error); } +static Set *unit_pid_set(pid_t main_pid, pid_t control_pid) { + Set *pid_set; + int r; + + pid_set = set_new(trivial_hash_func, trivial_compare_func); + if (!pid_set) + return NULL; + + /* Exclude the main/control pids from being killed via the cgroup */ + if (main_pid > 0) { + r = set_put(pid_set, LONG_TO_PTR(main_pid)); + if (r < 0) + goto fail; + } + + if (control_pid > 0) { + r = set_put(pid_set, LONG_TO_PTR(control_pid)); + if (r < 0) + goto fail; + } + + return pid_set; + +fail: + set_free(pid_set); + return NULL; +} + int unit_kill_common( Unit *u, KillWho who, @@ -2591,23 +2602,11 @@ int unit_kill_common( _cleanup_set_free_ Set *pid_set = NULL; int q; - pid_set = set_new(trivial_hash_func, trivial_compare_func); + /* Exclude the main/control pids from being killed via the cgroup */ + pid_set = unit_pid_set(main_pid, control_pid); if (!pid_set) return -ENOMEM; - /* Exclude the control/main pid from being killed via the cgroup */ - if (control_pid > 0) { - q = set_put(pid_set, LONG_TO_PTR(control_pid)); - if (q < 0) - return q; - } - - if (main_pid > 0) { - q = set_put(pid_set, LONG_TO_PTR(main_pid)); - if (q < 0) - return q; - } - q = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, signo, false, true, false, pid_set); if (q < 0 && q != -EAGAIN && q != -ESRCH && q != -ENOENT) r = q; @@ -2747,6 +2746,7 @@ CGroupContext *unit_get_cgroup_context(Unit *u) { } static int drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, char **_p, char **_q) { + _cleanup_free_ char *b = NULL; char *p, *q; int r; @@ -2756,7 +2756,11 @@ static int drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, c assert(_q); assert(mode & (UNIT_PERSISTENT|UNIT_RUNTIME)); - if (!filename_is_safe(name)) + b = xescape(name, "/."); + if (!b) + return -ENOMEM; + + if (!filename_is_safe(b)) return -EINVAL; if (u->manager->running_as == SYSTEMD_USER) { @@ -2776,7 +2780,7 @@ static int drop_in_file(Unit *u, UnitSetPropertiesMode mode, const char *name, c if (!p) return -ENOMEM; - q = strjoin(p, "/90-", name, ".conf", NULL); + q = strjoin(p, "/90-", b, ".conf", NULL); if (!q) { free(p); return -ENOMEM; @@ -2806,7 +2810,29 @@ int unit_write_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name, co return write_string_file_atomic_label(q, data); } -int unit_write_drop_in_private_section(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { +int unit_write_drop_in_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { + _cleanup_free_ char *p = NULL; + va_list ap; + int r; + + assert(u); + assert(name); + assert(format); + + if (!(mode & (UNIT_PERSISTENT|UNIT_RUNTIME))) + return 0; + + va_start(ap, format); + r = vasprintf(&p, format, ap); + va_end(ap); + + if (r < 0) + return -ENOMEM; + + return unit_write_drop_in(u, mode, name, p); +} + +int unit_write_drop_in_private(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *data) { _cleanup_free_ char *ndata = NULL; assert(u); @@ -2816,6 +2842,9 @@ int unit_write_drop_in_private_section(Unit *u, UnitSetPropertiesMode mode, cons if (!UNIT_VTABLE(u)->private_section) return -EINVAL; + if (!(mode & (UNIT_PERSISTENT|UNIT_RUNTIME))) + return 0; + ndata = strjoin("[", UNIT_VTABLE(u)->private_section, "]\n", data, NULL); if (!ndata) return -ENOMEM; @@ -2823,6 +2852,28 @@ int unit_write_drop_in_private_section(Unit *u, UnitSetPropertiesMode mode, cons return unit_write_drop_in(u, mode, name, ndata); } +int unit_write_drop_in_private_format(Unit *u, UnitSetPropertiesMode mode, const char *name, const char *format, ...) { + _cleanup_free_ char *p = NULL; + va_list ap; + int r; + + assert(u); + assert(name); + assert(format); + + if (!(mode & (UNIT_PERSISTENT|UNIT_RUNTIME))) + return 0; + + va_start(ap, format); + r = vasprintf(&p, format, ap); + va_end(ap); + + if (r < 0) + return -ENOMEM; + + return unit_write_drop_in_private(u, mode, name, p); +} + int unit_remove_drop_in(Unit *u, UnitSetPropertiesMode mode, const char *name) { _cleanup_free_ char *p = NULL, *q = NULL; int r; @@ -2906,8 +2957,12 @@ int unit_kill_context( log_warning_unit(u->id, "Failed to kill main process %li (%s): %s", (long) main_pid, strna(comm), strerror(-r)); - } else + } else { wait_for_exit = !main_pid_alien; + + if (c->send_sighup) + kill(main_pid, SIGHUP); + } } if (control_pid > 0) { @@ -2920,36 +2975,38 @@ int unit_kill_context( log_warning_unit(u->id, "Failed to kill control process %li (%s): %s", (long) control_pid, strna(comm), strerror(-r)); - } else + } else { wait_for_exit = true; + + if (c->send_sighup) + kill(control_pid, SIGHUP); + } } if (c->kill_mode == KILL_CONTROL_GROUP && u->cgroup_path) { _cleanup_set_free_ Set *pid_set = NULL; - pid_set = set_new(trivial_hash_func, trivial_compare_func); + /* Exclude the main/control pids from being killed via the cgroup */ + pid_set = unit_pid_set(main_pid, control_pid); if (!pid_set) return -ENOMEM; - /* Exclude the main/control pids from being killed via the cgroup */ - if (main_pid > 0) { - r = set_put(pid_set, LONG_TO_PTR(main_pid)); - if (r < 0) - return r; - } - - if (control_pid > 0) { - r = set_put(pid_set, LONG_TO_PTR(control_pid)); - if (r < 0) - return r; - } - r = cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, sig, true, true, false, pid_set); if (r < 0) { if (r != -EAGAIN && r != -ESRCH && r != -ENOENT) log_warning_unit(u->id, "Failed to kill control group: %s", strerror(-r)); - } else if (r > 0) + } else if (r > 0) { wait_for_exit = true; + if (c->send_sighup) { + set_free(pid_set); + + pid_set = unit_pid_set(main_pid, control_pid); + if (!pid_set) + return -ENOMEM; + + cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, SIGHUP, true, true, false, pid_set); + } + } } return wait_for_exit;