1 /* 2 * Copyright (c) 1988, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #if defined(LIBC_SCCS) && !defined(lint) 35 static char rcsid[] = "$OpenBSD: ttyname.c,v 1.7 2000/01/06 08:24:16 d Exp $"; 36 #endif /* LIBC_SCCS and not lint */ 37 38 #include <sys/types.h> 39 #include <sys/stat.h> 40 #include <fcntl.h> 41 #include <dirent.h> 42 #include <termios.h> 43 #include <db.h> 44 #include <string.h> 45 #include <unistd.h> 46 #include <paths.h> 47 #include <limits.h> 48 #include <errno.h> 49 #include "thread_private.h" 50 51 static char buf[TTY_NAME_MAX]; 52 static int oldttyname __P((int, struct stat *, char *, size_t)); 53 static int __ttyname_r_basic __P((int, char *, size_t)); 54 55 int 56 ttyname_r(int fd, char *buf, size_t buflen) 57 { 58 int ret; 59 60 if ((ret = _FD_LOCK(fd, FD_READ, NULL)) == 0) { 61 ret = __ttyname_r_basic(fd, buf, buflen); 62 _FD_UNLOCK(fd, FD_READ); 63 } 64 return ret; 65 } 66 67 char * 68 ttyname(int fd) 69 { 70 _THREAD_PRIVATE_KEY(ttyname); 71 char * bufp = (char*) _THREAD_PRIVATE(ttyname, buf, NULL); 72 int err; 73 74 if (bufp == NULL) 75 return NULL; 76 err = ttyname_r(fd, bufp, sizeof buf); 77 if (err) { 78 errno = err; 79 return NULL; 80 } 81 else 82 return bufp; 83 } 84 85 static int 86 __ttyname_r_basic(fd, buf, len) 87 int fd; 88 char *buf; 89 size_t len; 90 { 91 struct stat sb; 92 struct termios ttyb; 93 DB *db; 94 DBT data, key; 95 struct { 96 mode_t type; 97 dev_t dev; 98 } bkey; 99 100 /* Must be a terminal. */ 101 if (tcgetattr(fd, &ttyb) < 0) 102 return (errno); 103 /* Must be a character device. */ 104 if (fstat(fd, &sb)) 105 return (errno); 106 if (!S_ISCHR(sb.st_mode)) 107 return (ENOTTY); 108 if (len < sizeof(_PATH_DEV)) 109 return (ERANGE); 110 111 memcpy(buf, _PATH_DEV, sizeof(_PATH_DEV)); 112 113 if ((db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL))) { 114 memset(&bkey, 0, sizeof(bkey)); 115 bkey.type = S_IFCHR; 116 bkey.dev = sb.st_rdev; 117 key.data = &bkey; 118 key.size = sizeof(bkey); 119 if (!(db->get)(db, &key, &data, 0)) { 120 if (data.size > len - (sizeof(_PATH_DEV) - 1)) { 121 (void)(db->close)(db); 122 return (ERANGE); 123 } 124 memcpy(buf + sizeof(_PATH_DEV) - 1, data.data, 125 data.size); 126 (void)(db->close)(db); 127 return (0); 128 } 129 (void)(db->close)(db); 130 } 131 return (oldttyname(fd, &sb, buf, len)); 132 } 133 134 /* ARGSUSED */ 135 static int 136 oldttyname(fd, sb, buf, len) 137 int fd; 138 struct stat *sb; 139 char *buf; 140 size_t len; 141 { 142 register struct dirent *dirp; 143 register DIR *dp; 144 struct stat dsb; 145 146 if ((dp = opendir(_PATH_DEV)) == NULL) 147 return (errno); 148 149 while ((dirp = readdir(dp))) { 150 if (dirp->d_fileno != sb->st_ino) 151 continue; 152 if (dirp->d_namlen > len - sizeof(_PATH_DEV)) { 153 (void)closedir(dp); 154 return (ERANGE); 155 } 156 memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name, 157 dirp->d_namlen + 1); 158 if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev || 159 sb->st_ino != dsb.st_ino) 160 continue; 161 (void)closedir(dp); 162 return (0); 163 } 164 (void)closedir(dp); 165 return (ENOTTY); 166 } 167