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
This commit is contained in:
parent
73bac36f82
commit
26d4f5e969
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=188497
@ -256,6 +256,8 @@ int cgetustr(char *, const char *, char **);
|
|||||||
int daemon(int, int);
|
int daemon(int, int);
|
||||||
char *devname(__dev_t, __mode_t);
|
char *devname(__dev_t, __mode_t);
|
||||||
char *devname_r(__dev_t, __mode_t, char *, int);
|
char *devname_r(__dev_t, __mode_t, char *, int);
|
||||||
|
char *fdevname(int);
|
||||||
|
char *fdevname_r(int, char *, int);
|
||||||
int getloadavg(double [], int);
|
int getloadavg(double [], int);
|
||||||
__const char *
|
__const char *
|
||||||
getprogname(void);
|
getprogname(void);
|
||||||
|
@ -10,7 +10,7 @@ SRCS+= __getosreldate.c __xuname.c \
|
|||||||
clock.c closedir.c confstr.c \
|
clock.c closedir.c confstr.c \
|
||||||
crypt.c ctermid.c daemon.c devname.c dirname.c disklabel.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 \
|
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 \
|
fpclassify.c frexp.c fstab.c ftok.c fts.c fts-compat.c ftw.c \
|
||||||
getbootfile.c getbsize.c \
|
getbootfile.c getbsize.c \
|
||||||
getcap.c getcwd.c getdomainname.c getgrent.c getgrouplist.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
|
arc4random.3 arc4random_buf.3 arc4random.3 arc4random_uniform.3
|
||||||
MLINKS+=ctermid.3 ctermid_r.3
|
MLINKS+=ctermid.3 ctermid_r.3
|
||||||
MLINKS+=devname.3 devname_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 \
|
MLINKS+=directory.3 closedir.3 directory.3 dirfd.3 directory.3 opendir.3 \
|
||||||
directory.3 fdopendir.3 \
|
directory.3 fdopendir.3 \
|
||||||
directory.3 readdir.3 directory.3 readdir_r.3 directory.3 rewinddir.3 \
|
directory.3 readdir.3 directory.3 readdir_r.3 directory.3 rewinddir.3 \
|
||||||
|
@ -331,6 +331,8 @@ FBSD_1.0 {
|
|||||||
FBSD_1.1 {
|
FBSD_1.1 {
|
||||||
arc4random_buf;
|
arc4random_buf;
|
||||||
arc4random_uniform;
|
arc4random_uniform;
|
||||||
|
fdevname;
|
||||||
|
fdevname_r;
|
||||||
fdopendir;
|
fdopendir;
|
||||||
feature_present;
|
feature_present;
|
||||||
fts_open;
|
fts_open;
|
||||||
|
@ -43,6 +43,10 @@
|
|||||||
.Fn devname "dev_t dev" "mode_t type"
|
.Fn devname "dev_t dev" "mode_t type"
|
||||||
.Ft char *
|
.Ft char *
|
||||||
.Fn devname_r "dev_t dev" "mode_t type" "char *buf" "int len"
|
.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
|
.Sh DESCRIPTION
|
||||||
The
|
The
|
||||||
.Fn devname
|
.Fn devname
|
||||||
@ -69,11 +73,24 @@ and
|
|||||||
.Fa type
|
.Fa type
|
||||||
in a human-readable format.
|
in a human-readable format.
|
||||||
.Pp
|
.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
|
.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.
|
on subsequent calls.
|
||||||
.Fn devname_r
|
.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
|
.Sh EXAMPLES
|
||||||
.Bd -literal -compact
|
.Bd -literal -compact
|
||||||
int fd;
|
int fd;
|
||||||
@ -83,6 +100,7 @@ char *name;
|
|||||||
fd = open("/dev/tun");
|
fd = open("/dev/tun");
|
||||||
fstat(fd, &buf);
|
fstat(fd, &buf);
|
||||||
printf("devname is /dev/%s\en", devname(buf.st_rdev, S_IFCHR));
|
printf("devname is /dev/%s\en", devname(buf.st_rdev, S_IFCHR));
|
||||||
|
printf("fdevname is /dev/%s\en", fdevname(fd));
|
||||||
.Ed
|
.Ed
|
||||||
.Sh SEE ALSO
|
.Sh SEE ALSO
|
||||||
.Xr stat 2
|
.Xr stat 2
|
||||||
@ -91,3 +109,7 @@ The
|
|||||||
.Fn devname
|
.Fn devname
|
||||||
function appeared in
|
function appeared in
|
||||||
.Bx 4.4 .
|
.Bx 4.4 .
|
||||||
|
The
|
||||||
|
.Fn fdevname
|
||||||
|
function appeared in
|
||||||
|
.Fx 8.0 .
|
||||||
|
54
lib/libc/gen/fdevname.c
Normal file
54
lib/libc/gen/fdevname.c
Normal file
@ -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)));
|
||||||
|
}
|
@ -35,7 +35,6 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
#include "namespace.h"
|
#include "namespace.h"
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/stat.h>
|
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <sys/filio.h>
|
#include <sys/filio.h>
|
||||||
#include <fcntl.h>
|
#include <fcntl.h>
|
||||||
@ -60,8 +59,6 @@ static int ttyname_keycreated = 0;
|
|||||||
int
|
int
|
||||||
ttyname_r(int fd, char *buf, size_t len)
|
ttyname_r(int fd, char *buf, size_t len)
|
||||||
{
|
{
|
||||||
struct stat sb;
|
|
||||||
struct fiodgname_arg fgn;
|
|
||||||
size_t used;
|
size_t used;
|
||||||
|
|
||||||
*buf = '\0';
|
*buf = '\0';
|
||||||
@ -69,21 +66,14 @@ ttyname_r(int fd, char *buf, size_t len)
|
|||||||
/* Must be a terminal. */
|
/* Must be a terminal. */
|
||||||
if (!isatty(fd))
|
if (!isatty(fd))
|
||||||
return (ENOTTY);
|
return (ENOTTY);
|
||||||
/* Must be a character device. */
|
|
||||||
if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
|
|
||||||
return (ENOTTY);
|
|
||||||
/* Must have enough room */
|
/* Must have enough room */
|
||||||
if (len <= sizeof(_PATH_DEV))
|
if (len <= sizeof(_PATH_DEV))
|
||||||
return (ERANGE);
|
return (ERANGE);
|
||||||
|
|
||||||
strcpy(buf, _PATH_DEV);
|
strcpy(buf, _PATH_DEV);
|
||||||
used = strlen(buf);
|
used = strlen(buf);
|
||||||
fgn.len = len - used;
|
if (fdevname_r(fd, buf + used, len - used) == NULL)
|
||||||
fgn.buf = buf + used;
|
return (ENOTTY);
|
||||||
if (!_ioctl(fd, FIODGNAME, &fgn))
|
|
||||||
return (0);
|
|
||||||
used = strlen(buf);
|
|
||||||
devname_r(sb.st_rdev, S_IFCHR, buf + used, len - used);
|
|
||||||
return (0);
|
return (0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -38,6 +38,7 @@ __FBSDID("$FreeBSD$");
|
|||||||
|
|
||||||
#include <errno.h>
|
#include <errno.h>
|
||||||
#include <paths.h>
|
#include <paths.h>
|
||||||
|
#include <stdlib.h>
|
||||||
#include "un-namespace.h"
|
#include "un-namespace.h"
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -75,7 +76,6 @@ char *
|
|||||||
ptsname(int fildes)
|
ptsname(int fildes)
|
||||||
{
|
{
|
||||||
static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
|
static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
|
||||||
struct fiodgname_arg fgn;
|
|
||||||
char *ret = NULL;
|
char *ret = NULL;
|
||||||
int sverrno = errno;
|
int sverrno = errno;
|
||||||
|
|
||||||
@ -83,10 +83,8 @@ ptsname(int fildes)
|
|||||||
if (__isptmaster(fildes) != 0)
|
if (__isptmaster(fildes) != 0)
|
||||||
goto done;
|
goto done;
|
||||||
|
|
||||||
/* Obtain the device name through FIODGNAME. */
|
if (fdevname_r(fildes, pt_slave + (sizeof _PATH_DEV - 1),
|
||||||
fgn.len = sizeof pt_slave - (sizeof _PATH_DEV - 1);
|
sizeof pt_slave - (sizeof _PATH_DEV - 1)) != NULL)
|
||||||
fgn.buf = pt_slave + (sizeof _PATH_DEV - 1);
|
|
||||||
if (_ioctl(fildes, FIODGNAME, &fgn) == 0)
|
|
||||||
ret = pt_slave;
|
ret = pt_slave;
|
||||||
|
|
||||||
done: /* Make sure ptsname() does not overwrite errno. */
|
done: /* Make sure ptsname() does not overwrite errno. */
|
||||||
|
Loading…
Reference in New Issue
Block a user