Skip to content
This repository has been archived by the owner on Feb 8, 2021. It is now read-only.

container.c: Fix compiler errors that gcc 8.1.0 reports #356

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 28 additions & 19 deletions src/container.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "syscall.h"
#include "netlink.h"

#define MAX_PBUF 512
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please just use PATH_MAX defined in <limits.h>, as being done in container_check_volume.


static int container_populate_volume(char *src, char *dest)
{
struct stat st;
Expand Down Expand Up @@ -99,12 +101,12 @@ static int container_setup_volume(struct hyper_pod *pod,
struct hyper_container *container)
{
int i;
char dev[512], path[512];
char dev[MAX_PBUF], path[MAX_PBUF];
struct volume *vol;

for (i = 0; i < container->vols_num; i++) {
char volume[512];
char mountpoint[512];
char volume[MAX_PBUF];
char mountpoint[MAX_PBUF];
char *options = NULL;
const char *filevolume = NULL;
vol = &container->vols[i];
Expand All @@ -128,7 +130,8 @@ static int container_setup_volume(struct hyper_pod *pod,
if (hyper_mount_nfs(vol->device, path) < 0)
return -1;
/* nfs export has implicitly included _data part of the volume */
sprintf(volume, "/%s/", path);
if (snprintf(volume, MAX_PBUF, "/%s/", path) >= MAX_PBUF)
return -1;
} else {
fprintf(stdout, "mount %s to %s, tmp path %s\n",
dev, vol->mountpoint, path);
Expand All @@ -137,7 +140,7 @@ static int container_setup_volume(struct hyper_pod *pod,
options = "nouuid";

if (access(dev, R_OK) < 0) {
char device[512];
char device[MAX_PBUF];
sprintf(device, "/block/%s", vol->device);
hyper_netlink_wait_dev(pod->ueventfd, device);
}
Expand All @@ -146,7 +149,8 @@ static int container_setup_volume(struct hyper_pod *pod,
perror("mount volume device failed");
return -1;
}
sprintf(volume, "/%s/_data", path);
if (snprintf(volume, MAX_PBUF, "/%s/_data", path) >= MAX_PBUF)
return -1;
}

if (container_check_file_volume(volume, &filevolume) < 0)
Expand All @@ -173,7 +177,8 @@ static int container_setup_volume(struct hyper_pod *pod,
perror("create volume file failed");
return -1;
}
sprintf(volume, "/%s/_data/%s", path, filevolume);
if (snprintf(volume, MAX_PBUF, "/%s/_data/%s", path, filevolume) >= MAX_PBUF)
return -1;
/* 0777 so that any user can read/write the new file volume */
if (chmod(volume, 0777) < 0) {
fprintf(stderr, "fail to chmod directory %s\n", volume);
Expand All @@ -197,9 +202,9 @@ static int container_setup_volume(struct hyper_pod *pod,

for (i = 0; i < container->maps_num; i++) {
struct stat st;
char *src, path[512], volume[512];
char *src, path[MAX_PBUF], volume[MAX_PBUF];
struct fsmap *map = &container->maps[i];
char mountpoint[512];
char mountpoint[MAX_PBUF];

sprintf(path, "%s/%s", SHARED_DIR, map->source);
sprintf(mountpoint, "./%s", map->path);
Expand All @@ -215,7 +220,8 @@ static int container_setup_volume(struct hyper_pod *pod,
}
if (map->docker) {
/* converted from volume */
sprintf(volume, "%s/_data", path);
if (snprintf(volume, MAX_PBUF, "%s/_data", path) >= MAX_PBUF)
return -1;
src = volume;
if (container->initialize &&
(container_populate_volume(mountpoint, volume) < 0)) {
Expand Down Expand Up @@ -251,15 +257,16 @@ static int container_setup_modules(struct hyper_container *container)
{
struct stat st;
struct utsname uts;
char src[512], dst[512];
char src[MAX_PBUF], dst[MAX_PBUF];

if (uname(&uts) < 0) {
perror("fail to call uname");
return -1;
}

sprintf(src, "/lib/modules/%s", uts.release);
sprintf(dst, "./%s", src);
if (snprintf(dst, MAX_PBUF, "./%s", src) >= MAX_PBUF)
return -1;

if (stat(dst, &st) == 0) {
struct dirent **list;
Expand Down Expand Up @@ -298,7 +305,7 @@ static int container_setup_modules(struct hyper_container *container)

static int container_setup_mount(struct hyper_container *container)
{
char src[512];
char src[MAX_PBUF];

// current dir is container rootfs, the operations on "./PATH" are the operations on container's "/PATH"
if (!container->readonly) {
Expand Down Expand Up @@ -556,7 +563,7 @@ static int hyper_setup_container_rootfs(void *data)
{
struct hyper_container_arg *arg = data;
struct hyper_container *container = arg->c;
char root[512], rootfs[512];
char root[MAX_PBUF], rootfs[MAX_PBUF];
int setup_dns;

/* wait for ns-opened ready message */
Expand Down Expand Up @@ -619,7 +626,7 @@ static int hyper_setup_container_rootfs(void *data)
goto fail;
}
} else {
char path[512];
char path[MAX_PBUF];

sprintf(path, "%s/%s/", SHARED_DIR, container->image);
fprintf(stdout, "src directory %s\n", path);
Expand All @@ -637,7 +644,9 @@ static int hyper_setup_container_rootfs(void *data)
fprintf(stdout, "root directory for container is %s/%s, init task %s\n",
root, container->rootfs, container->exec.argv[0]);

sprintf(rootfs, "%s/%s/", root, container->rootfs);
if (snprintf(rootfs, MAX_PBUF, "%s/%s/", root, container->rootfs) >= MAX_PBUF)
goto fail;

if (mount(rootfs, rootfs, NULL, MS_BIND|MS_REC, NULL) < 0) {
perror("failed to bind rootfs");
goto fail;
Expand Down Expand Up @@ -720,7 +729,7 @@ static int hyper_setup_container_rootfs(void *data)

static int hyper_setup_pty(struct hyper_container *c)
{
char root[512];
char root[MAX_PBUF];

sprintf(root, "/tmp/hyper/%s/devpts/", c->id);

Expand All @@ -740,7 +749,7 @@ static int hyper_setup_pty(struct hyper_container *c)

static void hyper_cleanup_pty(struct hyper_container *c)
{
char path[512];
char path[MAX_PBUF];

sprintf(path, "/tmp/hyper/%s/devpts/", c->id);
if (umount(path) < 0)
Expand All @@ -749,7 +758,7 @@ static void hyper_cleanup_pty(struct hyper_container *c)

int container_prepare_rootfs_dev(struct hyper_container *container, struct hyper_pod *pod)
{
char dev[512];
char dev[MAX_PBUF];

if (container->fstype == NULL)
return 0;
Expand Down