#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>
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) {
+ 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;
+}
+
+
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);
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) {
}
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");
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",