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$
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
153static void
154gctl_param_add(struct gctl_req *req, const char *name, int len, void *value,
155    int flag)
156{
157	struct gctl_req_arg *ap;
158
159	if (req == NULL || req->error != NULL)
160		return;
161	ap = gctl_new_arg(req);
162	if (ap == NULL)
163		return;
164	ap->name = strdup(name);
165	gctl_check_alloc(req, ap->name);
166	if (ap->name == NULL)
167		return;
168	ap->nlen = strlen(ap->name) + 1;
169	ap->value = value;
170	ap->flag = flag;
171	if (len >= 0)
172		ap->len = len;
173	else if (len < 0) {
174		ap->flag |= GCTL_PARAM_ASCII;
175		ap->len = strlen(value) + 1;
176	}
177}
178
179void
180gctl_ro_param(struct gctl_req *req, const char *name, int len, const void* value)
181{
182
183	gctl_param_add(req, name, len, __DECONST(void *, value), GCTL_PARAM_RD);
184}
185
186void
187gctl_rw_param(struct gctl_req *req, const char *name, int len, void *value)
188{
189
190	gctl_param_add(req, name, len, value, GCTL_PARAM_RW);
191}
192
193const char *
194gctl_issue(struct gctl_req *req)
195{
196	int fd, error;
197
198	if (req == NULL)
199		return ("NULL request pointer");
200	if (req->error != NULL)
201		return (req->error);
202
203	req->version = GCTL_VERSION;
204	req->lerror = BUFSIZ;		/* XXX: arbitrary number */
205	req->error = calloc(1, req->lerror);
206	if (req->error == NULL) {
207		gctl_check_alloc(req, req->error);
208		return (req->error);
209	}
210	req->lerror--;
211	fd = open(_PATH_DEV PATH_GEOM_CTL, O_RDONLY);
212	if (fd < 0)
213		return(strerror(errno));
214	error = ioctl(fd, GEOM_CTL, req);
215	close(fd);
216	if (req->error[0] != '\0')
217		return (req->error);
218	if (error != 0)
219		return(strerror(errno));
220	return (NULL);
221}
222
223void
224gctl_free(struct gctl_req *req)
225{
226	unsigned int i;
227
228	if (req == NULL)
229		return;
230	for (i = 0; i < req->narg; i++) {
231		if (req->arg[i].name != NULL)
232			free(req->arg[i].name);
233	}
234	free(req->arg);
235	if (req->error != NULL && req->error != nomemmsg)
236		free(req->error);
237	free(req);
238}
239