diff options
author | ed <ed@FreeBSD.org> | 2009-02-11 20:24:59 +0000 |
---|---|---|
committer | ed <ed@FreeBSD.org> | 2009-02-11 20:24:59 +0000 |
commit | de78bbbfe832781ef4718e4d175d53fbe4e7ac40 (patch) | |
tree | 0b13c353e44fe54b12f0be5c1ae687436add79ee /lib/libc/gen | |
parent | c1e92bfb206adffe9aca701014c9028722d1ed02 (diff) | |
download | FreeBSD-src-de78bbbfe832781ef4718e4d175d53fbe4e7ac40.zip FreeBSD-src-de78bbbfe832781ef4718e4d175d53fbe4e7ac40.tar.gz |
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
Diffstat (limited to 'lib/libc/gen')
-rw-r--r-- | lib/libc/gen/Makefile.inc | 4 | ||||
-rw-r--r-- | lib/libc/gen/Symbol.map | 2 | ||||
-rw-r--r-- | lib/libc/gen/devname.3 | 26 | ||||
-rw-r--r-- | lib/libc/gen/fdevname.c | 54 | ||||
-rw-r--r-- | lib/libc/gen/ttyname.c | 14 |
5 files changed, 85 insertions, 15 deletions
diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index cea797c..a7997ab 100644 --- a/lib/libc/gen/Makefile.inc +++ b/lib/libc/gen/Makefile.inc @@ -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_addrandom.3 arc4random.3 arc4random_stir.3 \ 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 \ diff --git a/lib/libc/gen/Symbol.map b/lib/libc/gen/Symbol.map index 2e64313..afdecb5 100644 --- a/lib/libc/gen/Symbol.map +++ b/lib/libc/gen/Symbol.map @@ -331,6 +331,8 @@ FBSD_1.0 { FBSD_1.1 { arc4random_buf; arc4random_uniform; + fdevname; + fdevname_r; fdopendir; feature_present; fts_open; diff --git a/lib/libc/gen/devname.3 b/lib/libc/gen/devname.3 index c05d0ba..7ff31c7 100644 --- a/lib/libc/gen/devname.3 +++ b/lib/libc/gen/devname.3 @@ -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 . diff --git a/lib/libc/gen/fdevname.c b/lib/libc/gen/fdevname.c new file mode 100644 index 0000000..be235f7 --- /dev/null +++ b/lib/libc/gen/fdevname.c @@ -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))); +} diff --git a/lib/libc/gen/ttyname.c b/lib/libc/gen/ttyname.c index 6543604..a21b77f 100644 --- a/lib/libc/gen/ttyname.c +++ b/lib/libc/gen/ttyname.c @@ -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); } |