From owner-svn-src-head@freebsd.org Sat Oct 17 04:14:40 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id 76C1842888A; Sat, 17 Oct 2020 04:14:40 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4CCqRN1KDrz4ZJJ; Sat, 17 Oct 2020 04:14:40 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 0FF2E1BC02; Sat, 17 Oct 2020 04:14:40 +0000 (UTC) (envelope-from delphij@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 09H4EdC0097525; Sat, 17 Oct 2020 04:14:39 GMT (envelope-from delphij@FreeBSD.org) Received: (from delphij@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 09H4EdBF097521; Sat, 17 Oct 2020 04:14:39 GMT (envelope-from delphij@FreeBSD.org) Message-Id: <202010170414.09H4EdBF097521@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: delphij set sender to delphij@FreeBSD.org using -f From: Xin LI Date: Sat, 17 Oct 2020 04:14:39 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r366781 - in head: include lib/libc/stdlib X-SVN-Group: head X-SVN-Commit-Author: delphij X-SVN-Commit-Paths: in head: include lib/libc/stdlib X-SVN-Commit-Revision: 366781 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 17 Oct 2020 04:14:40 -0000 Author: delphij Date: Sat Oct 17 04:14:38 2020 New Revision: 366781 URL: https://svnweb.freebsd.org/changeset/base/366781 Log: Implement ptsname_r. MFC after: 2 weeks PR: 250062 Reviewed by: jilles, 0mp, Ray Differential Revision: https://reviews.freebsd.org/D26647 Modified: head/include/stdlib.h head/lib/libc/stdlib/Makefile.inc head/lib/libc/stdlib/Symbol.map head/lib/libc/stdlib/ptsname.3 head/lib/libc/stdlib/ptsname.c Modified: head/include/stdlib.h ============================================================================== --- head/include/stdlib.h Sat Oct 17 01:06:04 2020 (r366780) +++ head/include/stdlib.h Sat Oct 17 04:14:38 2020 (r366781) @@ -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 Modified: head/lib/libc/stdlib/Makefile.inc ============================================================================== --- head/lib/libc/stdlib/Makefile.inc Sat Oct 17 01:06:04 2020 (r366780) +++ head/lib/libc/stdlib/Makefile.inc Sat Oct 17 04:14:38 2020 (r366781) @@ -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 Modified: head/lib/libc/stdlib/Symbol.map ============================================================================== --- head/lib/libc/stdlib/Symbol.map Sat Oct 17 01:06:04 2020 (r366780) +++ head/lib/libc/stdlib/Symbol.map Sat Oct 17 04:14:38 2020 (r366781) @@ -125,6 +125,7 @@ FBSD_1.6 { qsort_s; rand; srand; + ptsname_r; }; FBSDprivate_1.0 { Modified: head/lib/libc/stdlib/ptsname.3 ============================================================================== --- head/lib/libc/stdlib/ptsname.3 Sat Oct 17 01:06:04 2020 (r366780) +++ head/lib/libc/stdlib/ptsname.3 Sat Oct 17 04:14:38 2020 (r366781) @@ -31,12 +31,13 @@ .\" .\" $FreeBSD$ .\" -.Dd August 20, 2008 +.Dd October 17, 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 @@ -103,7 +117,8 @@ pointer is returned. .Sh ERRORS The .Fn grantpt , -.Fn ptsname +.Fn ptsname , +.Fn ptsname_r and .Fn unlockpt functions may fail and set @@ -116,6 +131,16 @@ is not a valid open file descriptor. .It Bq Er EINVAL .Fa fildes 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 Modified: head/lib/libc/stdlib/ptsname.c ============================================================================== --- head/lib/libc/stdlib/ptsname.c Sat Oct 17 01:06:04 2020 (r366780) +++ head/lib/libc/stdlib/ptsname.c Sat Oct 17 04:14:38 2020 (r366781) @@ -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); - if (fdevname_r(fildes, pt_slave + (sizeof _PATH_DEV - 1), - sizeof pt_slave - (sizeof _PATH_DEV - 1)) != NULL) - ret = pt_slave; + memcpy(buffer, _PATH_DEV, sizeof(_PATH_DEV)); + buffer += sizeof(_PATH_DEV) - 1; + buflen -= sizeof(_PATH_DEV) - 1; -done: - return (ret); + if (fdevname_r(fildes, buffer, buflen) == NULL) { + if (errno == EINVAL) + errno = ERANGE; + return (-1); + } + + return (0); +} + +/* + * 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]; + + if (ptsname_r(fildes, pt_slave, sizeof(pt_slave)) == 0) + return (pt_slave); + else + return (NULL); }