X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=blobdiff_plain;f=src%2Fcore%2Funit.c;h=e40e6f2068c682f58e135f254cf65202f84351d6;hb=7c52a17b1a31eedd40093a4fbb460cf492087d9b;hp=56102b360d13cd8a34c4381251295ac3030bfcb1;hpb=4c94096027f21d4ed0efe991534a926d39d52369;p=elogind.git diff --git a/src/core/unit.c b/src/core/unit.c index 56102b360..e40e6f206 100644 --- a/src/core/unit.c +++ b/src/core/unit.c @@ -81,7 +81,7 @@ Unit *unit_new(Manager *m, size_t size) { if (!u) return NULL; - u->names = set_new(string_hash_func, string_compare_func); + u->names = set_new(&string_hash_ops); if (!u->names) { free(u); return NULL; @@ -553,29 +553,38 @@ const char* unit_sub_state_to_string(Unit *u) { return UNIT_VTABLE(u)->sub_state_to_string(u); } -static void complete_move(Set **s, Set **other) { +static int complete_move(Set **s, Set **other) { + int r; + assert(s); assert(other); if (!*other) - return; + return 0; - if (*s) - set_move(*s, *other); - else { + if (*s) { + r = set_move(*s, *other); + if (r < 0) + return r; + } else { *s = *other; *other = NULL; } + + return 0; } -static void merge_names(Unit *u, Unit *other) { +static int merge_names(Unit *u, Unit *other) { char *t; Iterator i; + int r; assert(u); assert(other); - complete_move(&u->names, &other->names); + r = complete_move(&u->names, &other->names); + if (r < 0) + return r; set_free_free(other->names); other->names = NULL; @@ -583,6 +592,29 @@ static void merge_names(Unit *u, Unit *other) { SET_FOREACH(t, u->names, i) assert_se(hashmap_replace(u->manager->units, t, u) == 0); + + return 0; +} + +static int reserve_dependencies(Unit *u, Unit *other, UnitDependency d) { + unsigned n_reserve; + + assert(u); + assert(other); + assert(d < _UNIT_DEPENDENCY_MAX); + + /* + * If u does not have this dependency set allocated, there is no need + * to reserve anything. In that case other's set will be transfered + * as a whole to u by complete_move(). + */ + if (!u->dependencies[d]) + return 0; + + /* merge_dependencies() will skip a u-on-u dependency */ + n_reserve = set_size(other->dependencies[d]) - !!set_get(other->dependencies[d], u); + + return set_reserve(u->dependencies[d], n_reserve); } static void merge_dependencies(Unit *u, Unit *other, const char *other_id, UnitDependency d) { @@ -618,7 +650,8 @@ static void merge_dependencies(Unit *u, Unit *other, const char *other_id, UnitD if (back) maybe_warn_about_dependency(u->id, other_id, d); - complete_move(&u->dependencies[d], &other->dependencies[d]); + /* The move cannot fail. The caller must have performed a reservation. */ + assert_se(complete_move(&u->dependencies[d], &other->dependencies[d]) == 0); set_free(other->dependencies[d]); other->dependencies[d] = NULL; @@ -627,6 +660,7 @@ static void merge_dependencies(Unit *u, Unit *other, const char *other_id, UnitD int unit_merge(Unit *u, Unit *other) { UnitDependency d; const char *other_id = NULL; + int r; assert(u); assert(other); @@ -660,8 +694,21 @@ int unit_merge(Unit *u, Unit *other) { if (other->id) other_id = strdupa(other->id); + /* Make reservations to ensure merge_dependencies() won't fail */ + for (d = 0; d < _UNIT_DEPENDENCY_MAX; d++) { + r = reserve_dependencies(u, other, d); + /* + * We don't rollback reservations if we fail. We don't have + * a way to undo reservations. A reservation is not a leak. + */ + if (r < 0) + return r; + } + /* Merge names */ - merge_names(u, other); + r = merge_names(u, other); + if (r < 0) + return r; /* Redirect all references */ while (other->refs) @@ -1843,17 +1890,17 @@ int unit_watch_pid(Unit *u, pid_t pid) { /* Watch a specific PID. We only support one or two units * watching each PID for now, not more. */ - r = set_ensure_allocated(&u->pids, trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&u->pids, NULL); if (r < 0) return r; - r = hashmap_ensure_allocated(&u->manager->watch_pids1, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&u->manager->watch_pids1, NULL); if (r < 0) return r; r = hashmap_put(u->manager->watch_pids1, LONG_TO_PTR(pid), u); if (r == -EEXIST) { - r = hashmap_ensure_allocated(&u->manager->watch_pids2, trivial_hash_func, trivial_compare_func); + r = hashmap_ensure_allocated(&u->manager->watch_pids2, NULL); if (r < 0) return r; @@ -2086,22 +2133,22 @@ int unit_add_dependency(Unit *u, UnitDependency d, Unit *other, bool add_referen return 0; } - r = set_ensure_allocated(&u->dependencies[d], trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&u->dependencies[d], NULL); if (r < 0) return r; if (inverse_table[d] != _UNIT_DEPENDENCY_INVALID) { - r = set_ensure_allocated(&other->dependencies[inverse_table[d]], trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&other->dependencies[inverse_table[d]], NULL); if (r < 0) return r; } if (add_reference) { - r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&u->dependencies[UNIT_REFERENCES], NULL); if (r < 0) return r; - r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], trivial_hash_func, trivial_compare_func); + r = set_ensure_allocated(&other->dependencies[UNIT_REFERENCED_BY], NULL); if (r < 0) return r; } @@ -2847,7 +2894,7 @@ 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); + pid_set = set_new(NULL); if (!pid_set) return NULL; @@ -3080,7 +3127,11 @@ static int unit_drop_in_dir(Unit *u, UnitSetPropertiesMode mode, bool transient, if (u->manager->running_as == SYSTEMD_USER) { int r; - r = user_config_home(dir); + if (mode == UNIT_PERSISTENT && !transient) + r = user_config_home(dir); + else + r = user_runtime_dir(dir); + if (r == 0) return -ENOENT; return r; @@ -3228,7 +3279,7 @@ int unit_make_transient(Unit *u) { if (u->manager->running_as == SYSTEMD_USER) { _cleanup_free_ char *c = NULL; - r = user_config_home(&c); + r = user_runtime_dir(&c); if (r < 0) return r; if (r == 0) @@ -3385,7 +3436,7 @@ int unit_require_mounts_for(Unit *u, const char *path) { char *q; if (!u->manager->units_requiring_mounts_for) { - u->manager->units_requiring_mounts_for = hashmap_new(string_hash_func, string_compare_func); + u->manager->units_requiring_mounts_for = hashmap_new(&string_hash_ops); if (!u->manager->units_requiring_mounts_for) return -ENOMEM; } @@ -3394,7 +3445,7 @@ int unit_require_mounts_for(Unit *u, const char *path) { if (!q) return -ENOMEM; - x = set_new(NULL, NULL); + x = set_new(NULL); if (!x) { free(q); return -ENOMEM; @@ -3425,7 +3476,7 @@ int unit_setup_exec_runtime(Unit *u) { offset = UNIT_VTABLE(u)->exec_runtime_offset; assert(offset > 0); - /* Check if ther already is an ExecRuntime for this unit? */ + /* Check if there already is an ExecRuntime for this unit? */ rt = (ExecRuntime**) ((uint8_t*) u + offset); if (*rt) return 0; @@ -3453,32 +3504,3 @@ static const char* const unit_active_state_table[_UNIT_ACTIVE_STATE_MAX] = { }; DEFINE_STRING_TABLE_LOOKUP(unit_active_state, UnitActiveState); - -static const char* const unit_dependency_table[_UNIT_DEPENDENCY_MAX] = { - [UNIT_REQUIRES] = "Requires", - [UNIT_REQUIRES_OVERRIDABLE] = "RequiresOverridable", - [UNIT_REQUISITE] = "Requisite", - [UNIT_REQUISITE_OVERRIDABLE] = "RequisiteOverridable", - [UNIT_WANTS] = "Wants", - [UNIT_BINDS_TO] = "BindsTo", - [UNIT_PART_OF] = "PartOf", - [UNIT_REQUIRED_BY] = "RequiredBy", - [UNIT_REQUIRED_BY_OVERRIDABLE] = "RequiredByOverridable", - [UNIT_WANTED_BY] = "WantedBy", - [UNIT_BOUND_BY] = "BoundBy", - [UNIT_CONSISTS_OF] = "ConsistsOf", - [UNIT_CONFLICTS] = "Conflicts", - [UNIT_CONFLICTED_BY] = "ConflictedBy", - [UNIT_BEFORE] = "Before", - [UNIT_AFTER] = "After", - [UNIT_ON_FAILURE] = "OnFailure", - [UNIT_TRIGGERS] = "Triggers", - [UNIT_TRIGGERED_BY] = "TriggeredBy", - [UNIT_PROPAGATES_RELOAD_TO] = "PropagatesReloadTo", - [UNIT_RELOAD_PROPAGATED_FROM] = "ReloadPropagatedFrom", - [UNIT_JOINS_NAMESPACE_OF] = "JoinsNamespaceOf", - [UNIT_REFERENCES] = "References", - [UNIT_REFERENCED_BY] = "ReferencedBy", -}; - -DEFINE_STRING_TABLE_LOOKUP(unit_dependency, UnitDependency);