Deleted Added
sdiff udiff text old ( 196452 ) new ( 196480 )
full compact
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 196452 2009-08-23 08:04:40Z 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/limits.h>
54#include <sys/malloc.h>
55#include <sys/poll.h>
56#include <sys/proc.h>
57#include <sys/resourcevar.h>
58#include <sys/serial.h>
59#include <sys/stat.h>
60#include <sys/syscall.h>
61#include <sys/syscallsubr.h>
62#include <sys/sysctl.h>
63#include <sys/sysent.h>
64#include <sys/sysproto.h>
65#include <sys/systm.h>
66#include <sys/tty.h>
67#include <sys/ttycom.h>
68
69#include <machine/stdarg.h>
70
71/*
72 * Our utmp(5) format is limited to 8-byte TTY line names. This means
73 * we can at most allocate 1000 pseudo-terminals ("pts/999"). Allow
74 * users to increase this number, assuming they have manually increased
75 * UT_LINESIZE.
76 */
77static struct unrhdr *pts_pool;
78static unsigned int pts_maxdev = 999;
79SYSCTL_UINT(_kern, OID_AUTO, pts_maxdev, CTLFLAG_RW, &pts_maxdev, 0,
80 "Maximum amount of pts(4) pseudo-terminals");
81
82static MALLOC_DEFINE(M_PTS, "pts", "pseudo tty device");
83
84/*
85 * Per-PTS structure.
86 *
87 * List of locks
88 * (t) locked by tty_lock()
89 * (c) const until freeing
90 */
91struct pts_softc {
92 int pts_unit; /* (c) Device unit number. */
93 unsigned int pts_flags; /* (t) Device flags. */
94#define PTS_PKT 0x1 /* Packet mode. */
95#define PTS_FINISHED 0x2 /* Return errors on read()/write(). */
96 char pts_pkt; /* (t) Unread packet mode data. */
97
98 struct cv pts_inwait; /* (t) Blocking write() on master. */
99 struct selinfo pts_inpoll; /* (t) Select queue for write(). */
100 struct cv pts_outwait; /* (t) Blocking read() on master. */
101 struct selinfo pts_outpoll; /* (t) Select queue for read(). */
102
103#ifdef PTS_EXTERNAL
104 struct cdev *pts_cdev; /* (c) Master device node. */
105#endif /* PTS_EXTERNAL */
106
107 struct uidinfo *pts_uidinfo; /* (c) Resource limit. */
108};
109
110/*
111 * Controller-side file operations.
112 */
113
114static int
115ptsdev_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
116 int flags, struct thread *td)
117{
118 struct tty *tp = fp->f_data;
119 struct pts_softc *psc = tty_softc(tp);
120 int error = 0;
121 char pkt;
122
123 if (uio->uio_resid == 0)
124 return (0);
125
126 tty_lock(tp);
127
128 for (;;) {
129 /*
130 * Implement packet mode. When packet mode is turned on,
131 * the first byte contains a bitmask of events that
132 * occured (start, stop, flush, window size, etc).
133 */
134 if (psc->pts_flags & PTS_PKT && psc->pts_pkt) {
135 pkt = psc->pts_pkt;
136 psc->pts_pkt = 0;
137 tty_unlock(tp);
138
139 error = ureadc(pkt, uio);
140 return (error);
141 }
142
143 /*
144 * Transmit regular data.
145 *
146 * XXX: We shouldn't use ttydisc_getc_poll()! Even
147 * though in this implementation, there is likely going
148 * to be data, we should just call ttydisc_getc_uio()
149 * and use its return value to sleep.
150 */
151 if (ttydisc_getc_poll(tp)) {
152 if (psc->pts_flags & PTS_PKT) {
153 /*
154 * XXX: Small race. Fortunately PTY
155 * consumers aren't multithreaded.
156 */
157
158 tty_unlock(tp);
159 error = ureadc(TIOCPKT_DATA, uio);
160 if (error)
161 return (error);
162 tty_lock(tp);
163 }
164
165 error = ttydisc_getc_uio(tp, uio);
166 break;
167 }
168
169 /* Maybe the device isn't used anyway. */
170 if (psc->pts_flags & PTS_FINISHED)
171 break;
172
173 /* Wait for more data. */
174 if (fp->f_flag & O_NONBLOCK) {
175 error = EWOULDBLOCK;
176 break;
177 }
178 error = cv_wait_sig(&psc->pts_outwait, tp->t_mtx);
179 if (error != 0)
180 break;
181 }
182
183 tty_unlock(tp);
184
185 return (error);
186}
187
188static int
189ptsdev_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
190 int flags, struct thread *td)
191{
192 struct tty *tp = fp->f_data;
193 struct pts_softc *psc = tty_softc(tp);
194 char ib[256], *ibstart;
195 size_t iblen, rintlen;
196 int error = 0;
197
198 if (uio->uio_resid == 0)
199 return (0);
200
201 for (;;) {
202 ibstart = ib;
203 iblen = MIN(uio->uio_resid, sizeof ib);
204 error = uiomove(ib, iblen, uio);
205
206 tty_lock(tp);
207 if (error != 0) {
208 iblen = 0;
209 goto done;
210 }
211
212 /*
213 * When possible, avoid the slow path. rint_bypass()
214 * copies all input to the input queue at once.
215 */
216 MPASS(iblen > 0);
217 do {
218 rintlen = ttydisc_rint_simple(tp, ibstart, iblen);
219 ibstart += rintlen;
220 iblen -= rintlen;
221 if (iblen == 0) {
222 /* All data written. */
223 break;
224 }
225
226 /* Maybe the device isn't used anyway. */
227 if (psc->pts_flags & PTS_FINISHED) {
228 error = EIO;
229 goto done;
230 }
231
232 /* Wait for more data. */
233 if (fp->f_flag & O_NONBLOCK) {
234 error = EWOULDBLOCK;
235 goto done;
236 }
237
238 /* Wake up users on the slave side. */
239 ttydisc_rint_done(tp);
240 error = cv_wait_sig(&psc->pts_inwait, tp->t_mtx);
241 if (error != 0)
242 goto done;
243 } while (iblen > 0);
244
245 if (uio->uio_resid == 0)
246 break;
247 tty_unlock(tp);
248 }
249
250done: ttydisc_rint_done(tp);
251 tty_unlock(tp);
252
253 /*
254 * Don't account for the part of the buffer that we couldn't
255 * pass to the TTY.
256 */
257 uio->uio_resid += iblen;
258 return (error);
259}
260
261static int
262ptsdev_truncate(struct file *fp, off_t length, struct ucred *active_cred,
263 struct thread *td)
264{
265
266 return (EINVAL);
267}
268
269static int
270ptsdev_ioctl(struct file *fp, u_long cmd, void *data,
271 struct ucred *active_cred, struct thread *td)
272{
273 struct tty *tp = fp->f_data;
274 struct pts_softc *psc = tty_softc(tp);
275 int error = 0, sig;
276
277 switch (cmd) {
278 case FIONBIO:
279 /* This device supports non-blocking operation. */
280 return (0);
281 case FIONREAD:
282 tty_lock(tp);
283 if (psc->pts_flags & PTS_FINISHED) {
284 /* Force read() to be called. */
285 *(int *)data = 1;
286 } else {
287 *(int *)data = ttydisc_getc_poll(tp);
288 }
289 tty_unlock(tp);
290 return (0);
291 case FIODGNAME: {
292 struct fiodgname_arg *fgn;
293 const char *p;
294 int i;
295
296 /* Reverse device name lookups, for ptsname() and ttyname(). */
297 fgn = data;
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 *(struct termios*)data = tp->t_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 if (error == ENOIOCTL)
389 error = ENOTTY;
390
391 return (error);
392}
393
394static int
395ptsdev_poll(struct file *fp, int events, struct ucred *active_cred,
396 struct thread *td)
397{
398 struct tty *tp = fp->f_data;
399 struct pts_softc *psc = tty_softc(tp);
400 int revents = 0;
401
402 tty_lock(tp);
403
404 if (psc->pts_flags & PTS_FINISHED) {
405 /* Slave device is not opened. */
406 tty_unlock(tp);
407 return ((events & (POLLIN|POLLRDNORM)) | POLLHUP);
408 }
409
410 if (events & (POLLIN|POLLRDNORM)) {
411 /* See if we can getc something. */
412 if (ttydisc_getc_poll(tp) ||
413 (psc->pts_flags & PTS_PKT && psc->pts_pkt))
414 revents |= events & (POLLIN|POLLRDNORM);
415 }
416 if (events & (POLLOUT|POLLWRNORM)) {
417 /* See if we can rint something. */
418 if (ttydisc_rint_poll(tp))
419 revents |= events & (POLLOUT|POLLWRNORM);
420 }
421
422 /*
423 * No need to check for POLLHUP here. This device cannot be used
424 * as a callout device, which means we always have a carrier,
425 * because the master is.
426 */
427
428 if (revents == 0) {
429 /*
430 * This code might look misleading, but the naming of
431 * poll events on this side is the opposite of the slave
432 * device.
433 */
434 if (events & (POLLIN|POLLRDNORM))
435 selrecord(td, &psc->pts_outpoll);
436 if (events & (POLLOUT|POLLWRNORM))
437 selrecord(td, &psc->pts_inpoll);
438 }
439
440 tty_unlock(tp);
441
442 return (revents);
443}
444
445/*
446 * kqueue support.
447 */
448
449static void
450pts_kqops_read_detach(struct knote *kn)
451{
452 struct file *fp = kn->kn_fp;
453 struct tty *tp = fp->f_data;
454 struct pts_softc *psc = tty_softc(tp);
455
456 knlist_remove(&psc->pts_outpoll.si_note, kn, 0);
457}
458
459static int
460pts_kqops_read_event(struct knote *kn, long hint)
461{
462 struct file *fp = kn->kn_fp;
463 struct tty *tp = fp->f_data;
464 struct pts_softc *psc = tty_softc(tp);
465
466 if (psc->pts_flags & PTS_FINISHED) {
467 kn->kn_flags |= EV_EOF;
468 return (1);
469 } else {
470 kn->kn_data = ttydisc_getc_poll(tp);
471 return (kn->kn_data > 0);
472 }
473}
474
475static void
476pts_kqops_write_detach(struct knote *kn)
477{
478 struct file *fp = kn->kn_fp;
479 struct tty *tp = fp->f_data;
480 struct pts_softc *psc = tty_softc(tp);
481
482 knlist_remove(&psc->pts_inpoll.si_note, kn, 0);
483}
484
485static int
486pts_kqops_write_event(struct knote *kn, long hint)
487{
488 struct file *fp = kn->kn_fp;
489 struct tty *tp = fp->f_data;
490 struct pts_softc *psc = tty_softc(tp);
491
492 if (psc->pts_flags & PTS_FINISHED) {
493 kn->kn_flags |= EV_EOF;
494 return (1);
495 } else {
496 kn->kn_data = ttydisc_rint_poll(tp);
497 return (kn->kn_data > 0);
498 }
499}
500
501static struct filterops pts_kqops_read =
502 { 1, NULL, pts_kqops_read_detach, pts_kqops_read_event };
503static struct filterops pts_kqops_write =
504 { 1, NULL, pts_kqops_write_detach, pts_kqops_write_event };
505
506static int
507ptsdev_kqfilter(struct file *fp, struct knote *kn)
508{
509 struct tty *tp = fp->f_data;
510 struct pts_softc *psc = tty_softc(tp);
511 int error = 0;
512
513 tty_lock(tp);
514
515 switch (kn->kn_filter) {
516 case EVFILT_READ:
517 kn->kn_fop = &pts_kqops_read;
518 knlist_add(&psc->pts_outpoll.si_note, kn, 1);
519 break;
520 case EVFILT_WRITE:
521 kn->kn_fop = &pts_kqops_write;
522 knlist_add(&psc->pts_inpoll.si_note, kn, 1);
523 break;
524 default:
525 error = EINVAL;
526 break;
527 }
528
529 tty_unlock(tp);
530 return (error);
531}
532
533static int
534ptsdev_stat(struct file *fp, struct stat *sb, struct ucred *active_cred,
535 struct thread *td)
536{
537 struct tty *tp = fp->f_data;
538#ifdef PTS_EXTERNAL
539 struct pts_softc *psc = tty_softc(tp);
540#endif /* PTS_EXTERNAL */
541 struct cdev *dev = tp->t_dev;
542
543 /*
544 * According to POSIX, we must implement an fstat(). This also
545 * makes this implementation compatible with Linux binaries,
546 * because Linux calls fstat() on the pseudo-terminal master to
547 * obtain st_rdev.
548 *
549 * XXX: POSIX also mentions we must fill in st_dev, but how?
550 */
551
552 bzero(sb, sizeof *sb);
553#ifdef PTS_EXTERNAL
554 if (psc->pts_cdev != NULL)
555 sb->st_ino = sb->st_rdev = dev2udev(psc->pts_cdev);
556 else
557#endif /* PTS_EXTERNAL */
558 sb->st_ino = sb->st_rdev = tty_udev(tp);
559
560 sb->st_atimespec = dev->si_atime;
561 sb->st_ctimespec = dev->si_ctime;
562 sb->st_mtimespec = dev->si_mtime;
563 sb->st_uid = dev->si_uid;
564 sb->st_gid = dev->si_gid;
565 sb->st_mode = dev->si_mode | S_IFCHR;
566
567 return (0);
568}
569
570static int
571ptsdev_close(struct file *fp, struct thread *td)
572{
573 struct tty *tp = fp->f_data;
574
575 /* Deallocate TTY device. */
576 tty_lock(tp);
577 tty_rel_gone(tp);
578
579 return (0);
580}
581
582static struct fileops ptsdev_ops = {
583 .fo_read = ptsdev_read,
584 .fo_write = ptsdev_write,
585 .fo_truncate = ptsdev_truncate,
586 .fo_ioctl = ptsdev_ioctl,
587 .fo_poll = ptsdev_poll,
588 .fo_kqfilter = ptsdev_kqfilter,
589 .fo_stat = ptsdev_stat,
590 .fo_close = ptsdev_close,
591 .fo_flags = DFLAG_PASSABLE,
592};
593
594/*
595 * Driver-side hooks.
596 */
597
598static void
599ptsdrv_outwakeup(struct tty *tp)
600{
601 struct pts_softc *psc = tty_softc(tp);
602
603 cv_broadcast(&psc->pts_outwait);
604 selwakeup(&psc->pts_outpoll);
605 KNOTE_LOCKED(&psc->pts_outpoll.si_note, 0);
606}
607
608static void
609ptsdrv_inwakeup(struct tty *tp)
610{
611 struct pts_softc *psc = tty_softc(tp);
612
613 cv_broadcast(&psc->pts_inwait);
614 selwakeup(&psc->pts_inpoll);
615 KNOTE_LOCKED(&psc->pts_inpoll.si_note, 0);
616}
617
618static int
619ptsdrv_open(struct tty *tp)
620{
621 struct pts_softc *psc = tty_softc(tp);
622
623 psc->pts_flags &= ~PTS_FINISHED;
624
625 return (0);
626}
627
628static void
629ptsdrv_close(struct tty *tp)
630{
631 struct pts_softc *psc = tty_softc(tp);
632
633 /* Wake up any blocked readers/writers. */
634 psc->pts_flags |= PTS_FINISHED;
635 ptsdrv_outwakeup(tp);
636 ptsdrv_inwakeup(tp);
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 if (unit > pts_maxdev) {
724 free_unr(pts_pool, unit);
725 chgptscnt(uid, -1, 0);
726 return (EAGAIN);
727 }
728
729 /* Allocate TTY and softc. */
730 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
731 cv_init(&psc->pts_inwait, "pts inwait");
732 cv_init(&psc->pts_outwait, "pts outwait");
733
734 psc->pts_unit = unit;
735 psc->pts_uidinfo = uid;
736 uihold(uid);
737
738 tp = tty_alloc(&pts_class, psc);
739 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
740 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
741
742 /* Expose the slave device as well. */
743 tty_makedev(tp, td->td_ucred, "pts/%u", psc->pts_unit);
744
745 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
746
747 return (0);
748}
749
750#ifdef PTS_EXTERNAL
751int
752pts_alloc_external(int fflags, struct thread *td, struct file *fp,
753 struct cdev *dev, const char *name)
754{
755 int ok;
756 struct tty *tp;
757 struct pts_softc *psc;
758 struct proc *p = td->td_proc;
759 struct uidinfo *uid = td->td_ucred->cr_ruidinfo;
760
761 /* Resource limiting. */
762 PROC_LOCK(p);
763 ok = chgptscnt(uid, 1, lim_cur(p, RLIMIT_NPTS));
764 PROC_UNLOCK(p);
765 if (!ok)
766 return (EAGAIN);
767
768 /* Allocate TTY and softc. */
769 psc = malloc(sizeof(struct pts_softc), M_PTS, M_WAITOK|M_ZERO);
770 cv_init(&psc->pts_inwait, "pts inwait");
771 cv_init(&psc->pts_outwait, "pts outwait");
772
773 psc->pts_unit = -1;
774 psc->pts_cdev = dev;
775 psc->pts_uidinfo = uid;
776 uihold(uid);
777
778 tp = tty_alloc(&pts_class, psc);
779 knlist_init_mtx(&psc->pts_inpoll.si_note, tp->t_mtx);
780 knlist_init_mtx(&psc->pts_outpoll.si_note, tp->t_mtx);
781
782 /* Expose the slave device as well. */
783 tty_makedev(tp, td->td_ucred, "%s", name);
784
785 finit(fp, fflags, DTYPE_PTS, tp, &ptsdev_ops);
786
787 return (0);
788}
789#endif /* PTS_EXTERNAL */
790
791int
792posix_openpt(struct thread *td, struct posix_openpt_args *uap)
793{
794 int error, fd;
795 struct file *fp;
796
797 /*
798 * POSIX states it's unspecified when other flags are passed. We
799 * don't allow this.
800 */
801 if (uap->flags & ~(O_RDWR|O_NOCTTY))
802 return (EINVAL);
803
804 error = falloc(td, &fp, &fd);
805 if (error)
806 return (error);
807
808 /* Allocate the actual pseudo-TTY. */
809 error = pts_alloc(FFLAGS(uap->flags & O_ACCMODE), td, fp);
810 if (error != 0) {
811 fdclose(td->td_proc->p_fd, fp, fd, td);
812 return (error);
813 }
814
815 /* Pass it back to userspace. */
816 td->td_retval[0] = fd;
817 fdrop(fp, td);
818
819 return (0);
820}
821
822#if defined(PTS_COMPAT) || defined(PTS_LINUX)
823static int
824ptmx_fdopen(struct cdev *dev, int fflags, struct thread *td, struct file *fp)
825{
826
827 return (pts_alloc(fflags & (FREAD|FWRITE), td, fp));
828}
829
830static struct cdevsw ptmx_cdevsw = {
831 .d_version = D_VERSION,
832 .d_fdopen = ptmx_fdopen,
833 .d_name = "ptmx",
834};
835#endif /* PTS_COMPAT || PTS_LINUX */
836
837static void
838pts_init(void *unused)
839{
840
841 pts_pool = new_unrhdr(0, INT_MAX, NULL);
842#if defined(PTS_COMPAT) || defined(PTS_LINUX)
843 make_dev(&ptmx_cdevsw, 0, UID_ROOT, GID_WHEEL, 0666, "ptmx");
844#endif /* PTS_COMPAT || PTS_LINUX */
845}
846
847SYSINIT(pts, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, pts_init, NULL);