]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/tty_pty.c
xnu-1228.12.14.tar.gz
[apple/xnu.git] / bsd / kern / tty_pty.c
CommitLineData
1c79356b 1/*
2d21ac55 2 * Copyright (c) 1997-2006 Apple Computer, Inc. All rights reserved.
5d5c5d0d 3 *
2d21ac55 4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
1c79356b 5 *
2d21ac55
A
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.
8f6c56a5 14 *
2d21ac55
A
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
17 *
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
8f6c56a5
A
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
2d21ac55
A
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.
8f6c56a5 25 *
2d21ac55 26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
1c79356b 27 */
1c79356b
A
28/*
29 * Copyright (c) 1982, 1986, 1989, 1993
30 * The Regents of the University of California. All rights reserved.
31 *
32 * Redistribution and use in source and binary forms, with or without
33 * modification, are permitted provided that the following conditions
34 * are met:
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.
47 *
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
58 * SUCH DAMAGE.
59 *
60 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
61 */
62
63/*
64 * Pseudo-teletype Driver
65 * (Actually two drivers, requiring two entries in 'cdevsw')
66 */
67#include "pty.h" /* XXX */
68
69#include <sys/param.h>
70#include <sys/systm.h>
71#include <sys/ioctl.h>
91447636
A
72#include <sys/proc_internal.h>
73#include <sys/kauth.h>
1c79356b
A
74#include <sys/tty.h>
75#include <sys/conf.h>
91447636 76#include <sys/file_internal.h>
1c79356b
A
77#include <sys/uio.h>
78#include <sys/kernel.h>
79#include <sys/vnode.h>
9bccf70c 80#include <sys/user.h>
1c79356b
A
81#include <sys/signalvar.h>
82
1c79356b 83#define d_devtotty_t struct tty **
55e303ae
A
84
85#ifdef d_stop_t
86#undef d_stop_t
87#endif
91447636 88typedef void d_stop_t(struct tty *tp, int rw);
55e303ae 89
91447636
A
90/* XXX function should be removed??? */
91int pty_init(int n_ptys);
92
2d21ac55
A
93/* XXX should be a devfs function */
94int _devfs_setattr(void * handle, unsigned short mode, uid_t uid, gid_t gid);
95
91447636
A
96static void ptsstart(struct tty *tp);
97static void ptcwakeup(struct tty *tp, int flag);
1c79356b 98
2d21ac55
A
99__private_extern__ d_open_t ptsopen;
100__private_extern__ d_close_t ptsclose;
101__private_extern__ d_read_t ptsread;
102__private_extern__ d_write_t ptswrite;
103__private_extern__ d_ioctl_t ptyioctl;
104__private_extern__ d_stop_t ptsstop;
105__private_extern__ d_devtotty_t ptydevtotty;
106__private_extern__ d_open_t ptcopen;
107__private_extern__ d_close_t ptcclose;
108__private_extern__ d_read_t ptcread;
109__private_extern__ d_write_t ptcwrite;
110__private_extern__ d_select_t ptcselect;
1c79356b
A
111
112#if NPTY == 1
113#undef NPTY
114#define NPTY 32 /* crude XXX */
115#warning You have only one pty defined, redefining to 32.
116#endif
117
1c79356b
A
118#define BUFSIZ 100 /* Chunk size iomoved to/from user */
119
120/*
121 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
122 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
123 */
2d21ac55
A
124/* All references to have been changed to indirections in the file */
125__private_extern__ struct tty *pt_tty[NPTY] = { NULL };
1c79356b
A
126
127static struct pt_ioctl {
128 int pt_flags;
129 struct selinfo pt_selr, pt_selw;
130 u_char pt_send;
131 u_char pt_ucntl;
2d21ac55 132 void *pt_devhandle; /* slave device handle for grantpt() */
1c79356b
A
133} pt_ioctl[NPTY]; /* XXX */
134static int npty = NPTY; /* for pstat -t */
135
136#define PF_PKT 0x08 /* packet mode */
137#define PF_STOPPED 0x10 /* user told stopped */
138#define PF_REMOTE 0x20 /* remote and flow controlled input */
139#define PF_NOSTOP 0x40
140#define PF_UCNTL 0x80 /* user control mode */
141
1c79356b 142#ifndef DEVFS
91447636
A
143int
144pty_init(__unused int n_ptys)
1c79356b
A
145{
146 return 0;
147}
148#else
149#include <miscfs/devfs/devfs.h>
150#define START_CHAR 'p'
151#define HEX_BASE 16
91447636
A
152int
153pty_init(int n_ptys)
1c79356b
A
154{
155 int i;
156 int j;
157
158 /* create the pseudo tty device nodes */
159 for (j = 0; j < 10; j++) {
160 for (i = 0; i < HEX_BASE; i++) {
161 int m = j * HEX_BASE + i;
162 if (m == n_ptys)
163 goto done;
2d21ac55 164 pt_ioctl[m].pt_devhandle = devfs_make_node(makedev(4, m),
1c79356b
A
165 DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
166 "tty%c%x", j + START_CHAR, i);
167 (void)devfs_make_node(makedev(5, m),
168 DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
169 "pty%c%x", j + START_CHAR, i);
170 }
171 }
172 done:
173 return (0);
174}
55e303ae 175#endif /* DEVFS */
1c79356b 176
2d21ac55 177__private_extern__ int
91447636 178ptsopen(dev_t dev, int flag, __unused int devtype, __unused struct proc *p)
1c79356b 179{
2d21ac55 180 struct tty *tp;
1c79356b 181 int error;
91447636 182 boolean_t funnel_state;
1c79356b 183
91447636 184 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b 185 /*
91447636 186 * You will see this sort of code coming up in diffs later both
1c79356b
A
187 * the ttymalloc and the tp indirection.
188 */
91447636
A
189 if (minor(dev) >= npty) {
190 error = ENXIO;
191 goto out;
192 }
1c79356b 193 if (!pt_tty[minor(dev)]) {
2d21ac55
A
194 /*
195 * If we can't allocate a new one, act as if we had run out
196 * of device nodes.
197 */
198 if ((tp = pt_tty[minor(dev)] = ttymalloc()) == NULL) {
199 error = ENXIO;
200 goto out;
201 }
1c79356b
A
202 } else
203 tp = pt_tty[minor(dev)];
1c79356b
A
204 if ((tp->t_state & TS_ISOPEN) == 0) {
205 ttychars(tp); /* Set up default chars */
206 tp->t_iflag = TTYDEF_IFLAG;
207 tp->t_oflag = TTYDEF_OFLAG;
208 tp->t_lflag = TTYDEF_LFLAG;
209 tp->t_cflag = TTYDEF_CFLAG;
210 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
211 ttsetwater(tp); /* would be done in xxparam() */
91447636
A
212 } else if (tp->t_state&TS_XCLUDE && suser(kauth_cred_get(), NULL)) {
213 error = EBUSY;
214 goto out;
215 }
1c79356b
A
216 if (tp->t_oproc) /* Ctrlr still around. */
217 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
218 while ((tp->t_state & TS_CARR_ON) == 0) {
219 if (flag&FNONBLOCK)
220 break;
221 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
222 "ptsopn", 0);
223 if (error)
91447636 224 goto out;
1c79356b
A
225 }
226 error = (*linesw[tp->t_line].l_open)(dev, tp);
227 if (error == 0)
228 ptcwakeup(tp, FREAD|FWRITE);
91447636
A
229out:
230 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
231 return (error);
232}
233
2d21ac55 234__private_extern__ int
91447636 235ptsclose(dev_t dev, int flag, __unused int mode, __unused proc_t p)
1c79356b 236{
2d21ac55 237 struct tty *tp;
1c79356b 238 int err;
91447636 239 boolean_t funnel_state;
2d21ac55
A
240 /*
241 * This is temporary until the VSX conformance tests
242 * are fixed. They are hanging with a deadlock
243 * where close(pts) will not complete without t_timeout set
244 */
245#define FIX_VSX_HANG 1
246#ifdef FIX_VSX_HANG
247 int save_timeout;
248#endif
91447636 249 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
250
251 tp = pt_tty[minor(dev)];
2d21ac55
A
252#ifdef FIX_VSX_HANG
253 save_timeout = tp->t_timeout;
254 tp->t_timeout = 60;
255#endif
1c79356b
A
256 err = (*linesw[tp->t_line].l_close)(tp, flag);
257 ptsstop(tp, FREAD|FWRITE);
258 (void) ttyclose(tp);
2d21ac55
A
259#ifdef FIX_VSX_HANG
260 tp->t_timeout = save_timeout;
261#endif
91447636 262 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
263 return (err);
264}
265
2d21ac55
A
266__private_extern__ int
267ptsread(dev_t dev, struct uio *uio, int flag)
1c79356b 268{
1c79356b 269 struct proc *p = current_proc();
2d21ac55
A
270 struct tty *tp = pt_tty[minor(dev)];
271 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
1c79356b 272 int error = 0;
9bccf70c 273 struct uthread *ut;
91447636 274 boolean_t funnel_state;
2d21ac55 275 struct pgrp *pg;
1c79356b 276
91447636
A
277 funnel_state = thread_funnel_set(kernel_flock, TRUE);
278
279
280 ut = (struct uthread *)get_bsdthread_info(current_thread());
1c79356b
A
281again:
282 if (pti->pt_flags & PF_REMOTE) {
283 while (isbackground(p, tp)) {
284 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
9bccf70c 285 (ut->uu_sigmask & sigmask(SIGTTIN)) ||
2d21ac55 286 p->p_lflag & P_LPPWAIT) {
91447636
A
287 error = EIO;
288 goto out;
289 }
2d21ac55
A
290
291
292 pg = proc_pgrp(p);
293 if (pg == PGRP_NULL) {
294 error = EIO;
295 goto out;
296 }
297 if (pg->pg_jobc == 0) {
298 pg_rele(pg);
299 error = EIO;
300 goto out;
301 }
302 pgsignal(pg, SIGTTIN, 1);
303 pg_rele(pg);
304
1c79356b
A
305 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH | PTTYBLOCK, "ptsbg",
306 0);
307 if (error)
91447636 308 goto out;
1c79356b
A
309 }
310 if (tp->t_canq.c_cc == 0) {
311 if (flag & IO_NDELAY)
312 return (EWOULDBLOCK);
313 error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
314 "ptsin", 0);
315 if (error)
91447636 316 goto out;
1c79356b
A
317 goto again;
318 }
91447636
A
319 while (tp->t_canq.c_cc > 1 && uio_resid(uio) > 0) {
320 int cc;
321 char buf[BUFSIZ];
322
323 cc = min(uio_resid(uio), BUFSIZ);
324 // Don't copy the very last byte
325 cc = min(cc, tp->t_canq.c_cc - 1);
2d21ac55 326 cc = q_to_b(&tp->t_canq, (u_char *)buf, cc);
91447636
A
327 error = uiomove(buf, cc, uio);
328 if (error)
1c79356b 329 break;
91447636 330 }
1c79356b
A
331 if (tp->t_canq.c_cc == 1)
332 (void) getc(&tp->t_canq);
333 if (tp->t_canq.c_cc)
91447636 334 goto out;
1c79356b
A
335 } else
336 if (tp->t_oproc)
337 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
338 ptcwakeup(tp, FWRITE);
91447636
A
339out:
340 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
341 return (error);
342}
343
344/*
345 * Write to pseudo-tty.
346 * Wakeups of controlling tty will happen
347 * indirectly, when tty driver calls ptsstart.
348 */
2d21ac55
A
349__private_extern__ int
350ptswrite(dev_t dev, struct uio *uio, int flag)
1c79356b 351{
2d21ac55 352 struct tty *tp;
91447636
A
353 int error;
354 boolean_t funnel_state;
355
356 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
357
358 tp = pt_tty[minor(dev)];
359 if (tp->t_oproc == 0)
91447636
A
360 error = EIO;
361 else
362 error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
363
364 (void) thread_funnel_set(kernel_flock, funnel_state);
365 return (error);
1c79356b
A
366}
367
368/*
369 * Start output on pseudo-tty.
370 * Wake up process selecting or sleeping for input from controlling tty.
371 */
372static void
2d21ac55 373ptsstart(struct tty *tp)
1c79356b 374{
2d21ac55 375 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
91447636
A
376 boolean_t funnel_state;
377
378 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
379
380 if (tp->t_state & TS_TTSTOP)
91447636 381 goto out;
1c79356b
A
382 if (pti->pt_flags & PF_STOPPED) {
383 pti->pt_flags &= ~PF_STOPPED;
384 pti->pt_send = TIOCPKT_START;
385 }
386 ptcwakeup(tp, FREAD);
91447636
A
387out:
388 (void) thread_funnel_set(kernel_flock, funnel_state);
389 return;
1c79356b
A
390}
391
392static void
2d21ac55 393ptcwakeup(struct tty *tp, int flag)
1c79356b
A
394{
395 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
91447636
A
396 boolean_t funnel_state;
397
398 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
399
400 if (flag & FREAD) {
401 selwakeup(&pti->pt_selr);
402 wakeup(TSA_PTC_READ(tp));
403 }
404 if (flag & FWRITE) {
405 selwakeup(&pti->pt_selw);
406 wakeup(TSA_PTC_WRITE(tp));
407 }
91447636 408 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
409}
410
2d21ac55 411__private_extern__ int
91447636 412ptcopen(dev_t dev, __unused int flag, __unused int devtype, __unused proc_t p)
1c79356b 413{
2d21ac55 414 struct tty *tp;
1c79356b 415 struct pt_ioctl *pti;
91447636
A
416 int error = 0;
417 boolean_t funnel_state;
1c79356b 418
91447636
A
419 funnel_state = thread_funnel_set(kernel_flock, TRUE);
420
421 if (minor(dev) >= npty) {
422 error = ENXIO;
423 goto out;
424 }
1c79356b
A
425 if(!pt_tty[minor(dev)]) {
426 tp = pt_tty[minor(dev)] = ttymalloc();
427 } else
428 tp = pt_tty[minor(dev)];
2d21ac55
A
429 /* If master is open OR slave is still draining, pty is still busy */
430 if (tp->t_oproc || (tp->t_state & TS_ISOPEN)) {
431 error = EBUSY;
91447636
A
432 goto out;
433 }
1c79356b 434 tp->t_oproc = ptsstart;
743b1565 435 CLR(tp->t_state, TS_ZOMBIE);
1c79356b
A
436#ifdef sun4c
437 tp->t_stop = ptsstop;
438#endif
439 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
440 tp->t_lflag &= ~EXTPROC;
441 pti = &pt_ioctl[minor(dev)];
442 pti->pt_flags = 0;
443 pti->pt_send = 0;
444 pti->pt_ucntl = 0;
91447636
A
445out:
446 (void) thread_funnel_set(kernel_flock, funnel_state);
447 return (error);
1c79356b
A
448}
449
2d21ac55 450__private_extern__ int
91447636 451ptcclose(dev_t dev, __unused int flags, __unused int fmt, __unused proc_t p)
1c79356b 452{
2d21ac55 453 struct tty *tp;
91447636
A
454 boolean_t funnel_state;
455
456 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
457
458 tp = pt_tty[minor(dev)];
459 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
460
461 /*
462 * XXX MDMBUF makes no sense for ptys but would inhibit the above
463 * l_modem(). CLOCAL makes sense but isn't supported. Special
464 * l_modem()s that ignore carrier drop make no sense for ptys but
465 * may be in use because other parts of the line discipline make
466 * sense for ptys. Recover by doing everything that a normal
467 * ttymodem() would have done except for sending a SIGHUP.
468 */
469 if (tp->t_state & TS_ISOPEN) {
470 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
471 tp->t_state |= TS_ZOMBIE;
472 ttyflush(tp, FREAD | FWRITE);
473 }
474
475 tp->t_oproc = 0; /* mark closed */
91447636
A
476
477 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
478 return (0);
479}
480
2d21ac55
A
481__private_extern__ int
482ptcread(dev_t dev, struct uio *uio, int flag)
1c79356b 483{
2d21ac55 484 struct tty *tp = pt_tty[minor(dev)];
1c79356b
A
485 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
486 char buf[BUFSIZ];
487 int error = 0, cc;
91447636
A
488 boolean_t funnel_state;
489
490 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
491
492 /*
493 * We want to block until the slave
494 * is open, and there's something to read;
495 * but if we lost the slave or we're NBIO,
496 * then return the appropriate error instead.
497 */
498 for (;;) {
499 if (tp->t_state&TS_ISOPEN) {
500 if (pti->pt_flags&PF_PKT && pti->pt_send) {
501 error = ureadc((int)pti->pt_send, uio);
502 if (error)
91447636 503 goto out;
1c79356b 504 if (pti->pt_send & TIOCPKT_IOCTL) {
91447636 505 cc = min(uio_resid(uio),
1c79356b
A
506 sizeof(tp->t_termios));
507 uiomove((caddr_t)&tp->t_termios, cc,
508 uio);
509 }
510 pti->pt_send = 0;
91447636 511 goto out;
1c79356b
A
512 }
513 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
514 error = ureadc((int)pti->pt_ucntl, uio);
515 if (error)
91447636 516 goto out;
1c79356b 517 pti->pt_ucntl = 0;
91447636 518 goto out;
1c79356b
A
519 }
520 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
521 break;
522 }
523 if ((tp->t_state & TS_CONNECTED) == 0)
91447636
A
524 goto out; /* EOF */
525 if (flag & IO_NDELAY) {
526 error = EWOULDBLOCK;
527 goto out;
528 }
1c79356b
A
529 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
530 if (error)
91447636 531 goto out;
1c79356b
A
532 }
533 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
534 error = ureadc(0, uio);
91447636 535 while (uio_resid(uio) > 0 && error == 0) {
2d21ac55 536 cc = q_to_b(&tp->t_outq, (u_char *)buf, min(uio_resid(uio), BUFSIZ));
1c79356b
A
537 if (cc <= 0)
538 break;
539 error = uiomove(buf, cc, uio);
540 }
91447636
A
541 (*linesw[tp->t_line].l_start)(tp);
542
543out:
544 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
545 return (error);
546}
547
2d21ac55
A
548__private_extern__ void
549ptsstop(struct tty *tp, int flush)
1c79356b
A
550{
551 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
552 int flag;
91447636
A
553 boolean_t funnel_state;
554
555 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
556
557 /* note: FLUSHREAD and FLUSHWRITE already ok */
558 if (flush == 0) {
559 flush = TIOCPKT_STOP;
560 pti->pt_flags |= PF_STOPPED;
561 } else
562 pti->pt_flags &= ~PF_STOPPED;
563 pti->pt_send |= flush;
564 /* change of perspective */
565 flag = 0;
566 if (flush & FREAD)
567 flag |= FWRITE;
568 if (flush & FWRITE)
569 flag |= FREAD;
570 ptcwakeup(tp, flag);
91447636
A
571
572 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
573}
574
2d21ac55
A
575__private_extern__ int
576ptcselect(dev_t dev, int rw, void *wql, struct proc *p)
1c79356b 577{
2d21ac55 578 struct tty *tp = pt_tty[minor(dev)];
1c79356b 579 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
91447636
A
580 int retval = 0;
581 boolean_t funnel_state;
1c79356b 582
91447636
A
583 funnel_state = thread_funnel_set(kernel_flock, TRUE);
584
585 if ((tp->t_state & TS_CONNECTED) == 0) {
586 retval = 1;
587 goto out;
588 }
1c79356b
A
589 switch (rw) {
590
591 case FREAD:
592 /*
593 * Need to block timeouts (ttrstart).
594 */
1c79356b
A
595 if ((tp->t_state&TS_ISOPEN) &&
596 tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
91447636
A
597 retval = 1;
598 goto out;
1c79356b 599 }
1c79356b
A
600 /* FALLTHROUGH */
601
602 case 0: /* exceptional */
603 if ((tp->t_state&TS_ISOPEN) &&
604 ((pti->pt_flags&PF_PKT && pti->pt_send) ||
91447636
A
605 (pti->pt_flags&PF_UCNTL && pti->pt_ucntl))) {
606 retval = 1;
607 goto out;
608 }
0b4e3aa0 609 selrecord(p, &pti->pt_selr, wql);
1c79356b
A
610 break;
611
612
613 case FWRITE:
614 if (tp->t_state&TS_ISOPEN) {
615 if (pti->pt_flags & PF_REMOTE) {
91447636
A
616 if (tp->t_canq.c_cc == 0) {
617 retval = 1;
618 goto out;
619 }
1c79356b 620 } else {
91447636
A
621 if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) {
622 retval = 1;
623 goto out;
624 }
2d21ac55 625 if (tp->t_canq.c_cc == 0 && (tp->t_lflag&ICANON)) {
91447636
A
626 retval = 1;
627 goto out;
628 }
1c79356b
A
629 }
630 }
0b4e3aa0 631 selrecord(p, &pti->pt_selw, wql);
1c79356b
A
632 break;
633
634 }
91447636
A
635out:
636 (void) thread_funnel_set(kernel_flock, funnel_state);
637 return (retval);
1c79356b
A
638}
639
2d21ac55
A
640__private_extern__ int
641ptcwrite(dev_t dev, struct uio *uio, int flag)
1c79356b 642{
2d21ac55
A
643 struct tty *tp = pt_tty[minor(dev)];
644 u_char *cp = NULL;
645 int cc = 0;
1c79356b 646 u_char locbuf[BUFSIZ];
91447636 647 int wcnt = 0;
1c79356b
A
648 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
649 int error = 0;
91447636
A
650 boolean_t funnel_state;
651
652 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
653
654again:
655 if ((tp->t_state&TS_ISOPEN) == 0)
656 goto block;
657 if (pti->pt_flags & PF_REMOTE) {
658 if (tp->t_canq.c_cc)
659 goto block;
91447636 660 while ((uio_resid(uio) > 0 || cc > 0) &&
1c79356b
A
661 tp->t_canq.c_cc < TTYHOG - 1) {
662 if (cc == 0) {
91447636 663 cc = min(uio_resid(uio), BUFSIZ);
1c79356b
A
664 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
665 cp = locbuf;
666 error = uiomove((caddr_t)cp, cc, uio);
667 if (error)
91447636 668 goto out;
1c79356b
A
669 /* check again for safety */
670 if ((tp->t_state & TS_ISOPEN) == 0) {
671 /* adjust as usual */
91447636
A
672 uio_setresid(uio, (uio_resid(uio) + cc));
673 error = EIO;
674 goto out;
1c79356b
A
675 }
676 }
677 if (cc > 0) {
2d21ac55 678 cc = b_to_q((u_char *)cp, cc, &tp->t_canq);
1c79356b
A
679 /*
680 * XXX we don't guarantee that the canq size
681 * is >= TTYHOG, so the above b_to_q() may
682 * leave some bytes uncopied. However, space
683 * is guaranteed for the null terminator if
684 * we don't fail here since (TTYHOG - 1) is
685 * not a multiple of CBSIZE.
686 */
687 if (cc > 0)
688 break;
689 }
690 }
691 /* adjust for data copied in but not written */
91447636 692 uio_setresid(uio, (uio_resid(uio) + cc));
1c79356b
A
693 (void) putc(0, &tp->t_canq);
694 ttwakeup(tp);
695 wakeup(TSA_PTS_READ(tp));
91447636 696 goto out;
1c79356b 697 }
91447636 698 while (uio_resid(uio) > 0 || cc > 0) {
1c79356b 699 if (cc == 0) {
91447636 700 cc = min(uio_resid(uio), BUFSIZ);
1c79356b
A
701 cp = locbuf;
702 error = uiomove((caddr_t)cp, cc, uio);
703 if (error)
91447636 704 goto out;
1c79356b
A
705 /* check again for safety */
706 if ((tp->t_state & TS_ISOPEN) == 0) {
707 /* adjust for data copied in but not written */
91447636
A
708 uio_setresid(uio, (uio_resid(uio) + cc));
709 error = EIO;
710 goto out;
1c79356b
A
711 }
712 }
713 while (cc > 0) {
714 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
2d21ac55 715 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
1c79356b
A
716 wakeup(TSA_HUP_OR_INPUT(tp));
717 goto block;
718 }
719 (*linesw[tp->t_line].l_rint)(*cp++, tp);
91447636 720 wcnt++;
1c79356b
A
721 cc--;
722 }
723 cc = 0;
724 }
91447636
A
725out:
726 (void) thread_funnel_set(kernel_flock, funnel_state);
727 return (error);
1c79356b
A
728block:
729 /*
730 * Come here to wait for slave to open, for space
731 * in outq, or space in rawq, or an empty canq.
732 */
733 if ((tp->t_state & TS_CONNECTED) == 0) {
734 /* adjust for data copied in but not written */
91447636
A
735 uio_setresid(uio, (uio_resid(uio) + cc));
736 error = EIO;
737 goto out;
1c79356b
A
738 }
739 if (flag & IO_NDELAY) {
740 /* adjust for data copied in but not written */
91447636
A
741 uio_setresid(uio, (uio_resid(uio) + cc));
742 if (wcnt == 0)
743 error = EWOULDBLOCK;
744 goto out;
1c79356b
A
745 }
746 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
747 if (error) {
748 /* adjust for data copied in but not written */
91447636
A
749 uio_setresid(uio, (uio_resid(uio) + cc));
750 goto out;
1c79356b
A
751 }
752 goto again;
753}
754
2d21ac55
A
755__private_extern__ int
756ptyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
1c79356b 757{
2d21ac55
A
758 struct tty *tp = pt_tty[minor(dev)];
759 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
760 u_char *cc = tp->t_cc;
91447636
A
761 int stop, error = 0;
762 boolean_t funnel_state;
763
764 funnel_state = thread_funnel_set(kernel_flock, TRUE);
1c79356b
A
765
766 /*
767 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
768 * ttywflush(tp) will hang if there are characters in the outq.
769 */
770 if (cmd == TIOCEXT) {
771 /*
772 * When the EXTPROC bit is being toggled, we need
773 * to send an TIOCPKT_IOCTL if the packet driver
774 * is turned on.
775 */
776 if (*(int *)data) {
777 if (pti->pt_flags & PF_PKT) {
778 pti->pt_send |= TIOCPKT_IOCTL;
779 ptcwakeup(tp, FREAD);
780 }
781 tp->t_lflag |= EXTPROC;
782 } else {
783 if ((tp->t_lflag & EXTPROC) &&
784 (pti->pt_flags & PF_PKT)) {
785 pti->pt_send |= TIOCPKT_IOCTL;
786 ptcwakeup(tp, FREAD);
787 }
788 tp->t_lflag &= ~EXTPROC;
789 }
91447636 790 goto out;
1c79356b 791 } else
1c79356b 792 if (cdevsw[major(dev)].d_open == ptcopen)
1c79356b
A
793 switch (cmd) {
794
795 case TIOCGPGRP:
796 /*
797 * We aviod calling ttioctl on the controller since,
798 * in that case, tp must be the controlling terminal.
799 */
800 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
91447636 801 goto out;
1c79356b
A
802
803 case TIOCPKT:
804 if (*(int *)data) {
91447636
A
805 if (pti->pt_flags & PF_UCNTL) {
806 error = EINVAL;
807 goto out;
808 }
1c79356b
A
809 pti->pt_flags |= PF_PKT;
810 } else
811 pti->pt_flags &= ~PF_PKT;
91447636 812 goto out;
1c79356b
A
813
814 case TIOCUCNTL:
815 if (*(int *)data) {
91447636
A
816 if (pti->pt_flags & PF_PKT) {
817 error = EINVAL;
818 goto out;
819 }
1c79356b
A
820 pti->pt_flags |= PF_UCNTL;
821 } else
822 pti->pt_flags &= ~PF_UCNTL;
91447636 823 goto out;
1c79356b
A
824
825 case TIOCREMOTE:
826 if (*(int *)data)
827 pti->pt_flags |= PF_REMOTE;
828 else
829 pti->pt_flags &= ~PF_REMOTE;
830 ttyflush(tp, FREAD|FWRITE);
91447636 831 goto out;
1c79356b 832
91447636 833#if COMPAT_43_TTY
1c79356b
A
834 case TIOCSETP:
835 case TIOCSETN:
836#endif
837 case TIOCSETD:
838 case TIOCSETA:
839 case TIOCSETAW:
840 case TIOCSETAF:
841 ndflush(&tp->t_outq, tp->t_outq.c_cc);
842 break;
843
844 case TIOCSIG:
845 if (*(unsigned int *)data >= NSIG ||
91447636
A
846 *(unsigned int *)data == 0) {
847 error = EINVAL;
848 goto out;
849 }
1c79356b
A
850 if ((tp->t_lflag&NOFLSH) == 0)
851 ttyflush(tp, FREAD|FWRITE);
2d21ac55 852 tty_pgsignal(tp, *(unsigned int *)data, 1);
1c79356b
A
853 if ((*(unsigned int *)data == SIGINFO) &&
854 ((tp->t_lflag&NOKERNINFO) == 0))
855 ttyinfo(tp);
91447636 856 goto out;
2d21ac55
A
857
858 case TIOCPTYGRANT: /* grantpt(3) */
859 /*
860 * Change the uid of the slave to that of the calling
861 * thread, change the gid of the slave to GID_TTY,
862 * change the mode to 0620 (rw--w----).
863 */
864 {
865 _devfs_setattr(pti->pt_devhandle, 0620, kauth_getuid(), GID_TTY);
866 goto out;
867 }
868
869 case TIOCPTYGNAME: /* ptsname(3) */
870 /*
871 * Report the name of the slave device in *data
872 * (128 bytes max.). Use the same derivation method
873 * used for calling devfs_make_node() to create it.
874 */
875 snprintf(data, 128, "/dev/tty%c%x",
876 START_CHAR + (minor(dev) / HEX_BASE),
877 minor(dev) % HEX_BASE);
878 error = 0;
879 goto out;
880
881 case TIOCPTYUNLK: /* unlockpt(3) */
882 /*
883 * Unlock the slave device so that it can be opened.
884 */
885 error = 0;
886 goto out;
1c79356b
A
887 }
888 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
91447636
A
889 if (error == ENOTTY) {
890 error = ttioctl(tp, cmd, data, flag, p);
2d21ac55
A
891 if (error == ENOTTY) {
892 if (pti->pt_flags & PF_UCNTL && (cmd & ~0xff) == UIOCCMD(0)) {
893 /* Process the UIOCMD ioctl group */
894 if (cmd & 0xff) {
895 pti->pt_ucntl = (u_char)cmd;
896 ptcwakeup(tp, FREAD);
897 }
898 error = 0;
899 goto out;
900 } else if (cmd == TIOCSBRK || cmd == TIOCCBRK) {
901 /*
902 * POSIX conformance; rdar://3936338
903 *
904 * Clear ENOTTY in the case of setting or
905 * clearing a break failing because pty's
906 * don't support break like real serial
907 * ports.
908 */
909 error = 0;
910 goto out;
1c79356b 911 }
1c79356b 912 }
1c79356b 913 }
91447636 914
1c79356b
A
915 /*
916 * If external processing and packet mode send ioctl packet.
917 */
918 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
919 switch(cmd) {
920 case TIOCSETA:
921 case TIOCSETAW:
922 case TIOCSETAF:
91447636 923#if COMPAT_43_TTY
1c79356b
A
924 case TIOCSETP:
925 case TIOCSETN:
926#endif
91447636 927#if COMPAT_43_TTY || defined(COMPAT_SUNOS)
1c79356b
A
928 case TIOCSETC:
929 case TIOCSLTC:
930 case TIOCLBIS:
931 case TIOCLBIC:
932 case TIOCLSET:
933#endif
934 pti->pt_send |= TIOCPKT_IOCTL;
935 ptcwakeup(tp, FREAD);
936 default:
937 break;
938 }
939 }
940 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
941 && CCEQ(cc[VSTART], CTRL('q'));
942 if (pti->pt_flags & PF_NOSTOP) {
943 if (stop) {
944 pti->pt_send &= ~TIOCPKT_NOSTOP;
945 pti->pt_send |= TIOCPKT_DOSTOP;
946 pti->pt_flags &= ~PF_NOSTOP;
947 ptcwakeup(tp, FREAD);
948 }
949 } else {
950 if (!stop) {
951 pti->pt_send &= ~TIOCPKT_DOSTOP;
952 pti->pt_send |= TIOCPKT_NOSTOP;
953 pti->pt_flags |= PF_NOSTOP;
954 ptcwakeup(tp, FREAD);
955 }
956 }
91447636
A
957out:
958 (void) thread_funnel_set(kernel_flock, funnel_state);
1c79356b
A
959 return (error);
960}