ccdconfig.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  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. #include <sys/param.h>
  35. #include <sys/linker.h>
  36. #include <sys/module.h>
  37. #include <ctype.h>
  38. #include <err.h>
  39. #include <errno.h>
  40. #include <limits.h>
  41. #include <paths.h>
  42. #include <stdio.h>
  43. #include <stdlib.h>
  44. #include <string.h>
  45. #include <unistd.h>
  46. #include <libgeom.h>
  47. #define CCDF_UNIFORM 0x02 /* use LCCD of sizes for uniform interleave */
  48. #define CCDF_MIRROR 0x04 /* use mirroring */
  49. #define CCDF_NO_OFFSET 0x08 /* do not leave space in front */
  50. #define CCDF_LINUX 0x10 /* use Linux compatibility mode */
  51. #include "pathnames.h"
  52. static int lineno = 0;
  53. static int verbose = 0;
  54. static const char *ccdconf = _PATH_CCDCONF;
  55. static struct flagval {
  56. const char *fv_flag;
  57. int fv_val;
  58. } flagvaltab[] = {
  59. { "CCDF_UNIFORM", CCDF_UNIFORM },
  60. { "uniform", CCDF_UNIFORM },
  61. { "CCDF_MIRROR", CCDF_MIRROR },
  62. { "mirror", CCDF_MIRROR },
  63. { "CCDF_NO_OFFSET", CCDF_NO_OFFSET },
  64. { "no_offset", CCDF_NO_OFFSET },
  65. { "CCDF_LINUX", CCDF_LINUX },
  66. { "linux", CCDF_LINUX },
  67. { "none", 0 },
  68. { NULL, 0 },
  69. };
  70. #define CCD_CONFIG 0 /* configure a device */
  71. #define CCD_CONFIGALL 1 /* configure all devices */
  72. #define CCD_UNCONFIG 2 /* unconfigure a device */
  73. #define CCD_UNCONFIGALL 3 /* unconfigure all devices */
  74. #define CCD_DUMP 4 /* dump a ccd's configuration */
  75. static int do_single(int, char **, int);
  76. static int do_all(int);
  77. static int dump_ccd(int, char **);
  78. static int flags_to_val(char *);
  79. static int resolve_ccdname(char *);
  80. static void usage(void);
  81. int
  82. main(int argc, char *argv[])
  83. {
  84. int ch, options = 0, action = CCD_CONFIG;
  85. while ((ch = getopt(argc, argv, "cCf:guUv")) != -1) {
  86. switch (ch) {
  87. case 'c':
  88. action = CCD_CONFIG;
  89. ++options;
  90. break;
  91. case 'C':
  92. action = CCD_CONFIGALL;
  93. ++options;
  94. break;
  95. case 'f':
  96. ccdconf = optarg;
  97. break;
  98. case 'g':
  99. action = CCD_DUMP;
  100. break;
  101. case 'u':
  102. action = CCD_UNCONFIG;
  103. ++options;
  104. break;
  105. case 'U':
  106. action = CCD_UNCONFIGALL;
  107. ++options;
  108. break;
  109. case 'v':
  110. verbose = 1;
  111. break;
  112. default:
  113. usage();
  114. }
  115. }
  116. argc -= optind;
  117. argv += optind;
  118. if (options > 1)
  119. usage();
  120. if (modfind("g_ccd") < 0) {
  121. /* Not present in kernel, try loading it */
  122. if (kldload("geom_ccd") < 0 || modfind("g_ccd") < 0)
  123. warn("geom_ccd module not available!");
  124. }
  125. switch (action) {
  126. case CCD_CONFIG:
  127. case CCD_UNCONFIG:
  128. exit(do_single(argc, argv, action));
  129. /* NOTREACHED */
  130. case CCD_CONFIGALL:
  131. case CCD_UNCONFIGALL:
  132. exit(do_all(action));
  133. /* NOTREACHED */
  134. case CCD_DUMP:
  135. exit(dump_ccd(argc, argv));
  136. /* NOTREACHED */
  137. }
  138. /* NOTREACHED */
  139. return (0);
  140. }
  141. static int
  142. do_single(int argc, char **argv, int action)
  143. {
  144. char *cp, *cp2;
  145. int ccd, noflags = 0, i, ileave, flags = 0;
  146. struct gctl_req *grq;
  147. char const *errstr;
  148. char buf1[BUFSIZ];
  149. int ex;
  150. /*
  151. * If unconfiguring, all arguments are treated as ccds.
  152. */
  153. if (action == CCD_UNCONFIG || action == CCD_UNCONFIGALL) {
  154. ex = 0;
  155. for (; argc != 0;) {
  156. cp = *argv++; --argc;
  157. if ((ccd = resolve_ccdname(cp)) < 0) {
  158. warnx("invalid ccd name: %s", cp);
  159. continue;
  160. }
  161. grq = gctl_get_handle();
  162. gctl_ro_param(grq, "verb", -1, "destroy geom");
  163. gctl_ro_param(grq, "class", -1, "CCD");
  164. sprintf(buf1, "ccd%d", ccd);
  165. gctl_ro_param(grq, "geom", -1, buf1);
  166. errstr = gctl_issue(grq);
  167. if (errstr == NULL) {
  168. if (verbose)
  169. printf("%s unconfigured\n", cp);
  170. gctl_free(grq);
  171. continue;
  172. }
  173. warnx(
  174. "%s\nor possibly kernel and ccdconfig out of sync",
  175. errstr);
  176. ex = 1;
  177. }
  178. return (ex);
  179. }
  180. /* Make sure there are enough arguments. */
  181. if (argc < 4) {
  182. if (argc == 3) {
  183. /* Assume that no flags are specified. */
  184. noflags = 1;
  185. } else {
  186. if (action == CCD_CONFIGALL) {
  187. warnx("%s: bad line: %d", ccdconf, lineno);
  188. return (1);
  189. } else
  190. usage();
  191. }
  192. }
  193. /* First argument is the ccd to configure. */
  194. cp = *argv++; --argc;
  195. if ((ccd = resolve_ccdname(cp)) < 0) {
  196. warnx("invalid ccd name: %s", cp);
  197. return (1);
  198. }
  199. /* Next argument is the interleave factor. */
  200. cp = *argv++; --argc;
  201. errno = 0; /* to check for ERANGE */
  202. ileave = (int)strtol(cp, &cp2, 10);
  203. if ((errno == ERANGE) || (ileave < 0) || (*cp2 != '\0')) {
  204. warnx("invalid interleave factor: %s", cp);
  205. return (1);
  206. }
  207. if (noflags == 0) {
  208. /* Next argument is the ccd configuration flags. */
  209. cp = *argv++; --argc;
  210. if ((flags = flags_to_val(cp)) < 0) {
  211. warnx("invalid flags argument: %s", cp);
  212. return (1);
  213. }
  214. }
  215. grq = gctl_get_handle();
  216. gctl_ro_param(grq, "verb", -1, "create geom");
  217. gctl_ro_param(grq, "class", -1, "CCD");
  218. gctl_ro_param(grq, "unit", sizeof(ccd), &ccd);
  219. gctl_ro_param(grq, "ileave", sizeof(ileave), &ileave);
  220. if (flags & CCDF_UNIFORM)
  221. gctl_ro_param(grq, "uniform", -1, "");
  222. if (flags & CCDF_MIRROR)
  223. gctl_ro_param(grq, "mirror", -1, "");
  224. if (flags & CCDF_NO_OFFSET)
  225. gctl_ro_param(grq, "no_offset", -1, "");
  226. if (flags & CCDF_LINUX)
  227. gctl_ro_param(grq, "linux", -1, "");
  228. gctl_ro_param(grq, "nprovider", sizeof(argc), &argc);
  229. for (i = 0; i < argc; i++) {
  230. sprintf(buf1, "provider%d", i);
  231. cp = argv[i];
  232. if (!strncmp(cp, _PATH_DEV, strlen(_PATH_DEV)))
  233. cp += strlen(_PATH_DEV);
  234. gctl_ro_param(grq, buf1, -1, cp);
  235. }
  236. buf1[0] = '\0';
  237. gctl_add_param(grq, "output", sizeof(buf1), buf1,
  238. GCTL_PARAM_WR | GCTL_PARAM_ASCII);
  239. errstr = gctl_issue(grq);
  240. if (errstr == NULL) {
  241. if (verbose) {
  242. printf("%s", buf1);
  243. }
  244. gctl_free(grq);
  245. return (0);
  246. }
  247. warnx(
  248. "%s\nor possibly kernel and ccdconfig out of sync",
  249. errstr);
  250. return (1);
  251. }
  252. static int
  253. do_all(int action)
  254. {
  255. FILE *f;
  256. char line[_POSIX2_LINE_MAX];
  257. char *cp, **argv;
  258. int argc, rval;
  259. gid_t egid;
  260. rval = 0;
  261. egid = getegid();
  262. if (setegid(getgid()) != 0)
  263. err(1, "setegid failed");
  264. if ((f = fopen(ccdconf, "r")) == NULL) {
  265. if (setegid(egid) != 0)
  266. err(1, "setegid failed");
  267. warn("fopen: %s", ccdconf);
  268. return (1);
  269. }
  270. if (setegid(egid) != 0)
  271. err(1, "setegid failed");
  272. while (fgets(line, sizeof(line), f) != NULL) {
  273. argc = 0;
  274. argv = NULL;
  275. ++lineno;
  276. if ((cp = strrchr(line, '\n')) != NULL)
  277. *cp = '\0';
  278. /* Break up the line and pass it's contents to do_single(). */
  279. if (line[0] == '\0')
  280. goto end_of_line;
  281. for (cp = line; (cp = strtok(cp, " \t")) != NULL; cp = NULL) {
  282. if (*cp == '#')
  283. break;
  284. if ((argv = realloc(argv,
  285. sizeof(char *) * ++argc)) == NULL) {
  286. warnx("no memory to configure ccds");
  287. return (1);
  288. }
  289. argv[argc - 1] = cp;
  290. /*
  291. * If our action is to unconfigure all, then pass
  292. * just the first token to do_single() and ignore
  293. * the rest. Since this will be encountered on
  294. * our first pass through the line, the Right
  295. * Thing will happen.
  296. */
  297. if (action == CCD_UNCONFIGALL) {
  298. if (do_single(argc, argv, action))
  299. rval = 1;
  300. goto end_of_line;
  301. }
  302. }
  303. if (argc != 0)
  304. if (do_single(argc, argv, action))
  305. rval = 1;
  306. end_of_line:
  307. if (argv != NULL)
  308. free(argv);
  309. }
  310. (void)fclose(f);
  311. return (rval);
  312. }
  313. static int
  314. resolve_ccdname(char *name)
  315. {
  316. if (!strncmp(name, _PATH_DEV, strlen(_PATH_DEV)))
  317. name += strlen(_PATH_DEV);
  318. if (strncmp(name, "ccd", 3))
  319. return -1;
  320. name += 3;
  321. if (!isdigit(*name))
  322. return -1;
  323. return (strtoul(name, NULL, 10));
  324. }
  325. static int
  326. dumpout(int unit)
  327. {
  328. static int v;
  329. struct gctl_req *grq;
  330. int ncp;
  331. char *cp;
  332. char const *errstr;
  333. grq = gctl_get_handle();
  334. ncp = 65536;
  335. cp = malloc(ncp);
  336. cp[0] = '\0';
  337. gctl_ro_param(grq, "verb", -1, "list");
  338. gctl_ro_param(grq, "class", -1, "CCD");
  339. gctl_ro_param(grq, "unit", sizeof(unit), &unit);
  340. gctl_add_param(grq, "output", ncp, cp,
  341. GCTL_PARAM_WR | GCTL_PARAM_ASCII);
  342. errstr = gctl_issue(grq);
  343. if (errstr != NULL)
  344. errx(1, "%s\nor possibly kernel and ccdconfig out of sync",
  345. errstr);
  346. if (strlen(cp) == 0)
  347. errx(1, "ccd%d not configured", unit);
  348. if (verbose && !v) {
  349. printf("# ccd\t\tileave\tflags\tcomponent devices\n");
  350. v = 1;
  351. }
  352. printf("%s", cp);
  353. free(cp);
  354. return (0);
  355. }
  356. static int
  357. dump_ccd(int argc, char **argv)
  358. {
  359. int i, error;
  360. if (argc == 0) {
  361. error = dumpout(-1);
  362. } else {
  363. error = 0;
  364. for (i = 0; error == 0 && i < argc; i++)
  365. error = dumpout(resolve_ccdname(argv[i]));
  366. }
  367. return (error);
  368. }
  369. static int
  370. flags_to_val(char *flags)
  371. {
  372. char *cp, *tok;
  373. int i, tmp, val;
  374. errno = 0; /* to check for ERANGE */
  375. val = (int)strtol(flags, &cp, 0);
  376. if ((errno != ERANGE) && (*cp == '\0')) {
  377. if (val & ~(CCDF_UNIFORM|CCDF_MIRROR))
  378. return (-1);
  379. return (val);
  380. }
  381. /* Check for values represented by strings. */
  382. if ((cp = strdup(flags)) == NULL)
  383. err(1, "no memory to parse flags");
  384. tmp = 0;
  385. for (tok = cp; (tok = strtok(tok, ",")) != NULL; tok = NULL) {
  386. for (i = 0; flagvaltab[i].fv_flag != NULL; ++i)
  387. if (strcmp(tok, flagvaltab[i].fv_flag) == 0)
  388. break;
  389. if (flagvaltab[i].fv_flag == NULL) {
  390. free(cp);
  391. return (-1);
  392. }
  393. tmp |= flagvaltab[i].fv_val;
  394. }
  395. /* If we get here, the string was ok. */
  396. free(cp);
  397. return (tmp);
  398. }
  399. static void
  400. usage(void)
  401. {
  402. fprintf(stderr, "%s\n%s\n%s\n%s\n%s\n",
  403. "usage: ccdconfig [-cv] ccd ileave [flags] dev ...",
  404. " ccdconfig -C [-v] [-f config_file]",
  405. " ccdconfig -u [-v] ccd ...",
  406. " ccdconfig -U [-v] [-f config_file]",
  407. " ccdconfig -g [ccd ...]");
  408. exit(1);
  409. }