geom_ctl.c revision 214128
1/*-
2 * Copyright (c) 2003 Poul-Henning Kamp
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 3. The names of the authors may not be used to endorse or promote
14 *    products derived from this software without specific prior written
15 *    permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * $FreeBSD: head/lib/libgeom/geom_ctl.c 214128 2010-10-21 10:36:36Z pjd $
30 */
31
32#include <stdio.h>
33#include <fcntl.h>
34#include <errno.h>
35#include <stdint.h>
36#include <sys/types.h>
37#include <stdarg.h>
38#include <unistd.h>
39#include <string.h>
40#include <stdlib.h>
41#include <paths.h>
42
43#include <sys/queue.h>
44
45#define GCTL_TABLE 1
46#include <libgeom.h>
47
48/*
49 * Global pointer to a string that is used to avoid an errorneous free in
50 * gctl_free.
51 */
52static char nomemmsg[] = "Could not allocate memory";
53
54void
55gctl_dump(struct gctl_req *req, FILE *f)
56{
57	unsigned int i;
58	int j;
59	struct gctl_req_arg *ap;
60
61	if (req == NULL) {
62		fprintf(f, "Dump of gctl request at NULL\n");
63		return;
64	}
65	fprintf(f, "Dump of gctl request at %p:\n", req);
66	if (req->error != NULL)
67		fprintf(f, "  error:\t\"%s\"\n", req->error);
68	else
69		fprintf(f, "  error:\tNULL\n");
70	for (i = 0; i < req->narg; i++) {
71		ap = &req->arg[i];
72		fprintf(f, "  param:\t\"%s\" (%d)", ap->name, ap->nlen);
73		fprintf(f, " [%s%s",
74		    ap->flag & GCTL_PARAM_RD ? "R" : "",
75		    ap->flag & GCTL_PARAM_WR ? "W" : "");
76		fflush(f);
77		if (ap->flag & GCTL_PARAM_ASCII)
78			fprintf(f, "%d] = \"%s\"", ap->len, (char *)ap->value);
79		else if (ap->len > 0) {
80			fprintf(f, "%d] = ", ap->len);
81			fflush(f);
82			for (j = 0; j < ap->len; j++) {
83				fprintf(f, " %02x", ((u_char *)ap->value)[j]);
84			}
85		} else {
86			fprintf(f, "0] = %p", ap->value);
87		}
88		fprintf(f, "\n");
89	}
90}
91
92/*
93 * Set an error message, if one does not already exist.
94 */
95static void
96gctl_set_error(struct gctl_req *req, const char *error, ...)
97{
98	va_list ap;
99
100	if (req->error != NULL)
101		return;
102	va_start(ap, error);
103	vasprintf(&req->error, error, ap);
104	va_end(ap);
105}
106
107/*
108 * Check that a malloc operation succeeded, and set a consistent error
109 * message if not.
110 */
111static void
112gctl_check_alloc(struct gctl_req *req, void *ptr)
113{
114
115	if (ptr != NULL)
116		return;
117	gctl_set_error(req, nomemmsg);
118	if (req->error == NULL)
119		req->error = nomemmsg;
120}
121
122/*
123 * Allocate a new request handle of the specified type.
124 * XXX: Why bother checking the type ?
125 */
126struct gctl_req *
127gctl_get_handle(void)
128{
129
130	return (calloc(1, sizeof(struct gctl_req)));
131}
132
133/*
134 * Allocate space for another argument.
135 */
136static struct gctl_req_arg *
137gctl_new_arg(struct gctl_req *req)
138{
139	struct gctl_req_arg *ap;
140
141	req->narg++;
142	req->arg = reallocf(req->arg, sizeof *ap * req->narg);
143	gctl_check_alloc(req, req->arg);
144	if (req->arg == NULL) {
145		req->narg = 0;
146		return (NULL);
147	}
148	ap = req->arg + (req->narg - 1);
149	memset(ap, 0, sizeof *ap);
150	return (ap);
151}
152
153void
154gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
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 = __DECONST(void *, value);
169	ap->flag = GCTL_PARAM_RD;
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
178void
179gctl_rw_param(struct gctl_req *req, const char *name, int len, void* value)
180{
181	struct gctl_req_arg *ap;
182
183	if (req == NULL || req->error != NULL)
184		return;
185	ap = gctl_new_arg(req);
186	if (ap == NULL)
187		return;
188	ap->name = strdup(name);
189	gctl_check_alloc(req, ap->name);
190	if (ap->name == NULL)
191		return;
192	ap->nlen = strlen(ap->name) + 1;
193	ap->value = value;
194	ap->flag = GCTL_PARAM_RW;
195	if (len >= 0)
196		ap->len = len;
197	else if (len < 0)
198		ap->len = strlen(value) + 1;
199}
200
201const char *
202gctl_issue(struct gctl_req *req)
203{
204	int fd, error;
205
206	if (req == NULL)
207		return ("NULL request pointer");
208	if (req->error != NULL)
209		return (req->error);
210
211	req->version = GCTL_VERSION;
212	req->lerror = BUFSIZ;		/* XXX: arbitrary number */
213	req->error = calloc(1, req->lerror);
214	if (req->error == NULL) {
215		gctl_check_alloc(req, req->error);
216		return (req->error);
217	}
218	req->lerror--;
219	fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
220	if (fd < 0)
221		return(strerror(errno));
222	error = ioctl(fd, GEOM_CTL, req);
223	close(fd);
224	if (req->error[0] != '\0')
225		return (req->error);
226	if (error != 0)
227		return(strerror(errno));
228	return (NULL);
229}
230
231void
232gctl_free(struct gctl_req *req)
233{
234	unsigned int i;
235
236	if (req == NULL)
237		return;
238	for (i = 0; i < req->narg; i++) {
239		if (req->arg[i].name != NULL)
240			free(req->arg[i].name);
241	}
242	free(req->arg);
243	if (req->error != NULL && req->error != nomemmsg)
244		free(req->error);
245	free(req);
246}
247