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;
94 #define CDEV_MAJOR_S 5
95 static struct dev_ops pts_ops
= {
96 { "pts", CDEV_MAJOR_S
, D_TTY
| D_KQFILTER
},
103 .d_kqfilter
= ttykqfilter
,
104 .d_revoke
= ttyrevoke
107 #define CDEV_MAJOR_C 6
108 static struct dev_ops ptc_ops
= {
109 { "ptc", CDEV_MAJOR_C
, D_TTY
| D_KQFILTER
| D_MASTER
},
116 .d_kqfilter
= ttykqfilter
,
117 .d_revoke
= ttyrevoke
120 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
125 struct selinfo pt_selr
, pt_selw
;
130 struct prison
*pt_prison
;
133 #define PF_PKT 0x08 /* packet mode */
134 #define PF_STOPPED 0x10 /* user told stopped */
135 #define PF_REMOTE 0x20 /* remote and flow controlled input */
136 #define PF_NOSTOP 0x40
137 #define PF_UCNTL 0x80 /* user control mode */
139 #define PF_UNIX98 0x01
140 #define PF_SOPEN 0x02
141 #define PF_MOPEN 0x04
144 ptydebug(int level
, char *fmt
, ...)
149 if (level
<= pty_debug_level
)
157 * This function creates and initializes a pts/ptc pair
159 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
160 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
162 * XXX: define and add mapping of upper minor bits to allow more
169 char *names
= "pqrsPQRS";
172 /* For now we only map the lower 8 bits of the minor */
176 pt
= kmalloc(sizeof(*pt
), M_PTY
, M_WAITOK
| M_ZERO
);
177 pt
->devs
= devs
= make_dev(&pts_ops
, n
,
178 0, 0, 0666, "tty%c%r", names
[n
/ 32], n
% 32);
179 pt
->devc
= devc
= make_dev(&ptc_ops
, n
,
180 0, 0, 0666, "pty%c%r", names
[n
/ 32], n
% 32);
182 devs
->si_drv1
= devc
->si_drv1
= pt
;
183 devs
->si_tty
= devc
->si_tty
= &pt
->pt_tty
;
184 devs
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
185 devc
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
186 pt
->pt_tty
.t_dev
= devs
;
187 ttyregister(&pt
->pt_tty
);
192 ptyclone(struct dev_clone_args
*ap
)
198 * Limit the number of unix98 pty (slave) devices to 1000, as
199 * the utmp(5) format only allows for 8 bytes for the tty,
201 * If this limit is reached, we don't clone and return error
204 unit
= devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty
), 1000);
211 pt
= kmalloc(sizeof(*pt
), M_PTY
, M_WAITOK
| M_ZERO
);
213 pt
->devc
= ap
->a_dev
= make_only_dev(&ptc_ops
, unit
, ap
->a_cred
->cr_ruid
,
214 0, 0600, "ptm/%d", unit
);
215 pt
->devs
= make_dev(&pts_ops
, unit
, ap
->a_cred
->cr_ruid
, GID_TTY
, 0620,
218 pt
->devs
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
219 pt
->devc
->si_flags
|= SI_OVERRIDE
; /* uid, gid, perms from dev */
221 pt
->devs
->si_drv1
= pt
->devc
->si_drv1
= pt
;
222 pt
->devs
->si_tty
= pt
->devc
->si_tty
= &pt
->pt_tty
;
223 pt
->pt_tty
.t_dev
= pt
->devs
;
224 pt
->pt_flags2
|= PF_UNIX98
;
226 ttyregister(&pt
->pt_tty
);
234 ptsopen(struct dev_open_args
*ap
)
236 cdev_t dev
= ap
->a_head
.a_dev
;
239 struct pt_ioctl
*pti
;
247 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
248 ttychars(tp
); /* Set up default chars */
249 tp
->t_iflag
= TTYDEF_IFLAG
;
250 tp
->t_oflag
= TTYDEF_OFLAG
;
251 tp
->t_lflag
= TTYDEF_LFLAG
;
252 tp
->t_cflag
= TTYDEF_CFLAG
;
253 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
254 } else if ((tp
->t_state
& TS_XCLUDE
) && priv_check_cred(ap
->a_cred
, PRIV_ROOT
, 0)) {
256 } else if (pti
->pt_prison
!= ap
->a_cred
->cr_prison
) {
259 if (tp
->t_oproc
) /* Ctrlr still around. */
260 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
261 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
262 if (ap
->a_oflags
& FNONBLOCK
)
264 error
= ttysleep(tp
, TSA_CARR_ON(tp
), PCATCH
, "ptsopn", 0);
268 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
270 ptcwakeup(tp
, FREAD
|FWRITE
);
275 * On open of the slave, we set the corresponding flag in the common
278 ptydebug(1, "ptsopen=%s | unix98? %s\n", dev
->si_name
,
279 (pti
->pt_flags2
& PF_UNIX98
)?"yes":"no");
281 if ((!error
) && (pti
->pt_flags2
& PF_UNIX98
)) {
282 pti
->pt_flags2
|= PF_SOPEN
;
290 ptsclose(struct dev_close_args
*ap
)
292 cdev_t dev
= ap
->a_head
.a_dev
;
294 struct pt_ioctl
*pti
= dev
->si_drv1
;
298 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, ap
->a_fflag
);
299 ptsstop(tp
, FREAD
|FWRITE
);
305 * On close of the slave, we unset the corresponding flag, and if the master
306 * isn't open anymore, we destroy the slave and unset the unit.
308 ptydebug(1, "ptsclose=%s | unix98? %s\n", dev
->si_name
,
309 (pti
->pt_flags2
& PF_UNIX98
)?"yes":"no");
311 if (pti
->pt_flags2
& PF_UNIX98
) {
312 pti
->pt_flags2
&= ~PF_SOPEN
;
313 KKASSERT((pti
->pt_flags2
& PF_SOPEN
) == 0);
314 ptydebug(1, "master open? %s\n",
315 (pti
->pt_flags2
& PF_MOPEN
)?"yes":"no");
317 if (!(pti
->pt_flags2
& PF_SOPEN
) && !(pti
->pt_flags2
& PF_MOPEN
)) {
318 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty
), dev
->si_uminor
);
328 ptsread(struct dev_read_args
*ap
)
330 cdev_t dev
= ap
->a_head
.a_dev
;
331 struct proc
*p
= curproc
;
332 struct tty
*tp
= dev
->si_tty
;
333 struct pt_ioctl
*pti
= dev
->si_drv1
;
338 lp
= curthread
->td_lwp
;
341 if (pti
->pt_flags
& PF_REMOTE
) {
342 while (isbackground(p
, tp
)) {
343 if (SIGISMEMBER(p
->p_sigignore
, SIGTTIN
) ||
344 SIGISMEMBER(lp
->lwp_sigmask
, SIGTTIN
) ||
345 p
->p_pgrp
->pg_jobc
== 0 || p
->p_flag
& P_PPWAIT
)
347 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
348 error
= ttysleep(tp
, &lbolt
, PCATCH
, "ptsbg", 0);
352 if (tp
->t_canq
.c_cc
== 0) {
353 if (ap
->a_ioflag
& IO_NDELAY
)
354 return (EWOULDBLOCK
);
355 error
= ttysleep(tp
, TSA_PTS_READ(tp
), PCATCH
,
361 while (tp
->t_canq
.c_cc
> 1 && ap
->a_uio
->uio_resid
> 0)
362 if (ureadc(clist_getc(&tp
->t_canq
), ap
->a_uio
) < 0) {
366 if (tp
->t_canq
.c_cc
== 1)
367 clist_getc(&tp
->t_canq
);
372 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, ap
->a_uio
, ap
->a_ioflag
);
373 ptcwakeup(tp
, FWRITE
);
378 * Write to pseudo-tty.
379 * Wakeups of controlling tty will happen
380 * indirectly, when tty driver calls ptsstart.
383 ptswrite(struct dev_write_args
*ap
)
385 cdev_t dev
= ap
->a_head
.a_dev
;
389 if (tp
->t_oproc
== 0)
391 return ((*linesw
[tp
->t_line
].l_write
)(tp
, ap
->a_uio
, ap
->a_ioflag
));
395 * Start output on pseudo-tty.
396 * Wake up process selecting or sleeping for input from controlling tty.
399 ptsstart(struct tty
*tp
)
401 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
403 if (tp
->t_state
& TS_TTSTOP
)
405 if (pti
->pt_flags
& PF_STOPPED
) {
406 pti
->pt_flags
&= ~PF_STOPPED
;
407 pti
->pt_send
= TIOCPKT_START
;
409 ptcwakeup(tp
, FREAD
);
413 ptcwakeup(struct tty
*tp
, int flag
)
415 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
418 selwakeup(&pti
->pt_selr
);
419 wakeup(TSA_PTC_READ(tp
));
422 selwakeup(&pti
->pt_selw
);
423 wakeup(TSA_PTC_WRITE(tp
));
428 ptcopen(struct dev_open_args
*ap
)
430 cdev_t dev
= ap
->a_head
.a_dev
;
432 struct pt_ioctl
*pti
;
439 if (pti
->pt_prison
&& pti
->pt_prison
!= ap
->a_cred
->cr_prison
)
444 tp
->t_oproc
= ptsstart
;
445 tp
->t_stop
= ptsstop
;
446 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
447 tp
->t_lflag
&= ~EXTPROC
;
448 pti
->pt_prison
= ap
->a_cred
->cr_prison
;
453 pti
->devs
->si_uid
= ap
->a_cred
->cr_uid
;
454 pti
->devs
->si_gid
= 0;
455 pti
->devs
->si_perms
= 0600;
456 pti
->devc
->si_uid
= ap
->a_cred
->cr_uid
;
457 pti
->devc
->si_gid
= 0;
458 pti
->devc
->si_perms
= 0600;
463 * On open of the master, we set the corresponding flag in the common
466 ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev
->si_name
,
467 (pti
->pt_flags2
& PF_UNIX98
)?"yes":"no");
469 if (pti
->pt_flags2
& PF_UNIX98
) {
470 pti
->pt_flags2
|= PF_MOPEN
;
478 ptcclose(struct dev_close_args
*ap
)
480 cdev_t dev
= ap
->a_head
.a_dev
;
482 struct pt_ioctl
*pti
= dev
->si_drv1
;
485 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
490 * On close of the master, we unset the corresponding flag in the common
493 pti
->pt_flags2
&= ~PF_MOPEN
;
497 * XXX MDMBUF makes no sense for ptys but would inhibit the above
498 * l_modem(). CLOCAL makes sense but isn't supported. Special
499 * l_modem()s that ignore carrier drop make no sense for ptys but
500 * may be in use because other parts of the line discipline make
501 * sense for ptys. Recover by doing everything that a normal
502 * ttymodem() would have done except for sending a SIGHUP.
504 if (tp
->t_state
& TS_ISOPEN
) {
505 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
506 tp
->t_state
|= TS_ZOMBIE
;
507 ttyflush(tp
, FREAD
| FWRITE
);
509 tp
->t_oproc
= 0; /* mark closed */
512 pti
->pt_prison
= NULL
;
513 pti
->devs
->si_uid
= 0;
514 pti
->devs
->si_gid
= 0;
515 pti
->devs
->si_perms
= 0666;
516 pti
->devc
->si_uid
= 0;
517 pti
->devc
->si_gid
= 0;
518 pti
->devc
->si_perms
= 0666;
523 * On close of the master, we destroy the master and, if no slaves are open,
524 * we destroy the slave device and unset the unit.
526 ptydebug(1, "ptcclose=%s (master) | unix98? %s\n", dev
->si_name
,
527 (pti
->pt_flags2
& PF_UNIX98
)?"yes":"no");
528 if (pti
->pt_flags2
& PF_UNIX98
) {
529 KKASSERT((pti
->pt_flags2
& PF_MOPEN
) == 0);
533 if (!(pti
->pt_flags2
& PF_SOPEN
)) {
534 ptydebug(1, "ptcclose: slaves are not open\n");
535 destroy_dev(pti
->devs
);
536 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty
), dev
->si_uminor
);
545 ptcread(struct dev_read_args
*ap
)
547 cdev_t dev
= ap
->a_head
.a_dev
;
548 struct tty
*tp
= dev
->si_tty
;
549 struct pt_ioctl
*pti
= dev
->si_drv1
;
554 * We want to block until the slave
555 * is open, and there's something to read;
556 * but if we lost the slave or we're NBIO,
557 * then return the appropriate error instead.
560 if (tp
->t_state
&TS_ISOPEN
) {
561 if (pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) {
562 error
= ureadc((int)pti
->pt_send
, ap
->a_uio
);
565 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
566 cc
= (int)szmin(ap
->a_uio
->uio_resid
,
567 sizeof(tp
->t_termios
));
568 uiomove((caddr_t
)&tp
->t_termios
, cc
,
574 if (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
) {
575 error
= ureadc((int)pti
->pt_ucntl
, ap
->a_uio
);
581 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
584 if ((tp
->t_state
& TS_CONNECTED
) == 0)
585 return (0); /* EOF */
586 if (ap
->a_ioflag
& IO_NDELAY
)
587 return (EWOULDBLOCK
);
588 error
= tsleep(TSA_PTC_READ(tp
), PCATCH
, "ptcin", 0);
592 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
593 error
= ureadc(0, ap
->a_uio
);
594 while (ap
->a_uio
->uio_resid
> 0 && error
== 0) {
595 cc
= q_to_b(&tp
->t_outq
, buf
,
596 (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
));
599 error
= uiomove(buf
, (size_t)cc
, ap
->a_uio
);
606 ptsstop(struct tty
*tp
, int flush
)
608 struct pt_ioctl
*pti
= tp
->t_dev
->si_drv1
;
611 /* note: FLUSHREAD and FLUSHWRITE already ok */
613 flush
= TIOCPKT_STOP
;
614 pti
->pt_flags
|= PF_STOPPED
;
616 pti
->pt_flags
&= ~PF_STOPPED
;
617 pti
->pt_send
|= flush
;
618 /* change of perspective */
628 ptcpoll(struct dev_poll_args
*ap
)
630 cdev_t dev
= ap
->a_head
.a_dev
;
631 struct tty
*tp
= dev
->si_tty
;
632 struct pt_ioctl
*pti
= dev
->si_drv1
;
635 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
636 ap
->a_events
= seltrue(dev
, ap
->a_events
) | POLLHUP
;
641 * Need to block timeouts (ttrstart).
645 if (ap
->a_events
& (POLLIN
| POLLRDNORM
))
646 if ((tp
->t_state
& TS_ISOPEN
) &&
647 ((tp
->t_outq
.c_cc
&& (tp
->t_state
& TS_TTSTOP
) == 0) ||
648 ((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
649 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
)))
650 revents
|= ap
->a_events
& (POLLIN
| POLLRDNORM
);
652 if (ap
->a_events
& (POLLOUT
| POLLWRNORM
))
653 if (tp
->t_state
& TS_ISOPEN
&&
654 ((pti
->pt_flags
& PF_REMOTE
) ?
655 (tp
->t_canq
.c_cc
== 0) :
656 ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
- 2) ||
657 (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
& ICANON
)))))
658 revents
|= ap
->a_events
& (POLLOUT
| POLLWRNORM
);
660 if (ap
->a_events
& POLLHUP
)
661 if ((tp
->t_state
& TS_CARR_ON
) == 0)
665 if (ap
->a_events
& (POLLIN
| POLLRDNORM
))
666 selrecord(curthread
, &pti
->pt_selr
);
668 if (ap
->a_events
& (POLLOUT
| POLLWRNORM
))
669 selrecord(curthread
, &pti
->pt_selw
);
673 ap
->a_events
= revents
;
678 ptcwrite(struct dev_write_args
*ap
)
680 cdev_t dev
= ap
->a_head
.a_dev
;
681 struct tty
*tp
= dev
->si_tty
;
684 u_char locbuf
[BUFSIZ
];
686 struct pt_ioctl
*pti
= dev
->si_drv1
;
690 if ((tp
->t_state
&TS_ISOPEN
) == 0)
692 if (pti
->pt_flags
& PF_REMOTE
) {
695 while ((ap
->a_uio
->uio_resid
> 0 || cc
> 0) &&
696 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
698 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
699 cc
= imin(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
701 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
704 /* check again for safety */
705 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
706 /* adjust as usual */
707 ap
->a_uio
->uio_resid
+= cc
;
712 cc
= b_to_q((char *)cp
, cc
, &tp
->t_canq
);
714 * XXX we don't guarantee that the canq size
715 * is >= TTYHOG, so the above b_to_q() may
716 * leave some bytes uncopied. However, space
717 * is guaranteed for the null terminator if
718 * we don't fail here since (TTYHOG - 1) is
719 * not a multiple of CBSIZE.
725 /* adjust for data copied in but not written */
726 ap
->a_uio
->uio_resid
+= cc
;
727 clist_putc(0, &tp
->t_canq
);
729 wakeup(TSA_PTS_READ(tp
));
732 while (ap
->a_uio
->uio_resid
> 0 || cc
> 0) {
734 cc
= (int)szmin(ap
->a_uio
->uio_resid
, BUFSIZ
);
736 error
= uiomove(cp
, (size_t)cc
, ap
->a_uio
);
739 /* check again for safety */
740 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
741 /* adjust for data copied in but not written */
742 ap
->a_uio
->uio_resid
+= cc
;
747 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
748 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
749 wakeup(TSA_HUP_OR_INPUT(tp
));
752 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
761 * Come here to wait for slave to open, for space
762 * in outq, or space in rawq, or an empty canq.
764 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
765 /* adjust for data copied in but not written */
766 ap
->a_uio
->uio_resid
+= cc
;
769 if (ap
->a_ioflag
& IO_NDELAY
) {
770 /* adjust for data copied in but not written */
771 ap
->a_uio
->uio_resid
+= cc
;
773 return (EWOULDBLOCK
);
776 error
= tsleep(TSA_PTC_WRITE(tp
), PCATCH
, "ptcout", 0);
778 /* adjust for data copied in but not written */
779 ap
->a_uio
->uio_resid
+= cc
;
787 ptyioctl(struct dev_ioctl_args
*ap
)
789 cdev_t dev
= ap
->a_head
.a_dev
;
790 struct tty
*tp
= dev
->si_tty
;
791 struct pt_ioctl
*pti
= dev
->si_drv1
;
792 u_char
*cc
= tp
->t_cc
;
795 if (dev_dflags(dev
) & D_MASTER
) {
800 * We avoid calling ttioctl on the controller since,
801 * in that case, tp must be the controlling terminal.
803 *(int *)ap
->a_data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
807 if (*(int *)ap
->a_data
) {
808 if (pti
->pt_flags
& PF_UCNTL
)
810 pti
->pt_flags
|= PF_PKT
;
812 pti
->pt_flags
&= ~PF_PKT
;
816 if (*(int *)ap
->a_data
) {
817 if (pti
->pt_flags
& PF_PKT
)
819 pti
->pt_flags
|= PF_UCNTL
;
821 pti
->pt_flags
&= ~PF_UCNTL
;
825 if (*(int *)ap
->a_data
)
826 pti
->pt_flags
|= PF_REMOTE
;
828 pti
->pt_flags
&= ~PF_REMOTE
;
829 ttyflush(tp
, FREAD
|FWRITE
);
833 if ((pti
->pt_flags2
& PF_UNIX98
) && (pti
->devc
== dev
))
840 * The rest of the ioctls shouldn't be called until
843 if ((tp
->t_state
& TS_ISOPEN
) == 0)
856 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
857 * ttywflush(tp) will hang if there are characters in
860 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
864 if (*(unsigned int *)ap
->a_data
>= NSIG
||
865 *(unsigned int *)ap
->a_data
== 0)
867 if ((tp
->t_lflag
&NOFLSH
) == 0)
868 ttyflush(tp
, FREAD
|FWRITE
);
869 pgsignal(tp
->t_pgrp
, *(unsigned int *)ap
->a_data
, 1);
870 if ((*(unsigned int *)ap
->a_data
== SIGINFO
) &&
871 ((tp
->t_lflag
&NOKERNINFO
) == 0))
876 if (ap
->a_cmd
== TIOCEXT
) {
878 * When the EXTPROC bit is being toggled, we need
879 * to send an TIOCPKT_IOCTL if the packet driver
882 if (*(int *)ap
->a_data
) {
883 if (pti
->pt_flags
& PF_PKT
) {
884 pti
->pt_send
|= TIOCPKT_IOCTL
;
885 ptcwakeup(tp
, FREAD
);
887 tp
->t_lflag
|= EXTPROC
;
889 if ((tp
->t_lflag
& EXTPROC
) &&
890 (pti
->pt_flags
& PF_PKT
)) {
891 pti
->pt_send
|= TIOCPKT_IOCTL
;
892 ptcwakeup(tp
, FREAD
);
894 tp
->t_lflag
&= ~EXTPROC
;
898 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, ap
->a_cmd
, ap
->a_data
,
899 ap
->a_fflag
, ap
->a_cred
);
900 if (error
== ENOIOCTL
)
901 error
= ttioctl(tp
, ap
->a_cmd
, ap
->a_data
, ap
->a_fflag
);
902 if (error
== ENOIOCTL
) {
903 if (pti
->pt_flags
& PF_UCNTL
&&
904 (ap
->a_cmd
& ~0xff) == UIOCCMD(0)) {
905 if (ap
->a_cmd
& 0xff) {
906 pti
->pt_ucntl
= (u_char
)ap
->a_cmd
;
907 ptcwakeup(tp
, FREAD
);
914 * If external processing and packet mode send ioctl packet.
916 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
925 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
932 pti
->pt_send
|= TIOCPKT_IOCTL
;
933 ptcwakeup(tp
, FREAD
);
938 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
939 && CCEQ(cc
[VSTART
], CTRL('q'));
940 if (pti
->pt_flags
& PF_NOSTOP
) {
942 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
943 pti
->pt_send
|= TIOCPKT_DOSTOP
;
944 pti
->pt_flags
&= ~PF_NOSTOP
;
945 ptcwakeup(tp
, FREAD
);
949 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
950 pti
->pt_send
|= TIOCPKT_NOSTOP
;
951 pti
->pt_flags
|= PF_NOSTOP
;
952 ptcwakeup(tp
, FREAD
);
959 static void ptc_drvinit (void *unused
);
962 SYSCTL_INT(_kern
, OID_AUTO
, pty_debug
, CTLFLAG_RW
, &pty_debug_level
,
963 0, "Change pty debug level");
967 ptc_drvinit(void *unused
)
974 * Create the clonable base device.
976 make_autoclone_dev(&ptc_ops
, &DEVFS_CLONE_BITMAP(pty
), ptyclone
,
980 for (i
= 0; i
< 256; i
++) {
985 SYSINIT(ptcdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR_C
,ptc_drvinit
,NULL
)