]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty_pty.c
xnu-792.12.6.tar.gz
[apple/xnu.git] / bsd / kern / tty_pty.c
1 /*
2 * Copyright (c) 2006 Apple Computer, Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
14 * agreement.
15 *
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
18 * file.
19 *
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
27 *
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
29 */
30 /* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
31 /*
32 * Copyright (c) 1982, 1986, 1989, 1993
33 * The Regents of the University of California. All rights reserved.
34 *
35 * Redistribution and use in source and binary forms, with or without
36 * modification, are permitted provided that the following conditions
37 * are met:
38 * 1. Redistributions of source code must retain the above copyright
39 * notice, this list of conditions and the following disclaimer.
40 * 2. Redistributions in binary form must reproduce the above copyright
41 * notice, this list of conditions and the following disclaimer in the
42 * documentation and/or other materials provided with the distribution.
43 * 3. All advertising materials mentioning features or use of this software
44 * must display the following acknowledgement:
45 * This product includes software developed by the University of
46 * California, Berkeley and its contributors.
47 * 4. Neither the name of the University nor the names of its contributors
48 * may be used to endorse or promote products derived from this software
49 * without specific prior written permission.
50 *
51 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61 * SUCH DAMAGE.
62 *
63 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
64 */
65
66 /*
67 * Pseudo-teletype Driver
68 * (Actually two drivers, requiring two entries in 'cdevsw')
69 */
70 #include "pty.h" /* XXX */
71
72 #include <sys/param.h>
73 #include <sys/systm.h>
74 #include <sys/ioctl.h>
75 #include <sys/proc_internal.h>
76 #include <sys/kauth.h>
77 #include <sys/tty.h>
78 #include <sys/conf.h>
79 #include <sys/file_internal.h>
80 #include <sys/uio.h>
81 #include <sys/kernel.h>
82 #include <sys/vnode.h>
83 #include <sys/user.h>
84 #include <sys/signalvar.h>
85
86 #ifndef NeXT
87
88 #define FREE_BSDSTATIC static
89 #else
90 #define FREE_BSDSTATIC __private_extern__
91 #define d_devtotty_t struct tty **
92
93 #ifdef d_stop_t
94 #undef d_stop_t
95 #endif
96 typedef void d_stop_t(struct tty *tp, int rw);
97
98 #endif /* NeXT */
99
100 /* XXX function should be removed??? */
101 int pty_init(int n_ptys);
102
103 #ifdef notyet
104 static void ptyattach(int n);
105 #endif
106 static void ptsstart(struct tty *tp);
107 static void ptcwakeup(struct tty *tp, int flag);
108
109 FREE_BSDSTATIC d_open_t ptsopen;
110 FREE_BSDSTATIC d_close_t ptsclose;
111 FREE_BSDSTATIC d_read_t ptsread;
112 FREE_BSDSTATIC d_write_t ptswrite;
113 FREE_BSDSTATIC d_ioctl_t ptyioctl;
114 FREE_BSDSTATIC d_stop_t ptsstop;
115 FREE_BSDSTATIC d_devtotty_t ptydevtotty;
116 FREE_BSDSTATIC d_open_t ptcopen;
117 FREE_BSDSTATIC d_close_t ptcclose;
118 FREE_BSDSTATIC d_read_t ptcread;
119 FREE_BSDSTATIC d_write_t ptcwrite;
120 FREE_BSDSTATIC d_select_t ptcselect;
121
122 #ifndef NeXT
123 #define CDEV_MAJOR_S 5
124 #define CDEV_MAJOR_C 6
125 static struct cdevsw pts_cdevsw =
126 { ptsopen, ptsclose, ptsread, ptswrite, /*5*/
127 ptyioctl, ptsstop, nullreset, ptydevtotty,/* ttyp */
128 ttselect, nommap, NULL, "pts", NULL, -1 };
129
130 static struct cdevsw ptc_cdevsw =
131 { ptcopen, ptcclose, ptcread, ptcwrite, /*6*/
132 ptyioctl, nullstop, nullreset, ptydevtotty,/* ptyp */
133 ptcselect, nommap, NULL, "ptc", NULL, -1 };
134 #endif /* !NeXT */
135
136
137 #if NPTY == 1
138 #undef NPTY
139 #define NPTY 32 /* crude XXX */
140 #warning You have only one pty defined, redefining to 32.
141 #endif
142
143 #ifndef NeXT
144 #ifdef DEVFS
145 #define MAXUNITS (8 * 32)
146 static void *devfs_token_pts[MAXUNITS];
147 static void *devfs_token_ptc[MAXUNITS];
148 static const char jnames[] = "pqrsPQRS";
149 #if NPTY > MAXUNITS
150 #undef NPTY
151 #define NPTY MAXUNITS
152 #warning Can't have more than 256 pty's with DEVFS defined.
153 #endif /* NPTY > MAXUNITS */
154 #endif /* DEVFS */
155 #endif /* !NeXT */
156
157 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
158
159 /*
160 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
161 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
162 */
163 #ifndef NeXT
164 FREE_BSDSTATIC struct tty pt_tty[NPTY]; /* XXX */
165 #else /* NeXT */
166 /* NeXT All references to have been changed to indirections in the file */
167 FREE_BSDSTATIC struct tty *pt_tty[NPTY] = { NULL };
168 #endif /* ! NeXT */
169
170 static struct pt_ioctl {
171 int pt_flags;
172 struct selinfo pt_selr, pt_selw;
173 u_char pt_send;
174 u_char pt_ucntl;
175 } pt_ioctl[NPTY]; /* XXX */
176 static int npty = NPTY; /* for pstat -t */
177
178 #define PF_PKT 0x08 /* packet mode */
179 #define PF_STOPPED 0x10 /* user told stopped */
180 #define PF_REMOTE 0x20 /* remote and flow controlled input */
181 #define PF_NOSTOP 0x40
182 #define PF_UCNTL 0x80 /* user control mode */
183
184 #ifdef notyet
185 /*
186 * Establish n (or default if n is 1) ptys in the system.
187 *
188 * XXX cdevsw & pstat require the array `pty[]' to be an array
189 */
190 FREEBSD_STATIC void
191 ptyattach(n)
192 int n;
193 {
194 char *mem;
195 register u_long ntb;
196 #define DEFAULT_NPTY 32
197
198 /* maybe should allow 0 => none? */
199 if (n <= 1)
200 n = DEFAULT_NPTY;
201 ntb = n * sizeof(struct tty);
202 #ifndef NeXT
203 mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl),
204 M_DEVBUF, M_WAITOK);
205 #else
206 MALLOC(mem, char *, ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl),
207 M_DEVBUF, M_WAITOK);
208 #endif /* !NeXT */
209 pt_tty = (struct tty *)mem;
210 mem = (char *)ALIGN(mem + ntb);
211 pt_ioctl = (struct pt_ioctl *)mem;
212 npty = n;
213 }
214 #endif
215
216 #ifndef DEVFS
217 int
218 pty_init(__unused int n_ptys)
219 {
220 return 0;
221 }
222 #else
223 #include <miscfs/devfs/devfs.h>
224 #define START_CHAR 'p'
225 #define HEX_BASE 16
226 int
227 pty_init(int n_ptys)
228 {
229 int i;
230 int j;
231
232 /* create the pseudo tty device nodes */
233 for (j = 0; j < 10; j++) {
234 for (i = 0; i < HEX_BASE; i++) {
235 int m = j * HEX_BASE + i;
236 if (m == n_ptys)
237 goto done;
238 (void)devfs_make_node(makedev(4, m),
239 DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
240 "tty%c%x", j + START_CHAR, i);
241 (void)devfs_make_node(makedev(5, m),
242 DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
243 "pty%c%x", j + START_CHAR, i);
244 }
245 }
246 done:
247 return (0);
248 }
249 #endif /* DEVFS */
250
251 /*ARGSUSED*/
252 FREE_BSDSTATIC int
253 ptsopen(dev_t dev, int flag, __unused int devtype, __unused struct proc *p)
254 {
255 register struct tty *tp;
256 int error;
257 boolean_t funnel_state;
258
259 funnel_state = thread_funnel_set(kernel_flock, TRUE);
260 #ifndef NeXT
261 tp = &pt_tty[minor(dev)];
262 #else
263 /*
264 * You will see this sort of code coming up in diffs later both
265 * the ttymalloc and the tp indirection.
266 */
267 if (minor(dev) >= npty) {
268 error = ENXIO;
269 goto out;
270 }
271 if (!pt_tty[minor(dev)]) {
272 tp = pt_tty[minor(dev)] = ttymalloc();
273 } else
274 tp = pt_tty[minor(dev)];
275 #endif
276 if ((tp->t_state & TS_ISOPEN) == 0) {
277 ttychars(tp); /* Set up default chars */
278 tp->t_iflag = TTYDEF_IFLAG;
279 tp->t_oflag = TTYDEF_OFLAG;
280 tp->t_lflag = TTYDEF_LFLAG;
281 tp->t_cflag = TTYDEF_CFLAG;
282 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
283 ttsetwater(tp); /* would be done in xxparam() */
284 } else if (tp->t_state&TS_XCLUDE && suser(kauth_cred_get(), NULL)) {
285 error = EBUSY;
286 goto out;
287 }
288 if (tp->t_oproc) /* Ctrlr still around. */
289 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
290 while ((tp->t_state & TS_CARR_ON) == 0) {
291 if (flag&FNONBLOCK)
292 break;
293 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
294 "ptsopn", 0);
295 if (error)
296 goto out;
297 }
298 error = (*linesw[tp->t_line].l_open)(dev, tp);
299 if (error == 0)
300 ptcwakeup(tp, FREAD|FWRITE);
301 out:
302 (void) thread_funnel_set(kernel_flock, funnel_state);
303 return (error);
304 }
305
306 FREE_BSDSTATIC int
307 ptsclose(dev_t dev, int flag, __unused int mode, __unused proc_t p)
308 {
309 register struct tty *tp;
310 int err;
311 boolean_t funnel_state;
312
313 funnel_state = thread_funnel_set(kernel_flock, TRUE);
314
315 tp = pt_tty[minor(dev)];
316 err = (*linesw[tp->t_line].l_close)(tp, flag);
317 ptsstop(tp, FREAD|FWRITE);
318 (void) ttyclose(tp);
319
320 (void) thread_funnel_set(kernel_flock, funnel_state);
321 return (err);
322 }
323
324 FREE_BSDSTATIC int
325 ptsread(dev, uio, flag)
326 dev_t dev;
327 struct uio *uio;
328 int flag;
329 {
330 #ifndef NeXT
331 struct proc *p = curproc;
332 #else
333 struct proc *p = current_proc();
334 #endif /* NeXT */
335 register struct tty *tp = pt_tty[minor(dev)];
336 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
337 int error = 0;
338 struct uthread *ut;
339 boolean_t funnel_state;
340
341 funnel_state = thread_funnel_set(kernel_flock, TRUE);
342
343
344 ut = (struct uthread *)get_bsdthread_info(current_thread());
345 again:
346 if (pti->pt_flags & PF_REMOTE) {
347 while (isbackground(p, tp)) {
348 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
349 (ut->uu_sigmask & sigmask(SIGTTIN)) ||
350 p->p_pgrp->pg_jobc == 0 ||
351 p->p_flag & P_PPWAIT) {
352 error = EIO;
353 goto out;
354 }
355 pgsignal(p->p_pgrp, SIGTTIN, 1);
356 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH | PTTYBLOCK, "ptsbg",
357 0);
358 if (error)
359 goto out;
360 }
361 if (tp->t_canq.c_cc == 0) {
362 if (flag & IO_NDELAY)
363 return (EWOULDBLOCK);
364 error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
365 "ptsin", 0);
366 if (error)
367 goto out;
368 goto again;
369 }
370 while (tp->t_canq.c_cc > 1 && uio_resid(uio) > 0) {
371 int cc;
372 char buf[BUFSIZ];
373
374 cc = min(uio_resid(uio), BUFSIZ);
375 // Don't copy the very last byte
376 cc = min(cc, tp->t_canq.c_cc - 1);
377 cc = q_to_b(&tp->t_canq, buf, cc);
378 error = uiomove(buf, cc, uio);
379 if (error)
380 break;
381 }
382 if (tp->t_canq.c_cc == 1)
383 (void) getc(&tp->t_canq);
384 if (tp->t_canq.c_cc)
385 goto out;
386 } else
387 if (tp->t_oproc)
388 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
389 ptcwakeup(tp, FWRITE);
390 out:
391 (void) thread_funnel_set(kernel_flock, funnel_state);
392 return (error);
393 }
394
395 /*
396 * Write to pseudo-tty.
397 * Wakeups of controlling tty will happen
398 * indirectly, when tty driver calls ptsstart.
399 */
400 FREE_BSDSTATIC int
401 ptswrite(dev, uio, flag)
402 dev_t dev;
403 struct uio *uio;
404 int flag;
405 {
406 register struct tty *tp;
407 int error;
408 boolean_t funnel_state;
409
410 funnel_state = thread_funnel_set(kernel_flock, TRUE);
411
412 tp = pt_tty[minor(dev)];
413 if (tp->t_oproc == 0)
414 error = EIO;
415 else
416 error = (*linesw[tp->t_line].l_write)(tp, uio, flag);
417
418 (void) thread_funnel_set(kernel_flock, funnel_state);
419 return (error);
420 }
421
422 /*
423 * Start output on pseudo-tty.
424 * Wake up process selecting or sleeping for input from controlling tty.
425 */
426 static void
427 ptsstart(tp)
428 struct tty *tp;
429 {
430 register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
431 boolean_t funnel_state;
432
433 funnel_state = thread_funnel_set(kernel_flock, TRUE);
434
435 if (tp->t_state & TS_TTSTOP)
436 goto out;
437 if (pti->pt_flags & PF_STOPPED) {
438 pti->pt_flags &= ~PF_STOPPED;
439 pti->pt_send = TIOCPKT_START;
440 }
441 ptcwakeup(tp, FREAD);
442 out:
443 (void) thread_funnel_set(kernel_flock, funnel_state);
444 return;
445 }
446
447 static void
448 ptcwakeup(tp, flag)
449 struct tty *tp;
450 int flag;
451 {
452 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
453 boolean_t funnel_state;
454
455 funnel_state = thread_funnel_set(kernel_flock, TRUE);
456
457 if (flag & FREAD) {
458 selwakeup(&pti->pt_selr);
459 wakeup(TSA_PTC_READ(tp));
460 }
461 if (flag & FWRITE) {
462 selwakeup(&pti->pt_selw);
463 wakeup(TSA_PTC_WRITE(tp));
464 }
465 (void) thread_funnel_set(kernel_flock, funnel_state);
466 }
467
468 FREE_BSDSTATIC int
469 ptcopen(dev_t dev, __unused int flag, __unused int devtype, __unused proc_t p)
470 {
471 register struct tty *tp;
472 struct pt_ioctl *pti;
473 int error = 0;
474 boolean_t funnel_state;
475
476 funnel_state = thread_funnel_set(kernel_flock, TRUE);
477
478 if (minor(dev) >= npty) {
479 error = ENXIO;
480 goto out;
481 }
482 if(!pt_tty[minor(dev)]) {
483 tp = pt_tty[minor(dev)] = ttymalloc();
484 } else
485 tp = pt_tty[minor(dev)];
486 if (tp->t_oproc) {
487 error = EIO;
488 goto out;
489 }
490 tp->t_oproc = ptsstart;
491 CLR(tp->t_state, TS_ZOMBIE);
492 #ifdef sun4c
493 tp->t_stop = ptsstop;
494 #endif
495 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
496 tp->t_lflag &= ~EXTPROC;
497 pti = &pt_ioctl[minor(dev)];
498 pti->pt_flags = 0;
499 pti->pt_send = 0;
500 pti->pt_ucntl = 0;
501 out:
502 (void) thread_funnel_set(kernel_flock, funnel_state);
503 return (error);
504 }
505
506 FREE_BSDSTATIC int
507 ptcclose(dev_t dev, __unused int flags, __unused int fmt, __unused proc_t p)
508 {
509 register struct tty *tp;
510 boolean_t funnel_state;
511
512 funnel_state = thread_funnel_set(kernel_flock, TRUE);
513
514 tp = pt_tty[minor(dev)];
515 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
516
517 /*
518 * XXX MDMBUF makes no sense for ptys but would inhibit the above
519 * l_modem(). CLOCAL makes sense but isn't supported. Special
520 * l_modem()s that ignore carrier drop make no sense for ptys but
521 * may be in use because other parts of the line discipline make
522 * sense for ptys. Recover by doing everything that a normal
523 * ttymodem() would have done except for sending a SIGHUP.
524 */
525 if (tp->t_state & TS_ISOPEN) {
526 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
527 tp->t_state |= TS_ZOMBIE;
528 ttyflush(tp, FREAD | FWRITE);
529 }
530
531 tp->t_oproc = 0; /* mark closed */
532
533 (void) thread_funnel_set(kernel_flock, funnel_state);
534 return (0);
535 }
536
537 FREE_BSDSTATIC int
538 ptcread(dev, uio, flag)
539 dev_t dev;
540 struct uio *uio;
541 int flag;
542 {
543 register struct tty *tp = pt_tty[minor(dev)];
544 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
545 char buf[BUFSIZ];
546 int error = 0, cc;
547 boolean_t funnel_state;
548
549 funnel_state = thread_funnel_set(kernel_flock, TRUE);
550
551 /*
552 * We want to block until the slave
553 * is open, and there's something to read;
554 * but if we lost the slave or we're NBIO,
555 * then return the appropriate error instead.
556 */
557 for (;;) {
558 if (tp->t_state&TS_ISOPEN) {
559 if (pti->pt_flags&PF_PKT && pti->pt_send) {
560 error = ureadc((int)pti->pt_send, uio);
561 if (error)
562 goto out;
563 if (pti->pt_send & TIOCPKT_IOCTL) {
564 cc = min(uio_resid(uio),
565 sizeof(tp->t_termios));
566 uiomove((caddr_t)&tp->t_termios, cc,
567 uio);
568 }
569 pti->pt_send = 0;
570 goto out;
571 }
572 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
573 error = ureadc((int)pti->pt_ucntl, uio);
574 if (error)
575 goto out;
576 pti->pt_ucntl = 0;
577 goto out;
578 }
579 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
580 break;
581 }
582 if ((tp->t_state & TS_CONNECTED) == 0)
583 goto out; /* EOF */
584 if (flag & IO_NDELAY) {
585 error = EWOULDBLOCK;
586 goto out;
587 }
588 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
589 if (error)
590 goto out;
591 }
592 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
593 error = ureadc(0, uio);
594 while (uio_resid(uio) > 0 && error == 0) {
595 cc = q_to_b(&tp->t_outq, buf, min(uio_resid(uio), BUFSIZ));
596 if (cc <= 0)
597 break;
598 error = uiomove(buf, cc, uio);
599 }
600 (*linesw[tp->t_line].l_start)(tp);
601
602 out:
603 (void) thread_funnel_set(kernel_flock, funnel_state);
604 return (error);
605 }
606
607 FREE_BSDSTATIC void
608 ptsstop(tp, flush)
609 register struct tty *tp;
610 int flush;
611 {
612 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
613 int flag;
614 boolean_t funnel_state;
615
616 funnel_state = thread_funnel_set(kernel_flock, TRUE);
617
618 /* note: FLUSHREAD and FLUSHWRITE already ok */
619 if (flush == 0) {
620 flush = TIOCPKT_STOP;
621 pti->pt_flags |= PF_STOPPED;
622 } else
623 pti->pt_flags &= ~PF_STOPPED;
624 pti->pt_send |= flush;
625 /* change of perspective */
626 flag = 0;
627 if (flush & FREAD)
628 flag |= FWRITE;
629 if (flush & FWRITE)
630 flag |= FREAD;
631 ptcwakeup(tp, flag);
632
633 (void) thread_funnel_set(kernel_flock, funnel_state);
634 }
635
636 FREE_BSDSTATIC int
637 ptcselect(dev, rw, wql, p)
638 dev_t dev;
639 int rw;
640 void * wql;
641 struct proc *p;
642 {
643 register struct tty *tp = pt_tty[minor(dev)];
644 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
645 int retval = 0;
646 boolean_t funnel_state;
647
648 funnel_state = thread_funnel_set(kernel_flock, TRUE);
649
650 if ((tp->t_state & TS_CONNECTED) == 0) {
651 retval = 1;
652 goto out;
653 }
654 switch (rw) {
655
656 case FREAD:
657 /*
658 * Need to block timeouts (ttrstart).
659 */
660 if ((tp->t_state&TS_ISOPEN) &&
661 tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
662 retval = 1;
663 goto out;
664 }
665 /* FALLTHROUGH */
666
667 case 0: /* exceptional */
668 if ((tp->t_state&TS_ISOPEN) &&
669 ((pti->pt_flags&PF_PKT && pti->pt_send) ||
670 (pti->pt_flags&PF_UCNTL && pti->pt_ucntl))) {
671 retval = 1;
672 goto out;
673 }
674 selrecord(p, &pti->pt_selr, wql);
675 break;
676
677
678 case FWRITE:
679 if (tp->t_state&TS_ISOPEN) {
680 if (pti->pt_flags & PF_REMOTE) {
681 if (tp->t_canq.c_cc == 0) {
682 retval = 1;
683 goto out;
684 }
685 } else {
686 if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) {
687 retval = 1;
688 goto out;
689 }
690 if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON)) {
691 retval = 1;
692 goto out;
693 }
694 }
695 }
696 selrecord(p, &pti->pt_selw, wql);
697 break;
698
699 }
700 out:
701 (void) thread_funnel_set(kernel_flock, funnel_state);
702 return (retval);
703 }
704
705 FREE_BSDSTATIC int
706 ptcwrite(dev, uio, flag)
707 dev_t dev;
708 register struct uio *uio;
709 int flag;
710 {
711 register struct tty *tp = pt_tty[minor(dev)];
712 register u_char *cp = NULL;
713 register int cc = 0;
714 u_char locbuf[BUFSIZ];
715 int wcnt = 0;
716 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
717 int error = 0;
718 boolean_t funnel_state;
719
720 funnel_state = thread_funnel_set(kernel_flock, TRUE);
721
722 again:
723 if ((tp->t_state&TS_ISOPEN) == 0)
724 goto block;
725 if (pti->pt_flags & PF_REMOTE) {
726 if (tp->t_canq.c_cc)
727 goto block;
728 while ((uio_resid(uio) > 0 || cc > 0) &&
729 tp->t_canq.c_cc < TTYHOG - 1) {
730 if (cc == 0) {
731 cc = min(uio_resid(uio), BUFSIZ);
732 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
733 cp = locbuf;
734 error = uiomove((caddr_t)cp, cc, uio);
735 if (error)
736 goto out;
737 /* check again for safety */
738 if ((tp->t_state & TS_ISOPEN) == 0) {
739 /* adjust as usual */
740 uio_setresid(uio, (uio_resid(uio) + cc));
741 error = EIO;
742 goto out;
743 }
744 }
745 if (cc > 0) {
746 cc = b_to_q((char *)cp, cc, &tp->t_canq);
747 /*
748 * XXX we don't guarantee that the canq size
749 * is >= TTYHOG, so the above b_to_q() may
750 * leave some bytes uncopied. However, space
751 * is guaranteed for the null terminator if
752 * we don't fail here since (TTYHOG - 1) is
753 * not a multiple of CBSIZE.
754 */
755 if (cc > 0)
756 break;
757 }
758 }
759 /* adjust for data copied in but not written */
760 uio_setresid(uio, (uio_resid(uio) + cc));
761 (void) putc(0, &tp->t_canq);
762 ttwakeup(tp);
763 wakeup(TSA_PTS_READ(tp));
764 goto out;
765 }
766 while (uio_resid(uio) > 0 || cc > 0) {
767 if (cc == 0) {
768 cc = min(uio_resid(uio), BUFSIZ);
769 cp = locbuf;
770 error = uiomove((caddr_t)cp, cc, uio);
771 if (error)
772 goto out;
773 /* check again for safety */
774 if ((tp->t_state & TS_ISOPEN) == 0) {
775 /* adjust for data copied in but not written */
776 uio_setresid(uio, (uio_resid(uio) + cc));
777 error = EIO;
778 goto out;
779 }
780 }
781 while (cc > 0) {
782 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
783 (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
784 wakeup(TSA_HUP_OR_INPUT(tp));
785 goto block;
786 }
787 (*linesw[tp->t_line].l_rint)(*cp++, tp);
788 wcnt++;
789 cc--;
790 }
791 cc = 0;
792 }
793 out:
794 (void) thread_funnel_set(kernel_flock, funnel_state);
795 return (error);
796 block:
797 /*
798 * Come here to wait for slave to open, for space
799 * in outq, or space in rawq, or an empty canq.
800 */
801 if ((tp->t_state & TS_CONNECTED) == 0) {
802 /* adjust for data copied in but not written */
803 uio_setresid(uio, (uio_resid(uio) + cc));
804 error = EIO;
805 goto out;
806 }
807 if (flag & IO_NDELAY) {
808 /* adjust for data copied in but not written */
809 uio_setresid(uio, (uio_resid(uio) + cc));
810 if (wcnt == 0)
811 error = EWOULDBLOCK;
812 goto out;
813 }
814 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
815 if (error) {
816 /* adjust for data copied in but not written */
817 uio_setresid(uio, (uio_resid(uio) + cc));
818 goto out;
819 }
820 goto again;
821 }
822
823 #ifndef NeXT
824 /* XXX we eventually want to go to this model,
825 * but premier can't change the cdevsw */
826 static struct tty *
827 ptydevtotty(dev)
828 dev_t dev;
829 {
830 if (minor(dev) >= npty)
831 return (NULL);
832
833 return &pt_tty[minor(dev)];
834 }
835 #endif /* !NeXT */
836
837 /*ARGSUSED*/
838 FREE_BSDSTATIC int
839 #ifndef NeXT
840 ptyioctl(dev, cmd, data, flag)
841 dev_t dev;
842 int cmd;
843 caddr_t data;
844 int flag;
845 #else
846 ptyioctl(dev, cmd, data, flag, p)
847 dev_t dev;
848 u_long cmd;
849 caddr_t data;
850 int flag;
851 struct proc *p;
852 #endif
853 {
854 register struct tty *tp = pt_tty[minor(dev)];
855 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
856 register u_char *cc = tp->t_cc;
857 int stop, error = 0;
858 boolean_t funnel_state;
859
860 funnel_state = thread_funnel_set(kernel_flock, TRUE);
861
862 /*
863 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
864 * ttywflush(tp) will hang if there are characters in the outq.
865 */
866 if (cmd == TIOCEXT) {
867 /*
868 * When the EXTPROC bit is being toggled, we need
869 * to send an TIOCPKT_IOCTL if the packet driver
870 * is turned on.
871 */
872 if (*(int *)data) {
873 if (pti->pt_flags & PF_PKT) {
874 pti->pt_send |= TIOCPKT_IOCTL;
875 ptcwakeup(tp, FREAD);
876 }
877 tp->t_lflag |= EXTPROC;
878 } else {
879 if ((tp->t_lflag & EXTPROC) &&
880 (pti->pt_flags & PF_PKT)) {
881 pti->pt_send |= TIOCPKT_IOCTL;
882 ptcwakeup(tp, FREAD);
883 }
884 tp->t_lflag &= ~EXTPROC;
885 }
886 goto out;
887 } else
888 #ifndef NeXT
889 if (cdevsw[major(dev)]->d_open == ptcopen)
890 #else
891 if (cdevsw[major(dev)].d_open == ptcopen)
892 #endif
893 switch (cmd) {
894
895 case TIOCGPGRP:
896 /*
897 * We aviod calling ttioctl on the controller since,
898 * in that case, tp must be the controlling terminal.
899 */
900 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
901 goto out;
902
903 case TIOCPKT:
904 if (*(int *)data) {
905 if (pti->pt_flags & PF_UCNTL) {
906 error = EINVAL;
907 goto out;
908 }
909 pti->pt_flags |= PF_PKT;
910 } else
911 pti->pt_flags &= ~PF_PKT;
912 goto out;
913
914 case TIOCUCNTL:
915 if (*(int *)data) {
916 if (pti->pt_flags & PF_PKT) {
917 error = EINVAL;
918 goto out;
919 }
920 pti->pt_flags |= PF_UCNTL;
921 } else
922 pti->pt_flags &= ~PF_UCNTL;
923 goto out;
924
925 case TIOCREMOTE:
926 if (*(int *)data)
927 pti->pt_flags |= PF_REMOTE;
928 else
929 pti->pt_flags &= ~PF_REMOTE;
930 ttyflush(tp, FREAD|FWRITE);
931 goto out;
932
933 #if COMPAT_43_TTY
934 case TIOCSETP:
935 case TIOCSETN:
936 #endif
937 case TIOCSETD:
938 case TIOCSETA:
939 case TIOCSETAW:
940 case TIOCSETAF:
941 ndflush(&tp->t_outq, tp->t_outq.c_cc);
942 break;
943
944 case TIOCSIG:
945 if (*(unsigned int *)data >= NSIG ||
946 *(unsigned int *)data == 0) {
947 error = EINVAL;
948 goto out;
949 }
950 if ((tp->t_lflag&NOFLSH) == 0)
951 ttyflush(tp, FREAD|FWRITE);
952 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
953 if ((*(unsigned int *)data == SIGINFO) &&
954 ((tp->t_lflag&NOKERNINFO) == 0))
955 ttyinfo(tp);
956 goto out;
957 }
958 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
959 if (error == ENOTTY) {
960 error = ttioctl(tp, cmd, data, flag, p);
961 if (error == ENOTTY
962 && pti->pt_flags & PF_UCNTL && (cmd & ~0xff) == UIOCCMD(0)) {
963 /* Process the UIOCMD ioctl group */
964 if (cmd & 0xff) {
965 pti->pt_ucntl = (u_char)cmd;
966 ptcwakeup(tp, FREAD);
967 }
968 error = 0;
969 goto out;
970 }
971 }
972
973 /*
974 * If external processing and packet mode send ioctl packet.
975 */
976 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
977 switch(cmd) {
978 case TIOCSETA:
979 case TIOCSETAW:
980 case TIOCSETAF:
981 #if COMPAT_43_TTY
982 case TIOCSETP:
983 case TIOCSETN:
984 #endif
985 #if COMPAT_43_TTY || defined(COMPAT_SUNOS)
986 case TIOCSETC:
987 case TIOCSLTC:
988 case TIOCLBIS:
989 case TIOCLBIC:
990 case TIOCLSET:
991 #endif
992 pti->pt_send |= TIOCPKT_IOCTL;
993 ptcwakeup(tp, FREAD);
994 default:
995 break;
996 }
997 }
998 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
999 && CCEQ(cc[VSTART], CTRL('q'));
1000 if (pti->pt_flags & PF_NOSTOP) {
1001 if (stop) {
1002 pti->pt_send &= ~TIOCPKT_NOSTOP;
1003 pti->pt_send |= TIOCPKT_DOSTOP;
1004 pti->pt_flags &= ~PF_NOSTOP;
1005 ptcwakeup(tp, FREAD);
1006 }
1007 } else {
1008 if (!stop) {
1009 pti->pt_send &= ~TIOCPKT_DOSTOP;
1010 pti->pt_send |= TIOCPKT_NOSTOP;
1011 pti->pt_flags |= PF_NOSTOP;
1012 ptcwakeup(tp, FREAD);
1013 }
1014 }
1015 out:
1016 (void) thread_funnel_set(kernel_flock, funnel_state);
1017 return (error);
1018 }
1019
1020 #ifndef NeXT
1021 static ptc_devsw_installed = 0;
1022
1023 static void
1024 ptc_drvinit(void *unused)
1025 {
1026 #ifdef DEVFS
1027 int i,j,k;
1028 #endif
1029 dev_t dev;
1030
1031 if( ! ptc_devsw_installed ) {
1032 dev = makedev(CDEV_MAJOR_S, 0);
1033 cdevsw_add(&dev, &pts_cdevsw, NULL);
1034 dev = makedev(CDEV_MAJOR_C, 0);
1035 cdevsw_add(&dev, &ptc_cdevsw, NULL);
1036 ptc_devsw_installed = 1;
1037 #ifdef DEVFS
1038 for ( i = 0 ; i<NPTY ; i++ ) {
1039 j = i / 32;
1040 k = i % 32;
1041 devfs_token_pts[i] =
1042 devfs_add_devswf(&pts_cdevsw,i,
1043 DV_CHR,0,0,0666,
1044 "tty%c%n",jnames[j],k);
1045 devfs_token_ptc[i] =
1046 devfs_add_devswf(&ptc_cdevsw,i,
1047 DV_CHR,0,0,0666,
1048 "pty%c%n",jnames[j],k);
1049 }
1050 #endif
1051 }
1052 }
1053
1054 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
1055 #endif /* !NeXT */