Update.
[glibc.git] / sysdeps / unix / grantpt.c
blob5f27ce91e3537ea660a9841696947b7ced96b073
1 /* Copyright (C) 1998, 2000, 2001, 2002 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3 Contributed by Zack Weinberg <zack@rabi.phys.columbia.edu>, 1998.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, write to the Free
17 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
18 02111-1307 USA. */
20 #include <assert.h>
21 #include <errno.h>
22 #include <grp.h>
23 #include <limits.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <sys/resource.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/wait.h>
30 #include <unistd.h>
32 #include "pty-private.h"
35 /* Return the result of ptsname_r in the buffer pointed to by PTS,
36 which should be of length BUF_LEN. If it is too long to fit in
37 this buffer, a sufficiently long buffer is allocated using malloc,
38 and returned in PTS. 0 is returned upon success, -1 otherwise. */
39 static int
40 pts_name (int fd, char **pts, size_t buf_len)
42 int rv;
43 char *buf = *pts;
45 for (;;)
47 char *new_buf;
49 if (buf_len)
51 rv = __ptsname_r (fd, buf, buf_len);
52 if (rv != 0)
54 if (rv == ENOTTY)
55 /* ptsname_r returns with ENOTTY to indicate
56 a descriptor not referring to a pty master.
57 For this condition, grantpt must return EINVAL. */
58 errno = EINVAL;
59 break;
62 if (memchr (buf, '\0', buf_len))
63 /* We succeeded and the returned name fit in the buffer. */
64 break;
66 /* Try again with a longer buffer. */
67 buf_len += buf_len; /* Double it */
69 else
70 /* No initial buffer; start out by mallocing one. */
71 buf_len = 128; /* First time guess. */
73 if (buf != *pts)
74 /* We've already malloced another buffer at least once. */
75 new_buf = (char *) realloc (buf, buf_len);
76 else
77 new_buf = (char *) malloc (buf_len);
78 if (! new_buf)
80 rv = -1;
81 __set_errno (ENOMEM);
82 break;
84 buf = new_buf;
87 if (rv == 0)
88 *pts = buf; /* Return buffer to the user. */
89 else if (buf != *pts)
90 free (buf); /* Free what we malloced when returning an error. */
92 return rv;
95 /* Change the ownership and access permission of the slave pseudo
96 terminal associated with the master pseudo terminal specified
97 by FD. */
98 int
99 grantpt (int fd)
101 int retval = -1;
102 #ifdef PATH_MAX
103 char _buf[PATH_MAX];
104 #else
105 char _buf[512];
106 #endif
107 char *buf = _buf;
108 struct stat64 st;
109 char *grtmpbuf;
110 struct group grbuf;
111 size_t grbuflen = __sysconf (_SC_GETGR_R_SIZE_MAX);
112 struct group *p;
113 uid_t uid;
114 gid_t gid;
115 pid_t pid;
117 if (pts_name (fd, &buf, sizeof (_buf)))
118 return -1;
120 if (__xstat64 (_STAT_VER, buf, &st) < 0)
121 goto cleanup;
123 /* Make sure that we own the device. */
124 uid = __getuid ();
125 if (st.st_uid != uid)
127 if (__chown (buf, uid, st.st_gid) < 0)
128 goto helper;
131 /* Get the group ID of the special `tty' group. */
132 if (grbuflen == -1)
133 /* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
134 Try a moderate value. */
135 grbuflen = 1024;
136 grtmpbuf = (char *) __alloca (grbuflen);
137 __getgrnam_r (TTY_GROUP, &grbuf, grtmpbuf, grbuflen, &p);
138 gid = p ? p->gr_gid : __getgid ();
140 /* Make sure the group of the device is that special group. */
141 if (st.st_gid != gid)
143 if (__chown (buf, uid, gid) < 0)
144 goto helper;
147 /* Make sure the permission mode is set to readable and writable by
148 the owner, and writable by the group. */
149 if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP))
151 if (__chmod (buf, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
152 goto helper;
155 retval = 0;
156 goto cleanup;
158 /* We have to use the helper program. */
159 helper:
161 pid = __fork ();
162 if (pid == -1)
163 goto cleanup;
164 else if (pid == 0)
166 /* Disable core dumps. */
167 struct rlimit rl = { 0, 0 };
168 __setrlimit (RLIMIT_CORE, &rl);
170 /* We pass the master pseudo terminal as file descriptor PTY_FILENO. */
171 if (fd != PTY_FILENO)
172 if (__dup2 (fd, PTY_FILENO) < 0)
173 _exit (FAIL_EBADF);
175 execle (_PATH_PT_CHOWN, basename (_PATH_PT_CHOWN), NULL, NULL);
176 _exit (FAIL_EXEC);
178 else
180 int w;
182 if (__waitpid (pid, &w, 0) == -1)
183 goto cleanup;
184 if (!WIFEXITED (w))
185 __set_errno (ENOEXEC);
186 else
187 switch (WEXITSTATUS(w))
189 case 0:
190 retval = 0;
191 break;
192 case FAIL_EBADF:
193 __set_errno (EBADF);
194 break;
195 case FAIL_EINVAL:
196 __set_errno (EINVAL);
197 break;
198 case FAIL_EACCES:
199 __set_errno (EACCES);
200 break;
201 case FAIL_EXEC:
202 __set_errno (ENOEXEC);
203 break;
205 default:
206 assert(! "getpt: internal error: invalid exit code from pt_chown");
210 cleanup:
211 if (buf != _buf)
212 free (buf);
214 return retval;