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 /* 23*7257d1b4Sraf * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate /* Copyright (c) 1988 AT&T */ 287c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 297c478bd9Sstevel@tonic-gate 30*7257d1b4Sraf #pragma ident "%Z%%M% %I% %E% SMI" 317c478bd9Sstevel@tonic-gate 32*7257d1b4Sraf #pragma weak _ptsname = ptsname 33*7257d1b4Sraf #pragma weak _grantpt = grantpt 34*7257d1b4Sraf #pragma weak _unlockpt = unlockpt 35*7257d1b4Sraf 36*7257d1b4Sraf #include "lint.h" 37494a4c51Sraf #include "libc.h" 38494a4c51Sraf #include "mtlib.h" 397c478bd9Sstevel@tonic-gate #include <sys/types.h> 407c478bd9Sstevel@tonic-gate #include <signal.h> 417c478bd9Sstevel@tonic-gate #include <sys/param.h> 427c478bd9Sstevel@tonic-gate #include <sys/mkdev.h> 437c478bd9Sstevel@tonic-gate #include <sys/stream.h> 447c478bd9Sstevel@tonic-gate #include <sys/stropts.h> 457c478bd9Sstevel@tonic-gate #include <sys/wait.h> 467c478bd9Sstevel@tonic-gate #include <sys/signal.h> 477c478bd9Sstevel@tonic-gate #include <errno.h> 487c478bd9Sstevel@tonic-gate #include <fcntl.h> 497c478bd9Sstevel@tonic-gate #include <sys/stat.h> 507c478bd9Sstevel@tonic-gate #include <sys/ptms.h> 517c478bd9Sstevel@tonic-gate #include <string.h> 527c478bd9Sstevel@tonic-gate #include <stdlib.h> 537c478bd9Sstevel@tonic-gate #include <unistd.h> 547c478bd9Sstevel@tonic-gate #include <wait.h> 557c478bd9Sstevel@tonic-gate #include <spawn.h> 56facf4a8dSllai1 #include <grp.h> 577c478bd9Sstevel@tonic-gate #include "tsd.h" 587c478bd9Sstevel@tonic-gate 597c478bd9Sstevel@tonic-gate #define PTSNAME "/dev/pts/" /* slave name */ 607c478bd9Sstevel@tonic-gate #define PTLEN 32 /* slave name length */ 61facf4a8dSllai1 #define DEFAULT_TTY_GROUP "tty" /* slave device group owner */ 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate static void itoa(int, char *); 647c478bd9Sstevel@tonic-gate 657c478bd9Sstevel@tonic-gate /* 667c478bd9Sstevel@tonic-gate * Check that fd argument is a file descriptor of an opened master. 677c478bd9Sstevel@tonic-gate * Do this by sending an ISPTM ioctl message down stream. Ioctl() 687c478bd9Sstevel@tonic-gate * will fail if:(1) fd is not a valid file descriptor.(2) the file 697c478bd9Sstevel@tonic-gate * represented by fd does not understand ISPTM(not a master device). 707c478bd9Sstevel@tonic-gate * If we have a valid master, get its minor number via fstat(). 717c478bd9Sstevel@tonic-gate * Concatenate it to PTSNAME and return it as the name of the slave 727c478bd9Sstevel@tonic-gate * device. 737c478bd9Sstevel@tonic-gate */ 747c478bd9Sstevel@tonic-gate static dev_t 757c478bd9Sstevel@tonic-gate ptsdev(int fd) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate struct stat64 status; 787c478bd9Sstevel@tonic-gate struct strioctl istr; 797c478bd9Sstevel@tonic-gate 807c478bd9Sstevel@tonic-gate istr.ic_cmd = ISPTM; 817c478bd9Sstevel@tonic-gate istr.ic_len = 0; 827c478bd9Sstevel@tonic-gate istr.ic_timout = 0; 837c478bd9Sstevel@tonic-gate istr.ic_dp = NULL; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) < 0 || fstat64(fd, &status) < 0) 867c478bd9Sstevel@tonic-gate return (NODEV); 877c478bd9Sstevel@tonic-gate 887c478bd9Sstevel@tonic-gate return (minor(status.st_rdev)); 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate char * 927c478bd9Sstevel@tonic-gate ptsname(int fd) 937c478bd9Sstevel@tonic-gate { 947c478bd9Sstevel@tonic-gate dev_t dev; 957c478bd9Sstevel@tonic-gate char *sname; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate if ((dev = ptsdev(fd)) == NODEV) 987c478bd9Sstevel@tonic-gate return (NULL); 997c478bd9Sstevel@tonic-gate 1007c478bd9Sstevel@tonic-gate sname = tsdalloc(_T_PTSNAME, PTLEN, NULL); 1017c478bd9Sstevel@tonic-gate if (sname == NULL) 1027c478bd9Sstevel@tonic-gate return (NULL); 1037c478bd9Sstevel@tonic-gate (void) strcpy(sname, PTSNAME); 1047c478bd9Sstevel@tonic-gate itoa(dev, sname + strlen(PTSNAME)); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate /* 107facf4a8dSllai1 * This lookup will create the /dev/pts node (if the corresponding 108facf4a8dSllai1 * pty exists. 1097c478bd9Sstevel@tonic-gate */ 110facf4a8dSllai1 if (access(sname, F_OK) == 0) 1117c478bd9Sstevel@tonic-gate return (sname); 112facf4a8dSllai1 1137c478bd9Sstevel@tonic-gate return (NULL); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* 1177c478bd9Sstevel@tonic-gate * Send an ioctl down to the master device requesting the 1187c478bd9Sstevel@tonic-gate * master/slave pair be unlocked. 1197c478bd9Sstevel@tonic-gate */ 1207c478bd9Sstevel@tonic-gate int 1217c478bd9Sstevel@tonic-gate unlockpt(int fd) 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate struct strioctl istr; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate istr.ic_cmd = UNLKPT; 1267c478bd9Sstevel@tonic-gate istr.ic_len = 0; 1277c478bd9Sstevel@tonic-gate istr.ic_timout = 0; 1287c478bd9Sstevel@tonic-gate istr.ic_dp = NULL; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) < 0) 1317c478bd9Sstevel@tonic-gate return (-1); 1327c478bd9Sstevel@tonic-gate 1337c478bd9Sstevel@tonic-gate return (0); 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate 136facf4a8dSllai1 int 137facf4a8dSllai1 grantpt(int fd) 1387c478bd9Sstevel@tonic-gate { 139facf4a8dSllai1 struct strioctl istr; 140facf4a8dSllai1 pt_own_t pto; 141facf4a8dSllai1 struct group *gr_name; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate /* validate the file descriptor before proceeding */ 144facf4a8dSllai1 if (ptsdev(fd) == NODEV) 1457c478bd9Sstevel@tonic-gate return (-1); 1467c478bd9Sstevel@tonic-gate 147facf4a8dSllai1 pto.pto_ruid = getuid(); 1487c478bd9Sstevel@tonic-gate 149facf4a8dSllai1 gr_name = getgrnam(DEFAULT_TTY_GROUP); 150facf4a8dSllai1 if (gr_name) 151facf4a8dSllai1 pto.pto_rgid = gr_name->gr_gid; 152facf4a8dSllai1 else 153facf4a8dSllai1 pto.pto_rgid = getgid(); 1547c478bd9Sstevel@tonic-gate 15549e92448Svikram istr.ic_cmd = OWNERPT; 156facf4a8dSllai1 istr.ic_len = sizeof (pt_own_t); 157facf4a8dSllai1 istr.ic_timout = 0; 158facf4a8dSllai1 istr.ic_dp = (char *)&pto; 1597c478bd9Sstevel@tonic-gate 160facf4a8dSllai1 if (ioctl(fd, I_STR, &istr) != 0) { 1617c478bd9Sstevel@tonic-gate errno = EACCES; 1627c478bd9Sstevel@tonic-gate return (-1); 1637c478bd9Sstevel@tonic-gate } 1647c478bd9Sstevel@tonic-gate 165facf4a8dSllai1 return (0); 1667c478bd9Sstevel@tonic-gate } 1677c478bd9Sstevel@tonic-gate 1687c478bd9Sstevel@tonic-gate /* 1697c478bd9Sstevel@tonic-gate * Send an ioctl down to the master device requesting the master/slave pair 1707c478bd9Sstevel@tonic-gate * be assigned to the given zone. 1717c478bd9Sstevel@tonic-gate */ 1727c478bd9Sstevel@tonic-gate int 1737c478bd9Sstevel@tonic-gate zonept(int fd, zoneid_t zoneid) 1747c478bd9Sstevel@tonic-gate { 1757c478bd9Sstevel@tonic-gate struct strioctl istr; 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate istr.ic_cmd = ZONEPT; 1787c478bd9Sstevel@tonic-gate istr.ic_len = sizeof (zoneid); 1797c478bd9Sstevel@tonic-gate istr.ic_timout = 0; 1807c478bd9Sstevel@tonic-gate istr.ic_dp = (char *)&zoneid; 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate if (ioctl(fd, I_STR, &istr) != 0) { 1837c478bd9Sstevel@tonic-gate return (-1); 1847c478bd9Sstevel@tonic-gate } 1857c478bd9Sstevel@tonic-gate return (0); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate 1897c478bd9Sstevel@tonic-gate static void 1907c478bd9Sstevel@tonic-gate itoa(int i, char *ptr) 1917c478bd9Sstevel@tonic-gate { 1927c478bd9Sstevel@tonic-gate int dig = 0; 1937c478bd9Sstevel@tonic-gate int tempi; 1947c478bd9Sstevel@tonic-gate 1957c478bd9Sstevel@tonic-gate tempi = i; 1967c478bd9Sstevel@tonic-gate do { 1977c478bd9Sstevel@tonic-gate dig++; 1987c478bd9Sstevel@tonic-gate tempi /= 10; 1997c478bd9Sstevel@tonic-gate } while (tempi); 2007c478bd9Sstevel@tonic-gate 2017c478bd9Sstevel@tonic-gate ptr += dig; 2027c478bd9Sstevel@tonic-gate *ptr = '\0'; 2037c478bd9Sstevel@tonic-gate while (--dig >= 0) { 2047c478bd9Sstevel@tonic-gate *(--ptr) = i % 10 + '0'; 2057c478bd9Sstevel@tonic-gate i /= 10; 2067c478bd9Sstevel@tonic-gate } 2077c478bd9Sstevel@tonic-gate } 2087c478bd9Sstevel@tonic-gate 2097c478bd9Sstevel@tonic-gate 2107c478bd9Sstevel@tonic-gate /* 2117c478bd9Sstevel@tonic-gate * added for SUSv3 standard 2127c478bd9Sstevel@tonic-gate * 2137c478bd9Sstevel@tonic-gate * Open a pseudo-terminal device. External interface. 2147c478bd9Sstevel@tonic-gate */ 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate int 2177c478bd9Sstevel@tonic-gate posix_openpt(int oflag) 2187c478bd9Sstevel@tonic-gate { 2197c478bd9Sstevel@tonic-gate return (open("/dev/ptmx", oflag)); 2207c478bd9Sstevel@tonic-gate } 221