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