ryantimwilson / rpms / systemd

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