2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
34 * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $
35 * $DragonFly: src/sys/kern/tty_pty.c,v 1.21 2008/08/13 10:29:38 swildner Exp $
39 * Pseudo-teletype Driver
40 * (Actually two drivers, requiring two dev_ops structures)
42 #include "use_pty.h" /* XXX */
43 #include "opt_compat.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48 #include <sys/ioctl_compat.h>
54 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/vnode.h>
58 #include <sys/signalvar.h>
59 #include <sys/malloc.h>
60 #include <sys/device.h>
61 #include <sys/thread2.h>
62 #include <sys/devfs.h>
64 #include <sys/sysctl.h>
68 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
70 static void ptsstart (struct tty *tp);
71 static void ptsstop (struct tty *tp, int rw);
72 static void ptcwakeup (struct tty *tp, int flag);
73 static void ptyinit (int n);
75 static d_open_t ptsopen;
76 static d_close_t ptsclose;
77 static d_read_t ptsread;
78 static d_write_t ptswrite;
79 static d_ioctl_t ptyioctl;
80 static d_open_t ptcopen;
81 static d_close_t ptcclose;
82 static d_read_t ptcread;
83 static d_write_t ptcwrite;
84 static d_poll_t ptcpoll;
87 DEVFS_DECLARE_CLONE_BITMAP(pty);
89 static d_clone_t ptyclone;
91 static int pty_debug_level = 0;
93 static struct dev_ops pts98_ops = {
94 { "pts98", 0, D_TTY | D_KQFILTER },
101 .d_kqfilter = ttykqfilter,
102 .d_revoke = ttyrevoke
105 static struct dev_ops ptc98_ops = {
106 { "ptc98", 0, D_TTY | D_KQFILTER | D_MASTER },
113 .d_kqfilter = ttykqfilter,
114 .d_revoke = ttyrevoke
118 #define CDEV_MAJOR_S 5
119 static struct dev_ops pts_ops = {
120 { "pts", CDEV_MAJOR_S, D_TTY | D_KQFILTER },
127 .d_kqfilter = ttykqfilter,
128 .d_revoke = ttyrevoke
131 #define CDEV_MAJOR_C 6
132 static struct dev_ops ptc_ops = {
133 { "ptc", CDEV_MAJOR_C, D_TTY | D_KQFILTER | D_MASTER },
140 .d_kqfilter = ttykqfilter,
141 .d_revoke = ttyrevoke
144 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
149 struct selinfo pt_selr, pt_selw;
154 struct prison *pt_prison;
157 #define PF_PKT 0x08 /* packet mode */
158 #define PF_STOPPED 0x10 /* user told stopped */
159 #define PF_REMOTE 0x20 /* remote and flow controlled input */
160 #define PF_NOSTOP 0x40
161 #define PF_UCNTL 0x80 /* user control mode */
163 #define PF_UNIX98 0x01
164 #define PF_SOPEN 0x02
165 #define PF_MOPEN 0x04
168 ptydebug(int level, char *fmt, ...)
173 if (level <= pty_debug_level)
181 * This function creates and initializes a pts/ptc pair
183 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
184 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
186 * XXX: define and add mapping of upper minor bits to allow more
193 char *names = "pqrsPQRS";
196 /* For now we only map the lower 8 bits of the minor */
200 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
201 pt->devs = devs = make_dev(&pts_ops, n,
202 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
203 pt->devc = devc = make_dev(&ptc_ops, n,
204 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
206 devs->si_drv1 = devc->si_drv1 = pt;
207 devs->si_tty = devc->si_tty = &pt->pt_tty;
208 devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
209 devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
210 pt->pt_tty.t_dev = devs;
211 ttyregister(&pt->pt_tty);
216 ptyclone(struct dev_clone_args *ap)
222 * Limit the number of unix98 pty (slave) devices to 1000, as
223 * the utmp(5) format only allows for 8 bytes for the tty,
225 * If this limit is reached, we don't clone and return error
228 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 1000);
235 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
237 pt->devc = ap->a_dev = make_only_dev(&ptc98_ops, unit, ap->a_cred->cr_ruid,
238 0, 0600, "ptm/%d", unit);
239 pt->devs = make_dev(&pts98_ops, unit, ap->a_cred->cr_ruid, GID_TTY, 0620,
242 pt->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
243 pt->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
245 pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
246 pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
247 pt->pt_tty.t_dev = pt->devs;
248 pt->pt_flags2 |= PF_UNIX98;
250 ttyregister(&pt->pt_tty);
258 ptsopen(struct dev_open_args *ap)
260 cdev_t dev = ap->a_head.a_dev;
263 struct pt_ioctl *pti;
271 if ((tp->t_state & TS_ISOPEN) == 0) {
272 ttychars(tp); /* Set up default chars */
273 tp->t_iflag = TTYDEF_IFLAG;
274 tp->t_oflag = TTYDEF_OFLAG;
275 tp->t_lflag = TTYDEF_LFLAG;
276 tp->t_cflag = TTYDEF_CFLAG;
277 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
278 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
280 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
283 if (tp->t_oproc) /* Ctrlr still around. */
284 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
285 while ((tp->t_state & TS_CARR_ON) == 0) {
286 if (ap->a_oflags & FNONBLOCK)
288 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
292 error = (*linesw[tp->t_line].l_open)(dev, tp);
294 ptcwakeup(tp, FREAD|FWRITE);
299 * On open of the slave, we set the corresponding flag in the common
302 ptydebug(1, "ptsopen=%s | unix98? %s\n", dev->si_name,
303 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
305 if ((!error) && (pti->pt_flags2 & PF_UNIX98)) {
306 pti->pt_flags2 |= PF_SOPEN;
314 ptsclose(struct dev_close_args *ap)
316 cdev_t dev = ap->a_head.a_dev;
318 struct pt_ioctl *pti = dev->si_drv1;
322 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
323 ptsstop(tp, FREAD|FWRITE);
329 * On close of the slave, we unset the corresponding flag, and if the master
330 * isn't open anymore, we destroy the slave and unset the unit.
332 ptydebug(1, "ptsclose=%s | unix98? %s\n", dev->si_name,
333 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
335 if (pti->pt_flags2 & PF_UNIX98) {
336 pti->pt_flags2 &= ~PF_SOPEN;
337 KKASSERT((pti->pt_flags2 & PF_SOPEN) == 0);
338 ptydebug(1, "master open? %s\n",
339 (pti->pt_flags2 & PF_MOPEN)?"yes":"no");
341 if (!(pti->pt_flags2 & PF_SOPEN) && !(pti->pt_flags2 & PF_MOPEN)) {
342 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
352 ptsread(struct dev_read_args *ap)
354 cdev_t dev = ap->a_head.a_dev;
355 struct proc *p = curproc;
356 struct tty *tp = dev->si_tty;
357 struct pt_ioctl *pti = dev->si_drv1;
362 lp = curthread->td_lwp;
365 if (pti->pt_flags & PF_REMOTE) {
366 while (isbackground(p, tp)) {
367 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
368 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
369 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
371 pgsignal(p->p_pgrp, SIGTTIN, 1);
372 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
376 if (tp->t_canq.c_cc == 0) {
377 if (ap->a_ioflag & IO_NDELAY)
378 return (EWOULDBLOCK);
379 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
385 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
386 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
390 if (tp->t_canq.c_cc == 1)
391 clist_getc(&tp->t_canq);
396 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
397 ptcwakeup(tp, FWRITE);
402 * Write to pseudo-tty.
403 * Wakeups of controlling tty will happen
404 * indirectly, when tty driver calls ptsstart.
407 ptswrite(struct dev_write_args *ap)
409 cdev_t dev = ap->a_head.a_dev;
413 if (tp->t_oproc == 0)
415 return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
419 * Start output on pseudo-tty.
420 * Wake up process selecting or sleeping for input from controlling tty.
423 ptsstart(struct tty *tp)
425 struct pt_ioctl *pti = tp->t_dev->si_drv1;
427 if (tp->t_state & TS_TTSTOP)
429 if (pti->pt_flags & PF_STOPPED) {
430 pti->pt_flags &= ~PF_STOPPED;
431 pti->pt_send = TIOCPKT_START;
433 ptcwakeup(tp, FREAD);
437 ptcwakeup(struct tty *tp, int flag)
439 struct pt_ioctl *pti = tp->t_dev->si_drv1;
442 selwakeup(&pti->pt_selr);
443 wakeup(TSA_PTC_READ(tp));
446 selwakeup(&pti->pt_selw);
447 wakeup(TSA_PTC_WRITE(tp));
452 ptcopen(struct dev_open_args *ap)
454 cdev_t dev = ap->a_head.a_dev;
456 struct pt_ioctl *pti;
463 if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison)
468 tp->t_oproc = ptsstart;
469 tp->t_stop = ptsstop;
470 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
471 tp->t_lflag &= ~EXTPROC;
472 pti->pt_prison = ap->a_cred->cr_prison;
477 pti->devs->si_uid = ap->a_cred->cr_uid;
478 pti->devs->si_gid = 0;
479 pti->devs->si_perms = 0600;
480 pti->devc->si_uid = ap->a_cred->cr_uid;
481 pti->devc->si_gid = 0;
482 pti->devc->si_perms = 0600;
487 * On open of the master, we set the corresponding flag in the common
490 ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev->si_name,
491 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
493 if (pti->pt_flags2 & PF_UNIX98) {
494 pti->pt_flags2 |= PF_MOPEN;
502 ptcclose(struct dev_close_args *ap)
504 cdev_t dev = ap->a_head.a_dev;
506 struct pt_ioctl *pti = dev->si_drv1;
509 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
514 * On close of the master, we unset the corresponding flag in the common
517 pti->pt_flags2 &= ~PF_MOPEN;
521 * XXX MDMBUF makes no sense for ptys but would inhibit the above
522 * l_modem(). CLOCAL makes sense but isn't supported. Special
523 * l_modem()s that ignore carrier drop make no sense for ptys but
524 * may be in use because other parts of the line discipline make
525 * sense for ptys. Recover by doing everything that a normal
526 * ttymodem() would have done except for sending a SIGHUP.
528 if (tp->t_state & TS_ISOPEN) {
529 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
530 tp->t_state |= TS_ZOMBIE;
531 ttyflush(tp, FREAD | FWRITE);
533 tp->t_oproc = 0; /* mark closed */
536 pti->pt_prison = NULL;
537 pti->devs->si_uid = 0;
538 pti->devs->si_gid = 0;
539 pti->devs->si_perms = 0666;
540 pti->devc->si_uid = 0;
541 pti->devc->si_gid = 0;
542 pti->devc->si_perms = 0666;
547 * On close of the master, we destroy the master and, if no slaves are open,
548 * we destroy the slave device and unset the unit.
550 ptydebug(1, "ptcclose=%s (master) | unix98? %s\n", dev->si_name,
551 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
552 if (pti->pt_flags2 & PF_UNIX98) {
553 KKASSERT((pti->pt_flags2 & PF_MOPEN) == 0);
557 if (!(pti->pt_flags2 & PF_SOPEN)) {
558 ptydebug(1, "ptcclose: slaves are not open\n");
559 destroy_dev(pti->devs);
560 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
569 ptcread(struct dev_read_args *ap)
571 cdev_t dev = ap->a_head.a_dev;
572 struct tty *tp = dev->si_tty;
573 struct pt_ioctl *pti = dev->si_drv1;
578 * We want to block until the slave
579 * is open, and there's something to read;
580 * but if we lost the slave or we're NBIO,
581 * then return the appropriate error instead.
584 if (tp->t_state&TS_ISOPEN) {
585 if (pti->pt_flags&PF_PKT && pti->pt_send) {
586 error = ureadc((int)pti->pt_send, ap->a_uio);
589 if (pti->pt_send & TIOCPKT_IOCTL) {
590 cc = (int)szmin(ap->a_uio->uio_resid,
591 sizeof(tp->t_termios));
592 uiomove((caddr_t)&tp->t_termios, cc,
598 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
599 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
605 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
608 if ((tp->t_state & TS_CONNECTED) == 0)
609 return (0); /* EOF */
610 if (ap->a_ioflag & IO_NDELAY)
611 return (EWOULDBLOCK);
612 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
616 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
617 error = ureadc(0, ap->a_uio);
618 while (ap->a_uio->uio_resid > 0 && error == 0) {
619 cc = q_to_b(&tp->t_outq, buf,
620 (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
623 error = uiomove(buf, (size_t)cc, ap->a_uio);
630 ptsstop(struct tty *tp, int flush)
632 struct pt_ioctl *pti = tp->t_dev->si_drv1;
635 /* note: FLUSHREAD and FLUSHWRITE already ok */
637 flush = TIOCPKT_STOP;
638 pti->pt_flags |= PF_STOPPED;
640 pti->pt_flags &= ~PF_STOPPED;
641 pti->pt_send |= flush;
642 /* change of perspective */
652 ptcpoll(struct dev_poll_args *ap)
654 cdev_t dev = ap->a_head.a_dev;
655 struct tty *tp = dev->si_tty;
656 struct pt_ioctl *pti = dev->si_drv1;
659 if ((tp->t_state & TS_CONNECTED) == 0) {
660 ap->a_events = seltrue(dev, ap->a_events) | POLLHUP;
665 * Need to block timeouts (ttrstart).
669 if (ap->a_events & (POLLIN | POLLRDNORM))
670 if ((tp->t_state & TS_ISOPEN) &&
671 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
672 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
673 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
674 revents |= ap->a_events & (POLLIN | POLLRDNORM);
676 if (ap->a_events & (POLLOUT | POLLWRNORM))
677 if (tp->t_state & TS_ISOPEN &&
678 ((pti->pt_flags & PF_REMOTE) ?
679 (tp->t_canq.c_cc == 0) :
680 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
681 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
682 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
684 if (ap->a_events & POLLHUP)
685 if ((tp->t_state & TS_CARR_ON) == 0)
689 if (ap->a_events & (POLLIN | POLLRDNORM))
690 selrecord(curthread, &pti->pt_selr);
692 if (ap->a_events & (POLLOUT | POLLWRNORM))
693 selrecord(curthread, &pti->pt_selw);
697 ap->a_events = revents;
702 ptcwrite(struct dev_write_args *ap)
704 cdev_t dev = ap->a_head.a_dev;
705 struct tty *tp = dev->si_tty;
708 u_char locbuf[BUFSIZ];
710 struct pt_ioctl *pti = dev->si_drv1;
714 if ((tp->t_state&TS_ISOPEN) == 0)
716 if (pti->pt_flags & PF_REMOTE) {
719 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
720 tp->t_canq.c_cc < TTYHOG - 1) {
722 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
723 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
725 error = uiomove(cp, (size_t)cc, ap->a_uio);
728 /* check again for safety */
729 if ((tp->t_state & TS_ISOPEN) == 0) {
730 /* adjust as usual */
731 ap->a_uio->uio_resid += cc;
736 cc = b_to_q((char *)cp, cc, &tp->t_canq);
738 * XXX we don't guarantee that the canq size
739 * is >= TTYHOG, so the above b_to_q() may
740 * leave some bytes uncopied. However, space
741 * is guaranteed for the null terminator if
742 * we don't fail here since (TTYHOG - 1) is
743 * not a multiple of CBSIZE.
749 /* adjust for data copied in but not written */
750 ap->a_uio->uio_resid += cc;
751 clist_putc(0, &tp->t_canq);
753 wakeup(TSA_PTS_READ(tp));
756 while (ap->a_uio->uio_resid > 0 || cc > 0) {
758 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
760 error = uiomove(cp, (size_t)cc, ap->a_uio);
763 /* check again for safety */
764 if ((tp->t_state & TS_ISOPEN) == 0) {
765 /* adjust for data copied in but not written */
766 ap->a_uio->uio_resid += cc;
771 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
772 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
773 wakeup(TSA_HUP_OR_INPUT(tp));
776 (*linesw[tp->t_line].l_rint)(*cp++, tp);
785 * Come here to wait for slave to open, for space
786 * in outq, or space in rawq, or an empty canq.
788 if ((tp->t_state & TS_CONNECTED) == 0) {
789 /* adjust for data copied in but not written */
790 ap->a_uio->uio_resid += cc;
793 if (ap->a_ioflag & IO_NDELAY) {
794 /* adjust for data copied in but not written */
795 ap->a_uio->uio_resid += cc;
797 return (EWOULDBLOCK);
800 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
802 /* adjust for data copied in but not written */
803 ap->a_uio->uio_resid += cc;
811 ptyioctl(struct dev_ioctl_args *ap)
813 cdev_t dev = ap->a_head.a_dev;
814 struct tty *tp = dev->si_tty;
815 struct pt_ioctl *pti = dev->si_drv1;
816 u_char *cc = tp->t_cc;
819 if (dev_dflags(dev) & D_MASTER) {
824 * We avoid calling ttioctl on the controller since,
825 * in that case, tp must be the controlling terminal.
827 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
831 if (*(int *)ap->a_data) {
832 if (pti->pt_flags & PF_UCNTL)
834 pti->pt_flags |= PF_PKT;
836 pti->pt_flags &= ~PF_PKT;
840 if (*(int *)ap->a_data) {
841 if (pti->pt_flags & PF_PKT)
843 pti->pt_flags |= PF_UCNTL;
845 pti->pt_flags &= ~PF_UCNTL;
849 if (*(int *)ap->a_data)
850 pti->pt_flags |= PF_REMOTE;
852 pti->pt_flags &= ~PF_REMOTE;
853 ttyflush(tp, FREAD|FWRITE);
857 if ((pti->pt_flags2 & PF_UNIX98) && (pti->devc == dev))
864 * The rest of the ioctls shouldn't be called until
867 if ((tp->t_state & TS_ISOPEN) == 0)
880 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
881 * ttywflush(tp) will hang if there are characters in
884 ndflush(&tp->t_outq, tp->t_outq.c_cc);
888 if (*(unsigned int *)ap->a_data >= NSIG ||
889 *(unsigned int *)ap->a_data == 0)
891 if ((tp->t_lflag&NOFLSH) == 0)
892 ttyflush(tp, FREAD|FWRITE);
893 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
894 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
895 ((tp->t_lflag&NOKERNINFO) == 0))
900 if (ap->a_cmd == TIOCEXT) {
902 * When the EXTPROC bit is being toggled, we need
903 * to send an TIOCPKT_IOCTL if the packet driver
906 if (*(int *)ap->a_data) {
907 if (pti->pt_flags & PF_PKT) {
908 pti->pt_send |= TIOCPKT_IOCTL;
909 ptcwakeup(tp, FREAD);
911 tp->t_lflag |= EXTPROC;
913 if ((tp->t_lflag & EXTPROC) &&
914 (pti->pt_flags & PF_PKT)) {
915 pti->pt_send |= TIOCPKT_IOCTL;
916 ptcwakeup(tp, FREAD);
918 tp->t_lflag &= ~EXTPROC;
922 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
923 ap->a_fflag, ap->a_cred);
924 if (error == ENOIOCTL)
925 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
926 if (error == ENOIOCTL) {
927 if (pti->pt_flags & PF_UCNTL &&
928 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
929 if (ap->a_cmd & 0xff) {
930 pti->pt_ucntl = (u_char)ap->a_cmd;
931 ptcwakeup(tp, FREAD);
938 * If external processing and packet mode send ioctl packet.
940 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
949 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
956 pti->pt_send |= TIOCPKT_IOCTL;
957 ptcwakeup(tp, FREAD);
962 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
963 && CCEQ(cc[VSTART], CTRL('q'));
964 if (pti->pt_flags & PF_NOSTOP) {
966 pti->pt_send &= ~TIOCPKT_NOSTOP;
967 pti->pt_send |= TIOCPKT_DOSTOP;
968 pti->pt_flags &= ~PF_NOSTOP;
969 ptcwakeup(tp, FREAD);
973 pti->pt_send &= ~TIOCPKT_DOSTOP;
974 pti->pt_send |= TIOCPKT_NOSTOP;
975 pti->pt_flags |= PF_NOSTOP;
976 ptcwakeup(tp, FREAD);
983 static void ptc_drvinit (void *unused);
986 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
987 0, "Change pty debug level");
991 ptc_drvinit(void *unused)
998 * Create the clonable base device.
1000 make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1001 0, 0, 0666, "ptmx");
1004 for (i = 0; i < 256; i++) {
1009 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)