Movatterモバイル変換
[0]ホーム
This is the mail archive of thelibc-alpha@sourceware.orgmailing list for theglibc project.
[PATCH 2/2 v4] openpty: use TIOCGPTPEER to open slave side fd
- From: Christian Brauner <christian dot brauner at ubuntu dot com>
- To: libc-alpha at sourceware dot org,stgraber at stgraber dot org,serge at hallyn dot com,fweimer at redhat dot com,joseph at codesourcery dot com
- Cc: Christian Brauner <christian dot brauner at ubuntu dot com>
- Date: Tue, 29 Aug 2017 15:45:15 +0200
- Subject: [PATCH 2/2 v4] openpty: use TIOCGPTPEER to open slave side fd
- Authentication-results: sourceware.org; auth=none
- References: <a05d5603-c074-966d-f75e-cb268d0dc249@redhat.com> <20170829134515.9345-1-christian.brauner@ubuntu.com>
Newer kernels expose the ioctl TIOCGPTPEER [1] call to userspace which allows tosafely allocate a file descriptor for a pty slave based solely on the masterfile descriptor. This allows us to avoid path-based operations and makes thisfunction a lot safer in the face of devpts mounts in different mount namespaces.[1]:https://patchwork.kernel.org/patch/9760743/Signed-off-by: Christian Brauner <christian.brauner@ubuntu.com>---Changelog 2017-08-28:* Instead of #ifdefing the TIOCGPTPEER ioctl flag we now try the ioctl() first and if it fails we fallback to path-based allocation of the slave fd. This allows us retain backward compatibility with kernels that do not support this ioctl call.* A note on the following codepath if (name != NULL) { if (*buf == '\0') if (pts_name (master, &buf, sizeof (_buf))) goto fail; strcpy (name, buf); } "buf" is guaranteed to be allocated in this case. If the pts_name() call above failed we would have never reached this code path. If it has been called succesfully it will either have handed us a valid buffer or "buf" will still point to the static char array "_buf" which is initialized to 0.Changelog 2017-08-28:* Preserve #ifdef for TIOCGPTPEER since it needs to work on non-Linux distros too.* Only intialize first byte of "_buf".Changelog 2017-08-29:* Adapt to unified error handling as suggested by Florian.--- ChangeLog | 5 +++++ login/openpty.c | 30 ++++++++++++++++++++++++------ 2 files changed, 29 insertions(+), 6 deletions(-)diff --git a/ChangeLog b/ChangeLogindex bc5fb8e27f..30829e4c16 100644--- a/ChangeLog+++ b/ChangeLog@@ -1,3 +1,8 @@+2017-08-26 Christian Brauner <christian.brauner@ubuntu.com>++* login/openpty.c (openpty): If defined, use the TIOCGPTPEER ioctl call+to allocate the slave pty file descriptor.+ 2017-08-26 Christian Brauner <christian.brauner@ubuntu.com> * login/openpty.c (openpty): Close slave pty file descriptor on error.diff --git a/login/openpty.c b/login/openpty.cindex a7b1ab5dde..766aa958ff 100644--- a/login/openpty.c+++ b/login/openpty.c@@ -94,6 +94,8 @@ openpty (int *amaster, int *aslave, char *name, char *buf = _buf; int master, ret = -1, slave = -1; + *buf = '\0';+ master = getpt (); if (master == -1) return -1;@@ -104,12 +106,22 @@ openpty (int *amaster, int *aslave, char *name, if (unlockpt (master)) goto on_error; - if (pts_name (master, &buf, sizeof (_buf)))- goto on_error;-- slave = open (buf, O_RDWR | O_NOCTTY);+#ifdef TIOCGPTPEER+ /* Try to allocate slave fd solely based on master fd first. */+ slave = ioctl (master, TIOCGPTPEER, O_RDWR | O_NOCTTY);+#endif if (slave == -1)- goto on_error;+ {+ /* Fallback to path-based slave fd allocation in case kernel doesn't+ * support TIOCGPTPEER.+ */+ if (pts_name (master, &buf, sizeof (_buf)))+ goto on_error;++ slave = open (buf, O_RDWR | O_NOCTTY);+ if (slave == -1)+ goto on_error;+ } /* XXX Should we ignore errors here? */ if (termp)@@ -122,7 +134,13 @@ openpty (int *amaster, int *aslave, char *name, *amaster = master; *aslave = slave; if (name != NULL)- strcpy (name, buf);+ {+ if (*buf == '\0')+ if (pts_name (master, &buf, sizeof (_buf)))+ goto on_error;++ strcpy (name, buf);+ } ret = 0; -- 2.14.1
[8]ページ先頭