From: Christian Brauner Date: Sat, 27 May 2017 06:16:01 +0000 (+0200) Subject: conf: improve write_id_mapping() X-Git-Tag: lxc-1.0.11~42 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6def43230b47d5981ca15787dd30966679ac1d75;p=thirdparty%2Flxc.git conf: improve write_id_mapping() Signed-off-by: Christian Brauner --- diff --git a/src/lxc/conf.c b/src/lxc/conf.c index df6e50131..1057ac971 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -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)