]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty_pty.c
xnu-1228.7.58.tar.gz
[apple/xnu.git] / bsd / kern / tty_pty.c
1 /*
2 * Copyright (c) 1997-2006 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5 *
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.
14 *
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
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
25 *
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27 */
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>
72 #include <sys/proc_internal.h>
73 #include <sys/kauth.h>
74 #include <sys/tty.h>
75 #include <sys/conf.h>
76 #include <sys/file_internal.h>
77 #include <sys/uio.h>
78 #include <sys/kernel.h>
79 #include <sys/vnode.h>
80 #include <sys/user.h>
81 #include <sys/signalvar.h>
82
83 #define d_devtotty_t struct tty **
84
85 #ifdef d_stop_t
86 #undef d_stop_t
87 #endif
88 typedef void d_stop_t(struct tty *tp, int rw);
89
90 /* XXX function should be removed??? */
91 int pty_init(int n_ptys);
92
93 /* XXX should be a devfs function */
94 int _devfs_setattr(void * handle, unsigned short mode, uid_t uid, gid_t gid);
95
96 static void ptsstart(struct tty *tp);
97 static void ptcwakeup(struct tty *tp, int flag);
98
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;
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
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 */
124 /* All references to have been changed to indirections in the file */
125 __private_extern__ struct tty *pt_tty[NPTY] = { NULL };
126
127 static struct pt_ioctl {
128 int pt_flags;
129 struct selinfo pt_selr, pt_selw;
130 u_char pt_send;
131 u_char pt_ucntl;
132 void *pt_devhandle; /* slave device handle for grantpt() */
133 } pt_ioctl[NPTY]; /* XXX */
134 static 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
142 #ifndef DEVFS
143 int
144 pty_init(__unused int n_ptys)
145 {
146 return 0;
147 }
148 #else
149 #include <miscfs/devfs/devfs.h>
150 #define START_CHAR 'p'
151 #define HEX_BASE 16
152 int
153 pty_init(int n_ptys)
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;
164 pt_ioctl[m].pt_devhandle = devfs_make_node(makedev(4, m),
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 }
175 #endif /* DEVFS */
176
177 __private_extern__ int
178 ptsopen(dev_t dev, int flag, __unused int devtype, __unused struct proc *p)
179 {
180 struct tty *tp;
181 int error;
182 boolean_t funnel_state;
183
184 funnel_state = thread_funnel_set(kernel_flock, TRUE);
185 /*
186 * You will see this sort of code coming up in diffs later both
187 * the ttymalloc and the tp indirection.
188 */
189 if (minor(dev) >= npty) {
190 error = ENXIO;
191 goto out;
192 }
193 if (!pt_tty[minor(dev)]) {
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 }
202 } else
203 tp = pt_tty[minor(dev)];
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() */
212 } else if (tp->t_state&TS_XCLUDE && suser(kauth_cred_get(), NULL)) {
213 error = EBUSY;
214 goto out;
215 }
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)
224 goto out;
225 }
226 error = (*linesw[tp->t_line].l_open)(dev, tp);
227 if (error == 0)
228 ptcwakeup(tp, FREAD|FWRITE);
229 out:
230 (void) thread_funnel_set(kernel_flock, funnel_state);
231 return (error);
232 }
233
234 __private_extern__ int
235 ptsclose(dev_t dev, int flag, __unused int mode, __unused proc_t p)
236 {
237 struct tty *tp;
238 int err;
239 boolean_t funnel_state;
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
249 funnel_state = thread_funnel_set(kernel_flock, TRUE);
250
251 tp = pt_tty[minor(dev)];
252 #ifdef FIX_VSX_HANG
253 save_timeout = tp->t_timeout;
254 tp->t_timeout = 60;
255 #endif
256 err = (*linesw[tp->t_line].l_close)(tp, flag);
257 ptsstop(tp, FREAD|FWRITE);
258 (void) ttyclose(tp);
259 #ifdef FIX_VSX_HANG
260 tp->t_timeout = save_timeout;
261 #endif
262 (void) thread_funnel_set(kernel_flock, funnel_state);
263 return (err);
264 }
265
266 __private_extern__ int
267 ptsread(dev_t dev, struct uio *uio, int flag)
268 {
269 struct proc *p = current_proc();
270 struct tty *tp = pt_tty[minor(dev)];
271 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
272 int error = 0;
273 struct uthread *ut;
274 boolean_t funnel_state;
275 struct pgrp *pg;
276
277 funnel_state = thread_funnel_set(kernel_flock, TRUE);
278
279
280 ut = (struct uthread *)get_bsdthread_info(current_thread());
281 again:
282 if (pti->pt_flags & PF_REMOTE) {
283 while (isbackground(p, tp)) {
284 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
285 (ut->uu_sigmask & sigmask(SIGTTIN)) ||
286 p->p_lflag & P_LPPWAIT) {
287 error = EIO;
288 goto out;
289 }
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
305 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH | PTTYBLOCK, "ptsbg",
306 0);
307 if (error)
308 goto out;
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)
316 goto out;
317 goto again;
318 }
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);
326 cc = q_to_b(&tp->t_canq, (u_char *)buf, cc);
327 error = uiomove(buf, cc, uio);
328 if (error)
329 break;
330 }
331 if (tp->t_canq.c_cc == 1)
332 (void) getc(&tp->t_canq);
333 if (tp->t_canq.c_cc)
334 goto out;
335 } else
336 if (tp->t_oproc)
337 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
338 ptcwakeup(tp, FWRITE);
339 out:
340 (void) thread_funnel_set(kernel_flock, funnel_state);
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 */
349 __private_extern__ int
350 ptswrite(dev_t dev, struct uio *uio, int flag)
351 {
352 struct tty *tp;
353 int error;
354 boolean_t funnel_state;
355
356 funnel_state = thread_funnel_set(kernel_flock, TRUE);
357
358 tp = pt_tty[minor(dev)];
359 if (tp->t_oproc == 0)
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);
366 }
367
368 /*
369 * Start output on pseudo-tty.
370 * Wake up process selecting or sleeping for input from controlling tty.
371 */
372 static void
373 ptsstart(struct tty *tp)
374 {
375 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
376 boolean_t funnel_state;
377
378 funnel_state = thread_funnel_set(kernel_flock, TRUE);
379
380 if (tp->t_state & TS_TTSTOP)
381 goto out;
382 if (pti->pt_flags & PF_STOPPED) {
383 pti->pt_flags &= ~PF_STOPPED;
384 pti->pt_send = TIOCPKT_START;
385 }
386 ptcwakeup(tp, FREAD);
387 out:
388 (void) thread_funnel_set(kernel_flock, funnel_state);
389 return;
390 }
391
392 static void
393 ptcwakeup(struct tty *tp, int flag)
394 {
395 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
396 boolean_t funnel_state;
397
398 funnel_state = thread_funnel_set(kernel_flock, TRUE);
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 }
408 (void) thread_funnel_set(kernel_flock, funnel_state);
409 }
410
411 __private_extern__ int
412 ptcopen(dev_t dev, __unused int flag, __unused int devtype, __unused proc_t p)
413 {
414 struct tty *tp;
415 struct pt_ioctl *pti;
416 int error = 0;
417 boolean_t funnel_state;
418
419 funnel_state = thread_funnel_set(kernel_flock, TRUE);
420
421 if (minor(dev) >= npty) {
422 error = ENXIO;
423 goto out;
424 }
425 if(!pt_tty[minor(dev)]) {
426 tp = pt_tty[minor(dev)] = ttymalloc();
427 } else
428 tp = pt_tty[minor(dev)];
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;
432 goto out;
433 }
434 tp->t_oproc = ptsstart;
435 CLR(tp->t_state, TS_ZOMBIE);
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;
445 out:
446 (void) thread_funnel_set(kernel_flock, funnel_state);
447 return (error);
448 }
449
450 __private_extern__ int
451 ptcclose(dev_t dev, __unused int flags, __unused int fmt, __unused proc_t p)
452 {
453 struct tty *tp;
454 boolean_t funnel_state;
455
456 funnel_state = thread_funnel_set(kernel_flock, TRUE);
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 */
476
477 (void) thread_funnel_set(kernel_flock, funnel_state);
478 return (0);
479 }
480
481 __private_extern__ int
482 ptcread(dev_t dev, struct uio *uio, int flag)
483 {
484 struct tty *tp = pt_tty[minor(dev)];
485 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
486 char buf[BUFSIZ];
487 int error = 0, cc;
488 boolean_t funnel_state;
489
490 funnel_state = thread_funnel_set(kernel_flock, TRUE);
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)
503 goto out;
504 if (pti->pt_send & TIOCPKT_IOCTL) {
505 cc = min(uio_resid(uio),
506 sizeof(tp->t_termios));
507 uiomove((caddr_t)&tp->t_termios, cc,
508 uio);
509 }
510 pti->pt_send = 0;
511 goto out;
512 }
513 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
514 error = ureadc((int)pti->pt_ucntl, uio);
515 if (error)
516 goto out;
517 pti->pt_ucntl = 0;
518 goto out;
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)
524 goto out; /* EOF */
525 if (flag & IO_NDELAY) {
526 error = EWOULDBLOCK;
527 goto out;
528 }
529 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
530 if (error)
531 goto out;
532 }
533 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
534 error = ureadc(0, uio);
535 while (uio_resid(uio) > 0 && error == 0) {
536 cc = q_to_b(&tp->t_outq, (u_char *)buf, min(uio_resid(uio), BUFSIZ));
537 if (cc <= 0)
538 break;
539 error = uiomove(buf, cc, uio);
540 }
541 (*linesw[tp->t_line].l_start)(tp);
542
543 out:
544 (void) thread_funnel_set(kernel_flock, funnel_state);
545 return (error);
546 }
547
548 __private_extern__ void
549 ptsstop(struct tty *tp, int flush)
550 {
551 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
552 int flag;
553 boolean_t funnel_state;
554
555 funnel_state = thread_funnel_set(kernel_flock, TRUE);
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);
571
572 (void) thread_funnel_set(kernel_flock, funnel_state);
573 }
574
575 __private_extern__ int
576 ptcselect(dev_t dev, int rw, void *wql, struct proc *p)
577 {
578 struct tty *tp = pt_tty[minor(dev)];
579 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
580 int retval = 0;
581 boolean_t funnel_state;
582
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 }
589 switch (rw) {
590
591 case FREAD:
592 /*
593 * Need to block timeouts (ttrstart).
594 */
595 if ((tp->t_state&TS_ISOPEN) &&
596 tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
597 retval = 1;
598 goto out;
599 }
600 /* FALLTHROUGH */
601
602 case 0: /* exceptional */
603 if ((tp->t_state&TS_ISOPEN) &&
604 ((pti->pt_flags&PF_PKT && pti->pt_send) ||
605 (pti->pt_flags&PF_UCNTL && pti->pt_ucntl))) {
606 retval = 1;
607 goto out;
608 }
609 selrecord(p, &pti->pt_selr, wql);
610 break;
611
612
613 case FWRITE:
614 if (tp->t_state&TS_ISOPEN) {
615 if (pti->pt_flags & PF_REMOTE) {
616 if (tp->t_canq.c_cc == 0) {
617 retval = 1;
618 goto out;
619 }
620 } else {
621 if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) {
622 retval = 1;
623 goto out;
624 }
625 if (tp->t_canq.c_cc == 0 && (tp->t_lflag&ICANON)) {
626 retval = 1;
627 goto out;
628 }
629 }
630 }
631 selrecord(p, &pti->pt_selw, wql);
632 break;
633
634 }
635 out:
636 (void) thread_funnel_set(kernel_flock, funnel_state);
637 return (retval);
638 }
639
640 __private_extern__ int
641 ptcwrite(dev_t dev, struct uio *uio, int flag)
642 {
643 struct tty *tp = pt_tty[minor(dev)];
644 u_char *cp = NULL;
645 int cc = 0;
646 u_char locbuf[BUFSIZ];
647 int wcnt = 0;
648 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
649 int error = 0;
650 boolean_t funnel_state;
651
652 funnel_state = thread_funnel_set(kernel_flock, TRUE);
653
654 again:
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;
660 while ((uio_resid(uio) > 0 || cc > 0) &&
661 tp->t_canq.c_cc < TTYHOG - 1) {
662 if (cc == 0) {
663 cc = min(uio_resid(uio), BUFSIZ);
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)
668 goto out;
669 /* check again for safety */
670 if ((tp->t_state & TS_ISOPEN) == 0) {
671 /* adjust as usual */
672 uio_setresid(uio, (uio_resid(uio) + cc));
673 error = EIO;
674 goto out;
675 }
676 }
677 if (cc > 0) {
678 cc = b_to_q((u_char *)cp, cc, &tp->t_canq);
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 */
692 uio_setresid(uio, (uio_resid(uio) + cc));
693 (void) putc(0, &tp->t_canq);
694 ttwakeup(tp);
695 wakeup(TSA_PTS_READ(tp));
696 goto out;
697 }
698 while (uio_resid(uio) > 0 || cc > 0) {
699 if (cc == 0) {
700 cc = min(uio_resid(uio), BUFSIZ);
701 cp = locbuf;
702 error = uiomove((caddr_t)cp, cc, uio);
703 if (error)
704 goto out;
705 /* check again for safety */
706 if ((tp->t_state & TS_ISOPEN) == 0) {
707 /* adjust for data copied in but not written */
708 uio_setresid(uio, (uio_resid(uio) + cc));
709 error = EIO;
710 goto out;
711 }
712 }
713 while (cc > 0) {
714 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
715 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
716 wakeup(TSA_HUP_OR_INPUT(tp));
717 goto block;
718 }
719 (*linesw[tp->t_line].l_rint)(*cp++, tp);
720 wcnt++;
721 cc--;
722 }
723 cc = 0;
724 }
725 out:
726 (void) thread_funnel_set(kernel_flock, funnel_state);
727 return (error);
728 block:
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 */
735 uio_setresid(uio, (uio_resid(uio) + cc));
736 error = EIO;
737 goto out;
738 }
739 if (flag & IO_NDELAY) {
740 /* adjust for data copied in but not written */
741 uio_setresid(uio, (uio_resid(uio) + cc));
742 if (wcnt == 0)
743 error = EWOULDBLOCK;
744 goto out;
745 }
746 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
747 if (error) {
748 /* adjust for data copied in but not written */
749 uio_setresid(uio, (uio_resid(uio) + cc));
750 goto out;
751 }
752 goto again;
753 }
754
755 __private_extern__ int
756 ptyioctl(dev_t dev, u_long cmd, caddr_t data, int flag, struct proc *p)
757 {
758 struct tty *tp = pt_tty[minor(dev)];
759 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
760 u_char *cc = tp->t_cc;
761 int stop, error = 0;
762 boolean_t funnel_state;
763
764 funnel_state = thread_funnel_set(kernel_flock, TRUE);
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 }
790 goto out;
791 } else
792 if (cdevsw[major(dev)].d_open == ptcopen)
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;
801 goto out;
802
803 case TIOCPKT:
804 if (*(int *)data) {
805 if (pti->pt_flags & PF_UCNTL) {
806 error = EINVAL;
807 goto out;
808 }
809 pti->pt_flags |= PF_PKT;
810 } else
811 pti->pt_flags &= ~PF_PKT;
812 goto out;
813
814 case TIOCUCNTL:
815 if (*(int *)data) {
816 if (pti->pt_flags & PF_PKT) {
817 error = EINVAL;
818 goto out;
819 }
820 pti->pt_flags |= PF_UCNTL;
821 } else
822 pti->pt_flags &= ~PF_UCNTL;
823 goto out;
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);
831 goto out;
832
833 #if COMPAT_43_TTY
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 ||
846 *(unsigned int *)data == 0) {
847 error = EINVAL;
848 goto out;
849 }
850 if ((tp->t_lflag&NOFLSH) == 0)
851 ttyflush(tp, FREAD|FWRITE);
852 tty_pgsignal(tp, *(unsigned int *)data, 1);
853 if ((*(unsigned int *)data == SIGINFO) &&
854 ((tp->t_lflag&NOKERNINFO) == 0))
855 ttyinfo(tp);
856 goto out;
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;
887 }
888 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
889 if (error == ENOTTY) {
890 error = ttioctl(tp, cmd, data, flag, p);
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;
911 }
912 }
913 }
914
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:
923 #if COMPAT_43_TTY
924 case TIOCSETP:
925 case TIOCSETN:
926 #endif
927 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
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 }
957 out:
958 (void) thread_funnel_set(kernel_flock, funnel_state);
959 return (error);
960 }