geom_ctl.c revision 112709
1112510Sphk/*- 2112510Sphk * Copyright (c) 2003 Poul-Henning Kamp 3112510Sphk * All rights reserved. 4112510Sphk * 5112510Sphk * Redistribution and use in source and binary forms, with or without 6112510Sphk * modification, are permitted provided that the following conditions 7112510Sphk * are met: 8112510Sphk * 1. Redistributions of source code must retain the above copyright 9112510Sphk * notice, this list of conditions and the following disclaimer. 10112510Sphk * 2. Redistributions in binary form must reproduce the above copyright 11112510Sphk * notice, this list of conditions and the following disclaimer in the 12112510Sphk * documentation and/or other materials provided with the distribution. 13112510Sphk * 3. The names of the authors may not be used to endorse or promote 14112510Sphk * products derived from this software without specific prior written 15112510Sphk * permission. 16112510Sphk * 17112510Sphk * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18112510Sphk * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19112510Sphk * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20112510Sphk * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21112510Sphk * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22112510Sphk * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23112510Sphk * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24112510Sphk * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25112510Sphk * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26112510Sphk * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27112510Sphk * SUCH DAMAGE. 28112510Sphk * 29112510Sphk * $FreeBSD: head/lib/libgeom/geom_ctl.c 112709 2003-03-27 14:35:00Z phk $ 30112510Sphk */ 31112510Sphk 32112510Sphk#include <stdio.h> 33112510Sphk#include <fcntl.h> 34112510Sphk#include <errno.h> 35112510Sphk#include <stdint.h> 36112510Sphk#include <sys/types.h> 37112510Sphk#include <stdarg.h> 38112709Sphk#include <unistd.h> 39112510Sphk#include <string.h> 40112510Sphk#include <stdlib.h> 41112510Sphk#include <paths.h> 42112510Sphk 43112510Sphk#include <sys/queue.h> 44112510Sphk 45112709Sphk#define GCTL_TABLE 1 46112510Sphk#include <libgeom.h> 47112510Sphk 48112510Sphk#include <geom/geom_ext.h> 49112510Sphk 50112510Sphkvoid 51112709Sphkgctl_dump(struct gctl_req *req, FILE *f) 52112510Sphk{ 53112510Sphk u_int i; 54112510Sphk int j; 55112709Sphk struct gctl_req_arg *ap; 56112510Sphk 57112510Sphk if (req == NULL) { 58112709Sphk fprintf(f, "Dump of gctl request at NULL\n"); 59112510Sphk return; 60112510Sphk } 61112709Sphk fprintf(f, "Dump of gctl %s request at %p:\n", req->reqt->name, req); 62112510Sphk if (req->error != NULL) 63112510Sphk fprintf(f, " error:\t\"%s\"\n", req->error); 64112510Sphk else 65112510Sphk fprintf(f, " error:\tNULL\n"); 66112510Sphk for (i = 0; i < req->narg; i++) { 67112510Sphk ap = &req->arg[i]; 68112510Sphk if (ap->name != NULL) 69112510Sphk fprintf(f, " param:\t\"%s\"", ap->name); 70112510Sphk else 71112510Sphk fprintf(f, " meta:\t@%jd", (intmax_t)ap->offset); 72112709Sphk fprintf(f, " [%s%s", 73112709Sphk ap->flag & GCTL_PARAM_RD ? "R" : "", 74112709Sphk ap->flag & GCTL_PARAM_WR ? "W" : ""); 75112510Sphk fflush(f); 76112709Sphk if (ap->flag & GCTL_PARAM_ASCII) 77112709Sphk fprintf(f, "%d] = \"%s\"", ap->len, (char *)ap->value); 78112510Sphk else if (ap->len > 0) { 79112709Sphk fprintf(f, "%d] = ", ap->len); 80112510Sphk fflush(f); 81112510Sphk for (j = 0; j < ap->len; j++) { 82112510Sphk fprintf(f, " %02x", ((u_char *)ap->value)[j]); 83112510Sphk } 84112510Sphk } else { 85112709Sphk fprintf(f, "0] = %p", ap->value); 86112510Sphk } 87112510Sphk fprintf(f, "\n"); 88112510Sphk } 89112510Sphk} 90112510Sphk 91112709Sphk/* 92112709Sphk * Set an error message, if one does not already exist. 93112709Sphk */ 94112510Sphkstatic void 95112709Sphkgctl_set_error(struct gctl_req *req, const char *error, ...) 96112510Sphk{ 97112510Sphk va_list ap; 98112510Sphk 99112510Sphk if (req->error != NULL) 100112510Sphk return; 101112510Sphk va_start(ap, error); 102112510Sphk vasprintf(&req->error, error, ap); 103112709Sphk va_end(ap); 104112510Sphk} 105112510Sphk 106112709Sphk/* 107112709Sphk * Check that a malloc operation succeeded, and set a consistent error 108112709Sphk * message if not. 109112709Sphk */ 110112510Sphkstatic void 111112709Sphkgctl_check_alloc(struct gctl_req *req, void *ptr) 112112510Sphk{ 113112510Sphk if (ptr != NULL) 114112510Sphk return; 115112709Sphk gctl_set_error(req, "Could not allocate memory"); 116112709Sphk if (req->error == NULL) 117112709Sphk req->error = "Could not allocate memory"; 118112510Sphk} 119112510Sphk 120112709Sphk/* 121112709Sphk * Allocate a new request handle of the specified type. 122112709Sphk * XXX: Why bother checking the type ? 123112709Sphk */ 124112709Sphkstruct gctl_req * 125112709Sphkgctl_get_handle(enum gctl_request req) 126112510Sphk{ 127112709Sphk struct gctl_req_table *gtp; 128112709Sphk struct gctl_req *rp; 129112510Sphk 130112510Sphk rp = calloc(1, sizeof *rp); 131112510Sphk if (rp == NULL) 132112510Sphk return (NULL); 133112510Sphk for (gtp = gcrt; gtp->request != req; gtp++) 134112709Sphk if (gtp->request == GCTL_INVALID_REQUEST) 135112510Sphk break; 136112510Sphk 137112510Sphk rp->request = req; 138112510Sphk rp->reqt = gtp; 139112709Sphk if (rp->reqt->request == GCTL_INVALID_REQUEST) 140112709Sphk gctl_set_error(rp, "Invalid request"); 141112510Sphk return (rp); 142112510Sphk} 143112510Sphk 144112709Sphk/* 145112709Sphk * Allocate space for another argument. 146112709Sphk */ 147112709Sphkstatic struct gctl_req_arg * 148112709Sphkgctl_new_arg(struct gctl_req *req) 149112510Sphk{ 150112709Sphk struct gctl_req_arg *ap; 151112510Sphk 152112510Sphk req->narg++; 153112510Sphk req->arg = realloc(req->arg, sizeof *ap * req->narg); 154112709Sphk gctl_check_alloc(req, req->arg); 155112709Sphk if (req->arg == NULL) { 156112510Sphk req->narg = 0; 157112709Sphk return (NULL); 158112510Sphk } 159112709Sphk ap = req->arg + (req->narg - 1); 160112709Sphk memset(ap, 0, sizeof *ap); 161112709Sphk return (ap); 162112510Sphk} 163112510Sphk 164112510Sphkvoid 165112709Sphkgctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value) 166112510Sphk{ 167112709Sphk struct gctl_req_arg *ap; 168112510Sphk 169112510Sphk if (req == NULL || req->error != NULL) 170112510Sphk return; 171112709Sphk ap = gctl_new_arg(req); 172112709Sphk if (ap == NULL) 173112510Sphk return; 174112709Sphk ap->name = strdup(name); 175112709Sphk gctl_check_alloc(req, ap->name); 176112709Sphk ap->nlen = strlen(ap->name) + 1; 177112709Sphk ap->value = __DECONST(void *, value); 178112709Sphk ap->flag = GCTL_PARAM_RD; 179112709Sphk if (len >= 0) 180112510Sphk ap->len = len; 181112709Sphk else if (len < 0) { 182112709Sphk ap->flag |= GCTL_PARAM_ASCII; 183112709Sphk ap->len = strlen(value) + 1; 184112510Sphk } 185112510Sphk} 186112510Sphk 187112709Sphkvoid 188112709Sphkgctl_rw_param(struct gctl_req *req, const char *name, int len, void* value) 189112709Sphk{ 190112709Sphk struct gctl_req_arg *ap; 191112709Sphk 192112709Sphk if (req == NULL || req->error != NULL) 193112709Sphk return; 194112709Sphk ap = gctl_new_arg(req); 195112709Sphk if (ap == NULL) 196112709Sphk return; 197112709Sphk ap->name = strdup(name); 198112709Sphk gctl_check_alloc(req, ap->name); 199112709Sphk ap->nlen = strlen(ap->name) + 1; 200112709Sphk ap->value = value; 201112709Sphk ap->flag = GCTL_PARAM_RW; 202112709Sphk if (len >= 0) 203112709Sphk ap->len = len; 204112709Sphk else if (len < 0) 205112709Sphk ap->len = strlen(value) + 1; 206112709Sphk} 207112709Sphk 208112709Sphkvoid 209112709Sphkgctl_ro_meta(struct gctl_req *req, off_t offset, u_int len, const void* value) 210112709Sphk{ 211112709Sphk struct gctl_req_arg *ap; 212112709Sphk 213112709Sphk if (req == NULL || req->error != NULL) 214112709Sphk return; 215112709Sphk ap = gctl_new_arg(req); 216112709Sphk if (ap == NULL) 217112709Sphk return; 218112709Sphk ap->value = __DECONST(void *, value); 219112709Sphk ap->flag = GCTL_PARAM_RD; 220112709Sphk ap->offset = offset; 221112709Sphk ap->len = len; 222112709Sphk} 223112709Sphk 224112709Sphkvoid 225112709Sphkgctl_rw_meta(struct gctl_req *req, off_t offset, u_int len, void* value) 226112709Sphk{ 227112709Sphk struct gctl_req_arg *ap; 228112709Sphk 229112709Sphk if (req == NULL || req->error != NULL) 230112709Sphk return; 231112709Sphk ap = gctl_new_arg(req); 232112709Sphk if (ap == NULL) 233112709Sphk return; 234112709Sphk ap->value = value; 235112709Sphk ap->flag = GCTL_PARAM_RW; 236112709Sphk ap->offset = offset; 237112709Sphk ap->len = len; 238112709Sphk} 239112709Sphk 240112510Sphkconst char * 241112709Sphkgctl_issue(struct gctl_req *req) 242112510Sphk{ 243112510Sphk int fd, error; 244112510Sphk 245112510Sphk if (req == NULL) 246112510Sphk return ("NULL request pointer"); 247112510Sphk if (req->error != NULL) 248112510Sphk return (req->error); 249112510Sphk 250112709Sphk req->version = GCTL_VERSION; 251112510Sphk req->lerror = BUFSIZ; /* XXX: arbitrary number */ 252112510Sphk req->error = malloc(req->lerror); 253112709Sphk if (req->error == NULL) { 254112709Sphk gctl_check_alloc(req, req->error); 255112709Sphk return (req->error); 256112709Sphk } 257112510Sphk memset(req->error, 0, req->lerror); 258112510Sphk req->lerror--; 259112510Sphk fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY); 260112510Sphk if (fd < 0) 261112510Sphk return(strerror(errno)); 262112510Sphk error = ioctl(fd, GEOM_CTL, req); 263112709Sphk close(fd); 264112709Sphk if (req->error[0] != '\0') 265112510Sphk return (req->error); 266112510Sphk if (error != 0) 267112510Sphk return(strerror(errno)); 268112510Sphk return (NULL); 269112510Sphk} 270112510Sphk 271112510Sphkvoid 272112709Sphkgctl_free(struct gctl_req *req) 273112510Sphk{ 274112510Sphk u_int i; 275112510Sphk 276112709Sphk if (req == NULL) 277112709Sphk return; 278112510Sphk for (i = 0; i < req->narg; i++) { 279112510Sphk if (req->arg[i].name != NULL) 280112510Sphk free(req->arg[i].name); 281112510Sphk } 282112709Sphk free(req->arg); 283112510Sphk if (req->error != NULL) 284112510Sphk free(req->error); 285112510Sphk free(req); 286112510Sphk} 287