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 | D_KQFILTER },
103 .d_kqfilter = ttykqfilter,
104 .d_revoke = ttyrevoke
107 static struct dev_ops ptc98_ops = {
108 { "ptc98", 0, D_TTY | D_KQFILTER | 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 | 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 },
139 .d_kqfilter = ptckqfilter,
140 .d_revoke = ttyrevoke
143 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
148 struct kqinfo pt_kqr, pt_kqw;
153 struct prison *pt_prison;
156 #define PF_PKT 0x08 /* packet mode */
157 #define PF_STOPPED 0x10 /* user told stopped */
158 #define PF_REMOTE 0x20 /* remote and flow controlled input */
159 #define PF_NOSTOP 0x40
160 #define PF_UCNTL 0x80 /* user control mode */
162 #define PF_UNIX98 0x01
163 #define PF_SOPEN 0x02
164 #define PF_MOPEN 0x04
167 ptydebug(int level, char *fmt, ...)
172 if (level <= pty_debug_level)
180 * This function creates and initializes a pts/ptc pair
182 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
183 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
185 * XXX: define and add mapping of upper minor bits to allow more
192 char *names = "pqrsPQRS";
195 /* For now we only map the lower 8 bits of the minor */
199 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
200 pt->devs = devs = make_dev(&pts_ops, n,
201 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
202 pt->devc = devc = make_dev(&ptc_ops, n,
203 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
205 devs->si_drv1 = devc->si_drv1 = pt;
206 devs->si_tty = devc->si_tty = &pt->pt_tty;
207 devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
208 devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
209 pt->pt_tty.t_dev = devs;
210 ttyregister(&pt->pt_tty);
215 ptyclone(struct dev_clone_args *ap)
221 * Limit the number of unix98 pty (slave) devices to 1000, as
222 * the utmp(5) format only allows for 8 bytes for the tty,
224 * If this limit is reached, we don't clone and return error
227 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 1000);
234 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
236 pt->devc = ap->a_dev = make_only_dev(&ptc98_ops, unit, ap->a_cred->cr_ruid,
237 0, 0600, "ptm/%d", unit);
238 pt->devs = make_dev(&pts98_ops, unit, ap->a_cred->cr_ruid, GID_TTY, 0620,
241 pt->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
242 pt->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
244 pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
245 pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
246 pt->pt_tty.t_dev = pt->devs;
247 pt->pt_flags2 |= PF_UNIX98;
249 ttyregister(&pt->pt_tty);
257 ptsopen(struct dev_open_args *ap)
259 cdev_t dev = ap->a_head.a_dev;
262 struct pt_ioctl *pti;
270 if ((tp->t_state & TS_ISOPEN) == 0) {
271 ttychars(tp); /* Set up default chars */
272 tp->t_iflag = TTYDEF_IFLAG;
273 tp->t_oflag = TTYDEF_OFLAG;
274 tp->t_lflag = TTYDEF_LFLAG;
275 tp->t_cflag = TTYDEF_CFLAG;
276 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
277 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
279 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
282 if (tp->t_oproc) /* Ctrlr still around. */
283 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
284 while ((tp->t_state & TS_CARR_ON) == 0) {
285 if (ap->a_oflags & FNONBLOCK)
287 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
291 tp->t_state &= ~TS_ZOMBIE;
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;
323 uminor_no = dev->si_uminor;
324 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
325 ptsstop(tp, FREAD|FWRITE);
326 (void) ttyclose(tp); /* clears t_state */
327 tp->t_state |= TS_ZOMBIE;
332 * On close of the slave, we unset the corresponding flag, and if the master
333 * isn't open anymore, we destroy the slave and unset the unit.
335 ptydebug(1, "ptsclose=%s | unix98? %s\n", dev->si_name,
336 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
338 if (pti->pt_flags2 & PF_UNIX98) {
339 pti->pt_flags2 &= ~PF_SOPEN;
340 KKASSERT((pti->pt_flags2 & PF_SOPEN) == 0);
341 ptydebug(1, "master open? %s\n",
342 (pti->pt_flags2 & PF_MOPEN)?"yes":"no");
344 if (!(pti->pt_flags2 & PF_SOPEN) &&
345 !(pti->pt_flags2 & PF_MOPEN)) {
347 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty),
349 ttyunregister(&pti->pt_tty);
359 ptsread(struct dev_read_args *ap)
361 cdev_t dev = ap->a_head.a_dev;
362 struct proc *p = curproc;
363 struct tty *tp = dev->si_tty;
364 struct pt_ioctl *pti = dev->si_drv1;
369 lp = curthread->td_lwp;
372 if (pti->pt_flags & PF_REMOTE) {
373 while (isbackground(p, tp)) {
374 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
375 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
376 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
378 pgsignal(p->p_pgrp, SIGTTIN, 1);
379 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
383 if (tp->t_canq.c_cc == 0) {
384 if (ap->a_ioflag & IO_NDELAY)
385 return (EWOULDBLOCK);
386 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
392 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
393 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
397 if (tp->t_canq.c_cc == 1)
398 clist_getc(&tp->t_canq);
403 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
404 ptcwakeup(tp, FWRITE);
409 * Write to pseudo-tty.
410 * Wakeups of controlling tty will happen
411 * indirectly, when tty driver calls ptsstart.
414 ptswrite(struct dev_write_args *ap)
416 cdev_t dev = ap->a_head.a_dev;
420 if (tp->t_oproc == 0)
422 return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
426 * Start output on pseudo-tty.
427 * Wake up process selecting or sleeping for input from controlling tty.
430 ptsstart(struct tty *tp)
432 struct pt_ioctl *pti = tp->t_dev->si_drv1;
434 if (tp->t_state & TS_TTSTOP)
436 if (pti->pt_flags & PF_STOPPED) {
437 pti->pt_flags &= ~PF_STOPPED;
438 pti->pt_send = TIOCPKT_START;
440 ptcwakeup(tp, FREAD);
444 ptcwakeup(struct tty *tp, int flag)
447 wakeup(TSA_PTC_READ(tp));
448 KNOTE(&tp->t_rkq.ki_note, 0);
451 wakeup(TSA_PTC_WRITE(tp));
452 KNOTE(&tp->t_wkq.ki_note, 0);
457 ptcopen(struct dev_open_args *ap)
459 cdev_t dev = ap->a_head.a_dev;
461 struct pt_ioctl *pti;
468 if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison)
473 tp->t_oproc = ptsstart;
474 tp->t_stop = ptsstop;
475 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
476 tp->t_lflag &= ~EXTPROC;
477 pti->pt_prison = ap->a_cred->cr_prison;
482 pti->devs->si_uid = ap->a_cred->cr_uid;
483 pti->devs->si_gid = 0;
484 pti->devs->si_perms = 0600;
485 pti->devc->si_uid = ap->a_cred->cr_uid;
486 pti->devc->si_gid = 0;
487 pti->devc->si_perms = 0600;
492 * On open of the master, we set the corresponding flag in the common
495 ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev->si_name,
496 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
498 if (pti->pt_flags2 & PF_UNIX98) {
499 pti->pt_flags2 |= PF_MOPEN;
507 ptcclose(struct dev_close_args *ap)
509 cdev_t dev = ap->a_head.a_dev;
511 struct pt_ioctl *pti = dev->si_drv1;
515 uminor_no = dev->si_uminor;
516 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
521 * On close of the master, we unset the corresponding flag in the common
524 pti->pt_flags2 &= ~PF_MOPEN;
528 * XXX MDMBUF makes no sense for ptys but would inhibit the above
529 * l_modem(). CLOCAL makes sense but isn't supported. Special
530 * l_modem()s that ignore carrier drop make no sense for ptys but
531 * may be in use because other parts of the line discipline make
532 * sense for ptys. Recover by doing everything that a normal
533 * ttymodem() would have done except for sending a SIGHUP.
535 if (tp->t_state & TS_ISOPEN) {
536 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
537 tp->t_state |= TS_ZOMBIE;
538 ttyflush(tp, FREAD | FWRITE);
540 tp->t_oproc = 0; /* mark closed */
543 pti->pt_prison = NULL;
544 pti->devs->si_uid = 0;
545 pti->devs->si_gid = 0;
546 pti->devs->si_perms = 0666;
547 pti->devc->si_uid = 0;
548 pti->devc->si_gid = 0;
549 pti->devc->si_perms = 0666;
554 * On close of the master, we destroy the master and, if no slaves are open,
555 * we destroy the slave device and unset the unit.
557 ptydebug(1, "ptcclose=%s (master) | unix98? %s\n", dev->si_name,
558 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
559 if (pti->pt_flags2 & PF_UNIX98) {
560 KKASSERT((pti->pt_flags2 & PF_MOPEN) == 0);
564 if (!(pti->pt_flags2 & PF_SOPEN)) {
565 ptydebug(1, "ptcclose: slaves are not open\n");
566 destroy_dev(pti->devs);
567 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty),
569 ttyunregister(&pti->pt_tty);
579 ptcread(struct dev_read_args *ap)
581 cdev_t dev = ap->a_head.a_dev;
582 struct tty *tp = dev->si_tty;
583 struct pt_ioctl *pti = dev->si_drv1;
588 * We want to block until the slave
589 * is open, and there's something to read;
590 * but if we lost the slave or we're NBIO,
591 * then return the appropriate error instead.
594 if (tp->t_state&TS_ISOPEN) {
595 if (pti->pt_flags&PF_PKT && pti->pt_send) {
596 error = ureadc((int)pti->pt_send, ap->a_uio);
599 if (pti->pt_send & TIOCPKT_IOCTL) {
600 cc = (int)szmin(ap->a_uio->uio_resid,
601 sizeof(tp->t_termios));
602 uiomove((caddr_t)&tp->t_termios, cc,
608 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
609 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
615 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
618 if ((tp->t_state & TS_CONNECTED) == 0)
619 return (0); /* EOF */
620 if (ap->a_ioflag & IO_NDELAY)
621 return (EWOULDBLOCK);
622 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
626 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
627 error = ureadc(0, ap->a_uio);
628 while (ap->a_uio->uio_resid > 0 && error == 0) {
629 cc = q_to_b(&tp->t_outq, buf,
630 (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
633 error = uiomove(buf, (size_t)cc, ap->a_uio);
640 ptsstop(struct tty *tp, int flush)
642 struct pt_ioctl *pti = tp->t_dev->si_drv1;
645 /* note: FLUSHREAD and FLUSHWRITE already ok */
647 flush = TIOCPKT_STOP;
648 pti->pt_flags |= PF_STOPPED;
650 pti->pt_flags &= ~PF_STOPPED;
651 pti->pt_send |= flush;
652 /* change of perspective */
662 * kqueue ops for pseudo-terminals.
664 static struct filterops ptcread_filtops =
665 { FILTEROP_ISFD, NULL, filt_ptcrdetach, filt_ptcread };
666 static struct filterops ptcwrite_filtops =
667 { FILTEROP_ISFD, NULL, filt_ptcwdetach, filt_ptcwrite };
670 ptckqfilter(struct dev_kqfilter_args *ap)
672 cdev_t dev = ap->a_head.a_dev;
673 struct knote *kn = ap->a_kn;
674 struct tty *tp = dev->si_tty;
678 switch (kn->kn_filter) {
680 klist = &tp->t_rkq.ki_note;
681 kn->kn_fop = &ptcread_filtops;
684 klist = &tp->t_wkq.ki_note;
685 kn->kn_fop = &ptcwrite_filtops;
688 ap->a_result = EOPNOTSUPP;
692 kn->kn_hook = (caddr_t)dev;
693 knote_insert(klist, kn);
698 filt_ptcread (struct knote *kn, long hint)
700 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
701 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
703 if (tp->t_state & TS_ZOMBIE) {
704 kn->kn_flags |= EV_EOF;
708 if ((tp->t_state & TS_ISOPEN) &&
709 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
710 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
711 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
712 kn->kn_data = tp->t_outq.c_cc;
720 filt_ptcwrite (struct knote *kn, long hint)
722 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
723 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
725 if (tp->t_state & TS_ZOMBIE) {
726 kn->kn_flags |= EV_EOF;
730 if (tp->t_state & TS_ISOPEN &&
731 ((pti->pt_flags & PF_REMOTE) ?
732 (tp->t_canq.c_cc == 0) :
733 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
734 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
735 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
743 filt_ptcrdetach (struct knote *kn)
745 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
747 knote_remove(&tp->t_rkq.ki_note, kn);
751 filt_ptcwdetach (struct knote *kn)
753 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
755 knote_remove(&tp->t_wkq.ki_note, kn);
762 ptcwrite(struct dev_write_args *ap)
764 cdev_t dev = ap->a_head.a_dev;
765 struct tty *tp = dev->si_tty;
768 u_char locbuf[BUFSIZ];
770 struct pt_ioctl *pti = dev->si_drv1;
774 if ((tp->t_state&TS_ISOPEN) == 0)
776 if (pti->pt_flags & PF_REMOTE) {
779 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
780 tp->t_canq.c_cc < TTYHOG - 1) {
782 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
783 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
785 error = uiomove(cp, (size_t)cc, ap->a_uio);
788 /* check again for safety */
789 if ((tp->t_state & TS_ISOPEN) == 0) {
790 /* adjust as usual */
791 ap->a_uio->uio_resid += cc;
796 cc = b_to_q((char *)cp, cc, &tp->t_canq);
798 * XXX we don't guarantee that the canq size
799 * is >= TTYHOG, so the above b_to_q() may
800 * leave some bytes uncopied. However, space
801 * is guaranteed for the null terminator if
802 * we don't fail here since (TTYHOG - 1) is
803 * not a multiple of CBSIZE.
809 /* adjust for data copied in but not written */
810 ap->a_uio->uio_resid += cc;
811 clist_putc(0, &tp->t_canq);
813 wakeup(TSA_PTS_READ(tp));
816 while (ap->a_uio->uio_resid > 0 || cc > 0) {
818 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
820 error = uiomove(cp, (size_t)cc, ap->a_uio);
823 /* check again for safety */
824 if ((tp->t_state & TS_ISOPEN) == 0) {
825 /* adjust for data copied in but not written */
826 ap->a_uio->uio_resid += cc;
831 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
832 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
833 wakeup(TSA_HUP_OR_INPUT(tp));
836 (*linesw[tp->t_line].l_rint)(*cp++, tp);
845 * Come here to wait for slave to open, for space
846 * in outq, or space in rawq, or an empty canq.
848 if ((tp->t_state & TS_CONNECTED) == 0) {
849 /* adjust for data copied in but not written */
850 ap->a_uio->uio_resid += cc;
853 if (ap->a_ioflag & IO_NDELAY) {
854 /* adjust for data copied in but not written */
855 ap->a_uio->uio_resid += cc;
857 return (EWOULDBLOCK);
860 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
862 /* adjust for data copied in but not written */
863 ap->a_uio->uio_resid += cc;
871 ptyioctl(struct dev_ioctl_args *ap)
873 cdev_t dev = ap->a_head.a_dev;
874 struct tty *tp = dev->si_tty;
875 struct pt_ioctl *pti = dev->si_drv1;
876 u_char *cc = tp->t_cc;
879 if (dev_dflags(dev) & D_MASTER) {
884 * We avoid calling ttioctl on the controller since,
885 * in that case, tp must be the controlling terminal.
887 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
891 if (*(int *)ap->a_data) {
892 if (pti->pt_flags & PF_UCNTL)
894 pti->pt_flags |= PF_PKT;
896 pti->pt_flags &= ~PF_PKT;
900 if (*(int *)ap->a_data) {
901 if (pti->pt_flags & PF_PKT)
903 pti->pt_flags |= PF_UCNTL;
905 pti->pt_flags &= ~PF_UCNTL;
909 if (*(int *)ap->a_data)
910 pti->pt_flags |= PF_REMOTE;
912 pti->pt_flags &= ~PF_REMOTE;
913 ttyflush(tp, FREAD|FWRITE);
917 if ((pti->pt_flags2 & PF_UNIX98) && (pti->devc == dev))
924 * The rest of the ioctls shouldn't be called until
927 if ((tp->t_state & TS_ISOPEN) == 0)
940 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
941 * ttywflush(tp) will hang if there are characters in
944 ndflush(&tp->t_outq, tp->t_outq.c_cc);
948 if (*(unsigned int *)ap->a_data >= NSIG ||
949 *(unsigned int *)ap->a_data == 0)
951 if ((tp->t_lflag&NOFLSH) == 0)
952 ttyflush(tp, FREAD|FWRITE);
953 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
954 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
955 ((tp->t_lflag&NOKERNINFO) == 0))
960 if (ap->a_cmd == TIOCEXT) {
962 * When the EXTPROC bit is being toggled, we need
963 * to send an TIOCPKT_IOCTL if the packet driver
966 if (*(int *)ap->a_data) {
967 if (pti->pt_flags & PF_PKT) {
968 pti->pt_send |= TIOCPKT_IOCTL;
969 ptcwakeup(tp, FREAD);
971 tp->t_lflag |= EXTPROC;
973 if ((tp->t_lflag & EXTPROC) &&
974 (pti->pt_flags & PF_PKT)) {
975 pti->pt_send |= TIOCPKT_IOCTL;
976 ptcwakeup(tp, FREAD);
978 tp->t_lflag &= ~EXTPROC;
982 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
983 ap->a_fflag, ap->a_cred);
984 if (error == ENOIOCTL)
985 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
986 if (error == ENOIOCTL) {
987 if (pti->pt_flags & PF_UCNTL &&
988 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
989 if (ap->a_cmd & 0xff) {
990 pti->pt_ucntl = (u_char)ap->a_cmd;
991 ptcwakeup(tp, FREAD);
998 * If external processing and packet mode send ioctl packet.
1000 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
1009 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1016 pti->pt_send |= TIOCPKT_IOCTL;
1017 ptcwakeup(tp, FREAD);
1022 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1023 && CCEQ(cc[VSTART], CTRL('q'));
1024 if (pti->pt_flags & PF_NOSTOP) {
1026 pti->pt_send &= ~TIOCPKT_NOSTOP;
1027 pti->pt_send |= TIOCPKT_DOSTOP;
1028 pti->pt_flags &= ~PF_NOSTOP;
1029 ptcwakeup(tp, FREAD);
1033 pti->pt_send &= ~TIOCPKT_DOSTOP;
1034 pti->pt_send |= TIOCPKT_NOSTOP;
1035 pti->pt_flags |= PF_NOSTOP;
1036 ptcwakeup(tp, FREAD);
1043 static void ptc_drvinit (void *unused);
1046 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1047 0, "Change pty debug level");
1051 ptc_drvinit(void *unused)
1058 * Create the clonable base device.
1060 make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1061 0, 0, 0666, "ptmx");
1064 for (i = 0; i < 256; i++) {
1069 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)