tty_bsdpty.c revision 1.4 1 /* $NetBSD: tty_bsdpty.c,v 1.4 2005/05/29 22:24:15 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.4 2005/05/29 22:24:15 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 /*
69 * All this hard-coding is really evil.
70 */
71 #define TTY_GID 4
72 #define TTY_PERM (S_IRUSR|S_IWUSR|S_IWGRP)
73 #define TTY_TEMPLATE "/dev/XtyXX"
74 #define TTY_NAMESIZE sizeof(TTY_TEMPLATE)
75 #define TTY_LETTERS "pqrstuvwxyzPQRST"
76 #define TTY_OLD_SUFFIX "0123456789abcdef"
77 #define TTY_NEW_SUFFIX "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
78
79 static int pty_makename(struct ptm_pty *, char *, size_t, dev_t, char);
80 static int pty_allocvp(struct ptm_pty *, struct proc *, struct vnode **,
81 dev_t, char);
82 static void pty_getvattr(struct ptm_pty *, struct proc *, struct vattr *);
83
84 struct ptm_pty ptm_bsdpty = {
85 pty_allocvp,
86 pty_makename,
87 pty_getvattr,
88 NULL
89 };
90
91 static int
92 /*ARGSUSED*/
93 pty_makename(struct ptm_pty *ptm, char *bf, size_t bufsiz, dev_t dev, char c)
94 {
95 size_t nt;
96 dev_t minor = minor(dev);
97 if (bufsiz < TTY_NAMESIZE)
98 return EINVAL;
99 (void)memcpy(bf, TTY_TEMPLATE, TTY_NAMESIZE);
100
101 bf[5] = c;
102
103 if (minor < 256) {
104 nt = sizeof(TTY_OLD_SUFFIX) - 1;
105 bf[8] = TTY_LETTERS[minor / nt];
106 bf[9] = TTY_OLD_SUFFIX[minor % nt];
107 } else {
108 minor -= 256;
109 nt = sizeof(TTY_NEW_SUFFIX) - sizeof(TTY_OLD_SUFFIX);
110 bf[8] = TTY_LETTERS[minor / nt];
111 bf[9] = TTY_NEW_SUFFIX[minor % nt];
112 }
113 return 0;
114 }
115
116
117 static int
118 /*ARGSUSED*/
119 pty_allocvp(struct ptm_pty *ptm, struct proc *p, struct vnode **vp, dev_t dev,
120 char ms)
121 {
122 int error;
123 struct nameidata nd;
124 char name[TTY_NAMESIZE];
125
126 error = (*ptm->makename)(ptm, name, sizeof(name), dev, ms);
127 if (error)
128 return error;
129
130 NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, name, p);
131 if ((error = namei(&nd)) != 0)
132 return error;
133 *vp = nd.ni_vp;
134 return 0;
135 }
136
137
138 static void
139 /*ARGSUSED*/
140 pty_getvattr(struct ptm_pty *ptm, struct proc *p, struct vattr *vattr)
141 {
142 VATTR_NULL(vattr);
143 /* get real uid */
144 vattr->va_uid = p->p_cred->p_ruid;
145 vattr->va_gid = TTY_GID;
146 vattr->va_mode = TTY_PERM;
147 }
148 #endif
149