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
32 #include <sys/types.h>
33 #include <sys/queue.h>
48 * Global pointer to a string that is used to avoid an errorneous free in
51 static char nomemmsg[] = "Could not allocate memory";
54 gctl_dump(struct gctl_req *req, FILE *f)
58 struct gctl_req_arg *ap;
61 fprintf(f, "Dump of gctl request at NULL\n");
64 fprintf(f, "Dump of gctl request at %p:\n", req);
65 if (req->error != NULL)
66 fprintf(f, " error:\t\"%s\"\n", req->error);
68 fprintf(f, " error:\tNULL\n");
69 for (i = 0; i < req->narg; i++) {
71 fprintf(f, " param:\t\"%s\" (%d)", ap->name, ap->nlen);
73 ap->flag & GCTL_PARAM_RD ? "R" : "",
74 ap->flag & GCTL_PARAM_WR ? "W" : "");
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);
81 for (j = 0; j < ap->len; j++) {
82 fprintf(f, " %02x", ((u_char *)ap->value)[j]);
85 fprintf(f, "0] = %p", ap->value);
92 * Set an error message, if one does not already exist.
95 gctl_set_error(struct gctl_req *req, const char *error, ...)
99 if (req->error != NULL)
102 vasprintf(&req->error, error, ap);
107 * Check that a malloc operation succeeded, and set a consistent error
111 gctl_check_alloc(struct gctl_req *req, void *ptr)
116 gctl_set_error(req, nomemmsg);
117 if (req->error == NULL)
118 req->error = nomemmsg;
122 * Allocate a new request handle of the specified type.
123 * XXX: Why bother checking the type ?
126 gctl_get_handle(void)
129 return (calloc(1, sizeof(struct gctl_req)));
133 * Allocate space for another argument.
135 static struct gctl_req_arg *
136 gctl_new_arg(struct gctl_req *req)
138 struct gctl_req_arg *ap;
141 req->arg = reallocf(req->arg, sizeof *ap * req->narg);
142 gctl_check_alloc(req, req->arg);
143 if (req->arg == NULL) {
147 ap = req->arg + (req->narg - 1);
148 memset(ap, 0, sizeof *ap);
153 gctl_param_add(struct gctl_req *req, const char *name, int len, void *value,
156 struct gctl_req_arg *ap;
158 if (req == NULL || req->error != NULL)
160 ap = gctl_new_arg(req);
163 ap->name = strdup(name);
164 gctl_check_alloc(req, ap->name);
165 if (ap->name == NULL)
167 ap->nlen = strlen(ap->name) + 1;
173 ap->flag |= GCTL_PARAM_ASCII;
174 ap->len = strlen(value) + 1;
179 gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
182 gctl_param_add(req, name, len, __DECONST(void *, value), GCTL_PARAM_RD);
186 gctl_rw_param(struct gctl_req *req, const char *name, int len, void *value)
189 gctl_param_add(req, name, len, value, GCTL_PARAM_RW);
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 = calloc(1, req->lerror);
205 if (req->error == NULL) {
206 gctl_check_alloc(req, req->error);
210 fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
212 return(strerror(errno));
213 error = ioctl(fd, GEOM_CTL, req);
215 if (req->error[0] != '\0')
218 return(strerror(errno));
223 gctl_free(struct gctl_req *req)
229 for (i = 0; i < req->narg; i++) {
230 if (req->arg[i].name != NULL)
231 free(req->arg[i].name);
234 if (req->error != NULL && req->error != nomemmsg)