diff options
| author | Bastian Wissler | 2008-10-17 10:52:20 +0200 |
|---|---|---|
| committer | Bastian Wissler | 2008-10-17 10:52:20 +0200 |
| commit | 2cc392c20e25602e575af67135e99e7fc5b8faf0 (patch) | |
| tree | ecb83abeba4c2226c9dd2e659c7be7a305077c39 | |
| parent | vmchooser version 0.0.4: * fixed horizontal scrollbar -> no extend symbols / ... (diff) | |
| download | vmchooser-2cc392c20e25602e575af67135e99e7fc5b8faf0.tar.gz vmchooser-2cc392c20e25602e575af67135e99e7fc5b8faf0.tar.xz vmchooser-2cc392c20e25602e575af67135e99e7fc5b8faf0.zip | |
vmchooser plugin: * added username / hostname generation to vmware xml
git-svn-id: http://svn.openslx.org/svn/openslx/openslx-src-tools/vmchooser/trunk@2353 95ad53e4-c205-0410-b2fa-d234c58c8868
| -rw-r--r-- | vmchooser/addPrinters.cxx | 90 | ||||
| -rw-r--r-- | vmchooser/addScanners.cxx | 1 | ||||
| -rw-r--r-- | vmchooser/inc/functions.h | 1 | ||||
| -rw-r--r-- | vmchooser/readXmlDir.cxx | 107 | ||||
| -rw-r--r-- | vmchooser/runImage.cxx | 19 |
5 files changed, 156 insertions, 62 deletions
diff --git a/vmchooser/addPrinters.cxx b/vmchooser/addPrinters.cxx index fd2197b..9505e90 100644 --- a/vmchooser/addPrinters.cxx +++ b/vmchooser/addPrinters.cxx @@ -6,6 +6,9 @@ #include <string> #include <vector> #include <queue> +#include <unistd.h> +#include <pwd.h> +#include <sys/types.h> /** * function addPrinters(xmlNode* node, char* script) @@ -122,9 +125,96 @@ bool addPrinters(xmlNode* node, char* script) { tindex = 0; } } + pclose(inp); return true; } fprintf(stderr, "Couldn't run \"%s\" script!", script); return false; } + +/****************************************** + * Adds user info and hostname to xml + * + * usernode: <username param="user" /> + * hostnamenode: <hostname param="host" /> + ******************************************/ +void addInfo(xmlNode* node) { + + if(node == NULL) { + return; + } + + bool user = false; + bool host = false; + + const int MAX_LENGTH = 200; + char hostname[MAX_LENGTH]; + uid_t id; + passwd *pwd; + + string strline; + xmlNodePtr cur = node->children; + xmlNodePtr usernode = NULL; + xmlNodePtr hostnamenode = NULL; + + // just use some standard Linux functions here ... + id = geteuid(); // gets effective user id + pwd = getpwuid(id); // gets passwd struct (including username) + gethostname(hostname, MAX_LENGTH-1); // gets hostname + + // Get <username> node and add "username#param" attribute + while(cur != NULL) { + if (!xmlStrcmp(cur->name, (const xmlChar *)"username")){ + user = true; + usernode = cur; + break; + } + cur = cur->next; + } + if(! user) { + usernode = xmlNewNode(NULL, (const xmlChar*) "username"); + if(usernode != NULL ) { + xmlNewProp(usernode, (const xmlChar*) "param", (const xmlChar*) pwd->pw_name); + xmlAddChild(node, usernode); + } + else { + cerr << "<username> node could not be created!" << endl; + } + } + else { + // set param attribute in <username> + xmlSetProp(usernode, (const xmlChar*) "param", (const xmlChar*) pwd->pw_name); + } + + cur = node->children; + + // Get <hostname> node and add "hostname#param" attribute + while(cur != NULL) { + if (!xmlStrcmp(cur->name, (const xmlChar *)"hostname")){ + host = true; + hostnamenode = cur; + break; + } + cur = cur->next; + } + if(! host) { + hostnamenode = xmlNewNode(NULL, (const xmlChar*) "hostname"); + if(hostnamenode != NULL ) { + xmlNewProp(hostnamenode, (const xmlChar*) "param", (const xmlChar*) hostname); + xmlAddChild(node, hostnamenode); + } + else { + cerr << "<hostname> node could not be created!" << endl; + } + } + else { + // add param value to existant hostname-node + xmlSetProp(hostnamenode, (const xmlChar*) "param", (xmlChar*) hostname); + } + + return; +} + + + diff --git a/vmchooser/addScanners.cxx b/vmchooser/addScanners.cxx index 360e9c7..3d3eb7f 100644 --- a/vmchooser/addScanners.cxx +++ b/vmchooser/addScanners.cxx @@ -122,6 +122,7 @@ bool addScanners(xmlNode* node, char* script) { tindex = 0; } } + pclose(inp); return true; } fprintf(stderr, "Couldn't run \"%s\" script!", script); diff --git a/vmchooser/inc/functions.h b/vmchooser/inc/functions.h index 238a70a..b4a1bbc 100644 --- a/vmchooser/inc/functions.h +++ b/vmchooser/inc/functions.h @@ -27,6 +27,7 @@ char* getFolderName(); /* Adds the elements into xmlNode "printers" (1. argument) */ bool addPrinters(xmlNode* node, char* script); bool addScanners(xmlNode* node, char* script); +void addInfo(xmlNode* node); /* This is defined in addPrinters.cxx */ /* Write configuration xml */ string writeConfXml(DataEntry& dat); diff --git a/vmchooser/readXmlDir.cxx b/vmchooser/readXmlDir.cxx index 985920e..de6afed 100644 --- a/vmchooser/readXmlDir.cxx +++ b/vmchooser/readXmlDir.cxx @@ -234,59 +234,60 @@ DataEntry** readXmlDir(char* path) char* fpath = getFolderName(); FILE* inp; - LIBXML_TEST_VERSION - if ( path== NULL) { - return NULL; - } - - if( (inp = popen(string(fpath).append("/") - .append(filterscript).append(" ") - .append(path).c_str(), "r" )) ) { - while(fgets(line, MAX_LENGTH, inp ) != NULL) { - xmlVec.push_back(string(line).substr(0,strlen(line)-1) ); - } - pclose(inp); - } - - xmlDoc *doc = NULL; - int c = 0; - string::size_type loc; - - DataEntry** result = (DataEntry**) malloc(xmlVec.size() * sizeof(DataEntry*) +1); - - for (unsigned int i=0; i < xmlVec.size(); i++) { - loc = xmlVec[i].find( "Vorlage" ); - if( loc != string::npos ) { - // FOUND Vorlage - continue; - } - - struct stat m; - stat(xmlVec[i].c_str(), &m); - - - /* DEBUG */ - //printf("File: %s, COUNT: %d\n", xmlVec[i].c_str(), xmlVec.size()); - if ( S_ISDIR(m.st_mode) ) { - continue; - } - - doc = xmlReadFile(xmlVec[i].c_str(), NULL, XML_PARSE_RECOVER); - if (doc == NULL) { - fprintf(stderr, "error: could not parse file %s\n", xmlVec[i].c_str()); - continue; - } - - result[c] = get_entry(doc); - if (result[c] != NULL) { - c++; - } - /* xmlDoc still needed to write back information for VMware etc. */ - // xmlFreeDoc(doc); - } - - result[c] = NULL; - return result; + LIBXML_TEST_VERSION + if ( path== NULL) { + return NULL; + } + + if( (inp = popen(string(fpath).append("/") + .append(filterscript).append(" ") + .append(path).c_str(), "r" )) ) { + while(fgets(line, MAX_LENGTH, inp ) != NULL) { + xmlVec.push_back(string(line).substr(0,strlen(line)-1) ); + } + pclose(inp); + } + free(fpath); + + xmlDoc *doc = NULL; + int c = 0; + string::size_type loc; + + DataEntry** result = (DataEntry**) malloc(xmlVec.size() * sizeof(DataEntry*) +1); + + for (unsigned int i=0; i < xmlVec.size(); i++) { + loc = xmlVec[i].find( "Vorlage" ); + if( loc != string::npos ) { + // FOUND Vorlage + continue; + } + + struct stat m; + stat(xmlVec[i].c_str(), &m); + + + /* DEBUG */ + //printf("File: %s, COUNT: %d\n", xmlVec[i].c_str(), xmlVec.size()); + if ( S_ISDIR(m.st_mode) ) { + continue; + } + + doc = xmlReadFile(xmlVec[i].c_str(), NULL, XML_PARSE_RECOVER); + if (doc == NULL) { + fprintf(stderr, "error: could not parse file %s\n", xmlVec[i].c_str()); + continue; + } + + result[c] = get_entry(doc); + if (result[c] != NULL) { + c++; + } + /* xmlDoc still needed to write back information for VMware etc. */ + // xmlFreeDoc(doc); + } + + result[c] = NULL; + return result; } diff --git a/vmchooser/runImage.cxx b/vmchooser/runImage.cxx index eba99a6..7810314 100644 --- a/vmchooser/runImage.cxx +++ b/vmchooser/runImage.cxx @@ -12,6 +12,9 @@ #include <string> #include <boost/regex.hpp> +/* define MAX_LENGTH for string in getFolderName */ +const int MAX_LENGTH = 200; + /** ************************************************************* * void runImage runs a (virtual machine) image using fork() @@ -83,8 +86,7 @@ string runImage(DataEntry& dat, string confxml) * Helper-Function: Get folder name */ char* getFolderName() { - const int MAX_LENGTH = 200; - + /* Var for the folder name */ char* pname = (char*) malloc(MAX_LENGTH); int result; @@ -136,21 +138,20 @@ string writeConfXml(DataEntry& dat) { // add "printers" and "scanners" - XML-Nodes addPrinters(root, (char*)pskript.c_str()); - //char* pname = getFolderName(); pskript = pname + "/scanners.sh"; addScanners(root, (char*)pskript.c_str()); + + // add hostname and username information + addInfo(root); - //xmlSaveFile("-", dat.xml); + srand(time(NULL)); - string xmlfile; ostringstream i; i << "/tmp/run" << rand() << ".xml"; xmlfile = i.str(); - - //ofstream file("/tmp/debug", ios_base::app); - //file << xmlfile << rand()<< endl; - + + //xmlSaveFile("-", dat.xml); xmlSaveFile( (char*) xmlfile.c_str(), dat.xml); return xmlfile; } |
