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);
74 static int filt_ptcread (struct knote *kn, long hint);
75 static void filt_ptcrdetach (struct knote *kn);
76 static int filt_ptcwrite (struct knote *kn, long hint);
77 static void filt_ptcwdetach (struct knote *kn);
79 static d_open_t ptsopen;
80 static d_close_t ptsclose;
81 static d_read_t ptsread;
82 static d_write_t ptswrite;
83 static d_ioctl_t ptyioctl;
84 static d_open_t ptcopen;
85 static d_close_t ptcclose;
86 static d_read_t ptcread;
87 static d_write_t ptcwrite;
88 static d_poll_t ptcpoll;
89 static d_kqfilter_t ptckqfilter;
92 DEVFS_DECLARE_CLONE_BITMAP(pty);
94 static d_clone_t ptyclone;
96 static int pty_debug_level = 0;
98 static struct dev_ops pts98_ops = {
99 { "pts98", 0, D_TTY | D_KQFILTER },
106 .d_kqfilter = ttykqfilter,
107 .d_revoke = ttyrevoke
110 static struct dev_ops ptc98_ops = {
111 { "ptc98", 0, D_TTY | D_KQFILTER | D_MASTER },
118 .d_kqfilter = ptckqfilter,
119 .d_revoke = ttyrevoke
123 #define CDEV_MAJOR_S 5
124 static struct dev_ops pts_ops = {
125 { "pts", CDEV_MAJOR_S, D_TTY | D_KQFILTER },
132 .d_kqfilter = ttykqfilter,
133 .d_revoke = ttyrevoke
136 #define CDEV_MAJOR_C 6
137 static struct dev_ops ptc_ops = {
138 { "ptc", CDEV_MAJOR_C, D_TTY | D_KQFILTER | D_MASTER },
145 .d_kqfilter = ptckqfilter,
146 .d_revoke = ttyrevoke
149 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
154 struct selinfo pt_selr, pt_selw;
159 struct prison *pt_prison;
162 #define PF_PKT 0x08 /* packet mode */
163 #define PF_STOPPED 0x10 /* user told stopped */
164 #define PF_REMOTE 0x20 /* remote and flow controlled input */
165 #define PF_NOSTOP 0x40
166 #define PF_UCNTL 0x80 /* user control mode */
168 #define PF_UNIX98 0x01
169 #define PF_SOPEN 0x02
170 #define PF_MOPEN 0x04
173 ptydebug(int level, char *fmt, ...)
178 if (level <= pty_debug_level)
186 * This function creates and initializes a pts/ptc pair
188 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
189 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
191 * XXX: define and add mapping of upper minor bits to allow more
198 char *names = "pqrsPQRS";
201 /* For now we only map the lower 8 bits of the minor */
205 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
206 pt->devs = devs = make_dev(&pts_ops, n,
207 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
208 pt->devc = devc = make_dev(&ptc_ops, n,
209 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
211 devs->si_drv1 = devc->si_drv1 = pt;
212 devs->si_tty = devc->si_tty = &pt->pt_tty;
213 devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
214 devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
215 pt->pt_tty.t_dev = devs;
216 ttyregister(&pt->pt_tty);
221 ptyclone(struct dev_clone_args *ap)
227 * Limit the number of unix98 pty (slave) devices to 1000, as
228 * the utmp(5) format only allows for 8 bytes for the tty,
230 * If this limit is reached, we don't clone and return error
233 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 1000);
240 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
242 pt->devc = ap->a_dev = make_only_dev(&ptc98_ops, unit, ap->a_cred->cr_ruid,
243 0, 0600, "ptm/%d", unit);
244 pt->devs = make_dev(&pts98_ops, unit, ap->a_cred->cr_ruid, GID_TTY, 0620,
247 pt->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
248 pt->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
250 pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
251 pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
252 pt->pt_tty.t_dev = pt->devs;
253 pt->pt_flags2 |= PF_UNIX98;
255 ttyregister(&pt->pt_tty);
263 ptsopen(struct dev_open_args *ap)
265 cdev_t dev = ap->a_head.a_dev;
268 struct pt_ioctl *pti;
276 if ((tp->t_state & TS_ISOPEN) == 0) {
277 ttychars(tp); /* Set up default chars */
278 tp->t_iflag = TTYDEF_IFLAG;
279 tp->t_oflag = TTYDEF_OFLAG;
280 tp->t_lflag = TTYDEF_LFLAG;
281 tp->t_cflag = TTYDEF_CFLAG;
282 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
283 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
285 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
288 if (tp->t_oproc) /* Ctrlr still around. */
289 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
290 while ((tp->t_state & TS_CARR_ON) == 0) {
291 if (ap->a_oflags & FNONBLOCK)
293 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
297 error = (*linesw[tp->t_line].l_open)(dev, tp);
299 ptcwakeup(tp, FREAD|FWRITE);
304 * On open of the slave, we set the corresponding flag in the common
307 ptydebug(1, "ptsopen=%s | unix98? %s\n", dev->si_name,
308 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
310 if ((!error) && (pti->pt_flags2 & PF_UNIX98)) {
311 pti->pt_flags2 |= PF_SOPEN;
319 ptsclose(struct dev_close_args *ap)
321 cdev_t dev = ap->a_head.a_dev;
323 struct pt_ioctl *pti = dev->si_drv1;
327 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
328 ptsstop(tp, FREAD|FWRITE);
334 * On close of the slave, we unset the corresponding flag, and if the master
335 * isn't open anymore, we destroy the slave and unset the unit.
337 ptydebug(1, "ptsclose=%s | unix98? %s\n", dev->si_name,
338 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
340 if (pti->pt_flags2 & PF_UNIX98) {
341 pti->pt_flags2 &= ~PF_SOPEN;
342 KKASSERT((pti->pt_flags2 & PF_SOPEN) == 0);
343 ptydebug(1, "master open? %s\n",
344 (pti->pt_flags2 & PF_MOPEN)?"yes":"no");
346 if (!(pti->pt_flags2 & PF_SOPEN) && !(pti->pt_flags2 & PF_MOPEN)) {
347 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
357 ptsread(struct dev_read_args *ap)
359 cdev_t dev = ap->a_head.a_dev;
360 struct proc *p = curproc;
361 struct tty *tp = dev->si_tty;
362 struct pt_ioctl *pti = dev->si_drv1;
367 lp = curthread->td_lwp;
370 if (pti->pt_flags & PF_REMOTE) {
371 while (isbackground(p, tp)) {
372 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
373 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
374 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
376 pgsignal(p->p_pgrp, SIGTTIN, 1);
377 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
381 if (tp->t_canq.c_cc == 0) {
382 if (ap->a_ioflag & IO_NDELAY)
383 return (EWOULDBLOCK);
384 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
390 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
391 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
395 if (tp->t_canq.c_cc == 1)
396 clist_getc(&tp->t_canq);
401 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
402 ptcwakeup(tp, FWRITE);
407 * Write to pseudo-tty.
408 * Wakeups of controlling tty will happen
409 * indirectly, when tty driver calls ptsstart.
412 ptswrite(struct dev_write_args *ap)
414 cdev_t dev = ap->a_head.a_dev;
418 if (tp->t_oproc == 0)
420 return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
424 * Start output on pseudo-tty.
425 * Wake up process selecting or sleeping for input from controlling tty.
428 ptsstart(struct tty *tp)
430 struct pt_ioctl *pti = tp->t_dev->si_drv1;
432 if (tp->t_state & TS_TTSTOP)
434 if (pti->pt_flags & PF_STOPPED) {
435 pti->pt_flags &= ~PF_STOPPED;
436 pti->pt_send = TIOCPKT_START;
438 ptcwakeup(tp, FREAD);
442 ptcwakeup(struct tty *tp, int flag)
444 struct pt_ioctl *pti = tp->t_dev->si_drv1;
447 selwakeup(&pti->pt_selr);
448 wakeup(TSA_PTC_READ(tp));
449 KNOTE(&tp->t_rsel.si_note, 0);
452 selwakeup(&pti->pt_selw);
453 wakeup(TSA_PTC_WRITE(tp));
454 KNOTE(&tp->t_wsel.si_note, 0);
459 ptcopen(struct dev_open_args *ap)
461 cdev_t dev = ap->a_head.a_dev;
463 struct pt_ioctl *pti;
470 if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison)
475 tp->t_oproc = ptsstart;
476 tp->t_stop = ptsstop;
477 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
478 tp->t_lflag &= ~EXTPROC;
479 pti->pt_prison = ap->a_cred->cr_prison;
484 pti->devs->si_uid = ap->a_cred->cr_uid;
485 pti->devs->si_gid = 0;
486 pti->devs->si_perms = 0600;
487 pti->devc->si_uid = ap->a_cred->cr_uid;
488 pti->devc->si_gid = 0;
489 pti->devc->si_perms = 0600;
494 * On open of the master, we set the corresponding flag in the common
497 ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev->si_name,
498 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
500 if (pti->pt_flags2 & PF_UNIX98) {
501 pti->pt_flags2 |= PF_MOPEN;
509 ptcclose(struct dev_close_args *ap)
511 cdev_t dev = ap->a_head.a_dev;
513 struct pt_ioctl *pti = dev->si_drv1;
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), dev->si_uminor);
576 ptcread(struct dev_read_args *ap)
578 cdev_t dev = ap->a_head.a_dev;
579 struct tty *tp = dev->si_tty;
580 struct pt_ioctl *pti = dev->si_drv1;
585 * We want to block until the slave
586 * is open, and there's something to read;
587 * but if we lost the slave or we're NBIO,
588 * then return the appropriate error instead.
591 if (tp->t_state&TS_ISOPEN) {
592 if (pti->pt_flags&PF_PKT && pti->pt_send) {
593 error = ureadc((int)pti->pt_send, ap->a_uio);
596 if (pti->pt_send & TIOCPKT_IOCTL) {
597 cc = (int)szmin(ap->a_uio->uio_resid,
598 sizeof(tp->t_termios));
599 uiomove((caddr_t)&tp->t_termios, cc,
605 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
606 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
612 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
615 if ((tp->t_state & TS_CONNECTED) == 0)
616 return (0); /* EOF */
617 if (ap->a_ioflag & IO_NDELAY)
618 return (EWOULDBLOCK);
619 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
623 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
624 error = ureadc(0, ap->a_uio);
625 while (ap->a_uio->uio_resid > 0 && error == 0) {
626 cc = q_to_b(&tp->t_outq, buf,
627 (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
630 error = uiomove(buf, (size_t)cc, ap->a_uio);
637 ptsstop(struct tty *tp, int flush)
639 struct pt_ioctl *pti = tp->t_dev->si_drv1;
642 /* note: FLUSHREAD and FLUSHWRITE already ok */
644 flush = TIOCPKT_STOP;
645 pti->pt_flags |= PF_STOPPED;
647 pti->pt_flags &= ~PF_STOPPED;
648 pti->pt_send |= flush;
649 /* change of perspective */
659 ptcpoll(struct dev_poll_args *ap)
661 cdev_t dev = ap->a_head.a_dev;
662 struct tty *tp = dev->si_tty;
663 struct pt_ioctl *pti = dev->si_drv1;
666 if ((tp->t_state & TS_CONNECTED) == 0) {
667 ap->a_events = seltrue(dev, ap->a_events) | POLLHUP;
672 * Need to block timeouts (ttrstart).
676 if (ap->a_events & (POLLIN | POLLRDNORM))
677 if ((tp->t_state & TS_ISOPEN) &&
678 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
679 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
680 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
681 revents |= ap->a_events & (POLLIN | POLLRDNORM);
683 if (ap->a_events & (POLLOUT | POLLWRNORM))
684 if (tp->t_state & TS_ISOPEN &&
685 ((pti->pt_flags & PF_REMOTE) ?
686 (tp->t_canq.c_cc == 0) :
687 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
688 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
689 revents |= ap->a_events & (POLLOUT | POLLWRNORM);
691 if (ap->a_events & POLLHUP)
692 if ((tp->t_state & TS_CARR_ON) == 0)
696 if (ap->a_events & (POLLIN | POLLRDNORM))
697 selrecord(curthread, &pti->pt_selr);
699 if (ap->a_events & (POLLOUT | POLLWRNORM))
700 selrecord(curthread, &pti->pt_selw);
704 ap->a_events = revents;
709 * kqueue ops for pseudo-terminals.
711 static struct filterops ptcread_filtops =
712 { 1, NULL, filt_ptcrdetach, filt_ptcread };
713 static struct filterops ptcwrite_filtops =
714 { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
717 ptckqfilter(struct dev_kqfilter_args *ap)
719 cdev_t dev = ap->a_head.a_dev;
720 struct knote *kn = ap->a_kn;
721 struct tty *tp = dev->si_tty;
725 switch (kn->kn_filter) {
727 klist = &tp->t_rsel.si_note;
728 kn->kn_fop = &ptcread_filtops;
731 klist = &tp->t_wsel.si_note;
732 kn->kn_fop = &ptcwrite_filtops;
739 kn->kn_hook = (caddr_t)dev;
742 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
749 filt_ptcread (struct knote *kn, long hint)
751 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
752 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
754 if ((tp->t_state & TS_ISOPEN) &&
755 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
756 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
757 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
758 kn->kn_data = tp->t_outq.c_cc;
766 filt_ptcwrite (struct knote *kn, long hint)
768 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
769 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
771 if (tp->t_state & TS_ISOPEN &&
772 ((pti->pt_flags & PF_REMOTE) ?
773 (tp->t_canq.c_cc == 0) :
774 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
775 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
776 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
784 filt_ptcrdetach (struct knote *kn)
786 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
789 SLIST_REMOVE(&tp->t_rsel.si_note, kn, knote, kn_selnext);
794 filt_ptcwdetach (struct knote *kn)
796 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
799 SLIST_REMOVE(&tp->t_wsel.si_note, kn, knote, kn_selnext);
807 ptcwrite(struct dev_write_args *ap)
809 cdev_t dev = ap->a_head.a_dev;
810 struct tty *tp = dev->si_tty;
813 u_char locbuf[BUFSIZ];
815 struct pt_ioctl *pti = dev->si_drv1;
819 if ((tp->t_state&TS_ISOPEN) == 0)
821 if (pti->pt_flags & PF_REMOTE) {
824 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
825 tp->t_canq.c_cc < TTYHOG - 1) {
827 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
828 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
830 error = uiomove(cp, (size_t)cc, ap->a_uio);
833 /* check again for safety */
834 if ((tp->t_state & TS_ISOPEN) == 0) {
835 /* adjust as usual */
836 ap->a_uio->uio_resid += cc;
841 cc = b_to_q((char *)cp, cc, &tp->t_canq);
843 * XXX we don't guarantee that the canq size
844 * is >= TTYHOG, so the above b_to_q() may
845 * leave some bytes uncopied. However, space
846 * is guaranteed for the null terminator if
847 * we don't fail here since (TTYHOG - 1) is
848 * not a multiple of CBSIZE.
854 /* adjust for data copied in but not written */
855 ap->a_uio->uio_resid += cc;
856 clist_putc(0, &tp->t_canq);
858 wakeup(TSA_PTS_READ(tp));
861 while (ap->a_uio->uio_resid > 0 || cc > 0) {
863 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
865 error = uiomove(cp, (size_t)cc, ap->a_uio);
868 /* check again for safety */
869 if ((tp->t_state & TS_ISOPEN) == 0) {
870 /* adjust for data copied in but not written */
871 ap->a_uio->uio_resid += cc;
876 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
877 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
878 wakeup(TSA_HUP_OR_INPUT(tp));
881 (*linesw[tp->t_line].l_rint)(*cp++, tp);
890 * Come here to wait for slave to open, for space
891 * in outq, or space in rawq, or an empty canq.
893 if ((tp->t_state & TS_CONNECTED) == 0) {
894 /* adjust for data copied in but not written */
895 ap->a_uio->uio_resid += cc;
898 if (ap->a_ioflag & IO_NDELAY) {
899 /* adjust for data copied in but not written */
900 ap->a_uio->uio_resid += cc;
902 return (EWOULDBLOCK);
905 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
907 /* adjust for data copied in but not written */
908 ap->a_uio->uio_resid += cc;
916 ptyioctl(struct dev_ioctl_args *ap)
918 cdev_t dev = ap->a_head.a_dev;
919 struct tty *tp = dev->si_tty;
920 struct pt_ioctl *pti = dev->si_drv1;
921 u_char *cc = tp->t_cc;
924 if (dev_dflags(dev) & D_MASTER) {
929 * We avoid calling ttioctl on the controller since,
930 * in that case, tp must be the controlling terminal.
932 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
936 if (*(int *)ap->a_data) {
937 if (pti->pt_flags & PF_UCNTL)
939 pti->pt_flags |= PF_PKT;
941 pti->pt_flags &= ~PF_PKT;
945 if (*(int *)ap->a_data) {
946 if (pti->pt_flags & PF_PKT)
948 pti->pt_flags |= PF_UCNTL;
950 pti->pt_flags &= ~PF_UCNTL;
954 if (*(int *)ap->a_data)
955 pti->pt_flags |= PF_REMOTE;
957 pti->pt_flags &= ~PF_REMOTE;
958 ttyflush(tp, FREAD|FWRITE);
962 if ((pti->pt_flags2 & PF_UNIX98) && (pti->devc == dev))
969 * The rest of the ioctls shouldn't be called until
972 if ((tp->t_state & TS_ISOPEN) == 0)
985 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
986 * ttywflush(tp) will hang if there are characters in
989 ndflush(&tp->t_outq, tp->t_outq.c_cc);
993 if (*(unsigned int *)ap->a_data >= NSIG ||
994 *(unsigned int *)ap->a_data == 0)
996 if ((tp->t_lflag&NOFLSH) == 0)
997 ttyflush(tp, FREAD|FWRITE);
998 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
999 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
1000 ((tp->t_lflag&NOKERNINFO) == 0))
1005 if (ap->a_cmd == TIOCEXT) {
1007 * When the EXTPROC bit is being toggled, we need
1008 * to send an TIOCPKT_IOCTL if the packet driver
1011 if (*(int *)ap->a_data) {
1012 if (pti->pt_flags & PF_PKT) {
1013 pti->pt_send |= TIOCPKT_IOCTL;
1014 ptcwakeup(tp, FREAD);
1016 tp->t_lflag |= EXTPROC;
1018 if ((tp->t_lflag & EXTPROC) &&
1019 (pti->pt_flags & PF_PKT)) {
1020 pti->pt_send |= TIOCPKT_IOCTL;
1021 ptcwakeup(tp, FREAD);
1023 tp->t_lflag &= ~EXTPROC;
1027 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1028 ap->a_fflag, ap->a_cred);
1029 if (error == ENOIOCTL)
1030 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
1031 if (error == ENOIOCTL) {
1032 if (pti->pt_flags & PF_UCNTL &&
1033 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
1034 if (ap->a_cmd & 0xff) {
1035 pti->pt_ucntl = (u_char)ap->a_cmd;
1036 ptcwakeup(tp, FREAD);
1043 * If external processing and packet mode send ioctl packet.
1045 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
1054 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1061 pti->pt_send |= TIOCPKT_IOCTL;
1062 ptcwakeup(tp, FREAD);
1067 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1068 && CCEQ(cc[VSTART], CTRL('q'));
1069 if (pti->pt_flags & PF_NOSTOP) {
1071 pti->pt_send &= ~TIOCPKT_NOSTOP;
1072 pti->pt_send |= TIOCPKT_DOSTOP;
1073 pti->pt_flags &= ~PF_NOSTOP;
1074 ptcwakeup(tp, FREAD);
1078 pti->pt_send &= ~TIOCPKT_DOSTOP;
1079 pti->pt_send |= TIOCPKT_NOSTOP;
1080 pti->pt_flags |= PF_NOSTOP;
1081 ptcwakeup(tp, FREAD);
1088 static void ptc_drvinit (void *unused);
1091 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1092 0, "Change pty debug level");
1096 ptc_drvinit(void *unused)
1103 * Create the clonable base device.
1105 make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1106 0, 0, 0666, "ptmx");
1109 for (i = 0; i < 256; i++) {
1114 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)