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