From owner-freebsd-current@FreeBSD.ORG Tue Mar 16 10:03:36 2004 Return-Path: Delivered-To: freebsd-current@freebsd.org Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id CF1E916A4CE; Tue, 16 Mar 2004 10:03:36 -0800 (PST) Received: from mail1.zer0.org (klapaucius.zer0.org [204.152.186.45]) by mx1.FreeBSD.org (Postfix) with ESMTP id 8379343D1F; Tue, 16 Mar 2004 10:03:36 -0800 (PST) (envelope-from wes@softweyr.com) Received: from localhost (localhost [127.0.0.1]) by mail1.zer0.org (Postfix) with ESMTP id DC56F239AD0; Tue, 16 Mar 2004 10:03:35 -0800 (PST) Received: from mail1.zer0.org ([127.0.0.1]) by localhost (klapaucius.zer0.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 20807-01; Tue, 16 Mar 2004 10:03:35 -0800 (PST) Received: from obie.softweyr.com (66-91-236-204.san.rr.com [66.91.236.204]) by mail1.zer0.org (Postfix) with ESMTP id 5CC2A239A0F; Tue, 16 Mar 2004 10:03:35 -0800 (PST) Received: from salty.rapid.stbernard.com (ca053217ef2d25c152ae171ac64314f8@intergate.ipinc.com [198.147.128.71] (may be forged)) (authenticated bits=0) by obie.softweyr.com (8.12.10/8.12.10) with ESMTP id i2GI3RYq029329 (version=TLSv1/SSLv3 cipher=DHE-DSS-AES256-SHA bits=256 verify=NO); Tue, 16 Mar 2004 10:03:30 -0800 (PST) (envelope-from wes@opensail.org) From: Wes Peters Organization: OpenSail Project To: current@freebsd.org Date: Tue, 16 Mar 2004 10:03:01 -0800 User-Agent: KMail/1.5.4 MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Content-Disposition: inline Message-Id: <200403161003.01889.wes@opensail.org> X-Scanned-By: MIMEDefang 2.39 X-Virus-Scanned: by amavisd-new at zer0.org X-Mailman-Approved-At: Wed, 17 Mar 2004 05:04:03 -0800 Subject: ramdisks revisited X-BeenThere: freebsd-current@freebsd.org X-Mailman-Version: 2.1.1 Precedence: list List-Id: Discussions about the use of FreeBSD-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 16 Mar 2004 18:03:37 -0000 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 #include #include +#include +#include #include #include #include @@ -21,6 +23,7 @@ #include #include #include +#include #include #include @@ -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