tty_pty.c revision 1.95 1 /* $NetBSD: tty_pty.c,v 1.95 2006/10/12 01:32:19 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.95 2006/10/12 01:32:19 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/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 struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
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 simple_lock(&pt_softc_mutex);
143 minor = pt == NULL || pt->pt_tty == NULL ||
144 pt->pt_tty->t_oproc == NULL;
145 if (lock)
146 simple_unlock(&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 simple_lock(&pt_softc_mutex);
197
198 if (newnpty >= maxptys) {
199 /* limit cut away beneath us... */
200 newnpty = maxptys;
201 if (ptn >= newnpty) {
202 simple_unlock(&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 simple_unlock(&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 simple_lock(&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 simple_unlock(&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 simple_lock(&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 simple_unlock(&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 /* maybe should allow 0 => none? */
298 if (n <= 1)
299 n = DEFAULT_NPTYS;
300 pt_softc = ptyarralloc(n);
301 npty = n;
302 #ifndef NO_DEV_PTM
303 ptmattach(1);
304 #endif
305 }
306
307 /*ARGSUSED*/
308 int
309 ptsopen(dev_t dev, int flag, int devtype __unused, struct lwp *l)
310 {
311 struct pt_softc *pti;
312 struct tty *tp;
313 int error;
314 int ptn = minor(dev);
315 int s;
316
317 if ((error = pty_check(ptn)) != 0)
318 return (error);
319
320 pti = pt_softc[ptn];
321 tp = pti->pt_tty;
322
323 if (!ISSET(tp->t_state, TS_ISOPEN)) {
324 ttychars(tp); /* Set up default chars */
325 tp->t_iflag = TTYDEF_IFLAG;
326 tp->t_oflag = TTYDEF_OFLAG;
327 tp->t_lflag = TTYDEF_LFLAG;
328 tp->t_cflag = TTYDEF_CFLAG;
329 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
330 ttsetwater(tp); /* would be done in xxparam() */
331 } else if (ISSET(tp->t_state, TS_XCLUDE) &&
332 kauth_cred_geteuid(l->l_cred) != 0)
333 return (EBUSY);
334 if (tp->t_oproc) /* Ctrlr still around. */
335 SET(tp->t_state, TS_CARR_ON);
336
337 if (!ISSET(flag, O_NONBLOCK)) {
338 s = spltty();
339 TTY_LOCK(tp);
340 while (!ISSET(tp->t_state, TS_CARR_ON)) {
341 tp->t_wopen++;
342 error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
343 ttopen, 0);
344 tp->t_wopen--;
345 if (error) {
346 TTY_UNLOCK(tp);
347 splx(s);
348 return (error);
349 }
350 }
351 TTY_UNLOCK(tp);
352 splx(s);
353 }
354 error = (*tp->t_linesw->l_open)(dev, tp);
355 ptcwakeup(tp, FREAD|FWRITE);
356 return (error);
357 }
358
359 int
360 ptsclose(dev_t dev, int flag, int mode __unused, struct lwp *l __unused)
361 {
362 struct pt_softc *pti = pt_softc[minor(dev)];
363 struct tty *tp = pti->pt_tty;
364 int error;
365
366 error = (*tp->t_linesw->l_close)(tp, flag);
367 error |= ttyclose(tp);
368 ptcwakeup(tp, FREAD|FWRITE);
369 return (error);
370 }
371
372 int
373 ptsread(dev, uio, flag)
374 dev_t dev;
375 struct uio *uio;
376 int flag;
377 {
378 struct proc *p = curproc;
379 struct pt_softc *pti = pt_softc[minor(dev)];
380 struct tty *tp = pti->pt_tty;
381 int error = 0;
382 int cc;
383 int s;
384
385 again:
386 if (pti->pt_flags & PF_REMOTE) {
387 while (isbackground(p, tp)) {
388 if (sigismasked(p, SIGTTIN) ||
389 p->p_pgrp->pg_jobc == 0 ||
390 p->p_flag & P_PPWAIT)
391 return (EIO);
392 pgsignal(p->p_pgrp, SIGTTIN, 1);
393 s = spltty();
394 TTY_LOCK(tp);
395 error = ttysleep(tp, (caddr_t)&lbolt,
396 TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
397 splx(s);
398 if (error)
399 return (error);
400 }
401 s = spltty();
402 TTY_LOCK(tp);
403 if (tp->t_canq.c_cc == 0) {
404 if (flag & IO_NDELAY) {
405 TTY_UNLOCK(tp);
406 splx(s);
407 return (EWOULDBLOCK);
408 }
409 error = ttysleep(tp, (caddr_t)&tp->t_canq,
410 TTIPRI | PCATCH | PNORELOCK, ttyin, 0);
411 splx(s);
412 if (error)
413 return (error);
414 goto again;
415 }
416 while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
417 TTY_UNLOCK(tp);
418 splx(s);
419 error = ureadc(getc(&tp->t_canq), uio);
420 s = spltty();
421 TTY_LOCK(tp);
422 /* Re-check terminal state here? */
423 }
424 if (tp->t_canq.c_cc == 1)
425 (void) getc(&tp->t_canq);
426 cc = tp->t_canq.c_cc;
427 TTY_UNLOCK(tp);
428 splx(s);
429 if (cc)
430 return (error);
431 } else
432 if (tp->t_oproc)
433 error = (*tp->t_linesw->l_read)(tp, uio, flag);
434 ptcwakeup(tp, FWRITE);
435 return (error);
436 }
437
438 /*
439 * Write to pseudo-tty.
440 * Wakeups of controlling tty will happen
441 * indirectly, when tty driver calls ptsstart.
442 */
443 int
444 ptswrite(dev, uio, flag)
445 dev_t dev;
446 struct uio *uio;
447 int flag;
448 {
449 struct pt_softc *pti = pt_softc[minor(dev)];
450 struct tty *tp = pti->pt_tty;
451
452 if (tp->t_oproc == 0)
453 return (EIO);
454 return ((*tp->t_linesw->l_write)(tp, uio, flag));
455 }
456
457 /*
458 * Poll pseudo-tty.
459 */
460 int
461 ptspoll(dev, events, l)
462 dev_t dev;
463 int events;
464 struct lwp *l;
465 {
466 struct pt_softc *pti = pt_softc[minor(dev)];
467 struct tty *tp = pti->pt_tty;
468
469 if (tp->t_oproc == 0)
470 return (POLLHUP);
471
472 return ((*tp->t_linesw->l_poll)(tp, events, l));
473 }
474
475 /*
476 * Start output on pseudo-tty.
477 * Wake up process polling or sleeping for input from controlling tty.
478 * Called with tty slock held.
479 */
480 void
481 ptsstart(tp)
482 struct tty *tp;
483 {
484 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
485
486 if (ISSET(tp->t_state, TS_TTSTOP))
487 return;
488 if (pti->pt_flags & PF_STOPPED) {
489 pti->pt_flags &= ~PF_STOPPED;
490 pti->pt_send = TIOCPKT_START;
491 }
492
493 selnotify(&pti->pt_selr, NOTE_SUBMIT);
494 wakeup((caddr_t)&tp->t_outq.c_cf);
495 }
496
497 /*
498 * Stop output.
499 * Called with tty slock held.
500 */
501 void
502 ptsstop(tp, flush)
503 struct tty *tp;
504 int flush;
505 {
506 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
507
508 /* note: FLUSHREAD and FLUSHWRITE already ok */
509 if (flush == 0) {
510 flush = TIOCPKT_STOP;
511 pti->pt_flags |= PF_STOPPED;
512 } else
513 pti->pt_flags &= ~PF_STOPPED;
514 pti->pt_send |= flush;
515
516 /* change of perspective */
517 if (flush & FREAD) {
518 selnotify(&pti->pt_selw, NOTE_SUBMIT);
519 wakeup((caddr_t)&tp->t_rawq.c_cf);
520 }
521 if (flush & FWRITE) {
522 selnotify(&pti->pt_selr, NOTE_SUBMIT);
523 wakeup((caddr_t)&tp->t_outq.c_cf);
524 }
525 }
526
527 void
528 ptcwakeup(tp, flag)
529 struct tty *tp;
530 int flag;
531 {
532 struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
533 int s;
534
535 s = spltty();
536 TTY_LOCK(tp);
537 if (flag & FREAD) {
538 selnotify(&pti->pt_selr, NOTE_SUBMIT);
539 wakeup((caddr_t)&tp->t_outq.c_cf);
540 }
541 if (flag & FWRITE) {
542 selnotify(&pti->pt_selw, NOTE_SUBMIT);
543 wakeup((caddr_t)&tp->t_rawq.c_cf);
544 }
545 TTY_UNLOCK(tp);
546 splx(s);
547 }
548
549 /*ARGSUSED*/
550 int
551 ptcopen(dev_t dev, int flag __unused, int devtype __unused,
552 struct lwp *l __unused)
553 {
554 struct pt_softc *pti;
555 struct tty *tp;
556 int error;
557 int ptn = minor(dev);
558 int s;
559
560 if ((error = pty_check(ptn)) != 0)
561 return (error);
562
563 pti = pt_softc[ptn];
564 tp = pti->pt_tty;
565
566 s = spltty();
567 TTY_LOCK(tp);
568 if (tp->t_oproc) {
569 TTY_UNLOCK(tp);
570 splx(s);
571 return (EIO);
572 }
573 tp->t_oproc = ptsstart;
574 TTY_UNLOCK(tp);
575 splx(s);
576 (void)(*tp->t_linesw->l_modem)(tp, 1);
577 CLR(tp->t_lflag, EXTPROC);
578 pti->pt_flags = 0;
579 pti->pt_send = 0;
580 pti->pt_ucntl = 0;
581 return (0);
582 }
583
584 /*ARGSUSED*/
585 int
586 ptcclose(dev_t dev, int flag __unused, int devtype __unused,
587 struct lwp *l __unused)
588 {
589 struct pt_softc *pti = pt_softc[minor(dev)];
590 struct tty *tp = pti->pt_tty;
591 int s;
592
593 (void)(*tp->t_linesw->l_modem)(tp, 0);
594 CLR(tp->t_state, TS_CARR_ON);
595 s = spltty();
596 TTY_LOCK(tp);
597 tp->t_oproc = 0; /* mark closed */
598 TTY_UNLOCK(tp);
599 splx(s);
600 return (0);
601 }
602
603 int
604 ptcread(dev, uio, flag)
605 dev_t dev;
606 struct uio *uio;
607 int flag;
608 {
609 struct pt_softc *pti = pt_softc[minor(dev)];
610 struct tty *tp = pti->pt_tty;
611 u_char bf[BUFSIZ];
612 int error = 0, cc;
613 int s;
614
615 /*
616 * We want to block until the slave
617 * is open, and there's something to read;
618 * but if we lost the slave or we're NBIO,
619 * then return the appropriate error instead.
620 */
621 s = spltty();
622 TTY_LOCK(tp);
623 for (;;) {
624 if (ISSET(tp->t_state, TS_ISOPEN)) {
625 if (pti->pt_flags & PF_PKT && pti->pt_send) {
626 TTY_UNLOCK(tp);
627 splx(s);
628 error = ureadc((int)pti->pt_send, uio);
629 if (error)
630 return (error);
631 /*
632 * Since we don't have the tty locked, there's
633 * a risk of messing up `t_termios'. This is
634 * relevant only if the tty got closed and then
635 * opened again while we were out uiomoving.
636 */
637 if (pti->pt_send & TIOCPKT_IOCTL) {
638 cc = min(uio->uio_resid,
639 sizeof(tp->t_termios));
640 uiomove((caddr_t) &tp->t_termios,
641 cc, uio);
642 }
643 pti->pt_send = 0;
644 return (0);
645 }
646 if (pti->pt_flags & PF_UCNTL && pti->pt_ucntl) {
647 TTY_UNLOCK(tp);
648 splx(s);
649 error = ureadc((int)pti->pt_ucntl, uio);
650 if (error)
651 return (error);
652 pti->pt_ucntl = 0;
653 return (0);
654 }
655 if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
656 break;
657 }
658 if (!ISSET(tp->t_state, TS_CARR_ON)) {
659 error = 0; /* EOF */
660 goto out;
661 }
662 if (flag & IO_NDELAY) {
663 error = EWOULDBLOCK;
664 goto out;
665 }
666 error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
667 ttyin, 0, &tp->t_slock);
668 if (error)
669 goto out;
670 }
671
672 if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
673 TTY_UNLOCK(tp);
674 splx(s);
675 error = ureadc(0, uio);
676 s = spltty();
677 TTY_LOCK(tp);
678 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
679 error = EIO;
680 }
681 while (uio->uio_resid > 0 && error == 0) {
682 cc = q_to_b(&tp->t_outq, bf, min(uio->uio_resid, BUFSIZ));
683 if (cc <= 0)
684 break;
685 TTY_UNLOCK(tp);
686 splx(s);
687 error = uiomove(bf, cc, uio);
688 s = spltty();
689 TTY_LOCK(tp);
690 if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
691 error = EIO;
692 }
693
694 if (tp->t_outq.c_cc <= tp->t_lowat) {
695 if (ISSET(tp->t_state, TS_ASLEEP)) {
696 CLR(tp->t_state, TS_ASLEEP);
697 wakeup((caddr_t)&tp->t_outq);
698 }
699 selnotify(&tp->t_wsel, NOTE_SUBMIT);
700 }
701 out:
702 TTY_UNLOCK(tp);
703 splx(s);
704 return (error);
705 }
706
707
708 int
709 ptcwrite(dev, uio, flag)
710 dev_t dev;
711 struct uio *uio;
712 int flag;
713 {
714 struct pt_softc *pti = pt_softc[minor(dev)];
715 struct tty *tp = pti->pt_tty;
716 u_char *cp = NULL;
717 int cc = 0;
718 u_char locbuf[BUFSIZ];
719 int cnt = 0;
720 int error = 0;
721 int s;
722
723 again:
724 s = spltty();
725 TTY_LOCK(tp);
726 if (!ISSET(tp->t_state, TS_ISOPEN))
727 goto block;
728 if (pti->pt_flags & PF_REMOTE) {
729 if (tp->t_canq.c_cc)
730 goto block;
731 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
732 if (cc == 0) {
733 cc = min(uio->uio_resid, BUFSIZ);
734 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
735 cp = locbuf;
736 TTY_UNLOCK(tp);
737 splx(s);
738 error = uiomove((caddr_t)cp, cc, uio);
739 if (error)
740 return (error);
741 s = spltty();
742 TTY_LOCK(tp);
743 /* check again for safety */
744 if (!ISSET(tp->t_state, TS_ISOPEN)) {
745 /*
746 * adjust for data copied in but not
747 * written
748 */
749 uio->uio_resid += cc;
750 error = EIO;
751 goto out;
752 }
753 }
754 if (cc)
755 (void) b_to_q(cp, cc, &tp->t_canq);
756 cc = 0;
757 }
758 (void) putc(0, &tp->t_canq);
759 ttwakeup(tp);
760 wakeup((caddr_t)&tp->t_canq);
761 error = 0;
762 goto out;
763 }
764 while (uio->uio_resid > 0) {
765 if (cc == 0) {
766 cc = min(uio->uio_resid, BUFSIZ);
767 cp = locbuf;
768 TTY_UNLOCK(tp);
769 splx(s);
770 error = uiomove((caddr_t)cp, cc, uio);
771 if (error)
772 return (error);
773 s = spltty();
774 TTY_LOCK(tp);
775 /* check again for safety */
776 if (!ISSET(tp->t_state, TS_ISOPEN)) {
777 /* adjust for data copied in but not written */
778 uio->uio_resid += cc;
779 error = EIO;
780 goto out;
781 }
782 }
783 while (cc > 0) {
784 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
785 (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
786 wakeup((caddr_t)&tp->t_rawq);
787 goto block;
788 }
789 /* XXX - should change l_rint to be called with lock
790 * see also tty.c:ttyinput_wlock()
791 */
792 TTY_UNLOCK(tp);
793 splx(s);
794 (*tp->t_linesw->l_rint)(*cp++, tp);
795 s = spltty();
796 TTY_LOCK(tp);
797 cnt++;
798 cc--;
799 }
800 cc = 0;
801 }
802 error = 0;
803 goto out;
804
805 block:
806 /*
807 * Come here to wait for slave to open, for space
808 * in outq, or space in rawq.
809 */
810 if (!ISSET(tp->t_state, TS_CARR_ON)) {
811 /* adjust for data copied in but not written */
812 uio->uio_resid += cc;
813 error = EIO;
814 goto out;
815 }
816 if (flag & IO_NDELAY) {
817 /* adjust for data copied in but not written */
818 uio->uio_resid += cc;
819 error = cnt == 0 ? EWOULDBLOCK : 0;
820 goto out;
821 }
822 error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK,
823 ttyout, 0, &tp->t_slock);
824 splx(s);
825 if (error) {
826 /* adjust for data copied in but not written */
827 uio->uio_resid += cc;
828 return (error);
829 }
830 goto again;
831
832 out:
833 TTY_UNLOCK(tp);
834 splx(s);
835 return (error);
836 }
837
838 int
839 ptcpoll(dev, events, l)
840 dev_t dev;
841 int events;
842 struct lwp *l;
843 {
844 struct pt_softc *pti = pt_softc[minor(dev)];
845 struct tty *tp = pti->pt_tty;
846 int revents = 0;
847 int s = splsoftclock();
848
849 if (events & (POLLIN | POLLRDNORM))
850 if (ISSET(tp->t_state, TS_ISOPEN) &&
851 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
852 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
853 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
854 revents |= events & (POLLIN | POLLRDNORM);
855
856 if (events & (POLLOUT | POLLWRNORM))
857 if (ISSET(tp->t_state, TS_ISOPEN) &&
858 ((pti->pt_flags & PF_REMOTE) ?
859 (tp->t_canq.c_cc == 0) :
860 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
861 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
862 revents |= events & (POLLOUT | POLLWRNORM);
863
864 if (events & POLLHUP)
865 if (!ISSET(tp->t_state, TS_CARR_ON))
866 revents |= POLLHUP;
867
868 if (revents == 0) {
869 if (events & (POLLIN | POLLHUP | POLLRDNORM))
870 selrecord(l, &pti->pt_selr);
871
872 if (events & (POLLOUT | POLLWRNORM))
873 selrecord(l, &pti->pt_selw);
874 }
875
876 splx(s);
877 return (revents);
878 }
879
880 static void
881 filt_ptcrdetach(struct knote *kn)
882 {
883 struct pt_softc *pti;
884 int s;
885
886 pti = kn->kn_hook;
887 s = spltty();
888 SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
889 splx(s);
890 }
891
892 static int
893 filt_ptcread(struct knote *kn, long hint __unused)
894 {
895 struct pt_softc *pti;
896 struct tty *tp;
897 int canread;
898
899 pti = kn->kn_hook;
900 tp = pti->pt_tty;
901
902 canread = (ISSET(tp->t_state, TS_ISOPEN) &&
903 ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
904 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
905 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
906
907 if (canread) {
908 /*
909 * c_cc is number of characters after output post-processing;
910 * the amount of data actually read(2) depends on
911 * setting of input flags for the terminal.
912 */
913 kn->kn_data = tp->t_outq.c_cc;
914 if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
915 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
916 kn->kn_data++;
917 }
918
919 return (canread);
920 }
921
922 static void
923 filt_ptcwdetach(struct knote *kn)
924 {
925 struct pt_softc *pti;
926 int s;
927
928 pti = kn->kn_hook;
929 s = spltty();
930 SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
931 splx(s);
932 }
933
934 static int
935 filt_ptcwrite(struct knote *kn, long hint __unused)
936 {
937 struct pt_softc *pti;
938 struct tty *tp;
939 int canwrite;
940 int nwrite;
941
942 pti = kn->kn_hook;
943 tp = pti->pt_tty;
944
945 canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
946 ((pti->pt_flags & PF_REMOTE) ?
947 (tp->t_canq.c_cc == 0) :
948 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
949 (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
950
951 if (canwrite) {
952 if (pti->pt_flags & PF_REMOTE)
953 nwrite = tp->t_canq.c_cn;
954 else {
955 /* this is guaranteed to be > 0 due to above check */
956 nwrite = tp->t_canq.c_cn
957 - (tp->t_rawq.c_cc + tp->t_canq.c_cc);
958 }
959 kn->kn_data = nwrite;
960 }
961
962 return (canwrite);
963 }
964
965 static const struct filterops ptcread_filtops =
966 { 1, NULL, filt_ptcrdetach, filt_ptcread };
967 static const struct filterops ptcwrite_filtops =
968 { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
969
970 int
971 ptckqfilter(dev_t dev, struct knote *kn)
972 {
973 struct pt_softc *pti = pt_softc[minor(dev)];
974 struct klist *klist;
975 int s;
976
977 switch (kn->kn_filter) {
978 case EVFILT_READ:
979 klist = &pti->pt_selr.sel_klist;
980 kn->kn_fop = &ptcread_filtops;
981 break;
982 case EVFILT_WRITE:
983 klist = &pti->pt_selw.sel_klist;
984 kn->kn_fop = &ptcwrite_filtops;
985 break;
986 default:
987 return (1);
988 }
989
990 kn->kn_hook = pti;
991
992 s = spltty();
993 SLIST_INSERT_HEAD(klist, kn, kn_selnext);
994 splx(s);
995
996 return (0);
997 }
998
999 struct tty *
1000 ptytty(dev)
1001 dev_t dev;
1002 {
1003 struct pt_softc *pti = pt_softc[minor(dev)];
1004 struct tty *tp = pti->pt_tty;
1005
1006 return (tp);
1007 }
1008
1009 /*ARGSUSED*/
1010 int
1011 ptyioctl(dev, cmd, data, flag, l)
1012 dev_t dev;
1013 u_long cmd;
1014 caddr_t data;
1015 int flag;
1016 struct lwp *l;
1017 {
1018 struct pt_softc *pti = pt_softc[minor(dev)];
1019 struct tty *tp = pti->pt_tty;
1020 const struct cdevsw *cdev;
1021 u_char *cc = tp->t_cc;
1022 int stop, error, sig;
1023 int s;
1024
1025 /*
1026 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1027 * ttywflush(tp) will hang if there are characters in the outq.
1028 */
1029 if (cmd == TIOCEXT) {
1030 /*
1031 * When the EXTPROC bit is being toggled, we need
1032 * to send an TIOCPKT_IOCTL if the packet driver
1033 * is turned on.
1034 */
1035 if (*(int *)data) {
1036 if (pti->pt_flags & PF_PKT) {
1037 pti->pt_send |= TIOCPKT_IOCTL;
1038 ptcwakeup(tp, FREAD);
1039 }
1040 SET(tp->t_lflag, EXTPROC);
1041 } else {
1042 if (ISSET(tp->t_lflag, EXTPROC) &&
1043 (pti->pt_flags & PF_PKT)) {
1044 pti->pt_send |= TIOCPKT_IOCTL;
1045 ptcwakeup(tp, FREAD);
1046 }
1047 CLR(tp->t_lflag, EXTPROC);
1048 }
1049 return(0);
1050 }
1051
1052 #ifndef NO_DEV_PTM
1053 /* Allow getting the name from either the master or the slave */
1054 if (cmd == TIOCPTSNAME)
1055 return pty_fill_ptmget(l, dev, -1, -1, data);
1056 #endif
1057
1058 cdev = cdevsw_lookup(dev);
1059 if (cdev != NULL && cdev->d_open == ptcopen)
1060 switch (cmd) {
1061 #ifndef NO_DEV_PTM
1062 case TIOCGRANTPT:
1063 return pty_grant_slave(l, dev);
1064 #endif
1065
1066 case TIOCGPGRP:
1067 /*
1068 * We avoid calling ttioctl on the controller since,
1069 * in that case, tp must be the controlling terminal.
1070 */
1071 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1072 return (0);
1073
1074 case TIOCPKT:
1075 if (*(int *)data) {
1076 if (pti->pt_flags & PF_UCNTL)
1077 return (EINVAL);
1078 pti->pt_flags |= PF_PKT;
1079 } else
1080 pti->pt_flags &= ~PF_PKT;
1081 return (0);
1082
1083 case TIOCUCNTL:
1084 if (*(int *)data) {
1085 if (pti->pt_flags & PF_PKT)
1086 return (EINVAL);
1087 pti->pt_flags |= PF_UCNTL;
1088 } else
1089 pti->pt_flags &= ~PF_UCNTL;
1090 return (0);
1091
1092 case TIOCREMOTE:
1093 if (*(int *)data)
1094 pti->pt_flags |= PF_REMOTE;
1095 else
1096 pti->pt_flags &= ~PF_REMOTE;
1097 s = spltty();
1098 TTY_LOCK(tp);
1099 ttyflush(tp, FREAD|FWRITE);
1100 TTY_UNLOCK(tp);
1101 splx(s);
1102 return (0);
1103
1104 #ifdef COMPAT_OLDTTY
1105 case TIOCSETP:
1106 case TIOCSETN:
1107 #endif
1108 case TIOCSETD:
1109 case TIOCSETA:
1110 case TIOCSETAW:
1111 case TIOCSETAF:
1112 TTY_LOCK(tp);
1113 ndflush(&tp->t_outq, tp->t_outq.c_cc);
1114 TTY_UNLOCK(tp);
1115 break;
1116
1117 case TIOCSIG:
1118 sig = (int)(long)*(caddr_t *)data;
1119 if (sig <= 0 || sig >= NSIG)
1120 return (EINVAL);
1121 TTY_LOCK(tp);
1122 if (!ISSET(tp->t_lflag, NOFLSH))
1123 ttyflush(tp, FREAD|FWRITE);
1124 if ((sig == SIGINFO) &&
1125 (!ISSET(tp->t_lflag, NOKERNINFO)))
1126 ttyinfo(tp, 1);
1127 TTY_UNLOCK(tp);
1128 pgsignal(tp->t_pgrp, sig, 1);
1129 return(0);
1130 }
1131
1132 error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, l);
1133 if (error == EPASSTHROUGH)
1134 error = ttioctl(tp, cmd, data, flag, l);
1135 if (error == EPASSTHROUGH) {
1136 if (pti->pt_flags & PF_UCNTL &&
1137 (cmd & ~0xff) == UIOCCMD(0)) {
1138 if (cmd & 0xff) {
1139 pti->pt_ucntl = (u_char)cmd;
1140 ptcwakeup(tp, FREAD);
1141 }
1142 return (0);
1143 }
1144 }
1145 /*
1146 * If external processing and packet mode send ioctl packet.
1147 */
1148 if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
1149 switch(cmd) {
1150 case TIOCSETA:
1151 case TIOCSETAW:
1152 case TIOCSETAF:
1153 #ifdef COMPAT_OLDTTY
1154 case TIOCSETP:
1155 case TIOCSETN:
1156 case TIOCSETC:
1157 case TIOCSLTC:
1158 case TIOCLBIS:
1159 case TIOCLBIC:
1160 case TIOCLSET:
1161 #endif
1162 pti->pt_send |= TIOCPKT_IOCTL;
1163 ptcwakeup(tp, FREAD);
1164 default:
1165 break;
1166 }
1167 }
1168 stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1169 && CCEQ(cc[VSTART], CTRL('q'));
1170 if (pti->pt_flags & PF_NOSTOP) {
1171 if (stop) {
1172 pti->pt_send &= ~TIOCPKT_NOSTOP;
1173 pti->pt_send |= TIOCPKT_DOSTOP;
1174 pti->pt_flags &= ~PF_NOSTOP;
1175 ptcwakeup(tp, FREAD);
1176 }
1177 } else {
1178 if (!stop) {
1179 pti->pt_send &= ~TIOCPKT_DOSTOP;
1180 pti->pt_send |= TIOCPKT_NOSTOP;
1181 pti->pt_flags |= PF_NOSTOP;
1182 ptcwakeup(tp, FREAD);
1183 }
1184 }
1185 return (error);
1186 }
1187