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