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