1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2003 Poul-Henning Kamp 5 * All rights reserved. 6 * Copyright (c) 2022 Alexander Motin <mav@FreeBSD.org> 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The names of the authors may not be used to endorse or promote 17 * products derived from this software without specific prior written 18 * permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30 * SUCH DAMAGE. 31 * 32 * $FreeBSD$ 33 */ 34 35 #include <sys/types.h> 36 #include <sys/sysctl.h> 37 #include <errno.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include "libgeom.h" 41 42 /* 43 * Amount of extra space we allocate to try and anticipate the size of 44 * confxml. 45 */ 46 #define GEOM_GETXML_SLACK 4096 47 48 /* 49 * Number of times to retry in the face of the size of confxml exceeding 50 * that of our buffer. 51 */ 52 #define GEOM_GETXML_RETRIES 4 53 54 /* 55 * Size of confxml buffer to request via getxml control request. It is 56 * expected to be sufficient for single geom and its parents. In case of 57 * overflow fall back to requesting full confxml via sysctl interface. 58 */ 59 #define GEOM_GETXML_BUFFER 65536 60 61 char * 62 geom_getxml(void) 63 { 64 char *p; 65 size_t l = 0; 66 int mib[3]; 67 size_t sizep; 68 int retries; 69 70 sizep = sizeof(mib) / sizeof(*mib); 71 if (sysctlnametomib("kern.geom.confxml", mib, &sizep) != 0) 72 return (NULL); 73 if (sysctl(mib, sizep, NULL, &l, NULL, 0) != 0) 74 return (NULL); 75 l += GEOM_GETXML_SLACK; 76 77 for (retries = 0; retries < GEOM_GETXML_RETRIES; retries++) { 78 p = malloc(l); 79 if (p == NULL) 80 return (NULL); 81 if (sysctl(mib, sizep, p, &l, NULL, 0) == 0) 82 return (reallocf(p, strlen(p) + 1)); 83 84 free(p); 85 86 if (errno != ENOMEM) 87 return (NULL); 88 89 /* 90 * Our buffer wasn't big enough. Make it bigger and 91 * try again. 92 */ 93 l *= 2; 94 } 95 96 return (NULL); 97 } 98 99 char * 100 geom_getxml_geom(const char *class, const char *geom, int parents) 101 { 102 struct gctl_req *r; 103 char *p; 104 const char *errstr; 105 int nargs = 0; 106 107 p = malloc(GEOM_GETXML_BUFFER); 108 if (p == NULL) 109 return (NULL); 110 r = gctl_get_handle(); 111 gctl_ro_param(r, "class", -1, class); 112 gctl_ro_param(r, "verb", -1, "getxml"); 113 gctl_ro_param(r, "parents", sizeof(parents), &parents); 114 if (geom) { 115 gctl_ro_param(r, "arg0", -1, geom); 116 nargs = 1; 117 } 118 gctl_ro_param(r, "nargs", sizeof(nargs), &nargs); 119 p[0] = '\0'; 120 gctl_add_param(r, "output", GEOM_GETXML_BUFFER, p, 121 GCTL_PARAM_WR | GCTL_PARAM_ASCII); 122 errstr = gctl_issue(r); 123 if (errstr != NULL && errstr[0] != '\0') { 124 gctl_free(r); 125 free(p); 126 return (geom_getxml()); 127 } 128 gctl_free(r); 129 return (p); 130 } 131