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 */ 21*3453Sraf 220Sstevel@tonic-gate /* 233442Svikram * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 240Sstevel@tonic-gate * Use is subject to license terms. 250Sstevel@tonic-gate */ 260Sstevel@tonic-gate 270Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 280Sstevel@tonic-gate 290Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 300Sstevel@tonic-gate /* All Rights Reserved */ 310Sstevel@tonic-gate 320Sstevel@tonic-gate #pragma weak ptsname = _ptsname 330Sstevel@tonic-gate #pragma weak grantpt = _grantpt 340Sstevel@tonic-gate #pragma weak unlockpt = _unlockpt 350Sstevel@tonic-gate #pragma weak posix_openpt = _posix_openpt 360Sstevel@tonic-gate 370Sstevel@tonic-gate #include "synonyms.h" 38*3453Sraf #include "libc.h" 39*3453Sraf #include "mtlib.h" 400Sstevel@tonic-gate #include <sys/types.h> 410Sstevel@tonic-gate #include <signal.h> 420Sstevel@tonic-gate #include <sys/param.h> 430Sstevel@tonic-gate #include <sys/mkdev.h> 440Sstevel@tonic-gate #include <sys/stream.h> 450Sstevel@tonic-gate #include <sys/stropts.h> 460Sstevel@tonic-gate #include <sys/wait.h> 470Sstevel@tonic-gate #include <sys/signal.h> 480Sstevel@tonic-gate #include <errno.h> 490Sstevel@tonic-gate #include <fcntl.h> 500Sstevel@tonic-gate #include <sys/stat.h> 510Sstevel@tonic-gate #include <sys/ptms.h> 520Sstevel@tonic-gate #include <string.h> 530Sstevel@tonic-gate #include <stdlib.h> 540Sstevel@tonic-gate #include <unistd.h> 550Sstevel@tonic-gate #include <wait.h> 560Sstevel@tonic-gate #include <spawn.h> 572621Sllai1 #include <grp.h> 580Sstevel@tonic-gate #include "tsd.h" 590Sstevel@tonic-gate 600Sstevel@tonic-gate #define PTSNAME "/dev/pts/" /* slave name */ 610Sstevel@tonic-gate #define PTLEN 32 /* slave name length */ 622621Sllai1 #define DEFAULT_TTY_GROUP "tty" /* slave device group owner */ 630Sstevel@tonic-gate 640Sstevel@tonic-gate static void itoa(int, char *); 650Sstevel@tonic-gate 660Sstevel@tonic-gate /* 670Sstevel@tonic-gate * Check that fd argument is a file descriptor of an opened master. 680Sstevel@tonic-gate * Do this by sending an ISPTM ioctl message down stream. Ioctl() 690Sstevel@tonic-gate * will fail if:(1) fd is not a valid file descriptor.(2) the file 700Sstevel@tonic-gate * represented by fd does not understand ISPTM(not a master device). 710Sstevel@tonic-gate * If we have a valid master, get its minor number via fstat(). 720Sstevel@tonic-gate * Concatenate it to PTSNAME and return it as the name of the slave 730Sstevel@tonic-gate * device. 740Sstevel@tonic-gate */ 750Sstevel@tonic-gate static dev_t 760Sstevel@tonic-gate ptsdev(int fd) 770Sstevel@tonic-gate { 780Sstevel@tonic-gate struct stat64 status; 790Sstevel@tonic-gate struct strioctl istr; 800Sstevel@tonic-gate 810Sstevel@tonic-gate istr.ic_cmd = ISPTM; 820Sstevel@tonic-gate istr.ic_len = 0; 830Sstevel@tonic-gate istr.ic_timout = 0; 840Sstevel@tonic-gate istr.ic_dp = NULL; 850Sstevel@tonic-gate 860Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) < 0 || fstat64(fd, &status) < 0) 870Sstevel@tonic-gate return (NODEV); 880Sstevel@tonic-gate 890Sstevel@tonic-gate return (minor(status.st_rdev)); 900Sstevel@tonic-gate } 910Sstevel@tonic-gate 920Sstevel@tonic-gate char * 930Sstevel@tonic-gate ptsname(int fd) 940Sstevel@tonic-gate { 950Sstevel@tonic-gate dev_t dev; 960Sstevel@tonic-gate char *sname; 970Sstevel@tonic-gate 980Sstevel@tonic-gate if ((dev = ptsdev(fd)) == NODEV) 990Sstevel@tonic-gate return (NULL); 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate sname = tsdalloc(_T_PTSNAME, PTLEN, NULL); 1020Sstevel@tonic-gate if (sname == NULL) 1030Sstevel@tonic-gate return (NULL); 1040Sstevel@tonic-gate (void) strcpy(sname, PTSNAME); 1050Sstevel@tonic-gate itoa(dev, sname + strlen(PTSNAME)); 1060Sstevel@tonic-gate 1070Sstevel@tonic-gate /* 1082621Sllai1 * This lookup will create the /dev/pts node (if the corresponding 1092621Sllai1 * pty exists. 1100Sstevel@tonic-gate */ 1112621Sllai1 if (access(sname, F_OK) == 0) 1120Sstevel@tonic-gate return (sname); 1132621Sllai1 1140Sstevel@tonic-gate return (NULL); 1150Sstevel@tonic-gate } 1160Sstevel@tonic-gate 1170Sstevel@tonic-gate /* 1180Sstevel@tonic-gate * Send an ioctl down to the master device requesting the 1190Sstevel@tonic-gate * master/slave pair be unlocked. 1200Sstevel@tonic-gate */ 1210Sstevel@tonic-gate int 1220Sstevel@tonic-gate unlockpt(int fd) 1230Sstevel@tonic-gate { 1240Sstevel@tonic-gate struct strioctl istr; 1250Sstevel@tonic-gate 1260Sstevel@tonic-gate istr.ic_cmd = UNLKPT; 1270Sstevel@tonic-gate istr.ic_len = 0; 1280Sstevel@tonic-gate istr.ic_timout = 0; 1290Sstevel@tonic-gate istr.ic_dp = NULL; 1300Sstevel@tonic-gate 1310Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) < 0) 1320Sstevel@tonic-gate return (-1); 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate return (0); 1350Sstevel@tonic-gate } 1360Sstevel@tonic-gate 1372621Sllai1 int 1382621Sllai1 grantpt(int fd) 1390Sstevel@tonic-gate { 1402621Sllai1 struct strioctl istr; 1412621Sllai1 pt_own_t pto; 1422621Sllai1 struct group *gr_name; 1430Sstevel@tonic-gate 1440Sstevel@tonic-gate /* validate the file descriptor before proceeding */ 1452621Sllai1 if (ptsdev(fd) == NODEV) 1460Sstevel@tonic-gate return (-1); 1470Sstevel@tonic-gate 1482621Sllai1 pto.pto_ruid = getuid(); 1492621Sllai1 1502621Sllai1 gr_name = getgrnam(DEFAULT_TTY_GROUP); 1512621Sllai1 if (gr_name) 1522621Sllai1 pto.pto_rgid = gr_name->gr_gid; 1532621Sllai1 else 1542621Sllai1 pto.pto_rgid = getgid(); 1550Sstevel@tonic-gate 1563442Svikram istr.ic_cmd = OWNERPT; 1572621Sllai1 istr.ic_len = sizeof (pt_own_t); 1582621Sllai1 istr.ic_timout = 0; 1592621Sllai1 istr.ic_dp = (char *)&pto; 1602621Sllai1 1612621Sllai1 if (ioctl(fd, I_STR, &istr) != 0) { 1622621Sllai1 errno = EACCES; 1630Sstevel@tonic-gate return (-1); 1640Sstevel@tonic-gate } 1650Sstevel@tonic-gate 1662621Sllai1 return (0); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1690Sstevel@tonic-gate /* 1700Sstevel@tonic-gate * Send an ioctl down to the master device requesting the master/slave pair 1710Sstevel@tonic-gate * be assigned to the given zone. 1720Sstevel@tonic-gate */ 1730Sstevel@tonic-gate int 1740Sstevel@tonic-gate zonept(int fd, zoneid_t zoneid) 1750Sstevel@tonic-gate { 1760Sstevel@tonic-gate struct strioctl istr; 1770Sstevel@tonic-gate 1780Sstevel@tonic-gate istr.ic_cmd = ZONEPT; 1790Sstevel@tonic-gate istr.ic_len = sizeof (zoneid); 1800Sstevel@tonic-gate istr.ic_timout = 0; 1810Sstevel@tonic-gate istr.ic_dp = (char *)&zoneid; 1820Sstevel@tonic-gate 1830Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) != 0) { 1840Sstevel@tonic-gate return (-1); 1850Sstevel@tonic-gate } 1860Sstevel@tonic-gate return (0); 1870Sstevel@tonic-gate } 1880Sstevel@tonic-gate 1890Sstevel@tonic-gate 1900Sstevel@tonic-gate static void 1910Sstevel@tonic-gate itoa(int i, char *ptr) 1920Sstevel@tonic-gate { 1930Sstevel@tonic-gate int dig = 0; 1940Sstevel@tonic-gate int tempi; 1950Sstevel@tonic-gate 1960Sstevel@tonic-gate tempi = i; 1970Sstevel@tonic-gate do { 1980Sstevel@tonic-gate dig++; 1990Sstevel@tonic-gate tempi /= 10; 2000Sstevel@tonic-gate } while (tempi); 2010Sstevel@tonic-gate 2020Sstevel@tonic-gate ptr += dig; 2030Sstevel@tonic-gate *ptr = '\0'; 2040Sstevel@tonic-gate while (--dig >= 0) { 2050Sstevel@tonic-gate *(--ptr) = i % 10 + '0'; 2060Sstevel@tonic-gate i /= 10; 2070Sstevel@tonic-gate } 2080Sstevel@tonic-gate } 2090Sstevel@tonic-gate 2100Sstevel@tonic-gate 2110Sstevel@tonic-gate /* 2120Sstevel@tonic-gate * added for SUSv3 standard 2130Sstevel@tonic-gate * 2140Sstevel@tonic-gate * Open a pseudo-terminal device. External interface. 2150Sstevel@tonic-gate */ 2160Sstevel@tonic-gate 2170Sstevel@tonic-gate int 2180Sstevel@tonic-gate posix_openpt(int oflag) 2190Sstevel@tonic-gate { 2200Sstevel@tonic-gate return (open("/dev/ptmx", oflag)); 2210Sstevel@tonic-gate } 222