1bc093719SEd Schouten /*- 2d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3d915a14eSPedro F. Giffuni * 4bc093719SEd Schouten * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org> 5bc093719SEd Schouten * All rights reserved. 6bc093719SEd Schouten * 7bc093719SEd Schouten * Portions of this software were developed under sponsorship from Snow 8bc093719SEd Schouten * B.V., the Netherlands. 9bc093719SEd Schouten * 10bc093719SEd Schouten * Redistribution and use in source and binary forms, with or without 11bc093719SEd Schouten * modification, are permitted provided that the following conditions 12bc093719SEd Schouten * are met: 13bc093719SEd Schouten * 1. Redistributions of source code must retain the above copyright 14bc093719SEd Schouten * notice, this list of conditions and the following disclaimer. 15bc093719SEd Schouten * 2. Redistributions in binary form must reproduce the above copyright 16bc093719SEd Schouten * notice, this list of conditions and the following disclaimer in the 17bc093719SEd Schouten * documentation and/or other materials provided with the distribution. 18bc093719SEd Schouten * 19bc093719SEd Schouten * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 20bc093719SEd Schouten * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21bc093719SEd Schouten * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22bc093719SEd Schouten * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 23bc093719SEd Schouten * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24bc093719SEd Schouten * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25bc093719SEd Schouten * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26bc093719SEd Schouten * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27bc093719SEd Schouten * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28bc093719SEd Schouten * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29bc093719SEd Schouten * SUCH DAMAGE. 30bc093719SEd Schouten */ 31bc093719SEd Schouten 32bc093719SEd Schouten #include <sys/cdefs.h> 33bc093719SEd Schouten #ifndef lint 34bc093719SEd Schouten __FBSDID("$FreeBSD$"); 35bc093719SEd Schouten #endif /* not lint */ 36bc093719SEd Schouten 37bc093719SEd Schouten #include "namespace.h" 38bc093719SEd Schouten #include <sys/param.h> 39bc093719SEd Schouten #include <sys/ioctl.h> 40bc093719SEd Schouten 41bc093719SEd Schouten #include <errno.h> 42bc093719SEd Schouten #include <paths.h> 4326d4f5e9SEd Schouten #include <stdlib.h> 443e7224dfSXin LI #include <string.h> 45bc093719SEd Schouten #include "un-namespace.h" 46bc093719SEd Schouten 47bc093719SEd Schouten /* 48bc093719SEd Schouten * __isptmaster(): return whether the file descriptor refers to a 49bc093719SEd Schouten * pseudo-terminal master device. 50bc093719SEd Schouten */ 51bc093719SEd Schouten static int 52bc093719SEd Schouten __isptmaster(int fildes) 53bc093719SEd Schouten { 54bc093719SEd Schouten 55bc093719SEd Schouten if (_ioctl(fildes, TIOCPTMASTER) == 0) 56bc093719SEd Schouten return (0); 57bc093719SEd Schouten 58bc093719SEd Schouten if (errno != EBADF) 59bc093719SEd Schouten errno = EINVAL; 60bc093719SEd Schouten 61bc093719SEd Schouten return (-1); 62bc093719SEd Schouten } 63bc093719SEd Schouten 64bc093719SEd Schouten /* 65bc093719SEd Schouten * In our implementation, grantpt() and unlockpt() don't actually have 66bc093719SEd Schouten * any use, because PTY's are created on the fly and already have proper 67bc093719SEd Schouten * permissions upon creation. 68bc093719SEd Schouten * 69bc093719SEd Schouten * Just make sure `fildes' actually points to a real PTY master device. 70bc093719SEd Schouten */ 71bc093719SEd Schouten __strong_reference(__isptmaster, grantpt); 72bc093719SEd Schouten __strong_reference(__isptmaster, unlockpt); 73bc093719SEd Schouten 74bc093719SEd Schouten /* 753e7224dfSXin LI * ptsname_r(): return the pathname of the slave pseudo-terminal device 763e7224dfSXin LI * associated with the specified master. 773e7224dfSXin LI */ 783e7224dfSXin LI int 79*5011fb43SXin LI __ptsname_r(int fildes, char *buffer, size_t buflen) 803e7224dfSXin LI { 813e7224dfSXin LI 823e7224dfSXin LI if (buflen <= sizeof(_PATH_DEV)) { 833e7224dfSXin LI errno = ERANGE; 843e7224dfSXin LI return (-1); 853e7224dfSXin LI } 863e7224dfSXin LI 873e7224dfSXin LI /* Make sure fildes points to a master device. */ 883e7224dfSXin LI if (__isptmaster(fildes) != 0) 893e7224dfSXin LI return (-1); 903e7224dfSXin LI 913e7224dfSXin LI memcpy(buffer, _PATH_DEV, sizeof(_PATH_DEV)); 923e7224dfSXin LI buffer += sizeof(_PATH_DEV) - 1; 933e7224dfSXin LI buflen -= sizeof(_PATH_DEV) - 1; 943e7224dfSXin LI 953e7224dfSXin LI if (fdevname_r(fildes, buffer, buflen) == NULL) { 963e7224dfSXin LI if (errno == EINVAL) 973e7224dfSXin LI errno = ERANGE; 983e7224dfSXin LI return (-1); 993e7224dfSXin LI } 1003e7224dfSXin LI 1013e7224dfSXin LI return (0); 1023e7224dfSXin LI } 1033e7224dfSXin LI 104*5011fb43SXin LI __strong_reference(__ptsname_r, ptsname_r); 105*5011fb43SXin LI 1063e7224dfSXin LI /* 107bc093719SEd Schouten * ptsname(): return the pathname of the slave pseudo-terminal device 108bc093719SEd Schouten * associated with the specified master. 109bc093719SEd Schouten */ 110bc093719SEd Schouten char * 111bc093719SEd Schouten ptsname(int fildes) 112bc093719SEd Schouten { 113*5011fb43SXin LI static char pt_slave[sizeof(_PATH_DEV) + SPECNAMELEN]; 114bc093719SEd Schouten 115*5011fb43SXin LI if (__ptsname_r(fildes, pt_slave, sizeof(pt_slave)) == 0) 1163e7224dfSXin LI return (pt_slave); 117*5011fb43SXin LI 1183e7224dfSXin LI return (NULL); 119bc093719SEd Schouten } 120