1 /* Copyright (C) 1998, 2000, 2001, 2002, 2009 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
27 #include <sys/resource.h>
29 #include <sys/types.h>
33 #include "pty-private.h"
36 /* Return the result of ptsname_r in the buffer pointed to by PTS,
37 which should be of length BUF_LEN. If it is too long to fit in
38 this buffer, a sufficiently long buffer is allocated using malloc,
39 and returned in PTS. 0 is returned upon success, -1 otherwise. */
41 pts_name (int fd
, char **pts
, size_t buf_len
, struct stat64
*stp
)
52 rv
= __ptsname_internal (fd
, buf
, buf_len
, stp
);
56 /* ptsname_r returns with ENOTTY to indicate
57 a descriptor not referring to a pty master.
58 For this condition, grantpt must return EINVAL. */
60 errno
= rv
; /* Not necessarily set by __ptsname_r. */
64 if (memchr (buf
, '\0', buf_len
))
65 /* We succeeded and the returned name fit in the buffer. */
68 /* Try again with a longer buffer. */
69 buf_len
+= buf_len
; /* Double it */
72 /* No initial buffer; start out by mallocing one. */
73 buf_len
= 128; /* First time guess. */
76 /* We've already malloced another buffer at least once. */
77 new_buf
= (char *) realloc (buf
, buf_len
);
79 new_buf
= (char *) malloc (buf_len
);
90 *pts
= buf
; /* Return buffer to the user. */
92 free (buf
); /* Free what we malloced when returning an error. */
97 /* Change the ownership and access permission of the slave pseudo
98 terminal associated with the master pseudo terminal specified
112 if (__builtin_expect (pts_name (fd
, &buf
, sizeof (_buf
), &st
), 0))
114 int save_errno
= errno
;
116 /* Check, if the file descriptor is valid. pts_name returns the
117 wrong errno number, so we cannot use that. */
118 if (__libc_fcntl (fd
, F_GETFD
) == -1 && errno
== EBADF
)
121 /* If the filedescriptor is no TTY, grantpt has to set errno
123 if (save_errno
== ENOTTY
)
124 __set_errno (EINVAL
);
126 __set_errno (save_errno
);
131 /* Make sure that we own the device. */
132 uid_t uid
= __getuid ();
133 if (st
.st_uid
!= uid
)
135 if (__chown (buf
, uid
, st
.st_gid
) < 0)
139 static int tty_gid
= -1;
140 if (__builtin_expect (tty_gid
== -1, 0))
144 size_t grbuflen
= __sysconf (_SC_GETGR_R_SIZE_MAX
);
147 /* Get the group ID of the special `tty' group. */
148 if (grbuflen
== (size_t) -1L)
149 /* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
150 Try a moderate value. */
152 grtmpbuf
= (char *) __alloca (grbuflen
);
153 __getgrnam_r (TTY_GROUP
, &grbuf
, grtmpbuf
, grbuflen
, &p
);
157 gid_t gid
= tty_gid
== -1 ? __getgid () : tty_gid
;
159 /* Make sure the group of the device is that special group. */
160 if (st
.st_gid
!= gid
)
162 if (__chown (buf
, uid
, gid
) < 0)
166 /* Make sure the permission mode is set to readable and writable by
167 the owner, and writable by the group. */
168 if ((st
.st_mode
& ACCESSPERMS
) != (S_IRUSR
|S_IWUSR
|S_IWGRP
))
170 if (__chmod (buf
, S_IRUSR
|S_IWUSR
|S_IWGRP
) < 0)
177 /* We have to use the helper program. */
180 pid_t pid
= __fork ();
185 /* Disable core dumps. */
186 struct rlimit rl
= { 0, 0 };
187 __setrlimit (RLIMIT_CORE
, &rl
);
189 /* We pass the master pseudo terminal as file descriptor PTY_FILENO. */
190 if (fd
!= PTY_FILENO
)
191 if (__dup2 (fd
, PTY_FILENO
) < 0)
198 execle (_PATH_PT_CHOWN
, basename (_PATH_PT_CHOWN
), NULL
, NULL
);
205 if (__waitpid (pid
, &w
, 0) == -1)
208 __set_errno (ENOEXEC
);
210 switch (WEXITSTATUS (w
))
219 __set_errno (EINVAL
);
222 __set_errno (EACCES
);
225 __set_errno (ENOEXEC
);
228 __set_errno (ENOMEM
);
232 assert(! "getpt: internal error: invalid exit code from pt_chown");