1 /*- 2 * Copyright (c) 2003 Poul-Henning Kamp 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. The names of the authors may not be used to endorse or promote 14 * products derived from this software without specific prior written 15 * permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 * $FreeBSD$ 30 */ 31 32 #include <stdio.h> 33 #include <inttypes.h> 34 #include <stdlib.h> 35 #include <string.h> 36 #include <unistd.h> 37 #include <errno.h> 38 #include <fcntl.h> 39 #include <ctype.h> 40 #include <sys/stat.h> 41 #include <sys/mman.h> 42 #include <sys/queue.h> 43 #include <sys/sbuf.h> 44 #include <sys/sysctl.h> 45 #include <err.h> 46 #include <bsdxml.h> 47 #include <libgeom.h> 48 49 struct mystate { 50 struct gmesh *mesh; 51 struct gclass *class; 52 struct ggeom *geom; 53 struct gprovider *provider; 54 struct gconsumer *consumer; 55 int level; 56 struct sbuf *sbuf[20]; 57 struct gconf *config; 58 int nident; 59 XML_Parser parser; 60 int error; 61 }; 62 63 static void 64 StartElement(void *userData, const char *name, const char **attr) 65 { 66 struct mystate *mt; 67 void *id; 68 void *ref; 69 int i; 70 71 mt = userData; 72 mt->level++; 73 mt->sbuf[mt->level] = sbuf_new_auto(); 74 id = NULL; 75 ref = NULL; 76 for (i = 0; attr[i] != NULL; i += 2) { 77 if (!strcmp(attr[i], "id")) { 78 id = (void *)strtoul(attr[i + 1], NULL, 0); 79 mt->nident++; 80 } else if (!strcmp(attr[i], "ref")) { 81 ref = (void *)strtoul(attr[i + 1], NULL, 0); 82 } else 83 printf("%*.*s[%s = %s]\n", 84 mt->level + 1, mt->level + 1, "", 85 attr[i], attr[i + 1]); 86 } 87 if (!strcmp(name, "class") && mt->class == NULL) { 88 mt->class = calloc(1, sizeof *mt->class); 89 if (mt->class == NULL) { 90 mt->error = errno; 91 XML_StopParser(mt->parser, 0); 92 warn("Cannot allocate memory during processing of '%s' " 93 "element", name); 94 return; 95 } 96 mt->class->lg_id = id; 97 LIST_INSERT_HEAD(&mt->mesh->lg_class, mt->class, lg_class); 98 LIST_INIT(&mt->class->lg_geom); 99 LIST_INIT(&mt->class->lg_config); 100 return; 101 } 102 if (!strcmp(name, "geom") && mt->geom == NULL) { 103 mt->geom = calloc(1, sizeof *mt->geom); 104 if (mt->geom == NULL) { 105 mt->error = errno; 106 XML_StopParser(mt->parser, 0); 107 warn("Cannot allocate memory during processing of '%s' " 108 "element", name); 109 return; 110 } 111 mt->geom->lg_id = id; 112 LIST_INSERT_HEAD(&mt->class->lg_geom, mt->geom, lg_geom); 113 LIST_INIT(&mt->geom->lg_provider); 114 LIST_INIT(&mt->geom->lg_consumer); 115 LIST_INIT(&mt->geom->lg_config); 116 return; 117 } 118 if (!strcmp(name, "class") && mt->geom != NULL) { 119 mt->geom->lg_class = ref; 120 return; 121 } 122 if (!strcmp(name, "consumer") && mt->consumer == NULL) { 123 mt->consumer = calloc(1, sizeof *mt->consumer); 124 if (mt->consumer == NULL) { 125 mt->error = errno; 126 XML_StopParser(mt->parser, 0); 127 warn("Cannot allocate memory during processing of '%s' " 128 "element", name); 129 return; 130 } 131 mt->consumer->lg_id = id; 132 LIST_INSERT_HEAD(&mt->geom->lg_consumer, mt->consumer, 133 lg_consumer); 134 LIST_INIT(&mt->consumer->lg_config); 135 return; 136 } 137 if (!strcmp(name, "geom") && mt->consumer != NULL) { 138 mt->consumer->lg_geom = ref; 139 return; 140 } 141 if (!strcmp(name, "provider") && mt->consumer != NULL) { 142 mt->consumer->lg_provider = ref; 143 return; 144 } 145 if (!strcmp(name, "provider") && mt->provider == NULL) { 146 mt->provider = calloc(1, sizeof *mt->provider); 147 if (mt->provider == NULL) { 148 mt->error = errno; 149 XML_StopParser(mt->parser, 0); 150 warn("Cannot allocate memory during processing of '%s' " 151 "element", name); 152 return; 153 } 154 mt->provider->lg_id = id; 155 LIST_INSERT_HEAD(&mt->geom->lg_provider, mt->provider, 156 lg_provider); 157 LIST_INIT(&mt->provider->lg_consumers); 158 LIST_INIT(&mt->provider->lg_config); 159 return; 160 } 161 if (!strcmp(name, "geom") && mt->provider != NULL) { 162 mt->provider->lg_geom = ref; 163 return; 164 } 165 if (!strcmp(name, "config")) { 166 if (mt->provider != NULL) { 167 mt->config = &mt->provider->lg_config; 168 return; 169 } 170 if (mt->consumer != NULL) { 171 mt->config = &mt->consumer->lg_config; 172 return; 173 } 174 if (mt->geom != NULL) { 175 mt->config = &mt->geom->lg_config; 176 return; 177 } 178 if (mt->class != NULL) { 179 mt->config = &mt->class->lg_config; 180 return; 181 } 182 } 183 } 184 185 static void 186 EndElement(void *userData, const char *name) 187 { 188 struct mystate *mt; 189 struct gconf *c; 190 struct gconfig *gc; 191 char *p; 192 193 mt = userData; 194 p = NULL; 195 if (sbuf_finish(mt->sbuf[mt->level]) == 0) 196 p = strdup(sbuf_data(mt->sbuf[mt->level])); 197 sbuf_delete(mt->sbuf[mt->level]); 198 mt->sbuf[mt->level] = NULL; 199 mt->level--; 200 if (p == NULL) { 201 mt->error = errno; 202 XML_StopParser(mt->parser, 0); 203 warn("Cannot allocate memory during processing of '%s' " 204 "element", name); 205 return; 206 } 207 if (strlen(p) == 0) { 208 free(p); 209 p = NULL; 210 } 211 212 if (!strcmp(name, "name")) { 213 if (mt->provider != NULL) { 214 mt->provider->lg_name = p; 215 return; 216 } else if (mt->geom != NULL) { 217 mt->geom->lg_name = p; 218 return; 219 } else if (mt->class != NULL) { 220 mt->class->lg_name = p; 221 return; 222 } 223 } 224 if (!strcmp(name, "rank") && mt->geom != NULL) { 225 mt->geom->lg_rank = strtoul(p, NULL, 0); 226 free(p); 227 return; 228 } 229 if (!strcmp(name, "mode") && mt->provider != NULL) { 230 mt->provider->lg_mode = p; 231 return; 232 } 233 if (!strcmp(name, "mode") && mt->consumer != NULL) { 234 mt->consumer->lg_mode = p; 235 return; 236 } 237 if (!strcmp(name, "mediasize") && mt->provider != NULL) { 238 mt->provider->lg_mediasize = strtoumax(p, NULL, 0); 239 free(p); 240 return; 241 } 242 if (!strcmp(name, "sectorsize") && mt->provider != NULL) { 243 mt->provider->lg_sectorsize = strtoul(p, NULL, 0); 244 free(p); 245 return; 246 } 247 if (!strcmp(name, "stripesize") && mt->provider != NULL) { 248 mt->provider->lg_stripesize = strtoumax(p, NULL, 0); 249 free(p); 250 return; 251 } 252 if (!strcmp(name, "stripeoffset") && mt->provider != NULL) { 253 mt->provider->lg_stripeoffset = strtoumax(p, NULL, 0); 254 free(p); 255 return; 256 } 257 258 if (!strcmp(name, "config")) { 259 mt->config = NULL; 260 return; 261 } 262 263 if (mt->config != NULL || (!strcmp(name, "wither") && 264 (mt->provider != NULL || mt->geom != NULL))) { 265 if (mt->config != NULL) 266 c = mt->config; 267 else if (mt->provider != NULL) 268 c = &mt->provider->lg_config; 269 else 270 c = &mt->geom->lg_config; 271 gc = calloc(1, sizeof *gc); 272 if (gc == NULL) { 273 mt->error = errno; 274 XML_StopParser(mt->parser, 0); 275 warn("Cannot allocate memory during processing of '%s' " 276 "element", name); 277 return; 278 } 279 gc->lg_name = strdup(name); 280 if (gc->lg_name == NULL) { 281 free(gc); 282 mt->error = errno; 283 XML_StopParser(mt->parser, 0); 284 warn("Cannot allocate memory during processing of '%s' " 285 "element", name); 286 return; 287 } 288 gc->lg_val = p ? p : strdup("1"); 289 LIST_INSERT_HEAD(c, gc, lg_config); 290 return; 291 } 292 293 if (p != NULL) { 294 #if DEBUG_LIBGEOM > 0 295 printf("Unexpected XML: name=%s data=\"%s\"\n", name, p); 296 #endif 297 free(p); 298 } 299 300 if (!strcmp(name, "consumer") && mt->consumer != NULL) { 301 mt->consumer = NULL; 302 return; 303 } 304 if (!strcmp(name, "provider") && mt->provider != NULL) { 305 mt->provider = NULL; 306 return; 307 } 308 if (!strcmp(name, "geom") && mt->consumer != NULL) { 309 return; 310 } 311 if (!strcmp(name, "geom") && mt->provider != NULL) { 312 return; 313 } 314 if (!strcmp(name, "geom") && mt->geom != NULL) { 315 mt->geom = NULL; 316 return; 317 } 318 if (!strcmp(name, "class") && mt->geom != NULL) { 319 return; 320 } 321 if (!strcmp(name, "class") && mt->class != NULL) { 322 mt->class = NULL; 323 return; 324 } 325 } 326 327 static void 328 CharData(void *userData , const XML_Char *s , int len) 329 { 330 struct mystate *mt; 331 const char *b, *e; 332 333 mt = userData; 334 335 b = s; 336 e = s + len - 1; 337 while (isspace(*b) && b < e) 338 b++; 339 while (isspace(*e) && e > b) 340 e--; 341 if (e != b || (*b && !isspace(*b))) 342 sbuf_bcat(mt->sbuf[mt->level], b, e - b + 1); 343 } 344 345 struct gident * 346 geom_lookupid(struct gmesh *gmp, const void *id) 347 { 348 struct gident *gip; 349 350 for (gip = gmp->lg_ident; gip->lg_id != NULL; gip++) 351 if (gip->lg_id == id) 352 return (gip); 353 return (NULL); 354 } 355 356 int 357 geom_xml2tree(struct gmesh *gmp, char *p) 358 { 359 XML_Parser parser; 360 struct mystate *mt; 361 struct gclass *cl; 362 struct ggeom *ge; 363 struct gprovider *pr; 364 struct gconsumer *co; 365 int error, i; 366 367 memset(gmp, 0, sizeof *gmp); 368 LIST_INIT(&gmp->lg_class); 369 parser = XML_ParserCreate(NULL); 370 if (parser == NULL) 371 return (ENOMEM); 372 mt = calloc(1, sizeof *mt); 373 if (mt == NULL) { 374 XML_ParserFree(parser); 375 return (ENOMEM); 376 } 377 mt->mesh = gmp; 378 mt->parser = parser; 379 error = 0; 380 XML_SetUserData(parser, mt); 381 XML_SetElementHandler(parser, StartElement, EndElement); 382 XML_SetCharacterDataHandler(parser, CharData); 383 i = XML_Parse(parser, p, strlen(p), 1); 384 if (mt->error != 0) 385 error = mt->error; 386 else if (i != 1) { 387 error = XML_GetErrorCode(parser) == XML_ERROR_NO_MEMORY ? 388 ENOMEM : EILSEQ; 389 } 390 XML_ParserFree(parser); 391 if (error != 0) { 392 free(mt); 393 return (error); 394 } 395 gmp->lg_ident = calloc(sizeof *gmp->lg_ident, mt->nident + 1); 396 free(mt); 397 if (gmp->lg_ident == NULL) 398 return (ENOMEM); 399 i = 0; 400 /* Collect all identifiers */ 401 LIST_FOREACH(cl, &gmp->lg_class, lg_class) { 402 gmp->lg_ident[i].lg_id = cl->lg_id; 403 gmp->lg_ident[i].lg_ptr = cl; 404 gmp->lg_ident[i].lg_what = ISCLASS; 405 i++; 406 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) { 407 gmp->lg_ident[i].lg_id = ge->lg_id; 408 gmp->lg_ident[i].lg_ptr = ge; 409 gmp->lg_ident[i].lg_what = ISGEOM; 410 i++; 411 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) { 412 gmp->lg_ident[i].lg_id = pr->lg_id; 413 gmp->lg_ident[i].lg_ptr = pr; 414 gmp->lg_ident[i].lg_what = ISPROVIDER; 415 i++; 416 } 417 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) { 418 gmp->lg_ident[i].lg_id = co->lg_id; 419 gmp->lg_ident[i].lg_ptr = co; 420 gmp->lg_ident[i].lg_what = ISCONSUMER; 421 i++; 422 } 423 } 424 } 425 /* Substitute all identifiers */ 426 LIST_FOREACH(cl, &gmp->lg_class, lg_class) { 427 LIST_FOREACH(ge, &cl->lg_geom, lg_geom) { 428 ge->lg_class = 429 geom_lookupid(gmp, ge->lg_class)->lg_ptr; 430 LIST_FOREACH(pr, &ge->lg_provider, lg_provider) { 431 pr->lg_geom = 432 geom_lookupid(gmp, pr->lg_geom)->lg_ptr; 433 } 434 LIST_FOREACH(co, &ge->lg_consumer, lg_consumer) { 435 co->lg_geom = 436 geom_lookupid(gmp, co->lg_geom)->lg_ptr; 437 if (co->lg_provider != NULL) { 438 co->lg_provider = 439 geom_lookupid(gmp, 440 co->lg_provider)->lg_ptr; 441 LIST_INSERT_HEAD( 442 &co->lg_provider->lg_consumers, 443 co, lg_consumers); 444 } 445 } 446 } 447 } 448 return (0); 449 } 450 451 int 452 geom_gettree(struct gmesh *gmp) 453 { 454 char *p; 455 int error; 456 457 p = geom_getxml(); 458 if (p == NULL) 459 return (errno); 460 error = geom_xml2tree(gmp, p); 461 free(p); 462 return (error); 463 } 464 465 static void 466 delete_config(struct gconf *gp) 467 { 468 struct gconfig *cf; 469 470 for (;;) { 471 cf = LIST_FIRST(gp); 472 if (cf == NULL) 473 return; 474 LIST_REMOVE(cf, lg_config); 475 free(cf->lg_name); 476 free(cf->lg_val); 477 free(cf); 478 } 479 } 480 481 void 482 geom_deletetree(struct gmesh *gmp) 483 { 484 struct gclass *cl; 485 struct ggeom *ge; 486 struct gprovider *pr; 487 struct gconsumer *co; 488 489 free(gmp->lg_ident); 490 gmp->lg_ident = NULL; 491 for (;;) { 492 cl = LIST_FIRST(&gmp->lg_class); 493 if (cl == NULL) 494 break; 495 LIST_REMOVE(cl, lg_class); 496 delete_config(&cl->lg_config); 497 if (cl->lg_name) free(cl->lg_name); 498 for (;;) { 499 ge = LIST_FIRST(&cl->lg_geom); 500 if (ge == NULL) 501 break; 502 LIST_REMOVE(ge, lg_geom); 503 delete_config(&ge->lg_config); 504 if (ge->lg_name) free(ge->lg_name); 505 for (;;) { 506 pr = LIST_FIRST(&ge->lg_provider); 507 if (pr == NULL) 508 break; 509 LIST_REMOVE(pr, lg_provider); 510 delete_config(&pr->lg_config); 511 if (pr->lg_name) free(pr->lg_name); 512 if (pr->lg_mode) free(pr->lg_mode); 513 free(pr); 514 } 515 for (;;) { 516 co = LIST_FIRST(&ge->lg_consumer); 517 if (co == NULL) 518 break; 519 LIST_REMOVE(co, lg_consumer); 520 delete_config(&co->lg_config); 521 if (co->lg_mode) free(co->lg_mode); 522 free(co); 523 } 524 free(ge); 525 } 526 free(cl); 527 } 528 } 529