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>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58 #include <sys/malloc.h>
59 #include <sys/device.h>
60 #include <sys/thread2.h>
61 #include <sys/devfs.h>
63 #include <sys/sysctl.h>
67 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
69 static void ptsstart (struct tty *tp);
70 static void ptsstop (struct tty *tp, int rw);
71 static void ptcwakeup (struct tty *tp, int flag);
72 static void ptyinit (int n);
73 static int filt_ptcread (struct knote *kn, long hint);
74 static void filt_ptcrdetach (struct knote *kn);
75 static int filt_ptcwrite (struct knote *kn, long hint);
76 static void filt_ptcwdetach (struct knote *kn);
78 static d_open_t ptsopen;
79 static d_close_t ptsclose;
80 static d_read_t ptsread;
81 static d_write_t ptswrite;
82 static d_ioctl_t ptyioctl;
83 static d_open_t ptcopen;
84 static d_close_t ptcclose;
85 static d_read_t ptcread;
86 static d_write_t ptcwrite;
87 static d_kqfilter_t ptckqfilter;
90 DEVFS_DECLARE_CLONE_BITMAP(pty);
92 static d_clone_t ptyclone;
94 static int pty_debug_level = 0;
96 static struct dev_ops pts98_ops = {
97 { "pts98", 0, D_TTY },
103 .d_kqfilter = ttykqfilter,
104 .d_revoke = ttyrevoke
107 static struct dev_ops ptc98_ops = {
108 { "ptc98", 0, D_TTY | D_MASTER },
114 .d_kqfilter = ptckqfilter,
115 .d_revoke = ttyrevoke
119 #define CDEV_MAJOR_S 5
120 static struct dev_ops pts_ops = {
121 { "pts", CDEV_MAJOR_S, D_TTY },
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_MASTER },
139 .d_kqfilter = ptckqfilter,
140 .d_revoke = ttyrevoke
143 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
148 int pt_refs; /* Structural references interlock S/MOPEN */
150 struct kqinfo pt_kqr, pt_kqw;
155 struct prison *pt_prison;
158 #define PF_PKT 0x08 /* packet mode */
159 #define PF_STOPPED 0x10 /* user told stopped */
160 #define PF_REMOTE 0x20 /* remote and flow controlled input */
161 #define PF_NOSTOP 0x40
162 #define PF_UCNTL 0x80 /* user control mode */
164 #define PF_UNIX98 0x01
165 #define PF_SOPEN 0x02
166 #define PF_MOPEN 0x04
167 #define PF_TERMINATED 0x08
170 ptydebug(int level, char *fmt, ...)
175 if (level <= pty_debug_level)
183 * This function creates and initializes a pts/ptc pair
185 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
186 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
188 * XXX: define and add mapping of upper minor bits to allow more
195 char *names = "pqrsPQRS";
198 /* For now we only map the lower 8 bits of the minor */
202 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
203 pt->devs = devs = make_dev(&pts_ops, n,
204 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
205 pt->devc = devc = make_dev(&ptc_ops, n,
206 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
208 devs->si_drv1 = devc->si_drv1 = pt;
209 devs->si_tty = devc->si_tty = &pt->pt_tty;
210 devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
211 devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
212 pt->pt_tty.t_dev = devs;
214 ttyregister(&pt->pt_tty);
219 ptyclone(struct dev_clone_args *ap)
225 * Limit the number of unix98 pty (slave) devices to 1000, as
226 * the utmp(5) format only allows for 8 bytes for the tty,
228 * If this limit is reached, we don't clone and return error
231 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 1000);
238 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
240 pt->devc = make_only_dev(&ptc98_ops, unit,
242 0, 0600, "ptm/%d", unit);
243 pt->devs = make_dev(&pts98_ops, unit,
245 GID_TTY, 0620, "pts/%d", unit);
246 ap->a_dev = pt->devc;
248 pt->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
249 pt->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
251 pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
252 pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
253 pt->pt_tty.t_dev = pt->devs;
254 pt->pt_flags2 |= PF_UNIX98;
255 pt->pt_uminor = unit;
257 ttyregister(&pt->pt_tty);
264 * pti_hold() prevents the pti from being destroyed due to a termination
265 * while a pt*open() is blocked.
267 * This function returns non-zero if we cannot hold due to a termination
271 pti_hold(struct pt_ioctl *pti)
273 if (pti->pt_flags2 & PF_TERMINATED)
280 * pti_done() releases the reference and checks to see if both sides have
281 * been closed on a unix98 pty, allowing us to destroy the device and
284 * We do not release resources on non-unix98 ptys. Those are left
285 * statically allocated.
288 pti_done(struct pt_ioctl *pti)
290 if (--pti->pt_refs == 0) {
296 * Only unix09 ptys are freed up
298 if ((pti->pt_flags2 & PF_UNIX98) == 0)
302 * Interlock open attempts against termination by setting
303 * PF_TERMINATED. This allows us to block while cleaning
304 * out the device infrastructure.
306 * Do not terminate the tty if it still has a session
307 * association (t_refs).
309 if ((pti->pt_flags2 & (PF_SOPEN|PF_MOPEN)) == 0 &&
310 pti->pt_tty.t_refs == 0) {
311 pti->pt_flags2 |= PF_TERMINATED;
312 uminor_no = pti->pt_uminor;
314 if ((dev = pti->devs) != NULL) {
319 if ((dev = pti->devc) != NULL) {
324 ttyunregister(&pti->pt_tty);
325 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty),
335 ptsopen(struct dev_open_args *ap)
337 cdev_t dev = ap->a_head.a_dev;
340 struct pt_ioctl *pti;
343 * The pti will already be assigned by the clone code or
344 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
345 * we are somehow racing a unix98 termination.
347 if (dev->si_drv1 == NULL)
353 if ((tp->t_state & TS_ISOPEN) == 0) {
354 ttychars(tp); /* Set up default chars */
355 tp->t_iflag = TTYDEF_IFLAG;
356 tp->t_oflag = TTYDEF_OFLAG;
357 tp->t_lflag = TTYDEF_LFLAG;
358 tp->t_cflag = TTYDEF_CFLAG;
359 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
360 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
363 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
367 if (tp->t_oproc) /* Ctrlr still around. */
368 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
369 while ((tp->t_state & TS_CARR_ON) == 0) {
370 if (ap->a_oflags & FNONBLOCK)
372 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
378 tp->t_state &= ~TS_ZOMBIE;
379 error = (*linesw[tp->t_line].l_open)(dev, tp);
381 ptcwakeup(tp, FREAD|FWRITE);
386 * On open of the slave, we set the corresponding flag in the common
389 ptydebug(1, "ptsopen=%s | unix98? %s\n", dev->si_name,
390 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
392 if (error == 0 && (pti->pt_flags2 & PF_UNIX98)) {
393 pti->pt_flags2 |= PF_SOPEN;
402 ptsclose(struct dev_close_args *ap)
404 cdev_t dev = ap->a_head.a_dev;
406 struct pt_ioctl *pti = dev->si_drv1;
410 panic("ptsclose on terminated pti");
412 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
413 ptsstop(tp, FREAD|FWRITE);
414 (void) ttyclose(tp); /* clears t_state */
415 tp->t_state |= TS_ZOMBIE;
420 * On close of the slave, we unset the corresponding flag, and if the master
421 * isn't open anymore, we destroy the slave and unset the unit.
423 ptydebug(1, "ptsclose=%s | unix98? %s\n", dev->si_name,
424 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
426 if (pti->pt_flags2 & PF_UNIX98) {
427 pti->pt_flags2 &= ~PF_SOPEN;
428 KKASSERT((pti->pt_flags2 & PF_SOPEN) == 0);
429 ptydebug(1, "master open? %s\n",
430 (pti->pt_flags2 & PF_MOPEN)?"yes":"no");
438 ptsread(struct dev_read_args *ap)
440 cdev_t dev = ap->a_head.a_dev;
441 struct proc *p = curproc;
442 struct tty *tp = dev->si_tty;
443 struct pt_ioctl *pti = dev->si_drv1;
448 lp = curthread->td_lwp;
451 if (pti->pt_flags & PF_REMOTE) {
452 while (isbackground(p, tp)) {
453 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
454 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
455 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
457 pgsignal(p->p_pgrp, SIGTTIN, 1);
458 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
462 if (tp->t_canq.c_cc == 0) {
463 if (ap->a_ioflag & IO_NDELAY)
464 return (EWOULDBLOCK);
465 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
471 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
472 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
476 if (tp->t_canq.c_cc == 1)
477 clist_getc(&tp->t_canq);
482 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
483 ptcwakeup(tp, FWRITE);
488 * Write to pseudo-tty.
489 * Wakeups of controlling tty will happen
490 * indirectly, when tty driver calls ptsstart.
493 ptswrite(struct dev_write_args *ap)
495 cdev_t dev = ap->a_head.a_dev;
499 if (tp->t_oproc == 0)
501 return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
505 * Start output on pseudo-tty.
506 * Wake up process selecting or sleeping for input from controlling tty.
509 ptsstart(struct tty *tp)
511 struct pt_ioctl *pti = tp->t_dev->si_drv1;
513 if (tp->t_state & TS_TTSTOP)
516 if (pti->pt_flags & PF_STOPPED) {
517 pti->pt_flags &= ~PF_STOPPED;
518 pti->pt_send = TIOCPKT_START;
521 ptcwakeup(tp, FREAD);
525 ptcwakeup(struct tty *tp, int flag)
528 wakeup(TSA_PTC_READ(tp));
529 KNOTE(&tp->t_rkq.ki_note, 0);
532 wakeup(TSA_PTC_WRITE(tp));
533 KNOTE(&tp->t_wkq.ki_note, 0);
538 ptcopen(struct dev_open_args *ap)
540 cdev_t dev = ap->a_head.a_dev;
542 struct pt_ioctl *pti;
545 * The pti will already be assigned by the clone code or
546 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
547 * we are somehow racing a unix98 termination.
549 if (dev->si_drv1 == NULL)
554 if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison) {
563 tp->t_oproc = ptsstart;
564 tp->t_stop = ptsstop;
565 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
566 tp->t_lflag &= ~EXTPROC;
567 pti->pt_prison = ap->a_cred->cr_prison;
572 pti->devs->si_uid = ap->a_cred->cr_uid;
573 pti->devs->si_gid = 0;
574 pti->devs->si_perms = 0600;
575 pti->devc->si_uid = ap->a_cred->cr_uid;
576 pti->devc->si_gid = 0;
577 pti->devc->si_perms = 0600;
582 * On open of the master, we set the corresponding flag in the common
585 ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev->si_name,
586 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
588 if (pti->pt_flags2 & PF_UNIX98) {
589 pti->pt_flags2 |= PF_MOPEN;
598 ptcclose(struct dev_close_args *ap)
600 cdev_t dev = ap->a_head.a_dev;
602 struct pt_ioctl *pti = dev->si_drv1;
605 panic("ptcclose on terminated pti");
608 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
611 * XXX MDMBUF makes no sense for ptys but would inhibit the above
612 * l_modem(). CLOCAL makes sense but isn't supported. Special
613 * l_modem()s that ignore carrier drop make no sense for ptys but
614 * may be in use because other parts of the line discipline make
615 * sense for ptys. Recover by doing everything that a normal
616 * ttymodem() would have done except for sending a SIGHUP.
618 if (tp->t_state & TS_ISOPEN) {
619 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
620 tp->t_state |= TS_ZOMBIE;
621 ttyflush(tp, FREAD | FWRITE);
623 tp->t_oproc = 0; /* mark closed */
628 * On close of the master, we unset the corresponding flag in the common
631 pti->pt_flags2 &= ~PF_MOPEN;
634 pti->pt_prison = NULL;
635 pti->devs->si_uid = 0;
636 pti->devs->si_gid = 0;
637 pti->devs->si_perms = 0666;
638 pti->devc->si_uid = 0;
639 pti->devc->si_gid = 0;
640 pti->devc->si_perms = 0666;
647 ptcread(struct dev_read_args *ap)
649 cdev_t dev = ap->a_head.a_dev;
650 struct tty *tp = dev->si_tty;
651 struct pt_ioctl *pti = dev->si_drv1;
656 * We want to block until the slave
657 * is open, and there's something to read;
658 * but if we lost the slave or we're NBIO,
659 * then return the appropriate error instead.
662 if (tp->t_state&TS_ISOPEN) {
663 if (pti->pt_flags&PF_PKT && pti->pt_send) {
664 error = ureadc((int)pti->pt_send, ap->a_uio);
667 if (pti->pt_send & TIOCPKT_IOCTL) {
668 cc = (int)szmin(ap->a_uio->uio_resid,
669 sizeof(tp->t_termios));
670 uiomove((caddr_t)&tp->t_termios, cc,
676 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
677 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
683 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
686 if ((tp->t_state & TS_CONNECTED) == 0)
687 return (0); /* EOF */
688 if (ap->a_ioflag & IO_NDELAY)
689 return (EWOULDBLOCK);
690 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
694 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
695 error = ureadc(0, ap->a_uio);
696 while (ap->a_uio->uio_resid > 0 && error == 0) {
697 cc = q_to_b(&tp->t_outq, buf,
698 (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
701 error = uiomove(buf, (size_t)cc, ap->a_uio);
708 ptsstop(struct tty *tp, int flush)
710 struct pt_ioctl *pti = tp->t_dev->si_drv1;
713 /* note: FLUSHREAD and FLUSHWRITE already ok */
716 flush = TIOCPKT_STOP;
717 pti->pt_flags |= PF_STOPPED;
719 pti->pt_flags &= ~PF_STOPPED;
721 pti->pt_send |= flush;
722 /* change of perspective */
733 * kqueue ops for pseudo-terminals.
735 static struct filterops ptcread_filtops =
736 { FILTEROP_ISFD, NULL, filt_ptcrdetach, filt_ptcread };
737 static struct filterops ptcwrite_filtops =
738 { FILTEROP_ISFD, NULL, filt_ptcwdetach, filt_ptcwrite };
741 ptckqfilter(struct dev_kqfilter_args *ap)
743 cdev_t dev = ap->a_head.a_dev;
744 struct knote *kn = ap->a_kn;
745 struct tty *tp = dev->si_tty;
749 switch (kn->kn_filter) {
751 klist = &tp->t_rkq.ki_note;
752 kn->kn_fop = &ptcread_filtops;
755 klist = &tp->t_wkq.ki_note;
756 kn->kn_fop = &ptcwrite_filtops;
759 ap->a_result = EOPNOTSUPP;
763 kn->kn_hook = (caddr_t)dev;
764 knote_insert(klist, kn);
769 filt_ptcread (struct knote *kn, long hint)
771 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
772 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
774 if (tp->t_state & TS_ZOMBIE) {
775 kn->kn_flags |= EV_EOF;
779 if ((tp->t_state & TS_ISOPEN) &&
780 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
781 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
782 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
783 kn->kn_data = tp->t_outq.c_cc;
791 filt_ptcwrite (struct knote *kn, long hint)
793 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
794 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
796 if (tp->t_state & TS_ZOMBIE) {
797 kn->kn_flags |= EV_EOF;
801 if (tp->t_state & TS_ISOPEN &&
802 ((pti->pt_flags & PF_REMOTE) ?
803 (tp->t_canq.c_cc == 0) :
804 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
805 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
806 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
814 filt_ptcrdetach (struct knote *kn)
816 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
818 knote_remove(&tp->t_rkq.ki_note, kn);
822 filt_ptcwdetach (struct knote *kn)
824 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
826 knote_remove(&tp->t_wkq.ki_note, kn);
833 ptcwrite(struct dev_write_args *ap)
835 cdev_t dev = ap->a_head.a_dev;
836 struct tty *tp = dev->si_tty;
839 u_char locbuf[BUFSIZ];
841 struct pt_ioctl *pti = dev->si_drv1;
845 if ((tp->t_state&TS_ISOPEN) == 0)
847 if (pti->pt_flags & PF_REMOTE) {
850 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
851 tp->t_canq.c_cc < TTYHOG - 1) {
853 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
854 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
856 error = uiomove(cp, (size_t)cc, ap->a_uio);
859 /* check again for safety */
860 if ((tp->t_state & TS_ISOPEN) == 0) {
861 /* adjust as usual */
862 ap->a_uio->uio_resid += cc;
867 cc = b_to_q((char *)cp, cc, &tp->t_canq);
869 * XXX we don't guarantee that the canq size
870 * is >= TTYHOG, so the above b_to_q() may
871 * leave some bytes uncopied. However, space
872 * is guaranteed for the null terminator if
873 * we don't fail here since (TTYHOG - 1) is
874 * not a multiple of CBSIZE.
880 /* adjust for data copied in but not written */
881 ap->a_uio->uio_resid += cc;
882 clist_putc(0, &tp->t_canq);
884 wakeup(TSA_PTS_READ(tp));
887 while (ap->a_uio->uio_resid > 0 || cc > 0) {
889 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
891 error = uiomove(cp, (size_t)cc, ap->a_uio);
894 /* check again for safety */
895 if ((tp->t_state & TS_ISOPEN) == 0) {
896 /* adjust for data copied in but not written */
897 ap->a_uio->uio_resid += cc;
902 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
903 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
904 wakeup(TSA_HUP_OR_INPUT(tp));
907 (*linesw[tp->t_line].l_rint)(*cp++, tp);
916 * Come here to wait for slave to open, for space
917 * in outq, or space in rawq, or an empty canq.
919 if ((tp->t_state & TS_CONNECTED) == 0) {
920 /* adjust for data copied in but not written */
921 ap->a_uio->uio_resid += cc;
924 if (ap->a_ioflag & IO_NDELAY) {
925 /* adjust for data copied in but not written */
926 ap->a_uio->uio_resid += cc;
928 return (EWOULDBLOCK);
931 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
933 /* adjust for data copied in but not written */
934 ap->a_uio->uio_resid += cc;
942 ptyioctl(struct dev_ioctl_args *ap)
944 cdev_t dev = ap->a_head.a_dev;
945 struct tty *tp = dev->si_tty;
946 struct pt_ioctl *pti = dev->si_drv1;
947 u_char *cc = tp->t_cc;
950 if (dev_dflags(dev) & D_MASTER) {
955 * We avoid calling ttioctl on the controller since,
956 * in that case, tp must be the controlling terminal.
958 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
962 if (*(int *)ap->a_data) {
963 if (pti->pt_flags & PF_UCNTL)
965 pti->pt_flags |= PF_PKT;
967 pti->pt_flags &= ~PF_PKT;
971 if (*(int *)ap->a_data) {
972 if (pti->pt_flags & PF_PKT)
974 pti->pt_flags |= PF_UCNTL;
976 pti->pt_flags &= ~PF_UCNTL;
980 if (*(int *)ap->a_data)
981 pti->pt_flags |= PF_REMOTE;
983 pti->pt_flags &= ~PF_REMOTE;
984 ttyflush(tp, FREAD|FWRITE);
988 if ((pti->pt_flags2 & PF_UNIX98) && (pti->devc == dev))
995 * The rest of the ioctls shouldn't be called until
998 if ((tp->t_state & TS_ISOPEN) == 0)
1001 switch (ap->a_cmd) {
1011 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1012 * ttywflush(tp) will hang if there are characters in
1015 ndflush(&tp->t_outq, tp->t_outq.c_cc);
1019 if (*(unsigned int *)ap->a_data >= NSIG ||
1020 *(unsigned int *)ap->a_data == 0)
1022 if ((tp->t_lflag&NOFLSH) == 0)
1023 ttyflush(tp, FREAD|FWRITE);
1024 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
1025 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
1026 ((tp->t_lflag&NOKERNINFO) == 0))
1031 if (ap->a_cmd == TIOCEXT) {
1033 * When the EXTPROC bit is being toggled, we need
1034 * to send an TIOCPKT_IOCTL if the packet driver
1037 if (*(int *)ap->a_data) {
1038 if (pti->pt_flags & PF_PKT) {
1039 pti->pt_send |= TIOCPKT_IOCTL;
1040 ptcwakeup(tp, FREAD);
1042 tp->t_lflag |= EXTPROC;
1044 if ((tp->t_lflag & EXTPROC) &&
1045 (pti->pt_flags & PF_PKT)) {
1046 pti->pt_send |= TIOCPKT_IOCTL;
1047 ptcwakeup(tp, FREAD);
1049 tp->t_lflag &= ~EXTPROC;
1053 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1054 ap->a_fflag, ap->a_cred);
1055 if (error == ENOIOCTL)
1056 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1057 if (error == ENOIOCTL) {
1058 if (pti->pt_flags & PF_UCNTL &&
1059 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
1060 if (ap->a_cmd & 0xff) {
1061 pti->pt_ucntl = (u_char)ap->a_cmd;
1062 ptcwakeup(tp, FREAD);
1069 * If external processing and packet mode send ioctl packet.
1071 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
1080 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1087 pti->pt_send |= TIOCPKT_IOCTL;
1088 ptcwakeup(tp, FREAD);
1093 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1094 && CCEQ(cc[VSTART], CTRL('q'));
1095 if (pti->pt_flags & PF_NOSTOP) {
1097 pti->pt_send &= ~TIOCPKT_NOSTOP;
1098 pti->pt_send |= TIOCPKT_DOSTOP;
1099 pti->pt_flags &= ~PF_NOSTOP;
1100 ptcwakeup(tp, FREAD);
1104 pti->pt_send &= ~TIOCPKT_DOSTOP;
1105 pti->pt_send |= TIOCPKT_NOSTOP;
1106 pti->pt_flags |= PF_NOSTOP;
1107 ptcwakeup(tp, FREAD);
1114 static void ptc_drvinit (void *unused);
1117 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1118 0, "Change pty debug level");
1122 ptc_drvinit(void *unused)
1129 * Create the clonable base device.
1131 make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1132 0, 0, 0666, "ptmx");
1135 for (i = 0; i < 256; i++) {
1140 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)