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