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