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