From 86530b0ae1180a47fd0df6033f3685c5bb923d97 Mon Sep 17 00:00:00 2001 From: LiFeng Date: Mon, 22 Jan 2018 06:13:52 -0500 Subject: [PATCH] Fix comments and add check in lxc_poll. Add check whether handler->conf->console.path is 'none' Signed-off-by: LiFeng --- src/lxc/conf.c | 59 +++++++++++++++++++++++++++---------------------- src/lxc/start.c | 47 +++++++++++++++++++++++++-------------- 2 files changed, 63 insertions(+), 43 deletions(-) diff --git a/src/lxc/conf.c b/src/lxc/conf.c index 8397a981b..ff5c0c225 100644 --- a/src/lxc/conf.c +++ b/src/lxc/conf.c @@ -1565,11 +1565,12 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs, { char path[MAXPATHLEN]; int ret, fd; + char *rootfs_path = rootfs->path ? rootfs->mount : ""; if (console->path && !strcmp(console->path, "none")) 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)) return -1; @@ -1579,10 +1580,10 @@ static int lxc_setup_dev_console(const struct lxc_rootfs *rootfs, if (file_exists(path)) { ret = lxc_unstack_mountpoint(path, false); if (ret < 0) { - ERROR("failed to unmount \"%s\": %s", path, strerror(errno)); + ERROR("Failed to unmount \"%s\": %s", path, strerror(errno)); return -ret; } 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, fd = open(path, O_CREAT | O_EXCL, S_IXUSR | S_IXGRP | S_IXOTH); if (fd < 0) { if (errno != EEXIST) { - SYSERROR("failed to create console"); + SYSERROR("Failed to create console"); return -errno; } } else { close(fd); } - if (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); + ret = chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH); + if (ret < 0) { + SYSERROR("Failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name); return -errno; } - if (safe_mount(console->name, path, "none", MS_BIND, 0, rootfs->path ? rootfs->mount : "") < 0) { - ERROR("failed to mount '%s' on '%s'", console->name, path); + ret = safe_mount(console->name, path, "none", MS_BIND, 0, rootfs_path); + if (ret < 0) { + ERROR("Failed to mount '%s' on '%s'", console->name, path); return -1; } - DEBUG("mounted pts device \"%s\" onto \"%s\"", console->name, path); + DEBUG("Mounted pts device \"%s\" onto \"%s\"", console->name, path); return 0; } @@ -1619,78 +1622,82 @@ static int lxc_setup_ttydir_console(const struct lxc_rootfs *rootfs, { int ret, fd; char path[MAXPATHLEN], lxcpath[MAXPATHLEN]; + char *rootfs_path = rootfs->path ? rootfs->mount : ""; if (console->path && !strcmp(console->path, "none")) return 0; /* create rootfs/dev/ 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)) return -1; ret = mkdir(path, 0755); 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; } 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)) return -1; ret = creat(lxcpath, 0660); if (ret == -1 && errno != EEXIST) { - SYSERROR("error %d creating %s", errno, lxcpath); + SYSERROR("Error %d creating %s", errno, lxcpath); return -errno; } if (ret >= 0) 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)) return -1; if (file_exists(path)) { ret = lxc_unstack_mountpoint(path, false); if (ret < 0) { - ERROR("failed to unmount \"%s\": %s", path, strerror(errno)); + ERROR("Failed to unmount \"%s\": %s", path, strerror(errno)); return -ret; } 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); if (fd < 0) { if (errno != EEXIST) { - SYSERROR("failed to create console"); + SYSERROR("Failed to create console"); return -errno; } } else { close(fd); } - if (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); + ret = chmod(console->name, S_IXUSR | S_IXGRP | S_IXOTH); + if (ret < 0) { + SYSERROR("Failed to set mode '0%o' to '%s'", S_IXUSR | S_IXGRP | S_IXOTH, console->name); return -errno; } /* bind mount console->name to '/dev//console' */ - if (safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs->path ? rootfs->mount : "") < 0) { - ERROR("failed to mount '%s' on '%s'", console->name, lxcpath); + ret = safe_mount(console->name, lxcpath, "none", MS_BIND, 0, rootfs_path); + if (ret < 0) { + ERROR("Failed to mount '%s' on '%s'", console->name, lxcpath); return -1; } - DEBUG("mounted \"%s\" onto \"%s\"", console->name, lxcpath); + DEBUG("Mounted \"%s\" onto \"%s\"", console->name, lxcpath); /* bind mount '/dev//console' to '/dev/console' */ - if (safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs->path ? rootfs->mount : "") < 0) { - ERROR("failed to mount '%s' on '%s'", console->name, lxcpath); + ret = safe_mount(lxcpath, path, "none", MS_BIND, 0, rootfs_path); + if (ret < 0) { + ERROR("Failed to mount '%s' on '%s'", console->name, lxcpath); 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; } diff --git a/src/lxc/start.c b/src/lxc/start.c index 97e3231f1..1b14a6575 100644 --- a/src/lxc/start.c +++ b/src/lxc/start.c @@ -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 ret; - struct lxc_console *console = &handler->conf->console; + bool has_console = true; 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); if (ret < 0) { ERROR("Failed to create mainloop"); goto out_sigfd; } - ret = lxc_mainloop_open(&descr_console); - if (ret < 0) { - ERROR("Failed to create console mainloop"); - goto out_mainloop; + if (has_console) { + ret = lxc_mainloop_open(&descr_console); + if (ret < 0) { + ERROR("Failed to create console mainloop"); + goto out_mainloop; + } } 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) goto out_mainloop_console; } - ret = lxc_console_mainloop_add(&descr, console); - if (ret < 0) { - ERROR("Failed to add console handlers to mainloop"); - goto out_mainloop_console; - } + if (has_console) { + struct lxc_console *console = &handler->conf->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_console_mainloop_add(&descr, console); + if (ret < 0) { + ERROR("Failed to add console handlers to mainloop"); + 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); @@ -519,15 +528,19 @@ int lxc_poll(const char *name, struct lxc_handler *handler) if (ret < 0 || !handler->init_died) goto out_mainloop; - ret = lxc_mainloop(&descr_console, 0); + if (has_console) + ret = lxc_mainloop(&descr_console, 0); + out_mainloop: lxc_mainloop_close(&descr); TRACE("Closed mainloop"); out_mainloop_console: - lxc_mainloop_close(&descr_console); - TRACE("Closed console mainloop"); + if (has_console) { + lxc_mainloop_close(&descr_console); + TRACE("Closed console mainloop"); + } out_sigfd: close(handler->sigfd); -- 2.47.2