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