xref: /freebsd/lib/libc/gen/ttyname.c (revision 97679e71b0bb9261412b5b8721ba5479657856cb)
158f0484fSRodney W. Grimes /*
258f0484fSRodney W. Grimes  * Copyright (c) 1988, 1993
358f0484fSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
458f0484fSRodney W. Grimes  *
558f0484fSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
658f0484fSRodney W. Grimes  * modification, are permitted provided that the following conditions
758f0484fSRodney W. Grimes  * are met:
858f0484fSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
958f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
1058f0484fSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
1158f0484fSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
1258f0484fSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
1358f0484fSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
1458f0484fSRodney W. Grimes  *    must display the following acknowledgement:
1558f0484fSRodney W. Grimes  *	This product includes software developed by the University of
1658f0484fSRodney W. Grimes  *	California, Berkeley and its contributors.
1758f0484fSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
1858f0484fSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
1958f0484fSRodney W. Grimes  *    without specific prior written permission.
2058f0484fSRodney W. Grimes  *
2158f0484fSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2258f0484fSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2358f0484fSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2458f0484fSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2558f0484fSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2658f0484fSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2758f0484fSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2858f0484fSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2958f0484fSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3058f0484fSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3158f0484fSRodney W. Grimes  * SUCH DAMAGE.
3258f0484fSRodney W. Grimes  */
3358f0484fSRodney W. Grimes 
3458f0484fSRodney W. Grimes #if defined(LIBC_SCCS) && !defined(lint)
3558f0484fSRodney W. Grimes static char sccsid[] = "@(#)ttyname.c	8.2 (Berkeley) 1/27/94";
3658f0484fSRodney W. Grimes #endif /* LIBC_SCCS and not lint */
37b231cb39SDavid E. O'Brien #include <sys/cdefs.h>
38b231cb39SDavid E. O'Brien __FBSDID("$FreeBSD$");
3958f0484fSRodney W. Grimes 
40d201fe46SDaniel Eischen #include "namespace.h"
4158f0484fSRodney W. Grimes #include <sys/types.h>
4258f0484fSRodney W. Grimes #include <sys/stat.h>
4358f0484fSRodney W. Grimes #include <fcntl.h>
4458f0484fSRodney W. Grimes #include <dirent.h>
45f70177e7SJulian Elischer #include <stdlib.h>
46fcc3b699SPeter Wemm #include <termios.h>
47f70177e7SJulian Elischer #include <unistd.h>
4858f0484fSRodney W. Grimes #include <string.h>
4958f0484fSRodney W. Grimes #include <paths.h>
50f70177e7SJulian Elischer #include <pthread.h>
51d201fe46SDaniel Eischen #include "un-namespace.h"
52d201fe46SDaniel Eischen 
53d201fe46SDaniel Eischen #include "libc_private.h"
54d201fe46SDaniel Eischen 
55d201fe46SDaniel Eischen static char buf[sizeof(_PATH_DEV) + MAXNAMLEN] = _PATH_DEV;
56b231cb39SDavid E. O'Brien static char *oldttyname(int, struct stat *);
57d201fe46SDaniel Eischen static char *ttyname_threaded(int fd);
58d201fe46SDaniel Eischen static char *ttyname_unthreaded(int fd);
59d201fe46SDaniel Eischen 
60d201fe46SDaniel Eischen static pthread_mutex_t	ttyname_lock = PTHREAD_MUTEX_INITIALIZER;
61f70177e7SJulian Elischer static pthread_key_t	ttyname_key;
62f70177e7SJulian Elischer static int		ttyname_init = 0;
63f70177e7SJulian Elischer 
64f70177e7SJulian Elischer char *
65f70177e7SJulian Elischer ttyname(int fd)
66f70177e7SJulian Elischer {
67f70177e7SJulian Elischer 	char           *ret;
68f70177e7SJulian Elischer 
69d201fe46SDaniel Eischen 	if (__isthreaded == 0)
70d201fe46SDaniel Eischen 		ret = ttyname_unthreaded(fd);
71d201fe46SDaniel Eischen 	else
72d201fe46SDaniel Eischen 		ret = ttyname_threaded(fd);
73f70177e7SJulian Elischer 	return (ret);
74f70177e7SJulian Elischer }
75f70177e7SJulian Elischer 
76f70177e7SJulian Elischer char *
77d201fe46SDaniel Eischen ttyname_r(int fd, char *buf, size_t len)
78f70177e7SJulian Elischer {
79f70177e7SJulian Elischer 	struct stat	dsb;
80f70177e7SJulian Elischer 	struct stat	sb;
81f70177e7SJulian Elischer 	char		*rval;
82f70177e7SJulian Elischer 	int		minlen;
83f70177e7SJulian Elischer 
84f70177e7SJulian Elischer 	rval = NULL;
85f70177e7SJulian Elischer 
86f70177e7SJulian Elischer 	/* Must be a terminal. */
87f70177e7SJulian Elischer 	if (!isatty(fd))
88f70177e7SJulian Elischer 		return (rval);
89f70177e7SJulian Elischer 	/* Must be a character device. */
90d201fe46SDaniel Eischen 	if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
91f70177e7SJulian Elischer 		return (rval);
92f70177e7SJulian Elischer 	/* Must have enough room */
93f70177e7SJulian Elischer 	if (len <= sizeof(_PATH_DEV))
94f70177e7SJulian Elischer 		return (rval);
95f70177e7SJulian Elischer 
9697679e71SPoul-Henning Kamp 	strcpy(buf, _PATH_DEV);
9797679e71SPoul-Henning Kamp 	devname_r(sb.st_rdev, S_IFCHR,
9897679e71SPoul-Henning Kamp 	    buf + strlen(buf), sizeof(buf) - strlen(buf));
9997679e71SPoul-Henning Kamp 	return (buf);
100f70177e7SJulian Elischer }
101f70177e7SJulian Elischer 
102d201fe46SDaniel Eischen static char *
103d201fe46SDaniel Eischen ttyname_threaded(int fd)
104f70177e7SJulian Elischer {
105f70177e7SJulian Elischer 	char	*buf;
106f70177e7SJulian Elischer 
107f70177e7SJulian Elischer 	if (ttyname_init == 0) {
108d201fe46SDaniel Eischen 		_pthread_mutex_lock(&ttyname_lock);
109d201fe46SDaniel Eischen 		if (ttyname_init == 0) {
110d201fe46SDaniel Eischen 			if (_pthread_key_create(&ttyname_key, free)) {
111d201fe46SDaniel Eischen 				_pthread_mutex_unlock(&ttyname_lock);
112f70177e7SJulian Elischer 				return (NULL);
113f70177e7SJulian Elischer 			}
114f70177e7SJulian Elischer 			ttyname_init = 1;
115f70177e7SJulian Elischer 		}
116d201fe46SDaniel Eischen 		_pthread_mutex_unlock(&ttyname_lock);
117d201fe46SDaniel Eischen 	}
118f70177e7SJulian Elischer 
119f70177e7SJulian Elischer 	/* Must have thread specific data field to put data */
120d201fe46SDaniel Eischen 	if ((buf = _pthread_getspecific(ttyname_key)) == NULL) {
121f70177e7SJulian Elischer 		if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) {
122d201fe46SDaniel Eischen 			if (_pthread_setspecific(ttyname_key, buf) != 0) {
123f70177e7SJulian Elischer 				free(buf);
124f70177e7SJulian Elischer 				return (NULL);
125f70177e7SJulian Elischer 			}
126f70177e7SJulian Elischer 		} else {
127f70177e7SJulian Elischer 			return (NULL);
128f70177e7SJulian Elischer 		}
129f70177e7SJulian Elischer 	}
130d201fe46SDaniel Eischen 	return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
131f70177e7SJulian Elischer }
132f70177e7SJulian Elischer 
133d201fe46SDaniel Eischen static char *
134d201fe46SDaniel Eischen ttyname_unthreaded(int fd)
13558f0484fSRodney W. Grimes {
13658f0484fSRodney W. Grimes 	struct stat	sb;
137fcc3b699SPeter Wemm 	struct termios	ttyb;
13858f0484fSRodney W. Grimes 
13958f0484fSRodney W. Grimes 	/* Must be a terminal. */
140fcc3b699SPeter Wemm 	if (tcgetattr(fd, &ttyb) < 0)
14158f0484fSRodney W. Grimes 		return (NULL);
14258f0484fSRodney W. Grimes 	/* Must be a character device. */
143d201fe46SDaniel Eischen 	if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
14458f0484fSRodney W. Grimes 		return (NULL);
14558f0484fSRodney W. Grimes 
14697679e71SPoul-Henning Kamp 	strcpy(buf, _PATH_DEV);
14797679e71SPoul-Henning Kamp 	devname_r(sb.st_rdev, S_IFCHR,
14897679e71SPoul-Henning Kamp 	    buf + strlen(buf), sizeof(buf) - strlen(buf));
14997679e71SPoul-Henning Kamp 	return (buf);
15058f0484fSRodney W. Grimes }
151