From cyrille.lefevre@laposte.net  Mon Jun 28 00:14:51 2004
Return-Path: <cyrille.lefevre@laposte.net>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id BA6C316A4CE
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Jun 2004 00:14:51 +0000 (GMT)
Received: from ioskeha.hittite.isp.9tel.net (ioskeha.hittite.isp.9tel.net [62.62.156.27])
	by mx1.FreeBSD.org (Postfix) with ESMTP id B89E543D2F
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Jun 2004 00:14:50 +0000 (GMT)
	(envelope-from cyrille.lefevre@laposte.net)
Received: from mail.gits.dyndns.org (unknown [84.97.140.172])
	by ioskeha.hittite.isp.9tel.net (Postfix) with ESMTP id 38AC714B655
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Jun 2004 02:15:17 +0200 (CEST)
Received: from gits.invalid (IDENT:wihfked9omrkqzl1@localhost [127.0.0.1])
	by mail.gits.dyndns.org (8.12.11/8.12.11) with ESMTP id i5S0EMpH052867
	for <FreeBSD-gnats-submit@freebsd.org>; Mon, 28 Jun 2004 02:14:41 +0200 (CEST)
	(envelope-from cyrille.lefevre@laposte.net)
Received: by gits.invalid (tmda-sendmail, from uid 0);
	Mon, 28 Jun 2004 02:14:06 +0200 (CEST)
Message-Id: <20040628001353.GA30844@gits.dyndns.org>
Date: Mon, 28 Jun 2004 02:13:54 +0200
From: Cyrille Lefevre <cyrille.lefevre@laposte.net>
Reply-To: Cyrille Lefevre <cyrille.lefevre@laposte.net>
To: FreeBSD-gnats-submit@freebsd.org
Subject: conscontrol DEVDIR -> _PATH_DEV fix and more
X-Send-Pr-Version: 3.113

>Number:         68437
>Category:       bin
>Synopsis:       [patch] conscontrol(8) DEVDIR -> _PATH_DEV fix and more
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          change-request
>Submitter-Id:   current-users
>Arrival-Date:   Mon Jun 28 00:20:14 GMT 2004
>Closed-Date:    
>Last-Modified:  Sat May 24 20:20:23 UTC 2008
>Originator:     Cyrille Lefevre
>Release:        FreeBSD 5.2-CURRENT i386
>Organization:
ACME
>Environment:
System: FreeBSD gits.invalid 5.2-CURRENT FreeBSD 5.2-CURRENT #35: Sun Jun 20 01:53:37 CEST 2004 root@gits:/disk3/freebsd/current/obj/disk3/freebsd/current/src/sys/CUSTOM i386
>Description:
* required fix:
- includes <paths.h>, replaces DEVDIR by _PATH_DEV and _PATH_CONSOLE.
* optional fix:
- adds ctty_oid (kern.console) and mute_oid (kern.consmute) constants.
- adds a check for the trailing comma removing (who says if sysctlbyname
will not change in the future ?)
>How-To-Repeat:
	n/a
>Fix:
Index: conscontrol.c
===================================================================
RCS file: /home/ncvs/src/sbin/conscontrol/conscontrol.c,v
retrieving revision 1.4
diff -u -I$Id.*$ -I$.+BSD.*$ -r1.4 conscontrol.c
--- conscontrol.c	18 Jun 2004 06:33:44 -0000	1.4
+++ conscontrol.c	28 Jun 2004 00:07:14 -0000
@@ -35,12 +35,14 @@
 #include <err.h>
 #include <errno.h>
 #include <fcntl.h>
+#include <paths.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 
-#define DEVDIR	"/dev/"
+static const char *ctty_oid = "kern.console";
+static const char *mute_oid = "kern.consmute";
 
 static void __dead2
 usage(void)
@@ -62,23 +64,23 @@
 	char *buf, *p, *avail;
 
 	len = sizeof(mute);
-	if (sysctlbyname("kern.consmute", &mute, &len, NULL, 0) == -1)
-		err(1, "kern.consmute retrieval failed");
-	if (sysctlbyname("kern.console", NULL, &len, NULL, 0) == -1)
-		err(1, "kern.console estimate failed");
+	if (sysctlbyname(mute_oid, &mute, &len, NULL, 0) == -1)
+		err(1, "%s retrieval failed", mute_oid);
+	if (sysctlbyname(ctty_oid, NULL, &len, NULL, 0) == -1)
+		err(1, "%s estimate failed", ctty_oid);
 	if ((buf = malloc(len)) == NULL)
-		errx(1, "kern.console malloc failed");
-	if (sysctlbyname("kern.console", buf, &len, NULL, 0) == -1)
-		err(1, "kern.console retrieval failed");
+		errx(1, "%s malloc failed", ctty_oid);
+	if (sysctlbyname(ctty_oid, buf, &len, NULL, 0) == -1)
+		err(1, "%s retrieval failed", ctty_oid);
 	if ((avail = strchr(buf, '/')) == NULL)
-		errx(1, "kern.console format not understood");
+		errx(1, "%s format not understood", ctty_oid);
 	p = avail;
 	*avail++ = '\0';
-	if (p != buf)
-		*--p = '\0';			/* remove trailing ',' */
+	if (p != buf && *--p == ',')
+		*p = '\0';			/* remove trailing ',' */
 	p = avail + strlen(avail);
-	if (p != avail)
-		*--p = '\0';			/* remove trailing ',' */
+	if (p != avail && *--p == ',')
+		*p = '\0';			/* remove trailing ',' */
 	printf("Configured: %s\n", buf);
 	printf(" Available: %s\n", avail);
 	printf("    Muting: %s\n", mute ? "on" : "off");
@@ -98,7 +100,7 @@
 	else
 		usage();
 	len = sizeof(mute);
-	if (sysctlbyname("kern.consmute", NULL, NULL, &mute, len) == -1)
+	if (sysctlbyname(mute_oid, NULL, NULL, &mute, len) == -1)
 		err(1, "could not change console muting");
 }
 
@@ -111,10 +113,10 @@
 static char*
 stripdev(char *devnam)
 {
-	if (memcmp (devnam, DEVDIR, strlen(DEVDIR)) == 0)
-		return (&devnam[strlen(DEVDIR)]);	    /* remove /dev */
+	if (memcmp (devnam, _PATH_DEV, strlen(_PATH_DEV)) == 0)
+		return (&devnam[strlen(_PATH_DEV)]);	    /* remove /dev */
 	else if (strchr (devnam, '/')) {
-		fprintf(stderr, "Not a device in /dev: %s\n", devnam);
+		fprintf(stderr, "Not a device in %s: %s\n", _PATH_DEV, devnam);
 		return (NULL);				    /* end of string */
 	} else
 		return (devnam);			    /* passed */
@@ -129,7 +131,7 @@
 	if (devnam == NULL)
 		return;
 	len = strlen(devnam);
-	if (sysctlbyname("kern.console", NULL, NULL, devnam, len) == -1)
+	if (sysctlbyname(ctty_oid, NULL, NULL, devnam, len) == -1)
 		err(1, "could not add %s as a console", devnam);
 }
 
@@ -147,7 +149,7 @@
 		errx(1, "malloc failed");
 	buf[0] = '-';
 	strcpy(buf + 1, devnam);
-	if (sysctlbyname("kern.console", NULL, NULL, buf, len) == -1)
+	if (sysctlbyname(ctty_oid, NULL, NULL, buf, len) == -1)
 		err(1, "could not remove %s as a console", devnam);
 	free(buf);
 }
@@ -170,7 +172,7 @@
 {
 	int ttyfd, flag = 0;
 
-	ttyfd = open(DEVDIR "console", O_RDONLY);
+	ttyfd = open(_PATH_CONSOLE, O_RDONLY);
 	if (ttyfd == -1)
 		err(1, "opening virtual console");
 	if (ioctl(ttyfd, TIOCCONS, &flag) == -1)
>Release-Note:
>Audit-Trail:
>Unformatted:
