[libvirt] [PATCH 2/8] Use virFileReadAll/virFileWriteStr for key cgroup read/write helpers

Daniel P. Berrange berrange at redhat.com
Wed Jul 22 15:23:41 UTC 2009


Signed-off-by: Daniel P. Berrange <berrange at redhat.com>
---
 src/cgroup.c |   87 ++++++++++++++++++----------------------------------------
 1 files changed, 27 insertions(+), 60 deletions(-)

diff --git a/src/cgroup.c b/src/cgroup.c
index 4955298..aecdf47 100644
--- a/src/cgroup.c
+++ b/src/cgroup.c
@@ -178,7 +178,6 @@ static int virCgroupSetValueStr(virCgroupPtr group,
                                 const char *key,
                                 const char *value)
 {
-    int fd = -1;
     int rc = 0;
     char *keypath = NULL;
 
@@ -186,47 +185,16 @@ static int virCgroupSetValueStr(virCgroupPtr group,
     if (rc != 0)
         return rc;
 
-    fd = open(keypath, O_WRONLY);
-    if (fd < 0) {
-        DEBUG("Unable to open %s: %m", keypath);
-        rc = -ENOENT;
-        goto out;
-    }
-
-    DEBUG("Writing '%s' to '%s'", value, keypath);
-
-    rc = safewrite(fd, value, strlen(value));
+    VIR_DEBUG("Set value %s", keypath);
+    rc = virFileWriteStr(keypath, value);
     if (rc < 0) {
         DEBUG("Failed to write value '%s': %m", value);
         rc = -errno;
-        goto out;
-    } else if (rc != strlen(value)) {
-        DEBUG("Short write of value '%s'", value);
-        rc = -ENOSPC;
-        goto out;
+    } else {
+        rc = 0;
     }
 
-    rc = 0;
-out:
     VIR_FREE(keypath);
-    close(fd);
-
-    return rc;
-}
-
-static int virCgroupSetValueU64(virCgroupPtr group,
-                                const char *key,
-                                uint64_t value)
-{
-    char *strval = NULL;
-    int rc;
-
-    if (virAsprintf(&strval, "%" PRIu64, value) == -1)
-        return -ENOMEM;
-
-    rc = virCgroupSetValueStr(group, key, strval);
-
-    VIR_FREE(strval);
 
     return rc;
 }
@@ -235,12 +203,10 @@ static int virCgroupGetValueStr(virCgroupPtr group,
                                 const char *key,
                                 char **value)
 {
-    int fd = -1;
     int rc;
     char *keypath = NULL;
-    char buf[CGROUP_MAX_VAL];
 
-    memset(buf, 0, sizeof(buf));
+    *value = NULL;
 
     rc = virCgroupPathOf(group->path, key, &keypath);
     if (rc != 0) {
@@ -248,38 +214,39 @@ static int virCgroupGetValueStr(virCgroupPtr group,
         return rc;
     }
 
-    fd = open(keypath, O_RDONLY);
-    if (fd < 0) {
-        DEBUG("Unable to open %s: %m", keypath);
-        rc = -ENOENT;
-        goto out;
-    }
+    VIR_DEBUG("Get value %s", keypath);
 
-    rc = saferead(fd, buf, sizeof(buf));
+    rc = virFileReadAll(keypath, 1024, value);
     if (rc < 0) {
         DEBUG("Failed to read %s: %m\n", keypath);
         rc = -errno;
-        goto out;
-    } else if (rc == 0) {
-        DEBUG("Short read of %s\n", keypath);
-        rc = -EIO;
-        goto out;
-    }
-
-    *value = strdup(buf);
-    if (*value == NULL) {
-        rc = -ENOMEM;
-        goto out;
+    } else {
+        rc = 0;
     }
 
-    rc = 0;
-out:
     VIR_FREE(keypath);
-    close(fd);
 
     return rc;
 }
 
+static int virCgroupSetValueU64(virCgroupPtr group,
+                                const char *key,
+                                uint64_t value)
+{
+    char *strval = NULL;
+    int rc;
+
+    if (virAsprintf(&strval, "%" PRIu64, value) == -1)
+        return -ENOMEM;
+
+    rc = virCgroupSetValueStr(group, key, strval);
+
+    VIR_FREE(strval);
+
+    return rc;
+}
+
+
 #if 0
 /* This is included for completeness, but not yet used */
 
-- 
1.6.2.5




More information about the libvir-list mailing list