xref: /dragonfly/lib/libutil/pty.c (revision 7ff0fc30)
1 /*-
2  * Copyright (c) 1990, 1993, 1994
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * @(#)pty.c	8.3 (Berkeley) 5/16/94
30  * $FreeBSD: head/lib/libutil/pty.c 184634 2008-11-04 13:50:50Z des $
31  */
32 
33 #include <sys/types.h>
34 #include <sys/ioctl.h>
35 #include <sys/stat.h>
36 
37 #include <errno.h>
38 #include <fcntl.h>
39 #include <grp.h>
40 #include <libutil.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <termios.h>
44 #include <unistd.h>
45 
46 int
47 openpty(int *amaster, int *aslave, char *name, struct termios *termp,
48 	struct winsize *winp)
49 {
50 	const char *slavename;
51 	int master, slave;
52 
53 	master = posix_openpt(O_RDWR|O_NOCTTY);
54 	if (master == -1)
55 		return (-1);
56 
57 	if (grantpt(master) == -1)
58 		goto bad;
59 
60 	if (unlockpt(master) == -1)
61 		goto bad;
62 
63 	slavename = ptsname(master);
64 	if (slavename == NULL)
65 		goto bad;
66 
67 	slave = open(slavename, O_RDWR);
68 	if (slave == -1)
69 		goto bad;
70 
71 	*amaster = master;
72 	*aslave = slave;
73 
74 	if (name)
75 		strcpy(name, slavename);
76 	if (termp)
77 		tcsetattr(slave, TCSAFLUSH, termp);
78 	if (winp)
79 		ioctl(slave, TIOCSWINSZ, (char *)winp);
80 
81 	return (0);
82 
83 bad:	close(master);
84 	return (-1);
85 }
86 
87 int
88 forkpty(int *amaster, char *name, struct termios *termp, struct winsize *winp)
89 {
90 	int master, slave, pid;
91 
92 	if (openpty(&master, &slave, name, termp, winp) == -1)
93 		return (-1);
94 	switch (pid = fork()) {
95 	case -1:
96 		(void) close(slave);
97 		return (-1);
98 	case 0:
99 		/*
100 		 * child
101 		 */
102 		(void) close(master);
103 		login_tty(slave);
104 		return (0);
105 	}
106 	/*
107 	 * parent
108 	 */
109 	*amaster = master;
110 	(void) close(slave);
111 	return (pid);
112 }
113