From 26d4f5e969a5850cbb0b2344835d515c368d5652 Mon Sep 17 00:00:00 2001 From: Ed Schouten Date: Wed, 11 Feb 2009 20:24:59 +0000 Subject: [PATCH] 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 --- include/stdlib.h | 2 ++ lib/libc/gen/Makefile.inc | 4 ++- lib/libc/gen/Symbol.map | 2 ++ lib/libc/gen/devname.3 | 26 +++++++++++++++++-- lib/libc/gen/fdevname.c | 54 +++++++++++++++++++++++++++++++++++++++ lib/libc/gen/ttyname.c | 14 ++-------- lib/libc/stdlib/ptsname.c | 8 +++--- 7 files changed, 90 insertions(+), 20 deletions(-) create mode 100644 lib/libc/gen/fdevname.c diff --git a/include/stdlib.h b/include/stdlib.h index 91be536668b7..5c8e4e363ef0 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -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); diff --git a/lib/libc/gen/Makefile.inc b/lib/libc/gen/Makefile.inc index cea797cdbc2b..a7997ab8c364 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 2e643134d4fb..afdecb5ef8eb 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 c05d0bac4781..7ff31c720653 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 000000000000..be235f78916f --- /dev/null +++ b/lib/libc/gen/fdevname.c @@ -0,0 +1,54 @@ +/*- + * Copyright (c) 2009 Ed Schouten + * 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 +__FBSDID("$FreeBSD$"); + +#include "namespace.h" +#include +#include +#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 65436046f6b4..a21b77f9806e 100644 --- a/lib/libc/gen/ttyname.c +++ b/lib/libc/gen/ttyname.c @@ -35,7 +35,6 @@ __FBSDID("$FreeBSD$"); #include "namespace.h" #include -#include #include #include #include @@ -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); } diff --git a/lib/libc/stdlib/ptsname.c b/lib/libc/stdlib/ptsname.c index fa606f6a9c00..fc3b719a1f10 100644 --- a/lib/libc/stdlib/ptsname.c +++ b/lib/libc/stdlib/ptsname.c @@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$"); #include #include +#include #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. */