New patch for grantpt

Thorsten Kukuk kukuk@suse.de
Tue Dec 18 08:49:00 GMT 2001


On Tue, Dec 18, Jakub Jelinek wrote:

> Cannot the
> > +  if (__builtin_expect (__libc_fcntl (fd, F_GETFD), 0) == -1
> > +      && errno == EBADF)
> test be done only if pts_name failed, so that apps passing correct file
> descriptors are not punished?
> (saving/restoring errno around it).

Yes, I append a new version of the patch.

  Thorsten

-- 
Thorsten Kukuk       http://www.suse.de/~kukuk/        kukuk@suse.de
SuSE GmbH            Deutschherrenstr. 15-19       D-90429 Nuernberg
--------------------------------------------------------------------    
Key fingerprint = A368 676B 5E1B 3E46 CFCE  2D97 F8FD 4E23 56C6 FB4B
-------------- next part --------------
2001-12-18  Thorsten Kukuk  <kukuk@suse.de>

        * sysdeps/unix/sysv/linux/grantpt.c: Make errno results standard
        conform: retrun EBADF if file descriptor is invalid and EINVAL
        if file descriptor is no valid tty.

--- sysdeps/unix/sysv/linux/grantpt.c
+++ sysdeps/unix/sysv/linux/grantpt.c	2001/12/18 16:30:05
@@ -1,4 +1,4 @@
-/* Copyright (C) 1998, 1999 Free Software Foundation, Inc.
+/* Copyright (C) 1998, 1999, 2001 Free Software Foundation, Inc.
    This file is part of the GNU C Library.
 
    The GNU C Library is free software; you can redistribute it and/or
@@ -16,6 +16,8 @@
    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
    02111-1307 USA.  */
 
+#include <errno.h>
+#include <fcntl.h>
 #include <limits.h>
 #include <stdlib.h>
 #include <sys/statfs.h>
@@ -46,8 +48,24 @@
   char *buf = _buf;
 
   if (pts_name (fd, &buf, sizeof (_buf)))
-    return -1;
-  
+    {
+      int save_errno = errno;
+
+      /* Check, if the file descriptor is valid. pts_name returns the
+	 wrong errno number, so we cannot use that.  */
+      if (__builtin_expect (__libc_fcntl (fd, F_GETFD), 0) == -1
+	  && errno == EBADF)
+	return -1;
+
+      __set_errno (save_errno);
+
+       /* If the filedescriptor is no TTY, grantpt has to set errno
+          to EINVAL.  */
+       if (errno == ENOTTY)
+         __set_errno (EINVAL);
+       return -1;
+    }
+
   if (__statfs (buf, &fsbuf) < 0)
     return -1;
 


More information about the Libc-hacker mailing list