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 <sys/types.h>
20#include <termios.h>
21#include <unistd.h>
22#include <utmp.h>
23#include <pty.h>
24#include <shlib-compat.h>
25
26int
27__forkpty (int *pptmx, char *name, const struct termios *termp,
28 const struct winsize *winp)
29{
30 int ptmx, terminal, pid;
31
32 if (openpty (&ptmx, &terminal, name, termp, winp) == -1)
33 return -1;
34
35 switch (pid = __fork ())
36 {
37 case -1:
38 __close (ptmx);
39 __close (terminal);
40 return -1;
41 case 0:
42 /* Child. */
43 __close (ptmx);
44 if (login_tty (terminal))
45 _exit (1);
46
47 return 0;
48 default:
49 /* Parent. */
50 *pptmx = ptmx;
51 __close (terminal);
52
53 return pid;
54 }
55}
56versioned_symbol (libc, __forkpty, forkpty, GLIBC_2_34);
57libc_hidden_ver (__forkpty, forkpty)
58
59#if OTHER_SHLIB_COMPAT (libutil, GLIBC_2_0, GLIBC_2_34)
60compat_symbol (libutil, __forkpty, forkpty, GLIBC_2_0);
61#endif
62