xref: /freebsd/sys/kern/tty_pts.c (revision 13014ca04aad1931d41958b56f71a2c65b9a7a2c)
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/malloc.h>
54 #include <sys/poll.h>
55 #include <sys/proc.h>
56 #include <sys/resourcevar.h>
57 #include <sys/serial.h>
58 #include <sys/stat.h>
59 #include <sys/syscall.h>
60 #include <sys/syscallsubr.h>
61 #include <sys/sysent.h>
62 #include <sys/sysproto.h>
63 #include <sys/systm.h>
64 #include <sys/tty.h>
65 #include <sys/ttycom.h>
66 
67 #include <machine/stdarg.h>
68 
69 static struct unrhdr *pts_pool;
70 #define MAXPTSDEVS 999
71 
72 static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device");
73 
74 /*
75  * Per-PTS structure.
76  *
77  * List of locks
78  * (t)	locked by tty_lock()
79  * (c)	const until freeing
80  */
81 struct pts_softc {
82 	int		pts_unit;	/* (c) Device unit number. */
83 	unsigned int	pts_flags;	/* (t) Device flags. */
84 #define	PTS_PKT		0x1	/* Packet mode. */
85 #define	PTS_FINISHED	0x2	/* Return errors on read()/write(). */
86 	char		pts_pkt;	/* (t) Unread packet mode data. */
87 
88 	struct cv	pts_inwait;	/* (t) Blocking write() on master. */
89 	struct selinfo	pts_inpoll;	/* (t) Select queue for write(). */
90 	struct cv	pts_outwait;	/* (t) Blocking read() on master. */
91 	struct selinfo	pts_outpoll;	/* (t) Select queue for read(). */
92 
93 #ifdef PTS_EXTERNAL
94 	struct cdev	*pts_cdev;	/* (c) Master device node. */
95 #endif /* PTS_EXTERNAL */
96 
97 	struct uidinfo	*pts_uidinfo;	/* (c) Resource limit. */
98 };
99 
100 /*
101  * Controller-side file operations.
102  */
103 
104 static int
105 ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
106     int flags, struct thread *td)
107 {
108 	struct tty *tp = fp->f_data;
109 	struct pts_softc *psc = tty_softc(tp);
110 	int error = 0;
111 	char pkt;
112 
113 	if (uio->uio_resid == 0)
114 		return (0);
115 
116 	tty_lock(tp);
117 
118 	for (;;) {
119 		/*
120 		 * Implement packet mode. When packet mode is turned on,
121 		 * the first byte contains a bitmask of events that
122 		 * occured (start, stop, flush, window size, etc).
123 		 */
124 		if (psc->pts_flags & PTS_PKT && psc->pts_pkt) {
125 			pkt = psc->pts_pkt;
126 			psc->pts_pkt = 0;
127 			tty_unlock(tp);
128 
129 			error = ureadc(pkt, uio);
130 			return (error);
131 		}
132 
133 		/*
134 		 * Transmit regular data.
135 		 *
136 		 * XXX: We shouldn't use ttydisc_getc_poll()! Even
137 		 * though in this implementation, there is likely going
138 		 * to be data, we should just call ttydisc_getc_uio()
139 		 * and use its return value to sleep.
140 		 */
141 		if (ttydisc_getc_poll(tp)) {
142 			if (psc->pts_flags & PTS_PKT) {
143 				/*
144 				 * XXX: Small race. Fortunately PTY
145 				 * consumers aren't multithreaded.
146 				 */
147 
148 				tty_unlock(tp);
149 				error = ureadc(TIOCPKT_DATA, uio);
150 				if (error)
151 					return (error);
152 				tty_lock(tp);
153 			}
154 
155 			error = ttydisc_getc_uio(tp, uio);
156 			break;
157 		}
158 
159 		/* Maybe the device isn't used anyway. */
160 		if (psc->pts_flags & PTS_FINISHED)
161 			break;
162 
163 		/* Wait for more data. */
164 		if (fp->f_flag & O_NONBLOCK) {
165 			error = EWOULDBLOCK;
166 			break;
167 		}
168 		error = cv_wait_sig(&psc->pts_outwait, tp->t_mtx);
169 		if (error != 0)
170 			break;
171 	}
172 
173 	tty_unlock(tp);
174 
175 	return (error);
176 }
177 
178 static int
179 ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
180     int flags, struct thread *td)
181 {
182 	struct tty *tp = fp->f_data;
183 	struct pts_softc *psc = tty_softc(tp);
184 	char ib[256], *ibstart;
185 	size_t iblen, rintlen;
186 	int error = 0;
187 
188 	if (uio->uio_resid == 0)
189 		return (0);
190 
191 	for (;;) {
192 		ibstart = ib;
193 		iblen = MIN(uio->uio_resid, sizeof ib);
194 		error = uiomove(ib, iblen, uio);
195 
196 		tty_lock(tp);
197 		if (error != 0)
198 			goto done;
199 
200 		/*
201 		 * When possible, avoid the slow path. rint_bypass()
202 		 * copies all input to the input queue at once.
203 		 */
204 		MPASS(iblen > 0);
205 		do {
206 			if (ttydisc_can_bypass(tp)) {
207 				/* Store data at once. */
208 				rintlen = ttydisc_rint_bypass(tp,
209 				    ibstart, iblen);
210 				ibstart += rintlen;
211 				iblen -= rintlen;
212 
213 				if (iblen == 0) {
214 					/* All data written. */
215 					break;
216 				}
217 			} else {
218 				error = ttydisc_rint(tp, *ibstart, 0);
219 				if (error == 0) {
220 					/* Character stored successfully. */
221 					ibstart++;
222 					iblen--;
223 					continue;
224 				}
225 			}
226 
227 			/* Maybe the device isn't used anyway. */
228 			if (psc->pts_flags & PTS_FINISHED) {
229 				error = EIO;
230 				goto done;
231 			}
232 
233 			/* Wait for more data. */
234 			if (fp->f_flag & O_NONBLOCK) {
235 				error = EWOULDBLOCK;
236 				goto done;
237 			}
238 
239 			/* Wake up users on the slave side. */
240 			ttydisc_rint_done(tp);
241 			error = cv_wait_sig(&psc->pts_inwait, tp->t_mtx);
242 			if (error != 0)
243 				goto done;
244 		} while (iblen > 0);
245 
246 		if (uio->uio_resid == 0)
247 			break;
248 		tty_unlock(tp);
249 	}
250 
251 done:	ttydisc_rint_done(tp);
252 	tty_unlock(tp);
253 	return (error);
254 }
255 
256 static int
257 ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
258     struct ucred *active_cred, struct thread *td)
259 {
260 	struct tty *tp = fp->f_data;
261 	struct pts_softc *psc = tty_softc(tp);
262 	int error = 0, sig;
263 
264 	switch (cmd) {
265 	case FIONBIO:
266 		/* This device supports non-blocking operation. */
267 		return (0);
268 	case FIODGNAME: {
269 		struct fiodgname_arg *fgn;
270 		const char *p;
271 		int i;
272 
273 		/* Reverse device name lookups, for ptsname() and ttyname(). */
274 		fgn = data;
275 #ifdef PTS_EXTERNAL
276 		if (psc->pts_cdev != NULL)
277 			p = devtoname(psc->pts_cdev);
278 		else
279 #endif /* PTS_EXTERNAL */
280 			p = tty_devname(tp);
281 		i = strlen(p) + 1;
282 		if (i > fgn->len)
283 			return (EINVAL);
284 		return copyout(p, fgn->buf, i);
285 	}
286 
287 	/*
288 	 * We need to implement TIOCGPGRP and TIOCGSID here again. When
289 	 * called on the pseudo-terminal master, it should not check if
290 	 * the terminal is the foreground terminal of the calling
291 	 * process.
292 	 *
293 	 * TIOCGETA is also implemented here. Various Linux PTY routines
294 	 * often call isatty(), which is implemented by tcgetattr().
295 	 */
296 #ifdef PTS_LINUX
297 	case TIOCGETA:
298 		/* Obtain terminal flags through tcgetattr(). */
299 		tty_lock(tp);
300 		bcopy(&tp->t_termios, data, sizeof(struct termios));
301 		tty_unlock(tp);
302 		return (0);
303 #endif /* PTS_LINUX */
304 	case TIOCSETAF:
305 	case TIOCSETAW:
306 		/*
307 		 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and
308 		 * TCSADRAIN into something different. If an application would
309 		 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may
310 		 * deadlock waiting for all data to be read.
311 		 */
312 		cmd = TIOCSETA;
313 		break;
314 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
315 	case TIOCGPTN:
316 		/*
317 		 * Get the device unit number.
318 		 */
319 		if (psc->pts_unit < 0)
320 			return (ENOTTY);
321 		*(unsigned int *)data = psc->pts_unit;
322 		return (0);
323 #endif /* PTS_COMPAT || PTS_LINUX */
324 	case TIOCGPGRP:
325 		/* Get the foreground process group ID. */
326 		tty_lock(tp);
327 		if (tp->t_pgrp != NULL)
328 			*(int *)data = tp->t_pgrp->pg_id;
329 		else
330 			*(int *)data = NO_PID;
331 		tty_unlock(tp);
332 		return (0);
333 	case TIOCGSID:
334 		/* Get the session leader process ID. */
335 		tty_lock(tp);
336 		if (tp->t_session == NULL)
337 			error = ENOTTY;
338 		else
339 			*(int *)data = tp->t_session->s_sid;
340 		tty_unlock(tp);
341 		return (error);
342 	case TIOCPTMASTER:
343 		/* Yes, we are a pseudo-terminal master. */
344 		return (0);
345 	case TIOCSIG:
346 		/* Signal the foreground process group. */
347 		sig = *(int *)data;
348 		if (sig < 1 || sig >= NSIG)
349 			return (EINVAL);
350 
351 		tty_lock(tp);
352 		tty_signal_pgrp(tp, sig);
353 		tty_unlock(tp);
354 		return (0);
355 	case TIOCPKT:
356 		/* Enable/disable packet mode. */
357 		tty_lock(tp);
358 		if (*(int *)data)
359 			psc->pts_flags |= PTS_PKT;
360 		else
361 			psc->pts_flags &= ~PTS_PKT;
362 		tty_unlock(tp);
363 		return (0);
364 	}
365 
366 	/* Just redirect this ioctl to the slave device. */
367 	tty_lock(tp);
368 	error = tty_ioctl(tp, cmd, data, td);
369 	tty_unlock(tp);
370 
371 	return (error);
372 }
373 
374 static int
375 ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
376     struct thread *td)
377 {
378 	struct tty *tp = fp->f_data;
379 	struct pts_softc *psc = tty_softc(tp);
380 	int revents = 0;
381 
382 	tty_lock(tp);
383 
384 	if (psc->pts_flags & PTS_FINISHED) {
385 		/* Slave device is not opened. */
386 		tty_unlock(tp);
387 		return (events &
388 		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
389 	}
390 
391 	if (events & (POLLIN|POLLRDNORM)) {
392 		/* See if we can getc something. */
393 		if (ttydisc_getc_poll(tp) ||
394 		    (psc->pts_flags & PTS_PKT && psc->pts_pkt))
395 			revents |= events & (POLLIN|POLLRDNORM);
396 	}
397 	if (events & (POLLOUT|POLLWRNORM)) {
398 		/* See if we can rint something. */
399 		if (ttydisc_rint_poll(tp))
400 			revents |= events & (POLLOUT|POLLWRNORM);
401 	}
402 
403 	/*
404 	 * No need to check for POLLHUP here. This device cannot be used
405 	 * as a callout device, which means we always have a carrier,
406 	 * because the master is.
407 	 */
408 
409 	if (revents == 0) {
410 		/*
411 		 * This code might look misleading, but the naming of
412 		 * poll events on this side is the opposite of the slave
413 		 * device.
414 		 */
415 		if (events & (POLLIN|POLLRDNORM))
416 			selrecord(td, &psc->pts_outpoll);
417 		if (events & (POLLOUT|POLLWRNORM))
418 			selrecord(td, &psc->pts_inpoll);
419 	}
420 
421 	tty_unlock(tp);
422 
423 	return (revents);
424 }
425 
426 static int
427 ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
428     struct thread *td)
429 {
430 	struct tty *tp = fp->f_data;
431 #ifdef PTS_EXTERNAL
432 	struct pts_softc *psc = tty_softc(tp);
433 #endif /* PTS_EXTERNAL */
434 	struct cdev *dev = tp->t_dev;
435 
436 	/*
437 	 * According to POSIX, we must implement an fstat(). This also
438 	 * makes this implementation compatible with Linux binaries,
439 	 * because Linux calls fstat() on the pseudo-terminal master to
440 	 * obtain st_rdev.
441 	 *
442 	 * XXX: POSIX also mentions we must fill in st_dev, but how?
443 	 */
444 
445 	bzero(sb, sizeof *sb);
446 #ifdef PTS_EXTERNAL
447 	if (psc->pts_cdev != NULL)
448 		sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
449 	else
450 #endif /* PTS_EXTERNAL */
451 		sb->st_ino = sb->st_rdev = tty_udev(tp);
452 
453 	sb->st_atimespec = dev->si_atime;
454 	sb->st_ctimespec = dev->si_ctime;
455 	sb->st_mtimespec = dev->si_mtime;
456 	sb->st_uid = dev->si_uid;
457 	sb->st_gid = dev->si_gid;
458 	sb->st_mode = dev->si_mode | S_IFCHR;
459 
460 	return (0);
461 }
462 
463 static int
464 ptsdev_close(struct file *fp, struct thread *td)
465 {
466 	struct tty *tp = fp->f_data;
467 
468 	/* Deallocate TTY device. */
469 	tty_lock(tp);
470 	tty_rel_gone(tp);
471 
472 	return (0);
473 }
474 
475 static struct fileops ptsdev_ops = {
476 	.fo_read	= ptsdev_read,
477 	.fo_write	= ptsdev_write,
478 	.fo_ioctl	= ptsdev_ioctl,
479 	.fo_poll	= ptsdev_poll,
480 	.fo_stat	= ptsdev_stat,
481 	.fo_close	= ptsdev_close,
482 	.fo_flags	= DFLAG_PASSABLE,
483 };
484 
485 /*
486  * Driver-side hooks.
487  */
488 
489 static void
490 ptsdrv_outwakeup(struct tty *tp)
491 {
492 	struct pts_softc *psc = tty_softc(tp);
493 
494 	cv_broadcast(&psc->pts_outwait);
495 	selwakeup(&psc->pts_outpoll);
496 }
497 
498 static void
499 ptsdrv_inwakeup(struct tty *tp)
500 {
501 	struct pts_softc *psc = tty_softc(tp);
502 
503 	cv_broadcast(&psc->pts_inwait);
504 	selwakeup(&psc->pts_inpoll);
505 }
506 
507 static int
508 ptsdrv_open(struct tty *tp)
509 {
510 	struct pts_softc *psc = tty_softc(tp);
511 
512 	psc->pts_flags &= ~PTS_FINISHED;
513 
514 	return (0);
515 }
516 
517 static void
518 ptsdrv_close(struct tty *tp)
519 {
520 	struct pts_softc *psc = tty_softc(tp);
521 
522 	/* Wake up any blocked readers/writers. */
523 	ptsdrv_outwakeup(tp);
524 	ptsdrv_inwakeup(tp);
525 
526 	psc->pts_flags |= PTS_FINISHED;
527 }
528 
529 static void
530 ptsdrv_pktnotify(struct tty *tp, char event)
531 {
532 	struct pts_softc *psc = tty_softc(tp);
533 
534 	/*
535 	 * Clear conflicting flags.
536 	 */
537 
538 	switch (event) {
539 	case TIOCPKT_STOP:
540 		psc->pts_pkt &= ~TIOCPKT_START;
541 		break;
542 	case TIOCPKT_START:
543 		psc->pts_pkt &= ~TIOCPKT_STOP;
544 		break;
545 	case TIOCPKT_NOSTOP:
546 		psc->pts_pkt &= ~TIOCPKT_DOSTOP;
547 		break;
548 	case TIOCPKT_DOSTOP:
549 		psc->pts_pkt &= ~TIOCPKT_NOSTOP;
550 		break;
551 	}
552 
553 	psc->pts_pkt |= event;
554 	ptsdrv_outwakeup(tp);
555 }
556 
557 static void
558 ptsdrv_free(void *softc)
559 {
560 	struct pts_softc *psc = softc;
561 
562 	/* Make device number available again. */
563 	if (psc->pts_unit >= 0)
564 		free_unr(pts_pool, psc->pts_unit);
565 
566 	chgptscnt(psc->pts_uidinfo, -1, 0);
567 	uifree(psc->pts_uidinfo);
568 
569 #ifdef PTS_EXTERNAL
570 	/* Destroy master device as well. */
571 	if (psc->pts_cdev != NULL)
572 		destroy_dev_sched(psc->pts_cdev);
573 #endif /* PTS_EXTERNAL */
574 
575 	free(psc, M_PTS);
576 }
577 
578 static struct ttydevsw pts_class = {
579 	.tsw_flags	= TF_NOPREFIX,
580 	.tsw_outwakeup	= ptsdrv_outwakeup,
581 	.tsw_inwakeup	= ptsdrv_inwakeup,
582 	.tsw_open	= ptsdrv_open,
583 	.tsw_close	= ptsdrv_close,
584 	.tsw_pktnotify	= ptsdrv_pktnotify,
585 	.tsw_free	= ptsdrv_free,
586 };
587 
588 static int
589 pts_alloc(int fflags, struct thread *td, struct file *fp)
590 {
591 	int unit, ok;
592 	struct tty *tp;
593 	struct pts_softc *psc;
594 	struct proc *p = td->td_proc;
595 	struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
596 
597 	/* Resource limiting. */
598 	PROC_LOCK(p);
599 	ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
600 	PROC_UNLOCK(p);
601 	if (!ok)
602 		return (EAGAIN);
603 
604 	/* Try to allocate a new pts unit number. */
605 	unit = alloc_unr(pts_pool);
606 	if (unit < 0) {
607 		chgptscnt(uid, -1, 0);
608 		return (EAGAIN);
609 	}
610 
611 	/* Allocate TTY and softc. */
612 	psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
613 	cv_init(&psc->pts_inwait, "pts inwait");
614 	cv_init(&psc->pts_outwait, "pts outwait");
615 
616 	psc->pts_unit = unit;
617 	psc->pts_uidinfo = uid;
618 	uihold(uid);
619 
620 	tp = tty_alloc(&pts_class, psc, NULL);
621 
622 	/* Expose the slave device as well. */
623 	tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit);
624 
625 	finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
626 
627 	return (0);
628 }
629 
630 #ifdef PTS_EXTERNAL
631 int
632 pts_alloc_external(int fflags, struct thread *td, struct file *fp,
633     struct cdev *dev, const char *name)
634 {
635 	int ok;
636 	struct tty *tp;
637 	struct pts_softc *psc;
638 	struct proc *p = td->td_proc;
639 	struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
640 
641 	/* Resource limiting. */
642 	PROC_LOCK(p);
643 	ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
644 	PROC_UNLOCK(p);
645 	if (!ok)
646 		return (EAGAIN);
647 
648 	/* Allocate TTY and softc. */
649 	psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
650 	cv_init(&psc->pts_inwait, "pts inwait");
651 	cv_init(&psc->pts_outwait, "pts outwait");
652 
653 	psc->pts_unit = -1;
654 	psc->pts_cdev = dev;
655 	psc->pts_uidinfo = uid;
656 	uihold(uid);
657 
658 	tp = tty_alloc(&pts_class, psc, NULL);
659 
660 	/* Expose the slave device as well. */
661 	tty_makedev(tp, td->td_ucred, "%s", name);
662 
663 	finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
664 
665 	return (0);
666 }
667 #endif /* PTS_EXTERNAL */
668 
669 int
670 posix_openpt(struct thread *td, struct posix_openpt_args *uap)
671 {
672 	int error, fd;
673 	struct file *fp;
674 
675 	/*
676 	 * POSIX states it's unspecified when other flags are passed. We
677 	 * don't allow this.
678 	 */
679 	if (uap->flags & ~(O_RDWR|O_NOCTTY))
680 		return (EINVAL);
681 
682 	error = falloc(td, &fp, &fd);
683 	if (error)
684 		return (error);
685 
686 	/* Allocate the actual pseudo-TTY. */
687 	error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
688 	if (error != 0) {
689 		fdclose(td->td_proc->p_fd, fp, fd, td);
690 		return (error);
691 	}
692 
693 	/* Pass it back to userspace. */
694 	td->td_retval[0] = fd;
695 	fdrop(fp, td);
696 
697 	return (0);
698 }
699 
700 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
701 static int
702 ptmx_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp)
703 {
704 	int error;
705 
706 	error = pts_alloc(fflags & (FREAD|FWRITE), td, fp);
707 	if (error != 0)
708 		return (error);
709 
710 	return (0);
711 }
712 
713 static struct cdevsw ptmx_cdevsw = {
714 	.d_version	= D_VERSION,
715 	.d_fdopen	= ptmx_fdopen,
716 	.d_name		= "ptmx",
717 };
718 #endif /* PTS_COMPAT || PTS_LINUX */
719 
720 static void
721 pts_init(void *unused)
722 {
723 
724 	pts_pool = new_unrhdr(0, MAXPTSDEVS, NULL);
725 #if defined(PTS_COMPAT) || defined(PTS_LINUX)
726 	make_dev(&ptmx_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "ptmx");
727 #endif /* PTS_COMPAT || PTS_LINUX */
728 }
729 
730 SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);
731