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