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>
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
, 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 * Locks: tty_lock() on ptmx_ioctl->pt_tty NOT held on entry or exit.
313 static struct ptmx_ioctl
*
314 ptmx_get_ioctl(int minor
, int open_flag
)
316 struct ptmx_ioctl
*new_ptmx_ioctl
;
318 if (open_flag
& PF_OPEN_M
) {
321 * If we are about to allocate more memory, but we have
322 * already hit the administrative limit, then fail the
325 * Note: Subtract free from total when making this
326 * check to allow unit increments, rather than
327 * snapping to the nearest PTMX_GROW_VECTOR...
329 if ((_state
.pis_total
- _state
.pis_free
) >= ptmx_max
) {
333 MALLOC(new_ptmx_ioctl
, struct ptmx_ioctl
*, sizeof(struct ptmx_ioctl
), M_TTYS
, M_WAITOK
|M_ZERO
);
334 if (new_ptmx_ioctl
== NULL
) {
338 if ((new_ptmx_ioctl
->pt_tty
= ttymalloc()) == NULL
) {
339 FREE(new_ptmx_ioctl
, M_TTYS
);
344 * Hold the DEVFS_LOCK() over this whole operation; devfs
345 * itself does this over malloc/free as well, so this should
346 * be safe to do. We hold it longer than we want to, but
347 * doing so avoids a reallocation race on the minor number.
350 /* Need to allocate a larger vector? */
351 if (_state
.pis_free
== 0) {
352 struct ptmx_ioctl
**new_pis_ioctl_list
;
353 struct ptmx_ioctl
**old_pis_ioctl_list
= NULL
;
356 MALLOC(new_pis_ioctl_list
, struct ptmx_ioctl
**, sizeof(struct ptmx_ioctl
*) * (_state
.pis_total
+ PTMX_GROW_VECTOR
), M_TTYS
, M_WAITOK
|M_ZERO
);
357 if (new_pis_ioctl_list
== NULL
) {
358 ttyfree(new_ptmx_ioctl
->pt_tty
);
360 FREE(new_ptmx_ioctl
, M_TTYS
);
364 /* If this is not the first time, copy the old over */
365 bcopy(_state
.pis_ioctl_list
, new_pis_ioctl_list
, sizeof(struct ptmx_ioctl
*) * _state
.pis_total
);
366 old_pis_ioctl_list
= _state
.pis_ioctl_list
;
367 _state
.pis_ioctl_list
= new_pis_ioctl_list
;
368 _state
.pis_free
+= PTMX_GROW_VECTOR
;
369 _state
.pis_total
+= PTMX_GROW_VECTOR
;
370 if (old_pis_ioctl_list
)
371 FREE(old_pis_ioctl_list
, M_TTYS
);
374 if (_state
.pis_ioctl_list
[minor
] != NULL
) {
375 ttyfree(new_ptmx_ioctl
->pt_tty
);
377 FREE(new_ptmx_ioctl
, M_TTYS
);
379 /* Special error value so we know to redrive the open, we've been raced */
380 return (struct ptmx_ioctl
*)-1;
384 /* Vector is large enough; grab a new ptmx_ioctl */
386 /* Now grab a free slot... */
387 _state
.pis_ioctl_list
[minor
] = new_ptmx_ioctl
;
389 /* reduce free count */
392 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_M
;
395 /* Create the /dev/ttysXXX device {<major>,XXX} */
396 _state
.pis_ioctl_list
[minor
]->pt_devhandle
= devfs_make_node(
397 makedev(ptsd_major
, minor
),
398 DEVFS_CHAR
, UID_ROOT
, GID_TTY
, 0620,
399 PTSD_TEMPLATE
, minor
);
400 if (_state
.pis_ioctl_list
[minor
]->pt_devhandle
== NULL
) {
401 printf("devfs_make_node() call failed for ptmx_get_ioctl()!!!!\n");
403 } else if (open_flag
& PF_OPEN_S
) {
405 _state
.pis_ioctl_list
[minor
]->pt_flags
|= PF_OPEN_S
;
408 return (_state
.pis_ioctl_list
[minor
]);
412 * Locks: tty_lock() of old_ptmx_ioctl->pt_tty NOT held for this call.
415 ptmx_free_ioctl(int minor
, int open_flag
)
417 struct ptmx_ioctl
*old_ptmx_ioctl
= NULL
;
420 _state
.pis_ioctl_list
[minor
]->pt_flags
&= ~(open_flag
);
423 * Was this the last close? We will recognize it because we only get
424 * a notification on the last close of a device, and we will have
425 * cleared both the master and the slave open bits in the flags.
427 if (!(_state
.pis_ioctl_list
[minor
]->pt_flags
& (PF_OPEN_M
|PF_OPEN_S
))) {
428 /* Mark as free so it can be reallocated later */
429 old_ptmx_ioctl
= _state
.pis_ioctl_list
[ minor
];
433 /* Free old after dropping lock */
434 if (old_ptmx_ioctl
!= NULL
) {
436 * XXX See <rdar://5348651> and <rdar://4854638>
438 * XXX Conditional to be removed when/if tty/pty reference
439 * XXX counting and mutex implemented.
441 if (old_ptmx_ioctl
->pt_devhandle
!= NULL
)
442 devfs_remove(old_ptmx_ioctl
->pt_devhandle
);
443 ttyfree(old_ptmx_ioctl
->pt_tty
);
444 FREE(old_ptmx_ioctl
, M_TTYS
);
446 /* Don't remove the entry until the devfs slot is free */
448 _state
.pis_ioctl_list
[ minor
] = NULL
;
453 return (0); /* Success */
460 * Given the dev entry that's being opened, we clone the device. This driver
461 * doesn't actually use the dev entry, since we alreaqdy know who we are by
462 * being called from this code. This routine is a callback registered from
463 * devfs_make_node_clone() in ptmx_init(); it's purpose is to provide a new
464 * minor number, or to return -1, if one can't be provided.
466 * Parameters: dev The device we are cloning from
468 * Returns: >= 0 A new minor device number
469 * -1 Error: ENOMEM ("Can't alloc device")
471 * NOTE: Called with DEVFS_LOCK() held
474 ptmx_clone(__unused dev_t dev
, int action
)
478 if (action
== DEVFS_CLONE_ALLOC
) {
480 if (_state
.pis_total
== 0)
484 * Note: We can add hinting on free slots, if this linear search
485 * ends up being a performance bottleneck...
487 for(i
= 0; i
< _state
.pis_total
; i
++) {
488 if (_state
.pis_ioctl_list
[ i
] == NULL
)
493 * XXX We fall off the end here; if we did this twice at the
494 * XXX same time, we could return the same minor to two
495 * XXX callers; we should probably exand the pointer vector
496 * XXX here, but I need more information on the MALLOC/FREE
497 * XXX locking to ensure against a deadlock. Maybe we can
498 * XXX just high watermark it at 1/2 of PTMX_GROW_VECTOR?
499 * XXX That would require returning &minor as implict return
500 * XXX and an error code ("EAGAIN/ERESTART") or 0 as our
501 * XXX explicit return.
504 return (i
); /* empty slot or next slot */
510 ptsd_open(dev_t dev
, int flag
, __unused
int devtype
, __unused proc_t p
)
513 struct ptmx_ioctl
*pti
;
516 if ((pti
= ptmx_get_ioctl(minor(dev
), 0)) == NULL
) {
520 if (!(pti
->pt_flags
& PF_UNLOCKED
)) {
527 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
528 termioschars(&tp
->t_termios
); /* Set up default chars */
529 tp
->t_iflag
= TTYDEF_IFLAG
;
530 tp
->t_oflag
= TTYDEF_OFLAG
;
531 tp
->t_lflag
= TTYDEF_LFLAG
;
532 tp
->t_cflag
= TTYDEF_CFLAG
;
533 tp
->t_ispeed
= tp
->t_ospeed
= TTYDEF_SPEED
;
534 ttsetwater(tp
); /* would be done in xxparam() */
535 } else if (tp
->t_state
&TS_XCLUDE
&& suser(kauth_cred_get(), NULL
)) {
539 if (tp
->t_oproc
) /* Ctrlr still around. */
540 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
541 while ((tp
->t_state
& TS_CARR_ON
) == 0) {
544 error
= ttysleep(tp
, TSA_CARR_ON(tp
), TTIPRI
| PCATCH
,
549 error
= (*linesw
[tp
->t_line
].l_open
)(dev
, tp
);
550 /* Successful open; mark as open by the slave */
551 pti
->pt_flags
|= PF_OPEN_S
;
553 ptmx_wakeup(tp
, FREAD
|FWRITE
);
560 ptsd_close(dev_t dev
, int flag
, __unused
int mode
, __unused proc_t p
)
563 struct ptmx_ioctl
*pti
;
567 * This is temporary until the VSX conformance tests
568 * are fixed. They are hanging with a deadlock
569 * where close(ptsd) will not complete without t_timeout set
571 #define FIX_VSX_HANG 1
575 pti
= ptmx_get_ioctl(minor(dev
), 0);
581 save_timeout
= tp
->t_timeout
;
584 err
= (*linesw
[tp
->t_line
].l_close
)(tp
, flag
);
585 ptsd_stop(tp
, FREAD
|FWRITE
);
588 tp
->t_timeout
= save_timeout
;
593 /* unconditional, just like ttyclose() */
594 ptmx_free_ioctl(minor(dev
), PF_OPEN_S
);
600 ptsd_read(dev_t dev
, struct uio
*uio
, int flag
)
602 proc_t p
= current_proc();
605 struct ptmx_ioctl
*pti
;
610 pti
= ptmx_get_ioctl(minor(dev
), 0);
615 ut
= (struct uthread
*)get_bsdthread_info(current_thread());
617 if (pti
->pt_flags
& PF_REMOTE
) {
618 while (isbackground(p
, tp
)) {
619 if ((p
->p_sigignore
& sigmask(SIGTTIN
)) ||
620 (ut
->uu_sigmask
& sigmask(SIGTTIN
)) ||
621 p
->p_lflag
& P_LPPWAIT
) {
626 if (pg
== PGRP_NULL
) {
631 * SAFE: We about to drop the lock ourselves by
632 * SAFE: erroring out or sleeping anyway.
635 if (pg
->pg_jobc
== 0) {
641 pgsignal(pg
, SIGTTIN
, 1);
645 error
= ttysleep(tp
, &lbolt
, TTIPRI
| PCATCH
| PTTYBLOCK
, "ptsd_bg",
650 if (tp
->t_canq
.c_cc
== 0) {
651 if (flag
& IO_NDELAY
) {
655 error
= ttysleep(tp
, TSA_PTS_READ(tp
), TTIPRI
| PCATCH
,
661 while (tp
->t_canq
.c_cc
> 1 && uio_resid(uio
) > 0) {
665 cc
= min(uio_resid(uio
), BUFSIZ
);
666 // Don't copy the very last byte
667 cc
= min(cc
, tp
->t_canq
.c_cc
- 1);
668 cc
= q_to_b(&tp
->t_canq
, (u_char
*)buf
, cc
);
669 error
= uiomove(buf
, cc
, uio
);
673 if (tp
->t_canq
.c_cc
== 1)
674 (void) getc(&tp
->t_canq
);
679 error
= (*linesw
[tp
->t_line
].l_read
)(tp
, uio
, flag
);
680 ptmx_wakeup(tp
, FWRITE
);
687 * Write to pseudo-tty.
688 * Wakeups of controlling tty will happen
689 * indirectly, when tty driver calls ptsd_start.
692 ptsd_write(dev_t dev
, struct uio
*uio
, int flag
)
695 struct ptmx_ioctl
*pti
;
698 pti
= ptmx_get_ioctl(minor(dev
), 0);
703 if (tp
->t_oproc
== 0)
706 error
= (*linesw
[tp
->t_line
].l_write
)(tp
, uio
, flag
);
713 * Start output on pseudo-tty.
714 * Wake up process selecting or sleeping for input from controlling tty.
716 * t_oproc for this driver; called from within the line discipline
718 * Locks: Assumes tp is locked on entry, remains locked on exit
721 ptsd_start(struct tty
*tp
)
723 struct ptmx_ioctl
*pti
;
725 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
727 if (tp
->t_state
& TS_TTSTOP
)
729 if (pti
->pt_flags
& PF_STOPPED
) {
730 pti
->pt_flags
&= ~PF_STOPPED
;
731 pti
->pt_send
= TIOCPKT_START
;
733 ptmx_wakeup(tp
, FREAD
);
739 * Locks: Assumes tty_lock() is held over this call.
742 ptmx_wakeup(struct tty
*tp
, int flag
)
744 struct ptmx_ioctl
*pti
;
746 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
749 selwakeup(&pti
->pt_selr
);
750 wakeup(TSA_PTC_READ(tp
));
753 selwakeup(&pti
->pt_selw
);
754 wakeup(TSA_PTC_WRITE(tp
));
759 ptmx_open(dev_t dev
, __unused
int flag
, __unused
int devtype
, __unused proc_t p
)
762 struct ptmx_ioctl
*pti
;
765 pti
= ptmx_get_ioctl(minor(dev
), PF_OPEN_M
);
768 } else if (pti
== (struct ptmx_ioctl
*)-1) {
769 return (EREDRIVEOPEN
);
775 /* If master is open OR slave is still draining, pty is still busy */
776 if (tp
->t_oproc
|| (tp
->t_state
& TS_ISOPEN
)) {
779 * If master is closed, we are the only reference, so we
780 * need to clear the master open bit
783 ptmx_free_ioctl(minor(dev
), PF_OPEN_M
);
787 tp
->t_oproc
= ptsd_start
;
788 CLR(tp
->t_state
, TS_ZOMBIE
);
790 tp
->t_stop
= ptsd_stop
;
792 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 1);
793 tp
->t_lflag
&= ~EXTPROC
;
801 ptmx_close(dev_t dev
, __unused
int flags
, __unused
int fmt
, __unused proc_t p
)
804 struct ptmx_ioctl
*pti
;
806 pti
= ptmx_get_ioctl(minor(dev
), 0);
811 (void)(*linesw
[tp
->t_line
].l_modem
)(tp
, 0);
814 * XXX MDMBUF makes no sense for ptys but would inhibit the above
815 * l_modem(). CLOCAL makes sense but isn't supported. Special
816 * l_modem()s that ignore carrier drop make no sense for ptys but
817 * may be in use because other parts of the line discipline make
818 * sense for ptys. Recover by doing everything that a normal
819 * ttymodem() would have done except for sending a SIGHUP.
821 if (tp
->t_state
& TS_ISOPEN
) {
822 tp
->t_state
&= ~(TS_CARR_ON
| TS_CONNECTED
);
823 tp
->t_state
|= TS_ZOMBIE
;
824 ttyflush(tp
, FREAD
| FWRITE
);
827 tp
->t_oproc
= 0; /* mark closed */
831 ptmx_free_ioctl(minor(dev
), PF_OPEN_M
);
837 ptmx_read(dev_t dev
, struct uio
*uio
, int flag
)
840 struct ptmx_ioctl
*pti
;
844 pti
= ptmx_get_ioctl(minor(dev
), 0);
850 * We want to block until the slave
851 * is open, and there's something to read;
852 * but if we lost the slave or we're NBIO,
853 * then return the appropriate error instead.
856 if (tp
->t_state
&TS_ISOPEN
) {
857 if (pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) {
858 error
= ureadc((int)pti
->pt_send
, uio
);
861 if (pti
->pt_send
& TIOCPKT_IOCTL
) {
862 cc
= min(uio_resid(uio
),
863 sizeof(tp
->t_termios
));
864 uiomove((caddr_t
)&tp
->t_termios
, cc
,
870 if (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
) {
871 error
= ureadc((int)pti
->pt_ucntl
, uio
);
877 if (tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0)
880 if ((tp
->t_state
& TS_CONNECTED
) == 0)
882 if (flag
& IO_NDELAY
) {
886 error
= ttysleep(tp
, TSA_PTC_READ(tp
), TTIPRI
| PCATCH
, "ptmx_in", 0);
890 if (pti
->pt_flags
& (PF_PKT
|PF_UCNTL
))
891 error
= ureadc(0, uio
);
892 while (uio_resid(uio
) > 0 && error
== 0) {
893 cc
= q_to_b(&tp
->t_outq
, (u_char
*)buf
, min(uio_resid(uio
), BUFSIZ
));
896 error
= uiomove(buf
, cc
, uio
);
898 (*linesw
[tp
->t_line
].l_start
)(tp
);
906 * Line discipline callback
908 * Locks: tty_lock() is assumed held on entry and exit.
911 ptsd_stop(struct tty
*tp
, int flush
)
913 struct ptmx_ioctl
*pti
;
916 pti
= ptmx_get_ioctl(minor(tp
->t_dev
), 0);
918 /* note: FLUSHREAD and FLUSHWRITE already ok */
920 flush
= TIOCPKT_STOP
;
921 pti
->pt_flags
|= PF_STOPPED
;
923 pti
->pt_flags
&= ~PF_STOPPED
;
924 pti
->pt_send
|= flush
;
925 /* change of perspective */
931 ptmx_wakeup(tp
, flag
);
937 ptsd_reset(__unused
int uban
)
943 * Reinput pending characters after state switch
946 * XXX Code duplication: static function, should be inlined
949 ttypend(struct tty
*tp
)
954 CLR(tp
->t_lflag
, PENDIN
);
955 SET(tp
->t_state
, TS_TYPEN
);
958 tp
->t_rawq
.c_cf
= tp
->t_rawq
.c_cl
= 0;
959 while ((c
= getc(&tq
)) >= 0)
961 CLR(tp
->t_state
, TS_TYPEN
);
965 * Must be called at spltty().
967 * XXX Code duplication: static function, should be inlined
970 ttnread(struct tty
*tp
)
974 if (ISSET(tp
->t_lflag
, PENDIN
))
976 nread
= tp
->t_canq
.c_cc
;
977 if (!ISSET(tp
->t_lflag
, ICANON
)) {
978 nread
+= tp
->t_rawq
.c_cc
;
979 if (nread
< tp
->t_cc
[VMIN
] && tp
->t_cc
[VTIME
] == 0)
986 ptsd_select(dev_t dev
, int rw
, void *wql
, proc_t p
)
988 struct ptmx_ioctl
*pti
;
992 pti
= ptmx_get_ioctl(minor(dev
), 0);
1003 if (ttnread(tp
) > 0 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1007 selrecord(p
, &tp
->t_rsel
, wql
);
1010 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
&&
1011 ISSET(tp
->t_state
, TS_CONNECTED
))
1012 || ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1016 selrecord(p
, &tp
->t_wsel
, wql
);
1025 ptmx_select(dev_t dev
, int rw
, void *wql
, proc_t p
)
1028 struct ptmx_ioctl
*pti
;
1031 pti
= ptmx_get_ioctl(minor(dev
), 0);
1036 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1043 * Need to block timeouts (ttrstart).
1045 if ((tp
->t_state
&TS_ISOPEN
) &&
1046 tp
->t_outq
.c_cc
&& (tp
->t_state
&TS_TTSTOP
) == 0) {
1052 case 0: /* exceptional */
1053 if ((tp
->t_state
&TS_ISOPEN
) &&
1054 ((pti
->pt_flags
& PF_PKT
&& pti
->pt_send
) ||
1055 (pti
->pt_flags
& PF_UCNTL
&& pti
->pt_ucntl
))) {
1059 selrecord(p
, &pti
->pt_selr
, wql
);
1063 if (tp
->t_state
&TS_ISOPEN
) {
1064 if (pti
->pt_flags
& PF_REMOTE
) {
1065 if (tp
->t_canq
.c_cc
== 0) {
1070 if (tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
< TTYHOG
-2) {
1074 if (tp
->t_canq
.c_cc
== 0 && (tp
->t_lflag
&ICANON
)) {
1080 selrecord(p
, &pti
->pt_selw
, wql
);
1090 ptmx_stop(__unused
struct tty
*tp
, __unused
int flush
)
1096 ptmx_reset(__unused
int uban
)
1102 ptmx_write(dev_t dev
, struct uio
*uio
, int flag
)
1105 struct ptmx_ioctl
*pti
;
1108 u_char locbuf
[BUFSIZ
];
1112 pti
= ptmx_get_ioctl(minor(dev
), 0);
1118 if ((tp
->t_state
&TS_ISOPEN
) == 0)
1120 if (pti
->pt_flags
& PF_REMOTE
) {
1121 if (tp
->t_canq
.c_cc
)
1123 while ((uio_resid(uio
) > 0 || cc
> 0) &&
1124 tp
->t_canq
.c_cc
< TTYHOG
- 1) {
1126 cc
= min(uio_resid(uio
), BUFSIZ
);
1127 cc
= min(cc
, TTYHOG
- 1 - tp
->t_canq
.c_cc
);
1129 error
= uiomove((caddr_t
)cp
, cc
, uio
);
1132 /* check again for safety */
1133 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1134 /* adjust as usual */
1135 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1141 cc
= b_to_q((u_char
*)cp
, cc
, &tp
->t_canq
);
1143 * XXX we don't guarantee that the canq size
1144 * is >= TTYHOG, so the above b_to_q() may
1145 * leave some bytes uncopied. However, space
1146 * is guaranteed for the null terminator if
1147 * we don't fail here since (TTYHOG - 1) is
1148 * not a multiple of CBSIZE.
1154 /* adjust for data copied in but not written */
1155 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1156 (void) putc(0, &tp
->t_canq
);
1158 wakeup(TSA_PTS_READ(tp
));
1161 while (uio_resid(uio
) > 0 || cc
> 0) {
1163 cc
= min(uio_resid(uio
), BUFSIZ
);
1165 error
= uiomove((caddr_t
)cp
, cc
, uio
);
1168 /* check again for safety */
1169 if ((tp
->t_state
& TS_ISOPEN
) == 0) {
1170 /* adjust for data copied in but not written */
1171 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1177 if ((tp
->t_rawq
.c_cc
+ tp
->t_canq
.c_cc
) >= TTYHOG
- 2 &&
1178 (tp
->t_canq
.c_cc
> 0 || !(tp
->t_lflag
&ICANON
))) {
1179 wakeup(TSA_HUP_OR_INPUT(tp
));
1182 (*linesw
[tp
->t_line
].l_rint
)(*cp
++, tp
);
1195 * Come here to wait for slave to open, for space
1196 * in outq, or space in rawq, or an empty canq.
1198 if ((tp
->t_state
& TS_CONNECTED
) == 0) {
1199 /* adjust for data copied in but not written */
1200 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1204 if (flag
& IO_NDELAY
) {
1205 /* adjust for data copied in but not written */
1206 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1208 error
= EWOULDBLOCK
;
1211 error
= ttysleep(tp
, TSA_PTC_WRITE(tp
), TTOPRI
| PCATCH
, "ptmx_out", 0);
1213 /* adjust for data copied in but not written */
1214 uio_setresid(uio
, (uio_resid(uio
) + cc
));
1222 cptyioctl(dev_t dev
, u_long cmd
, caddr_t data
, int flag
, proc_t p
)
1225 struct ptmx_ioctl
*pti
;
1227 int stop
, error
= 0;
1229 pti
= ptmx_get_ioctl(minor(dev
), 0);
1237 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1238 * ttywflush(tp) will hang if there are characters in the outq.
1240 if (cmd
== TIOCEXT
) {
1242 * When the EXTPROC bit is being toggled, we need
1243 * to send an TIOCPKT_IOCTL if the packet driver
1247 if (pti
->pt_flags
& PF_PKT
) {
1248 pti
->pt_send
|= TIOCPKT_IOCTL
;
1249 ptmx_wakeup(tp
, FREAD
);
1251 tp
->t_lflag
|= EXTPROC
;
1253 if ((tp
->t_lflag
& EXTPROC
) &&
1254 (pti
->pt_flags
& PF_PKT
)) {
1255 pti
->pt_send
|= TIOCPKT_IOCTL
;
1256 ptmx_wakeup(tp
, FREAD
);
1258 tp
->t_lflag
&= ~EXTPROC
;
1262 if (cdevsw
[major(dev
)].d_open
== ptmx_open
)
1267 * We aviod calling ttioctl on the controller since,
1268 * in that case, tp must be the controlling terminal.
1270 *(int *)data
= tp
->t_pgrp
? tp
->t_pgrp
->pg_id
: 0;
1275 if (pti
->pt_flags
& PF_UCNTL
) {
1279 pti
->pt_flags
|= PF_PKT
;
1281 pti
->pt_flags
&= ~PF_PKT
;
1286 if (pti
->pt_flags
& PF_PKT
) {
1290 pti
->pt_flags
|= PF_UCNTL
;
1292 pti
->pt_flags
&= ~PF_UCNTL
;
1297 pti
->pt_flags
|= PF_REMOTE
;
1299 pti
->pt_flags
&= ~PF_REMOTE
;
1300 ttyflush(tp
, FREAD
|FWRITE
);
1312 ndflush(&tp
->t_outq
, tp
->t_outq
.c_cc
);
1316 if (*(unsigned int *)data
>= NSIG
||
1317 *(unsigned int *)data
== 0) {
1321 if ((tp
->t_lflag
&NOFLSH
) == 0)
1322 ttyflush(tp
, FREAD
|FWRITE
);
1323 if ((*(unsigned int *)data
== SIGINFO
) &&
1324 ((tp
->t_lflag
&NOKERNINFO
) == 0))
1327 * SAFE: All callers drop the lock on return and
1328 * SAFE: the linesw[] will short circut this call
1329 * SAFE: if the ioctl() is eaten before the lower
1330 * SAFE: level code gets to see it.
1333 tty_pgsignal(tp
, *(unsigned int *)data
, 1);
1337 case TIOCPTYGRANT
: /* grantpt(3) */
1339 * Change the uid of the slave to that of the calling
1340 * thread, change the gid of the slave to GID_TTY,
1341 * change the mode to 0620 (rw--w----).
1344 error
= _devfs_setattr(pti
->pt_devhandle
, 0620, kauth_getuid(), GID_TTY
);
1348 case TIOCPTYGNAME
: /* ptsname(3) */
1350 * Report the name of the slave device in *data
1351 * (128 bytes max.). Use the same template string
1352 * used for calling devfs_make_node() to create it.
1354 snprintf(data
, 128, "/dev/" PTSD_TEMPLATE
, minor(dev
));
1358 case TIOCPTYUNLK
: /* unlockpt(3) */
1360 * Unlock the slave device so that it can be opened.
1362 pti
->pt_flags
|= PF_UNLOCKED
;
1366 error
= (*linesw
[tp
->t_line
].l_ioctl
)(tp
, cmd
, data
, flag
, p
);
1367 if (error
== ENOTTY
) {
1368 error
= ttioctl_locked(tp
, cmd
, data
, flag
, p
);
1369 if (error
== ENOTTY
) {
1370 if (pti
->pt_flags
& PF_UCNTL
&& (cmd
& ~0xff) == UIOCCMD(0)) {
1371 /* Process the UIOCMD ioctl group */
1373 pti
->pt_ucntl
= (u_char
)cmd
;
1374 ptmx_wakeup(tp
, FREAD
);
1378 } else if (cmd
== TIOCSBRK
|| cmd
== TIOCCBRK
) {
1380 * POSIX conformance; rdar://3936338
1382 * Clear ENOTTY in the case of setting or
1383 * clearing a break failing because pty's
1384 * don't support break like real serial
1394 * If external processing and packet mode send ioctl packet.
1396 if ((tp
->t_lflag
&EXTPROC
) && (pti
->pt_flags
& PF_PKT
)) {
1411 pti
->pt_send
|= TIOCPKT_IOCTL
;
1412 ptmx_wakeup(tp
, FREAD
);
1417 stop
= (tp
->t_iflag
& IXON
) && CCEQ(cc
[VSTOP
], CTRL('s'))
1418 && CCEQ(cc
[VSTART
], CTRL('q'));
1419 if (pti
->pt_flags
& PF_NOSTOP
) {
1421 pti
->pt_send
&= ~TIOCPKT_NOSTOP
;
1422 pti
->pt_send
|= TIOCPKT_DOSTOP
;
1423 pti
->pt_flags
&= ~PF_NOSTOP
;
1424 ptmx_wakeup(tp
, FREAD
);
1428 pti
->pt_send
&= ~TIOCPKT_DOSTOP
;
1429 pti
->pt_send
|= TIOCPKT_NOSTOP
;
1430 pti
->pt_flags
|= PF_NOSTOP
;
1431 ptmx_wakeup(tp
, FREAD
);
1442 int ptsd_kqfilter(dev_t
, struct knote
*);
1443 static void ptsd_kqops_read_detach(struct knote
*);
1444 static int ptsd_kqops_read_event(struct knote
*, long);
1445 static void ptsd_kqops_write_detach(struct knote
*);
1446 static int ptsd_kqops_write_event(struct knote
*, long);
1448 static struct filterops ptsd_kqops_read
= {
1450 .f_detach
= ptsd_kqops_read_detach
,
1451 .f_event
= ptsd_kqops_read_event
,
1453 static struct filterops ptsd_kqops_write
= {
1455 .f_detach
= ptsd_kqops_write_detach
,
1456 .f_event
= ptsd_kqops_write_event
,
1460 ptsd_kqops_read_detach(struct knote
*kn
)
1462 struct ptmx_ioctl
*pti
;
1464 dev_t dev
= (dev_t
) kn
->kn_hookid
;
1466 pti
= ptmx_get_ioctl(minor(dev
), 0);
1473 KNOTE_DETACH(&tp
->t_rsel
.si_note
, kn
);
1480 ptsd_kqops_read_event(struct knote
*kn
, long hint
)
1482 struct ptmx_ioctl
*pti
;
1484 dev_t dev
= (dev_t
) kn
->kn_hookid
;
1487 pti
= ptmx_get_ioctl(minor(dev
), 0);
1496 kn
->kn_data
= ttnread(tp
);
1497 if (kn
->kn_data
> 0) {
1501 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1502 kn
->kn_flags
|= EV_EOF
;
1511 ptsd_kqops_write_detach(struct knote
*kn
)
1513 struct ptmx_ioctl
*pti
;
1515 dev_t dev
= (dev_t
) kn
->kn_hookid
;
1517 pti
= ptmx_get_ioctl(minor(dev
), 0);
1524 KNOTE_DETACH(&tp
->t_wsel
.si_note
, kn
);
1531 ptsd_kqops_write_event(struct knote
*kn
, long hint
)
1533 struct ptmx_ioctl
*pti
;
1535 dev_t dev
= (dev_t
) kn
->kn_hookid
;
1538 pti
= ptmx_get_ioctl(minor(dev
), 0);
1547 if ((tp
->t_outq
.c_cc
<= tp
->t_lowat
) &&
1548 ISSET(tp
->t_state
, TS_CONNECTED
)) {
1549 kn
->kn_data
= tp
->t_outq
.c_cn
- tp
->t_outq
.c_cc
;
1553 if (ISSET(tp
->t_state
, TS_ZOMBIE
)) {
1554 kn
->kn_flags
|= EV_EOF
;
1565 ptsd_kqfilter(dev_t dev
, struct knote
*kn
)
1567 struct tty
*tp
= NULL
;
1568 struct ptmx_ioctl
*pti
= NULL
;
1571 /* make sure we're talking about the right device type */
1572 if (cdevsw
[major(dev
)].d_open
!= ptsd_open
) {
1576 if ((pti
= ptmx_get_ioctl(minor(dev
), 0)) == NULL
) {
1583 kn
->kn_hookid
= dev
;
1585 switch (kn
->kn_filter
) {
1587 kn
->kn_fop
= &ptsd_kqops_read
;
1588 SLIST_INIT(&tp
->t_rsel
.si_note
);
1589 KNOTE_ATTACH(&tp
->t_rsel
.si_note
, kn
);
1592 kn
->kn_fop
= &ptsd_kqops_write
;
1593 SLIST_INIT(&tp
->t_wsel
.si_note
);
1594 KNOTE_ATTACH(&tp
->t_wsel
.si_note
, kn
);