xref: /freebsd/lib/libgeom/geom_ctl.c (revision b3e76948)
14b8938c1SPoul-Henning Kamp /*-
25e53a4f9SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
35e53a4f9SPedro F. Giffuni  *
44b8938c1SPoul-Henning Kamp  * Copyright (c) 2003 Poul-Henning Kamp
54b8938c1SPoul-Henning Kamp  * All rights reserved.
64b8938c1SPoul-Henning Kamp  *
74b8938c1SPoul-Henning Kamp  * Redistribution and use in source and binary forms, with or without
84b8938c1SPoul-Henning Kamp  * modification, are permitted provided that the following conditions
94b8938c1SPoul-Henning Kamp  * are met:
104b8938c1SPoul-Henning Kamp  * 1. Redistributions of source code must retain the above copyright
114b8938c1SPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer.
124b8938c1SPoul-Henning Kamp  * 2. Redistributions in binary form must reproduce the above copyright
134b8938c1SPoul-Henning Kamp  *    notice, this list of conditions and the following disclaimer in the
144b8938c1SPoul-Henning Kamp  *    documentation and/or other materials provided with the distribution.
154b8938c1SPoul-Henning Kamp  * 3. The names of the authors may not be used to endorse or promote
164b8938c1SPoul-Henning Kamp  *    products derived from this software without specific prior written
174b8938c1SPoul-Henning Kamp  *    permission.
184b8938c1SPoul-Henning Kamp  *
194b8938c1SPoul-Henning Kamp  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
204b8938c1SPoul-Henning Kamp  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
214b8938c1SPoul-Henning Kamp  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
224b8938c1SPoul-Henning Kamp  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
234b8938c1SPoul-Henning Kamp  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
244b8938c1SPoul-Henning Kamp  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
254b8938c1SPoul-Henning Kamp  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
264b8938c1SPoul-Henning Kamp  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
274b8938c1SPoul-Henning Kamp  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
284b8938c1SPoul-Henning Kamp  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
294b8938c1SPoul-Henning Kamp  * SUCH DAMAGE.
304b8938c1SPoul-Henning Kamp  */
314b8938c1SPoul-Henning Kamp 
32f06b2368SEnji Cooper #include <sys/types.h>
33f06b2368SEnji Cooper #include <sys/queue.h>
344b8938c1SPoul-Henning Kamp #include <fcntl.h>
354b8938c1SPoul-Henning Kamp #include <errno.h>
364b8938c1SPoul-Henning Kamp #include <paths.h>
37f06b2368SEnji Cooper #include <stdarg.h>
38f06b2368SEnji Cooper #include <stdint.h>
39f06b2368SEnji Cooper #include <stdio.h>
40f06b2368SEnji Cooper #include <stdlib.h>
41f06b2368SEnji Cooper #include <string.h>
42f06b2368SEnji Cooper #include <unistd.h>
434b8938c1SPoul-Henning Kamp 
4463728c47SPoul-Henning Kamp #define GCTL_TABLE 1
454b8938c1SPoul-Henning Kamp #include <libgeom.h>
464b8938c1SPoul-Henning Kamp 
477b6942a1SUlf Lilleengen /*
487b6942a1SUlf Lilleengen  * Global pointer to a string that is used to avoid an errorneous free in
497b6942a1SUlf Lilleengen  * gctl_free.
507b6942a1SUlf Lilleengen  */
517b6942a1SUlf Lilleengen static char nomemmsg[] = "Could not allocate memory";
527b6942a1SUlf Lilleengen 
534b8938c1SPoul-Henning Kamp void
gctl_dump(struct gctl_req * req,FILE * f)5463728c47SPoul-Henning Kamp gctl_dump(struct gctl_req *req, FILE *f)
554b8938c1SPoul-Henning Kamp {
5691cca30dSPawel Jakub Dawidek 	unsigned int i;
574b8938c1SPoul-Henning Kamp 	int j;
5863728c47SPoul-Henning Kamp 	struct gctl_req_arg *ap;
594b8938c1SPoul-Henning Kamp 
604b8938c1SPoul-Henning Kamp 	if (req == NULL) {
6163728c47SPoul-Henning Kamp 		fprintf(f, "Dump of gctl request at NULL\n");
624b8938c1SPoul-Henning Kamp 		return;
634b8938c1SPoul-Henning Kamp 	}
647e02e189SPoul-Henning Kamp 	fprintf(f, "Dump of gctl request at %p:\n", req);
654b8938c1SPoul-Henning Kamp 	if (req->error != NULL)
664b8938c1SPoul-Henning Kamp 		fprintf(f, "  error:\t\"%s\"\n", req->error);
674b8938c1SPoul-Henning Kamp 	else
684b8938c1SPoul-Henning Kamp 		fprintf(f, "  error:\tNULL\n");
694b8938c1SPoul-Henning Kamp 	for (i = 0; i < req->narg; i++) {
704b8938c1SPoul-Henning Kamp 		ap = &req->arg[i];
717e02e189SPoul-Henning Kamp 		fprintf(f, "  param:\t\"%s\" (%d)", ap->name, ap->nlen);
7263728c47SPoul-Henning Kamp 		fprintf(f, " [%s%s",
7363728c47SPoul-Henning Kamp 		    ap->flag & GCTL_PARAM_RD ? "R" : "",
7463728c47SPoul-Henning Kamp 		    ap->flag & GCTL_PARAM_WR ? "W" : "");
754b8938c1SPoul-Henning Kamp 		fflush(f);
7663728c47SPoul-Henning Kamp 		if (ap->flag & GCTL_PARAM_ASCII)
7763728c47SPoul-Henning Kamp 			fprintf(f, "%d] = \"%s\"", ap->len, (char *)ap->value);
784b8938c1SPoul-Henning Kamp 		else if (ap->len > 0) {
7963728c47SPoul-Henning Kamp 			fprintf(f, "%d] = ", ap->len);
804b8938c1SPoul-Henning Kamp 			fflush(f);
814b8938c1SPoul-Henning Kamp 			for (j = 0; j < ap->len; j++) {
824b8938c1SPoul-Henning Kamp 				fprintf(f, " %02x", ((u_char *)ap->value)[j]);
834b8938c1SPoul-Henning Kamp 			}
844b8938c1SPoul-Henning Kamp 		} else {
8563728c47SPoul-Henning Kamp 			fprintf(f, "0] = %p", ap->value);
864b8938c1SPoul-Henning Kamp 		}
874b8938c1SPoul-Henning Kamp 		fprintf(f, "\n");
884b8938c1SPoul-Henning Kamp 	}
894b8938c1SPoul-Henning Kamp }
904b8938c1SPoul-Henning Kamp 
9163728c47SPoul-Henning Kamp /*
9263728c47SPoul-Henning Kamp  * Set an error message, if one does not already exist.
9363728c47SPoul-Henning Kamp  */
944b8938c1SPoul-Henning Kamp static void
gctl_set_error(struct gctl_req * req,const char * error,...)9563728c47SPoul-Henning Kamp gctl_set_error(struct gctl_req *req, const char *error, ...)
964b8938c1SPoul-Henning Kamp {
974b8938c1SPoul-Henning Kamp 	va_list ap;
984b8938c1SPoul-Henning Kamp 
994b8938c1SPoul-Henning Kamp 	if (req->error != NULL)
1004b8938c1SPoul-Henning Kamp 		return;
1014b8938c1SPoul-Henning Kamp 	va_start(ap, error);
1024b8938c1SPoul-Henning Kamp 	vasprintf(&req->error, error, ap);
10363728c47SPoul-Henning Kamp 	va_end(ap);
1044b8938c1SPoul-Henning Kamp }
1054b8938c1SPoul-Henning Kamp 
10663728c47SPoul-Henning Kamp /*
10763728c47SPoul-Henning Kamp  * Check that a malloc operation succeeded, and set a consistent error
10863728c47SPoul-Henning Kamp  * message if not.
10963728c47SPoul-Henning Kamp  */
1104b8938c1SPoul-Henning Kamp static void
gctl_check_alloc(struct gctl_req * req,void * ptr)11163728c47SPoul-Henning Kamp gctl_check_alloc(struct gctl_req *req, void *ptr)
1124b8938c1SPoul-Henning Kamp {
1137b6942a1SUlf Lilleengen 
1144b8938c1SPoul-Henning Kamp 	if (ptr != NULL)
1154b8938c1SPoul-Henning Kamp 		return;
1167b6942a1SUlf Lilleengen 	gctl_set_error(req, nomemmsg);
11763728c47SPoul-Henning Kamp 	if (req->error == NULL)
1187b6942a1SUlf Lilleengen 		req->error = nomemmsg;
1194b8938c1SPoul-Henning Kamp }
1204b8938c1SPoul-Henning Kamp 
12163728c47SPoul-Henning Kamp /*
12263728c47SPoul-Henning Kamp  * Allocate a new request handle of the specified type.
12363728c47SPoul-Henning Kamp  * XXX: Why bother checking the type ?
12463728c47SPoul-Henning Kamp  */
12563728c47SPoul-Henning Kamp struct gctl_req *
gctl_get_handle(void)1267e02e189SPoul-Henning Kamp gctl_get_handle(void)
1274b8938c1SPoul-Henning Kamp {
1284b8938c1SPoul-Henning Kamp 
12991cca30dSPawel Jakub Dawidek 	return (calloc(1, sizeof(struct gctl_req)));
1304b8938c1SPoul-Henning Kamp }
1314b8938c1SPoul-Henning Kamp 
13263728c47SPoul-Henning Kamp /*
13363728c47SPoul-Henning Kamp  * Allocate space for another argument.
13463728c47SPoul-Henning Kamp  */
13563728c47SPoul-Henning Kamp static struct gctl_req_arg *
gctl_new_arg(struct gctl_req * req)13663728c47SPoul-Henning Kamp gctl_new_arg(struct gctl_req *req)
1374b8938c1SPoul-Henning Kamp {
13863728c47SPoul-Henning Kamp 	struct gctl_req_arg *ap;
1394b8938c1SPoul-Henning Kamp 
1404b8938c1SPoul-Henning Kamp 	req->narg++;
1417b6942a1SUlf Lilleengen 	req->arg = reallocf(req->arg, sizeof *ap * req->narg);
14263728c47SPoul-Henning Kamp 	gctl_check_alloc(req, req->arg);
14363728c47SPoul-Henning Kamp 	if (req->arg == NULL) {
1444b8938c1SPoul-Henning Kamp 		req->narg = 0;
14563728c47SPoul-Henning Kamp 		return (NULL);
14663728c47SPoul-Henning Kamp 	}
14763728c47SPoul-Henning Kamp 	ap = req->arg + (req->narg - 1);
14863728c47SPoul-Henning Kamp 	memset(ap, 0, sizeof *ap);
14963728c47SPoul-Henning Kamp 	return (ap);
15063728c47SPoul-Henning Kamp }
15163728c47SPoul-Henning Kamp 
1522117cdd4SAlexander Motin void
gctl_add_param(struct gctl_req * req,const char * name,int len,void * value,int flag)1532117cdd4SAlexander Motin gctl_add_param(struct gctl_req *req, const char *name, int len, void *value,
15442e2e899SPawel Jakub Dawidek     int flag)
1554b8938c1SPoul-Henning Kamp {
15663728c47SPoul-Henning Kamp 	struct gctl_req_arg *ap;
1574b8938c1SPoul-Henning Kamp 
1584b8938c1SPoul-Henning Kamp 	if (req == NULL || req->error != NULL)
1594b8938c1SPoul-Henning Kamp 		return;
16063728c47SPoul-Henning Kamp 	ap = gctl_new_arg(req);
16163728c47SPoul-Henning Kamp 	if (ap == NULL)
1624b8938c1SPoul-Henning Kamp 		return;
16363728c47SPoul-Henning Kamp 	ap->name = strdup(name);
16463728c47SPoul-Henning Kamp 	gctl_check_alloc(req, ap->name);
1657b6942a1SUlf Lilleengen 	if (ap->name == NULL)
1667b6942a1SUlf Lilleengen 		return;
16763728c47SPoul-Henning Kamp 	ap->nlen = strlen(ap->name) + 1;
1684b8938c1SPoul-Henning Kamp 	ap->value = value;
16942e2e899SPawel Jakub Dawidek 	ap->flag = flag;
17063728c47SPoul-Henning Kamp 	if (len >= 0)
17163728c47SPoul-Henning Kamp 		ap->len = len;
17242e2e899SPawel Jakub Dawidek 	else if (len < 0) {
17342e2e899SPawel Jakub Dawidek 		ap->flag |= GCTL_PARAM_ASCII;
17463728c47SPoul-Henning Kamp 		ap->len = strlen(value) + 1;
17563728c47SPoul-Henning Kamp 	}
17642e2e899SPawel Jakub Dawidek }
17742e2e899SPawel Jakub Dawidek 
17842e2e899SPawel Jakub Dawidek void
gctl_ro_param(struct gctl_req * req,const char * name,int len,const void * value)17942e2e899SPawel Jakub Dawidek gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
18042e2e899SPawel Jakub Dawidek {
18142e2e899SPawel Jakub Dawidek 
1822117cdd4SAlexander Motin 	gctl_add_param(req, name, len, __DECONST(void *, value), GCTL_PARAM_RD);
18342e2e899SPawel Jakub Dawidek }
18442e2e899SPawel Jakub Dawidek 
18542e2e899SPawel Jakub Dawidek void
gctl_rw_param(struct gctl_req * req,const char * name,int len,void * value)18642e2e899SPawel Jakub Dawidek gctl_rw_param(struct gctl_req *req, const char *name, int len, void *value)
18742e2e899SPawel Jakub Dawidek {
18842e2e899SPawel Jakub Dawidek 
1892117cdd4SAlexander Motin 	gctl_add_param(req, name, len, value, GCTL_PARAM_RW);
19042e2e899SPawel Jakub Dawidek }
19163728c47SPoul-Henning Kamp 
1924b8938c1SPoul-Henning Kamp const char *
gctl_issue(struct gctl_req * req)19363728c47SPoul-Henning Kamp gctl_issue(struct gctl_req *req)
1944b8938c1SPoul-Henning Kamp {
19590e29718SKirk McKusick 	int fd;
1964b8938c1SPoul-Henning Kamp 
1974b8938c1SPoul-Henning Kamp 	if (req == NULL)
1984b8938c1SPoul-Henning Kamp 		return ("NULL request pointer");
1994b8938c1SPoul-Henning Kamp 	if (req->error != NULL)
2004b8938c1SPoul-Henning Kamp 		return (req->error);
2014b8938c1SPoul-Henning Kamp 
20263728c47SPoul-Henning Kamp 	req->version = GCTL_VERSION;
2034b8938c1SPoul-Henning Kamp 	req->lerror = BUFSIZ;		/* XXX: arbitrary number */
2047b6942a1SUlf Lilleengen 	req->error = calloc(1, req->lerror);
20563728c47SPoul-Henning Kamp 	if (req->error == NULL) {
20663728c47SPoul-Henning Kamp 		gctl_check_alloc(req, req->error);
20763728c47SPoul-Henning Kamp 		return (req->error);
20863728c47SPoul-Henning Kamp 	}
2094b8938c1SPoul-Henning Kamp 	req->lerror--;
2104b8938c1SPoul-Henning Kamp 	fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
2114b8938c1SPoul-Henning Kamp 	if (fd < 0)
2124b8938c1SPoul-Henning Kamp 		return(strerror(errno));
21390e29718SKirk McKusick 	req->nerror = ioctl(fd, GEOM_CTL, req);
21463728c47SPoul-Henning Kamp 	close(fd);
21563728c47SPoul-Henning Kamp 	if (req->error[0] != '\0')
2164b8938c1SPoul-Henning Kamp 		return (req->error);
21790e29718SKirk McKusick 	if (req->nerror == -1)
2184b8938c1SPoul-Henning Kamp 		return(strerror(errno));
2194b8938c1SPoul-Henning Kamp 	return (NULL);
2204b8938c1SPoul-Henning Kamp }
2214b8938c1SPoul-Henning Kamp 
2224b8938c1SPoul-Henning Kamp void
gctl_free(struct gctl_req * req)22363728c47SPoul-Henning Kamp gctl_free(struct gctl_req *req)
2244b8938c1SPoul-Henning Kamp {
22591cca30dSPawel Jakub Dawidek 	unsigned int i;
2264b8938c1SPoul-Henning Kamp 
22763728c47SPoul-Henning Kamp 	if (req == NULL)
22863728c47SPoul-Henning Kamp 		return;
2294b8938c1SPoul-Henning Kamp 	for (i = 0; i < req->narg; i++) {
2304b8938c1SPoul-Henning Kamp 		if (req->arg[i].name != NULL)
2314b8938c1SPoul-Henning Kamp 			free(req->arg[i].name);
2324b8938c1SPoul-Henning Kamp 	}
23363728c47SPoul-Henning Kamp 	free(req->arg);
2347b6942a1SUlf Lilleengen 	if (req->error != NULL && req->error != nomemmsg)
2354b8938c1SPoul-Henning Kamp 		free(req->error);
2364b8938c1SPoul-Henning Kamp 	free(req);
2374b8938c1SPoul-Henning Kamp }
238