Back to index

glibc  2.9
grantpt.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998, 1999, 2001, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <fcntl.h>
00021 #include <limits.h>
00022 #include <stdlib.h>
00023 #include <sys/statfs.h>
00024 
00025 #include "linux_fsinfo.h"
00026 
00027 /* Prototype for function that changes ownership and access permission
00028    for slave pseudo terminals that do not live on a `devpts'
00029    filesystem.  */
00030 static int __unix_grantpt (int fd);
00031 
00032 /* Prototype for private function that gets the name of the slave
00033    pseudo terminal in a safe way.  */
00034 static int pts_name (int fd, char **pts, size_t buf_len);
00035 
00036 /* Change the ownership and access permission of the slave pseudo
00037    terminal associated with the master pseudo terminal specified
00038    by FD.  */
00039 int
00040 grantpt (int fd)
00041 {
00042   struct statfs fsbuf;
00043 #ifdef PATH_MAX
00044   char _buf[PATH_MAX];
00045 #else
00046   char _buf[512];
00047 #endif
00048   char *buf = _buf;
00049 
00050   if (__builtin_expect (pts_name (fd, &buf, sizeof (_buf)), 0))
00051     {
00052       int save_errno = errno;
00053 
00054       /* Check, if the file descriptor is valid. pts_name returns the
00055         wrong errno number, so we cannot use that.  */
00056       if (__libc_fcntl (fd, F_GETFD) == -1 && errno == EBADF)
00057        return -1;
00058 
00059        /* If the filedescriptor is no TTY, grantpt has to set errno
00060           to EINVAL.  */
00061        if (save_errno == ENOTTY)
00062          __set_errno (EINVAL);
00063        else
00064         __set_errno (save_errno);
00065 
00066        return -1;
00067     }
00068 
00069   if (__statfs (buf, &fsbuf) < 0)
00070     return -1;
00071 
00072   /* If the slave pseudo terminal lives on a `devpts' filesystem, the
00073      ownership and access permission are already set.  */
00074   if (fsbuf.f_type == DEVPTS_SUPER_MAGIC || fsbuf.f_type == DEVFS_SUPER_MAGIC)
00075     return 0;
00076 
00077   return __unix_grantpt (fd);
00078 }
00079 
00080 #define grantpt static __unix_grantpt
00081 #include <sysdeps/unix/grantpt.c>