1d43f7615SAlex Hornung /*
2d43f7615SAlex Hornung * Copyright (c) 2009 The DragonFly Project. All rights reserved.
3d43f7615SAlex Hornung *
4d43f7615SAlex Hornung * This code is derived from software contributed to The DragonFly Project
5d43f7615SAlex Hornung * by Alex Hornung <ahornung@gmail.com>
6d43f7615SAlex Hornung *
7d43f7615SAlex Hornung * Redistribution and use in source and binary forms, with or without
8d43f7615SAlex Hornung * modification, are permitted provided that the following conditions
9d43f7615SAlex Hornung * are met:
10d43f7615SAlex Hornung *
11d43f7615SAlex Hornung * 1. Redistributions of source code must retain the above copyright
12d43f7615SAlex Hornung * notice, this list of conditions and the following disclaimer.
13d43f7615SAlex Hornung * 2. Redistributions in binary form must reproduce the above copyright
14d43f7615SAlex Hornung * notice, this list of conditions and the following disclaimer in
15d43f7615SAlex Hornung * the documentation and/or other materials provided with the
16d43f7615SAlex Hornung * distribution.
17d43f7615SAlex Hornung * 3. Neither the name of The DragonFly Project nor the names of its
18d43f7615SAlex Hornung * contributors may be used to endorse or promote products derived
19d43f7615SAlex Hornung * from this software without specific, prior written permission.
20d43f7615SAlex Hornung *
21d43f7615SAlex Hornung * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22d43f7615SAlex Hornung * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23d43f7615SAlex Hornung * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24d43f7615SAlex Hornung * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25d43f7615SAlex Hornung * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26d43f7615SAlex Hornung * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27d43f7615SAlex Hornung * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28d43f7615SAlex Hornung * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29d43f7615SAlex Hornung * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30d43f7615SAlex Hornung * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31d43f7615SAlex Hornung * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32d43f7615SAlex Hornung * SUCH DAMAGE.
33d43f7615SAlex Hornung */
34d43f7615SAlex Hornung #include "namespace.h"
35d43f7615SAlex Hornung #include <sys/types.h>
36d43f7615SAlex Hornung #include <fcntl.h>
37d43f7615SAlex Hornung #include <dirent.h>
38d43f7615SAlex Hornung #include <stdlib.h>
39d43f7615SAlex Hornung #include <termios.h>
40d43f7615SAlex Hornung #include <unistd.h>
41d43f7615SAlex Hornung #include <string.h>
42d43f7615SAlex Hornung #include <paths.h>
43d43f7615SAlex Hornung #include <errno.h>
44*32cb8272SSascha Wildner #include <limits.h>
45d43f7615SAlex Hornung #include <machine/stdint.h>
46d43f7615SAlex Hornung #include <sys/stat.h>
47d43f7615SAlex Hornung #include <sys/ioctl.h>
48d43f7615SAlex Hornung #include "reentrant.h"
49d43f7615SAlex Hornung #include "un-namespace.h"
50d43f7615SAlex Hornung
51d43f7615SAlex Hornung #include "libc_private.h"
52d43f7615SAlex Hornung
53d43f7615SAlex Hornung #define TTYNAME_DEVFS_COMPAT 1
54d43f7615SAlex Hornung
55d43f7615SAlex Hornung static char ptsname_buf[sizeof(_PATH_DEV) + NAME_MAX];
56d43f7615SAlex Hornung
57d43f7615SAlex Hornung static once_t ptsname_init_once = ONCE_INITIALIZER;
58d43f7615SAlex Hornung static thread_key_t ptsname_key;
59d43f7615SAlex Hornung static int ptsname_keycreated = 0;
60d43f7615SAlex Hornung
61d43f7615SAlex Hornung static int
__isptmaster(int fd)62d43f7615SAlex Hornung __isptmaster(int fd)
63d43f7615SAlex Hornung {
64d43f7615SAlex Hornung int error;
65d43f7615SAlex Hornung
66d43f7615SAlex Hornung error = _ioctl(fd, TIOCISPTMASTER);
67d43f7615SAlex Hornung if ((error) && (errno != EBADF))
68d43f7615SAlex Hornung errno = EINVAL;
69d43f7615SAlex Hornung
70d43f7615SAlex Hornung return error;
71d43f7615SAlex Hornung }
72d43f7615SAlex Hornung
73d43f7615SAlex Hornung static void
ptsname_keycreate(void)74d43f7615SAlex Hornung ptsname_keycreate(void)
75d43f7615SAlex Hornung {
76d43f7615SAlex Hornung ptsname_keycreated = (thr_keycreate(&ptsname_key, free) == 0);
77d43f7615SAlex Hornung }
78d43f7615SAlex Hornung
79d43f7615SAlex Hornung char *
ptsname(int fd)80d43f7615SAlex Hornung ptsname(int fd)
81d43f7615SAlex Hornung {
82d43f7615SAlex Hornung int error;
83d43f7615SAlex Hornung size_t used;
84d43f7615SAlex Hornung char *buf;
85d43f7615SAlex Hornung
86d43f7615SAlex Hornung error = __isptmaster(fd);
87d43f7615SAlex Hornung if (error)
88d43f7615SAlex Hornung return (NULL);
89d43f7615SAlex Hornung
90d43f7615SAlex Hornung if (thr_main() != 0)
91d43f7615SAlex Hornung buf = ptsname_buf;
92d43f7615SAlex Hornung else {
93d43f7615SAlex Hornung if (thr_once(&ptsname_init_once, ptsname_keycreate) != 0 ||
94d43f7615SAlex Hornung !ptsname_keycreated)
95d43f7615SAlex Hornung return (NULL);
96d43f7615SAlex Hornung if ((buf = thr_getspecific(ptsname_key)) == NULL) {
97d43f7615SAlex Hornung if ((buf = malloc(sizeof ptsname_buf)) == NULL)
98d43f7615SAlex Hornung return (NULL);
99d43f7615SAlex Hornung if (thr_setspecific(ptsname_key, buf) != 0) {
100d43f7615SAlex Hornung free(buf);
101d43f7615SAlex Hornung return (NULL);
102d43f7615SAlex Hornung }
103d43f7615SAlex Hornung }
104d43f7615SAlex Hornung }
105d43f7615SAlex Hornung
106d43f7615SAlex Hornung strcpy(buf, "/dev/");
107d43f7615SAlex Hornung used = strlen(buf);
108d43f7615SAlex Hornung
109d43f7615SAlex Hornung if (((error = fdevname_r(fd, buf+used, sizeof(ptsname_buf)-used))) != 0) {
110d43f7615SAlex Hornung errno = error;
111d43f7615SAlex Hornung return (NULL);
112d43f7615SAlex Hornung }
113d43f7615SAlex Hornung
114d43f7615SAlex Hornung buf[used+2] = 's';
115d43f7615SAlex Hornung
116d43f7615SAlex Hornung return (buf);
117d43f7615SAlex Hornung }
118d43f7615SAlex Hornung
119d43f7615SAlex Hornung int
posix_openpt(int oflag)120d43f7615SAlex Hornung posix_openpt(int oflag)
121d43f7615SAlex Hornung {
122e2b7bcaeSSascha Wildner return _open("/dev/ptmx", oflag);
123d43f7615SAlex Hornung }
124d43f7615SAlex Hornung
125d43f7615SAlex Hornung int
unlockpt(int fd)126d43f7615SAlex Hornung unlockpt(int fd)
127d43f7615SAlex Hornung {
128d43f7615SAlex Hornung return __isptmaster(fd);
129d43f7615SAlex Hornung }
130d43f7615SAlex Hornung
131d43f7615SAlex Hornung int
grantpt(int fd)132d43f7615SAlex Hornung grantpt(int fd)
133d43f7615SAlex Hornung {
134d43f7615SAlex Hornung return __isptmaster(fd);
135d43f7615SAlex Hornung }
136