2 * Copyright (c) 2003 Poul-Henning Kamp
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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
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
36 #include <sys/types.h>
43 #include <sys/queue.h>
49 gctl_dump(struct gctl_req *req, FILE *f)
53 struct gctl_req_arg *ap;
56 fprintf(f, "Dump of gctl request at NULL\n");
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);
63 fprintf(f, " error:\tNULL\n");
64 for (i = 0; i < req->narg; i++) {
66 fprintf(f, " param:\t\"%s\" (%d)", ap->name, ap->nlen);
68 ap->flag & GCTL_PARAM_RD ? "R" : "",
69 ap->flag & GCTL_PARAM_WR ? "W" : "");
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);
76 for (j = 0; j < ap->len; j++) {
77 fprintf(f, " %02x", ((u_char *)ap->value)[j]);
80 fprintf(f, "0] = %p", ap->value);
87 * Set an error message, if one does not already exist.
90 gctl_set_error(struct gctl_req *req, const char *error, ...)
94 if (req->error != NULL)
97 vasprintf(&req->error, error, ap);
102 * Check that a malloc operation succeeded, and set a consistent error
106 gctl_check_alloc(struct gctl_req *req, void *ptr)
110 gctl_set_error(req, "Could not allocate memory");
111 if (req->error == NULL)
112 req->error = "Could not allocate memory";
116 * Allocate a new request handle of the specified type.
117 * XXX: Why bother checking the type ?
120 gctl_get_handle(void)
124 rp = calloc(1, sizeof *rp);
129 * Allocate space for another argument.
131 static struct gctl_req_arg *
132 gctl_new_arg(struct gctl_req *req)
134 struct gctl_req_arg *ap;
137 req->arg = realloc(req->arg, sizeof *ap * req->narg);
138 gctl_check_alloc(req, req->arg);
139 if (req->arg == NULL) {
143 ap = req->arg + (req->narg - 1);
144 memset(ap, 0, sizeof *ap);
149 gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
151 struct gctl_req_arg *ap;
153 if (req == NULL || req->error != NULL)
155 ap = gctl_new_arg(req);
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;
166 ap->flag |= GCTL_PARAM_ASCII;
167 ap->len = strlen(value) + 1;
172 gctl_rw_param(struct gctl_req *req, const char *name, int len, void* value)
174 struct gctl_req_arg *ap;
176 if (req == NULL || req->error != NULL)
178 ap = gctl_new_arg(req);
181 ap->name = strdup(name);
182 gctl_check_alloc(req, ap->name);
183 ap->nlen = strlen(ap->name) + 1;
185 ap->flag = GCTL_PARAM_RW;
189 ap->len = strlen(value) + 1;
193 gctl_issue(struct gctl_req *req)
198 return ("NULL request pointer");
199 if (req->error != NULL)
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);
209 memset(req->error, 0, req->lerror);
211 fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
213 return(strerror(errno));
214 error = ioctl(fd, GEOM_CTL, req);
216 if (req->error[0] != '\0')
219 return(strerror(errno));
224 gctl_free(struct gctl_req *req)
230 for (i = 0; i < req->narg; i++) {
231 if (req->arg[i].name != NULL)
232 free(req->arg[i].name);
235 if (req->error != NULL)