(elf_machine_rela): Remove unnecessary code and add missing reloc types.
[glibc.git] / sysdeps / unix / grantpt.c
blobdad7d16b43122a0cf3ed02b20258e548beb333cb
1 /* Copyright (C) 1998, 2000, 2001 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);
53 if (rv != 0 || memchr (buf, '\0', buf_len))
54 /* We either got an error, or we succeeded and the
55 returned name fit in the buffer. */
56 break;
58 /* Try again with a longer buffer. */
59 buf_len += buf_len; /* Double it */
61 else
62 /* No initial buffer; start out by mallocing one. */
63 buf_len = 128; /* First time guess. */
65 if (buf != *pts)
66 /* We've already malloced another buffer at least once. */
67 new_buf = (char *) realloc (buf, buf_len);
68 else
69 new_buf = (char *) malloc (buf_len);
70 if (! new_buf)
72 rv = -1;
73 __set_errno (ENOMEM);
74 break;
76 buf = new_buf;
79 if (rv == 0)
80 *pts = buf; /* Return buffer to the user. */
81 else if (buf != *pts)
82 free (buf); /* Free what we malloced when returning an error. */
84 return rv;
87 /* Change the ownership and access permission of the slave pseudo
88 terminal associated with the master pseudo terminal specified
89 by FD. */
90 int
91 grantpt (int fd)
93 int retval = -1;
94 #ifdef PATH_MAX
95 char _buf[PATH_MAX];
96 #else
97 char _buf[512];
98 #endif
99 char *buf = _buf;
100 struct stat64 st;
101 char *grtmpbuf;
102 struct group grbuf;
103 size_t grbuflen = __sysconf (_SC_GETGR_R_SIZE_MAX);
104 struct group *p;
105 uid_t uid;
106 gid_t gid;
107 pid_t pid;
109 if (pts_name (fd, &buf, sizeof (_buf)))
110 return -1;
112 if (__xstat64 (_STAT_VER, buf, &st) < 0)
113 goto cleanup;
115 /* Make sure that we own the device. */
116 uid = __getuid ();
117 if (st.st_uid != uid)
119 if (__chown (buf, uid, st.st_gid) < 0)
120 goto helper;
123 /* Get the group ID of the special `tty' group. */
124 if (grbuflen == -1)
125 /* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
126 Try a moderate value. */
127 grbuflen = 1024;
128 grtmpbuf = (char *) __alloca (grbuflen);
129 __getgrnam_r (TTY_GROUP, &grbuf, grtmpbuf, grbuflen, &p);
130 gid = p ? p->gr_gid : __getgid ();
132 /* Make sure the group of the device is that special group. */
133 if (st.st_gid != gid)
135 if (__chown (buf, uid, gid) < 0)
136 goto helper;
139 /* Make sure the permission mode is set to readable and writable by
140 the owner, and writable by the group. */
141 if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP))
143 if (__chmod (buf, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
144 goto helper;
147 retval = 0;
148 goto cleanup;
150 /* We have to use the helper program. */
151 helper:
153 pid = __fork ();
154 if (pid == -1)
155 goto cleanup;
156 else if (pid == 0)
158 /* Disable core dumps. */
159 struct rlimit rl = { 0, 0 };
160 __setrlimit (RLIMIT_CORE, &rl);
162 /* We pass the master pseudo terminal as file descriptor PTY_FILENO. */
163 if (fd != PTY_FILENO)
164 if (__dup2 (fd, PTY_FILENO) < 0)
165 _exit (FAIL_EBADF);
167 execle (_PATH_PT_CHOWN, basename (_PATH_PT_CHOWN), NULL, NULL);
168 _exit (FAIL_EXEC);
170 else
172 int w;
174 if (__waitpid (pid, &w, 0) == -1)
175 goto cleanup;
176 if (!WIFEXITED (w))
177 __set_errno (ENOEXEC);
178 else
179 switch (WEXITSTATUS(w))
181 case 0:
182 retval = 0;
183 break;
184 case FAIL_EBADF:
185 __set_errno (EBADF);
186 break;
187 case FAIL_EINVAL:
188 __set_errno (EINVAL);
189 break;
190 case FAIL_EACCES:
191 __set_errno (EACCES);
192 break;
193 case FAIL_EXEC:
194 __set_errno (ENOEXEC);
195 break;
197 default:
198 assert(! "getpt: internal error: invalid exit code from pt_chown");
202 cleanup:
203 if (buf != _buf)
204 free (buf);
206 return retval;