pty.c revision 136680
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 136680 2004-10-18 21:51:27Z 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		ttyinitmode(tp, 1, 0);
179	} else if (tp->t_state & TS_XCLUDE && suser(td))
180		return (EBUSY);
181	else if (pt->pt_prison != td->td_ucred->cr_prison)
182		return (EBUSY);
183	if (tp->t_oproc)			/* Ctrlr still around. */
184		(void)ttyld_modem(tp, 1);
185	while ((tp->t_state & TS_CARR_ON) == 0) {
186		if (flag&FNONBLOCK)
187			break;
188		error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
189				 "ptsopn", 0);
190		if (error)
191			return (error);
192	}
193	error = ttyld_open(tp, dev);
194	if (error == 0)
195		ptcwakeup(tp, FREAD|FWRITE);
196	return (error);
197}
198
199static	int
200ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
201{
202	struct tty *tp;
203	int err;
204
205	tp = dev->si_tty;
206	err = ttyld_close(tp, flag);
207	(void) tty_close(tp);
208	return (err);
209}
210
211static	int
212ptsread(struct cdev *dev, struct uio *uio, int flag)
213{
214	struct tty *tp = dev->si_tty;
215	int error = 0;
216
217	if (tp->t_oproc)
218		error = ttyld_read(tp, uio, flag);
219	ptcwakeup(tp, FWRITE);
220	return (error);
221}
222
223/*
224 * Write to pseudo-tty.
225 * Wakeups of controlling tty will happen
226 * indirectly, when tty driver calls ptsstart.
227 */
228static	int
229ptswrite(struct cdev *dev, struct uio *uio, int flag)
230{
231	struct tty *tp;
232
233	tp = dev->si_tty;
234	if (tp->t_oproc == 0)
235		return (EIO);
236	return (ttyld_write(tp, uio, flag));
237}
238
239/*
240 * Start output on pseudo-tty.
241 * Wake up process selecting or sleeping for input from controlling tty.
242 */
243static void
244ptsstart(struct tty *tp)
245{
246	struct ptsc *pt = tp->t_sc;
247
248	if (tp->t_state & TS_TTSTOP)
249		return;
250	if (pt->pt_flags & PF_STOPPED) {
251		pt->pt_flags &= ~PF_STOPPED;
252		pt->pt_send = TIOCPKT_START;
253	}
254	ptcwakeup(tp, FREAD);
255}
256
257static void
258ptcwakeup(struct tty *tp, int flag)
259{
260	struct ptsc *pt = tp->t_sc;
261
262	if (flag & FREAD) {
263		selwakeuppri(&pt->pt_selr, TTIPRI);
264		wakeup(TSA_PTC_READ(tp));
265	}
266	if (flag & FWRITE) {
267		selwakeuppri(&pt->pt_selw, TTOPRI);
268		wakeup(TSA_PTC_WRITE(tp));
269	}
270}
271
272static	int
273ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
274{
275	struct tty *tp;
276	struct ptsc *pt;
277
278	if (!dev->si_drv1)
279		ptyinit(dev);
280	if (!dev->si_drv1)
281		return(ENXIO);
282	tp = dev->si_tty;
283	if (tp->t_oproc)
284		return (EIO);
285	tp->t_timeout = -1;
286	tp->t_oproc = ptsstart;
287	tp->t_stop = ptsstop;
288	(void)ttyld_modem(tp, 1);
289	tp->t_lflag &= ~EXTPROC;
290	pt = dev->si_drv1;
291	pt->pt_prison = td->td_ucred->cr_prison;
292	pt->pt_flags = 0;
293	pt->pt_send = 0;
294	pt->pt_ucntl = 0;
295	return (0);
296}
297
298static	int
299ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
300{
301	struct tty *tp;
302
303	tp = dev->si_tty;
304	(void)ttyld_modem(tp, 0);
305
306	/*
307	 * XXX MDMBUF makes no sense for ptys but would inhibit the above
308	 * l_modem().  CLOCAL makes sense but isn't supported.   Special
309	 * l_modem()s that ignore carrier drop make no sense for ptys but
310	 * may be in use because other parts of the line discipline make
311	 * sense for ptys.  Recover by doing everything that a normal
312	 * ttymodem() would have done except for sending a SIGHUP.
313	 */
314	if (tp->t_state & TS_ISOPEN) {
315		tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
316		tp->t_state |= TS_ZOMBIE;
317		ttyflush(tp, FREAD | FWRITE);
318	}
319
320	tp->t_oproc = 0;		/* mark closed */
321	return (0);
322}
323
324static	int
325ptcread(struct cdev *dev, struct uio *uio, int flag)
326{
327	struct tty *tp = dev->si_tty;
328	struct ptsc *pt = dev->si_drv1;
329	char buf[BUFSIZ];
330	int error = 0, cc;
331
332	/*
333	 * We want to block until the slave
334	 * is open, and there's something to read;
335	 * but if we lost the slave or we're NBIO,
336	 * then return the appropriate error instead.
337	 */
338	for (;;) {
339		if (tp->t_state&TS_ISOPEN) {
340			if (pt->pt_flags&PF_PKT && pt->pt_send) {
341				error = ureadc((int)pt->pt_send, uio);
342				if (error)
343					return (error);
344				if (pt->pt_send & TIOCPKT_IOCTL) {
345					cc = min(uio->uio_resid,
346						sizeof(tp->t_termios));
347					uiomove(&tp->t_termios, cc, uio);
348				}
349				pt->pt_send = 0;
350				return (0);
351			}
352			if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
353				error = ureadc((int)pt->pt_ucntl, uio);
354				if (error)
355					return (error);
356				pt->pt_ucntl = 0;
357				return (0);
358			}
359			if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
360				break;
361		}
362		if ((tp->t_state & TS_CONNECTED) == 0)
363			return (0);	/* EOF */
364		if (flag & IO_NDELAY)
365			return (EWOULDBLOCK);
366		error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
367		if (error)
368			return (error);
369	}
370	if (pt->pt_flags & (PF_PKT|PF_UCNTL))
371		error = ureadc(0, uio);
372	while (uio->uio_resid > 0 && error == 0) {
373		cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
374		if (cc <= 0)
375			break;
376		error = uiomove(buf, cc, uio);
377	}
378	ttwwakeup(tp);
379	return (error);
380}
381
382static	void
383ptsstop(struct tty *tp, int flush)
384{
385	struct ptsc *pt = tp->t_sc;
386	int flag;
387
388	/* note: FLUSHREAD and FLUSHWRITE already ok */
389	if (flush == 0) {
390		flush = TIOCPKT_STOP;
391		pt->pt_flags |= PF_STOPPED;
392	} else
393		pt->pt_flags &= ~PF_STOPPED;
394	pt->pt_send |= flush;
395	/* change of perspective */
396	flag = 0;
397	if (flush & FREAD)
398		flag |= FWRITE;
399	if (flush & FWRITE)
400		flag |= FREAD;
401	ptcwakeup(tp, flag);
402}
403
404static	int
405ptcpoll(struct cdev *dev, int events, struct thread *td)
406{
407	struct tty *tp = dev->si_tty;
408	struct ptsc *pt = dev->si_drv1;
409	int revents = 0;
410	int s;
411
412	if ((tp->t_state & TS_CONNECTED) == 0)
413		return (events &
414		   (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
415
416	/*
417	 * Need to block timeouts (ttrstart).
418	 */
419	s = spltty();
420
421	if (events & (POLLIN | POLLRDNORM))
422		if ((tp->t_state & TS_ISOPEN) &&
423		    ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
424		     ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
425		     ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
426			revents |= events & (POLLIN | POLLRDNORM);
427
428	if (events & (POLLOUT | POLLWRNORM))
429		if (tp->t_state & TS_ISOPEN &&
430		    (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
431		      (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
432			revents |= events & (POLLOUT | POLLWRNORM);
433
434	if (events & POLLHUP)
435		if ((tp->t_state & TS_CARR_ON) == 0)
436			revents |= POLLHUP;
437
438	if (revents == 0) {
439		if (events & (POLLIN | POLLRDNORM))
440			selrecord(td, &pt->pt_selr);
441
442		if (events & (POLLOUT | POLLWRNORM))
443			selrecord(td, &pt->pt_selw);
444	}
445	splx(s);
446
447	return (revents);
448}
449
450static	int
451ptcwrite(struct cdev *dev, struct uio *uio, int flag)
452{
453	struct tty *tp = dev->si_tty;
454	u_char *cp = 0;
455	int cc = 0;
456	u_char locbuf[BUFSIZ];
457	int cnt = 0;
458	int error = 0;
459
460again:
461	if ((tp->t_state&TS_ISOPEN) == 0)
462		goto block;
463	while (uio->uio_resid > 0 || cc > 0) {
464		if (cc == 0) {
465			cc = min(uio->uio_resid, BUFSIZ);
466			cp = locbuf;
467			error = uiomove(cp, cc, uio);
468			if (error)
469				return (error);
470			/* check again for safety */
471			if ((tp->t_state & TS_ISOPEN) == 0) {
472				/* adjust for data copied in but not written */
473				uio->uio_resid += cc;
474				return (EIO);
475			}
476		}
477		while (cc > 0) {
478			if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
479			   (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
480				wakeup(TSA_HUP_OR_INPUT(tp));
481				goto block;
482			}
483			ttyld_rint(tp, *cp++);
484			cnt++;
485			cc--;
486		}
487		cc = 0;
488	}
489	return (0);
490block:
491	/*
492	 * Come here to wait for slave to open, for space
493	 * in outq, or space in rawq, or an empty canq.
494	 */
495	if ((tp->t_state & TS_CONNECTED) == 0) {
496		/* adjust for data copied in but not written */
497		uio->uio_resid += cc;
498		return (EIO);
499	}
500	if (flag & IO_NDELAY) {
501		/* adjust for data copied in but not written */
502		uio->uio_resid += cc;
503		if (cnt == 0)
504			return (EWOULDBLOCK);
505		return (0);
506	}
507	error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
508	if (error) {
509		/* adjust for data copied in but not written */
510		uio->uio_resid += cc;
511		return (error);
512	}
513	goto again;
514}
515
516/*ARGSUSED*/
517static	int
518ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
519{
520	struct tty *tp = dev->si_tty;
521	struct ptsc *pt = dev->si_drv1;
522
523	switch (cmd) {
524
525	case TIOCGPGRP:
526		/*
527		 * We avoid calling ttioctl on the controller since,
528		 * in that case, tp must be the controlling terminal.
529		 */
530		*(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
531		return (0);
532
533	case TIOCPKT:
534		if (*(int *)data) {
535			if (pt->pt_flags & PF_UCNTL)
536				return (EINVAL);
537			pt->pt_flags |= PF_PKT;
538		} else
539			pt->pt_flags &= ~PF_PKT;
540		return (0);
541
542	case TIOCUCNTL:
543		if (*(int *)data) {
544			if (pt->pt_flags & PF_PKT)
545				return (EINVAL);
546			pt->pt_flags |= PF_UCNTL;
547		} else
548			pt->pt_flags &= ~PF_UCNTL;
549		return (0);
550	}
551
552	/*
553	 * The rest of the ioctls shouldn't be called until
554	 * the slave is open.
555	 */
556	if ((tp->t_state & TS_ISOPEN) == 0)
557		return (EAGAIN);
558
559	switch (cmd) {
560#ifndef BURN_BRIDGES
561#ifdef COMPAT_43
562	case TIOCSETP:
563	case TIOCSETN:
564#endif
565#endif
566	case TIOCSETD:
567	case TIOCSETA:
568	case TIOCSETAW:
569	case TIOCSETAF:
570		/*
571		 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
572		 * ttywflush(tp) will hang if there are characters in
573		 * the outq.
574		 */
575		ndflush(&tp->t_outq, tp->t_outq.c_cc);
576		break;
577
578	case TIOCSIG:
579		if (*(unsigned int *)data >= NSIG ||
580		    *(unsigned int *)data == 0)
581			return(EINVAL);
582		if ((tp->t_lflag&NOFLSH) == 0)
583			ttyflush(tp, FREAD|FWRITE);
584		if (tp->t_pgrp != NULL) {
585			PGRP_LOCK(tp->t_pgrp);
586			pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
587			PGRP_UNLOCK(tp->t_pgrp);
588		}
589		if ((*(unsigned int *)data == SIGINFO) &&
590		    ((tp->t_lflag&NOKERNINFO) == 0))
591			ttyinfo(tp);
592		return(0);
593	}
594
595	return (ptsioctl(dev, cmd, data, flag, td));
596}
597
598/*ARGSUSED*/
599static	int
600ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
601{
602	struct tty *tp = dev->si_tty;
603	struct ptsc *pt = dev->si_drv1;
604	u_char *cc = tp->t_cc;
605	int stop, error;
606
607	if (cmd == TIOCEXT) {
608		/*
609		 * When the EXTPROC bit is being toggled, we need
610		 * to send an TIOCPKT_IOCTL if the packet driver
611		 * is turned on.
612		 */
613		if (*(int *)data) {
614			if (pt->pt_flags & PF_PKT) {
615				pt->pt_send |= TIOCPKT_IOCTL;
616				ptcwakeup(tp, FREAD);
617			}
618			tp->t_lflag |= EXTPROC;
619		} else {
620			if ((tp->t_lflag & EXTPROC) &&
621			    (pt->pt_flags & PF_PKT)) {
622				pt->pt_send |= TIOCPKT_IOCTL;
623				ptcwakeup(tp, FREAD);
624			}
625			tp->t_lflag &= ~EXTPROC;
626		}
627		return(0);
628	}
629	error = ttyioctl(dev, cmd, data, flag, td);
630	if (error == ENOTTY) {
631		if (pt->pt_flags & PF_UCNTL &&
632		    (cmd & ~0xff) == UIOCCMD(0)) {
633			if (cmd & 0xff) {
634				pt->pt_ucntl = (u_char)cmd;
635				ptcwakeup(tp, FREAD);
636			}
637			return (0);
638		}
639		error = ENOTTY;
640	}
641	/*
642	 * If external processing and packet mode send ioctl packet.
643	 */
644	if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
645		switch(cmd) {
646		case TIOCSETA:
647		case TIOCSETAW:
648		case TIOCSETAF:
649#ifndef BURN_BRIDGES
650#ifdef COMPAT_43
651		case TIOCSETP:
652		case TIOCSETN:
653		case TIOCSETC:
654		case TIOCSLTC:
655		case TIOCLBIS:
656		case TIOCLBIC:
657		case TIOCLSET:
658#endif
659#endif
660			pt->pt_send |= TIOCPKT_IOCTL;
661			ptcwakeup(tp, FREAD);
662			break;
663		default:
664			break;
665		}
666	}
667	stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
668		&& CCEQ(cc[VSTART], CTRL('q'));
669	if (pt->pt_flags & PF_NOSTOP) {
670		if (stop) {
671			pt->pt_send &= ~TIOCPKT_NOSTOP;
672			pt->pt_send |= TIOCPKT_DOSTOP;
673			pt->pt_flags &= ~PF_NOSTOP;
674			ptcwakeup(tp, FREAD);
675		}
676	} else {
677		if (!stop) {
678			pt->pt_send &= ~TIOCPKT_DOSTOP;
679			pt->pt_send |= TIOCPKT_NOSTOP;
680			pt->pt_flags |= PF_NOSTOP;
681			ptcwakeup(tp, FREAD);
682		}
683	}
684	return (error);
685}
686
687static void
688pty_clone(void *arg, char *name, int namelen, struct cdev **dev)
689{
690	int u;
691
692	if (*dev != NULL)
693		return;
694	if (bcmp(name, "pty", 3) != 0)
695		return;
696	if (name[5] != '\0')
697		return;
698	switch (name[3]) {
699	case 'p': u =   0; break;
700	case 'q': u =  32; break;
701	case 'r': u =  64; break;
702	case 's': u =  96; break;
703	case 'P': u = 128; break;
704	case 'Q': u = 160; break;
705	case 'R': u = 192; break;
706	case 'S': u = 224; break;
707	default: return;
708	}
709	if (name[4] >= '0' && name[4] <= '9')
710		u += name[4] - '0';
711	else if (name[4] >= 'a' && name[4] <= 'v')
712		u += name[4] - 'a' + 10;
713	else
714		return;
715	*dev = make_dev(&ptc_cdevsw, u,
716	    UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
717	(*dev)->si_flags |= SI_CHEAPCLONE;
718	return;
719}
720
721static void
722ptc_drvinit(void *unused)
723{
724
725	EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
726}
727
728SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
729