ttyname.c revision 1.24.6.2 1 1.24.6.2 ad /* $NetBSD: ttyname.c,v 1.24.6.2 2008/06/25 11:47:30 ad Exp $ */
2 1.24.6.2 ad
3 1.24.6.2 ad /*
4 1.24.6.2 ad * Copyright (c) 1988, 1993
5 1.24.6.2 ad * The Regents of the University of California. All rights reserved.
6 1.24.6.2 ad *
7 1.24.6.2 ad * Redistribution and use in source and binary forms, with or without
8 1.24.6.2 ad * modification, are permitted provided that the following conditions
9 1.24.6.2 ad * are met:
10 1.24.6.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.24.6.2 ad * notice, this list of conditions and the following disclaimer.
12 1.24.6.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.24.6.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.24.6.2 ad * documentation and/or other materials provided with the distribution.
15 1.24.6.2 ad * 3. Neither the name of the University nor the names of its contributors
16 1.24.6.2 ad * may be used to endorse or promote products derived from this software
17 1.24.6.2 ad * without specific prior written permission.
18 1.24.6.2 ad *
19 1.24.6.2 ad * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 1.24.6.2 ad * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 1.24.6.2 ad * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 1.24.6.2 ad * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 1.24.6.2 ad * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 1.24.6.2 ad * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 1.24.6.2 ad * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 1.24.6.2 ad * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 1.24.6.2 ad * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 1.24.6.2 ad * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 1.24.6.2 ad * SUCH DAMAGE.
30 1.24.6.2 ad */
31 1.24.6.2 ad
32 1.24.6.2 ad #include <sys/cdefs.h>
33 1.24.6.2 ad #if defined(LIBC_SCCS) && !defined(lint)
34 1.24.6.2 ad #if 0
35 1.24.6.2 ad static char sccsid[] = "@(#)ttyname.c 8.2 (Berkeley) 1/27/94";
36 1.24.6.2 ad #else
37 1.24.6.2 ad __RCSID("$NetBSD: ttyname.c,v 1.24.6.2 2008/06/25 11:47:30 ad Exp $");
38 1.24.6.2 ad #endif
39 1.24.6.2 ad #endif /* LIBC_SCCS and not lint */
40 1.24.6.2 ad
41 1.24.6.2 ad #include "namespace.h"
42 1.24.6.2 ad #include <sys/types.h>
43 1.24.6.2 ad #include <sys/param.h>
44 1.24.6.2 ad #include <sys/stat.h>
45 1.24.6.2 ad
46 1.24.6.2 ad #include <assert.h>
47 1.24.6.2 ad #include <db.h>
48 1.24.6.2 ad #include <dirent.h>
49 1.24.6.2 ad #include <errno.h>
50 1.24.6.2 ad #include <fcntl.h>
51 1.24.6.2 ad #include <paths.h>
52 1.24.6.2 ad #include <string.h>
53 1.24.6.2 ad #include <termios.h>
54 1.24.6.2 ad #include <unistd.h>
55 1.24.6.2 ad #include <sys/ioctl.h>
56 1.24.6.2 ad
57 1.24.6.2 ad #ifdef __weak_alias
58 1.24.6.2 ad __weak_alias(ttyname,_ttyname)
59 1.24.6.2 ad __weak_alias(ttyname_r,_ttyname_r)
60 1.24.6.2 ad #endif
61 1.24.6.2 ad
62 1.24.6.2 ad static int oldttyname(const struct stat *, char *, size_t);
63 1.24.6.2 ad
64 1.24.6.2 ad int
65 1.24.6.2 ad ttyname_r(int fd, char *buf, size_t len)
66 1.24.6.2 ad {
67 1.24.6.2 ad struct stat sb;
68 1.24.6.2 ad struct termios ttyb;
69 1.24.6.2 ad DB *db;
70 1.24.6.2 ad DBT data, key;
71 1.24.6.2 ad struct {
72 1.24.6.2 ad mode_t type;
73 1.24.6.2 ad dev_t dev;
74 1.24.6.2 ad } bkey;
75 1.24.6.2 ad struct ptmget ptm;
76 1.24.6.2 ad #define DEVSZ (sizeof(_PATH_DEV) - 1)
77 1.24.6.2 ad
78 1.24.6.2 ad _DIAGASSERT(fd != -1);
79 1.24.6.2 ad
80 1.24.6.2 ad if (len <= DEVSZ) {
81 1.24.6.2 ad return ERANGE;
82 1.24.6.2 ad }
83 1.24.6.2 ad
84 1.24.6.2 ad /* If it is a pty, deal with it quickly */
85 1.24.6.2 ad if (ioctl(fd, TIOCPTSNAME, &ptm) != -1) {
86 1.24.6.2 ad if (strlcpy(buf, ptm.sn, len) >= len) {
87 1.24.6.2 ad return ERANGE;
88 1.24.6.2 ad }
89 1.24.6.2 ad return 0;
90 1.24.6.2 ad }
91 1.24.6.2 ad /* Must be a terminal. */
92 1.24.6.2 ad if (tcgetattr(fd, &ttyb) == -1)
93 1.24.6.2 ad return errno;
94 1.24.6.2 ad
95 1.24.6.2 ad /* Must be a character device. */
96 1.24.6.2 ad if (fstat(fd, &sb))
97 1.24.6.2 ad return errno;
98 1.24.6.2 ad if (!S_ISCHR(sb.st_mode))
99 1.24.6.2 ad return ENOTTY;
100 1.24.6.2 ad
101 1.24.6.2 ad (void)memcpy(buf, _PATH_DEV, DEVSZ);
102 1.24.6.2 ad if ((db = dbopen(_PATH_DEVDB, O_RDONLY, 0, DB_HASH, NULL)) != NULL) {
103 1.24.6.2 ad (void)memset(&bkey, 0, sizeof(bkey));
104 1.24.6.2 ad bkey.type = S_IFCHR;
105 1.24.6.2 ad bkey.dev = sb.st_rdev;
106 1.24.6.2 ad key.data = &bkey;
107 1.24.6.2 ad key.size = sizeof(bkey);
108 1.24.6.2 ad if (!(db->get)(db, &key, &data, 0)) {
109 1.24.6.2 ad if (len - DEVSZ <= data.size) {
110 1.24.6.2 ad return ERANGE;
111 1.24.6.2 ad }
112 1.24.6.2 ad (void)memcpy(buf + DEVSZ, data.data, data.size);
113 1.24.6.2 ad (void)(db->close)(db);
114 1.24.6.2 ad return 0;
115 1.24.6.2 ad }
116 1.24.6.2 ad (void)(db->close)(db);
117 1.24.6.2 ad }
118 1.24.6.2 ad if (oldttyname(&sb, buf, len) == -1)
119 1.24.6.2 ad return errno;
120 1.24.6.2 ad return 0;
121 1.24.6.2 ad }
122 1.24.6.2 ad
123 1.24.6.2 ad static int
124 1.24.6.2 ad oldttyname(const struct stat *sb, char *buf, size_t len)
125 1.24.6.2 ad {
126 1.24.6.2 ad struct dirent *dirp;
127 1.24.6.2 ad DIR *dp;
128 1.24.6.2 ad struct stat dsb;
129 1.24.6.2 ad size_t dlen;
130 1.24.6.2 ad
131 1.24.6.2 ad _DIAGASSERT(sb != NULL);
132 1.24.6.2 ad
133 1.24.6.2 ad if ((dp = opendir(_PATH_DEV)) == NULL)
134 1.24.6.2 ad return -1;
135 1.24.6.2 ad
136 1.24.6.2 ad while ((dirp = readdir(dp)) != NULL) {
137 1.24.6.2 ad if (dirp->d_fileno != sb->st_ino)
138 1.24.6.2 ad continue;
139 1.24.6.2 ad dlen = dirp->d_namlen + 1;
140 1.24.6.2 ad if (len - DEVSZ <= dlen) {
141 1.24.6.2 ad /*
142 1.24.6.2 ad * XXX: we return an error if *any* entry does not
143 1.24.6.2 ad * fit
144 1.24.6.2 ad */
145 1.24.6.2 ad errno = ERANGE;
146 1.24.6.2 ad (void)closedir(dp);
147 1.24.6.2 ad return -1;
148 1.24.6.2 ad }
149 1.24.6.2 ad (void)memcpy(buf + DEVSZ, dirp->d_name, dlen);
150 1.24.6.2 ad if (stat(buf, &dsb) || sb->st_dev != dsb.st_dev ||
151 1.24.6.2 ad sb->st_ino != dsb.st_ino)
152 1.24.6.2 ad continue;
153 1.24.6.2 ad (void)closedir(dp);
154 1.24.6.2 ad return 0;
155 1.24.6.2 ad }
156 1.24.6.2 ad (void)closedir(dp);
157 1.24.6.2 ad /*
158 1.24.6.2 ad * XXX: Documented by TOG to return EBADF or ENOTTY only; neither are
159 1.24.6.2 ad * applicable here.
160 1.24.6.2 ad */
161 1.24.6.2 ad errno = ENOENT;
162 1.24.6.2 ad return -1;
163 1.24.6.2 ad }
164 1.24.6.2 ad
165 1.24.6.2 ad char *
166 1.24.6.2 ad ttyname(int fd)
167 1.24.6.2 ad {
168 1.24.6.2 ad static char buf[MAXPATHLEN];
169 1.24.6.2 ad int rv;
170 1.24.6.2 ad
171 1.24.6.2 ad rv = ttyname_r(fd, buf, sizeof(buf));
172 1.24.6.2 ad if (rv != 0) {
173 1.24.6.2 ad errno = rv;
174 1.24.6.2 ad return NULL;
175 1.24.6.2 ad }
176 1.24.6.2 ad return buf;
177 1.24.6.2 ad }
178