xref: /freebsd/sys/kern/tty_pts.c (revision 830940567b49bb0c08dfaed40418999e76616909)
1 /*-
2  * Copyright (c) 2008 Ed Schouten <ed@FreeBSD.org>
3  * All rights reserved.
4  *
5  * Portions of this software were developed under sponsorship from Snow
6  * B.V., the Netherlands.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR 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 AUTHOR 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 
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32 
33 #include "opt_tty.h"
34 
35 /* Add compatibility bits for FreeBSD. */
36 #define PTS_COMPAT
37 #ifdef DEV_PTY
38 /* Add /dev/ptyXX compat bits. */
39 #define PTS_EXTERNAL
40 #endif /* DEV_PTY */
41 /* Add bits to make Linux binaries work. */
42 #define PTS_LINUX
43 
44 #include <sys/param.h>
45 #include <sys/lock.h>
46 #include <sys/condvar.h>
47 #include <sys/conf.h>
48 #include <sys/fcntl.h>
49 #include <sys/file.h>
50 #include <sys/filedesc.h>
51 #include <sys/filio.h>
52 #include <sys/kernel.h>
53 #include <sys/limits.h>
54 #include <sys/malloc.h>
55 #include <sys/poll.h>
56 #include <sys/proc.h>
57 #include <sys/resourcevar.h>
58 #include <sys/serial.h>
59 #include <sys/stat.h>
60 #include <sys/syscall.h>
61 #include <sys/syscallsubr.h>
62 #include <sys/sysctl.h>
63 #include <sys/sysent.h>
64 #include <sys/sysproto.h>
65 #include <sys/systm.h>
66 #include <sys/tty.h>
67 #include <sys/ttycom.h>
68 
69 #include <machine/stdarg.h>
70 
71 /*
72  * Our utmp(5) format is limited to 8-byte TTY line names.  This means
73  * we can at most allocate 1000 pseudo-terminals ("pts/999").  Allow
74  * users to increase this number, assuming they have manually increased
75  * UT_LINESIZE.
76  */
77 static struct unrhdr *pts_pool;
78 static unsigned int pts_maxdev = 999;
79 SYSCTL_UINT(_kern, OID_AUTO, pts_maxdev, CTLFLAG_RW, &pts_maxdev, 0,
80     "Maximum amount of pts(4) pseudo-terminals");
81 
82 static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device");
83 
84 /*
85  * Per-PTS structure.
86  *
87  * List of locks
88  * (t)	locked by tty_lock()
89  * (c)	const until freeing
90  */
91 struct pts_softc {
92 	int		pts_unit;	/* (c) Device unit number. */
93 	unsigned int	pts_flags;	/* (t) Device flags. */
94 #define	PTS_PKT		0x1	/* Packet mode. */
95 #define	PTS_FINISHED	0x2	/* Return errors on read()/write(). */
96 	char		pts_pkt;	/* (t) Unread packet mode data. */
97 
98 	struct cv	pts_inwait;	/* (t) Blocking write() on master. */
99 	struct selinfo	pts_inpoll;	/* (t) Select queue for write(). */
100 	struct cv	pts_outwait;	/* (t) Blocking read() on master. */
101 	struct selinfo	pts_outpoll;	/* (t) Select queue for read(). */
102 
103 #ifdef PTS_EXTERNAL
104 	struct cdev	*pts_cdev;	/* (c) Master device node. */
105 #endif /* PTS_EXTERNAL */
106 
107 	struct uidinfo	*pts_uidinfo;	/* (c) Resource limit. */
108 };
109 
110 /*
111  * Controller-side file operations.
112  */
113 
114 static int
115 ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
116     int flags, struct thread *td)
117 {
118 	struct tty *tp = fp->f_data;
119 	struct pts_softc *psc = tty_softc(tp);
120 	int error = 0;
121 	char pkt;
122 
123 	if (uio->uio_resid == 0)
124 		return (0);
125 
126 	tty_lock(tp);
127 
128 	for (;;) {
129 		/*
130 		 * Implement packet mode. When packet mode is turned on,
131 		 * the first byte contains a bitmask of events that
132 		 * occured (start, stop, flush, window size, etc).
133 		 */
134 		if (psc->pts_flags & PTS_PKT && psc->pts_pkt) {
135 			pkt = psc->pts_pkt;
136 			psc->pts_pkt = 0;
137 			tty_unlock(tp);
138 
139 			error = ureadc(pkt, uio);
140 			return (error);
141 		}
142 
143 		/*
144 		 * Transmit regular data.
145 		 *
146 		 * XXX: We shouldn't use ttydisc_getc_poll()! Even
147 		 * though in this implementation, there is likely going
148 		 * to be data, we should just call ttydisc_getc_uio()
149 		 * and use its return value to sleep.
150 		 */
151 		if (ttydisc_getc_poll(tp)) {
152 			if (psc->pts_flags & PTS_PKT) {
153 				/*
154 				 * XXX: Small race. Fortunately PTY
155 				 * consumers aren't multithreaded.
156 				 */
157 
158 				tty_unlock(tp);
159 				error = ureadc(TIOCPKT_DATA, uio);
160 				if (error)
161 					return (error);
162 				tty_lock(tp);
163 			}
164 
165 			error = ttydisc_getc_uio(tp, uio);
166 			break;
167 		}
168 
169 		/* Maybe the device isn't used anyway. */
170 		if (psc->pts_flags & PTS_FINISHED)
171 			break;
172 
173 		/* Wait for more data. */
174 		if (fp->f_flag & O_NONBLOCK) {
175 			error = EWOULDBLOCK;
176 			break;
177 		}
178 		error = cv_wait_sig(&psc->pts_outwait, tp->t_mtx);
179 		if (error != 0)
180 			break;
181 	}
182 
183 	tty_unlock(tp);
184 
185 	return (error);
186 }
187 
188 static int
189 ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
190     int flags, struct thread *td)
191 {
192 	struct tty *tp = fp->f_data;
193 	struct pts_softc *psc = tty_softc(tp);
194 	char ib[256], *ibstart;
195 	size_t iblen, rintlen;
196 	int error = 0;
197 
198 	if (uio->uio_resid == 0)
199 		return (0);
200 
201 	for (;;) {
202 		ibstart = ib;
203 		iblen = MIN(uio->uio_resid, sizeof ib);
204 		error = uiomove(ib, iblen, uio);
205 
206 		tty_lock(tp);
207 		if (error != 0) {
208 			iblen = 0;
209 			goto done;
210 		}
211 
212 		/*
213 		 * When possible, avoid the slow path. rint_bypass()
214 		 * copies all input to the input queue at once.
215 		 */
216 		MPASS(iblen > 0);
217 		do {
218 			if (ttydisc_can_bypass(tp)) {
219 				/* Store data at once. */
220 				rintlen = ttydisc_rint_bypass(tp,
221 				    ibstart, iblen);
222 				ibstart += rintlen;
223 				iblen -= rintlen;
224 
225 				if (iblen == 0) {
226 					/* All data written. */
227 					break;
228 				}
229 			} else {
230 				error = ttydisc_rint(tp, *ibstart, 0);
231 				if (error == 0) {
232 					/* Character stored successfully. */
233 					ibstart++;
234 					iblen--;
235 					continue;
236 				}
237 			}
238 
239 			/* Maybe the device isn't used anyway. */
240 			if (psc->pts_flags & PTS_FINISHED) {
241 				error = EIO;
242 				goto done;
243 			}
244 
245 			/* Wait for more data. */
246 			if (fp->f_flag & O_NONBLOCK) {
247 				error = EWOULDBLOCK;
248 				goto done;
249 			}
250 
251 			/* Wake up users on the slave side. */
252 			ttydisc_rint_done(tp);
253 			error = cv_wait_sig(&psc->pts_inwait, tp->t_mtx);
254 			if (error != 0)
255 				goto done;
256 		} while (iblen > 0);
257 
258 		if (uio->uio_resid == 0)
259 			break;
260 		tty_unlock(tp);
261 	}
262 
263 done:	ttydisc_rint_done(tp);
264 	tty_unlock(tp);
265 
266 	/*
267 	 * Don't account for the part of the buffer that we couldn't
268 	 * pass to the TTY.
269 	 */
270 	uio->uio_resid += iblen;
271 	return (error);
272 }
273 
274 static int
275 ptsdev_truncate(struct file *fp, off_t length, struct ucred *active_cred,
276     struct thread *td)
277 {
278 
279 	return (EINVAL);
280 }
281 
282 static int
283 ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
284     struct ucred *active_cred, struct thread *td)
285 {
286 	struct tty *tp = fp->f_data;
287 	struct pts_softc *psc = tty_softc(tp);
288 	int error = 0, sig;
289 
290 	switch (cmd) {
291 	case FIONBIO:
292 		/* This device supports non-blocking operation. */
293 		return (0);
294 	case FIONREAD:
295 		tty_lock(tp);
296 		if (psc->pts_flags & PTS_FINISHED) {
297 			/* Force read() to be called. */
298 			*(int *)data = 1;
299 		} else {
300 			*(int *)data = ttydisc_getc_poll(tp);
301 		}
302 		tty_unlock(tp);
303 		return (0);
304 	case FIODGNAME: {
305 		struct fiodgname_arg *fgn;
306 		const char *p;
307 		int i;
308 
309 		/* Reverse device name lookups, for ptsname() and ttyname(). */
310 		fgn = data;
311 		p = tty_devname(tp);
312 		i = strlen(p) + 1;
313 		if (i > fgn->len)
314 			return (EINVAL);
315 		return copyout(p, fgn->buf, i);
316 	}
317 
318 	/*
319 	 * We need to implement TIOCGPGRP and TIOCGSID here again. When
320 	 * called on the pseudo-terminal master, it should not check if
321 	 * the terminal is the foreground terminal of the calling
322 	 * process.
323 	 *
324 	 * TIOCGETA is also implemented here. Various Linux PTY routines
325 	 * often call isatty(), which is implemented by tcgetattr().
326 	 */
327 #ifdef PTS_LINUX
328 	case TIOCGETA:
329 		/* Obtain terminal flags through tcgetattr(). */
330 		tty_lock(tp);
331 		*(struct termios*)data = tp->t_termios;
332 		tty_unlock(tp);
333 		return (0);
334 #endif /* PTS_LINUX */
335 	case TIOCSETAF:
336 	case TIOCSETAW:
337 		/*
338 		 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and
339 		 * TCSADRAIN into something different. If an application would
340 		 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may
341 		 * deadlock waiting for all data to be read.
342 		 */
343 		cmd = TIOCSETA;
344 		break;
345 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
346 	case TIOCGPTN:
347 		/*
348 		 * Get the device unit number.
349 		 */
350 		if (psc->pts_unit < 0)
351 			return (ENOTTY);
352 		*(unsigned int *)data = psc->pts_unit;
353 		return (0);
354 #endif /* PTS_COMPAT || PTS_LINUX */
355 	case TIOCGPGRP:
356 		/* Get the foreground process group ID. */
357 		tty_lock(tp);
358 		if (tp->t_pgrp != NULL)
359 			*(int *)data = tp->t_pgrp->pg_id;
360 		else
361 			*(int *)data = NO_PID;
362 		tty_unlock(tp);
363 		return (0);
364 	case TIOCGSID:
365 		/* Get the session leader process ID. */
366 		tty_lock(tp);
367 		if (tp->t_session == NULL)
368 			error = ENOTTY;
369 		else
370 			*(int *)data = tp->t_session->s_sid;
371 		tty_unlock(tp);
372 		return (error);
373 	case TIOCPTMASTER:
374 		/* Yes, we are a pseudo-terminal master. */
375 		return (0);
376 	case TIOCSIG:
377 		/* Signal the foreground process group. */
378 		sig = *(int *)data;
379 		if (sig < 1 || sig >= NSIG)
380 			return (EINVAL);
381 
382 		tty_lock(tp);
383 		tty_signal_pgrp(tp, sig);
384 		tty_unlock(tp);
385 		return (0);
386 	case TIOCPKT:
387 		/* Enable/disable packet mode. */
388 		tty_lock(tp);
389 		if (*(int *)data)
390 			psc->pts_flags |= PTS_PKT;
391 		else
392 			psc->pts_flags &= ~PTS_PKT;
393 		tty_unlock(tp);
394 		return (0);
395 	}
396 
397 	/* Just redirect this ioctl to the slave device. */
398 	tty_lock(tp);
399 	error = tty_ioctl(tp, cmd, data, td);
400 	tty_unlock(tp);
401 	if (error == ENOIOCTL)
402 		error = ENOTTY;
403 
404 	return (error);
405 }
406 
407 static int
408 ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
409     struct thread *td)
410 {
411 	struct tty *tp = fp->f_data;
412 	struct pts_softc *psc = tty_softc(tp);
413 	int revents = 0;
414 
415 	tty_lock(tp);
416 
417 	if (psc->pts_flags & PTS_FINISHED) {
418 		/* Slave device is not opened. */
419 		tty_unlock(tp);
420 		return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
421 	}
422 
423 	if (events & (POLLIN|POLLRDNORM)) {
424 		/* See if we can getc something. */
425 		if (ttydisc_getc_poll(tp) ||
426 		    (psc->pts_flags & PTS_PKT && psc->pts_pkt))
427 			revents |= events & (POLLIN|POLLRDNORM);
428 	}
429 	if (events & (POLLOUT|POLLWRNORM)) {
430 		/* See if we can rint something. */
431 		if (ttydisc_rint_poll(tp))
432 			revents |= events & (POLLOUT|POLLWRNORM);
433 	}
434 
435 	/*
436 	 * No need to check for POLLHUP here. This device cannot be used
437 	 * as a callout device, which means we always have a carrier,
438 	 * because the master is.
439 	 */
440 
441 	if (revents == 0) {
442 		/*
443 		 * This code might look misleading, but the naming of
444 		 * poll events on this side is the opposite of the slave
445 		 * device.
446 		 */
447 		if (events & (POLLIN|POLLRDNORM))
448 			selrecord(td, &psc->pts_outpoll);
449 		if (events & (POLLOUT|POLLWRNORM))
450 			selrecord(td, &psc->pts_inpoll);
451 	}
452 
453 	tty_unlock(tp);
454 
455 	return (revents);
456 }
457 
458 /*
459  * kqueue support.
460  */
461 
462 static void
463 pts_kqops_read_detach(struct knote *kn)
464 {
465 	struct file *fp = kn->kn_fp;
466 	struct tty *tp = fp->f_data;
467 	struct pts_softc *psc = tty_softc(tp);
468 
469 	knlist_remove(&psc->pts_outpoll.si_note, kn, 0);
470 }
471 
472 static int
473 pts_kqops_read_event(struct knote *kn, long hint)
474 {
475 	struct file *fp = kn->kn_fp;
476 	struct tty *tp = fp->f_data;
477 	struct pts_softc *psc = tty_softc(tp);
478 
479 	if (psc->pts_flags & PTS_FINISHED) {
480 		kn->kn_flags |= EV_EOF;
481 		return (1);
482 	} else {
483 		kn->kn_data = ttydisc_getc_poll(tp);
484 		return (kn->kn_data > 0);
485 	}
486 }
487 
488 static void
489 pts_kqops_write_detach(struct knote *kn)
490 {
491 	struct file *fp = kn->kn_fp;
492 	struct tty *tp = fp->f_data;
493 	struct pts_softc *psc = tty_softc(tp);
494 
495 	knlist_remove(&psc->pts_inpoll.si_note, kn, 0);
496 }
497 
498 static int
499 pts_kqops_write_event(struct knote *kn, long hint)
500 {
501 	struct file *fp = kn->kn_fp;
502 	struct tty *tp = fp->f_data;
503 	struct pts_softc *psc = tty_softc(tp);
504 
505 	if (psc->pts_flags & PTS_FINISHED) {
506 		kn->kn_flags |= EV_EOF;
507 		return (1);
508 	} else {
509 		kn->kn_data = ttydisc_rint_poll(tp);
510 		return (kn->kn_data > 0);
511 	}
512 }
513 
514 static struct filterops pts_kqops_read =
515     { 1, NULL, pts_kqops_read_detach, pts_kqops_read_event };
516 static struct filterops pts_kqops_write =
517     { 1, NULL, pts_kqops_write_detach, pts_kqops_write_event };
518 
519 static int
520 ptsdev_kqfilter(struct file *fp, struct knote *kn)
521 {
522 	struct tty *tp = fp->f_data;
523 	struct pts_softc *psc = tty_softc(tp);
524 	int error = 0;
525 
526 	tty_lock(tp);
527 
528 	switch (kn->kn_filter) {
529 	case EVFILT_READ:
530 		kn->kn_fop = &pts_kqops_read;
531 		knlist_add(&psc->pts_outpoll.si_note, kn, 1);
532 		break;
533 	case EVFILT_WRITE:
534 		kn->kn_fop = &pts_kqops_write;
535 		knlist_add(&psc->pts_inpoll.si_note, kn, 1);
536 		break;
537 	default:
538 		error = EINVAL;
539 		break;
540 	}
541 
542 	tty_unlock(tp);
543 	return (error);
544 }
545 
546 static int
547 ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
548     struct thread *td)
549 {
550 	struct tty *tp = fp->f_data;
551 #ifdef PTS_EXTERNAL
552 	struct pts_softc *psc = tty_softc(tp);
553 #endif /* PTS_EXTERNAL */
554 	struct cdev *dev = tp->t_dev;
555 
556 	/*
557 	 * According to POSIX, we must implement an fstat(). This also
558 	 * makes this implementation compatible with Linux binaries,
559 	 * because Linux calls fstat() on the pseudo-terminal master to
560 	 * obtain st_rdev.
561 	 *
562 	 * XXX: POSIX also mentions we must fill in st_dev, but how?
563 	 */
564 
565 	bzero(sb, sizeof *sb);
566 #ifdef PTS_EXTERNAL
567 	if (psc->pts_cdev != NULL)
568 		sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
569 	else
570 #endif /* PTS_EXTERNAL */
571 		sb->st_ino = sb->st_rdev = tty_udev(tp);
572 
573 	sb->st_atimespec = dev->si_atime;
574 	sb->st_ctimespec = dev->si_ctime;
575 	sb->st_mtimespec = dev->si_mtime;
576 	sb->st_uid = dev->si_uid;
577 	sb->st_gid = dev->si_gid;
578 	sb->st_mode = dev->si_mode | S_IFCHR;
579 
580 	return (0);
581 }
582 
583 static int
584 ptsdev_close(struct file *fp, struct thread *td)
585 {
586 	struct tty *tp = fp->f_data;
587 
588 	/* Deallocate TTY device. */
589 	tty_lock(tp);
590 	tty_rel_gone(tp);
591 
592 	return (0);
593 }
594 
595 static struct fileops ptsdev_ops = {
596 	.fo_read	= ptsdev_read,
597 	.fo_write	= ptsdev_write,
598 	.fo_truncate	= ptsdev_truncate,
599 	.fo_ioctl	= ptsdev_ioctl,
600 	.fo_poll	= ptsdev_poll,
601 	.fo_kqfilter	= ptsdev_kqfilter,
602 	.fo_stat	= ptsdev_stat,
603 	.fo_close	= ptsdev_close,
604 	.fo_flags	= DFLAG_PASSABLE,
605 };
606 
607 /*
608  * Driver-side hooks.
609  */
610 
611 static void
612 ptsdrv_outwakeup(struct tty *tp)
613 {
614 	struct pts_softc *psc = tty_softc(tp);
615 
616 	cv_broadcast(&psc->pts_outwait);
617 	selwakeup(&psc->pts_outpoll);
618 	KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0);
619 }
620 
621 static void
622 ptsdrv_inwakeup(struct tty *tp)
623 {
624 	struct pts_softc *psc = tty_softc(tp);
625 
626 	cv_broadcast(&psc->pts_inwait);
627 	selwakeup(&psc->pts_inpoll);
628 	KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0);
629 }
630 
631 static int
632 ptsdrv_open(struct tty *tp)
633 {
634 	struct pts_softc *psc = tty_softc(tp);
635 
636 	psc->pts_flags &= ~PTS_FINISHED;
637 
638 	return (0);
639 }
640 
641 static void
642 ptsdrv_close(struct tty *tp)
643 {
644 	struct pts_softc *psc = tty_softc(tp);
645 
646 	/* Wake up any blocked readers/writers. */
647 	psc->pts_flags |= PTS_FINISHED;
648 	ptsdrv_outwakeup(tp);
649 	ptsdrv_inwakeup(tp);
650 }
651 
652 static void
653 ptsdrv_pktnotify(struct tty *tp, char event)
654 {
655 	struct pts_softc *psc = tty_softc(tp);
656 
657 	/*
658 	 * Clear conflicting flags.
659 	 */
660 
661 	switch (event) {
662 	case TIOCPKT_STOP:
663 		psc->pts_pkt &= ~TIOCPKT_START;
664 		break;
665 	case TIOCPKT_START:
666 		psc->pts_pkt &= ~TIOCPKT_STOP;
667 		break;
668 	case TIOCPKT_NOSTOP:
669 		psc->pts_pkt &= ~TIOCPKT_DOSTOP;
670 		break;
671 	case TIOCPKT_DOSTOP:
672 		psc->pts_pkt &= ~TIOCPKT_NOSTOP;
673 		break;
674 	}
675 
676 	psc->pts_pkt |= event;
677 	ptsdrv_outwakeup(tp);
678 }
679 
680 static void
681 ptsdrv_free(void *softc)
682 {
683 	struct pts_softc *psc = softc;
684 
685 	/* Make device number available again. */
686 	if (psc->pts_unit >= 0)
687 		free_unr(pts_pool, psc->pts_unit);
688 
689 	chgptscnt(psc->pts_uidinfo, -1, 0);
690 	uifree(psc->pts_uidinfo);
691 
692 	knlist_destroy(&psc->pts_inpoll.si_note);
693 	knlist_destroy(&psc->pts_outpoll.si_note);
694 
695 #ifdef PTS_EXTERNAL
696 	/* Destroy master device as well. */
697 	if (psc->pts_cdev != NULL)
698 		destroy_dev_sched(psc->pts_cdev);
699 #endif /* PTS_EXTERNAL */
700 
701 	free(psc, M_PTS);
702 }
703 
704 static struct ttydevsw pts_class = {
705 	.tsw_flags	= TF_NOPREFIX,
706 	.tsw_outwakeup	= ptsdrv_outwakeup,
707 	.tsw_inwakeup	= ptsdrv_inwakeup,
708 	.tsw_open	= ptsdrv_open,
709 	.tsw_close	= ptsdrv_close,
710 	.tsw_pktnotify	= ptsdrv_pktnotify,
711 	.tsw_free	= ptsdrv_free,
712 };
713 
714 static int
715 pts_alloc(int fflags, struct thread *td, struct file *fp)
716 {
717 	int unit, ok;
718 	struct tty *tp;
719 	struct pts_softc *psc;
720 	struct proc *p = td->td_proc;
721 	struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
722 
723 	/* Resource limiting. */
724 	PROC_LOCK(p);
725 	ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
726 	PROC_UNLOCK(p);
727 	if (!ok)
728 		return (EAGAIN);
729 
730 	/* Try to allocate a new pts unit number. */
731 	unit = alloc_unr(pts_pool);
732 	if (unit < 0) {
733 		chgptscnt(uid, -1, 0);
734 		return (EAGAIN);
735 	}
736 	if (unit > pts_maxdev) {
737 		free_unr(pts_pool, unit);
738 		chgptscnt(uid, -1, 0);
739 		return (EAGAIN);
740 	}
741 
742 	/* Allocate TTY and softc. */
743 	psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
744 	cv_init(&psc->pts_inwait, "pts inwait");
745 	cv_init(&psc->pts_outwait, "pts outwait");
746 
747 	psc->pts_unit = unit;
748 	psc->pts_uidinfo = uid;
749 	uihold(uid);
750 
751 	tp = tty_alloc(&pts_class, psc);
752 	knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
753 	knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
754 
755 	/* Expose the slave device as well. */
756 	tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit);
757 
758 	finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
759 
760 	return (0);
761 }
762 
763 #ifdef PTS_EXTERNAL
764 int
765 pts_alloc_external(int fflags, struct thread *td, struct file *fp,
766     struct cdev *dev, const char *name)
767 {
768 	int ok;
769 	struct tty *tp;
770 	struct pts_softc *psc;
771 	struct proc *p = td->td_proc;
772 	struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
773 
774 	/* Resource limiting. */
775 	PROC_LOCK(p);
776 	ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
777 	PROC_UNLOCK(p);
778 	if (!ok)
779 		return (EAGAIN);
780 
781 	/* Allocate TTY and softc. */
782 	psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
783 	cv_init(&psc->pts_inwait, "pts inwait");
784 	cv_init(&psc->pts_outwait, "pts outwait");
785 
786 	psc->pts_unit = -1;
787 	psc->pts_cdev = dev;
788 	psc->pts_uidinfo = uid;
789 	uihold(uid);
790 
791 	tp = tty_alloc(&pts_class, psc);
792 	knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
793 	knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
794 
795 	/* Expose the slave device as well. */
796 	tty_makedev(tp, td->td_ucred, "%s", name);
797 
798 	finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
799 
800 	return (0);
801 }
802 #endif /* PTS_EXTERNAL */
803 
804 int
805 posix_openpt(struct thread *td, struct posix_openpt_args *uap)
806 {
807 	int error, fd;
808 	struct file *fp;
809 
810 	/*
811 	 * POSIX states it's unspecified when other flags are passed. We
812 	 * don't allow this.
813 	 */
814 	if (uap->flags & ~(O_RDWR|O_NOCTTY))
815 		return (EINVAL);
816 
817 	error = falloc(td, &fp, &fd);
818 	if (error)
819 		return (error);
820 
821 	/* Allocate the actual pseudo-TTY. */
822 	error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
823 	if (error != 0) {
824 		fdclose(td->td_proc->p_fd, fp, fd, td);
825 		return (error);
826 	}
827 
828 	/* Pass it back to userspace. */
829 	td->td_retval[0] = fd;
830 	fdrop(fp, td);
831 
832 	return (0);
833 }
834 
835 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
836 static int
837 ptmx_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp)
838 {
839 
840 	return (pts_alloc(fflags & (FREAD|FWRITE), td, fp));
841 }
842 
843 static struct cdevsw ptmx_cdevsw = {
844 	.d_version	= D_VERSION,
845 	.d_fdopen	= ptmx_fdopen,
846 	.d_name		= "ptmx",
847 };
848 #endif /* PTS_COMPAT || PTS_LINUX */
849 
850 static void
851 pts_init(void *unused)
852 {
853 
854 	pts_pool = new_unrhdr(0, INT_MAX, NULL);
855 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
856 	make_dev(&ptmx_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "ptmx");
857 #endif /* PTS_COMPAT || PTS_LINUX */
858 }
859 
860 SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);
861