mirror of https://gitee.com/openkylin/libvirt.git
Move qemuProcessLogReadFD and qemuProcessLogFD methods
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
This commit is contained in:
parent
718ac9b52f
commit
ce1b1f4186
|
@ -34,8 +34,10 @@
|
||||||
#include "cpu/cpu.h"
|
#include "cpu/cpu.h"
|
||||||
#include "ignore-value.h"
|
#include "ignore-value.h"
|
||||||
#include "uuid.h"
|
#include "uuid.h"
|
||||||
|
#include "files.h"
|
||||||
|
|
||||||
#include <sys/time.h>
|
#include <sys/time.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include <libxml/xpathInternals.h>
|
#include <libxml/xpathInternals.h>
|
||||||
|
|
||||||
|
@ -805,3 +807,87 @@ void qemuDomainObjCheckNetTaint(struct qemud_driver *driver,
|
||||||
net->data.bridge.script != NULL))
|
net->data.bridge.script != NULL))
|
||||||
qemuDomainObjTaint(driver, obj, VIR_DOMAIN_TAINT_SHELL_SCRIPTS);
|
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) {
|
||||||
|
if (whence == SEEK_END)
|
||||||
|
virReportSystemError(errno,
|
||||||
|
_("unable to seek to end of log for %s"),
|
||||||
|
vm->def->name);
|
||||||
|
else
|
||||||
|
virReportSystemError(errno,
|
||||||
|
_("unable to seek to %lld from start for %s"),
|
||||||
|
(long long)off, vm->def->name);
|
||||||
|
VIR_FORCE_CLOSE(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
return fd;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -128,4 +128,8 @@ void qemuDomainObjCheckNetTaint(struct qemud_driver *driver,
|
||||||
virDomainObjPtr obj,
|
virDomainObjPtr obj,
|
||||||
virDomainNetDefPtr net);
|
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__ */
|
#endif /* __QEMU_DOMAIN_H__ */
|
||||||
|
|
|
@ -679,82 +679,6 @@ error:
|
||||||
return ret;
|
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,
|
typedef int qemuProcessLogHandleOutput(virDomainObjPtr vm,
|
||||||
const char *output,
|
const char *output,
|
||||||
int fd);
|
int fd);
|
||||||
|
@ -1051,7 +975,7 @@ qemuProcessWaitForMonitor(struct qemud_driver* driver,
|
||||||
virHashTablePtr paths = NULL;
|
virHashTablePtr paths = NULL;
|
||||||
qemuDomainObjPrivatePtr priv;
|
qemuDomainObjPrivatePtr priv;
|
||||||
|
|
||||||
if ((logfd = qemuProcessLogReadFD(driver->logDir, vm->def->name, pos)) < 0)
|
if ((logfd = qemuDomainOpenLog(driver, vm, pos)) < 0)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if (VIR_ALLOC_N(buf, buf_size) < 0) {
|
if (VIR_ALLOC_N(buf, buf_size) < 0) {
|
||||||
|
@ -2198,7 +2122,7 @@ int qemuProcessStart(virConnectPtr conn,
|
||||||
}
|
}
|
||||||
|
|
||||||
VIR_DEBUG0("Creating domain log file");
|
VIR_DEBUG0("Creating domain log file");
|
||||||
if ((logfile = qemuProcessLogFD(driver, vm->def->name, false)) < 0)
|
if ((logfile = qemuDomainCreateLog(driver, vm, false)) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
VIR_DEBUG0("Determining emulator version");
|
VIR_DEBUG0("Determining emulator version");
|
||||||
|
@ -2461,7 +2385,7 @@ void qemuProcessStop(struct qemud_driver *driver,
|
||||||
return;
|
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
|
/* To not break the normal domain shutdown process, skip the
|
||||||
* timestamp log writing if failed on opening log file. */
|
* timestamp log writing if failed on opening log file. */
|
||||||
VIR_WARN("Unable to open logfile: %s",
|
VIR_WARN("Unable to open logfile: %s",
|
||||||
|
|
Loading…
Reference in New Issue