diff options
Diffstat (limited to 'sbin/mdconfig/mdconfig.c')
-rw-r--r-- | sbin/mdconfig/mdconfig.c | 79 |
1 files changed, 46 insertions, 33 deletions
diff --git a/sbin/mdconfig/mdconfig.c b/sbin/mdconfig/mdconfig.c index 70cefe9..233058d 100644 --- a/sbin/mdconfig/mdconfig.c +++ b/sbin/mdconfig/mdconfig.c @@ -47,6 +47,7 @@ #include <inttypes.h> #include <libgeom.h> #include <libutil.h> +#include <paths.h> #include <stdarg.h> #include <stdio.h> #include <stdlib.h> @@ -59,9 +60,9 @@ static int nflag; static void usage(void); static void md_set_file(const char *); -static int md_find(char *, const char *); -static int md_query(char *name); -static int md_list(char *units, int opt); +static int md_find(const char *, const char *); +static int md_query(const char *, const int, const char *); +static int md_list(const char *, int, const char *); static char *geom_config_get(struct gconf *g, const char *name); static void md_prthumanval(char *length); @@ -82,7 +83,7 @@ usage(void) " [-x sectors/track] [-y heads/cylinder]\n" " mdconfig -d -u unit [-o [no]force]\n" " mdconfig -r -u unit -s size [-o [no]force]\n" -" mdconfig -l [-v] [-n] [-u unit]\n" +" mdconfig -l [-v] [-n] [-f file] [-u unit]\n" " mdconfig file\n"); fprintf(stderr, "\t\ttype = {malloc, vnode, swap}\n"); fprintf(stderr, "\t\toption = {cluster, compress, reserve}\n"); @@ -160,7 +161,9 @@ main(int argc, char **argv) case 'f': if (fflag != NULL) errx(1, "-f can be passed only once"); - fflag = optarg; + fflag = realpath(optarg, NULL); + if (fflag == NULL) + err(1, "realpath"); break; case 'o': if (!strcmp(optarg, "async")) @@ -215,8 +218,8 @@ main(int argc, char **argv) errx(1, "unknown suffix on -s argument"); break; case 'u': - if (!strncmp(optarg, "/dev/", 5)) - optarg += 5; + if (!strncmp(optarg, _PATH_DEV, sizeof(_PATH_DEV) - 1)) + optarg += sizeof(_PATH_DEV) - 1; if (!strncmp(optarg, MD_NAME, sizeof(MD_NAME) - 1)) optarg += sizeof(MD_NAME) - 1; uflag = optarg; @@ -298,8 +301,8 @@ main(int argc, char **argv) errx(1, "-x can only be used with -a"); if (mdio.md_fwheads != 0) errx(1, "-y can only be used with -a"); - if (fflag != NULL) - errx(1, "-f can only be used with -a"); + if (fflag != NULL && action != LIST) + errx(1, "-f can only be used with -a and -l"); if (tflag != NULL) errx(1, "-t can only be used with -a"); if (argc > 0) @@ -333,14 +336,14 @@ main(int argc, char **argv) if (!kld_isloaded("g_md") && kld_load("geom_md") == -1) err(1, "failed to load geom_md module"); - fd = open("/dev/" MDCTL_NAME, O_RDWR, 0); + fd = open(_PATH_DEV MDCTL_NAME, O_RDWR, 0); if (fd < 0) - err(1, "open(/dev/%s)", MDCTL_NAME); + err(1, "open(%s%s)", _PATH_DEV, MDCTL_NAME); if (action == ATTACH) { i = ioctl(fd, MDIOCATTACH, &mdio); if (i < 0) - err(1, "ioctl(/dev/%s)", MDCTL_NAME); + err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); if (mdio.md_options & MD_AUTOUNIT) printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit); } else if (action == DETACH) { @@ -348,22 +351,22 @@ main(int argc, char **argv) errx(1, "-d requires -u"); i = ioctl(fd, MDIOCDETACH, &mdio); if (i < 0) - err(1, "ioctl(/dev/%s)", MDCTL_NAME); + err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); } else if (action == RESIZE) { if (mdio.md_options & MD_AUTOUNIT) errx(1, "-r requires -u"); i = ioctl(fd, MDIOCRESIZE, &mdio); if (i < 0) - err(1, "ioctl(/dev/%s)", MDCTL_NAME); + err(1, "ioctl(%s%s)", _PATH_DEV, MDCTL_NAME); } else if (action == LIST) { if (mdio.md_options & MD_AUTOUNIT) { /* * Listing all devices. This is why we pass NULL * together with OPT_LIST. */ - md_list(NULL, OPT_LIST | vflag); + return (md_list(NULL, OPT_LIST | vflag, fflag)); } else - return (md_query(uflag)); + return (md_query(uflag, vflag, fflag)); } else usage(); close(fd); @@ -397,7 +400,7 @@ md_set_file(const char *fn) * between list and query mode. */ static int -md_list(char *units, int opt) +md_list(const char *units, int opt, const char *fflag) { struct gmesh gm; struct gprovider *pp; @@ -407,7 +410,7 @@ md_list(char *units, int opt) struct ggeom *gg; struct gclass *gcl; void *sq; - int retcode, found; + int retcode, ffound, ufound; char *type, *file, *length; type = file = length = NULL; @@ -422,7 +425,7 @@ md_list(char *units, int opt) if (sq == NULL) return (-1); - found = 0; + ffound = ufound = 0; while ((gsp = geom_stats_snapshot_next(sq)) != NULL) { gid = geom_lookupid(&gm, gsp->id); if (gid == NULL) @@ -438,18 +441,26 @@ md_list(char *units, int opt) if (retcode != 1) continue; else - found = 1; + ufound = 1; } gc = &pp->lg_config; - if (nflag && strncmp(pp->lg_name, "md", 2) == 0) + type = geom_config_get(gc, "type"); + if (strcmp(type, "vnode") == 0) { + file = geom_config_get(gc, "file"); + if (fflag != NULL && + strcmp(fflag, file) != 0) + continue; + else + ffound = 1; + } else if (fflag != NULL) + continue; + if (nflag && strncmp(pp->lg_name, MD_NAME, 2) == 0) printf("%s", pp->lg_name + 2); else printf("%s", pp->lg_name); - if (opt & OPT_VERBOSE || opt & OPT_UNIT) { - type = geom_config_get(gc, "type"); - if (strcmp(type, "vnode") == 0) - file = geom_config_get(gc, "file"); + if (opt & OPT_VERBOSE || + ((opt & OPT_UNIT) && fflag == NULL)) { length = geom_config_get(gc, "length"); printf("\t%s\t", type); if (length != NULL) @@ -470,7 +481,9 @@ md_list(char *units, int opt) printf("\n"); /* XXX: Check if it's enough to clean everything. */ geom_stats_snapshot_free(sq); - if ((opt & OPT_UNIT) && found) + if (((opt & OPT_UNIT) && (fflag == NULL) && ufound) || + ((opt & OPT_UNIT) == 0 && (fflag != NULL) && ffound) || + ((opt & OPT_UNIT) && (fflag != NULL) && ufound && ffound)) return (0); else return (-1); @@ -497,10 +510,10 @@ geom_config_get(struct gconf *g, const char *name) * otherwise. */ static int -md_find(char *list, const char *name) +md_find(const char *list, const char *name) { int ret; - char num[16]; + char num[PATH_MAX]; char *ptr, *p, *u; ret = 0; @@ -508,10 +521,10 @@ md_find(char *list, const char *name) if (ptr == NULL) return (-1); for (p = ptr; (u = strsep(&p, ",")) != NULL;) { - if (strncmp(u, "/dev/", 5) == 0) - u += 5; + if (strncmp(u, _PATH_DEV, sizeof(_PATH_DEV) - 1) == 0) + u += sizeof(_PATH_DEV) - 1; /* Just in case user specified number instead of full name */ - snprintf(num, sizeof(num), "md%s", u); + snprintf(num, sizeof(num), "%s%s", MD_NAME, u); if (strcmp(u, name) == 0 || strcmp(num, name) == 0) { ret = 1; break; @@ -538,8 +551,8 @@ md_prthumanval(char *length) } static int -md_query(char *name) +md_query(const char *name, const int opt, const char *fflag) { - return (md_list(name, OPT_UNIT)); + return (md_list(name, opt | OPT_UNIT, fflag)); } |