tty_pts.c revision 186030
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: head/sys/kern/tty_pts.c 186030 2008-12-13 07:23:55Z ed $");
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
69static struct unrhdr *pts_pool;
70#define MAXPTSDEVS 999
71
72static 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 */
81struct 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
104static int
105ptsdev_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
178static int
179ptsdev_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
251done:	ttydisc_rint_done(tp);
252	tty_unlock(tp);
253	return (error);
254}
255
256static int
257ptsdev_truncate(struct file *fp, off_t length, struct ucred *active_cred,
258    struct thread *td)
259{
260
261	return (EINVAL);
262}
263
264static int
265ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
266    struct ucred *active_cred, struct thread *td)
267{
268	struct tty *tp = fp->f_data;
269	struct pts_softc *psc = tty_softc(tp);
270	int error = 0, sig;
271
272	switch (cmd) {
273	case FIONBIO:
274		/* This device supports non-blocking operation. */
275		return (0);
276	case FIONREAD:
277		tty_lock(tp);
278		if (psc->pts_flags & PTS_FINISHED) {
279			/* Force read() to be called. */
280			*(int *)data = 1;
281		} else {
282			*(int *)data = ttydisc_getc_poll(tp);
283		}
284		tty_unlock(tp);
285		return (0);
286	case FIODGNAME: {
287		struct fiodgname_arg *fgn;
288		const char *p;
289		int i;
290
291		/* Reverse device name lookups, for ptsname() and ttyname(). */
292		fgn = data;
293#ifdef PTS_EXTERNAL
294		if (psc->pts_cdev != NULL)
295			p = devtoname(psc->pts_cdev);
296		else
297#endif /* PTS_EXTERNAL */
298			p = tty_devname(tp);
299		i = strlen(p) + 1;
300		if (i > fgn->len)
301			return (EINVAL);
302		return copyout(p, fgn->buf, i);
303	}
304
305	/*
306	 * We need to implement TIOCGPGRP and TIOCGSID here again. When
307	 * called on the pseudo-terminal master, it should not check if
308	 * the terminal is the foreground terminal of the calling
309	 * process.
310	 *
311	 * TIOCGETA is also implemented here. Various Linux PTY routines
312	 * often call isatty(), which is implemented by tcgetattr().
313	 */
314#ifdef PTS_LINUX
315	case TIOCGETA:
316		/* Obtain terminal flags through tcgetattr(). */
317		tty_lock(tp);
318		bcopy(&tp->t_termios, data, sizeof(struct termios));
319		tty_unlock(tp);
320		return (0);
321#endif /* PTS_LINUX */
322	case TIOCSETAF:
323	case TIOCSETAW:
324		/*
325		 * We must make sure we turn tcsetattr() calls of TCSAFLUSH and
326		 * TCSADRAIN into something different. If an application would
327		 * call TCSAFLUSH or TCSADRAIN on the master descriptor, it may
328		 * deadlock waiting for all data to be read.
329		 */
330		cmd = TIOCSETA;
331		break;
332#if defined(PTS_COMPAT) || defined(PTS_LINUX)
333	case TIOCGPTN:
334		/*
335		 * Get the device unit number.
336		 */
337		if (psc->pts_unit < 0)
338			return (ENOTTY);
339		*(unsigned int *)data = psc->pts_unit;
340		return (0);
341#endif /* PTS_COMPAT || PTS_LINUX */
342	case TIOCGPGRP:
343		/* Get the foreground process group ID. */
344		tty_lock(tp);
345		if (tp->t_pgrp != NULL)
346			*(int *)data = tp->t_pgrp->pg_id;
347		else
348			*(int *)data = NO_PID;
349		tty_unlock(tp);
350		return (0);
351	case TIOCGSID:
352		/* Get the session leader process ID. */
353		tty_lock(tp);
354		if (tp->t_session == NULL)
355			error = ENOTTY;
356		else
357			*(int *)data = tp->t_session->s_sid;
358		tty_unlock(tp);
359		return (error);
360	case TIOCPTMASTER:
361		/* Yes, we are a pseudo-terminal master. */
362		return (0);
363	case TIOCSIG:
364		/* Signal the foreground process group. */
365		sig = *(int *)data;
366		if (sig < 1 || sig >= NSIG)
367			return (EINVAL);
368
369		tty_lock(tp);
370		tty_signal_pgrp(tp, sig);
371		tty_unlock(tp);
372		return (0);
373	case TIOCPKT:
374		/* Enable/disable packet mode. */
375		tty_lock(tp);
376		if (*(int *)data)
377			psc->pts_flags |= PTS_PKT;
378		else
379			psc->pts_flags &= ~PTS_PKT;
380		tty_unlock(tp);
381		return (0);
382	}
383
384	/* Just redirect this ioctl to the slave device. */
385	tty_lock(tp);
386	error = tty_ioctl(tp, cmd, data, td);
387	tty_unlock(tp);
388
389	return (error);
390}
391
392static int
393ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
394    struct thread *td)
395{
396	struct tty *tp = fp->f_data;
397	struct pts_softc *psc = tty_softc(tp);
398	int revents = 0;
399
400	tty_lock(tp);
401
402	if (psc->pts_flags & PTS_FINISHED) {
403		/* Slave device is not opened. */
404		tty_unlock(tp);
405		return (events &
406		    (POLLHUP|POLLIN|POLLRDNORM|POLLOUT|POLLWRNORM));
407	}
408
409	if (events & (POLLIN|POLLRDNORM)) {
410		/* See if we can getc something. */
411		if (ttydisc_getc_poll(tp) ||
412		    (psc->pts_flags & PTS_PKT && psc->pts_pkt))
413			revents |= events & (POLLIN|POLLRDNORM);
414	}
415	if (events & (POLLOUT|POLLWRNORM)) {
416		/* See if we can rint something. */
417		if (ttydisc_rint_poll(tp))
418			revents |= events & (POLLOUT|POLLWRNORM);
419	}
420
421	/*
422	 * No need to check for POLLHUP here. This device cannot be used
423	 * as a callout device, which means we always have a carrier,
424	 * because the master is.
425	 */
426
427	if (revents == 0) {
428		/*
429		 * This code might look misleading, but the naming of
430		 * poll events on this side is the opposite of the slave
431		 * device.
432		 */
433		if (events & (POLLIN|POLLRDNORM))
434			selrecord(td, &psc->pts_outpoll);
435		if (events & (POLLOUT|POLLWRNORM))
436			selrecord(td, &psc->pts_inpoll);
437	}
438
439	tty_unlock(tp);
440
441	return (revents);
442}
443
444/*
445 * kqueue support.
446 */
447
448static void
449pts_kqops_read_detach(struct knote *kn)
450{
451	struct file *fp = kn->kn_fp;
452	struct tty *tp = fp->f_data;
453	struct pts_softc *psc = tty_softc(tp);
454
455	knlist_remove(&psc->pts_outpoll.si_note, kn, 0);
456}
457
458static int
459pts_kqops_read_event(struct knote *kn, long hint)
460{
461	struct file *fp = kn->kn_fp;
462	struct tty *tp = fp->f_data;
463	struct pts_softc *psc = tty_softc(tp);
464
465	if (psc->pts_flags & PTS_FINISHED) {
466		kn->kn_flags |= EV_EOF;
467		return (1);
468	} else {
469		kn->kn_data = ttydisc_getc_poll(tp);
470		return (kn->kn_data > 0);
471	}
472}
473
474static void
475pts_kqops_write_detach(struct knote *kn)
476{
477	struct file *fp = kn->kn_fp;
478	struct tty *tp = fp->f_data;
479	struct pts_softc *psc = tty_softc(tp);
480
481	knlist_remove(&psc->pts_inpoll.si_note, kn, 0);
482}
483
484static int
485pts_kqops_write_event(struct knote *kn, long hint)
486{
487	struct file *fp = kn->kn_fp;
488	struct tty *tp = fp->f_data;
489	struct pts_softc *psc = tty_softc(tp);
490
491	if (psc->pts_flags & PTS_FINISHED) {
492		kn->kn_flags |= EV_EOF;
493		return (1);
494	} else {
495		kn->kn_data = ttydisc_rint_poll(tp);
496		return (kn->kn_data > 0);
497	}
498}
499
500static struct filterops pts_kqops_read =
501    { 1, NULL, pts_kqops_read_detach, pts_kqops_read_event };
502static struct filterops pts_kqops_write =
503    { 1, NULL, pts_kqops_write_detach, pts_kqops_write_event };
504
505static int
506ptsdev_kqfilter(struct file *fp, struct knote *kn)
507{
508	struct tty *tp = fp->f_data;
509	struct pts_softc *psc = tty_softc(tp);
510	int error = 0;
511
512	tty_lock(tp);
513
514	switch (kn->kn_filter) {
515	case EVFILT_READ:
516		kn->kn_fop = &pts_kqops_read;
517		knlist_add(&psc->pts_outpoll.si_note, kn, 1);
518		break;
519	case EVFILT_WRITE:
520		kn->kn_fop = &pts_kqops_write;
521		knlist_add(&psc->pts_inpoll.si_note, kn, 1);
522		break;
523	default:
524		error = EINVAL;
525		break;
526	}
527
528	tty_unlock(tp);
529	return (error);
530}
531
532static int
533ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
534    struct thread *td)
535{
536	struct tty *tp = fp->f_data;
537#ifdef PTS_EXTERNAL
538	struct pts_softc *psc = tty_softc(tp);
539#endif /* PTS_EXTERNAL */
540	struct cdev *dev = tp->t_dev;
541
542	/*
543	 * According to POSIX, we must implement an fstat(). This also
544	 * makes this implementation compatible with Linux binaries,
545	 * because Linux calls fstat() on the pseudo-terminal master to
546	 * obtain st_rdev.
547	 *
548	 * XXX: POSIX also mentions we must fill in st_dev, but how?
549	 */
550
551	bzero(sb, sizeof *sb);
552#ifdef PTS_EXTERNAL
553	if (psc->pts_cdev != NULL)
554		sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
555	else
556#endif /* PTS_EXTERNAL */
557		sb->st_ino = sb->st_rdev = tty_udev(tp);
558
559	sb->st_atimespec = dev->si_atime;
560	sb->st_ctimespec = dev->si_ctime;
561	sb->st_mtimespec = dev->si_mtime;
562	sb->st_uid = dev->si_uid;
563	sb->st_gid = dev->si_gid;
564	sb->st_mode = dev->si_mode | S_IFCHR;
565
566	return (0);
567}
568
569static int
570ptsdev_close(struct file *fp, struct thread *td)
571{
572	struct tty *tp = fp->f_data;
573
574	/* Deallocate TTY device. */
575	tty_lock(tp);
576	tty_rel_gone(tp);
577
578	return (0);
579}
580
581static struct fileops ptsdev_ops = {
582	.fo_read	= ptsdev_read,
583	.fo_write	= ptsdev_write,
584	.fo_truncate	= ptsdev_truncate,
585	.fo_ioctl	= ptsdev_ioctl,
586	.fo_poll	= ptsdev_poll,
587	.fo_kqfilter	= ptsdev_kqfilter,
588	.fo_stat	= ptsdev_stat,
589	.fo_close	= ptsdev_close,
590	.fo_flags	= DFLAG_PASSABLE,
591};
592
593/*
594 * Driver-side hooks.
595 */
596
597static void
598ptsdrv_outwakeup(struct tty *tp)
599{
600	struct pts_softc *psc = tty_softc(tp);
601
602	cv_broadcast(&psc->pts_outwait);
603	selwakeup(&psc->pts_outpoll);
604	KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0);
605}
606
607static void
608ptsdrv_inwakeup(struct tty *tp)
609{
610	struct pts_softc *psc = tty_softc(tp);
611
612	cv_broadcast(&psc->pts_inwait);
613	selwakeup(&psc->pts_inpoll);
614	KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0);
615}
616
617static int
618ptsdrv_open(struct tty *tp)
619{
620	struct pts_softc *psc = tty_softc(tp);
621
622	psc->pts_flags &= ~PTS_FINISHED;
623
624	return (0);
625}
626
627static void
628ptsdrv_close(struct tty *tp)
629{
630	struct pts_softc *psc = tty_softc(tp);
631
632	/* Wake up any blocked readers/writers. */
633	ptsdrv_outwakeup(tp);
634	ptsdrv_inwakeup(tp);
635
636	psc->pts_flags |= PTS_FINISHED;
637}
638
639static void
640ptsdrv_pktnotify(struct tty *tp, char event)
641{
642	struct pts_softc *psc = tty_softc(tp);
643
644	/*
645	 * Clear conflicting flags.
646	 */
647
648	switch (event) {
649	case TIOCPKT_STOP:
650		psc->pts_pkt &= ~TIOCPKT_START;
651		break;
652	case TIOCPKT_START:
653		psc->pts_pkt &= ~TIOCPKT_STOP;
654		break;
655	case TIOCPKT_NOSTOP:
656		psc->pts_pkt &= ~TIOCPKT_DOSTOP;
657		break;
658	case TIOCPKT_DOSTOP:
659		psc->pts_pkt &= ~TIOCPKT_NOSTOP;
660		break;
661	}
662
663	psc->pts_pkt |= event;
664	ptsdrv_outwakeup(tp);
665}
666
667static void
668ptsdrv_free(void *softc)
669{
670	struct pts_softc *psc = softc;
671
672	/* Make device number available again. */
673	if (psc->pts_unit >= 0)
674		free_unr(pts_pool, psc->pts_unit);
675
676	chgptscnt(psc->pts_uidinfo, -1, 0);
677	uifree(psc->pts_uidinfo);
678
679	knlist_destroy(&psc->pts_inpoll.si_note);
680	knlist_destroy(&psc->pts_outpoll.si_note);
681
682#ifdef PTS_EXTERNAL
683	/* Destroy master device as well. */
684	if (psc->pts_cdev != NULL)
685		destroy_dev_sched(psc->pts_cdev);
686#endif /* PTS_EXTERNAL */
687
688	free(psc, M_PTS);
689}
690
691static struct ttydevsw pts_class = {
692	.tsw_flags	= TF_NOPREFIX,
693	.tsw_outwakeup	= ptsdrv_outwakeup,
694	.tsw_inwakeup	= ptsdrv_inwakeup,
695	.tsw_open	= ptsdrv_open,
696	.tsw_close	= ptsdrv_close,
697	.tsw_pktnotify	= ptsdrv_pktnotify,
698	.tsw_free	= ptsdrv_free,
699};
700
701static int
702pts_alloc(int fflags, struct thread *td, struct file *fp)
703{
704	int unit, ok;
705	struct tty *tp;
706	struct pts_softc *psc;
707	struct proc *p = td->td_proc;
708	struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
709
710	/* Resource limiting. */
711	PROC_LOCK(p);
712	ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
713	PROC_UNLOCK(p);
714	if (!ok)
715		return (EAGAIN);
716
717	/* Try to allocate a new pts unit number. */
718	unit = alloc_unr(pts_pool);
719	if (unit < 0) {
720		chgptscnt(uid, -1, 0);
721		return (EAGAIN);
722	}
723
724	/* Allocate TTY and softc. */
725	psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
726	cv_init(&psc->pts_inwait, "pts inwait");
727	cv_init(&psc->pts_outwait, "pts outwait");
728
729	psc->pts_unit = unit;
730	psc->pts_uidinfo = uid;
731	uihold(uid);
732
733	tp = tty_alloc(&pts_class, psc, NULL);
734	knlist_init(&psc->pts_inpoll.si_note, tp->t_mtx, NULL, NULL, NULL);
735	knlist_init(&psc->pts_outpoll.si_note, tp->t_mtx, NULL, NULL, NULL);
736
737	/* Expose the slave device as well. */
738	tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit);
739
740	finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
741
742	return (0);
743}
744
745#ifdef PTS_EXTERNAL
746int
747pts_alloc_external(int fflags, struct thread *td, struct file *fp,
748    struct cdev *dev, const char *name)
749{
750	int ok;
751	struct tty *tp;
752	struct pts_softc *psc;
753	struct proc *p = td->td_proc;
754	struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
755
756	/* Resource limiting. */
757	PROC_LOCK(p);
758	ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
759	PROC_UNLOCK(p);
760	if (!ok)
761		return (EAGAIN);
762
763	/* Allocate TTY and softc. */
764	psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
765	cv_init(&psc->pts_inwait, "pts inwait");
766	cv_init(&psc->pts_outwait, "pts outwait");
767
768	psc->pts_unit = -1;
769	psc->pts_cdev = dev;
770	psc->pts_uidinfo = uid;
771	uihold(uid);
772
773	tp = tty_alloc(&pts_class, psc, NULL);
774	knlist_init(&psc->pts_inpoll.si_note, tp->t_mtx, NULL, NULL, NULL);
775	knlist_init(&psc->pts_outpoll.si_note, tp->t_mtx, NULL, NULL, NULL);
776
777	/* Expose the slave device as well. */
778	tty_makedev(tp, td->td_ucred, "%s", name);
779
780	finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
781
782	return (0);
783}
784#endif /* PTS_EXTERNAL */
785
786int
787posix_openpt(struct thread *td, struct posix_openpt_args *uap)
788{
789	int error, fd;
790	struct file *fp;
791
792	/*
793	 * POSIX states it's unspecified when other flags are passed. We
794	 * don't allow this.
795	 */
796	if (uap->flags & ~(O_RDWR|O_NOCTTY))
797		return (EINVAL);
798
799	error = falloc(td, &fp, &fd);
800	if (error)
801		return (error);
802
803	/* Allocate the actual pseudo-TTY. */
804	error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
805	if (error != 0) {
806		fdclose(td->td_proc->p_fd, fp, fd, td);
807		return (error);
808	}
809
810	/* Pass it back to userspace. */
811	td->td_retval[0] = fd;
812	fdrop(fp, td);
813
814	return (0);
815}
816
817#if defined(PTS_COMPAT) || defined(PTS_LINUX)
818static int
819ptmx_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp)
820{
821
822	return (pts_alloc(fflags & (FREAD|FWRITE), td, fp));
823}
824
825static struct cdevsw ptmx_cdevsw = {
826	.d_version	= D_VERSION,
827	.d_fdopen	= ptmx_fdopen,
828	.d_name		= "ptmx",
829};
830#endif /* PTS_COMPAT || PTS_LINUX */
831
832static void
833pts_init(void *unused)
834{
835
836	pts_pool = new_unrhdr(0, MAXPTSDEVS, NULL);
837#if defined(PTS_COMPAT) || defined(PTS_LINUX)
838	make_dev(&ptmx_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "ptmx");
839#endif /* PTS_COMPAT || PTS_LINUX */
840}
841
842SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);
843