Commit 86530b0a by LiFeng

Fix comments and add check in lxc_poll.

Add check whether handler->conf->console.path is 'none' Signed-off-by: 's avatarLiFeng <lifeng68@huawei.com>
parent 3dc035f1
...@@ -1565,11 +1565,12 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs, ...@@ -1565,11 +1565,12 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs,
{ {
char path[MAXPATHLEN]; char path[MAXPATHLEN];
int ret, fd; int ret, fd;
char *rootfs_path = rootfs->path ? rootfs->mount : "";
if (console->path && !strcmp(console->path, "none")) if (console->path && !strcmp(console->path, "none"))
return 0; return 0;
ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs->path ? rootfs->mount : ""); ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path);
if (ret < 0 || (size_t)ret >= sizeof(path)) if (ret < 0 || (size_t)ret >= sizeof(path))
return -1; return -1;
...@@ -1579,10 +1580,10 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs, ...@@ -1579,10 +1580,10 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs,
if (file_exists(path)) { if (file_exists(path)) {
ret = lxc_unstack_mountpoint(path, false); ret = lxc_unstack_mountpoint(path, false);
if (ret < 0) { if (ret < 0) {
ERROR("failed to unmount \"%s\": %s", path, strerror(errno)); ERROR("Failed to unmount \"%s\": %s", path, strerror(errno));
return -ret; return -ret;
} else { } else {
DEBUG("cleared all (%d) mounts from \"%s\"", ret, path); DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path);
} }
} }
...@@ -1592,24 +1593,26 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs, ...@@ -1592,24 +1593,26 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs,
fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH); fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH);
if (fd < 0) { if (fd < 0) {
if (errno != EEXIST) { if (errno != EEXIST) {
SYSERROR("failed to create console"); SYSERROR("Failed to create console");
return -errno; return -errno;
} }
} else { } else {
close(fd); close(fd);
} }
if (chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH)) { ret = chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH);
SYSERROR("failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name); if (ret < 0) {
SYSERROR("Failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name);
return -errno; return -errno;
} }
if (safe_mount(console->name, path, "none", MS_BIND, 0, rootfs->path ? rootfs->mount : "") < 0) { ret = safe_mount(console->name, path, "none", MS_BIND, 0, rootfs_path);
ERROR("failed to mount '%s' on '%s'", console->name, path); if (ret < 0) {
ERROR("Failed to mount '%s' on '%s'", console->name, path);
return -1; return -1;
} }
DEBUG("mounted pts device \"%s\" onto \"%s\"", console->name, path); DEBUG("Mounted pts device \"%s\" onto \"%s\"", console->name, path);
return 0; return 0;
} }
...@@ -1619,78 +1622,82 @@ static int lxc_setup_ttydir_console(const struct lxc_rootfs *rootfs, ...@@ -1619,78 +1622,82 @@ static int lxc_setup_ttydir_console(const struct lxc_rootfs *rootfs,
{ {
int ret, fd; int ret, fd;
char path[MAXPATHLEN], lxcpath[MAXPATHLEN]; char path[MAXPATHLEN], lxcpath[MAXPATHLEN];
char *rootfs_path = rootfs->path ? rootfs->mount : "";
if (console->path && !strcmp(console->path, "none")) if (console->path && !strcmp(console->path, "none"))
return 0; return 0;
/* create rootfs/dev/<ttydir> directory */ /* create rootfs/dev/<ttydir> directory */
ret = snprintf(path, sizeof(path), "%s/dev/%s", rootfs->path ? rootfs->mount : "", ttydir); ret = snprintf(path, sizeof(path), "%s/dev/%s", rootfs_path, ttydir);
if (ret < 0 || (size_t)ret >= sizeof(path)) if (ret < 0 || (size_t)ret >= sizeof(path))
return -1; return -1;
ret = mkdir(path, 0755); ret = mkdir(path, 0755);
if (ret && errno != EEXIST) { if (ret && errno != EEXIST) {
SYSERROR("failed with errno %d to create %s", errno, path); SYSERROR("Failed with errno %d to create %s", errno, path);
return -errno; return -errno;
} }
DEBUG("Created directory for console and tty devices at \"%s\"", path); DEBUG("Created directory for console and tty devices at \"%s\"", path);
ret = snprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console", rootfs->path ? rootfs->mount : "", ttydir); ret = snprintf(lxcpath, sizeof(lxcpath), "%s/dev/%s/console", rootfs_path, ttydir);
if (ret < 0 || (size_t)ret >= sizeof(lxcpath)) if (ret < 0 || (size_t)ret >= sizeof(lxcpath))
return -1; return -1;
ret = creat(lxcpath, 0660); ret = creat(lxcpath, 0660);
if (ret == -1 && errno != EEXIST) { if (ret == -1 && errno != EEXIST) {
SYSERROR("error %d creating %s", errno, lxcpath); SYSERROR("Error %d creating %s", errno, lxcpath);
return -errno; return -errno;
} }
if (ret >= 0) if (ret >= 0)
close(ret); close(ret);
ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs->path ? rootfs->mount : ""); ret = snprintf(path, sizeof(path), "%s/dev/console", rootfs_path);
if (ret < 0 || (size_t)ret >= sizeof(path)) if (ret < 0 || (size_t)ret >= sizeof(path))
return -1; return -1;
if (file_exists(path)) { if (file_exists(path)) {
ret = lxc_unstack_mountpoint(path, false); ret = lxc_unstack_mountpoint(path, false);
if (ret < 0) { if (ret < 0) {
ERROR("failed to unmount \"%s\": %s", path, strerror(errno)); ERROR("Failed to unmount \"%s\": %s", path, strerror(errno));
return -ret; return -ret;
} else { } else {
DEBUG("cleared all (%d) mounts from \"%s\"", ret, path); DEBUG("Cleared all (%d) mounts from \"%s\"", ret, path);
} }
} }
fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH); fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH);
if (fd < 0) { if (fd < 0) {
if (errno != EEXIST) { if (errno != EEXIST) {
SYSERROR("failed to create console"); SYSERROR("Failed to create console");
return -errno; return -errno;
} }
} else { } else {
close(fd); close(fd);
} }
if (chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH)) { ret = chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH);
SYSERROR("failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name); if (ret < 0) {
SYSERROR("Failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name);
return -errno; return -errno;
} }
/* bind mount console->name to '/dev/<ttydir>/console' */ /* bind mount console->name to '/dev/<ttydir>/console' */
if (safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs->path ? rootfs->mount : "") < 0) { ret = safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs_path);
ERROR("failed to mount '%s' on '%s'", console->name, lxcpath); if (ret < 0) {
ERROR("Failed to mount '%s' on '%s'", console->name, lxcpath);
return -1; return -1;
} }
DEBUG("mounted \"%s\" onto \"%s\"", console->name, lxcpath); DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath);
/* bind mount '/dev/<ttydir>/console' to '/dev/console' */ /* bind mount '/dev/<ttydir>/console' to '/dev/console' */
if (safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs->path ? rootfs->mount : "") < 0) { ret = safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs_path);
ERROR("failed to mount '%s' on '%s'", console->name, lxcpath); if (ret < 0) {
ERROR("Failed to mount '%s' on '%s'", console->name, lxcpath);
return -1; return -1;
} }
DEBUG("mounted \"%s\" onto \"%s\"", console->name, lxcpath); DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath);
DEBUG("console has been setup under \"%s\" and mounted to \"%s\"", lxcpath, path); DEBUG("Console has been setup under \"%s\" and mounted to \"%s\"", lxcpath, path);
return 0; return 0;
} }
......
...@@ -472,19 +472,24 @@ int lxc_set_state(const char *name, struct lxc_handler *handler, ...@@ -472,19 +472,24 @@ int lxc_set_state(const char *name, struct lxc_handler *handler,
int lxc_poll(const char *name, struct lxc_handler *handler) int lxc_poll(const char *name, struct lxc_handler *handler)
{ {
int ret; int ret;
struct lxc_console *console = &handler->conf->console; bool has_console = true;
struct lxc_epoll_descr descr, descr_console; struct lxc_epoll_descr descr, descr_console;
if (handler->conf->console.path && !strcmp(handler->conf->console.path, "none"))
has_console = false;
ret = lxc_mainloop_open(&descr); ret = lxc_mainloop_open(&descr);
if (ret < 0) { if (ret < 0) {
ERROR("Failed to create mainloop"); ERROR("Failed to create mainloop");
goto out_sigfd; goto out_sigfd;
} }
ret = lxc_mainloop_open(&descr_console); if (has_console) {
if (ret < 0) { ret = lxc_mainloop_open(&descr_console);
ERROR("Failed to create console mainloop"); if (ret < 0) {
goto out_mainloop; ERROR("Failed to create console mainloop");
goto out_mainloop;
}
} }
ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler); ret = lxc_mainloop_add_handler(&descr, handler->sigfd, signal_handler, handler);
...@@ -493,16 +498,20 @@ int lxc_poll(const char *name, struct lxc_handler *handler) ...@@ -493,16 +498,20 @@ int lxc_poll(const char *name, struct lxc_handler *handler)
goto out_mainloop_console; goto out_mainloop_console;
} }
ret = lxc_console_mainloop_add(&descr, console); if (has_console) {
if (ret < 0) { struct lxc_console *console = &handler->conf->console;
ERROR("Failed to add console handlers to mainloop");
goto out_mainloop_console;
}
ret = lxc_console_mainloop_add(&descr_console, console); ret = lxc_console_mainloop_add(&descr, console);
if (ret < 0) { if (ret < 0) {
ERROR("Failed to add console handlers to console mainloop"); ERROR("Failed to add console handlers to mainloop");
goto out_mainloop_console; goto out_mainloop_console;
}
ret = lxc_console_mainloop_add(&descr_console, console);
if (ret < 0) {
ERROR("Failed to add console handlers to console mainloop");
goto out_mainloop_console;
}
} }
ret = lxc_cmd_mainloop_add(name, &descr, handler); ret = lxc_cmd_mainloop_add(name, &descr, handler);
...@@ -519,15 +528,19 @@ int lxc_poll(const char *name, struct lxc_handler *handler) ...@@ -519,15 +528,19 @@ int lxc_poll(const char *name, struct lxc_handler *handler)
if (ret < 0 || !handler->init_died) if (ret < 0 || !handler->init_died)
goto out_mainloop; goto out_mainloop;
ret = lxc_mainloop(&descr_console, 0); if (has_console)
ret = lxc_mainloop(&descr_console, 0);
out_mainloop: out_mainloop:
lxc_mainloop_close(&descr); lxc_mainloop_close(&descr);
TRACE("Closed mainloop"); TRACE("Closed mainloop");
out_mainloop_console: out_mainloop_console:
lxc_mainloop_close(&descr_console); if (has_console) {
TRACE("Closed console mainloop"); lxc_mainloop_close(&descr_console);
TRACE("Closed console mainloop");
}
out_sigfd: out_sigfd:
close(handler->sigfd); close(handler->sigfd);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment