2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
30 * Copyright (c) 1982, 1986, 1989, 1993
31 * The Regents of the University of California. All rights reserved.
33 * Redistribution and use in source and binary forms, with or without
34 * modification, are permitted provided that the following conditions
36 * 1. Redistributions of source code must retain the above copyright
37 * notice, this list of conditions and the following disclaimer.
38 * 2. Redistributions in binary form must reproduce the above copyright
39 * notice, this list of conditions and the following disclaimer in the
40 * documentation and/or other materials provided with the distribution.
41 * 3. All advertising materials mentioning features or use of this software
42 * must display the following acknowledgement:
43 * This product includes software developed by the University of
44 * California, Berkeley and its contributors.
45 * 4. Neither the name of the University nor the names of its contributors
46 * may be used to endorse or promote products derived from this software
47 * without specific prior written permission.
49 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
50 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
51 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
52 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
53 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
54 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
55 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
56 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
57 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
58 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
65 * Pseudo-teletype Driver
66 * (Actually two drivers, requiring two entries in 'cdevsw')
68 #include "pty.h" /* XXX */
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/ioctl.h>
73 #include <sys/proc_internal.h>
74 #include <sys/kauth.h>
77 #include <sys/file_internal.h>
79 #include <sys/kernel.h>
80 #include <sys/vnode.h>
82 #include <sys/signalvar.h>
86 #define FREE_BSDSTATIC static
88 #define FREE_BSDSTATIC __private_extern__
89 #define d_devtotty_t struct tty **
94 typedef void d_stop_t(struct tty
*tp
, int rw
);
98 /* XXX function should be removed??? */
99 int pty_init(int n_ptys
);
102 static void ptyattach(int n
);
104 static void ptsstart(struct tty
*tp
);
105 static void ptcwakeup(struct tty
*tp
, int flag
);
107 FREE_BSDSTATIC d_open_t ptsopen
;
108 FREE_BSDSTATIC d_close_t ptsclose
;
109 FREE_BSDSTATIC d_read_t ptsread
;
110 FREE_BSDSTATIC d_write_t ptswrite
;
111 FREE_BSDSTATIC d_ioctl_t ptyioctl
;
112 FREE_BSDSTATIC d_stop_t ptsstop
;
113 FREE_BSDSTATIC d_devtotty_t ptydevtotty
;
114 FREE_BSDSTATIC d_open_t ptcopen
;
115 FREE_BSDSTATIC d_close_t ptcclose
;
116 FREE_BSDSTATIC d_read_t ptcread
;
117 FREE_BSDSTATIC d_write_t ptcwrite
;
118 FREE_BSDSTATIC d_select_t ptcselect
;
121 #define CDEV_MAJOR_S 5
122 #define CDEV_MAJOR_C 6
123 static struct cdevsw pts_cdevsw
=
124 { ptsopen
, ptsclose
, ptsread
, ptswrite
, /*5*/
125 ptyioctl
, ptsstop
, nullreset
, ptydevtotty
,/* ttyp */
126 ttselect
, nommap
, NULL
, "pts", NULL
, -1 };
128 static struct cdevsw ptc_cdevsw
=
129 { ptcopen
, ptcclose
, ptcread
, ptcwrite
, /*6*/
130 ptyioctl
, nullstop
, nullreset
, ptydevtotty
,/* ptyp */
131 ptcselect
, nommap
, NULL
, "ptc", NULL
, -1 };
137 #define NPTY 32 /* crude XXX */
138 #warning You have only one pty defined, redefining to 32.
143 #define MAXUNITS (8 * 32)
144 static void *devfs_token_pts
[MAXUNITS
];
145 static void *devfs_token_ptc
[MAXUNITS
];
146 static const char jnames
[] = "pqrsPQRS";
149 #define NPTY MAXUNITS
150 #warning Can't have more than 256 pty's with DEVFS defined.
151 #endif /* NPTY > MAXUNITS */
155 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
158 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
159 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
162 FREE_BSDSTATIC
struct tty pt_tty
[NPTY
]; /* XXX */
164 /* NeXT All references to have been changed to indirections in the file */
165 FREE_BSDSTATIC
struct tty
*pt_tty
[NPTY
] = { NULL
};
168 static struct pt_ioctl
{
170 struct selinfo pt_selr
, pt_selw
;
173 } pt_ioctl
[NPTY
]; /* XXX */
174 static int npty
= NPTY
; /* for pstat -t */
176 #define PF_PKT 0x08 /* packet mode */
177 #define PF_STOPPED 0x10 /* user told stopped */
178 #define PF_REMOTE 0x20 /* remote and flow controlled input */
179 #define PF_NOSTOP 0x40
180 #define PF_UCNTL 0x80 /* user control mode */
184 * Establish n (or default if n is 1) ptys in the system.
186 * XXX cdevsw & pstat require the array `pty[]' to be an array
194 #define DEFAULT_NPTY 32
196 /* maybe should allow 0 => none? */
199 ntb
= n
* sizeof(struct tty
);
201 mem
= malloc(ntb
+ ALIGNBYTES
+ n
* sizeof(struct pt_ioctl
),
204 MALLOC(mem
, char *, ntb
+ ALIGNBYTES
+ n
* sizeof(struct pt_ioctl
),
207 pt_tty
= (struct tty
*)mem
;
208 mem
= (char *)ALIGN(mem
+ ntb
);
209 pt_ioctl
= (struct pt_ioctl
*)mem
;
216 pty_init(__unused
int n_ptys
)
221 #include <miscfs/devfs/devfs.h>
222 #define START_CHAR 'p'
230 /* create the pseudo tty device nodes */
231 for (j
= 0; j
< 10; j
++) {
232 for (i
= 0; i
< HEX_BASE
; i
++) {
233 int m
= j
* HEX_BASE
+ i
;
236 (void)devfs_make_node(makedev(4, m
),
237 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
238 "tty%c%x", j
+ START_CHAR
, i
);
239 (void)devfs_make_node(makedev(5, m
),
240 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
241 "pty%c%x", j
+ START_CHAR
, i
);
251 ptsopen(dev_t dev
, int flag
, __unused
int devtype
, __unused
struct proc
*p
)
253 register struct tty
*tp
;
255 boolean_t funnel_state
;
257 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
259 tp
= &pt_tty
[minor(dev
)];
262 * You will see this sort of code coming up in diffs later both
263 * the ttymalloc and the tp indirection.
265 if (minor(dev
) >= npty
) {
269 if (!pt_tty
[minor(dev
)]) {
270 tp
= pt_tty
[minor(dev
)] = ttymalloc();
272 tp
= pt_tty
[minor(dev
)];
274 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
275 ttychars(tp
); /* Set up default chars */
276 tp
->t_iflag
= TTYDEF_IFLAG
;
277 tp
->t_oflag
= TTYDEF_OFLAG
;
278 tp
->t_lflag
= TTYDEF_LFLAG
;
279 tp
->t_cflag
= TTYDEF_CFLAG
;
280 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
281 ttsetwater(tp
); /* would be done in xxparam() */
282 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
286 if (tp
->t_oproc
) /* Ctrlr still around. */
287 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
288 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
291 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
296 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
298 ptcwakeup(tp
, FREAD
|FWRITE
);
300 (void) thread_funnel_set(kernel_flock
, funnel_state
);
305 ptsclose(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
307 register struct tty
*tp
;
309 boolean_t funnel_state
;
311 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
313 tp
= pt_tty
[minor(dev
)];
314 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
315 ptsstop(tp
, FREAD
|FWRITE
);
318 (void) thread_funnel_set(kernel_flock
, funnel_state
);
323 ptsread(dev
, uio
, flag
)
329 struct proc
*p
= curproc
;
331 struct proc
*p
= current_proc();
333 register struct tty
*tp
= pt_tty
[minor(dev
)];
334 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
337 boolean_t funnel_state
;
339 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
342 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
344 if (pti
->pt_flags
& PF_REMOTE
) {
345 while (isbackground(p
, tp
)) {
346 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
347 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
348 p
->p_pgrp
->pg_jobc
== 0 ||
349 p
->p_flag
& P_PPWAIT
) {
353 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
354 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsbg",
359 if (tp
->t_canq
.c_cc
== 0) {
360 if (flag
& IO_NDELAY
)
361 return (EWOULDBLOCK
);
362 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
368 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
372 cc
= min(uio_resid(uio
), BUFSIZ
);
373 // Don't copy the very last byte
374 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
375 cc
= q_to_b(&tp
->t_canq
, buf
, cc
);
376 error
= uiomove(buf
, cc
, uio
);
380 if (tp
->t_canq
.c_cc
== 1)
381 (void) getc(&tp
->t_canq
);
386 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
387 ptcwakeup(tp
, FWRITE
);
389 (void) thread_funnel_set(kernel_flock
, funnel_state
);
394 * Write to pseudo-tty.
395 * Wakeups of controlling tty will happen
396 * indirectly, when tty driver calls ptsstart.
399 ptswrite(dev
, uio
, flag
)
404 register struct tty
*tp
;
406 boolean_t funnel_state
;
408 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
410 tp
= pt_tty
[minor(dev
)];
411 if (tp
->t_oproc
== 0)
414 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
416 (void) thread_funnel_set(kernel_flock
, funnel_state
);
421 * Start output on pseudo-tty.
422 * Wake up process selecting or sleeping for input from controlling tty.
428 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
429 boolean_t funnel_state
;
431 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
433 if (tp
->t_state
& TS_TTSTOP
)
435 if (pti
->pt_flags
& PF_STOPPED
) {
436 pti
->pt_flags
&= ~PF_STOPPED
;
437 pti
->pt_send
= TIOCPKT_START
;
439 ptcwakeup(tp
, FREAD
);
441 (void) thread_funnel_set(kernel_flock
, funnel_state
);
450 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
451 boolean_t funnel_state
;
453 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
456 selwakeup(&pti
->pt_selr
);
457 wakeup(TSA_PTC_READ(tp
));
460 selwakeup(&pti
->pt_selw
);
461 wakeup(TSA_PTC_WRITE(tp
));
463 (void) thread_funnel_set(kernel_flock
, funnel_state
);
467 ptcopen(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
469 register struct tty
*tp
;
470 struct pt_ioctl
*pti
;
472 boolean_t funnel_state
;
474 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
476 if (minor(dev
) >= npty
) {
480 if(!pt_tty
[minor(dev
)]) {
481 tp
= pt_tty
[minor(dev
)] = ttymalloc();
483 tp
= pt_tty
[minor(dev
)];
488 tp
->t_oproc
= ptsstart
;
489 CLR(tp
->t_state
, TS_ZOMBIE
);
491 tp
->t_stop
= ptsstop
;
493 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
494 tp
->t_lflag
&= ~EXTPROC
;
495 pti
= &pt_ioctl
[minor(dev
)];
500 (void) thread_funnel_set(kernel_flock
, funnel_state
);
505 ptcclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
507 register struct tty
*tp
;
508 boolean_t funnel_state
;
510 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
512 tp
= pt_tty
[minor(dev
)];
513 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
516 * XXX MDMBUF makes no sense for ptys but would inhibit the above
517 * l_modem(). CLOCAL makes sense but isn't supported. Special
518 * l_modem()s that ignore carrier drop make no sense for ptys but
519 * may be in use because other parts of the line discipline make
520 * sense for ptys. Recover by doing everything that a normal
521 * ttymodem() would have done except for sending a SIGHUP.
523 if (tp
->t_state
& TS_ISOPEN
) {
524 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
525 tp
->t_state
|= TS_ZOMBIE
;
526 ttyflush(tp
, FREAD
| FWRITE
);
529 tp
->t_oproc
= 0; /* mark closed */
531 (void) thread_funnel_set(kernel_flock
, funnel_state
);
536 ptcread(dev
, uio
, flag
)
541 register struct tty
*tp
= pt_tty
[minor(dev
)];
542 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
545 boolean_t funnel_state
;
547 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
550 * We want to block until the slave
551 * is open, and there's something to read;
552 * but if we lost the slave or we're NBIO,
553 * then return the appropriate error instead.
556 if (tp
->t_state
&TS_ISOPEN
) {
557 if (pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) {
558 error
= ureadc((int)pti
->pt_send
, uio
);
561 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
562 cc
= min(uio_resid(uio
),
563 sizeof(tp
->t_termios
));
564 uiomove((caddr_t
)&tp
->t_termios
, cc
,
570 if (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
) {
571 error
= ureadc((int)pti
->pt_ucntl
, uio
);
577 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
580 if ((tp
->t_state
& TS_CONNECTED
) == 0)
582 if (flag
& IO_NDELAY
) {
586 error
= tsleep(TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptcin", 0);
590 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
591 error
= ureadc(0, uio
);
592 while (uio_resid(uio
) > 0 && error
== 0) {
593 cc
= q_to_b(&tp
->t_outq
, buf
, min(uio_resid(uio
), BUFSIZ
));
596 error
= uiomove(buf
, cc
, uio
);
598 (*linesw
[tp
->t_line
].l_start
)(tp
);
601 (void) thread_funnel_set(kernel_flock
, funnel_state
);
607 register struct tty
*tp
;
610 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
612 boolean_t funnel_state
;
614 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
616 /* note: FLUSHREAD and FLUSHWRITE already ok */
618 flush
= TIOCPKT_STOP
;
619 pti
->pt_flags
|= PF_STOPPED
;
621 pti
->pt_flags
&= ~PF_STOPPED
;
622 pti
->pt_send
|= flush
;
623 /* change of perspective */
631 (void) thread_funnel_set(kernel_flock
, funnel_state
);
635 ptcselect(dev
, rw
, wql
, p
)
641 register struct tty
*tp
= pt_tty
[minor(dev
)];
642 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
644 boolean_t funnel_state
;
646 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
648 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
656 * Need to block timeouts (ttrstart).
658 if ((tp
->t_state
&TS_ISOPEN
) &&
659 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
665 case 0: /* exceptional */
666 if ((tp
->t_state
&TS_ISOPEN
) &&
667 ((pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) ||
668 (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
))) {
672 selrecord(p
, &pti
->pt_selr
, wql
);
677 if (tp
->t_state
&TS_ISOPEN
) {
678 if (pti
->pt_flags
& PF_REMOTE
) {
679 if (tp
->t_canq
.c_cc
== 0) {
684 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
-2) {
688 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_iflag
&ICANON
)) {
694 selrecord(p
, &pti
->pt_selw
, wql
);
699 (void) thread_funnel_set(kernel_flock
, funnel_state
);
704 ptcwrite(dev
, uio
, flag
)
706 register struct uio
*uio
;
709 register struct tty
*tp
= pt_tty
[minor(dev
)];
710 register u_char
*cp
= NULL
;
712 u_char locbuf
[BUFSIZ
];
714 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
716 boolean_t funnel_state
;
718 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
721 if ((tp
->t_state
&TS_ISOPEN
) == 0)
723 if (pti
->pt_flags
& PF_REMOTE
) {
726 while ((uio_resid(uio
) > 0 || cc
> 0) &&
727 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
729 cc
= min(uio_resid(uio
), BUFSIZ
);
730 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
732 error
= uiomove((caddr_t
)cp
, cc
, uio
);
735 /* check again for safety */
736 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
737 /* adjust as usual */
738 uio_setresid(uio
, (uio_resid(uio
) + cc
));
744 cc
= b_to_q((char *)cp
, cc
, &tp
->t_canq
);
746 * XXX we don't guarantee that the canq size
747 * is >= TTYHOG, so the above b_to_q() may
748 * leave some bytes uncopied. However, space
749 * is guaranteed for the null terminator if
750 * we don't fail here since (TTYHOG - 1) is
751 * not a multiple of CBSIZE.
757 /* adjust for data copied in but not written */
758 uio_setresid(uio
, (uio_resid(uio
) + cc
));
759 (void) putc(0, &tp
->t_canq
);
761 wakeup(TSA_PTS_READ(tp
));
764 while (uio_resid(uio
) > 0 || cc
> 0) {
766 cc
= min(uio_resid(uio
), BUFSIZ
);
768 error
= uiomove((caddr_t
)cp
, cc
, uio
);
771 /* check again for safety */
772 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
773 /* adjust for data copied in but not written */
774 uio_setresid(uio
, (uio_resid(uio
) + cc
));
780 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
781 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_iflag
&ICANON
))) {
782 wakeup(TSA_HUP_OR_INPUT(tp
));
785 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
792 (void) thread_funnel_set(kernel_flock
, funnel_state
);
796 * Come here to wait for slave to open, for space
797 * in outq, or space in rawq, or an empty canq.
799 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
800 /* adjust for data copied in but not written */
801 uio_setresid(uio
, (uio_resid(uio
) + cc
));
805 if (flag
& IO_NDELAY
) {
806 /* adjust for data copied in but not written */
807 uio_setresid(uio
, (uio_resid(uio
) + cc
));
812 error
= tsleep(TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptcout", 0);
814 /* adjust for data copied in but not written */
815 uio_setresid(uio
, (uio_resid(uio
) + cc
));
822 /* XXX we eventually want to go to this model,
823 * but premier can't change the cdevsw */
828 if (minor(dev
) >= npty
)
831 return &pt_tty
[minor(dev
)];
838 ptyioctl(dev
, cmd
, data
, flag
)
844 ptyioctl(dev
, cmd
, data
, flag
, p
)
852 register struct tty
*tp
= pt_tty
[minor(dev
)];
853 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
854 register u_char
*cc
= tp
->t_cc
;
856 boolean_t funnel_state
;
858 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
861 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
862 * ttywflush(tp) will hang if there are characters in the outq.
864 if (cmd
== TIOCEXT
) {
866 * When the EXTPROC bit is being toggled, we need
867 * to send an TIOCPKT_IOCTL if the packet driver
871 if (pti
->pt_flags
& PF_PKT
) {
872 pti
->pt_send
|= TIOCPKT_IOCTL
;
873 ptcwakeup(tp
, FREAD
);
875 tp
->t_lflag
|= EXTPROC
;
877 if ((tp
->t_lflag
& EXTPROC
) &&
878 (pti
->pt_flags
& PF_PKT
)) {
879 pti
->pt_send
|= TIOCPKT_IOCTL
;
880 ptcwakeup(tp
, FREAD
);
882 tp
->t_lflag
&= ~EXTPROC
;
887 if (cdevsw
[major(dev
)]->d_open
== ptcopen
)
889 if (cdevsw
[major(dev
)].d_open
== ptcopen
)
895 * We aviod calling ttioctl on the controller since,
896 * in that case, tp must be the controlling terminal.
898 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
903 if (pti
->pt_flags
& PF_UCNTL
) {
907 pti
->pt_flags
|= PF_PKT
;
909 pti
->pt_flags
&= ~PF_PKT
;
914 if (pti
->pt_flags
& PF_PKT
) {
918 pti
->pt_flags
|= PF_UCNTL
;
920 pti
->pt_flags
&= ~PF_UCNTL
;
925 pti
->pt_flags
|= PF_REMOTE
;
927 pti
->pt_flags
&= ~PF_REMOTE
;
928 ttyflush(tp
, FREAD
|FWRITE
);
939 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
943 if (*(unsigned int *)data
>= NSIG
||
944 *(unsigned int *)data
== 0) {
948 if ((tp
->t_lflag
&NOFLSH
) == 0)
949 ttyflush(tp
, FREAD
|FWRITE
);
950 pgsignal(tp
->t_pgrp
, *(unsigned int *)data
, 1);
951 if ((*(unsigned int *)data
== SIGINFO
) &&
952 ((tp
->t_lflag
&NOKERNINFO
) == 0))
956 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
957 if (error
== ENOTTY
) {
958 error
= ttioctl(tp
, cmd
, data
, flag
, p
);
960 && pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
961 /* Process the UIOCMD ioctl group */
963 pti
->pt_ucntl
= (u_char
)cmd
;
964 ptcwakeup(tp
, FREAD
);
972 * If external processing and packet mode send ioctl packet.
974 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
983 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
990 pti
->pt_send
|= TIOCPKT_IOCTL
;
991 ptcwakeup(tp
, FREAD
);
996 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
997 && CCEQ(cc
[VSTART
], CTRL('q'));
998 if (pti
->pt_flags
& PF_NOSTOP
) {
1000 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1001 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1002 pti
->pt_flags
&= ~PF_NOSTOP
;
1003 ptcwakeup(tp
, FREAD
);
1007 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1008 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1009 pti
->pt_flags
|= PF_NOSTOP
;
1010 ptcwakeup(tp
, FREAD
);
1014 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1019 static ptc_devsw_installed
= 0;
1022 ptc_drvinit(void *unused
)
1029 if( ! ptc_devsw_installed
) {
1030 dev
= makedev(CDEV_MAJOR_S
, 0);
1031 cdevsw_add(&dev
, &pts_cdevsw
, NULL
);
1032 dev
= makedev(CDEV_MAJOR_C
, 0);
1033 cdevsw_add(&dev
, &ptc_cdevsw
, NULL
);
1034 ptc_devsw_installed
= 1;
1036 for ( i
= 0 ; i
<NPTY
; i
++ ) {
1039 devfs_token_pts
[i
] =
1040 devfs_add_devswf(&pts_cdevsw
,i
,
1042 "tty%c%n",jnames
[j
],k
);
1043 devfs_token_ptc
[i
] =
1044 devfs_add_devswf(&ptc_cdevsw
,i
,
1046 "pty%c%n",jnames
[j
],k
);
1052 SYSINIT(ptcdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR_C
,ptc_drvinit
,NULL
)