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