pty.c revision 135622
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 *	The Regents of the University of California.  All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 *    notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in the
12 *    documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 *    may be used to endorse or promote products derived from this software
15 *    without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 *	@(#)tty_pty.c	8.4 (Berkeley) 2/20/95
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/tty_pty.c 135622 2004-09-23 16:13:46Z phk $");
34
35/*
36 * Pseudo-teletype Driver
37 * (Actually two drivers, requiring two entries in 'cdevsw')
38 */
39#include "opt_compat.h"
40#include "opt_tty.h"
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/lock.h>
44#include <sys/mutex.h>
45#include <sys/sx.h>
46#ifndef BURN_BRIDGES
47#if defined(COMPAT_43)
48#include <sys/ioctl_compat.h>
49#endif
50#endif
51#include <sys/proc.h>
52#include <sys/tty.h>
53#include <sys/conf.h>
54#include <sys/fcntl.h>
55#include <sys/poll.h>
56#include <sys/kernel.h>
57#include <sys/vnode.h>
58#include <sys/signalvar.h>
59#include <sys/malloc.h>
60
61static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
62
63static void ptsstart(struct tty *tp);
64static void ptsstop(struct tty *tp, int rw);
65static void ptcwakeup(struct tty *tp, int flag);
66static struct cdev *ptyinit(struct cdev *cdev);
67
68static	d_open_t	ptsopen;
69static	d_close_t	ptsclose;
70static	d_read_t	ptsread;
71static	d_write_t	ptswrite;
72static	d_ioctl_t	ptsioctl;
73static	d_open_t	ptcopen;
74static	d_close_t	ptcclose;
75static	d_read_t	ptcread;
76static	d_ioctl_t	ptcioctl;
77static	d_write_t	ptcwrite;
78static	d_poll_t	ptcpoll;
79
80#define	CDEV_MAJOR_S	5
81static struct cdevsw pts_cdevsw = {
82	.d_version =	D_VERSION,
83	.d_open =	ptsopen,
84	.d_close =	ptsclose,
85	.d_read =	ptsread,
86	.d_write =	ptswrite,
87	.d_ioctl =	ptsioctl,
88	.d_name =	"pts",
89	.d_maj =	CDEV_MAJOR_S,
90	.d_flags =	D_TTY | D_NEEDGIANT,
91};
92
93#define	CDEV_MAJOR_C	6
94static struct cdevsw ptc_cdevsw = {
95	.d_version =	D_VERSION,
96	.d_open =	ptcopen,
97	.d_close =	ptcclose,
98	.d_read =	ptcread,
99	.d_write =	ptcwrite,
100	.d_ioctl =	ptcioctl,
101	.d_poll =	ptcpoll,
102	.d_name =	"ptc",
103	.d_maj =	CDEV_MAJOR_C,
104	.d_flags =	D_TTY | D_NEEDGIANT,
105};
106
107#define BUFSIZ 100		/* Chunk size iomoved to/from user */
108
109struct	ptsc {
110	int	pt_flags;
111	struct	selinfo pt_selr, pt_selw;
112	u_char	pt_send;
113	u_char	pt_ucntl;
114	struct tty *pt_tty;
115	struct cdev *devs, *devc;
116	struct	prison *pt_prison;
117};
118
119#define	PF_PKT		0x08		/* packet mode */
120#define	PF_STOPPED	0x10		/* user told stopped */
121#define	PF_NOSTOP	0x40
122#define PF_UCNTL	0x80		/* user control mode */
123
124#define	TSA_PTC_READ(tp)	((void *)&(tp)->t_outq.c_cf)
125#define	TSA_PTC_WRITE(tp)	((void *)&(tp)->t_rawq.c_cl)
126#define	TSA_PTS_READ(tp)	((void *)&(tp)->t_canq)
127
128static char *names = "pqrsPQRS";
129/*
130 * This function creates and initializes a pts/ptc pair
131 *
132 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
133 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
134 *
135 * XXX: define and add mapping of upper minor bits to allow more
136 *      than 256 ptys.
137 */
138static struct cdev *
139ptyinit(struct cdev *devc)
140{
141	struct cdev *devs;
142	struct ptsc *pt;
143	int n;
144
145	n = minor(devc);
146	/* For now we only map the lower 8 bits of the minor */
147	if (n & ~0xff)
148		return (NULL);
149
150	devc->si_flags &= ~SI_CHEAPCLONE;
151
152	pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
153	pt->devs = devs = make_dev(&pts_cdevsw, n,
154	    UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
155	pt->devc = devc;
156
157	pt->pt_tty = ttymalloc(pt->pt_tty);
158	pt->pt_tty->t_sc = pt;
159	devs->si_drv1 = devc->si_drv1 = pt;
160	devs->si_tty = devc->si_tty = pt->pt_tty;
161	pt->pt_tty->t_dev = devs;
162	return (devc);
163}
164
165/*ARGSUSED*/
166static	int
167ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
168{
169	struct tty *tp;
170	int error;
171	struct ptsc *pt;
172
173	if (!dev->si_drv1)
174		return(ENXIO);
175	pt = dev->si_drv1;
176	tp = dev->si_tty;
177	if ((tp->t_state & TS_ISOPEN) == 0) {
178		ttychars(tp);		/* Set up default chars */
179		tp->t_iflag = TTYDEF_IFLAG;
180		tp->t_oflag = TTYDEF_OFLAG;
181		tp->t_lflag = TTYDEF_LFLAG;
182		tp->t_cflag = TTYDEF_CFLAG;
183		tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
184	} else if (tp->t_state & TS_XCLUDE && suser(td))
185		return (EBUSY);
186	else if (pt->pt_prison != td->td_ucred->cr_prison)
187		return (EBUSY);
188	if (tp->t_oproc)			/* Ctrlr still around. */
189		(void)ttyld_modem(tp, 1);
190	while ((tp->t_state & TS_CARR_ON) == 0) {
191		if (flag&FNONBLOCK)
192			break;
193		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
194				 "ptsopn", 0);
195		if (error)
196			return (error);
197	}
198	error = ttyld_open(tp, dev);
199	if (error == 0)
200		ptcwakeup(tp, FREAD|FWRITE);
201	return (error);
202}
203
204static	int
205ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
206{
207	struct tty *tp;
208	int err;
209
210	tp = dev->si_tty;
211	err = ttyld_close(tp, flag);
212	(void) tty_close(tp);
213	return (err);
214}
215
216static	int
217ptsread(struct cdev *dev, struct uio *uio, int flag)
218{
219	struct tty *tp = dev->si_tty;
220	int error = 0;
221
222	if (tp->t_oproc)
223		error = ttyld_read(tp, uio, flag);
224	ptcwakeup(tp, FWRITE);
225	return (error);
226}
227
228/*
229 * Write to pseudo-tty.
230 * Wakeups of controlling tty will happen
231 * indirectly, when tty driver calls ptsstart.
232 */
233static	int
234ptswrite(struct cdev *dev, struct uio *uio, int flag)
235{
236	struct tty *tp;
237
238	tp = dev->si_tty;
239	if (tp->t_oproc == 0)
240		return (EIO);
241	return (ttyld_write(tp, uio, flag));
242}
243
244/*
245 * Start output on pseudo-tty.
246 * Wake up process selecting or sleeping for input from controlling tty.
247 */
248static void
249ptsstart(struct tty *tp)
250{
251	struct ptsc *pt = tp->t_sc;
252
253	if (tp->t_state & TS_TTSTOP)
254		return;
255	if (pt->pt_flags & PF_STOPPED) {
256		pt->pt_flags &= ~PF_STOPPED;
257		pt->pt_send = TIOCPKT_START;
258	}
259	ptcwakeup(tp, FREAD);
260}
261
262static void
263ptcwakeup(struct tty *tp, int flag)
264{
265	struct ptsc *pt = tp->t_sc;
266
267	if (flag & FREAD) {
268		selwakeuppri(&pt->pt_selr, TTIPRI);
269		wakeup(TSA_PTC_READ(tp));
270	}
271	if (flag & FWRITE) {
272		selwakeuppri(&pt->pt_selw, TTOPRI);
273		wakeup(TSA_PTC_WRITE(tp));
274	}
275}
276
277static	int
278ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
279{
280	struct tty *tp;
281	struct ptsc *pt;
282
283	if (!dev->si_drv1)
284		ptyinit(dev);
285	if (!dev->si_drv1)
286		return(ENXIO);
287	tp = dev->si_tty;
288	if (tp->t_oproc)
289		return (EIO);
290	tp->t_timeout = -1;
291	tp->t_oproc = ptsstart;
292	tp->t_stop = ptsstop;
293	(void)ttyld_modem(tp, 1);
294	tp->t_lflag &= ~EXTPROC;
295	pt = dev->si_drv1;
296	pt->pt_prison = td->td_ucred->cr_prison;
297	pt->pt_flags = 0;
298	pt->pt_send = 0;
299	pt->pt_ucntl = 0;
300	return (0);
301}
302
303static	int
304ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
305{
306	struct tty *tp;
307
308	tp = dev->si_tty;
309	(void)ttyld_modem(tp, 0);
310
311	/*
312	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
313	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
314	 * l_modem()s that ignore carrier drop make no sense for ptys but
315	 * may be in use because other parts of the line discipline make
316	 * sense for ptys.  Recover by doing everything that a normal
317	 * ttymodem() would have done except for sending a SIGHUP.
318	 */
319	if (tp->t_state & TS_ISOPEN) {
320		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
321		tp->t_state |= TS_ZOMBIE;
322		ttyflush(tp, FREAD | FWRITE);
323	}
324
325	tp->t_oproc = 0;		/* mark closed */
326	return (0);
327}
328
329static	int
330ptcread(struct cdev *dev, struct uio *uio, int flag)
331{
332	struct tty *tp = dev->si_tty;
333	struct ptsc *pt = dev->si_drv1;
334	char buf[BUFSIZ];
335	int error = 0, cc;
336
337	/*
338	 * We want to block until the slave
339	 * is open, and there's something to read;
340	 * but if we lost the slave or we're NBIO,
341	 * then return the appropriate error instead.
342	 */
343	for (;;) {
344		if (tp->t_state&TS_ISOPEN) {
345			if (pt->pt_flags&PF_PKT && pt->pt_send) {
346				error = ureadc((int)pt->pt_send, uio);
347				if (error)
348					return (error);
349				if (pt->pt_send & TIOCPKT_IOCTL) {
350					cc = min(uio->uio_resid,
351						sizeof(tp->t_termios));
352					uiomove(&tp->t_termios, cc, uio);
353				}
354				pt->pt_send = 0;
355				return (0);
356			}
357			if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
358				error = ureadc((int)pt->pt_ucntl, uio);
359				if (error)
360					return (error);
361				pt->pt_ucntl = 0;
362				return (0);
363			}
364			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
365				break;
366		}
367		if ((tp->t_state & TS_CONNECTED) == 0)
368			return (0);	/* EOF */
369		if (flag & IO_NDELAY)
370			return (EWOULDBLOCK);
371		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
372		if (error)
373			return (error);
374	}
375	if (pt->pt_flags & (PF_PKT|PF_UCNTL))
376		error = ureadc(0, uio);
377	while (uio->uio_resid > 0 && error == 0) {
378		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
379		if (cc <= 0)
380			break;
381		error = uiomove(buf, cc, uio);
382	}
383	ttwwakeup(tp);
384	return (error);
385}
386
387static	void
388ptsstop(struct tty *tp, int flush)
389{
390	struct ptsc *pt = tp->t_sc;
391	int flag;
392
393	/* note: FLUSHREAD and FLUSHWRITE already ok */
394	if (flush == 0) {
395		flush = TIOCPKT_STOP;
396		pt->pt_flags |= PF_STOPPED;
397	} else
398		pt->pt_flags &= ~PF_STOPPED;
399	pt->pt_send |= flush;
400	/* change of perspective */
401	flag = 0;
402	if (flush & FREAD)
403		flag |= FWRITE;
404	if (flush & FWRITE)
405		flag |= FREAD;
406	ptcwakeup(tp, flag);
407}
408
409static	int
410ptcpoll(struct cdev *dev, int events, struct thread *td)
411{
412	struct tty *tp = dev->si_tty;
413	struct ptsc *pt = dev->si_drv1;
414	int revents = 0;
415	int s;
416
417	if ((tp->t_state & TS_CONNECTED) == 0)
418		return (events &
419		   (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
420
421	/*
422	 * Need to block timeouts (ttrstart).
423	 */
424	s = spltty();
425
426	if (events & (POLLIN | POLLRDNORM))
427		if ((tp->t_state & TS_ISOPEN) &&
428		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
429		     ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
430		     ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
431			revents |= events & (POLLIN | POLLRDNORM);
432
433	if (events & (POLLOUT | POLLWRNORM))
434		if (tp->t_state & TS_ISOPEN &&
435		    (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
436		      (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
437			revents |= events & (POLLOUT | POLLWRNORM);
438
439	if (events & POLLHUP)
440		if ((tp->t_state & TS_CARR_ON) == 0)
441			revents |= POLLHUP;
442
443	if (revents == 0) {
444		if (events & (POLLIN | POLLRDNORM))
445			selrecord(td, &pt->pt_selr);
446
447		if (events & (POLLOUT | POLLWRNORM))
448			selrecord(td, &pt->pt_selw);
449	}
450	splx(s);
451
452	return (revents);
453}
454
455static	int
456ptcwrite(struct cdev *dev, struct uio *uio, int flag)
457{
458	struct tty *tp = dev->si_tty;
459	u_char *cp = 0;
460	int cc = 0;
461	u_char locbuf[BUFSIZ];
462	int cnt = 0;
463	int error = 0;
464
465again:
466	if ((tp->t_state&TS_ISOPEN) == 0)
467		goto block;
468	while (uio->uio_resid > 0 || cc > 0) {
469		if (cc == 0) {
470			cc = min(uio->uio_resid, BUFSIZ);
471			cp = locbuf;
472			error = uiomove(cp, cc, uio);
473			if (error)
474				return (error);
475			/* check again for safety */
476			if ((tp->t_state & TS_ISOPEN) == 0) {
477				/* adjust for data copied in but not written */
478				uio->uio_resid += cc;
479				return (EIO);
480			}
481		}
482		while (cc > 0) {
483			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
484			   (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
485				wakeup(TSA_HUP_OR_INPUT(tp));
486				goto block;
487			}
488			ttyld_rint(tp, *cp++);
489			cnt++;
490			cc--;
491		}
492		cc = 0;
493	}
494	return (0);
495block:
496	/*
497	 * Come here to wait for slave to open, for space
498	 * in outq, or space in rawq, or an empty canq.
499	 */
500	if ((tp->t_state & TS_CONNECTED) == 0) {
501		/* adjust for data copied in but not written */
502		uio->uio_resid += cc;
503		return (EIO);
504	}
505	if (flag & IO_NDELAY) {
506		/* adjust for data copied in but not written */
507		uio->uio_resid += cc;
508		if (cnt == 0)
509			return (EWOULDBLOCK);
510		return (0);
511	}
512	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
513	if (error) {
514		/* adjust for data copied in but not written */
515		uio->uio_resid += cc;
516		return (error);
517	}
518	goto again;
519}
520
521/*ARGSUSED*/
522static	int
523ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
524{
525	struct tty *tp = dev->si_tty;
526	struct ptsc *pt = dev->si_drv1;
527
528	switch (cmd) {
529
530	case TIOCGPGRP:
531		/*
532		 * We avoid calling ttioctl on the controller since,
533		 * in that case, tp must be the controlling terminal.
534		 */
535		*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
536		return (0);
537
538	case TIOCPKT:
539		if (*(int *)data) {
540			if (pt->pt_flags & PF_UCNTL)
541				return (EINVAL);
542			pt->pt_flags |= PF_PKT;
543		} else
544			pt->pt_flags &= ~PF_PKT;
545		return (0);
546
547	case TIOCUCNTL:
548		if (*(int *)data) {
549			if (pt->pt_flags & PF_PKT)
550				return (EINVAL);
551			pt->pt_flags |= PF_UCNTL;
552		} else
553			pt->pt_flags &= ~PF_UCNTL;
554		return (0);
555	}
556
557	/*
558	 * The rest of the ioctls shouldn't be called until
559	 * the slave is open.
560	 */
561	if ((tp->t_state & TS_ISOPEN) == 0)
562		return (EAGAIN);
563
564	switch (cmd) {
565#ifndef BURN_BRIDGES
566#ifdef COMPAT_43
567	case TIOCSETP:
568	case TIOCSETN:
569#endif
570#endif
571	case TIOCSETD:
572	case TIOCSETA:
573	case TIOCSETAW:
574	case TIOCSETAF:
575		/*
576		 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
577		 * ttywflush(tp) will hang if there are characters in
578		 * the outq.
579		 */
580		ndflush(&tp->t_outq, tp->t_outq.c_cc);
581		break;
582
583	case TIOCSIG:
584		if (*(unsigned int *)data >= NSIG ||
585		    *(unsigned int *)data == 0)
586			return(EINVAL);
587		if ((tp->t_lflag&NOFLSH) == 0)
588			ttyflush(tp, FREAD|FWRITE);
589		if (tp->t_pgrp != NULL) {
590			PGRP_LOCK(tp->t_pgrp);
591			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
592			PGRP_UNLOCK(tp->t_pgrp);
593		}
594		if ((*(unsigned int *)data == SIGINFO) &&
595		    ((tp->t_lflag&NOKERNINFO) == 0))
596			ttyinfo(tp);
597		return(0);
598	}
599
600	return (ptsioctl(dev, cmd, data, flag, td));
601}
602
603/*ARGSUSED*/
604static	int
605ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
606{
607	struct tty *tp = dev->si_tty;
608	struct ptsc *pt = dev->si_drv1;
609	u_char *cc = tp->t_cc;
610	int stop, error;
611
612	if (cmd == TIOCEXT) {
613		/*
614		 * When the EXTPROC bit is being toggled, we need
615		 * to send an TIOCPKT_IOCTL if the packet driver
616		 * is turned on.
617		 */
618		if (*(int *)data) {
619			if (pt->pt_flags & PF_PKT) {
620				pt->pt_send |= TIOCPKT_IOCTL;
621				ptcwakeup(tp, FREAD);
622			}
623			tp->t_lflag |= EXTPROC;
624		} else {
625			if ((tp->t_lflag & EXTPROC) &&
626			    (pt->pt_flags & PF_PKT)) {
627				pt->pt_send |= TIOCPKT_IOCTL;
628				ptcwakeup(tp, FREAD);
629			}
630			tp->t_lflag &= ~EXTPROC;
631		}
632		return(0);
633	}
634	error = ttyioctl(dev, cmd, data, flag, td);
635	if (error == ENOTTY) {
636		if (pt->pt_flags & PF_UCNTL &&
637		    (cmd & ~0xff) == UIOCCMD(0)) {
638			if (cmd & 0xff) {
639				pt->pt_ucntl = (u_char)cmd;
640				ptcwakeup(tp, FREAD);
641			}
642			return (0);
643		}
644		error = ENOTTY;
645	}
646	/*
647	 * If external processing and packet mode send ioctl packet.
648	 */
649	if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
650		switch(cmd) {
651		case TIOCSETA:
652		case TIOCSETAW:
653		case TIOCSETAF:
654#ifndef BURN_BRIDGES
655#ifdef COMPAT_43
656		case TIOCSETP:
657		case TIOCSETN:
658		case TIOCSETC:
659		case TIOCSLTC:
660		case TIOCLBIS:
661		case TIOCLBIC:
662		case TIOCLSET:
663#endif
664#endif
665			pt->pt_send |= TIOCPKT_IOCTL;
666			ptcwakeup(tp, FREAD);
667			break;
668		default:
669			break;
670		}
671	}
672	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
673		&& CCEQ(cc[VSTART], CTRL('q'));
674	if (pt->pt_flags & PF_NOSTOP) {
675		if (stop) {
676			pt->pt_send &= ~TIOCPKT_NOSTOP;
677			pt->pt_send |= TIOCPKT_DOSTOP;
678			pt->pt_flags &= ~PF_NOSTOP;
679			ptcwakeup(tp, FREAD);
680		}
681	} else {
682		if (!stop) {
683			pt->pt_send &= ~TIOCPKT_DOSTOP;
684			pt->pt_send |= TIOCPKT_NOSTOP;
685			pt->pt_flags |= PF_NOSTOP;
686			ptcwakeup(tp, FREAD);
687		}
688	}
689	return (error);
690}
691
692static void
693pty_clone(void *arg, char *name, int namelen, struct cdev **dev)
694{
695	int u;
696
697	if (*dev != NULL)
698		return;
699	if (bcmp(name, "pty", 3) != 0)
700		return;
701	if (name[5] != '\0')
702		return;
703	switch (name[3]) {
704	case 'p': u =   0; break;
705	case 'q': u =  32; break;
706	case 'r': u =  64; break;
707	case 's': u =  96; break;
708	case 'P': u = 128; break;
709	case 'Q': u = 160; break;
710	case 'R': u = 192; break;
711	case 'S': u = 224; break;
712	default: return;
713	}
714	if (name[4] >= '0' && name[4] <= '9')
715		u += name[4] - '0';
716	else if (name[4] >= 'a' && name[4] <= 'v')
717		u += name[4] - 'a' + 10;
718	else
719		return;
720	*dev = make_dev(&ptc_cdevsw, u,
721	    UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
722	(*dev)->si_flags |= SI_CHEAPCLONE;
723	return;
724}
725
726static void
727ptc_drvinit(void *unused)
728{
729
730	EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
731}
732
733SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
734