2 * Copyright (c) 1997-2020 Apple 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
63 /* Common callbacks for the pseudo-teletype driver (pty/tty)
64 * and cloning pseudo-teletype driver (ptmx/pts).
67 #include <sys/param.h>
68 #include <sys/systm.h>
69 #include <sys/ioctl.h>
70 #include <sys/proc_internal.h>
71 #include <sys/kauth.h>
74 #include <sys/file_internal.h>
75 #include <sys/uio_internal.h>
76 #include <sys/kernel.h>
77 #include <sys/vnode.h>
78 #include <sys/vnode_internal.h> /* _devfs_setattr() */
79 #include <sys/stat.h> /* _devfs_setattr() */
81 #include <sys/signalvar.h>
82 #include <sys/sysctl.h>
83 #include <miscfs/devfs/devfs.h>
84 #include <miscfs/devfs/devfsdefs.h> /* DEVFS_LOCK()/DEVFS_UNLOCK() */
85 #include <dev/kmreg_com.h>
88 #include <security/mac_framework.h>
93 /* XXX belongs in devfs somewhere - LATER */
94 static int _devfs_setattr(void *, unsigned short, uid_t
, gid_t
);
97 * Forward declarations
99 static void ptcwakeup(struct tty
*tp
, int flag
);
100 __XNU_PRIVATE_EXTERN d_open_t ptsopen
;
101 __XNU_PRIVATE_EXTERN d_close_t ptsclose
;
102 __XNU_PRIVATE_EXTERN d_read_t ptsread
;
103 __XNU_PRIVATE_EXTERN d_write_t ptswrite
;
104 __XNU_PRIVATE_EXTERN d_ioctl_t ptyioctl
; /* common ioctl */
105 __XNU_PRIVATE_EXTERN d_stop_t ptsstop
;
106 __XNU_PRIVATE_EXTERN d_reset_t ptsreset
;
107 __XNU_PRIVATE_EXTERN d_select_t ptsselect
;
108 __XNU_PRIVATE_EXTERN d_open_t ptcopen
;
109 __XNU_PRIVATE_EXTERN d_close_t ptcclose
;
110 __XNU_PRIVATE_EXTERN d_read_t ptcread
;
111 __XNU_PRIVATE_EXTERN d_write_t ptcwrite
;
112 __XNU_PRIVATE_EXTERN d_stop_t ptcstop
; /* NO-OP */
113 __XNU_PRIVATE_EXTERN d_reset_t ptcreset
;
114 __XNU_PRIVATE_EXTERN d_select_t ptcselect
;
117 * XXX Should be devfs function... and use VATTR mechanisms, per
118 * XXX vnode_setattr2(); only we maybe can't really get back to the
119 * XXX vnode here for cloning devices (but it works for *cloned* devices
120 * XXX that are not themselves cloning).
127 _devfs_setattr(void * handle
, unsigned short mode
, uid_t uid
, gid_t gid
)
129 devdirent_t
*direntp
= (devdirent_t
*)handle
;
132 vfs_context_t ctx
= vfs_context_current();;
133 struct vnode_attr va
;
136 VATTR_SET(&va
, va_uid
, uid
);
137 VATTR_SET(&va
, va_gid
, gid
);
138 VATTR_SET(&va
, va_mode
, mode
& ALLPERMS
);
141 * If the TIOCPTYGRANT loses the race with the clone operation because
142 * this function is not part of devfs, and therefore can't take the
143 * devfs lock to protect the direntp update, then force user space to
144 * redrive the grant request.
146 if (direntp
== NULL
|| (devnodep
= direntp
->de_dnp
) == NULL
) {
152 * Only do this if we are operating on device that doesn't clone
153 * each time it's referenced. We perform a lookup on the device
154 * to insure we get the right instance. We can't just use the call
155 * to devfs_dntovn() to get the vp for the operation, because
156 * dn_dvm may not have been initialized.
158 if (devnodep
->dn_clone
== NULL
) {
162 snprintf(name
, sizeof(name
), "/dev/%s", direntp
->de_name
);
163 NDINIT(&nd
, LOOKUP
, OP_SETATTR
, FOLLOW
, UIO_SYSSPACE
, CAST_USER_ADDR_T(name
), ctx
);
168 error
= vnode_setattr(nd
.ni_vp
, &va
, ctx
);
178 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
180 static struct tty_dev_t
*tty_dev_head
;
182 __private_extern__
void
183 tty_dev_register(struct tty_dev_t
*driver
)
186 driver
->next
= tty_dev_head
;
187 tty_dev_head
= driver
;
192 * Given a minor number, return the corresponding structure for that minor
193 * number. If there isn't one, and the create flag is specified, we create
196 * Parameters: minor Minor number of ptmx device
197 * open_flag PF_OPEN_M First open of master
198 * PF_OPEN_S First open of slave
199 * 0 Just want ioctl struct
201 * Returns: NULL Did not exist/could not create
202 * !NULL structure corresponding minor number
204 * Locks: tty_lock() on ptmx_ioctl->pt_tty NOT held on entry or exit.
207 static struct tty_dev_t
*
208 pty_get_driver(dev_t dev
)
210 int major
= major(dev
);
211 struct tty_dev_t
*driver
;
212 for (driver
= tty_dev_head
; driver
!= NULL
; driver
= driver
->next
) {
213 if ((driver
->master
== major
|| driver
->slave
== major
)) {
220 static struct ptmx_ioctl
*
221 pty_get_ioctl(dev_t dev
, int open_flag
, struct tty_dev_t
**out_driver
)
223 struct tty_dev_t
*driver
= pty_get_driver(dev
);
225 *out_driver
= driver
;
227 if (driver
&& driver
->open
) {
228 return driver
->open(minor(dev
), open_flag
);
234 * Locks: tty_lock() of old_ptmx_ioctl->pt_tty NOT held for this call.
237 pty_free_ioctl(dev_t dev
, int open_flag
)
239 struct tty_dev_t
*driver
= pty_get_driver(dev
);
240 if (driver
&& driver
->free
) {
241 return driver
->free(minor(dev
), open_flag
);
247 pty_get_name(dev_t dev
, char *buffer
, size_t size
)
249 struct tty_dev_t
*driver
= pty_get_driver(dev
);
250 if (driver
&& driver
->name
) {
251 return driver
->name(minor(dev
), buffer
, size
);
256 __private_extern__
int
257 ptsopen(dev_t dev
, int flag
, __unused
int devtype
, __unused
struct proc
*p
)
260 struct tty_dev_t
*driver
;
261 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, PF_OPEN_S
, &driver
);
265 if (!(pti
->pt_flags
& PF_UNLOCKED
)) {
269 struct tty
*tp
= pti
->pt_tty
;
272 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
273 termioschars(&tp
->t_termios
); /* Set up default chars */
274 tp
->t_iflag
= TTYDEF_IFLAG
;
275 tp
->t_oflag
= TTYDEF_OFLAG
;
276 tp
->t_lflag
= TTYDEF_LFLAG
;
277 tp
->t_cflag
= TTYDEF_CFLAG
;
278 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
279 ttsetwater(tp
); /* would be done in xxparam() */
280 } else if ((tp
->t_state
& TS_XCLUDE
) && kauth_cred_issuser(kauth_cred_get())) {
284 if (tp
->t_oproc
) { /* Ctrlr still around. */
285 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
287 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
288 if (flag
& FNONBLOCK
) {
291 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
, __FUNCTION__
, 0);
296 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
297 /* Successful open; mark as open by the slave */
299 pti
->pt_flags
|= PF_OPEN_S
;
300 CLR(tp
->t_state
, TS_IOCTL_NOT_OK
);
302 ptcwakeup(tp
, FREAD
| FWRITE
);
310 __private_extern__
int
311 ptsclose(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
316 * This is temporary until the VSX conformance tests
317 * are fixed. They are hanging with a deadlock
318 * where close() will not complete without t_timeout set
320 #define FIX_VSX_HANG 1
324 struct tty_dev_t
*driver
;
325 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, &driver
);
335 save_timeout
= tp
->t_timeout
;
339 * Close the line discipline and backing TTY structures.
341 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
345 * Flush data and notify any waiters on the master side of this PTY.
347 ptsstop(tp
, FREAD
| FWRITE
);
349 tp
->t_timeout
= save_timeout
;
353 if ((flag
& IO_REVOKE
) == IO_REVOKE
&& driver
->revoke
) {
354 driver
->revoke(minor(dev
), tp
);
356 /* unconditional, just like ttyclose() */
357 pty_free_ioctl(dev
, PF_OPEN_S
);
362 __private_extern__
int
363 ptsread(dev_t dev
, struct uio
*uio
, int flag
)
365 proc_t p
= current_proc();
366 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
378 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
380 if (pti
->pt_flags
& PF_REMOTE
) {
381 while (isbackground(p
, tp
)) {
382 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
383 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
384 p
->p_lflag
& P_LPPWAIT
) {
391 if (pg
== PGRP_NULL
) {
396 * SAFE: We about to drop the lock ourselves by
397 * SAFE: erroring out or sleeping anyway.
400 if (pg
->pg_jobc
== 0) {
406 pgsignal(pg
, SIGTTIN
, 1);
410 error
= ttysleep(tp
, &ptsread
, TTIPRI
| PCATCH
| PTTYBLOCK
, __FUNCTION__
, hz
);
415 if (tp
->t_canq
.c_cc
== 0) {
416 if (flag
& IO_NDELAY
) {
420 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
, __FUNCTION__
, 0);
426 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
430 cc
= MIN((int)uio_resid(uio
), BUFSIZ
);
431 // Don't copy the very last byte
432 cc
= MIN(cc
, tp
->t_canq
.c_cc
- 1);
433 cc
= q_to_b(&tp
->t_canq
, (u_char
*)buf
, cc
);
434 error
= uiomove(buf
, cc
, uio
);
439 if (tp
->t_canq
.c_cc
== 1) {
440 (void) getc(&tp
->t_canq
);
442 if (tp
->t_canq
.c_cc
) {
445 } else if (tp
->t_oproc
) {
446 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
448 ptcwakeup(tp
, FWRITE
);
455 * Write to pseudo-tty.
456 * Wakeups of controlling tty will happen
457 * indirectly, when tty driver calls ptsstart.
459 __private_extern__
int
460 ptswrite(dev_t dev
, struct uio
*uio
, int flag
)
462 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
472 if (tp
->t_oproc
== 0) {
475 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
484 * Start output on pseudo-tty.
485 * Wake up process selecting or sleeping for input from controlling tty.
487 * t_oproc for this driver; called from within the line discipline
489 * Locks: Assumes tp is locked on entry, remains locked on exit
492 ptsstart(struct tty
*tp
)
494 struct ptmx_ioctl
*pti
= pty_get_ioctl(tp
->t_dev
, 0, NULL
);
498 if (tp
->t_state
& TS_TTSTOP
) {
501 if (pti
->pt_flags
& PF_STOPPED
) {
502 pti
->pt_flags
&= ~PF_STOPPED
;
503 pti
->pt_send
= TIOCPKT_START
;
505 ptcwakeup(tp
, FREAD
);
511 ptcwakeup_knote(struct selinfo
*sip
, long hint
)
513 if ((sip
->si_flags
& SI_KNPOSTING
) == 0) {
514 sip
->si_flags
|= SI_KNPOSTING
;
515 KNOTE(&sip
->si_note
, hint
);
516 sip
->si_flags
&= ~SI_KNPOSTING
;
521 * Locks: Assumes tty_lock() is held over this call.
524 ptcwakeup(struct tty
*tp
, int flag
)
526 struct ptmx_ioctl
*pti
= pty_get_ioctl(tp
->t_dev
, 0, NULL
);
532 selwakeup(&pti
->pt_selr
);
533 wakeup(TSA_PTC_READ(tp
));
534 ptcwakeup_knote(&pti
->pt_selr
, 1);
537 selwakeup(&pti
->pt_selw
);
538 wakeup(TSA_PTC_WRITE(tp
));
539 ptcwakeup_knote(&pti
->pt_selw
, 1);
543 __private_extern__
int
544 ptcopen(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
546 struct tty_dev_t
*driver
;
547 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, PF_OPEN_M
, &driver
);
550 } else if (pti
== (struct ptmx_ioctl
*)-1) {
554 struct tty
*tp
= pti
->pt_tty
;
557 /* If master is open OR slave is still draining, pty is still busy */
558 if (tp
->t_oproc
|| (tp
->t_state
& TS_ISOPEN
)) {
561 * If master is closed, we are the only reference, so we
562 * need to clear the master open bit
565 pty_free_ioctl(dev
, PF_OPEN_M
);
569 tp
->t_oproc
= ptsstart
;
570 CLR(tp
->t_state
, TS_ZOMBIE
);
571 SET(tp
->t_state
, TS_IOCTL_NOT_OK
);
573 tp
->t_stop
= ptsstop
;
575 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
576 tp
->t_lflag
&= ~EXTPROC
;
578 if (driver
->open_reset
) {
579 pti
->pt_flags
= PF_UNLOCKED
;
588 __private_extern__
int
589 ptcclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
591 struct tty_dev_t
*driver
;
592 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, &driver
);
604 * XXX MDMBUF makes no sense for PTYs, but would inhibit an `l_modem`.
605 * CLOCAL makes sense but isn't supported. Special `l_modem`s that ignore
606 * carrier drop make no sense for PTYs but may be in use because other parts
607 * of the line discipline make sense for PTYs. Recover by doing everything
608 * that a normal `ttymodem` would have done except for sending SIGHUP.
610 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
611 if (tp
->t_state
& TS_ISOPEN
) {
612 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
613 tp
->t_state
|= TS_ZOMBIE
;
614 ttyflush(tp
, FREAD
| FWRITE
);
618 * Null out the backing TTY struct's open procedure to prevent starting
619 * slaves through `ptsstart`.
624 * Clear any select or kevent waiters under the lock.
626 SLIST_FOREACH(kn
, &pti
->pt_selr
.si_note
, kn_selnext
) {
627 KNOTE_DETACH(&pti
->pt_selr
.si_note
, kn
);
629 selthreadclear(&pti
->pt_selr
);
630 SLIST_FOREACH(kn
, &pti
->pt_selw
.si_note
, kn_selnext
) {
631 KNOTE_DETACH(&pti
->pt_selw
.si_note
, kn
);
633 selthreadclear(&pti
->pt_selw
);
637 pty_free_ioctl(dev
, PF_OPEN_M
);
639 if (driver
->mac_notify
) {
640 mac_pty_notify_close(p
, tp
, dev
, NULL
);
647 __private_extern__
int
648 ptcread(dev_t dev
, struct uio
*uio
, int flag
)
650 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
662 * We want to block until the slave
663 * is open, and there's something to read;
664 * but if we lost the slave or we're NBIO,
665 * then return the appropriate error instead.
668 if (tp
->t_state
& TS_ISOPEN
) {
669 if (pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) {
670 error
= ureadc((int)pti
->pt_send
, uio
);
674 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
676 if (uio
->uio_segflg
== UIO_USERSPACE32
) {
677 static struct termios32 tio32
;
678 cc
= MIN((int)uio_resid(uio
), (int)sizeof(tio32
));
679 termios64to32((struct user_termios
*)&tp
->t_termios
,
680 (struct termios32
*)&tio32
);
681 uiomove((caddr_t
)&tio32
, cc
, uio
);
683 if (uio
->uio_segflg
== UIO_USERSPACE64
) {
684 static struct user_termios tio64
;
685 cc
= MIN((int)uio_resid(uio
), (int)sizeof(tio64
));
686 termios32to64((struct termios32
*)&tp
->t_termios
,
687 (struct user_termios
*)&tio64
);
688 uiomove((caddr_t
)&tio64
, cc
, uio
);
691 cc
= MIN((int)uio_resid(uio
), (int)sizeof(tp
->t_termios
));
692 uiomove((caddr_t
)&tp
->t_termios
, cc
, uio
);
698 if (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
) {
699 error
= ureadc((int)pti
->pt_ucntl
, uio
);
706 if (tp
->t_outq
.c_cc
&& (tp
->t_state
& TS_TTSTOP
) == 0) {
710 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
713 if (flag
& IO_NDELAY
) {
717 error
= ttysleep(tp
, TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, __FUNCTION__
, 0);
722 if (pti
->pt_flags
& (PF_PKT
| PF_UCNTL
)) {
723 error
= ureadc(0, uio
);
725 while (uio_resid(uio
) > 0 && error
== 0) {
726 cc
= q_to_b(&tp
->t_outq
, (u_char
*)buf
, MIN((int)uio_resid(uio
), BUFSIZ
));
730 error
= uiomove(buf
, cc
, uio
);
732 (*linesw
[tp
->t_line
].l_start
)(tp
);
741 * Line discipline callback
743 * Locks: tty_lock() is assumed held on entry and exit.
745 __private_extern__
int
746 ptsstop(struct tty
* tp
, int flush
)
748 struct ptmx_ioctl
*pti
= pty_get_ioctl(tp
->t_dev
, 0, NULL
);
755 /* note: FLUSHREAD and FLUSHWRITE already ok */
757 flush
= TIOCPKT_STOP
;
758 pti
->pt_flags
|= PF_STOPPED
;
760 pti
->pt_flags
&= ~PF_STOPPED
;
762 pti
->pt_send
|= flush
;
763 /* change of perspective */
768 if (flush
& FWRITE
) {
775 __private_extern__
int
776 ptsreset(__unused
int uban
)
782 ptsselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
784 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
800 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
805 retval
= ttnread(tp
);
810 selrecord(p
, &tp
->t_rsel
, wql
);
813 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
818 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
819 ISSET(tp
->t_state
, TS_CONNECTED
)) {
820 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
824 selrecord(p
, &tp
->t_wsel
, wql
);
832 __private_extern__
int
833 ptcselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
835 struct tty_dev_t
*driver
;
836 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, &driver
);
846 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
853 * Need to block timeouts (ttrstart).
855 if ((tp
->t_state
& TS_ISOPEN
) &&
856 tp
->t_outq
.c_cc
&& (tp
->t_state
& TS_TTSTOP
) == 0) {
857 retval
= (driver
->fix_7828447
) ? tp
->t_outq
.c_cc
: 1;
862 case 0: /* exceptional */
863 if ((tp
->t_state
& TS_ISOPEN
) &&
864 (((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
865 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
))) {
869 selrecord(p
, &pti
->pt_selr
, wql
);
874 if (tp
->t_state
& TS_ISOPEN
) {
875 if (pti
->pt_flags
& PF_REMOTE
) {
876 if (tp
->t_canq
.c_cc
== 0) {
877 retval
= (driver
->fix_7828447
) ? (TTYHOG
- 1) : 1;
881 retval
= (TTYHOG
- 2) - (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
);
883 retval
= (driver
->fix_7828447
) ? retval
: 1;
886 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
& ICANON
)) {
893 selrecord(p
, &pti
->pt_selw
, wql
);
902 __private_extern__
int
903 ptcstop(__unused
struct tty
*tp
, __unused
int flush
)
908 __private_extern__
int
909 ptcreset(__unused
int uban
)
914 __private_extern__
int
915 ptcwrite(dev_t dev
, struct uio
*uio
, int flag
)
917 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
921 u_char locbuf
[BUFSIZ
];
932 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
935 if (pti
->pt_flags
& PF_REMOTE
) {
936 if (tp
->t_canq
.c_cc
) {
939 while ((uio_resid(uio
) > 0 || cc
> 0) &&
940 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
942 cc
= MIN((int)uio_resid(uio
), BUFSIZ
);
943 cc
= MIN(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
945 error
= uiomove((caddr_t
)cp
, cc
, uio
);
949 /* check again for safety */
950 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
951 /* adjust as usual */
952 uio_setresid(uio
, (uio_resid(uio
) + cc
));
958 cc
= b_to_q((u_char
*)cp
, cc
, &tp
->t_canq
);
960 * XXX we don't guarantee that the canq size
961 * is >= TTYHOG, so the above b_to_q() may
962 * leave some bytes uncopied. However, space
963 * is guaranteed for the null terminator if
964 * we don't fail here since (TTYHOG - 1) is
965 * not a multiple of CBSIZE.
972 /* adjust for data copied in but not written */
973 uio_setresid(uio
, (uio_resid(uio
) + cc
));
974 (void) putc(0, &tp
->t_canq
);
976 wakeup(TSA_PTS_READ(tp
));
979 while (uio_resid(uio
) > 0 || cc
> 0) {
981 cc
= MIN((int)uio_resid(uio
), BUFSIZ
);
983 error
= uiomove((caddr_t
)cp
, cc
, uio
);
987 /* check again for safety */
988 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
989 /* adjust for data copied in but not written */
990 uio_setresid(uio
, (uio_resid(uio
) + cc
));
996 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
997 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
& ICANON
))) {
998 wakeup(TSA_HUP_OR_INPUT(tp
));
1001 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1014 * Come here to wait for slave to open, for space
1015 * in outq, or space in rawq, or an empty canq.
1017 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1018 /* adjust for data copied in but not written */
1019 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1023 if (flag
& IO_NDELAY
) {
1024 /* adjust for data copied in but not written */
1025 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1027 error
= EWOULDBLOCK
;
1031 error
= ttysleep(tp
, TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, __FUNCTION__
, 0);
1033 /* adjust for data copied in but not written */
1034 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1041 * ptyioctl: Assumes dev was opened and lock was initilized
1043 __private_extern__
int
1044 ptyioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
)
1046 struct tty_dev_t
*driver
;
1047 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, &driver
);
1049 int stop
, error
= 0;
1050 int allow_ext_ioctl
= 1;
1052 if (pti
== NULL
|| pti
->pt_tty
== NULL
) {
1056 if (cmd
== KMIOCDISABLCONS
) {
1063 u_char
*cc
= tp
->t_cc
;
1066 * Do not permit extended ioctls on the master side of the pty unless
1067 * the slave side has been successfully opened and initialized.
1069 if (major(dev
) == driver
->master
&&
1070 driver
->fix_7070978
&&
1071 ISSET(tp
->t_state
, TS_IOCTL_NOT_OK
)) {
1072 allow_ext_ioctl
= 0;
1076 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1077 * ttywflush(tp) will hang if there are characters in the outq.
1079 if (cmd
== TIOCEXT
&& allow_ext_ioctl
) {
1081 * When the EXTPROC bit is being toggled, we need
1082 * to send an TIOCPKT_IOCTL if the packet driver
1086 if (pti
->pt_flags
& PF_PKT
) {
1087 pti
->pt_send
|= TIOCPKT_IOCTL
;
1088 ptcwakeup(tp
, FREAD
);
1090 tp
->t_lflag
|= EXTPROC
;
1092 if ((tp
->t_lflag
& EXTPROC
) &&
1093 (pti
->pt_flags
& PF_PKT
)) {
1094 pti
->pt_send
|= TIOCPKT_IOCTL
;
1095 ptcwakeup(tp
, FREAD
);
1097 tp
->t_lflag
&= ~EXTPROC
;
1100 } else if (cdevsw
[major(dev
)].d_open
== ptcopen
) {
1104 * We aviod calling ttioctl on the controller since,
1105 * in that case, tp must be the controlling terminal.
1107 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1112 if (pti
->pt_flags
& PF_UCNTL
) {
1116 pti
->pt_flags
|= PF_PKT
;
1118 pti
->pt_flags
&= ~PF_PKT
;
1124 if (pti
->pt_flags
& PF_PKT
) {
1128 pti
->pt_flags
|= PF_UCNTL
;
1130 pti
->pt_flags
&= ~PF_UCNTL
;
1136 pti
->pt_flags
|= PF_REMOTE
;
1138 pti
->pt_flags
&= ~PF_REMOTE
;
1140 ttyflush(tp
, FREAD
| FWRITE
);
1152 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1156 if (*(unsigned int *)data
>= NSIG
||
1157 *(unsigned int *)data
== 0) {
1161 if ((tp
->t_lflag
& NOFLSH
) == 0) {
1162 ttyflush(tp
, FREAD
| FWRITE
);
1164 if ((*(unsigned int *)data
== SIGINFO
) &&
1165 ((tp
->t_lflag
& NOKERNINFO
) == 0)) {
1169 * SAFE: All callers drop the lock on return and
1170 * SAFE: the linesw[] will short circut this call
1171 * SAFE: if the ioctl() is eaten before the lower
1172 * SAFE: level code gets to see it.
1175 tty_pgsignal(tp
, *(unsigned int *)data
, 1);
1179 case TIOCPTYGRANT
: /* grantpt(3) */
1181 * Change the uid of the slave to that of the calling
1182 * thread, change the gid of the slave to GID_TTY,
1183 * change the mode to 0620 (rw--w----).
1186 error
= _devfs_setattr(pti
->pt_devhandle
, 0620, kauth_getuid(), GID_TTY
);
1187 if (major(dev
) == driver
->master
) {
1188 if (driver
->mac_notify
) {
1192 mac_pty_notify_grant(p
, tp
, dev
, NULL
);
1203 case TIOCPTYGNAME
: /* ptsname(3) */
1205 * Report the name of the slave device in *data
1206 * (128 bytes max.). Use the same template string
1207 * used for calling devfs_make_node() to create it.
1209 pty_get_name(dev
, data
, 128);
1213 case TIOCPTYUNLK
: /* unlockpt(3) */
1215 * Unlock the slave device so that it can be opened.
1217 if (major(dev
) == driver
->master
) {
1218 pti
->pt_flags
|= PF_UNLOCKED
;
1225 * Fail all other calls; pty masters are not serial devices;
1226 * we only pretend they are when the slave side of the pty is
1229 if (!allow_ext_ioctl
) {
1234 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
1235 if (error
== ENOTTY
) {
1236 error
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
1237 if (error
== ENOTTY
) {
1238 if (pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
1239 /* Process the UIOCMD ioctl group */
1241 pti
->pt_ucntl
= (u_char
)cmd
;
1242 ptcwakeup(tp
, FREAD
);
1246 } else if (cmd
== TIOCSBRK
|| cmd
== TIOCCBRK
) {
1248 * POSIX conformance; rdar://3936338
1250 * Clear ENOTTY in the case of setting or
1251 * clearing a break failing because pty's
1252 * don't support break like real serial
1262 * If external processing and packet mode send ioctl packet.
1264 if ((tp
->t_lflag
& EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1279 pti
->pt_send
|= TIOCPKT_IOCTL
;
1280 ptcwakeup(tp
, FREAD
);
1286 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1287 && CCEQ(cc
[VSTART
], CTRL('q'));
1288 if (pti
->pt_flags
& PF_NOSTOP
) {
1290 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1291 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1292 pti
->pt_flags
&= ~PF_NOSTOP
;
1293 ptcwakeup(tp
, FREAD
);
1297 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1298 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1299 pti
->pt_flags
|= PF_NOSTOP
;
1300 ptcwakeup(tp
, FREAD
);