1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2003 Poul-Henning Kamp 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 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 17 * permission. 18 * 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 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/types.h> 33 #include <sys/queue.h> 34 #include <fcntl.h> 35 #include <errno.h> 36 #include <paths.h> 37 #include <stdarg.h> 38 #include <stdint.h> 39 #include <stdio.h> 40 #include <stdlib.h> 41 #include <string.h> 42 #include <unistd.h> 43 44 #define GCTL_TABLE 1 45 #include <libgeom.h> 46 47 /* 48 * Global pointer to a string that is used to avoid an errorneous free in 49 * gctl_free. 50 */ 51 static char nomemmsg[] = "Could not allocate memory"; 52 53 void 54 gctl_dump(struct gctl_req *req, FILE *f) 55 { 56 unsigned int i; 57 int j; 58 struct gctl_req_arg *ap; 59 60 if (req == NULL) { 61 fprintf(f, "Dump of gctl request at NULL\n"); 62 return; 63 } 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); 67 else 68 fprintf(f, " error:\tNULL\n"); 69 for (i = 0; i < req->narg; i++) { 70 ap = &req->arg[i]; 71 fprintf(f, " param:\t\"%s\" (%d)", ap->name, ap->nlen); 72 fprintf(f, " [%s%s", 73 ap->flag & GCTL_PARAM_RD ? "R" : "", 74 ap->flag & GCTL_PARAM_WR ? "W" : ""); 75 fflush(f); 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); 80 fflush(f); 81 for (j = 0; j < ap->len; j++) { 82 fprintf(f, " %02x", ((u_char *)ap->value)[j]); 83 } 84 } else { 85 fprintf(f, "0] = %p", ap->value); 86 } 87 fprintf(f, "\n"); 88 } 89 } 90 91 /* 92 * Set an error message, if one does not already exist. 93 */ 94 static void 95 gctl_set_error(struct gctl_req *req, const char *error, ...) 96 { 97 va_list ap; 98 99 if (req->error != NULL) 100 return; 101 va_start(ap, error); 102 vasprintf(&req->error, error, ap); 103 va_end(ap); 104 } 105 106 /* 107 * Check that a malloc operation succeeded, and set a consistent error 108 * message if not. 109 */ 110 static void 111 gctl_check_alloc(struct gctl_req *req, void *ptr) 112 { 113 114 if (ptr != NULL) 115 return; 116 gctl_set_error(req, nomemmsg); 117 if (req->error == NULL) 118 req->error = nomemmsg; 119 } 120 121 /* 122 * Allocate a new request handle of the specified type. 123 * XXX: Why bother checking the type ? 124 */ 125 struct gctl_req * 126 gctl_get_handle(void) 127 { 128 129 return (calloc(1, sizeof(struct gctl_req))); 130 } 131 132 /* 133 * Allocate space for another argument. 134 */ 135 static struct gctl_req_arg * 136 gctl_new_arg(struct gctl_req *req) 137 { 138 struct gctl_req_arg *ap; 139 140 req->narg++; 141 req->arg = reallocf(req->arg, sizeof *ap * req->narg); 142 gctl_check_alloc(req, req->arg); 143 if (req->arg == NULL) { 144 req->narg = 0; 145 return (NULL); 146 } 147 ap = req->arg + (req->narg - 1); 148 memset(ap, 0, sizeof *ap); 149 return (ap); 150 } 151 152 void 153 gctl_add_param(struct gctl_req *req, const char *name, int len, void *value, 154 int flag) 155 { 156 struct gctl_req_arg *ap; 157 158 if (req == NULL || req->error != NULL) 159 return; 160 ap = gctl_new_arg(req); 161 if (ap == NULL) 162 return; 163 ap->name = strdup(name); 164 gctl_check_alloc(req, ap->name); 165 if (ap->name == NULL) 166 return; 167 ap->nlen = strlen(ap->name) + 1; 168 ap->value = value; 169 ap->flag = flag; 170 if (len >= 0) 171 ap->len = len; 172 else if (len < 0) { 173 ap->flag |= GCTL_PARAM_ASCII; 174 ap->len = strlen(value) + 1; 175 } 176 } 177 178 void 179 gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value) 180 { 181 182 gctl_add_param(req, name, len, __DECONST(void *, value), GCTL_PARAM_RD); 183 } 184 185 void 186 gctl_rw_param(struct gctl_req *req, const char *name, int len, void *value) 187 { 188 189 gctl_add_param(req, name, len, value, GCTL_PARAM_RW); 190 } 191 192 const char * 193 gctl_issue(struct gctl_req *req) 194 { 195 int fd; 196 197 if (req == NULL) 198 return ("NULL request pointer"); 199 if (req->error != NULL) 200 return (req->error); 201 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); 207 return (req->error); 208 } 209 req->lerror--; 210 fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY); 211 if (fd < 0) 212 return(strerror(errno)); 213 req->nerror = ioctl(fd, GEOM_CTL, req); 214 close(fd); 215 if (req->error[0] != '\0') 216 return (req->error); 217 if (req->nerror == -1) 218 return(strerror(errno)); 219 return (NULL); 220 } 221 222 void 223 gctl_free(struct gctl_req *req) 224 { 225 unsigned int i; 226 227 if (req == NULL) 228 return; 229 for (i = 0; i < req->narg; i++) { 230 if (req->arg[i].name != NULL) 231 free(req->arg[i].name); 232 } 233 free(req->arg); 234 if (req->error != NULL && req->error != nomemmsg) 235 free(req->error); 236 free(req); 237 } 238