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>
77 #include <sys/uio_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 __XNU_PRIVATE_EXTERN d_open_t ptsopen
;
100 __XNU_PRIVATE_EXTERN d_close_t ptsclose
;
101 __XNU_PRIVATE_EXTERN d_read_t ptsread
;
102 __XNU_PRIVATE_EXTERN d_write_t ptswrite
;
103 __XNU_PRIVATE_EXTERN d_ioctl_t ptyioctl
;
104 __XNU_PRIVATE_EXTERN d_stop_t ptsstop
;
105 __XNU_PRIVATE_EXTERN d_devtotty_t ptydevtotty
;
106 __XNU_PRIVATE_EXTERN d_open_t ptcopen
;
107 __XNU_PRIVATE_EXTERN d_close_t ptcclose
;
108 __XNU_PRIVATE_EXTERN d_read_t ptcread
;
109 __XNU_PRIVATE_EXTERN d_write_t ptcwrite
;
110 __XNU_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
)
184 * You will see this sort of code coming up in diffs later both
185 * the ttymalloc and the tp indirection.
187 if (minor(dev
) >= npty
) {
191 if (!pt_tty
[minor(dev
)]) {
193 * If we can't allocate a new one, act as if we had run out
196 if ((tp
= pt_tty
[minor(dev
)] = ttymalloc()) == NULL
) {
201 tp
= pt_tty
[minor(dev
)];
205 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
206 termioschars(&tp
->t_termios
); /* Set up default chars */
207 tp
->t_iflag
= TTYDEF_IFLAG
;
208 tp
->t_oflag
= TTYDEF_OFLAG
;
209 tp
->t_lflag
= TTYDEF_LFLAG
;
210 tp
->t_cflag
= TTYDEF_CFLAG
;
211 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
212 ttsetwater(tp
); /* would be done in xxparam() */
213 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
217 if (tp
->t_oproc
) /* Ctrlr still around. */
218 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
219 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
222 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
227 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
229 ptcwakeup(tp
, FREAD
|FWRITE
);
237 __private_extern__
int
238 ptsclose(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
244 * This is temporary until the VSX conformance tests
245 * are fixed. They are hanging with a deadlock
246 * where close(pts) will not complete without t_timeout set
248 #define FIX_VSX_HANG 1
253 tp
= pt_tty
[minor(dev
)];
256 save_timeout
= tp
->t_timeout
;
259 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
260 ptsstop(tp
, FREAD
|FWRITE
);
263 tp
->t_timeout
= save_timeout
;
269 __private_extern__
int
270 ptsread(dev_t dev
, struct uio
*uio
, int flag
)
272 struct proc
*p
= current_proc();
273 struct tty
*tp
= pt_tty
[minor(dev
)];
274 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
281 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
283 if (pti
->pt_flags
& PF_REMOTE
) {
284 while (isbackground(p
, tp
)) {
285 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
286 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
287 p
->p_lflag
& P_LPPWAIT
) {
294 if (pg
== PGRP_NULL
) {
299 * SAFE: We about to drop the lock ourselves by
300 * SAFE: erroring out or sleeping anyway.
303 if (pg
->pg_jobc
== 0) {
309 pgsignal(pg
, SIGTTIN
, 1);
313 error
= ttysleep(tp
, &ptsread
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsbg",
318 if (tp
->t_canq
.c_cc
== 0) {
319 if (flag
& IO_NDELAY
) {
323 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
329 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
333 cc
= min(uio_resid(uio
), BUFSIZ
);
334 // Don't copy the very last byte
335 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
336 cc
= q_to_b(&tp
->t_canq
, (u_char
*)buf
, cc
);
337 error
= uiomove(buf
, cc
, uio
);
341 if (tp
->t_canq
.c_cc
== 1)
342 (void) getc(&tp
->t_canq
);
347 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
348 ptcwakeup(tp
, FWRITE
);
355 * Write to pseudo-tty.
356 * Wakeups of controlling tty will happen
357 * indirectly, when tty driver calls ptsstart.
359 __private_extern__
int
360 ptswrite(dev_t dev
, struct uio
*uio
, int flag
)
365 tp
= pt_tty
[minor(dev
)];
369 if (tp
->t_oproc
== 0)
372 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
380 * Start output on pseudo-tty.
381 * Wake up process selecting or sleeping for input from controlling tty.
383 * t_oproc for this driver; called from within the line discipline
385 * Locks: Assumes tp is locked on entry, remains locked on exit
388 ptsstart(struct tty
*tp
)
390 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
392 if (tp
->t_state
& TS_TTSTOP
)
394 if (pti
->pt_flags
& PF_STOPPED
) {
395 pti
->pt_flags
&= ~PF_STOPPED
;
396 pti
->pt_send
= TIOCPKT_START
;
398 ptcwakeup(tp
, FREAD
);
404 * Locks: Assumes tty_lock() is held over this call.
407 ptcwakeup(struct tty
*tp
, int flag
)
409 struct pt_ioctl
*pti
= &pt_ioctl
[minor(tp
->t_dev
)];
412 selwakeup(&pti
->pt_selr
);
413 wakeup(TSA_PTC_READ(tp
));
416 selwakeup(&pti
->pt_selw
);
417 wakeup(TSA_PTC_WRITE(tp
));
421 __private_extern__
int
422 ptcopen(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
425 struct pt_ioctl
*pti
;
428 if (minor(dev
) >= npty
) {
432 if(!pt_tty
[minor(dev
)]) {
433 tp
= pt_tty
[minor(dev
)] = ttymalloc();
435 tp
= pt_tty
[minor(dev
)];
439 /* If master is open OR slave is still draining, pty is still busy */
440 if (tp
->t_oproc
|| (tp
->t_state
& TS_ISOPEN
)) {
443 tp
->t_oproc
= ptsstart
;
444 CLR(tp
->t_state
, TS_ZOMBIE
);
446 tp
->t_stop
= ptsstop
;
448 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
449 tp
->t_lflag
&= ~EXTPROC
;
450 pti
= &pt_ioctl
[minor(dev
)];
462 __private_extern__
int
463 ptcclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
465 struct tty
*tp
= pt_tty
[minor(dev
)];
469 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
472 * XXX MDMBUF makes no sense for ptys but would inhibit the above
473 * l_modem(). CLOCAL makes sense but isn't supported. Special
474 * l_modem()s that ignore carrier drop make no sense for ptys but
475 * may be in use because other parts of the line discipline make
476 * sense for ptys. Recover by doing everything that a normal
477 * ttymodem() would have done except for sending a SIGHUP.
479 if (tp
->t_state
& TS_ISOPEN
) {
480 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
481 tp
->t_state
|= TS_ZOMBIE
;
482 ttyflush(tp
, FREAD
| FWRITE
);
485 tp
->t_oproc
= 0; /* mark closed */
492 __private_extern__
int
493 ptcread(dev_t dev
, struct uio
*uio
, int flag
)
495 struct tty
*tp
= pt_tty
[minor(dev
)];
496 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
503 * We want to block until the slave
504 * is open, and there's something to read;
505 * but if we lost the slave or we're NBIO,
506 * then return the appropriate error instead.
509 if (tp
->t_state
&TS_ISOPEN
) {
510 if (pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) {
511 error
= ureadc((int)pti
->pt_send
, uio
);
514 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
515 cc
= min(uio_resid(uio
),
516 sizeof(tp
->t_termios
));
517 uiomove((caddr_t
)&tp
->t_termios
, cc
,
523 if (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
) {
524 error
= ureadc((int)pti
->pt_ucntl
, uio
);
530 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
533 if ((tp
->t_state
& TS_CONNECTED
) == 0)
535 if (flag
& IO_NDELAY
) {
539 error
= ttysleep(tp
, TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptcin", 0);
543 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
544 error
= ureadc(0, uio
);
545 while (uio_resid(uio
) > 0 && error
== 0) {
546 cc
= q_to_b(&tp
->t_outq
, (u_char
*)buf
, min(uio_resid(uio
), BUFSIZ
));
549 error
= uiomove(buf
, cc
, uio
);
551 (*linesw
[tp
->t_line
].l_start
)(tp
);
560 * Line discipline callback
562 * Locks: tty_lock() is assumed held on entry and exit.
564 __private_extern__
void
565 ptsstop(struct tty
*tp
, int flush
)
567 struct pt_ioctl
*pti
;
570 pti
= &pt_ioctl
[minor(tp
->t_dev
)];
572 /* note: FLUSHREAD and FLUSHWRITE already ok */
574 flush
= TIOCPKT_STOP
;
575 pti
->pt_flags
|= PF_STOPPED
;
577 pti
->pt_flags
&= ~PF_STOPPED
;
578 pti
->pt_send
|= flush
;
579 /* change of perspective */
588 __private_extern__
int
589 ptcselect(dev_t dev
, int rw
, void *wql
, struct proc
*p
)
591 struct tty
*tp
= pt_tty
[minor(dev
)];
592 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
597 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
605 * Need to block timeouts (ttrstart).
607 if ((tp
->t_state
&TS_ISOPEN
) &&
608 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
614 case 0: /* exceptional */
615 if ((tp
->t_state
&TS_ISOPEN
) &&
616 ((pti
->pt_flags
&PF_PKT
&& pti
->pt_send
) ||
617 (pti
->pt_flags
&PF_UCNTL
&& pti
->pt_ucntl
))) {
621 selrecord(p
, &pti
->pt_selr
, wql
);
626 if (tp
->t_state
&TS_ISOPEN
) {
627 if (pti
->pt_flags
& PF_REMOTE
) {
628 if (tp
->t_canq
.c_cc
== 0) {
633 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
-2) {
637 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
&ICANON
)) {
643 selrecord(p
, &pti
->pt_selw
, wql
);
653 __private_extern__
int
654 ptcwrite(dev_t dev
, struct uio
*uio
, int flag
)
656 struct tty
*tp
= pt_tty
[minor(dev
)];
659 u_char locbuf
[BUFSIZ
];
661 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
667 if ((tp
->t_state
&TS_ISOPEN
) == 0)
669 if (pti
->pt_flags
& PF_REMOTE
) {
672 while ((uio_resid(uio
) > 0 || cc
> 0) &&
673 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
675 cc
= min(uio_resid(uio
), BUFSIZ
);
676 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
678 error
= uiomove((caddr_t
)cp
, cc
, uio
);
681 /* check again for safety */
682 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
683 /* adjust as usual */
684 uio_setresid(uio
, (uio_resid(uio
) + cc
));
690 cc
= b_to_q((u_char
*)cp
, cc
, &tp
->t_canq
);
692 * XXX we don't guarantee that the canq size
693 * is >= TTYHOG, so the above b_to_q() may
694 * leave some bytes uncopied. However, space
695 * is guaranteed for the null terminator if
696 * we don't fail here since (TTYHOG - 1) is
697 * not a multiple of CBSIZE.
703 /* adjust for data copied in but not written */
704 uio_setresid(uio
, (uio_resid(uio
) + cc
));
705 (void) putc(0, &tp
->t_canq
);
707 wakeup(TSA_PTS_READ(tp
));
710 while (uio_resid(uio
) > 0 || cc
> 0) {
712 cc
= min(uio_resid(uio
), BUFSIZ
);
714 error
= uiomove((caddr_t
)cp
, cc
, uio
);
717 /* check again for safety */
718 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
719 /* adjust for data copied in but not written */
720 uio_setresid(uio
, (uio_resid(uio
) + cc
));
726 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
727 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
728 wakeup(TSA_HUP_OR_INPUT(tp
));
731 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
744 * Come here to wait for slave to open, for space
745 * in outq, or space in rawq, or an empty canq.
747 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
748 /* adjust for data copied in but not written */
749 uio_setresid(uio
, (uio_resid(uio
) + cc
));
753 if (flag
& IO_NDELAY
) {
754 /* adjust for data copied in but not written */
755 uio_setresid(uio
, (uio_resid(uio
) + cc
));
760 error
= ttysleep(tp
, TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptcout", 0);
762 /* adjust for data copied in but not written */
763 uio_setresid(uio
, (uio_resid(uio
) + cc
));
769 __private_extern__
int
770 ptyioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
)
772 struct tty
*tp
= pt_tty
[minor(dev
)];
773 struct pt_ioctl
*pti
= &pt_ioctl
[minor(dev
)];
774 u_char
*cc
= tp
->t_cc
;
780 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
781 * ttywflush(tp) will hang if there are characters in the outq.
783 if (cmd
== TIOCEXT
) {
785 * When the EXTPROC bit is being toggled, we need
786 * to send an TIOCPKT_IOCTL if the packet driver
790 if (pti
->pt_flags
& PF_PKT
) {
791 pti
->pt_send
|= TIOCPKT_IOCTL
;
792 ptcwakeup(tp
, FREAD
);
794 tp
->t_lflag
|= EXTPROC
;
796 if ((tp
->t_lflag
& EXTPROC
) &&
797 (pti
->pt_flags
& PF_PKT
)) {
798 pti
->pt_send
|= TIOCPKT_IOCTL
;
799 ptcwakeup(tp
, FREAD
);
801 tp
->t_lflag
&= ~EXTPROC
;
805 if (cdevsw
[major(dev
)].d_open
== ptcopen
)
810 * We aviod calling ttioctl on the controller since,
811 * in that case, tp must be the controlling terminal.
813 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
818 if (pti
->pt_flags
& PF_UCNTL
) {
822 pti
->pt_flags
|= PF_PKT
;
824 pti
->pt_flags
&= ~PF_PKT
;
829 if (pti
->pt_flags
& PF_PKT
) {
833 pti
->pt_flags
|= PF_UCNTL
;
835 pti
->pt_flags
&= ~PF_UCNTL
;
840 pti
->pt_flags
|= PF_REMOTE
;
842 pti
->pt_flags
&= ~PF_REMOTE
;
843 ttyflush(tp
, FREAD
|FWRITE
);
855 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
859 if (*(unsigned int *)data
>= NSIG
||
860 *(unsigned int *)data
== 0) {
864 if ((tp
->t_lflag
&NOFLSH
) == 0)
865 ttyflush(tp
, FREAD
|FWRITE
);
866 if ((*(unsigned int *)data
== SIGINFO
) &&
867 ((tp
->t_lflag
&NOKERNINFO
) == 0))
870 * SAFE: All callers drop the lock on return and
871 * SAFE: the linesw[] will short circut this call
872 * SAFE: if the ioctl() is eaten before the lower
873 * SAFE: level code gets to see it.
876 tty_pgsignal(tp
, *(unsigned int *)data
, 1);
880 case TIOCPTYGRANT
: /* grantpt(3) */
882 * Change the uid of the slave to that of the calling
883 * thread, change the gid of the slave to GID_TTY,
884 * change the mode to 0620 (rw--w----).
887 _devfs_setattr(pti
->pt_devhandle
, 0620, kauth_getuid(), GID_TTY
);
891 case TIOCPTYGNAME
: /* ptsname(3) */
893 * Report the name of the slave device in *data
894 * (128 bytes max.). Use the same derivation method
895 * used for calling devfs_make_node() to create it.
897 snprintf(data
, 128, "/dev/tty%c%x",
898 START_CHAR
+ (minor(dev
) / HEX_BASE
),
899 minor(dev
) % HEX_BASE
);
903 case TIOCPTYUNLK
: /* unlockpt(3) */
905 * Unlock the slave device so that it can be opened.
910 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
911 if (error
== ENOTTY
) {
912 error
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
913 if (error
== ENOTTY
) {
914 if (pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
915 /* Process the UIOCMD ioctl group */
917 pti
->pt_ucntl
= (u_char
)cmd
;
918 ptcwakeup(tp
, FREAD
);
922 } else if (cmd
== TIOCSBRK
|| cmd
== TIOCCBRK
) {
924 * POSIX conformance; rdar://3936338
926 * Clear ENOTTY in the case of setting or
927 * clearing a break failing because pty's
928 * don't support break like real serial
938 * If external processing and packet mode send ioctl packet.
940 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
955 pti
->pt_send
|= TIOCPKT_IOCTL
;
956 ptcwakeup(tp
, FREAD
);
961 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
962 && CCEQ(cc
[VSTART
], CTRL('q'));
963 if (pti
->pt_flags
& PF_NOSTOP
) {
965 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
966 pti
->pt_send
|= TIOCPKT_DOSTOP
;
967 pti
->pt_flags
&= ~PF_NOSTOP
;
968 ptcwakeup(tp
, FREAD
);
972 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
973 pti
->pt_send
|= TIOCPKT_NOSTOP
;
974 pti
->pt_flags
|= PF_NOSTOP
;
975 ptcwakeup(tp
, FREAD
);