xref: /illumos-gate/usr/src/lib/libc/port/gen/pt.c (revision 7d8deab2)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5facf4a8dSllai1  * Common Development and Distribution License (the "License").
6facf4a8dSllai1  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21494a4c51Sraf 
227c478bd9Sstevel@tonic-gate /*
237257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
27*7d8deab2SAndy Fiddaman /*
28*7d8deab2SAndy Fiddaman  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
29*7d8deab2SAndy Fiddaman  */
30*7d8deab2SAndy Fiddaman 
317c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
327c478bd9Sstevel@tonic-gate /*	  All Rights Reserved	*/
337c478bd9Sstevel@tonic-gate 
347257d1b4Sraf #pragma weak _ptsname = ptsname
357257d1b4Sraf #pragma weak _grantpt = grantpt
367257d1b4Sraf #pragma weak _unlockpt = unlockpt
377257d1b4Sraf 
387257d1b4Sraf #include "lint.h"
39494a4c51Sraf #include "libc.h"
40494a4c51Sraf #include "mtlib.h"
417c478bd9Sstevel@tonic-gate #include <sys/types.h>
427c478bd9Sstevel@tonic-gate #include <signal.h>
437c478bd9Sstevel@tonic-gate #include <sys/param.h>
447c478bd9Sstevel@tonic-gate #include <sys/mkdev.h>
457c478bd9Sstevel@tonic-gate #include <sys/stream.h>
467c478bd9Sstevel@tonic-gate #include <sys/stropts.h>
477c478bd9Sstevel@tonic-gate #include <sys/wait.h>
487c478bd9Sstevel@tonic-gate #include <sys/signal.h>
497c478bd9Sstevel@tonic-gate #include <errno.h>
507c478bd9Sstevel@tonic-gate #include <fcntl.h>
517c478bd9Sstevel@tonic-gate #include <sys/stat.h>
527c478bd9Sstevel@tonic-gate #include <sys/ptms.h>
537c478bd9Sstevel@tonic-gate #include <string.h>
547c478bd9Sstevel@tonic-gate #include <stdlib.h>
557c478bd9Sstevel@tonic-gate #include <unistd.h>
567c478bd9Sstevel@tonic-gate #include <wait.h>
577c478bd9Sstevel@tonic-gate #include <spawn.h>
58facf4a8dSllai1 #include <grp.h>
597c478bd9Sstevel@tonic-gate #include "tsd.h"
607c478bd9Sstevel@tonic-gate 
617c478bd9Sstevel@tonic-gate #define	PTSNAME "/dev/pts/"		/* slave name */
627c478bd9Sstevel@tonic-gate #define	PTLEN   32			/* slave name length */
63facf4a8dSllai1 #define	DEFAULT_TTY_GROUP	"tty"	/* slave device group owner */
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static void itoa(int, char *);
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  *  Check that fd argument is a file descriptor of an opened master.
697c478bd9Sstevel@tonic-gate  *  Do this by sending an ISPTM ioctl message down stream. Ioctl()
707c478bd9Sstevel@tonic-gate  *  will fail if:(1) fd is not a valid file descriptor.(2) the file
717c478bd9Sstevel@tonic-gate  *  represented by fd does not understand ISPTM(not a master device).
727c478bd9Sstevel@tonic-gate  *  If we have a valid master, get its minor number via fstat().
737c478bd9Sstevel@tonic-gate  *  Concatenate it to PTSNAME and return it as the name of the slave
747c478bd9Sstevel@tonic-gate  *  device.
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate static dev_t
ptsdev(int fd)777c478bd9Sstevel@tonic-gate ptsdev(int fd)
787c478bd9Sstevel@tonic-gate {
797c478bd9Sstevel@tonic-gate 	struct stat64 status;
807c478bd9Sstevel@tonic-gate 	struct strioctl istr;
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate 	istr.ic_cmd = ISPTM;
837c478bd9Sstevel@tonic-gate 	istr.ic_len = 0;
847c478bd9Sstevel@tonic-gate 	istr.ic_timout = 0;
857c478bd9Sstevel@tonic-gate 	istr.ic_dp = NULL;
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &istr) < 0 || fstat64(fd, &status) < 0)
887c478bd9Sstevel@tonic-gate 		return (NODEV);
897c478bd9Sstevel@tonic-gate 
907c478bd9Sstevel@tonic-gate 	return (minor(status.st_rdev));
917c478bd9Sstevel@tonic-gate }
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate char *
ptsname(int fd)947c478bd9Sstevel@tonic-gate ptsname(int fd)
957c478bd9Sstevel@tonic-gate {
967c478bd9Sstevel@tonic-gate 	dev_t dev;
977c478bd9Sstevel@tonic-gate 	char *sname;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 	if ((dev = ptsdev(fd)) == NODEV)
1007c478bd9Sstevel@tonic-gate 		return (NULL);
1017c478bd9Sstevel@tonic-gate 
1027c478bd9Sstevel@tonic-gate 	sname = tsdalloc(_T_PTSNAME, PTLEN, NULL);
1037c478bd9Sstevel@tonic-gate 	if (sname == NULL)
1047c478bd9Sstevel@tonic-gate 		return (NULL);
1057c478bd9Sstevel@tonic-gate 	(void) strcpy(sname, PTSNAME);
1067c478bd9Sstevel@tonic-gate 	itoa(dev, sname + strlen(PTSNAME));
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	/*
109facf4a8dSllai1 	 * This lookup will create the /dev/pts node (if the corresponding
110facf4a8dSllai1 	 * pty exists.
1117c478bd9Sstevel@tonic-gate 	 */
112facf4a8dSllai1 	if (access(sname, F_OK) ==  0)
1137c478bd9Sstevel@tonic-gate 		return (sname);
114facf4a8dSllai1 
1157c478bd9Sstevel@tonic-gate 	return (NULL);
1167c478bd9Sstevel@tonic-gate }
1177c478bd9Sstevel@tonic-gate 
1187c478bd9Sstevel@tonic-gate /*
1197c478bd9Sstevel@tonic-gate  * Send an ioctl down to the master device requesting the
1207c478bd9Sstevel@tonic-gate  * master/slave pair be unlocked.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate int
unlockpt(int fd)1237c478bd9Sstevel@tonic-gate unlockpt(int fd)
1247c478bd9Sstevel@tonic-gate {
1257c478bd9Sstevel@tonic-gate 	struct strioctl istr;
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate 	istr.ic_cmd = UNLKPT;
1287c478bd9Sstevel@tonic-gate 	istr.ic_len = 0;
1297c478bd9Sstevel@tonic-gate 	istr.ic_timout = 0;
1307c478bd9Sstevel@tonic-gate 	istr.ic_dp = NULL;
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &istr) < 0)
1337c478bd9Sstevel@tonic-gate 		return (-1);
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 	return (0);
1367c478bd9Sstevel@tonic-gate }
1377c478bd9Sstevel@tonic-gate 
138*7d8deab2SAndy Fiddaman /*
139*7d8deab2SAndy Fiddaman  * XPG4v2 requires that open of a slave pseudo terminal device
140*7d8deab2SAndy Fiddaman  * provides the process with an interface that is identical to
141*7d8deab2SAndy Fiddaman  * the terminal interface.
142*7d8deab2SAndy Fiddaman  *
143*7d8deab2SAndy Fiddaman  * To satisfy this, in strict XPG4v2 mode, this routine also sends
144*7d8deab2SAndy Fiddaman  * a message down the stream that sets a flag in the kernel module
145*7d8deab2SAndy Fiddaman  * so that additional actions are performed when opening an
146*7d8deab2SAndy Fiddaman  * associated slave PTY device. When this happens, modules are
147*7d8deab2SAndy Fiddaman  * automatically pushed onto the stream to provide terminal
148*7d8deab2SAndy Fiddaman  * semantics and those modules are then informed that they should
149*7d8deab2SAndy Fiddaman  * behave in strict XPG4v2 mode which modifies their behaviour. In
150*7d8deab2SAndy Fiddaman  * particular, in strict XPG4v2 mode, empty blocks will be sent up
151*7d8deab2SAndy Fiddaman  * the master side of the stream rather than being suppressed.
152*7d8deab2SAndy Fiddaman  *
153*7d8deab2SAndy Fiddaman  * Most applications do not expect this behaviour so it is only
154*7d8deab2SAndy Fiddaman  * enabled for programs compiled in strict XPG4v2 mode (see
155*7d8deab2SAndy Fiddaman  * stdlib.h).
156*7d8deab2SAndy Fiddaman  */
157*7d8deab2SAndy Fiddaman int
__unlockpt_xpg4(int fd)158*7d8deab2SAndy Fiddaman __unlockpt_xpg4(int fd)
159*7d8deab2SAndy Fiddaman {
160*7d8deab2SAndy Fiddaman 	int ret;
161*7d8deab2SAndy Fiddaman 
162*7d8deab2SAndy Fiddaman 	if ((ret = unlockpt(fd)) == 0) {
163*7d8deab2SAndy Fiddaman 		struct strioctl istr;
164*7d8deab2SAndy Fiddaman 
165*7d8deab2SAndy Fiddaman 		istr.ic_cmd = PTSSTTY;
166*7d8deab2SAndy Fiddaman 		istr.ic_len = 0;
167*7d8deab2SAndy Fiddaman 		istr.ic_timout = 0;
168*7d8deab2SAndy Fiddaman 		istr.ic_dp = NULL;
169*7d8deab2SAndy Fiddaman 
170*7d8deab2SAndy Fiddaman 		if (ioctl(fd, I_STR, &istr) < 0)
171*7d8deab2SAndy Fiddaman 			ret = -1;
172*7d8deab2SAndy Fiddaman 	}
173*7d8deab2SAndy Fiddaman 
174*7d8deab2SAndy Fiddaman 	return (ret);
175*7d8deab2SAndy Fiddaman }
176*7d8deab2SAndy Fiddaman 
177facf4a8dSllai1 int
grantpt(int fd)178facf4a8dSllai1 grantpt(int fd)
1797c478bd9Sstevel@tonic-gate {
180facf4a8dSllai1 	struct strioctl istr;
181facf4a8dSllai1 	pt_own_t pto;
182facf4a8dSllai1 	struct group *gr_name;
1837c478bd9Sstevel@tonic-gate 
1847c478bd9Sstevel@tonic-gate 	/* validate the file descriptor before proceeding */
185facf4a8dSllai1 	if (ptsdev(fd) == NODEV)
1867c478bd9Sstevel@tonic-gate 		return (-1);
1877c478bd9Sstevel@tonic-gate 
188facf4a8dSllai1 	pto.pto_ruid = getuid();
1897c478bd9Sstevel@tonic-gate 
190facf4a8dSllai1 	gr_name = getgrnam(DEFAULT_TTY_GROUP);
191facf4a8dSllai1 	if (gr_name)
192facf4a8dSllai1 		pto.pto_rgid = gr_name->gr_gid;
193facf4a8dSllai1 	else
194facf4a8dSllai1 		pto.pto_rgid = getgid();
1957c478bd9Sstevel@tonic-gate 
19649e92448Svikram 	istr.ic_cmd = OWNERPT;
197facf4a8dSllai1 	istr.ic_len = sizeof (pt_own_t);
198facf4a8dSllai1 	istr.ic_timout = 0;
199facf4a8dSllai1 	istr.ic_dp = (char *)&pto;
2007c478bd9Sstevel@tonic-gate 
201facf4a8dSllai1 	if (ioctl(fd, I_STR, &istr) != 0) {
2027c478bd9Sstevel@tonic-gate 		errno = EACCES;
2037c478bd9Sstevel@tonic-gate 		return (-1);
2047c478bd9Sstevel@tonic-gate 	}
2057c478bd9Sstevel@tonic-gate 
206facf4a8dSllai1 	return (0);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /*
2107c478bd9Sstevel@tonic-gate  * Send an ioctl down to the master device requesting the master/slave pair
2117c478bd9Sstevel@tonic-gate  * be assigned to the given zone.
2127c478bd9Sstevel@tonic-gate  */
2137c478bd9Sstevel@tonic-gate int
zonept(int fd,zoneid_t zoneid)2147c478bd9Sstevel@tonic-gate zonept(int fd, zoneid_t zoneid)
2157c478bd9Sstevel@tonic-gate {
2167c478bd9Sstevel@tonic-gate 	struct strioctl istr;
2177c478bd9Sstevel@tonic-gate 
2187c478bd9Sstevel@tonic-gate 	istr.ic_cmd = ZONEPT;
2197c478bd9Sstevel@tonic-gate 	istr.ic_len = sizeof (zoneid);
2207c478bd9Sstevel@tonic-gate 	istr.ic_timout = 0;
2217c478bd9Sstevel@tonic-gate 	istr.ic_dp = (char *)&zoneid;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &istr) != 0) {
2247c478bd9Sstevel@tonic-gate 		return (-1);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	return (0);
2277c478bd9Sstevel@tonic-gate }
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate static void
itoa(int i,char * ptr)2317c478bd9Sstevel@tonic-gate itoa(int i, char *ptr)
2327c478bd9Sstevel@tonic-gate {
2337c478bd9Sstevel@tonic-gate 	int dig = 0;
2347c478bd9Sstevel@tonic-gate 	int tempi;
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate 	tempi = i;
2377c478bd9Sstevel@tonic-gate 	do {
2387c478bd9Sstevel@tonic-gate 		dig++;
2397c478bd9Sstevel@tonic-gate 		tempi /= 10;
2407c478bd9Sstevel@tonic-gate 	} while (tempi);
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	ptr += dig;
2437c478bd9Sstevel@tonic-gate 	*ptr = '\0';
2447c478bd9Sstevel@tonic-gate 	while (--dig >= 0) {
2457c478bd9Sstevel@tonic-gate 		*(--ptr) = i % 10 + '0';
2467c478bd9Sstevel@tonic-gate 		i /= 10;
2477c478bd9Sstevel@tonic-gate 	}
2487c478bd9Sstevel@tonic-gate }
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate /*
2527c478bd9Sstevel@tonic-gate  * added for SUSv3 standard
2537c478bd9Sstevel@tonic-gate  *
2547c478bd9Sstevel@tonic-gate  * Open a pseudo-terminal device.  External interface.
2557c478bd9Sstevel@tonic-gate  */
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate int
posix_openpt(int oflag)2587c478bd9Sstevel@tonic-gate posix_openpt(int oflag)
2597c478bd9Sstevel@tonic-gate {
2607c478bd9Sstevel@tonic-gate 	return (open("/dev/ptmx", oflag));
2617c478bd9Sstevel@tonic-gate }
262