]>
git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty_ptmx.c
aa583d857b7eb29f8371fabbd3d9ccf6dd1764c6
2 * Copyright (c) 1997-2013 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
29 * Copyright (c) 1982, 1986, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
35 * 1. Redistributions of source code must retain the above copyright
36 * notice, this list of conditions and the following disclaimer.
37 * 2. Redistributions in binary form must reproduce the above copyright
38 * notice, this list of conditions and the following disclaimer in the
39 * documentation and/or other materials provided with the distribution.
40 * 3. All advertising materials mentioning features or use of this software
41 * must display the following acknowledgement:
42 * This product includes software developed by the University of
43 * California, Berkeley and its contributors.
44 * 4. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
60 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
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>
81 #include <sys/signalvar.h>
82 #include <sys/sysctl.h>
83 #include <miscfs/devfs/devfs.h>
84 #include <miscfs/devfs/devfsdefs.h> /* DEVFS_LOCK()/DEVFS_UNLOCK() */
85 #include <libkern/section_keywords.h>
88 #include <security/mac_framework.h>
94 * Forward declarations
96 int ptmx_init(int n_ptys
);
97 static struct ptmx_ioctl
*ptmx_get_ioctl(int minor
, int open_flag
);
98 static int ptmx_free_ioctl(int minor
, int open_flag
);
99 static int ptmx_get_name(int minor
, char *buffer
, size_t size
);
100 static void ptsd_revoke_knotes(int minor
, struct tty
*tp
);
102 extern d_open_t ptsopen
;
103 extern d_close_t ptsclose
;
104 extern d_read_t ptsread
;
105 extern d_write_t ptswrite
;
106 extern d_ioctl_t ptyioctl
;
107 extern d_stop_t ptsstop
;
108 extern d_reset_t ptsreset
;
109 extern d_select_t ptsselect
;
111 extern d_open_t ptcopen
;
112 extern d_close_t ptcclose
;
113 extern d_read_t ptcread
;
114 extern d_write_t ptcwrite
;
115 extern d_stop_t ptcstop
;
116 extern d_reset_t ptcreset
;
117 extern d_select_t ptcselect
;
119 static int ptmx_major
; /* dynamically assigned major number */
120 static struct cdevsw ptmx_cdev
= {
121 ptcopen
, ptcclose
, ptcread
, ptcwrite
,
122 ptyioctl
, ptcstop
, ptcreset
, 0,
123 ptcselect
, eno_mmap
, eno_strat
, eno_getc
,
127 static int ptsd_major
; /* dynamically assigned major number */
128 static struct cdevsw ptsd_cdev
= {
129 ptsopen
, ptsclose
, ptsread
, ptswrite
,
130 ptyioctl
, ptsstop
, ptsreset
, 0,
131 ptsselect
, eno_mmap
, eno_strat
, eno_getc
,
137 * ptsd == /dev/pts[0123456789]{3}
139 #define PTMX_TEMPLATE "ptmx"
140 #define PTSD_TEMPLATE "ttys%03d"
143 * System-wide limit on the max number of cloned ptys
145 #define PTMX_MAX_DEFAULT 511 /* 512 entries */
146 #define PTMX_MAX_HARD 999 /* 1000 entries, due to PTSD_TEMPLATE */
148 static int ptmx_max
= PTMX_MAX_DEFAULT
; /* default # of clones we allow */
150 /* Range enforcement for the sysctl */
152 sysctl_ptmx_max(__unused
struct sysctl_oid
*oidp
, __unused
void *arg1
,
153 __unused
int arg2
, struct sysctl_req
*req
)
155 int new_value
, changed
;
156 int error
= sysctl_io_number(req
, ptmx_max
, sizeof(int), &new_value
, &changed
);
158 if (new_value
> 0 && new_value
<= PTMX_MAX_HARD
)
159 ptmx_max
= new_value
;
166 SYSCTL_NODE(_kern
, KERN_TTY
, tty
, CTLFLAG_RW
|CTLFLAG_LOCKED
, 0, "TTY");
167 SYSCTL_PROC(_kern_tty
, OID_AUTO
, ptmx_max
,
168 CTLTYPE_INT
| CTLFLAG_RW
| CTLFLAG_LOCKED
,
169 &ptmx_max
, 0, &sysctl_ptmx_max
, "I", "ptmx_max");
171 static int ptmx_clone(dev_t dev
, int minor
);
173 static struct tty_dev_t _ptmx_driver
;
176 ptmx_init( __unused
int config_count
)
179 * We start looking at slot 10, since there are inits that will
180 * stomp explicit slots (e.g. vndevice stomps 1) below that.
183 /* Get a major number for /dev/ptmx */
184 if ((ptmx_major
= cdevsw_add(-15, &ptmx_cdev
)) == -1) {
185 printf("ptmx_init: failed to obtain /dev/ptmx major number\n");
189 if (cdevsw_setkqueueok(ptmx_major
, &ptmx_cdev
, CDEVSW_IS_PTC
) == -1) {
190 panic("Failed to set flags on ptmx cdevsw entry.");
193 /* Get a major number for /dev/pts/nnn */
194 if ((ptsd_major
= cdevsw_add(-15, &ptsd_cdev
)) == -1) {
195 (void)cdevsw_remove(ptmx_major
, &ptmx_cdev
);
196 printf("ptmx_init: failed to obtain /dev/ptmx major number\n");
200 if (cdevsw_setkqueueok(ptsd_major
, &ptsd_cdev
, CDEVSW_IS_PTS
) == -1) {
201 panic("Failed to set flags on ptmx cdevsw entry.");
204 /* Create the /dev/ptmx device {<major>,0} */
205 (void)devfs_make_node_clone(makedev(ptmx_major
, 0),
206 DEVFS_CHAR
, UID_ROOT
, GID_TTY
, 0666,
207 ptmx_clone
, PTMX_TEMPLATE
);
209 _ptmx_driver
.master
= ptmx_major
;
210 _ptmx_driver
.slave
= ptsd_major
;
211 _ptmx_driver
.fix_7828447
= 1;
212 _ptmx_driver
.fix_7070978
= 1;
214 _ptmx_driver
.mac_notify
= 1;
216 _ptmx_driver
.open
= &ptmx_get_ioctl
;
217 _ptmx_driver
.free
= &ptmx_free_ioctl
;
218 _ptmx_driver
.name
= &ptmx_get_name
;
219 _ptmx_driver
.revoke
= &ptsd_revoke_knotes
;
220 tty_dev_register(&_ptmx_driver
);
226 static struct _ptmx_ioctl_state
{
227 struct ptmx_ioctl
**pis_ioctl_list
; /* pointer vector */
228 int pis_total
; /* total slots */
229 int pis_free
; /* free slots */
231 #define PTMX_GROW_VECTOR 16 /* Grow by this many slots at a time */
234 * Given a minor number, return the corresponding structure for that minor
235 * number. If there isn't one, and the create flag is specified, we create
238 * Parameters: minor Minor number of ptmx device
239 * open_flag PF_OPEN_M First open of master
240 * PF_OPEN_S First open of slave
241 * 0 Just want ioctl struct
243 * Returns: NULL Did not exist/could not create
244 * !NULL structure corresponding minor number
246 * Locks: tty_lock() on ptmx_ioctl->pt_tty NOT held on entry or exit.
248 static struct ptmx_ioctl
*
249 ptmx_get_ioctl(int minor
, int open_flag
)
251 struct ptmx_ioctl
*new_ptmx_ioctl
;
253 if (open_flag
& PF_OPEN_M
) {
256 * If we are about to allocate more memory, but we have
257 * already hit the administrative limit, then fail the
260 * Note: Subtract free from total when making this
261 * check to allow unit increments, rather than
262 * snapping to the nearest PTMX_GROW_VECTOR...
264 if ((_state
.pis_total
- _state
.pis_free
) >= ptmx_max
) {
268 MALLOC(new_ptmx_ioctl
, struct ptmx_ioctl
*, sizeof(struct ptmx_ioctl
), M_TTYS
, M_WAITOK
|M_ZERO
);
269 if (new_ptmx_ioctl
== NULL
) {
273 if ((new_ptmx_ioctl
->pt_tty
= ttymalloc()) == NULL
) {
274 FREE(new_ptmx_ioctl
, M_TTYS
);
279 * Hold the DEVFS_LOCK() over this whole operation; devfs
280 * itself does this over malloc/free as well, so this should
281 * be safe to do. We hold it longer than we want to, but
282 * doing so avoids a reallocation race on the minor number.
285 /* Need to allocate a larger vector? */
286 if (_state
.pis_free
== 0) {
287 struct ptmx_ioctl
**new_pis_ioctl_list
;
288 struct ptmx_ioctl
**old_pis_ioctl_list
= NULL
;
291 MALLOC(new_pis_ioctl_list
, struct ptmx_ioctl
**, sizeof(struct ptmx_ioctl
*) * (_state
.pis_total
+ PTMX_GROW_VECTOR
), M_TTYS
, M_WAITOK
|M_ZERO
);
292 if (new_pis_ioctl_list
== NULL
) {
293 ttyfree(new_ptmx_ioctl
->pt_tty
);
295 FREE(new_ptmx_ioctl
, M_TTYS
);
299 /* If this is not the first time, copy the old over */
300 bcopy(_state
.pis_ioctl_list
, new_pis_ioctl_list
, sizeof(struct ptmx_ioctl
*) * _state
.pis_total
);
301 old_pis_ioctl_list
= _state
.pis_ioctl_list
;
302 _state
.pis_ioctl_list
= new_pis_ioctl_list
;
303 _state
.pis_free
+= PTMX_GROW_VECTOR
;
304 _state
.pis_total
+= PTMX_GROW_VECTOR
;
305 if (old_pis_ioctl_list
)
306 FREE(old_pis_ioctl_list
, M_TTYS
);
309 /* is minor in range now? */
310 if (minor
< 0 || minor
>= _state
.pis_total
) {
311 ttyfree(new_ptmx_ioctl
->pt_tty
);
313 FREE(new_ptmx_ioctl
, M_TTYS
);
317 if (_state
.pis_ioctl_list
[minor
] != NULL
) {
318 ttyfree(new_ptmx_ioctl
->pt_tty
);
320 FREE(new_ptmx_ioctl
, M_TTYS
);
322 /* Special error value so we know to redrive the open, we've been raced */
323 return (struct ptmx_ioctl
*)-1;
327 /* Vector is large enough; grab a new ptmx_ioctl */
329 /* Now grab a free slot... */
330 _state
.pis_ioctl_list
[minor
] = new_ptmx_ioctl
;
332 /* reduce free count */
335 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_M
;
338 /* Create the /dev/ttysXXX device {<major>,XXX} */
339 _state
.pis_ioctl_list
[minor
]->pt_devhandle
= devfs_make_node(
340 makedev(ptsd_major
, minor
),
341 DEVFS_CHAR
, UID_ROOT
, GID_TTY
, 0620,
342 PTSD_TEMPLATE
, minor
);
343 if (_state
.pis_ioctl_list
[minor
]->pt_devhandle
== NULL
) {
344 printf("devfs_make_node() call failed for ptmx_get_ioctl()!!!!\n");
348 if (minor
< 0 || minor
>= _state
.pis_total
) {
352 return (_state
.pis_ioctl_list
[minor
]);
356 * Locks: tty_lock() of old_ptmx_ioctl->pt_tty NOT held for this call.
359 ptmx_free_ioctl(int minor
, int open_flag
)
361 struct ptmx_ioctl
*old_ptmx_ioctl
= NULL
;
365 if (minor
< 0 || minor
>= _state
.pis_total
) {
370 _state
.pis_ioctl_list
[minor
]->pt_flags
&= ~(open_flag
);
373 * Was this the last close? We will recognize it because we only get
374 * a notification on the last close of a device, and we will have
375 * cleared both the master and the slave open bits in the flags.
377 if (!(_state
.pis_ioctl_list
[minor
]->pt_flags
& (PF_OPEN_M
|PF_OPEN_S
))) {
378 /* Mark as free so it can be reallocated later */
379 old_ptmx_ioctl
= _state
.pis_ioctl_list
[ minor
];
380 _state
.pis_ioctl_list
[minor
] = NULL
;
385 /* Free old after dropping lock */
386 if (old_ptmx_ioctl
!= NULL
) {
388 * XXX See <rdar://5348651> and <rdar://4854638>
390 * XXX Conditional to be removed when/if tty/pty reference
391 * XXX counting and mutex implemented.
393 if (old_ptmx_ioctl
->pt_devhandle
!= NULL
)
394 devfs_remove(old_ptmx_ioctl
->pt_devhandle
);
395 ttyfree(old_ptmx_ioctl
->pt_tty
);
396 FREE(old_ptmx_ioctl
, M_TTYS
);
399 return (0); /* Success */
403 ptmx_get_name(int minor
, char *buffer
, size_t size
)
405 return snprintf(buffer
, size
, "/dev/" PTSD_TEMPLATE
, minor
);
411 * Given the dev entry that's being opened, we clone the device. This driver
412 * doesn't actually use the dev entry, since we alreaqdy know who we are by
413 * being called from this code. This routine is a callback registered from
414 * devfs_make_node_clone() in ptmx_init(); it's purpose is to provide a new
415 * minor number, or to return -1, if one can't be provided.
417 * Parameters: dev The device we are cloning from
419 * Returns: >= 0 A new minor device number
420 * -1 Error: ENOMEM ("Can't alloc device")
422 * NOTE: Called with DEVFS_LOCK() held
425 ptmx_clone(__unused dev_t dev
, int action
)
429 if (action
== DEVFS_CLONE_ALLOC
) {
431 if (_state
.pis_total
== 0)
435 * Note: We can add hinting on free slots, if this linear search
436 * ends up being a performance bottleneck...
438 for(i
= 0; i
< _state
.pis_total
; i
++) {
439 if (_state
.pis_ioctl_list
[ i
] == NULL
)
444 * XXX We fall off the end here; if we did this twice at the
445 * XXX same time, we could return the same minor to two
446 * XXX callers; we should probably exand the pointer vector
447 * XXX here, but I need more information on the MALLOC/FREE
448 * XXX locking to ensure against a deadlock. Maybe we can
449 * XXX just high watermark it at 1/2 of PTMX_GROW_VECTOR?
450 * XXX That would require returning &minor as implict return
451 * XXX and an error code ("EAGAIN/ERESTART") or 0 as our
452 * XXX explicit return.
455 return (i
); /* empty slot or next slot */
464 int ptsd_kqfilter(dev_t dev
, struct knote
*kn
);
465 static void ptsd_kqops_detach(struct knote
*);
466 static int ptsd_kqops_event(struct knote
*, long);
467 static int ptsd_kqops_touch(struct knote
*kn
, struct kevent_internal_s
*kev
);
468 static int ptsd_kqops_process(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
470 SECURITY_READ_ONLY_EARLY(struct filterops
) ptsd_kqops
= {
472 /* attach is handled by ptsd_kqfilter -- the dev node must be passed in */
473 .f_detach
= ptsd_kqops_detach
,
474 .f_event
= ptsd_kqops_event
,
475 .f_touch
= ptsd_kqops_touch
,
476 .f_process
= ptsd_kqops_process
,
480 * In the normal case, by the time the driver_close() routine is called
481 * on the slave, all knotes have been detached. However in the revoke(2)
482 * case, the driver's close routine is called while there are knotes active
483 * that reference the handlers below. And we have no obvious means to
484 * reach from the driver out to the kqueue's that reference them to get
489 ptsd_kqops_detach(struct knote
*kn
)
499 * Only detach knotes from open ttys -- ttyclose detaches all knotes
500 * under the lock and unsets TS_ISOPEN.
502 if (tp
->t_state
& TS_ISOPEN
) {
503 switch (kn
->kn_filter
) {
505 KNOTE_DETACH(&tp
->t_rsel
.si_note
, kn
);
509 KNOTE_DETACH(&tp
->t_wsel
.si_note
, kn
);
513 panic("invalid knote %p detach, filter: %d", kn
, kn
->kn_filter
);
525 ptsd_kqops_common(struct knote
*kn
, struct tty
*tp
)
531 switch (kn
->kn_filter
) {
533 kn
->kn_data
= ttnread(tp
);
534 if (kn
->kn_data
> 0) {
540 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
541 (tp
->t_state
& TS_CONNECTED
)) {
542 kn
->kn_data
= tp
->t_outq
.c_cn
- tp
->t_outq
.c_cc
;
548 panic("ptsd kevent: unexpected filter: %d, kn = %p, tty = %p",
549 kn
->kn_filter
, kn
, tp
);
553 if (tp
->t_state
& TS_ZOMBIE
) {
554 kn
->kn_flags
|= EV_EOF
;
562 ptsd_kqops_event(struct knote
*kn
, long hint
)
564 struct tty
*tp
= kn
->kn_hook
;
566 bool revoked
= hint
& NOTE_REVOKE
;
567 hint
&= ~NOTE_REVOKE
;
574 kn
->kn_flags
|= EV_EOF
| EV_ONESHOT
;
577 ret
= ptsd_kqops_common(kn
, tp
);
588 ptsd_kqops_touch(struct knote
*kn
, struct kevent_internal_s
*kev
)
597 /* accept new kevent state */
598 kn
->kn_sfflags
= kev
->fflags
;
599 kn
->kn_sdata
= kev
->data
;
600 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0) {
601 kn
->kn_udata
= kev
->udata
;
604 /* recapture fired state of knote */
605 ret
= ptsd_kqops_common(kn
, tp
);
613 ptsd_kqops_process(struct knote
*kn
, __unused
struct filt_process_s
*data
,
614 struct kevent_internal_s
*kev
)
616 struct tty
*tp
= kn
->kn_hook
;
620 ret
= ptsd_kqops_common(kn
, tp
);
622 *kev
= kn
->kn_kevent
;
623 if (kn
->kn_flags
& EV_CLEAR
) {
634 ptsd_kqfilter(dev_t dev
, struct knote
*kn
)
636 struct tty
*tp
= NULL
;
637 struct ptmx_ioctl
*pti
= NULL
;
640 /* make sure we're talking about the right device type */
641 if (cdevsw
[major(dev
)].d_open
!= ptsopen
) {
642 knote_set_error(kn
, ENODEV
);
646 if ((pti
= ptmx_get_ioctl(minor(dev
), 0)) == NULL
) {
647 knote_set_error(kn
, ENXIO
);
654 assert(tp
->t_state
& TS_ISOPEN
);
656 kn
->kn_filtid
= EVFILTID_PTSD
;
657 /* the tty will be freed when detaching the knote */
661 switch (kn
->kn_filter
) {
663 KNOTE_ATTACH(&tp
->t_rsel
.si_note
, kn
);
666 KNOTE_ATTACH(&tp
->t_wsel
.si_note
, kn
);
669 panic("ptsd kevent: unexpected filter: %d, kn = %p, tty = %p",
670 kn
->kn_filter
, kn
, tp
);
674 /* capture current event state */
675 ret
= ptsd_kqops_common(kn
, tp
);
683 * Support for revoke(2).
686 ptsd_revoke_knotes(__unused
int minor
, struct tty
*tp
)
691 KNOTE(&tp
->t_rsel
.si_note
, NOTE_REVOKE
| 1 /* the lock is already held */);
694 KNOTE(&tp
->t_wsel
.si_note
, NOTE_REVOKE
| 1);
700 * kevent filter routines for the master side of a pty, a ptmx.
702 * Stuff the ptmx_ioctl structure into the hook for ptmx knotes. Use the
703 * embedded tty's lock for synchronization.
706 int ptmx_kqfilter(dev_t dev
, struct knote
*kn
);
707 static void ptmx_kqops_detach(struct knote
*);
708 static int ptmx_kqops_event(struct knote
*, long);
709 static int ptmx_kqops_touch(struct knote
*kn
, struct kevent_internal_s
*kev
);
710 static int ptmx_kqops_process(struct knote
*kn
, struct filt_process_s
*data
, struct kevent_internal_s
*kev
);
711 static int ptmx_kqops_common(struct knote
*kn
, struct ptmx_ioctl
*pti
, struct tty
*tp
);
713 SECURITY_READ_ONLY_EARLY(struct filterops
) ptmx_kqops
= {
715 /* attach is handled by ptmx_kqfilter -- the dev node must be passed in */
716 .f_detach
= ptmx_kqops_detach
,
717 .f_event
= ptmx_kqops_event
,
718 .f_touch
= ptmx_kqops_touch
,
719 .f_process
= ptmx_kqops_process
,
722 static struct ptmx_ioctl
*
723 ptmx_knote_ioctl(struct knote
*kn
)
725 return (struct ptmx_ioctl
*)kn
->kn_hook
;
729 ptmx_knote_tty(struct knote
*kn
)
731 struct ptmx_ioctl
*pti
= kn
->kn_hook
;
736 ptmx_kqfilter(dev_t dev
, struct knote
*kn
)
738 struct tty
*tp
= NULL
;
739 struct ptmx_ioctl
*pti
= NULL
;
742 /* make sure we're talking about the right device type */
743 if (cdevsw
[major(dev
)].d_open
!= ptcopen
) {
744 knote_set_error(kn
, ENODEV
);
748 if ((pti
= ptmx_get_ioctl(minor(dev
), 0)) == NULL
) {
749 knote_set_error(kn
, ENXIO
);
756 kn
->kn_filtid
= EVFILTID_PTMX
;
760 * Attach to the ptmx's selinfo structures. This is the major difference
761 * to the ptsd filtops, which use the selinfo structures in the tty
764 switch (kn
->kn_filter
) {
766 KNOTE_ATTACH(&pti
->pt_selr
.si_note
, kn
);
769 KNOTE_ATTACH(&pti
->pt_selw
.si_note
, kn
);
772 panic("ptmx kevent: unexpected filter: %d, kn = %p, tty = %p",
773 kn
->kn_filter
, kn
, tp
);
777 /* capture current event state */
778 ret
= ptmx_kqops_common(kn
, pti
, tp
);
780 /* take a reference on the TTY */
788 ptmx_kqops_detach(struct knote
*kn
)
790 struct ptmx_ioctl
*pti
= kn
->kn_hook
;
791 struct tty
*tp
= pti
->pt_tty
;
797 switch (kn
->kn_filter
) {
799 KNOTE_DETACH(&pti
->pt_selr
.si_note
, kn
);
803 KNOTE_DETACH(&pti
->pt_selw
.si_note
, kn
);
807 panic("invalid knote %p detach, filter: %d", kn
, kn
->kn_filter
);
818 ptmx_kqops_common(struct knote
*kn
, struct ptmx_ioctl
*pti
, struct tty
*tp
)
824 /* disconnects should force a wakeup (EOF) */
825 if (!(tp
->t_state
& TS_CONNECTED
)) {
829 switch (kn
->kn_filter
) {
831 /* there's data on the TTY and it's not stopped */
832 if (tp
->t_outq
.c_cc
&& !(tp
->t_state
& TS_TTSTOP
)) {
833 retval
= tp
->t_outq
.c_cc
;
834 } else if (((pti
->pt_flags
& PF_PKT
) && pti
->pt_send
) ||
835 ((pti
->pt_flags
& PF_UCNTL
) && pti
->pt_ucntl
)) {
841 if (pti
->pt_flags
& PF_REMOTE
) {
842 if (tp
->t_canq
.c_cc
== 0) {
846 retval
= (TTYHOG
- 2) - (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
);
847 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
& ICANON
)) {
857 panic("ptmx kevent: unexpected filter: %d, kn = %p, tty = %p",
858 kn
->kn_filter
, kn
, tp
);
862 if (tp
->t_state
& TS_ZOMBIE
) {
863 kn
->kn_flags
|= EV_EOF
;
871 ptmx_kqops_event(struct knote
*kn
, long hint
)
873 struct ptmx_ioctl
*pti
= ptmx_knote_ioctl(kn
);
874 struct tty
*tp
= ptmx_knote_tty(kn
);
876 bool revoked
= hint
& NOTE_REVOKE
;
877 hint
&= ~NOTE_REVOKE
;
884 kn
->kn_flags
|= EV_EOF
| EV_ONESHOT
;
887 ret
= ptmx_kqops_common(kn
, pti
, tp
);
898 ptmx_kqops_touch(struct knote
*kn
, struct kevent_internal_s
*kev
)
900 struct ptmx_ioctl
*pti
= ptmx_knote_ioctl(kn
);
901 struct tty
*tp
= ptmx_knote_tty(kn
);
906 /* accept new kevent state */
907 kn
->kn_sfflags
= kev
->fflags
;
908 kn
->kn_sdata
= kev
->data
;
909 if ((kn
->kn_status
& KN_UDATA_SPECIFIC
) == 0) {
910 kn
->kn_udata
= kev
->udata
;
913 /* recapture fired state of knote */
914 ret
= ptmx_kqops_common(kn
, pti
, tp
);
922 ptmx_kqops_process(struct knote
*kn
, __unused
struct filt_process_s
*data
,
923 struct kevent_internal_s
*kev
)
925 struct ptmx_ioctl
*pti
= ptmx_knote_ioctl(kn
);
926 struct tty
*tp
= ptmx_knote_tty(kn
);
930 ret
= ptmx_kqops_common(kn
, pti
, tp
);
932 *kev
= kn
->kn_kevent
;
933 if (kn
->kn_flags
& EV_CLEAR
) {