Move the qemuProcessLogReadFD and qemuProcessLogFD methods into qemu_domain.c, renaming them to qemuDomainCreateLog and qemuDomainOpenLog. * src/qemu/qemu_domain.c, src/qemu/qemu_domain.h: Add qemuDomainCreateLog and qemuDomainOpenLog. * src/qemu/qemu_process.c: Remove qemuProcessLogFD and qemuProcessLogReadFD --- src/qemu/qemu_domain.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ src/qemu/qemu_domain.h | 4 ++ src/qemu/qemu_process.c | 82 ++-------------------------------------------- 3 files changed, 90 insertions(+), 79 deletions(-) diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 938c063..204a628 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -34,8 +34,10 @@ #include "cpu/cpu.h" #include "ignore-value.h" #include "uuid.h" +#include "files.h" #include <sys/time.h> +#include <fcntl.h> #include <libxml/xpathInternals.h> @@ -805,3 +807,84 @@ void qemuDomainObjCheckNetTaint(struct qemud_driver *driver, net->data.bridge.script != NULL)) qemuDomainObjTaint(driver, obj, VIR_DOMAIN_TAINT_SHELL_SCRIPTS); } + + +static int +qemuDomainOpenLogHelper(struct qemud_driver *driver, + virDomainObjPtr vm, + int flags, + mode_t mode) +{ + char *logfile; + int fd = -1; + + if (virAsprintf(&logfile, "%s/%s.log", driver->logDir, vm->def->name) < 0) { + virReportOOMError(); + return -1; + } + + if ((fd = open(logfile, flags, mode)) < 0) { + virReportSystemError(errno, _("failed to create logfile %s"), + logfile); + goto cleanup; + } + if (virSetCloseExec(fd) < 0) { + virReportSystemError(errno, _("failed to set close-on-exec flag on %s"), + logfile); + VIR_FORCE_CLOSE(fd); + goto cleanup; + } + +cleanup: + VIR_FREE(logfile); + return fd; +} + + +int +qemuDomainCreateLog(struct qemud_driver *driver, virDomainObjPtr vm, bool append) +{ + int flags; + + flags = O_CREAT | O_WRONLY; + /* Only logrotate files in /var/log, so only append if running privileged */ + if (driver->privileged || append) + flags |= O_APPEND; + else + flags |= O_TRUNC; + + return qemuDomainOpenLogHelper(driver, vm, flags, S_IRUSR | S_IWUSR); +} + + +int +qemuDomainOpenLog(struct qemud_driver *driver, virDomainObjPtr vm, off_t pos) +{ + int fd; + off_t off; + int whence; + + if ((fd = qemuDomainOpenLogHelper(driver, vm, O_RDONLY, 0)) < 0) + return -1; + + if (pos < 0) { + off = 0; + whence = SEEK_END; + } else { + off = pos; + whence = SEEK_SET; + } + + if (lseek(fd, off, whence) < 0) { + virReportSystemError(pos < 0 ? 0 : errno, + _("Unable to seek to %lld from %s in log for %s"), + (long long)off, + whence == SEEK_END ? "end" : "start", + vm->def->name); + VIR_FORCE_CLOSE(fd); + } + + return fd; +} + + diff --git a/src/qemu/qemu_domain.h b/src/qemu/qemu_domain.h index 9f454f8..dfb25fb 100644 --- a/src/qemu/qemu_domain.h +++ b/src/qemu/qemu_domain.h @@ -128,4 +128,8 @@ void qemuDomainObjCheckNetTaint(struct qemud_driver *driver, virDomainObjPtr obj, virDomainNetDefPtr net); + +int qemuDomainCreateLog(struct qemud_driver *driver, virDomainObjPtr vm, bool append); +int qemuDomainOpenLog(struct qemud_driver *driver, virDomainObjPtr vm, off_t pos); + #endif /* __QEMU_DOMAIN_H__ */ diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 573336d..eca85ae 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -679,82 +679,6 @@ error: return ret; } -static int -qemuProcessLogFD(struct qemud_driver *driver, const char* name, bool append) -{ - char *logfile; - mode_t logmode; - int fd = -1; - - if (virAsprintf(&logfile, "%s/%s.log", driver->logDir, name) < 0) { - virReportOOMError(); - return -1; - } - - logmode = O_CREAT | O_WRONLY; - /* Only logrotate files in /var/log, so only append if running privileged */ - if (driver->privileged || append) - logmode |= O_APPEND; - else - logmode |= O_TRUNC; - - if ((fd = open(logfile, logmode, S_IRUSR | S_IWUSR)) < 0) { - virReportSystemError(errno, - _("failed to create logfile %s"), - logfile); - VIR_FREE(logfile); - return -1; - } - VIR_FREE(logfile); - if (virSetCloseExec(fd) < 0) { - virReportSystemError(errno, "%s", - _("Unable to set VM logfile close-on-exec flag")); - VIR_FORCE_CLOSE(fd); - return -1; - } - return fd; -} - - -static int -qemuProcessLogReadFD(const char* logDir, const char* name, off_t pos) -{ - char *logfile; - mode_t logmode = O_RDONLY; - int fd = -1; - - if (virAsprintf(&logfile, "%s/%s.log", logDir, name) < 0) { - qemuReportError(VIR_ERR_INTERNAL_ERROR, - _("failed to build logfile name %s/%s.log"), - logDir, name); - return -1; - } - - if ((fd = open(logfile, logmode)) < 0) { - virReportSystemError(errno, - _("failed to create logfile %s"), - logfile); - VIR_FREE(logfile); - return -1; - } - if (virSetCloseExec(fd) < 0) { - virReportSystemError(errno, "%s", - _("Unable to set VM logfile close-on-exec flag")); - VIR_FORCE_CLOSE(fd); - VIR_FREE(logfile); - return -1; - } - if (pos < 0 || lseek(fd, pos, SEEK_SET) < 0) { - virReportSystemError(pos < 0 ? 0 : errno, - _("Unable to seek to %lld in %s"), - (long long) pos, logfile); - VIR_FORCE_CLOSE(fd); - } - VIR_FREE(logfile); - return fd; -} - - typedef int qemuProcessLogHandleOutput(virDomainObjPtr vm, const char *output, int fd); @@ -1051,7 +975,7 @@ qemuProcessWaitForMonitor(struct qemud_driver* driver, virHashTablePtr paths = NULL; qemuDomainObjPrivatePtr priv; - if ((logfd = qemuProcessLogReadFD(driver->logDir, vm->def->name, pos)) < 0) + if ((logfd = qemuDomainOpenLog(driver, vm, pos)) < 0) return -1; if (VIR_ALLOC_N(buf, buf_size) < 0) { @@ -2198,7 +2122,7 @@ int qemuProcessStart(virConnectPtr conn, } VIR_DEBUG0("Creating domain log file"); - if ((logfile = qemuProcessLogFD(driver, vm->def->name, false)) < 0) + if ((logfile = qemuDomainCreateLog(driver, vm, false)) < 0) goto cleanup; VIR_DEBUG0("Determining emulator version"); @@ -2461,7 +2385,7 @@ void qemuProcessStop(struct qemud_driver *driver, return; } - if ((logfile = qemuProcessLogFD(driver, vm->def->name, true)) < 0) { + if ((logfile = qemuDomainCreateLog(driver, vm, true)) < 0) { /* To not break the normal domain shutdown process, skip the * timestamp log writing if failed on opening log file. */ VIR_WARN("Unable to open logfile: %s", -- 1.7.4.4 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list