2003-02-10 00:11:43 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003 Poul-Henning Kamp
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <inttypes.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <sys/queue.h>
|
|
|
|
#include <sys/sbuf.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <err.h>
|
|
|
|
#include <bsdxml.h>
|
|
|
|
#include <libgeom.h>
|
|
|
|
|
|
|
|
struct mystate {
|
|
|
|
struct gmesh *mesh;
|
|
|
|
struct gclass *class;
|
|
|
|
struct ggeom *geom;
|
|
|
|
struct gprovider *provider;
|
|
|
|
struct gconsumer *consumer;
|
|
|
|
int level;
|
|
|
|
struct sbuf *sbuf[20];
|
|
|
|
struct gconf *config;
|
2012-04-10 17:37:24 +00:00
|
|
|
int nident;
|
2012-10-26 12:46:33 +00:00
|
|
|
XML_Parser parser;
|
|
|
|
int error;
|
2003-02-10 00:11:43 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
static void
|
|
|
|
StartElement(void *userData, const char *name, const char **attr)
|
|
|
|
{
|
|
|
|
struct mystate *mt;
|
|
|
|
void *id;
|
|
|
|
void *ref;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
mt = userData;
|
|
|
|
mt->level++;
|
2008-08-09 11:14:05 +00:00
|
|
|
mt->sbuf[mt->level] = sbuf_new_auto();
|
2003-02-10 00:11:43 +00:00
|
|
|
id = NULL;
|
2004-03-09 21:14:18 +00:00
|
|
|
ref = NULL;
|
2003-02-10 00:11:43 +00:00
|
|
|
for (i = 0; attr[i] != NULL; i += 2) {
|
|
|
|
if (!strcmp(attr[i], "id")) {
|
2013-07-12 04:22:46 +00:00
|
|
|
id = (void *)strtoul(attr[i + 1], NULL, 0);
|
2012-04-10 17:37:24 +00:00
|
|
|
mt->nident++;
|
2003-02-10 00:11:43 +00:00
|
|
|
} else if (!strcmp(attr[i], "ref")) {
|
2013-07-12 04:22:46 +00:00
|
|
|
ref = (void *)strtoul(attr[i + 1], NULL, 0);
|
2003-02-10 00:11:43 +00:00
|
|
|
} else
|
|
|
|
printf("%*.*s[%s = %s]\n",
|
|
|
|
mt->level + 1, mt->level + 1, "",
|
|
|
|
attr[i], attr[i + 1]);
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "class") && mt->class == NULL) {
|
|
|
|
mt->class = calloc(1, sizeof *mt->class);
|
2008-07-08 17:34:50 +00:00
|
|
|
if (mt->class == NULL) {
|
2012-10-26 12:46:33 +00:00
|
|
|
mt->error = errno;
|
|
|
|
XML_StopParser(mt->parser, 0);
|
2008-07-08 17:34:50 +00:00
|
|
|
warn("Cannot allocate memory during processing of '%s' "
|
|
|
|
"element", name);
|
|
|
|
return;
|
|
|
|
}
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->class->lg_id = id;
|
|
|
|
LIST_INSERT_HEAD(&mt->mesh->lg_class, mt->class, lg_class);
|
|
|
|
LIST_INIT(&mt->class->lg_geom);
|
|
|
|
LIST_INIT(&mt->class->lg_config);
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "geom") && mt->geom == NULL) {
|
|
|
|
mt->geom = calloc(1, sizeof *mt->geom);
|
2008-07-08 17:34:50 +00:00
|
|
|
if (mt->geom == NULL) {
|
2012-10-26 12:46:33 +00:00
|
|
|
mt->error = errno;
|
|
|
|
XML_StopParser(mt->parser, 0);
|
2008-07-08 17:34:50 +00:00
|
|
|
warn("Cannot allocate memory during processing of '%s' "
|
|
|
|
"element", name);
|
|
|
|
return;
|
|
|
|
}
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->geom->lg_id = id;
|
|
|
|
LIST_INSERT_HEAD(&mt->class->lg_geom, mt->geom, lg_geom);
|
|
|
|
LIST_INIT(&mt->geom->lg_provider);
|
|
|
|
LIST_INIT(&mt->geom->lg_consumer);
|
|
|
|
LIST_INIT(&mt->geom->lg_config);
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "class") && mt->geom != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->geom->lg_class = ref;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "consumer") && mt->consumer == NULL) {
|
|
|
|
mt->consumer = calloc(1, sizeof *mt->consumer);
|
2008-07-08 17:34:50 +00:00
|
|
|
if (mt->consumer == NULL) {
|
2012-10-26 12:46:33 +00:00
|
|
|
mt->error = errno;
|
|
|
|
XML_StopParser(mt->parser, 0);
|
2008-07-08 17:34:50 +00:00
|
|
|
warn("Cannot allocate memory during processing of '%s' "
|
|
|
|
"element", name);
|
|
|
|
return;
|
|
|
|
}
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->consumer->lg_id = id;
|
|
|
|
LIST_INSERT_HEAD(&mt->geom->lg_consumer, mt->consumer,
|
|
|
|
lg_consumer);
|
|
|
|
LIST_INIT(&mt->consumer->lg_config);
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "geom") && mt->consumer != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->consumer->lg_geom = ref;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "provider") && mt->consumer != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->consumer->lg_provider = ref;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "provider") && mt->provider == NULL) {
|
|
|
|
mt->provider = calloc(1, sizeof *mt->provider);
|
2008-07-08 17:34:50 +00:00
|
|
|
if (mt->provider == NULL) {
|
2012-10-26 12:46:33 +00:00
|
|
|
mt->error = errno;
|
|
|
|
XML_StopParser(mt->parser, 0);
|
2008-07-08 17:34:50 +00:00
|
|
|
warn("Cannot allocate memory during processing of '%s' "
|
|
|
|
"element", name);
|
|
|
|
return;
|
|
|
|
}
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->provider->lg_id = id;
|
|
|
|
LIST_INSERT_HEAD(&mt->geom->lg_provider, mt->provider,
|
|
|
|
lg_provider);
|
|
|
|
LIST_INIT(&mt->provider->lg_consumers);
|
|
|
|
LIST_INIT(&mt->provider->lg_config);
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "geom") && mt->provider != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->provider->lg_geom = ref;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "config")) {
|
|
|
|
if (mt->provider != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->config = &mt->provider->lg_config;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mt->consumer != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->config = &mt->consumer->lg_config;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mt->geom != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->config = &mt->geom->lg_config;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (mt->class != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->config = &mt->class->lg_config;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
EndElement(void *userData, const char *name)
|
|
|
|
{
|
|
|
|
struct mystate *mt;
|
2015-03-26 12:17:47 +00:00
|
|
|
struct gconf *c;
|
2003-02-10 00:11:43 +00:00
|
|
|
struct gconfig *gc;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
mt = userData;
|
2012-10-26 12:46:33 +00:00
|
|
|
p = NULL;
|
|
|
|
if (sbuf_finish(mt->sbuf[mt->level]) == 0)
|
|
|
|
p = strdup(sbuf_data(mt->sbuf[mt->level]));
|
|
|
|
sbuf_delete(mt->sbuf[mt->level]);
|
|
|
|
mt->sbuf[mt->level] = NULL;
|
|
|
|
mt->level--;
|
2008-07-08 17:34:50 +00:00
|
|
|
if (p == NULL) {
|
2012-10-26 12:46:33 +00:00
|
|
|
mt->error = errno;
|
|
|
|
XML_StopParser(mt->parser, 0);
|
2008-07-08 17:34:50 +00:00
|
|
|
warn("Cannot allocate memory during processing of '%s' "
|
|
|
|
"element", name);
|
|
|
|
return;
|
|
|
|
}
|
2003-02-10 00:11:43 +00:00
|
|
|
if (strlen(p) == 0) {
|
|
|
|
free(p);
|
|
|
|
p = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name, "name")) {
|
|
|
|
if (mt->provider != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->provider->lg_name = p;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
} else if (mt->geom != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->geom->lg_name = p;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
} else if (mt->class != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->class->lg_name = p;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "rank") && mt->geom != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->geom->lg_rank = strtoul(p, NULL, 0);
|
2003-02-10 00:11:43 +00:00
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "mode") && mt->provider != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->provider->lg_mode = p;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "mode") && mt->consumer != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->consumer->lg_mode = p;
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "mediasize") && mt->provider != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->provider->lg_mediasize = strtoumax(p, NULL, 0);
|
2003-02-10 00:11:43 +00:00
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "sectorsize") && mt->provider != NULL) {
|
2004-03-09 21:14:18 +00:00
|
|
|
mt->provider->lg_sectorsize = strtoul(p, NULL, 0);
|
2003-02-10 00:11:43 +00:00
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
2010-01-17 06:20:30 +00:00
|
|
|
if (!strcmp(name, "stripesize") && mt->provider != NULL) {
|
|
|
|
mt->provider->lg_stripesize = strtoumax(p, NULL, 0);
|
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "stripeoffset") && mt->provider != NULL) {
|
|
|
|
mt->provider->lg_stripeoffset = strtoumax(p, NULL, 0);
|
|
|
|
free(p);
|
|
|
|
return;
|
|
|
|
}
|
2003-02-10 00:11:43 +00:00
|
|
|
|
|
|
|
if (!strcmp(name, "config")) {
|
|
|
|
mt->config = NULL;
|
2015-04-22 22:23:56 +00:00
|
|
|
free(p);
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-03-26 12:17:47 +00:00
|
|
|
if (mt->config != NULL || (!strcmp(name, "wither") &&
|
|
|
|
(mt->provider != NULL || mt->geom != NULL))) {
|
|
|
|
if (mt->config != NULL)
|
|
|
|
c = mt->config;
|
|
|
|
else if (mt->provider != NULL)
|
|
|
|
c = &mt->provider->lg_config;
|
|
|
|
else
|
|
|
|
c = &mt->geom->lg_config;
|
2008-07-08 17:34:50 +00:00
|
|
|
gc = calloc(1, sizeof *gc);
|
|
|
|
if (gc == NULL) {
|
2012-10-26 12:46:33 +00:00
|
|
|
mt->error = errno;
|
|
|
|
XML_StopParser(mt->parser, 0);
|
2008-07-08 17:34:50 +00:00
|
|
|
warn("Cannot allocate memory during processing of '%s' "
|
|
|
|
"element", name);
|
2015-08-28 06:41:40 +00:00
|
|
|
free(p);
|
2008-07-08 17:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2004-03-09 21:14:18 +00:00
|
|
|
gc->lg_name = strdup(name);
|
2008-07-08 17:34:50 +00:00
|
|
|
if (gc->lg_name == NULL) {
|
2012-10-26 12:46:33 +00:00
|
|
|
mt->error = errno;
|
|
|
|
XML_StopParser(mt->parser, 0);
|
2008-07-08 17:34:50 +00:00
|
|
|
warn("Cannot allocate memory during processing of '%s' "
|
|
|
|
"element", name);
|
2015-08-28 06:41:40 +00:00
|
|
|
free(gc);
|
|
|
|
free(p);
|
2008-07-08 17:34:50 +00:00
|
|
|
return;
|
|
|
|
}
|
2015-08-13 13:19:56 +00:00
|
|
|
gc->lg_val = p;
|
2015-03-26 12:17:47 +00:00
|
|
|
LIST_INSERT_HEAD(c, gc, lg_config);
|
2003-02-10 00:11:43 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p != NULL) {
|
2013-07-19 06:42:15 +00:00
|
|
|
#if DEBUG_LIBGEOM > 0
|
2003-03-17 08:22:48 +00:00
|
|
|
printf("Unexpected XML: name=%s data=\"%s\"\n", name, p);
|
2013-07-19 06:42:15 +00:00
|
|
|
#endif
|
2003-02-10 00:11:43 +00:00
|
|
|
free(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(name, "consumer") && mt->consumer != NULL) {
|
|
|
|
mt->consumer = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "provider") && mt->provider != NULL) {
|
|
|
|
mt->provider = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "geom") && mt->consumer != NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "geom") && mt->provider != NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "geom") && mt->geom != NULL) {
|
|
|
|
mt->geom = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "class") && mt->geom != NULL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (!strcmp(name, "class") && mt->class != NULL) {
|
|
|
|
mt->class = NULL;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
CharData(void *userData , const XML_Char *s , int len)
|
|
|
|
{
|
|
|
|
struct mystate *mt;
|
|
|
|
const char *b, *e;
|
|
|
|
|
|
|
|
mt = userData;
|
|
|
|
|
|
|
|
b = s;
|
|
|
|
e = s + len - 1;
|
|
|
|
while (isspace(*b) && b < e)
|
|
|
|
b++;
|
|
|
|
while (isspace(*e) && e > b)
|
|
|
|
e--;
|
|
|
|
if (e != b || (*b && !isspace(*b)))
|
|
|
|
sbuf_bcat(mt->sbuf[mt->level], b, e - b + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
struct gident *
|
2003-03-17 08:22:48 +00:00
|
|
|
geom_lookupid(struct gmesh *gmp, const void *id)
|
2003-02-10 00:11:43 +00:00
|
|
|
{
|
2012-04-10 17:37:24 +00:00
|
|
|
struct gident *gip;
|
Fix 32-bit libgeom consumers run on 64-bit kernels with COMPAT_FREEBSD32.
Kernel pointer values are used as opaque unique identifiers, which are then
used to reconstruct references between various providers, classes, etc., inside
libgeom from the source XML. Unfortunately, they're converted to pointer-width
integers (in the form of pointers) to do this, and 32-bit userland pointers
cannot hold sensible representations (however opaque) of 64-bit kernel pointers
on all systems.
In the case where the leading bits are zero and 32 distinct bits of pointer can
be identified, this will happen to work. On systems where the upper 32-bits of
kernel pointers are non-zero and the same for all kernel pointers, this will
result in double frees and all kinds of bizarre crashes and linkage between
objects inside libgeom.
To mitigate this problem, treat the opaque identifiers in the XML as C strings
instead, and internalize them to give unique and consistent per-object pointer
values in userland for each identifier in the XML. This allows us to keep the
libgeom logic the same with only minor changes to initial setup and parsing.
It might be more sensible for speed reasons to treat the identifiers as numbers
of a large size (uintmax_t, say) rather than strings, but strings seem fine for
now.
(As an added side-effect, this makes it slightly easier to identify unresolved
references, but nothing has been added to inform the user of those.)
2012-03-29 03:13:43 +00:00
|
|
|
|
2012-04-10 17:37:24 +00:00
|
|
|
for (gip = gmp->lg_ident; gip->lg_id != NULL; gip++)
|
|
|
|
if (gip->lg_id == id)
|
|
|
|
return (gip);
|
2003-02-10 00:11:43 +00:00
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
geom_xml2tree(struct gmesh *gmp, char *p)
|
|
|
|
{
|
|
|
|
XML_Parser parser;
|
|
|
|
struct mystate *mt;
|
|
|
|
struct gclass *cl;
|
|
|
|
struct ggeom *ge;
|
|
|
|
struct gprovider *pr;
|
|
|
|
struct gconsumer *co;
|
2012-10-26 12:46:33 +00:00
|
|
|
int error, i;
|
2003-02-10 00:11:43 +00:00
|
|
|
|
|
|
|
memset(gmp, 0, sizeof *gmp);
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_INIT(&gmp->lg_class);
|
2003-02-10 00:11:43 +00:00
|
|
|
parser = XML_ParserCreate(NULL);
|
2010-10-05 15:27:44 +00:00
|
|
|
if (parser == NULL)
|
|
|
|
return (ENOMEM);
|
2003-02-10 00:11:43 +00:00
|
|
|
mt = calloc(1, sizeof *mt);
|
2010-10-05 15:27:44 +00:00
|
|
|
if (mt == NULL) {
|
|
|
|
XML_ParserFree(parser);
|
2003-02-10 00:11:43 +00:00
|
|
|
return (ENOMEM);
|
2010-10-05 15:27:44 +00:00
|
|
|
}
|
2003-02-10 00:11:43 +00:00
|
|
|
mt->mesh = gmp;
|
2012-10-26 12:46:33 +00:00
|
|
|
mt->parser = parser;
|
|
|
|
error = 0;
|
2003-02-10 00:11:43 +00:00
|
|
|
XML_SetUserData(parser, mt);
|
|
|
|
XML_SetElementHandler(parser, StartElement, EndElement);
|
|
|
|
XML_SetCharacterDataHandler(parser, CharData);
|
|
|
|
i = XML_Parse(parser, p, strlen(p), 1);
|
2012-10-26 12:46:33 +00:00
|
|
|
if (mt->error != 0)
|
|
|
|
error = mt->error;
|
|
|
|
else if (i != 1) {
|
|
|
|
error = XML_GetErrorCode(parser) == XML_ERROR_NO_MEMORY ?
|
|
|
|
ENOMEM : EILSEQ;
|
|
|
|
}
|
2003-02-10 00:11:43 +00:00
|
|
|
XML_ParserFree(parser);
|
2012-10-26 12:46:33 +00:00
|
|
|
if (error != 0) {
|
2010-10-05 15:27:44 +00:00
|
|
|
free(mt);
|
2012-10-26 12:46:33 +00:00
|
|
|
return (error);
|
2010-10-05 15:27:44 +00:00
|
|
|
}
|
2012-04-10 17:37:24 +00:00
|
|
|
gmp->lg_ident = calloc(sizeof *gmp->lg_ident, mt->nident + 1);
|
Fix 32-bit libgeom consumers run on 64-bit kernels with COMPAT_FREEBSD32.
Kernel pointer values are used as opaque unique identifiers, which are then
used to reconstruct references between various providers, classes, etc., inside
libgeom from the source XML. Unfortunately, they're converted to pointer-width
integers (in the form of pointers) to do this, and 32-bit userland pointers
cannot hold sensible representations (however opaque) of 64-bit kernel pointers
on all systems.
In the case where the leading bits are zero and 32 distinct bits of pointer can
be identified, this will happen to work. On systems where the upper 32-bits of
kernel pointers are non-zero and the same for all kernel pointers, this will
result in double frees and all kinds of bizarre crashes and linkage between
objects inside libgeom.
To mitigate this problem, treat the opaque identifiers in the XML as C strings
instead, and internalize them to give unique and consistent per-object pointer
values in userland for each identifier in the XML. This allows us to keep the
libgeom logic the same with only minor changes to initial setup and parsing.
It might be more sensible for speed reasons to treat the identifiers as numbers
of a large size (uintmax_t, say) rather than strings, but strings seem fine for
now.
(As an added side-effect, this makes it slightly easier to identify unresolved
references, but nothing has been added to inform the user of those.)
2012-03-29 03:13:43 +00:00
|
|
|
free(mt);
|
2012-04-10 17:37:24 +00:00
|
|
|
if (gmp->lg_ident == NULL)
|
|
|
|
return (ENOMEM);
|
|
|
|
i = 0;
|
2003-02-10 00:11:43 +00:00
|
|
|
/* Collect all identifiers */
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
|
2012-04-10 17:37:24 +00:00
|
|
|
gmp->lg_ident[i].lg_id = cl->lg_id;
|
|
|
|
gmp->lg_ident[i].lg_ptr = cl;
|
|
|
|
gmp->lg_ident[i].lg_what = ISCLASS;
|
|
|
|
i++;
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
|
2012-04-10 17:37:24 +00:00
|
|
|
gmp->lg_ident[i].lg_id = ge->lg_id;
|
|
|
|
gmp->lg_ident[i].lg_ptr = ge;
|
|
|
|
gmp->lg_ident[i].lg_what = ISGEOM;
|
|
|
|
i++;
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
|
2012-04-10 17:37:24 +00:00
|
|
|
gmp->lg_ident[i].lg_id = pr->lg_id;
|
|
|
|
gmp->lg_ident[i].lg_ptr = pr;
|
|
|
|
gmp->lg_ident[i].lg_what = ISPROVIDER;
|
|
|
|
i++;
|
2003-02-10 00:11:43 +00:00
|
|
|
}
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
|
2012-04-10 17:37:24 +00:00
|
|
|
gmp->lg_ident[i].lg_id = co->lg_id;
|
|
|
|
gmp->lg_ident[i].lg_ptr = co;
|
|
|
|
gmp->lg_ident[i].lg_what = ISCONSUMER;
|
|
|
|
i++;
|
2003-02-10 00:11:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Substitute all identifiers */
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_FOREACH(cl, &gmp->lg_class, lg_class) {
|
|
|
|
LIST_FOREACH(ge, &cl->lg_geom, lg_geom) {
|
|
|
|
ge->lg_class =
|
|
|
|
geom_lookupid(gmp, ge->lg_class)->lg_ptr;
|
|
|
|
LIST_FOREACH(pr, &ge->lg_provider, lg_provider) {
|
|
|
|
pr->lg_geom =
|
|
|
|
geom_lookupid(gmp, pr->lg_geom)->lg_ptr;
|
2003-02-10 00:11:43 +00:00
|
|
|
}
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) {
|
|
|
|
co->lg_geom =
|
|
|
|
geom_lookupid(gmp, co->lg_geom)->lg_ptr;
|
|
|
|
if (co->lg_provider != NULL) {
|
|
|
|
co->lg_provider =
|
|
|
|
geom_lookupid(gmp,
|
|
|
|
co->lg_provider)->lg_ptr;
|
2004-03-08 16:37:08 +00:00
|
|
|
LIST_INSERT_HEAD(
|
2004-03-09 21:14:18 +00:00
|
|
|
&co->lg_provider->lg_consumers,
|
|
|
|
co, lg_consumers);
|
2004-03-08 16:37:08 +00:00
|
|
|
}
|
2003-02-10 00:11:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
geom_gettree(struct gmesh *gmp)
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
int error;
|
|
|
|
|
|
|
|
p = geom_getxml();
|
2005-05-24 10:10:38 +00:00
|
|
|
if (p == NULL)
|
|
|
|
return (errno);
|
2003-02-10 00:11:43 +00:00
|
|
|
error = geom_xml2tree(gmp, p);
|
|
|
|
free(p);
|
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
delete_config(struct gconf *gp)
|
|
|
|
{
|
|
|
|
struct gconfig *cf;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
cf = LIST_FIRST(gp);
|
|
|
|
if (cf == NULL)
|
|
|
|
return;
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_REMOVE(cf, lg_config);
|
|
|
|
free(cf->lg_name);
|
|
|
|
free(cf->lg_val);
|
2003-02-10 00:11:43 +00:00
|
|
|
free(cf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
geom_deletetree(struct gmesh *gmp)
|
|
|
|
{
|
|
|
|
struct gclass *cl;
|
|
|
|
struct ggeom *ge;
|
|
|
|
struct gprovider *pr;
|
|
|
|
struct gconsumer *co;
|
|
|
|
|
2004-03-09 21:14:18 +00:00
|
|
|
free(gmp->lg_ident);
|
|
|
|
gmp->lg_ident = NULL;
|
2003-02-10 00:11:43 +00:00
|
|
|
for (;;) {
|
2004-03-09 21:14:18 +00:00
|
|
|
cl = LIST_FIRST(&gmp->lg_class);
|
2003-02-10 00:11:43 +00:00
|
|
|
if (cl == NULL)
|
|
|
|
break;
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_REMOVE(cl, lg_class);
|
|
|
|
delete_config(&cl->lg_config);
|
|
|
|
if (cl->lg_name) free(cl->lg_name);
|
2003-02-10 00:11:43 +00:00
|
|
|
for (;;) {
|
2004-03-09 21:14:18 +00:00
|
|
|
ge = LIST_FIRST(&cl->lg_geom);
|
2003-02-10 00:11:43 +00:00
|
|
|
if (ge == NULL)
|
|
|
|
break;
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_REMOVE(ge, lg_geom);
|
|
|
|
delete_config(&ge->lg_config);
|
|
|
|
if (ge->lg_name) free(ge->lg_name);
|
2003-02-10 00:11:43 +00:00
|
|
|
for (;;) {
|
2004-03-09 21:14:18 +00:00
|
|
|
pr = LIST_FIRST(&ge->lg_provider);
|
2003-02-10 00:11:43 +00:00
|
|
|
if (pr == NULL)
|
|
|
|
break;
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_REMOVE(pr, lg_provider);
|
|
|
|
delete_config(&pr->lg_config);
|
|
|
|
if (pr->lg_name) free(pr->lg_name);
|
|
|
|
if (pr->lg_mode) free(pr->lg_mode);
|
2003-02-10 00:11:43 +00:00
|
|
|
free(pr);
|
|
|
|
}
|
|
|
|
for (;;) {
|
2004-03-09 21:14:18 +00:00
|
|
|
co = LIST_FIRST(&ge->lg_consumer);
|
2003-02-10 00:11:43 +00:00
|
|
|
if (co == NULL)
|
|
|
|
break;
|
2004-03-09 21:14:18 +00:00
|
|
|
LIST_REMOVE(co, lg_consumer);
|
|
|
|
delete_config(&co->lg_config);
|
|
|
|
if (co->lg_mode) free(co->lg_mode);
|
2003-02-10 00:11:43 +00:00
|
|
|
free(co);
|
|
|
|
}
|
|
|
|
free(ge);
|
|
|
|
}
|
|
|
|
free(cl);
|
|
|
|
}
|
|
|
|
}
|