tty_pty.c revision 1.100 1 /* $NetBSD: tty_pty.c,v 1.100 2007/03/12 21:33:07 ad Exp $ */
2
3 /*
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 *
31 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
32 */
33
34 /*
35 * Pseudo-teletype Driver
36 * (Actually two drivers, requiring two entries in 'cdevsw')
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.100 2007/03/12 21:33:07 ad Exp $");
41
42 #include "opt_compat_sunos.h"
43 #include "opt_ptm.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/ioctl.h>
48 #include <sys/proc.h>
49 #include <sys/tty.h>
50 #include <sys/stat.h>
51 #include <sys/file.h>
52 #include <sys/kernel.h>
53 #include <sys/vnode.h>
54 #include <sys/namei.h>
55 #include <sys/signalvar.h>
56 #include <sys/uio.h>
57 #include <sys/filedesc.h>
58 #include <sys/conf.h>
59 #include <sys/poll.h>
60 #include <sys/malloc.h>
61 #include <sys/pty.h>
62 #include <sys/kauth.h>
63
64 #define DEFAULT_NPTYS 16 /* default number of initial ptys */
65 #define DEFAULT_MAXPTYS 992 /* default maximum number of ptys */
66
67 #define BUFSIZ 100 /* Chunk size iomoved to/from user */
68
69 struct pt_softc {
70 struct tty *pt_tty;
71 int pt_flags;
72 struct selinfo pt_selr, pt_selw;
73 u_char pt_send;
74 u_char pt_ucntl;
75 };
76
77 static struct pt_softc **pt_softc = NULL; /* pty array */
78 static int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */
79 kmutex_t pt_softc_mutex;
80 int npty = 0; /* for pstat -t */
81
82 #define PF_PKT 0x08 /* packet mode */
83 #define PF_STOPPED 0x10 /* user told stopped */
84 #define PF_REMOTE 0x20 /* remote and flow controlled input */
85 #define PF_NOSTOP 0x40
86 #define PF_UCNTL 0x80 /* user control mode */
87
88 void ptyattach(int);
89 void ptcwakeup(struct tty *, int);
90 void ptsstart(struct tty *);
91 int pty_maxptys(int, int);
92
93 static struct pt_softc **ptyarralloc(int);
94
95 dev_type_open(ptcopen);
96 dev_type_close(ptcclose);
97 dev_type_read(ptcread);
98 dev_type_write(ptcwrite);
99 dev_type_poll(ptcpoll);
100 dev_type_kqfilter(ptckqfilter);
101
102 dev_type_open(ptsopen);
103 dev_type_close(ptsclose);
104 dev_type_read(ptsread);
105 dev_type_write(ptswrite);
106 dev_type_stop(ptsstop);
107 dev_type_poll(ptspoll);
108
109 dev_type_ioctl(ptyioctl);
110 dev_type_tty(ptytty);
111
112 const struct cdevsw ptc_cdevsw = {
113 ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
114 nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
115 };
116
117 const struct cdevsw pts_cdevsw = {
118 ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
119 ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
120 };
121
122 #if defined(pmax)
123 const struct cdevsw ptc_ultrix_cdevsw = {
124 ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
125 nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
126 };
127
128 const struct cdevsw pts_ultrix_cdevsw = {
129 ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
130 ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
131 };
132 #endif /* defined(pmax) */
133
134 /*
135 * Check if a pty is free to use.
136 */
137 int
138 pty_isfree(int minor, int lock)
139 {
140 struct pt_softc *pt = pt_softc[minor];
141 if (lock)
142 mutex_enter(&pt_softc_mutex);
143 minor = pt == NULL || pt->pt_tty == NULL ||
144 pt->pt_tty->t_oproc == NULL;
145 if (lock)
146 mutex_exit(&pt_softc_mutex);
147 return minor;
148 }
149
150 /*
151 * Allocate and zero array of nelem elements.
152 */
153 static struct pt_softc **
154 ptyarralloc(nelem)
155 int nelem;
156 {
157 struct pt_softc **pt;
158 nelem += 10;
159 pt = malloc(nelem * sizeof *pt, M_DEVBUF, M_WAITOK | M_ZERO);
160 return pt;
161 }
162
163 /*
164 * Check if the minor is correct and ensure necessary structures
165 * are properly allocated.
166 */
167 int
168 pty_check(int ptn)
169 {
170 struct pt_softc *pti;
171
172 if (ptn >= npty) {
173 struct pt_softc **newpt, **oldpt;
174 int newnpty;
175
176 /* check if the requested pty can be granted */
177 if (ptn >= maxptys) {
178 limit_reached:
179 tablefull("pty", "increase kern.maxptys");
180 return (ENXIO);
181 }
182
183 /* Allocate a larger pty array */
184 for (newnpty = npty; newnpty <= ptn;)
185 newnpty *= 2;
186 if (newnpty > maxptys)
187 newnpty = maxptys;
188 newpt = ptyarralloc(newnpty);
189
190 /*
191 * Now grab the pty array mutex - we need to ensure
192 * that the pty array is consistent while copying it's
193 * content to newly allocated, larger space; we also
194 * need to be safe against pty_maxptys().
195 */
196 mutex_enter(&pt_softc_mutex);
197
198 if (newnpty >= maxptys) {
199 /* limit cut away beneath us... */
200 newnpty = maxptys;
201 if (ptn >= newnpty) {
202 mutex_exit(&pt_softc_mutex);
203 free(newpt, M_DEVBUF);
204 goto limit_reached;
205 }
206 }
207
208 /*
209 * If the pty array was not enlarged while we were waiting
210 * for mutex, copy current contents of pt_softc[] to newly
211 * allocated array and start using the new bigger array.
212 */
213 if (newnpty > npty) {
214 memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
215 oldpt = pt_softc;
216 pt_softc = newpt;
217 npty = newnpty;
218 } else {
219 /* was enlarged when waited for lock, free new space */
220 oldpt = newpt;
221 }
222
223 mutex_exit(&pt_softc_mutex);
224 free(oldpt, M_DEVBUF);
225 }
226
227 /*
228 * If the entry is not yet allocated, allocate one. The mutex is
229 * needed so that the state of pt_softc[] array is consistant
230 * in case it has been lengthened above.
231 */
232 if (!pt_softc[ptn]) {
233 MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
234 M_DEVBUF, M_WAITOK);
235 memset(pti, 0, sizeof(struct pt_softc));
236
237 pti->pt_tty = ttymalloc();
238
239 mutex_enter(&pt_softc_mutex);
240
241 /*
242 * Check the entry again - it might have been
243 * added while we were waiting for mutex.
244 */
245 if (!pt_softc[ptn]) {
246 tty_attach(pti->pt_tty);
247 pt_softc[ptn] = pti;
248 } else {
249 ttyfree(pti->pt_tty);
250 free(pti, M_DEVBUF);
251 }
252
253 mutex_exit(&pt_softc_mutex);
254 }
255
256 return (0);
257 }
258
259 /*
260 * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
261 * new value of maxptys.
262 */
263 int
264 pty_maxptys(newmax, set)
265 int newmax, set;
266 {
267 if (!set)
268 return (maxptys);
269
270 /*
271 * We have to grab the pt_softc lock, so that we would pick correct
272 * value of npty (might be modified in pty_check()).
273 */
274 mutex_enter(&pt_softc_mutex);
275
276 /*
277 * The value cannot be set to value lower than the highest pty
278 * number ever allocated.
279 */
280 if (newmax >= npty)
281 maxptys = newmax;
282 else
283 newmax = 0;
284
285 mutex_exit(&pt_softc_mutex);
286
287 return newmax;
288 }
289
290 /*
291 * Establish n (or default if n is 1) ptys in the system.
292 */
293 void
294 ptyattach(n)
295 int n;
296 {
297
298 mutex_init(&pt_softc_mutex, MUTEX_DEFAULT, IPL_NONE);
299
300 /* maybe should allow 0 => none? */
301 if (n <= 1)
302 n = DEFAULT_NPTYS;
303 pt_softc = ptyarralloc(n);
304 npty = n;
305 #ifndef NO_DEV_PTM
306 ptmattach(1);
307 #endif
308 }
309
310 /*ARGSUSED*/
311 int
312 ptsopen(dev_t dev, int flag, int devtype, struct lwp *l)
313 {
314 struct pt_softc *pti;
315 struct tty *tp;
316 int error;
317 int ptn = minor(dev);
318 int s;
319
320 if ((error = pty_check(ptn)) != 0)
321 return (error);
322
323 pti = pt_softc[ptn];
324 tp = pti->pt_tty;
325
326 if (!ISSET(tp->t_state, TS_ISOPEN)) {
327 ttychars(tp); /* Set up default chars */
328 tp->t_iflag = TTYDEF_IFLAG;
329 tp->t_oflag = TTYDEF_OFLAG;
330 tp->t_lflag = TTYDEF_LFLAG;
331 tp->t_cflag = TTYDEF_CFLAG;
332 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
333 ttsetwater(tp); /* would be done in xxparam() */
334 } else if (kauth_authorize_device_tty(l->l_cred, KAUTH_DEVICE_TTY_OPEN,
335 tp) != 0)
336 return (EBUSY);
337 if (tp->t_oproc) /* Ctrlr still around. */
338 SET(tp->t_state, TS_CARR_ON);
339
340 if (!ISSET(flag, O_NONBLOCK)) {
341 s = spltty();
342 TTY_LOCK(tp);
343 while (!ISSET(tp->t_state, TS_CARR_ON)) {
344 tp->t_wopen++;
345 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
346 ttopen, 0);
347 tp->t_wopen--;
348 if (error) {
349 TTY_UNLOCK(tp);
350 splx(s);
351 return (error);
352 }
353 }
354 TTY_UNLOCK(tp);
355 splx(s);
356 }
357 error = (*tp->t_linesw->l_open)(dev, tp);
358 ptcwakeup(tp, FREAD|FWRITE);
359 return (error);
360 }
361
362 int
363 ptsclose(dev_t dev, int flag, int mode, struct lwp *l)
364 {
365 struct pt_softc *pti = pt_softc[minor(dev)];
366 struct tty *tp = pti->pt_tty;
367 int error;
368
369 error = (*tp->t_linesw->l_close)(tp, flag);
370 error |= ttyclose(tp);
371 ptcwakeup(tp, FREAD|FWRITE);
372 return (error);
373 }
374
375 int
376 ptsread(dev, uio, flag)
377 dev_t dev;
378 struct uio *uio;
379 int flag;
380 {
381 struct proc *p = curproc;
382 struct pt_softc *pti = pt_softc[minor(dev)];
383 struct tty *tp = pti->pt_tty;
384 int error = 0;
385 int cc;
386 int s;
387
388 again:
389 if (pti->pt_flags & PF_REMOTE) {
390 while (isbackground(p, tp)) { /* XXXSMP */
391 if (sigismasked(curlwp, SIGTTIN) ||
392 p->p_pgrp->pg_jobc == 0 ||
393 p->p_flag & PS_PPWAIT)
394 return (EIO);
395 pgsignal(p->p_pgrp, SIGTTIN, 1);
396 s = spltty();
397 TTY_LOCK(tp);
398 error = ttysleep(tp, (void *)&lbolt,
399 TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
400 splx(s);
401 if (error)
402 return (error);
403 }
404 s = spltty();
405 TTY_LOCK(tp);
406 if (tp->t_canq.c_cc == 0) {
407 if (flag & IO_NDELAY) {
408 TTY_UNLOCK(tp);
409 splx(s);
410 return (EWOULDBLOCK);
411 }
412 error = ttysleep(tp, (void *)&tp->t_canq,
413 TTIPRI | PCATCH | PNORELOCK, ttyin, 0);
414 splx(s);
415 if (error)
416 return (error);
417 goto again;
418 }
419 while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
420 TTY_UNLOCK(tp);
421 splx(s);
422 error = ureadc(getc(&tp->t_canq), uio);
423 s = spltty();
424 TTY_LOCK(tp);
425 /* Re-check terminal state here? */
426 }
427 if (tp->t_canq.c_cc == 1)
428 (void) getc(&tp->t_canq);
429 cc = tp->t_canq.c_cc;
430 TTY_UNLOCK(tp);
431 splx(s);
432 if (cc)
433 return (error);
434 } else
435 if (tp->t_oproc)
436 error = (*tp->t_linesw->l_read)(tp, uio, flag);
437 ptcwakeup(tp, FWRITE);
438 return (error);
439 }
440
441 /*
442 * Write to pseudo-tty.
443 * Wakeups of controlling tty will happen
444 * indirectly, when tty driver calls ptsstart.
445 */
446 int
447 ptswrite(dev, uio, flag)
448 dev_t dev;
449 struct uio *uio;
450 int flag;
451 {
452 struct pt_softc *pti = pt_softc[minor(dev)];
453 struct tty *tp = pti->pt_tty;
454
455 if (tp->t_oproc == 0)
456 return (EIO);
457 return ((*tp->t_linesw->l_write)(tp, uio, flag));
458 }
459
460 /*
461 * Poll pseudo-tty.
462 */
463 int
464 ptspoll(dev, events, l)
465 dev_t dev;
466 int events;
467 struct lwp *l;
468 {
469 struct pt_softc *pti = pt_softc[minor(dev)];
470 struct tty *tp = pti->pt_tty;
471
472 if (tp->t_oproc == 0)
473 return (POLLHUP);
474
475 return ((*tp->t_linesw->l_poll)(tp, events, l));
476 }
477
478 /*
479 * Start output on pseudo-tty.
480 * Wake up process polling or sleeping for input from controlling tty.
481 * Called with tty slock held.
482 */
483 void
484 ptsstart(tp)
485 struct tty *tp;
486 {
487 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
488
489 if (ISSET(tp->t_state, TS_TTSTOP))
490 return;
491 if (pti->pt_flags & PF_STOPPED) {
492 pti->pt_flags &= ~PF_STOPPED;
493 pti->pt_send = TIOCPKT_START;
494 }
495
496 selnotify(&pti->pt_selr, NOTE_SUBMIT);
497 wakeup((void *)&tp->t_outq.c_cf);
498 }
499
500 /*
501 * Stop output.
502 * Called with tty slock held.
503 */
504 void
505 ptsstop(tp, flush)
506 struct tty *tp;
507 int flush;
508 {
509 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
510
511 /* note: FLUSHREAD and FLUSHWRITE already ok */
512 if (flush == 0) {
513 flush = TIOCPKT_STOP;
514 pti->pt_flags |= PF_STOPPED;
515 } else
516 pti->pt_flags &= ~PF_STOPPED;
517 pti->pt_send |= flush;
518
519 /* change of perspective */
520 if (flush & FREAD) {
521 selnotify(&pti->pt_selw, NOTE_SUBMIT);
522 wakeup((void *)&tp->t_rawq.c_cf);
523 }
524 if (flush & FWRITE) {
525 selnotify(&pti->pt_selr, NOTE_SUBMIT);
526 wakeup((void *)&tp->t_outq.c_cf);
527 }
528 }
529
530 void
531 ptcwakeup(tp, flag)
532 struct tty *tp;
533 int flag;
534 {
535 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
536 int s;
537
538 s = spltty();
539 TTY_LOCK(tp);
540 if (flag & FREAD) {
541 selnotify(&pti->pt_selr, NOTE_SUBMIT);
542 wakeup((void *)&tp->t_outq.c_cf);
543 }
544 if (flag & FWRITE) {
545 selnotify(&pti->pt_selw, NOTE_SUBMIT);
546 wakeup((void *)&tp->t_rawq.c_cf);
547 }
548 TTY_UNLOCK(tp);
549 splx(s);
550 }
551
552 /*ARGSUSED*/
553 int
554 ptcopen(dev_t dev, int flag, int devtype, struct lwp *l)
555 {
556 struct pt_softc *pti;
557 struct tty *tp;
558 int error;
559 int ptn = minor(dev);
560 int s;
561
562 if ((error = pty_check(ptn)) != 0)
563 return (error);
564
565 pti = pt_softc[ptn];
566 tp = pti->pt_tty;
567
568 s = spltty();
569 TTY_LOCK(tp);
570 if (tp->t_oproc) {
571 TTY_UNLOCK(tp);
572 splx(s);
573 return (EIO);
574 }
575 tp->t_oproc = ptsstart;
576 TTY_UNLOCK(tp);
577 splx(s);
578 (void)(*tp->t_linesw->l_modem)(tp, 1);
579 CLR(tp->t_lflag, EXTPROC);
580 pti->pt_flags = 0;
581 pti->pt_send = 0;
582 pti->pt_ucntl = 0;
583 return (0);
584 }
585
586 /*ARGSUSED*/
587 int
588 ptcclose(dev_t dev, int flag, int devtype, struct lwp *l)
589 {
590 struct pt_softc *pti = pt_softc[minor(dev)];
591 struct tty *tp = pti->pt_tty;
592 int s;
593
594 (void)(*tp->t_linesw->l_modem)(tp, 0);
595 CLR(tp->t_state, TS_CARR_ON);
596 s = spltty();
597 TTY_LOCK(tp);
598 tp->t_oproc = 0; /* mark closed */
599 TTY_UNLOCK(tp);
600 splx(s);
601 return (0);
602 }
603
604 int
605 ptcread(dev, uio, flag)
606 dev_t dev;
607 struct uio *uio;
608 int flag;
609 {
610 struct pt_softc *pti = pt_softc[minor(dev)];
611 struct tty *tp = pti->pt_tty;
612 u_char bf[BUFSIZ];
613 int error = 0, cc;
614 int s;
615
616 /*
617 * We want to block until the slave
618 * is open, and there's something to read;
619 * but if we lost the slave or we're NBIO,
620 * then return the appropriate error instead.
621 */
622 s = spltty();
623 TTY_LOCK(tp);
624 for (;;) {
625 if (ISSET(tp->t_state, TS_ISOPEN)) {
626 if (pti->pt_flags & PF_PKT && pti->pt_send) {
627 TTY_UNLOCK(tp);
628 splx(s);
629 error = ureadc((int)pti->pt_send, uio);
630 if (error)
631 return (error);
632 /*
633 * Since we don't have the tty locked, there's
634 * a risk of messing up `t_termios'. This is
635 * relevant only if the tty got closed and then
636 * opened again while we were out uiomoving.
637 */
638 if (pti->pt_send & TIOCPKT_IOCTL) {
639 cc = min(uio->uio_resid,
640 sizeof(tp->t_termios));
641 uiomove((void *) &tp->t_termios,
642 cc, uio);
643 }
644 pti->pt_send = 0;
645 return (0);
646 }
647 if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
648 TTY_UNLOCK(tp);
649 splx(s);
650 error = ureadc((int)pti->pt_ucntl, uio);
651 if (error)
652 return (error);
653 pti->pt_ucntl = 0;
654 return (0);
655 }
656 if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
657 break;
658 }
659 if (!ISSET(tp->t_state, TS_CARR_ON)) {
660 error = 0; /* EOF */
661 goto out;
662 }
663 if (flag & IO_NDELAY) {
664 error = EWOULDBLOCK;
665 goto out;
666 }
667 error = ltsleep((void *)&tp->t_outq.c_cf, TTIPRI | PCATCH,
668 ttyin, 0, &tp->t_slock);
669 if (error)
670 goto out;
671 }
672
673 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
674 TTY_UNLOCK(tp);
675 splx(s);
676 error = ureadc(0, uio);
677 s = spltty();
678 TTY_LOCK(tp);
679 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
680 error = EIO;
681 }
682 while (uio->uio_resid > 0 && error == 0) {
683 cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ));
684 if (cc <= 0)
685 break;
686 TTY_UNLOCK(tp);
687 splx(s);
688 error = uiomove(bf, cc, uio);
689 s = spltty();
690 TTY_LOCK(tp);
691 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
692 error = EIO;
693 }
694
695 if (tp->t_outq.c_cc <= tp->t_lowat) {
696 if (ISSET(tp->t_state, TS_ASLEEP)) {
697 CLR(tp->t_state, TS_ASLEEP);
698 wakeup((void *)&tp->t_outq);
699 }
700 selnotify(&tp->t_wsel, NOTE_SUBMIT);
701 }
702 out:
703 TTY_UNLOCK(tp);
704 splx(s);
705 return (error);
706 }
707
708
709 int
710 ptcwrite(dev, uio, flag)
711 dev_t dev;
712 struct uio *uio;
713 int flag;
714 {
715 struct pt_softc *pti = pt_softc[minor(dev)];
716 struct tty *tp = pti->pt_tty;
717 u_char *cp = NULL;
718 int cc = 0;
719 u_char locbuf[BUFSIZ];
720 int cnt = 0;
721 int error = 0;
722 int s;
723
724 again:
725 s = spltty();
726 TTY_LOCK(tp);
727 if (!ISSET(tp->t_state, TS_ISOPEN))
728 goto block;
729 if (pti->pt_flags & PF_REMOTE) {
730 if (tp->t_canq.c_cc)
731 goto block;
732 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
733 if (cc == 0) {
734 cc = min(uio->uio_resid, BUFSIZ);
735 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
736 cp = locbuf;
737 TTY_UNLOCK(tp);
738 splx(s);
739 error = uiomove((void *)cp, cc, uio);
740 if (error)
741 return (error);
742 s = spltty();
743 TTY_LOCK(tp);
744 /* check again for safety */
745 if (!ISSET(tp->t_state, TS_ISOPEN)) {
746 /*
747 * adjust for data copied in but not
748 * written
749 */
750 uio->uio_resid += cc;
751 error = EIO;
752 goto out;
753 }
754 }
755 if (cc)
756 (void) b_to_q(cp, cc, &tp->t_canq);
757 cc = 0;
758 }
759 (void) putc(0, &tp->t_canq);
760 ttwakeup(tp);
761 wakeup((void *)&tp->t_canq);
762 error = 0;
763 goto out;
764 }
765 while (uio->uio_resid > 0) {
766 if (cc == 0) {
767 cc = min(uio->uio_resid, BUFSIZ);
768 cp = locbuf;
769 TTY_UNLOCK(tp);
770 splx(s);
771 error = uiomove((void *)cp, cc, uio);
772 if (error)
773 return (error);
774 s = spltty();
775 TTY_LOCK(tp);
776 /* check again for safety */
777 if (!ISSET(tp->t_state, TS_ISOPEN)) {
778 /* adjust for data copied in but not written */
779 uio->uio_resid += cc;
780 error = EIO;
781 goto out;
782 }
783 }
784 while (cc > 0) {
785 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
786 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
787 wakeup((void *)&tp->t_rawq);
788 goto block;
789 }
790 /* XXX - should change l_rint to be called with lock
791 * see also tty.c:ttyinput_wlock()
792 */
793 TTY_UNLOCK(tp);
794 splx(s);
795 (*tp->t_linesw->l_rint)(*cp++, tp);
796 s = spltty();
797 TTY_LOCK(tp);
798 cnt++;
799 cc--;
800 }
801 cc = 0;
802 }
803 error = 0;
804 goto out;
805
806 block:
807 /*
808 * Come here to wait for slave to open, for space
809 * in outq, or space in rawq.
810 */
811 if (!ISSET(tp->t_state, TS_CARR_ON)) {
812 /* adjust for data copied in but not written */
813 uio->uio_resid += cc;
814 error = EIO;
815 goto out;
816 }
817 if (flag & IO_NDELAY) {
818 /* adjust for data copied in but not written */
819 uio->uio_resid += cc;
820 error = cnt == 0 ? EWOULDBLOCK : 0;
821 goto out;
822 }
823 error = ltsleep((void *)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK,
824 ttyout, 0, &tp->t_slock);
825 splx(s);
826 if (error) {
827 /* adjust for data copied in but not written */
828 uio->uio_resid += cc;
829 return (error);
830 }
831 goto again;
832
833 out:
834 TTY_UNLOCK(tp);
835 splx(s);
836 return (error);
837 }
838
839 int
840 ptcpoll(dev, events, l)
841 dev_t dev;
842 int events;
843 struct lwp *l;
844 {
845 struct pt_softc *pti = pt_softc[minor(dev)];
846 struct tty *tp = pti->pt_tty;
847 int revents = 0;
848 int s = splsoftclock();
849
850 if (events & (POLLIN | POLLRDNORM))
851 if (ISSET(tp->t_state, TS_ISOPEN) &&
852 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
853 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
854 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
855 revents |= events & (POLLIN | POLLRDNORM);
856
857 if (events & (POLLOUT | POLLWRNORM))
858 if (ISSET(tp->t_state, TS_ISOPEN) &&
859 ((pti->pt_flags & PF_REMOTE) ?
860 (tp->t_canq.c_cc == 0) :
861 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
862 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
863 revents |= events & (POLLOUT | POLLWRNORM);
864
865 if (events & POLLHUP)
866 if (!ISSET(tp->t_state, TS_CARR_ON))
867 revents |= POLLHUP;
868
869 if (revents == 0) {
870 if (events & (POLLIN | POLLHUP | POLLRDNORM))
871 selrecord(l, &pti->pt_selr);
872
873 if (events & (POLLOUT | POLLWRNORM))
874 selrecord(l, &pti->pt_selw);
875 }
876
877 splx(s);
878 return (revents);
879 }
880
881 static void
882 filt_ptcrdetach(struct knote *kn)
883 {
884 struct pt_softc *pti;
885 int s;
886
887 pti = kn->kn_hook;
888 s = spltty();
889 SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
890 splx(s);
891 }
892
893 static int
894 filt_ptcread(struct knote *kn, long hint)
895 {
896 struct pt_softc *pti;
897 struct tty *tp;
898 int canread;
899
900 pti = kn->kn_hook;
901 tp = pti->pt_tty;
902
903 canread = (ISSET(tp->t_state, TS_ISOPEN) &&
904 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
905 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
906 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
907
908 if (canread) {
909 /*
910 * c_cc is number of characters after output post-processing;
911 * the amount of data actually read(2) depends on
912 * setting of input flags for the terminal.
913 */
914 kn->kn_data = tp->t_outq.c_cc;
915 if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
916 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
917 kn->kn_data++;
918 }
919
920 return (canread);
921 }
922
923 static void
924 filt_ptcwdetach(struct knote *kn)
925 {
926 struct pt_softc *pti;
927 int s;
928
929 pti = kn->kn_hook;
930 s = spltty();
931 SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
932 splx(s);
933 }
934
935 static int
936 filt_ptcwrite(struct knote *kn, long hint)
937 {
938 struct pt_softc *pti;
939 struct tty *tp;
940 int canwrite;
941 int nwrite;
942
943 pti = kn->kn_hook;
944 tp = pti->pt_tty;
945
946 canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
947 ((pti->pt_flags & PF_REMOTE) ?
948 (tp->t_canq.c_cc == 0) :
949 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
950 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
951
952 if (canwrite) {
953 if (pti->pt_flags & PF_REMOTE)
954 nwrite = tp->t_canq.c_cn;
955 else {
956 /* this is guaranteed to be > 0 due to above check */
957 nwrite = tp->t_canq.c_cn
958 - (tp->t_rawq.c_cc + tp->t_canq.c_cc);
959 }
960 kn->kn_data = nwrite;
961 }
962
963 return (canwrite);
964 }
965
966 static const struct filterops ptcread_filtops =
967 { 1, NULL, filt_ptcrdetach, filt_ptcread };
968 static const struct filterops ptcwrite_filtops =
969 { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
970
971 int
972 ptckqfilter(dev_t dev, struct knote *kn)
973 {
974 struct pt_softc *pti = pt_softc[minor(dev)];
975 struct klist *klist;
976 int s;
977
978 switch (kn->kn_filter) {
979 case EVFILT_READ:
980 klist = &pti->pt_selr.sel_klist;
981 kn->kn_fop = &ptcread_filtops;
982 break;
983 case EVFILT_WRITE:
984 klist = &pti->pt_selw.sel_klist;
985 kn->kn_fop = &ptcwrite_filtops;
986 break;
987 default:
988 return (1);
989 }
990
991 kn->kn_hook = pti;
992
993 s = spltty();
994 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
995 splx(s);
996
997 return (0);
998 }
999
1000 struct tty *
1001 ptytty(dev)
1002 dev_t dev;
1003 {
1004 struct pt_softc *pti = pt_softc[minor(dev)];
1005 struct tty *tp = pti->pt_tty;
1006
1007 return (tp);
1008 }
1009
1010 /*ARGSUSED*/
1011 int
1012 ptyioctl(dev, cmd, data, flag, l)
1013 dev_t dev;
1014 u_long cmd;
1015 void *data;
1016 int flag;
1017 struct lwp *l;
1018 {
1019 struct pt_softc *pti = pt_softc[minor(dev)];
1020 struct tty *tp = pti->pt_tty;
1021 const struct cdevsw *cdev;
1022 u_char *cc = tp->t_cc;
1023 int stop, error, sig;
1024 int s;
1025
1026 /*
1027 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1028 * ttywflush(tp) will hang if there are characters in the outq.
1029 */
1030 if (cmd == TIOCEXT) {
1031 /*
1032 * When the EXTPROC bit is being toggled, we need
1033 * to send an TIOCPKT_IOCTL if the packet driver
1034 * is turned on.
1035 */
1036 if (*(int *)data) {
1037 if (pti->pt_flags & PF_PKT) {
1038 pti->pt_send |= TIOCPKT_IOCTL;
1039 ptcwakeup(tp, FREAD);
1040 }
1041 SET(tp->t_lflag, EXTPROC);
1042 } else {
1043 if (ISSET(tp->t_lflag, EXTPROC) &&
1044 (pti->pt_flags & PF_PKT)) {
1045 pti->pt_send |= TIOCPKT_IOCTL;
1046 ptcwakeup(tp, FREAD);
1047 }
1048 CLR(tp->t_lflag, EXTPROC);
1049 }
1050 return(0);
1051 }
1052
1053 #ifndef NO_DEV_PTM
1054 /* Allow getting the name from either the master or the slave */
1055 if (cmd == TIOCPTSNAME)
1056 return pty_fill_ptmget(l, dev, -1, -1, data);
1057 #endif
1058
1059 cdev = cdevsw_lookup(dev);
1060 if (cdev != NULL && cdev->d_open == ptcopen)
1061 switch (cmd) {
1062 #ifndef NO_DEV_PTM
1063 case TIOCGRANTPT:
1064 return pty_grant_slave(l, dev);
1065 #endif
1066
1067 case TIOCGPGRP:
1068 /*
1069 * We avoid calling ttioctl on the controller since,
1070 * in that case, tp must be the controlling terminal.
1071 */
1072 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1073 return (0);
1074
1075 case TIOCPKT:
1076 if (*(int *)data) {
1077 if (pti->pt_flags & PF_UCNTL)
1078 return (EINVAL);
1079 pti->pt_flags |= PF_PKT;
1080 } else
1081 pti->pt_flags &= ~PF_PKT;
1082 return (0);
1083
1084 case TIOCUCNTL:
1085 if (*(int *)data) {
1086 if (pti->pt_flags & PF_PKT)
1087 return (EINVAL);
1088 pti->pt_flags |= PF_UCNTL;
1089 } else
1090 pti->pt_flags &= ~PF_UCNTL;
1091 return (0);
1092
1093 case TIOCREMOTE:
1094 if (*(int *)data)
1095 pti->pt_flags |= PF_REMOTE;
1096 else
1097 pti->pt_flags &= ~PF_REMOTE;
1098 s = spltty();
1099 TTY_LOCK(tp);
1100 ttyflush(tp, FREAD|FWRITE);
1101 TTY_UNLOCK(tp);
1102 splx(s);
1103 return (0);
1104
1105 #ifdef COMPAT_OLDTTY
1106 case TIOCSETP:
1107 case TIOCSETN:
1108 #endif
1109 case TIOCSETD:
1110 case TIOCSETA:
1111 case TIOCSETAW:
1112 case TIOCSETAF:
1113 TTY_LOCK(tp);
1114 ndflush(&tp->t_outq, tp->t_outq.c_cc);
1115 TTY_UNLOCK(tp);
1116 break;
1117
1118 case TIOCSIG:
1119 sig = (int)(long)*(void **)data;
1120 if (sig <= 0 || sig >= NSIG)
1121 return (EINVAL);
1122 TTY_LOCK(tp);
1123 if (!ISSET(tp->t_lflag, NOFLSH))
1124 ttyflush(tp, FREAD|FWRITE);
1125 if ((sig == SIGINFO) &&
1126 (!ISSET(tp->t_lflag, NOKERNINFO)))
1127 ttyinfo(tp, 1);
1128 TTY_UNLOCK(tp);
1129 pgsignal(tp->t_pgrp, sig, 1);
1130 return(0);
1131 }
1132
1133 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1134 if (error == EPASSTHROUGH)
1135 error = ttioctl(tp, cmd, data, flag, l);
1136 if (error == EPASSTHROUGH) {
1137 if (pti->pt_flags & PF_UCNTL &&
1138 (cmd & ~0xff) == UIOCCMD(0)) {
1139 if (cmd & 0xff) {
1140 pti->pt_ucntl = (u_char)cmd;
1141 ptcwakeup(tp, FREAD);
1142 }
1143 return (0);
1144 }
1145 }
1146 /*
1147 * If external processing and packet mode send ioctl packet.
1148 */
1149 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
1150 switch(cmd) {
1151 case TIOCSETA:
1152 case TIOCSETAW:
1153 case TIOCSETAF:
1154 #ifdef COMPAT_OLDTTY
1155 case TIOCSETP:
1156 case TIOCSETN:
1157 case TIOCSETC:
1158 case TIOCSLTC:
1159 case TIOCLBIS:
1160 case TIOCLBIC:
1161 case TIOCLSET:
1162 #endif
1163 pti->pt_send |= TIOCPKT_IOCTL;
1164 ptcwakeup(tp, FREAD);
1165 default:
1166 break;
1167 }
1168 }
1169 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1170 && CCEQ(cc[VSTART], CTRL('q'));
1171 if (pti->pt_flags & PF_NOSTOP) {
1172 if (stop) {
1173 pti->pt_send &= ~TIOCPKT_NOSTOP;
1174 pti->pt_send |= TIOCPKT_DOSTOP;
1175 pti->pt_flags &= ~PF_NOSTOP;
1176 ptcwakeup(tp, FREAD);
1177 }
1178 } else {
1179 if (!stop) {
1180 pti->pt_send &= ~TIOCPKT_DOSTOP;
1181 pti->pt_send |= TIOCPKT_NOSTOP;
1182 pti->pt_flags |= PF_NOSTOP;
1183 ptcwakeup(tp, FREAD);
1184 }
1185 }
1186 return (error);
1187 }
1188