1 /* Open a pseudoterminal.
2 Copyright (C) 2018-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
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, see
17 <https://www.gnu.org/licenses/>. */
19 #include <support/tty.h>
20 #include <support/check.h>
21 #include <support/support.h>
29 #include <sys/ioctl.h>
32 /* As ptsname, but allocates space for an appropriately-sized string
39 char *buf
= xmalloc (buf_len
);
42 rv
= ptsname_r (fd
, buf
, buf_len
);
44 FAIL_EXIT1 ("ptsname_r: %s", strerror (errno
));
46 if (memchr (buf
, '\0', buf_len
))
47 return buf
; /* ptsname succeeded and the buffer was not truncated */
50 buf
= xrealloc (buf
, buf_len
);
55 support_openpty (int *a_outer
, int *a_inner
, char **a_name
,
56 const struct termios
*termp
,
57 const struct winsize
*winp
)
59 int outer
= -1, inner
= -1;
62 outer
= posix_openpt (O_RDWR
| O_NOCTTY
);
64 FAIL_EXIT1 ("posix_openpt: %s", strerror (errno
));
67 FAIL_EXIT1 ("grantpt: %s", strerror (errno
));
70 FAIL_EXIT1 ("unlockpt: %s", strerror (errno
));
74 inner
= ioctl (outer
, TIOCGPTPEER
, O_RDWR
| O_NOCTTY
);
78 /* The kernel might not support TIOCGPTPEER, fall back to open
80 namebuf
= xptsname (outer
);
81 inner
= open (namebuf
, O_RDWR
| O_NOCTTY
);
83 FAIL_EXIT1 ("%s: %s", namebuf
, strerror (errno
));
88 if (tcsetattr (inner
, TCSAFLUSH
, termp
))
89 FAIL_EXIT1 ("tcsetattr: %s", strerror (errno
));
94 if (ioctl (inner
, TIOCSWINSZ
, winp
))
95 FAIL_EXIT1 ("TIOCSWINSZ: %s", strerror (errno
));
102 namebuf
= xptsname (outer
);