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