Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 11 Feb 2009 20:24:59 +0000 (UTC)
From:      Ed Schouten <ed@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r188497 - in head: include lib/libc/gen lib/libc/stdlib
Message-ID:  <200902112024.n1BKOxcc035265@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: ed
Date: Wed Feb 11 20:24:59 2009
New Revision: 188497
URL: http://svn.freebsd.org/changeset/base/188497

Log:
  Add two new routines: fdevname() and fdevname_r().
  
  A more elegant way of obtaining a name of a character device by its file
  descriptor on FreeBSD, is to use the FIODGNAME ioctl. Because a valid
  file descriptor implies a file descriptor is visible in /dev, it will
  always resolve a valid device name.
  
  I'm adding a more friendly wrapper for this ioctl, called fdevname(). It
  is a lot easier to use than devname() and also has better error
  handling. When a device name cannot be resolved, it will just return
  NULL instead of a generated device name that makes no sense.
  
  Discussed with:	kib

Added:
  head/lib/libc/gen/fdevname.c   (contents, props changed)
Modified:
  head/include/stdlib.h
  head/lib/libc/gen/Makefile.inc
  head/lib/libc/gen/Symbol.map
  head/lib/libc/gen/devname.3
  head/lib/libc/gen/ttyname.c
  head/lib/libc/stdlib/ptsname.c

Modified: head/include/stdlib.h
==============================================================================
--- head/include/stdlib.h	Wed Feb 11 18:42:56 2009	(r188496)
+++ head/include/stdlib.h	Wed Feb 11 20:24:59 2009	(r188497)
@@ -256,6 +256,8 @@ int	 cgetustr(char *, const char *, char
 int	 daemon(int, int);
 char	*devname(__dev_t, __mode_t);
 char 	*devname_r(__dev_t, __mode_t, char *, int);
+char	*fdevname(int);
+char 	*fdevname_r(int, char *, int);
 int	 getloadavg(double [], int);
 __const char *
 	 getprogname(void);

Modified: head/lib/libc/gen/Makefile.inc
==============================================================================
--- head/lib/libc/gen/Makefile.inc	Wed Feb 11 18:42:56 2009	(r188496)
+++ head/lib/libc/gen/Makefile.inc	Wed Feb 11 20:24:59 2009	(r188497)
@@ -10,7 +10,7 @@ SRCS+=  __getosreldate.c __xuname.c \
 	clock.c closedir.c confstr.c \
 	crypt.c ctermid.c daemon.c devname.c dirname.c disklabel.c \
 	dlfcn.c dlfunc.c drand48.c erand48.c err.c errlst.c errno.c \
-	exec.c feature_present.c fmtcheck.c fmtmsg.c fnmatch.c \
+	exec.c fdevname.c feature_present.c fmtcheck.c fmtmsg.c fnmatch.c \
 	fpclassify.c frexp.c fstab.c ftok.c fts.c fts-compat.c ftw.c \
 	getbootfile.c getbsize.c \
 	getcap.c getcwd.c getdomainname.c getgrent.c getgrouplist.c \
@@ -78,6 +78,8 @@ MLINKS+=arc4random.3 arc4random_addrando
 	arc4random.3 arc4random_buf.3 arc4random.3 arc4random_uniform.3
 MLINKS+=ctermid.3 ctermid_r.3
 MLINKS+=devname.3 devname_r.3
+MLINKS+=devname.3 fdevname.3
+MLINKS+=devname.3 fdevname_r.3
 MLINKS+=directory.3 closedir.3 directory.3 dirfd.3 directory.3 opendir.3 \
 	directory.3 fdopendir.3 \
 	directory.3 readdir.3 directory.3 readdir_r.3 directory.3 rewinddir.3 \

Modified: head/lib/libc/gen/Symbol.map
==============================================================================
--- head/lib/libc/gen/Symbol.map	Wed Feb 11 18:42:56 2009	(r188496)
+++ head/lib/libc/gen/Symbol.map	Wed Feb 11 20:24:59 2009	(r188497)
@@ -331,6 +331,8 @@ FBSD_1.0 {
 FBSD_1.1 {
 	arc4random_buf;
 	arc4random_uniform;
+	fdevname;
+	fdevname_r;
 	fdopendir;
 	feature_present;
 	fts_open;

Modified: head/lib/libc/gen/devname.3
==============================================================================
--- head/lib/libc/gen/devname.3	Wed Feb 11 18:42:56 2009	(r188496)
+++ head/lib/libc/gen/devname.3	Wed Feb 11 20:24:59 2009	(r188497)
@@ -43,6 +43,10 @@
 .Fn devname "dev_t dev" "mode_t type"
 .Ft char *
 .Fn devname_r "dev_t dev" "mode_t type" "char *buf" "int len"
+.Ft char *
+.Fn fdevname "int fd"
+.Ft char *
+.Fn fdevname_r "int fd" "char *buf" "int len"
 .Sh DESCRIPTION
 The
 .Fn devname
@@ -69,11 +73,24 @@ and
 .Fa type
 in a human-readable format.
 .Pp
+The
+.Fn fdevname
+and
+.Fn fdevname_r
+function obtains the device name directly from a file descriptor
+pointing to a character device.
+If it is unable to come up with a suitable name, these functions will
+return a NULL pointer.
+.Pp
 .Fn devname
-returns the name stored in a static buffer which will be overwritten
+and
+.Fn fdevname
+return the name stored in a static buffer which will be overwritten
 on subsequent calls.
 .Fn devname_r
-takes a buffer and length as argument to avoid this problem.
+and
+.Fn fdevname_r
+take a buffer and length as argument to avoid this problem.
 .Sh EXAMPLES
 .Bd -literal -compact
 int fd;
@@ -83,6 +100,7 @@ char *name;
 	fd = open("/dev/tun");
 	fstat(fd, &buf);
 	printf("devname is /dev/%s\en", devname(buf.st_rdev, S_IFCHR));
+	printf("fdevname is /dev/%s\en", fdevname(fd));
 .Ed
 .Sh SEE ALSO
 .Xr stat 2
@@ -91,3 +109,7 @@ The
 .Fn devname
 function appeared in
 .Bx 4.4 .
+The
+.Fn fdevname
+function appeared in
+.Fx 8.0 .

Added: head/lib/libc/gen/fdevname.c
==============================================================================
--- /dev/null	00:00:00 1970	(empty, because file is newly added)
+++ head/lib/libc/gen/fdevname.c	Wed Feb 11 20:24:59 2009	(r188497)
@@ -0,0 +1,54 @@
+/*-
+ * Copyright (c) 2009 Ed Schouten <ed@FreeBSD.org>
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__FBSDID("$FreeBSD$");
+
+#include "namespace.h"
+#include <sys/param.h>
+#include <sys/ioctl.h>
+#include "un-namespace.h"
+
+char *
+fdevname_r(int fd, char *buf, int len)
+{
+	struct fiodgname_arg fgn;
+
+	fgn.buf = buf;
+	fgn.len = len;
+
+	if (_ioctl(fd, FIODGNAME, &fgn) == -1)
+		return (NULL);
+	return (buf);
+}
+
+char *
+fdevname(int fd)
+{
+	static char buf[SPECNAMELEN + 1];
+
+	return (fdevname_r(fd, buf, sizeof(buf)));
+}

Modified: head/lib/libc/gen/ttyname.c
==============================================================================
--- head/lib/libc/gen/ttyname.c	Wed Feb 11 18:42:56 2009	(r188496)
+++ head/lib/libc/gen/ttyname.c	Wed Feb 11 20:24:59 2009	(r188497)
@@ -35,7 +35,6 @@ __FBSDID("$FreeBSD$");
 
 #include "namespace.h"
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <sys/ioctl.h>
 #include <sys/filio.h>
 #include <fcntl.h>
@@ -60,8 +59,6 @@ static int		ttyname_keycreated = 0;
 int
 ttyname_r(int fd, char *buf, size_t len)
 {
-	struct stat	sb;
-	struct fiodgname_arg fgn;
 	size_t used;
 
 	*buf = '\0';
@@ -69,21 +66,14 @@ ttyname_r(int fd, char *buf, size_t len)
 	/* Must be a terminal. */
 	if (!isatty(fd))
 		return (ENOTTY);
-	/* Must be a character device. */
-	if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
-		return (ENOTTY);
 	/* Must have enough room */
 	if (len <= sizeof(_PATH_DEV))
 		return (ERANGE);
 
 	strcpy(buf, _PATH_DEV);
 	used = strlen(buf);
-	fgn.len = len - used;
-	fgn.buf = buf + used;
-	if (!_ioctl(fd, FIODGNAME, &fgn))
-		return (0);
-	used = strlen(buf);
-	devname_r(sb.st_rdev, S_IFCHR, buf + used, len - used);
+	if (fdevname_r(fd, buf + used, len - used) == NULL)
+		return (ENOTTY);
 	return (0);
 }
 

Modified: head/lib/libc/stdlib/ptsname.c
==============================================================================
--- head/lib/libc/stdlib/ptsname.c	Wed Feb 11 18:42:56 2009	(r188496)
+++ head/lib/libc/stdlib/ptsname.c	Wed Feb 11 20:24:59 2009	(r188497)
@@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
 
 #include <errno.h>
 #include <paths.h>
+#include <stdlib.h>
 #include "un-namespace.h"
 
 /*
@@ -75,7 +76,6 @@ char *
 ptsname(int fildes)
 {
 	static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
-	struct fiodgname_arg fgn;
 	char *ret = NULL;
 	int sverrno = errno;
 
@@ -83,10 +83,8 @@ ptsname(int fildes)
 	if (__isptmaster(fildes) != 0)
 		goto done;
 	
-	/* Obtain the device name through FIODGNAME. */
-	fgn.len = sizeof pt_slave - (sizeof _PATH_DEV - 1);
-	fgn.buf = pt_slave + (sizeof _PATH_DEV - 1);
-	if (_ioctl(fildes, FIODGNAME, &fgn) == 0)
+	if (fdevname_r(fildes, pt_slave + (sizeof _PATH_DEV - 1),
+	    sizeof pt_slave - (sizeof _PATH_DEV - 1)) != NULL)
 		ret = pt_slave;
 
 done:	/* Make sure ptsname() does not overwrite errno. */



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