1 /*-
2 * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Portions of this software were developed under sponsorship from Snow
6 * B.V., the Netherlands.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #include "opt_capsicum.h"
34 #include "opt_compat.h"
35
36 #include <sys/param.h>
37 #include <sys/capsicum.h>
38 #include <sys/conf.h>
39 #include <sys/cons.h>
40 #include <sys/fcntl.h>
41 #include <sys/file.h>
42 #include <sys/filedesc.h>
43 #include <sys/filio.h>
44 #ifdef COMPAT_43TTY
45 #include <sys/ioctl_compat.h>
46 #endif /* COMPAT_43TTY */
47 #include <sys/kernel.h>
48 #include <sys/limits.h>
49 #include <sys/malloc.h>
50 #include <sys/mount.h>
51 #include <sys/poll.h>
52 #include <sys/priv.h>
53 #include <sys/proc.h>
54 #include <sys/serial.h>
55 #include <sys/signal.h>
56 #include <sys/stat.h>
57 #include <sys/sx.h>
58 #include <sys/sysctl.h>
59 #include <sys/systm.h>
60 #include <sys/tty.h>
61 #include <sys/ttycom.h>
62 #define TTYDEFCHARS
63 #include <sys/ttydefaults.h>
64 #undef TTYDEFCHARS
65 #include <sys/ucred.h>
66 #include <sys/vnode.h>
67
68 #include <machine/stdarg.h>
69
70 static MALLOC_DEFINE(M_TTY, "tty", "tty device");
71
72 static void tty_rel_free(struct tty *tp);
73
74 static TAILQ_HEAD(, tty) tty_list = TAILQ_HEAD_INITIALIZER(tty_list);
75 static struct sx tty_list_sx;
76 SX_SYSINIT(tty_list, &tty_list_sx, "tty list");
77 static unsigned int tty_list_count = 0;
78
79 /* Character device of /dev/console. */
80 static struct cdev *dev_console;
81 static const char *dev_console_filename;
82
83 /*
84 * Flags that are supported and stored by this implementation.
85 */
86 #define TTYSUP_IFLAG (IGNBRK|BRKINT|IGNPAR|PARMRK|INPCK|ISTRIP|\
87 INLCR|IGNCR|ICRNL|IXON|IXOFF|IXANY|IMAXBEL)
88 #define TTYSUP_OFLAG (OPOST|ONLCR|TAB3|ONOEOT|OCRNL|ONOCR|ONLRET)
89 #define TTYSUP_LFLAG (ECHOKE|ECHOE|ECHOK|ECHO|ECHONL|ECHOPRT|\
90 ECHOCTL|ISIG|ICANON|ALTWERASE|IEXTEN|TOSTOP|\
91 FLUSHO|NOKERNINFO|NOFLSH)
92 #define TTYSUP_CFLAG (CIGNORE|CSIZE|CSTOPB|CREAD|PARENB|PARODD|\
93 HUPCL|CLOCAL|CCTS_OFLOW|CRTS_IFLOW|CDTR_IFLOW|\
94 CDSR_OFLOW|CCAR_OFLOW)
95
96 #define TTY_CALLOUT(tp,d) (dev2unit(d) & TTYUNIT_CALLOUT)
97
98 /*
99 * Set TTY buffer sizes.
100 */
101
102 #define TTYBUF_MAX 65536
103
104 static void
tty_watermarks(struct tty * tp)105 tty_watermarks(struct tty *tp)
106 {
107 size_t bs = 0;
108
109 /* Provide an input buffer for 0.2 seconds of data. */
110 if (tp->t_termios.c_cflag & CREAD)
111 bs = MIN(tp->t_termios.c_ispeed / 5, TTYBUF_MAX);
112 ttyinq_setsize(&tp->t_inq, tp, bs);
113
114 /* Set low watermark at 10% (when 90% is available). */
115 tp->t_inlow = (ttyinq_getallocatedsize(&tp->t_inq) * 9) / 10;
116
117 /* Provide an output buffer for 0.2 seconds of data. */
118 bs = MIN(tp->t_termios.c_ospeed / 5, TTYBUF_MAX);
119 ttyoutq_setsize(&tp->t_outq, tp, bs);
120
121 /* Set low watermark at 10% (when 90% is available). */
122 tp->t_outlow = (ttyoutq_getallocatedsize(&tp->t_outq) * 9) / 10;
123 }
124
125 static int
tty_drain(struct tty * tp,int leaving)126 tty_drain(struct tty *tp, int leaving)
127 {
128 size_t bytesused;
129 int error, revokecnt;
130
131 if (ttyhook_hashook(tp, getc_inject))
132 /* buffer is inaccessible */
133 return (0);
134
135 while (ttyoutq_bytesused(&tp->t_outq) > 0) {
136 ttydevsw_outwakeup(tp);
137 /* Could be handled synchronously. */
138 bytesused = ttyoutq_bytesused(&tp->t_outq);
139 if (bytesused == 0)
140 return (0);
141
142 /* Wait for data to be drained. */
143 if (leaving) {
144 revokecnt = tp->t_revokecnt;
145 error = tty_timedwait(tp, &tp->t_outwait, hz);
146 switch (error) {
147 case ERESTART:
148 if (revokecnt != tp->t_revokecnt)
149 error = 0;
150 break;
151 case EWOULDBLOCK:
152 if (ttyoutq_bytesused(&tp->t_outq) < bytesused)
153 error = 0;
154 break;
155 }
156 } else
157 error = tty_wait(tp, &tp->t_outwait);
158
159 if (error)
160 return (error);
161 }
162
163 return (0);
164 }
165
166 /*
167 * Though ttydev_enter() and ttydev_leave() seem to be related, they
168 * don't have to be used together. ttydev_enter() is used by the cdev
169 * operations to prevent an actual operation from being processed when
170 * the TTY has been abandoned. ttydev_leave() is used by ttydev_open()
171 * and ttydev_close() to determine whether per-TTY data should be
172 * deallocated.
173 */
174
175 static __inline int
ttydev_enter(struct tty * tp)176 ttydev_enter(struct tty *tp)
177 {
178 tty_lock(tp);
179
180 if (tty_gone(tp) || !tty_opened(tp)) {
181 /* Device is already gone. */
182 tty_unlock(tp);
183 return (ENXIO);
184 }
185
186 return (0);
187 }
188
189 static void
ttydev_leave(struct tty * tp)190 ttydev_leave(struct tty *tp)
191 {
192 tty_lock_assert(tp, MA_OWNED);
193
194 if (tty_opened(tp) || tp->t_flags & TF_OPENCLOSE) {
195 /* Device is still opened somewhere. */
196 tty_unlock(tp);
197 return;
198 }
199
200 tp->t_flags |= TF_OPENCLOSE;
201
202 /* Stop asynchronous I/O. */
203 funsetown(&tp->t_sigio);
204
205 /* Remove console TTY. */
206 if (constty == tp)
207 constty_clear();
208
209 /* Drain any output. */
210 MPASS((tp->t_flags & TF_STOPPED) == 0);
211 if (!tty_gone(tp))
212 tty_drain(tp, 1);
213
214 ttydisc_close(tp);
215
216 /* Destroy associated buffers already. */
217 ttyinq_free(&tp->t_inq);
218 tp->t_inlow = 0;
219 ttyoutq_free(&tp->t_outq);
220 tp->t_outlow = 0;
221
222 knlist_clear(&tp->t_inpoll.si_note, 1);
223 knlist_clear(&tp->t_outpoll.si_note, 1);
224
225 if (!tty_gone(tp))
226 ttydevsw_close(tp);
227
228 tp->t_flags &= ~TF_OPENCLOSE;
229 cv_broadcast(&tp->t_dcdwait);
230 tty_rel_free(tp);
231 }
232
233 /*
234 * Operations that are exposed through the character device in /dev.
235 */
236 static int
ttydev_open(struct cdev * dev,int oflags,int devtype,struct thread * td)237 ttydev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
238 {
239 struct tty *tp;
240 int error;
241
242 tp = dev->si_drv1;
243 error = 0;
244 tty_lock(tp);
245 if (tty_gone(tp)) {
246 /* Device is already gone. */
247 tty_unlock(tp);
248 return (ENXIO);
249 }
250
251 /*
252 * Block when other processes are currently opening or closing
253 * the TTY.
254 */
255 while (tp->t_flags & TF_OPENCLOSE) {
256 error = tty_wait(tp, &tp->t_dcdwait);
257 if (error != 0) {
258 tty_unlock(tp);
259 return (error);
260 }
261 }
262 tp->t_flags |= TF_OPENCLOSE;
263
264 /*
265 * Make sure the "tty" and "cua" device cannot be opened at the
266 * same time.
267 */
268 if (TTY_CALLOUT(tp, dev)) {
269 if (tp->t_flags & TF_OPENED_IN) {
270 error = EBUSY;
271 goto done;
272 }
273 } else {
274 if (tp->t_flags & TF_OPENED_OUT) {
275 error = EBUSY;
276 goto done;
277 }
278 }
279
280 if (tp->t_flags & TF_EXCLUDE && priv_check(td, PRIV_TTY_EXCLUSIVE)) {
281 error = EBUSY;
282 goto done;
283 }
284
285 if (!tty_opened(tp)) {
286 /* Set proper termios flags. */
287 if (TTY_CALLOUT(tp, dev))
288 tp->t_termios = tp->t_termios_init_out;
289 else
290 tp->t_termios = tp->t_termios_init_in;
291 ttydevsw_param(tp, &tp->t_termios);
292 /* Prevent modem control on callout devices and /dev/console. */
293 if (TTY_CALLOUT(tp, dev) || dev == dev_console)
294 tp->t_termios.c_cflag |= CLOCAL;
295
296 ttydevsw_modem(tp, SER_DTR|SER_RTS, 0);
297
298 error = ttydevsw_open(tp);
299 if (error != 0)
300 goto done;
301
302 ttydisc_open(tp);
303 tty_watermarks(tp); /* XXXGL: drops lock */
304 }
305
306 /* Wait for Carrier Detect. */
307 if ((oflags & O_NONBLOCK) == 0 &&
308 (tp->t_termios.c_cflag & CLOCAL) == 0) {
309 while ((ttydevsw_modem(tp, 0, 0) & SER_DCD) == 0) {
310 error = tty_wait(tp, &tp->t_dcdwait);
311 if (error != 0)
312 goto done;
313 }
314 }
315
316 if (dev == dev_console)
317 tp->t_flags |= TF_OPENED_CONS;
318 else if (TTY_CALLOUT(tp, dev))
319 tp->t_flags |= TF_OPENED_OUT;
320 else
321 tp->t_flags |= TF_OPENED_IN;
322
323 done: tp->t_flags &= ~TF_OPENCLOSE;
324 cv_broadcast(&tp->t_dcdwait);
325 ttydev_leave(tp);
326
327 return (error);
328 }
329
330 static int
ttydev_close(struct cdev * dev,int fflag,int devtype,struct thread * td)331 ttydev_close(struct cdev *dev, int fflag, int devtype, struct thread *td)
332 {
333 struct tty *tp = dev->si_drv1;
334
335 tty_lock(tp);
336
337 /*
338 * Don't actually close the device if it is being used as the
339 * console.
340 */
341 MPASS((tp->t_flags & TF_OPENED) != TF_OPENED);
342 if (dev == dev_console)
343 tp->t_flags &= ~TF_OPENED_CONS;
344 else
345 tp->t_flags &= ~(TF_OPENED_IN|TF_OPENED_OUT);
346
347 if (tp->t_flags & TF_OPENED) {
348 tty_unlock(tp);
349 return (0);
350 }
351
352 /*
353 * This can only be called once. The callin and the callout
354 * devices cannot be opened at the same time.
355 */
356 tp->t_flags &= ~(TF_EXCLUDE|TF_STOPPED);
357
358 /* Properly wake up threads that are stuck - revoke(). */
359 tp->t_revokecnt++;
360 tty_wakeup(tp, FREAD|FWRITE);
361 cv_broadcast(&tp->t_bgwait);
362 cv_broadcast(&tp->t_dcdwait);
363
364 ttydev_leave(tp);
365
366 return (0);
367 }
368
369 static __inline int
tty_is_ctty(struct tty * tp,struct proc * p)370 tty_is_ctty(struct tty *tp, struct proc *p)
371 {
372 tty_lock_assert(tp, MA_OWNED);
373
374 return (p->p_session == tp->t_session && p->p_flag & P_CONTROLT);
375 }
376
377 int
tty_wait_background(struct tty * tp,struct thread * td,int sig)378 tty_wait_background(struct tty *tp, struct thread *td, int sig)
379 {
380 struct proc *p = td->td_proc;
381 struct pgrp *pg;
382 ksiginfo_t ksi;
383 int error;
384
385 MPASS(sig == SIGTTIN || sig == SIGTTOU);
386 tty_lock_assert(tp, MA_OWNED);
387
388 for (;;) {
389 PROC_LOCK(p);
390 /*
391 * The process should only sleep, when:
392 * - This terminal is the controling terminal
393 * - Its process group is not the foreground process
394 * group
395 * - The parent process isn't waiting for the child to
396 * exit
397 * - the signal to send to the process isn't masked
398 */
399 if (!tty_is_ctty(tp, p) || p->p_pgrp == tp->t_pgrp) {
400 /* Allow the action to happen. */
401 PROC_UNLOCK(p);
402 return (0);
403 }
404
405 if (SIGISMEMBER(p->p_sigacts->ps_sigignore, sig) ||
406 SIGISMEMBER(td->td_sigmask, sig)) {
407 /* Only allow them in write()/ioctl(). */
408 PROC_UNLOCK(p);
409 return (sig == SIGTTOU ? 0 : EIO);
410 }
411
412 pg = p->p_pgrp;
413 if (p->p_flag & P_PPWAIT || pg->pg_jobc == 0) {
414 /* Don't allow the action to happen. */
415 PROC_UNLOCK(p);
416 return (EIO);
417 }
418 PROC_UNLOCK(p);
419
420 /*
421 * Send the signal and sleep until we're the new
422 * foreground process group.
423 */
424 if (sig != 0) {
425 ksiginfo_init(&ksi);
426 ksi.ksi_code = SI_KERNEL;
427 ksi.ksi_signo = sig;
428 sig = 0;
429 }
430 PGRP_LOCK(pg);
431 pgsignal(pg, ksi.ksi_signo, 1, &ksi);
432 PGRP_UNLOCK(pg);
433
434 error = tty_wait(tp, &tp->t_bgwait);
435 if (error)
436 return (error);
437 }
438 }
439
440 static int
ttydev_read(struct cdev * dev,struct uio * uio,int ioflag)441 ttydev_read(struct cdev *dev, struct uio *uio, int ioflag)
442 {
443 struct tty *tp = dev->si_drv1;
444 int error;
445
446 error = ttydev_enter(tp);
447 if (error)
448 goto done;
449 error = ttydisc_read(tp, uio, ioflag);
450 tty_unlock(tp);
451
452 /*
453 * The read() call should not throw an error when the device is
454 * being destroyed. Silently convert it to an EOF.
455 */
456 done: if (error == ENXIO)
457 error = 0;
458 return (error);
459 }
460
461 static int
ttydev_write(struct cdev * dev,struct uio * uio,int ioflag)462 ttydev_write(struct cdev *dev, struct uio *uio, int ioflag)
463 {
464 struct tty *tp = dev->si_drv1;
465 int error;
466
467 error = ttydev_enter(tp);
468 if (error)
469 return (error);
470
471 if (tp->t_termios.c_lflag & TOSTOP) {
472 error = tty_wait_background(tp, curthread, SIGTTOU);
473 if (error)
474 goto done;
475 }
476
477 if (ioflag & IO_NDELAY && tp->t_flags & TF_BUSY_OUT) {
478 /* Allow non-blocking writes to bypass serialization. */
479 error = ttydisc_write(tp, uio, ioflag);
480 } else {
481 /* Serialize write() calls. */
482 while (tp->t_flags & TF_BUSY_OUT) {
483 error = tty_wait(tp, &tp->t_outserwait);
484 if (error)
485 goto done;
486 }
487
488 tp->t_flags |= TF_BUSY_OUT;
489 error = ttydisc_write(tp, uio, ioflag);
490 tp->t_flags &= ~TF_BUSY_OUT;
491 cv_signal(&tp->t_outserwait);
492 }
493
494 done: tty_unlock(tp);
495 return (error);
496 }
497
498 static int
ttydev_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)499 ttydev_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
500 struct thread *td)
501 {
502 struct tty *tp = dev->si_drv1;
503 int error;
504
505 error = ttydev_enter(tp);
506 if (error)
507 return (error);
508
509 switch (cmd) {
510 case TIOCCBRK:
511 case TIOCCONS:
512 case TIOCDRAIN:
513 case TIOCEXCL:
514 case TIOCFLUSH:
515 case TIOCNXCL:
516 case TIOCSBRK:
517 case TIOCSCTTY:
518 case TIOCSETA:
519 case TIOCSETAF:
520 case TIOCSETAW:
521 case TIOCSPGRP:
522 case TIOCSTART:
523 case TIOCSTAT:
524 case TIOCSTI:
525 case TIOCSTOP:
526 case TIOCSWINSZ:
527 #if 0
528 case TIOCSDRAINWAIT:
529 case TIOCSETD:
530 #endif
531 #ifdef COMPAT_43TTY
532 case TIOCLBIC:
533 case TIOCLBIS:
534 case TIOCLSET:
535 case TIOCSETC:
536 case OTIOCSETD:
537 case TIOCSETN:
538 case TIOCSETP:
539 case TIOCSLTC:
540 #endif /* COMPAT_43TTY */
541 /*
542 * If the ioctl() causes the TTY to be modified, let it
543 * wait in the background.
544 */
545 error = tty_wait_background(tp, curthread, SIGTTOU);
546 if (error)
547 goto done;
548 }
549
550 if (cmd == TIOCSETA || cmd == TIOCSETAW || cmd == TIOCSETAF) {
551 struct termios *old = &tp->t_termios;
552 struct termios *new = (struct termios *)data;
553 struct termios *lock = TTY_CALLOUT(tp, dev) ?
554 &tp->t_termios_lock_out : &tp->t_termios_lock_in;
555 int cc;
556
557 /*
558 * Lock state devices. Just overwrite the values of the
559 * commands that are currently in use.
560 */
561 new->c_iflag = (old->c_iflag & lock->c_iflag) |
562 (new->c_iflag & ~lock->c_iflag);
563 new->c_oflag = (old->c_oflag & lock->c_oflag) |
564 (new->c_oflag & ~lock->c_oflag);
565 new->c_cflag = (old->c_cflag & lock->c_cflag) |
566 (new->c_cflag & ~lock->c_cflag);
567 new->c_lflag = (old->c_lflag & lock->c_lflag) |
568 (new->c_lflag & ~lock->c_lflag);
569 for (cc = 0; cc < NCCS; ++cc)
570 if (lock->c_cc[cc])
571 new->c_cc[cc] = old->c_cc[cc];
572 if (lock->c_ispeed)
573 new->c_ispeed = old->c_ispeed;
574 if (lock->c_ospeed)
575 new->c_ospeed = old->c_ospeed;
576 }
577
578 error = tty_ioctl(tp, cmd, data, fflag, td);
579 done: tty_unlock(tp);
580
581 return (error);
582 }
583
584 static int
ttydev_poll(struct cdev * dev,int events,struct thread * td)585 ttydev_poll(struct cdev *dev, int events, struct thread *td)
586 {
587 struct tty *tp = dev->si_drv1;
588 int error, revents = 0;
589
590 error = ttydev_enter(tp);
591 if (error)
592 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
593
594 if (events & (POLLIN|POLLRDNORM)) {
595 /* See if we can read something. */
596 if (ttydisc_read_poll(tp) > 0)
597 revents |= events & (POLLIN|POLLRDNORM);
598 }
599
600 if (tp->t_flags & TF_ZOMBIE) {
601 /* Hangup flag on zombie state. */
602 revents |= POLLHUP;
603 } else if (events & (POLLOUT|POLLWRNORM)) {
604 /* See if we can write something. */
605 if (ttydisc_write_poll(tp) > 0)
606 revents |= events & (POLLOUT|POLLWRNORM);
607 }
608
609 if (revents == 0) {
610 if (events & (POLLIN|POLLRDNORM))
611 selrecord(td, &tp->t_inpoll);
612 if (events & (POLLOUT|POLLWRNORM))
613 selrecord(td, &tp->t_outpoll);
614 }
615
616 tty_unlock(tp);
617
618 return (revents);
619 }
620
621 static int
ttydev_mmap(struct cdev * dev,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)622 ttydev_mmap(struct cdev *dev, vm_ooffset_t offset, vm_paddr_t *paddr,
623 int nprot, vm_memattr_t *memattr)
624 {
625 struct tty *tp = dev->si_drv1;
626 int error;
627
628 /* Handle mmap() through the driver. */
629
630 error = ttydev_enter(tp);
631 if (error)
632 return (-1);
633 error = ttydevsw_mmap(tp, offset, paddr, nprot, memattr);
634 tty_unlock(tp);
635
636 return (error);
637 }
638
639 /*
640 * kqueue support.
641 */
642
643 static void
tty_kqops_read_detach(struct knote * kn)644 tty_kqops_read_detach(struct knote *kn)
645 {
646 struct tty *tp = kn->kn_hook;
647
648 knlist_remove(&tp->t_inpoll.si_note, kn, 0);
649 }
650
651 static int
tty_kqops_read_event(struct knote * kn,long hint)652 tty_kqops_read_event(struct knote *kn, long hint)
653 {
654 struct tty *tp = kn->kn_hook;
655
656 tty_lock_assert(tp, MA_OWNED);
657
658 if (tty_gone(tp) || tp->t_flags & TF_ZOMBIE) {
659 kn->kn_flags |= EV_EOF;
660 return (1);
661 } else {
662 kn->kn_data = ttydisc_read_poll(tp);
663 return (kn->kn_data > 0);
664 }
665 }
666
667 static void
tty_kqops_write_detach(struct knote * kn)668 tty_kqops_write_detach(struct knote *kn)
669 {
670 struct tty *tp = kn->kn_hook;
671
672 knlist_remove(&tp->t_outpoll.si_note, kn, 0);
673 }
674
675 static int
tty_kqops_write_event(struct knote * kn,long hint)676 tty_kqops_write_event(struct knote *kn, long hint)
677 {
678 struct tty *tp = kn->kn_hook;
679
680 tty_lock_assert(tp, MA_OWNED);
681
682 if (tty_gone(tp)) {
683 kn->kn_flags |= EV_EOF;
684 return (1);
685 } else {
686 kn->kn_data = ttydisc_write_poll(tp);
687 return (kn->kn_data > 0);
688 }
689 }
690
691 static struct filterops tty_kqops_read = {
692 .f_isfd = 1,
693 .f_detach = tty_kqops_read_detach,
694 .f_event = tty_kqops_read_event,
695 };
696 static struct filterops tty_kqops_write = {
697 .f_isfd = 1,
698 .f_detach = tty_kqops_write_detach,
699 .f_event = tty_kqops_write_event,
700 };
701
702 static int
ttydev_kqfilter(struct cdev * dev,struct knote * kn)703 ttydev_kqfilter(struct cdev *dev, struct knote *kn)
704 {
705 struct tty *tp = dev->si_drv1;
706 int error;
707
708 error = ttydev_enter(tp);
709 if (error)
710 return (error);
711
712 switch (kn->kn_filter) {
713 case EVFILT_READ:
714 kn->kn_hook = tp;
715 kn->kn_fop = &tty_kqops_read;
716 knlist_add(&tp->t_inpoll.si_note, kn, 1);
717 break;
718 case EVFILT_WRITE:
719 kn->kn_hook = tp;
720 kn->kn_fop = &tty_kqops_write;
721 knlist_add(&tp->t_outpoll.si_note, kn, 1);
722 break;
723 default:
724 error = EINVAL;
725 break;
726 }
727
728 tty_unlock(tp);
729 return (error);
730 }
731
732 static struct cdevsw ttydev_cdevsw = {
733 .d_version = D_VERSION,
734 .d_open = ttydev_open,
735 .d_close = ttydev_close,
736 .d_read = ttydev_read,
737 .d_write = ttydev_write,
738 .d_ioctl = ttydev_ioctl,
739 .d_kqfilter = ttydev_kqfilter,
740 .d_poll = ttydev_poll,
741 .d_mmap = ttydev_mmap,
742 .d_name = "ttydev",
743 .d_flags = D_TTY,
744 };
745
746 /*
747 * Init/lock-state devices
748 */
749
750 static int
ttyil_open(struct cdev * dev,int oflags,int devtype,struct thread * td)751 ttyil_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
752 {
753 struct tty *tp;
754 int error;
755
756 tp = dev->si_drv1;
757 error = 0;
758 tty_lock(tp);
759 if (tty_gone(tp))
760 error = ENODEV;
761 tty_unlock(tp);
762
763 return (error);
764 }
765
766 static int
ttyil_close(struct cdev * dev,int flag,int mode,struct thread * td)767 ttyil_close(struct cdev *dev, int flag, int mode, struct thread *td)
768 {
769 return (0);
770 }
771
772 static int
ttyil_rdwr(struct cdev * dev,struct uio * uio,int ioflag)773 ttyil_rdwr(struct cdev *dev, struct uio *uio, int ioflag)
774 {
775 return (ENODEV);
776 }
777
778 static int
ttyil_ioctl(struct cdev * dev,u_long cmd,caddr_t data,int fflag,struct thread * td)779 ttyil_ioctl(struct cdev *dev, u_long cmd, caddr_t data, int fflag,
780 struct thread *td)
781 {
782 struct tty *tp = dev->si_drv1;
783 int error;
784
785 tty_lock(tp);
786 if (tty_gone(tp)) {
787 error = ENODEV;
788 goto done;
789 }
790
791 error = ttydevsw_cioctl(tp, dev2unit(dev), cmd, data, td);
792 if (error != ENOIOCTL)
793 goto done;
794 error = 0;
795
796 switch (cmd) {
797 case TIOCGETA:
798 /* Obtain terminal flags through tcgetattr(). */
799 *(struct termios*)data = *(struct termios*)dev->si_drv2;
800 break;
801 case TIOCSETA:
802 /* Set terminal flags through tcsetattr(). */
803 error = priv_check(td, PRIV_TTY_SETA);
804 if (error)
805 break;
806 *(struct termios*)dev->si_drv2 = *(struct termios*)data;
807 break;
808 case TIOCGETD:
809 *(int *)data = TTYDISC;
810 break;
811 case TIOCGWINSZ:
812 bzero(data, sizeof(struct winsize));
813 break;
814 default:
815 error = ENOTTY;
816 }
817
818 done: tty_unlock(tp);
819 return (error);
820 }
821
822 static struct cdevsw ttyil_cdevsw = {
823 .d_version = D_VERSION,
824 .d_open = ttyil_open,
825 .d_close = ttyil_close,
826 .d_read = ttyil_rdwr,
827 .d_write = ttyil_rdwr,
828 .d_ioctl = ttyil_ioctl,
829 .d_name = "ttyil",
830 .d_flags = D_TTY,
831 };
832
833 static void
tty_init_termios(struct tty * tp)834 tty_init_termios(struct tty *tp)
835 {
836 struct termios *t = &tp->t_termios_init_in;
837
838 t->c_cflag = TTYDEF_CFLAG;
839 t->c_iflag = TTYDEF_IFLAG;
840 t->c_lflag = TTYDEF_LFLAG;
841 t->c_oflag = TTYDEF_OFLAG;
842 t->c_ispeed = TTYDEF_SPEED;
843 t->c_ospeed = TTYDEF_SPEED;
844 memcpy(&t->c_cc, ttydefchars, sizeof ttydefchars);
845
846 tp->t_termios_init_out = *t;
847 }
848
849 void
tty_init_console(struct tty * tp,speed_t s)850 tty_init_console(struct tty *tp, speed_t s)
851 {
852 struct termios *ti = &tp->t_termios_init_in;
853 struct termios *to = &tp->t_termios_init_out;
854
855 if (s != 0) {
856 ti->c_ispeed = ti->c_ospeed = s;
857 to->c_ispeed = to->c_ospeed = s;
858 }
859
860 ti->c_cflag |= CLOCAL;
861 to->c_cflag |= CLOCAL;
862 }
863
864 /*
865 * Standard device routine implementations, mostly meant for
866 * pseudo-terminal device drivers. When a driver creates a new terminal
867 * device class, missing routines are patched.
868 */
869
870 static int
ttydevsw_defopen(struct tty * tp)871 ttydevsw_defopen(struct tty *tp)
872 {
873
874 return (0);
875 }
876
877 static void
ttydevsw_defclose(struct tty * tp)878 ttydevsw_defclose(struct tty *tp)
879 {
880 }
881
882 static void
ttydevsw_defoutwakeup(struct tty * tp)883 ttydevsw_defoutwakeup(struct tty *tp)
884 {
885
886 panic("Terminal device has output, while not implemented");
887 }
888
889 static void
ttydevsw_definwakeup(struct tty * tp)890 ttydevsw_definwakeup(struct tty *tp)
891 {
892 }
893
894 static int
ttydevsw_defioctl(struct tty * tp,u_long cmd,caddr_t data,struct thread * td)895 ttydevsw_defioctl(struct tty *tp, u_long cmd, caddr_t data, struct thread *td)
896 {
897
898 return (ENOIOCTL);
899 }
900
901 static int
ttydevsw_defcioctl(struct tty * tp,int unit,u_long cmd,caddr_t data,struct thread * td)902 ttydevsw_defcioctl(struct tty *tp, int unit, u_long cmd, caddr_t data, struct thread *td)
903 {
904
905 return (ENOIOCTL);
906 }
907
908 static int
ttydevsw_defparam(struct tty * tp,struct termios * t)909 ttydevsw_defparam(struct tty *tp, struct termios *t)
910 {
911
912 /*
913 * Allow the baud rate to be adjusted for pseudo-devices, but at
914 * least restrict it to 115200 to prevent excessive buffer
915 * usage. Also disallow 0, to prevent foot shooting.
916 */
917 if (t->c_ispeed < B50)
918 t->c_ispeed = B50;
919 else if (t->c_ispeed > B115200)
920 t->c_ispeed = B115200;
921 if (t->c_ospeed < B50)
922 t->c_ospeed = B50;
923 else if (t->c_ospeed > B115200)
924 t->c_ospeed = B115200;
925 t->c_cflag |= CREAD;
926
927 return (0);
928 }
929
930 static int
ttydevsw_defmodem(struct tty * tp,int sigon,int sigoff)931 ttydevsw_defmodem(struct tty *tp, int sigon, int sigoff)
932 {
933
934 /* Simulate a carrier to make the TTY layer happy. */
935 return (SER_DCD);
936 }
937
938 static int
ttydevsw_defmmap(struct tty * tp,vm_ooffset_t offset,vm_paddr_t * paddr,int nprot,vm_memattr_t * memattr)939 ttydevsw_defmmap(struct tty *tp, vm_ooffset_t offset, vm_paddr_t *paddr,
940 int nprot, vm_memattr_t *memattr)
941 {
942
943 return (-1);
944 }
945
946 static void
ttydevsw_defpktnotify(struct tty * tp,char event)947 ttydevsw_defpktnotify(struct tty *tp, char event)
948 {
949 }
950
951 static void
ttydevsw_deffree(void * softc)952 ttydevsw_deffree(void *softc)
953 {
954
955 panic("Terminal device freed without a free-handler");
956 }
957
958 /*
959 * TTY allocation and deallocation. TTY devices can be deallocated when
960 * the driver doesn't use it anymore, when the TTY isn't a session's
961 * controlling TTY and when the device node isn't opened through devfs.
962 */
963
964 struct tty *
tty_alloc(struct ttydevsw * tsw,void * sc)965 tty_alloc(struct ttydevsw *tsw, void *sc)
966 {
967
968 return (tty_alloc_mutex(tsw, sc, NULL));
969 }
970
971 struct tty *
tty_alloc_mutex(struct ttydevsw * tsw,void * sc,struct mtx * mutex)972 tty_alloc_mutex(struct ttydevsw *tsw, void *sc, struct mtx *mutex)
973 {
974 struct tty *tp;
975
976 /* Make sure the driver defines all routines. */
977 #define PATCH_FUNC(x) do { \
978 if (tsw->tsw_ ## x == NULL) \
979 tsw->tsw_ ## x = ttydevsw_def ## x; \
980 } while (0)
981 PATCH_FUNC(open);
982 PATCH_FUNC(close);
983 PATCH_FUNC(outwakeup);
984 PATCH_FUNC(inwakeup);
985 PATCH_FUNC(ioctl);
986 PATCH_FUNC(cioctl);
987 PATCH_FUNC(param);
988 PATCH_FUNC(modem);
989 PATCH_FUNC(mmap);
990 PATCH_FUNC(pktnotify);
991 PATCH_FUNC(free);
992 #undef PATCH_FUNC
993
994 tp = malloc(sizeof(struct tty), M_TTY, M_WAITOK|M_ZERO);
995 tp->t_devsw = tsw;
996 tp->t_devswsoftc = sc;
997 tp->t_flags = tsw->tsw_flags;
998
999 tty_init_termios(tp);
1000
1001 cv_init(&tp->t_inwait, "ttyin");
1002 cv_init(&tp->t_outwait, "ttyout");
1003 cv_init(&tp->t_outserwait, "ttyosr");
1004 cv_init(&tp->t_bgwait, "ttybg");
1005 cv_init(&tp->t_dcdwait, "ttydcd");
1006
1007 /* Allow drivers to use a custom mutex to lock the TTY. */
1008 if (mutex != NULL) {
1009 tp->t_mtx = mutex;
1010 } else {
1011 tp->t_mtx = &tp->t_mtxobj;
1012 mtx_init(&tp->t_mtxobj, "ttymtx", NULL, MTX_DEF);
1013 }
1014
1015 knlist_init_mtx(&tp->t_inpoll.si_note, tp->t_mtx);
1016 knlist_init_mtx(&tp->t_outpoll.si_note, tp->t_mtx);
1017
1018 return (tp);
1019 }
1020
1021 static void
tty_dealloc(void * arg)1022 tty_dealloc(void *arg)
1023 {
1024 struct tty *tp = arg;
1025
1026 /* Make sure we haven't leaked buffers. */
1027 MPASS(ttyinq_getsize(&tp->t_inq) == 0);
1028 MPASS(ttyoutq_getsize(&tp->t_outq) == 0);
1029
1030 seldrain(&tp->t_inpoll);
1031 seldrain(&tp->t_outpoll);
1032 knlist_destroy(&tp->t_inpoll.si_note);
1033 knlist_destroy(&tp->t_outpoll.si_note);
1034
1035 cv_destroy(&tp->t_inwait);
1036 cv_destroy(&tp->t_outwait);
1037 cv_destroy(&tp->t_bgwait);
1038 cv_destroy(&tp->t_dcdwait);
1039 cv_destroy(&tp->t_outserwait);
1040
1041 if (tp->t_mtx == &tp->t_mtxobj)
1042 mtx_destroy(&tp->t_mtxobj);
1043 ttydevsw_free(tp);
1044 free(tp, M_TTY);
1045 }
1046
1047 static void
tty_rel_free(struct tty * tp)1048 tty_rel_free(struct tty *tp)
1049 {
1050 struct cdev *dev;
1051
1052 tty_lock_assert(tp, MA_OWNED);
1053
1054 #define TF_ACTIVITY (TF_GONE|TF_OPENED|TF_HOOK|TF_OPENCLOSE)
1055 if (tp->t_sessioncnt != 0 || (tp->t_flags & TF_ACTIVITY) != TF_GONE) {
1056 /* TTY is still in use. */
1057 tty_unlock(tp);
1058 return;
1059 }
1060
1061 /* TTY can be deallocated. */
1062 dev = tp->t_dev;
1063 tp->t_dev = NULL;
1064 tty_unlock(tp);
1065
1066 if (dev != NULL) {
1067 sx_xlock(&tty_list_sx);
1068 TAILQ_REMOVE(&tty_list, tp, t_list);
1069 tty_list_count--;
1070 sx_xunlock(&tty_list_sx);
1071 destroy_dev_sched_cb(dev, tty_dealloc, tp);
1072 }
1073 }
1074
1075 void
tty_rel_pgrp(struct tty * tp,struct pgrp * pg)1076 tty_rel_pgrp(struct tty *tp, struct pgrp *pg)
1077 {
1078 MPASS(tp->t_sessioncnt > 0);
1079 tty_lock_assert(tp, MA_OWNED);
1080
1081 if (tp->t_pgrp == pg)
1082 tp->t_pgrp = NULL;
1083
1084 tty_unlock(tp);
1085 }
1086
1087 void
tty_rel_sess(struct tty * tp,struct session * sess)1088 tty_rel_sess(struct tty *tp, struct session *sess)
1089 {
1090 MPASS(tp->t_sessioncnt > 0);
1091
1092 /* Current session has left. */
1093 if (tp->t_session == sess) {
1094 tp->t_session = NULL;
1095 MPASS(tp->t_pgrp == NULL);
1096 }
1097 tp->t_sessioncnt--;
1098 tty_rel_free(tp);
1099 }
1100
1101 void
tty_rel_gone(struct tty * tp)1102 tty_rel_gone(struct tty *tp)
1103 {
1104 MPASS(!tty_gone(tp));
1105
1106 /* Simulate carrier removal. */
1107 ttydisc_modem(tp, 0);
1108
1109 /* Wake up all blocked threads. */
1110 tty_wakeup(tp, FREAD|FWRITE);
1111 cv_broadcast(&tp->t_bgwait);
1112 cv_broadcast(&tp->t_dcdwait);
1113
1114 tp->t_flags |= TF_GONE;
1115 tty_rel_free(tp);
1116 }
1117
1118 /*
1119 * Exposing information about current TTY's through sysctl
1120 */
1121
1122 static void
tty_to_xtty(struct tty * tp,struct xtty * xt)1123 tty_to_xtty(struct tty *tp, struct xtty *xt)
1124 {
1125 tty_lock_assert(tp, MA_OWNED);
1126
1127 xt->xt_size = sizeof(struct xtty);
1128 xt->xt_insize = ttyinq_getsize(&tp->t_inq);
1129 xt->xt_incc = ttyinq_bytescanonicalized(&tp->t_inq);
1130 xt->xt_inlc = ttyinq_bytesline(&tp->t_inq);
1131 xt->xt_inlow = tp->t_inlow;
1132 xt->xt_outsize = ttyoutq_getsize(&tp->t_outq);
1133 xt->xt_outcc = ttyoutq_bytesused(&tp->t_outq);
1134 xt->xt_outlow = tp->t_outlow;
1135 xt->xt_column = tp->t_column;
1136 xt->xt_pgid = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1137 xt->xt_sid = tp->t_session ? tp->t_session->s_sid : 0;
1138 xt->xt_flags = tp->t_flags;
1139 xt->xt_dev = tp->t_dev ? dev2udev(tp->t_dev) : NODEV;
1140 }
1141
1142 static int
sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)1143 sysctl_kern_ttys(SYSCTL_HANDLER_ARGS)
1144 {
1145 unsigned long lsize;
1146 struct xtty *xtlist, *xt;
1147 struct tty *tp;
1148 int error;
1149
1150 sx_slock(&tty_list_sx);
1151 lsize = tty_list_count * sizeof(struct xtty);
1152 if (lsize == 0) {
1153 sx_sunlock(&tty_list_sx);
1154 return (0);
1155 }
1156
1157 xtlist = xt = malloc(lsize, M_TTY, M_WAITOK);
1158
1159 TAILQ_FOREACH(tp, &tty_list, t_list) {
1160 tty_lock(tp);
1161 tty_to_xtty(tp, xt);
1162 tty_unlock(tp);
1163 xt++;
1164 }
1165 sx_sunlock(&tty_list_sx);
1166
1167 error = SYSCTL_OUT(req, xtlist, lsize);
1168 free(xtlist, M_TTY);
1169 return (error);
1170 }
1171
1172 SYSCTL_PROC(_kern, OID_AUTO, ttys, CTLTYPE_OPAQUE|CTLFLAG_RD|CTLFLAG_MPSAFE,
1173 0, 0, sysctl_kern_ttys, "S,xtty", "List of TTYs");
1174
1175 /*
1176 * Device node creation. Device has been set up, now we can expose it to
1177 * the user.
1178 */
1179
1180 int
tty_makedevf(struct tty * tp,struct ucred * cred,int flags,const char * fmt,...)1181 tty_makedevf(struct tty *tp, struct ucred *cred, int flags,
1182 const char *fmt, ...)
1183 {
1184 va_list ap;
1185 struct make_dev_args args;
1186 struct cdev *dev, *init, *lock, *cua, *cinit, *clock;
1187 const char *prefix = "tty";
1188 char name[SPECNAMELEN - 3]; /* for "tty" and "cua". */
1189 uid_t uid;
1190 gid_t gid;
1191 mode_t mode;
1192 int error;
1193
1194 /* Remove "tty" prefix from devices like PTY's. */
1195 if (tp->t_flags & TF_NOPREFIX)
1196 prefix = "";
1197
1198 va_start(ap, fmt);
1199 vsnrprintf(name, sizeof name, 32, fmt, ap);
1200 va_end(ap);
1201
1202 if (cred == NULL) {
1203 /* System device. */
1204 uid = UID_ROOT;
1205 gid = GID_WHEEL;
1206 mode = S_IRUSR|S_IWUSR;
1207 } else {
1208 /* User device. */
1209 uid = cred->cr_ruid;
1210 gid = GID_TTY;
1211 mode = S_IRUSR|S_IWUSR|S_IWGRP;
1212 }
1213
1214 flags = flags & TTYMK_CLONING ? MAKEDEV_REF : 0;
1215 flags |= MAKEDEV_CHECKNAME;
1216
1217 /* Master call-in device. */
1218 make_dev_args_init(&args);
1219 args.mda_flags = flags;
1220 args.mda_devsw = &ttydev_cdevsw;
1221 args.mda_cr = cred;
1222 args.mda_uid = uid;
1223 args.mda_gid = gid;
1224 args.mda_mode = mode;
1225 args.mda_si_drv1 = tp;
1226 error = make_dev_s(&args, &dev, "%s%s", prefix, name);
1227 if (error != 0)
1228 return (error);
1229 tp->t_dev = dev;
1230
1231 init = lock = cua = cinit = clock = NULL;
1232
1233 /* Slave call-in devices. */
1234 if (tp->t_flags & TF_INITLOCK) {
1235 args.mda_devsw = &ttyil_cdevsw;
1236 args.mda_unit = TTYUNIT_INIT;
1237 args.mda_si_drv1 = tp;
1238 args.mda_si_drv2 = &tp->t_termios_init_in;
1239 error = make_dev_s(&args, &init, "%s%s.init", prefix, name);
1240 if (error != 0)
1241 goto fail;
1242 dev_depends(dev, init);
1243
1244 args.mda_unit = TTYUNIT_LOCK;
1245 args.mda_si_drv2 = &tp->t_termios_lock_in;
1246 error = make_dev_s(&args, &lock, "%s%s.lock", prefix, name);
1247 if (error != 0)
1248 goto fail;
1249 dev_depends(dev, lock);
1250 }
1251
1252 /* Call-out devices. */
1253 if (tp->t_flags & TF_CALLOUT) {
1254 make_dev_args_init(&args);
1255 args.mda_flags = flags;
1256 args.mda_devsw = &ttydev_cdevsw;
1257 args.mda_cr = cred;
1258 args.mda_uid = UID_UUCP;
1259 args.mda_gid = GID_DIALER;
1260 args.mda_mode = 0660;
1261 args.mda_unit = TTYUNIT_CALLOUT;
1262 args.mda_si_drv1 = tp;
1263 error = make_dev_s(&args, &cua, "cua%s", name);
1264 if (error != 0)
1265 goto fail;
1266 dev_depends(dev, cua);
1267
1268 /* Slave call-out devices. */
1269 if (tp->t_flags & TF_INITLOCK) {
1270 args.mda_devsw = &ttyil_cdevsw;
1271 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_INIT;
1272 args.mda_si_drv2 = &tp->t_termios_init_out;
1273 error = make_dev_s(&args, &cinit, "cua%s.init", name);
1274 if (error != 0)
1275 goto fail;
1276 dev_depends(dev, cinit);
1277
1278 args.mda_unit = TTYUNIT_CALLOUT | TTYUNIT_LOCK;
1279 args.mda_si_drv2 = &tp->t_termios_lock_out;
1280 error = make_dev_s(&args, &clock, "cua%s.lock", name);
1281 if (error != 0)
1282 goto fail;
1283 dev_depends(dev, clock);
1284 }
1285 }
1286
1287 sx_xlock(&tty_list_sx);
1288 TAILQ_INSERT_TAIL(&tty_list, tp, t_list);
1289 tty_list_count++;
1290 sx_xunlock(&tty_list_sx);
1291
1292 return (0);
1293
1294 fail:
1295 destroy_dev(dev);
1296 if (init)
1297 destroy_dev(init);
1298 if (lock)
1299 destroy_dev(lock);
1300 if (cinit)
1301 destroy_dev(cinit);
1302 if (clock)
1303 destroy_dev(clock);
1304
1305 return (error);
1306 }
1307
1308 /*
1309 * Signalling processes.
1310 */
1311
1312 void
tty_signal_sessleader(struct tty * tp,int sig)1313 tty_signal_sessleader(struct tty *tp, int sig)
1314 {
1315 struct proc *p;
1316
1317 tty_lock_assert(tp, MA_OWNED);
1318 MPASS(sig >= 1 && sig < NSIG);
1319
1320 /* Make signals start output again. */
1321 tp->t_flags &= ~TF_STOPPED;
1322
1323 if (tp->t_session != NULL && tp->t_session->s_leader != NULL) {
1324 p = tp->t_session->s_leader;
1325 PROC_LOCK(p);
1326 kern_psignal(p, sig);
1327 PROC_UNLOCK(p);
1328 }
1329 }
1330
1331 void
tty_signal_pgrp(struct tty * tp,int sig)1332 tty_signal_pgrp(struct tty *tp, int sig)
1333 {
1334 ksiginfo_t ksi;
1335
1336 tty_lock_assert(tp, MA_OWNED);
1337 MPASS(sig >= 1 && sig < NSIG);
1338
1339 /* Make signals start output again. */
1340 tp->t_flags &= ~TF_STOPPED;
1341
1342 if (sig == SIGINFO && !(tp->t_termios.c_lflag & NOKERNINFO))
1343 tty_info(tp);
1344 if (tp->t_pgrp != NULL) {
1345 ksiginfo_init(&ksi);
1346 ksi.ksi_signo = sig;
1347 ksi.ksi_code = SI_KERNEL;
1348 PGRP_LOCK(tp->t_pgrp);
1349 pgsignal(tp->t_pgrp, sig, 1, &ksi);
1350 PGRP_UNLOCK(tp->t_pgrp);
1351 }
1352 }
1353
1354 void
tty_wakeup(struct tty * tp,int flags)1355 tty_wakeup(struct tty *tp, int flags)
1356 {
1357 if (tp->t_flags & TF_ASYNC && tp->t_sigio != NULL)
1358 pgsigio(&tp->t_sigio, SIGIO, (tp->t_session != NULL));
1359
1360 if (flags & FWRITE) {
1361 cv_broadcast(&tp->t_outwait);
1362 selwakeup(&tp->t_outpoll);
1363 KNOTE_LOCKED(&tp->t_outpoll.si_note, 0);
1364 }
1365 if (flags & FREAD) {
1366 cv_broadcast(&tp->t_inwait);
1367 selwakeup(&tp->t_inpoll);
1368 KNOTE_LOCKED(&tp->t_inpoll.si_note, 0);
1369 }
1370 }
1371
1372 int
tty_wait(struct tty * tp,struct cv * cv)1373 tty_wait(struct tty *tp, struct cv *cv)
1374 {
1375 int error;
1376 int revokecnt = tp->t_revokecnt;
1377
1378 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1379 MPASS(!tty_gone(tp));
1380
1381 error = cv_wait_sig(cv, tp->t_mtx);
1382
1383 /* Bail out when the device slipped away. */
1384 if (tty_gone(tp))
1385 return (ENXIO);
1386
1387 /* Restart the system call when we may have been revoked. */
1388 if (tp->t_revokecnt != revokecnt)
1389 return (ERESTART);
1390
1391 return (error);
1392 }
1393
1394 int
tty_timedwait(struct tty * tp,struct cv * cv,int hz)1395 tty_timedwait(struct tty *tp, struct cv *cv, int hz)
1396 {
1397 int error;
1398 int revokecnt = tp->t_revokecnt;
1399
1400 tty_lock_assert(tp, MA_OWNED|MA_NOTRECURSED);
1401 MPASS(!tty_gone(tp));
1402
1403 error = cv_timedwait_sig(cv, tp->t_mtx, hz);
1404
1405 /* Bail out when the device slipped away. */
1406 if (tty_gone(tp))
1407 return (ENXIO);
1408
1409 /* Restart the system call when we may have been revoked. */
1410 if (tp->t_revokecnt != revokecnt)
1411 return (ERESTART);
1412
1413 return (error);
1414 }
1415
1416 void
tty_flush(struct tty * tp,int flags)1417 tty_flush(struct tty *tp, int flags)
1418 {
1419 if (flags & FWRITE) {
1420 tp->t_flags &= ~TF_HIWAT_OUT;
1421 ttyoutq_flush(&tp->t_outq);
1422 tty_wakeup(tp, FWRITE);
1423 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHWRITE);
1424 }
1425 if (flags & FREAD) {
1426 tty_hiwat_in_unblock(tp);
1427 ttyinq_flush(&tp->t_inq);
1428 ttydevsw_inwakeup(tp);
1429 ttydevsw_pktnotify(tp, TIOCPKT_FLUSHREAD);
1430 }
1431 }
1432
1433 void
tty_set_winsize(struct tty * tp,const struct winsize * wsz)1434 tty_set_winsize(struct tty *tp, const struct winsize *wsz)
1435 {
1436
1437 if (memcmp(&tp->t_winsize, wsz, sizeof(*wsz)) == 0)
1438 return;
1439 tp->t_winsize = *wsz;
1440 tty_signal_pgrp(tp, SIGWINCH);
1441 }
1442
1443 static int
tty_generic_ioctl(struct tty * tp,u_long cmd,void * data,int fflag,struct thread * td)1444 tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
1445 struct thread *td)
1446 {
1447 int error;
1448
1449 switch (cmd) {
1450 /*
1451 * Modem commands.
1452 * The SER_* and TIOCM_* flags are the same, but one bit
1453 * shifted. I don't know why.
1454 */
1455 case TIOCSDTR:
1456 ttydevsw_modem(tp, SER_DTR, 0);
1457 return (0);
1458 case TIOCCDTR:
1459 ttydevsw_modem(tp, 0, SER_DTR);
1460 return (0);
1461 case TIOCMSET: {
1462 int bits = *(int *)data;
1463 ttydevsw_modem(tp,
1464 (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1,
1465 ((~bits) & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1466 return (0);
1467 }
1468 case TIOCMBIS: {
1469 int bits = *(int *)data;
1470 ttydevsw_modem(tp, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1, 0);
1471 return (0);
1472 }
1473 case TIOCMBIC: {
1474 int bits = *(int *)data;
1475 ttydevsw_modem(tp, 0, (bits & (TIOCM_DTR | TIOCM_RTS)) >> 1);
1476 return (0);
1477 }
1478 case TIOCMGET:
1479 *(int *)data = TIOCM_LE + (ttydevsw_modem(tp, 0, 0) << 1);
1480 return (0);
1481
1482 case FIOASYNC:
1483 if (*(int *)data)
1484 tp->t_flags |= TF_ASYNC;
1485 else
1486 tp->t_flags &= ~TF_ASYNC;
1487 return (0);
1488 case FIONBIO:
1489 /* This device supports non-blocking operation. */
1490 return (0);
1491 case FIONREAD:
1492 *(int *)data = ttyinq_bytescanonicalized(&tp->t_inq);
1493 return (0);
1494 case FIONWRITE:
1495 case TIOCOUTQ:
1496 *(int *)data = ttyoutq_bytesused(&tp->t_outq);
1497 return (0);
1498 case FIOSETOWN:
1499 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1500 /* Not allowed to set ownership. */
1501 return (ENOTTY);
1502
1503 /* Temporarily unlock the TTY to set ownership. */
1504 tty_unlock(tp);
1505 error = fsetown(*(int *)data, &tp->t_sigio);
1506 tty_lock(tp);
1507 return (error);
1508 case FIOGETOWN:
1509 if (tp->t_session != NULL && !tty_is_ctty(tp, td->td_proc))
1510 /* Not allowed to set ownership. */
1511 return (ENOTTY);
1512
1513 /* Get ownership. */
1514 *(int *)data = fgetown(&tp->t_sigio);
1515 return (0);
1516 case TIOCGETA:
1517 /* Obtain terminal flags through tcgetattr(). */
1518 *(struct termios*)data = tp->t_termios;
1519 return (0);
1520 case TIOCSETA:
1521 case TIOCSETAW:
1522 case TIOCSETAF: {
1523 struct termios *t = data;
1524
1525 /*
1526 * Who makes up these funny rules? According to POSIX,
1527 * input baud rate is set equal to the output baud rate
1528 * when zero.
1529 */
1530 if (t->c_ispeed == 0)
1531 t->c_ispeed = t->c_ospeed;
1532
1533 /* Discard any unsupported bits. */
1534 t->c_iflag &= TTYSUP_IFLAG;
1535 t->c_oflag &= TTYSUP_OFLAG;
1536 t->c_lflag &= TTYSUP_LFLAG;
1537 t->c_cflag &= TTYSUP_CFLAG;
1538
1539 /* Set terminal flags through tcsetattr(). */
1540 if (cmd == TIOCSETAW || cmd == TIOCSETAF) {
1541 error = tty_drain(tp, 0);
1542 if (error)
1543 return (error);
1544 if (cmd == TIOCSETAF)
1545 tty_flush(tp, FREAD);
1546 }
1547
1548 /*
1549 * Only call param() when the flags really change.
1550 */
1551 if ((t->c_cflag & CIGNORE) == 0 &&
1552 (tp->t_termios.c_cflag != t->c_cflag ||
1553 ((tp->t_termios.c_iflag ^ t->c_iflag) &
1554 (IXON|IXOFF|IXANY)) ||
1555 tp->t_termios.c_ispeed != t->c_ispeed ||
1556 tp->t_termios.c_ospeed != t->c_ospeed)) {
1557 error = ttydevsw_param(tp, t);
1558 if (error)
1559 return (error);
1560
1561 /* XXX: CLOCAL? */
1562
1563 tp->t_termios.c_cflag = t->c_cflag & ~CIGNORE;
1564 tp->t_termios.c_ispeed = t->c_ispeed;
1565 tp->t_termios.c_ospeed = t->c_ospeed;
1566
1567 /* Baud rate has changed - update watermarks. */
1568 tty_watermarks(tp);
1569 }
1570
1571 /* Copy new non-device driver parameters. */
1572 tp->t_termios.c_iflag = t->c_iflag;
1573 tp->t_termios.c_oflag = t->c_oflag;
1574 tp->t_termios.c_lflag = t->c_lflag;
1575 memcpy(&tp->t_termios.c_cc, t->c_cc, sizeof t->c_cc);
1576
1577 ttydisc_optimize(tp);
1578
1579 if ((t->c_lflag & ICANON) == 0) {
1580 /*
1581 * When in non-canonical mode, wake up all
1582 * readers. Canonicalize any partial input. VMIN
1583 * and VTIME could also be adjusted.
1584 */
1585 ttyinq_canonicalize(&tp->t_inq);
1586 tty_wakeup(tp, FREAD);
1587 }
1588
1589 /*
1590 * For packet mode: notify the PTY consumer that VSTOP
1591 * and VSTART may have been changed.
1592 */
1593 if (tp->t_termios.c_iflag & IXON &&
1594 tp->t_termios.c_cc[VSTOP] == CTRL('S') &&
1595 tp->t_termios.c_cc[VSTART] == CTRL('Q'))
1596 ttydevsw_pktnotify(tp, TIOCPKT_DOSTOP);
1597 else
1598 ttydevsw_pktnotify(tp, TIOCPKT_NOSTOP);
1599 return (0);
1600 }
1601 case TIOCGETD:
1602 /* For compatibility - we only support TTYDISC. */
1603 *(int *)data = TTYDISC;
1604 return (0);
1605 case TIOCGPGRP:
1606 if (!tty_is_ctty(tp, td->td_proc))
1607 return (ENOTTY);
1608
1609 if (tp->t_pgrp != NULL)
1610 *(int *)data = tp->t_pgrp->pg_id;
1611 else
1612 *(int *)data = NO_PID;
1613 return (0);
1614 case TIOCGSID:
1615 if (!tty_is_ctty(tp, td->td_proc))
1616 return (ENOTTY);
1617
1618 MPASS(tp->t_session);
1619 *(int *)data = tp->t_session->s_sid;
1620 return (0);
1621 case TIOCSCTTY: {
1622 struct proc *p = td->td_proc;
1623
1624 /* XXX: This looks awful. */
1625 tty_unlock(tp);
1626 sx_xlock(&proctree_lock);
1627 tty_lock(tp);
1628
1629 if (!SESS_LEADER(p)) {
1630 /* Only the session leader may do this. */
1631 sx_xunlock(&proctree_lock);
1632 return (EPERM);
1633 }
1634
1635 if (tp->t_session != NULL && tp->t_session == p->p_session) {
1636 /* This is already our controlling TTY. */
1637 sx_xunlock(&proctree_lock);
1638 return (0);
1639 }
1640
1641 if (p->p_session->s_ttyp != NULL ||
1642 (tp->t_session != NULL && tp->t_session->s_ttyvp != NULL &&
1643 tp->t_session->s_ttyvp->v_type != VBAD)) {
1644 /*
1645 * There is already a relation between a TTY and
1646 * a session, or the caller is not the session
1647 * leader.
1648 *
1649 * Allow the TTY to be stolen when the vnode is
1650 * invalid, but the reference to the TTY is
1651 * still active. This allows immediate reuse of
1652 * TTYs of which the session leader has been
1653 * killed or the TTY revoked.
1654 */
1655 sx_xunlock(&proctree_lock);
1656 return (EPERM);
1657 }
1658
1659 /* Connect the session to the TTY. */
1660 tp->t_session = p->p_session;
1661 tp->t_session->s_ttyp = tp;
1662 tp->t_sessioncnt++;
1663 sx_xunlock(&proctree_lock);
1664
1665 /* Assign foreground process group. */
1666 tp->t_pgrp = p->p_pgrp;
1667 PROC_LOCK(p);
1668 p->p_flag |= P_CONTROLT;
1669 PROC_UNLOCK(p);
1670
1671 return (0);
1672 }
1673 case TIOCSPGRP: {
1674 struct pgrp *pg;
1675
1676 /*
1677 * XXX: Temporarily unlock the TTY to locate the process
1678 * group. This code would be lot nicer if we would ever
1679 * decompose proctree_lock.
1680 */
1681 tty_unlock(tp);
1682 sx_slock(&proctree_lock);
1683 pg = pgfind(*(int *)data);
1684 if (pg != NULL)
1685 PGRP_UNLOCK(pg);
1686 if (pg == NULL || pg->pg_session != td->td_proc->p_session) {
1687 sx_sunlock(&proctree_lock);
1688 tty_lock(tp);
1689 return (EPERM);
1690 }
1691 tty_lock(tp);
1692
1693 /*
1694 * Determine if this TTY is the controlling TTY after
1695 * relocking the TTY.
1696 */
1697 if (!tty_is_ctty(tp, td->td_proc)) {
1698 sx_sunlock(&proctree_lock);
1699 return (ENOTTY);
1700 }
1701 tp->t_pgrp = pg;
1702 sx_sunlock(&proctree_lock);
1703
1704 /* Wake up the background process groups. */
1705 cv_broadcast(&tp->t_bgwait);
1706 return (0);
1707 }
1708 case TIOCFLUSH: {
1709 int flags = *(int *)data;
1710
1711 if (flags == 0)
1712 flags = (FREAD|FWRITE);
1713 else
1714 flags &= (FREAD|FWRITE);
1715 tty_flush(tp, flags);
1716 return (0);
1717 }
1718 case TIOCDRAIN:
1719 /* Drain TTY output. */
1720 return tty_drain(tp, 0);
1721 case TIOCCONS:
1722 /* Set terminal as console TTY. */
1723 if (*(int *)data) {
1724 error = priv_check(td, PRIV_TTY_CONSOLE);
1725 if (error)
1726 return (error);
1727
1728 /*
1729 * XXX: constty should really need to be locked!
1730 * XXX: allow disconnected constty's to be stolen!
1731 */
1732
1733 if (constty == tp)
1734 return (0);
1735 if (constty != NULL)
1736 return (EBUSY);
1737
1738 tty_unlock(tp);
1739 constty_set(tp);
1740 tty_lock(tp);
1741 } else if (constty == tp) {
1742 constty_clear();
1743 }
1744 return (0);
1745 case TIOCGWINSZ:
1746 /* Obtain window size. */
1747 *(struct winsize*)data = tp->t_winsize;
1748 return (0);
1749 case TIOCSWINSZ:
1750 /* Set window size. */
1751 tty_set_winsize(tp, data);
1752 return (0);
1753 case TIOCEXCL:
1754 tp->t_flags |= TF_EXCLUDE;
1755 return (0);
1756 case TIOCNXCL:
1757 tp->t_flags &= ~TF_EXCLUDE;
1758 return (0);
1759 case TIOCSTOP:
1760 tp->t_flags |= TF_STOPPED;
1761 ttydevsw_pktnotify(tp, TIOCPKT_STOP);
1762 return (0);
1763 case TIOCSTART:
1764 tp->t_flags &= ~TF_STOPPED;
1765 ttydevsw_outwakeup(tp);
1766 ttydevsw_pktnotify(tp, TIOCPKT_START);
1767 return (0);
1768 case TIOCSTAT:
1769 tty_info(tp);
1770 return (0);
1771 case TIOCSTI:
1772 if ((fflag & FREAD) == 0 && priv_check(td, PRIV_TTY_STI))
1773 return (EPERM);
1774 if (!tty_is_ctty(tp, td->td_proc) &&
1775 priv_check(td, PRIV_TTY_STI))
1776 return (EACCES);
1777 ttydisc_rint(tp, *(char *)data, 0);
1778 ttydisc_rint_done(tp);
1779 return (0);
1780 }
1781
1782 #ifdef COMPAT_43TTY
1783 return tty_ioctl_compat(tp, cmd, data, fflag, td);
1784 #else /* !COMPAT_43TTY */
1785 return (ENOIOCTL);
1786 #endif /* COMPAT_43TTY */
1787 }
1788
1789 int
tty_ioctl(struct tty * tp,u_long cmd,void * data,int fflag,struct thread * td)1790 tty_ioctl(struct tty *tp, u_long cmd, void *data, int fflag, struct thread *td)
1791 {
1792 int error;
1793
1794 tty_lock_assert(tp, MA_OWNED);
1795
1796 if (tty_gone(tp))
1797 return (ENXIO);
1798
1799 error = ttydevsw_ioctl(tp, cmd, data, td);
1800 if (error == ENOIOCTL)
1801 error = tty_generic_ioctl(tp, cmd, data, fflag, td);
1802
1803 return (error);
1804 }
1805
1806 dev_t
tty_udev(struct tty * tp)1807 tty_udev(struct tty *tp)
1808 {
1809 if (tp->t_dev)
1810 return dev2udev(tp->t_dev);
1811 else
1812 return NODEV;
1813 }
1814
1815 int
tty_checkoutq(struct tty * tp)1816 tty_checkoutq(struct tty *tp)
1817 {
1818
1819 /* 256 bytes should be enough to print a log message. */
1820 return (ttyoutq_bytesleft(&tp->t_outq) >= 256);
1821 }
1822
1823 void
tty_hiwat_in_block(struct tty * tp)1824 tty_hiwat_in_block(struct tty *tp)
1825 {
1826
1827 if ((tp->t_flags & TF_HIWAT_IN) == 0 &&
1828 tp->t_termios.c_iflag & IXOFF &&
1829 tp->t_termios.c_cc[VSTOP] != _POSIX_VDISABLE) {
1830 /*
1831 * Input flow control. Only enter the high watermark when we
1832 * can successfully store the VSTOP character.
1833 */
1834 if (ttyoutq_write_nofrag(&tp->t_outq,
1835 &tp->t_termios.c_cc[VSTOP], 1) == 0)
1836 tp->t_flags |= TF_HIWAT_IN;
1837 } else {
1838 /* No input flow control. */
1839 tp->t_flags |= TF_HIWAT_IN;
1840 }
1841 }
1842
1843 void
tty_hiwat_in_unblock(struct tty * tp)1844 tty_hiwat_in_unblock(struct tty *tp)
1845 {
1846
1847 if (tp->t_flags & TF_HIWAT_IN &&
1848 tp->t_termios.c_iflag & IXOFF &&
1849 tp->t_termios.c_cc[VSTART] != _POSIX_VDISABLE) {
1850 /*
1851 * Input flow control. Only leave the high watermark when we
1852 * can successfully store the VSTART character.
1853 */
1854 if (ttyoutq_write_nofrag(&tp->t_outq,
1855 &tp->t_termios.c_cc[VSTART], 1) == 0)
1856 tp->t_flags &= ~TF_HIWAT_IN;
1857 } else {
1858 /* No input flow control. */
1859 tp->t_flags &= ~TF_HIWAT_IN;
1860 }
1861
1862 if (!tty_gone(tp))
1863 ttydevsw_inwakeup(tp);
1864 }
1865
1866 /*
1867 * TTY hooks interface.
1868 */
1869
1870 static int
ttyhook_defrint(struct tty * tp,char c,int flags)1871 ttyhook_defrint(struct tty *tp, char c, int flags)
1872 {
1873
1874 if (ttyhook_rint_bypass(tp, &c, 1) != 1)
1875 return (-1);
1876
1877 return (0);
1878 }
1879
1880 int
ttyhook_register(struct tty ** rtp,struct proc * p,int fd,struct ttyhook * th,void * softc)1881 ttyhook_register(struct tty **rtp, struct proc *p, int fd,
1882 struct ttyhook *th, void *softc)
1883 {
1884 struct tty *tp;
1885 struct file *fp;
1886 struct cdev *dev;
1887 struct cdevsw *cdp;
1888 struct filedesc *fdp;
1889 cap_rights_t rights;
1890 int error, ref;
1891
1892 /* Validate the file descriptor. */
1893 fdp = p->p_fd;
1894 error = fget_unlocked(fdp, fd, cap_rights_init(&rights, CAP_TTYHOOK),
1895 &fp, NULL);
1896 if (error != 0)
1897 return (error);
1898 if (fp->f_ops == &badfileops) {
1899 error = EBADF;
1900 goto done1;
1901 }
1902
1903 /*
1904 * Make sure the vnode is bound to a character device.
1905 * Unlocked check for the vnode type is ok there, because we
1906 * only shall prevent calling devvn_refthread on the file that
1907 * never has been opened over a character device.
1908 */
1909 if (fp->f_type != DTYPE_VNODE || fp->f_vnode->v_type != VCHR) {
1910 error = EINVAL;
1911 goto done1;
1912 }
1913
1914 /* Make sure it is a TTY. */
1915 cdp = devvn_refthread(fp->f_vnode, &dev, &ref);
1916 if (cdp == NULL) {
1917 error = ENXIO;
1918 goto done1;
1919 }
1920 if (dev != fp->f_data) {
1921 error = ENXIO;
1922 goto done2;
1923 }
1924 if (cdp != &ttydev_cdevsw) {
1925 error = ENOTTY;
1926 goto done2;
1927 }
1928 tp = dev->si_drv1;
1929
1930 /* Try to attach the hook to the TTY. */
1931 error = EBUSY;
1932 tty_lock(tp);
1933 MPASS((tp->t_hook == NULL) == ((tp->t_flags & TF_HOOK) == 0));
1934 if (tp->t_flags & TF_HOOK)
1935 goto done3;
1936
1937 tp->t_flags |= TF_HOOK;
1938 tp->t_hook = th;
1939 tp->t_hooksoftc = softc;
1940 *rtp = tp;
1941 error = 0;
1942
1943 /* Maybe we can switch into bypass mode now. */
1944 ttydisc_optimize(tp);
1945
1946 /* Silently convert rint() calls to rint_bypass() when possible. */
1947 if (!ttyhook_hashook(tp, rint) && ttyhook_hashook(tp, rint_bypass))
1948 th->th_rint = ttyhook_defrint;
1949
1950 done3: tty_unlock(tp);
1951 done2: dev_relthread(dev, ref);
1952 done1: fdrop(fp, curthread);
1953 return (error);
1954 }
1955
1956 void
ttyhook_unregister(struct tty * tp)1957 ttyhook_unregister(struct tty *tp)
1958 {
1959
1960 tty_lock_assert(tp, MA_OWNED);
1961 MPASS(tp->t_flags & TF_HOOK);
1962
1963 /* Disconnect the hook. */
1964 tp->t_flags &= ~TF_HOOK;
1965 tp->t_hook = NULL;
1966
1967 /* Maybe we need to leave bypass mode. */
1968 ttydisc_optimize(tp);
1969
1970 /* Maybe deallocate the TTY as well. */
1971 tty_rel_free(tp);
1972 }
1973
1974 /*
1975 * /dev/console handling.
1976 */
1977
1978 static int
ttyconsdev_open(struct cdev * dev,int oflags,int devtype,struct thread * td)1979 ttyconsdev_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
1980 {
1981 struct tty *tp;
1982
1983 /* System has no console device. */
1984 if (dev_console_filename == NULL)
1985 return (ENXIO);
1986
1987 /* Look up corresponding TTY by device name. */
1988 sx_slock(&tty_list_sx);
1989 TAILQ_FOREACH(tp, &tty_list, t_list) {
1990 if (strcmp(dev_console_filename, tty_devname(tp)) == 0) {
1991 dev_console->si_drv1 = tp;
1992 break;
1993 }
1994 }
1995 sx_sunlock(&tty_list_sx);
1996
1997 /* System console has no TTY associated. */
1998 if (dev_console->si_drv1 == NULL)
1999 return (ENXIO);
2000
2001 return (ttydev_open(dev, oflags, devtype, td));
2002 }
2003
2004 static int
ttyconsdev_write(struct cdev * dev,struct uio * uio,int ioflag)2005 ttyconsdev_write(struct cdev *dev, struct uio *uio, int ioflag)
2006 {
2007
2008 log_console(uio);
2009
2010 return (ttydev_write(dev, uio, ioflag));
2011 }
2012
2013 /*
2014 * /dev/console is a little different than normal TTY's. When opened,
2015 * it determines which TTY to use. When data gets written to it, it
2016 * will be logged in the kernel message buffer.
2017 */
2018 static struct cdevsw ttyconsdev_cdevsw = {
2019 .d_version = D_VERSION,
2020 .d_open = ttyconsdev_open,
2021 .d_close = ttydev_close,
2022 .d_read = ttydev_read,
2023 .d_write = ttyconsdev_write,
2024 .d_ioctl = ttydev_ioctl,
2025 .d_kqfilter = ttydev_kqfilter,
2026 .d_poll = ttydev_poll,
2027 .d_mmap = ttydev_mmap,
2028 .d_name = "ttyconsdev",
2029 .d_flags = D_TTY,
2030 };
2031
2032 static void
ttyconsdev_init(void * unused)2033 ttyconsdev_init(void *unused)
2034 {
2035
2036 dev_console = make_dev_credf(MAKEDEV_ETERNAL, &ttyconsdev_cdevsw, 0,
2037 NULL, UID_ROOT, GID_WHEEL, 0600, "console");
2038 }
2039
2040 SYSINIT(tty, SI_SUB_DRIVERS, SI_ORDER_FIRST, ttyconsdev_init, NULL);
2041
2042 void
ttyconsdev_select(const char * name)2043 ttyconsdev_select(const char *name)
2044 {
2045
2046 dev_console_filename = name;
2047 }
2048
2049 /*
2050 * Debugging routines.
2051 */
2052
2053 #include "opt_ddb.h"
2054 #ifdef DDB
2055 #include <ddb/ddb.h>
2056 #include <ddb/db_sym.h>
2057
2058 static struct {
2059 int flag;
2060 char val;
2061 } ttystates[] = {
2062 #if 0
2063 { TF_NOPREFIX, 'N' },
2064 #endif
2065 { TF_INITLOCK, 'I' },
2066 { TF_CALLOUT, 'C' },
2067
2068 /* Keep these together -> 'Oi' and 'Oo'. */
2069 { TF_OPENED, 'O' },
2070 { TF_OPENED_IN, 'i' },
2071 { TF_OPENED_OUT, 'o' },
2072 { TF_OPENED_CONS, 'c' },
2073
2074 { TF_GONE, 'G' },
2075 { TF_OPENCLOSE, 'B' },
2076 { TF_ASYNC, 'Y' },
2077 { TF_LITERAL, 'L' },
2078
2079 /* Keep these together -> 'Hi' and 'Ho'. */
2080 { TF_HIWAT, 'H' },
2081 { TF_HIWAT_IN, 'i' },
2082 { TF_HIWAT_OUT, 'o' },
2083
2084 { TF_STOPPED, 'S' },
2085 { TF_EXCLUDE, 'X' },
2086 { TF_BYPASS, 'l' },
2087 { TF_ZOMBIE, 'Z' },
2088 { TF_HOOK, 's' },
2089
2090 /* Keep these together -> 'bi' and 'bo'. */
2091 { TF_BUSY, 'b' },
2092 { TF_BUSY_IN, 'i' },
2093 { TF_BUSY_OUT, 'o' },
2094
2095 { 0, '\0'},
2096 };
2097
2098 #define TTY_FLAG_BITS \
2099 "\20\1NOPREFIX\2INITLOCK\3CALLOUT\4OPENED_IN\5OPENED_OUT\6GONE" \
2100 "\7OPENCLOSE\10ASYNC\11LITERAL\12HIWAT_IN\13HIWAT_OUT\14STOPPED" \
2101 "\15EXCLUDE\16BYPASS\17ZOMBIE\20HOOK"
2102
2103 #define DB_PRINTSYM(name, addr) \
2104 db_printf("%s " #name ": ", sep); \
2105 db_printsym((db_addr_t) addr, DB_STGY_ANY); \
2106 db_printf("\n");
2107
2108 static void
_db_show_devsw(const char * sep,const struct ttydevsw * tsw)2109 _db_show_devsw(const char *sep, const struct ttydevsw *tsw)
2110 {
2111 db_printf("%sdevsw: ", sep);
2112 db_printsym((db_addr_t)tsw, DB_STGY_ANY);
2113 db_printf(" (%p)\n", tsw);
2114 DB_PRINTSYM(open, tsw->tsw_open);
2115 DB_PRINTSYM(close, tsw->tsw_close);
2116 DB_PRINTSYM(outwakeup, tsw->tsw_outwakeup);
2117 DB_PRINTSYM(inwakeup, tsw->tsw_inwakeup);
2118 DB_PRINTSYM(ioctl, tsw->tsw_ioctl);
2119 DB_PRINTSYM(param, tsw->tsw_param);
2120 DB_PRINTSYM(modem, tsw->tsw_modem);
2121 DB_PRINTSYM(mmap, tsw->tsw_mmap);
2122 DB_PRINTSYM(pktnotify, tsw->tsw_pktnotify);
2123 DB_PRINTSYM(free, tsw->tsw_free);
2124 }
2125 static void
_db_show_hooks(const char * sep,const struct ttyhook * th)2126 _db_show_hooks(const char *sep, const struct ttyhook *th)
2127 {
2128 db_printf("%shook: ", sep);
2129 db_printsym((db_addr_t)th, DB_STGY_ANY);
2130 db_printf(" (%p)\n", th);
2131 if (th == NULL)
2132 return;
2133 DB_PRINTSYM(rint, th->th_rint);
2134 DB_PRINTSYM(rint_bypass, th->th_rint_bypass);
2135 DB_PRINTSYM(rint_done, th->th_rint_done);
2136 DB_PRINTSYM(rint_poll, th->th_rint_poll);
2137 DB_PRINTSYM(getc_inject, th->th_getc_inject);
2138 DB_PRINTSYM(getc_capture, th->th_getc_capture);
2139 DB_PRINTSYM(getc_poll, th->th_getc_poll);
2140 DB_PRINTSYM(close, th->th_close);
2141 }
2142
2143 static void
_db_show_termios(const char * name,const struct termios * t)2144 _db_show_termios(const char *name, const struct termios *t)
2145 {
2146
2147 db_printf("%s: iflag 0x%x oflag 0x%x cflag 0x%x "
2148 "lflag 0x%x ispeed %u ospeed %u\n", name,
2149 t->c_iflag, t->c_oflag, t->c_cflag, t->c_lflag,
2150 t->c_ispeed, t->c_ospeed);
2151 }
2152
2153 /* DDB command to show TTY statistics. */
DB_SHOW_COMMAND(tty,db_show_tty)2154 DB_SHOW_COMMAND(tty, db_show_tty)
2155 {
2156 struct tty *tp;
2157
2158 if (!have_addr) {
2159 db_printf("usage: show tty <addr>\n");
2160 return;
2161 }
2162 tp = (struct tty *)addr;
2163
2164 db_printf("0x%p: %s\n", tp, tty_devname(tp));
2165 db_printf("\tmtx: %p\n", tp->t_mtx);
2166 db_printf("\tflags: %b\n", tp->t_flags, TTY_FLAG_BITS);
2167 db_printf("\trevokecnt: %u\n", tp->t_revokecnt);
2168
2169 /* Buffering mechanisms. */
2170 db_printf("\tinq: %p begin %u linestart %u reprint %u end %u "
2171 "nblocks %u quota %u\n", &tp->t_inq, tp->t_inq.ti_begin,
2172 tp->t_inq.ti_linestart, tp->t_inq.ti_reprint, tp->t_inq.ti_end,
2173 tp->t_inq.ti_nblocks, tp->t_inq.ti_quota);
2174 db_printf("\toutq: %p begin %u end %u nblocks %u quota %u\n",
2175 &tp->t_outq, tp->t_outq.to_begin, tp->t_outq.to_end,
2176 tp->t_outq.to_nblocks, tp->t_outq.to_quota);
2177 db_printf("\tinlow: %zu\n", tp->t_inlow);
2178 db_printf("\toutlow: %zu\n", tp->t_outlow);
2179 _db_show_termios("\ttermios", &tp->t_termios);
2180 db_printf("\twinsize: row %u col %u xpixel %u ypixel %u\n",
2181 tp->t_winsize.ws_row, tp->t_winsize.ws_col,
2182 tp->t_winsize.ws_xpixel, tp->t_winsize.ws_ypixel);
2183 db_printf("\tcolumn: %u\n", tp->t_column);
2184 db_printf("\twritepos: %u\n", tp->t_writepos);
2185 db_printf("\tcompatflags: 0x%x\n", tp->t_compatflags);
2186
2187 /* Init/lock-state devices. */
2188 _db_show_termios("\ttermios_init_in", &tp->t_termios_init_in);
2189 _db_show_termios("\ttermios_init_out", &tp->t_termios_init_out);
2190 _db_show_termios("\ttermios_lock_in", &tp->t_termios_lock_in);
2191 _db_show_termios("\ttermios_lock_out", &tp->t_termios_lock_out);
2192
2193 /* Hooks */
2194 _db_show_devsw("\t", tp->t_devsw);
2195 _db_show_hooks("\t", tp->t_hook);
2196
2197 /* Process info. */
2198 db_printf("\tpgrp: %p gid %d jobc %d\n", tp->t_pgrp,
2199 tp->t_pgrp ? tp->t_pgrp->pg_id : 0,
2200 tp->t_pgrp ? tp->t_pgrp->pg_jobc : 0);
2201 db_printf("\tsession: %p", tp->t_session);
2202 if (tp->t_session != NULL)
2203 db_printf(" count %u leader %p tty %p sid %d login %s",
2204 tp->t_session->s_count, tp->t_session->s_leader,
2205 tp->t_session->s_ttyp, tp->t_session->s_sid,
2206 tp->t_session->s_login);
2207 db_printf("\n");
2208 db_printf("\tsessioncnt: %u\n", tp->t_sessioncnt);
2209 db_printf("\tdevswsoftc: %p\n", tp->t_devswsoftc);
2210 db_printf("\thooksoftc: %p\n", tp->t_hooksoftc);
2211 db_printf("\tdev: %p\n", tp->t_dev);
2212 }
2213
2214 /* DDB command to list TTYs. */
DB_SHOW_ALL_COMMAND(ttys,db_show_all_ttys)2215 DB_SHOW_ALL_COMMAND(ttys, db_show_all_ttys)
2216 {
2217 struct tty *tp;
2218 size_t isiz, osiz;
2219 int i, j;
2220
2221 /* Make the output look like `pstat -t'. */
2222 db_printf("PTR ");
2223 #if defined(__LP64__)
2224 db_printf(" ");
2225 #endif
2226 db_printf(" LINE INQ CAN LIN LOW OUTQ USE LOW "
2227 "COL SESS PGID STATE\n");
2228
2229 TAILQ_FOREACH(tp, &tty_list, t_list) {
2230 isiz = tp->t_inq.ti_nblocks * TTYINQ_DATASIZE;
2231 osiz = tp->t_outq.to_nblocks * TTYOUTQ_DATASIZE;
2232
2233 db_printf("%p %10s %5zu %4u %4u %4zu %5zu %4u %4zu %5u %5d %5d ",
2234 tp,
2235 tty_devname(tp),
2236 isiz,
2237 tp->t_inq.ti_linestart - tp->t_inq.ti_begin,
2238 tp->t_inq.ti_end - tp->t_inq.ti_linestart,
2239 isiz - tp->t_inlow,
2240 osiz,
2241 tp->t_outq.to_end - tp->t_outq.to_begin,
2242 osiz - tp->t_outlow,
2243 MIN(tp->t_column, 99999),
2244 tp->t_session ? tp->t_session->s_sid : 0,
2245 tp->t_pgrp ? tp->t_pgrp->pg_id : 0);
2246
2247 /* Flag bits. */
2248 for (i = j = 0; ttystates[i].flag; i++)
2249 if (tp->t_flags & ttystates[i].flag) {
2250 db_printf("%c", ttystates[i].val);
2251 j++;
2252 }
2253 if (j == 0)
2254 db_printf("-");
2255 db_printf("\n");
2256 }
2257 }
2258 #endif /* DDB */
2259