FreeBSD/Linux Kernel Cross Reference
sys/kern/tty_bsdpty.c
1 /* $NetBSD: tty_bsdpty.c,v 1.11 2006/11/01 10:17:59 yamt 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.11 2006/11/01 10:17:59 yamt 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/lwp.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 #include <sys/kauth.h>
63
64 /*
65 * pts == /dev/tty[pqrs]?
66 * ptc == /dev/pty[pqrs]?
67 */
68
69 /*
70 * All this hard-coding is really evil.
71 */
72 #define TTY_GID 4
73 #define TTY_PERM (S_IRUSR|S_IWUSR|S_IWGRP)
74 #define TTY_TEMPLATE "/dev/XtyXX"
75 #define TTY_NAMESIZE sizeof(TTY_TEMPLATE)
76 #define TTY_LETTERS "pqrstuvwxyzPQRST"
77 #define TTY_OLD_SUFFIX "0123456789abcdef"
78 #define TTY_NEW_SUFFIX "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
79
80 static int pty_makename(struct ptm_pty *, struct lwp *, char *, size_t, dev_t,
81 char);
82 static int pty_allocvp(struct ptm_pty *, struct lwp *, struct vnode **,
83 dev_t, char);
84 static void pty_getvattr(struct ptm_pty *, struct lwp *, struct vattr *);
85
86 struct ptm_pty ptm_bsdpty = {
87 pty_allocvp,
88 pty_makename,
89 pty_getvattr,
90 NULL
91 };
92
93 static int
94 /*ARGSUSED*/
95 pty_makename(struct ptm_pty *ptm, struct lwp *l, char *bf,
96 size_t bufsiz, dev_t dev, char c)
97 {
98 size_t nt;
99 dev_t minor = minor(dev);
100 const char *suffix;
101
102 if (bufsiz < TTY_NAMESIZE)
103 return EINVAL;
104
105 (void)memcpy(bf, TTY_TEMPLATE, TTY_NAMESIZE);
106
107 if (minor < 256) {
108 suffix = TTY_OLD_SUFFIX;
109 nt = sizeof(TTY_OLD_SUFFIX) - 1;
110 } else {
111 minor -= 256;
112 suffix = TTY_NEW_SUFFIX;
113 nt = sizeof(TTY_NEW_SUFFIX) - 1;
114 }
115
116 bf[5] = c;
117 bf[8] = TTY_LETTERS[minor / nt];
118 bf[9] = suffix[minor % nt];
119 return 0;
120 }
121
122
123 static int
124 /*ARGSUSED*/
125 pty_allocvp(struct ptm_pty *ptm, struct lwp *l, struct vnode **vp, dev_t dev,
126 char ms)
127 {
128 int error;
129 struct nameidata nd;
130 char name[TTY_NAMESIZE];
131
132 error = (*ptm->makename)(ptm, l, name, sizeof(name), dev, ms);
133 if (error)
134 return error;
135
136 NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, name, l);
137 if ((error = namei(&nd)) != 0)
138 return error;
139 *vp = nd.ni_vp;
140 return 0;
141 }
142
143
144 static void
145 /*ARGSUSED*/
146 pty_getvattr(struct ptm_pty *ptm, struct lwp *l, struct vattr *vattr)
147 {
148 VATTR_NULL(vattr);
149 /* get real uid */
150 vattr->va_uid = kauth_cred_getuid(l->l_cred);
151 vattr->va_gid = TTY_GID;
152 vattr->va_mode = TTY_PERM;
153 }
154 #endif
Cache object: fd939aa134eb84eab404362df62ea9a4
|