Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 16 Mar 2004 10:03:01 -0800
From:      Wes Peters <wes@softweyr.com>
To:        current@freebsd.org
Subject:   ramdisks revisited
Message-ID:  <200403161003.01889.wes@opensail.org>

next in thread | raw e-mail | index | archive | help
The guy who originally asked for the ability to control ownership and 
permissions of his ramdisks discovered one critical flaw in my lovely 
rc.d script: chown isn't in /sbin (or /rescue) on a vanilla 5.x system.

As an alternative to moving binaries into the root filesystem, I propose 
the following patch to mdconfig to add options to specify the ownership 
and permissions of the newly attached md device.  Options added are 
-m mode, -g group, and -O owner (because -o and -u were both already used.) 
I will, of course, update the man page before committing.  Any objections?

Index: mdconfig.c
===================================================================
RCS file: /big/ncvs/src/sbin/mdconfig/mdconfig.c,v
retrieving revision 1.31
diff -u -w -r1.31 mdconfig.c
--- mdconfig.c	10 Mar 2004 20:41:09 -0000	1.31
+++ mdconfig.c	16 Mar 2004 17:52:38 -0000
@@ -13,6 +13,8 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
+#include <grp.h>
+#include <pwd.h>
 #include <unistd.h>
 #include <string.h>
 #include <err.h>
@@ -21,6 +23,7 @@
 #include <sys/module.h>
 #include <sys/linker.h>
 #include <sys/mdioctl.h>
+#include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/queue.h>
 
@@ -28,6 +31,9 @@
 void	 mdmaybeload(void);
 int	 query(const int, const int);
 void	 usage(void);
+gid_t	getgroup(char *);
+uid_t	getuser(char *);
+mode_t	getperms(char *);
 
 struct md_ioctl mdio;
 
@@ -39,7 +45,9 @@
 usage()
 {
 	fprintf(stderr, "usage:\n");
-	fprintf(stderr, "\tmdconfig -a -t type [-n] [-o [no]option]... [ -f file] 
[-s size] [-S sectorsize] [-u unit]\n");
+	fprintf(stderr, "\tmdconfig -a -t type [-n] [-o [no]option]... [-f 
file]\n");
+	fprintf(stderr, "\t         [-s size] [-S sectorsize] [-u unit]\n");
+	fprintf(stderr, "\t         [-O owner] [-g group] [-m mode]\n");
 	fprintf(stderr, "\tmdconfig -d -u unit\n");
 	fprintf(stderr, "\tmdconfig -l [-n] [-u unit]\n");
 	fprintf(stderr, "\t\ttype = {malloc, preload, vnode, swap}\n");
@@ -54,9 +62,12 @@
 	int ch, fd, i;
 	char *p;
 	int cmdline = 0;
+	gid_t group = 0;
+	uid_t owner = 0;
+	mode_t perms = 0;
 
 	for (;;) {
-		ch = getopt(argc, argv, "ab:df:lno:s:S:t:u:x:y:");
+		ch = getopt(argc, argv, "ab:df:g:lm:nO:o:s:S:t:u:x:y:");
 		if (ch == -1)
 			break;
 		switch (ch) {
@@ -182,6 +193,15 @@
 				usage();
 			mdio.md_fwheads = strtoul(optarg, &p, 0);
 			break;
+		case 'g':
+			group = getgroup(optarg);
+			break;
+		case 'm':
+			perms = getperms(optarg);
+			break;
+		case 'O':
+			owner = getuser(optarg);
+			break;
 		default:
 			usage();
 		}
@@ -205,6 +225,8 @@
 		else
 			query(fd, mdio.md_unit);
 	} else if (action == ATTACH) {
+		char devname[40];
+
 		if (cmdline < 2)
 			usage();
 		i = ioctl(fd, MDIOCATTACH, &mdio);
@@ -212,6 +234,12 @@
 			err(1, "ioctl(/dev/%s)", MDCTL_NAME);
 		if (mdio.md_options & MD_AUTOUNIT)
 			printf("%s%d\n", nflag ? "" : MD_NAME, mdio.md_unit);
+
+		snprintf(devname, 40, "/dev/%s%d", MD_NAME, mdio.md_unit);
+		if (owner || group)
+			chown(devname, (uid_t)owner, (gid_t)group);
+		if (perms)
+			chmod(devname, perms);
 	} else if (action == DETACH) {
 		if (mdio.md_options & MD_AUTOUNIT)
 			usage();
@@ -312,3 +340,35 @@
         kldload(name);
 }
 
+
+gid_t
+getgroup(char *name)
+{
+	struct group *gr;
+
+	if ((gr = getgrnam(name)) != NULL) {
+		endgrent();
+		return gr->gr_gid;
+	}
+	return ((gid_t)strtol(name, (char **)NULL, 10));
+}
+
+
+uid_t
+getuser(char *name)
+{
+	struct passwd *pw;
+
+	if ((pw = getpwnam(name)) != NULL) {
+		endpwent();
+		return pw->pw_uid;
+	}
+	return ((uid_t)strtol(name, (char **)NULL, 10));
+}
+
+
+mode_t
+getperms(char *string)
+{
+	return ((mode_t)strtol(string, (char **)NULL, 8));
+}

-- 
         "Where am I, and what am I doing in this handbasket?"

Wes Peters                                              wes@OpenSail.ORG




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200403161003.01889.wes>