1bc093719SEd Schouten /*- 2*d915a14eSPedro F. Giffuni * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3*d915a14eSPedro 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> 44bc093719SEd Schouten #include "un-namespace.h" 45bc093719SEd Schouten 46bc093719SEd Schouten /* 47bc093719SEd Schouten * __isptmaster(): return whether the file descriptor refers to a 48bc093719SEd Schouten * pseudo-terminal master device. 49bc093719SEd Schouten */ 50bc093719SEd Schouten static int 51bc093719SEd Schouten __isptmaster(int fildes) 52bc093719SEd Schouten { 53bc093719SEd Schouten 54bc093719SEd Schouten if (_ioctl(fildes, TIOCPTMASTER) == 0) 55bc093719SEd Schouten return (0); 56bc093719SEd Schouten 57bc093719SEd Schouten if (errno != EBADF) 58bc093719SEd Schouten errno = EINVAL; 59bc093719SEd Schouten 60bc093719SEd Schouten return (-1); 61bc093719SEd Schouten } 62bc093719SEd Schouten 63bc093719SEd Schouten /* 64bc093719SEd Schouten * In our implementation, grantpt() and unlockpt() don't actually have 65bc093719SEd Schouten * any use, because PTY's are created on the fly and already have proper 66bc093719SEd Schouten * permissions upon creation. 67bc093719SEd Schouten * 68bc093719SEd Schouten * Just make sure `fildes' actually points to a real PTY master device. 69bc093719SEd Schouten */ 70bc093719SEd Schouten __strong_reference(__isptmaster, grantpt); 71bc093719SEd Schouten __strong_reference(__isptmaster, unlockpt); 72bc093719SEd Schouten 73bc093719SEd Schouten /* 74bc093719SEd Schouten * ptsname(): return the pathname of the slave pseudo-terminal device 75bc093719SEd Schouten * associated with the specified master. 76bc093719SEd Schouten */ 77bc093719SEd Schouten char * 78bc093719SEd Schouten ptsname(int fildes) 79bc093719SEd Schouten { 80bc093719SEd Schouten static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV; 81bc093719SEd Schouten char *ret = NULL; 82bc093719SEd Schouten 83bc093719SEd Schouten /* Make sure fildes points to a master device. */ 84bc093719SEd Schouten if (__isptmaster(fildes) != 0) 85bc093719SEd Schouten goto done; 86bc093719SEd Schouten 8726d4f5e9SEd Schouten if (fdevname_r(fildes, pt_slave + (sizeof _PATH_DEV - 1), 8826d4f5e9SEd Schouten sizeof pt_slave - (sizeof _PATH_DEV - 1)) != NULL) 89bc093719SEd Schouten ret = pt_slave; 90bc093719SEd Schouten 916cbb6bbbSEd Maste done: 92bc093719SEd Schouten return (ret); 93bc093719SEd Schouten } 94