]> git.proxmox.com Git - mirror_lxcfs.git/commitdiff
bindings: introduce dot_or_empty()
authorChristian Brauner <christian.brauner@ubuntu.com>
Mon, 24 Feb 2020 09:54:08 +0000 (10:54 +0100)
committerChristian Brauner <christian.brauner@ubuntu.com>
Mon, 24 Feb 2020 09:56:10 +0000 (10:56 +0100)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
bindings.c
cgroups/cgfsng.c
cgroups/cgroup_utils.h

index 35c1e426cfba84417afec1ab403b450f912a0bbd..f612d44fe3df6c660af3b7e17b0be03287f0c280 100644 (file)
@@ -738,7 +738,7 @@ bool cgfs_set_value(const char *controller, const char *cgroup, const char *file
         */
        len = strlen(cgroup) + strlen(file) + 3;
        fnam = alloca(len);
-       ret = snprintf(fnam, len, "%s%s/%s", *cgroup == '/' ? "." : "", cgroup, file);
+       ret = snprintf(fnam, len, "%s%s/%s", dot_or_empty(cgroup), cgroup, file);
        if (ret < 0 || (size_t)ret >= len)
                return false;
 
@@ -804,7 +804,7 @@ int cgfs_create(const char *controller, const char *cg, uid_t uid, gid_t gid)
         */
        len = strlen(cg) + 2;
        dirnam = alloca(len);
-       snprintf(dirnam, len, "%s%s", *cg == '/' ? "." : "", cg);
+       snprintf(dirnam, len, "%s%s", dot_or_empty(cg), cg);
 
        if (mkdirat(cfd, dirnam, 0755) < 0)
                return -errno;
@@ -895,7 +895,7 @@ bool cgfs_remove(const char *controller, const char *cg)
         */
        len = strlen(cg) + 2;
        dirnam = alloca(len);
-       snprintf(dirnam, len, "%s%s", *cg == '/' ? "." : "", cg);
+       snprintf(dirnam, len, "%s%s", dot_or_empty(cg), cg);
 
        fd = openat(cfd, dirnam, O_DIRECTORY);
        if (fd < 0)
@@ -921,7 +921,7 @@ bool cgfs_chmod_file(const char *controller, const char *file, mode_t mode)
         */
        len = strlen(file) + 2;
        pathname = alloca(len);
-       snprintf(pathname, len, "%s%s", *file == '/' ? "." : "", file);
+       snprintf(pathname, len, "%s%s", dot_or_empty(file), file);
        if (fchmodat(cfd, pathname, mode, 0) < 0)
                return false;
        return true;
@@ -958,7 +958,7 @@ int cgfs_chown_file(const char *controller, const char *file, uid_t uid, gid_t g
         */
        len = strlen(file) + 2;
        pathname = alloca(len);
-       snprintf(pathname, len, "%s%s", *file == '/' ? "." : "", file);
+       snprintf(pathname, len, "%s%s", dot_or_empty(file), file);
        if (fchownat(cfd, pathname, uid, gid, 0) < 0)
                return -errno;
 
@@ -984,7 +984,7 @@ FILE *open_pids_file(const char *controller, const char *cgroup)
         */
        len = strlen(cgroup) + strlen("cgroup.procs") + 3;
        pathname = alloca(len);
-       snprintf(pathname, len, "%s%s/cgroup.procs", *cgroup == '/' ? "." : "", cgroup);
+       snprintf(pathname, len, "%s%s/cgroup.procs", dot_or_empty(cgroup), cgroup);
 
        fd = openat(cfd, pathname, O_WRONLY);
        if (fd < 0)
@@ -1013,7 +1013,7 @@ static bool cgfs_iterate_cgroup(const char *controller, const char *cgroup, bool
        /* Make sure we pass a relative path to *at() family of functions. */
        len = strlen(cgroup) + 1 /* . */ + 1 /* \0 */;
        cg = alloca(len);
-       ret = snprintf(cg, len, "%s%s", *cgroup == '/' ? "." : "", cgroup);
+       ret = snprintf(cg, len, "%s%s", dot_or_empty(cgroup), cgroup);
        if (ret < 0 || (size_t)ret >= len) {
                lxcfs_error("Pathname too long under %s\n", cgroup);
                return false;
@@ -1117,7 +1117,7 @@ bool cgfs_param_exist(const char *controller, const char *cgroup, const char *fi
         */
        len = strlen(cgroup) + strlen(file) + 3;
        fnam = alloca(len);
-       ret = snprintf(fnam, len, "%s%s/%s", *cgroup == '/' ? "." : "", cgroup, file);
+       ret = snprintf(fnam, len, "%s%s/%s", dot_or_empty(cgroup), cgroup, file);
        if (ret < 0 || (size_t)ret >= len)
                return false;
 
@@ -1149,7 +1149,7 @@ struct cgfs_files *cgfs_get_key(const char *controller, const char *cgroup, cons
        if (file)
                len += strlen(file) + 1;
        fnam = alloca(len);
-       snprintf(fnam, len, "%s%s%s%s", *cgroup == '/' ? "." : "", cgroup,
+       snprintf(fnam, len, "%s%s%s%s", dot_or_empty(cgroup), cgroup,
                 file ? "/" : "", file ? file : "");
 
        ret = fstatat(cfd, fnam, &sb, 0);
@@ -1204,7 +1204,7 @@ bool is_child_cgroup(const char *controller, const char *cgroup, const char *f)
         */
        len = strlen(cgroup) + strlen(f) + 3;
        fnam = alloca(len);
-       ret = snprintf(fnam, len, "%s%s/%s", *cgroup == '/' ? "." : "", cgroup, f);
+       ret = snprintf(fnam, len, "%s%s/%s", dot_or_empty(cgroup), cgroup, f);
        if (ret < 0 || (size_t)ret >= len)
                return false;
 
@@ -5597,7 +5597,7 @@ void *load_begin(void *arg)
                                        path = malloc(length);
                                } while (!path);
 
-                               ret = snprintf(path, length, "%s%s", *(f->cg) == '/' ? "." : "", f->cg);
+                               ret = snprintf(path, length, "%s%s", dot_or_empty(f->cg), f->cg);
                                if (ret < 0 || ret > length - 1) {
                                        /* snprintf failed, ignore the node.*/
                                        lxcfs_error("Refresh node %s failed for snprintf().\n", f->cg);
index 558f0f5615aa82f1f261ea13895ac6ceb9ec8c40..793176a00afaf4384bb2bccee567e813c72b9473 100644 (file)
@@ -598,7 +598,7 @@ static bool cgfsng_get(struct cgroup_ops *ops, const char *controller,
        if (!h)
                return false;
 
-       path = must_make_path(*cgroup == '/' ? "." : "", cgroup, file, NULL);
+       path = must_make_path(dot_or_empty(cgroup), cgroup, file, NULL);
        *value = readat_file(h->fd, path);
        return *value != NULL;
 }
@@ -628,7 +628,7 @@ static int cgfsng_get_memory(struct cgroup_ops *ops, const char *cgroup,
                ret = CGROUP2_SUPER_MAGIC;
        }
 
-       path = must_make_path(*cgroup == '/' ? "." : "", cgroup, file, NULL);
+       path = must_make_path(dot_or_empty(cgroup), cgroup, file, NULL);
        *value = readat_file(h->fd, path);
        if (!*value)
                ret = -1;
@@ -701,7 +701,7 @@ static int cgfsng_get_cpuset_cpus(struct cgroup_ops *ops, const char *cgroup,
                ret = CGROUP2_SUPER_MAGIC;
 
        *value = NULL;
-       path = must_make_path(*cgroup == '/' ? "." : "", cgroup, NULL);
+       path = must_make_path(dot_or_empty(cgroup), cgroup, NULL);
        cgroup_fd = openat_safe(h->fd, path);
        if (cgroup_fd < 0) {
                return -1;
index 74b20cba7716f2e41e4dee05ead7d0821f642719..b395aa2373924d0fdec6fd93f5111ef42aa3f5d6 100644 (file)
@@ -76,4 +76,9 @@ static inline int openat_safe(int fd, const char *path)
        return openat(fd, path, O_DIRECTORY | O_RDONLY | O_CLOEXEC | O_NOFOLLOW);
 }
 
+static inline const char *dot_or_empty(const char *s)
+{
+       return (*s == '/') ? (const char *){"."} : (const char *){""};
+}
+
 #endif /* __LXC_CGROUP_UTILS_H */