1 /* Copyright (C) 1998 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 Library General Public License as
7 published by the Free Software Foundation; either version 2 of the
8 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 Library General Public License for more details.
15 You should have received a copy of the GNU Library General Public
16 License along with the GNU C Library; see the file COPYING.LIB. If not,
17 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
26 #include <sys/resource.h>
28 #include <sys/types.h>
31 #include "pty-private.h"
34 /* Return the result of ptsname_r in the buffer pointed to by PTS,
35 which should be of length BUF_LEN. If it is too long to fit in
36 this buffer, a sufficiently long buffer is allocated using malloc,
37 and returned in PTS. 0 is returned upon success, -1 otherwise. */
39 pts_name (int fd, char **pts, size_t buf_len)
50 rv = ptsname_r (fd, buf, buf_len);
52 if (rv != 0 || memchr (buf, '\0', buf_len))
53 /* We either got an error, or we succeeded and the
54 returned name fit in the buffer. */
57 /* Try again with a longer buffer. */
58 buf_len += buf_len; /* Double it */
61 /* No initial buffer; start out by mallocing one. */
62 buf_len = 128; /* First time guess. */
65 /* We've already malloced another buffer at least once. */
66 new_buf = realloc (buf, buf_len);
68 new_buf = malloc (buf_len);
79 *pts = buf; /* Return buffer to the user. */
81 free (buf); /* Free what we malloced when returning an error. */
86 /* Change the ownership and access permission of the slave pseudo
87 terminal associated with the master pseudo terminal specified
104 if (pts_name (fd, &buf, sizeof (_buf)))
107 if (stat(buf, &st) < 0)
110 /* Make sure that we own the device. */
112 if (st.st_uid != uid)
114 if (chown (buf, uid, st.st_gid) < 0)
120 /* Make sure the group of the device is that special group. */
121 if (st.st_gid != gid)
123 if (chown (buf, uid, gid) < 0)
127 /* Make sure the permission mode is set to readable and writable by
128 the owner, and writable by the group. */
129 if ((st.st_mode & ACCESSPERMS) != (S_IRUSR|S_IWUSR|S_IWGRP))
131 if (chmod (buf, S_IRUSR|S_IWUSR|S_IWGRP) < 0)
138 /* We have to use the helper program. */
146 /* Disable core dumps. */
147 struct rlimit rl = { 0, 0 };
148 setrlimit (RLIMIT_CORE, &rl);
150 /* We pase the master pseudo terminal as file descriptor PTY_FILENO. */
151 if (fd != PTY_FILENO)
152 if (dup2 (fd, PTY_FILENO) < 0)
155 execle (_PATH_PT_CHOWN, _PATH_PT_CHOWN, NULL, NULL);
162 if (waitpid (pid, &w, 0) == -1)
167 switch (WEXITSTATUS(w))
186 assert(! "getpt: internal error: invalid exit code from pt_chown");