From 86c78228434ab6874923ddc72ba36c16ae6d9126 Mon Sep 17 00:00:00 2001 From: Xin Li Date: Fri, 2 Oct 2020 19:49:07 -0700 Subject: [PATCH] Implement ptsname_r. PR: 250062 Differential Revision: https://reviews.freebsd.org/D26647 --- include/stdlib.h | 1 + lib/libc/stdlib/Makefile.inc | 2 +- lib/libc/stdlib/Symbol.map | 1 + lib/libc/stdlib/ptsname.3 | 28 +++++++++++++++++++-- lib/libc/stdlib/ptsname.c | 48 +++++++++++++++++++++++++++--------- 5 files changed, 65 insertions(+), 15 deletions(-) diff --git a/include/stdlib.h b/include/stdlib.h index e84dca6ea42b..b09517751e8d 100644 --- a/include/stdlib.h +++ b/include/stdlib.h @@ -225,6 +225,7 @@ long mrand48(void); long nrand48(unsigned short[3]); int posix_openpt(int); char *ptsname(int); +int ptsname_r(int, char *, size_t); int putenv(char *); long random(void); unsigned short diff --git a/lib/libc/stdlib/Makefile.inc b/lib/libc/stdlib/Makefile.inc index 105077434272..de8d8484e135 100644 --- a/lib/libc/stdlib/Makefile.inc +++ b/lib/libc/stdlib/Makefile.inc @@ -50,7 +50,7 @@ MLINKS+=hcreate.3 hdestroy.3 hcreate.3 hsearch.3 MLINKS+=hcreate.3 hcreate_r.3 hcreate.3 hdestroy_r.3 hcreate.3 hsearch_r.3 MLINKS+=insque.3 remque.3 MLINKS+=lsearch.3 lfind.3 -MLINKS+=ptsname.3 grantpt.3 ptsname.3 unlockpt.3 +MLINKS+=ptsname.3 grantpt.3 ptsname.3 ptsname_r.3 ptsname.3 unlockpt.3 MLINKS+=qsort.3 heapsort.3 qsort.3 mergesort.3 qsort.3 qsort_r.3 \ qsort.3 qsort_s.3 MLINKS+=rand.3 rand_r.3 rand.3 srand.3 diff --git a/lib/libc/stdlib/Symbol.map b/lib/libc/stdlib/Symbol.map index 34ffca8c3693..3d8657f1c6a0 100644 --- a/lib/libc/stdlib/Symbol.map +++ b/lib/libc/stdlib/Symbol.map @@ -125,6 +125,7 @@ FBSD_1.6 { qsort_s; rand; srand; + ptsname_r; }; FBSDprivate_1.0 { diff --git a/lib/libc/stdlib/ptsname.3 b/lib/libc/stdlib/ptsname.3 index 8e8ec07d1e53..39fcac0a37b5 100644 --- a/lib/libc/stdlib/ptsname.3 +++ b/lib/libc/stdlib/ptsname.3 @@ -31,12 +31,13 @@ .\" .\" $FreeBSD$ .\" -.Dd August 20, 2008 +.Dd October 9, 2020 .Dt PTSNAME 3 .Os .Sh NAME .Nm grantpt , .Nm ptsname , +.Nm ptsname_r , .Nm unlockpt .Nd pseudo-terminal access functions .Sh LIBRARY @@ -47,6 +48,8 @@ .Fn grantpt "int fildes" .Ft "char *" .Fn ptsname "int fildes" +.Ft "int" +.Fn ptsname_r "int fildes" "char *buffer" "size_t buflen" .Ft int .Fn unlockpt "int fildes" .Sh DESCRIPTION @@ -87,12 +90,23 @@ and have been called. .Pp The +.Fn ptsname_r +function is the thread-safe version of +.Fn ptsname . +The caller must provide storage for the results of the full pathname of +the slave device in the +.Fa buffer +and +.Fa bufsize +arguments. +.Pp +The .Fn unlockpt function clears the lock held on the pseudo-terminal pair for the master device specified with .Fa fildes . .Sh RETURN VALUES -.Rv -std grantpt unlockpt +.Rv -std grantpt ptsname_r unlockpt .Pp The .Fn ptsname @@ -119,6 +133,16 @@ is not a master pseudo-terminal device. .El .Pp In addition, the +.Fn ptsname_r +function may set +.Va errno +to: +.Bl -tag -width Er +.It Bq Er ERANGE +the buffer was too small. +.El +.Pp +In addition, the .Fn grantpt function may set .Va errno diff --git a/lib/libc/stdlib/ptsname.c b/lib/libc/stdlib/ptsname.c index 328528537ab5..247c02a6002b 100644 --- a/lib/libc/stdlib/ptsname.c +++ b/lib/libc/stdlib/ptsname.c @@ -41,6 +41,7 @@ __FBSDID("$FreeBSD$"); #include #include #include +#include #include "un-namespace.h" /* @@ -71,23 +72,46 @@ __strong_reference(__isptmaster, grantpt); __strong_reference(__isptmaster, unlockpt); /* - * ptsname(): return the pathname of the slave pseudo-terminal device - * associated with the specified master. + * ptsname_r(): return the pathname of the slave pseudo-terminal device + * associated with the specified master. */ -char * -ptsname(int fildes) +int +ptsname_r(int fildes, char *buffer, size_t buflen) { - static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV; - char *ret = NULL; + + if (buflen <= sizeof(_PATH_DEV)) { + errno = ERANGE; + return (-1); + } /* Make sure fildes points to a master device. */ if (__isptmaster(fildes) != 0) - goto done; + return (-1); + + memcpy(buffer, _PATH_DEV, sizeof(_PATH_DEV)); + buffer += sizeof(_PATH_DEV) - 1; + buflen -= sizeof(_PATH_DEV) - 1; + + if (fdevname_r(fildes, buffer, buflen) == NULL) { + if (errno == EINVAL) + errno = ERANGE; + return (-1); + } + + return (0); +} - if (fdevname_r(fildes, pt_slave + (sizeof _PATH_DEV - 1), - sizeof pt_slave - (sizeof _PATH_DEV - 1)) != NULL) - ret = pt_slave; +/* + * ptsname(): return the pathname of the slave pseudo-terminal device + * associated with the specified master. + */ +char * +ptsname(int fildes) +{ + static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN]; -done: - return (ret); + if (ptsname_r(fildes, pt_slave, sizeof(pt_slave)) == 0) + return (pt_slave); + else + return (NULL); } -- 2.28.0