158f0484fSRodney W. Grimes /* 258f0484fSRodney W. Grimes * Copyright (c) 1988, 1993 358f0484fSRodney W. Grimes * The Regents of the University of California. All rights reserved. 458f0484fSRodney W. Grimes * 558f0484fSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 658f0484fSRodney W. Grimes * modification, are permitted provided that the following conditions 758f0484fSRodney W. Grimes * are met: 858f0484fSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 958f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 1058f0484fSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 1158f0484fSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 1258f0484fSRodney W. Grimes * documentation and/or other materials provided with the distribution. 1358f0484fSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 1458f0484fSRodney W. Grimes * must display the following acknowledgement: 1558f0484fSRodney W. Grimes * This product includes software developed by the University of 1658f0484fSRodney W. Grimes * California, Berkeley and its contributors. 1758f0484fSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 1858f0484fSRodney W. Grimes * may be used to endorse or promote products derived from this software 1958f0484fSRodney W. Grimes * without specific prior written permission. 2058f0484fSRodney W. Grimes * 2158f0484fSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 2258f0484fSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 2358f0484fSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2458f0484fSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 2558f0484fSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2658f0484fSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2758f0484fSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2858f0484fSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2958f0484fSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 3058f0484fSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 3158f0484fSRodney W. Grimes * SUCH DAMAGE. 3258f0484fSRodney W. Grimes */ 3358f0484fSRodney W. Grimes 3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint) 3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)ttyname.c 8.2 (Berkeley) 1/27/94"; 3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */ 37b231cb39SDavid E. O'Brien #include <sys/cdefs.h> 38b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$"); 3958f0484fSRodney W. Grimes 40d201fe46SDaniel Eischen #include "namespace.h" 4158f0484fSRodney W. Grimes #include <sys/types.h> 4258f0484fSRodney W. Grimes #include <sys/stat.h> 439a9ef8b6SPoul-Henning Kamp #include <sys/ioctl.h> 449a9ef8b6SPoul-Henning Kamp #include <sys/filio.h> 4558f0484fSRodney W. Grimes #include <fcntl.h> 4658f0484fSRodney W. Grimes #include <dirent.h> 47f70177e7SJulian Elischer #include <stdlib.h> 48fcc3b699SPeter Wemm #include <termios.h> 49f70177e7SJulian Elischer #include <unistd.h> 5058f0484fSRodney W. Grimes #include <string.h> 5158f0484fSRodney W. Grimes #include <paths.h> 52f70177e7SJulian Elischer #include <pthread.h> 538dcb56dcSXin LI #include <errno.h> 54d201fe46SDaniel Eischen #include "un-namespace.h" 55d201fe46SDaniel Eischen 56d201fe46SDaniel Eischen #include "libc_private.h" 57d201fe46SDaniel Eischen 589a9ef8b6SPoul-Henning Kamp static char ttyname_buf[sizeof(_PATH_DEV) + MAXNAMLEN]; 59d201fe46SDaniel Eischen 60d201fe46SDaniel Eischen static pthread_mutex_t ttyname_lock = PTHREAD_MUTEX_INITIALIZER; 61f70177e7SJulian Elischer static pthread_key_t ttyname_key; 62f70177e7SJulian Elischer static int ttyname_init = 0; 63f70177e7SJulian Elischer 648dcb56dcSXin LI int 65d201fe46SDaniel Eischen ttyname_r(int fd, char *buf, size_t len) 66f70177e7SJulian Elischer { 67f70177e7SJulian Elischer struct stat sb; 689a9ef8b6SPoul-Henning Kamp struct fiodgname_arg fgn; 69f70177e7SJulian Elischer 709a9ef8b6SPoul-Henning Kamp *buf = '\0'; 71f70177e7SJulian Elischer 72f70177e7SJulian Elischer /* Must be a terminal. */ 73f70177e7SJulian Elischer if (!isatty(fd)) 748dcb56dcSXin LI return (ENOTTY); 75f70177e7SJulian Elischer /* Must be a character device. */ 76d201fe46SDaniel Eischen if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode)) 778dcb56dcSXin LI return (ENOTTY); 78f70177e7SJulian Elischer /* Must have enough room */ 79f70177e7SJulian Elischer if (len <= sizeof(_PATH_DEV)) 808dcb56dcSXin LI return (ERANGE); 81f70177e7SJulian Elischer 8297679e71SPoul-Henning Kamp strcpy(buf, _PATH_DEV); 839a9ef8b6SPoul-Henning Kamp fgn.len = len - strlen(buf); 849a9ef8b6SPoul-Henning Kamp fgn.buf = buf + strlen(buf); 859a9ef8b6SPoul-Henning Kamp if (!_ioctl(fd, FIODGNAME, &fgn)) 8668b749ffSXin LI return (0); 8797679e71SPoul-Henning Kamp devname_r(sb.st_rdev, S_IFCHR, 88db297254SHajimu UMEMOTO buf + strlen(buf), len - strlen(buf)); 898dcb56dcSXin LI return (0); 90f70177e7SJulian Elischer } 91f70177e7SJulian Elischer 929a9ef8b6SPoul-Henning Kamp char * 939a9ef8b6SPoul-Henning Kamp ttyname(int fd) 94f70177e7SJulian Elischer { 95f70177e7SJulian Elischer char *buf; 96f70177e7SJulian Elischer 978dcb56dcSXin LI if (__isthreaded == 0) { 988dcb56dcSXin LI if (ttyname_r(fd, ttyname_buf, sizeof ttyname_buf) != 0) 998dcb56dcSXin LI return (NULL); 1008dcb56dcSXin LI else 1018dcb56dcSXin LI return (ttyname_buf); 1028dcb56dcSXin LI } 1039a9ef8b6SPoul-Henning Kamp 104f70177e7SJulian Elischer if (ttyname_init == 0) { 105d201fe46SDaniel Eischen _pthread_mutex_lock(&ttyname_lock); 106d201fe46SDaniel Eischen if (ttyname_init == 0) { 107d201fe46SDaniel Eischen if (_pthread_key_create(&ttyname_key, free)) { 108d201fe46SDaniel Eischen _pthread_mutex_unlock(&ttyname_lock); 109f70177e7SJulian Elischer return (NULL); 110f70177e7SJulian Elischer } 111f70177e7SJulian Elischer ttyname_init = 1; 112f70177e7SJulian Elischer } 113d201fe46SDaniel Eischen _pthread_mutex_unlock(&ttyname_lock); 114d201fe46SDaniel Eischen } 115f70177e7SJulian Elischer 116f70177e7SJulian Elischer /* Must have thread specific data field to put data */ 117d201fe46SDaniel Eischen if ((buf = _pthread_getspecific(ttyname_key)) == NULL) { 118f70177e7SJulian Elischer if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) { 119d201fe46SDaniel Eischen if (_pthread_setspecific(ttyname_key, buf) != 0) { 120f70177e7SJulian Elischer free(buf); 121f70177e7SJulian Elischer return (NULL); 122f70177e7SJulian Elischer } 123f70177e7SJulian Elischer } else { 124f70177e7SJulian Elischer return (NULL); 125f70177e7SJulian Elischer } 126f70177e7SJulian Elischer } 127532e65b8SHajimu UMEMOTO if (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN) != 0) 128532e65b8SHajimu UMEMOTO return (NULL); 1298dcb56dcSXin LI return (buf); 130f70177e7SJulian Elischer } 131f70177e7SJulian Elischer 132