2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
25 * Copyright (c) 1982, 1986, 1989, 1993
26 * The Regents of the University of California. All rights reserved.
28 * Redistribution and use in source and binary forms, with or without
29 * modification, are permitted provided that the following conditions
31 * 1. Redistributions of source code must retain the above copyright
32 * notice, this list of conditions and the following disclaimer.
33 * 2. Redistributions in binary form must reproduce the above copyright
34 * notice, this list of conditions and the following disclaimer in the
35 * documentation and/or other materials provided with the distribution.
36 * 3. All advertising materials mentioning features or use of this software
37 * must display the following acknowledgement:
38 * This product includes software developed by the University of
39 * California, Berkeley and its contributors.
40 * 4. Neither the name of the University nor the names of its contributors
41 * may be used to endorse or promote products derived from this software
42 * without specific prior written permission.
44 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
45 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
47 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
48 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
49 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
50 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
51 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
52 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
53 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
56 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
60 * Pseudo-teletype Driver
61 * (Actually two drivers, requiring two entries in 'cdevsw')
63 #include "pty.h" /* XXX */
65 #include <sys/param.h>
66 #include <sys/systm.h>
67 #include <sys/ioctl.h>
68 #include <sys/proc_internal.h>
69 #include <sys/kauth.h>
72 #include <sys/file_internal.h>
74 #include <sys/kernel.h>
75 #include <sys/vnode.h>
77 #include <sys/signalvar.h>
81 #define FREE_BSDSTATIC static
83 #define FREE_BSDSTATIC __private_extern__
84 #define d_devtotty_t struct tty **
89 typedef void d_stop_t(struct tty
*tp
, int rw
);
93 /* XXX function should be removed??? */
94 int pty_init(int n_ptys
);
97 static void ptyattach(int n
);
99 static void ptsstart(struct tty
*tp
);
100 static void ptcwakeup(struct tty
*tp
, int flag
);
102 FREE_BSDSTATIC d_open_t ptsopen
;
103 FREE_BSDSTATIC d_close_t ptsclose
;
104 FREE_BSDSTATIC d_read_t ptsread
;
105 FREE_BSDSTATIC d_write_t ptswrite
;
106 FREE_BSDSTATIC d_ioctl_t ptyioctl
;
107 FREE_BSDSTATIC d_stop_t ptsstop
;
108 FREE_BSDSTATIC d_devtotty_t ptydevtotty
;
109 FREE_BSDSTATIC d_open_t ptcopen
;
110 FREE_BSDSTATIC d_close_t ptcclose
;
111 FREE_BSDSTATIC d_read_t ptcread
;
112 FREE_BSDSTATIC d_write_t ptcwrite
;
113 FREE_BSDSTATIC d_select_t ptcselect
;
116 #define CDEV_MAJOR_S 5
117 #define CDEV_MAJOR_C 6
118 static struct cdevsw pts_cdevsw
=
119 { ptsopen
, ptsclose
, ptsread
, ptswrite
, /*5*/
120 ptyioctl
, ptsstop
, nullreset
, ptydevtotty
,/* ttyp */
121 ttselect
, nommap
, NULL
, "pts", NULL
, -1 };
123 static struct cdevsw ptc_cdevsw
=
124 { ptcopen
, ptcclose
, ptcread
, ptcwrite
, /*6*/
125 ptyioctl
, nullstop
, nullreset
, ptydevtotty
,/* ptyp */
126 ptcselect
, nommap
, NULL
, "ptc", NULL
, -1 };
132 #define NPTY 32 /* crude XXX */
133 #warning You have only one pty defined, redefining to 32.
138 #define MAXUNITS (8 * 32)
139 static void *devfs_token_pts
[MAXUNITS
];
140 static void *devfs_token_ptc
[MAXUNITS
];
141 static const char jnames
[] = "pqrsPQRS";
144 #define NPTY MAXUNITS
145 #warning Can't have more than 256 pty's with DEVFS defined.
146 #endif /* NPTY > MAXUNITS */
150 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
153 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
154 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
157 FREE_BSDSTATIC
struct tty pt_tty
[NPTY
]; /* XXX */
159 /* NeXT All references to have been changed to indirections in the file */
160 FREE_BSDSTATIC
struct tty
*pt_tty
[NPTY
] = { NULL
};
163 static struct pt_ioctl
{
165 struct selinfo pt_selr
, pt_selw
;
168 } pt_ioctl
[NPTY
]; /* XXX */
169 static int npty
= NPTY
; /* for pstat -t */
171 #define PF_PKT 0x08 /* packet mode */
172 #define PF_STOPPED 0x10 /* user told stopped */
173 #define PF_REMOTE 0x20 /* remote and flow controlled input */
174 #define PF_NOSTOP 0x40
175 #define PF_UCNTL 0x80 /* user control mode */
179 * Establish n (or default if n is 1) ptys in the system.
181 * XXX cdevsw & pstat require the array `pty[]' to be an array
189 #define DEFAULT_NPTY 32
191 /* maybe should allow 0 => none? */
194 ntb
= n
* sizeof(struct tty
);
196 mem
= malloc(ntb
+ ALIGNBYTES
+ n
* sizeof(struct pt_ioctl
),
199 MALLOC(mem
, char *, ntb
+ ALIGNBYTES
+ n
* sizeof(struct pt_ioctl
),
202 pt_tty
= (struct tty
*)mem
;
203 mem
= (char *)ALIGN(mem
+ ntb
);
204 pt_ioctl
= (struct pt_ioctl
*)mem
;
211 pty_init(__unused
int n_ptys
)
216 #include <miscfs/devfs/devfs.h>
217 #define START_CHAR 'p'
225 /* create the pseudo tty device nodes */
226 for (j
= 0; j
< 10; j
++) {
227 for (i
= 0; i
< HEX_BASE
; i
++) {
228 int m
= j
* HEX_BASE
+ i
;
231 (void)devfs_make_node(makedev(4, m
),
232 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
233 "tty%c%x", j
+ START_CHAR
, i
);
234 (void)devfs_make_node(makedev(5, m
),
235 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
236 "pty%c%x", j
+ START_CHAR
, i
);
246 ptsopen(dev_t dev
, int flag
, __unused
int devtype
, __unused
struct proc
*p
)
248 register struct tty
*tp
;
250 boolean_t funnel_state
;
252 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
254 tp
= &pt_tty
[minor(dev
)];
257 * You will see this sort of code coming up in diffs later both
258 * the ttymalloc and the tp indirection.
260 if (minor(dev
) >= npty
) {
264 if (!pt_tty
[minor(dev
)]) {
265 tp
= pt_tty
[minor(dev
)] = ttymalloc();
267 tp
= pt_tty
[minor(dev
)];
269 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
270 ttychars(tp
); /* Set up default chars */
271 tp
->t_iflag
= TTYDEF_IFLAG
;
272 tp
->t_oflag
= TTYDEF_OFLAG
;
273 tp
->t_lflag
= TTYDEF_LFLAG
;
274 tp
->t_cflag
= TTYDEF_CFLAG
;
275 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
276 ttsetwater(tp
); /* would be done in xxparam() */
277 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
281 if (tp
->t_oproc
) /* Ctrlr still around. */
282 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
283 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
286 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
291 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
293 ptcwakeup(tp
, FREAD
|FWRITE
);
295 (void) thread_funnel_set(kernel_flock
, funnel_state
);
300 ptsclose(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
302 register struct tty
*tp
;
304 boolean_t funnel_state
;
306 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
308 tp
= pt_tty
[minor(dev
)];
309 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
310 ptsstop(tp
, FREAD
|FWRITE
);
313 (void) thread_funnel_set(kernel_flock
, funnel_state
);
318 ptsread(dev
, uio
, flag
)
324 struct proc
*p
= curproc
;
326 struct proc
*p
= current_proc();
328 register struct tty
*tp
= pt_tty
[minor(dev
)];
329 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
332 boolean_t funnel_state
;
334 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
337 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
339 if (pti
->pt_flags
& PF_REMOTE
) {
340 while (isbackground(p
, tp
)) {
341 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
342 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
343 p
->p_pgrp
->pg_jobc
== 0 ||
344 p
->p_flag
& P_PPWAIT
) {
348 pgsignal(p
->p_pgrp
, SIGTTIN
, 1);
349 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsbg",
354 if (tp
->t_canq
.c_cc
== 0) {
355 if (flag
& IO_NDELAY
)
356 return (EWOULDBLOCK
);
357 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
363 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
367 cc
= min(uio_resid(uio
), BUFSIZ
);
368 // Don't copy the very last byte
369 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
370 cc
= q_to_b(&tp
->t_canq
, buf
, cc
);
371 error
= uiomove(buf
, cc
, uio
);
375 if (tp
->t_canq
.c_cc
== 1)
376 (void) getc(&tp
->t_canq
);
381 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
382 ptcwakeup(tp
, FWRITE
);
384 (void) thread_funnel_set(kernel_flock
, funnel_state
);
389 * Write to pseudo-tty.
390 * Wakeups of controlling tty will happen
391 * indirectly, when tty driver calls ptsstart.
394 ptswrite(dev
, uio
, flag
)
399 register struct tty
*tp
;
401 boolean_t funnel_state
;
403 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
405 tp
= pt_tty
[minor(dev
)];
406 if (tp
->t_oproc
== 0)
409 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
411 (void) thread_funnel_set(kernel_flock
, funnel_state
);
416 * Start output on pseudo-tty.
417 * Wake up process selecting or sleeping for input from controlling tty.
423 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
424 boolean_t funnel_state
;
426 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
428 if (tp
->t_state
& TS_TTSTOP
)
430 if (pti
->pt_flags
& PF_STOPPED
) {
431 pti
->pt_flags
&= ~PF_STOPPED
;
432 pti
->pt_send
= TIOCPKT_START
;
434 ptcwakeup(tp
, FREAD
);
436 (void) thread_funnel_set(kernel_flock
, funnel_state
);
445 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
446 boolean_t funnel_state
;
448 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
451 selwakeup(&pti
->pt_selr
);
452 wakeup(TSA_PTC_READ(tp
));
455 selwakeup(&pti
->pt_selw
);
456 wakeup(TSA_PTC_WRITE(tp
));
458 (void) thread_funnel_set(kernel_flock
, funnel_state
);
462 ptcopen(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
464 register struct tty
*tp
;
465 struct pt_ioctl
*pti
;
467 boolean_t funnel_state
;
469 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
471 if (minor(dev
) >= npty
) {
475 if(!pt_tty
[minor(dev
)]) {
476 tp
= pt_tty
[minor(dev
)] = ttymalloc();
478 tp
= pt_tty
[minor(dev
)];
483 tp
->t_oproc
= ptsstart
;
485 tp
->t_stop
= ptsstop
;
487 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
488 tp
->t_lflag
&= ~EXTPROC
;
489 pti
= &pt_ioctl
[minor(dev
)];
494 (void) thread_funnel_set(kernel_flock
, funnel_state
);
499 ptcclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
501 register struct tty
*tp
;
502 boolean_t funnel_state
;
504 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
506 tp
= pt_tty
[minor(dev
)];
507 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
510 * XXX MDMBUF makes no sense for ptys but would inhibit the above
511 * l_modem(). CLOCAL makes sense but isn't supported. Special
512 * l_modem()s that ignore carrier drop make no sense for ptys but
513 * may be in use because other parts of the line discipline make
514 * sense for ptys. Recover by doing everything that a normal
515 * ttymodem() would have done except for sending a SIGHUP.
517 if (tp
->t_state
& TS_ISOPEN
) {
518 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
519 tp
->t_state
|= TS_ZOMBIE
;
520 ttyflush(tp
, FREAD
| FWRITE
);
523 tp
->t_oproc
= 0; /* mark closed */
525 (void) thread_funnel_set(kernel_flock
, funnel_state
);
530 ptcread(dev
, uio
, flag
)
535 register struct tty
*tp
= pt_tty
[minor(dev
)];
536 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
539 boolean_t funnel_state
;
541 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
544 * We want to block until the slave
545 * is open, and there's something to read;
546 * but if we lost the slave or we're NBIO,
547 * then return the appropriate error instead.
550 if (tp
->t_state
&TS_ISOPEN
) {
551 if (pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) {
552 error
= ureadc((int)pti
->pt_send
, uio
);
555 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
556 cc
= min(uio_resid(uio
),
557 sizeof(tp
->t_termios
));
558 uiomove((caddr_t
)&tp
->t_termios
, cc
,
564 if (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
) {
565 error
= ureadc((int)pti
->pt_ucntl
, uio
);
571 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
574 if ((tp
->t_state
& TS_CONNECTED
) == 0)
576 if (flag
& IO_NDELAY
) {
580 error
= tsleep(TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptcin", 0);
584 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
585 error
= ureadc(0, uio
);
586 while (uio_resid(uio
) > 0 && error
== 0) {
587 cc
= q_to_b(&tp
->t_outq
, buf
, min(uio_resid(uio
), BUFSIZ
));
590 error
= uiomove(buf
, cc
, uio
);
592 (*linesw
[tp
->t_line
].l_start
)(tp
);
595 (void) thread_funnel_set(kernel_flock
, funnel_state
);
601 register struct tty
*tp
;
604 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
606 boolean_t funnel_state
;
608 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
610 /* note: FLUSHREAD and FLUSHWRITE already ok */
612 flush
= TIOCPKT_STOP
;
613 pti
->pt_flags
|= PF_STOPPED
;
615 pti
->pt_flags
&= ~PF_STOPPED
;
616 pti
->pt_send
|= flush
;
617 /* change of perspective */
625 (void) thread_funnel_set(kernel_flock
, funnel_state
);
629 ptcselect(dev
, rw
, wql
, p
)
635 register struct tty
*tp
= pt_tty
[minor(dev
)];
636 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
638 boolean_t funnel_state
;
640 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
642 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
650 * Need to block timeouts (ttrstart).
652 if ((tp
->t_state
&TS_ISOPEN
) &&
653 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
659 case 0: /* exceptional */
660 if ((tp
->t_state
&TS_ISOPEN
) &&
661 ((pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) ||
662 (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
))) {
666 selrecord(p
, &pti
->pt_selr
, wql
);
671 if (tp
->t_state
&TS_ISOPEN
) {
672 if (pti
->pt_flags
& PF_REMOTE
) {
673 if (tp
->t_canq
.c_cc
== 0) {
678 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
-2) {
682 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_iflag
&ICANON
)) {
688 selrecord(p
, &pti
->pt_selw
, wql
);
693 (void) thread_funnel_set(kernel_flock
, funnel_state
);
698 ptcwrite(dev
, uio
, flag
)
700 register struct uio
*uio
;
703 register struct tty
*tp
= pt_tty
[minor(dev
)];
704 register u_char
*cp
= NULL
;
706 u_char locbuf
[BUFSIZ
];
708 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
710 boolean_t funnel_state
;
712 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
715 if ((tp
->t_state
&TS_ISOPEN
) == 0)
717 if (pti
->pt_flags
& PF_REMOTE
) {
720 while ((uio_resid(uio
) > 0 || cc
> 0) &&
721 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
723 cc
= min(uio_resid(uio
), BUFSIZ
);
724 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
726 error
= uiomove((caddr_t
)cp
, cc
, uio
);
729 /* check again for safety */
730 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
731 /* adjust as usual */
732 uio_setresid(uio
, (uio_resid(uio
) + cc
));
738 cc
= b_to_q((char *)cp
, cc
, &tp
->t_canq
);
740 * XXX we don't guarantee that the canq size
741 * is >= TTYHOG, so the above b_to_q() may
742 * leave some bytes uncopied. However, space
743 * is guaranteed for the null terminator if
744 * we don't fail here since (TTYHOG - 1) is
745 * not a multiple of CBSIZE.
751 /* adjust for data copied in but not written */
752 uio_setresid(uio
, (uio_resid(uio
) + cc
));
753 (void) putc(0, &tp
->t_canq
);
755 wakeup(TSA_PTS_READ(tp
));
758 while (uio_resid(uio
) > 0 || cc
> 0) {
760 cc
= min(uio_resid(uio
), BUFSIZ
);
762 error
= uiomove((caddr_t
)cp
, cc
, uio
);
765 /* check again for safety */
766 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
767 /* adjust for data copied in but not written */
768 uio_setresid(uio
, (uio_resid(uio
) + cc
));
774 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
775 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_iflag
&ICANON
))) {
776 wakeup(TSA_HUP_OR_INPUT(tp
));
779 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
786 (void) thread_funnel_set(kernel_flock
, funnel_state
);
790 * Come here to wait for slave to open, for space
791 * in outq, or space in rawq, or an empty canq.
793 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
794 /* adjust for data copied in but not written */
795 uio_setresid(uio
, (uio_resid(uio
) + cc
));
799 if (flag
& IO_NDELAY
) {
800 /* adjust for data copied in but not written */
801 uio_setresid(uio
, (uio_resid(uio
) + cc
));
806 error
= tsleep(TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptcout", 0);
808 /* adjust for data copied in but not written */
809 uio_setresid(uio
, (uio_resid(uio
) + cc
));
816 /* XXX we eventually want to go to this model,
817 * but premier can't change the cdevsw */
822 if (minor(dev
) >= npty
)
825 return &pt_tty
[minor(dev
)];
832 ptyioctl(dev
, cmd
, data
, flag
)
838 ptyioctl(dev
, cmd
, data
, flag
, p
)
846 register struct tty
*tp
= pt_tty
[minor(dev
)];
847 register struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
848 register u_char
*cc
= tp
->t_cc
;
850 boolean_t funnel_state
;
852 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
855 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
856 * ttywflush(tp) will hang if there are characters in the outq.
858 if (cmd
== TIOCEXT
) {
860 * When the EXTPROC bit is being toggled, we need
861 * to send an TIOCPKT_IOCTL if the packet driver
865 if (pti
->pt_flags
& PF_PKT
) {
866 pti
->pt_send
|= TIOCPKT_IOCTL
;
867 ptcwakeup(tp
, FREAD
);
869 tp
->t_lflag
|= EXTPROC
;
871 if ((tp
->t_lflag
& EXTPROC
) &&
872 (pti
->pt_flags
& PF_PKT
)) {
873 pti
->pt_send
|= TIOCPKT_IOCTL
;
874 ptcwakeup(tp
, FREAD
);
876 tp
->t_lflag
&= ~EXTPROC
;
881 if (cdevsw
[major(dev
)]->d_open
== ptcopen
)
883 if (cdevsw
[major(dev
)].d_open
== ptcopen
)
889 * We aviod calling ttioctl on the controller since,
890 * in that case, tp must be the controlling terminal.
892 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
897 if (pti
->pt_flags
& PF_UCNTL
) {
901 pti
->pt_flags
|= PF_PKT
;
903 pti
->pt_flags
&= ~PF_PKT
;
908 if (pti
->pt_flags
& PF_PKT
) {
912 pti
->pt_flags
|= PF_UCNTL
;
914 pti
->pt_flags
&= ~PF_UCNTL
;
919 pti
->pt_flags
|= PF_REMOTE
;
921 pti
->pt_flags
&= ~PF_REMOTE
;
922 ttyflush(tp
, FREAD
|FWRITE
);
933 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
937 if (*(unsigned int *)data
>= NSIG
||
938 *(unsigned int *)data
== 0) {
942 if ((tp
->t_lflag
&NOFLSH
) == 0)
943 ttyflush(tp
, FREAD
|FWRITE
);
944 pgsignal(tp
->t_pgrp
, *(unsigned int *)data
, 1);
945 if ((*(unsigned int *)data
== SIGINFO
) &&
946 ((tp
->t_lflag
&NOKERNINFO
) == 0))
950 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
951 if (error
== ENOTTY
) {
952 error
= ttioctl(tp
, cmd
, data
, flag
, p
);
954 && pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
955 /* Process the UIOCMD ioctl group */
957 pti
->pt_ucntl
= (u_char
)cmd
;
958 ptcwakeup(tp
, FREAD
);
966 * If external processing and packet mode send ioctl packet.
968 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
977 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
984 pti
->pt_send
|= TIOCPKT_IOCTL
;
985 ptcwakeup(tp
, FREAD
);
990 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
991 && CCEQ(cc
[VSTART
], CTRL('q'));
992 if (pti
->pt_flags
& PF_NOSTOP
) {
994 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
995 pti
->pt_send
|= TIOCPKT_DOSTOP
;
996 pti
->pt_flags
&= ~PF_NOSTOP
;
997 ptcwakeup(tp
, FREAD
);
1001 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1002 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1003 pti
->pt_flags
|= PF_NOSTOP
;
1004 ptcwakeup(tp
, FREAD
);
1008 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1013 static ptc_devsw_installed
= 0;
1016 ptc_drvinit(void *unused
)
1023 if( ! ptc_devsw_installed
) {
1024 dev
= makedev(CDEV_MAJOR_S
, 0);
1025 cdevsw_add(&dev
, &pts_cdevsw
, NULL
);
1026 dev
= makedev(CDEV_MAJOR_C
, 0);
1027 cdevsw_add(&dev
, &ptc_cdevsw
, NULL
);
1028 ptc_devsw_installed
= 1;
1030 for ( i
= 0 ; i
<NPTY
; i
++ ) {
1033 devfs_token_pts
[i
] =
1034 devfs_add_devswf(&pts_cdevsw
,i
,
1036 "tty%c%n",jnames
[j
],k
);
1037 devfs_token_ptc
[i
] =
1038 devfs_add_devswf(&ptc_cdevsw
,i
,
1040 "pty%c%n",jnames
[j
],k
);
1046 SYSINIT(ptcdev
,SI_SUB_DRIVERS
,SI_ORDER_MIDDLE
+CDEV_MAJOR_C
,ptc_drvinit
,NULL
)