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