Merge branch 'kq_devices' into selwakeup
[dragonfly.git] / sys / kern / tty_pty.c
1 /*
2  * Copyright (c) 1982, 1986, 1989, 1993
3  *      The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. All advertising materials mentioning features or use of this software
14  *    must display the following acknowledgement:
15  *      This product includes software developed by the University of
16  *      California, Berkeley and its contributors.
17  * 4. Neither the name of the University nor the names of its contributors
18  *    may be used to endorse or promote products derived from this software
19  *    without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  *      @(#)tty_pty.c   8.4 (Berkeley) 2/20/95
34  * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $
35  * $DragonFly: src/sys/kern/tty_pty.c,v 1.21 2008/08/13 10:29:38 swildner Exp $
36  */
37
38 /*
39  * Pseudo-teletype Driver
40  * (Actually two drivers, requiring two dev_ops structures)
41  */
42 #include "use_pty.h"            /* XXX */
43 #include "opt_compat.h"
44
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48 #include <sys/ioctl_compat.h>
49 #endif
50 #include <sys/proc.h>
51 #include <sys/priv.h>
52 #include <sys/tty.h>
53 #include <sys/conf.h>
54 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
56 #include <sys/vnode.h>
57 #include <sys/signalvar.h>
58 #include <sys/malloc.h>
59 #include <sys/device.h>
60 #include <sys/thread2.h>
61 #include <sys/devfs.h>
62 #include <sys/stat.h>
63 #include <sys/sysctl.h>
64
65 #define UNIX98_PTYS     1
66
67 MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
68
69 static void ptsstart (struct tty *tp);
70 static void ptsstop (struct tty *tp, int rw);
71 static void ptcwakeup (struct tty *tp, int flag);
72 static void ptyinit (int n);
73 static int  filt_ptcread (struct knote *kn, long hint);
74 static void filt_ptcrdetach (struct knote *kn);
75 static int  filt_ptcwrite (struct knote *kn, long hint);
76 static void filt_ptcwdetach (struct knote *kn);
77
78 static  d_open_t        ptsopen;
79 static  d_close_t       ptsclose;
80 static  d_read_t        ptsread;
81 static  d_write_t       ptswrite;
82 static  d_ioctl_t       ptyioctl;
83 static  d_open_t        ptcopen;
84 static  d_close_t       ptcclose;
85 static  d_read_t        ptcread;
86 static  d_write_t       ptcwrite;
87 static  d_kqfilter_t    ptckqfilter;
88
89 #ifdef UNIX98_PTYS
90 DEVFS_DECLARE_CLONE_BITMAP(pty);
91
92 static  d_clone_t       ptyclone;
93
94 static int      pty_debug_level = 0;
95
96 static struct dev_ops pts98_ops = {
97         { "pts98", 0, D_TTY | D_KQFILTER },
98         .d_open =       ptsopen,
99         .d_close =      ptsclose,
100         .d_read =       ptsread,
101         .d_write =      ptswrite,
102         .d_ioctl =      ptyioctl,
103         .d_kqfilter =   ttykqfilter,
104         .d_revoke =     ttyrevoke
105 };
106
107 static struct dev_ops ptc98_ops = {
108         { "ptc98", 0, D_TTY | D_KQFILTER | D_MASTER },
109         .d_open =       ptcopen,
110         .d_close =      ptcclose,
111         .d_read =       ptcread,
112         .d_write =      ptcwrite,
113         .d_ioctl =      ptyioctl,
114         .d_kqfilter =   ptckqfilter,
115         .d_revoke =     ttyrevoke
116 };
117 #endif
118
119 #define CDEV_MAJOR_S    5
120 static struct dev_ops pts_ops = {
121         { "pts", CDEV_MAJOR_S, D_TTY | D_KQFILTER },
122         .d_open =       ptsopen,
123         .d_close =      ptsclose,
124         .d_read =       ptsread,
125         .d_write =      ptswrite,
126         .d_ioctl =      ptyioctl,
127         .d_kqfilter =   ttykqfilter,
128         .d_revoke =     ttyrevoke
129 };
130
131 #define CDEV_MAJOR_C    6
132 static struct dev_ops ptc_ops = {
133         { "ptc", CDEV_MAJOR_C, D_TTY | D_KQFILTER | D_MASTER },
134         .d_open =       ptcopen,
135         .d_close =      ptcclose,
136         .d_read =       ptcread,
137         .d_write =      ptcwrite,
138         .d_ioctl =      ptyioctl,
139         .d_kqfilter =   ptckqfilter,
140         .d_revoke =     ttyrevoke
141 };
142
143 #define BUFSIZ 100              /* Chunk size iomoved to/from user */
144
145 struct  pt_ioctl {
146         int     pt_flags;
147         int     pt_flags2;
148         struct  selinfo pt_selr, pt_selw;
149         u_char  pt_send;
150         u_char  pt_ucntl;
151         struct tty pt_tty;
152         cdev_t  devs, devc;
153         struct  prison *pt_prison;
154 };
155
156 #define PF_PKT          0x08            /* packet mode */
157 #define PF_STOPPED      0x10            /* user told stopped */
158 #define PF_REMOTE       0x20            /* remote and flow controlled input */
159 #define PF_NOSTOP       0x40
160 #define PF_UCNTL        0x80            /* user control mode */
161
162 #define PF_UNIX98       0x01
163 #define PF_SOPEN        0x02
164 #define PF_MOPEN        0x04
165
166 static int
167 ptydebug(int level, char *fmt, ...)
168 {
169         __va_list ap;
170
171         __va_start(ap, fmt);
172         if (level <= pty_debug_level)
173                 kvprintf(fmt, ap);
174         __va_end(ap);
175
176         return 0;
177 }
178
179 /*
180  * This function creates and initializes a pts/ptc pair
181  *
182  * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
183  * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
184  *
185  * XXX: define and add mapping of upper minor bits to allow more 
186  *      than 256 ptys.
187  */
188 static void
189 ptyinit(int n)
190 {
191         cdev_t devs, devc;
192         char *names = "pqrsPQRS";
193         struct pt_ioctl *pt;
194
195         /* For now we only map the lower 8 bits of the minor */
196         if (n & ~0xff)
197                 return;
198
199         pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
200         pt->devs = devs = make_dev(&pts_ops, n,
201             0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
202         pt->devc = devc = make_dev(&ptc_ops, n,
203             0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
204
205         devs->si_drv1 = devc->si_drv1 = pt;
206         devs->si_tty = devc->si_tty = &pt->pt_tty;
207         devs->si_flags |= SI_OVERRIDE;  /* uid, gid, perms from dev */
208         devc->si_flags |= SI_OVERRIDE;  /* uid, gid, perms from dev */
209         pt->pt_tty.t_dev = devs;
210         ttyregister(&pt->pt_tty);
211 }
212
213 #ifdef UNIX98_PTYS
214 static int
215 ptyclone(struct dev_clone_args *ap)
216 {
217         int unit;
218         struct pt_ioctl *pt;
219
220         /*
221          * Limit the number of unix98 pty (slave) devices to 1000, as
222          * the utmp(5) format only allows for 8 bytes for the tty,
223          * "pts/XXX".
224          * If this limit is reached, we don't clone and return error
225          * to devfs.
226          */
227         unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 1000);
228
229         if (unit < 0) {
230                 ap->a_dev = NULL;
231                 return 1;
232         }
233
234         pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
235
236         pt->devc = ap->a_dev = make_only_dev(&ptc98_ops, unit, ap->a_cred->cr_ruid,
237             0, 0600, "ptm/%d", unit);
238         pt->devs = make_dev(&pts98_ops, unit, ap->a_cred->cr_ruid, GID_TTY, 0620,
239             "pts/%d", unit);
240
241         pt->devs->si_flags |= SI_OVERRIDE;      /* uid, gid, perms from dev */
242         pt->devc->si_flags |= SI_OVERRIDE;      /* uid, gid, perms from dev */
243
244         pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
245         pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
246         pt->pt_tty.t_dev = pt->devs;
247         pt->pt_flags2 |= PF_UNIX98;
248
249         ttyregister(&pt->pt_tty);
250
251         return 0;
252 }
253 #endif
254
255 /*ARGSUSED*/
256 static  int
257 ptsopen(struct dev_open_args *ap)
258 {
259         cdev_t dev = ap->a_head.a_dev;
260         struct tty *tp;
261         int error;
262         struct pt_ioctl *pti;
263
264         if (!dev->si_drv1)
265                 ptyinit(minor(dev));
266         if (!dev->si_drv1)
267                 return(ENXIO);
268         pti = dev->si_drv1;
269         tp = dev->si_tty;
270         if ((tp->t_state & TS_ISOPEN) == 0) {
271                 ttychars(tp);           /* Set up default chars */
272                 tp->t_iflag = TTYDEF_IFLAG;
273                 tp->t_oflag = TTYDEF_OFLAG;
274                 tp->t_lflag = TTYDEF_LFLAG;
275                 tp->t_cflag = TTYDEF_CFLAG;
276                 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
277         } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
278                 return (EBUSY);
279         } else if (pti->pt_prison != ap->a_cred->cr_prison) {
280                 return (EBUSY);
281         }
282         if (tp->t_oproc)                        /* Ctrlr still around. */
283                 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
284         while ((tp->t_state & TS_CARR_ON) == 0) {
285                 if (ap->a_oflags & FNONBLOCK)
286                         break;
287                 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
288                 if (error)
289                         return (error);
290         }
291         error = (*linesw[tp->t_line].l_open)(dev, tp);
292         if (error == 0)
293                 ptcwakeup(tp, FREAD|FWRITE);
294
295 #ifdef UNIX98_PTYS
296         /*
297          * Unix98 pty stuff.
298          * On open of the slave, we set the corresponding flag in the common
299          * struct.
300          */
301         ptydebug(1, "ptsopen=%s | unix98? %s\n", dev->si_name,
302             (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
303
304         if ((!error) && (pti->pt_flags2 & PF_UNIX98)) {
305                 pti->pt_flags2 |= PF_SOPEN;
306         }
307 #endif
308
309         return (error);
310 }
311
312 static  int
313 ptsclose(struct dev_close_args *ap)
314 {
315         cdev_t dev = ap->a_head.a_dev;
316         struct tty *tp;
317         struct pt_ioctl *pti = dev->si_drv1;
318         int err;
319
320         tp = dev->si_tty;
321         err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
322         ptsstop(tp, FREAD|FWRITE);
323         (void) ttyclose(tp);
324
325 #ifdef UNIX98_PTYS
326         /*
327          * Unix98 pty stuff.
328          * On close of the slave, we unset the corresponding flag, and if the master
329          * isn't open anymore, we destroy the slave and unset the unit.
330          */
331         ptydebug(1, "ptsclose=%s | unix98? %s\n", dev->si_name,
332             (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
333
334         if (pti->pt_flags2 & PF_UNIX98) {
335                 pti->pt_flags2 &= ~PF_SOPEN;
336                 KKASSERT((pti->pt_flags2 & PF_SOPEN) == 0);
337                 ptydebug(1, "master open? %s\n",
338                     (pti->pt_flags2 & PF_MOPEN)?"yes":"no");
339
340                 if (!(pti->pt_flags2 & PF_SOPEN) && !(pti->pt_flags2 & PF_MOPEN)) {
341                         devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
342                         destroy_dev(dev);
343                 }
344         }
345 #endif
346
347         return (err);
348 }
349
350 static  int
351 ptsread(struct dev_read_args *ap)
352 {
353         cdev_t dev = ap->a_head.a_dev;
354         struct proc *p = curproc;
355         struct tty *tp = dev->si_tty;
356         struct pt_ioctl *pti = dev->si_drv1;
357         struct lwp *lp;
358
359         int error = 0;
360
361         lp = curthread->td_lwp;
362
363 again:
364         if (pti->pt_flags & PF_REMOTE) {
365                 while (isbackground(p, tp)) {
366                         if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
367                             SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
368                             p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
369                                 return (EIO);
370                         pgsignal(p->p_pgrp, SIGTTIN, 1);
371                         error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
372                         if (error)
373                                 return (error);
374                 }
375                 if (tp->t_canq.c_cc == 0) {
376                         if (ap->a_ioflag & IO_NDELAY)
377                                 return (EWOULDBLOCK);
378                         error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
379                                          "ptsin", 0);
380                         if (error)
381                                 return (error);
382                         goto again;
383                 }
384                 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
385                         if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
386                                 error = EFAULT;
387                                 break;
388                         }
389                 if (tp->t_canq.c_cc == 1)
390                         clist_getc(&tp->t_canq);
391                 if (tp->t_canq.c_cc)
392                         return (error);
393         } else
394                 if (tp->t_oproc)
395                         error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
396         ptcwakeup(tp, FWRITE);
397         return (error);
398 }
399
400 /*
401  * Write to pseudo-tty.
402  * Wakeups of controlling tty will happen
403  * indirectly, when tty driver calls ptsstart.
404  */
405 static  int
406 ptswrite(struct dev_write_args *ap)
407 {
408         cdev_t dev = ap->a_head.a_dev;
409         struct tty *tp;
410
411         tp = dev->si_tty;
412         if (tp->t_oproc == 0)
413                 return (EIO);
414         return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
415 }
416
417 /*
418  * Start output on pseudo-tty.
419  * Wake up process selecting or sleeping for input from controlling tty.
420  */
421 static void
422 ptsstart(struct tty *tp)
423 {
424         struct pt_ioctl *pti = tp->t_dev->si_drv1;
425
426         if (tp->t_state & TS_TTSTOP)
427                 return;
428         if (pti->pt_flags & PF_STOPPED) {
429                 pti->pt_flags &= ~PF_STOPPED;
430                 pti->pt_send = TIOCPKT_START;
431         }
432         ptcwakeup(tp, FREAD);
433 }
434
435 static void
436 ptcwakeup(struct tty *tp, int flag)
437 {
438         if (flag & FREAD) {
439                 wakeup(TSA_PTC_READ(tp));
440                 KNOTE(&tp->t_rsel.si_note, 0);
441         }
442         if (flag & FWRITE) {
443                 wakeup(TSA_PTC_WRITE(tp));
444                 KNOTE(&tp->t_wsel.si_note, 0);
445         }
446 }
447
448 static  int
449 ptcopen(struct dev_open_args *ap)
450 {
451         cdev_t dev = ap->a_head.a_dev;
452         struct tty *tp;
453         struct pt_ioctl *pti;
454
455         if (!dev->si_drv1)
456                 ptyinit(minor(dev));
457         if (!dev->si_drv1)
458                 return(ENXIO);  
459         pti = dev->si_drv1;
460         if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison)
461                 return(EBUSY);
462         tp = dev->si_tty;
463         if (tp->t_oproc)
464                 return (EIO);
465         tp->t_oproc = ptsstart;
466         tp->t_stop = ptsstop;
467         (void)(*linesw[tp->t_line].l_modem)(tp, 1);
468         tp->t_lflag &= ~EXTPROC;
469         pti->pt_prison = ap->a_cred->cr_prison;
470         pti->pt_flags = 0;
471         pti->pt_send = 0;
472         pti->pt_ucntl = 0;
473
474         pti->devs->si_uid = ap->a_cred->cr_uid;
475         pti->devs->si_gid = 0;
476         pti->devs->si_perms = 0600;
477         pti->devc->si_uid = ap->a_cred->cr_uid;
478         pti->devc->si_gid = 0;
479         pti->devc->si_perms = 0600;
480
481 #ifdef UNIX98_PTYS
482         /*
483          * Unix98 pty stuff.
484          * On open of the master, we set the corresponding flag in the common
485          * struct.
486          */
487         ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev->si_name,
488             (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
489
490         if (pti->pt_flags2 & PF_UNIX98) {
491                 pti->pt_flags2 |= PF_MOPEN;
492         }
493 #endif
494
495         return (0);
496 }
497
498 static  int
499 ptcclose(struct dev_close_args *ap)
500 {
501         cdev_t dev = ap->a_head.a_dev;
502         struct tty *tp;
503         struct pt_ioctl *pti = dev->si_drv1;
504
505         tp = dev->si_tty;
506         (void)(*linesw[tp->t_line].l_modem)(tp, 0);
507
508 #ifdef UNIX98_PTYS
509         /*
510          * Unix98 pty stuff.
511          * On close of the master, we unset the corresponding flag in the common
512          * struct asap.
513          */
514         pti->pt_flags2 &= ~PF_MOPEN;
515 #endif
516
517         /*
518          * XXX MDMBUF makes no sense for ptys but would inhibit the above
519          * l_modem().  CLOCAL makes sense but isn't supported.   Special
520          * l_modem()s that ignore carrier drop make no sense for ptys but
521          * may be in use because other parts of the line discipline make
522          * sense for ptys.  Recover by doing everything that a normal
523          * ttymodem() would have done except for sending a SIGHUP.
524          */
525         if (tp->t_state & TS_ISOPEN) {
526                 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
527                 tp->t_state |= TS_ZOMBIE;
528                 ttyflush(tp, FREAD | FWRITE);
529         }
530         tp->t_oproc = 0;                /* mark closed */
531
532         pti = dev->si_drv1;
533         pti->pt_prison = NULL;
534         pti->devs->si_uid = 0;
535         pti->devs->si_gid = 0;
536         pti->devs->si_perms = 0666;
537         pti->devc->si_uid = 0;
538         pti->devc->si_gid = 0;
539         pti->devc->si_perms = 0666;
540
541 #ifdef UNIX98_PTYS
542         /*
543          * Unix98 pty stuff.
544          * On close of the master, we destroy the master and, if no slaves are open,
545          * we destroy the slave device and unset the unit.
546          */
547         ptydebug(1, "ptcclose=%s (master) | unix98? %s\n", dev->si_name,
548             (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
549         if (pti->pt_flags2 & PF_UNIX98) {
550                 KKASSERT((pti->pt_flags2 & PF_MOPEN) == 0);
551                 destroy_dev(dev);
552                 pti->devc = NULL;
553
554                 if (!(pti->pt_flags2 & PF_SOPEN)) {
555                         ptydebug(1, "ptcclose: slaves are not open\n");
556                         destroy_dev(pti->devs);
557                         devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
558                 }
559         }
560 #endif
561
562         return (0);
563 }
564
565 static  int
566 ptcread(struct dev_read_args *ap)
567 {
568         cdev_t dev = ap->a_head.a_dev;
569         struct tty *tp = dev->si_tty;
570         struct pt_ioctl *pti = dev->si_drv1;
571         char buf[BUFSIZ];
572         int error = 0, cc;
573
574         /*
575          * We want to block until the slave
576          * is open, and there's something to read;
577          * but if we lost the slave or we're NBIO,
578          * then return the appropriate error instead.
579          */
580         for (;;) {
581                 if (tp->t_state&TS_ISOPEN) {
582                         if (pti->pt_flags&PF_PKT && pti->pt_send) {
583                                 error = ureadc((int)pti->pt_send, ap->a_uio);
584                                 if (error)
585                                         return (error);
586                                 if (pti->pt_send & TIOCPKT_IOCTL) {
587                                         cc = (int)szmin(ap->a_uio->uio_resid,
588                                                         sizeof(tp->t_termios));
589                                         uiomove((caddr_t)&tp->t_termios, cc,
590                                                 ap->a_uio);
591                                 }
592                                 pti->pt_send = 0;
593                                 return (0);
594                         }
595                         if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
596                                 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
597                                 if (error)
598                                         return (error);
599                                 pti->pt_ucntl = 0;
600                                 return (0);
601                         }
602                         if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
603                                 break;
604                 }
605                 if ((tp->t_state & TS_CONNECTED) == 0)
606                         return (0);     /* EOF */
607                 if (ap->a_ioflag & IO_NDELAY)
608                         return (EWOULDBLOCK);
609                 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
610                 if (error)
611                         return (error);
612         }
613         if (pti->pt_flags & (PF_PKT|PF_UCNTL))
614                 error = ureadc(0, ap->a_uio);
615         while (ap->a_uio->uio_resid > 0 && error == 0) {
616                 cc = q_to_b(&tp->t_outq, buf,
617                             (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
618                 if (cc <= 0)
619                         break;
620                 error = uiomove(buf, (size_t)cc, ap->a_uio);
621         }
622         ttwwakeup(tp);
623         return (error);
624 }
625
626 static  void
627 ptsstop(struct tty *tp, int flush)
628 {
629         struct pt_ioctl *pti = tp->t_dev->si_drv1;
630         int flag;
631
632         /* note: FLUSHREAD and FLUSHWRITE already ok */
633         if (flush == 0) {
634                 flush = TIOCPKT_STOP;
635                 pti->pt_flags |= PF_STOPPED;
636         } else
637                 pti->pt_flags &= ~PF_STOPPED;
638         pti->pt_send |= flush;
639         /* change of perspective */
640         flag = 0;
641         if (flush & FREAD)
642                 flag |= FWRITE;
643         if (flush & FWRITE)
644                 flag |= FREAD;
645         ptcwakeup(tp, flag);
646 }
647
648 /*
649  * kqueue ops for pseudo-terminals.
650  */
651 static struct filterops ptcread_filtops =
652         { 1, NULL, filt_ptcrdetach, filt_ptcread };
653 static struct filterops ptcwrite_filtops =
654         { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
655
656 static  int
657 ptckqfilter(struct dev_kqfilter_args *ap)
658 {
659         cdev_t dev = ap->a_head.a_dev;
660         struct knote *kn = ap->a_kn;
661         struct tty *tp = dev->si_tty;
662         struct klist *klist;
663
664         ap->a_result = 0;
665         switch (kn->kn_filter) {
666         case EVFILT_READ:
667                 klist = &tp->t_rsel.si_note;
668                 kn->kn_fop = &ptcread_filtops;
669                 break;
670         case EVFILT_WRITE:
671                 klist = &tp->t_wsel.si_note;
672                 kn->kn_fop = &ptcwrite_filtops;
673                 break;
674         default:
675                 ap->a_result = EOPNOTSUPP;
676                 return (0);
677         }
678
679         kn->kn_hook = (caddr_t)dev;
680
681         crit_enter();
682         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
683         crit_exit();
684
685         return (0);
686 }
687
688 static int
689 filt_ptcread (struct knote *kn, long hint)
690 {
691         struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
692         struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
693
694         if ((tp->t_state & TS_ISOPEN) &&
695             ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
696              ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
697              ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
698                 kn->kn_data = tp->t_outq.c_cc;
699                 return(1);
700         } else {
701                 return(0);
702         }
703 }
704
705 static int
706 filt_ptcwrite (struct knote *kn, long hint)
707 {
708         struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
709         struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
710
711         if (tp->t_state & TS_ISOPEN &&
712             ((pti->pt_flags & PF_REMOTE) ?
713              (tp->t_canq.c_cc == 0) :
714              ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
715               (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
716                 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
717                 return(1);
718         } else {
719                 return(0);
720         }
721 }
722
723 static void
724 filt_ptcrdetach (struct knote *kn)
725 {
726         struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
727
728         crit_enter();
729         SLIST_REMOVE(&tp->t_rsel.si_note, kn, knote, kn_selnext);
730         crit_exit();
731 }
732
733 static void
734 filt_ptcwdetach (struct knote *kn)
735 {
736         struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
737
738         crit_enter();
739         SLIST_REMOVE(&tp->t_wsel.si_note, kn, knote, kn_selnext);
740         crit_exit();
741 }
742
743 /*
744  * I/O ops
745  */
746 static  int
747 ptcwrite(struct dev_write_args *ap)
748 {
749         cdev_t dev = ap->a_head.a_dev;
750         struct tty *tp = dev->si_tty;
751         u_char *cp = 0;
752         int cc = 0;
753         u_char locbuf[BUFSIZ];
754         int cnt = 0;
755         struct pt_ioctl *pti = dev->si_drv1;
756         int error = 0;
757
758 again:
759         if ((tp->t_state&TS_ISOPEN) == 0)
760                 goto block;
761         if (pti->pt_flags & PF_REMOTE) {
762                 if (tp->t_canq.c_cc)
763                         goto block;
764                 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
765                        tp->t_canq.c_cc < TTYHOG - 1) {
766                         if (cc == 0) {
767                                 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
768                                 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
769                                 cp = locbuf;
770                                 error = uiomove(cp, (size_t)cc, ap->a_uio);
771                                 if (error)
772                                         return (error);
773                                 /* check again for safety */
774                                 if ((tp->t_state & TS_ISOPEN) == 0) {
775                                         /* adjust as usual */
776                                         ap->a_uio->uio_resid += cc;
777                                         return (EIO);
778                                 }
779                         }
780                         if (cc > 0) {
781                                 cc = b_to_q((char *)cp, cc, &tp->t_canq);
782                                 /*
783                                  * XXX we don't guarantee that the canq size
784                                  * is >= TTYHOG, so the above b_to_q() may
785                                  * leave some bytes uncopied.  However, space
786                                  * is guaranteed for the null terminator if
787                                  * we don't fail here since (TTYHOG - 1) is
788                                  * not a multiple of CBSIZE.
789                                  */
790                                 if (cc > 0)
791                                         break;
792                         }
793                 }
794                 /* adjust for data copied in but not written */
795                 ap->a_uio->uio_resid += cc;
796                 clist_putc(0, &tp->t_canq);
797                 ttwakeup(tp);
798                 wakeup(TSA_PTS_READ(tp));
799                 return (0);
800         }
801         while (ap->a_uio->uio_resid > 0 || cc > 0) {
802                 if (cc == 0) {
803                         cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
804                         cp = locbuf;
805                         error = uiomove(cp, (size_t)cc, ap->a_uio);
806                         if (error)
807                                 return (error);
808                         /* check again for safety */
809                         if ((tp->t_state & TS_ISOPEN) == 0) {
810                                 /* adjust for data copied in but not written */
811                                 ap->a_uio->uio_resid += cc;
812                                 return (EIO);
813                         }
814                 }
815                 while (cc > 0) {
816                         if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
817                            (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
818                                 wakeup(TSA_HUP_OR_INPUT(tp));
819                                 goto block;
820                         }
821                         (*linesw[tp->t_line].l_rint)(*cp++, tp);
822                         cnt++;
823                         cc--;
824                 }
825                 cc = 0;
826         }
827         return (0);
828 block:
829         /*
830          * Come here to wait for slave to open, for space
831          * in outq, or space in rawq, or an empty canq.
832          */
833         if ((tp->t_state & TS_CONNECTED) == 0) {
834                 /* adjust for data copied in but not written */
835                 ap->a_uio->uio_resid += cc;
836                 return (EIO);
837         }
838         if (ap->a_ioflag & IO_NDELAY) {
839                 /* adjust for data copied in but not written */
840                 ap->a_uio->uio_resid += cc;
841                 if (cnt == 0)
842                         return (EWOULDBLOCK);
843                 return (0);
844         }
845         error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
846         if (error) {
847                 /* adjust for data copied in but not written */
848                 ap->a_uio->uio_resid += cc;
849                 return (error);
850         }
851         goto again;
852 }
853
854 /*ARGSUSED*/
855 static  int
856 ptyioctl(struct dev_ioctl_args *ap)
857 {
858         cdev_t dev = ap->a_head.a_dev;
859         struct tty *tp = dev->si_tty;
860         struct pt_ioctl *pti = dev->si_drv1;
861         u_char *cc = tp->t_cc;
862         int stop, error;
863
864         if (dev_dflags(dev) & D_MASTER) {
865                 switch (ap->a_cmd) {
866
867                 case TIOCGPGRP:
868                         /*
869                          * We avoid calling ttioctl on the controller since,
870                          * in that case, tp must be the controlling terminal.
871                          */
872                         *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
873                         return (0);
874
875                 case TIOCPKT:
876                         if (*(int *)ap->a_data) {
877                                 if (pti->pt_flags & PF_UCNTL)
878                                         return (EINVAL);
879                                 pti->pt_flags |= PF_PKT;
880                         } else
881                                 pti->pt_flags &= ~PF_PKT;
882                         return (0);
883
884                 case TIOCUCNTL:
885                         if (*(int *)ap->a_data) {
886                                 if (pti->pt_flags & PF_PKT)
887                                         return (EINVAL);
888                                 pti->pt_flags |= PF_UCNTL;
889                         } else
890                                 pti->pt_flags &= ~PF_UCNTL;
891                         return (0);
892
893                 case TIOCREMOTE:
894                         if (*(int *)ap->a_data)
895                                 pti->pt_flags |= PF_REMOTE;
896                         else
897                                 pti->pt_flags &= ~PF_REMOTE;
898                         ttyflush(tp, FREAD|FWRITE);
899                         return (0);
900
901                 case TIOCISPTMASTER:
902                         if ((pti->pt_flags2 & PF_UNIX98) && (pti->devc == dev))
903                                 return (0);
904                         else
905                                 return (EINVAL);
906                 }
907
908                 /*
909                  * The rest of the ioctls shouldn't be called until 
910                  * the slave is open.
911                  */
912                 if ((tp->t_state & TS_ISOPEN) == 0)
913                         return (EAGAIN);
914
915                 switch (ap->a_cmd) {
916 #ifdef COMPAT_43
917                 case TIOCSETP:
918                 case TIOCSETN:
919 #endif
920                 case TIOCSETD:
921                 case TIOCSETA:
922                 case TIOCSETAW:
923                 case TIOCSETAF:
924                         /*
925                          * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
926                          * ttywflush(tp) will hang if there are characters in
927                          * the outq.
928                          */
929                         ndflush(&tp->t_outq, tp->t_outq.c_cc);
930                         break;
931
932                 case TIOCSIG:
933                         if (*(unsigned int *)ap->a_data >= NSIG ||
934                             *(unsigned int *)ap->a_data == 0)
935                                 return(EINVAL);
936                         if ((tp->t_lflag&NOFLSH) == 0)
937                                 ttyflush(tp, FREAD|FWRITE);
938                         pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
939                         if ((*(unsigned int *)ap->a_data == SIGINFO) &&
940                             ((tp->t_lflag&NOKERNINFO) == 0))
941                                 ttyinfo(tp);
942                         return(0);
943                 }
944         }
945         if (ap->a_cmd == TIOCEXT) {
946                 /*
947                  * When the EXTPROC bit is being toggled, we need
948                  * to send an TIOCPKT_IOCTL if the packet driver
949                  * is turned on.
950                  */
951                 if (*(int *)ap->a_data) {
952                         if (pti->pt_flags & PF_PKT) {
953                                 pti->pt_send |= TIOCPKT_IOCTL;
954                                 ptcwakeup(tp, FREAD);
955                         }
956                         tp->t_lflag |= EXTPROC;
957                 } else {
958                         if ((tp->t_lflag & EXTPROC) &&
959                             (pti->pt_flags & PF_PKT)) {
960                                 pti->pt_send |= TIOCPKT_IOCTL;
961                                 ptcwakeup(tp, FREAD);
962                         }
963                         tp->t_lflag &= ~EXTPROC;
964                 }
965                 return(0);
966         }
967         error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
968                                               ap->a_fflag, ap->a_cred);
969         if (error == ENOIOCTL)
970                  error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
971         if (error == ENOIOCTL) {
972                 if (pti->pt_flags & PF_UCNTL &&
973                     (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
974                         if (ap->a_cmd & 0xff) {
975                                 pti->pt_ucntl = (u_char)ap->a_cmd;
976                                 ptcwakeup(tp, FREAD);
977                         }
978                         return (0);
979                 }
980                 error = ENOTTY;
981         }
982         /*
983          * If external processing and packet mode send ioctl packet.
984          */
985         if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
986                 switch(ap->a_cmd) {
987                 case TIOCSETA:
988                 case TIOCSETAW:
989                 case TIOCSETAF:
990 #ifdef COMPAT_43
991                 case TIOCSETP:
992                 case TIOCSETN:
993 #endif
994 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
995                 case TIOCSETC:
996                 case TIOCSLTC:
997                 case TIOCLBIS:
998                 case TIOCLBIC:
999                 case TIOCLSET:
1000 #endif
1001                         pti->pt_send |= TIOCPKT_IOCTL;
1002                         ptcwakeup(tp, FREAD);
1003                 default:
1004                         break;
1005                 }
1006         }
1007         stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1008                 && CCEQ(cc[VSTART], CTRL('q'));
1009         if (pti->pt_flags & PF_NOSTOP) {
1010                 if (stop) {
1011                         pti->pt_send &= ~TIOCPKT_NOSTOP;
1012                         pti->pt_send |= TIOCPKT_DOSTOP;
1013                         pti->pt_flags &= ~PF_NOSTOP;
1014                         ptcwakeup(tp, FREAD);
1015                 }
1016         } else {
1017                 if (!stop) {
1018                         pti->pt_send &= ~TIOCPKT_DOSTOP;
1019                         pti->pt_send |= TIOCPKT_NOSTOP;
1020                         pti->pt_flags |= PF_NOSTOP;
1021                         ptcwakeup(tp, FREAD);
1022                 }
1023         }
1024         return (error);
1025 }
1026
1027
1028 static void ptc_drvinit (void *unused);
1029
1030 #ifdef UNIX98_PTYS
1031 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1032                 0, "Change pty debug level");
1033 #endif
1034
1035 static void
1036 ptc_drvinit(void *unused)
1037 {
1038         int i;
1039
1040 #ifdef UNIX98_PTYS
1041         /*
1042          * Unix98 pty stuff.
1043          * Create the clonable base device.
1044          */
1045         make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1046             0, 0, 0666, "ptmx");
1047 #endif
1048
1049         for (i = 0; i < 256; i++) {
1050                 ptyinit(i);
1051         }
1052 }
1053
1054 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)