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