2 * Copyright (c) 1997-2006 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@
29 * Copyright (c) 1982, 1986, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
64 * Pseudo-teletype Driver
65 * (Actually two drivers, requiring two entries in 'cdevsw')
67 #include "pty.h" /* XXX */
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/ioctl.h>
72 #include <sys/proc_internal.h>
73 #include <sys/kauth.h>
76 #include <sys/file_internal.h>
78 #include <sys/kernel.h>
79 #include <sys/vnode.h>
81 #include <sys/signalvar.h>
83 #define d_devtotty_t struct tty **
88 typedef void d_stop_t(struct tty
*tp
, int rw
);
90 /* XXX function should be removed??? */
91 int pty_init(int n_ptys
);
93 /* XXX should be a devfs function */
94 int _devfs_setattr(void * handle
, unsigned short mode
, uid_t uid
, gid_t gid
);
96 static void ptsstart(struct tty
*tp
);
97 static void ptcwakeup(struct tty
*tp
, int flag
);
99 __private_extern__ d_open_t ptsopen
;
100 __private_extern__ d_close_t ptsclose
;
101 __private_extern__ d_read_t ptsread
;
102 __private_extern__ d_write_t ptswrite
;
103 __private_extern__ d_ioctl_t ptyioctl
;
104 __private_extern__ d_stop_t ptsstop
;
105 __private_extern__ d_devtotty_t ptydevtotty
;
106 __private_extern__ d_open_t ptcopen
;
107 __private_extern__ d_close_t ptcclose
;
108 __private_extern__ d_read_t ptcread
;
109 __private_extern__ d_write_t ptcwrite
;
110 __private_extern__ d_select_t ptcselect
;
114 #define NPTY 32 /* crude XXX */
115 #warning You have only one pty defined, redefining to 32.
118 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
121 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
122 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
124 /* All references to have been changed to indirections in the file */
125 __private_extern__
struct tty
*pt_tty
[NPTY
] = { NULL
};
127 static struct pt_ioctl
{
129 struct selinfo pt_selr
, pt_selw
;
132 void *pt_devhandle
; /* slave device handle for grantpt() */
133 } pt_ioctl
[NPTY
]; /* XXX */
134 static int npty
= NPTY
; /* for pstat -t */
136 #define PF_PKT 0x08 /* packet mode */
137 #define PF_STOPPED 0x10 /* user told stopped */
138 #define PF_REMOTE 0x20 /* remote and flow controlled input */
139 #define PF_NOSTOP 0x40
140 #define PF_UCNTL 0x80 /* user control mode */
144 pty_init(__unused
int n_ptys
)
149 #include <miscfs/devfs/devfs.h>
150 #define START_CHAR 'p'
158 /* create the pseudo tty device nodes */
159 for (j
= 0; j
< 10; j
++) {
160 for (i
= 0; i
< HEX_BASE
; i
++) {
161 int m
= j
* HEX_BASE
+ i
;
164 pt_ioctl
[m
].pt_devhandle
= devfs_make_node(makedev(4, m
),
165 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
166 "tty%c%x", j
+ START_CHAR
, i
);
167 (void)devfs_make_node(makedev(5, m
),
168 DEVFS_CHAR
, UID_ROOT
, GID_WHEEL
, 0666,
169 "pty%c%x", j
+ START_CHAR
, i
);
177 __private_extern__
int
178 ptsopen(dev_t dev
, int flag
, __unused
int devtype
, __unused
struct proc
*p
)
182 boolean_t funnel_state
;
184 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
186 * You will see this sort of code coming up in diffs later both
187 * the ttymalloc and the tp indirection.
189 if (minor(dev
) >= npty
) {
193 if (!pt_tty
[minor(dev
)]) {
195 * If we can't allocate a new one, act as if we had run out
198 if ((tp
= pt_tty
[minor(dev
)] = ttymalloc()) == NULL
) {
203 tp
= pt_tty
[minor(dev
)];
204 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
205 ttychars(tp
); /* Set up default chars */
206 tp
->t_iflag
= TTYDEF_IFLAG
;
207 tp
->t_oflag
= TTYDEF_OFLAG
;
208 tp
->t_lflag
= TTYDEF_LFLAG
;
209 tp
->t_cflag
= TTYDEF_CFLAG
;
210 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
211 ttsetwater(tp
); /* would be done in xxparam() */
212 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
216 if (tp
->t_oproc
) /* Ctrlr still around. */
217 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
218 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
221 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
226 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
228 ptcwakeup(tp
, FREAD
|FWRITE
);
230 (void) thread_funnel_set(kernel_flock
, funnel_state
);
234 __private_extern__
int
235 ptsclose(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
239 boolean_t funnel_state
;
241 * This is temporary until the VSX conformance tests
242 * are fixed. They are hanging with a deadlock
243 * where close(pts) will not complete without t_timeout set
245 #define FIX_VSX_HANG 1
249 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
251 tp
= pt_tty
[minor(dev
)];
253 save_timeout
= tp
->t_timeout
;
256 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
257 ptsstop(tp
, FREAD
|FWRITE
);
260 tp
->t_timeout
= save_timeout
;
262 (void) thread_funnel_set(kernel_flock
, funnel_state
);
266 __private_extern__
int
267 ptsread(dev_t dev
, struct uio
*uio
, int flag
)
269 struct proc
*p
= current_proc();
270 struct tty
*tp
= pt_tty
[minor(dev
)];
271 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
274 boolean_t funnel_state
;
277 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
280 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
282 if (pti
->pt_flags
& PF_REMOTE
) {
283 while (isbackground(p
, tp
)) {
284 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
285 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
286 p
->p_lflag
& P_LPPWAIT
) {
293 if (pg
== PGRP_NULL
) {
297 if (pg
->pg_jobc
== 0) {
302 pgsignal(pg
, SIGTTIN
, 1);
305 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsbg",
310 if (tp
->t_canq
.c_cc
== 0) {
311 if (flag
& IO_NDELAY
)
312 return (EWOULDBLOCK
);
313 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
319 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
323 cc
= min(uio_resid(uio
), BUFSIZ
);
324 // Don't copy the very last byte
325 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
326 cc
= q_to_b(&tp
->t_canq
, (u_char
*)buf
, cc
);
327 error
= uiomove(buf
, cc
, uio
);
331 if (tp
->t_canq
.c_cc
== 1)
332 (void) getc(&tp
->t_canq
);
337 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
338 ptcwakeup(tp
, FWRITE
);
340 (void) thread_funnel_set(kernel_flock
, funnel_state
);
345 * Write to pseudo-tty.
346 * Wakeups of controlling tty will happen
347 * indirectly, when tty driver calls ptsstart.
349 __private_extern__
int
350 ptswrite(dev_t dev
, struct uio
*uio
, int flag
)
354 boolean_t funnel_state
;
356 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
358 tp
= pt_tty
[minor(dev
)];
359 if (tp
->t_oproc
== 0)
362 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
364 (void) thread_funnel_set(kernel_flock
, funnel_state
);
369 * Start output on pseudo-tty.
370 * Wake up process selecting or sleeping for input from controlling tty.
373 ptsstart(struct tty
*tp
)
375 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
376 boolean_t funnel_state
;
378 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
380 if (tp
->t_state
& TS_TTSTOP
)
382 if (pti
->pt_flags
& PF_STOPPED
) {
383 pti
->pt_flags
&= ~PF_STOPPED
;
384 pti
->pt_send
= TIOCPKT_START
;
386 ptcwakeup(tp
, FREAD
);
388 (void) thread_funnel_set(kernel_flock
, funnel_state
);
393 ptcwakeup(struct tty
*tp
, int flag
)
395 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
396 boolean_t funnel_state
;
398 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
401 selwakeup(&pti
->pt_selr
);
402 wakeup(TSA_PTC_READ(tp
));
405 selwakeup(&pti
->pt_selw
);
406 wakeup(TSA_PTC_WRITE(tp
));
408 (void) thread_funnel_set(kernel_flock
, funnel_state
);
411 __private_extern__
int
412 ptcopen(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
415 struct pt_ioctl
*pti
;
417 boolean_t funnel_state
;
419 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
421 if (minor(dev
) >= npty
) {
425 if(!pt_tty
[minor(dev
)]) {
426 tp
= pt_tty
[minor(dev
)] = ttymalloc();
428 tp
= pt_tty
[minor(dev
)];
429 /* If master is open OR slave is still draining, pty is still busy */
430 if (tp
->t_oproc
|| (tp
->t_state
& TS_ISOPEN
)) {
434 tp
->t_oproc
= ptsstart
;
435 CLR(tp
->t_state
, TS_ZOMBIE
);
437 tp
->t_stop
= ptsstop
;
439 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
440 tp
->t_lflag
&= ~EXTPROC
;
441 pti
= &pt_ioctl
[minor(dev
)];
446 (void) thread_funnel_set(kernel_flock
, funnel_state
);
450 __private_extern__
int
451 ptcclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
454 boolean_t funnel_state
;
456 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
458 tp
= pt_tty
[minor(dev
)];
459 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
462 * XXX MDMBUF makes no sense for ptys but would inhibit the above
463 * l_modem(). CLOCAL makes sense but isn't supported. Special
464 * l_modem()s that ignore carrier drop make no sense for ptys but
465 * may be in use because other parts of the line discipline make
466 * sense for ptys. Recover by doing everything that a normal
467 * ttymodem() would have done except for sending a SIGHUP.
469 if (tp
->t_state
& TS_ISOPEN
) {
470 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
471 tp
->t_state
|= TS_ZOMBIE
;
472 ttyflush(tp
, FREAD
| FWRITE
);
475 tp
->t_oproc
= 0; /* mark closed */
477 (void) thread_funnel_set(kernel_flock
, funnel_state
);
481 __private_extern__
int
482 ptcread(dev_t dev
, struct uio
*uio
, int flag
)
484 struct tty
*tp
= pt_tty
[minor(dev
)];
485 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
488 boolean_t funnel_state
;
490 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
493 * We want to block until the slave
494 * is open, and there's something to read;
495 * but if we lost the slave or we're NBIO,
496 * then return the appropriate error instead.
499 if (tp
->t_state
&TS_ISOPEN
) {
500 if (pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) {
501 error
= ureadc((int)pti
->pt_send
, uio
);
504 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
505 cc
= min(uio_resid(uio
),
506 sizeof(tp
->t_termios
));
507 uiomove((caddr_t
)&tp
->t_termios
, cc
,
513 if (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
) {
514 error
= ureadc((int)pti
->pt_ucntl
, uio
);
520 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
523 if ((tp
->t_state
& TS_CONNECTED
) == 0)
525 if (flag
& IO_NDELAY
) {
529 error
= tsleep(TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptcin", 0);
533 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
534 error
= ureadc(0, uio
);
535 while (uio_resid(uio
) > 0 && error
== 0) {
536 cc
= q_to_b(&tp
->t_outq
, (u_char
*)buf
, min(uio_resid(uio
), BUFSIZ
));
539 error
= uiomove(buf
, cc
, uio
);
541 (*linesw
[tp
->t_line
].l_start
)(tp
);
544 (void) thread_funnel_set(kernel_flock
, funnel_state
);
548 __private_extern__
void
549 ptsstop(struct tty
*tp
, int flush
)
551 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
553 boolean_t funnel_state
;
555 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
557 /* note: FLUSHREAD and FLUSHWRITE already ok */
559 flush
= TIOCPKT_STOP
;
560 pti
->pt_flags
|= PF_STOPPED
;
562 pti
->pt_flags
&= ~PF_STOPPED
;
563 pti
->pt_send
|= flush
;
564 /* change of perspective */
572 (void) thread_funnel_set(kernel_flock
, funnel_state
);
575 __private_extern__
int
576 ptcselect(dev_t dev
, int rw
, void *wql
, struct proc
*p
)
578 struct tty
*tp
= pt_tty
[minor(dev
)];
579 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
581 boolean_t funnel_state
;
583 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
585 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
593 * Need to block timeouts (ttrstart).
595 if ((tp
->t_state
&TS_ISOPEN
) &&
596 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
602 case 0: /* exceptional */
603 if ((tp
->t_state
&TS_ISOPEN
) &&
604 ((pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) ||
605 (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
))) {
609 selrecord(p
, &pti
->pt_selr
, wql
);
614 if (tp
->t_state
&TS_ISOPEN
) {
615 if (pti
->pt_flags
& PF_REMOTE
) {
616 if (tp
->t_canq
.c_cc
== 0) {
621 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
-2) {
625 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
&ICANON
)) {
631 selrecord(p
, &pti
->pt_selw
, wql
);
636 (void) thread_funnel_set(kernel_flock
, funnel_state
);
640 __private_extern__
int
641 ptcwrite(dev_t dev
, struct uio
*uio
, int flag
)
643 struct tty
*tp
= pt_tty
[minor(dev
)];
646 u_char locbuf
[BUFSIZ
];
648 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
650 boolean_t funnel_state
;
652 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
655 if ((tp
->t_state
&TS_ISOPEN
) == 0)
657 if (pti
->pt_flags
& PF_REMOTE
) {
660 while ((uio_resid(uio
) > 0 || cc
> 0) &&
661 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
663 cc
= min(uio_resid(uio
), BUFSIZ
);
664 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
666 error
= uiomove((caddr_t
)cp
, cc
, uio
);
669 /* check again for safety */
670 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
671 /* adjust as usual */
672 uio_setresid(uio
, (uio_resid(uio
) + cc
));
678 cc
= b_to_q((u_char
*)cp
, cc
, &tp
->t_canq
);
680 * XXX we don't guarantee that the canq size
681 * is >= TTYHOG, so the above b_to_q() may
682 * leave some bytes uncopied. However, space
683 * is guaranteed for the null terminator if
684 * we don't fail here since (TTYHOG - 1) is
685 * not a multiple of CBSIZE.
691 /* adjust for data copied in but not written */
692 uio_setresid(uio
, (uio_resid(uio
) + cc
));
693 (void) putc(0, &tp
->t_canq
);
695 wakeup(TSA_PTS_READ(tp
));
698 while (uio_resid(uio
) > 0 || cc
> 0) {
700 cc
= min(uio_resid(uio
), BUFSIZ
);
702 error
= uiomove((caddr_t
)cp
, cc
, uio
);
705 /* check again for safety */
706 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
707 /* adjust for data copied in but not written */
708 uio_setresid(uio
, (uio_resid(uio
) + cc
));
714 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
715 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
716 wakeup(TSA_HUP_OR_INPUT(tp
));
719 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
726 (void) thread_funnel_set(kernel_flock
, funnel_state
);
730 * Come here to wait for slave to open, for space
731 * in outq, or space in rawq, or an empty canq.
733 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
734 /* adjust for data copied in but not written */
735 uio_setresid(uio
, (uio_resid(uio
) + cc
));
739 if (flag
& IO_NDELAY
) {
740 /* adjust for data copied in but not written */
741 uio_setresid(uio
, (uio_resid(uio
) + cc
));
746 error
= tsleep(TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptcout", 0);
748 /* adjust for data copied in but not written */
749 uio_setresid(uio
, (uio_resid(uio
) + cc
));
755 __private_extern__
int
756 ptyioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
)
758 struct tty
*tp
= pt_tty
[minor(dev
)];
759 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
760 u_char
*cc
= tp
->t_cc
;
762 boolean_t funnel_state
;
764 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
767 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
768 * ttywflush(tp) will hang if there are characters in the outq.
770 if (cmd
== TIOCEXT
) {
772 * When the EXTPROC bit is being toggled, we need
773 * to send an TIOCPKT_IOCTL if the packet driver
777 if (pti
->pt_flags
& PF_PKT
) {
778 pti
->pt_send
|= TIOCPKT_IOCTL
;
779 ptcwakeup(tp
, FREAD
);
781 tp
->t_lflag
|= EXTPROC
;
783 if ((tp
->t_lflag
& EXTPROC
) &&
784 (pti
->pt_flags
& PF_PKT
)) {
785 pti
->pt_send
|= TIOCPKT_IOCTL
;
786 ptcwakeup(tp
, FREAD
);
788 tp
->t_lflag
&= ~EXTPROC
;
792 if (cdevsw
[major(dev
)].d_open
== ptcopen
)
797 * We aviod calling ttioctl on the controller since,
798 * in that case, tp must be the controlling terminal.
800 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
805 if (pti
->pt_flags
& PF_UCNTL
) {
809 pti
->pt_flags
|= PF_PKT
;
811 pti
->pt_flags
&= ~PF_PKT
;
816 if (pti
->pt_flags
& PF_PKT
) {
820 pti
->pt_flags
|= PF_UCNTL
;
822 pti
->pt_flags
&= ~PF_UCNTL
;
827 pti
->pt_flags
|= PF_REMOTE
;
829 pti
->pt_flags
&= ~PF_REMOTE
;
830 ttyflush(tp
, FREAD
|FWRITE
);
841 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
845 if (*(unsigned int *)data
>= NSIG
||
846 *(unsigned int *)data
== 0) {
850 if ((tp
->t_lflag
&NOFLSH
) == 0)
851 ttyflush(tp
, FREAD
|FWRITE
);
852 tty_pgsignal(tp
, *(unsigned int *)data
, 1);
853 if ((*(unsigned int *)data
== SIGINFO
) &&
854 ((tp
->t_lflag
&NOKERNINFO
) == 0))
858 case TIOCPTYGRANT
: /* grantpt(3) */
860 * Change the uid of the slave to that of the calling
861 * thread, change the gid of the slave to GID_TTY,
862 * change the mode to 0620 (rw--w----).
865 _devfs_setattr(pti
->pt_devhandle
, 0620, kauth_getuid(), GID_TTY
);
869 case TIOCPTYGNAME
: /* ptsname(3) */
871 * Report the name of the slave device in *data
872 * (128 bytes max.). Use the same derivation method
873 * used for calling devfs_make_node() to create it.
875 snprintf(data
, 128, "/dev/tty%c%x",
876 START_CHAR
+ (minor(dev
) / HEX_BASE
),
877 minor(dev
) % HEX_BASE
);
881 case TIOCPTYUNLK
: /* unlockpt(3) */
883 * Unlock the slave device so that it can be opened.
888 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
889 if (error
== ENOTTY
) {
890 error
= ttioctl(tp
, cmd
, data
, flag
, p
);
891 if (error
== ENOTTY
) {
892 if (pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
893 /* Process the UIOCMD ioctl group */
895 pti
->pt_ucntl
= (u_char
)cmd
;
896 ptcwakeup(tp
, FREAD
);
900 } else if (cmd
== TIOCSBRK
|| cmd
== TIOCCBRK
) {
902 * POSIX conformance; rdar://3936338
904 * Clear ENOTTY in the case of setting or
905 * clearing a break failing because pty's
906 * don't support break like real serial
916 * If external processing and packet mode send ioctl packet.
918 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
927 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
934 pti
->pt_send
|= TIOCPKT_IOCTL
;
935 ptcwakeup(tp
, FREAD
);
940 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
941 && CCEQ(cc
[VSTART
], CTRL('q'));
942 if (pti
->pt_flags
& PF_NOSTOP
) {
944 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
945 pti
->pt_send
|= TIOCPKT_DOSTOP
;
946 pti
->pt_flags
&= ~PF_NOSTOP
;
947 ptcwakeup(tp
, FREAD
);
951 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
952 pti
->pt_send
|= TIOCPKT_NOSTOP
;
953 pti
->pt_flags
|= PF_NOSTOP
;
954 ptcwakeup(tp
, FREAD
);
958 (void) thread_funnel_set(kernel_flock
, funnel_state
);