xref: /freebsd/lib/libc/gen/ttyname.c (revision ef5d438ed4bc17ad7ece3e40fe4d1f9baf3aadf7)
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 sccsid[] = "@(#)ttyname.c	8.2 (Berkeley) 1/27/94";
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 <stdlib.h>
43 #include <termios.h>
44 #include <unistd.h>
45 #include <db.h>
46 #include <string.h>
47 #include <paths.h>
48 
49 #ifdef _THREAD_SAFE
50 #include <pthread.h>
51 #include "pthread_private.h"
52 static pthread_mutex_t ttyname_lock = PTHREAD_MUTEX_INITIALIZER;
53 static pthread_key_t ttyname_key;
54 static int      ttyname_init = 0;
55 
56 char           *
57 ttyname(int fd)
58 {
59 	char           *ret;
60 
61 	if (_thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__) == 0) {
62 		ret = __ttyname_basic(fd);
63 		_thread_fd_unlock(fd, FD_READ);
64 	} else {
65 		ret = NULL;
66 	}
67 
68 	return (ret);
69 }
70 
71 char           *
72 __ttyname_r_basic(int fd, char *buf, size_t len)
73 {
74 	register struct dirent *dirp;
75 	register DIR   *dp;
76 	struct stat     dsb;
77 	struct stat     sb;
78 	char           *rval;
79 	int             minlen;
80 
81 	rval = NULL;
82 
83 	/* Must be a terminal. */
84 	if (!isatty(fd))
85 		return (rval);
86 	/* Must be a character device. */
87 	if (_thread_sys_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
88 		return (rval);
89 	/* Must have enough room */
90 	if (len <= sizeof(_PATH_DEV))
91 		return (rval);
92 
93 	if ((dp = opendir(_PATH_DEV)) != NULL) {
94 		memcpy(buf, _PATH_DEV, sizeof(_PATH_DEV));
95 		for (rval = NULL; (dirp = readdir(dp)) != NULL;) {
96 			if (dirp->d_fileno != sb.st_ino)
97 				continue;
98 			minlen = (len - (sizeof(_PATH_DEV) - 1)) < (dirp->d_namlen + 1) ?
99 				(len - (sizeof(_PATH_DEV) - 1)) : (dirp->d_namlen + 1);
100 			memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name, minlen);
101 			if (stat(buf, &dsb) || sb.st_dev != dsb.st_dev ||
102 			    sb.st_ino != dsb.st_ino)
103 				continue;
104 			rval = buf;
105 			break;
106 		}
107 		(void) closedir(dp);
108 	}
109 	return (rval);
110 }
111 
112 char           *
113 __ttyname_basic(int fd)
114 {
115 	char           *buf;
116 
117 	pthread_mutex_lock(&ttyname_lock);
118 	if (ttyname_init == 0) {
119 		if (pthread_keycreate(&ttyname_key, free)) {
120 			pthread_mutex_unlock(&ttyname_lock);
121 			return (NULL);
122 		}
123 		ttyname_init = 1;
124 	}
125 	pthread_mutex_unlock(&ttyname_lock);
126 
127 	/* Must have thread specific data field to put data */
128 	if (pthread_getspecific(ttyname_key, (void **) &buf) != 0) {
129 		return (NULL);
130 	} else if (buf == NULL) {
131 		if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) {
132 			if (pthread_setspecific(ttyname_key, buf) != 0) {
133 				free(buf);
134 				return (NULL);
135 			}
136 		} else {
137 			return (NULL);
138 		}
139 	}
140 	return (__ttyname_r_basic(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
141 }
142 
143 char           *
144 ttyname_r(int fd, char *buf, size_t len)
145 {
146 	char           *ret;
147 
148 	if (_thread_fd_lock(fd, FD_READ, NULL, __FILE__, __LINE__) == 0) {
149 		ret = __ttyname_r_basic(fd, buf, len);
150 		_thread_fd_unlock(fd, FD_READ);
151 	} else {
152 		ret = NULL;
153 	}
154 	return (ret);
155 }
156 #else
157 static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
158 static char *oldttyname __P((int, struct stat *));
159 
160 char *
161 ttyname(fd)
162 	int fd;
163 {
164 	struct stat sb;
165 	struct termios ttyb;
166 	DB *db;
167 	DBT data, key;
168 	struct {
169 		mode_t type;
170 		dev_t dev;
171 	} bkey;
172 
173 	/* Must be a terminal. */
174 	if (tcgetattr(fd, &ttyb) < 0)
175 		return (NULL);
176 	/* Must be a character device. */
177 	if (fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
178 		return (NULL);
179 
180 	if (db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)) {
181 		memset(&bkey, 0, sizeof(bkey));
182 		bkey.type = S_IFCHR;
183 		bkey.dev = sb.st_rdev;
184 		key.data = &bkey;
185 		key.size = sizeof(bkey);
186 		if (!(db->get)(db, &key, &data, 0)) {
187 			bcopy(data.data,
188 			    buf + sizeof(_PATH_DEV) - 1, data.size);
189 			(void)(db->close)(db);
190 			return (buf);
191 		}
192 		(void)(db->close)(db);
193 	}
194 	return (oldttyname(fd, &sb));
195 }
196 
197 static char *
198 oldttyname(fd, sb)
199 	int fd;
200 	struct stat *sb;
201 {
202 	register struct dirent *dirp;
203 	register DIR *dp;
204 	struct stat dsb;
205 
206 	if ((dp = opendir(_PATH_DEV)) == NULL)
207 		return (NULL);
208 
209 	while (dirp = readdir(dp)) {
210 		if (dirp->d_fileno != sb->st_ino)
211 			continue;
212 		bcopy(dirp->d_name, buf + sizeof(_PATH_DEV) - 1,
213 		    dirp->d_namlen + 1);
214 		if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev ||
215 		    sb->st_ino != dsb.st_ino)
216 			continue;
217 		(void)closedir(dp);
218 		return (buf);
219 	}
220 	(void)closedir(dp);
221 	return (NULL);
222 }
223 #endif
224