xref: /freebsd/lib/libgeom/geom_ctl.c (revision 1e413cf93298b5b97441a21d9a50fdcd0ee9945e)
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 <fcntl.h>
34 #include <errno.h>
35 #include <stdint.h>
36 #include <sys/types.h>
37 #include <stdarg.h>
38 #include <unistd.h>
39 #include <string.h>
40 #include <stdlib.h>
41 #include <paths.h>
42 
43 #include <sys/queue.h>
44 
45 #define GCTL_TABLE 1
46 #include <libgeom.h>
47 
48 void
49 gctl_dump(struct gctl_req *req, FILE *f)
50 {
51 	u_int i;
52 	int j;
53 	struct gctl_req_arg *ap;
54 
55 	if (req == NULL) {
56 		fprintf(f, "Dump of gctl request at NULL\n");
57 		return;
58 	}
59 	fprintf(f, "Dump of gctl request at %p:\n", req);
60 	if (req->error != NULL)
61 		fprintf(f, "  error:\t\"%s\"\n", req->error);
62 	else
63 		fprintf(f, "  error:\tNULL\n");
64 	for (i = 0; i < req->narg; i++) {
65 		ap = &req->arg[i];
66 		fprintf(f, "  param:\t\"%s\" (%d)", ap->name, ap->nlen);
67 		fprintf(f, " [%s%s",
68 		    ap->flag & GCTL_PARAM_RD ? "R" : "",
69 		    ap->flag & GCTL_PARAM_WR ? "W" : "");
70 		fflush(f);
71 		if (ap->flag & GCTL_PARAM_ASCII)
72 			fprintf(f, "%d] = \"%s\"", ap->len, (char *)ap->value);
73 		else if (ap->len > 0) {
74 			fprintf(f, "%d] = ", ap->len);
75 			fflush(f);
76 			for (j = 0; j < ap->len; j++) {
77 				fprintf(f, " %02x", ((u_char *)ap->value)[j]);
78 			}
79 		} else {
80 			fprintf(f, "0] = %p", ap->value);
81 		}
82 		fprintf(f, "\n");
83 	}
84 }
85 
86 /*
87  * Set an error message, if one does not already exist.
88  */
89 static void
90 gctl_set_error(struct gctl_req *req, const char *error, ...)
91 {
92 	va_list ap;
93 
94 	if (req->error != NULL)
95 		return;
96 	va_start(ap, error);
97 	vasprintf(&req->error, error, ap);
98 	va_end(ap);
99 }
100 
101 /*
102  * Check that a malloc operation succeeded, and set a consistent error
103  * message if not.
104  */
105 static void
106 gctl_check_alloc(struct gctl_req *req, void *ptr)
107 {
108 	if (ptr != NULL)
109 		return;
110 	gctl_set_error(req, "Could not allocate memory");
111 	if (req->error == NULL)
112 		req->error = "Could not allocate memory";
113 }
114 
115 /*
116  * Allocate a new request handle of the specified type.
117  * XXX: Why bother checking the type ?
118  */
119 struct gctl_req *
120 gctl_get_handle(void)
121 {
122 	struct gctl_req *rp;
123 
124 	rp = calloc(1, sizeof *rp);
125 	return (rp);
126 }
127 
128 /*
129  * Allocate space for another argument.
130  */
131 static struct gctl_req_arg *
132 gctl_new_arg(struct gctl_req *req)
133 {
134 	struct gctl_req_arg *ap;
135 
136 	req->narg++;
137 	req->arg = realloc(req->arg, sizeof *ap * req->narg);
138 	gctl_check_alloc(req, req->arg);
139 	if (req->arg == NULL) {
140 		req->narg = 0;
141 		return (NULL);
142 	}
143 	ap = req->arg + (req->narg - 1);
144 	memset(ap, 0, sizeof *ap);
145 	return (ap);
146 }
147 
148 void
149 gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
150 {
151 	struct gctl_req_arg *ap;
152 
153 	if (req == NULL || req->error != NULL)
154 		return;
155 	ap = gctl_new_arg(req);
156 	if (ap == NULL)
157 		return;
158 	ap->name = strdup(name);
159 	gctl_check_alloc(req, ap->name);
160 	ap->nlen = strlen(ap->name) + 1;
161 	ap->value = __DECONST(void *, value);
162 	ap->flag = GCTL_PARAM_RD;
163 	if (len >= 0)
164 		ap->len = len;
165 	else if (len < 0) {
166 		ap->flag |= GCTL_PARAM_ASCII;
167 		ap->len = strlen(value) + 1;
168 	}
169 }
170 
171 void
172 gctl_rw_param(struct gctl_req *req, const char *name, int len, void* value)
173 {
174 	struct gctl_req_arg *ap;
175 
176 	if (req == NULL || req->error != NULL)
177 		return;
178 	ap = gctl_new_arg(req);
179 	if (ap == NULL)
180 		return;
181 	ap->name = strdup(name);
182 	gctl_check_alloc(req, ap->name);
183 	ap->nlen = strlen(ap->name) + 1;
184 	ap->value = value;
185 	ap->flag = GCTL_PARAM_RW;
186 	if (len >= 0)
187 		ap->len = len;
188 	else if (len < 0)
189 		ap->len = strlen(value) + 1;
190 }
191 
192 const char *
193 gctl_issue(struct gctl_req *req)
194 {
195 	int fd, error;
196 
197 	if (req == NULL)
198 		return ("NULL request pointer");
199 	if (req->error != NULL)
200 		return (req->error);
201 
202 	req->version = GCTL_VERSION;
203 	req->lerror = BUFSIZ;		/* XXX: arbitrary number */
204 	req->error = malloc(req->lerror);
205 	if (req->error == NULL) {
206 		gctl_check_alloc(req, req->error);
207 		return (req->error);
208 	}
209 	memset(req->error, 0, req->lerror);
210 	req->lerror--;
211 	fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
212 	if (fd < 0)
213 		return(strerror(errno));
214 	error = ioctl(fd, GEOM_CTL, req);
215 	close(fd);
216 	if (req->error[0] != '\0')
217 		return (req->error);
218 	if (error != 0)
219 		return(strerror(errno));
220 	return (NULL);
221 }
222 
223 void
224 gctl_free(struct gctl_req *req)
225 {
226 	u_int i;
227 
228 	if (req == NULL)
229 		return;
230 	for (i = 0; i < req->narg; i++) {
231 		if (req->arg[i].name != NULL)
232 			free(req->arg[i].name);
233 	}
234 	free(req->arg);
235 	if (req->error != NULL)
236 		free(req->error);
237 	free(req);
238 }
239