ramdisks revisited

From: Wes Peters <wes_at_softweyr.com>
Date: Tue, 16 Mar 2004 10:03:01 -0800
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
_at__at_ -13,6 +13,8 _at__at_
 #include <stdio.h>
 #include <stdlib.h>
 #include <fcntl.h>
+#include <grp.h>
+#include <pwd.h>
 #include <unistd.h>
 #include <string.h>
 #include <err.h>
_at__at_ -21,6 +23,7 _at__at_
 #include <sys/module.h>
 #include <sys/linker.h>
 #include <sys/mdioctl.h>
+#include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/queue.h>
 
_at__at_ -28,6 +31,9 _at__at_
 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;
 
_at__at_ -39,7 +45,9 _at__at_
 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");
_at__at_ -54,9 +62,12 _at__at_
 	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) {
_at__at_ -182,6 +193,15 _at__at_
 				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();
 		}
_at__at_ -205,6 +225,8 _at__at_
 		else
 			query(fd, mdio.md_unit);
 	} else if (action == ATTACH) {
+		char devname[40];
+
 		if (cmdline < 2)
 			usage();
 		i = ioctl(fd, MDIOCATTACH, &mdio);
_at__at_ -212,6 +234,12 _at__at_
 			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();
_at__at_ -312,3 +340,35 _at__at_
         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_at_OpenSail.ORG
Received on Tue Mar 16 2004 - 09:03:36 UTC

This archive was generated by hypermail 2.4.0 : Wed May 19 2021 - 11:37:47 UTC