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