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