diff --git a/src/container.c b/src/container.c index d03e519..d66dc9c 100644 --- a/src/container.c +++ b/src/container.c @@ -41,7 +41,7 @@ static int container_populate_volume(char *src, char *dest) return -1; } - if (hyper_mkdir(dest) < 0) { + if (hyper_mkdir(dest, 0755) < 0) { fprintf(stderr, "fail to create directroy %s\n", dest); return -1; } @@ -108,7 +108,7 @@ static int container_setup_volume(struct hyper_container *container) fprintf(stdout, "mount %s to %s, tmp path %s\n", dev, vol->mountpoint, path); - if (hyper_mkdir(path) < 0) { + if (hyper_mkdir(path, 0755) < 0) { perror("create volume dir failed"); return -1; } @@ -122,7 +122,8 @@ static int container_setup_volume(struct hyper_container *container) } sprintf(volume, "/%s/_data", path); - if (hyper_mkdir(volume) < 0) { + /* 0777 so that any user can write to new volumes */ + if (hyper_mkdir(volume, 0777) < 0) { fprintf(stderr, "fail to create directroy %s\n", volume); return -1; } @@ -131,7 +132,7 @@ static int container_setup_volume(struct hyper_container *container) return -1; if (filevolume == NULL) { - if (hyper_mkdir(mountpoint) < 0) { + if (hyper_mkdir(mountpoint, 0755) < 0) { perror("create volume dir failed"); return -1; } @@ -178,7 +179,7 @@ static int container_setup_volume(struct hyper_container *container) stat(src, &st); if (st.st_mode & S_IFDIR) { - if (hyper_mkdir(mountpoint) < 0) { + if (hyper_mkdir(mountpoint, 0755) < 0) { perror("create map dir failed"); continue; } @@ -244,7 +245,7 @@ static int container_setup_modules(struct hyper_container *container) return 0; } } else if (errno == ENOENT) { - if (hyper_mkdir(dst) < 0) + if (hyper_mkdir(dst, 0755) < 0) return -1; } else { return -1; @@ -263,10 +264,10 @@ static int container_setup_mount(struct hyper_container *container) char src[512]; // current dir is container rootfs, the operations on "./PATH" are the operations on container's "/PATH" - hyper_mkdir("./proc"); - hyper_mkdir("./sys"); - hyper_mkdir("./dev"); - hyper_mkdir("./lib/modules"); + hyper_mkdir("./proc", 0755); + hyper_mkdir("./sys", 0755); + hyper_mkdir("./dev", 0755); + hyper_mkdir("./lib/modules", 0755); // mount proc filesystem when the container init process running in the pidns of podinit if (mount("sysfs", "./sys", "sysfs", MS_NOSUID| MS_NODEV| MS_NOEXEC, NULL) < 0 || @@ -275,7 +276,7 @@ static int container_setup_mount(struct hyper_container *container) return -1; } - if (hyper_mkdir("./dev/shm") < 0) { + if (hyper_mkdir("./dev/shm", 0755) < 0) { fprintf(stderr, "create /dev/shm failed\n"); return -1; } @@ -285,7 +286,7 @@ static int container_setup_mount(struct hyper_container *container) return -1; } - if (hyper_mkdir("./dev/pts") < 0) { + if (hyper_mkdir("./dev/pts", 0755) < 0) { fprintf(stderr, "create /dev/pts failed\n"); return -1; } @@ -352,7 +353,7 @@ static int container_setup_init_layer(struct hyper_container *container, if (!container->initialize) return 0; - hyper_mkdir("./etc/"); + hyper_mkdir("./etc/", 0755); if (setup_dns && container_recreate_file("./etc/resolv.conf") < 0) return -1; @@ -424,7 +425,7 @@ static int container_setup_dns(struct hyper_container *container) return -1; } - hyper_mkdir("./etc"); + hyper_mkdir("./etc", 0755); fd = open("./etc/resolv.conf", O_CREAT| O_WRONLY, 0644); if (fd < 0) { @@ -445,7 +446,7 @@ static int container_setup_workdir(struct hyper_container *container) { if (container->initialize) { // create workdir - return hyper_mkdir(container->exec.workdir); + return hyper_mkdir(container->exec.workdir, 0755); } return 0; @@ -527,7 +528,7 @@ static int hyper_setup_container_rootfs(void *data) } sprintf(root, "/tmp/hyper/%s/root/", container->id); - if (hyper_mkdir(root) < 0) { + if (hyper_mkdir(root, 0755) < 0) { perror("make root directroy failed"); goto fail; } @@ -646,7 +647,7 @@ static int hyper_setup_pty(struct hyper_container *c) sprintf(root, "/tmp/hyper/%s/devpts/", c->id); - if (hyper_mkdir(root) < 0) { + if (hyper_mkdir(root, 0755) < 0) { perror("make container pts directroy failed"); return -1; } diff --git a/src/hyper.h b/src/hyper.h index 8448670..f083db2 100644 --- a/src/hyper.h +++ b/src/hyper.h @@ -118,8 +118,6 @@ static inline int hyper_create(char *hyper_path) return 0; } -int hyper_create_file(const char *hyper_path); -int hyper_mkdir(char *hyper_path); int hyper_open_serial(char *tty); int hyper_start_containers(struct hyper_pod *pod); void hyper_cleanup_pod(struct hyper_pod *pod); diff --git a/src/init.c b/src/init.c index d0ede87..7f26fae 100644 --- a/src/init.c +++ b/src/init.c @@ -385,7 +385,7 @@ static int hyper_setup_shared(struct hyper_pod *pod) return 0; } - if (hyper_mkdir("/tmp/hyper/shared") < 0) { + if (hyper_mkdir("/tmp/hyper/shared", 0755) < 0) { perror("fail to create /tmp/hyper/shared"); return -1; } @@ -416,7 +416,7 @@ static int hyper_setup_shared(struct hyper_pod *pod) return 0; } - if (hyper_mkdir("/tmp/hyper/shared") < 0) { + if (hyper_mkdir("/tmp/hyper/shared", 0755) < 0) { perror("fail to create /tmp/hyper/shared"); return -1; } @@ -435,7 +435,7 @@ static int hyper_setup_shared(struct hyper_pod *pod) static int hyper_setup_pod(struct hyper_pod *pod) { /* create tmp proc directroy */ - if (hyper_mkdir("/tmp/hyper/proc") < 0) { + if (hyper_mkdir("/tmp/hyper/proc", 0755) < 0) { perror("create tmp proc failed"); return -1; } @@ -1174,10 +1174,10 @@ int main(int argc, char *argv[]) { char *cmdline, *ctl_serial, *tty_serial; - if (hyper_mkdir("/dev") < 0 || - hyper_mkdir("/sys") < 0 || - hyper_mkdir("/sbin") < 0 || - hyper_mkdir("/proc") < 0) { + if (hyper_mkdir("/dev", 0755) < 0 || + hyper_mkdir("/sys", 0755) < 0 || + hyper_mkdir("/sbin", 0755) < 0 || + hyper_mkdir("/proc", 0755) < 0) { perror("create basic directroy failed"); return -1; } @@ -1199,7 +1199,7 @@ int main(int argc, char *argv[]) return -1; } - if (hyper_mkdir("/dev/pts") < 0) { + if (hyper_mkdir("/dev/pts", 0755) < 0) { perror("create basic directroy failed"); return -1; } diff --git a/src/util.c b/src/util.c index 1e0f2ad..c1be6b9 100644 --- a/src/util.c +++ b/src/util.c @@ -228,7 +228,7 @@ int hyper_create_file(const char *hyper_path) return 0; } -int hyper_mkdir(char *hyper_path) +int hyper_mkdir(char *hyper_path, mode_t mode) { struct stat st; char *p, *path = strdup(hyper_path); @@ -257,14 +257,14 @@ int hyper_mkdir(char *hyper_path) if (p != path) { *p = '\0'; - if (hyper_mkdir(path) < 0) + if (hyper_mkdir(path, mode) < 0) goto fail; *p = '/'; } fprintf(stdout, "create directory %s\n", path); - if (mkdir(path, 0755) < 0 && errno != EEXIST) { + if (mkdir(path, mode) < 0 && errno != EEXIST) { perror("failed to create directory"); goto fail; } diff --git a/src/util.h b/src/util.h index 09e505f..7a1eeb1 100644 --- a/src/util.h +++ b/src/util.h @@ -25,7 +25,8 @@ void hyper_sync_time_hctosys(); void online_cpu(void); void online_memory(void); int hyper_cmd(char *cmd); -int hyper_mkdir(char *path); +int hyper_create_file(const char *hyper_path); +int hyper_mkdir(char *path, mode_t mode); int hyper_open_channel(char *channel, int mode); int hyper_open_serial_dev(char *tty); int hyper_setfd_cloexec(int fd);