1*171790efSkamil /* $NetBSD: pty.c,v 1.32 2018/06/24 09:30:26 kamil Exp $ */
2f5646a08Schristos
361f28255Scgd /*-
45e3388b0Sperry * Copyright (c) 1990, 1993, 1994
53a724adbScgd * The Regents of the University of California. All rights reserved.
661f28255Scgd *
761f28255Scgd * Redistribution and use in source and binary forms, with or without
861f28255Scgd * modification, are permitted provided that the following conditions
961f28255Scgd * are met:
1061f28255Scgd * 1. Redistributions of source code must retain the above copyright
1161f28255Scgd * notice, this list of conditions and the following disclaimer.
1261f28255Scgd * 2. Redistributions in binary form must reproduce the above copyright
1361f28255Scgd * notice, this list of conditions and the following disclaimer in the
1461f28255Scgd * documentation and/or other materials provided with the distribution.
15eb7c1594Sagc * 3. Neither the name of the University nor the names of its contributors
1661f28255Scgd * may be used to endorse or promote products derived from this software
1761f28255Scgd * without specific prior written permission.
1861f28255Scgd *
1961f28255Scgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2061f28255Scgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2161f28255Scgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2261f28255Scgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2361f28255Scgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2461f28255Scgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2561f28255Scgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2661f28255Scgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2761f28255Scgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2861f28255Scgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2961f28255Scgd * SUCH DAMAGE.
3061f28255Scgd */
3161f28255Scgd
32f5646a08Schristos #include <sys/cdefs.h>
3361f28255Scgd #if defined(LIBC_SCCS) && !defined(lint)
34e42a9484Sjtc #if 0
355e3388b0Sperry static char sccsid[] = "@(#)pty.c 8.3 (Berkeley) 5/16/94";
36e42a9484Sjtc #else
37*171790efSkamil __RCSID("$NetBSD: pty.c,v 1.32 2018/06/24 09:30:26 kamil Exp $");
38e42a9484Sjtc #endif
3961f28255Scgd #endif /* LIBC_SCCS and not lint */
4061f28255Scgd
4161f28255Scgd #include <sys/types.h>
4261f28255Scgd #include <sys/ioctl.h>
435e3388b0Sperry #include <sys/stat.h>
445e3388b0Sperry
45b48252f3Slukem #include <assert.h>
4661f28255Scgd #include <errno.h>
475e3388b0Sperry #include <fcntl.h>
485e3388b0Sperry #include <grp.h>
4961f28255Scgd #include <stdio.h>
5061f28255Scgd #include <string.h>
515e3388b0Sperry #include <termios.h>
525e3388b0Sperry #include <unistd.h>
53e42a9484Sjtc #include <util.h>
5461f28255Scgd
554db962fcSlukem /*
564db962fcSlukem * XXX: `v' removed until no ports are using console devices which use ttyv0
574db962fcSlukem */
584db962fcSlukem #define TTY_LETTERS "pqrstuwxyzPQRST"
59d76e3cd5Satatat #define TTY_OLD_SUFFIX "0123456789abcdef"
60d76e3cd5Satatat #define TTY_NEW_SUFFIX "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
618347998aSpk
628347998aSpk int
openpty(int * amaster,int * aslave,char * name,struct termios * term,struct winsize * winp)632611d5a6Schristos openpty(int *amaster, int *aslave, char *name, struct termios *term,
64fce98185Sad struct winsize *winp)
6561f28255Scgd {
6647b68f7fSchristos char line[] = "/dev/XtyXX";
67f099fa15Schristos const char *cp1, *cp2, *cp, *linep;
68ad96bb0dSchristos int master, slave;
69983a6b1fSchristos gid_t ttygid;
705a55e0c9Schristos mode_t mode;
713ff3472eSchristos struct group grs, *grp;
723ff3472eSchristos char grbuf[1024];
73*171790efSkamil struct ptmget pt;
7461f28255Scgd
75b48252f3Slukem _DIAGASSERT(amaster != NULL);
76b48252f3Slukem _DIAGASSERT(aslave != NULL);
77b48252f3Slukem /* name may be NULL */
782611d5a6Schristos /* term may be NULL */
79b48252f3Slukem /* winp may be NULL */
80b48252f3Slukem
81f099fa15Schristos if ((master = open("/dev/ptm", O_RDWR)) != -1) {
82f099fa15Schristos if (ioctl(master, TIOCPTMGET, &pt) != -1) {
83f099fa15Schristos (void)close(master);
84f099fa15Schristos master = pt.cfd;
85f099fa15Schristos slave = pt.sfd;
86f099fa15Schristos linep = pt.sn;
87f099fa15Schristos goto gotit;
88f099fa15Schristos }
89ebfdf0c0Smycroft (void)close(master);
90f099fa15Schristos }
91f099fa15Schristos
923ff3472eSchristos (void)getgrnam_r("tty", &grs, grbuf, sizeof(grbuf), &grp);
933ff3472eSchristos if (grp != NULL) {
943ff3472eSchristos ttygid = grp->gr_gid;
955a55e0c9Schristos mode = S_IRUSR|S_IWUSR|S_IWGRP;
965a55e0c9Schristos } else {
97db5d4e2dSchristos ttygid = getgid();
985a55e0c9Schristos mode = S_IRUSR|S_IWUSR;
995a55e0c9Schristos }
10061f28255Scgd
101e42a9484Sjtc for (cp1 = TTY_LETTERS; *cp1; cp1++) {
10261f28255Scgd line[8] = *cp1;
103d76e3cd5Satatat for (cp = cp2 = TTY_OLD_SUFFIX TTY_NEW_SUFFIX; *cp2; cp2++) {
1045e3388b0Sperry line[5] = 'p';
10561f28255Scgd line[9] = *cp2;
10661f28255Scgd if ((master = open(line, O_RDWR, 0)) == -1) {
1072611d5a6Schristos if (errno != ENOENT)
1082611d5a6Schristos continue; /* busy */
109c5eb4ab6Slukem if ((size_t)(cp2 - cp + 1) < sizeof(TTY_OLD_SUFFIX))
1102611d5a6Schristos return -1; /* out of ptys */
111d76e3cd5Satatat else
1122611d5a6Schristos break; /* out of ptys in this group */
113c6652502Stls }
11461f28255Scgd line[5] = 't';
115f099fa15Schristos linep = line;
1162936303cSyamt if (chown(line, getuid(), ttygid) == 0 &&
1175a55e0c9Schristos chmod(line, mode) == 0 &&
1182936303cSyamt revoke(line) == 0 &&
1192936303cSyamt (slave = open(line, O_RDWR, 0)) != -1) {
120f099fa15Schristos gotit:
12161f28255Scgd *amaster = master;
12261f28255Scgd *aslave = slave;
12361f28255Scgd if (name)
1242611d5a6Schristos (void)strcpy(name, linep);
1252611d5a6Schristos if (term)
1262611d5a6Schristos (void)tcsetattr(slave, TCSAFLUSH, term);
12761f28255Scgd if (winp)
1282611d5a6Schristos (void)ioctl(slave, TIOCSWINSZ, winp);
1292611d5a6Schristos return 0;
13061f28255Scgd }
13161f28255Scgd (void)close(master);
13261f28255Scgd }
13361f28255Scgd }
13461f28255Scgd errno = ENOENT; /* out of ptys */
1352611d5a6Schristos return -1;
13661f28255Scgd }
13761f28255Scgd
1388347998aSpk pid_t
forkpty(int * amaster,char * name,struct termios * term,struct winsize * winp)1392611d5a6Schristos forkpty(int *amaster, char *name, struct termios *term, struct winsize *winp)
14061f28255Scgd {
1418347998aSpk int master, slave;
1428347998aSpk pid_t pid;
14361f28255Scgd
144b48252f3Slukem _DIAGASSERT(amaster != NULL);
145b48252f3Slukem /* name may be NULL */
1462611d5a6Schristos /* term may be NULL */
147b48252f3Slukem /* winp may be NULL */
148b48252f3Slukem
1492611d5a6Schristos if (openpty(&master, &slave, name, term, winp) == -1)
1502611d5a6Schristos return -1;
15161f28255Scgd switch (pid = fork()) {
15261f28255Scgd case -1:
1532611d5a6Schristos return -1;
15461f28255Scgd case 0:
15561f28255Scgd /*
15661f28255Scgd * child
15761f28255Scgd */
15861f28255Scgd (void)close(master);
15961f28255Scgd login_tty(slave);
1602611d5a6Schristos return 0;
16161f28255Scgd }
16261f28255Scgd /*
16361f28255Scgd * parent
16461f28255Scgd */
16561f28255Scgd *amaster = master;
16661f28255Scgd (void)close(slave);
1672611d5a6Schristos return pid;
16861f28255Scgd }
169