blob: ff3823301513832310941b6baae9e8702d310b92 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/* 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.
4
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.
9
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.
14
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. */
19
20#include <stdio.h>
21#include <errno.h>
22#include <paths.h>
23#include <stdlib.h>
24#include <string.h>
25#include <sys/ioctl.h>
26#include <sys/stat.h>
27#include <sys/sysmacros.h>
28#include <termios.h>
29#include <unistd.h>
30#include <bits/uClibc_uintmaxtostr.h>
31
32
33#if !defined __UNIX98PTY_ONLY__
34
35/* Check if DEV corresponds to a master pseudo terminal device. */
36#define MASTER_P(Dev) \
37 (major ((Dev)) == 2 \
38 || (major ((Dev)) == 4 && minor ((Dev)) >= 128 && minor ((Dev)) < 192) \
39 || (major ((Dev)) >= 128 && major ((Dev)) < 136))
40
41/* Check if DEV corresponds to a slave pseudo terminal device. */
42#define SLAVE_P(Dev) \
43 (major ((Dev)) == 3 \
44 || (major ((Dev)) == 4 && minor ((Dev)) >= 192 && minor ((Dev)) < 256) \
45 || (major ((Dev)) >= 136 && major ((Dev)) < 144))
46
47/* Note that major number 4 corresponds to the old BSD style pseudo
48 terminal devices. As of Linux 2.1.115 these are no longer
49 supported. They have been replaced by major numbers 2 (masters)
50 and 3 (slaves). */
51
52/* The are declared in getpt.c. */
53extern const char __libc_ptyname1[] attribute_hidden;
54extern const char __libc_ptyname2[] attribute_hidden;
55
56#endif
57
58/* Directory where we can find the slave pty nodes. */
59#define _PATH_DEVPTS "/dev/pts/"
60
61/* Store at most BUFLEN characters of the pathname of the slave pseudo
62 terminal associated with the master FD is open on in BUF.
63 Return 0 on success, otherwise an error number. */
64int ptsname_r (int fd, char *buf, size_t buflen)
65{
66 int save_errno = errno;
67#if !defined __UNIX98PTY_ONLY__
68 struct stat st;
69#endif
70 int ptyno;
71
72 if (buf == NULL)
73 {
74 errno = EINVAL;
75 return EINVAL;
76 }
77
78#if !defined __UNIX98PTY_ONLY__
79 if (!isatty (fd))
80 {
81 errno = ENOTTY;
82 return ENOTTY;
83 }
84#elif !defined TIOCGPTN
85# error "__UNIX98PTY_ONLY__ enabled but TIOCGPTN ioctl not supported by your kernel."
86#endif
87#ifdef TIOCGPTN
88 if (ioctl (fd, TIOCGPTN, &ptyno) == 0)
89 {
90 /* Buffer we use to print the number in. */
91 char numbuf[__BUFLEN_INT10TOSTR];
92 static const char devpts[] = _PATH_DEVPTS;
93 char *p;
94
95 p = _int10tostr(&numbuf[sizeof numbuf - 1], ptyno);
96
97 if (buflen < sizeof(devpts) + (size_t)(&numbuf[sizeof(numbuf) - 1] - p))
98 {
99 errno = ERANGE;
100 return ERANGE;
101 }
102
103 strcpy (buf, devpts);
104 strcat (buf, p);
105 /* Note: Don't bother with stat on the slave name and checking the
106 driver's major device number - the ioctl above succeeded so
107 we know the fd was a Unix'98 master and the /dev/pts/ prefix
108 is set by definition. If the name isn't really a slave PTY,
109 the system is misconfigured anyway - something else will fail
110 later.
111 */
112 errno = save_errno;
113 return 0;
114 }
115#endif
116#if defined __UNIX98PTY_ONLY__
117 else
118 {
119 /* If the ioctl fails it wasn't a Unix 98 master PTY */
120 errno = ENOTTY;
121 return ENOTTY;
122 }
123#else
124# if defined TIOCGPTN
125 else if (errno == EINVAL)
126# endif
127 {
128 char *p;
129
130 if (buflen < strlen (_PATH_TTY) + 3)
131 {
132 errno = ERANGE;
133 return ERANGE;
134 }
135
136 if (fstat (fd, &st) < 0)
137 return errno;
138
139 /* Check if FD really is a master pseudo terminal. */
140 if (! MASTER_P (st.st_rdev))
141 {
142 errno = ENOTTY;
143 return ENOTTY;
144 }
145
146 ptyno = minor (st.st_rdev);
147 /* This is for the old BSD pseudo terminals. As of Linux
148 2.1.115 these are no longer supported. */
149 if (major (st.st_rdev) == 4)
150 ptyno -= 128;
151
152 if (ptyno / 16 >= strlen (__libc_ptyname1))
153 {
154 errno = ENOTTY;
155 return ENOTTY;
156 }
157
158 strcpy (buf, _PATH_TTY);
159 p = buf + strlen (buf);
160 p[0] = __libc_ptyname1[ptyno / 16];
161 p[1] = __libc_ptyname2[ptyno % 16];
162 p[2] = '\0';
163 }
164
165 if (stat(buf, &st) < 0)
166 return errno;
167
168 /* Check if the name we're about to return really corresponds to a
169 slave pseudo terminal. */
170 if (! S_ISCHR (st.st_mode) || ! SLAVE_P (st.st_rdev))
171 {
172 /* This really is a configuration problem. */
173 errno = ENOTTY;
174 return ENOTTY;
175 }
176#endif
177
178 errno = save_errno;
179 return 0;
180}
181libc_hidden_def(ptsname_r)
182
183/* Return the pathname of the pseudo terminal slave assoicated with
184 the master FD is open on, or NULL on errors.
185 The returned storage is good until the next call to this function. */
186char *
187ptsname (int fd)
188{
189 static char buffer[sizeof (_PATH_DEVPTS) + 20];
190
191 return ptsname_r (fd, buffer, sizeof (buffer)) != 0 ? NULL : buffer;
192}