jail: fix several issues discovered by Coverity
[project/procd.git] / jail / cgroups.c
index 3e53dd5e71410c7ee0ac5a420b1a06b4acd13539..a9484e8d8bc1759b100de1730c4d44a67e062ac4 100644 (file)
@@ -178,13 +178,14 @@ void cgroups_apply(pid_t pid)
                *cdir = '\0';
                snprintf(ent, maxlen, "%s/cgroup.subtree_control", cgroup_path);
                DEBUG(" * %s\n", ent);
-               if ((fd = open(ent, O_WRONLY)) == -1) {
+               if ((fd = open(ent, O_WRONLY)) < 0) {
                        ERROR("can't open %s: %m\n", ent);
                        continue;
                }
 
                if (write(fd, subtree_control, strlen(subtree_control)) == -1) {
                        ERROR("can't write to %s: %m\n", ent);
+                       close(fd);
                        continue;
                }
 
@@ -196,7 +197,7 @@ void cgroups_apply(pid_t pid)
                DEBUG("applying cgroup2 %s=\"%s\"\n", (char *)valp->avl.key, valp->val);
                snprintf(ent, maxlen, "%s/%s", cgroup_path, (char *)valp->avl.key);
                fd = open(ent, O_WRONLY);
-               if (fd == -1) {
+               if (fd < 0) {
                        ERROR("can't open %s: %m\n", ent);
                        continue;
                }