xen: Move xenParseSxprChar to xen_common

It's the only place where it's used.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
This commit is contained in:
Peter Krempa 2019-07-03 09:44:51 +02:00
parent 5998b3c526
commit 8efbee4ba2
4 changed files with 146 additions and 149 deletions

View File

@ -5,7 +5,6 @@
# xenconfig/xen_sxpr.h
xenGetDomIdFromSxpr;
xenGetDomIdFromSxprString;
xenParseSxprChar;
# xenconfig/xen_xm.h
xenFormatXM;

View File

@ -763,6 +763,152 @@ xenParseVfb(virConfPtr conf, virDomainDefPtr def)
}
/**
* xenParseSxprChar:
* @value: A string describing a character device.
* @tty: the console pty path
*
* Parse the xend S-expression for description of a character device.
*
* Returns a character device object or NULL in case of failure.
*/
static virDomainChrDefPtr
xenParseSxprChar(const char *value,
const char *tty)
{
const char *prefix;
char *tmp;
virDomainChrDefPtr def;
if (!(def = virDomainChrDefNew(NULL)))
return NULL;
prefix = value;
if (value[0] == '/') {
def->source->type = VIR_DOMAIN_CHR_TYPE_DEV;
if (VIR_STRDUP(def->source->data.file.path, value) < 0)
goto error;
} else {
if ((tmp = strchr(value, ':')) != NULL) {
*tmp = '\0';
value = tmp + 1;
}
if (STRPREFIX(prefix, "telnet")) {
def->source->type = VIR_DOMAIN_CHR_TYPE_TCP;
def->source->data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
} else {
if ((def->source->type = virDomainChrTypeFromString(prefix)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unknown chr device type '%s'"), prefix);
goto error;
}
}
}
switch (def->source->type) {
case VIR_DOMAIN_CHR_TYPE_PTY:
if (VIR_STRDUP(def->source->data.file.path, tty) < 0)
goto error;
break;
case VIR_DOMAIN_CHR_TYPE_FILE:
case VIR_DOMAIN_CHR_TYPE_PIPE:
if (VIR_STRDUP(def->source->data.file.path, value) < 0)
goto error;
break;
case VIR_DOMAIN_CHR_TYPE_TCP:
{
const char *offset = strchr(value, ':');
const char *offset2;
if (offset == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("malformed char device string"));
goto error;
}
if (offset != value &&
VIR_STRNDUP(def->source->data.tcp.host, value, offset - value) < 0)
goto error;
offset2 = strchr(offset, ',');
offset++;
if (VIR_STRNDUP(def->source->data.tcp.service, offset,
offset2 ? offset2 - offset : -1) < 0)
goto error;
if (offset2 && strstr(offset2, ",server"))
def->source->data.tcp.listen = true;
}
break;
case VIR_DOMAIN_CHR_TYPE_UDP:
{
const char *offset = strchr(value, ':');
const char *offset2, *offset3;
if (offset == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("malformed char device string"));
goto error;
}
if (offset != value &&
VIR_STRNDUP(def->source->data.udp.connectHost, value, offset - value) < 0)
goto error;
offset2 = strchr(offset, '@');
if (offset2 != NULL) {
if (VIR_STRNDUP(def->source->data.udp.connectService,
offset + 1, offset2 - offset - 1) < 0)
goto error;
offset3 = strchr(offset2, ':');
if (offset3 == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("malformed char device string"));
goto error;
}
if (offset3 > (offset2 + 1) &&
VIR_STRNDUP(def->source->data.udp.bindHost,
offset2 + 1, offset3 - offset2 - 1) < 0)
goto error;
if (VIR_STRDUP(def->source->data.udp.bindService, offset3 + 1) < 0)
goto error;
} else {
if (VIR_STRDUP(def->source->data.udp.connectService, offset + 1) < 0)
goto error;
}
}
break;
case VIR_DOMAIN_CHR_TYPE_UNIX:
{
const char *offset = strchr(value, ',');
if (VIR_STRNDUP(def->source->data.nix.path, value,
offset ? offset - value : -1) < 0)
goto error;
if (offset != NULL &&
strstr(offset, ",server") != NULL)
def->source->data.nix.listen = true;
}
break;
}
return def;
error:
virDomainChrDefFree(def);
return NULL;
}
static int
xenParseCharDev(virConfPtr conf, virDomainDefPtr def, const char *nativeFormat)
{

View File

@ -62,149 +62,3 @@ int xenGetDomIdFromSxpr(const struct sexpr *root, int *id)
*id = tmp ? sexpr_int(root, "domain/domid") : -1;
return 0;
}
/**
* xenParseSxprChar:
* @value: A string describing a character device.
* @tty: the console pty path
*
* Parse the xend S-expression for description of a character device.
*
* Returns a character device object or NULL in case of failure.
*/
virDomainChrDefPtr
xenParseSxprChar(const char *value,
const char *tty)
{
const char *prefix;
char *tmp;
virDomainChrDefPtr def;
if (!(def = virDomainChrDefNew(NULL)))
return NULL;
prefix = value;
if (value[0] == '/') {
def->source->type = VIR_DOMAIN_CHR_TYPE_DEV;
if (VIR_STRDUP(def->source->data.file.path, value) < 0)
goto error;
} else {
if ((tmp = strchr(value, ':')) != NULL) {
*tmp = '\0';
value = tmp + 1;
}
if (STRPREFIX(prefix, "telnet")) {
def->source->type = VIR_DOMAIN_CHR_TYPE_TCP;
def->source->data.tcp.protocol = VIR_DOMAIN_CHR_TCP_PROTOCOL_TELNET;
} else {
if ((def->source->type = virDomainChrTypeFromString(prefix)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("unknown chr device type '%s'"), prefix);
goto error;
}
}
}
switch (def->source->type) {
case VIR_DOMAIN_CHR_TYPE_PTY:
if (VIR_STRDUP(def->source->data.file.path, tty) < 0)
goto error;
break;
case VIR_DOMAIN_CHR_TYPE_FILE:
case VIR_DOMAIN_CHR_TYPE_PIPE:
if (VIR_STRDUP(def->source->data.file.path, value) < 0)
goto error;
break;
case VIR_DOMAIN_CHR_TYPE_TCP:
{
const char *offset = strchr(value, ':');
const char *offset2;
if (offset == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("malformed char device string"));
goto error;
}
if (offset != value &&
VIR_STRNDUP(def->source->data.tcp.host, value, offset - value) < 0)
goto error;
offset2 = strchr(offset, ',');
offset++;
if (VIR_STRNDUP(def->source->data.tcp.service, offset,
offset2 ? offset2 - offset : -1) < 0)
goto error;
if (offset2 && strstr(offset2, ",server"))
def->source->data.tcp.listen = true;
}
break;
case VIR_DOMAIN_CHR_TYPE_UDP:
{
const char *offset = strchr(value, ':');
const char *offset2, *offset3;
if (offset == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("malformed char device string"));
goto error;
}
if (offset != value &&
VIR_STRNDUP(def->source->data.udp.connectHost, value, offset - value) < 0)
goto error;
offset2 = strchr(offset, '@');
if (offset2 != NULL) {
if (VIR_STRNDUP(def->source->data.udp.connectService,
offset + 1, offset2 - offset - 1) < 0)
goto error;
offset3 = strchr(offset2, ':');
if (offset3 == NULL) {
virReportError(VIR_ERR_INTERNAL_ERROR,
"%s", _("malformed char device string"));
goto error;
}
if (offset3 > (offset2 + 1) &&
VIR_STRNDUP(def->source->data.udp.bindHost,
offset2 + 1, offset3 - offset2 - 1) < 0)
goto error;
if (VIR_STRDUP(def->source->data.udp.bindService, offset3 + 1) < 0)
goto error;
} else {
if (VIR_STRDUP(def->source->data.udp.connectService, offset + 1) < 0)
goto error;
}
}
break;
case VIR_DOMAIN_CHR_TYPE_UNIX:
{
const char *offset = strchr(value, ',');
if (VIR_STRNDUP(def->source->data.nix.path, value,
offset ? offset - value : -1) < 0)
goto error;
if (offset != NULL &&
strstr(offset, ",server") != NULL)
def->source->data.nix.listen = true;
}
break;
}
return def;
error:
virDomainChrDefFree(def);
return NULL;
}

View File

@ -30,5 +30,3 @@
/* helper functions to get the dom id from a sexpr */
int xenGetDomIdFromSxprString(const char *sexpr, int *id);
int xenGetDomIdFromSxpr(const struct sexpr *root, int *id);
virDomainChrDefPtr xenParseSxprChar(const char *value, const char *tty);