summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormillert <>2012-12-03 20:08:33 +0000
committermillert <>2012-12-03 20:08:33 +0000
commite10e53f72f10d68e0212baefb36069e88ef863a3 (patch)
treedcd259d2d99c6c999bb25321e53b0ea6d3fef8db
parent2a264863e549e493a466a263d4d8a7d2d3523756 (diff)
downloadopenbsd-e10e53f72f10d68e0212baefb36069e88ef863a3.tar.gz
openbsd-e10e53f72f10d68e0212baefb36069e88ef863a3.tar.bz2
openbsd-e10e53f72f10d68e0212baefb36069e88ef863a3.zip
Simple emulation of POSIX pty APIs posix_openpt(), ptsname(),
grantpt() and unlockpt() using /dev/ptm. Man pages from FreeBSD. OK kettenis@ deraadt@ beck@ ajacoutot@ naddy@
-rw-r--r--src/lib/libc/stdlib/Makefile.inc13
-rw-r--r--src/lib/libc/stdlib/posix_openpt.3128
-rw-r--r--src/lib/libc/stdlib/posix_pty.c119
-rw-r--r--src/lib/libc/stdlib/ptsname.3160
4 files changed, 414 insertions, 6 deletions
diff --git a/src/lib/libc/stdlib/Makefile.inc b/src/lib/libc/stdlib/Makefile.inc
index 652d6fb90f..5f3a142ec4 100644
--- a/src/lib/libc/stdlib/Makefile.inc
+++ b/src/lib/libc/stdlib/Makefile.inc
@@ -1,4 +1,4 @@
1# $OpenBSD: Makefile.inc,v 1.46 2012/09/15 20:59:38 miod Exp $ 1# $OpenBSD: Makefile.inc,v 1.47 2012/12/03 20:08:33 millert Exp $
2 2
3# stdlib sources 3# stdlib sources
4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib 4.PATH: ${LIBCSRCDIR}/arch/${MACHINE_CPU}/stdlib ${LIBCSRCDIR}/stdlib
@@ -6,8 +6,8 @@
6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \ 6SRCS+= a64l.c abort.c atexit.c atoi.c atof.c atol.c atoll.c bsearch.c \
7 cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \ 7 cfree.c exit.c ecvt.c gcvt.c getenv.c getopt_long.c \
8 getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \ 8 getsubopt.c hcreate.c heapsort.c imaxabs.c imaxdiv.c l64a.c llabs.c \
9 lldiv.c lsearch.c malloc.c merge.c qsort.c radixsort.c rand.c \ 9 lldiv.c lsearch.c malloc.c merge.c posix_pty.c qsort.c radixsort.c \
10 random.c realpath.c setenv.c strtoimax.c strtol.c \ 10 rand.c random.c realpath.c setenv.c strtoimax.c strtol.c \
11 strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \ 11 strtoll.c strtonum.c strtoul.c strtoull.c strtoumax.c system.c \
12 tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \ 12 tfind.c tsearch.c _rand48.c drand48.c erand48.c jrand48.c lcong48.c \
13 lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c 13 lrand48.c mrand48.c nrand48.c seed48.c srand48.c qabs.c qdiv.c _Exit.c
@@ -34,9 +34,9 @@ SRCS+= insque.c remque.c
34MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \ 34MAN+= a64l.3 abort.3 abs.3 alloca.3 atexit.3 atof.3 atoi.3 atol.3 atoll.3 \
35 bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \ 35 bsearch.3 div.3 ecvt.3 exit.3 getenv.3 getopt.3 getopt_long.3 \
36 getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \ 36 getsubopt.3 hcreate.3 imaxabs.3 imaxdiv.3 insque.3 labs.3 ldiv.3 \
37 lldiv.3 lsearch.3 malloc.3 posix_memalign.3 qabs.3 qdiv.3 qsort.3 \ 37 lldiv.3 lsearch.3 malloc.3 posix_memalign.3 posix_openpt.3 ptsname.3 \
38 radixsort.3 rand48.3 rand.3 random.3 realpath.3 strtod.3 strtonum.3 \ 38 qabs.3 qdiv.3 qsort.3 radixsort.3 rand48.3 rand.3 random.3 realpath.3 \
39 strtol.3 strtoul.3 system.3 tsearch.3 39 strtod.3 strtonum.3 strtol.3 strtoul.3 system.3 tsearch.3
40 40
41MLINKS+=exit.3 _Exit.3 41MLINKS+=exit.3 _Exit.3
42MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3 42MLINKS+=ecvt.3 fcvt.3 ecvt.3 gcvt.3
@@ -56,6 +56,7 @@ MLINKS+=random.3 srandom.3 random.3 srandomdev.3
56MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3 56MLINKS+=rand48.3 drand48.3 rand48.3 erand48.3 rand48.3 lrand48.3
57MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3 57MLINKS+=rand48.3 mrand48.3 rand48.3 nrand48.3 rand48.3 jrand48.3
58MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3 58MLINKS+=rand48.3 srand48.3 rand48.3 seed48.3 rand48.3 lcong48.3
59MLINKS+=ptsname.3 grantpt.3 ptsname.3 unlockpt.3
59MLINKS+=strtod.3 strtof.3 strtod.3 strtold.3 60MLINKS+=strtod.3 strtof.3 strtod.3 strtold.3
60MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3 61MLINKS+=strtol.3 strtoll.3 strtol.3 strtoq.3 strtol.3 strtoimax.3
61MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3 62MLINKS+=strtoul.3 strtoull.3 strtoul.3 strtouq.3 strtoul.3 strtoumax.3
diff --git a/src/lib/libc/stdlib/posix_openpt.3 b/src/lib/libc/stdlib/posix_openpt.3
new file mode 100644
index 0000000000..d292ac0a5f
--- /dev/null
+++ b/src/lib/libc/stdlib/posix_openpt.3
@@ -0,0 +1,128 @@
1.\" $OpenBSD: posix_openpt.3,v 1.1 2012/12/03 20:08:33 millert Exp $
2.\"
3.\" Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
4.\" All rights reserved.
5.\"
6.\" Portions of this software were developed under sponsorship from Snow
7.\" B.V., the Netherlands.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\" notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\" notice, this list of conditions and the following disclaimer in the
16.\" documentation and/or other materials provided with the distribution.
17.\"
18.\" THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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.\" Portions of this text are reprinted and reproduced in electronic form
31.\" from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology --
32.\" Portable Operating System Interface (POSIX), The Open Group Base
33.\" Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of
34.\" Electrical and Electronics Engineers, Inc and The Open Group. In the
35.\" event of any discrepancy between this version and the original IEEE and
36.\" The Open Group Standard, the original IEEE and The Open Group Standard is
37.\" the referee document. The original Standard can be obtained online at
38.\" http://www.opengroup.org/unix/online.html.
39.\"
40.\" $FreeBSD: head/lib/libc/sys/posix_openpt.2 181905 2008-08-20 08:31:58Z ed $
41.\"
42.Dd $Mdocdate: December 3 2012 $
43.Dt POSIX_OPENPT 3
44.Os
45.Sh NAME
46.Nm posix_openpt
47.Nd "open a pseudo-terminal device"
48.Sh SYNOPSIS
49.In stdlib.h
50.In fcntl.h
51.Ft int
52.Fn posix_openpt "int oflag"
53.Sh DESCRIPTION
54The
55.Fn posix_openpt
56function allocates a new pseudo-terminal and establishes a connection
57with its master device.
58After the pseudo-terminal has been allocated, the slave device should
59have the proper permissions before it can be used (see
60.Xr grantpt 3 ) .
61The name of the slave device can be determined by calling
62.Xr ptsname 3 .
63.Pp
64The file status flags and file access modes of the open file description
65shall be set according to the value of
66.Fa oflag .
67Values for
68.Fa oflag
69are constructed by a bitwise-inclusive OR of flags from the following
70list, defined in
71.In fcntl.h :
72.Bl -tag -width ".Dv O_NOCTTY"
73.It Dv O_RDWR
74Open for reading and writing.
75.It Dv O_NOCTTY
76If set
77.Fn posix_openpt
78shall not cause the terminal device to become the controlling terminal
79for the process.
80.El
81.Pp
82The
83.Fn posix_openpt
84function shall fail when
85.Fa oflag
86contains other values.
87.Sh RETURN VALUES
88Upon successful completion, the
89.Fn posix_openpt
90function shall allocate a new pseudo-terminal device and return a
91non-negative integer representing a file descriptor, which is connected
92to its master device.
93Otherwise, -1 shall be returned and errno set to indicate the error.
94.Sh ERRORS
95The
96.Fn posix_openpt
97function shall fail if:
98.Bl -tag -width Er
99.It Bq Er EMFILE
100The per-process descriptor table is full.
101.It Bq Er ENFILE
102The system file table is full.
103.It Bq Er EINVAL
104The value of
105.Fa oflag
106is not valid.
107.El
108.Sh SEE ALSO
109.Xr ptsname 3 ,
110.Xr pty 4 ,
111.Xr tty 4
112.Sh STANDARDS
113The
114.Fn posix_openpt
115function conforms to
116.St -p1003.1-2001 .
117.Sh HISTORY
118The
119.Fn posix_openpt
120function appeared in
121.Ox 5.3 .
122.Sh CAVEATS
123The flag
124.Dv O_NOCTTY
125is included for compatibility; in
126.Ox ,
127opening a terminal does not cause it to become a process's controlling
128terminal.
diff --git a/src/lib/libc/stdlib/posix_pty.c b/src/lib/libc/stdlib/posix_pty.c
new file mode 100644
index 0000000000..a2025ddbb6
--- /dev/null
+++ b/src/lib/libc/stdlib/posix_pty.c
@@ -0,0 +1,119 @@
1/* $OpenBSD: posix_pty.c,v 1.1 2012/12/03 20:08:33 millert Exp $ */
2
3/*
4 * Copyright (c) 2012 Todd C. Miller <Todd.Miller@courtesan.com>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19#include <sys/types.h>
20#include <sys/ioctl.h>
21#include <sys/stat.h>
22#include <sys/tty.h>
23#include <errno.h>
24#include <fcntl.h>
25#include <paths.h>
26#include <stddef.h>
27#include <stdlib.h>
28#include <string.h>
29#include <unistd.h>
30
31int
32posix_openpt(int oflag)
33{
34 struct ptmget ptm;
35 int fd, mfd = -1;
36
37 /* User must specify O_RDWR in oflag. */
38 if (!(oflag & O_RDWR)) {
39 errno = EINVAL;
40 return -1;
41 }
42
43 /* Get pty master and slave (this API only uses the master). */
44 fd = open(PATH_PTMDEV, O_RDWR);
45 if (fd != -1) {
46 if (ioctl(fd, PTMGET, &ptm) != -1) {
47 close(ptm.sfd);
48 mfd = ptm.cfd;
49 }
50 close(fd);
51 }
52
53 return mfd;
54}
55
56/*
57 * Look up the name of the specified pty master fd.
58 * Note that the name returned does *not* include the /dev/ prefix.
59 * Returns the name on success and NULL on error, setting errno.
60 */
61static const char *
62ptmname(int mfd)
63{
64 struct stat sb;
65 const char *name;
66
67 /* Make sure it is a pty master. */
68 if (fstat(mfd, &sb) != 0)
69 return NULL;
70 if (!S_ISCHR(sb.st_mode)) {
71 errno = EINVAL;
72 return NULL;
73 }
74 name = devname(sb.st_rdev, S_IFCHR);
75 if (strncmp(name, "pty", 3) != 0) {
76 errno = EINVAL;
77 return NULL;
78 }
79 return name;
80}
81
82/*
83 * The PTMGET ioctl handles the mode and owner for us.
84 */
85int
86grantpt(int mfd)
87{
88 return ptmname(mfd) ? 0 : -1;
89}
90
91/*
92 * The PTMGET ioctl unlocks the pty master and slave for us.
93 */
94int
95unlockpt(int mfd)
96{
97 return ptmname(mfd) ? 0 : -1;
98}
99
100/*
101 * Look up the path of the slave pty that corresponds to the master fd.
102 * Returns the path if successful or NULL on error.
103 */
104char *
105ptsname(int mfd)
106{
107 const char *master;
108 static char slave[sizeof(((struct ptmget *)NULL)->sn)];
109
110 if ((master = ptmname(mfd)) == NULL)
111 return NULL;
112
113 /* Add /dev/ prefix and convert "pty" to "tty". */
114 strlcpy(slave, _PATH_DEV, sizeof(slave));
115 strlcat(slave, master, sizeof(slave));
116 slave[sizeof(_PATH_DEV) - 1] = 't';
117
118 return slave;
119}
diff --git a/src/lib/libc/stdlib/ptsname.3 b/src/lib/libc/stdlib/ptsname.3
new file mode 100644
index 0000000000..3d7cd979d5
--- /dev/null
+++ b/src/lib/libc/stdlib/ptsname.3
@@ -0,0 +1,160 @@
1.\" $OpenBSD: ptsname.3,v 1.1 2012/12/03 20:08:33 millert Exp $
2.\"
3.\" Copyright (c) 2002 The FreeBSD Project, Inc.
4.\" All rights reserved.
5.\"
6.\" This software includes code contributed to the FreeBSD Project
7.\" by Ryan Younce of North Carolina State University.
8.\"
9.\" Redistribution and use in source and binary forms, with or without
10.\" modification, are permitted provided that the following conditions
11.\" are met:
12.\" 1. Redistributions of source code must retain the above copyright
13.\" notice, this list of conditions and the following disclaimer.
14.\" 2. Redistributions in binary form must reproduce the above copyright
15.\" notice, this list of conditions and the following disclaimer in the
16.\" documentation and/or other materials provided with the distribution.
17.\" 3. Neither the name of the FreeBSD Project nor the names of its
18.\" contributors may be used to endorse or promote products derived from
19.\" this software without specific prior written permission.
20.\"
21.\" THIS SOFTWARE IS PROVIDED BY THE FREEBSD PROJECT AND CONTRIBUTORS
22.\" ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23.\" LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
24.\" PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FREEBSD PROJECT
25.\" OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26.\" SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
27.\" TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28.\" PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29.\" LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30.\" NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31.\" SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32.\"
33.\" $FreeBSD: head/lib/libc/stdlib/ptsname.3 240412 2012-09-12 17:54:09Z emaste $
34.\"
35.Dd $Mdocdate: December 3 2012 $
36.Dt PTSNAME 3
37.Os
38.Sh NAME
39.Nm grantpt ,
40.Nm ptsname ,
41.Nm unlockpt
42.Nd pseudo-terminal access functions
43.Sh SYNOPSIS
44.In stdlib.h
45.Ft int
46.Fn grantpt "int fildes"
47.Ft "char *"
48.Fn ptsname "int fildes"
49.Ft int
50.Fn unlockpt "int fildes"
51.Sh DESCRIPTION
52The
53.Fn grantpt ,
54.Fn ptsname ,
55and
56.Fn unlockpt
57functions allow access to pseudo-terminal devices.
58These three functions accept a file descriptor that references the
59master half of a pseudo-terminal pair.
60This file descriptor is created with
61.Xr posix_openpt 3 .
62.Pp
63The
64.Fn grantpt
65function is used to establish ownership and permissions
66of the slave device counterpart to the master device
67specified with
68.Fa fildes .
69The slave device's ownership is set to the real user ID
70of the calling process, and the permissions are set to
71user readable-writable and group writable.
72The group owner of the slave device is also set to the
73group
74.Dq Li tty .
75.Pp
76The
77.Fn ptsname
78function returns the full pathname of the slave device
79counterpart to the master device specified with
80.Fa fildes .
81This value can be used
82to subsequently open the appropriate slave after
83.Xr posix_openpt 3
84and
85.Fn grantpt
86have been called.
87.Pp
88The
89.Fn unlockpt
90function clears the lock held on the pseudo-terminal pair
91for the master device specified with
92.Fa fildes .
93.Sh RETURN VALUES
94.Rv -std grantpt unlockpt
95.Pp
96The
97.Fn ptsname
98function returns a pointer to the name
99of the slave device on success; otherwise a
100.Dv NULL
101pointer is returned.
102.Sh ERRORS
103The
104.Fn grantpt ,
105.Fn ptsname
106and
107.Fn unlockpt
108functions may fail and set
109.Va errno
110to:
111.Bl -tag -width Er
112.It Bq Er EBADF
113.Fa fildes
114is not a valid open file descriptor.
115.It Bq Er EINVAL
116.Fa fildes
117is not a master pseudo-terminal device.
118.El
119.Pp
120In addition, the
121.Fn grantpt
122function may set
123.Va errno
124to:
125.Bl -tag -width Er
126.It Bq Er EACCES
127The slave pseudo-terminal device could not be accessed.
128.El
129.Sh SEE ALSO
130.Xr posix_openpt 3 ,
131.Xr pty 4 ,
132.Xr tty 4
133.Sh STANDARDS
134The
135.Fn ptsname
136function conforms to
137.St -p1003.1-2008 .
138.Pp
139This implementation of
140.Fn grantpt
141and
142.Fn unlockpt
143does not conform to
144.St -p1003.1-2008 ,
145because it depends on
146.Xr posix_openpt 3
147to create the pseudo-terminal device with proper permissions in place.
148It only validates whether
149.Fa fildes
150is a valid pseudo-terminal master device.
151Future revisions of the specification will likely allow this behaviour,
152as stated by the Austin Group.
153.Sh HISTORY
154The
155.Fn grantpt ,
156.Fn ptsname
157and
158.Fn unlockpt
159functions appeared in
160.Ox 5.3 .