xref: /onnv-gate/usr/src/lib/libc/port/gen/pt.c (revision 6812:febeba71273d)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52621Sllai1  * Common Development and Distribution License (the "License").
62621Sllai1  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
213453Sraf 
220Sstevel@tonic-gate /*
23*6812Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
280Sstevel@tonic-gate /*	  All Rights Reserved  	*/
290Sstevel@tonic-gate 
30*6812Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
310Sstevel@tonic-gate 
32*6812Sraf #pragma weak _ptsname = ptsname
33*6812Sraf #pragma weak _grantpt = grantpt
34*6812Sraf #pragma weak _unlockpt = unlockpt
35*6812Sraf 
36*6812Sraf #include "lint.h"
373453Sraf #include "libc.h"
383453Sraf #include "mtlib.h"
390Sstevel@tonic-gate #include <sys/types.h>
400Sstevel@tonic-gate #include <signal.h>
410Sstevel@tonic-gate #include <sys/param.h>
420Sstevel@tonic-gate #include <sys/mkdev.h>
430Sstevel@tonic-gate #include <sys/stream.h>
440Sstevel@tonic-gate #include <sys/stropts.h>
450Sstevel@tonic-gate #include <sys/wait.h>
460Sstevel@tonic-gate #include <sys/signal.h>
470Sstevel@tonic-gate #include <errno.h>
480Sstevel@tonic-gate #include <fcntl.h>
490Sstevel@tonic-gate #include <sys/stat.h>
500Sstevel@tonic-gate #include <sys/ptms.h>
510Sstevel@tonic-gate #include <string.h>
520Sstevel@tonic-gate #include <stdlib.h>
530Sstevel@tonic-gate #include <unistd.h>
540Sstevel@tonic-gate #include <wait.h>
550Sstevel@tonic-gate #include <spawn.h>
562621Sllai1 #include <grp.h>
570Sstevel@tonic-gate #include "tsd.h"
580Sstevel@tonic-gate 
590Sstevel@tonic-gate #define	PTSNAME "/dev/pts/"		/* slave name */
600Sstevel@tonic-gate #define	PTLEN   32			/* slave name length */
612621Sllai1 #define	DEFAULT_TTY_GROUP	"tty"	/* slave device group owner */
620Sstevel@tonic-gate 
630Sstevel@tonic-gate static void itoa(int, char *);
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  *  Check that fd argument is a file descriptor of an opened master.
670Sstevel@tonic-gate  *  Do this by sending an ISPTM ioctl message down stream. Ioctl()
680Sstevel@tonic-gate  *  will fail if:(1) fd is not a valid file descriptor.(2) the file
690Sstevel@tonic-gate  *  represented by fd does not understand ISPTM(not a master device).
700Sstevel@tonic-gate  *  If we have a valid master, get its minor number via fstat().
710Sstevel@tonic-gate  *  Concatenate it to PTSNAME and return it as the name of the slave
720Sstevel@tonic-gate  *  device.
730Sstevel@tonic-gate  */
740Sstevel@tonic-gate static dev_t
ptsdev(int fd)750Sstevel@tonic-gate ptsdev(int fd)
760Sstevel@tonic-gate {
770Sstevel@tonic-gate 	struct stat64 status;
780Sstevel@tonic-gate 	struct strioctl istr;
790Sstevel@tonic-gate 
800Sstevel@tonic-gate 	istr.ic_cmd = ISPTM;
810Sstevel@tonic-gate 	istr.ic_len = 0;
820Sstevel@tonic-gate 	istr.ic_timout = 0;
830Sstevel@tonic-gate 	istr.ic_dp = NULL;
840Sstevel@tonic-gate 
850Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &istr) < 0 || fstat64(fd, &status) < 0)
860Sstevel@tonic-gate 		return (NODEV);
870Sstevel@tonic-gate 
880Sstevel@tonic-gate 	return (minor(status.st_rdev));
890Sstevel@tonic-gate }
900Sstevel@tonic-gate 
910Sstevel@tonic-gate char *
ptsname(int fd)920Sstevel@tonic-gate ptsname(int fd)
930Sstevel@tonic-gate {
940Sstevel@tonic-gate 	dev_t dev;
950Sstevel@tonic-gate 	char *sname;
960Sstevel@tonic-gate 
970Sstevel@tonic-gate 	if ((dev = ptsdev(fd)) == NODEV)
980Sstevel@tonic-gate 		return (NULL);
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate 	sname = tsdalloc(_T_PTSNAME, PTLEN, NULL);
1010Sstevel@tonic-gate 	if (sname == NULL)
1020Sstevel@tonic-gate 		return (NULL);
1030Sstevel@tonic-gate 	(void) strcpy(sname, PTSNAME);
1040Sstevel@tonic-gate 	itoa(dev, sname + strlen(PTSNAME));
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate 	/*
1072621Sllai1 	 * This lookup will create the /dev/pts node (if the corresponding
1082621Sllai1 	 * pty exists.
1090Sstevel@tonic-gate 	 */
1102621Sllai1 	if (access(sname, F_OK) ==  0)
1110Sstevel@tonic-gate 		return (sname);
1122621Sllai1 
1130Sstevel@tonic-gate 	return (NULL);
1140Sstevel@tonic-gate }
1150Sstevel@tonic-gate 
1160Sstevel@tonic-gate /*
1170Sstevel@tonic-gate  * Send an ioctl down to the master device requesting the
1180Sstevel@tonic-gate  * master/slave pair be unlocked.
1190Sstevel@tonic-gate  */
1200Sstevel@tonic-gate int
unlockpt(int fd)1210Sstevel@tonic-gate unlockpt(int fd)
1220Sstevel@tonic-gate {
1230Sstevel@tonic-gate 	struct strioctl istr;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate 	istr.ic_cmd = UNLKPT;
1260Sstevel@tonic-gate 	istr.ic_len = 0;
1270Sstevel@tonic-gate 	istr.ic_timout = 0;
1280Sstevel@tonic-gate 	istr.ic_dp = NULL;
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &istr) < 0)
1310Sstevel@tonic-gate 		return (-1);
1320Sstevel@tonic-gate 
1330Sstevel@tonic-gate 	return (0);
1340Sstevel@tonic-gate }
1350Sstevel@tonic-gate 
1362621Sllai1 int
grantpt(int fd)1372621Sllai1 grantpt(int fd)
1380Sstevel@tonic-gate {
1392621Sllai1 	struct strioctl istr;
1402621Sllai1 	pt_own_t pto;
1412621Sllai1 	struct group *gr_name;
1420Sstevel@tonic-gate 
1430Sstevel@tonic-gate 	/* validate the file descriptor before proceeding */
1442621Sllai1 	if (ptsdev(fd) == NODEV)
1450Sstevel@tonic-gate 		return (-1);
1460Sstevel@tonic-gate 
1472621Sllai1 	pto.pto_ruid = getuid();
1482621Sllai1 
1492621Sllai1 	gr_name = getgrnam(DEFAULT_TTY_GROUP);
1502621Sllai1 	if (gr_name)
1512621Sllai1 		pto.pto_rgid = gr_name->gr_gid;
1522621Sllai1 	else
1532621Sllai1 		pto.pto_rgid = getgid();
1540Sstevel@tonic-gate 
1553442Svikram 	istr.ic_cmd = OWNERPT;
1562621Sllai1 	istr.ic_len = sizeof (pt_own_t);
1572621Sllai1 	istr.ic_timout = 0;
1582621Sllai1 	istr.ic_dp = (char *)&pto;
1592621Sllai1 
1602621Sllai1 	if (ioctl(fd, I_STR, &istr) != 0) {
1612621Sllai1 		errno = EACCES;
1620Sstevel@tonic-gate 		return (-1);
1630Sstevel@tonic-gate 	}
1640Sstevel@tonic-gate 
1652621Sllai1 	return (0);
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate /*
1690Sstevel@tonic-gate  * Send an ioctl down to the master device requesting the master/slave pair
1700Sstevel@tonic-gate  * be assigned to the given zone.
1710Sstevel@tonic-gate  */
1720Sstevel@tonic-gate int
zonept(int fd,zoneid_t zoneid)1730Sstevel@tonic-gate zonept(int fd, zoneid_t zoneid)
1740Sstevel@tonic-gate {
1750Sstevel@tonic-gate 	struct strioctl istr;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	istr.ic_cmd = ZONEPT;
1780Sstevel@tonic-gate 	istr.ic_len = sizeof (zoneid);
1790Sstevel@tonic-gate 	istr.ic_timout = 0;
1800Sstevel@tonic-gate 	istr.ic_dp = (char *)&zoneid;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	if (ioctl(fd, I_STR, &istr) != 0) {
1830Sstevel@tonic-gate 		return (-1);
1840Sstevel@tonic-gate 	}
1850Sstevel@tonic-gate 	return (0);
1860Sstevel@tonic-gate }
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 
1890Sstevel@tonic-gate static void
itoa(int i,char * ptr)1900Sstevel@tonic-gate itoa(int i, char *ptr)
1910Sstevel@tonic-gate {
1920Sstevel@tonic-gate 	int dig = 0;
1930Sstevel@tonic-gate 	int tempi;
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	tempi = i;
1960Sstevel@tonic-gate 	do {
1970Sstevel@tonic-gate 		dig++;
1980Sstevel@tonic-gate 		tempi /= 10;
1990Sstevel@tonic-gate 	} while (tempi);
2000Sstevel@tonic-gate 
2010Sstevel@tonic-gate 	ptr += dig;
2020Sstevel@tonic-gate 	*ptr = '\0';
2030Sstevel@tonic-gate 	while (--dig >= 0) {
2040Sstevel@tonic-gate 		*(--ptr) = i % 10 + '0';
2050Sstevel@tonic-gate 		i /= 10;
2060Sstevel@tonic-gate 	}
2070Sstevel@tonic-gate }
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate /*
2110Sstevel@tonic-gate  * added for SUSv3 standard
2120Sstevel@tonic-gate  *
2130Sstevel@tonic-gate  * Open a pseudo-terminal device.  External interface.
2140Sstevel@tonic-gate  */
2150Sstevel@tonic-gate 
2160Sstevel@tonic-gate int
posix_openpt(int oflag)2170Sstevel@tonic-gate posix_openpt(int oflag)
2180Sstevel@tonic-gate {
2190Sstevel@tonic-gate 	return (open("/dev/ptmx", oflag));
2200Sstevel@tonic-gate }
221