Skip to content

Commit

Permalink
conf: improve write_id_mapping()
Browse files Browse the repository at this point in the history
Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>
  • Loading branch information
Christian Brauner authored and stgraber committed Jul 1, 2017
1 parent ccab716 commit 843d122
Showing 1 changed file with 23 additions and 17 deletions.
40 changes: 23 additions & 17 deletions src/lxc/conf.c
Expand Up @@ -3285,27 +3285,33 @@ int lxc_assign_network(const char *lxcpath, char *lxcname,
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;
}

/* Check whether a binary exist and has either CAP_SETUID, CAP_SETGID or both. */
Expand Down

0 comments on commit 843d122

Please sign in to comment.