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>
80 #include <sys/vnode_internal.h> /* _devfs_setattr() */
81 #include <sys/stat.h> /* _devfs_setattr() */
83 #include <sys/signalvar.h>
84 #include <sys/sysctl.h>
85 #include <miscfs/devfs/devfs.h>
86 #include <miscfs/devfs/devfsdefs.h> /* DEVFS_LOCK()/DEVFS_UNLOCK() */
88 /* XXX belongs in devfs somewhere - LATER */
89 int _devfs_setattr(void *, unsigned short, uid_t
, gid_t
);
92 #define FREE_BSDSTATIC __private_extern__
93 #define d_devtotty_t struct tty **
96 * Forward declarations
98 int ptmx_init(int n_ptys
);
99 static void ptsd_start(struct tty
*tp
);
100 static void ptmx_wakeup(struct tty
*tp
, int flag
);
101 FREE_BSDSTATIC d_open_t ptsd_open
;
102 FREE_BSDSTATIC d_close_t ptsd_close
;
103 FREE_BSDSTATIC d_read_t ptsd_read
;
104 FREE_BSDSTATIC d_write_t ptsd_write
;
105 FREE_BSDSTATIC d_ioctl_t cptyioctl
; /* common ioctl */
106 FREE_BSDSTATIC d_stop_t ptsd_stop
;
107 FREE_BSDSTATIC d_reset_t ptsd_reset
;
108 FREE_BSDSTATIC d_devtotty_t ptydevtotty
;
109 FREE_BSDSTATIC d_open_t ptmx_open
;
110 FREE_BSDSTATIC d_close_t ptmx_close
;
111 FREE_BSDSTATIC d_read_t ptmx_read
;
112 FREE_BSDSTATIC d_write_t ptmx_write
;
113 FREE_BSDSTATIC d_stop_t ptmx_stop
; /* NO-OP */
114 FREE_BSDSTATIC d_reset_t ptmx_reset
;
115 FREE_BSDSTATIC d_select_t ptmx_select
;
116 FREE_BSDSTATIC d_select_t ptsd_select
;
118 static int ptmx_major
; /* dynamically assigned major number */
119 static struct cdevsw ptmx_cdev
= {
120 ptmx_open
, ptmx_close
, ptmx_read
, ptmx_write
,
121 cptyioctl
, ptmx_stop
, ptmx_reset
, 0,
122 ptmx_select
, eno_mmap
, eno_strat
, eno_getc
,
126 static int ptsd_major
; /* dynamically assigned major number */
127 static struct cdevsw ptsd_cdev
= {
128 ptsd_open
, ptsd_close
, ptsd_read
, ptsd_write
,
129 cptyioctl
, ptsd_stop
, ptsd_reset
, 0,
130 ptsd_select
, eno_mmap
, eno_strat
, eno_getc
,
135 * XXX Should be devfs function... and use VATTR mechanisms, per
136 * XXX vnode_setattr2(); only we maybe can't really get back to the
137 * XXX vnode here for cloning devices (but it works for *cloned* devices
138 * XXX that are not themselves cloning).
145 _devfs_setattr(void * handle
, unsigned short mode
, uid_t uid
, gid_t gid
)
147 devdirent_t
*direntp
= (devdirent_t
*)handle
;
150 vfs_context_t ctx
= vfs_context_current();;
151 struct vnode_attr va
;
154 VATTR_SET(&va
, va_uid
, uid
);
155 VATTR_SET(&va
, va_gid
, gid
);
156 VATTR_SET(&va
, va_mode
, mode
& ALLPERMS
);
159 * If the TIOCPTYGRANT loses the race with the clone operation because
160 * this function is not part of devfs, and therefore can't take the
161 * devfs lock to protect the direntp update, then force user space to
162 * redrive the grant request.
164 if (direntp
== NULL
|| (devnodep
= direntp
->de_dnp
) == NULL
) {
170 * Only do this if we are operating on device that doesn't clone
171 * each time it's referenced. We perform a lookup on the device
172 * to insure we get the right instance. We can't just use the call
173 * to devfs_dntovn() to get the vp for the operation, because
174 * dn_dvm may not have been initialized.
176 if (devnodep
->dn_clone
== NULL
) {
180 snprintf(name
, sizeof(name
), "/dev/%s", direntp
->de_name
);
181 NDINIT(&nd
, LOOKUP
, FOLLOW
, UIO_SYSSPACE
, CAST_USER_ADDR_T(name
), ctx
);
185 error
= vnode_setattr(nd
.ni_vp
, &va
, ctx
);
197 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
201 * ptsd == /dev/pts[0123456789]{3}
203 #define PTMX_TEMPLATE "ptmx"
204 #define PTSD_TEMPLATE "ttys%03d"
207 * System-wide limit on the max number of cloned ptys
209 #define PTMX_MAX_DEFAULT 127 /* 128 entries */
210 #define PTMX_MAX_HARD 999 /* 1000 entries, due to PTSD_TEMPLATE */
212 static int ptmx_max
= PTMX_MAX_DEFAULT
; /* default # of clones we allow */
214 /* Range enforcement for the sysctl */
216 sysctl_ptmx_max(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
217 __unused
int arg2
, struct sysctl_req
*req
)
219 int new_value
, changed
;
220 int error
= sysctl_io_number(req
, ptmx_max
, sizeof(int), &new_value
, &changed
);
222 if (new_value
> 0 && new_value
<= PTMX_MAX_HARD
)
223 ptmx_max
= new_value
;
230 SYSCTL_NODE(_kern
, KERN_TTY
, tty
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "TTY");
231 SYSCTL_PROC(_kern_tty
, OID_AUTO
, ptmx_max
,
232 CTLTYPE_INT
| CTLFLAG_RW
,
233 &ptmx_max
, 0, &sysctl_ptmx_max
, "I", "ptmx_max");
237 * ptmx_ioctl is a pointer to a list of pointers to tty structures which is
238 * grown, as necessary, copied, and replaced, but never shrunk. The ioctl
239 * structures themselves pointed to from this list come and go as needed.
242 struct tty
*pt_tty
; /* pointer to ttymalloc()'ed data */
244 struct selinfo pt_selr
;
245 struct selinfo pt_selw
;
248 void *pt_devhandle
; /* cloned slave device handle */
251 #define PF_PKT 0x0008 /* packet mode */
252 #define PF_STOPPED 0x0010 /* user told stopped */
253 #define PF_REMOTE 0x0020 /* remote and flow controlled input */
254 #define PF_NOSTOP 0x0040
255 #define PF_UCNTL 0x0080 /* user control mode */
256 #define PF_UNLOCKED 0x0100 /* slave unlock (master open resets) */
257 #define PF_OPEN_M 0x0200 /* master is open */
258 #define PF_OPEN_S 0x0400 /* slave is open */
260 static int ptmx_clone(dev_t dev
, int minor
);
263 ptmx_init( __unused
int config_count
)
266 * We start looking at slot 10, since there are inits that will
267 * stomp explicit slots (e.g. vndevice stomps 1) below that.
270 /* Get a major number for /dev/ptmx */
271 if((ptmx_major
= cdevsw_add(-15, &ptmx_cdev
)) == -1) {
272 printf("ptmx_init: failed to obtain /dev/ptmx major number\n");
276 /* Get a major number for /dev/pts/nnn */
277 if ((ptsd_major
= cdevsw_add(-15, &ptsd_cdev
)) == -1) {
278 (void)cdevsw_remove(ptmx_major
, &ptmx_cdev
);
279 printf("ptmx_init: failed to obtain /dev/ptmx major number\n");
283 /* Create the /dev/ptmx device {<major>,0} */
284 (void)devfs_make_node_clone(makedev(ptmx_major
, 0),
285 DEVFS_CHAR
, UID_ROOT
, GID_TTY
, 0666,
286 ptmx_clone
, PTMX_TEMPLATE
);
291 static struct _ptmx_ioctl_state
{
292 struct ptmx_ioctl
**pis_ioctl_list
; /* pointer vector */
293 int pis_total
; /* total slots */
294 int pis_free
; /* free slots */
296 #define PTMX_GROW_VECTOR 16 /* Grow by this many slots at a time */
299 * Given a minor number, return the corresponding structure for that minor
300 * number. If there isn't one, and the create flag is specified, we create
303 * Parameters: minor Minor number of ptmx device
304 * open_flag PF_OPEN_M First open of master
305 * PF_OPEN_S First open of slave
306 * 0 Just want ioctl struct
308 * Returns: NULL Did not exist/could not create
309 * !NULL structure corresponding minor number
311 static struct ptmx_ioctl
*
312 ptmx_get_ioctl(int minor
, int open_flag
)
314 struct ptmx_ioctl
*new_ptmx_ioctl
;
316 if (open_flag
& PF_OPEN_M
) {
319 * If we are about to allocate more memory, but we have
320 * already hit the administrative limit, then fail the
323 * Note: Subtract free from total when making this
324 * check to allow unit increments, rather than
325 * snapping to the nearest PTMX_GROW_VECTOR...
327 if ((_state
.pis_total
- _state
.pis_free
) >= ptmx_max
) {
331 MALLOC(new_ptmx_ioctl
, struct ptmx_ioctl
*, sizeof(struct ptmx_ioctl
), M_TTYS
, M_WAITOK
|M_ZERO
);
332 if (new_ptmx_ioctl
== NULL
) {
336 if ((new_ptmx_ioctl
->pt_tty
= ttymalloc()) == NULL
) {
337 FREE(new_ptmx_ioctl
, M_TTYS
);
342 * Hold the DEVFS_LOCK() over this whole operation; devfs
343 * itself does this over malloc/free as well, so this should
344 * be safe to do. We hold it longer than we want to, but
345 * doing so avoids a reallocation race on the minor number.
348 /* Need to allocate a larger vector? */
349 if (_state
.pis_free
== 0) {
350 struct ptmx_ioctl
**new_pis_ioctl_list
;
351 struct ptmx_ioctl
**old_pis_ioctl_list
= NULL
;
354 MALLOC(new_pis_ioctl_list
, struct ptmx_ioctl
**, sizeof(struct ptmx_ioctl
*) * (_state
.pis_total
+ PTMX_GROW_VECTOR
), M_TTYS
, M_WAITOK
|M_ZERO
);
355 if (new_pis_ioctl_list
== NULL
) {
356 ttyfree(new_ptmx_ioctl
->pt_tty
);
358 FREE(new_ptmx_ioctl
, M_TTYS
);
362 /* If this is not the first time, copy the old over */
363 bcopy(_state
.pis_ioctl_list
, new_pis_ioctl_list
, sizeof(struct ptmx_ioctl
*) * _state
.pis_total
);
364 old_pis_ioctl_list
= _state
.pis_ioctl_list
;
365 _state
.pis_ioctl_list
= new_pis_ioctl_list
;
366 _state
.pis_free
+= PTMX_GROW_VECTOR
;
367 _state
.pis_total
+= PTMX_GROW_VECTOR
;
368 if (old_pis_ioctl_list
)
369 FREE(old_pis_ioctl_list
, M_TTYS
);
372 if (_state
.pis_ioctl_list
[minor
] != NULL
) {
373 ttyfree(new_ptmx_ioctl
->pt_tty
);
375 FREE(new_ptmx_ioctl
, M_TTYS
);
377 /* Special error value so we know to redrive the open, we've been raced */
378 return (struct ptmx_ioctl
*)-1;
382 /* Vector is large enough; grab a new ptmx_ioctl */
384 /* Now grab a free slot... */
385 _state
.pis_ioctl_list
[minor
] = new_ptmx_ioctl
;
387 /* reduce free count */
390 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_M
;
393 /* Create the /dev/ttysXXX device {<major>,XXX} */
394 _state
.pis_ioctl_list
[minor
]->pt_devhandle
= devfs_make_node(
395 makedev(ptsd_major
, minor
),
396 DEVFS_CHAR
, UID_ROOT
, GID_TTY
, 0620,
397 PTSD_TEMPLATE
, minor
);
398 } else if (open_flag
& PF_OPEN_S
) {
400 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_S
;
403 return (_state
.pis_ioctl_list
[minor
]);
407 ptmx_free_ioctl(int minor
, int open_flag
)
409 struct ptmx_ioctl
*old_ptmx_ioctl
= NULL
;
414 * We have to check after taking the DEVFS_LOCK, since the pointer
415 * is protected by the lock
417 if (_state
.pis_ioctl_list
[minor
] == NULL
) {
422 _state
.pis_ioctl_list
[minor
]->pt_flags
&= ~(open_flag
);
425 * Was this the last close? We will recognize it because we only get
426 * a notification on the last close of a device, and we will have
427 * cleared both the master and the slave open bits in the flags.
429 if (!(_state
.pis_ioctl_list
[minor
]->pt_flags
& (PF_OPEN_M
|PF_OPEN_S
))) {
430 /* Mark as free so it can be reallocated later */
431 old_ptmx_ioctl
= _state
.pis_ioctl_list
[ minor
];
435 /* Free old after dropping lock */
436 if (old_ptmx_ioctl
!= NULL
) {
438 * XXX See <rdar://5348651> and <rdar://4854638>
440 * XXX Conditional to be removed when/if tty/pty reference
441 * XXX counting and mutex implemented.
443 if (old_ptmx_ioctl
->pt_devhandle
!= NULL
)
444 devfs_remove(old_ptmx_ioctl
->pt_devhandle
);
445 ttyfree(old_ptmx_ioctl
->pt_tty
);
446 FREE(old_ptmx_ioctl
, M_TTYS
);
448 /* Don't remove the entry until the devfs slot is free */
450 _state
.pis_ioctl_list
[ minor
] = NULL
;
455 return (0); /* Success */
462 * Given the dev entry that's being opened, we clone the device. This driver
463 * doesn't actually use the dev entry, since we alreaqdy know who we are by
464 * being called from this code. This routine is a callback registered from
465 * devfs_make_node_clone() in ptmx_init(); it's purpose is to provide a new
466 * minor number, or to return -1, if one can't be provided.
468 * Parameters: dev The device we are cloning from
470 * Returns: >= 0 A new minor device number
471 * -1 Error: ENOMEM ("Can't alloc device")
473 * NOTE: Called with DEVFS_LOCK() held
476 ptmx_clone(__unused dev_t dev
, int action
)
480 if (action
== DEVFS_CLONE_ALLOC
) {
482 if (_state
.pis_total
== 0)
486 * Note: We can add hinting on free slots, if this linear search
487 * ends up being a performance bottleneck...
489 for(i
= 0; i
< _state
.pis_total
; i
++) {
490 if (_state
.pis_ioctl_list
[ i
] == NULL
)
495 * XXX We fall off the end here; if we did this twice at the
496 * XXX same time, we could return the same minor to two
497 * XXX callers; we should probably exand the pointer vector
498 * XXX here, but I need more information on the MALLOC/FREE
499 * XXX locking to ensure against a deadlock. Maybe we can
500 * XXX just high watermark it at 1/2 of PTMX_GROW_VECTOR?
501 * XXX That would require returning &minor as implict return
502 * XXX and an error code ("EAGAIN/ERESTART") or 0 as our
503 * XXX explicit return.
506 return (i
); /* empty slot or next slot */
512 ptsd_open(dev_t dev
, int flag
, __unused
int devtype
, __unused proc_t p
)
515 struct ptmx_ioctl
*pti
;
517 boolean_t funnel_state
;
519 if ((pti
= ptmx_get_ioctl(minor(dev
), 0)) == NULL
) {
524 if (!(pti
->pt_flags
& PF_UNLOCKED
)) {
528 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
530 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
531 ttychars(tp
); /* Set up default chars */
532 tp
->t_iflag
= TTYDEF_IFLAG
;
533 tp
->t_oflag
= TTYDEF_OFLAG
;
534 tp
->t_lflag
= TTYDEF_LFLAG
;
535 tp
->t_cflag
= TTYDEF_CFLAG
;
536 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
537 ttsetwater(tp
); /* would be done in xxparam() */
538 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
542 if (tp
->t_oproc
) /* Ctrlr still around. */
543 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
544 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
547 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
552 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
553 /* Successful open; mark as open by the slave */
554 pti
->pt_flags
|= PF_OPEN_S
;
556 ptmx_wakeup(tp
, FREAD
|FWRITE
);
558 (void) thread_funnel_set(kernel_flock
, funnel_state
);
563 ptsd_close(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
566 struct ptmx_ioctl
*pti
;
568 boolean_t funnel_state
;
571 * This is temporary until the VSX conformance tests
572 * are fixed. They are hanging with a deadlock
573 * where close(ptsd) will not complete without t_timeout set
575 #define FIX_VSX_HANG 1
579 pti
= ptmx_get_ioctl(minor(dev
), 0);
581 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
586 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
589 save_timeout
= tp
->t_timeout
;
592 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
593 ptsd_stop(tp
, FREAD
|FWRITE
);
596 tp
->t_timeout
= save_timeout
;
598 (void) thread_funnel_set(kernel_flock
, funnel_state
);
600 /* unconditional, just like ttyclose() */
601 ptmx_free_ioctl(minor(dev
), PF_OPEN_S
);
607 ptsd_read(dev_t dev
, struct uio
*uio
, int flag
)
609 proc_t p
= current_proc();
612 struct ptmx_ioctl
*pti
;
615 boolean_t funnel_state
;
618 pti
= ptmx_get_ioctl(minor(dev
), 0);
620 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
625 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
628 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
630 if (pti
->pt_flags
& PF_REMOTE
) {
631 while (isbackground(p
, tp
)) {
632 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
633 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
634 p
->p_lflag
& P_LPPWAIT
) {
639 if (pg
== PGRP_NULL
) {
643 if (pg
->pg_jobc
== 0) {
648 pgsignal(pg
, SIGTTIN
, 1);
651 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsd_bg",
656 if (tp
->t_canq
.c_cc
== 0) {
657 if (flag
& IO_NDELAY
)
658 return (EWOULDBLOCK
);
659 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
665 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
669 cc
= min(uio_resid(uio
), BUFSIZ
);
670 // Don't copy the very last byte
671 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
672 cc
= q_to_b(&tp
->t_canq
, (u_char
*)buf
, cc
);
673 error
= uiomove(buf
, cc
, uio
);
677 if (tp
->t_canq
.c_cc
== 1)
678 (void) getc(&tp
->t_canq
);
683 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
684 ptmx_wakeup(tp
, FWRITE
);
686 (void) thread_funnel_set(kernel_flock
, funnel_state
);
691 * Write to pseudo-tty.
692 * Wakeups of controlling tty will happen
693 * indirectly, when tty driver calls ptsd_start.
696 ptsd_write(dev_t dev
, struct uio
*uio
, int flag
)
699 struct ptmx_ioctl
*pti
;
701 boolean_t funnel_state
;
703 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
705 pti
= ptmx_get_ioctl(minor(dev
), 0);
707 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
712 if (tp
->t_oproc
== 0)
715 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
717 (void) thread_funnel_set(kernel_flock
, funnel_state
);
722 * Start output on pseudo-tty.
723 * Wake up process selecting or sleeping for input from controlling tty.
726 ptsd_start(struct tty
*tp
)
728 struct ptmx_ioctl
*pti
;
729 boolean_t funnel_state
;
731 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
734 return; /* XXX ENXIO, but this function is void! */
737 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
739 if (tp
->t_state
& TS_TTSTOP
)
741 if (pti
->pt_flags
& PF_STOPPED
) {
742 pti
->pt_flags
&= ~PF_STOPPED
;
743 pti
->pt_send
= TIOCPKT_START
;
745 ptmx_wakeup(tp
, FREAD
);
747 (void) thread_funnel_set(kernel_flock
, funnel_state
);
752 ptmx_wakeup(struct tty
*tp
, int flag
)
754 struct ptmx_ioctl
*pti
;
755 boolean_t funnel_state
;
757 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
760 return; /* XXX ENXIO, but this function is void! */
763 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
766 selwakeup(&pti
->pt_selr
);
767 wakeup(TSA_PTC_READ(tp
));
770 selwakeup(&pti
->pt_selw
);
771 wakeup(TSA_PTC_WRITE(tp
));
773 (void) thread_funnel_set(kernel_flock
, funnel_state
);
777 ptmx_open(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
780 struct ptmx_ioctl
*pti
;
782 boolean_t funnel_state
;
784 pti
= ptmx_get_ioctl(minor(dev
), PF_OPEN_M
);
787 } else if (pti
== (struct ptmx_ioctl
*)-1) {
788 return (EREDRIVEOPEN
);
792 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
794 /* If master is open OR slave is still draining, pty is still busy */
795 if (tp
->t_oproc
|| (tp
->t_state
& TS_ISOPEN
)) {
797 * If master is closed, we are the only reference, so we
798 * need to clear the master open bit
801 ptmx_free_ioctl(minor(dev
), PF_OPEN_M
);
805 tp
->t_oproc
= ptsd_start
;
806 CLR(tp
->t_state
, TS_ZOMBIE
);
808 tp
->t_stop
= ptsd_stop
;
810 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
811 tp
->t_lflag
&= ~EXTPROC
;
814 (void) thread_funnel_set(kernel_flock
, funnel_state
);
819 ptmx_close(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
822 struct ptmx_ioctl
*pti
;
823 boolean_t funnel_state
;
825 pti
= ptmx_get_ioctl(minor(dev
), 0);
827 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
832 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
834 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
837 * XXX MDMBUF makes no sense for ptys but would inhibit the above
838 * l_modem(). CLOCAL makes sense but isn't supported. Special
839 * l_modem()s that ignore carrier drop make no sense for ptys but
840 * may be in use because other parts of the line discipline make
841 * sense for ptys. Recover by doing everything that a normal
842 * ttymodem() would have done except for sending a SIGHUP.
844 if (tp
->t_state
& TS_ISOPEN
) {
845 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
846 tp
->t_state
|= TS_ZOMBIE
;
847 ttyflush(tp
, FREAD
| FWRITE
);
850 tp
->t_oproc
= 0; /* mark closed */
852 (void) thread_funnel_set(kernel_flock
, funnel_state
);
854 ptmx_free_ioctl(minor(dev
), PF_OPEN_M
);
860 ptmx_read(dev_t dev
, struct uio
*uio
, int flag
)
863 struct ptmx_ioctl
*pti
;
866 boolean_t funnel_state
;
868 pti
= ptmx_get_ioctl(minor(dev
), 0);
870 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
875 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
878 * We want to block until the slave
879 * is open, and there's something to read;
880 * but if we lost the slave or we're NBIO,
881 * then return the appropriate error instead.
884 if (tp
->t_state
&TS_ISOPEN
) {
885 if (pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) {
886 error
= ureadc((int)pti
->pt_send
, uio
);
889 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
890 cc
= min(uio_resid(uio
),
891 sizeof(tp
->t_termios
));
892 uiomove((caddr_t
)&tp
->t_termios
, cc
,
898 if (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
) {
899 error
= ureadc((int)pti
->pt_ucntl
, uio
);
905 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
908 if ((tp
->t_state
& TS_CONNECTED
) == 0)
910 if (flag
& IO_NDELAY
) {
914 error
= tsleep(TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptmx_in", 0);
918 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
919 error
= ureadc(0, uio
);
920 while (uio_resid(uio
) > 0 && error
== 0) {
921 cc
= q_to_b(&tp
->t_outq
, (u_char
*)buf
, min(uio_resid(uio
), BUFSIZ
));
924 error
= uiomove(buf
, cc
, uio
);
926 (*linesw
[tp
->t_line
].l_start
)(tp
);
929 (void) thread_funnel_set(kernel_flock
, funnel_state
);
934 ptsd_stop(struct tty
*tp
, int flush
)
936 struct ptmx_ioctl
*pti
;
938 boolean_t funnel_state
;
940 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
946 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
948 /* note: FLUSHREAD and FLUSHWRITE already ok */
950 flush
= TIOCPKT_STOP
;
951 pti
->pt_flags
|= PF_STOPPED
;
953 pti
->pt_flags
&= ~PF_STOPPED
;
954 pti
->pt_send
|= flush
;
955 /* change of perspective */
961 ptmx_wakeup(tp
, flag
);
963 (void) thread_funnel_set(kernel_flock
, funnel_state
);
969 ptsd_reset(__unused
int uban
)
975 * Reinput pending characters after state switch
978 * XXX Code duplication: static function, should be inlined
981 ttypend(struct tty
*tp
)
986 CLR(tp
->t_lflag
, PENDIN
);
987 SET(tp
->t_state
, TS_TYPEN
);
990 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
991 while ((c
= getc(&tq
)) >= 0)
993 CLR(tp
->t_state
, TS_TYPEN
);
997 * Must be called at spltty().
999 * XXX Code duplication: static function, should be inlined
1002 ttnread(struct tty
*tp
)
1006 if (ISSET(tp
->t_lflag
, PENDIN
))
1008 nread
= tp
->t_canq
.c_cc
;
1009 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1010 nread
+= tp
->t_rawq
.c_cc
;
1011 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1018 ptsd_select(dev_t dev
, int rw
, void *wql
, proc_t p
)
1020 struct ptmx_ioctl
*pti
;
1023 pti
= ptmx_get_ioctl(minor(dev
), 0);
1025 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
1027 #endif /* 5161374 */
1035 if (ttnread(tp
) > 0 || ISSET(tp
->t_state
, TS_ZOMBIE
))
1037 selrecord(p
, &tp
->t_rsel
, wql
);
1040 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
&&
1041 ISSET(tp
->t_state
, TS_CONNECTED
))
1042 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1045 selrecord(p
, &tp
->t_wsel
, wql
);
1052 ptmx_select(dev_t dev
, int rw
, void *wql
, proc_t p
)
1055 struct ptmx_ioctl
*pti
;
1057 boolean_t funnel_state
;
1059 pti
= ptmx_get_ioctl(minor(dev
), 0);
1061 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
1063 #endif /* 5161374 */
1066 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1068 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1076 * Need to block timeouts (ttrstart).
1078 if ((tp
->t_state
&TS_ISOPEN
) &&
1079 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
1085 case 0: /* exceptional */
1086 if ((tp
->t_state
&TS_ISOPEN
) &&
1087 ((pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) ||
1088 (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
))) {
1092 selrecord(p
, &pti
->pt_selr
, wql
);
1097 if (tp
->t_state
&TS_ISOPEN
) {
1098 if (pti
->pt_flags
& PF_REMOTE
) {
1099 if (tp
->t_canq
.c_cc
== 0) {
1104 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
-2) {
1108 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
&ICANON
)) {
1114 selrecord(p
, &pti
->pt_selw
, wql
);
1119 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1124 ptmx_stop(__unused
struct tty
*tp
, __unused
int flush
)
1130 ptmx_reset(__unused
int uban
)
1136 ptmx_write(dev_t dev
, struct uio
*uio
, int flag
)
1139 struct ptmx_ioctl
*pti
;
1142 u_char locbuf
[BUFSIZ
];
1145 boolean_t funnel_state
;
1147 pti
= ptmx_get_ioctl(minor(dev
), 0);
1149 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
1151 #endif /* 5161374 */
1154 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1157 if ((tp
->t_state
&TS_ISOPEN
) == 0)
1159 if (pti
->pt_flags
& PF_REMOTE
) {
1160 if (tp
->t_canq
.c_cc
)
1162 while ((uio_resid(uio
) > 0 || cc
> 0) &&
1163 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
1165 cc
= min(uio_resid(uio
), BUFSIZ
);
1166 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
1168 error
= uiomove((caddr_t
)cp
, cc
, uio
);
1171 /* check again for safety */
1172 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1173 /* adjust as usual */
1174 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1180 cc
= b_to_q((u_char
*)cp
, cc
, &tp
->t_canq
);
1182 * XXX we don't guarantee that the canq size
1183 * is >= TTYHOG, so the above b_to_q() may
1184 * leave some bytes uncopied. However, space
1185 * is guaranteed for the null terminator if
1186 * we don't fail here since (TTYHOG - 1) is
1187 * not a multiple of CBSIZE.
1193 /* adjust for data copied in but not written */
1194 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1195 (void) putc(0, &tp
->t_canq
);
1197 wakeup(TSA_PTS_READ(tp
));
1200 while (uio_resid(uio
) > 0 || cc
> 0) {
1202 cc
= min(uio_resid(uio
), BUFSIZ
);
1204 error
= uiomove((caddr_t
)cp
, cc
, uio
);
1207 /* check again for safety */
1208 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1209 /* adjust for data copied in but not written */
1210 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1216 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
1217 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
1218 wakeup(TSA_HUP_OR_INPUT(tp
));
1221 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1228 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1232 * Come here to wait for slave to open, for space
1233 * in outq, or space in rawq, or an empty canq.
1235 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1236 /* adjust for data copied in but not written */
1237 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1241 if (flag
& IO_NDELAY
) {
1242 /* adjust for data copied in but not written */
1243 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1245 error
= EWOULDBLOCK
;
1248 error
= tsleep(TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptmx_out", 0);
1250 /* adjust for data copied in but not written */
1251 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1259 cptyioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1262 struct ptmx_ioctl
*pti
;
1264 int stop
, error
= 0;
1265 boolean_t funnel_state
;
1267 pti
= ptmx_get_ioctl(minor(dev
), 0);
1269 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
1271 #endif /* 5161374 */
1275 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1278 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1279 * ttywflush(tp) will hang if there are characters in the outq.
1281 if (cmd
== TIOCEXT
) {
1283 * When the EXTPROC bit is being toggled, we need
1284 * to send an TIOCPKT_IOCTL if the packet driver
1288 if (pti
->pt_flags
& PF_PKT
) {
1289 pti
->pt_send
|= TIOCPKT_IOCTL
;
1290 ptmx_wakeup(tp
, FREAD
);
1292 tp
->t_lflag
|= EXTPROC
;
1294 if ((tp
->t_lflag
& EXTPROC
) &&
1295 (pti
->pt_flags
& PF_PKT
)) {
1296 pti
->pt_send
|= TIOCPKT_IOCTL
;
1297 ptmx_wakeup(tp
, FREAD
);
1299 tp
->t_lflag
&= ~EXTPROC
;
1303 if (cdevsw
[major(dev
)].d_open
== ptmx_open
)
1308 * We aviod calling ttioctl on the controller since,
1309 * in that case, tp must be the controlling terminal.
1311 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1316 if (pti
->pt_flags
& PF_UCNTL
) {
1320 pti
->pt_flags
|= PF_PKT
;
1322 pti
->pt_flags
&= ~PF_PKT
;
1327 if (pti
->pt_flags
& PF_PKT
) {
1331 pti
->pt_flags
|= PF_UCNTL
;
1333 pti
->pt_flags
&= ~PF_UCNTL
;
1338 pti
->pt_flags
|= PF_REMOTE
;
1340 pti
->pt_flags
&= ~PF_REMOTE
;
1341 ttyflush(tp
, FREAD
|FWRITE
);
1352 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1356 if (*(unsigned int *)data
>= NSIG
||
1357 *(unsigned int *)data
== 0) {
1361 if ((tp
->t_lflag
&NOFLSH
) == 0)
1362 ttyflush(tp
, FREAD
|FWRITE
);
1363 tty_pgsignal(tp
, *(unsigned int *)data
, 1);
1364 if ((*(unsigned int *)data
== SIGINFO
) &&
1365 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1369 case TIOCPTYGRANT
: /* grantpt(3) */
1371 * Change the uid of the slave to that of the calling
1372 * thread, change the gid of the slave to GID_TTY,
1373 * change the mode to 0620 (rw--w----).
1376 error
= _devfs_setattr(pti
->pt_devhandle
, 0620, kauth_getuid(), GID_TTY
);
1380 case TIOCPTYGNAME
: /* ptsname(3) */
1382 * Report the name of the slave device in *data
1383 * (128 bytes max.). Use the same template string
1384 * used for calling devfs_make_node() to create it.
1386 snprintf(data
, 128, "/dev/" PTSD_TEMPLATE
, minor(dev
));
1390 case TIOCPTYUNLK
: /* unlockpt(3) */
1392 * Unlock the slave device so that it can be opened.
1394 pti
->pt_flags
|= PF_UNLOCKED
;
1398 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
1399 if (error
== ENOTTY
) {
1400 error
= ttioctl(tp
, cmd
, data
, flag
, p
);
1401 if (error
== ENOTTY
) {
1402 if (pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
1403 /* Process the UIOCMD ioctl group */
1405 pti
->pt_ucntl
= (u_char
)cmd
;
1406 ptmx_wakeup(tp
, FREAD
);
1410 } else if (cmd
== TIOCSBRK
|| cmd
== TIOCCBRK
) {
1412 * POSIX conformance; rdar://3936338
1414 * Clear ENOTTY in the case of setting or
1415 * clearing a break failing because pty's
1416 * don't support break like real serial
1426 * If external processing and packet mode send ioctl packet.
1428 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1437 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
1444 pti
->pt_send
|= TIOCPKT_IOCTL
;
1445 ptmx_wakeup(tp
, FREAD
);
1450 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1451 && CCEQ(cc
[VSTART
], CTRL('q'));
1452 if (pti
->pt_flags
& PF_NOSTOP
) {
1454 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1455 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1456 pti
->pt_flags
&= ~PF_NOSTOP
;
1457 ptmx_wakeup(tp
, FREAD
);
1461 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1462 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1463 pti
->pt_flags
|= PF_NOSTOP
;
1464 ptmx_wakeup(tp
, FREAD
);
1468 (void) thread_funnel_set(kernel_flock
, funnel_state
);