| 1 |
/* Fork a child process attached to the slave of a pseudo-terminal. |
| 2 |
Copyright (C) 2010-2026 Free Software Foundation, Inc. |
| 3 |
|
| 4 |
This file is free software: you can redistribute it and/or modify |
| 5 |
it under the terms of the GNU Lesser General Public License as |
| 6 |
published by the Free Software Foundation, either version 3 of the |
| 7 |
License, or (at your option) any later version. |
| 8 |
|
| 9 |
This file 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 |
| 12 |
GNU Lesser General Public License for more details. |
| 13 |
|
| 14 |
You should have received a copy of the GNU Lesser General Public License |
| 15 |
along with this program. If not, see <https://www.gnu.org/licenses/>. */ |
| 16 |
|
| 17 |
#include <config.h> |
| 18 |
|
| 19 |
/* Specification. */ |
| 20 |
#include <pty.h> |
| 21 |
|
| 22 |
#if HAVE_FORKPTY |
| 23 |
|
| 24 |
/* Provide a wrapper with the prototype of glibc-2.8 and newer. */ |
| 25 |
# undef forkpty |
| 26 |
int |
| 27 |
rpl_forkpty (int *amaster, char *name, struct termios const *termp, |
| 28 |
struct winsize const *winp) |
| 29 |
{ |
| 30 |
/* Cast away const, for implementations with weaker prototypes. */ |
| 31 |
return forkpty (amaster, name, (struct termios *) termp, |
| 32 |
(struct winsize *) winp); |
| 33 |
} |
| 34 |
|
| 35 |
#else /* AIX 5.1, HP-UX 11, Solaris 10. */ |
| 36 |
|
| 37 |
# include <pty.h> |
| 38 |
# include <unistd.h> |
| 39 |
|
| 40 |
extern int login_tty (int slave_fd); |
| 41 |
|
| 42 |
int |
| 43 |
forkpty (int *amaster, char *name, |
| 44 |
const struct termios *termp, const struct winsize *winp) |
| 45 |
{ |
| 46 |
int master, slave; |
| 47 |
|
| 48 |
if (openpty (&master, &slave, name, termp, winp) == -1) |
| 49 |
return -1; |
| 50 |
|
| 51 |
int pid = fork (); |
| 52 |
switch (pid) |
| 53 |
{ |
| 54 |
case -1: |
| 55 |
close (master); |
| 56 |
close (slave); |
| 57 |
return -1; |
| 58 |
|
| 59 |
case 0: |
| 60 |
/* Child. */ |
| 61 |
close (master); |
| 62 |
if (login_tty (slave)) |
| 63 |
_exit (1); |
| 64 |
return 0; |
| 65 |
|
| 66 |
default: |
| 67 |
/* Parent. */ |
| 68 |
*amaster = master; |
| 69 |
close (slave); |
| 70 |
return pid; |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
#endif |