2 * Copyright (c) 1997-2010 Apple Computer, Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
64 * Pseudo-teletype Driver
65 * (Actually two drivers, requiring two entries in 'cdevsw')
67 #include "pty.h" /* XXX */
69 #include <sys/param.h>
70 #include <sys/systm.h>
71 #include <sys/ioctl.h>
72 #include <sys/proc_internal.h>
73 #include <sys/kauth.h>
76 #include <sys/file_internal.h>
77 #include <sys/uio_internal.h>
78 #include <sys/kernel.h>
79 #include <sys/vnode.h>
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
, OP_SETATTR
, 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
| CTLFLAG_LOCKED
,
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 * Set of locks to keep the interaction between kevents and revoke
264 * from causing havoc.
267 #define LOG2_PTSD_KE_NLCK 2
268 #define PTSD_KE_NLCK (1l << LOG2_PTSD_KE_NLCK)
269 #define PTSD_KE_LOCK_INDEX(x) ((x) & (PTSD_KE_NLCK - 1))
271 static lck_mtx_t ptsd_kevent_lock
[PTSD_KE_NLCK
];
274 ptsd_kevent_lock_init(void)
277 lck_grp_t
*lgrp
= lck_grp_alloc_init("ptsd kevent", LCK_GRP_ATTR_NULL
);
279 for (i
= 0; i
< PTSD_KE_NLCK
; i
++)
280 lck_mtx_init(&ptsd_kevent_lock
[i
], lgrp
, LCK_ATTR_NULL
);
284 ptsd_kevent_mtx_lock(int minor
)
286 lck_mtx_lock(&ptsd_kevent_lock
[PTSD_KE_LOCK_INDEX(minor
)]);
290 ptsd_kevent_mtx_unlock(int minor
)
292 lck_mtx_unlock(&ptsd_kevent_lock
[PTSD_KE_LOCK_INDEX(minor
)]);
296 ptmx_init( __unused
int config_count
)
299 * We start looking at slot 10, since there are inits that will
300 * stomp explicit slots (e.g. vndevice stomps 1) below that.
303 /* Get a major number for /dev/ptmx */
304 if((ptmx_major
= cdevsw_add(-15, &ptmx_cdev
)) == -1) {
305 printf("ptmx_init: failed to obtain /dev/ptmx major number\n");
309 if (cdevsw_setkqueueok(ptmx_major
, &ptmx_cdev
, 0) == -1) {
310 panic("Failed to set flags on ptmx cdevsw entry.");
313 /* Get a major number for /dev/pts/nnn */
314 if ((ptsd_major
= cdevsw_add(-15, &ptsd_cdev
)) == -1) {
315 (void)cdevsw_remove(ptmx_major
, &ptmx_cdev
);
316 printf("ptmx_init: failed to obtain /dev/ptmx major number\n");
320 if (cdevsw_setkqueueok(ptsd_major
, &ptsd_cdev
, 0) == -1) {
321 panic("Failed to set flags on ptmx cdevsw entry.");
325 * Locks to guard against races between revoke and kevents
327 ptsd_kevent_lock_init();
329 /* Create the /dev/ptmx device {<major>,0} */
330 (void)devfs_make_node_clone(makedev(ptmx_major
, 0),
331 DEVFS_CHAR
, UID_ROOT
, GID_TTY
, 0666,
332 ptmx_clone
, PTMX_TEMPLATE
);
337 static struct _ptmx_ioctl_state
{
338 struct ptmx_ioctl
**pis_ioctl_list
; /* pointer vector */
339 int pis_total
; /* total slots */
340 int pis_free
; /* free slots */
342 #define PTMX_GROW_VECTOR 16 /* Grow by this many slots at a time */
345 * Given a minor number, return the corresponding structure for that minor
346 * number. If there isn't one, and the create flag is specified, we create
349 * Parameters: minor Minor number of ptmx device
350 * open_flag PF_OPEN_M First open of master
351 * PF_OPEN_S First open of slave
352 * 0 Just want ioctl struct
354 * Returns: NULL Did not exist/could not create
355 * !NULL structure corresponding minor number
357 * Locks: tty_lock() on ptmx_ioctl->pt_tty NOT held on entry or exit.
359 static struct ptmx_ioctl
*
360 ptmx_get_ioctl(int minor
, int open_flag
)
362 struct ptmx_ioctl
*new_ptmx_ioctl
;
364 if (open_flag
& PF_OPEN_M
) {
367 * If we are about to allocate more memory, but we have
368 * already hit the administrative limit, then fail the
371 * Note: Subtract free from total when making this
372 * check to allow unit increments, rather than
373 * snapping to the nearest PTMX_GROW_VECTOR...
375 if ((_state
.pis_total
- _state
.pis_free
) >= ptmx_max
) {
379 MALLOC(new_ptmx_ioctl
, struct ptmx_ioctl
*, sizeof(struct ptmx_ioctl
), M_TTYS
, M_WAITOK
|M_ZERO
);
380 if (new_ptmx_ioctl
== NULL
) {
384 if ((new_ptmx_ioctl
->pt_tty
= ttymalloc()) == NULL
) {
385 FREE(new_ptmx_ioctl
, M_TTYS
);
390 * Hold the DEVFS_LOCK() over this whole operation; devfs
391 * itself does this over malloc/free as well, so this should
392 * be safe to do. We hold it longer than we want to, but
393 * doing so avoids a reallocation race on the minor number.
396 /* Need to allocate a larger vector? */
397 if (_state
.pis_free
== 0) {
398 struct ptmx_ioctl
**new_pis_ioctl_list
;
399 struct ptmx_ioctl
**old_pis_ioctl_list
= NULL
;
402 MALLOC(new_pis_ioctl_list
, struct ptmx_ioctl
**, sizeof(struct ptmx_ioctl
*) * (_state
.pis_total
+ PTMX_GROW_VECTOR
), M_TTYS
, M_WAITOK
|M_ZERO
);
403 if (new_pis_ioctl_list
== NULL
) {
404 ttyfree(new_ptmx_ioctl
->pt_tty
);
406 FREE(new_ptmx_ioctl
, M_TTYS
);
410 /* If this is not the first time, copy the old over */
411 bcopy(_state
.pis_ioctl_list
, new_pis_ioctl_list
, sizeof(struct ptmx_ioctl
*) * _state
.pis_total
);
412 old_pis_ioctl_list
= _state
.pis_ioctl_list
;
413 _state
.pis_ioctl_list
= new_pis_ioctl_list
;
414 _state
.pis_free
+= PTMX_GROW_VECTOR
;
415 _state
.pis_total
+= PTMX_GROW_VECTOR
;
416 if (old_pis_ioctl_list
)
417 FREE(old_pis_ioctl_list
, M_TTYS
);
420 if (_state
.pis_ioctl_list
[minor
] != NULL
) {
421 ttyfree(new_ptmx_ioctl
->pt_tty
);
423 FREE(new_ptmx_ioctl
, M_TTYS
);
425 /* Special error value so we know to redrive the open, we've been raced */
426 return (struct ptmx_ioctl
*)-1;
430 /* Vector is large enough; grab a new ptmx_ioctl */
432 /* Now grab a free slot... */
433 _state
.pis_ioctl_list
[minor
] = new_ptmx_ioctl
;
435 /* reduce free count */
438 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_M
;
441 /* Create the /dev/ttysXXX device {<major>,XXX} */
442 _state
.pis_ioctl_list
[minor
]->pt_devhandle
= devfs_make_node(
443 makedev(ptsd_major
, minor
),
444 DEVFS_CHAR
, UID_ROOT
, GID_TTY
, 0620,
445 PTSD_TEMPLATE
, minor
);
446 if (_state
.pis_ioctl_list
[minor
]->pt_devhandle
== NULL
) {
447 printf("devfs_make_node() call failed for ptmx_get_ioctl()!!!!\n");
449 } else if (open_flag
& PF_OPEN_S
) {
451 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_S
;
454 return (_state
.pis_ioctl_list
[minor
]);
458 * Locks: tty_lock() of old_ptmx_ioctl->pt_tty NOT held for this call.
461 ptmx_free_ioctl(int minor
, int open_flag
)
463 struct ptmx_ioctl
*old_ptmx_ioctl
= NULL
;
466 _state
.pis_ioctl_list
[minor
]->pt_flags
&= ~(open_flag
);
469 * Was this the last close? We will recognize it because we only get
470 * a notification on the last close of a device, and we will have
471 * cleared both the master and the slave open bits in the flags.
473 if (!(_state
.pis_ioctl_list
[minor
]->pt_flags
& (PF_OPEN_M
|PF_OPEN_S
))) {
474 /* Mark as free so it can be reallocated later */
475 old_ptmx_ioctl
= _state
.pis_ioctl_list
[ minor
];
479 /* Free old after dropping lock */
480 if (old_ptmx_ioctl
!= NULL
) {
482 * XXX See <rdar://5348651> and <rdar://4854638>
484 * XXX Conditional to be removed when/if tty/pty reference
485 * XXX counting and mutex implemented.
487 if (old_ptmx_ioctl
->pt_devhandle
!= NULL
)
488 devfs_remove(old_ptmx_ioctl
->pt_devhandle
);
489 ttyfree(old_ptmx_ioctl
->pt_tty
);
490 FREE(old_ptmx_ioctl
, M_TTYS
);
492 /* Don't remove the entry until the devfs slot is free */
494 _state
.pis_ioctl_list
[ minor
] = NULL
;
499 return (0); /* Success */
506 * Given the dev entry that's being opened, we clone the device. This driver
507 * doesn't actually use the dev entry, since we alreaqdy know who we are by
508 * being called from this code. This routine is a callback registered from
509 * devfs_make_node_clone() in ptmx_init(); it's purpose is to provide a new
510 * minor number, or to return -1, if one can't be provided.
512 * Parameters: dev The device we are cloning from
514 * Returns: >= 0 A new minor device number
515 * -1 Error: ENOMEM ("Can't alloc device")
517 * NOTE: Called with DEVFS_LOCK() held
520 ptmx_clone(__unused dev_t dev
, int action
)
524 if (action
== DEVFS_CLONE_ALLOC
) {
526 if (_state
.pis_total
== 0)
530 * Note: We can add hinting on free slots, if this linear search
531 * ends up being a performance bottleneck...
533 for(i
= 0; i
< _state
.pis_total
; i
++) {
534 if (_state
.pis_ioctl_list
[ i
] == NULL
)
539 * XXX We fall off the end here; if we did this twice at the
540 * XXX same time, we could return the same minor to two
541 * XXX callers; we should probably exand the pointer vector
542 * XXX here, but I need more information on the MALLOC/FREE
543 * XXX locking to ensure against a deadlock. Maybe we can
544 * XXX just high watermark it at 1/2 of PTMX_GROW_VECTOR?
545 * XXX That would require returning &minor as implict return
546 * XXX and an error code ("EAGAIN/ERESTART") or 0 as our
547 * XXX explicit return.
550 return (i
); /* empty slot or next slot */
556 ptsd_open(dev_t dev
, int flag
, __unused
int devtype
, __unused proc_t p
)
559 struct ptmx_ioctl
*pti
;
562 if ((pti
= ptmx_get_ioctl(minor(dev
), 0)) == NULL
) {
566 if (!(pti
->pt_flags
& PF_UNLOCKED
)) {
573 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
574 termioschars(&tp
->t_termios
); /* Set up default chars */
575 tp
->t_iflag
= TTYDEF_IFLAG
;
576 tp
->t_oflag
= TTYDEF_OFLAG
;
577 tp
->t_lflag
= TTYDEF_LFLAG
;
578 tp
->t_cflag
= TTYDEF_CFLAG
;
579 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
580 ttsetwater(tp
); /* would be done in xxparam() */
581 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
585 if (tp
->t_oproc
) /* Ctrlr still around. */
586 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
587 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
590 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
595 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
596 /* Successful open; mark as open by the slave */
597 pti
->pt_flags
|= PF_OPEN_S
;
598 CLR(tp
->t_state
, TS_IOCTL_NOT_OK
);
600 ptmx_wakeup(tp
, FREAD
|FWRITE
);
606 static void ptsd_revoke_knotes(dev_t
, struct tty
*);
609 ptsd_close(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
612 struct ptmx_ioctl
*pti
;
616 * This is temporary until the VSX conformance tests
617 * are fixed. They are hanging with a deadlock
618 * where close(ptsd) will not complete without t_timeout set
620 #define FIX_VSX_HANG 1
624 pti
= ptmx_get_ioctl(minor(dev
), 0);
630 save_timeout
= tp
->t_timeout
;
633 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
634 ptsd_stop(tp
, FREAD
|FWRITE
);
637 tp
->t_timeout
= save_timeout
;
641 if ((flag
& IO_REVOKE
) == IO_REVOKE
)
642 ptsd_revoke_knotes(dev
, tp
);
644 /* unconditional, just like ttyclose() */
645 ptmx_free_ioctl(minor(dev
), PF_OPEN_S
);
651 ptsd_read(dev_t dev
, struct uio
*uio
, int flag
)
653 proc_t p
= current_proc();
656 struct ptmx_ioctl
*pti
;
661 pti
= ptmx_get_ioctl(minor(dev
), 0);
666 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
668 if (pti
->pt_flags
& PF_REMOTE
) {
669 while (isbackground(p
, tp
)) {
670 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
671 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
672 p
->p_lflag
& P_LPPWAIT
) {
677 if (pg
== PGRP_NULL
) {
682 * SAFE: We about to drop the lock ourselves by
683 * SAFE: erroring out or sleeping anyway.
686 if (pg
->pg_jobc
== 0) {
692 pgsignal(pg
, SIGTTIN
, 1);
696 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsd_bg",
701 if (tp
->t_canq
.c_cc
== 0) {
702 if (flag
& IO_NDELAY
) {
706 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
712 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
716 cc
= min(uio_resid(uio
), BUFSIZ
);
717 // Don't copy the very last byte
718 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
719 cc
= q_to_b(&tp
->t_canq
, (u_char
*)buf
, cc
);
720 error
= uiomove(buf
, cc
, uio
);
724 if (tp
->t_canq
.c_cc
== 1)
725 (void) getc(&tp
->t_canq
);
730 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
731 ptmx_wakeup(tp
, FWRITE
);
738 * Write to pseudo-tty.
739 * Wakeups of controlling tty will happen
740 * indirectly, when tty driver calls ptsd_start.
743 ptsd_write(dev_t dev
, struct uio
*uio
, int flag
)
746 struct ptmx_ioctl
*pti
;
749 pti
= ptmx_get_ioctl(minor(dev
), 0);
754 if (tp
->t_oproc
== 0)
757 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
764 * Start output on pseudo-tty.
765 * Wake up process selecting or sleeping for input from controlling tty.
767 * t_oproc for this driver; called from within the line discipline
769 * Locks: Assumes tp is locked on entry, remains locked on exit
772 ptsd_start(struct tty
*tp
)
774 struct ptmx_ioctl
*pti
;
776 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
778 if (tp
->t_state
& TS_TTSTOP
)
780 if (pti
->pt_flags
& PF_STOPPED
) {
781 pti
->pt_flags
&= ~PF_STOPPED
;
782 pti
->pt_send
= TIOCPKT_START
;
784 ptmx_wakeup(tp
, FREAD
);
790 * Locks: Assumes tty_lock() is held over this call.
793 ptmx_wakeup(struct tty
*tp
, int flag
)
795 struct ptmx_ioctl
*pti
;
797 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
800 selwakeup(&pti
->pt_selr
);
801 wakeup(TSA_PTC_READ(tp
));
804 selwakeup(&pti
->pt_selw
);
805 wakeup(TSA_PTC_WRITE(tp
));
810 ptmx_open(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
813 struct ptmx_ioctl
*pti
;
816 pti
= ptmx_get_ioctl(minor(dev
), PF_OPEN_M
);
819 } else if (pti
== (struct ptmx_ioctl
*)-1) {
820 return (EREDRIVEOPEN
);
826 /* If master is open OR slave is still draining, pty is still busy */
827 if (tp
->t_oproc
|| (tp
->t_state
& TS_ISOPEN
)) {
830 * If master is closed, we are the only reference, so we
831 * need to clear the master open bit
834 ptmx_free_ioctl(minor(dev
), PF_OPEN_M
);
838 tp
->t_oproc
= ptsd_start
;
839 CLR(tp
->t_state
, TS_ZOMBIE
);
840 SET(tp
->t_state
, TS_IOCTL_NOT_OK
);
842 tp
->t_stop
= ptsd_stop
;
844 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
845 tp
->t_lflag
&= ~EXTPROC
;
853 ptmx_close(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
856 struct ptmx_ioctl
*pti
;
858 pti
= ptmx_get_ioctl(minor(dev
), 0);
863 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
866 * XXX MDMBUF makes no sense for ptys but would inhibit the above
867 * l_modem(). CLOCAL makes sense but isn't supported. Special
868 * l_modem()s that ignore carrier drop make no sense for ptys but
869 * may be in use because other parts of the line discipline make
870 * sense for ptys. Recover by doing everything that a normal
871 * ttymodem() would have done except for sending a SIGHUP.
873 if (tp
->t_state
& TS_ISOPEN
) {
874 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
875 tp
->t_state
|= TS_ZOMBIE
;
876 ttyflush(tp
, FREAD
| FWRITE
);
879 tp
->t_oproc
= 0; /* mark closed */
883 ptmx_free_ioctl(minor(dev
), PF_OPEN_M
);
889 ptmx_read(dev_t dev
, struct uio
*uio
, int flag
)
892 struct ptmx_ioctl
*pti
;
896 pti
= ptmx_get_ioctl(minor(dev
), 0);
902 * We want to block until the slave
903 * is open, and there's something to read;
904 * but if we lost the slave or we're NBIO,
905 * then return the appropriate error instead.
908 if (tp
->t_state
&TS_ISOPEN
) {
909 if (pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) {
910 error
= ureadc((int)pti
->pt_send
, uio
);
913 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
914 cc
= min(uio_resid(uio
),
915 sizeof(tp
->t_termios
));
916 uiomove((caddr_t
)&tp
->t_termios
, cc
,
922 if (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
) {
923 error
= ureadc((int)pti
->pt_ucntl
, uio
);
929 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
932 if ((tp
->t_state
& TS_CONNECTED
) == 0)
934 if (flag
& IO_NDELAY
) {
938 error
= ttysleep(tp
, TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptmx_in", 0);
942 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
943 error
= ureadc(0, uio
);
944 while (uio_resid(uio
) > 0 && error
== 0) {
945 cc
= q_to_b(&tp
->t_outq
, (u_char
*)buf
, min(uio_resid(uio
), BUFSIZ
));
948 error
= uiomove(buf
, cc
, uio
);
950 (*linesw
[tp
->t_line
].l_start
)(tp
);
958 * Line discipline callback
960 * Locks: tty_lock() is assumed held on entry and exit.
963 ptsd_stop(struct tty
*tp
, int flush
)
965 struct ptmx_ioctl
*pti
;
968 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
970 /* note: FLUSHREAD and FLUSHWRITE already ok */
972 flush
= TIOCPKT_STOP
;
973 pti
->pt_flags
|= PF_STOPPED
;
975 pti
->pt_flags
&= ~PF_STOPPED
;
976 pti
->pt_send
|= flush
;
977 /* change of perspective */
983 ptmx_wakeup(tp
, flag
);
989 ptsd_reset(__unused
int uban
)
995 * Reinput pending characters after state switch
998 * XXX Code duplication: static function, should be inlined
1001 ttypend(struct tty
*tp
)
1006 CLR(tp
->t_lflag
, PENDIN
);
1007 SET(tp
->t_state
, TS_TYPEN
);
1009 tp
->t_rawq
.c_cc
= 0;
1010 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
1011 while ((c
= getc(&tq
)) >= 0)
1013 CLR(tp
->t_state
, TS_TYPEN
);
1017 * Must be called at spltty().
1019 * XXX Code duplication: static function, should be inlined
1022 ttnread(struct tty
*tp
)
1026 if (ISSET(tp
->t_lflag
, PENDIN
))
1028 nread
= tp
->t_canq
.c_cc
;
1029 if (!ISSET(tp
->t_lflag
, ICANON
)) {
1030 nread
+= tp
->t_rawq
.c_cc
;
1031 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
1038 ptsd_select(dev_t dev
, int rw
, void *wql
, proc_t p
)
1040 struct ptmx_ioctl
*pti
;
1044 pti
= ptmx_get_ioctl(minor(dev
), 0);
1055 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1060 retval
= ttnread(tp
);
1065 selrecord(p
, &tp
->t_rsel
, wql
);
1068 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1073 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1074 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1075 retval
= tp
->t_hiwat
- tp
->t_outq
.c_cc
;
1079 selrecord(p
, &tp
->t_wsel
, wql
);
1088 ptmx_select(dev_t dev
, int rw
, void *wql
, proc_t p
)
1091 struct ptmx_ioctl
*pti
;
1094 pti
= ptmx_get_ioctl(minor(dev
), 0);
1099 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1106 * Need to block timeouts (ttrstart).
1108 if ((tp
->t_state
&TS_ISOPEN
) &&
1109 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
1110 retval
= tp
->t_outq
.c_cc
;
1115 case 0: /* exceptional */
1116 if ((tp
->t_state
&TS_ISOPEN
) &&
1117 ((pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) ||
1118 (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
))) {
1122 selrecord(p
, &pti
->pt_selr
, wql
);
1126 if (tp
->t_state
&TS_ISOPEN
) {
1127 if (pti
->pt_flags
& PF_REMOTE
) {
1128 if (tp
->t_canq
.c_cc
== 0) {
1129 retval
= (TTYHOG
-1) ;
1133 retval
= (TTYHOG
- 2) - (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
);
1137 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
&ICANON
)) {
1144 selrecord(p
, &pti
->pt_selw
, wql
);
1154 ptmx_stop(__unused
struct tty
*tp
, __unused
int flush
)
1160 ptmx_reset(__unused
int uban
)
1166 ptmx_write(dev_t dev
, struct uio
*uio
, int flag
)
1169 struct ptmx_ioctl
*pti
;
1172 u_char locbuf
[BUFSIZ
];
1176 pti
= ptmx_get_ioctl(minor(dev
), 0);
1182 if ((tp
->t_state
&TS_ISOPEN
) == 0)
1184 if (pti
->pt_flags
& PF_REMOTE
) {
1185 if (tp
->t_canq
.c_cc
)
1187 while ((uio_resid(uio
) > 0 || cc
> 0) &&
1188 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
1190 cc
= min(uio_resid(uio
), BUFSIZ
);
1191 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
1193 error
= uiomove((caddr_t
)cp
, cc
, uio
);
1196 /* check again for safety */
1197 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1198 /* adjust as usual */
1199 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1205 cc
= b_to_q((u_char
*)cp
, cc
, &tp
->t_canq
);
1207 * XXX we don't guarantee that the canq size
1208 * is >= TTYHOG, so the above b_to_q() may
1209 * leave some bytes uncopied. However, space
1210 * is guaranteed for the null terminator if
1211 * we don't fail here since (TTYHOG - 1) is
1212 * not a multiple of CBSIZE.
1218 /* adjust for data copied in but not written */
1219 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1220 (void) putc(0, &tp
->t_canq
);
1222 wakeup(TSA_PTS_READ(tp
));
1225 while (uio_resid(uio
) > 0 || cc
> 0) {
1227 cc
= min(uio_resid(uio
), BUFSIZ
);
1229 error
= uiomove((caddr_t
)cp
, cc
, uio
);
1232 /* check again for safety */
1233 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1234 /* adjust for data copied in but not written */
1235 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1241 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
1242 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
1243 wakeup(TSA_HUP_OR_INPUT(tp
));
1246 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1259 * Come here to wait for slave to open, for space
1260 * in outq, or space in rawq, or an empty canq.
1262 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1263 /* adjust for data copied in but not written */
1264 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1268 if (flag
& IO_NDELAY
) {
1269 /* adjust for data copied in but not written */
1270 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1272 error
= EWOULDBLOCK
;
1275 error
= ttysleep(tp
, TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptmx_out", 0);
1277 /* adjust for data copied in but not written */
1278 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1286 cptyioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1289 struct ptmx_ioctl
*pti
;
1291 int stop
, error
= 0;
1292 int allow_ext_ioctl
= 1;
1294 pti
= ptmx_get_ioctl(minor(dev
), 0);
1302 * Do not permit extended ioctls on the master side of the pty unless
1303 * the slave side has been successfully opened and initialized.
1305 if (cdevsw
[major(dev
)].d_open
== ptmx_open
&& ISSET(tp
->t_state
, TS_IOCTL_NOT_OK
))
1306 allow_ext_ioctl
= 0;
1309 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1310 * ttywflush(tp) will hang if there are characters in the outq.
1312 if (cmd
== TIOCEXT
&& allow_ext_ioctl
) {
1314 * When the EXTPROC bit is being toggled, we need
1315 * to send an TIOCPKT_IOCTL if the packet driver
1319 if (pti
->pt_flags
& PF_PKT
) {
1320 pti
->pt_send
|= TIOCPKT_IOCTL
;
1321 ptmx_wakeup(tp
, FREAD
);
1323 tp
->t_lflag
|= EXTPROC
;
1325 if ((tp
->t_lflag
& EXTPROC
) &&
1326 (pti
->pt_flags
& PF_PKT
)) {
1327 pti
->pt_send
|= TIOCPKT_IOCTL
;
1328 ptmx_wakeup(tp
, FREAD
);
1330 tp
->t_lflag
&= ~EXTPROC
;
1334 if (cdevsw
[major(dev
)].d_open
== ptmx_open
) {
1339 * We aviod calling ttioctl on the controller since,
1340 * in that case, tp must be the controlling terminal.
1342 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1347 if (pti
->pt_flags
& PF_UCNTL
) {
1351 pti
->pt_flags
|= PF_PKT
;
1353 pti
->pt_flags
&= ~PF_PKT
;
1358 if (pti
->pt_flags
& PF_PKT
) {
1362 pti
->pt_flags
|= PF_UCNTL
;
1364 pti
->pt_flags
&= ~PF_UCNTL
;
1369 pti
->pt_flags
|= PF_REMOTE
;
1371 pti
->pt_flags
&= ~PF_REMOTE
;
1372 ttyflush(tp
, FREAD
|FWRITE
);
1384 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1388 if (*(unsigned int *)data
>= NSIG
||
1389 *(unsigned int *)data
== 0) {
1393 if ((tp
->t_lflag
&NOFLSH
) == 0)
1394 ttyflush(tp
, FREAD
|FWRITE
);
1395 if ((*(unsigned int *)data
== SIGINFO
) &&
1396 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1399 * SAFE: All callers drop the lock on return and
1400 * SAFE: the linesw[] will short circut this call
1401 * SAFE: if the ioctl() is eaten before the lower
1402 * SAFE: level code gets to see it.
1405 tty_pgsignal(tp
, *(unsigned int *)data
, 1);
1409 case TIOCPTYGRANT
: /* grantpt(3) */
1411 * Change the uid of the slave to that of the calling
1412 * thread, change the gid of the slave to GID_TTY,
1413 * change the mode to 0620 (rw--w----).
1416 error
= _devfs_setattr(pti
->pt_devhandle
, 0620, kauth_getuid(), GID_TTY
);
1420 case TIOCPTYGNAME
: /* ptsname(3) */
1422 * Report the name of the slave device in *data
1423 * (128 bytes max.). Use the same template string
1424 * used for calling devfs_make_node() to create it.
1426 snprintf(data
, 128, "/dev/" PTSD_TEMPLATE
, minor(dev
));
1430 case TIOCPTYUNLK
: /* unlockpt(3) */
1432 * Unlock the slave device so that it can be opened.
1434 pti
->pt_flags
|= PF_UNLOCKED
;
1440 * Fail all other calls; pty masters are not serial devices;
1441 * we only pretend they are when the slave side of the pty is
1444 if (!allow_ext_ioctl
) {
1449 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
1450 if (error
== ENOTTY
) {
1451 error
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
1452 if (error
== ENOTTY
) {
1453 if (pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
1454 /* Process the UIOCMD ioctl group */
1456 pti
->pt_ucntl
= (u_char
)cmd
;
1457 ptmx_wakeup(tp
, FREAD
);
1461 } else if (cmd
== TIOCSBRK
|| cmd
== TIOCCBRK
) {
1463 * POSIX conformance; rdar://3936338
1465 * Clear ENOTTY in the case of setting or
1466 * clearing a break failing because pty's
1467 * don't support break like real serial
1477 * If external processing and packet mode send ioctl packet.
1479 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1494 pti
->pt_send
|= TIOCPKT_IOCTL
;
1495 ptmx_wakeup(tp
, FREAD
);
1500 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1501 && CCEQ(cc
[VSTART
], CTRL('q'));
1502 if (pti
->pt_flags
& PF_NOSTOP
) {
1504 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1505 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1506 pti
->pt_flags
&= ~PF_NOSTOP
;
1507 ptmx_wakeup(tp
, FREAD
);
1511 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1512 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1513 pti
->pt_flags
|= PF_NOSTOP
;
1514 ptmx_wakeup(tp
, FREAD
);
1525 int ptsd_kqfilter(dev_t
, struct knote
*);
1526 static void ptsd_kqops_detach(struct knote
*);
1527 static int ptsd_kqops_event(struct knote
*, long);
1529 static struct filterops ptsd_kqops
= {
1531 .f_detach
= ptsd_kqops_detach
,
1532 .f_event
= ptsd_kqops_event
,
1535 #define PTSD_KNOTE_VALID NULL
1536 #define PTSD_KNOTE_REVOKED ((void *)-911l)
1539 * In the normal case, by the time the driver_close() routine is called
1540 * on the slave, all knotes have been detached. However in the revoke(2)
1541 * case, the driver's close routine is called while there are knotes active
1542 * that reference the handlers below. And we have no obvious means to
1543 * reach from the driver out to the kqueue's that reference them to get
1548 ptsd_kqops_detach(struct knote
*kn
)
1550 struct ptmx_ioctl
*pti
;
1552 dev_t dev
, lockdev
= (dev_t
)kn
->kn_hookid
;
1554 ptsd_kevent_mtx_lock(minor(lockdev
));
1556 if ((dev
= (dev_t
)kn
->kn_hookid
) != 0) {
1557 pti
= ptmx_get_ioctl(minor(dev
), 0);
1558 if (pti
!= NULL
&& (tp
= pti
->pt_tty
) != NULL
) {
1560 if (kn
->kn_filter
== EVFILT_READ
)
1561 KNOTE_DETACH(&tp
->t_rsel
.si_note
, kn
);
1563 KNOTE_DETACH(&tp
->t_wsel
.si_note
, kn
);
1569 ptsd_kevent_mtx_unlock(minor(lockdev
));
1573 ptsd_kqops_event(struct knote
*kn
, long hint
)
1575 struct ptmx_ioctl
*pti
;
1577 dev_t dev
= (dev_t
)kn
->kn_hookid
;
1580 ptsd_kevent_mtx_lock(minor(dev
));
1583 if (kn
->kn_hook
!= PTSD_KNOTE_VALID
) {
1584 /* We were revoked */
1586 kn
->kn_flags
|= EV_EOF
;
1591 pti
= ptmx_get_ioctl(minor(dev
), 0);
1592 if (pti
== NULL
|| (tp
= pti
->pt_tty
) == NULL
) {
1593 kn
->kn_data
= ENXIO
;
1594 kn
->kn_flags
|= EV_ERROR
;
1602 if (kn
->kn_filter
== EVFILT_READ
) {
1603 kn
->kn_data
= ttnread(tp
);
1604 if (kn
->kn_data
> 0)
1606 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1607 kn
->kn_flags
|= EV_EOF
;
1610 } else { /* EVFILT_WRITE */
1611 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1612 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1613 kn
->kn_data
= tp
->t_outq
.c_cn
- tp
->t_outq
.c_cc
;
1616 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1617 kn
->kn_flags
|= EV_EOF
;
1626 ptsd_kevent_mtx_unlock(minor(dev
));
1631 ptsd_kqfilter(dev_t dev
, struct knote
*kn
)
1633 struct tty
*tp
= NULL
;
1634 struct ptmx_ioctl
*pti
= NULL
;
1637 /* make sure we're talking about the right device type */
1638 if (cdevsw
[major(dev
)].d_open
!= ptsd_open
) {
1642 if ((pti
= ptmx_get_ioctl(minor(dev
), 0)) == NULL
) {
1649 kn
->kn_hookid
= dev
;
1650 kn
->kn_hook
= PTSD_KNOTE_VALID
;
1651 kn
->kn_fop
= &ptsd_kqops
;
1653 switch (kn
->kn_filter
) {
1655 KNOTE_ATTACH(&tp
->t_rsel
.si_note
, kn
);
1658 KNOTE_ATTACH(&tp
->t_wsel
.si_note
, kn
);
1670 * Support for revoke(2).
1672 * Mark all the kn_hook fields so that future invocations of the
1673 * f_event op will just say "EOF" *without* looking at the
1674 * ptmx_ioctl structure (which may disappear or be recycled at
1675 * the end of ptsd_close). Issue wakeups to post that EOF to
1676 * anyone listening. And finally remove the knotes from the
1677 * tty's klists to keep ttyclose() happy, and set the hookid to
1678 * zero to make the final detach passively successful.
1681 ptsd_revoke_knotes(dev_t dev
, struct tty
*tp
)
1684 struct knote
*kn
, *tkn
;
1686 /* (Hold and drop the right locks in the right order.) */
1688 ptsd_kevent_mtx_lock(minor(dev
));
1691 list
= &tp
->t_rsel
.si_note
;
1692 SLIST_FOREACH(kn
, list
, kn_selnext
)
1693 kn
->kn_hook
= PTSD_KNOTE_REVOKED
;
1695 list
= &tp
->t_wsel
.si_note
;
1696 SLIST_FOREACH(kn
, list
, kn_selnext
)
1697 kn
->kn_hook
= PTSD_KNOTE_REVOKED
;
1700 ptsd_kevent_mtx_unlock(minor(dev
));
1707 ptsd_kevent_mtx_lock(minor(dev
));
1710 list
= &tp
->t_rsel
.si_note
;
1711 SLIST_FOREACH_SAFE(kn
, list
, kn_selnext
, tkn
) {
1712 (void) KNOTE_DETACH(list
, kn
);
1716 list
= &tp
->t_wsel
.si_note
;
1717 SLIST_FOREACH_SAFE(kn
, list
, kn_selnext
, tkn
) {
1718 (void) KNOTE_DETACH(list
, kn
);
1723 ptsd_kevent_mtx_unlock(minor(dev
));