1 | /* Copyright (C) 1998-2021 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, see |
17 | <https://www.gnu.org/licenses/>. */ |
18 | |
19 | #include <errno.h> |
20 | #include <fcntl.h> |
21 | #include <limits.h> |
22 | #include <pty.h> |
23 | #include <stdlib.h> |
24 | #include <string.h> |
25 | #include <termios.h> |
26 | #include <unistd.h> |
27 | #include <sys/types.h> |
28 | #include <shlib-compat.h> |
29 | |
30 | /* Return the result of ptsname_r in the buffer pointed to by PTS, |
31 | which should be of length BUF_LEN. If it is too long to fit in |
32 | this buffer, a sufficiently long buffer is allocated using malloc, |
33 | and returned in PTS. 0 is returned upon success, -1 otherwise. */ |
34 | static int |
35 | pts_name (int fd, char **pts, size_t buf_len) |
36 | { |
37 | int rv; |
38 | char *buf = *pts; |
39 | |
40 | for (;;) |
41 | { |
42 | char *new_buf; |
43 | |
44 | if (buf_len) |
45 | { |
46 | rv = __ptsname_r (fd, buf, buf_len); |
47 | |
48 | if (rv != 0 || memchr (buf, '\0', buf_len)) |
49 | /* We either got an error, or we succeeded and the |
50 | returned name fit in the buffer. */ |
51 | break; |
52 | |
53 | /* Try again with a longer buffer. */ |
54 | buf_len += buf_len; /* Double it */ |
55 | } |
56 | else |
57 | /* No initial buffer; start out by mallocing one. */ |
58 | buf_len = 128; /* First time guess. */ |
59 | |
60 | if (buf != *pts) |
61 | /* We've already malloced another buffer at least once. */ |
62 | new_buf = realloc (buf, buf_len); |
63 | else |
64 | new_buf = malloc (buf_len); |
65 | if (! new_buf) |
66 | { |
67 | rv = -1; |
68 | __set_errno (ENOMEM); |
69 | break; |
70 | } |
71 | buf = new_buf; |
72 | } |
73 | |
74 | if (rv == 0) |
75 | *pts = buf; /* Return buffer to the user. */ |
76 | else if (buf != *pts) |
77 | free (buf); /* Free what we malloced when returning an error. */ |
78 | |
79 | return rv; |
80 | } |
81 | |
82 | /* Create pseudo tty multiplexer/terminal pair and set terminal attributes |
83 | according to TERMP and WINP. Return handles for both ends in |
84 | *PPTMX and *PTERMINAL, and return the name of the terminal end in NAME. */ |
85 | int |
86 | __openpty (int *pptmx, int *pterminal, char *name, |
87 | const struct termios *termp, const struct winsize *winp) |
88 | { |
89 | #ifdef PATH_MAX |
90 | char _buf[PATH_MAX]; |
91 | #else |
92 | char _buf[512]; |
93 | #endif |
94 | char *buf = _buf; |
95 | int ptmx, ret = -1, terminal = -1; |
96 | |
97 | *buf = '\0'; |
98 | |
99 | ptmx = __getpt (); |
100 | if (ptmx == -1) |
101 | return -1; |
102 | |
103 | if (grantpt (ptmx)) |
104 | goto on_error; |
105 | |
106 | if (unlockpt (ptmx)) |
107 | goto on_error; |
108 | |
109 | #ifdef TIOCGPTPEER |
110 | /* Try to allocate terminal fd solely based on PTMX fd first. */ |
111 | terminal = __ioctl (ptmx, TIOCGPTPEER, O_RDWR | O_NOCTTY); |
112 | #endif |
113 | if (terminal == -1) |
114 | { |
115 | /* Fallback to path-based terminal fd allocation in case kernel doesn't |
116 | * support TIOCGPTPEER. |
117 | */ |
118 | if (pts_name (ptmx, &buf, sizeof (_buf))) |
119 | goto on_error; |
120 | |
121 | terminal = __open64 (buf, O_RDWR | O_NOCTTY); |
122 | if (terminal == -1) |
123 | goto on_error; |
124 | } |
125 | |
126 | /* XXX Should we ignore errors here? */ |
127 | if (termp) |
128 | tcsetattr (terminal, TCSAFLUSH, termp); |
129 | #ifdef TIOCSWINSZ |
130 | if (winp) |
131 | __ioctl (terminal, TIOCSWINSZ, winp); |
132 | #endif |
133 | |
134 | *pptmx = ptmx; |
135 | *pterminal = terminal; |
136 | if (name != NULL) |
137 | { |
138 | if (*buf == '\0') |
139 | if (pts_name (ptmx, &buf, sizeof (_buf))) |
140 | goto on_error; |
141 | |
142 | strcpy (name, buf); |
143 | } |
144 | |
145 | ret = 0; |
146 | |
147 | on_error: |
148 | if (ret == -1) { |
149 | __close (ptmx); |
150 | |
151 | if (terminal != -1) |
152 | __close (terminal); |
153 | } |
154 | |
155 | if (buf != _buf) |
156 | free (buf); |
157 | |
158 | return ret; |
159 | } |
160 | versioned_symbol (libc, __openpty, openpty, GLIBC_2_34); |
161 | libc_hidden_ver (__openpty, openpty) |
162 | |
163 | #if OTHER_SHLIB_COMPAT (libutil, GLIBC_2_0, GLIBC_2_34) |
164 | compat_symbol (libutil, __openpty, openpty, GLIBC_2_0); |
165 | #endif |
166 | |