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> 44*3e7224dfSXin 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 /* 75*3e7224dfSXin LI * ptsname_r(): return the pathname of the slave pseudo-terminal device 76*3e7224dfSXin LI * associated with the specified master. 77*3e7224dfSXin LI */ 78*3e7224dfSXin LI int 79*3e7224dfSXin LI ptsname_r(int fildes, char *buffer, size_t buflen) 80*3e7224dfSXin LI { 81*3e7224dfSXin LI 82*3e7224dfSXin LI if (buflen <= sizeof(_PATH_DEV)) { 83*3e7224dfSXin LI errno = ERANGE; 84*3e7224dfSXin LI return (-1); 85*3e7224dfSXin LI } 86*3e7224dfSXin LI 87*3e7224dfSXin LI /* Make sure fildes points to a master device. */ 88*3e7224dfSXin LI if (__isptmaster(fildes) != 0) 89*3e7224dfSXin LI return (-1); 90*3e7224dfSXin LI 91*3e7224dfSXin LI memcpy(buffer, _PATH_DEV, sizeof(_PATH_DEV)); 92*3e7224dfSXin LI buffer += sizeof(_PATH_DEV) - 1; 93*3e7224dfSXin LI buflen -= sizeof(_PATH_DEV) - 1; 94*3e7224dfSXin LI 95*3e7224dfSXin LI if (fdevname_r(fildes, buffer, buflen) == NULL) { 96*3e7224dfSXin LI if (errno == EINVAL) 97*3e7224dfSXin LI errno = ERANGE; 98*3e7224dfSXin LI return (-1); 99*3e7224dfSXin LI } 100*3e7224dfSXin LI 101*3e7224dfSXin LI return (0); 102*3e7224dfSXin LI } 103*3e7224dfSXin LI 104*3e7224dfSXin LI /* 105bc093719SEd Schouten * ptsname(): return the pathname of the slave pseudo-terminal device 106bc093719SEd Schouten * associated with the specified master. 107bc093719SEd Schouten */ 108bc093719SEd Schouten char * 109bc093719SEd Schouten ptsname(int fildes) 110bc093719SEd Schouten { 111*3e7224dfSXin LI static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN]; 112bc093719SEd Schouten 113*3e7224dfSXin LI if (ptsname_r(fildes, pt_slave, sizeof(pt_slave)) == 0) 114*3e7224dfSXin LI return (pt_slave); 115*3e7224dfSXin LI else 116*3e7224dfSXin LI return (NULL); 117bc093719SEd Schouten } 118