]> git.ipfire.org Git - thirdparty/glibc.git/blame - login/openpty.c
Linux: consolidate sendfile implementation
[thirdparty/glibc.git] / login / openpty.c
CommitLineData
581c785b 1/* Copyright (C) 1998-2022 Free Software Foundation, Inc.
6591c335 2 This file is part of the GNU C Library.
6591c335
UD
3
4 The GNU C Library is free software; you can redistribute it and/or
41bdb6e2
AJ
5 modify it under the terms of the GNU Lesser General Public
6 License as published by the Free Software Foundation; either
7 version 2.1 of the License, or (at your option) any later version.
6591c335
UD
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
41bdb6e2 12 Lesser General Public License for more details.
6591c335 13
41bdb6e2 14 You should have received a copy of the GNU Lesser General Public
59ba27a6 15 License along with the GNU C Library; if not, see
5a82c748 16 <https://www.gnu.org/licenses/>. */
6591c335 17
00bc5db0 18#include <errno.h>
6591c335 19#include <fcntl.h>
00bc5db0 20#include <limits.h>
6591c335
UD
21#include <pty.h>
22#include <stdlib.h>
23#include <string.h>
24#include <termios.h>
25#include <unistd.h>
26#include <sys/types.h>
734c60eb 27#include <shlib-compat.h>
6591c335 28
00bc5db0
UD
29/* Return the result of ptsname_r in the buffer pointed to by PTS,
30 which should be of length BUF_LEN. If it is too long to fit in
31 this buffer, a sufficiently long buffer is allocated using malloc,
32 and returned in PTS. 0 is returned upon success, -1 otherwise. */
33static int
34pts_name (int fd, char **pts, size_t buf_len)
35{
36 int rv;
37 char *buf = *pts;
38
39 for (;;)
40 {
41 char *new_buf;
42
43 if (buf_len)
44 {
734c60eb 45 rv = __ptsname_r (fd, buf, buf_len);
00bc5db0
UD
46
47 if (rv != 0 || memchr (buf, '\0', buf_len))
48 /* We either got an error, or we succeeded and the
49 returned name fit in the buffer. */
50 break;
51
52 /* Try again with a longer buffer. */
53 buf_len += buf_len; /* Double it */
54 }
55 else
56 /* No initial buffer; start out by mallocing one. */
57 buf_len = 128; /* First time guess. */
58
59 if (buf != *pts)
60 /* We've already malloced another buffer at least once. */
61 new_buf = realloc (buf, buf_len);
62 else
63 new_buf = malloc (buf_len);
64 if (! new_buf)
65 {
66 rv = -1;
67 __set_errno (ENOMEM);
68 break;
69 }
70 buf = new_buf;
71 }
72
73 if (rv == 0)
74 *pts = buf; /* Return buffer to the user. */
75 else if (buf != *pts)
76 free (buf); /* Free what we malloced when returning an error. */
77
78 return rv;
79}
80
734c60eb 81/* Create pseudo tty multiplexer/terminal pair and set terminal attributes
00bc5db0 82 according to TERMP and WINP. Return handles for both ends in
734c60eb 83 *PPTMX and *PTERMINAL, and return the name of the terminal end in NAME. */
6591c335 84int
734c60eb
FW
85__openpty (int *pptmx, int *pterminal, char *name,
86 const struct termios *termp, const struct winsize *winp)
6591c335 87{
00bc5db0
UD
88#ifdef PATH_MAX
89 char _buf[PATH_MAX];
90#else
91 char _buf[512];
92#endif
93 char *buf = _buf;
734c60eb 94 int ptmx, ret = -1, terminal = -1;
6591c335 95
645ac9aa
CB
96 *buf = '\0';
97
734c60eb
FW
98 ptmx = __getpt ();
99 if (ptmx == -1)
6591c335
UD
100 return -1;
101
734c60eb 102 if (grantpt (ptmx))
98e07420 103 goto on_error;
00bc5db0 104
734c60eb 105 if (unlockpt (ptmx))
98e07420 106 goto on_error;
6591c335 107
645ac9aa 108#ifdef TIOCGPTPEER
734c60eb
FW
109 /* Try to allocate terminal fd solely based on PTMX fd first. */
110 terminal = __ioctl (ptmx, TIOCGPTPEER, O_RDWR | O_NOCTTY);
645ac9aa 111#endif
734c60eb 112 if (terminal == -1)
645ac9aa 113 {
734c60eb 114 /* Fallback to path-based terminal fd allocation in case kernel doesn't
645ac9aa
CB
115 * support TIOCGPTPEER.
116 */
734c60eb 117 if (pts_name (ptmx, &buf, sizeof (_buf)))
645ac9aa
CB
118 goto on_error;
119
734c60eb
FW
120 terminal = __open64 (buf, O_RDWR | O_NOCTTY);
121 if (terminal == -1)
645ac9aa
CB
122 goto on_error;
123 }
6591c335
UD
124
125 /* XXX Should we ignore errors here? */
b301e68e 126 if (termp)
734c60eb 127 tcsetattr (terminal, TCSAFLUSH, termp);
b301e68e 128#ifdef TIOCSWINSZ
00bc5db0 129 if (winp)
734c60eb 130 __ioctl (terminal, TIOCSWINSZ, winp);
b301e68e 131#endif
00bc5db0 132
734c60eb
FW
133 *pptmx = ptmx;
134 *pterminal = terminal;
00bc5db0 135 if (name != NULL)
645ac9aa
CB
136 {
137 if (*buf == '\0')
734c60eb 138 if (pts_name (ptmx, &buf, sizeof (_buf)))
645ac9aa
CB
139 goto on_error;
140
141 strcpy (name, buf);
142 }
6591c335 143
98e07420
CB
144 ret = 0;
145
146 on_error:
147 if (ret == -1) {
734c60eb 148 __close (ptmx);
98e07420 149
734c60eb
FW
150 if (terminal != -1)
151 __close (terminal);
98e07420
CB
152 }
153
00bc5db0
UD
154 if (buf != _buf)
155 free (buf);
6d8ec2b1 156
98e07420 157 return ret;
6591c335 158}
734c60eb
FW
159versioned_symbol (libc, __openpty, openpty, GLIBC_2_34);
160libc_hidden_ver (__openpty, openpty)
161
162#if OTHER_SHLIB_COMPAT (libutil, GLIBC_2_0, GLIBC_2_34)
163compat_symbol (libutil, __openpty, openpty, GLIBC_2_0);
164#endif