2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2003 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the authors may not be used to endorse or promote
16 * products derived from this software without specific prior written
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include <sys/types.h>
35 #include <sys/queue.h>
50 * Global pointer to a string that is used to avoid an errorneous free in
53 static char nomemmsg[] = "Could not allocate memory";
56 gctl_dump(struct gctl_req *req, FILE *f)
60 struct gctl_req_arg *ap;
63 fprintf(f, "Dump of gctl request at NULL\n");
66 fprintf(f, "Dump of gctl request at %p:\n", req);
67 if (req->error != NULL)
68 fprintf(f, " error:\t\"%s\"\n", req->error);
70 fprintf(f, " error:\tNULL\n");
71 for (i = 0; i < req->narg; i++) {
73 fprintf(f, " param:\t\"%s\" (%d)", ap->name, ap->nlen);
75 ap->flag & GCTL_PARAM_RD ? "R" : "",
76 ap->flag & GCTL_PARAM_WR ? "W" : "");
78 if (ap->flag & GCTL_PARAM_ASCII)
79 fprintf(f, "%d] = \"%s\"", ap->len, (char *)ap->value);
80 else if (ap->len > 0) {
81 fprintf(f, "%d] = ", ap->len);
83 for (j = 0; j < ap->len; j++) {
84 fprintf(f, " %02x", ((u_char *)ap->value)[j]);
87 fprintf(f, "0] = %p", ap->value);
94 * Set an error message, if one does not already exist.
97 gctl_set_error(struct gctl_req *req, const char *error, ...)
101 if (req->error != NULL)
104 vasprintf(&req->error, error, ap);
109 * Check that a malloc operation succeeded, and set a consistent error
113 gctl_check_alloc(struct gctl_req *req, void *ptr)
118 gctl_set_error(req, nomemmsg);
119 if (req->error == NULL)
120 req->error = nomemmsg;
124 * Allocate a new request handle of the specified type.
125 * XXX: Why bother checking the type ?
128 gctl_get_handle(void)
131 return (calloc(1, sizeof(struct gctl_req)));
135 * Allocate space for another argument.
137 static struct gctl_req_arg *
138 gctl_new_arg(struct gctl_req *req)
140 struct gctl_req_arg *ap;
143 req->arg = reallocf(req->arg, sizeof *ap * req->narg);
144 gctl_check_alloc(req, req->arg);
145 if (req->arg == NULL) {
149 ap = req->arg + (req->narg - 1);
150 memset(ap, 0, sizeof *ap);
155 gctl_add_param(struct gctl_req *req, const char *name, int len, 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;
175 ap->flag |= GCTL_PARAM_ASCII;
176 ap->len = strlen(value) + 1;
181 gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
184 gctl_add_param(req, name, len, __DECONST(void *, value), GCTL_PARAM_RD);
188 gctl_rw_param(struct gctl_req *req, const char *name, int len, void *value)
191 gctl_add_param(req, name, len, value, GCTL_PARAM_RW);
195 gctl_issue(struct gctl_req *req)
200 return ("NULL request pointer");
201 if (req->error != NULL)
204 req->version = GCTL_VERSION;
205 req->lerror = BUFSIZ; /* XXX: arbitrary number */
206 req->error = calloc(1, req->lerror);
207 if (req->error == NULL) {
208 gctl_check_alloc(req, req->error);
212 fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
214 return(strerror(errno));
215 req->nerror = ioctl(fd, GEOM_CTL, req);
217 if (req->error[0] != '\0')
219 if (req->nerror == -1)
220 return(strerror(errno));
225 gctl_free(struct gctl_req *req)
231 for (i = 0; i < req->narg; i++) {
232 if (req->arg[i].name != NULL)
233 free(req->arg[i].name);
236 if (req->error != NULL && req->error != nomemmsg)