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