xref: /freebsd/lib/libc/gen/ttyname.c (revision 9a9ef8b65a2d3fc64c5e448acc39f079221881fa)
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>
439a9ef8b6SPoul-Henning Kamp #include <sys/ioctl.h>
449a9ef8b6SPoul-Henning Kamp #include <sys/filio.h>
4558f0484fSRodney W. Grimes #include <fcntl.h>
4658f0484fSRodney W. Grimes #include <dirent.h>
47f70177e7SJulian Elischer #include <stdlib.h>
48fcc3b699SPeter Wemm #include <termios.h>
49f70177e7SJulian Elischer #include <unistd.h>
5058f0484fSRodney W. Grimes #include <string.h>
5158f0484fSRodney W. Grimes #include <paths.h>
52f70177e7SJulian Elischer #include <pthread.h>
53d201fe46SDaniel Eischen #include "un-namespace.h"
54d201fe46SDaniel Eischen 
55d201fe46SDaniel Eischen #include "libc_private.h"
56d201fe46SDaniel Eischen 
579a9ef8b6SPoul-Henning Kamp static char ttyname_buf[sizeof(_PATH_DEV) + MAXNAMLEN];
58d201fe46SDaniel Eischen 
59d201fe46SDaniel Eischen static pthread_mutex_t	ttyname_lock = PTHREAD_MUTEX_INITIALIZER;
60f70177e7SJulian Elischer static pthread_key_t	ttyname_key;
61f70177e7SJulian Elischer static int		ttyname_init = 0;
62f70177e7SJulian Elischer 
63f70177e7SJulian Elischer char *
64d201fe46SDaniel Eischen ttyname_r(int fd, char *buf, size_t len)
65f70177e7SJulian Elischer {
66f70177e7SJulian Elischer 	struct stat	sb;
67f70177e7SJulian Elischer 	char		*rval;
689a9ef8b6SPoul-Henning Kamp 	struct fiodgname_arg fgn;
69f70177e7SJulian Elischer 
70f70177e7SJulian Elischer 	rval = NULL;
719a9ef8b6SPoul-Henning Kamp 	*buf = '\0';
72f70177e7SJulian Elischer 
73f70177e7SJulian Elischer 	/* Must be a terminal. */
74f70177e7SJulian Elischer 	if (!isatty(fd))
75f70177e7SJulian Elischer 		return (rval);
76f70177e7SJulian Elischer 	/* Must be a character device. */
77d201fe46SDaniel Eischen 	if (_fstat(fd, &sb) || !S_ISCHR(sb.st_mode))
78f70177e7SJulian Elischer 		return (rval);
79f70177e7SJulian Elischer 	/* Must have enough room */
80f70177e7SJulian Elischer 	if (len <= sizeof(_PATH_DEV))
81f70177e7SJulian Elischer 		return (rval);
82f70177e7SJulian Elischer 
8397679e71SPoul-Henning Kamp 	strcpy(buf, _PATH_DEV);
849a9ef8b6SPoul-Henning Kamp 	fgn.len = len - strlen(buf);
859a9ef8b6SPoul-Henning Kamp 	fgn.buf = buf + strlen(buf);
869a9ef8b6SPoul-Henning Kamp 	if (!_ioctl(fd, FIODGNAME, &fgn))
879a9ef8b6SPoul-Henning Kamp 		return(buf);
8897679e71SPoul-Henning Kamp 	devname_r(sb.st_rdev, S_IFCHR,
8997679e71SPoul-Henning Kamp 	    buf + strlen(buf), sizeof(buf) - strlen(buf));
9097679e71SPoul-Henning Kamp 	return (buf);
91f70177e7SJulian Elischer }
92f70177e7SJulian Elischer 
939a9ef8b6SPoul-Henning Kamp char *
949a9ef8b6SPoul-Henning Kamp ttyname(int fd)
95f70177e7SJulian Elischer {
96f70177e7SJulian Elischer 	char	*buf;
97f70177e7SJulian Elischer 
989a9ef8b6SPoul-Henning Kamp 	if (__isthreaded == 0)
999a9ef8b6SPoul-Henning Kamp 		return (ttyname_r(fd, ttyname_buf, sizeof ttyname_buf));
1009a9ef8b6SPoul-Henning Kamp 
101f70177e7SJulian Elischer 	if (ttyname_init == 0) {
102d201fe46SDaniel Eischen 		_pthread_mutex_lock(&ttyname_lock);
103d201fe46SDaniel Eischen 		if (ttyname_init == 0) {
104d201fe46SDaniel Eischen 			if (_pthread_key_create(&ttyname_key, free)) {
105d201fe46SDaniel Eischen 				_pthread_mutex_unlock(&ttyname_lock);
106f70177e7SJulian Elischer 				return (NULL);
107f70177e7SJulian Elischer 			}
108f70177e7SJulian Elischer 			ttyname_init = 1;
109f70177e7SJulian Elischer 		}
110d201fe46SDaniel Eischen 		_pthread_mutex_unlock(&ttyname_lock);
111d201fe46SDaniel Eischen 	}
112f70177e7SJulian Elischer 
113f70177e7SJulian Elischer 	/* Must have thread specific data field to put data */
114d201fe46SDaniel Eischen 	if ((buf = _pthread_getspecific(ttyname_key)) == NULL) {
115f70177e7SJulian Elischer 		if ((buf = malloc(sizeof(_PATH_DEV) + MAXNAMLEN)) != NULL) {
116d201fe46SDaniel Eischen 			if (_pthread_setspecific(ttyname_key, buf) != 0) {
117f70177e7SJulian Elischer 				free(buf);
118f70177e7SJulian Elischer 				return (NULL);
119f70177e7SJulian Elischer 			}
120f70177e7SJulian Elischer 		} else {
121f70177e7SJulian Elischer 			return (NULL);
122f70177e7SJulian Elischer 		}
123f70177e7SJulian Elischer 	}
124d201fe46SDaniel Eischen 	return (ttyname_r(fd, buf, sizeof(_PATH_DEV) + MAXNAMLEN));
125f70177e7SJulian Elischer }
126f70177e7SJulian Elischer 
127