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 */ 210Sstevel@tonic-gate /* 22*3442Svikram * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 230Sstevel@tonic-gate * Use is subject to license terms. 240Sstevel@tonic-gate */ 250Sstevel@tonic-gate 260Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 270Sstevel@tonic-gate 280Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 290Sstevel@tonic-gate /* All Rights Reserved */ 300Sstevel@tonic-gate 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" 380Sstevel@tonic-gate #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/fs/ufs_fsdir.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 <synch.h> 570Sstevel@tonic-gate #include <thread.h> 580Sstevel@tonic-gate #include <spawn.h> 590Sstevel@tonic-gate #include <libc.h> 602621Sllai1 #include <grp.h> 610Sstevel@tonic-gate #include "tsd.h" 620Sstevel@tonic-gate 630Sstevel@tonic-gate #define PTSNAME "/dev/pts/" /* slave name */ 640Sstevel@tonic-gate #define PTLEN 32 /* slave name length */ 652621Sllai1 #define DEFAULT_TTY_GROUP "tty" /* slave device group owner */ 660Sstevel@tonic-gate 670Sstevel@tonic-gate static void itoa(int, char *); 680Sstevel@tonic-gate 690Sstevel@tonic-gate /* 700Sstevel@tonic-gate * Check that fd argument is a file descriptor of an opened master. 710Sstevel@tonic-gate * Do this by sending an ISPTM ioctl message down stream. Ioctl() 720Sstevel@tonic-gate * will fail if:(1) fd is not a valid file descriptor.(2) the file 730Sstevel@tonic-gate * represented by fd does not understand ISPTM(not a master device). 740Sstevel@tonic-gate * If we have a valid master, get its minor number via fstat(). 750Sstevel@tonic-gate * Concatenate it to PTSNAME and return it as the name of the slave 760Sstevel@tonic-gate * device. 770Sstevel@tonic-gate */ 780Sstevel@tonic-gate static dev_t 790Sstevel@tonic-gate ptsdev(int fd) 800Sstevel@tonic-gate { 810Sstevel@tonic-gate struct stat64 status; 820Sstevel@tonic-gate struct strioctl istr; 830Sstevel@tonic-gate 840Sstevel@tonic-gate istr.ic_cmd = ISPTM; 850Sstevel@tonic-gate istr.ic_len = 0; 860Sstevel@tonic-gate istr.ic_timout = 0; 870Sstevel@tonic-gate istr.ic_dp = NULL; 880Sstevel@tonic-gate 890Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) < 0 || fstat64(fd, &status) < 0) 900Sstevel@tonic-gate return (NODEV); 910Sstevel@tonic-gate 920Sstevel@tonic-gate return (minor(status.st_rdev)); 930Sstevel@tonic-gate } 940Sstevel@tonic-gate 950Sstevel@tonic-gate char * 960Sstevel@tonic-gate ptsname(int fd) 970Sstevel@tonic-gate { 980Sstevel@tonic-gate dev_t dev; 990Sstevel@tonic-gate char *sname; 1000Sstevel@tonic-gate 1010Sstevel@tonic-gate if ((dev = ptsdev(fd)) == NODEV) 1020Sstevel@tonic-gate return (NULL); 1030Sstevel@tonic-gate 1040Sstevel@tonic-gate sname = tsdalloc(_T_PTSNAME, PTLEN, NULL); 1050Sstevel@tonic-gate if (sname == NULL) 1060Sstevel@tonic-gate return (NULL); 1070Sstevel@tonic-gate (void) strcpy(sname, PTSNAME); 1080Sstevel@tonic-gate itoa(dev, sname + strlen(PTSNAME)); 1090Sstevel@tonic-gate 1100Sstevel@tonic-gate /* 1112621Sllai1 * This lookup will create the /dev/pts node (if the corresponding 1122621Sllai1 * pty exists. 1130Sstevel@tonic-gate */ 1142621Sllai1 if (access(sname, F_OK) == 0) 1150Sstevel@tonic-gate return (sname); 1162621Sllai1 1170Sstevel@tonic-gate return (NULL); 1180Sstevel@tonic-gate } 1190Sstevel@tonic-gate 1200Sstevel@tonic-gate /* 1210Sstevel@tonic-gate * Send an ioctl down to the master device requesting the 1220Sstevel@tonic-gate * master/slave pair be unlocked. 1230Sstevel@tonic-gate */ 1240Sstevel@tonic-gate int 1250Sstevel@tonic-gate unlockpt(int fd) 1260Sstevel@tonic-gate { 1270Sstevel@tonic-gate struct strioctl istr; 1280Sstevel@tonic-gate 1290Sstevel@tonic-gate istr.ic_cmd = UNLKPT; 1300Sstevel@tonic-gate istr.ic_len = 0; 1310Sstevel@tonic-gate istr.ic_timout = 0; 1320Sstevel@tonic-gate istr.ic_dp = NULL; 1330Sstevel@tonic-gate 1340Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) < 0) 1350Sstevel@tonic-gate return (-1); 1360Sstevel@tonic-gate 1370Sstevel@tonic-gate return (0); 1380Sstevel@tonic-gate } 1390Sstevel@tonic-gate 1402621Sllai1 int 1412621Sllai1 grantpt(int fd) 1420Sstevel@tonic-gate { 1432621Sllai1 struct strioctl istr; 1442621Sllai1 pt_own_t pto; 1452621Sllai1 struct group *gr_name; 1460Sstevel@tonic-gate 1470Sstevel@tonic-gate /* validate the file descriptor before proceeding */ 1482621Sllai1 if (ptsdev(fd) == NODEV) 1490Sstevel@tonic-gate return (-1); 1500Sstevel@tonic-gate 1512621Sllai1 pto.pto_ruid = getuid(); 1522621Sllai1 1532621Sllai1 gr_name = getgrnam(DEFAULT_TTY_GROUP); 1542621Sllai1 if (gr_name) 1552621Sllai1 pto.pto_rgid = gr_name->gr_gid; 1562621Sllai1 else 1572621Sllai1 pto.pto_rgid = getgid(); 1580Sstevel@tonic-gate 159*3442Svikram istr.ic_cmd = OWNERPT; 1602621Sllai1 istr.ic_len = sizeof (pt_own_t); 1612621Sllai1 istr.ic_timout = 0; 1622621Sllai1 istr.ic_dp = (char *)&pto; 1632621Sllai1 1642621Sllai1 if (ioctl(fd, I_STR, &istr) != 0) { 1652621Sllai1 errno = EACCES; 1660Sstevel@tonic-gate return (-1); 1670Sstevel@tonic-gate } 1680Sstevel@tonic-gate 1692621Sllai1 return (0); 1700Sstevel@tonic-gate } 1710Sstevel@tonic-gate 1720Sstevel@tonic-gate /* 1730Sstevel@tonic-gate * Send an ioctl down to the master device requesting the master/slave pair 1740Sstevel@tonic-gate * be assigned to the given zone. 1750Sstevel@tonic-gate */ 1760Sstevel@tonic-gate int 1770Sstevel@tonic-gate zonept(int fd, zoneid_t zoneid) 1780Sstevel@tonic-gate { 1790Sstevel@tonic-gate struct strioctl istr; 1800Sstevel@tonic-gate 1810Sstevel@tonic-gate istr.ic_cmd = ZONEPT; 1820Sstevel@tonic-gate istr.ic_len = sizeof (zoneid); 1830Sstevel@tonic-gate istr.ic_timout = 0; 1840Sstevel@tonic-gate istr.ic_dp = (char *)&zoneid; 1850Sstevel@tonic-gate 1860Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) != 0) { 1870Sstevel@tonic-gate return (-1); 1880Sstevel@tonic-gate } 1890Sstevel@tonic-gate return (0); 1900Sstevel@tonic-gate } 1910Sstevel@tonic-gate 1920Sstevel@tonic-gate 1930Sstevel@tonic-gate static void 1940Sstevel@tonic-gate itoa(int i, char *ptr) 1950Sstevel@tonic-gate { 1960Sstevel@tonic-gate int dig = 0; 1970Sstevel@tonic-gate int tempi; 1980Sstevel@tonic-gate 1990Sstevel@tonic-gate tempi = i; 2000Sstevel@tonic-gate do { 2010Sstevel@tonic-gate dig++; 2020Sstevel@tonic-gate tempi /= 10; 2030Sstevel@tonic-gate } while (tempi); 2040Sstevel@tonic-gate 2050Sstevel@tonic-gate ptr += dig; 2060Sstevel@tonic-gate *ptr = '\0'; 2070Sstevel@tonic-gate while (--dig >= 0) { 2080Sstevel@tonic-gate *(--ptr) = i % 10 + '0'; 2090Sstevel@tonic-gate i /= 10; 2100Sstevel@tonic-gate } 2110Sstevel@tonic-gate } 2120Sstevel@tonic-gate 2130Sstevel@tonic-gate 2140Sstevel@tonic-gate /* 2150Sstevel@tonic-gate * added for SUSv3 standard 2160Sstevel@tonic-gate * 2170Sstevel@tonic-gate * Open a pseudo-terminal device. External interface. 2180Sstevel@tonic-gate */ 2190Sstevel@tonic-gate 2200Sstevel@tonic-gate int 2210Sstevel@tonic-gate posix_openpt(int oflag) 2220Sstevel@tonic-gate { 2230Sstevel@tonic-gate return (open("/dev/ptmx", oflag)); 2240Sstevel@tonic-gate } 225