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