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