]> git.proxmox.com Git - mirror_lxcfs.git/commitdiff
bindings: cgfs_set_value(), write_string()
authorChristian Brauner <cbrauner@suse.de>
Fri, 5 Aug 2016 22:58:33 +0000 (00:58 +0200)
committerChristian Brauner <cbrauner@suse.de>
Mon, 8 Aug 2016 18:00:45 +0000 (20:00 +0200)
Enable cgfs_set_value(), write_string() to be used with *at() family of
functions.

Signed-off-by: Christian Brauner <cbrauner@suse.de>
bindings.c

index fb905c19c62daa8736e6d495e2b5dc1c1d6e7200..150d0996dde28459bbeeb36749aec71057f4d01b 100644 (file)
@@ -329,12 +329,12 @@ static char *slurp_file(const char *from, int fd)
        return contents;
 }
 
-static bool write_string(const char *fnam, const char *string)
+static bool write_string(const char *fnam, const char *string, int fd)
 {
        FILE *f;
        size_t len, ret;
 
-       if (!(f = fopen(fnam, "w")))
+       if (!(f = fdopen(fd, "w")))
                return false;
        len = strlen(string);
        ret = fwrite(string, 1, len, f);
@@ -432,18 +432,24 @@ static char *find_mounted_controller(const char *controller, int *cfd)
 bool cgfs_set_value(const char *controller, const char *cgroup, const char *file,
                const char *value)
 {
-       int cfd;
+       int ret, fd, cfd;
        size_t len;
        char *fnam, *tmpc = find_mounted_controller(controller, &cfd);
 
        if (!tmpc)
                return false;
-       /* BASEDIR / tmpc / cgroup / file \0 */
-       len = strlen(BASEDIR) + strlen(tmpc) + strlen(cgroup) + strlen(file) + 4;
+       /* . + /cgroup + / + file + \0 */
+       len = strlen(cgroup) + strlen(file) + 3;
        fnam = alloca(len);
-       snprintf(fnam, len, "%s/%s/%s/%s", BASEDIR, tmpc, cgroup, file);
+       ret = snprintf(fnam, len, "%s%s/%s", *cgroup == '/' ? "." : "", cgroup, file);
+       if (ret < 0 || (size_t)ret >= len)
+               return false;
+
+       fd = openat(cfd, fnam, O_WRONLY);
+       if (fd < 0)
+               return false;
 
-       return write_string(fnam, value);
+       return write_string(fnam, value, fd);
 }
 
 // Chown all the files in the cgroup directory.  We do this when we create