From thomas@cuivre.fr.eu.org  Mon Dec 24 06:42:54 2001
Return-Path: <thomas@cuivre.fr.eu.org>
Received: from melchior.cuivre.fr.eu.org (melchior.enst.fr [137.194.161.6])
	by hub.freebsd.org (Postfix) with ESMTP id 798D337B405
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 24 Dec 2001 06:42:53 -0800 (PST)
Received: from melusine.cuivre.fr.eu.org (melusine.enst.fr [137.194.160.34])
	by melchior.cuivre.fr.eu.org (Postfix) with ESMTP id EC7E97A3F
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 24 Dec 2001 15:42:49 +0100 (CET)
Received: by melusine.cuivre.fr.eu.org (Postfix, from userid 1000)
	id A91547B; Mon, 24 Dec 2001 15:42:48 +0100 (CET)
Message-Id: <20011224144248.A91547B@melusine.cuivre.fr.eu.org>
Date: Mon, 24 Dec 2001 15:42:48 +0100 (CET)
From: Thomas Quinot <thomas@cuivre.fr.eu.org>
Reply-To: Thomas Quinot <thomas@cuivre.fr.eu.org>
To: FreeBSD-gnats-submit@freebsd.org
Cc:
Subject: sbin/sysctl: allow setting dev_t values [patch included]
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         33150
>Category:       bin
>Synopsis:       sbin/sysctl: allow setting dev_t values [patch included]
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Dec 24 06:50:00 PST 2001
>Closed-Date:    Sun Dec 30 02:33:57 PST 2001
>Last-Modified:  Sun Dec 30 02:34:05 PST 2001
>Originator:     Thomas Quinot
>Release:        FreeBSD 4.4-STABLE i386
>Organization:
>Environment:
System: FreeBSD melusine.cuivre.fr.eu.org 4.4-STABLE FreeBSD 4.4-STABLE #3: Mon Dec 17 01:07:53 CET 2001 root@melusine.cuivre.fr.eu.org:/usr/obj/usr/src/sys/MELUSINE i386


	
>Description:
	The enclosed patch adds a new functionality to sbin/sysctl:
	it allows setting of variables of type dev_t by indicating
	the name of a special file on the command line, eg:
	  sysctl kern.dumpdev=/dev/ad1s1b
	(same effect as dumpon /dev/ad1s1b).

	This would come in handy for controlling any other sysctl variable
	that is supposed to designate a device.

>How-To-Repeat:
	
>Fix:

--- sysctl.c.dist	Mon Dec 24 15:39:37 2001
+++ sysctl.c	Mon Dec 24 15:36:18 2001
@@ -49,6 +49,7 @@
 #include <sys/stat.h>
 #include <sys/sysctl.h>
 #include <sys/resource.h>
+#include <sys/param.h>
 
 #include <ctype.h>
 #include <err.h>
@@ -66,6 +67,8 @@
 static int	sysctl_all (int *oid, int len);
 static int	name2oid(char *, int *);
 
+static void	set_T_dev_t (char *, void **, int *);
+
 static void
 usage(void)
 {
@@ -153,7 +156,7 @@
 	size_t newsize = 0;
 	quad_t quadval;
 	int mib[CTL_MAXNAME];
-	char *cp, *bufp, buf[BUFSIZ];
+	char *cp, *bufp, buf[BUFSIZ], fmt[BUFSIZ];
 	u_int kind;
 
 	bufp = buf;
@@ -171,7 +174,7 @@
 	if (len < 0) 
 		errx(1, "unknown oid '%s'", bufp);
 
-	if (oidfmt(mib, len, 0, &kind))
+	if (oidfmt(mib, len, fmt, &kind))
 		err(1, "couldn't find format of oid '%s'", bufp);
 
 	if (newval == NULL) {
@@ -217,6 +220,12 @@
 				newval = &quadval;
 				newsize = sizeof(quadval);
 				break;
+			case CTLTYPE_OPAQUE:
+				if (strcmp(fmt, "T,dev_t") == 0) {
+					set_T_dev_t ((char*)newval, &newval, &newsize);
+					break;
+				}
+				/* FALLTHROUGH */
 			default:
 				errx(1, "oid '%s' is type %d,"
 					" cannot set that", bufp,
@@ -316,6 +325,27 @@
 				major(*d), minor(*d));
 	}
 	return (0);
+}
+
+static void
+set_T_dev_t (char *path, void **val, int *size)
+{
+	static struct stat statb;
+
+	if (strcmp(path, "none") && strcmp(path, "off")) {
+		int rc = stat (path, &statb);
+		if (rc) {
+			err(1, "cannot stat %s", path);
+		}
+
+		if (!S_ISCHR(statb.st_mode)) {
+			errx(1, "must specify a device special file.");
+		}
+	} else {
+		statb.st_rdev = NODEV;
+	}
+	*val = (char*) &statb.st_rdev;
+	*size = sizeof statb.st_rdev;
 }
 
 /*
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: phk 
State-Changed-When: Sun Dec 30 02:33:57 PST 2001 
State-Changed-Why:  
committed thanks 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=33150 
>Unformatted:
