New patch for grantpt

Thorsten Kukuk kukuk@suse.de
Tue Dec 18 05:53:00 GMT 2001


Hi,

I appended a new patch for the grantpt errno problem and a short
test case for the fix.

  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/17 13:37:58
@@ -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>
@@ -45,8 +47,20 @@
 #endif
   char *buf = _buf;
 
-  if (pts_name (fd, &buf, sizeof (_buf)))
+  /* 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;
+
+  if (pts_name (fd, &buf, sizeof (_buf)))
+    {
+       /* 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;

-------------- next part --------------

#define _GNU_SOURCE

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>

static int
test_ebadf()
{
  int fd, ret, err;

  fd = open("/dev/ptmx", O_RDWR);
  if (fd == -1)
    {
      printf ("open(\"/dev/ptmx\", O_RDWR) failed\nerrno %d (%s)\n",
	      errno, strerror(errno));
      return 1;
    }
  unlockpt(fd);
  close(fd);

  ret = grantpt(fd);
  err = errno;
  if (ret != -1 || err != EBADF)
    {
      printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EBADF);
      printf ("           got: return = %d, errno = %d\n", ret, err);
      return 1;
    }
  return 0;
}

static int
test_einval()
{
  int fd, ret, err;
  char *file = "./grantpt-einval";

  fd = open(file, O_RDWR|O_CREAT);
  if (fd == -1)
    {
      printf ("open(\"%s\", O_RDWR) failed\nerrno %d (%s)\n",
	      file, errno, strerror(errno));
      return 1;
    }

  ret = grantpt(fd);
  err = errno;
  if (ret != -1 || err != EINVAL)
    {
      printf ("grantpt(): expected: return = %d, errno = %d\n", -1, EINVAL);
      printf ("           got: return = %d, errno = %d\n", ret, err);
      ret = 1;
    }
  else
    ret = 0;

  close(fd);
  unlink(file);

  return ret;
}

int
main ()
{
  int result = 0;

  result += test_ebadf();
  result += test_einval();

  return result;
}


More information about the Libc-hacker mailing list