FreeBSD/Linux Kernel Cross Reference
sys/kern/tty_bsdpty.c
1 /* $NetBSD: tty_bsdpty.c,v 1.14 2008/04/28 20:24:05 martin 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 *
16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 __KERNEL_RCSID(0, "$NetBSD: tty_bsdpty.c,v 1.14 2008/04/28 20:24:05 martin Exp $");
31
32 #include "opt_ptm.h"
33
34 #ifdef COMPAT_BSDPTY
35 /* bsd tty implementation for pty multiplexor driver /dev/ptm{,x} */
36
37 #include <sys/param.h>
38 #include <sys/systm.h>
39 #include <sys/ioctl.h>
40 #include <sys/lwp.h>
41 #include <sys/tty.h>
42 #include <sys/stat.h>
43 #include <sys/file.h>
44 #include <sys/uio.h>
45 #include <sys/kernel.h>
46 #include <sys/vnode.h>
47 #include <sys/namei.h>
48 #include <sys/signalvar.h>
49 #include <sys/filedesc.h>
50 #include <sys/conf.h>
51 #include <sys/poll.h>
52 #include <sys/malloc.h>
53 #include <sys/pty.h>
54 #include <sys/kauth.h>
55
56 /*
57 * pts == /dev/tty[pqrs]?
58 * ptc == /dev/pty[pqrs]?
59 */
60
61 /*
62 * All this hard-coding is really evil.
63 */
64 #define TTY_GID 4
65 #define TTY_PERM (S_IRUSR|S_IWUSR|S_IWGRP)
66 #define TTY_TEMPLATE "/dev/XtyXX"
67 #define TTY_NAMESIZE sizeof(TTY_TEMPLATE)
68 #define TTY_LETTERS "pqrstuvwxyzPQRST"
69 #define TTY_OLD_SUFFIX "0123456789abcdef"
70 #define TTY_NEW_SUFFIX "ghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
71
72 static int pty_makename(struct ptm_pty *, struct lwp *, char *, size_t, dev_t,
73 char);
74 static int pty_allocvp(struct ptm_pty *, struct lwp *, struct vnode **,
75 dev_t, char);
76 static void pty_getvattr(struct ptm_pty *, struct lwp *, struct vattr *);
77
78 struct ptm_pty ptm_bsdpty = {
79 pty_allocvp,
80 pty_makename,
81 pty_getvattr,
82 NULL
83 };
84
85 static int
86 /*ARGSUSED*/
87 pty_makename(struct ptm_pty *ptm, struct lwp *l, char *bf,
88 size_t bufsiz, dev_t dev, char c)
89 {
90 size_t nt;
91 dev_t minor = minor(dev);
92 const char *suffix;
93
94 if (bufsiz < TTY_NAMESIZE)
95 return EINVAL;
96
97 (void)memcpy(bf, TTY_TEMPLATE, TTY_NAMESIZE);
98
99 if (minor < 256) {
100 suffix = TTY_OLD_SUFFIX;
101 nt = sizeof(TTY_OLD_SUFFIX) - 1;
102 } else {
103 minor -= 256;
104 suffix = TTY_NEW_SUFFIX;
105 nt = sizeof(TTY_NEW_SUFFIX) - 1;
106 }
107
108 bf[5] = c;
109 bf[8] = TTY_LETTERS[minor / nt];
110 bf[9] = suffix[minor % nt];
111 return 0;
112 }
113
114
115 static int
116 /*ARGSUSED*/
117 pty_allocvp(struct ptm_pty *ptm, struct lwp *l, struct vnode **vp, dev_t dev,
118 char ms)
119 {
120 int error;
121 struct nameidata nd;
122 char name[TTY_NAMESIZE];
123
124 error = (*ptm->makename)(ptm, l, name, sizeof(name), dev, ms);
125 if (error)
126 return error;
127
128 NDINIT(&nd, LOOKUP, NOFOLLOW|LOCKLEAF, UIO_SYSSPACE, name);
129 if ((error = namei(&nd)) != 0)
130 return error;
131 *vp = nd.ni_vp;
132 return 0;
133 }
134
135
136 static void
137 /*ARGSUSED*/
138 pty_getvattr(struct ptm_pty *ptm, struct lwp *l, struct vattr *vattr)
139 {
140 VATTR_NULL(vattr);
141 /* get real uid */
142 vattr->va_uid = kauth_cred_getuid(l->l_cred);
143 vattr->va_gid = TTY_GID;
144 vattr->va_mode = TTY_PERM;
145 }
146 #endif
Cache object: d26166b3517ddefd95ccff94a8c4f903
|