9fb6fb33c0457b2e250d12ab89de43f0dc474811
[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         tp->t_state &= ~TS_ZOMBIE;
292         error = (*linesw[tp->t_line].l_open)(dev, tp);
293         if (error == 0)
294                 ptcwakeup(tp, FREAD|FWRITE);
295
296 #ifdef UNIX98_PTYS
297         /*
298          * Unix98 pty stuff.
299          * On open of the slave, we set the corresponding flag in the common
300          * struct.
301          */
302         ptydebug(1, "ptsopen=%s | unix98? %s\n", dev->si_name,
303             (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
304
305         if ((!error) && (pti->pt_flags2 & PF_UNIX98)) {
306                 pti->pt_flags2 |= PF_SOPEN;
307         }
308 #endif
309
310         return (error);
311 }
312
313 static  int
314 ptsclose(struct dev_close_args *ap)
315 {
316         cdev_t dev = ap->a_head.a_dev;
317         struct tty *tp;
318         struct pt_ioctl *pti = dev->si_drv1;
319         int err;
320
321         tp = dev->si_tty;
322         err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
323         ptsstop(tp, FREAD|FWRITE);
324         (void) ttyclose(tp); /* clears t_state */
325         tp->t_state |= TS_ZOMBIE;
326
327 #ifdef UNIX98_PTYS
328         /*
329          * Unix98 pty stuff.
330          * On close of the slave, we unset the corresponding flag, and if the master
331          * isn't open anymore, we destroy the slave and unset the unit.
332          */
333         ptydebug(1, "ptsclose=%s | unix98? %s\n", dev->si_name,
334             (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
335
336         if (pti->pt_flags2 & PF_UNIX98) {
337                 pti->pt_flags2 &= ~PF_SOPEN;
338                 KKASSERT((pti->pt_flags2 & PF_SOPEN) == 0);
339                 ptydebug(1, "master open? %s\n",
340                     (pti->pt_flags2 & PF_MOPEN)?"yes":"no");
341
342                 if (!(pti->pt_flags2 & PF_SOPEN) && !(pti->pt_flags2 & PF_MOPEN)) {
343                         devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
344                         destroy_dev(dev);
345                 }
346         }
347 #endif
348
349         return (err);
350 }
351
352 static  int
353 ptsread(struct dev_read_args *ap)
354 {
355         cdev_t dev = ap->a_head.a_dev;
356         struct proc *p = curproc;
357         struct tty *tp = dev->si_tty;
358         struct pt_ioctl *pti = dev->si_drv1;
359         struct lwp *lp;
360
361         int error = 0;
362
363         lp = curthread->td_lwp;
364
365 again:
366         if (pti->pt_flags & PF_REMOTE) {
367                 while (isbackground(p, tp)) {
368                         if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
369                             SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
370                             p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT)
371                                 return (EIO);
372                         pgsignal(p->p_pgrp, SIGTTIN, 1);
373                         error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
374                         if (error)
375                                 return (error);
376                 }
377                 if (tp->t_canq.c_cc == 0) {
378                         if (ap->a_ioflag & IO_NDELAY)
379                                 return (EWOULDBLOCK);
380                         error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
381                                          "ptsin", 0);
382                         if (error)
383                                 return (error);
384                         goto again;
385                 }
386                 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
387                         if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
388                                 error = EFAULT;
389                                 break;
390                         }
391                 if (tp->t_canq.c_cc == 1)
392                         clist_getc(&tp->t_canq);
393                 if (tp->t_canq.c_cc)
394                         return (error);
395         } else
396                 if (tp->t_oproc)
397                         error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
398         ptcwakeup(tp, FWRITE);
399         return (error);
400 }
401
402 /*
403  * Write to pseudo-tty.
404  * Wakeups of controlling tty will happen
405  * indirectly, when tty driver calls ptsstart.
406  */
407 static  int
408 ptswrite(struct dev_write_args *ap)
409 {
410         cdev_t dev = ap->a_head.a_dev;
411         struct tty *tp;
412
413         tp = dev->si_tty;
414         if (tp->t_oproc == 0)
415                 return (EIO);
416         return ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
417 }
418
419 /*
420  * Start output on pseudo-tty.
421  * Wake up process selecting or sleeping for input from controlling tty.
422  */
423 static void
424 ptsstart(struct tty *tp)
425 {
426         struct pt_ioctl *pti = tp->t_dev->si_drv1;
427
428         if (tp->t_state & TS_TTSTOP)
429                 return;
430         if (pti->pt_flags & PF_STOPPED) {
431                 pti->pt_flags &= ~PF_STOPPED;
432                 pti->pt_send = TIOCPKT_START;
433         }
434         ptcwakeup(tp, FREAD);
435 }
436
437 static void
438 ptcwakeup(struct tty *tp, int flag)
439 {
440         if (flag & FREAD) {
441                 wakeup(TSA_PTC_READ(tp));
442                 KNOTE(&tp->t_rsel.si_note, 0);
443         }
444         if (flag & FWRITE) {
445                 wakeup(TSA_PTC_WRITE(tp));
446                 KNOTE(&tp->t_wsel.si_note, 0);
447         }
448 }
449
450 static  int
451 ptcopen(struct dev_open_args *ap)
452 {
453         cdev_t dev = ap->a_head.a_dev;
454         struct tty *tp;
455         struct pt_ioctl *pti;
456
457         if (!dev->si_drv1)
458                 ptyinit(minor(dev));
459         if (!dev->si_drv1)
460                 return(ENXIO);  
461         pti = dev->si_drv1;
462         if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison)
463                 return(EBUSY);
464         tp = dev->si_tty;
465         if (tp->t_oproc)
466                 return (EIO);
467         tp->t_oproc = ptsstart;
468         tp->t_stop = ptsstop;
469         (void)(*linesw[tp->t_line].l_modem)(tp, 1);
470         tp->t_lflag &= ~EXTPROC;
471         pti->pt_prison = ap->a_cred->cr_prison;
472         pti->pt_flags = 0;
473         pti->pt_send = 0;
474         pti->pt_ucntl = 0;
475
476         pti->devs->si_uid = ap->a_cred->cr_uid;
477         pti->devs->si_gid = 0;
478         pti->devs->si_perms = 0600;
479         pti->devc->si_uid = ap->a_cred->cr_uid;
480         pti->devc->si_gid = 0;
481         pti->devc->si_perms = 0600;
482
483 #ifdef UNIX98_PTYS
484         /*
485          * Unix98 pty stuff.
486          * On open of the master, we set the corresponding flag in the common
487          * struct.
488          */
489         ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev->si_name,
490             (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
491
492         if (pti->pt_flags2 & PF_UNIX98) {
493                 pti->pt_flags2 |= PF_MOPEN;
494         }
495 #endif
496
497         return (0);
498 }
499
500 static  int
501 ptcclose(struct dev_close_args *ap)
502 {
503         cdev_t dev = ap->a_head.a_dev;
504         struct tty *tp;
505         struct pt_ioctl *pti = dev->si_drv1;
506
507         tp = dev->si_tty;
508         (void)(*linesw[tp->t_line].l_modem)(tp, 0);
509
510 #ifdef UNIX98_PTYS
511         /*
512          * Unix98 pty stuff.
513          * On close of the master, we unset the corresponding flag in the common
514          * struct asap.
515          */
516         pti->pt_flags2 &= ~PF_MOPEN;
517 #endif
518
519         /*
520          * XXX MDMBUF makes no sense for ptys but would inhibit the above
521          * l_modem().  CLOCAL makes sense but isn't supported.   Special
522          * l_modem()s that ignore carrier drop make no sense for ptys but
523          * may be in use because other parts of the line discipline make
524          * sense for ptys.  Recover by doing everything that a normal
525          * ttymodem() would have done except for sending a SIGHUP.
526          */
527         if (tp->t_state & TS_ISOPEN) {
528                 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
529                 tp->t_state |= TS_ZOMBIE;
530                 ttyflush(tp, FREAD | FWRITE);
531         }
532         tp->t_oproc = 0;                /* mark closed */
533
534         pti = dev->si_drv1;
535         pti->pt_prison = NULL;
536         pti->devs->si_uid = 0;
537         pti->devs->si_gid = 0;
538         pti->devs->si_perms = 0666;
539         pti->devc->si_uid = 0;
540         pti->devc->si_gid = 0;
541         pti->devc->si_perms = 0666;
542
543 #ifdef UNIX98_PTYS
544         /*
545          * Unix98 pty stuff.
546          * On close of the master, we destroy the master and, if no slaves are open,
547          * we destroy the slave device and unset the unit.
548          */
549         ptydebug(1, "ptcclose=%s (master) | unix98? %s\n", dev->si_name,
550             (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
551         if (pti->pt_flags2 & PF_UNIX98) {
552                 KKASSERT((pti->pt_flags2 & PF_MOPEN) == 0);
553                 destroy_dev(dev);
554                 pti->devc = NULL;
555
556                 if (!(pti->pt_flags2 & PF_SOPEN)) {
557                         ptydebug(1, "ptcclose: slaves are not open\n");
558                         destroy_dev(pti->devs);
559                         devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty), dev->si_uminor);
560                 }
561         }
562 #endif
563
564         return (0);
565 }
566
567 static  int
568 ptcread(struct dev_read_args *ap)
569 {
570         cdev_t dev = ap->a_head.a_dev;
571         struct tty *tp = dev->si_tty;
572         struct pt_ioctl *pti = dev->si_drv1;
573         char buf[BUFSIZ];
574         int error = 0, cc;
575
576         /*
577          * We want to block until the slave
578          * is open, and there's something to read;
579          * but if we lost the slave or we're NBIO,
580          * then return the appropriate error instead.
581          */
582         for (;;) {
583                 if (tp->t_state&TS_ISOPEN) {
584                         if (pti->pt_flags&PF_PKT && pti->pt_send) {
585                                 error = ureadc((int)pti->pt_send, ap->a_uio);
586                                 if (error)
587                                         return (error);
588                                 if (pti->pt_send & TIOCPKT_IOCTL) {
589                                         cc = (int)szmin(ap->a_uio->uio_resid,
590                                                         sizeof(tp->t_termios));
591                                         uiomove((caddr_t)&tp->t_termios, cc,
592                                                 ap->a_uio);
593                                 }
594                                 pti->pt_send = 0;
595                                 return (0);
596                         }
597                         if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
598                                 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
599                                 if (error)
600                                         return (error);
601                                 pti->pt_ucntl = 0;
602                                 return (0);
603                         }
604                         if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
605                                 break;
606                 }
607                 if ((tp->t_state & TS_CONNECTED) == 0)
608                         return (0);     /* EOF */
609                 if (ap->a_ioflag & IO_NDELAY)
610                         return (EWOULDBLOCK);
611                 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
612                 if (error)
613                         return (error);
614         }
615         if (pti->pt_flags & (PF_PKT|PF_UCNTL))
616                 error = ureadc(0, ap->a_uio);
617         while (ap->a_uio->uio_resid > 0 && error == 0) {
618                 cc = q_to_b(&tp->t_outq, buf,
619                             (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
620                 if (cc <= 0)
621                         break;
622                 error = uiomove(buf, (size_t)cc, ap->a_uio);
623         }
624         ttwwakeup(tp);
625         return (error);
626 }
627
628 static  void
629 ptsstop(struct tty *tp, int flush)
630 {
631         struct pt_ioctl *pti = tp->t_dev->si_drv1;
632         int flag;
633
634         /* note: FLUSHREAD and FLUSHWRITE already ok */
635         if (flush == 0) {
636                 flush = TIOCPKT_STOP;
637                 pti->pt_flags |= PF_STOPPED;
638         } else
639                 pti->pt_flags &= ~PF_STOPPED;
640         pti->pt_send |= flush;
641         /* change of perspective */
642         flag = 0;
643         if (flush & FREAD)
644                 flag |= FWRITE;
645         if (flush & FWRITE)
646                 flag |= FREAD;
647         ptcwakeup(tp, flag);
648 }
649
650 /*
651  * kqueue ops for pseudo-terminals.
652  */
653 static struct filterops ptcread_filtops =
654         { 1, NULL, filt_ptcrdetach, filt_ptcread };
655 static struct filterops ptcwrite_filtops =
656         { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
657
658 static  int
659 ptckqfilter(struct dev_kqfilter_args *ap)
660 {
661         cdev_t dev = ap->a_head.a_dev;
662         struct knote *kn = ap->a_kn;
663         struct tty *tp = dev->si_tty;
664         struct klist *klist;
665
666         ap->a_result = 0;
667         switch (kn->kn_filter) {
668         case EVFILT_READ:
669                 klist = &tp->t_rsel.si_note;
670                 kn->kn_fop = &ptcread_filtops;
671                 break;
672         case EVFILT_WRITE:
673                 klist = &tp->t_wsel.si_note;
674                 kn->kn_fop = &ptcwrite_filtops;
675                 break;
676         default:
677                 ap->a_result = EOPNOTSUPP;
678                 return (0);
679         }
680
681         kn->kn_hook = (caddr_t)dev;
682
683         crit_enter();
684         SLIST_INSERT_HEAD(klist, kn, kn_selnext);
685         crit_exit();
686
687         return (0);
688 }
689
690 static int
691 filt_ptcread (struct knote *kn, long hint)
692 {
693         struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
694         struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
695
696         if (tp->t_state & TS_ZOMBIE) {
697                 kn->kn_flags |= EV_EOF;
698                 return (1);
699         }
700
701         if ((tp->t_state & TS_ISOPEN) &&
702             ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
703              ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
704              ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
705                 kn->kn_data = tp->t_outq.c_cc;
706                 return(1);
707         } else {
708                 return(0);
709         }
710 }
711
712 static int
713 filt_ptcwrite (struct knote *kn, long hint)
714 {
715         struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
716         struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
717
718         if (tp->t_state & TS_ZOMBIE) {
719                 kn->kn_flags |= EV_EOF;
720                 return (1);
721         }
722
723         if (tp->t_state & TS_ISOPEN &&
724             ((pti->pt_flags & PF_REMOTE) ?
725              (tp->t_canq.c_cc == 0) :
726              ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
727               (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
728                 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
729                 return(1);
730         } else {
731                 return(0);
732         }
733 }
734
735 static void
736 filt_ptcrdetach (struct knote *kn)
737 {
738         struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
739
740         crit_enter();
741         SLIST_REMOVE(&tp->t_rsel.si_note, kn, knote, kn_selnext);
742         crit_exit();
743 }
744
745 static void
746 filt_ptcwdetach (struct knote *kn)
747 {
748         struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
749
750         crit_enter();
751         SLIST_REMOVE(&tp->t_wsel.si_note, kn, knote, kn_selnext);
752         crit_exit();
753 }
754
755 /*
756  * I/O ops
757  */
758 static  int
759 ptcwrite(struct dev_write_args *ap)
760 {
761         cdev_t dev = ap->a_head.a_dev;
762         struct tty *tp = dev->si_tty;
763         u_char *cp = 0;
764         int cc = 0;
765         u_char locbuf[BUFSIZ];
766         int cnt = 0;
767         struct pt_ioctl *pti = dev->si_drv1;
768         int error = 0;
769
770 again:
771         if ((tp->t_state&TS_ISOPEN) == 0)
772                 goto block;
773         if (pti->pt_flags & PF_REMOTE) {
774                 if (tp->t_canq.c_cc)
775                         goto block;
776                 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
777                        tp->t_canq.c_cc < TTYHOG - 1) {
778                         if (cc == 0) {
779                                 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
780                                 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
781                                 cp = locbuf;
782                                 error = uiomove(cp, (size_t)cc, ap->a_uio);
783                                 if (error)
784                                         return (error);
785                                 /* check again for safety */
786                                 if ((tp->t_state & TS_ISOPEN) == 0) {
787                                         /* adjust as usual */
788                                         ap->a_uio->uio_resid += cc;
789                                         return (EIO);
790                                 }
791                         }
792                         if (cc > 0) {
793                                 cc = b_to_q((char *)cp, cc, &tp->t_canq);
794                                 /*
795                                  * XXX we don't guarantee that the canq size
796                                  * is >= TTYHOG, so the above b_to_q() may
797                                  * leave some bytes uncopied.  However, space
798                                  * is guaranteed for the null terminator if
799                                  * we don't fail here since (TTYHOG - 1) is
800                                  * not a multiple of CBSIZE.
801                                  */
802                                 if (cc > 0)
803                                         break;
804                         }
805                 }
806                 /* adjust for data copied in but not written */
807                 ap->a_uio->uio_resid += cc;
808                 clist_putc(0, &tp->t_canq);
809                 ttwakeup(tp);
810                 wakeup(TSA_PTS_READ(tp));
811                 return (0);
812         }
813         while (ap->a_uio->uio_resid > 0 || cc > 0) {
814                 if (cc == 0) {
815                         cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
816                         cp = locbuf;
817                         error = uiomove(cp, (size_t)cc, ap->a_uio);
818                         if (error)
819                                 return (error);
820                         /* check again for safety */
821                         if ((tp->t_state & TS_ISOPEN) == 0) {
822                                 /* adjust for data copied in but not written */
823                                 ap->a_uio->uio_resid += cc;
824                                 return (EIO);
825                         }
826                 }
827                 while (cc > 0) {
828                         if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
829                            (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
830                                 wakeup(TSA_HUP_OR_INPUT(tp));
831                                 goto block;
832                         }
833                         (*linesw[tp->t_line].l_rint)(*cp++, tp);
834                         cnt++;
835                         cc--;
836                 }
837                 cc = 0;
838         }
839         return (0);
840 block:
841         /*
842          * Come here to wait for slave to open, for space
843          * in outq, or space in rawq, or an empty canq.
844          */
845         if ((tp->t_state & TS_CONNECTED) == 0) {
846                 /* adjust for data copied in but not written */
847                 ap->a_uio->uio_resid += cc;
848                 return (EIO);
849         }
850         if (ap->a_ioflag & IO_NDELAY) {
851                 /* adjust for data copied in but not written */
852                 ap->a_uio->uio_resid += cc;
853                 if (cnt == 0)
854                         return (EWOULDBLOCK);
855                 return (0);
856         }
857         error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
858         if (error) {
859                 /* adjust for data copied in but not written */
860                 ap->a_uio->uio_resid += cc;
861                 return (error);
862         }
863         goto again;
864 }
865
866 /*ARGSUSED*/
867 static  int
868 ptyioctl(struct dev_ioctl_args *ap)
869 {
870         cdev_t dev = ap->a_head.a_dev;
871         struct tty *tp = dev->si_tty;
872         struct pt_ioctl *pti = dev->si_drv1;
873         u_char *cc = tp->t_cc;
874         int stop, error;
875
876         if (dev_dflags(dev) & D_MASTER) {
877                 switch (ap->a_cmd) {
878
879                 case TIOCGPGRP:
880                         /*
881                          * We avoid calling ttioctl on the controller since,
882                          * in that case, tp must be the controlling terminal.
883                          */
884                         *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
885                         return (0);
886
887                 case TIOCPKT:
888                         if (*(int *)ap->a_data) {
889                                 if (pti->pt_flags & PF_UCNTL)
890                                         return (EINVAL);
891                                 pti->pt_flags |= PF_PKT;
892                         } else
893                                 pti->pt_flags &= ~PF_PKT;
894                         return (0);
895
896                 case TIOCUCNTL:
897                         if (*(int *)ap->a_data) {
898                                 if (pti->pt_flags & PF_PKT)
899                                         return (EINVAL);
900                                 pti->pt_flags |= PF_UCNTL;
901                         } else
902                                 pti->pt_flags &= ~PF_UCNTL;
903                         return (0);
904
905                 case TIOCREMOTE:
906                         if (*(int *)ap->a_data)
907                                 pti->pt_flags |= PF_REMOTE;
908                         else
909                                 pti->pt_flags &= ~PF_REMOTE;
910                         ttyflush(tp, FREAD|FWRITE);
911                         return (0);
912
913                 case TIOCISPTMASTER:
914                         if ((pti->pt_flags2 & PF_UNIX98) && (pti->devc == dev))
915                                 return (0);
916                         else
917                                 return (EINVAL);
918                 }
919
920                 /*
921                  * The rest of the ioctls shouldn't be called until 
922                  * the slave is open.
923                  */
924                 if ((tp->t_state & TS_ISOPEN) == 0)
925                         return (EAGAIN);
926
927                 switch (ap->a_cmd) {
928 #ifdef COMPAT_43
929                 case TIOCSETP:
930                 case TIOCSETN:
931 #endif
932                 case TIOCSETD:
933                 case TIOCSETA:
934                 case TIOCSETAW:
935                 case TIOCSETAF:
936                         /*
937                          * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
938                          * ttywflush(tp) will hang if there are characters in
939                          * the outq.
940                          */
941                         ndflush(&tp->t_outq, tp->t_outq.c_cc);
942                         break;
943
944                 case TIOCSIG:
945                         if (*(unsigned int *)ap->a_data >= NSIG ||
946                             *(unsigned int *)ap->a_data == 0)
947                                 return(EINVAL);
948                         if ((tp->t_lflag&NOFLSH) == 0)
949                                 ttyflush(tp, FREAD|FWRITE);
950                         pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
951                         if ((*(unsigned int *)ap->a_data == SIGINFO) &&
952                             ((tp->t_lflag&NOKERNINFO) == 0))
953                                 ttyinfo(tp);
954                         return(0);
955                 }
956         }
957         if (ap->a_cmd == TIOCEXT) {
958                 /*
959                  * When the EXTPROC bit is being toggled, we need
960                  * to send an TIOCPKT_IOCTL if the packet driver
961                  * is turned on.
962                  */
963                 if (*(int *)ap->a_data) {
964                         if (pti->pt_flags & PF_PKT) {
965                                 pti->pt_send |= TIOCPKT_IOCTL;
966                                 ptcwakeup(tp, FREAD);
967                         }
968                         tp->t_lflag |= EXTPROC;
969                 } else {
970                         if ((tp->t_lflag & EXTPROC) &&
971                             (pti->pt_flags & PF_PKT)) {
972                                 pti->pt_send |= TIOCPKT_IOCTL;
973                                 ptcwakeup(tp, FREAD);
974                         }
975                         tp->t_lflag &= ~EXTPROC;
976                 }
977                 return(0);
978         }
979         error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
980                                               ap->a_fflag, ap->a_cred);
981         if (error == ENOIOCTL)
982                  error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
983         if (error == ENOIOCTL) {
984                 if (pti->pt_flags & PF_UCNTL &&
985                     (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
986                         if (ap->a_cmd & 0xff) {
987                                 pti->pt_ucntl = (u_char)ap->a_cmd;
988                                 ptcwakeup(tp, FREAD);
989                         }
990                         return (0);
991                 }
992                 error = ENOTTY;
993         }
994         /*
995          * If external processing and packet mode send ioctl packet.
996          */
997         if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
998                 switch(ap->a_cmd) {
999                 case TIOCSETA:
1000                 case TIOCSETAW:
1001                 case TIOCSETAF:
1002 #ifdef COMPAT_43
1003                 case TIOCSETP:
1004                 case TIOCSETN:
1005 #endif
1006 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1007                 case TIOCSETC:
1008                 case TIOCSLTC:
1009                 case TIOCLBIS:
1010                 case TIOCLBIC:
1011                 case TIOCLSET:
1012 #endif
1013                         pti->pt_send |= TIOCPKT_IOCTL;
1014                         ptcwakeup(tp, FREAD);
1015                 default:
1016                         break;
1017                 }
1018         }
1019         stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1020                 && CCEQ(cc[VSTART], CTRL('q'));
1021         if (pti->pt_flags & PF_NOSTOP) {
1022                 if (stop) {
1023                         pti->pt_send &= ~TIOCPKT_NOSTOP;
1024                         pti->pt_send |= TIOCPKT_DOSTOP;
1025                         pti->pt_flags &= ~PF_NOSTOP;
1026                         ptcwakeup(tp, FREAD);
1027                 }
1028         } else {
1029                 if (!stop) {
1030                         pti->pt_send &= ~TIOCPKT_DOSTOP;
1031                         pti->pt_send |= TIOCPKT_NOSTOP;
1032                         pti->pt_flags |= PF_NOSTOP;
1033                         ptcwakeup(tp, FREAD);
1034                 }
1035         }
1036         return (error);
1037 }
1038
1039
1040 static void ptc_drvinit (void *unused);
1041
1042 #ifdef UNIX98_PTYS
1043 SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1044                 0, "Change pty debug level");
1045 #endif
1046
1047 static void
1048 ptc_drvinit(void *unused)
1049 {
1050         int i;
1051
1052 #ifdef UNIX98_PTYS
1053         /*
1054          * Unix98 pty stuff.
1055          * Create the clonable base device.
1056          */
1057         make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1058             0, 0, 0666, "ptmx");
1059 #endif
1060
1061         for (i = 0; i < 256; i++) {
1062                 ptyinit(i);
1063         }
1064 }
1065
1066 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)