Author: cwittich Date: Sat Aug 30 03:38:15 2008 New Revision: 35795
URL: http://svn.reactos.org/svn/reactos?rev=35795&view=rev Log: start rewriting sysreg2 with libvirt
Added: trunk/tools/sysreg2/ (with props) trunk/tools/sysreg2/console.c (with props) trunk/tools/sysreg2/makefile (with props) trunk/tools/sysreg2/sysreg.h (with props) trunk/tools/sysreg2/utils.c (with props) trunk/tools/sysreg2/virt.c (with props)
Propchange: trunk/tools/sysreg2/ ------------------------------------------------------------------------------ --- bugtraq:logregex (added) +++ bugtraq:logregex Sat Aug 30 03:38:15 2008 @@ -1,0 +1,2 @@ +([Ii]ssue|[Bb]ug)s? #?(\d+)(,? ?#?(\d+))*(,? ?(and |or )?#?(\d+))? +(\d+)
Propchange: trunk/tools/sysreg2/ ------------------------------------------------------------------------------ bugtraq:message = See issue #%BUGID% for more details.
Propchange: trunk/tools/sysreg2/ ------------------------------------------------------------------------------ bugtraq:url = http://www.reactos.org/bugzilla/show_bug.cgi?id=%BUGID%
Added: trunk/tools/sysreg2/console.c URL: http://svn.reactos.org/svn/reactos/trunk/tools/sysreg2/console.c?rev=35795&a... ============================================================================== --- trunk/tools/sysreg2/console.c (added) +++ trunk/tools/sysreg2/console.c [iso-8859-1] Sat Aug 30 03:38:15 2008 @@ -1,0 +1,94 @@ +#include "sysreg.h" +#include <termios.h> +#include <poll.h> + +void ProcessDebugData(const char* tty, int timeout) +{ + int ttyfd, i; + struct termios ttyattr, rawattr; + + if ((ttyfd = open(tty, O_NOCTTY | O_RDWR)) < 0) + { + printf("error opening tty\n"); + return; + } + + if (tcgetattr(STDIN_FILENO, &ttyattr) < 0) + return; + + rawattr = ttyattr; + rawattr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP + | IGNCR | ICRNL | IXON); + rawattr.c_lflag &= ~(ICANON | ECHO | ECHONL); + rawattr.c_oflag &= ~OPOST; + rawattr.c_cflag &= ~(CSIZE | PARENB); + rawattr.c_cflag |= CS8; + + if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &rawattr) < 0) + return; + + while (1) + { + int ret; + struct pollfd fds[] = { + { STDIN_FILENO, POLLIN, 0 }, + { ttyfd, POLLIN, 0 }, + }; + + ret = poll(fds, (sizeof(fds) / sizeof(struct pollfd)), timeout); + if (ret < 0) + { + if (errno == EINTR || errno == EAGAIN) + continue; + goto cleanup; + } + else if (ret == 0) + { + /* timeout */ + printf("timeout\n"); + goto cleanup; + } + + for (i=0; i<(sizeof(fds) / sizeof(struct pollfd)); i++) + { + if (!fds[i].revents) + continue; + if (fds[i].revents & POLLIN) + { + char buf[4096]; + int got, sent = 0; + + got = read(fds[i].fd, buf, sizeof(buf)); + if (got < 0) + goto cleanup; + if (!got || got == 1 && buf[0] == '\33') + goto cleanup; + + if (fds[i].fd != STDIN_FILENO) + { + while (sent < got) + { + int done; + if ((done = safewrite(STDOUT_FILENO, + buf + sent, got -sent)) <= 0) + { + goto cleanup; + } + sent += done; + } + } + if (buf) + free(buf); + } + } + + } + + +cleanup: + tcsetattr(STDIN_FILENO, TCSAFLUSH, &ttyattr); + close(ttyfd); + + +} +
Propchange: trunk/tools/sysreg2/console.c ------------------------------------------------------------------------------ svn:eol-style = native
Added: trunk/tools/sysreg2/makefile URL: http://svn.reactos.org/svn/reactos/trunk/tools/sysreg2/makefile?rev=35795&am... ============================================================================== --- trunk/tools/sysreg2/makefile (added) +++ trunk/tools/sysreg2/makefile [iso-8859-1] Sat Aug 30 03:38:15 2008 @@ -1,0 +1,28 @@ +TARGET := sysreg2 + +.PHONY: all + +all: $(TARGET) + +CC=gcc +CFLAGS := -O2 -std=c99 -D_GNU_SOURCE +LFLAGS := -s -L/usr/lib64 +LIBS := -lgcc -lm -lvirt -ltasn1 -lz -lxml2 -lgnutls +INC := -I/usr/include/libvirt/ -I/usr/include/libxml2/ + +SRCS := virt.c utils.c console.c + +OBJS := $(SRCS:.c=.o) + +$(TARGET): $(OBJS) + $(CC) $(LFLAGS) -o $@ $(OBJS) $(LIBS) + +.c.o: $< + $(CC) $(INC) $(CFLAGS) -c $< -o $@ + + +.PHONY: clean +clean: + -@rm $(TARGET) + -@rm $(OBJS) +
Propchange: trunk/tools/sysreg2/makefile ------------------------------------------------------------------------------ svn:eol-style = native
Added: trunk/tools/sysreg2/sysreg.h URL: http://svn.reactos.org/svn/reactos/trunk/tools/sysreg2/sysreg.h?rev=35795&am... ============================================================================== --- trunk/tools/sysreg2/sysreg.h (added) +++ trunk/tools/sysreg2/sysreg.h [iso-8859-1] Sat Aug 30 03:38:15 2008 @@ -1,0 +1,15 @@ +#include <fcntl.h> +#include <stdio.h> +#include <stdlib.h> +#include <stdbool.h> +#include <string.h> +#include <unistd.h> +#include <errno.h> +#include <libxml/parser.h> +#include <libxml/tree.h> +#include <libxml/xpath.h> +#include <libxml/xpathInternals.h> + +char* ReadFile (const char *filename); +void ProcessDebugData(const char* tty, int timeout); +ssize_t safewrite(int fd, const void *buf, size_t count);
Propchange: trunk/tools/sysreg2/sysreg.h ------------------------------------------------------------------------------ svn:eol-style = native
Added: trunk/tools/sysreg2/utils.c URL: http://svn.reactos.org/svn/reactos/trunk/tools/sysreg2/utils.c?rev=35795&... ============================================================================== --- trunk/tools/sysreg2/utils.c (added) +++ trunk/tools/sysreg2/utils.c [iso-8859-1] Sat Aug 30 03:38:15 2008 @@ -1,0 +1,64 @@ +#include "sysreg.h" + +ssize_t safewrite(int fd, const void *buf, size_t count) +{ + size_t nwritten = 0; + while (count > 0) { + ssize_t r = write(fd, buf, count); + + if (r < 0 && errno == EINTR) + continue; + if (r < 0) + return r; + if (r == 0) + return nwritten; + buf = (const char *)buf + r; + count -= r; + nwritten += r; + } + return nwritten; +} + +char * ReadFile (const char *filename) +{ + char *buffer = NULL, *oldbuffer; + int len = 0, fd, r; + char b[1024]; + + fd = open (filename, O_RDONLY); + if (fd == -1) + return NULL; + + for (;;) { + r = read (fd, b, sizeof b); + if (r == -1) + { + if (buffer) free (buffer); + close(fd); + return NULL; + } + if (r == 0) break; /* end of file. */ + oldbuffer = buffer; + buffer = realloc (buffer, len+r); + if (buffer == NULL) { + /* out of memory */ + close(fd); + return NULL; + } + memcpy (buffer+len, b, r); + len += r; + } + + buffer = realloc (buffer, len+1); + if (buffer == NULL) + { + /* out of memory */ + close(fd); + return NULL; + } + buffer[len] = '\0'; + close (fd); + return buffer; +} + +
Propchange: trunk/tools/sysreg2/utils.c ------------------------------------------------------------------------------ svn:eol-style = native
Added: trunk/tools/sysreg2/virt.c URL: http://svn.reactos.org/svn/reactos/trunk/tools/sysreg2/virt.c?rev=35795&... ============================================================================== --- trunk/tools/sysreg2/virt.c (added) +++ trunk/tools/sysreg2/virt.c [iso-8859-1] Sat Aug 30 03:38:15 2008 @@ -1,0 +1,152 @@ +#include "sysreg.h" +#include <libvirt.h> + +const char* GetConsole(virDomainPtr vDomPtr) +{ + xmlDocPtr xml = NULL; + xmlXPathObjectPtr obj = NULL; + xmlXPathContextPtr ctxt = NULL; + char* XmlDoc; + const char* RetVal = NULL; + + XmlDoc = virDomainGetXMLDesc(vDomPtr, 0); + if (!XmlDoc) + return NULL; + + xml = xmlReadDoc((const xmlChar *) XmlDoc, "domain.xml", NULL, + XML_PARSE_NOENT | XML_PARSE_NONET | + XML_PARSE_NOWARNING); + free(XmlDoc); + if (!xml) + return NULL; + + ctxt = xmlXPathNewContext(xml); + if (!ctxt) + { + xmlFreeDoc(xml); + return NULL; + } + + obj = xmlXPathEval(BAD_CAST "string(/domain/devices/console/@tty)", ctxt); + if ((obj != NULL) && ((obj->type == XPATH_STRING) && + (obj->stringval != NULL) && (obj->stringval[0] != 0))) + { + RetVal = (const char*)obj->stringval; + } + + xmlFreeDoc(xml); + xmlXPathFreeContext(ctxt); + return RetVal; +} + +bool IsVirtualMachineRunning(virConnectPtr vConn, const char* name) +{ + char* names[10]; + int iDomains; + int iMaxnames = 10; + + iDomains = virConnectListDefinedDomains(vConn, names, iMaxnames); + if (iDomains != -1) + { + int i; + for(i=0; i<iDomains; i++) + { + if (strcmp(name, names[i]) == 0) + return true; + } + } + return false; +} + +virDomainPtr LaunchVirtualMachine(virConnectPtr vConn, const char* XmlFileName, const char* BootDevice) +{ + xmlDocPtr xml = NULL; + xmlXPathObjectPtr obj = NULL; + xmlXPathContextPtr ctxt = NULL; + char* XmlDoc; + char* buffer; + const char* name; + char* domname; + int len = 0; + + buffer = ReadFile(XmlFileName); + if (buffer == NULL) + return NULL; + + xml = xmlReadDoc((const xmlChar *) buffer, "domain.xml", NULL, + XML_PARSE_NOENT | XML_PARSE_NONET | + XML_PARSE_NOWARNING); + if (!xml) + return NULL; + + ctxt = xmlXPathNewContext(xml); + if (!ctxt) + return NULL; + + obj = xmlXPathEval(BAD_CAST "/domain/os/boot", ctxt); + if ((obj != NULL) && (obj->type == XPATH_NODESET) + && (obj->nodesetval != NULL) && (obj->nodesetval->nodeTab != NULL)) + { + xmlSetProp(obj->nodesetval->nodeTab[0], "dev", BootDevice); + } + + free(buffer); + xmlDocDumpMemory(xml, (xmlChar**) &buffer, &len); + xmlFreeDoc(xml); + xmlXPathFreeContext(ctxt); + xmlXPathFreeObject(obj); + + virDomainPtr vDomPtr = virDomainDefineXML(vConn, buffer); + xmlFree((xmlChar*)buffer); + if (vDomPtr) + { + if (virDomainCreate(vDomPtr) != 0) + { + virDomainUndefine(vDomPtr); + vDomPtr = NULL; + } + else + { + /* workaround a bug in libvirt */ + name = virDomainGetName(vDomPtr); + domname = strdup(name); + virDomainFree(vDomPtr); + vDomPtr = virDomainLookupByName(vConn, domname); + free(domname); + } + } + return vDomPtr; +} + + +int main() +{ + + virConnectPtr vConn; + virDomainPtr vDom; + + vConn = virConnectOpen("qemu:///session"); + + if (IsVirtualMachineRunning(vConn, "reactos")) + printf("Virtual Machine is already running\n"); + else + { + vDom = LaunchVirtualMachine(vConn, "/opt/buildbot/kvmtest/reactos.xml", "cdrom"); + { + if (vDom) + { + printf("Domain %s started.\n", virDomainGetName(vDom)); + printf("%s\n", GetConsole(vDom)); + ProcessDebugData(GetConsole(vDom), 10000 /*10 sec */); + + virDomainDestroy(vDom); + virDomainUndefine(vDom); + virDomainFree(vDom); + } + } + } + + virConnectClose(vConn); + return 1; +} +
Propchange: trunk/tools/sysreg2/virt.c ------------------------------------------------------------------------------ svn:eol-style = native