xref: /freebsd/lib/libc/stdlib/ptsname.c (revision 26d4f5e969a5850cbb0b2344835d515c368d5652)
1bc093719SEd Schouten /*-
2bc093719SEd Schouten  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3bc093719SEd Schouten  * All rights reserved.
4bc093719SEd Schouten  *
5bc093719SEd Schouten  * Portions of this software were developed under sponsorship from Snow
6bc093719SEd Schouten  * B.V., the Netherlands.
7bc093719SEd Schouten  *
8bc093719SEd Schouten  * Redistribution and use in source and binary forms, with or without
9bc093719SEd Schouten  * modification, are permitted provided that the following conditions
10bc093719SEd Schouten  * are met:
11bc093719SEd Schouten  * 1. Redistributions of source code must retain the above copyright
12bc093719SEd Schouten  *    notice, this list of conditions and the following disclaimer.
13bc093719SEd Schouten  * 2. Redistributions in binary form must reproduce the above copyright
14bc093719SEd Schouten  *    notice, this list of conditions and the following disclaimer in the
15bc093719SEd Schouten  *    documentation and/or other materials provided with the distribution.
16bc093719SEd Schouten  *
17bc093719SEd Schouten  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18bc093719SEd Schouten  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19bc093719SEd Schouten  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20bc093719SEd Schouten  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21bc093719SEd Schouten  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22bc093719SEd Schouten  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23bc093719SEd Schouten  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24bc093719SEd Schouten  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25bc093719SEd Schouten  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26bc093719SEd Schouten  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27bc093719SEd Schouten  * SUCH DAMAGE.
28bc093719SEd Schouten  */
29bc093719SEd Schouten 
30bc093719SEd Schouten #include <sys/cdefs.h>
31bc093719SEd Schouten #ifndef lint
32bc093719SEd Schouten __FBSDID("$FreeBSD$");
33bc093719SEd Schouten #endif /* not lint */
34bc093719SEd Schouten 
35bc093719SEd Schouten #include "namespace.h"
36bc093719SEd Schouten #include <sys/param.h>
37bc093719SEd Schouten #include <sys/ioctl.h>
38bc093719SEd Schouten 
39bc093719SEd Schouten #include <errno.h>
40bc093719SEd Schouten #include <paths.h>
4126d4f5e9SEd Schouten #include <stdlib.h>
42bc093719SEd Schouten #include "un-namespace.h"
43bc093719SEd Schouten 
44bc093719SEd Schouten /*
45bc093719SEd Schouten  * __isptmaster():  return whether the file descriptor refers to a
46bc093719SEd Schouten  *                  pseudo-terminal master device.
47bc093719SEd Schouten  */
48bc093719SEd Schouten static int
49bc093719SEd Schouten __isptmaster(int fildes)
50bc093719SEd Schouten {
51bc093719SEd Schouten 
52bc093719SEd Schouten 	if (_ioctl(fildes, TIOCPTMASTER) == 0)
53bc093719SEd Schouten 		return (0);
54bc093719SEd Schouten 
55bc093719SEd Schouten 	if (errno != EBADF)
56bc093719SEd Schouten 		errno = EINVAL;
57bc093719SEd Schouten 
58bc093719SEd Schouten 	return (-1);
59bc093719SEd Schouten }
60bc093719SEd Schouten 
61bc093719SEd Schouten /*
62bc093719SEd Schouten  * In our implementation, grantpt() and unlockpt() don't actually have
63bc093719SEd Schouten  * any use, because PTY's are created on the fly and already have proper
64bc093719SEd Schouten  * permissions upon creation.
65bc093719SEd Schouten  *
66bc093719SEd Schouten  * Just make sure `fildes' actually points to a real PTY master device.
67bc093719SEd Schouten  */
68bc093719SEd Schouten __strong_reference(__isptmaster, grantpt);
69bc093719SEd Schouten __strong_reference(__isptmaster, unlockpt);
70bc093719SEd Schouten 
71bc093719SEd Schouten /*
72bc093719SEd Schouten  * ptsname():  return the pathname of the slave pseudo-terminal device
73bc093719SEd Schouten  *             associated with the specified master.
74bc093719SEd Schouten  */
75bc093719SEd Schouten char *
76bc093719SEd Schouten ptsname(int fildes)
77bc093719SEd Schouten {
78bc093719SEd Schouten 	static char pt_slave[sizeof _PATH_DEV + SPECNAMELEN] = _PATH_DEV;
79bc093719SEd Schouten 	char *ret = NULL;
80bc093719SEd Schouten 	int sverrno = errno;
81bc093719SEd Schouten 
82bc093719SEd Schouten 	/* Make sure fildes points to a master device. */
83bc093719SEd Schouten 	if (__isptmaster(fildes) != 0)
84bc093719SEd Schouten 		goto done;
85bc093719SEd Schouten 
8626d4f5e9SEd Schouten 	if (fdevname_r(fildes, pt_slave + (sizeof _PATH_DEV - 1),
8726d4f5e9SEd Schouten 	    sizeof pt_slave - (sizeof _PATH_DEV - 1)) != NULL)
88bc093719SEd Schouten 		ret = pt_slave;
89bc093719SEd Schouten 
90bc093719SEd Schouten done:	/* Make sure ptsname() does not overwrite errno. */
91bc093719SEd Schouten 	errno = sverrno;
92bc093719SEd Schouten 	return (ret);
93bc093719SEd Schouten }
94