1*1b191cb5SApple OSS Distributions /*
2*1b191cb5SApple OSS Distributions * Copyright (c) 1997-2020 Apple Inc. All rights reserved.
3*1b191cb5SApple OSS Distributions *
4*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*1b191cb5SApple OSS Distributions *
6*1b191cb5SApple OSS Distributions * This file contains Original Code and/or Modifications of Original Code
7*1b191cb5SApple OSS Distributions * as defined in and that are subject to the Apple Public Source License
8*1b191cb5SApple OSS Distributions * Version 2.0 (the 'License'). You may not use this file except in
9*1b191cb5SApple OSS Distributions * compliance with the License. The rights granted to you under the License
10*1b191cb5SApple OSS Distributions * may not be used to create, or enable the creation or redistribution of,
11*1b191cb5SApple OSS Distributions * unlawful or unlicensed copies of an Apple operating system, or to
12*1b191cb5SApple OSS Distributions * circumvent, violate, or enable the circumvention or violation of, any
13*1b191cb5SApple OSS Distributions * terms of an Apple operating system software license agreement.
14*1b191cb5SApple OSS Distributions *
15*1b191cb5SApple OSS Distributions * Please obtain a copy of the License at
16*1b191cb5SApple OSS Distributions * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*1b191cb5SApple OSS Distributions *
18*1b191cb5SApple OSS Distributions * The Original Code and all software distributed under the License are
19*1b191cb5SApple OSS Distributions * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*1b191cb5SApple OSS Distributions * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*1b191cb5SApple OSS Distributions * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*1b191cb5SApple OSS Distributions * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*1b191cb5SApple OSS Distributions * Please see the License for the specific language governing rights and
24*1b191cb5SApple OSS Distributions * limitations under the License.
25*1b191cb5SApple OSS Distributions *
26*1b191cb5SApple OSS Distributions * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*1b191cb5SApple OSS Distributions */
28*1b191cb5SApple OSS Distributions /*
29*1b191cb5SApple OSS Distributions * Copyright (c) 1982, 1986, 1989, 1993
30*1b191cb5SApple OSS Distributions * The Regents of the University of California. All rights reserved.
31*1b191cb5SApple OSS Distributions *
32*1b191cb5SApple OSS Distributions * Redistribution and use in source and binary forms, with or without
33*1b191cb5SApple OSS Distributions * modification, are permitted provided that the following conditions
34*1b191cb5SApple OSS Distributions * are met:
35*1b191cb5SApple OSS Distributions * 1. Redistributions of source code must retain the above copyright
36*1b191cb5SApple OSS Distributions * notice, this list of conditions and the following disclaimer.
37*1b191cb5SApple OSS Distributions * 2. Redistributions in binary form must reproduce the above copyright
38*1b191cb5SApple OSS Distributions * notice, this list of conditions and the following disclaimer in the
39*1b191cb5SApple OSS Distributions * documentation and/or other materials provided with the distribution.
40*1b191cb5SApple OSS Distributions * 3. All advertising materials mentioning features or use of this software
41*1b191cb5SApple OSS Distributions * must display the following acknowledgement:
42*1b191cb5SApple OSS Distributions * This product includes software developed by the University of
43*1b191cb5SApple OSS Distributions * California, Berkeley and its contributors.
44*1b191cb5SApple OSS Distributions * 4. Neither the name of the University nor the names of its contributors
45*1b191cb5SApple OSS Distributions * may be used to endorse or promote products derived from this software
46*1b191cb5SApple OSS Distributions * without specific prior written permission.
47*1b191cb5SApple OSS Distributions *
48*1b191cb5SApple OSS Distributions * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49*1b191cb5SApple OSS Distributions * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50*1b191cb5SApple OSS Distributions * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51*1b191cb5SApple OSS Distributions * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52*1b191cb5SApple OSS Distributions * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53*1b191cb5SApple OSS Distributions * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54*1b191cb5SApple OSS Distributions * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55*1b191cb5SApple OSS Distributions * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56*1b191cb5SApple OSS Distributions * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57*1b191cb5SApple OSS Distributions * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58*1b191cb5SApple OSS Distributions * SUCH DAMAGE.
59*1b191cb5SApple OSS Distributions *
60*1b191cb5SApple OSS Distributions * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
61*1b191cb5SApple OSS Distributions */
62*1b191cb5SApple OSS Distributions
63*1b191cb5SApple OSS Distributions /* Common callbacks for the pseudo-teletype driver (pty/tty)
64*1b191cb5SApple OSS Distributions * and cloning pseudo-teletype driver (ptmx/pts).
65*1b191cb5SApple OSS Distributions */
66*1b191cb5SApple OSS Distributions
67*1b191cb5SApple OSS Distributions #include <sys/param.h>
68*1b191cb5SApple OSS Distributions #include <sys/systm.h>
69*1b191cb5SApple OSS Distributions #include <sys/ioctl.h>
70*1b191cb5SApple OSS Distributions #include <sys/proc_internal.h>
71*1b191cb5SApple OSS Distributions #include <sys/kauth.h>
72*1b191cb5SApple OSS Distributions #include <sys/tty.h>
73*1b191cb5SApple OSS Distributions #include <sys/conf.h>
74*1b191cb5SApple OSS Distributions #include <sys/file_internal.h>
75*1b191cb5SApple OSS Distributions #include <sys/uio_internal.h>
76*1b191cb5SApple OSS Distributions #include <sys/kernel.h>
77*1b191cb5SApple OSS Distributions #include <sys/vnode.h>
78*1b191cb5SApple OSS Distributions #include <sys/vnode_internal.h> /* _devfs_setattr() */
79*1b191cb5SApple OSS Distributions #include <sys/stat.h> /* _devfs_setattr() */
80*1b191cb5SApple OSS Distributions #include <sys/user.h>
81*1b191cb5SApple OSS Distributions #include <sys/signalvar.h>
82*1b191cb5SApple OSS Distributions #include <sys/sysctl.h>
83*1b191cb5SApple OSS Distributions #include <miscfs/devfs/devfs.h>
84*1b191cb5SApple OSS Distributions #include <miscfs/devfs/devfsdefs.h> /* DEVFS_LOCK()/DEVFS_UNLOCK() */
85*1b191cb5SApple OSS Distributions #include <dev/kmreg_com.h>
86*1b191cb5SApple OSS Distributions #include <machine/cons.h>
87*1b191cb5SApple OSS Distributions
88*1b191cb5SApple OSS Distributions #if CONFIG_MACF
89*1b191cb5SApple OSS Distributions #include <security/mac_framework.h>
90*1b191cb5SApple OSS Distributions #endif
91*1b191cb5SApple OSS Distributions
92*1b191cb5SApple OSS Distributions #include "tty_dev.h"
93*1b191cb5SApple OSS Distributions
94*1b191cb5SApple OSS Distributions /* XXX belongs in devfs somewhere - LATER */
95*1b191cb5SApple OSS Distributions static int _devfs_setattr(void *, unsigned short, uid_t, gid_t);
96*1b191cb5SApple OSS Distributions
97*1b191cb5SApple OSS Distributions /*
98*1b191cb5SApple OSS Distributions * Forward declarations
99*1b191cb5SApple OSS Distributions */
100*1b191cb5SApple OSS Distributions static void ptcwakeup(struct tty *tp, int flag);
101*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_open_t ptsopen;
102*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_close_t ptsclose;
103*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_read_t ptsread;
104*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_write_t ptswrite;
105*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_ioctl_t ptyioctl; /* common ioctl */
106*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_stop_t ptsstop;
107*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_reset_t ptsreset;
108*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_select_t ptsselect;
109*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_open_t ptcopen;
110*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_close_t ptcclose;
111*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_read_t ptcread;
112*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_write_t ptcwrite;
113*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_stop_t ptcstop; /* NO-OP */
114*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_reset_t ptcreset;
115*1b191cb5SApple OSS Distributions __XNU_PRIVATE_EXTERN d_select_t ptcselect;
116*1b191cb5SApple OSS Distributions
117*1b191cb5SApple OSS Distributions /*
118*1b191cb5SApple OSS Distributions * XXX Should be devfs function... and use VATTR mechanisms, per
119*1b191cb5SApple OSS Distributions * XXX vnode_setattr2(); only we maybe can't really get back to the
120*1b191cb5SApple OSS Distributions * XXX vnode here for cloning devices (but it works for *cloned* devices
121*1b191cb5SApple OSS Distributions * XXX that are not themselves cloning).
122*1b191cb5SApple OSS Distributions *
123*1b191cb5SApple OSS Distributions * Returns: 0 Success
124*1b191cb5SApple OSS Distributions * namei:???
125*1b191cb5SApple OSS Distributions * vnode_setattr:???
126*1b191cb5SApple OSS Distributions */
127*1b191cb5SApple OSS Distributions static int
_devfs_setattr(void * handle,unsigned short mode,uid_t uid,gid_t gid)128*1b191cb5SApple OSS Distributions _devfs_setattr(void * handle, unsigned short mode, uid_t uid, gid_t gid)
129*1b191cb5SApple OSS Distributions {
130*1b191cb5SApple OSS Distributions devdirent_t *direntp = (devdirent_t *)handle;
131*1b191cb5SApple OSS Distributions devnode_t *devnodep;
132*1b191cb5SApple OSS Distributions int error = EACCES;
133*1b191cb5SApple OSS Distributions vfs_context_t ctx = vfs_context_current();
134*1b191cb5SApple OSS Distributions struct vnode_attr va;
135*1b191cb5SApple OSS Distributions
136*1b191cb5SApple OSS Distributions VATTR_INIT(&va);
137*1b191cb5SApple OSS Distributions VATTR_SET(&va, va_uid, uid);
138*1b191cb5SApple OSS Distributions VATTR_SET(&va, va_gid, gid);
139*1b191cb5SApple OSS Distributions VATTR_SET(&va, va_mode, mode & ALLPERMS);
140*1b191cb5SApple OSS Distributions
141*1b191cb5SApple OSS Distributions /*
142*1b191cb5SApple OSS Distributions * If the TIOCPTYGRANT loses the race with the clone operation because
143*1b191cb5SApple OSS Distributions * this function is not part of devfs, and therefore can't take the
144*1b191cb5SApple OSS Distributions * devfs lock to protect the direntp update, then force user space to
145*1b191cb5SApple OSS Distributions * redrive the grant request.
146*1b191cb5SApple OSS Distributions */
147*1b191cb5SApple OSS Distributions if (direntp == NULL || (devnodep = direntp->de_dnp) == NULL) {
148*1b191cb5SApple OSS Distributions error = ERESTART;
149*1b191cb5SApple OSS Distributions goto out;
150*1b191cb5SApple OSS Distributions }
151*1b191cb5SApple OSS Distributions
152*1b191cb5SApple OSS Distributions /*
153*1b191cb5SApple OSS Distributions * Only do this if we are operating on device that doesn't clone
154*1b191cb5SApple OSS Distributions * each time it's referenced. We perform a lookup on the device
155*1b191cb5SApple OSS Distributions * to insure we get the right instance. We can't just use the call
156*1b191cb5SApple OSS Distributions * to devfs_dntovn() to get the vp for the operation, because
157*1b191cb5SApple OSS Distributions * dn_dvm may not have been initialized.
158*1b191cb5SApple OSS Distributions */
159*1b191cb5SApple OSS Distributions if (devnodep->dn_clone == NULL) {
160*1b191cb5SApple OSS Distributions struct nameidata nd;
161*1b191cb5SApple OSS Distributions char name[128];
162*1b191cb5SApple OSS Distributions
163*1b191cb5SApple OSS Distributions snprintf(name, sizeof(name), "/dev/%s", direntp->de_name);
164*1b191cb5SApple OSS Distributions NDINIT(&nd, LOOKUP, OP_SETATTR, FOLLOW, UIO_SYSSPACE, CAST_USER_ADDR_T(name), ctx);
165*1b191cb5SApple OSS Distributions error = namei(&nd);
166*1b191cb5SApple OSS Distributions if (error) {
167*1b191cb5SApple OSS Distributions goto out;
168*1b191cb5SApple OSS Distributions }
169*1b191cb5SApple OSS Distributions error = vnode_setattr(nd.ni_vp, &va, ctx);
170*1b191cb5SApple OSS Distributions vnode_put(nd.ni_vp);
171*1b191cb5SApple OSS Distributions nameidone(&nd);
172*1b191cb5SApple OSS Distributions goto out;
173*1b191cb5SApple OSS Distributions }
174*1b191cb5SApple OSS Distributions
175*1b191cb5SApple OSS Distributions out:
176*1b191cb5SApple OSS Distributions return error;
177*1b191cb5SApple OSS Distributions }
178*1b191cb5SApple OSS Distributions
179*1b191cb5SApple OSS Distributions #define BUFSIZ 100 /* Chunk size iomoved to/from user */
180*1b191cb5SApple OSS Distributions
181*1b191cb5SApple OSS Distributions static struct tty_dev_t *tty_dev_head;
182*1b191cb5SApple OSS Distributions
183*1b191cb5SApple OSS Distributions __private_extern__ void
tty_dev_register(struct tty_dev_t * driver)184*1b191cb5SApple OSS Distributions tty_dev_register(struct tty_dev_t *driver)
185*1b191cb5SApple OSS Distributions {
186*1b191cb5SApple OSS Distributions if (driver) {
187*1b191cb5SApple OSS Distributions driver->next = tty_dev_head;
188*1b191cb5SApple OSS Distributions tty_dev_head = driver;
189*1b191cb5SApple OSS Distributions }
190*1b191cb5SApple OSS Distributions }
191*1b191cb5SApple OSS Distributions
192*1b191cb5SApple OSS Distributions /*
193*1b191cb5SApple OSS Distributions * Given a minor number, return the corresponding structure for that minor
194*1b191cb5SApple OSS Distributions * number. If there isn't one, and the create flag is specified, we create
195*1b191cb5SApple OSS Distributions * one if possible.
196*1b191cb5SApple OSS Distributions *
197*1b191cb5SApple OSS Distributions * Parameters: minor Minor number of ptmx device
198*1b191cb5SApple OSS Distributions * open_flag PF_OPEN_M First open of primary
199*1b191cb5SApple OSS Distributions * PF_OPEN_S First open of replica
200*1b191cb5SApple OSS Distributions * 0 Just want ioctl struct
201*1b191cb5SApple OSS Distributions *
202*1b191cb5SApple OSS Distributions * Returns: NULL Did not exist/could not create
203*1b191cb5SApple OSS Distributions * !NULL structure corresponding minor number
204*1b191cb5SApple OSS Distributions *
205*1b191cb5SApple OSS Distributions * Locks: tty_lock() on ptmx_ioctl->pt_tty NOT held on entry or exit.
206*1b191cb5SApple OSS Distributions */
207*1b191cb5SApple OSS Distributions
208*1b191cb5SApple OSS Distributions static struct tty_dev_t *
pty_get_driver(dev_t dev)209*1b191cb5SApple OSS Distributions pty_get_driver(dev_t dev)
210*1b191cb5SApple OSS Distributions {
211*1b191cb5SApple OSS Distributions int major = major(dev);
212*1b191cb5SApple OSS Distributions struct tty_dev_t *driver;
213*1b191cb5SApple OSS Distributions for (driver = tty_dev_head; driver != NULL; driver = driver->next) {
214*1b191cb5SApple OSS Distributions if ((driver->primary == major || driver->replica == major)) {
215*1b191cb5SApple OSS Distributions break;
216*1b191cb5SApple OSS Distributions }
217*1b191cb5SApple OSS Distributions }
218*1b191cb5SApple OSS Distributions return driver;
219*1b191cb5SApple OSS Distributions }
220*1b191cb5SApple OSS Distributions
221*1b191cb5SApple OSS Distributions static struct ptmx_ioctl *
pty_get_ioctl(dev_t dev,int open_flag,struct tty_dev_t ** out_driver)222*1b191cb5SApple OSS Distributions pty_get_ioctl(dev_t dev, int open_flag, struct tty_dev_t **out_driver)
223*1b191cb5SApple OSS Distributions {
224*1b191cb5SApple OSS Distributions struct tty_dev_t *driver = pty_get_driver(dev);
225*1b191cb5SApple OSS Distributions if (out_driver) {
226*1b191cb5SApple OSS Distributions *out_driver = driver;
227*1b191cb5SApple OSS Distributions }
228*1b191cb5SApple OSS Distributions if (driver && driver->open) {
229*1b191cb5SApple OSS Distributions return driver->open(minor(dev), open_flag);
230*1b191cb5SApple OSS Distributions }
231*1b191cb5SApple OSS Distributions return NULL;
232*1b191cb5SApple OSS Distributions }
233*1b191cb5SApple OSS Distributions
234*1b191cb5SApple OSS Distributions /*
235*1b191cb5SApple OSS Distributions * Locks: tty_lock() of old_ptmx_ioctl->pt_tty NOT held for this call.
236*1b191cb5SApple OSS Distributions */
237*1b191cb5SApple OSS Distributions static int
pty_free_ioctl(dev_t dev,int open_flag)238*1b191cb5SApple OSS Distributions pty_free_ioctl(dev_t dev, int open_flag)
239*1b191cb5SApple OSS Distributions {
240*1b191cb5SApple OSS Distributions struct tty_dev_t *driver = pty_get_driver(dev);
241*1b191cb5SApple OSS Distributions if (driver && driver->free) {
242*1b191cb5SApple OSS Distributions return driver->free(minor(dev), open_flag);
243*1b191cb5SApple OSS Distributions }
244*1b191cb5SApple OSS Distributions return 0;
245*1b191cb5SApple OSS Distributions }
246*1b191cb5SApple OSS Distributions
247*1b191cb5SApple OSS Distributions static int
pty_get_name(dev_t dev,char * buffer,size_t size)248*1b191cb5SApple OSS Distributions pty_get_name(dev_t dev, char *buffer, size_t size)
249*1b191cb5SApple OSS Distributions {
250*1b191cb5SApple OSS Distributions struct tty_dev_t *driver = pty_get_driver(dev);
251*1b191cb5SApple OSS Distributions if (driver && driver->name) {
252*1b191cb5SApple OSS Distributions return driver->name(minor(dev), buffer, size);
253*1b191cb5SApple OSS Distributions }
254*1b191cb5SApple OSS Distributions return 0;
255*1b191cb5SApple OSS Distributions }
256*1b191cb5SApple OSS Distributions
257*1b191cb5SApple OSS Distributions __private_extern__ int
ptsopen(dev_t dev,int flag,__unused int devtype,__unused struct proc * p)258*1b191cb5SApple OSS Distributions ptsopen(dev_t dev, int flag, __unused int devtype, __unused struct proc *p)
259*1b191cb5SApple OSS Distributions {
260*1b191cb5SApple OSS Distributions int error;
261*1b191cb5SApple OSS Distributions struct tty_dev_t *driver;
262*1b191cb5SApple OSS Distributions bool free_ptmx_ioctl = true;
263*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, PF_OPEN_S, &driver);
264*1b191cb5SApple OSS Distributions if (pti == NULL) {
265*1b191cb5SApple OSS Distributions return ENXIO;
266*1b191cb5SApple OSS Distributions }
267*1b191cb5SApple OSS Distributions if (!(pti->pt_flags & PF_UNLOCKED)) {
268*1b191cb5SApple OSS Distributions error = EAGAIN;
269*1b191cb5SApple OSS Distributions goto out_free;
270*1b191cb5SApple OSS Distributions }
271*1b191cb5SApple OSS Distributions
272*1b191cb5SApple OSS Distributions struct tty *tp = pti->pt_tty;
273*1b191cb5SApple OSS Distributions tty_lock(tp);
274*1b191cb5SApple OSS Distributions
275*1b191cb5SApple OSS Distributions if ((tp->t_state & TS_ISOPEN) == 0) {
276*1b191cb5SApple OSS Distributions termioschars(&tp->t_termios); /* Set up default chars */
277*1b191cb5SApple OSS Distributions tp->t_iflag = TTYDEF_IFLAG;
278*1b191cb5SApple OSS Distributions tp->t_oflag = TTYDEF_OFLAG;
279*1b191cb5SApple OSS Distributions tp->t_lflag = TTYDEF_LFLAG;
280*1b191cb5SApple OSS Distributions tp->t_cflag = TTYDEF_CFLAG;
281*1b191cb5SApple OSS Distributions tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
282*1b191cb5SApple OSS Distributions ttsetwater(tp); /* would be done in xxparam() */
283*1b191cb5SApple OSS Distributions } else if ((tp->t_state & TS_XCLUDE) && kauth_cred_issuser(kauth_cred_get())) {
284*1b191cb5SApple OSS Distributions error = EBUSY;
285*1b191cb5SApple OSS Distributions goto out_unlock;
286*1b191cb5SApple OSS Distributions }
287*1b191cb5SApple OSS Distributions if (tp->t_oproc) { /* Ctrlr still around. */
288*1b191cb5SApple OSS Distributions (void)(*linesw[tp->t_line].l_modem)(tp, 1);
289*1b191cb5SApple OSS Distributions }
290*1b191cb5SApple OSS Distributions while ((tp->t_state & TS_CARR_ON) == 0) {
291*1b191cb5SApple OSS Distributions if (flag & FNONBLOCK) {
292*1b191cb5SApple OSS Distributions break;
293*1b191cb5SApple OSS Distributions }
294*1b191cb5SApple OSS Distributions error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH, __FUNCTION__, 0);
295*1b191cb5SApple OSS Distributions if (error) {
296*1b191cb5SApple OSS Distributions goto out_unlock;
297*1b191cb5SApple OSS Distributions }
298*1b191cb5SApple OSS Distributions }
299*1b191cb5SApple OSS Distributions error = (*linesw[tp->t_line].l_open)(dev, tp);
300*1b191cb5SApple OSS Distributions /* Successful open; mark as open by the replica */
301*1b191cb5SApple OSS Distributions
302*1b191cb5SApple OSS Distributions free_ptmx_ioctl = false;
303*1b191cb5SApple OSS Distributions CLR(tp->t_state, TS_IOCTL_NOT_OK);
304*1b191cb5SApple OSS Distributions if (error == 0) {
305*1b191cb5SApple OSS Distributions ptcwakeup(tp, FREAD | FWRITE);
306*1b191cb5SApple OSS Distributions }
307*1b191cb5SApple OSS Distributions
308*1b191cb5SApple OSS Distributions out_unlock:
309*1b191cb5SApple OSS Distributions tty_unlock(tp);
310*1b191cb5SApple OSS Distributions
311*1b191cb5SApple OSS Distributions out_free:
312*1b191cb5SApple OSS Distributions if (free_ptmx_ioctl) {
313*1b191cb5SApple OSS Distributions pty_free_ioctl(dev, PF_OPEN_S);
314*1b191cb5SApple OSS Distributions }
315*1b191cb5SApple OSS Distributions
316*1b191cb5SApple OSS Distributions return error;
317*1b191cb5SApple OSS Distributions }
318*1b191cb5SApple OSS Distributions
319*1b191cb5SApple OSS Distributions __private_extern__ int
ptsclose(dev_t dev,int flag,__unused int mode,__unused proc_t p)320*1b191cb5SApple OSS Distributions ptsclose(dev_t dev, int flag, __unused int mode, __unused proc_t p)
321*1b191cb5SApple OSS Distributions {
322*1b191cb5SApple OSS Distributions int err;
323*1b191cb5SApple OSS Distributions
324*1b191cb5SApple OSS Distributions /*
325*1b191cb5SApple OSS Distributions * This is temporary until the VSX conformance tests
326*1b191cb5SApple OSS Distributions * are fixed. They are hanging with a deadlock
327*1b191cb5SApple OSS Distributions * where close() will not complete without t_timeout set
328*1b191cb5SApple OSS Distributions */
329*1b191cb5SApple OSS Distributions #define FIX_VSX_HANG 1
330*1b191cb5SApple OSS Distributions #ifdef FIX_VSX_HANG
331*1b191cb5SApple OSS Distributions int save_timeout;
332*1b191cb5SApple OSS Distributions #endif
333*1b191cb5SApple OSS Distributions struct tty_dev_t *driver;
334*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, &driver);
335*1b191cb5SApple OSS Distributions struct tty *tp;
336*1b191cb5SApple OSS Distributions
337*1b191cb5SApple OSS Distributions if (pti == NULL) {
338*1b191cb5SApple OSS Distributions return ENXIO;
339*1b191cb5SApple OSS Distributions }
340*1b191cb5SApple OSS Distributions
341*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
342*1b191cb5SApple OSS Distributions tty_lock(tp);
343*1b191cb5SApple OSS Distributions #ifdef FIX_VSX_HANG
344*1b191cb5SApple OSS Distributions save_timeout = tp->t_timeout;
345*1b191cb5SApple OSS Distributions tp->t_timeout = 60;
346*1b191cb5SApple OSS Distributions #endif
347*1b191cb5SApple OSS Distributions /*
348*1b191cb5SApple OSS Distributions * Close the line discipline and backing TTY structures.
349*1b191cb5SApple OSS Distributions */
350*1b191cb5SApple OSS Distributions err = (*linesw[tp->t_line].l_close)(tp, flag);
351*1b191cb5SApple OSS Distributions (void)ttyclose(tp);
352*1b191cb5SApple OSS Distributions
353*1b191cb5SApple OSS Distributions /*
354*1b191cb5SApple OSS Distributions * Flush data and notify any waiters on the primary side of this PTY.
355*1b191cb5SApple OSS Distributions */
356*1b191cb5SApple OSS Distributions ptsstop(tp, FREAD | FWRITE);
357*1b191cb5SApple OSS Distributions #ifdef FIX_VSX_HANG
358*1b191cb5SApple OSS Distributions tp->t_timeout = save_timeout;
359*1b191cb5SApple OSS Distributions #endif
360*1b191cb5SApple OSS Distributions tty_unlock(tp);
361*1b191cb5SApple OSS Distributions
362*1b191cb5SApple OSS Distributions if ((flag & IO_REVOKE) == IO_REVOKE && driver->revoke) {
363*1b191cb5SApple OSS Distributions driver->revoke(minor(dev), tp);
364*1b191cb5SApple OSS Distributions }
365*1b191cb5SApple OSS Distributions /* unconditional, just like ttyclose() */
366*1b191cb5SApple OSS Distributions pty_free_ioctl(dev, PF_OPEN_S);
367*1b191cb5SApple OSS Distributions
368*1b191cb5SApple OSS Distributions return err;
369*1b191cb5SApple OSS Distributions }
370*1b191cb5SApple OSS Distributions
371*1b191cb5SApple OSS Distributions __private_extern__ int
ptsread(dev_t dev,struct uio * uio,int flag)372*1b191cb5SApple OSS Distributions ptsread(dev_t dev, struct uio *uio, int flag)
373*1b191cb5SApple OSS Distributions {
374*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, NULL);
375*1b191cb5SApple OSS Distributions struct tty *tp;
376*1b191cb5SApple OSS Distributions int error = 0;
377*1b191cb5SApple OSS Distributions struct uthread *ut;
378*1b191cb5SApple OSS Distributions
379*1b191cb5SApple OSS Distributions if (pti == NULL) {
380*1b191cb5SApple OSS Distributions return ENXIO;
381*1b191cb5SApple OSS Distributions }
382*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
383*1b191cb5SApple OSS Distributions tty_lock(tp);
384*1b191cb5SApple OSS Distributions
385*1b191cb5SApple OSS Distributions ut = current_uthread();
386*1b191cb5SApple OSS Distributions if (tp->t_oproc) {
387*1b191cb5SApple OSS Distributions error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
388*1b191cb5SApple OSS Distributions }
389*1b191cb5SApple OSS Distributions ptcwakeup(tp, FWRITE);
390*1b191cb5SApple OSS Distributions tty_unlock(tp);
391*1b191cb5SApple OSS Distributions return error;
392*1b191cb5SApple OSS Distributions }
393*1b191cb5SApple OSS Distributions
394*1b191cb5SApple OSS Distributions /*
395*1b191cb5SApple OSS Distributions * Write to pseudo-tty.
396*1b191cb5SApple OSS Distributions * Wakeups of controlling tty will happen
397*1b191cb5SApple OSS Distributions * indirectly, when tty driver calls ptsstart.
398*1b191cb5SApple OSS Distributions */
399*1b191cb5SApple OSS Distributions __private_extern__ int
ptswrite(dev_t dev,struct uio * uio,int flag)400*1b191cb5SApple OSS Distributions ptswrite(dev_t dev, struct uio *uio, int flag)
401*1b191cb5SApple OSS Distributions {
402*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, NULL);
403*1b191cb5SApple OSS Distributions struct tty *tp;
404*1b191cb5SApple OSS Distributions int error;
405*1b191cb5SApple OSS Distributions
406*1b191cb5SApple OSS Distributions if (pti == NULL) {
407*1b191cb5SApple OSS Distributions return ENXIO;
408*1b191cb5SApple OSS Distributions }
409*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
410*1b191cb5SApple OSS Distributions tty_lock(tp);
411*1b191cb5SApple OSS Distributions
412*1b191cb5SApple OSS Distributions if (tp->t_oproc == 0) {
413*1b191cb5SApple OSS Distributions error = EIO;
414*1b191cb5SApple OSS Distributions } else {
415*1b191cb5SApple OSS Distributions error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
416*1b191cb5SApple OSS Distributions }
417*1b191cb5SApple OSS Distributions
418*1b191cb5SApple OSS Distributions tty_unlock(tp);
419*1b191cb5SApple OSS Distributions
420*1b191cb5SApple OSS Distributions return error;
421*1b191cb5SApple OSS Distributions }
422*1b191cb5SApple OSS Distributions
423*1b191cb5SApple OSS Distributions /*
424*1b191cb5SApple OSS Distributions * Start output on pseudo-tty.
425*1b191cb5SApple OSS Distributions * Wake up process selecting or sleeping for input from controlling tty.
426*1b191cb5SApple OSS Distributions *
427*1b191cb5SApple OSS Distributions * t_oproc for this driver; called from within the line discipline
428*1b191cb5SApple OSS Distributions *
429*1b191cb5SApple OSS Distributions * Locks: Assumes tp is locked on entry, remains locked on exit
430*1b191cb5SApple OSS Distributions */
431*1b191cb5SApple OSS Distributions static void
ptsstart(struct tty * tp)432*1b191cb5SApple OSS Distributions ptsstart(struct tty *tp)
433*1b191cb5SApple OSS Distributions {
434*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(tp->t_dev, 0, NULL);
435*1b191cb5SApple OSS Distributions if (pti == NULL) {
436*1b191cb5SApple OSS Distributions goto out;
437*1b191cb5SApple OSS Distributions }
438*1b191cb5SApple OSS Distributions if (tp->t_state & TS_TTSTOP) {
439*1b191cb5SApple OSS Distributions goto out;
440*1b191cb5SApple OSS Distributions }
441*1b191cb5SApple OSS Distributions if (pti->pt_flags & PF_STOPPED) {
442*1b191cb5SApple OSS Distributions pti->pt_flags &= ~PF_STOPPED;
443*1b191cb5SApple OSS Distributions pti->pt_send = TIOCPKT_START;
444*1b191cb5SApple OSS Distributions }
445*1b191cb5SApple OSS Distributions ptcwakeup(tp, FREAD);
446*1b191cb5SApple OSS Distributions out:
447*1b191cb5SApple OSS Distributions return;
448*1b191cb5SApple OSS Distributions }
449*1b191cb5SApple OSS Distributions
450*1b191cb5SApple OSS Distributions static void
ptcwakeup_knote(struct selinfo * sip,long hint)451*1b191cb5SApple OSS Distributions ptcwakeup_knote(struct selinfo *sip, long hint)
452*1b191cb5SApple OSS Distributions {
453*1b191cb5SApple OSS Distributions if ((sip->si_flags & SI_KNPOSTING) == 0) {
454*1b191cb5SApple OSS Distributions sip->si_flags |= SI_KNPOSTING;
455*1b191cb5SApple OSS Distributions KNOTE(&sip->si_note, hint);
456*1b191cb5SApple OSS Distributions sip->si_flags &= ~SI_KNPOSTING;
457*1b191cb5SApple OSS Distributions }
458*1b191cb5SApple OSS Distributions }
459*1b191cb5SApple OSS Distributions
460*1b191cb5SApple OSS Distributions /*
461*1b191cb5SApple OSS Distributions * Locks: Assumes tty_lock() is held over this call.
462*1b191cb5SApple OSS Distributions */
463*1b191cb5SApple OSS Distributions static void
ptcwakeup(struct tty * tp,int flag)464*1b191cb5SApple OSS Distributions ptcwakeup(struct tty *tp, int flag)
465*1b191cb5SApple OSS Distributions {
466*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(tp->t_dev, 0, NULL);
467*1b191cb5SApple OSS Distributions if (pti == NULL) {
468*1b191cb5SApple OSS Distributions return;
469*1b191cb5SApple OSS Distributions }
470*1b191cb5SApple OSS Distributions
471*1b191cb5SApple OSS Distributions if (flag & FREAD) {
472*1b191cb5SApple OSS Distributions selwakeup(&pti->pt_selr);
473*1b191cb5SApple OSS Distributions wakeup(TSA_PTC_READ(tp));
474*1b191cb5SApple OSS Distributions ptcwakeup_knote(&pti->pt_selr, 1);
475*1b191cb5SApple OSS Distributions }
476*1b191cb5SApple OSS Distributions if (flag & FWRITE) {
477*1b191cb5SApple OSS Distributions selwakeup(&pti->pt_selw);
478*1b191cb5SApple OSS Distributions wakeup(TSA_PTC_WRITE(tp));
479*1b191cb5SApple OSS Distributions ptcwakeup_knote(&pti->pt_selw, 1);
480*1b191cb5SApple OSS Distributions }
481*1b191cb5SApple OSS Distributions }
482*1b191cb5SApple OSS Distributions
483*1b191cb5SApple OSS Distributions __private_extern__ int
ptcopen(dev_t dev,__unused int flag,__unused int devtype,__unused proc_t p)484*1b191cb5SApple OSS Distributions ptcopen(dev_t dev, __unused int flag, __unused int devtype, __unused proc_t p)
485*1b191cb5SApple OSS Distributions {
486*1b191cb5SApple OSS Distributions struct tty_dev_t *driver;
487*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, PF_OPEN_M, &driver);
488*1b191cb5SApple OSS Distributions if (pti == NULL) {
489*1b191cb5SApple OSS Distributions return ENXIO;
490*1b191cb5SApple OSS Distributions } else if (pti == (struct ptmx_ioctl*)-1) {
491*1b191cb5SApple OSS Distributions return EREDRIVEOPEN;
492*1b191cb5SApple OSS Distributions }
493*1b191cb5SApple OSS Distributions
494*1b191cb5SApple OSS Distributions struct tty *tp = pti->pt_tty;
495*1b191cb5SApple OSS Distributions tty_lock(tp);
496*1b191cb5SApple OSS Distributions
497*1b191cb5SApple OSS Distributions /* If primary is open OR replica is still draining, pty is still busy */
498*1b191cb5SApple OSS Distributions if (tp->t_oproc || (tp->t_state & TS_ISOPEN)) {
499*1b191cb5SApple OSS Distributions tty_unlock(tp);
500*1b191cb5SApple OSS Distributions /*
501*1b191cb5SApple OSS Distributions * If primary is closed, we are the only reference, so we
502*1b191cb5SApple OSS Distributions * need to clear the primary open bit
503*1b191cb5SApple OSS Distributions */
504*1b191cb5SApple OSS Distributions if (!tp->t_oproc) {
505*1b191cb5SApple OSS Distributions pty_free_ioctl(dev, PF_OPEN_M);
506*1b191cb5SApple OSS Distributions }
507*1b191cb5SApple OSS Distributions return EBUSY;
508*1b191cb5SApple OSS Distributions }
509*1b191cb5SApple OSS Distributions tp->t_oproc = ptsstart;
510*1b191cb5SApple OSS Distributions CLR(tp->t_state, TS_ZOMBIE);
511*1b191cb5SApple OSS Distributions SET(tp->t_state, TS_IOCTL_NOT_OK);
512*1b191cb5SApple OSS Distributions #ifdef sun4c
513*1b191cb5SApple OSS Distributions tp->t_stop = ptsstop;
514*1b191cb5SApple OSS Distributions #endif
515*1b191cb5SApple OSS Distributions (void)(*linesw[tp->t_line].l_modem)(tp, 1);
516*1b191cb5SApple OSS Distributions tp->t_lflag &= ~EXTPROC;
517*1b191cb5SApple OSS Distributions
518*1b191cb5SApple OSS Distributions if (driver->open_reset) {
519*1b191cb5SApple OSS Distributions pti->pt_flags = PF_UNLOCKED;
520*1b191cb5SApple OSS Distributions pti->pt_send = 0;
521*1b191cb5SApple OSS Distributions pti->pt_ucntl = 0;
522*1b191cb5SApple OSS Distributions }
523*1b191cb5SApple OSS Distributions
524*1b191cb5SApple OSS Distributions tty_unlock(tp);
525*1b191cb5SApple OSS Distributions return 0;
526*1b191cb5SApple OSS Distributions }
527*1b191cb5SApple OSS Distributions
528*1b191cb5SApple OSS Distributions __private_extern__ int
ptcclose(dev_t dev,__unused int flags,__unused int fmt,__unused proc_t p)529*1b191cb5SApple OSS Distributions ptcclose(dev_t dev, __unused int flags, __unused int fmt, __unused proc_t p)
530*1b191cb5SApple OSS Distributions {
531*1b191cb5SApple OSS Distributions struct tty_dev_t *driver;
532*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, &driver);
533*1b191cb5SApple OSS Distributions struct tty *tp;
534*1b191cb5SApple OSS Distributions
535*1b191cb5SApple OSS Distributions if (!pti) {
536*1b191cb5SApple OSS Distributions return ENXIO;
537*1b191cb5SApple OSS Distributions }
538*1b191cb5SApple OSS Distributions
539*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
540*1b191cb5SApple OSS Distributions tty_lock(tp);
541*1b191cb5SApple OSS Distributions
542*1b191cb5SApple OSS Distributions if (constty == tp) {
543*1b191cb5SApple OSS Distributions constty = NULL;
544*1b191cb5SApple OSS Distributions
545*1b191cb5SApple OSS Distributions
546*1b191cb5SApple OSS Distributions /*
547*1b191cb5SApple OSS Distributions * Closing current console tty; disable printing of console
548*1b191cb5SApple OSS Distributions * messages at bottom-level driver.
549*1b191cb5SApple OSS Distributions */
550*1b191cb5SApple OSS Distributions (*cdevsw[major(tp->t_dev)].d_ioctl)
551*1b191cb5SApple OSS Distributions (tp->t_dev, KMIOCDISABLCONS, NULL, 0, current_proc());
552*1b191cb5SApple OSS Distributions }
553*1b191cb5SApple OSS Distributions
554*1b191cb5SApple OSS Distributions /*
555*1b191cb5SApple OSS Distributions * XXX MDMBUF makes no sense for PTYs, but would inhibit an `l_modem`.
556*1b191cb5SApple OSS Distributions * CLOCAL makes sense but isn't supported. Special `l_modem`s that ignore
557*1b191cb5SApple OSS Distributions * carrier drop make no sense for PTYs but may be in use because other parts
558*1b191cb5SApple OSS Distributions * of the line discipline make sense for PTYs. Recover by doing everything
559*1b191cb5SApple OSS Distributions * that a normal `ttymodem` would have done except for sending SIGHUP.
560*1b191cb5SApple OSS Distributions */
561*1b191cb5SApple OSS Distributions (void)(*linesw[tp->t_line].l_modem)(tp, 0);
562*1b191cb5SApple OSS Distributions if (tp->t_state & TS_ISOPEN) {
563*1b191cb5SApple OSS Distributions tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
564*1b191cb5SApple OSS Distributions tp->t_state |= TS_ZOMBIE;
565*1b191cb5SApple OSS Distributions ttyflush(tp, FREAD | FWRITE);
566*1b191cb5SApple OSS Distributions }
567*1b191cb5SApple OSS Distributions
568*1b191cb5SApple OSS Distributions /*
569*1b191cb5SApple OSS Distributions * Null out the backing TTY struct's open procedure to prevent starting
570*1b191cb5SApple OSS Distributions * replicas through `ptsstart`.
571*1b191cb5SApple OSS Distributions */
572*1b191cb5SApple OSS Distributions tp->t_oproc = NULL;
573*1b191cb5SApple OSS Distributions
574*1b191cb5SApple OSS Distributions /*
575*1b191cb5SApple OSS Distributions * Clear any select or kevent waiters under the lock.
576*1b191cb5SApple OSS Distributions */
577*1b191cb5SApple OSS Distributions knote(&pti->pt_selr.si_note, NOTE_REVOKE, true);
578*1b191cb5SApple OSS Distributions selthreadclear(&pti->pt_selr);
579*1b191cb5SApple OSS Distributions knote(&pti->pt_selw.si_note, NOTE_REVOKE, true);
580*1b191cb5SApple OSS Distributions selthreadclear(&pti->pt_selw);
581*1b191cb5SApple OSS Distributions
582*1b191cb5SApple OSS Distributions tty_unlock(tp);
583*1b191cb5SApple OSS Distributions
584*1b191cb5SApple OSS Distributions pty_free_ioctl(dev, PF_OPEN_M);
585*1b191cb5SApple OSS Distributions #if CONFIG_MACF
586*1b191cb5SApple OSS Distributions if (driver->mac_notify) {
587*1b191cb5SApple OSS Distributions mac_pty_notify_close(p, tp, dev, NULL);
588*1b191cb5SApple OSS Distributions }
589*1b191cb5SApple OSS Distributions #endif
590*1b191cb5SApple OSS Distributions
591*1b191cb5SApple OSS Distributions return 0;
592*1b191cb5SApple OSS Distributions }
593*1b191cb5SApple OSS Distributions
594*1b191cb5SApple OSS Distributions __private_extern__ int
ptcread(dev_t dev,struct uio * uio,int flag)595*1b191cb5SApple OSS Distributions ptcread(dev_t dev, struct uio *uio, int flag)
596*1b191cb5SApple OSS Distributions {
597*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, NULL);
598*1b191cb5SApple OSS Distributions struct tty *tp;
599*1b191cb5SApple OSS Distributions char buf[BUFSIZ];
600*1b191cb5SApple OSS Distributions int error = 0, cc;
601*1b191cb5SApple OSS Distributions
602*1b191cb5SApple OSS Distributions if (pti == NULL) {
603*1b191cb5SApple OSS Distributions return ENXIO;
604*1b191cb5SApple OSS Distributions }
605*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
606*1b191cb5SApple OSS Distributions tty_lock(tp);
607*1b191cb5SApple OSS Distributions
608*1b191cb5SApple OSS Distributions /*
609*1b191cb5SApple OSS Distributions * We want to block until the replica
610*1b191cb5SApple OSS Distributions * is open, and there's something to read;
611*1b191cb5SApple OSS Distributions * but if we lost the replica or we're NBIO,
612*1b191cb5SApple OSS Distributions * then return the appropriate error instead.
613*1b191cb5SApple OSS Distributions */
614*1b191cb5SApple OSS Distributions for (;;) {
615*1b191cb5SApple OSS Distributions if (tp->t_state & TS_ISOPEN) {
616*1b191cb5SApple OSS Distributions if (pti->pt_flags & PF_PKT && pti->pt_send) {
617*1b191cb5SApple OSS Distributions error = ureadc((int)pti->pt_send, uio);
618*1b191cb5SApple OSS Distributions if (error) {
619*1b191cb5SApple OSS Distributions goto out;
620*1b191cb5SApple OSS Distributions }
621*1b191cb5SApple OSS Distributions if (pti->pt_send & TIOCPKT_IOCTL) {
622*1b191cb5SApple OSS Distributions #ifdef __LP64__
623*1b191cb5SApple OSS Distributions if (uio->uio_segflg == UIO_USERSPACE32) {
624*1b191cb5SApple OSS Distributions static struct termios32 tio32;
625*1b191cb5SApple OSS Distributions cc = MIN((int)uio_resid(uio), (int)sizeof(tio32));
626*1b191cb5SApple OSS Distributions termios64to32((struct user_termios *)&tp->t_termios,
627*1b191cb5SApple OSS Distributions (struct termios32 *)&tio32);
628*1b191cb5SApple OSS Distributions uiomove((caddr_t)&tio32, cc, uio);
629*1b191cb5SApple OSS Distributions #else
630*1b191cb5SApple OSS Distributions if (uio->uio_segflg == UIO_USERSPACE64) {
631*1b191cb5SApple OSS Distributions static struct user_termios tio64;
632*1b191cb5SApple OSS Distributions cc = MIN((int)uio_resid(uio), (int)sizeof(tio64));
633*1b191cb5SApple OSS Distributions termios32to64((struct termios32 *)&tp->t_termios,
634*1b191cb5SApple OSS Distributions (struct user_termios *)&tio64);
635*1b191cb5SApple OSS Distributions uiomove((caddr_t)&tio64, cc, uio);
636*1b191cb5SApple OSS Distributions #endif
637*1b191cb5SApple OSS Distributions } else {
638*1b191cb5SApple OSS Distributions cc = MIN((int)uio_resid(uio), (int)sizeof(tp->t_termios));
639*1b191cb5SApple OSS Distributions uiomove((caddr_t)&tp->t_termios, cc, uio);
640*1b191cb5SApple OSS Distributions }
641*1b191cb5SApple OSS Distributions }
642*1b191cb5SApple OSS Distributions pti->pt_send = 0;
643*1b191cb5SApple OSS Distributions goto out;
644*1b191cb5SApple OSS Distributions }
645*1b191cb5SApple OSS Distributions if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
646*1b191cb5SApple OSS Distributions error = ureadc((int)pti->pt_ucntl, uio);
647*1b191cb5SApple OSS Distributions if (error) {
648*1b191cb5SApple OSS Distributions goto out;
649*1b191cb5SApple OSS Distributions }
650*1b191cb5SApple OSS Distributions pti->pt_ucntl = 0;
651*1b191cb5SApple OSS Distributions goto out;
652*1b191cb5SApple OSS Distributions }
653*1b191cb5SApple OSS Distributions if (tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) {
654*1b191cb5SApple OSS Distributions break;
655*1b191cb5SApple OSS Distributions }
656*1b191cb5SApple OSS Distributions }
657*1b191cb5SApple OSS Distributions if ((tp->t_state & TS_CONNECTED) == 0) {
658*1b191cb5SApple OSS Distributions goto out; /* EOF */
659*1b191cb5SApple OSS Distributions }
660*1b191cb5SApple OSS Distributions if (flag & IO_NDELAY) {
661*1b191cb5SApple OSS Distributions error = EWOULDBLOCK;
662*1b191cb5SApple OSS Distributions goto out;
663*1b191cb5SApple OSS Distributions }
664*1b191cb5SApple OSS Distributions error = ttysleep(tp, TSA_PTC_READ(tp), TTIPRI | PCATCH, __FUNCTION__, 0);
665*1b191cb5SApple OSS Distributions if (error) {
666*1b191cb5SApple OSS Distributions goto out;
667*1b191cb5SApple OSS Distributions }
668*1b191cb5SApple OSS Distributions }
669*1b191cb5SApple OSS Distributions if (pti->pt_flags & (PF_PKT | PF_UCNTL)) {
670*1b191cb5SApple OSS Distributions error = ureadc(0, uio);
671*1b191cb5SApple OSS Distributions }
672*1b191cb5SApple OSS Distributions while (uio_resid(uio) > 0 && error == 0) {
673*1b191cb5SApple OSS Distributions cc = q_to_b(&tp->t_outq, (u_char *)buf, MIN((int)uio_resid(uio), BUFSIZ));
674*1b191cb5SApple OSS Distributions if (cc <= 0) {
675*1b191cb5SApple OSS Distributions break;
676*1b191cb5SApple OSS Distributions }
677*1b191cb5SApple OSS Distributions error = uiomove(buf, cc, uio);
678*1b191cb5SApple OSS Distributions }
679*1b191cb5SApple OSS Distributions (*linesw[tp->t_line].l_start)(tp);
680*1b191cb5SApple OSS Distributions
681*1b191cb5SApple OSS Distributions out:
682*1b191cb5SApple OSS Distributions tty_unlock(tp);
683*1b191cb5SApple OSS Distributions
684*1b191cb5SApple OSS Distributions return error;
685*1b191cb5SApple OSS Distributions }
686*1b191cb5SApple OSS Distributions
687*1b191cb5SApple OSS Distributions /*
688*1b191cb5SApple OSS Distributions * Line discipline callback
689*1b191cb5SApple OSS Distributions *
690*1b191cb5SApple OSS Distributions * Locks: tty_lock() is assumed held on entry and exit.
691*1b191cb5SApple OSS Distributions */
692*1b191cb5SApple OSS Distributions __private_extern__ int
693*1b191cb5SApple OSS Distributions ptsstop(struct tty* tp, int flush)
694*1b191cb5SApple OSS Distributions {
695*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(tp->t_dev, 0, NULL);
696*1b191cb5SApple OSS Distributions int flag;
697*1b191cb5SApple OSS Distributions
698*1b191cb5SApple OSS Distributions if (pti == NULL) {
699*1b191cb5SApple OSS Distributions return ENXIO;
700*1b191cb5SApple OSS Distributions }
701*1b191cb5SApple OSS Distributions
702*1b191cb5SApple OSS Distributions /* note: FLUSHREAD and FLUSHWRITE already ok */
703*1b191cb5SApple OSS Distributions if (flush == 0) {
704*1b191cb5SApple OSS Distributions flush = TIOCPKT_STOP;
705*1b191cb5SApple OSS Distributions pti->pt_flags |= PF_STOPPED;
706*1b191cb5SApple OSS Distributions } else {
707*1b191cb5SApple OSS Distributions pti->pt_flags &= ~PF_STOPPED;
708*1b191cb5SApple OSS Distributions }
709*1b191cb5SApple OSS Distributions pti->pt_send |= flush;
710*1b191cb5SApple OSS Distributions /* change of perspective */
711*1b191cb5SApple OSS Distributions flag = 0;
712*1b191cb5SApple OSS Distributions if (flush & FREAD) {
713*1b191cb5SApple OSS Distributions flag |= FWRITE;
714*1b191cb5SApple OSS Distributions }
715*1b191cb5SApple OSS Distributions if (flush & FWRITE) {
716*1b191cb5SApple OSS Distributions flag |= FREAD;
717*1b191cb5SApple OSS Distributions }
718*1b191cb5SApple OSS Distributions ptcwakeup(tp, flag);
719*1b191cb5SApple OSS Distributions return 0;
720*1b191cb5SApple OSS Distributions }
721*1b191cb5SApple OSS Distributions
722*1b191cb5SApple OSS Distributions __private_extern__ int
723*1b191cb5SApple OSS Distributions ptsreset(__unused int uban)
724*1b191cb5SApple OSS Distributions {
725*1b191cb5SApple OSS Distributions return 0;
726*1b191cb5SApple OSS Distributions }
727*1b191cb5SApple OSS Distributions
728*1b191cb5SApple OSS Distributions int
729*1b191cb5SApple OSS Distributions ptsselect(dev_t dev, int rw, void *wql, proc_t p)
730*1b191cb5SApple OSS Distributions {
731*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, NULL);
732*1b191cb5SApple OSS Distributions struct tty *tp;
733*1b191cb5SApple OSS Distributions int retval = 0;
734*1b191cb5SApple OSS Distributions
735*1b191cb5SApple OSS Distributions if (pti == NULL) {
736*1b191cb5SApple OSS Distributions return ENXIO;
737*1b191cb5SApple OSS Distributions }
738*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
739*1b191cb5SApple OSS Distributions if (tp == NULL) {
740*1b191cb5SApple OSS Distributions return ENXIO;
741*1b191cb5SApple OSS Distributions }
742*1b191cb5SApple OSS Distributions
743*1b191cb5SApple OSS Distributions tty_lock(tp);
744*1b191cb5SApple OSS Distributions
745*1b191cb5SApple OSS Distributions switch (rw) {
746*1b191cb5SApple OSS Distributions case FREAD:
747*1b191cb5SApple OSS Distributions if (ISSET(tp->t_state, TS_ZOMBIE)) {
748*1b191cb5SApple OSS Distributions retval = 1;
749*1b191cb5SApple OSS Distributions break;
750*1b191cb5SApple OSS Distributions }
751*1b191cb5SApple OSS Distributions
752*1b191cb5SApple OSS Distributions retval = ttnread(tp);
753*1b191cb5SApple OSS Distributions if (retval > 0) {
754*1b191cb5SApple OSS Distributions break;
755*1b191cb5SApple OSS Distributions }
756*1b191cb5SApple OSS Distributions
757*1b191cb5SApple OSS Distributions selrecord(p, &tp->t_rsel, wql);
758*1b191cb5SApple OSS Distributions break;
759*1b191cb5SApple OSS Distributions case FWRITE:
760*1b191cb5SApple OSS Distributions if (ISSET(tp->t_state, TS_ZOMBIE)) {
761*1b191cb5SApple OSS Distributions retval = 1;
762*1b191cb5SApple OSS Distributions break;
763*1b191cb5SApple OSS Distributions }
764*1b191cb5SApple OSS Distributions
765*1b191cb5SApple OSS Distributions if ((tp->t_outq.c_cc <= tp->t_lowat) &&
766*1b191cb5SApple OSS Distributions ISSET(tp->t_state, TS_CONNECTED)) {
767*1b191cb5SApple OSS Distributions retval = tp->t_hiwat - tp->t_outq.c_cc;
768*1b191cb5SApple OSS Distributions break;
769*1b191cb5SApple OSS Distributions }
770*1b191cb5SApple OSS Distributions
771*1b191cb5SApple OSS Distributions selrecord(p, &tp->t_wsel, wql);
772*1b191cb5SApple OSS Distributions break;
773*1b191cb5SApple OSS Distributions }
774*1b191cb5SApple OSS Distributions
775*1b191cb5SApple OSS Distributions tty_unlock(tp);
776*1b191cb5SApple OSS Distributions return retval;
777*1b191cb5SApple OSS Distributions }
778*1b191cb5SApple OSS Distributions
779*1b191cb5SApple OSS Distributions __private_extern__ int
780*1b191cb5SApple OSS Distributions ptcselect(dev_t dev, int rw, void *wql, proc_t p)
781*1b191cb5SApple OSS Distributions {
782*1b191cb5SApple OSS Distributions struct tty_dev_t *driver;
783*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, &driver);
784*1b191cb5SApple OSS Distributions struct tty *tp;
785*1b191cb5SApple OSS Distributions int retval = 0;
786*1b191cb5SApple OSS Distributions
787*1b191cb5SApple OSS Distributions if (pti == NULL) {
788*1b191cb5SApple OSS Distributions return ENXIO;
789*1b191cb5SApple OSS Distributions }
790*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
791*1b191cb5SApple OSS Distributions tty_lock(tp);
792*1b191cb5SApple OSS Distributions
793*1b191cb5SApple OSS Distributions if ((tp->t_state & TS_CONNECTED) == 0) {
794*1b191cb5SApple OSS Distributions retval = 1;
795*1b191cb5SApple OSS Distributions goto out;
796*1b191cb5SApple OSS Distributions }
797*1b191cb5SApple OSS Distributions switch (rw) {
798*1b191cb5SApple OSS Distributions case FREAD:
799*1b191cb5SApple OSS Distributions /*
800*1b191cb5SApple OSS Distributions * Need to block timeouts (ttrstart).
801*1b191cb5SApple OSS Distributions */
802*1b191cb5SApple OSS Distributions if ((tp->t_state & TS_ISOPEN) &&
803*1b191cb5SApple OSS Distributions tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) {
804*1b191cb5SApple OSS Distributions retval = (driver->fix_7828447) ? tp->t_outq.c_cc : 1;
805*1b191cb5SApple OSS Distributions break;
806*1b191cb5SApple OSS Distributions }
807*1b191cb5SApple OSS Distributions OS_FALLTHROUGH;
808*1b191cb5SApple OSS Distributions
809*1b191cb5SApple OSS Distributions case 0: /* exceptional */
810*1b191cb5SApple OSS Distributions if ((tp->t_state & TS_ISOPEN) &&
811*1b191cb5SApple OSS Distributions (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
812*1b191cb5SApple OSS Distributions ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
813*1b191cb5SApple OSS Distributions retval = 1;
814*1b191cb5SApple OSS Distributions break;
815*1b191cb5SApple OSS Distributions }
816*1b191cb5SApple OSS Distributions selrecord(p, &pti->pt_selr, wql);
817*1b191cb5SApple OSS Distributions break;
818*1b191cb5SApple OSS Distributions
819*1b191cb5SApple OSS Distributions
820*1b191cb5SApple OSS Distributions case FWRITE:
821*1b191cb5SApple OSS Distributions if (tp->t_state & TS_ISOPEN) {
822*1b191cb5SApple OSS Distributions retval = (TTYHOG - 2) - (tp->t_rawq.c_cc + tp->t_canq.c_cc);
823*1b191cb5SApple OSS Distributions if (retval > 0) {
824*1b191cb5SApple OSS Distributions retval = (driver->fix_7828447) ? retval : 1;
825*1b191cb5SApple OSS Distributions break;
826*1b191cb5SApple OSS Distributions }
827*1b191cb5SApple OSS Distributions if (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)) {
828*1b191cb5SApple OSS Distributions retval = 1;
829*1b191cb5SApple OSS Distributions break;
830*1b191cb5SApple OSS Distributions }
831*1b191cb5SApple OSS Distributions retval = 0;
832*1b191cb5SApple OSS Distributions }
833*1b191cb5SApple OSS Distributions selrecord(p, &pti->pt_selw, wql);
834*1b191cb5SApple OSS Distributions break;
835*1b191cb5SApple OSS Distributions }
836*1b191cb5SApple OSS Distributions out:
837*1b191cb5SApple OSS Distributions tty_unlock(tp);
838*1b191cb5SApple OSS Distributions
839*1b191cb5SApple OSS Distributions return retval;
840*1b191cb5SApple OSS Distributions }
841*1b191cb5SApple OSS Distributions
842*1b191cb5SApple OSS Distributions __private_extern__ int
843*1b191cb5SApple OSS Distributions ptcstop(__unused struct tty *tp, __unused int flush)
844*1b191cb5SApple OSS Distributions {
845*1b191cb5SApple OSS Distributions return 0;
846*1b191cb5SApple OSS Distributions }
847*1b191cb5SApple OSS Distributions
848*1b191cb5SApple OSS Distributions __private_extern__ int
849*1b191cb5SApple OSS Distributions ptcreset(__unused int uban)
850*1b191cb5SApple OSS Distributions {
851*1b191cb5SApple OSS Distributions return 0;
852*1b191cb5SApple OSS Distributions }
853*1b191cb5SApple OSS Distributions
854*1b191cb5SApple OSS Distributions __private_extern__ int
855*1b191cb5SApple OSS Distributions ptcwrite(dev_t dev, struct uio *uio, int flag)
856*1b191cb5SApple OSS Distributions {
857*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, NULL);
858*1b191cb5SApple OSS Distributions struct tty *tp;
859*1b191cb5SApple OSS Distributions u_char *cp = NULL;
860*1b191cb5SApple OSS Distributions int cc = 0;
861*1b191cb5SApple OSS Distributions u_char locbuf[BUFSIZ];
862*1b191cb5SApple OSS Distributions int wcnt = 0;
863*1b191cb5SApple OSS Distributions int error = 0;
864*1b191cb5SApple OSS Distributions
865*1b191cb5SApple OSS Distributions if (pti == NULL) {
866*1b191cb5SApple OSS Distributions return ENXIO;
867*1b191cb5SApple OSS Distributions }
868*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
869*1b191cb5SApple OSS Distributions tty_lock(tp);
870*1b191cb5SApple OSS Distributions
871*1b191cb5SApple OSS Distributions again:
872*1b191cb5SApple OSS Distributions if ((tp->t_state & TS_ISOPEN) == 0) {
873*1b191cb5SApple OSS Distributions goto block;
874*1b191cb5SApple OSS Distributions }
875*1b191cb5SApple OSS Distributions while (uio_resid(uio) > 0 || cc > 0) {
876*1b191cb5SApple OSS Distributions if (cc == 0) {
877*1b191cb5SApple OSS Distributions cc = MIN((int)uio_resid(uio), BUFSIZ);
878*1b191cb5SApple OSS Distributions cp = locbuf;
879*1b191cb5SApple OSS Distributions error = uiomove((caddr_t)cp, cc, uio);
880*1b191cb5SApple OSS Distributions if (error) {
881*1b191cb5SApple OSS Distributions goto out;
882*1b191cb5SApple OSS Distributions }
883*1b191cb5SApple OSS Distributions /* check again for safety */
884*1b191cb5SApple OSS Distributions if ((tp->t_state & TS_ISOPEN) == 0) {
885*1b191cb5SApple OSS Distributions /* adjust for data copied in but not written */
886*1b191cb5SApple OSS Distributions uio_setresid(uio, (uio_resid(uio) + cc));
887*1b191cb5SApple OSS Distributions error = EIO;
888*1b191cb5SApple OSS Distributions goto out;
889*1b191cb5SApple OSS Distributions }
890*1b191cb5SApple OSS Distributions }
891*1b191cb5SApple OSS Distributions while (cc > 0) {
892*1b191cb5SApple OSS Distributions if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
893*1b191cb5SApple OSS Distributions (tp->t_canq.c_cc > 0 || !(tp->t_lflag & ICANON))) {
894*1b191cb5SApple OSS Distributions wakeup(TSA_HUP_OR_INPUT(tp));
895*1b191cb5SApple OSS Distributions goto block;
896*1b191cb5SApple OSS Distributions }
897*1b191cb5SApple OSS Distributions OS_ANALYZER_SUPPRESS("80961525") (*linesw[tp->t_line].l_rint)(*cp++, tp);
898*1b191cb5SApple OSS Distributions wcnt++;
899*1b191cb5SApple OSS Distributions cc--;
900*1b191cb5SApple OSS Distributions }
901*1b191cb5SApple OSS Distributions cc = 0;
902*1b191cb5SApple OSS Distributions }
903*1b191cb5SApple OSS Distributions out:
904*1b191cb5SApple OSS Distributions tty_unlock(tp);
905*1b191cb5SApple OSS Distributions
906*1b191cb5SApple OSS Distributions return error;
907*1b191cb5SApple OSS Distributions
908*1b191cb5SApple OSS Distributions block:
909*1b191cb5SApple OSS Distributions /*
910*1b191cb5SApple OSS Distributions * Come here to wait for replica to open, for space
911*1b191cb5SApple OSS Distributions * in outq, or space in rawq, or an empty canq.
912*1b191cb5SApple OSS Distributions */
913*1b191cb5SApple OSS Distributions if ((tp->t_state & TS_CONNECTED) == 0) {
914*1b191cb5SApple OSS Distributions /* adjust for data copied in but not written */
915*1b191cb5SApple OSS Distributions uio_setresid(uio, (uio_resid(uio) + cc));
916*1b191cb5SApple OSS Distributions error = EIO;
917*1b191cb5SApple OSS Distributions goto out;
918*1b191cb5SApple OSS Distributions }
919*1b191cb5SApple OSS Distributions if (flag & IO_NDELAY) {
920*1b191cb5SApple OSS Distributions /* adjust for data copied in but not written */
921*1b191cb5SApple OSS Distributions uio_setresid(uio, (uio_resid(uio) + cc));
922*1b191cb5SApple OSS Distributions if (wcnt == 0) {
923*1b191cb5SApple OSS Distributions error = EWOULDBLOCK;
924*1b191cb5SApple OSS Distributions }
925*1b191cb5SApple OSS Distributions goto out;
926*1b191cb5SApple OSS Distributions }
927*1b191cb5SApple OSS Distributions error = ttysleep(tp, TSA_PTC_WRITE(tp), TTOPRI | PCATCH, __FUNCTION__, 0);
928*1b191cb5SApple OSS Distributions if (error) {
929*1b191cb5SApple OSS Distributions /* adjust for data copied in but not written */
930*1b191cb5SApple OSS Distributions uio_setresid(uio, (uio_resid(uio) + cc));
931*1b191cb5SApple OSS Distributions goto out;
932*1b191cb5SApple OSS Distributions }
933*1b191cb5SApple OSS Distributions goto again;
934*1b191cb5SApple OSS Distributions }
935*1b191cb5SApple OSS Distributions
936*1b191cb5SApple OSS Distributions /*
937*1b191cb5SApple OSS Distributions * ptyioctl: Assumes dev was opened and lock was initilized
938*1b191cb5SApple OSS Distributions */
939*1b191cb5SApple OSS Distributions __private_extern__ int
940*1b191cb5SApple OSS Distributions ptyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
941*1b191cb5SApple OSS Distributions {
942*1b191cb5SApple OSS Distributions struct tty_dev_t *driver;
943*1b191cb5SApple OSS Distributions struct ptmx_ioctl *pti = pty_get_ioctl(dev, 0, &driver);
944*1b191cb5SApple OSS Distributions struct tty *tp;
945*1b191cb5SApple OSS Distributions int stop, error = 0;
946*1b191cb5SApple OSS Distributions int allow_ext_ioctl = 1;
947*1b191cb5SApple OSS Distributions
948*1b191cb5SApple OSS Distributions if (pti == NULL || pti->pt_tty == NULL) {
949*1b191cb5SApple OSS Distributions return ENXIO;
950*1b191cb5SApple OSS Distributions }
951*1b191cb5SApple OSS Distributions
952*1b191cb5SApple OSS Distributions if (cmd == KMIOCDISABLCONS) {
953*1b191cb5SApple OSS Distributions return 0;
954*1b191cb5SApple OSS Distributions }
955*1b191cb5SApple OSS Distributions
956*1b191cb5SApple OSS Distributions tp = pti->pt_tty;
957*1b191cb5SApple OSS Distributions tty_lock(tp);
958*1b191cb5SApple OSS Distributions
959*1b191cb5SApple OSS Distributions u_char *cc = tp->t_cc;
960*1b191cb5SApple OSS Distributions
961*1b191cb5SApple OSS Distributions /*
962*1b191cb5SApple OSS Distributions * Do not permit extended ioctls on the primary side of the pty unless
963*1b191cb5SApple OSS Distributions * the replica side has been successfully opened and initialized.
964*1b191cb5SApple OSS Distributions */
965*1b191cb5SApple OSS Distributions if (major(dev) == driver->primary &&
966*1b191cb5SApple OSS Distributions driver->fix_7070978 &&
967*1b191cb5SApple OSS Distributions ISSET(tp->t_state, TS_IOCTL_NOT_OK)) {
968*1b191cb5SApple OSS Distributions allow_ext_ioctl = 0;
969*1b191cb5SApple OSS Distributions }
970*1b191cb5SApple OSS Distributions
971*1b191cb5SApple OSS Distributions /*
972*1b191cb5SApple OSS Distributions * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
973*1b191cb5SApple OSS Distributions * ttywflush(tp) will hang if there are characters in the outq.
974*1b191cb5SApple OSS Distributions */
975*1b191cb5SApple OSS Distributions if (cmd == TIOCEXT && allow_ext_ioctl) {
976*1b191cb5SApple OSS Distributions /*
977*1b191cb5SApple OSS Distributions * When the EXTPROC bit is being toggled, we need
978*1b191cb5SApple OSS Distributions * to send an TIOCPKT_IOCTL if the packet driver
979*1b191cb5SApple OSS Distributions * is turned on.
980*1b191cb5SApple OSS Distributions */
981*1b191cb5SApple OSS Distributions if (*(int *)data) {
982*1b191cb5SApple OSS Distributions if (pti->pt_flags & PF_PKT) {
983*1b191cb5SApple OSS Distributions pti->pt_send |= TIOCPKT_IOCTL;
984*1b191cb5SApple OSS Distributions ptcwakeup(tp, FREAD);
985*1b191cb5SApple OSS Distributions }
986*1b191cb5SApple OSS Distributions tp->t_lflag |= EXTPROC;
987*1b191cb5SApple OSS Distributions } else {
988*1b191cb5SApple OSS Distributions if ((tp->t_lflag & EXTPROC) &&
989*1b191cb5SApple OSS Distributions (pti->pt_flags & PF_PKT)) {
990*1b191cb5SApple OSS Distributions pti->pt_send |= TIOCPKT_IOCTL;
991*1b191cb5SApple OSS Distributions ptcwakeup(tp, FREAD);
992*1b191cb5SApple OSS Distributions }
993*1b191cb5SApple OSS Distributions tp->t_lflag &= ~EXTPROC;
994*1b191cb5SApple OSS Distributions }
995*1b191cb5SApple OSS Distributions goto out;
996*1b191cb5SApple OSS Distributions } else if (cdevsw[major(dev)].d_open == ptcopen) {
997*1b191cb5SApple OSS Distributions switch (cmd) {
998*1b191cb5SApple OSS Distributions case TIOCGPGRP:
999*1b191cb5SApple OSS Distributions /*
1000*1b191cb5SApple OSS Distributions * We aviod calling ttioctl on the controller since,
1001*1b191cb5SApple OSS Distributions * in that case, tp must be the controlling terminal.
1002*1b191cb5SApple OSS Distributions */
1003*1b191cb5SApple OSS Distributions *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1004*1b191cb5SApple OSS Distributions goto out;
1005*1b191cb5SApple OSS Distributions
1006*1b191cb5SApple OSS Distributions case TIOCPKT:
1007*1b191cb5SApple OSS Distributions if (*(int *)data) {
1008*1b191cb5SApple OSS Distributions if (pti->pt_flags & PF_UCNTL) {
1009*1b191cb5SApple OSS Distributions error = EINVAL;
1010*1b191cb5SApple OSS Distributions goto out;
1011*1b191cb5SApple OSS Distributions }
1012*1b191cb5SApple OSS Distributions pti->pt_flags |= PF_PKT;
1013*1b191cb5SApple OSS Distributions } else {
1014*1b191cb5SApple OSS Distributions pti->pt_flags &= ~PF_PKT;
1015*1b191cb5SApple OSS Distributions }
1016*1b191cb5SApple OSS Distributions goto out;
1017*1b191cb5SApple OSS Distributions
1018*1b191cb5SApple OSS Distributions case TIOCUCNTL:
1019*1b191cb5SApple OSS Distributions if (*(int *)data) {
1020*1b191cb5SApple OSS Distributions if (pti->pt_flags & PF_PKT) {
1021*1b191cb5SApple OSS Distributions error = EINVAL;
1022*1b191cb5SApple OSS Distributions goto out;
1023*1b191cb5SApple OSS Distributions }
1024*1b191cb5SApple OSS Distributions pti->pt_flags |= PF_UCNTL;
1025*1b191cb5SApple OSS Distributions } else {
1026*1b191cb5SApple OSS Distributions pti->pt_flags &= ~PF_UCNTL;
1027*1b191cb5SApple OSS Distributions }
1028*1b191cb5SApple OSS Distributions goto out;
1029*1b191cb5SApple OSS Distributions
1030*1b191cb5SApple OSS Distributions case TIOCSETP:
1031*1b191cb5SApple OSS Distributions case TIOCSETN:
1032*1b191cb5SApple OSS Distributions case TIOCSETD:
1033*1b191cb5SApple OSS Distributions case TIOCSETA_32:
1034*1b191cb5SApple OSS Distributions case TIOCSETAW_32:
1035*1b191cb5SApple OSS Distributions case TIOCSETAF_32:
1036*1b191cb5SApple OSS Distributions case TIOCSETA_64:
1037*1b191cb5SApple OSS Distributions case TIOCSETAW_64:
1038*1b191cb5SApple OSS Distributions case TIOCSETAF_64:
1039*1b191cb5SApple OSS Distributions ndflush(&tp->t_outq, tp->t_outq.c_cc);
1040*1b191cb5SApple OSS Distributions break;
1041*1b191cb5SApple OSS Distributions
1042*1b191cb5SApple OSS Distributions case TIOCSIG:
1043*1b191cb5SApple OSS Distributions if (*(unsigned int *)data >= NSIG ||
1044*1b191cb5SApple OSS Distributions *(unsigned int *)data == 0) {
1045*1b191cb5SApple OSS Distributions error = EINVAL;
1046*1b191cb5SApple OSS Distributions goto out;
1047*1b191cb5SApple OSS Distributions }
1048*1b191cb5SApple OSS Distributions if ((tp->t_lflag & NOFLSH) == 0) {
1049*1b191cb5SApple OSS Distributions ttyflush(tp, FREAD | FWRITE);
1050*1b191cb5SApple OSS Distributions }
1051*1b191cb5SApple OSS Distributions if ((*(unsigned int *)data == SIGINFO) &&
1052*1b191cb5SApple OSS Distributions ((tp->t_lflag & NOKERNINFO) == 0)) {
1053*1b191cb5SApple OSS Distributions ttyinfo_locked(tp);
1054*1b191cb5SApple OSS Distributions }
1055*1b191cb5SApple OSS Distributions /*
1056*1b191cb5SApple OSS Distributions * SAFE: All callers drop the lock on return and
1057*1b191cb5SApple OSS Distributions * SAFE: the linesw[] will short circut this call
1058*1b191cb5SApple OSS Distributions * SAFE: if the ioctl() is eaten before the lower
1059*1b191cb5SApple OSS Distributions * SAFE: level code gets to see it.
1060*1b191cb5SApple OSS Distributions */
1061*1b191cb5SApple OSS Distributions tty_pgsignal_locked(tp, *(unsigned int *)data, 1);
1062*1b191cb5SApple OSS Distributions goto out;
1063*1b191cb5SApple OSS Distributions
1064*1b191cb5SApple OSS Distributions case TIOCPTYGRANT: /* grantpt(3) */
1065*1b191cb5SApple OSS Distributions /*
1066*1b191cb5SApple OSS Distributions * Change the uid of the replica to that of the calling
1067*1b191cb5SApple OSS Distributions * thread, change the gid of the replica to GID_TTY,
1068*1b191cb5SApple OSS Distributions * change the mode to 0620 (rw--w----).
1069*1b191cb5SApple OSS Distributions */
1070*1b191cb5SApple OSS Distributions {
1071*1b191cb5SApple OSS Distributions error = _devfs_setattr(pti->pt_devhandle, 0620, kauth_getuid(), GID_TTY);
1072*1b191cb5SApple OSS Distributions if (major(dev) == driver->primary) {
1073*1b191cb5SApple OSS Distributions if (driver->mac_notify) {
1074*1b191cb5SApple OSS Distributions #if CONFIG_MACF
1075*1b191cb5SApple OSS Distributions if (!error) {
1076*1b191cb5SApple OSS Distributions tty_unlock(tp);
1077*1b191cb5SApple OSS Distributions mac_pty_notify_grant(p, tp, dev, NULL);
1078*1b191cb5SApple OSS Distributions tty_lock(tp);
1079*1b191cb5SApple OSS Distributions }
1080*1b191cb5SApple OSS Distributions #endif
1081*1b191cb5SApple OSS Distributions } else {
1082*1b191cb5SApple OSS Distributions error = 0;
1083*1b191cb5SApple OSS Distributions }
1084*1b191cb5SApple OSS Distributions }
1085*1b191cb5SApple OSS Distributions goto out;
1086*1b191cb5SApple OSS Distributions }
1087*1b191cb5SApple OSS Distributions
1088*1b191cb5SApple OSS Distributions case TIOCPTYGNAME: /* ptsname(3) */
1089*1b191cb5SApple OSS Distributions /*
1090*1b191cb5SApple OSS Distributions * Report the name of the replica device in *data
1091*1b191cb5SApple OSS Distributions * (128 bytes max.). Use the same template string
1092*1b191cb5SApple OSS Distributions * used for calling devfs_make_node() to create it.
1093*1b191cb5SApple OSS Distributions */
1094*1b191cb5SApple OSS Distributions pty_get_name(dev, data, 128);
1095*1b191cb5SApple OSS Distributions error = 0;
1096*1b191cb5SApple OSS Distributions goto out;
1097*1b191cb5SApple OSS Distributions
1098*1b191cb5SApple OSS Distributions case TIOCPTYUNLK: /* unlockpt(3) */
1099*1b191cb5SApple OSS Distributions /*
1100*1b191cb5SApple OSS Distributions * Unlock the replica device so that it can be opened.
1101*1b191cb5SApple OSS Distributions */
1102*1b191cb5SApple OSS Distributions if (major(dev) == driver->primary) {
1103*1b191cb5SApple OSS Distributions pti->pt_flags |= PF_UNLOCKED;
1104*1b191cb5SApple OSS Distributions }
1105*1b191cb5SApple OSS Distributions error = 0;
1106*1b191cb5SApple OSS Distributions goto out;
1107*1b191cb5SApple OSS Distributions }
1108*1b191cb5SApple OSS Distributions
1109*1b191cb5SApple OSS Distributions /*
1110*1b191cb5SApple OSS Distributions * Fail all other calls; pty primaries are not serial devices;
1111*1b191cb5SApple OSS Distributions * we only pretend they are when the replica side of the pty is
1112*1b191cb5SApple OSS Distributions * already open.
1113*1b191cb5SApple OSS Distributions */
1114*1b191cb5SApple OSS Distributions if (!allow_ext_ioctl) {
1115*1b191cb5SApple OSS Distributions error = ENOTTY;
1116*1b191cb5SApple OSS Distributions goto out;
1117*1b191cb5SApple OSS Distributions }
1118*1b191cb5SApple OSS Distributions }
1119*1b191cb5SApple OSS Distributions error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
1120*1b191cb5SApple OSS Distributions if (error == ENOTTY) {
1121*1b191cb5SApple OSS Distributions error = ttioctl_locked(tp, cmd, data, flag, p);
1122*1b191cb5SApple OSS Distributions if (error == ENOTTY) {
1123*1b191cb5SApple OSS Distributions if (pti->pt_flags & PF_UCNTL && (cmd & ~0xff) == UIOCCMD(0)) {
1124*1b191cb5SApple OSS Distributions /* Process the UIOCMD ioctl group */
1125*1b191cb5SApple OSS Distributions if (cmd & 0xff) {
1126*1b191cb5SApple OSS Distributions pti->pt_ucntl = (u_char)cmd;
1127*1b191cb5SApple OSS Distributions ptcwakeup(tp, FREAD);
1128*1b191cb5SApple OSS Distributions }
1129*1b191cb5SApple OSS Distributions error = 0;
1130*1b191cb5SApple OSS Distributions goto out;
1131*1b191cb5SApple OSS Distributions } else if (cmd == TIOCSBRK || cmd == TIOCCBRK) {
1132*1b191cb5SApple OSS Distributions /*
1133*1b191cb5SApple OSS Distributions * POSIX conformance; rdar://3936338
1134*1b191cb5SApple OSS Distributions *
1135*1b191cb5SApple OSS Distributions * Clear ENOTTY in the case of setting or
1136*1b191cb5SApple OSS Distributions * clearing a break failing because pty's
1137*1b191cb5SApple OSS Distributions * don't support break like real serial
1138*1b191cb5SApple OSS Distributions * ports.
1139*1b191cb5SApple OSS Distributions */
1140*1b191cb5SApple OSS Distributions error = 0;
1141*1b191cb5SApple OSS Distributions goto out;
1142*1b191cb5SApple OSS Distributions }
1143*1b191cb5SApple OSS Distributions }
1144*1b191cb5SApple OSS Distributions }
1145*1b191cb5SApple OSS Distributions
1146*1b191cb5SApple OSS Distributions /*
1147*1b191cb5SApple OSS Distributions * If external processing and packet mode send ioctl packet.
1148*1b191cb5SApple OSS Distributions */
1149*1b191cb5SApple OSS Distributions if ((tp->t_lflag & EXTPROC) && (pti->pt_flags & PF_PKT)) {
1150*1b191cb5SApple OSS Distributions switch (cmd) {
1151*1b191cb5SApple OSS Distributions case TIOCSETA_32:
1152*1b191cb5SApple OSS Distributions case TIOCSETAW_32:
1153*1b191cb5SApple OSS Distributions case TIOCSETAF_32:
1154*1b191cb5SApple OSS Distributions case TIOCSETA_64:
1155*1b191cb5SApple OSS Distributions case TIOCSETAW_64:
1156*1b191cb5SApple OSS Distributions case TIOCSETAF_64:
1157*1b191cb5SApple OSS Distributions case TIOCSETP:
1158*1b191cb5SApple OSS Distributions case TIOCSETN:
1159*1b191cb5SApple OSS Distributions case TIOCSETC:
1160*1b191cb5SApple OSS Distributions case TIOCSLTC:
1161*1b191cb5SApple OSS Distributions case TIOCLBIS:
1162*1b191cb5SApple OSS Distributions case TIOCLBIC:
1163*1b191cb5SApple OSS Distributions case TIOCLSET:
1164*1b191cb5SApple OSS Distributions pti->pt_send |= TIOCPKT_IOCTL;
1165*1b191cb5SApple OSS Distributions ptcwakeup(tp, FREAD);
1166*1b191cb5SApple OSS Distributions break;
1167*1b191cb5SApple OSS Distributions default:
1168*1b191cb5SApple OSS Distributions break;
1169*1b191cb5SApple OSS Distributions }
1170*1b191cb5SApple OSS Distributions }
1171*1b191cb5SApple OSS Distributions stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1172*1b191cb5SApple OSS Distributions && CCEQ(cc[VSTART], CTRL('q'));
1173*1b191cb5SApple OSS Distributions if (pti->pt_flags & PF_NOSTOP) {
1174*1b191cb5SApple OSS Distributions if (stop) {
1175*1b191cb5SApple OSS Distributions pti->pt_send &= ~TIOCPKT_NOSTOP;
1176*1b191cb5SApple OSS Distributions pti->pt_send |= TIOCPKT_DOSTOP;
1177*1b191cb5SApple OSS Distributions pti->pt_flags &= ~PF_NOSTOP;
1178*1b191cb5SApple OSS Distributions ptcwakeup(tp, FREAD);
1179*1b191cb5SApple OSS Distributions }
1180*1b191cb5SApple OSS Distributions } else {
1181*1b191cb5SApple OSS Distributions if (!stop) {
1182*1b191cb5SApple OSS Distributions pti->pt_send &= ~TIOCPKT_DOSTOP;
1183*1b191cb5SApple OSS Distributions pti->pt_send |= TIOCPKT_NOSTOP;
1184*1b191cb5SApple OSS Distributions pti->pt_flags |= PF_NOSTOP;
1185*1b191cb5SApple OSS Distributions ptcwakeup(tp, FREAD);
1186*1b191cb5SApple OSS Distributions }
1187*1b191cb5SApple OSS Distributions }
1188*1b191cb5SApple OSS Distributions out:
1189*1b191cb5SApple OSS Distributions tty_unlock(tp);
1190*1b191cb5SApple OSS Distributions
1191*1b191cb5SApple OSS Distributions return error;
1192*1b191cb5SApple OSS Distributions }
1193