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