2003-03-23 10:15:02 +00:00
|
|
|
/*-
|
|
|
|
* Copyright (c) 2003 Poul-Henning Kamp
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. The names of the authors may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
|
|
|
* permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* $FreeBSD$
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <stdarg.h>
|
2003-03-27 14:35:00 +00:00
|
|
|
#include <unistd.h>
|
2003-03-23 10:15:02 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <paths.h>
|
|
|
|
|
|
|
|
#include <sys/queue.h>
|
|
|
|
|
2003-03-27 14:35:00 +00:00
|
|
|
#define GCTL_TABLE 1
|
2003-03-23 10:15:02 +00:00
|
|
|
#include <libgeom.h>
|
|
|
|
|
2008-07-08 17:34:50 +00:00
|
|
|
/*
|
|
|
|
* Global pointer to a string that is used to avoid an errorneous free in
|
|
|
|
* gctl_free.
|
|
|
|
*/
|
|
|
|
static char nomemmsg[] = "Could not allocate memory";
|
|
|
|
|
2003-03-23 10:15:02 +00:00
|
|
|
void
|
2003-03-27 14:35:00 +00:00
|
|
|
gctl_dump(struct gctl_req *req, FILE *f)
|
2003-03-23 10:15:02 +00:00
|
|
|
{
|
2010-10-21 10:36:36 +00:00
|
|
|
unsigned int i;
|
2003-03-23 10:15:02 +00:00
|
|
|
int j;
|
2003-03-27 14:35:00 +00:00
|
|
|
struct gctl_req_arg *ap;
|
2003-03-23 10:15:02 +00:00
|
|
|
|
|
|
|
if (req == NULL) {
|
2003-03-27 14:35:00 +00:00
|
|
|
fprintf(f, "Dump of gctl request at NULL\n");
|
2003-03-23 10:15:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2003-06-01 15:05:22 +00:00
|
|
|
fprintf(f, "Dump of gctl request at %p:\n", req);
|
2003-03-23 10:15:02 +00:00
|
|
|
if (req->error != NULL)
|
|
|
|
fprintf(f, " error:\t\"%s\"\n", req->error);
|
|
|
|
else
|
|
|
|
fprintf(f, " error:\tNULL\n");
|
|
|
|
for (i = 0; i < req->narg; i++) {
|
|
|
|
ap = &req->arg[i];
|
2003-06-01 15:05:22 +00:00
|
|
|
fprintf(f, " param:\t\"%s\" (%d)", ap->name, ap->nlen);
|
2003-03-27 14:35:00 +00:00
|
|
|
fprintf(f, " [%s%s",
|
|
|
|
ap->flag & GCTL_PARAM_RD ? "R" : "",
|
|
|
|
ap->flag & GCTL_PARAM_WR ? "W" : "");
|
2003-03-23 10:15:02 +00:00
|
|
|
fflush(f);
|
2003-03-27 14:35:00 +00:00
|
|
|
if (ap->flag & GCTL_PARAM_ASCII)
|
|
|
|
fprintf(f, "%d] = \"%s\"", ap->len, (char *)ap->value);
|
2003-03-23 10:15:02 +00:00
|
|
|
else if (ap->len > 0) {
|
2003-03-27 14:35:00 +00:00
|
|
|
fprintf(f, "%d] = ", ap->len);
|
2003-03-23 10:15:02 +00:00
|
|
|
fflush(f);
|
|
|
|
for (j = 0; j < ap->len; j++) {
|
|
|
|
fprintf(f, " %02x", ((u_char *)ap->value)[j]);
|
|
|
|
}
|
|
|
|
} else {
|
2003-03-27 14:35:00 +00:00
|
|
|
fprintf(f, "0] = %p", ap->value);
|
2003-03-23 10:15:02 +00:00
|
|
|
}
|
|
|
|
fprintf(f, "\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-27 14:35:00 +00:00
|
|
|
/*
|
|
|
|
* Set an error message, if one does not already exist.
|
|
|
|
*/
|
2003-03-23 10:15:02 +00:00
|
|
|
static void
|
2003-03-27 14:35:00 +00:00
|
|
|
gctl_set_error(struct gctl_req *req, const char *error, ...)
|
2003-03-23 10:15:02 +00:00
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
|
|
|
|
if (req->error != NULL)
|
|
|
|
return;
|
|
|
|
va_start(ap, error);
|
|
|
|
vasprintf(&req->error, error, ap);
|
2003-03-27 14:35:00 +00:00
|
|
|
va_end(ap);
|
2003-03-23 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
2003-03-27 14:35:00 +00:00
|
|
|
/*
|
|
|
|
* Check that a malloc operation succeeded, and set a consistent error
|
|
|
|
* message if not.
|
|
|
|
*/
|
2003-03-23 10:15:02 +00:00
|
|
|
static void
|
2003-03-27 14:35:00 +00:00
|
|
|
gctl_check_alloc(struct gctl_req *req, void *ptr)
|
2003-03-23 10:15:02 +00:00
|
|
|
{
|
2008-07-08 17:34:50 +00:00
|
|
|
|
2003-03-23 10:15:02 +00:00
|
|
|
if (ptr != NULL)
|
|
|
|
return;
|
2008-07-08 17:34:50 +00:00
|
|
|
gctl_set_error(req, nomemmsg);
|
2003-03-27 14:35:00 +00:00
|
|
|
if (req->error == NULL)
|
2008-07-08 17:34:50 +00:00
|
|
|
req->error = nomemmsg;
|
2003-03-23 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
2003-03-27 14:35:00 +00:00
|
|
|
/*
|
|
|
|
* Allocate a new request handle of the specified type.
|
|
|
|
* XXX: Why bother checking the type ?
|
|
|
|
*/
|
|
|
|
struct gctl_req *
|
2003-06-01 15:05:22 +00:00
|
|
|
gctl_get_handle(void)
|
2003-03-23 10:15:02 +00:00
|
|
|
{
|
|
|
|
|
2010-10-21 10:36:36 +00:00
|
|
|
return (calloc(1, sizeof(struct gctl_req)));
|
2003-03-23 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
2003-03-27 14:35:00 +00:00
|
|
|
/*
|
|
|
|
* Allocate space for another argument.
|
|
|
|
*/
|
|
|
|
static struct gctl_req_arg *
|
|
|
|
gctl_new_arg(struct gctl_req *req)
|
2003-03-23 10:15:02 +00:00
|
|
|
{
|
2003-03-27 14:35:00 +00:00
|
|
|
struct gctl_req_arg *ap;
|
2003-03-23 10:15:02 +00:00
|
|
|
|
|
|
|
req->narg++;
|
2008-07-08 17:34:50 +00:00
|
|
|
req->arg = reallocf(req->arg, sizeof *ap * req->narg);
|
2003-03-27 14:35:00 +00:00
|
|
|
gctl_check_alloc(req, req->arg);
|
|
|
|
if (req->arg == NULL) {
|
2003-03-23 10:15:02 +00:00
|
|
|
req->narg = 0;
|
2003-03-27 14:35:00 +00:00
|
|
|
return (NULL);
|
2003-03-23 10:15:02 +00:00
|
|
|
}
|
2003-03-27 14:35:00 +00:00
|
|
|
ap = req->arg + (req->narg - 1);
|
|
|
|
memset(ap, 0, sizeof *ap);
|
|
|
|
return (ap);
|
2003-03-23 10:15:02 +00:00
|
|
|
}
|
|
|
|
|
2010-10-21 10:38:14 +00:00
|
|
|
static void
|
|
|
|
gctl_param_add(struct gctl_req *req, const char *name, int len, void *value,
|
|
|
|
int flag)
|
2003-03-23 10:15:02 +00:00
|
|
|
{
|
2003-03-27 14:35:00 +00:00
|
|
|
struct gctl_req_arg *ap;
|
2003-03-23 10:15:02 +00:00
|
|
|
|
|
|
|
if (req == NULL || req->error != NULL)
|
|
|
|
return;
|
2003-03-27 14:35:00 +00:00
|
|
|
ap = gctl_new_arg(req);
|
|
|
|
if (ap == NULL)
|
2003-03-23 10:15:02 +00:00
|
|
|
return;
|
2003-03-27 14:35:00 +00:00
|
|
|
ap->name = strdup(name);
|
|
|
|
gctl_check_alloc(req, ap->name);
|
2008-07-08 17:34:50 +00:00
|
|
|
if (ap->name == NULL)
|
|
|
|
return;
|
2003-03-27 14:35:00 +00:00
|
|
|
ap->nlen = strlen(ap->name) + 1;
|
2010-10-21 10:38:14 +00:00
|
|
|
ap->value = value;
|
|
|
|
ap->flag = flag;
|
2003-03-27 14:35:00 +00:00
|
|
|
if (len >= 0)
|
2003-03-23 10:15:02 +00:00
|
|
|
ap->len = len;
|
2003-03-27 14:35:00 +00:00
|
|
|
else if (len < 0) {
|
|
|
|
ap->flag |= GCTL_PARAM_ASCII;
|
|
|
|
ap->len = strlen(value) + 1;
|
2003-03-23 10:15:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-27 14:35:00 +00:00
|
|
|
void
|
2010-10-21 10:38:14 +00:00
|
|
|
gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
|
2003-03-27 14:35:00 +00:00
|
|
|
{
|
|
|
|
|
2010-10-21 10:38:14 +00:00
|
|
|
gctl_param_add(req, name, len, __DECONST(void *, value), GCTL_PARAM_RD);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
gctl_rw_param(struct gctl_req *req, const char *name, int len, void *value)
|
|
|
|
{
|
|
|
|
|
|
|
|
gctl_param_add(req, name, len, value, GCTL_PARAM_RW);
|
2003-03-27 14:35:00 +00:00
|
|
|
}
|
|
|
|
|
2003-03-23 10:15:02 +00:00
|
|
|
const char *
|
2003-03-27 14:35:00 +00:00
|
|
|
gctl_issue(struct gctl_req *req)
|
2003-03-23 10:15:02 +00:00
|
|
|
{
|
|
|
|
int fd, error;
|
|
|
|
|
|
|
|
if (req == NULL)
|
|
|
|
return ("NULL request pointer");
|
|
|
|
if (req->error != NULL)
|
|
|
|
return (req->error);
|
|
|
|
|
2003-03-27 14:35:00 +00:00
|
|
|
req->version = GCTL_VERSION;
|
2003-03-23 10:15:02 +00:00
|
|
|
req->lerror = BUFSIZ; /* XXX: arbitrary number */
|
2008-07-08 17:34:50 +00:00
|
|
|
req->error = calloc(1, req->lerror);
|
2003-03-27 14:35:00 +00:00
|
|
|
if (req->error == NULL) {
|
|
|
|
gctl_check_alloc(req, req->error);
|
|
|
|
return (req->error);
|
|
|
|
}
|
2003-03-23 10:15:02 +00:00
|
|
|
req->lerror--;
|
|
|
|
fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
|
|
|
|
if (fd < 0)
|
|
|
|
return(strerror(errno));
|
|
|
|
error = ioctl(fd, GEOM_CTL, req);
|
2003-03-27 14:35:00 +00:00
|
|
|
close(fd);
|
|
|
|
if (req->error[0] != '\0')
|
2003-03-23 10:15:02 +00:00
|
|
|
return (req->error);
|
|
|
|
if (error != 0)
|
|
|
|
return(strerror(errno));
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2003-03-27 14:35:00 +00:00
|
|
|
gctl_free(struct gctl_req *req)
|
2003-03-23 10:15:02 +00:00
|
|
|
{
|
2010-10-21 10:36:36 +00:00
|
|
|
unsigned int i;
|
2003-03-23 10:15:02 +00:00
|
|
|
|
2003-03-27 14:35:00 +00:00
|
|
|
if (req == NULL)
|
|
|
|
return;
|
2003-03-23 10:15:02 +00:00
|
|
|
for (i = 0; i < req->narg; i++) {
|
|
|
|
if (req->arg[i].name != NULL)
|
|
|
|
free(req->arg[i].name);
|
|
|
|
}
|
2003-03-27 14:35:00 +00:00
|
|
|
free(req->arg);
|
2008-07-08 17:34:50 +00:00
|
|
|
if (req->error != NULL && req->error != nomemmsg)
|
2003-03-23 10:15:02 +00:00
|
|
|
free(req->error);
|
|
|
|
free(req);
|
|
|
|
}
|