]> git.ipfire.org Git - thirdparty/lxc.git/commitdiff
conf: improve write_id_mapping()
authorChristian Brauner <christian.brauner@ubuntu.com>
Sat, 27 May 2017 06:16:01 +0000 (08:16 +0200)
committerStéphane Graber <stgraber@ubuntu.com>
Tue, 15 Aug 2017 21:36:07 +0000 (17:36 -0400)
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
src/lxc/conf.c

index df6e50131b91eeefebff2b7a36cf76b738a6bd69..1057ac9716cdb876b7c1db7ef5e3fba5fa3be5ba 100644 (file)
@@ -3581,27 +3581,33 @@ int lxc_assign_network(struct lxc_list *network, pid_t pid)
 static int write_id_mapping(enum idtype idtype, pid_t pid, const char *buf,
                            size_t buf_size)
 {
-       char path[PATH_MAX];
-       int ret, closeret;
-       FILE *f;
+       char path[MAXPATHLEN];
+       int fd, ret;
 
-       ret = snprintf(path, PATH_MAX, "/proc/%d/%cid_map", pid, idtype == ID_TYPE_UID ? 'u' : 'g');
-       if (ret < 0 || ret >= PATH_MAX) {
-               fprintf(stderr, "%s: path name too long\n", __func__);
+       ret = snprintf(path, MAXPATHLEN, "/proc/%d/%cid_map", pid,
+                      idtype == ID_TYPE_UID ? 'u' : 'g');
+       if (ret < 0 || ret >= MAXPATHLEN) {
+               ERROR("failed to create path \"%s\"", path);
                return -E2BIG;
        }
-       f = fopen(path, "w");
-       if (!f) {
-               perror("open");
-               return -EINVAL;
+
+       fd = open(path, O_WRONLY);
+       if (fd < 0) {
+               SYSERROR("failed to open \"%s\"", path);
+               return -1;
        }
-       ret = fwrite(buf, buf_size, 1, f);
-       if (ret < 0)
-               SYSERROR("writing id mapping");
-       closeret = fclose(f);
-       if (closeret)
-               SYSERROR("writing id mapping");
-       return ret < 0 ? ret : closeret;
+
+       errno = 0;
+       ret = lxc_write_nointr(fd, buf, buf_size);
+       if (ret != buf_size) {
+               SYSERROR("failed to write %cid mapping to \"%s\"",
+                        idtype == ID_TYPE_UID ? 'u' : 'g', path);
+               close(fd);
+               return -1;
+       }
+       close(fd);
+
+       return 0;
 }
 
 int lxc_map_ids(struct lxc_list *idmap, pid_t pid)