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 /* Vector is large enough; grab a new ptmx_ioctl */
374 /* Now grab a free slot... */
375 _state
.pis_ioctl_list
[minor
] = new_ptmx_ioctl
;
377 /* reduce free count */
380 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_M
;
383 /* Create the /dev/ttysXXX device {<major>,XXX} */
384 _state
.pis_ioctl_list
[minor
]->pt_devhandle
= devfs_make_node(
385 makedev(ptsd_major
, minor
),
386 DEVFS_CHAR
, UID_ROOT
, GID_TTY
, 0620,
387 PTSD_TEMPLATE
, minor
);
388 } else if (open_flag
& PF_OPEN_S
) {
390 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_S
;
393 return (_state
.pis_ioctl_list
[minor
]);
397 ptmx_free_ioctl(int minor
, int open_flag
)
399 struct ptmx_ioctl
*old_ptmx_ioctl
= NULL
;
404 * We have to check after taking the DEVFS_LOCK, since the pointer
405 * is protected by the lock
407 if (_state
.pis_ioctl_list
[minor
] == NULL
) {
412 _state
.pis_ioctl_list
[minor
]->pt_flags
&= ~(open_flag
);
415 * Was this the last close? We will recognize it because we only get
416 * a notification on the last close of a device, and we will have
417 * cleared both the master and the slave open bits in the flags.
419 if (!(_state
.pis_ioctl_list
[minor
]->pt_flags
& (PF_OPEN_M
|PF_OPEN_S
))) {
420 /* Mark as free so it can be reallocated later */
421 old_ptmx_ioctl
= _state
.pis_ioctl_list
[ minor
];
422 _state
.pis_ioctl_list
[ minor
] = NULL
;
427 /* Free old after dropping lock */
428 if (old_ptmx_ioctl
!= NULL
) {
430 * XXX See <rdar://5348651> and <rdar://4854638>
432 * XXX Conditional to be removed when/if tty/pty reference
433 * XXX counting and mutex implemented.
435 if (old_ptmx_ioctl
->pt_devhandle
!= NULL
)
436 devfs_remove(old_ptmx_ioctl
->pt_devhandle
);
437 ttyfree(old_ptmx_ioctl
->pt_tty
);
438 FREE(old_ptmx_ioctl
, M_TTYS
);
441 return (0); /* Success */
448 * Given the dev entry that's being opened, we clone the device. This driver
449 * doesn't actually use the dev entry, since we alreaqdy know who we are by
450 * being called from this code. This routine is a callback registered from
451 * devfs_make_node_clone() in ptmx_init(); it's purpose is to provide a new
452 * minor number, or to return -1, if one can't be provided.
454 * Parameters: dev The device we are cloning from
456 * Returns: >= 0 A new minor device number
457 * -1 Error: ENOMEM ("Can't alloc device")
459 * NOTE: Called with DEVFS_LOCK() held
462 ptmx_clone(__unused dev_t dev
, int action
)
466 if (action
== DEVFS_CLONE_ALLOC
) {
468 if (_state
.pis_total
== 0)
472 * Note: We can add hinting on free slots, if this linear search
473 * ends up being a performance bottleneck...
475 for(i
= 0; i
< _state
.pis_total
; i
++) {
476 if (_state
.pis_ioctl_list
[ i
] == NULL
)
481 * XXX We fall off the end here; if we did this twice at the
482 * XXX same time, we could return the same minor to two
483 * XXX callers; we should probably exand the pointer vector
484 * XXX here, but I need more information on the MALLOC/FREE
485 * XXX locking to ensure against a deadlock. Maybe we can
486 * XXX just high watermark it at 1/2 of PTMX_GROW_VECTOR?
487 * XXX That would require returning &minor as implict return
488 * XXX and an error code ("EAGAIN/ERESTART") or 0 as our
489 * XXX explicit return.
492 return (i
); /* empty slot or next slot */
498 ptsd_open(dev_t dev
, int flag
, __unused
int devtype
, __unused proc_t p
)
501 struct ptmx_ioctl
*pti
;
503 boolean_t funnel_state
;
505 if ((pti
= ptmx_get_ioctl(minor(dev
), 0)) == NULL
) {
510 if (!(pti
->pt_flags
& PF_UNLOCKED
)) {
514 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
516 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
517 ttychars(tp
); /* Set up default chars */
518 tp
->t_iflag
= TTYDEF_IFLAG
;
519 tp
->t_oflag
= TTYDEF_OFLAG
;
520 tp
->t_lflag
= TTYDEF_LFLAG
;
521 tp
->t_cflag
= TTYDEF_CFLAG
;
522 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
523 ttsetwater(tp
); /* would be done in xxparam() */
524 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
528 if (tp
->t_oproc
) /* Ctrlr still around. */
529 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
530 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
533 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
538 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
539 /* Successful open; mark as open by the slave */
540 pti
->pt_flags
|= PF_OPEN_S
;
542 ptmx_wakeup(tp
, FREAD
|FWRITE
);
544 (void) thread_funnel_set(kernel_flock
, funnel_state
);
549 ptsd_close(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
552 struct ptmx_ioctl
*pti
;
554 boolean_t funnel_state
;
557 * This is temporary until the VSX conformance tests
558 * are fixed. They are hanging with a deadlock
559 * where close(ptsd) will not complete without t_timeout set
561 #define FIX_VSX_HANG 1
565 pti
= ptmx_get_ioctl(minor(dev
), 0);
567 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
572 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
575 save_timeout
= tp
->t_timeout
;
578 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
579 ptsd_stop(tp
, FREAD
|FWRITE
);
582 tp
->t_timeout
= save_timeout
;
584 (void) thread_funnel_set(kernel_flock
, funnel_state
);
586 /* unconditional, just like ttyclose() */
587 ptmx_free_ioctl(minor(dev
), PF_OPEN_S
);
593 ptsd_read(dev_t dev
, struct uio
*uio
, int flag
)
595 proc_t p
= current_proc();
598 struct ptmx_ioctl
*pti
;
601 boolean_t funnel_state
;
604 pti
= ptmx_get_ioctl(minor(dev
), 0);
606 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
611 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
614 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
616 if (pti
->pt_flags
& PF_REMOTE
) {
617 while (isbackground(p
, tp
)) {
618 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
619 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
620 p
->p_lflag
& P_LPPWAIT
) {
625 if (pg
== PGRP_NULL
) {
629 if (pg
->pg_jobc
== 0) {
634 pgsignal(pg
, SIGTTIN
, 1);
637 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsd_bg",
642 if (tp
->t_canq
.c_cc
== 0) {
643 if (flag
& IO_NDELAY
)
644 return (EWOULDBLOCK
);
645 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
651 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
655 cc
= min(uio_resid(uio
), BUFSIZ
);
656 // Don't copy the very last byte
657 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
658 cc
= q_to_b(&tp
->t_canq
, (u_char
*)buf
, cc
);
659 error
= uiomove(buf
, cc
, uio
);
663 if (tp
->t_canq
.c_cc
== 1)
664 (void) getc(&tp
->t_canq
);
669 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
670 ptmx_wakeup(tp
, FWRITE
);
672 (void) thread_funnel_set(kernel_flock
, funnel_state
);
677 * Write to pseudo-tty.
678 * Wakeups of controlling tty will happen
679 * indirectly, when tty driver calls ptsd_start.
682 ptsd_write(dev_t dev
, struct uio
*uio
, int flag
)
685 struct ptmx_ioctl
*pti
;
687 boolean_t funnel_state
;
689 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
691 pti
= ptmx_get_ioctl(minor(dev
), 0);
693 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
698 if (tp
->t_oproc
== 0)
701 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
703 (void) thread_funnel_set(kernel_flock
, funnel_state
);
708 * Start output on pseudo-tty.
709 * Wake up process selecting or sleeping for input from controlling tty.
712 ptsd_start(struct tty
*tp
)
714 struct ptmx_ioctl
*pti
;
715 boolean_t funnel_state
;
717 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
720 return; /* XXX ENXIO, but this function is void! */
723 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
725 if (tp
->t_state
& TS_TTSTOP
)
727 if (pti
->pt_flags
& PF_STOPPED
) {
728 pti
->pt_flags
&= ~PF_STOPPED
;
729 pti
->pt_send
= TIOCPKT_START
;
731 ptmx_wakeup(tp
, FREAD
);
733 (void) thread_funnel_set(kernel_flock
, funnel_state
);
738 ptmx_wakeup(struct tty
*tp
, int flag
)
740 struct ptmx_ioctl
*pti
;
741 boolean_t funnel_state
;
743 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
746 return; /* XXX ENXIO, but this function is void! */
749 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
752 selwakeup(&pti
->pt_selr
);
753 wakeup(TSA_PTC_READ(tp
));
756 selwakeup(&pti
->pt_selw
);
757 wakeup(TSA_PTC_WRITE(tp
));
759 (void) thread_funnel_set(kernel_flock
, funnel_state
);
763 ptmx_open(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
766 struct ptmx_ioctl
*pti
;
768 boolean_t funnel_state
;
771 if ((pti
= ptmx_get_ioctl(minor(dev
), PF_OPEN_M
)) == NULL
) {
776 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
778 /* If master is open OR slave is still draining, pty is still busy */
779 if (tp
->t_oproc
|| (tp
->t_state
& TS_ISOPEN
)) {
781 * If master is closed, we are the only reference, so we
782 * need to clear the master open bit
785 ptmx_free_ioctl(minor(dev
), PF_OPEN_M
);
789 tp
->t_oproc
= ptsd_start
;
790 CLR(tp
->t_state
, TS_ZOMBIE
);
792 tp
->t_stop
= ptsd_stop
;
794 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
795 tp
->t_lflag
&= ~EXTPROC
;
798 (void) thread_funnel_set(kernel_flock
, funnel_state
);
803 ptmx_close(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
806 struct ptmx_ioctl
*pti
;
807 boolean_t funnel_state
;
809 pti
= ptmx_get_ioctl(minor(dev
), 0);
811 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
816 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
818 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
821 * XXX MDMBUF makes no sense for ptys but would inhibit the above
822 * l_modem(). CLOCAL makes sense but isn't supported. Special
823 * l_modem()s that ignore carrier drop make no sense for ptys but
824 * may be in use because other parts of the line discipline make
825 * sense for ptys. Recover by doing everything that a normal
826 * ttymodem() would have done except for sending a SIGHUP.
828 if (tp
->t_state
& TS_ISOPEN
) {
829 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
830 tp
->t_state
|= TS_ZOMBIE
;
831 ttyflush(tp
, FREAD
| FWRITE
);
834 tp
->t_oproc
= 0; /* mark closed */
836 (void) thread_funnel_set(kernel_flock
, funnel_state
);
838 ptmx_free_ioctl(minor(dev
), PF_OPEN_M
);
844 ptmx_read(dev_t dev
, struct uio
*uio
, int flag
)
847 struct ptmx_ioctl
*pti
;
850 boolean_t funnel_state
;
852 pti
= ptmx_get_ioctl(minor(dev
), 0);
854 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
859 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
862 * We want to block until the slave
863 * is open, and there's something to read;
864 * but if we lost the slave or we're NBIO,
865 * then return the appropriate error instead.
868 if (tp
->t_state
&TS_ISOPEN
) {
869 if (pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) {
870 error
= ureadc((int)pti
->pt_send
, uio
);
873 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
874 cc
= min(uio_resid(uio
),
875 sizeof(tp
->t_termios
));
876 uiomove((caddr_t
)&tp
->t_termios
, cc
,
882 if (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
) {
883 error
= ureadc((int)pti
->pt_ucntl
, uio
);
889 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
892 if ((tp
->t_state
& TS_CONNECTED
) == 0)
894 if (flag
& IO_NDELAY
) {
898 error
= tsleep(TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptmx_in", 0);
902 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
903 error
= ureadc(0, uio
);
904 while (uio_resid(uio
) > 0 && error
== 0) {
905 cc
= q_to_b(&tp
->t_outq
, (u_char
*)buf
, min(uio_resid(uio
), BUFSIZ
));
908 error
= uiomove(buf
, cc
, uio
);
910 (*linesw
[tp
->t_line
].l_start
)(tp
);
913 (void) thread_funnel_set(kernel_flock
, funnel_state
);
918 ptsd_stop(struct tty
*tp
, int flush
)
920 struct ptmx_ioctl
*pti
;
922 boolean_t funnel_state
;
924 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
930 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
932 /* note: FLUSHREAD and FLUSHWRITE already ok */
934 flush
= TIOCPKT_STOP
;
935 pti
->pt_flags
|= PF_STOPPED
;
937 pti
->pt_flags
&= ~PF_STOPPED
;
938 pti
->pt_send
|= flush
;
939 /* change of perspective */
945 ptmx_wakeup(tp
, flag
);
947 (void) thread_funnel_set(kernel_flock
, funnel_state
);
953 ptsd_reset(__unused
int uban
)
959 * Reinput pending characters after state switch
962 * XXX Code duplication: static function, should be inlined
965 ttypend(struct tty
*tp
)
970 CLR(tp
->t_lflag
, PENDIN
);
971 SET(tp
->t_state
, TS_TYPEN
);
974 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
975 while ((c
= getc(&tq
)) >= 0)
977 CLR(tp
->t_state
, TS_TYPEN
);
981 * Must be called at spltty().
983 * XXX Code duplication: static function, should be inlined
986 ttnread(struct tty
*tp
)
990 if (ISSET(tp
->t_lflag
, PENDIN
))
992 nread
= tp
->t_canq
.c_cc
;
993 if (!ISSET(tp
->t_lflag
, ICANON
)) {
994 nread
+= tp
->t_rawq
.c_cc
;
995 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1002 ptsd_select(dev_t dev
, int rw
, void *wql
, proc_t p
)
1004 struct ptmx_ioctl
*pti
;
1007 pti
= ptmx_get_ioctl(minor(dev
), 0);
1009 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
1011 #endif /* 5161374 */
1019 if (ttnread(tp
) > 0 || ISSET(tp
->t_state
, TS_ZOMBIE
))
1021 selrecord(p
, &tp
->t_rsel
, wql
);
1024 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
&&
1025 ISSET(tp
->t_state
, TS_CONNECTED
))
1026 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1029 selrecord(p
, &tp
->t_wsel
, wql
);
1036 ptmx_select(dev_t dev
, int rw
, void *wql
, proc_t p
)
1039 struct ptmx_ioctl
*pti
;
1041 boolean_t funnel_state
;
1043 pti
= ptmx_get_ioctl(minor(dev
), 0);
1045 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
1047 #endif /* 5161374 */
1050 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1052 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1060 * Need to block timeouts (ttrstart).
1062 if ((tp
->t_state
&TS_ISOPEN
) &&
1063 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
1069 case 0: /* exceptional */
1070 if ((tp
->t_state
&TS_ISOPEN
) &&
1071 ((pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) ||
1072 (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
))) {
1076 selrecord(p
, &pti
->pt_selr
, wql
);
1081 if (tp
->t_state
&TS_ISOPEN
) {
1082 if (pti
->pt_flags
& PF_REMOTE
) {
1083 if (tp
->t_canq
.c_cc
== 0) {
1088 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
-2) {
1092 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
&ICANON
)) {
1098 selrecord(p
, &pti
->pt_selw
, wql
);
1103 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1108 ptmx_stop(__unused
struct tty
*tp
, __unused
int flush
)
1114 ptmx_reset(__unused
int uban
)
1120 ptmx_write(dev_t dev
, struct uio
*uio
, int flag
)
1123 struct ptmx_ioctl
*pti
;
1126 u_char locbuf
[BUFSIZ
];
1129 boolean_t funnel_state
;
1131 pti
= ptmx_get_ioctl(minor(dev
), 0);
1133 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
1135 #endif /* 5161374 */
1138 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1141 if ((tp
->t_state
&TS_ISOPEN
) == 0)
1143 if (pti
->pt_flags
& PF_REMOTE
) {
1144 if (tp
->t_canq
.c_cc
)
1146 while ((uio_resid(uio
) > 0 || cc
> 0) &&
1147 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
1149 cc
= min(uio_resid(uio
), BUFSIZ
);
1150 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
1152 error
= uiomove((caddr_t
)cp
, cc
, uio
);
1155 /* check again for safety */
1156 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1157 /* adjust as usual */
1158 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1164 cc
= b_to_q((u_char
*)cp
, cc
, &tp
->t_canq
);
1166 * XXX we don't guarantee that the canq size
1167 * is >= TTYHOG, so the above b_to_q() may
1168 * leave some bytes uncopied. However, space
1169 * is guaranteed for the null terminator if
1170 * we don't fail here since (TTYHOG - 1) is
1171 * not a multiple of CBSIZE.
1177 /* adjust for data copied in but not written */
1178 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1179 (void) putc(0, &tp
->t_canq
);
1181 wakeup(TSA_PTS_READ(tp
));
1184 while (uio_resid(uio
) > 0 || cc
> 0) {
1186 cc
= min(uio_resid(uio
), BUFSIZ
);
1188 error
= uiomove((caddr_t
)cp
, cc
, uio
);
1191 /* check again for safety */
1192 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1193 /* adjust for data copied in but not written */
1194 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1200 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
1201 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
1202 wakeup(TSA_HUP_OR_INPUT(tp
));
1205 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1212 (void) thread_funnel_set(kernel_flock
, funnel_state
);
1216 * Come here to wait for slave to open, for space
1217 * in outq, or space in rawq, or an empty canq.
1219 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1220 /* adjust for data copied in but not written */
1221 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1225 if (flag
& IO_NDELAY
) {
1226 /* adjust for data copied in but not written */
1227 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1229 error
= EWOULDBLOCK
;
1232 error
= tsleep(TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptmx_out", 0);
1234 /* adjust for data copied in but not written */
1235 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1243 cptyioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1246 struct ptmx_ioctl
*pti
;
1248 int stop
, error
= 0;
1249 boolean_t funnel_state
;
1251 pti
= ptmx_get_ioctl(minor(dev
), 0);
1253 if (pti
== NULL
|| pti
->pt_tty
== NULL
)
1255 #endif /* 5161374 */
1259 funnel_state
= thread_funnel_set(kernel_flock
, TRUE
);
1262 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1263 * ttywflush(tp) will hang if there are characters in the outq.
1265 if (cmd
== TIOCEXT
) {
1267 * When the EXTPROC bit is being toggled, we need
1268 * to send an TIOCPKT_IOCTL if the packet driver
1272 if (pti
->pt_flags
& PF_PKT
) {
1273 pti
->pt_send
|= TIOCPKT_IOCTL
;
1274 ptmx_wakeup(tp
, FREAD
);
1276 tp
->t_lflag
|= EXTPROC
;
1278 if ((tp
->t_lflag
& EXTPROC
) &&
1279 (pti
->pt_flags
& PF_PKT
)) {
1280 pti
->pt_send
|= TIOCPKT_IOCTL
;
1281 ptmx_wakeup(tp
, FREAD
);
1283 tp
->t_lflag
&= ~EXTPROC
;
1287 if (cdevsw
[major(dev
)].d_open
== ptmx_open
)
1292 * We aviod calling ttioctl on the controller since,
1293 * in that case, tp must be the controlling terminal.
1295 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1300 if (pti
->pt_flags
& PF_UCNTL
) {
1304 pti
->pt_flags
|= PF_PKT
;
1306 pti
->pt_flags
&= ~PF_PKT
;
1311 if (pti
->pt_flags
& PF_PKT
) {
1315 pti
->pt_flags
|= PF_UCNTL
;
1317 pti
->pt_flags
&= ~PF_UCNTL
;
1322 pti
->pt_flags
|= PF_REMOTE
;
1324 pti
->pt_flags
&= ~PF_REMOTE
;
1325 ttyflush(tp
, FREAD
|FWRITE
);
1336 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1340 if (*(unsigned int *)data
>= NSIG
||
1341 *(unsigned int *)data
== 0) {
1345 if ((tp
->t_lflag
&NOFLSH
) == 0)
1346 ttyflush(tp
, FREAD
|FWRITE
);
1347 tty_pgsignal(tp
, *(unsigned int *)data
, 1);
1348 if ((*(unsigned int *)data
== SIGINFO
) &&
1349 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1353 case TIOCPTYGRANT
: /* grantpt(3) */
1355 * Change the uid of the slave to that of the calling
1356 * thread, change the gid of the slave to GID_TTY,
1357 * change the mode to 0620 (rw--w----).
1360 error
= _devfs_setattr(pti
->pt_devhandle
, 0620, kauth_getuid(), GID_TTY
);
1364 case TIOCPTYGNAME
: /* ptsname(3) */
1366 * Report the name of the slave device in *data
1367 * (128 bytes max.). Use the same template string
1368 * used for calling devfs_make_node() to create it.
1370 snprintf(data
, 128, "/dev/" PTSD_TEMPLATE
, minor(dev
));
1374 case TIOCPTYUNLK
: /* unlockpt(3) */
1376 * Unlock the slave device so that it can be opened.
1378 pti
->pt_flags
|= PF_UNLOCKED
;
1382 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
1383 if (error
== ENOTTY
) {
1384 error
= ttioctl(tp
, cmd
, data
, flag
, p
);
1385 if (error
== ENOTTY
) {
1386 if (pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
1387 /* Process the UIOCMD ioctl group */
1389 pti
->pt_ucntl
= (u_char
)cmd
;
1390 ptmx_wakeup(tp
, FREAD
);
1394 } else if (cmd
== TIOCSBRK
|| cmd
== TIOCCBRK
) {
1396 * POSIX conformance; rdar://3936338
1398 * Clear ENOTTY in the case of setting or
1399 * clearing a break failing because pty's
1400 * don't support break like real serial
1410 * If external processing and packet mode send ioctl packet.
1412 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1421 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
1428 pti
->pt_send
|= TIOCPKT_IOCTL
;
1429 ptmx_wakeup(tp
, FREAD
);
1434 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1435 && CCEQ(cc
[VSTART
], CTRL('q'));
1436 if (pti
->pt_flags
& PF_NOSTOP
) {
1438 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1439 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1440 pti
->pt_flags
&= ~PF_NOSTOP
;
1441 ptmx_wakeup(tp
, FREAD
);
1445 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1446 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1447 pti
->pt_flags
|= PF_NOSTOP
;
1448 ptmx_wakeup(tp
, FREAD
);
1452 (void) thread_funnel_set(kernel_flock
, funnel_state
);