2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003 Poul-Henning Kamp
6 * Copyright (c) 2022 Alexander Motin <mav@FreeBSD.org>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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
35 #include <sys/types.h>
36 #include <sys/sysctl.h>
43 * Amount of extra space we allocate to try and anticipate the size of
46 #define GEOM_GETXML_SLACK 4096
49 * Number of times to retry in the face of the size of confxml exceeding
52 #define GEOM_GETXML_RETRIES 4
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.
59 #define GEOM_GETXML_BUFFER 65536
70 sizep = sizeof(mib) / sizeof(*mib);
71 if (sysctlnametomib("kern.geom.confxml", mib, &sizep) != 0)
73 if (sysctl(mib, sizep, NULL, &l, NULL, 0) != 0)
75 l += GEOM_GETXML_SLACK;
77 for (retries = 0; retries < GEOM_GETXML_RETRIES; retries++) {
81 if (sysctl(mib, sizep, p, &l, NULL, 0) == 0)
82 return (reallocf(p, strlen(p) + 1));
90 * Our buffer wasn't big enough. Make it bigger and
100 geom_getxml_geom(const char *class, const char *geom, int parents)
107 p = malloc(GEOM_GETXML_BUFFER);
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);
115 gctl_ro_param(r, "arg0", -1, geom);
118 gctl_ro_param(r, "nargs", sizeof(nargs), &nargs);
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') {
126 return (geom_getxml());