This prevents corruption of the hashmap, because we would free() the
keys in the hashmap, if the unit is already in there, with the same
cgroup path.
static int unit_create_cgroups(Unit *u, CGroupControllerMask mask) {
char *path = NULL;
int r;
static int unit_create_cgroups(Unit *u, CGroupControllerMask mask) {
char *path = NULL;
int r;
+ bool is_in_hash = false;
return -ENOMEM;
r = hashmap_put(u->manager->cgroup_unit, path, u);
return -ENOMEM;
r = hashmap_put(u->manager->cgroup_unit, path, u);
+ if (r == 0)
+ is_in_hash = true;
+
+ if (r < 0) {
+ free(path);
+ log_error("cgroup %s exists already: %s", path, strerror(-r));
/* First, create our own group */
r = cg_create_with_mask(mask, path);
/* First, create our own group */
r = cg_create_with_mask(mask, path);
log_error("Failed to migrate cgroup %s: %s", path, strerror(-r));
}
log_error("Failed to migrate cgroup %s: %s", path, strerror(-r));
}
- /* And remember the new data */
- free(u->cgroup_path);
- u->cgroup_path = path;
+ if (!is_in_hash) {
+ /* And remember the new data */
+ free(u->cgroup_path);
+ u->cgroup_path = path;
+ }
+
u->cgroup_realized = true;
u->cgroup_mask = mask;
u->cgroup_realized = true;
u->cgroup_mask = mask;
free(u->cgroup_path);
u->cgroup_path = s;
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);