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