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