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