xref: /freebsd/lib/libgeom/geom_getxml.c (revision 2a63c3be158216222d89a073dcbd6a72ee4aab5a)
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 
33 #include <sys/types.h>
34 #include <sys/sysctl.h>
35 #include <errno.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include "libgeom.h"
39 
40 /*
41  * Amount of extra space we allocate to try and anticipate the size of
42  * confxml.
43  */
44 #define	GEOM_GETXML_SLACK	4096
45 
46 /*
47  * Number of times to retry in the face of the size of confxml exceeding
48  * that of our buffer.
49  */
50 #define	GEOM_GETXML_RETRIES	4
51 
52 /*
53  * Size of confxml buffer to request via getxml control request.  It is
54  * expected to be sufficient for single geom and its parents.  In case of
55  * overflow fall back to requesting full confxml via sysctl interface.
56  */
57 #define	GEOM_GETXML_BUFFER	65536
58 
59 char *
60 geom_getxml(void)
61 {
62 	char *p;
63 	size_t l = 0;
64 	int mib[3];
65 	size_t sizep;
66 	int retries;
67 
68 	sizep = sizeof(mib) / sizeof(*mib);
69 	if (sysctlnametomib("kern.geom.confxml", mib, &sizep) != 0)
70 		return (NULL);
71 	if (sysctl(mib, sizep, NULL, &l, NULL, 0) != 0)
72 		return (NULL);
73 	l += GEOM_GETXML_SLACK;
74 
75 	for (retries = 0; retries < GEOM_GETXML_RETRIES; retries++) {
76 		p = malloc(l);
77 		if (p == NULL)
78 			return (NULL);
79 		if (sysctl(mib, sizep, p, &l, NULL, 0) == 0)
80 			return (reallocf(p, strlen(p) + 1));
81 
82 		free(p);
83 
84 		if (errno != ENOMEM)
85 			return (NULL);
86 
87 		/*
88 		 * Our buffer wasn't big enough. Make it bigger and
89 		 * try again.
90 		 */
91 		l *= 2;
92 	}
93 
94 	return (NULL);
95 }
96 
97 char *
98 geom_getxml_geom(const char *class, const char *geom, int parents)
99 {
100 	struct gctl_req *r;
101 	char *p;
102 	const char *errstr;
103 	int nargs = 0;
104 
105 	p = malloc(GEOM_GETXML_BUFFER);
106 	if (p == NULL)
107 		return (NULL);
108 	r = gctl_get_handle();
109 	gctl_ro_param(r, "class", -1, class);
110 	gctl_ro_param(r, "verb", -1, "getxml");
111 	gctl_ro_param(r, "parents", sizeof(parents), &parents);
112 	if (geom) {
113 		gctl_ro_param(r, "arg0", -1, geom);
114 		nargs = 1;
115 	}
116 	gctl_ro_param(r, "nargs", sizeof(nargs), &nargs);
117 	p[0] = '\0';
118 	gctl_add_param(r, "output", GEOM_GETXML_BUFFER, p,
119 	    GCTL_PARAM_WR | GCTL_PARAM_ASCII);
120 	errstr = gctl_issue(r);
121 	if (errstr != NULL && errstr[0] != '\0') {
122 		gctl_free(r);
123 		free(p);
124 		return (geom_getxml());
125 	}
126 	gctl_free(r);
127 	return (p);
128 }
129