xref: /freebsd/sys/dev/snp/snp.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*-
2  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/conf.h>
32 #include <sys/fcntl.h>
33 #include <sys/filio.h>
34 #include <sys/kernel.h>
35 #include <sys/malloc.h>
36 #include <sys/module.h>
37 #include <sys/poll.h>
38 #include <sys/proc.h>
39 #include <sys/snoop.h>
40 #include <sys/sx.h>
41 #include <sys/systm.h>
42 #include <sys/tty.h>
43 #include <sys/uio.h>
44 
45 static struct cdev	*snp_dev;
46 static MALLOC_DEFINE(M_SNP, "snp", "tty snoop device");
47 
48 /* XXX: should be mtx, but TTY can be locked by Giant. */
49 #if 0
50 static struct mtx	snp_register_lock;
51 MTX_SYSINIT(snp_register_lock, &snp_register_lock,
52     "tty snoop registration", MTX_DEF);
53 #define	SNP_LOCK()	mtx_lock(&snp_register_lock)
54 #define	SNP_UNLOCK()	mtx_unlock(&snp_register_lock)
55 #else
56 static struct sx	snp_register_lock;
57 SX_SYSINIT(snp_register_lock, &snp_register_lock,
58     "tty snoop registration");
59 #define	SNP_LOCK()	sx_xlock(&snp_register_lock)
60 #define	SNP_UNLOCK()	sx_xunlock(&snp_register_lock)
61 #endif
62 
63 /*
64  * There is no need to have a big input buffer. In most typical setups,
65  * we won't inject much data into the TTY, because users can't type
66  * really fast.
67  */
68 #define SNP_INPUT_BUFSIZE	16
69 /*
70  * The output buffer has to be really big. Right now we don't support
71  * any form of flow control, which means we lost any data we can't
72  * accept. We set the output buffer size to about twice the size of a
73  * pseudo-terminal/virtual console's output buffer.
74  */
75 #define SNP_OUTPUT_BUFSIZE	16384
76 
77 static d_open_t		snp_open;
78 static d_read_t		snp_read;
79 static d_write_t	snp_write;
80 static d_ioctl_t	snp_ioctl;
81 static d_poll_t		snp_poll;
82 
83 static struct cdevsw snp_cdevsw = {
84 	.d_version	= D_VERSION,
85 	.d_open		= snp_open,
86 	.d_read		= snp_read,
87 	.d_write	= snp_write,
88 	.d_ioctl	= snp_ioctl,
89 	.d_poll		= snp_poll,
90 	.d_name		= "snp",
91 };
92 
93 static th_getc_capture_t	snp_getc_capture;
94 
95 static struct ttyhook snp_hook = {
96 	.th_getc_capture	= snp_getc_capture,
97 };
98 
99 /*
100  * Per-instance structure.
101  *
102  * List of locks
103  * (r)	locked by snp_register_lock on assignment
104  * (t)	locked by tty_lock
105  */
106 struct snp_softc {
107 	struct tty	*snp_tty;	/* (r) TTY we're snooping. */
108 	struct ttyoutq	snp_outq;	/* (t) Output queue. */
109 	struct cv	snp_outwait;	/* (t) Output wait queue. */
110 	struct selinfo	snp_outpoll;	/* (t) Output polling. */
111 };
112 
113 static void
114 snp_dtor(void *data)
115 {
116 	struct snp_softc *ss = data;
117 	struct tty *tp;
118 
119 	tp = ss->snp_tty;
120 	if (tp != NULL) {
121 		tty_lock(tp);
122 		ttyoutq_free(&ss->snp_outq);
123 		ttyhook_unregister(tp);
124 	}
125 
126 	cv_destroy(&ss->snp_outwait);
127 	free(ss, M_SNP);
128 }
129 
130 /*
131  * Snoop device node routines.
132  */
133 
134 static int
135 snp_open(struct cdev *dev, int flag, int mode, struct thread *td)
136 {
137 	struct snp_softc *ss;
138 
139 	/* Allocate per-snoop data. */
140 	ss = malloc(sizeof(struct snp_softc), M_SNP, M_WAITOK|M_ZERO);
141 	cv_init(&ss->snp_outwait, "snp out");
142 
143 	devfs_set_cdevpriv(ss, snp_dtor);
144 
145 	return (0);
146 }
147 
148 static int
149 snp_read(struct cdev *dev, struct uio *uio, int flag)
150 {
151 	int error, oresid = uio->uio_resid;
152 	struct snp_softc *ss;
153 	struct tty *tp;
154 
155 	if (uio->uio_resid == 0)
156 		return (0);
157 
158 	error = devfs_get_cdevpriv((void **)&ss);
159 	if (error != 0)
160 		return (error);
161 
162 	tp = ss->snp_tty;
163 	if (tp == NULL || tty_gone(tp))
164 		return (EIO);
165 
166 	tty_lock(tp);
167 	for (;;) {
168 		error = ttyoutq_read_uio(&ss->snp_outq, tp, uio);
169 		if (error != 0 || uio->uio_resid != oresid)
170 			break;
171 
172 		/* Wait for more data. */
173 		if (flag & O_NONBLOCK) {
174 			error = EWOULDBLOCK;
175 			break;
176 		}
177 		error = cv_wait_sig(&ss->snp_outwait, tp->t_mtx);
178 		if (error != 0)
179 			break;
180 		if (tty_gone(tp)) {
181 			error = EIO;
182 			break;
183 		}
184 	}
185 	tty_unlock(tp);
186 
187 	return (error);
188 }
189 
190 static int
191 snp_write(struct cdev *dev, struct uio *uio, int flag)
192 {
193 	struct snp_softc *ss;
194 	struct tty *tp;
195 	int error, len, i;
196 	char in[SNP_INPUT_BUFSIZE];
197 
198 	error = devfs_get_cdevpriv((void **)&ss);
199 	if (error != 0)
200 		return (error);
201 
202 	tp = ss->snp_tty;
203 	if (tp == NULL || tty_gone(tp))
204 		return (EIO);
205 
206 	while (uio->uio_resid > 0) {
207 		/* Read new data. */
208 		len = imin(uio->uio_resid, sizeof in);
209 		error = uiomove(in, len, uio);
210 		if (error != 0)
211 			return (error);
212 
213 		tty_lock(tp);
214 
215 		/* Driver could have abandoned the TTY in the mean time. */
216 		if (tty_gone(tp)) {
217 			tty_unlock(tp);
218 			return (ENXIO);
219 		}
220 
221 		/*
222 		 * Deliver data to the TTY. Ignore errors for now,
223 		 * because we shouldn't bail out when we're running
224 		 * close to the watermarks.
225 		 */
226 		if (ttydisc_can_bypass(tp)) {
227 			ttydisc_rint_bypass(tp, in, len);
228 		} else {
229 			for (i = 0; i < len; i++)
230 				ttydisc_rint(tp, in[i], 0);
231 		}
232 
233 		ttydisc_rint_done(tp);
234 		tty_unlock(tp);
235 	}
236 
237 	return (0);
238 }
239 
240 static int
241 snp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags,
242     struct thread *td)
243 {
244 	struct snp_softc *ss;
245 	struct tty *tp;
246 	int error;
247 
248 	error = devfs_get_cdevpriv((void **)&ss);
249 	if (error != 0)
250 		return (error);
251 
252 	switch (cmd) {
253 	case SNPSTTY:
254 		/* Bind TTY to snoop instance. */
255 		SNP_LOCK();
256 		if (ss->snp_tty != NULL) {
257 			SNP_UNLOCK();
258 			return (EBUSY);
259 		}
260 		error = ttyhook_register(&ss->snp_tty, td->td_proc,
261 		    *(int *)data, &snp_hook, ss);
262 		SNP_UNLOCK();
263 		if (error != 0)
264 			return (error);
265 
266 		/* Now that went okay, allocate a buffer for the queue. */
267 		tp = ss->snp_tty;
268 		tty_lock(tp);
269 		ttyoutq_setsize(&ss->snp_outq, tp, SNP_OUTPUT_BUFSIZE);
270 		tty_unlock(tp);
271 
272 		return (0);
273 	case SNPGTTY:
274 		/* Obtain device number of associated TTY. */
275 		if (ss->snp_tty == NULL)
276 			*(dev_t *)data = NODEV;
277 		else
278 			*(dev_t *)data = tty_udev(ss->snp_tty);
279 		return (0);
280 	case FIONREAD:
281 		tp = ss->snp_tty;
282 		if (tp != NULL) {
283 			tty_lock(tp);
284 			*(int *)data = ttyoutq_bytesused(&ss->snp_outq);
285 			tty_unlock(tp);
286 		} else {
287 			*(int *)data = 0;
288 		}
289 		return (0);
290 	default:
291 		return (ENOTTY);
292 	}
293 }
294 
295 static int
296 snp_poll(struct cdev *dev, int events, struct thread *td)
297 {
298 	struct snp_softc *ss;
299 	struct tty *tp;
300 	int revents;
301 
302 	if (devfs_get_cdevpriv((void **)&ss) != 0)
303 		return (events &
304 		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
305 
306 	revents = 0;
307 
308 	if (events & (POLLIN | POLLRDNORM)) {
309 		tp = ss->snp_tty;
310 		if (tp != NULL) {
311 			tty_lock(tp);
312 			if (ttyoutq_bytesused(&ss->snp_outq) > 0)
313 				revents |= events & (POLLIN | POLLRDNORM);
314 			tty_unlock(tp);
315 		}
316 	}
317 
318 	if (revents == 0)
319 		selrecord(td, &ss->snp_outpoll);
320 
321 	return (revents);
322 }
323 
324 /*
325  * TTY hook events.
326  */
327 
328 static int
329 snp_modevent(module_t mod, int type, void *data)
330 {
331 
332 	switch (type) {
333 	case MOD_LOAD:
334 		snp_dev = make_dev(&snp_cdevsw, 0,
335 		    UID_ROOT, GID_WHEEL, 0600, "snp");
336 		return (0);
337 	case MOD_UNLOAD:
338 		/* XXX: Make existing users leave. */
339 		destroy_dev(snp_dev);
340 		return (0);
341 	default:
342 		return (EOPNOTSUPP);
343 	}
344 }
345 
346 static void
347 snp_getc_capture(struct tty *tp, const void *buf, size_t len)
348 {
349 	struct snp_softc *ss = ttyhook_softc(tp);
350 
351 	ttyoutq_write(&ss->snp_outq, buf, len);
352 
353 	cv_broadcast(&ss->snp_outwait);
354 	selwakeup(&ss->snp_outpoll);
355 }
356 
357 static moduledata_t snp_mod = {
358 	"snp",
359 	snp_modevent,
360 	NULL
361 };
362 
363 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE);
364