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/snoop.h> 39 #include <sys/sx.h> 40 #include <sys/systm.h> 41 #include <sys/tty.h> 42 #include <sys/uio.h> 43 44 static struct cdev *snp_dev; 45 /* XXX: should be mtx, but TTY can be locked by Giant. */ 46 static struct sx snp_register_lock; 47 SX_SYSINIT(snp_register_lock, &snp_register_lock, 48 "tty snoop registration"); 49 static MALLOC_DEFINE(M_SNP, "snp", "tty snoop device"); 50 51 /* 52 * There is no need to have a big input buffer. In most typical setups, 53 * we won't inject much data into the TTY, because users can't type 54 * really fast. 55 */ 56 #define SNP_INPUT_BUFSIZE 16 57 /* 58 * The output buffer has to be really big. Right now we don't support 59 * any form of flow control, which means we lost any data we can't 60 * accept. We set the output buffer size to about twice the size of a 61 * pseudo-terminal/virtual console's output buffer. 62 */ 63 #define SNP_OUTPUT_BUFSIZE 16384 64 65 static d_open_t snp_open; 66 static d_read_t snp_read; 67 static d_write_t snp_write; 68 static d_ioctl_t snp_ioctl; 69 static d_poll_t snp_poll; 70 71 static struct cdevsw snp_cdevsw = { 72 .d_version = D_VERSION, 73 .d_open = snp_open, 74 .d_read = snp_read, 75 .d_write = snp_write, 76 .d_ioctl = snp_ioctl, 77 .d_poll = snp_poll, 78 .d_name = "snp", 79 }; 80 81 static th_getc_capture_t snp_getc_capture; 82 83 static struct ttyhook snp_hook = { 84 .th_getc_capture = snp_getc_capture, 85 }; 86 87 /* 88 * Per-instance structure. 89 * 90 * List of locks 91 * (r) locked by snp_register_lock on assignment 92 * (t) locked by tty_lock 93 */ 94 struct snp_softc { 95 struct tty *snp_tty; /* (r) TTY we're snooping. */ 96 struct ttyoutq snp_outq; /* (t) Output queue. */ 97 struct cv snp_outwait; /* (t) Output wait queue. */ 98 struct selinfo snp_outpoll; /* (t) Output polling. */ 99 }; 100 101 static void 102 snp_dtor(void *data) 103 { 104 struct snp_softc *ss = data; 105 struct tty *tp; 106 107 tp = ss->snp_tty; 108 if (tp != NULL) { 109 tty_lock(tp); 110 ttyoutq_free(&ss->snp_outq); 111 ttyhook_unregister(tp); 112 } 113 114 cv_destroy(&ss->snp_outwait); 115 free(ss, M_SNP); 116 } 117 118 /* 119 * Snoop device node routines. 120 */ 121 122 static int 123 snp_open(struct cdev *dev, int flag, int mode, struct thread *td) 124 { 125 struct snp_softc *ss; 126 127 /* Allocate per-snoop data. */ 128 ss = malloc(sizeof(struct snp_softc), M_SNP, M_WAITOK|M_ZERO); 129 ttyoutq_init(&ss->snp_outq); 130 cv_init(&ss->snp_outwait, "snp out"); 131 132 devfs_set_cdevpriv(ss, snp_dtor); 133 134 return (0); 135 } 136 137 static int 138 snp_read(struct cdev *dev, struct uio *uio, int flag) 139 { 140 int error, oresid = uio->uio_resid; 141 struct snp_softc *ss; 142 struct tty *tp; 143 144 if (uio->uio_resid == 0) 145 return (0); 146 147 error = devfs_get_cdevpriv((void **)&ss); 148 if (error != 0) 149 return (error); 150 151 tp = ss->snp_tty; 152 if (tp == NULL || tty_gone(tp)) 153 return (EIO); 154 155 tty_lock(tp); 156 for (;;) { 157 error = ttyoutq_read_uio(&ss->snp_outq, tp, uio); 158 if (error != 0 || uio->uio_resid != oresid) 159 break; 160 161 /* Wait for more data. */ 162 if (flag & O_NONBLOCK) { 163 error = EWOULDBLOCK; 164 break; 165 } 166 error = cv_wait_sig(&ss->snp_outwait, tp->t_mtx); 167 if (error != 0) 168 break; 169 if (tty_gone(tp)) { 170 error = EIO; 171 break; 172 } 173 } 174 tty_unlock(tp); 175 176 return (error); 177 } 178 179 static int 180 snp_write(struct cdev *dev, struct uio *uio, int flag) 181 { 182 struct snp_softc *ss; 183 struct tty *tp; 184 int error, len, i; 185 char in[SNP_INPUT_BUFSIZE]; 186 187 error = devfs_get_cdevpriv((void **)&ss); 188 if (error != 0) 189 return (error); 190 191 tp = ss->snp_tty; 192 if (tp == NULL || tty_gone(tp)) 193 return (EIO); 194 195 while (uio->uio_resid > 0) { 196 /* Read new data. */ 197 len = imin(uio->uio_resid, sizeof in); 198 error = uiomove(in, len, uio); 199 if (error != 0) 200 return (error); 201 202 tty_lock(tp); 203 204 /* Driver could have abandoned the TTY in the mean time. */ 205 if (tty_gone(tp)) { 206 tty_unlock(tp); 207 return (ENXIO); 208 } 209 210 /* 211 * Deliver data to the TTY. Ignore errors for now, 212 * because we shouldn't bail out when we're running 213 * close to the watermarks. 214 */ 215 if (ttydisc_can_bypass(tp)) { 216 ttydisc_rint_bypass(tp, in, len); 217 } else { 218 for (i = 0; i < len; i++) 219 ttydisc_rint(tp, in[i], 0); 220 } 221 222 ttydisc_rint_done(tp); 223 tty_unlock(tp); 224 } 225 226 return (0); 227 } 228 229 static int 230 snp_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int flags, 231 struct thread *td) 232 { 233 struct snp_softc *ss; 234 struct tty *tp; 235 int error; 236 237 error = devfs_get_cdevpriv((void **)&ss); 238 if (error != 0) 239 return (error); 240 241 switch (cmd) { 242 case SNPSTTY: 243 /* Bind TTY to snoop instance. */ 244 sx_xlock(&snp_register_lock); 245 if (ss->snp_tty != NULL) { 246 sx_xunlock(&snp_register_lock); 247 return (EBUSY); 248 } 249 error = ttyhook_register(&ss->snp_tty, td, *(int *)data, 250 &snp_hook, ss); 251 sx_xunlock(&snp_register_lock); 252 if (error != 0) 253 return (error); 254 255 /* Now that went okay, allocate a buffer for the queue. */ 256 tp = ss->snp_tty; 257 tty_lock(tp); 258 ttyoutq_setsize(&ss->snp_outq, tp, SNP_OUTPUT_BUFSIZE); 259 tty_unlock(tp); 260 261 return (0); 262 case SNPGTTY: 263 /* Obtain device number of associated TTY. */ 264 if (ss->snp_tty == NULL) 265 *(dev_t *)data = NODEV; 266 else 267 *(dev_t *)data = tty_udev(ss->snp_tty); 268 return (0); 269 case FIONREAD: 270 tp = ss->snp_tty; 271 if (tp != NULL) { 272 tty_lock(tp); 273 *(int *)data = ttyoutq_bytesused(&ss->snp_outq); 274 tty_unlock(tp); 275 } else { 276 *(int *)data = 0; 277 } 278 return (0); 279 default: 280 return (ENOTTY); 281 } 282 } 283 284 static int 285 snp_poll(struct cdev *dev, int events, struct thread *td) 286 { 287 struct snp_softc *ss; 288 struct tty *tp; 289 int revents; 290 291 if (devfs_get_cdevpriv((void **)&ss) != 0) 292 return (events & 293 (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM)); 294 295 revents = 0; 296 297 if (events & (POLLIN | POLLRDNORM)) { 298 tp = ss->snp_tty; 299 if (tp != NULL) { 300 tty_lock(tp); 301 if (ttyoutq_bytesused(&ss->snp_outq) > 0) 302 revents |= events & (POLLIN | POLLRDNORM); 303 tty_unlock(tp); 304 } 305 } 306 307 if (revents == 0) 308 selrecord(td, &ss->snp_outpoll); 309 310 return (revents); 311 } 312 313 /* 314 * TTY hook events. 315 */ 316 317 static int 318 snp_modevent(module_t mod, int type, void *data) 319 { 320 321 switch (type) { 322 case MOD_LOAD: 323 snp_dev = make_dev(&snp_cdevsw, 0, 324 UID_ROOT, GID_WHEEL, 0600, "snp"); 325 return (0); 326 case MOD_UNLOAD: 327 /* XXX: Make existing users leave. */ 328 destroy_dev(snp_dev); 329 return (0); 330 default: 331 return (EOPNOTSUPP); 332 } 333 } 334 335 static void 336 snp_getc_capture(struct tty *tp, const void *buf, size_t len) 337 { 338 struct snp_softc *ss = ttyhook_softc(tp); 339 340 ttyoutq_write(&ss->snp_outq, buf, len); 341 342 cv_broadcast(&ss->snp_outwait); 343 selwakeup(&ss->snp_outpoll); 344 } 345 346 static moduledata_t snp_mod = { 347 "snp", 348 snp_modevent, 349 NULL 350 }; 351 352 DECLARE_MODULE(snp, snp_mod, SI_SUB_DRIVERS, SI_ORDER_MIDDLE); 353