xref: /netbsd-src/sys/kern/tty_bsdpty.c (revision 23c8222edbfb0f0932d88a8351d3a0cf817dfb9e)
1 /*	$NetBSD: tty_bsdpty.c,v 1.1 2004/11/10 17:29:54 christos Exp $	*/
2 
3 /*-
4  * Copyright (c) 2004 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 3. All advertising materials mentioning features or use of this software
16  *    must display the following acknowledgement:
17  *        This product includes software developed by the NetBSD
18  *        Foundation, Inc. and its contributors.
19  * 4. Neither the name of The NetBSD Foundation nor the names of its
20  *    contributors may be used to endorse or promote products derived
21  *    from this software without specific prior written permission.
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
24  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
25  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
27  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
30  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
31  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
32  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33  * POSSIBILITY OF SUCH DAMAGE.
34  */
35 
36 #include <sys/cdefs.h>
37 __KERNEL_RCSID(0, "$NetBSD: tty_bsdpty.c,v 1.1 2004/11/10 17:29:54 christos Exp $");
38 
39 #include "opt_ptm.h"
40 
41 #ifdef COMPAT_BSDPTY
42 /* bsd tty implementation for pty multiplexor driver /dev/ptm{,x} */
43 
44 #include <sys/param.h>
45 #include <sys/systm.h>
46 #include <sys/ioctl.h>
47 #include <sys/proc.h>
48 #include <sys/tty.h>
49 #include <sys/stat.h>
50 #include <sys/file.h>
51 #include <sys/uio.h>
52 #include <sys/kernel.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
55 #include <sys/signalvar.h>
56 #include <sys/uio.h>
57 #include <sys/filedesc.h>
58 #include <sys/conf.h>
59 #include <sys/poll.h>
60 #include <sys/malloc.h>
61 #include <sys/pty.h>
62 
63 /*
64  * pts == /dev/tty[pqrs]?
65  * ptc == /dev/pty[pqrs]?
66  */
67 
68 #define TTY_TEMPLATE	"/dev/XtyXX"
69 #define TTY_NAMESIZE	sizeof(TTY_TEMPLATE)
70 /* XXX this needs to come from somewhere sane, and work with MAKEDEV */
71 #define TTY_LETTERS	"pqrstuvwxyzPQRST"
72 #define TTY_OLD_SUFFIX  "0123456789abcdef"
73 #define TTY_NEW_SUFFIX  "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
74 
75 static int pty_makename(struct ptm_pty *, char *, size_t, dev_t, char);
76 static int pty_allocvp(struct ptm_pty *, struct proc *, struct vnode **,
77     dev_t, char);
78 
79 struct ptm_pty ptm_bsdpty = {
80 	pty_allocvp,
81 	pty_makename,
82 	NULL
83 };
84 
85 static int
86 /*ARGSUSED*/
87 pty_makename(struct ptm_pty *ptm, char *buf, size_t bufsiz, dev_t dev, char c)
88 {
89 	size_t nt;
90 	dev_t minor = minor(dev);
91 	if (bufsiz < TTY_NAMESIZE)
92 		return EINVAL;
93 	(void)memcpy(buf, TTY_TEMPLATE, TTY_NAMESIZE);
94 
95 	buf[5] = c;
96 
97 	if (minor < 256) {
98 		nt = sizeof(TTY_OLD_SUFFIX) - 1;
99 		buf[8] = TTY_LETTERS[minor / nt];
100 		buf[9] = TTY_OLD_SUFFIX[minor % nt];
101 	} else {
102 		minor -= 256;
103 		nt = sizeof(TTY_NEW_SUFFIX) - sizeof(TTY_OLD_SUFFIX);
104 		buf[8] = TTY_LETTERS[minor / nt];
105 		buf[9] = TTY_NEW_SUFFIX[minor % nt];
106 	}
107 	return 0;
108 }
109 
110 
111 static int
112 /*ARGSUSED*/
113 pty_allocvp(struct ptm_pty *ptm, struct proc *p, struct vnode **vp, dev_t dev,
114     char ms)
115 {
116 	int error;
117 	struct nameidata nd;
118 	char name[TTY_NAMESIZE];
119 
120 	error = (*ptm->makename)(ptm, name, sizeof(name), dev, ms);
121 	if (error)
122 		return error;
123 
124 	NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, name, p);
125 	if ((error = namei(&nd)) != 0)
126 		return error;
127 	*vp = nd.ni_vp;
128 	return 0;
129 }
130 #endif
131