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