Back to index

glibc  2.9
grantpt.c
Go to the documentation of this file.
00001 /* Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003    Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <assert.h>
00021 #include <errno.h>
00022 #include <grp.h>
00023 #include <limits.h>
00024 #include <stdlib.h>
00025 #include <string.h>
00026 #include <sys/resource.h>
00027 #include <sys/stat.h>
00028 #include <sys/types.h>
00029 #include <sys/wait.h>
00030 #include <unistd.h>
00031 
00032 #include "pty-private.h"
00033 
00034 
00035 /* Return the result of ptsname_r in the buffer pointed to by PTS,
00036    which should be of length BUF_LEN.  If it is too long to fit in
00037    this buffer, a sufficiently long buffer is allocated using malloc,
00038    and returned in PTS.  0 is returned upon success, -1 otherwise.  */
00039 static int
00040 pts_name (int fd, char **pts, size_t buf_len)
00041 {
00042   int rv;
00043   char *buf = *pts;
00044 
00045   for (;;)
00046     {
00047       char *new_buf;
00048 
00049       if (buf_len)
00050        {
00051          rv = __ptsname_r (fd, buf, buf_len);
00052          if (rv != 0)
00053            {
00054              if (rv == ENOTTY)
00055               /* ptsname_r returns with ENOTTY to indicate
00056                  a descriptor not referring to a pty master.
00057                  For this condition, grantpt must return EINVAL.  */
00058               rv = EINVAL;
00059              errno = rv;    /* Not necessarily set by __ptsname_r.  */
00060              break;
00061            }
00062 
00063          if (memchr (buf, '\0', buf_len))
00064            /* We succeeded and the returned name fit in the buffer.  */
00065            break;
00066 
00067          /* Try again with a longer buffer.  */
00068          buf_len += buf_len;       /* Double it */
00069        }
00070       else
00071        /* No initial buffer; start out by mallocing one.  */
00072        buf_len = 128;              /* First time guess.  */
00073 
00074       if (buf != *pts)
00075        /* We've already malloced another buffer at least once.  */
00076        new_buf = (char *) realloc (buf, buf_len);
00077       else
00078        new_buf = (char *) malloc (buf_len);
00079       if (! new_buf)
00080        {
00081          rv = -1;
00082          __set_errno (ENOMEM);
00083          break;
00084        }
00085       buf = new_buf;
00086     }
00087 
00088   if (rv == 0)
00089     *pts = buf;             /* Return buffer to the user.  */
00090   else if (buf != *pts)
00091     free (buf);             /* Free what we malloced when returning an error.  */
00092 
00093   return rv;
00094 }
00095 
00096 /* Change the ownership and access permission of the slave pseudo
00097    terminal associated with the master pseudo terminal specified
00098    by FD.  */
00099 int
00100 grantpt (int fd)
00101 {
00102   int retval = -1;
00103 #ifdef PATH_MAX
00104   char _buf[PATH_MAX];
00105 #else
00106   char _buf[512];
00107 #endif
00108   char *buf = _buf;
00109   struct stat64 st;
00110   char *grtmpbuf;
00111   struct group grbuf;
00112   size_t grbuflen = __sysconf (_SC_GETGR_R_SIZE_MAX);
00113   struct group *p;
00114   uid_t uid;
00115   gid_t gid;
00116   pid_t pid;
00117 
00118   if (pts_name (fd, &buf, sizeof (_buf)))
00119     return -1;
00120 
00121   if (__xstat64 (_STAT_VER, buf, &st) < 0)
00122     goto cleanup;
00123 
00124   /* Make sure that we own the device.  */
00125   uid = __getuid ();
00126   if (st.st_uid != uid)
00127     {
00128       if (__chown (buf, uid, st.st_gid) < 0)
00129        goto helper;
00130     }
00131 
00132   /* Get the group ID of the special `tty' group.  */
00133   if (grbuflen == (size_t) -1L)
00134     /* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
00135        Try a moderate value.  */
00136     grbuflen = 1024;
00137   grtmpbuf = (char *) __alloca (grbuflen);
00138   __getgrnam_r (TTY_GROUP, &grbuf, grtmpbuf, grbuflen, &p);
00139   gid = p ? p->gr_gid : __getgid ();
00140 
00141   /* Make sure the group of the device is that special group.  */
00142   if (st.st_gid != gid)
00143     {
00144       if (__chown (buf, uid, gid) < 0)
00145        goto helper;
00146     }
00147 
00148   /* Make sure the permission mode is set to readable and writable by
00149      the owner, and writable by the group.  */
00150   if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP))
00151     {
00152       if (__chmod (buf, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
00153        goto helper;
00154     }
00155 
00156   retval = 0;
00157   goto cleanup;
00158 
00159   /* We have to use the helper program.  */
00160  helper:
00161 
00162   pid = __fork ();
00163   if (pid == -1)
00164     goto cleanup;
00165   else if (pid == 0)
00166     {
00167       /* Disable core dumps.  */
00168       struct rlimit rl = { 0, 0 };
00169       __setrlimit (RLIMIT_CORE, &rl);
00170 
00171       /* We pass the master pseudo terminal as file descriptor PTY_FILENO.  */
00172       if (fd != PTY_FILENO)
00173        if (__dup2 (fd, PTY_FILENO) < 0)
00174          _exit (FAIL_EBADF);
00175 
00176       execle (_PATH_PT_CHOWN, basename (_PATH_PT_CHOWN), NULL, NULL);
00177       _exit (FAIL_EXEC);
00178     }
00179   else
00180     {
00181       int w;
00182 
00183       if (__waitpid (pid, &w, 0) == -1)
00184        goto cleanup;
00185       if (!WIFEXITED (w))
00186        __set_errno (ENOEXEC);
00187       else
00188        switch (WEXITSTATUS(w))
00189          {
00190          case 0:
00191            retval = 0;
00192            break;
00193          case FAIL_EBADF:
00194            __set_errno (EBADF);
00195            break;
00196          case FAIL_EINVAL:
00197            __set_errno (EINVAL);
00198            break;
00199          case FAIL_EACCES:
00200            __set_errno (EACCES);
00201            break;
00202          case FAIL_EXEC:
00203            __set_errno (ENOEXEC);
00204            break;
00205 
00206          default:
00207            assert(! "getpt: internal error: invalid exit code from pt_chown");
00208          }
00209     }
00210 
00211  cleanup:
00212   if (buf != _buf)
00213     free (buf);
00214 
00215   return retval;
00216 }