xref: /titanic_52/usr/src/lib/libc/port/gen/pt.c (revision ae115bc77f6fcde83175c75b4206dc2e50747966)
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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 /*	Copyright (c) 1988 AT&T	*/
29 /*	  All Rights Reserved  	*/
30 
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 <mtlib.h>
39 #include <sys/types.h>
40 #include <signal.h>
41 #include <sys/param.h>
42 #include <sys/mkdev.h>
43 #include <sys/fs/ufs_fsdir.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 <synch.h>
57 #include <thread.h>
58 #include <spawn.h>
59 #include <libc.h>
60 #include <grp.h>
61 #include "tsd.h"
62 
63 #define	PTSNAME "/dev/pts/"		/* slave name */
64 #define	PTLEN   32			/* slave name length */
65 #define	DEFAULT_TTY_GROUP	"tty"	/* slave device group owner */
66 
67 static void itoa(int, char *);
68 
69 /*
70  *  Check that fd argument is a file descriptor of an opened master.
71  *  Do this by sending an ISPTM ioctl message down stream. Ioctl()
72  *  will fail if:(1) fd is not a valid file descriptor.(2) the file
73  *  represented by fd does not understand ISPTM(not a master device).
74  *  If we have a valid master, get its minor number via fstat().
75  *  Concatenate it to PTSNAME and return it as the name of the slave
76  *  device.
77  */
78 static dev_t
79 ptsdev(int fd)
80 {
81 	struct stat64 status;
82 	struct strioctl istr;
83 
84 	istr.ic_cmd = ISPTM;
85 	istr.ic_len = 0;
86 	istr.ic_timout = 0;
87 	istr.ic_dp = NULL;
88 
89 	if (ioctl(fd, I_STR, &istr) < 0 || fstat64(fd, &status) < 0)
90 		return (NODEV);
91 
92 	return (minor(status.st_rdev));
93 }
94 
95 char *
96 ptsname(int fd)
97 {
98 	dev_t dev;
99 	char *sname;
100 
101 	if ((dev = ptsdev(fd)) == NODEV)
102 		return (NULL);
103 
104 	sname = tsdalloc(_T_PTSNAME, PTLEN, NULL);
105 	if (sname == NULL)
106 		return (NULL);
107 	(void) strcpy(sname, PTSNAME);
108 	itoa(dev, sname + strlen(PTSNAME));
109 
110 	/*
111 	 * This lookup will create the /dev/pts node (if the corresponding
112 	 * pty exists.
113 	 */
114 	if (access(sname, F_OK) ==  0)
115 		return (sname);
116 
117 	return (NULL);
118 }
119 
120 /*
121  * Send an ioctl down to the master device requesting the
122  * master/slave pair be unlocked.
123  */
124 int
125 unlockpt(int fd)
126 {
127 	struct strioctl istr;
128 
129 	istr.ic_cmd = UNLKPT;
130 	istr.ic_len = 0;
131 	istr.ic_timout = 0;
132 	istr.ic_dp = NULL;
133 
134 	if (ioctl(fd, I_STR, &istr) < 0)
135 		return (-1);
136 
137 	return (0);
138 }
139 
140 int
141 grantpt(int fd)
142 {
143 	struct strioctl istr;
144 	pt_own_t pto;
145 	struct group *gr_name;
146 
147 	/* validate the file descriptor before proceeding */
148 	if (ptsdev(fd) == NODEV)
149 		return (-1);
150 
151 	pto.pto_ruid = getuid();
152 
153 	gr_name = getgrnam(DEFAULT_TTY_GROUP);
154 	if (gr_name)
155 		pto.pto_rgid = gr_name->gr_gid;
156 	else
157 		pto.pto_rgid = getgid();
158 
159 	istr.ic_cmd = OWNERPT;
160 	istr.ic_len = sizeof (pt_own_t);
161 	istr.ic_timout = 0;
162 	istr.ic_dp = (char *)&pto;
163 
164 	if (ioctl(fd, I_STR, &istr) != 0) {
165 		errno = EACCES;
166 		return (-1);
167 	}
168 
169 	return (0);
170 }
171 
172 /*
173  * Send an ioctl down to the master device requesting the master/slave pair
174  * be assigned to the given zone.
175  */
176 int
177 zonept(int fd, zoneid_t zoneid)
178 {
179 	struct strioctl istr;
180 
181 	istr.ic_cmd = ZONEPT;
182 	istr.ic_len = sizeof (zoneid);
183 	istr.ic_timout = 0;
184 	istr.ic_dp = (char *)&zoneid;
185 
186 	if (ioctl(fd, I_STR, &istr) != 0) {
187 		return (-1);
188 	}
189 	return (0);
190 }
191 
192 
193 static void
194 itoa(int i, char *ptr)
195 {
196 	int dig = 0;
197 	int tempi;
198 
199 	tempi = i;
200 	do {
201 		dig++;
202 		tempi /= 10;
203 	} while (tempi);
204 
205 	ptr += dig;
206 	*ptr = '\0';
207 	while (--dig >= 0) {
208 		*(--ptr) = i % 10 + '0';
209 		i /= 10;
210 	}
211 }
212 
213 
214 /*
215  * added for SUSv3 standard
216  *
217  * Open a pseudo-terminal device.  External interface.
218  */
219 
220 int
221 posix_openpt(int oflag)
222 {
223 	return (open("/dev/ptmx", oflag));
224 }
225