xref: /titanic_50/usr/src/lib/libc/port/gen/pt.c (revision 2df1fe9ca32bb227b9158c67f5c00b54c20b10fd)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 #pragma weak ptsname = _ptsname
33 #pragma weak grantpt = _grantpt
34 #pragma weak unlockpt = _unlockpt
35 #pragma weak posix_openpt = _posix_openpt
36 
37 #include "synonyms.h"
38 #include "libc.h"
39 #include "mtlib.h"
40 #include <sys/types.h>
41 #include <signal.h>
42 #include <sys/param.h>
43 #include <sys/mkdev.h>
44 #include <sys/stream.h>
45 #include <sys/stropts.h>
46 #include <sys/wait.h>
47 #include <sys/signal.h>
48 #include <errno.h>
49 #include <fcntl.h>
50 #include <sys/stat.h>
51 #include <sys/ptms.h>
52 #include <string.h>
53 #include <stdlib.h>
54 #include <unistd.h>
55 #include <wait.h>
56 #include <spawn.h>
57 #include <grp.h>
58 #include "tsd.h"
59 
60 #define	PTSNAME "/dev/pts/"		/* slave name */
61 #define	PTLEN   32			/* slave name length */
62 #define	DEFAULT_TTY_GROUP	"tty"	/* slave device group owner */
63 
64 static void itoa(int, char *);
65 
66 /*
67  *  Check that fd argument is a file descriptor of an opened master.
68  *  Do this by sending an ISPTM ioctl message down stream. Ioctl()
69  *  will fail if:(1) fd is not a valid file descriptor.(2) the file
70  *  represented by fd does not understand ISPTM(not a master device).
71  *  If we have a valid master, get its minor number via fstat().
72  *  Concatenate it to PTSNAME and return it as the name of the slave
73  *  device.
74  */
75 static dev_t
76 ptsdev(int fd)
77 {
78 	struct stat64 status;
79 	struct strioctl istr;
80 
81 	istr.ic_cmd = ISPTM;
82 	istr.ic_len = 0;
83 	istr.ic_timout = 0;
84 	istr.ic_dp = NULL;
85 
86 	if (ioctl(fd, I_STR, &istr) < 0 || fstat64(fd, &status) < 0)
87 		return (NODEV);
88 
89 	return (minor(status.st_rdev));
90 }
91 
92 char *
93 ptsname(int fd)
94 {
95 	dev_t dev;
96 	char *sname;
97 
98 	if ((dev = ptsdev(fd)) == NODEV)
99 		return (NULL);
100 
101 	sname = tsdalloc(_T_PTSNAME, PTLEN, NULL);
102 	if (sname == NULL)
103 		return (NULL);
104 	(void) strcpy(sname, PTSNAME);
105 	itoa(dev, sname + strlen(PTSNAME));
106 
107 	/*
108 	 * This lookup will create the /dev/pts node (if the corresponding
109 	 * pty exists.
110 	 */
111 	if (access(sname, F_OK) ==  0)
112 		return (sname);
113 
114 	return (NULL);
115 }
116 
117 /*
118  * Send an ioctl down to the master device requesting the
119  * master/slave pair be unlocked.
120  */
121 int
122 unlockpt(int fd)
123 {
124 	struct strioctl istr;
125 
126 	istr.ic_cmd = UNLKPT;
127 	istr.ic_len = 0;
128 	istr.ic_timout = 0;
129 	istr.ic_dp = NULL;
130 
131 	if (ioctl(fd, I_STR, &istr) < 0)
132 		return (-1);
133 
134 	return (0);
135 }
136 
137 int
138 grantpt(int fd)
139 {
140 	struct strioctl istr;
141 	pt_own_t pto;
142 	struct group *gr_name;
143 
144 	/* validate the file descriptor before proceeding */
145 	if (ptsdev(fd) == NODEV)
146 		return (-1);
147 
148 	pto.pto_ruid = getuid();
149 
150 	gr_name = getgrnam(DEFAULT_TTY_GROUP);
151 	if (gr_name)
152 		pto.pto_rgid = gr_name->gr_gid;
153 	else
154 		pto.pto_rgid = getgid();
155 
156 	istr.ic_cmd = OWNERPT;
157 	istr.ic_len = sizeof (pt_own_t);
158 	istr.ic_timout = 0;
159 	istr.ic_dp = (char *)&pto;
160 
161 	if (ioctl(fd, I_STR, &istr) != 0) {
162 		errno = EACCES;
163 		return (-1);
164 	}
165 
166 	return (0);
167 }
168 
169 /*
170  * Send an ioctl down to the master device requesting the master/slave pair
171  * be assigned to the given zone.
172  */
173 int
174 zonept(int fd, zoneid_t zoneid)
175 {
176 	struct strioctl istr;
177 
178 	istr.ic_cmd = ZONEPT;
179 	istr.ic_len = sizeof (zoneid);
180 	istr.ic_timout = 0;
181 	istr.ic_dp = (char *)&zoneid;
182 
183 	if (ioctl(fd, I_STR, &istr) != 0) {
184 		return (-1);
185 	}
186 	return (0);
187 }
188 
189 
190 static void
191 itoa(int i, char *ptr)
192 {
193 	int dig = 0;
194 	int tempi;
195 
196 	tempi = i;
197 	do {
198 		dig++;
199 		tempi /= 10;
200 	} while (tempi);
201 
202 	ptr += dig;
203 	*ptr = '\0';
204 	while (--dig >= 0) {
205 		*(--ptr) = i % 10 + '0';
206 		i /= 10;
207 	}
208 }
209 
210 
211 /*
212  * added for SUSv3 standard
213  *
214  * Open a pseudo-terminal device.  External interface.
215  */
216 
217 int
218 posix_openpt(int oflag)
219 {
220 	return (open("/dev/ptmx", oflag));
221 }
222