richardphibel / rpms / systemd

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