9fc0f6
From 19a5f184904952d20cf1a6ea81e6e295fe991765 Mon Sep 17 00:00:00 2001
9fc0f6
From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= <zbyszek@in.waw.pl>
9fc0f6
Date: Fri, 8 Nov 2013 08:41:13 -0500
9fc0f6
Subject: [PATCH] systemd: fix memory leak in cgroup code
9fc0f6
9fc0f6
If the unit already was in the hashmap, path would be leaked.
9fc0f6
---
9fc0f6
 src/core/cgroup.c | 24 +++++++++++++-----------
9fc0f6
 1 file changed, 13 insertions(+), 11 deletions(-)
9fc0f6
9fc0f6
diff --git a/src/core/cgroup.c b/src/core/cgroup.c
9fc0f6
index 8bf4d89..b60707c 100644
9fc0f6
--- a/src/core/cgroup.c
9fc0f6
+++ b/src/core/cgroup.c
9fc0f6
@@ -376,23 +376,23 @@ static CGroupControllerMask unit_get_siblings_mask(Unit *u) {
9fc0f6
 }
9fc0f6
 
9fc0f6
 static int unit_create_cgroups(Unit *u, CGroupControllerMask mask) {
9fc0f6
-        char *path = NULL;
9fc0f6
+        _cleanup_free_ char *path;
9fc0f6
         int r;
9fc0f6
-        bool is_in_hash = false;
9fc0f6
+        bool was_in_hash = false;
9fc0f6
 
9fc0f6
         assert(u);
9fc0f6
 
9fc0f6
         path = unit_default_cgroup_path(u);
9fc0f6
         if (!path)
9fc0f6
-                return -ENOMEM;
9fc0f6
+                return log_oom();
9fc0f6
 
9fc0f6
         r = hashmap_put(u->manager->cgroup_unit, path, u);
9fc0f6
         if (r == 0)
9fc0f6
-                is_in_hash = true;
9fc0f6
-
9fc0f6
-        if (r < 0) {
9fc0f6
-                log_error("cgroup %s exists already: %s", path, strerror(-r));
9fc0f6
-                free(path);
9fc0f6
+                was_in_hash = true;
9fc0f6
+        else if (r < 0) {
9fc0f6
+                log_error(r == -EEXIST ?
9fc0f6
+                          "cgroup %s exists already: %s" : "hashmap_put failed for %s: %s",
9fc0f6
+                          path, strerror(-r));
9fc0f6
                 return r;
9fc0f6
         }
9fc0f6
 
9fc0f6
@@ -405,13 +405,15 @@ static int unit_create_cgroups(Unit *u, CGroupControllerMask mask) {
9fc0f6
         if (u->cgroup_path) {
9fc0f6
                 r = cg_migrate_everywhere(u->manager->cgroup_supported, u->cgroup_path, path);
9fc0f6
                 if (r < 0)
9fc0f6
-                        log_error("Failed to migrate cgroup %s: %s", path, strerror(-r));
9fc0f6
+                        log_error("Failed to migrate cgroup from %s to %s: %s",
9fc0f6
+                                  u->cgroup_path, path, strerror(-r));
9fc0f6
         }
9fc0f6
 
9fc0f6
-        if (!is_in_hash) {
9fc0f6
-                /* And remember the new data */
9fc0f6
+        if (!was_in_hash) {
9fc0f6
+                /* Remember the new data */
9fc0f6
                 free(u->cgroup_path);
9fc0f6
                 u->cgroup_path = path;
9fc0f6
+                path = NULL;
9fc0f6
         }
9fc0f6
 
9fc0f6
         u->cgroup_realized = true;