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
26 #include <sys/resource.h>
28 #include <sys/types.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. */
40 pts_name (int fd
, char **pts
, size_t buf_len
)
51 rv
= __ptsname_r (fd
, buf
, buf_len
);
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. */
59 errno
= rv
; /* Not necessarily set by __ptsname_r. */
63 if (memchr (buf
, '\0', buf_len
))
64 /* We succeeded and the returned name fit in the buffer. */
67 /* Try again with a longer buffer. */
68 buf_len
+= buf_len
; /* Double it */
71 /* No initial buffer; start out by mallocing one. */
72 buf_len
= 128; /* First time guess. */
75 /* We've already malloced another buffer at least once. */
76 new_buf
= (char *) realloc (buf
, buf_len
);
78 new_buf
= (char *) malloc (buf_len
);
89 *pts
= buf
; /* Return buffer to the user. */
91 free (buf
); /* Free what we malloced when returning an error. */
96 /* Change the ownership and access permission of the slave pseudo
97 terminal associated with the master pseudo terminal specified
112 size_t grbuflen
= __sysconf (_SC_GETGR_R_SIZE_MAX
);
118 if (pts_name (fd
, &buf
, sizeof (_buf
)))
121 if (__xstat64 (_STAT_VER
, buf
, &st
) < 0)
124 /* Make sure that we own the device. */
126 if (st
.st_uid
!= uid
)
128 if (__chown (buf
, uid
, st
.st_gid
) < 0)
132 /* Get the group ID of the special `tty' group. */
133 if (grbuflen
== (size_t) -1L)
134 /* `sysconf' does not support _SC_GETGR_R_SIZE_MAX.
135 Try a moderate value. */
137 grtmpbuf
= (char *) __alloca (grbuflen
);
138 __getgrnam_r (TTY_GROUP
, &grbuf
, grtmpbuf
, grbuflen
, &p
);
139 gid
= p
? p
->gr_gid
: __getgid ();
141 /* Make sure the group of the device is that special group. */
142 if (st
.st_gid
!= gid
)
144 if (__chown (buf
, uid
, gid
) < 0)
148 /* Make sure the permission mode is set to readable and writable by
149 the owner, and writable by the group. */
150 if ((st
.st_mode
& ACCESSPERMS
) != (S_IRUSR
|S_IWUSR
|S_IWGRP
))
152 if (__chmod (buf
, S_IRUSR
|S_IWUSR
|S_IWGRP
) < 0)
159 /* We have to use the helper program. */
167 /* Disable core dumps. */
168 struct rlimit rl
= { 0, 0 };
169 __setrlimit (RLIMIT_CORE
, &rl
);
171 /* We pass the master pseudo terminal as file descriptor PTY_FILENO. */
172 if (fd
!= PTY_FILENO
)
173 if (__dup2 (fd
, PTY_FILENO
) < 0)
176 execle (_PATH_PT_CHOWN
, basename (_PATH_PT_CHOWN
), NULL
, NULL
);
183 if (__waitpid (pid
, &w
, 0) == -1)
186 __set_errno (ENOEXEC
);
188 switch (WEXITSTATUS(w
))
197 __set_errno (EINVAL
);
200 __set_errno (EACCES
);
203 __set_errno (ENOEXEC
);
207 assert(! "getpt: internal error: invalid exit code from pt_chown");