geom_ctl.c revision 112709
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: head/lib/libgeom/geom_ctl.c 112709 2003-03-27 14:35:00Z phk $
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#include <geom/geom_ext.h>
49
50void
51gctl_dump(struct gctl_req *req, FILE *f)
52{
53	u_int i;
54	int j;
55	struct gctl_req_arg *ap;
56
57	if (req == NULL) {
58		fprintf(f, "Dump of gctl request at NULL\n");
59		return;
60	}
61	fprintf(f, "Dump of gctl %s request at %p:\n", req->reqt->name, req);
62	if (req->error != NULL)
63		fprintf(f, "  error:\t\"%s\"\n", req->error);
64	else
65		fprintf(f, "  error:\tNULL\n");
66	for (i = 0; i < req->narg; i++) {
67		ap = &req->arg[i];
68		if (ap->name != NULL)
69			fprintf(f, "  param:\t\"%s\"", ap->name);
70		else
71			fprintf(f, "  meta:\t@%jd", (intmax_t)ap->offset);
72		fprintf(f, " [%s%s",
73		    ap->flag & GCTL_PARAM_RD ? "R" : "",
74		    ap->flag & GCTL_PARAM_WR ? "W" : "");
75		fflush(f);
76		if (ap->flag & GCTL_PARAM_ASCII)
77			fprintf(f, "%d] = \"%s\"", ap->len, (char *)ap->value);
78		else if (ap->len > 0) {
79			fprintf(f, "%d] = ", ap->len);
80			fflush(f);
81			for (j = 0; j < ap->len; j++) {
82				fprintf(f, " %02x", ((u_char *)ap->value)[j]);
83			}
84		} else {
85			fprintf(f, "0] = %p", ap->value);
86		}
87		fprintf(f, "\n");
88	}
89}
90
91/*
92 * Set an error message, if one does not already exist.
93 */
94static void
95gctl_set_error(struct gctl_req *req, const char *error, ...)
96{
97	va_list ap;
98
99	if (req->error != NULL)
100		return;
101	va_start(ap, error);
102	vasprintf(&req->error, error, ap);
103	va_end(ap);
104}
105
106/*
107 * Check that a malloc operation succeeded, and set a consistent error
108 * message if not.
109 */
110static void
111gctl_check_alloc(struct gctl_req *req, void *ptr)
112{
113	if (ptr != NULL)
114		return;
115	gctl_set_error(req, "Could not allocate memory");
116	if (req->error == NULL)
117		req->error = "Could not allocate memory";
118}
119
120/*
121 * Allocate a new request handle of the specified type.
122 * XXX: Why bother checking the type ?
123 */
124struct gctl_req *
125gctl_get_handle(enum gctl_request req)
126{
127	struct gctl_req_table *gtp;
128	struct gctl_req *rp;
129
130	rp = calloc(1, sizeof *rp);
131	if (rp == NULL)
132		return (NULL);
133	for (gtp = gcrt; gtp->request != req; gtp++)
134		if (gtp->request == GCTL_INVALID_REQUEST)
135			break;
136
137	rp->request = req;
138	rp->reqt = gtp;
139	if (rp->reqt->request == GCTL_INVALID_REQUEST)
140		gctl_set_error(rp, "Invalid request");
141	return (rp);
142}
143
144/*
145 * Allocate space for another argument.
146 */
147static struct gctl_req_arg *
148gctl_new_arg(struct gctl_req *req)
149{
150	struct gctl_req_arg *ap;
151
152	req->narg++;
153	req->arg = realloc(req->arg, sizeof *ap * req->narg);
154	gctl_check_alloc(req, req->arg);
155	if (req->arg == NULL) {
156		req->narg = 0;
157		return (NULL);
158	}
159	ap = req->arg + (req->narg - 1);
160	memset(ap, 0, sizeof *ap);
161	return (ap);
162}
163
164void
165gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
166{
167	struct gctl_req_arg *ap;
168
169	if (req == NULL || req->error != NULL)
170		return;
171	ap = gctl_new_arg(req);
172	if (ap == NULL)
173		return;
174	ap->name = strdup(name);
175	gctl_check_alloc(req, ap->name);
176	ap->nlen = strlen(ap->name) + 1;
177	ap->value = __DECONST(void *, value);
178	ap->flag = GCTL_PARAM_RD;
179	if (len >= 0)
180		ap->len = len;
181	else if (len < 0) {
182		ap->flag |= GCTL_PARAM_ASCII;
183		ap->len = strlen(value) + 1;
184	}
185}
186
187void
188gctl_rw_param(struct gctl_req *req, const char *name, int len, void* value)
189{
190	struct gctl_req_arg *ap;
191
192	if (req == NULL || req->error != NULL)
193		return;
194	ap = gctl_new_arg(req);
195	if (ap == NULL)
196		return;
197	ap->name = strdup(name);
198	gctl_check_alloc(req, ap->name);
199	ap->nlen = strlen(ap->name) + 1;
200	ap->value = value;
201	ap->flag = GCTL_PARAM_RW;
202	if (len >= 0)
203		ap->len = len;
204	else if (len < 0)
205		ap->len = strlen(value) + 1;
206}
207
208void
209gctl_ro_meta(struct gctl_req *req, off_t offset, u_int len, const void* value)
210{
211	struct gctl_req_arg *ap;
212
213	if (req == NULL || req->error != NULL)
214		return;
215	ap = gctl_new_arg(req);
216	if (ap == NULL)
217		return;
218	ap->value = __DECONST(void *, value);
219	ap->flag = GCTL_PARAM_RD;
220	ap->offset = offset;
221	ap->len = len;
222}
223
224void
225gctl_rw_meta(struct gctl_req *req, off_t offset, u_int len, void* value)
226{
227	struct gctl_req_arg *ap;
228
229	if (req == NULL || req->error != NULL)
230		return;
231	ap = gctl_new_arg(req);
232	if (ap == NULL)
233		return;
234	ap->value = value;
235	ap->flag = GCTL_PARAM_RW;
236	ap->offset = offset;
237	ap->len = len;
238}
239
240const char *
241gctl_issue(struct gctl_req *req)
242{
243	int fd, error;
244
245	if (req == NULL)
246		return ("NULL request pointer");
247	if (req->error != NULL)
248		return (req->error);
249
250	req->version = GCTL_VERSION;
251	req->lerror = BUFSIZ;		/* XXX: arbitrary number */
252	req->error = malloc(req->lerror);
253	if (req->error == NULL) {
254		gctl_check_alloc(req, req->error);
255		return (req->error);
256	}
257	memset(req->error, 0, req->lerror);
258	req->lerror--;
259	fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
260	if (fd < 0)
261		return(strerror(errno));
262	error = ioctl(fd, GEOM_CTL, req);
263	close(fd);
264	if (req->error[0] != '\0')
265		return (req->error);
266	if (error != 0)
267		return(strerror(errno));
268	return (NULL);
269}
270
271void
272gctl_free(struct gctl_req *req)
273{
274	u_int i;
275
276	if (req == NULL)
277		return;
278	for (i = 0; i < req->narg; i++) {
279		if (req->arg[i].name != NULL)
280			free(req->arg[i].name);
281	}
282	free(req->arg);
283	if (req->error != NULL)
284		free(req->error);
285	free(req);
286}
287