1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2003 Poul-Henning Kamp
5 * Copyright (c) 1996 The NetBSD Foundation, Inc.
6 * All rights reserved.
7 *
8 * This code is derived from software contributed to The NetBSD Foundation
9 * by Jason R. Thorpe.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 *
32 * NetBSD: ccdconfig.c,v 1.6 1996/05/16 07:11:18 thorpej Exp $
33 */
34
35 #include <sys/cdefs.h>
36 #include <sys/param.h>
37 #include <sys/linker.h>
38 #include <sys/module.h>
39 #include <ctype.h>
40 #include <err.h>
41 #include <errno.h>
42 #include <limits.h>
43 #include <paths.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47 #include <unistd.h>
48 #include <libgeom.h>
49
50 #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */
51 #define CCDF_MIRROR 0x04 /* use mirroring */
52 #define CCDF_NO_OFFSET 0x08 /* do not leave space in front */
53 #define CCDF_LINUX 0x10 /* use Linux compatibility mode */
54
55 #include "pathnames.h"
56
57 static int lineno = 0;
58 static int verbose = 0;
59 static const char *ccdconf = _PATH_CCDCONF;
60
61 static struct flagval {
62 const char *fv_flag;
63 int fv_val;
64 } flagvaltab[] = {
65 { "CCDF_UNIFORM", CCDF_UNIFORM },
66 { "uniform", CCDF_UNIFORM },
67 { "CCDF_MIRROR", CCDF_MIRROR },
68 { "mirror", CCDF_MIRROR },
69 { "CCDF_NO_OFFSET", CCDF_NO_OFFSET },
70 { "no_offset", CCDF_NO_OFFSET },
71 { "CCDF_LINUX", CCDF_LINUX },
72 { "linux", CCDF_LINUX },
73 { "none", 0 },
74 { NULL, 0 },
75 };
76
77 #define CCD_CONFIG 0 /* configure a device */
78 #define CCD_CONFIGALL 1 /* configure all devices */
79 #define CCD_UNCONFIG 2 /* unconfigure a device */
80 #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
81 #define CCD_DUMP 4 /* dump a ccd's configuration */
82
83 static int do_single(int, char **, int);
84 static int do_all(int);
85 static int dump_ccd(int, char **);
86 static int flags_to_val(char *);
87 static int resolve_ccdname(char *);
88 static void usage(void);
89
90 int
main(int argc,char * argv[])91 main(int argc, char *argv[])
92 {
93 int ch, options = 0, action = CCD_CONFIG;
94
95 while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
96 switch (ch) {
97 case 'c':
98 action = CCD_CONFIG;
99 ++options;
100 break;
101
102 case 'C':
103 action = CCD_CONFIGALL;
104 ++options;
105 break;
106
107 case 'f':
108 ccdconf = optarg;
109 break;
110
111 case 'g':
112 action = CCD_DUMP;
113 break;
114
115 case 'u':
116 action = CCD_UNCONFIG;
117 ++options;
118 break;
119
120 case 'U':
121 action = CCD_UNCONFIGALL;
122 ++options;
123 break;
124
125 case 'v':
126 verbose = 1;
127 break;
128
129 default:
130 usage();
131 }
132 }
133 argc -= optind;
134 argv += optind;
135
136 if (options > 1)
137 usage();
138
139 if (modfind("g_ccd") < 0) {
140 /* Not present in kernel, try loading it */
141 if (kldload("geom_ccd") < 0 || modfind("g_ccd") < 0)
142 warn("geom_ccd module not available!");
143 }
144
145 switch (action) {
146 case CCD_CONFIG:
147 case CCD_UNCONFIG:
148 exit(do_single(argc, argv, action));
149 /* NOTREACHED */
150
151 case CCD_CONFIGALL:
152 case CCD_UNCONFIGALL:
153 exit(do_all(action));
154 /* NOTREACHED */
155
156 case CCD_DUMP:
157 exit(dump_ccd(argc, argv));
158 /* NOTREACHED */
159 }
160 /* NOTREACHED */
161 return (0);
162 }
163
164 static int
do_single(int argc,char ** argv,int action)165 do_single(int argc, char **argv, int action)
166 {
167 char *cp, *cp2;
168 int ccd, noflags = 0, i, ileave, flags = 0;
169 struct gctl_req *grq;
170 char const *errstr;
171 char buf1[BUFSIZ];
172 int ex;
173
174 /*
175 * If unconfiguring, all arguments are treated as ccds.
176 */
177 if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
178 ex = 0;
179 for (; argc != 0;) {
180 cp = *argv++; --argc;
181 if ((ccd = resolve_ccdname(cp)) < 0) {
182 warnx("invalid ccd name: %s", cp);
183 continue;
184 }
185 grq = gctl_get_handle();
186 gctl_ro_param(grq, "verb", -1, "destroy geom");
187 gctl_ro_param(grq, "class", -1, "CCD");
188 sprintf(buf1, "ccd%d", ccd);
189 gctl_ro_param(grq, "geom", -1, buf1);
190 errstr = gctl_issue(grq);
191 if (errstr == NULL) {
192 if (verbose)
193 printf("%s unconfigured\n", cp);
194 gctl_free(grq);
195 continue;
196 }
197 warnx(
198 "%s\nor possibly kernel and ccdconfig out of sync",
199 errstr);
200 ex = 1;
201 }
202 return (ex);
203 }
204
205 /* Make sure there are enough arguments. */
206 if (argc < 4) {
207 if (argc == 3) {
208 /* Assume that no flags are specified. */
209 noflags = 1;
210 } else {
211 if (action == CCD_CONFIGALL) {
212 warnx("%s: bad line: %d", ccdconf, lineno);
213 return (1);
214 } else
215 usage();
216 }
217 }
218
219 /* First argument is the ccd to configure. */
220 cp = *argv++; --argc;
221 if ((ccd = resolve_ccdname(cp)) < 0) {
222 warnx("invalid ccd name: %s", cp);
223 return (1);
224 }
225
226 /* Next argument is the interleave factor. */
227 cp = *argv++; --argc;
228 errno = 0; /* to check for ERANGE */
229 ileave = (int)strtol(cp, &cp2, 10);
230 if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
231 warnx("invalid interleave factor: %s", cp);
232 return (1);
233 }
234
235 if (noflags == 0) {
236 /* Next argument is the ccd configuration flags. */
237 cp = *argv++; --argc;
238 if ((flags = flags_to_val(cp)) < 0) {
239 warnx("invalid flags argument: %s", cp);
240 return (1);
241 }
242 }
243 grq = gctl_get_handle();
244 gctl_ro_param(grq, "verb", -1, "create geom");
245 gctl_ro_param(grq, "class", -1, "CCD");
246 gctl_ro_param(grq, "unit", sizeof(ccd), &ccd);
247 gctl_ro_param(grq, "ileave", sizeof(ileave), &ileave);
248 if (flags & CCDF_UNIFORM)
249 gctl_ro_param(grq, "uniform", -1, "");
250 if (flags & CCDF_MIRROR)
251 gctl_ro_param(grq, "mirror", -1, "");
252 if (flags & CCDF_NO_OFFSET)
253 gctl_ro_param(grq, "no_offset", -1, "");
254 if (flags & CCDF_LINUX)
255 gctl_ro_param(grq, "linux", -1, "");
256 gctl_ro_param(grq, "nprovider", sizeof(argc), &argc);
257 for (i = 0; i < argc; i++) {
258 sprintf(buf1, "provider%d", i);
259 cp = argv[i];
260 if (!strncmp(cp, _PATH_DEV, strlen(_PATH_DEV)))
261 cp += strlen(_PATH_DEV);
262 gctl_ro_param(grq, buf1, -1, cp);
263 }
264 buf1[0] = '\0';
265 gctl_add_param(grq, "output", sizeof(buf1), buf1,
266 GCTL_PARAM_WR | GCTL_PARAM_ASCII);
267 errstr = gctl_issue(grq);
268 if (errstr == NULL) {
269 if (verbose) {
270 printf("%s", buf1);
271 }
272 gctl_free(grq);
273 return (0);
274 }
275 warnx(
276 "%s\nor possibly kernel and ccdconfig out of sync",
277 errstr);
278 return (1);
279 }
280
281 static int
do_all(int action)282 do_all(int action)
283 {
284 FILE *f;
285 char line[_POSIX2_LINE_MAX];
286 char *cp, **argv;
287 int argc, rval;
288 gid_t egid;
289
290 rval = 0;
291 egid = getegid();
292 if (setegid(getgid()) != 0)
293 err(1, "setegid failed");
294 if ((f = fopen(ccdconf, "r")) == NULL) {
295 if (setegid(egid) != 0)
296 err(1, "setegid failed");
297 warn("fopen: %s", ccdconf);
298 return (1);
299 }
300 if (setegid(egid) != 0)
301 err(1, "setegid failed");
302
303 while (fgets(line, sizeof(line), f) != NULL) {
304 argc = 0;
305 argv = NULL;
306 ++lineno;
307 if ((cp = strrchr(line, '\n')) != NULL)
308 *cp = '\0';
309
310 /* Break up the line and pass it's contents to do_single(). */
311 if (line[0] == '\0')
312 goto end_of_line;
313 for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
314 if (*cp == '#')
315 break;
316 if ((argv = realloc(argv,
317 sizeof(char *) * ++argc)) == NULL) {
318 warnx("no memory to configure ccds");
319 return (1);
320 }
321 argv[argc - 1] = cp;
322 /*
323 * If our action is to unconfigure all, then pass
324 * just the first token to do_single() and ignore
325 * the rest. Since this will be encountered on
326 * our first pass through the line, the Right
327 * Thing will happen.
328 */
329 if (action == CCD_UNCONFIGALL) {
330 if (do_single(argc, argv, action))
331 rval = 1;
332 goto end_of_line;
333 }
334 }
335 if (argc != 0)
336 if (do_single(argc, argv, action))
337 rval = 1;
338
339 end_of_line:
340 if (argv != NULL)
341 free(argv);
342 }
343
344 (void)fclose(f);
345 return (rval);
346 }
347
348 static int
resolve_ccdname(char * name)349 resolve_ccdname(char *name)
350 {
351
352 if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
353 name += strlen(_PATH_DEV);
354 if (strncmp(name, "ccd", 3))
355 return -1;
356 name += 3;
357 if (!isdigit(*name))
358 return -1;
359 return (strtoul(name, NULL, 10));
360 }
361
362 static int
dumpout(int unit)363 dumpout(int unit)
364 {
365 static int v;
366 struct gctl_req *grq;
367 int ncp;
368 char *cp;
369 char const *errstr;
370
371 grq = gctl_get_handle();
372 ncp = 65536;
373 cp = malloc(ncp);
374 cp[0] = '\0';
375 gctl_ro_param(grq, "verb", -1, "list");
376 gctl_ro_param(grq, "class", -1, "CCD");
377 gctl_ro_param(grq, "unit", sizeof(unit), &unit);
378 gctl_add_param(grq, "output", ncp, cp,
379 GCTL_PARAM_WR | GCTL_PARAM_ASCII);
380 errstr = gctl_issue(grq);
381 if (errstr != NULL)
382 errx(1, "%s\nor possibly kernel and ccdconfig out of sync",
383 errstr);
384 if (strlen(cp) == 0)
385 errx(1, "ccd%d not configured", unit);
386 if (verbose && !v) {
387 printf("# ccd\t\tileave\tflags\tcomponent devices\n");
388 v = 1;
389 }
390 printf("%s", cp);
391 free(cp);
392 return (0);
393 }
394
395 static int
dump_ccd(int argc,char ** argv)396 dump_ccd(int argc, char **argv)
397 {
398 int i, error;
399
400 if (argc == 0) {
401 error = dumpout(-1);
402 } else {
403 error = 0;
404 for (i = 0; error == 0 && i < argc; i++)
405 error = dumpout(resolve_ccdname(argv[i]));
406 }
407 return (error);
408 }
409
410 static int
flags_to_val(char * flags)411 flags_to_val(char *flags)
412 {
413 char *cp, *tok;
414 int i, tmp, val;
415
416 errno = 0; /* to check for ERANGE */
417 val = (int)strtol(flags, &cp, 0);
418 if ((errno != ERANGE) && (*cp == '\0')) {
419 if (val & ~(CCDF_UNIFORM|CCDF_MIRROR))
420 return (-1);
421 return (val);
422 }
423
424 /* Check for values represented by strings. */
425 if ((cp = strdup(flags)) == NULL)
426 err(1, "no memory to parse flags");
427 tmp = 0;
428 for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
429 for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
430 if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
431 break;
432 if (flagvaltab[i].fv_flag == NULL) {
433 free(cp);
434 return (-1);
435 }
436 tmp |= flagvaltab[i].fv_val;
437 }
438
439 /* If we get here, the string was ok. */
440 free(cp);
441 return (tmp);
442 }
443
444 static void
usage(void)445 usage(void)
446 {
447 fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
448 "usage: ccdconfig [-cv] ccd ileave [flags] dev ...",
449 " ccdconfig -C [-v] [-f config_file]",
450 " ccdconfig -u [-v] ccd ...",
451 " ccdconfig -U [-v] [-f config_file]",
452 " ccdconfig -g [ccd ...]");
453 exit(1);
454 }
455