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 * Global pointer to a string that is used to avoid an errorneous free in
52 static char nomemmsg[] = "Could not allocate memory";
55 gctl_dump(struct gctl_req *req, FILE *f)
59 struct gctl_req_arg *ap;
62 fprintf(f, "Dump of gctl request at NULL\n");
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);
69 fprintf(f, " error:\tNULL\n");
70 for (i = 0; i < req->narg; i++) {
72 fprintf(f, " param:\t\"%s\" (%d)", ap->name, ap->nlen);
74 ap->flag & GCTL_PARAM_RD ? "R" : "",
75 ap->flag & GCTL_PARAM_WR ? "W" : "");
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);
82 for (j = 0; j < ap->len; j++) {
83 fprintf(f, " %02x", ((u_char *)ap->value)[j]);
86 fprintf(f, "0] = %p", ap->value);
93 * Set an error message, if one does not already exist.
96 gctl_set_error(struct gctl_req *req, const char *error, ...)
100 if (req->error != NULL)
103 vasprintf(&req->error, error, ap);
108 * Check that a malloc operation succeeded, and set a consistent error
112 gctl_check_alloc(struct gctl_req *req, void *ptr)
117 gctl_set_error(req, nomemmsg);
118 if (req->error == NULL)
119 req->error = nomemmsg;
123 * Allocate a new request handle of the specified type.
124 * XXX: Why bother checking the type ?
127 gctl_get_handle(void)
131 rp = calloc(1, sizeof *rp);
136 * Allocate space for another argument.
138 static struct gctl_req_arg *
139 gctl_new_arg(struct gctl_req *req)
141 struct gctl_req_arg *ap;
144 req->arg = reallocf(req->arg, sizeof *ap * req->narg);
145 gctl_check_alloc(req, req->arg);
146 if (req->arg == NULL) {
150 ap = req->arg + (req->narg - 1);
151 memset(ap, 0, sizeof *ap);
156 gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
158 struct gctl_req_arg *ap;
160 if (req == NULL || req->error != NULL)
162 ap = gctl_new_arg(req);
165 ap->name = strdup(name);
166 gctl_check_alloc(req, ap->name);
167 if (ap->name == NULL)
169 ap->nlen = strlen(ap->name) + 1;
170 ap->value = __DECONST(void *, value);
171 ap->flag = GCTL_PARAM_RD;
175 ap->flag |= GCTL_PARAM_ASCII;
176 ap->len = strlen(value) + 1;
181 gctl_rw_param(struct gctl_req *req, const char *name, int len, void* value)
183 struct gctl_req_arg *ap;
185 if (req == NULL || req->error != NULL)
187 ap = gctl_new_arg(req);
190 ap->name = strdup(name);
191 gctl_check_alloc(req, ap->name);
192 if (ap->name == NULL)
194 ap->nlen = strlen(ap->name) + 1;
196 ap->flag = GCTL_PARAM_RW;
200 ap->len = strlen(value) + 1;
204 gctl_issue(struct gctl_req *req)
209 return ("NULL request pointer");
210 if (req->error != NULL)
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);
221 fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
223 return(strerror(errno));
224 error = ioctl(fd, GEOM_CTL, req);
226 if (req->error[0] != '\0')
229 return(strerror(errno));
234 gctl_free(struct gctl_req *req)
240 for (i = 0; i < req->narg; i++) {
241 if (req->arg[i].name != NULL)
242 free(req->arg[i].name);
245 if (req->error != NULL && req->error != nomemmsg)