2 * Copyright (c) 1997-2013 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() */
87 #include <security/mac_framework.h>
92 /* XXX belongs in devfs somewhere - LATER */
93 static int _devfs_setattr(void *, unsigned short, uid_t
, gid_t
);
96 * Forward declarations
98 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
; /* common ioctl */
104 __XNU_PRIVATE_EXTERN d_stop_t ptsstop
;
105 __XNU_PRIVATE_EXTERN d_reset_t ptsreset
;
106 __XNU_PRIVATE_EXTERN d_select_t ptsselect
;
107 __XNU_PRIVATE_EXTERN d_open_t ptcopen
;
108 __XNU_PRIVATE_EXTERN d_close_t ptcclose
;
109 __XNU_PRIVATE_EXTERN d_read_t ptcread
;
110 __XNU_PRIVATE_EXTERN d_write_t ptcwrite
;
111 __XNU_PRIVATE_EXTERN d_stop_t ptcstop
; /* NO-OP */
112 __XNU_PRIVATE_EXTERN d_reset_t ptcreset
;
113 __XNU_PRIVATE_EXTERN d_select_t ptcselect
;
116 * XXX Should be devfs function... and use VATTR mechanisms, per
117 * XXX vnode_setattr2(); only we maybe can't really get back to the
118 * XXX vnode here for cloning devices (but it works for *cloned* devices
119 * XXX that are not themselves cloning).
126 _devfs_setattr(void * handle
, unsigned short mode
, uid_t uid
, gid_t gid
)
128 devdirent_t
*direntp
= (devdirent_t
*)handle
;
131 vfs_context_t ctx
= vfs_context_current();;
132 struct vnode_attr va
;
135 VATTR_SET(&va
, va_uid
, uid
);
136 VATTR_SET(&va
, va_gid
, gid
);
137 VATTR_SET(&va
, va_mode
, mode
& ALLPERMS
);
140 * If the TIOCPTYGRANT loses the race with the clone operation because
141 * this function is not part of devfs, and therefore can't take the
142 * devfs lock to protect the direntp update, then force user space to
143 * redrive the grant request.
145 if (direntp
== NULL
|| (devnodep
= direntp
->de_dnp
) == NULL
) {
151 * Only do this if we are operating on device that doesn't clone
152 * each time it's referenced. We perform a lookup on the device
153 * to insure we get the right instance. We can't just use the call
154 * to devfs_dntovn() to get the vp for the operation, because
155 * dn_dvm may not have been initialized.
157 if (devnodep
->dn_clone
== NULL
) {
161 snprintf(name
, sizeof(name
), "/dev/%s", direntp
->de_name
);
162 NDINIT(&nd
, LOOKUP
, OP_SETATTR
, FOLLOW
, UIO_SYSSPACE
, CAST_USER_ADDR_T(name
), ctx
);
166 error
= vnode_setattr(nd
.ni_vp
, &va
, ctx
);
176 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
178 static struct tty_dev_t
*tty_dev_head
;
180 __private_extern__
void
181 tty_dev_register(struct tty_dev_t
*driver
)
184 driver
->next
= tty_dev_head
;
185 tty_dev_head
= driver
;
190 * Given a minor number, return the corresponding structure for that minor
191 * number. If there isn't one, and the create flag is specified, we create
194 * Parameters: minor Minor number of ptmx device
195 * open_flag PF_OPEN_M First open of master
196 * PF_OPEN_S First open of slave
197 * 0 Just want ioctl struct
199 * Returns: NULL Did not exist/could not create
200 * !NULL structure corresponding minor number
202 * Locks: tty_lock() on ptmx_ioctl->pt_tty NOT held on entry or exit.
205 static struct tty_dev_t
*
206 pty_get_driver(dev_t dev
)
208 int major
= major(dev
);
209 struct tty_dev_t
*driver
;
210 for (driver
= tty_dev_head
; driver
!= NULL
; driver
= driver
->next
) {
211 if ((driver
->master
== major
|| driver
->slave
== major
)) {
218 static struct ptmx_ioctl
*
219 pty_get_ioctl(dev_t dev
, int open_flag
, struct tty_dev_t
**out_driver
)
221 struct tty_dev_t
*driver
= pty_get_driver(dev
);
223 *out_driver
= driver
;
225 if (driver
&& driver
->open
) {
226 return driver
->open(minor(dev
), open_flag
);
232 * Locks: tty_lock() of old_ptmx_ioctl->pt_tty NOT held for this call.
235 pty_free_ioctl(dev_t dev
, int open_flag
)
237 struct tty_dev_t
*driver
= pty_get_driver(dev
);
238 if (driver
&& driver
->free
) {
239 return driver
->free(minor(dev
), open_flag
);
245 pty_get_name(dev_t dev
, char *buffer
, size_t size
)
247 struct tty_dev_t
*driver
= pty_get_driver(dev
);
248 if (driver
&& driver
->name
) {
249 return driver
->name(minor(dev
), buffer
, size
);
254 __private_extern__
int
255 ptsopen(dev_t dev
, int flag
, __unused
int devtype
, __unused
struct proc
*p
)
258 struct tty_dev_t
*driver
;
259 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, PF_OPEN_S
, &driver
);
263 if (!(pti
->pt_flags
& PF_UNLOCKED
)) {
267 struct tty
*tp
= pti
->pt_tty
;
270 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
271 termioschars(&tp
->t_termios
); /* Set up default chars */
272 tp
->t_iflag
= TTYDEF_IFLAG
;
273 tp
->t_oflag
= TTYDEF_OFLAG
;
274 tp
->t_lflag
= TTYDEF_LFLAG
;
275 tp
->t_cflag
= TTYDEF_CFLAG
;
276 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
277 ttsetwater(tp
); /* would be done in xxparam() */
278 } else if ((tp
->t_state
& TS_XCLUDE
) && kauth_cred_issuser(kauth_cred_get())) {
282 if (tp
->t_oproc
) /* Ctrlr still around. */
283 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
284 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
287 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
, __FUNCTION__
, 0);
291 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
292 /* Successful open; mark as open by the slave */
294 pti
->pt_flags
|= PF_OPEN_S
;
295 CLR(tp
->t_state
, TS_IOCTL_NOT_OK
);
297 ptcwakeup(tp
, FREAD
|FWRITE
);
304 __private_extern__
int
305 ptsclose(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
310 * This is temporary until the VSX conformance tests
311 * are fixed. They are hanging with a deadlock
312 * where close() will not complete without t_timeout set
314 #define FIX_VSX_HANG 1
318 struct tty_dev_t
*driver
;
319 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, &driver
);
328 save_timeout
= tp
->t_timeout
;
331 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
332 ptsstop(tp
, FREAD
|FWRITE
);
335 tp
->t_timeout
= save_timeout
;
339 if ((flag
& IO_REVOKE
) == IO_REVOKE
&& driver
->revoke
) {
340 driver
->revoke(minor(dev
), tp
);
342 /* unconditional, just like ttyclose() */
343 pty_free_ioctl(dev
, PF_OPEN_S
);
348 __private_extern__
int
349 ptsread(dev_t dev
, struct uio
*uio
, int flag
)
351 proc_t p
= current_proc();
352 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
363 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
365 if (pti
->pt_flags
& PF_REMOTE
) {
366 while (isbackground(p
, tp
)) {
367 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
368 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
369 p
->p_lflag
& P_LPPWAIT
) {
376 if (pg
== PGRP_NULL
) {
381 * SAFE: We about to drop the lock ourselves by
382 * SAFE: erroring out or sleeping anyway.
385 if (pg
->pg_jobc
== 0) {
391 pgsignal(pg
, SIGTTIN
, 1);
395 error
= ttysleep(tp
, &ptsread
, TTIPRI
| PCATCH
| PTTYBLOCK
, __FUNCTION__
, hz
);
399 if (tp
->t_canq
.c_cc
== 0) {
400 if (flag
& IO_NDELAY
) {
404 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
, __FUNCTION__
, 0);
409 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
413 cc
= MIN((int)uio_resid(uio
), BUFSIZ
);
414 // Don't copy the very last byte
415 cc
= MIN(cc
, tp
->t_canq
.c_cc
- 1);
416 cc
= q_to_b(&tp
->t_canq
, (u_char
*)buf
, cc
);
417 error
= uiomove(buf
, cc
, uio
);
421 if (tp
->t_canq
.c_cc
== 1)
422 (void) getc(&tp
->t_canq
);
427 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
428 ptcwakeup(tp
, FWRITE
);
435 * Write to pseudo-tty.
436 * Wakeups of controlling tty will happen
437 * indirectly, when tty driver calls ptsstart.
439 __private_extern__
int
440 ptswrite(dev_t dev
, struct uio
*uio
, int flag
)
442 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
451 if (tp
->t_oproc
== 0)
454 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
462 * Start output on pseudo-tty.
463 * Wake up process selecting or sleeping for input from controlling tty.
465 * t_oproc for this driver; called from within the line discipline
467 * Locks: Assumes tp is locked on entry, remains locked on exit
470 ptsstart(struct tty
*tp
)
472 struct ptmx_ioctl
*pti
= pty_get_ioctl(tp
->t_dev
, 0, NULL
);
475 if (tp
->t_state
& TS_TTSTOP
)
477 if (pti
->pt_flags
& PF_STOPPED
) {
478 pti
->pt_flags
&= ~PF_STOPPED
;
479 pti
->pt_send
= TIOCPKT_START
;
481 ptcwakeup(tp
, FREAD
);
487 * Locks: Assumes tty_lock() is held over this call.
490 ptcwakeup(struct tty
*tp
, int flag
)
492 struct ptmx_ioctl
*pti
= pty_get_ioctl(tp
->t_dev
, 0, NULL
);
497 selwakeup(&pti
->pt_selr
);
498 wakeup(TSA_PTC_READ(tp
));
499 KNOTE(&pti
->pt_selr
.si_note
, 1);
502 selwakeup(&pti
->pt_selw
);
503 wakeup(TSA_PTC_WRITE(tp
));
504 KNOTE(&pti
->pt_selw
.si_note
, 1);
508 __private_extern__
int
509 ptcopen(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
511 struct tty_dev_t
*driver
;
512 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, PF_OPEN_M
, &driver
);
515 } else if (pti
== (struct ptmx_ioctl
*)-1) {
516 return (EREDRIVEOPEN
);
519 struct tty
*tp
= pti
->pt_tty
;
522 /* If master is open OR slave is still draining, pty is still busy */
523 if (tp
->t_oproc
|| (tp
->t_state
& TS_ISOPEN
)) {
526 * If master is closed, we are the only reference, so we
527 * need to clear the master open bit
530 pty_free_ioctl(dev
, PF_OPEN_M
);
534 tp
->t_oproc
= ptsstart
;
535 CLR(tp
->t_state
, TS_ZOMBIE
);
536 SET(tp
->t_state
, TS_IOCTL_NOT_OK
);
538 tp
->t_stop
= ptsstop
;
540 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
541 tp
->t_lflag
&= ~EXTPROC
;
543 if (driver
->open_reset
) {
544 pti
->pt_flags
= PF_UNLOCKED
;
553 __private_extern__
int
554 ptcclose(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
556 struct tty_dev_t
*driver
;
557 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, &driver
);
565 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
568 * XXX MDMBUF makes no sense for ptys but would inhibit the above
569 * l_modem(). CLOCAL makes sense but isn't supported. Special
570 * l_modem()s that ignore carrier drop make no sense for ptys but
571 * may be in use because other parts of the line discipline make
572 * sense for ptys. Recover by doing everything that a normal
573 * ttymodem() would have done except for sending a SIGHUP.
575 if (tp
->t_state
& TS_ISOPEN
) {
576 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
577 tp
->t_state
|= TS_ZOMBIE
;
578 ttyflush(tp
, FREAD
| FWRITE
);
581 tp
->t_oproc
= 0; /* mark closed */
585 pty_free_ioctl(dev
, PF_OPEN_M
);
587 if (driver
->mac_notify
) {
588 mac_pty_notify_close(p
, tp
, dev
, NULL
);
595 __private_extern__
int
596 ptcread(dev_t dev
, struct uio
*uio
, int flag
)
598 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
609 * We want to block until the slave
610 * is open, and there's something to read;
611 * but if we lost the slave or we're NBIO,
612 * then return the appropriate error instead.
615 if (tp
->t_state
& TS_ISOPEN
) {
616 if (pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) {
617 error
= ureadc((int)pti
->pt_send
, uio
);
620 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
621 cc
= MIN((int)uio_resid(uio
),
622 (int)sizeof(tp
->t_termios
));
623 uiomove((caddr_t
)&tp
->t_termios
, cc
,
629 if (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
) {
630 error
= ureadc((int)pti
->pt_ucntl
, uio
);
636 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
639 if ((tp
->t_state
& TS_CONNECTED
) == 0)
641 if (flag
& IO_NDELAY
) {
645 error
= ttysleep(tp
, TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, __FUNCTION__
, 0);
649 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
650 error
= ureadc(0, uio
);
651 while (uio_resid(uio
) > 0 && error
== 0) {
652 cc
= q_to_b(&tp
->t_outq
, (u_char
*)buf
, MIN((int)uio_resid(uio
), BUFSIZ
));
655 error
= uiomove(buf
, cc
, uio
);
657 (*linesw
[tp
->t_line
].l_start
)(tp
);
666 * Line discipline callback
668 * Locks: tty_lock() is assumed held on entry and exit.
670 __private_extern__
int
671 ptsstop(struct tty
* tp
, int flush
)
673 struct ptmx_ioctl
*pti
= pty_get_ioctl(tp
->t_dev
, 0, NULL
);
679 /* note: FLUSHREAD and FLUSHWRITE already ok */
681 flush
= TIOCPKT_STOP
;
682 pti
->pt_flags
|= PF_STOPPED
;
684 pti
->pt_flags
&= ~PF_STOPPED
;
685 pti
->pt_send
|= flush
;
686 /* change of perspective */
696 __private_extern__
int
697 ptsreset(__unused
int uban
)
703 ptsselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
705 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
719 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
724 retval
= ttnread(tp
);
729 selrecord(p
, &tp
->t_rsel
, wql
);
732 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
737 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
738 ISSET(tp
->t_state
, TS_CONNECTED
)) {
739 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
743 selrecord(p
, &tp
->t_wsel
, wql
);
751 __private_extern__
int
752 ptcselect(dev_t dev
, int rw
, void *wql
, proc_t p
)
754 struct tty_dev_t
*driver
;
755 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, &driver
);
764 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
772 * Need to block timeouts (ttrstart).
774 if ((tp
->t_state
&TS_ISOPEN
) &&
775 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
776 retval
= (driver
->fix_7828447
) ? tp
->t_outq
.c_cc
: 1;
781 case 0: /* exceptional */
782 if ((tp
->t_state
&TS_ISOPEN
) &&
783 (((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
784 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
))) {
788 selrecord(p
, &pti
->pt_selr
, wql
);
793 if (tp
->t_state
&TS_ISOPEN
) {
794 if (pti
->pt_flags
& PF_REMOTE
) {
795 if (tp
->t_canq
.c_cc
== 0) {
796 retval
= (driver
->fix_7828447
) ? (TTYHOG
- 1) : 1;
800 retval
= (TTYHOG
- 2) - (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
);
802 retval
= (driver
->fix_7828447
) ? retval
: 1;
805 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
&ICANON
)) {
812 selrecord(p
, &pti
->pt_selw
, wql
);
822 __private_extern__
int
823 ptcstop(__unused
struct tty
*tp
, __unused
int flush
)
828 __private_extern__
int
829 ptcreset(__unused
int uban
)
834 __private_extern__
int
835 ptcwrite(dev_t dev
, struct uio
*uio
, int flag
)
837 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, NULL
);
841 u_char locbuf
[BUFSIZ
];
851 if ((tp
->t_state
& TS_ISOPEN
) == 0)
853 if (pti
->pt_flags
& PF_REMOTE
) {
856 while ((uio_resid(uio
) > 0 || cc
> 0) &&
857 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
859 cc
= MIN((int)uio_resid(uio
), BUFSIZ
);
860 cc
= MIN(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
862 error
= uiomove((caddr_t
)cp
, cc
, uio
);
865 /* check again for safety */
866 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
867 /* adjust as usual */
868 uio_setresid(uio
, (uio_resid(uio
) + cc
));
874 cc
= b_to_q((u_char
*)cp
, cc
, &tp
->t_canq
);
876 * XXX we don't guarantee that the canq size
877 * is >= TTYHOG, so the above b_to_q() may
878 * leave some bytes uncopied. However, space
879 * is guaranteed for the null terminator if
880 * we don't fail here since (TTYHOG - 1) is
881 * not a multiple of CBSIZE.
887 /* adjust for data copied in but not written */
888 uio_setresid(uio
, (uio_resid(uio
) + cc
));
889 (void) putc(0, &tp
->t_canq
);
891 wakeup(TSA_PTS_READ(tp
));
894 while (uio_resid(uio
) > 0 || cc
> 0) {
896 cc
= MIN((int)uio_resid(uio
), BUFSIZ
);
898 error
= uiomove((caddr_t
)cp
, cc
, uio
);
901 /* check again for safety */
902 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
903 /* adjust for data copied in but not written */
904 uio_setresid(uio
, (uio_resid(uio
) + cc
));
910 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
911 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
912 wakeup(TSA_HUP_OR_INPUT(tp
));
915 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
928 * Come here to wait for slave to open, for space
929 * in outq, or space in rawq, or an empty canq.
931 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
932 /* adjust for data copied in but not written */
933 uio_setresid(uio
, (uio_resid(uio
) + cc
));
937 if (flag
& IO_NDELAY
) {
938 /* adjust for data copied in but not written */
939 uio_setresid(uio
, (uio_resid(uio
) + cc
));
944 error
= ttysleep(tp
, TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, __FUNCTION__
, 0);
946 /* adjust for data copied in but not written */
947 uio_setresid(uio
, (uio_resid(uio
) + cc
));
953 __private_extern__
int
954 ptyioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, struct proc
*p
)
956 struct tty_dev_t
*driver
;
957 struct ptmx_ioctl
*pti
= pty_get_ioctl(dev
, 0, &driver
);
960 int allow_ext_ioctl
= 1;
967 u_char
*cc
= tp
->t_cc
;
970 * Do not permit extended ioctls on the master side of the pty unless
971 * the slave side has been successfully opened and initialized.
973 if (major(dev
) == driver
->master
&&
974 driver
->fix_7070978
&&
975 ISSET(tp
->t_state
, TS_IOCTL_NOT_OK
)) {
980 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
981 * ttywflush(tp) will hang if there are characters in the outq.
983 if (cmd
== TIOCEXT
&& allow_ext_ioctl
) {
985 * When the EXTPROC bit is being toggled, we need
986 * to send an TIOCPKT_IOCTL if the packet driver
990 if (pti
->pt_flags
& PF_PKT
) {
991 pti
->pt_send
|= TIOCPKT_IOCTL
;
992 ptcwakeup(tp
, FREAD
);
994 tp
->t_lflag
|= EXTPROC
;
996 if ((tp
->t_lflag
& EXTPROC
) &&
997 (pti
->pt_flags
& PF_PKT
)) {
998 pti
->pt_send
|= TIOCPKT_IOCTL
;
999 ptcwakeup(tp
, FREAD
);
1001 tp
->t_lflag
&= ~EXTPROC
;
1005 if (cdevsw
[major(dev
)].d_open
== ptcopen
) {
1010 * We aviod calling ttioctl on the controller since,
1011 * in that case, tp must be the controlling terminal.
1013 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1018 if (pti
->pt_flags
& PF_UCNTL
) {
1022 pti
->pt_flags
|= PF_PKT
;
1024 pti
->pt_flags
&= ~PF_PKT
;
1029 if (pti
->pt_flags
& PF_PKT
) {
1033 pti
->pt_flags
|= PF_UCNTL
;
1035 pti
->pt_flags
&= ~PF_UCNTL
;
1040 pti
->pt_flags
|= PF_REMOTE
;
1042 pti
->pt_flags
&= ~PF_REMOTE
;
1043 ttyflush(tp
, FREAD
|FWRITE
);
1055 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1059 if (*(unsigned int *)data
>= NSIG
||
1060 *(unsigned int *)data
== 0) {
1064 if ((tp
->t_lflag
&NOFLSH
) == 0)
1065 ttyflush(tp
, FREAD
|FWRITE
);
1066 if ((*(unsigned int *)data
== SIGINFO
) &&
1067 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1070 * SAFE: All callers drop the lock on return and
1071 * SAFE: the linesw[] will short circut this call
1072 * SAFE: if the ioctl() is eaten before the lower
1073 * SAFE: level code gets to see it.
1076 tty_pgsignal(tp
, *(unsigned int *)data
, 1);
1080 case TIOCPTYGRANT
: /* grantpt(3) */
1082 * Change the uid of the slave to that of the calling
1083 * thread, change the gid of the slave to GID_TTY,
1084 * change the mode to 0620 (rw--w----).
1087 error
= _devfs_setattr(pti
->pt_devhandle
, 0620, kauth_getuid(), GID_TTY
);
1088 if (major(dev
) == driver
->master
) {
1089 if (driver
->mac_notify
) {
1093 mac_pty_notify_grant(p
, tp
, dev
, NULL
);
1104 case TIOCPTYGNAME
: /* ptsname(3) */
1106 * Report the name of the slave device in *data
1107 * (128 bytes max.). Use the same template string
1108 * used for calling devfs_make_node() to create it.
1110 pty_get_name(dev
, data
, 128);
1114 case TIOCPTYUNLK
: /* unlockpt(3) */
1116 * Unlock the slave device so that it can be opened.
1118 if (major(dev
) == driver
->master
) {
1119 pti
->pt_flags
|= PF_UNLOCKED
;
1126 * Fail all other calls; pty masters are not serial devices;
1127 * we only pretend they are when the slave side of the pty is
1130 if (!allow_ext_ioctl
) {
1135 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
1136 if (error
== ENOTTY
) {
1137 error
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
1138 if (error
== ENOTTY
) {
1139 if (pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
1140 /* Process the UIOCMD ioctl group */
1142 pti
->pt_ucntl
= (u_char
)cmd
;
1143 ptcwakeup(tp
, FREAD
);
1147 } else if (cmd
== TIOCSBRK
|| cmd
== TIOCCBRK
) {
1149 * POSIX conformance; rdar://3936338
1151 * Clear ENOTTY in the case of setting or
1152 * clearing a break failing because pty's
1153 * don't support break like real serial
1163 * If external processing and packet mode send ioctl packet.
1165 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1180 pti
->pt_send
|= TIOCPKT_IOCTL
;
1181 ptcwakeup(tp
, FREAD
);
1186 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1187 && CCEQ(cc
[VSTART
], CTRL('q'));
1188 if (pti
->pt_flags
& PF_NOSTOP
) {
1190 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1191 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1192 pti
->pt_flags
&= ~PF_NOSTOP
;
1193 ptcwakeup(tp
, FREAD
);
1197 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1198 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1199 pti
->pt_flags
|= PF_NOSTOP
;
1200 ptcwakeup(tp
, FREAD
);