xref: /original-bsd/sys/kern/tty_pty.c (revision 35d77a20)
1 /*
2  * Copyright (c) 1982, 1986, 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)tty_pty.c	7.21 (Berkeley) 05/30/91
8  */
9 
10 /*
11  * Pseudo-teletype Driver
12  * (Actually two drivers, requiring two entries in 'cdevsw')
13  */
14 #include "pty.h"
15 
16 #if NPTY > 0
17 #include "param.h"
18 #include "systm.h"
19 #include "ioctl.h"
20 #include "tty.h"
21 #include "conf.h"
22 #include "file.h"
23 #include "proc.h"
24 #include "uio.h"
25 #include "kernel.h"
26 #include "vnode.h"
27 
28 #if NPTY == 1
29 #undef NPTY
30 #define	NPTY	32		/* crude XXX */
31 #endif
32 
33 #define BUFSIZ 100		/* Chunk size iomoved to/from user */
34 
35 /*
36  * pts == /dev/tty[pqrs]?
37  * ptc == /dev/pty[pqrs]?
38  */
39 struct	tty pt_tty[NPTY];
40 struct	pt_ioctl {
41 	int	pt_flags;
42 	struct	proc *pt_selr, *pt_selw;
43 	u_char	pt_send;
44 	u_char	pt_ucntl;
45 } pt_ioctl[NPTY];
46 int	npty = NPTY;		/* for pstat -t */
47 
48 #define	PF_RCOLL	0x01
49 #define	PF_WCOLL	0x02
50 #define	PF_PKT		0x08		/* packet mode */
51 #define	PF_STOPPED	0x10		/* user told stopped */
52 #define	PF_REMOTE	0x20		/* remote and flow controlled input */
53 #define	PF_NOSTOP	0x40
54 #define PF_UCNTL	0x80		/* user control mode */
55 
56 /*ARGSUSED*/
57 ptsopen(dev, flag, devtype, p)
58 	dev_t dev;
59 	struct proc *p;
60 {
61 	register struct tty *tp;
62 	int error;
63 
64 #ifdef lint
65 	npty = npty;
66 #endif
67 	if (minor(dev) >= NPTY)
68 		return (ENXIO);
69 	tp = &pt_tty[minor(dev)];
70 	if ((tp->t_state & TS_ISOPEN) == 0) {
71 		tp->t_state |= TS_WOPEN;
72 		ttychars(tp);		/* Set up default chars */
73 		tp->t_iflag = TTYDEF_IFLAG;
74 		tp->t_oflag = TTYDEF_OFLAG;
75 		tp->t_lflag = TTYDEF_LFLAG;
76 		tp->t_cflag = TTYDEF_CFLAG;
77 		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
78 		ttsetwater(tp);		/* would be done in xxparam() */
79 	} else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
80 		return (EBUSY);
81 	if (tp->t_oproc)			/* Ctrlr still around. */
82 		tp->t_state |= TS_CARR_ON;
83 	while ((tp->t_state & TS_CARR_ON) == 0) {
84 		tp->t_state |= TS_WOPEN;
85 		if (flag&FNONBLOCK)
86 			break;
87 		if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
88 		    ttopen, 0))
89 			return (error);
90 	}
91 	error = (*linesw[tp->t_line].l_open)(dev, tp, flag);
92 	ptcwakeup(tp, FREAD|FWRITE);
93 	return (error);
94 }
95 
96 ptsclose(dev, flag, mode, p)
97 	dev_t dev;
98 	int flag, mode;
99 	struct proc *p;
100 {
101 	register struct tty *tp;
102 
103 	tp = &pt_tty[minor(dev)];
104 	(*linesw[tp->t_line].l_close)(tp, flag);
105 	ttyclose(tp);
106 	ptcwakeup(tp, FREAD|FWRITE);
107 }
108 
109 ptsread(dev, uio, flag)
110 	dev_t dev;
111 	struct uio *uio;
112 {
113 	struct proc *p = curproc;
114 	register struct tty *tp = &pt_tty[minor(dev)];
115 	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
116 	int error = 0;
117 
118 again:
119 	if (pti->pt_flags & PF_REMOTE) {
120 		while (isbackground(p, tp)) {
121 			if ((p->p_sigignore & sigmask(SIGTTIN)) ||
122 			    (p->p_sigmask & sigmask(SIGTTIN)) ||
123 			    p->p_pgrp->pg_jobc == 0 ||
124 			    p->p_flag&SPPWAIT)
125 				return (EIO);
126 			pgsignal(p->p_pgrp, SIGTTIN, 1);
127 			if (error = ttysleep(tp, (caddr_t)&lbolt,
128 			    TTIPRI | PCATCH, ttybg, 0))
129 				return (error);
130 		}
131 		if (tp->t_canq.c_cc == 0) {
132 			if (flag & IO_NDELAY)
133 				return (EWOULDBLOCK);
134 			if (error = ttysleep(tp, (caddr_t)&tp->t_canq,
135 			    TTIPRI | PCATCH, ttyin, 0))
136 				return (error);
137 			goto again;
138 		}
139 		while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
140 			if (ureadc(getc(&tp->t_canq), uio) < 0) {
141 				error = EFAULT;
142 				break;
143 			}
144 		if (tp->t_canq.c_cc == 1)
145 			(void) getc(&tp->t_canq);
146 		if (tp->t_canq.c_cc)
147 			return (error);
148 	} else
149 		if (tp->t_oproc)
150 			error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
151 	ptcwakeup(tp, FWRITE);
152 	return (error);
153 }
154 
155 /*
156  * Write to pseudo-tty.
157  * Wakeups of controlling tty will happen
158  * indirectly, when tty driver calls ptsstart.
159  */
160 ptswrite(dev, uio, flag)
161 	dev_t dev;
162 	struct uio *uio;
163 {
164 	register struct tty *tp;
165 
166 	tp = &pt_tty[minor(dev)];
167 	if (tp->t_oproc == 0)
168 		return (EIO);
169 	return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
170 }
171 
172 /*
173  * Start output on pseudo-tty.
174  * Wake up process selecting or sleeping for input from controlling tty.
175  */
176 ptsstart(tp)
177 	struct tty *tp;
178 {
179 	register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
180 
181 	if (tp->t_state & TS_TTSTOP)
182 		return;
183 	if (pti->pt_flags & PF_STOPPED) {
184 		pti->pt_flags &= ~PF_STOPPED;
185 		pti->pt_send = TIOCPKT_START;
186 	}
187 	ptcwakeup(tp, FREAD);
188 }
189 
190 ptcwakeup(tp, flag)
191 	struct tty *tp;
192 {
193 	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
194 
195 	if (flag & FREAD) {
196 		if (pti->pt_selr) {
197 			selwakeup(pti->pt_selr, pti->pt_flags & PF_RCOLL);
198 			pti->pt_selr = 0;
199 			pti->pt_flags &= ~PF_RCOLL;
200 		}
201 		wakeup((caddr_t)&tp->t_outq.c_cf);
202 	}
203 	if (flag & FWRITE) {
204 		if (pti->pt_selw) {
205 			selwakeup(pti->pt_selw, pti->pt_flags & PF_WCOLL);
206 			pti->pt_selw = 0;
207 			pti->pt_flags &= ~PF_WCOLL;
208 		}
209 		wakeup((caddr_t)&tp->t_rawq.c_cf);
210 	}
211 }
212 
213 /*ARGSUSED*/
214 #ifdef __STDC__
215 ptcopen(dev_t dev, int flag, int devtype, struct proc *p)
216 #else
217 ptcopen(dev, flag, devtype, p)
218 	dev_t dev;
219 	int flag, devtype;
220 	struct proc *p;
221 #endif
222 {
223 	register struct tty *tp;
224 	struct pt_ioctl *pti;
225 
226 	if (minor(dev) >= NPTY)
227 		return (ENXIO);
228 	tp = &pt_tty[minor(dev)];
229 	if (tp->t_oproc)
230 		return (EIO);
231 	tp->t_oproc = ptsstart;
232 	(void)(*linesw[tp->t_line].l_modem)(tp, 1);
233 	tp->t_lflag &= ~EXTPROC;
234 	pti = &pt_ioctl[minor(dev)];
235 	pti->pt_flags = 0;
236 	pti->pt_send = 0;
237 	pti->pt_ucntl = 0;
238 	return (0);
239 }
240 
241 ptcclose(dev)
242 	dev_t dev;
243 {
244 	register struct tty *tp;
245 
246 	tp = &pt_tty[minor(dev)];
247 	(void)(*linesw[tp->t_line].l_modem)(tp, 0);
248 	tp->t_state &= ~TS_CARR_ON;
249 	tp->t_oproc = 0;		/* mark closed */
250 	tp->t_session = 0;
251 }
252 
253 ptcread(dev, uio, flag)
254 	dev_t dev;
255 	struct uio *uio;
256 {
257 	register struct tty *tp = &pt_tty[minor(dev)];
258 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
259 	char buf[BUFSIZ];
260 	int error = 0, cc;
261 
262 	/*
263 	 * We want to block until the slave
264 	 * is open, and there's something to read;
265 	 * but if we lost the slave or we're NBIO,
266 	 * then return the appropriate error instead.
267 	 */
268 	for (;;) {
269 		if (tp->t_state&TS_ISOPEN) {
270 			if (pti->pt_flags&PF_PKT && pti->pt_send) {
271 				error = ureadc((int)pti->pt_send, uio);
272 				if (error)
273 					return (error);
274 				if (pti->pt_send & TIOCPKT_IOCTL) {
275 					cc = MIN(uio->uio_resid,
276 						sizeof(tp->t_termios));
277 					uiomove(&tp->t_termios, cc, uio);
278 				}
279 				pti->pt_send = 0;
280 				return (0);
281 			}
282 			if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
283 				error = ureadc((int)pti->pt_ucntl, uio);
284 				if (error)
285 					return (error);
286 				pti->pt_ucntl = 0;
287 				return (0);
288 			}
289 			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
290 				break;
291 		}
292 		if ((tp->t_state&TS_CARR_ON) == 0)
293 			return (0);	/* EOF */
294 		if (flag & IO_NDELAY)
295 			return (EWOULDBLOCK);
296 		if (error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
297 		    ttyin, 0))
298 			return (error);
299 	}
300 	if (pti->pt_flags & (PF_PKT|PF_UCNTL))
301 		error = ureadc(0, uio);
302 	while (uio->uio_resid > 0 && error == 0) {
303 		cc = q_to_b(&tp->t_outq, buf, MIN(uio->uio_resid, BUFSIZ));
304 		if (cc <= 0)
305 			break;
306 		error = uiomove(buf, cc, uio);
307 	}
308 	if (tp->t_outq.c_cc <= tp->t_lowat) {
309 		if (tp->t_state&TS_ASLEEP) {
310 			tp->t_state &= ~TS_ASLEEP;
311 			wakeup((caddr_t)&tp->t_outq);
312 		}
313 		if (tp->t_wsel) {
314 			selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
315 			tp->t_wsel = 0;
316 			tp->t_state &= ~TS_WCOLL;
317 		}
318 	}
319 	return (error);
320 }
321 
322 ptsstop(tp, flush)
323 	register struct tty *tp;
324 	int flush;
325 {
326 	struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
327 	int flag;
328 
329 	/* note: FLUSHREAD and FLUSHWRITE already ok */
330 	if (flush == 0) {
331 		flush = TIOCPKT_STOP;
332 		pti->pt_flags |= PF_STOPPED;
333 	} else
334 		pti->pt_flags &= ~PF_STOPPED;
335 	pti->pt_send |= flush;
336 	/* change of perspective */
337 	flag = 0;
338 	if (flush & FREAD)
339 		flag |= FWRITE;
340 	if (flush & FWRITE)
341 		flag |= FREAD;
342 	ptcwakeup(tp, flag);
343 }
344 
345 ptcselect(dev, rw, p)
346 	dev_t dev;
347 	int rw;
348 	struct proc *p;
349 {
350 	register struct tty *tp = &pt_tty[minor(dev)];
351 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
352 	struct proc *prev;
353 	int s;
354 
355 	if ((tp->t_state&TS_CARR_ON) == 0)
356 		return (1);
357 	switch (rw) {
358 
359 	case FREAD:
360 		/*
361 		 * Need to block timeouts (ttrstart).
362 		 */
363 		s = spltty();
364 		if ((tp->t_state&TS_ISOPEN) &&
365 		     tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
366 			splx(s);
367 			return (1);
368 		}
369 		splx(s);
370 		/* FALLTHROUGH */
371 
372 	case 0:					/* exceptional */
373 		if ((tp->t_state&TS_ISOPEN) &&
374 		    (pti->pt_flags&PF_PKT && pti->pt_send ||
375 		     pti->pt_flags&PF_UCNTL && pti->pt_ucntl))
376 			return (1);
377 		if ((prev = pti->pt_selr) && prev->p_wchan == (caddr_t)&selwait)
378 			pti->pt_flags |= PF_RCOLL;
379 		else
380 			pti->pt_selr = p;
381 		break;
382 
383 
384 	case FWRITE:
385 		if (tp->t_state&TS_ISOPEN) {
386 			if (pti->pt_flags & PF_REMOTE) {
387 			    if (tp->t_canq.c_cc == 0)
388 				return (1);
389 			} else {
390 			    if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
391 				    return (1);
392 			    if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
393 				    return (1);
394 			}
395 		}
396 		if ((prev = pti->pt_selw) && prev->p_wchan == (caddr_t)&selwait)
397 			pti->pt_flags |= PF_WCOLL;
398 		else
399 			pti->pt_selw = p;
400 		break;
401 
402 	}
403 	return (0);
404 }
405 
406 ptcwrite(dev, uio, flag)
407 	dev_t dev;
408 	register struct uio *uio;
409 {
410 	register struct tty *tp = &pt_tty[minor(dev)];
411 	register u_char *cp;
412 	register int cc = 0;
413 	u_char locbuf[BUFSIZ];
414 	int cnt = 0;
415 	struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
416 	int error = 0;
417 
418 again:
419 	if ((tp->t_state&TS_ISOPEN) == 0)
420 		goto block;
421 	if (pti->pt_flags & PF_REMOTE) {
422 		if (tp->t_canq.c_cc)
423 			goto block;
424 		while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
425 			if (cc == 0) {
426 				cc = min(uio->uio_resid, BUFSIZ);
427 				cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
428 				cp = locbuf;
429 				error = uiomove((caddr_t)cp, cc, uio);
430 				if (error)
431 					return (error);
432 				/* check again for safety */
433 				if ((tp->t_state&TS_ISOPEN) == 0)
434 					return (EIO);
435 			}
436 			if (cc)
437 				(void) b_to_q((char *)cp, cc, &tp->t_canq);
438 			cc = 0;
439 		}
440 		(void) putc(0, &tp->t_canq);
441 		ttwakeup(tp);
442 		wakeup((caddr_t)&tp->t_canq);
443 		return (0);
444 	}
445 	while (uio->uio_resid > 0) {
446 		if (cc == 0) {
447 			cc = min(uio->uio_resid, BUFSIZ);
448 			cp = locbuf;
449 			error = uiomove((caddr_t)cp, cc, uio);
450 			if (error)
451 				return (error);
452 			/* check again for safety */
453 			if ((tp->t_state&TS_ISOPEN) == 0)
454 				return (EIO);
455 		}
456 		while (cc > 0) {
457 			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
458 			   (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
459 				wakeup((caddr_t)&tp->t_rawq);
460 				goto block;
461 			}
462 			(*linesw[tp->t_line].l_rint)(*cp++, tp);
463 			cnt++;
464 			cc--;
465 		}
466 		cc = 0;
467 	}
468 	return (0);
469 block:
470 	/*
471 	 * Come here to wait for slave to open, for space
472 	 * in outq, or space in rawq.
473 	 */
474 	if ((tp->t_state&TS_CARR_ON) == 0)
475 		return (EIO);
476 	if (flag & IO_NDELAY) {
477 		/* adjust for data copied in but not written */
478 		uio->uio_resid += cc;
479 		if (cnt == 0)
480 			return (EWOULDBLOCK);
481 		return (0);
482 	}
483 	if (error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
484 	    ttyout, 0)) {
485 		/* adjust for data copied in but not written */
486 		uio->uio_resid += cc;
487 		return (error);
488 	}
489 	goto again;
490 }
491 
492 /*ARGSUSED*/
493 ptyioctl(dev, cmd, data, flag)
494 	caddr_t data;
495 	dev_t dev;
496 {
497 	register struct tty *tp = &pt_tty[minor(dev)];
498 	register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
499 	register u_char *cc = tp->t_cc;
500 	int stop, error;
501 	extern ttyinput();
502 
503 	/*
504 	 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
505 	 * ttywflush(tp) will hang if there are characters in the outq.
506 	 */
507 	if (cmd == TIOCEXT) {
508 		/*
509 		 * When the EXTPROC bit is being toggled, we need
510 		 * to send an TIOCPKT_IOCTL if the packet driver
511 		 * is turned on.
512 		 */
513 		if (*(int *)data) {
514 			if (pti->pt_flags & PF_PKT) {
515 				pti->pt_send |= TIOCPKT_IOCTL;
516 				ptcwakeup(tp);
517 			}
518 			tp->t_lflag |= EXTPROC;
519 		} else {
520 			if ((tp->t_state & EXTPROC) &&
521 			    (pti->pt_flags & PF_PKT)) {
522 				pti->pt_send |= TIOCPKT_IOCTL;
523 				ptcwakeup(tp);
524 			}
525 			tp->t_lflag &= ~EXTPROC;
526 		}
527 		return(0);
528 	} else
529 	if (cdevsw[major(dev)].d_open == ptcopen)
530 		switch (cmd) {
531 
532 		case TIOCGPGRP:
533 			/*
534 			 * We aviod calling ttioctl on the controller since,
535 			 * in that case, tp must be the controlling terminal.
536 			 */
537 			*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
538 			return (0);
539 
540 		case TIOCPKT:
541 			if (*(int *)data) {
542 				if (pti->pt_flags & PF_UCNTL)
543 					return (EINVAL);
544 				pti->pt_flags |= PF_PKT;
545 			} else
546 				pti->pt_flags &= ~PF_PKT;
547 			return (0);
548 
549 		case TIOCUCNTL:
550 			if (*(int *)data) {
551 				if (pti->pt_flags & PF_PKT)
552 					return (EINVAL);
553 				pti->pt_flags |= PF_UCNTL;
554 			} else
555 				pti->pt_flags &= ~PF_UCNTL;
556 			return (0);
557 
558 		case TIOCREMOTE:
559 			if (*(int *)data)
560 				pti->pt_flags |= PF_REMOTE;
561 			else
562 				pti->pt_flags &= ~PF_REMOTE;
563 			ttyflush(tp, FREAD|FWRITE);
564 			return (0);
565 
566 		case TIOCSETP:
567 		case TIOCSETN:
568 		case TIOCSETD:
569 		case TIOCSETA:
570 		case TIOCSETAW:
571 		case TIOCSETAF:
572 			while (getc(&tp->t_outq) >= 0)
573 				;
574 			break;
575 
576 		case TIOCSIG:
577 			if (*(unsigned int *)data >= NSIG)
578 				return(EINVAL);
579 			if ((tp->t_lflag&NOFLSH) == 0)
580 				ttyflush(tp, FREAD|FWRITE);
581 			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
582 			if ((*(unsigned int *)data == SIGINFO) &&
583 			    ((tp->t_lflag&NOKERNINFO) == 0))
584 				ttyinfo(tp);
585 			return(0);
586 		}
587 	error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
588 	if (error < 0)
589 		 error = ttioctl(tp, cmd, data, flag);
590 	/*
591 	 * Since we use the tty queues internally,
592 	 * pty's can't be switched to disciplines which overwrite
593 	 * the queues.  We can't tell anything about the discipline
594 	 * from here...
595 	 */
596 	if (linesw[tp->t_line].l_rint != ttyinput) {
597 		(*linesw[tp->t_line].l_close)(tp, flag);
598 		tp->t_line = TTYDISC;
599 		(void)(*linesw[tp->t_line].l_open)(dev, tp, flag);
600 		error = ENOTTY;
601 	}
602 	if (error < 0) {
603 		if (pti->pt_flags & PF_UCNTL &&
604 		    (cmd & ~0xff) == UIOCCMD(0)) {
605 			if (cmd & 0xff) {
606 				pti->pt_ucntl = (u_char)cmd;
607 				ptcwakeup(tp, FREAD);
608 			}
609 			return (0);
610 		}
611 		error = ENOTTY;
612 	}
613 	/*
614 	 * If external processing and packet mode send ioctl packet.
615 	 */
616 	if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
617 		switch(cmd) {
618 		case TIOCSETA:
619 		case TIOCSETAW:
620 		case TIOCSETAF:
621 		case TIOCSETP:
622 		case TIOCSETN:
623 #ifdef	COMPAT_43
624 		case TIOCSETC:
625 		case TIOCSLTC:
626 		case TIOCLBIS:
627 		case TIOCLBIC:
628 		case TIOCLSET:
629 #endif
630 			pti->pt_send |= TIOCPKT_IOCTL;
631 		default:
632 			break;
633 		}
634 	}
635 	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
636 		&& CCEQ(cc[VSTART], CTRL('q'));
637 	if (pti->pt_flags & PF_NOSTOP) {
638 		if (stop) {
639 			pti->pt_send &= ~TIOCPKT_NOSTOP;
640 			pti->pt_send |= TIOCPKT_DOSTOP;
641 			pti->pt_flags &= ~PF_NOSTOP;
642 			ptcwakeup(tp, FREAD);
643 		}
644 	} else {
645 		if (!stop) {
646 			pti->pt_send &= ~TIOCPKT_DOSTOP;
647 			pti->pt_send |= TIOCPKT_NOSTOP;
648 			pti->pt_flags |= PF_NOSTOP;
649 			ptcwakeup(tp, FREAD);
650 		}
651 	}
652 	return (error);
653 }
654 #endif
655