158f0484fSRodney W. Grimes /*-
28a16b7a1SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause
38a16b7a1SPedro F. Giffuni *
41b88e35bSBruce Evans * Copyright (c) 1990, 1993, 1994
558f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved.
658f0484fSRodney W. Grimes *
758f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without
858f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions
958f0484fSRodney W. Grimes * are met:
1058f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright
1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer.
1258f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright
1358f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the
1458f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution.
15fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors
1658f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software
1758f0484fSRodney W. Grimes * without specific prior written permission.
1858f0484fSRodney W. Grimes *
1958f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2058f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2158f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2258f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2358f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2458f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2558f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2658f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2758f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2858f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2958f0484fSRodney W. Grimes * SUCH DAMAGE.
3058f0484fSRodney W. Grimes */
3158f0484fSRodney W. Grimes
3258f0484fSRodney W. Grimes
3358f0484fSRodney W. Grimes #include <sys/types.h>
3458f0484fSRodney W. Grimes #include <sys/ioctl.h>
351b88e35bSBruce Evans #include <sys/stat.h>
361b88e35bSBruce Evans
3758f0484fSRodney W. Grimes #include <errno.h>
381b88e35bSBruce Evans #include <fcntl.h>
3958f0484fSRodney W. Grimes #include <grp.h>
400ebec5d3SMark Murray #include <libutil.h>
411b88e35bSBruce Evans #include <stdlib.h>
421b88e35bSBruce Evans #include <string.h>
431b88e35bSBruce Evans #include <termios.h>
44b4d2cfd9SDag-Erling Smørgrav #include <unistd.h>
4558f0484fSRodney W. Grimes
46c50897c3SJohn Baldwin int
openpty(int * amaster,int * aslave,char * name,struct termios * termp,struct winsize * winp)47c50897c3SJohn Baldwin openpty(int *amaster, int *aslave, char *name, struct termios *termp,
48db256336SOlivier Houchard struct winsize *winp)
49db256336SOlivier Houchard {
50889befc4SJohn Baldwin const char *slavename;
51db256336SOlivier Houchard int master, slave;
52db256336SOlivier Houchard
538d333b3cSEd Schouten master = posix_openpt(O_RDWR|O_NOCTTY);
54db256336SOlivier Houchard if (master == -1)
55db256336SOlivier Houchard return (-1);
56db256336SOlivier Houchard
578d333b3cSEd Schouten if (grantpt(master) == -1)
588d333b3cSEd Schouten goto bad;
598d333b3cSEd Schouten
608d333b3cSEd Schouten if (unlockpt(master) == -1)
618d333b3cSEd Schouten goto bad;
62db256336SOlivier Houchard
63889befc4SJohn Baldwin slavename = ptsname(master);
648d333b3cSEd Schouten if (slavename == NULL)
658d333b3cSEd Schouten goto bad;
66889befc4SJohn Baldwin
67889befc4SJohn Baldwin slave = open(slavename, O_RDWR);
688d333b3cSEd Schouten if (slave == -1)
698d333b3cSEd Schouten goto bad;
70db256336SOlivier Houchard
71db256336SOlivier Houchard *amaster = master;
72db256336SOlivier Houchard *aslave = slave;
73db256336SOlivier Houchard
74db256336SOlivier Houchard if (name)
75c50897c3SJohn Baldwin strcpy(name, slavename);
76db256336SOlivier Houchard if (termp)
77db256336SOlivier Houchard tcsetattr(slave, TCSAFLUSH, termp);
78db256336SOlivier Houchard if (winp)
79db256336SOlivier Houchard ioctl(slave, TIOCSWINSZ, (char *)winp);
80db256336SOlivier Houchard
81db256336SOlivier Houchard return (0);
828d333b3cSEd Schouten
838d333b3cSEd Schouten bad: close(master);
848d333b3cSEd Schouten return (-1);
85db256336SOlivier Houchard }
86db256336SOlivier Houchard
873b7e1cc8SPeter Wemm int
forkpty(int * amaster,char * name,struct termios * termp,struct winsize * winp)88547fa0d9SMark Murray forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp)
8958f0484fSRodney W. Grimes {
9058f0484fSRodney W. Grimes int master, slave, pid;
9158f0484fSRodney W. Grimes
9258f0484fSRodney W. Grimes if (openpty(&master, &slave, name, termp, winp) == -1)
9358f0484fSRodney W. Grimes return (-1);
9458f0484fSRodney W. Grimes switch (pid = fork()) {
9558f0484fSRodney W. Grimes case -1:
96*a4aaee21SDag-Erling Smørgrav (void)close(master);
977e8fba78SWarner Losh (void)close(slave);
9858f0484fSRodney W. Grimes return (-1);
9958f0484fSRodney W. Grimes case 0:
10058f0484fSRodney W. Grimes /*
10158f0484fSRodney W. Grimes * child
10258f0484fSRodney W. Grimes */
10358f0484fSRodney W. Grimes (void)close(master);
10458f0484fSRodney W. Grimes login_tty(slave);
10558f0484fSRodney W. Grimes return (0);
10658f0484fSRodney W. Grimes }
10758f0484fSRodney W. Grimes /*
10858f0484fSRodney W. Grimes * parent
10958f0484fSRodney W. Grimes */
11058f0484fSRodney W. Grimes *amaster = master;
11158f0484fSRodney W. Grimes (void) close(slave);
11258f0484fSRodney W. Grimes return (pid);
11358f0484fSRodney W. Grimes }
114