1/*	$OpenBSD: ttyname.c,v 1.21 2024/01/22 17:22:58 deraadt 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 <db.h>
36#include <string.h>
37#include <unistd.h>
38#include <paths.h>
39#include <limits.h>
40#include <errno.h>
41#include "thread_private.h"
42
43static char buf[TTY_NAME_MAX];
44static int oldttyname(struct stat *, char *, size_t);
45
46char *
47ttyname(int fd)
48{
49	_THREAD_PRIVATE_KEY(ttyname);
50	char *bufp = (char *) _THREAD_PRIVATE(ttyname, buf, NULL);
51	int err;
52
53	if (bufp == NULL)
54		return NULL;
55
56	err = ttyname_r(fd, bufp, sizeof buf);
57	if (err) {
58		errno = err;
59		return NULL;
60	}
61
62	return bufp;
63}
64DEF_WEAK(ttyname);
65
66int
67ttyname_r(int fd, char *buf, size_t len)
68{
69	struct stat sb;
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 (!isatty(fd))
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 = __hash_open(_PATH_DEVDB, O_RDONLY, 0, NULL, 0))) {
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(&sb, buf, len));
109}
110DEF_WEAK(ttyname_r);
111
112static int
113oldttyname(struct stat *sb, char *buf, size_t len)
114{
115	struct dirent *dirp;
116	DIR *dp;
117	struct stat dsb;
118	int error = ENOTTY;
119
120	if ((dp = opendir(_PATH_DEV)) == NULL)
121		return (errno);
122
123	while ((dirp = readdir(dp))) {
124		if (dirp->d_type != DT_CHR && dirp->d_type != DT_UNKNOWN)
125			continue;
126		if (fstatat(dirfd(dp), dirp->d_name, &dsb, AT_SYMLINK_NOFOLLOW)
127		    || !S_ISCHR(dsb.st_mode) || sb->st_rdev != dsb.st_rdev)
128			continue;
129		if (dirp->d_namlen > len - sizeof(_PATH_DEV)) {
130			error = ERANGE;
131		} else {
132			memcpy(buf + sizeof(_PATH_DEV) - 1, dirp->d_name,
133			    dirp->d_namlen + 1);
134			error = 0;
135		}
136		break;
137	}
138	(void)closedir(dp);
139	return (error);
140}
141