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