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