]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/tty_pty.c
Move vnode-to-file-handle translation from vfs_vptofh to vop_vptofh method.
[FreeBSD/FreeBSD.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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  *      @(#)tty_pty.c   8.4 (Berkeley) 2/20/95
30  */
31
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
34
35 /*
36  * Pseudo-teletype Driver
37  * (Actually two drivers, requiring two entries in 'cdevsw')
38  */
39 #include "opt_compat.h"
40 #include "opt_tty.h"
41 #include <sys/param.h>
42 #include <sys/systm.h>
43 #include <sys/lock.h>
44 #include <sys/mutex.h>
45 #include <sys/sx.h>
46 #if defined(COMPAT_43TTY)
47 #include <sys/ioctl_compat.h>
48 #endif
49 #include <sys/priv.h>
50 #include <sys/proc.h>
51 #include <sys/tty.h>
52 #include <sys/conf.h>
53 #include <sys/fcntl.h>
54 #include <sys/poll.h>
55 #include <sys/kernel.h>
56 #include <sys/uio.h>
57 #include <sys/signalvar.h>
58 #include <sys/malloc.h>
59
60 static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
61
62 static void ptsstart(struct tty *tp);
63 static void ptsstop(struct tty *tp, int rw);
64 static void ptcwakeup(struct tty *tp, int flag);
65 static struct cdev *ptyinit(struct cdev *cdev, struct thread *td);
66
67 static  d_open_t        ptsopen;
68 static  d_close_t       ptsclose;
69 static  d_read_t        ptsread;
70 static  d_write_t       ptswrite;
71 static  d_ioctl_t       ptsioctl;
72 static  d_open_t        ptcopen;
73 static  d_close_t       ptcclose;
74 static  d_read_t        ptcread;
75 static  d_ioctl_t       ptcioctl;
76 static  d_write_t       ptcwrite;
77 static  d_poll_t        ptcpoll;
78
79 static struct cdevsw pts_cdevsw = {
80         .d_version =    D_VERSION,
81         .d_open =       ptsopen,
82         .d_close =      ptsclose,
83         .d_read =       ptsread,
84         .d_write =      ptswrite,
85         .d_ioctl =      ptsioctl,
86         .d_name =       "pts",
87         .d_flags =      D_TTY | D_NEEDGIANT,
88 };
89
90 static struct cdevsw ptc_cdevsw = {
91         .d_version =    D_VERSION,
92         .d_open =       ptcopen,
93         .d_close =      ptcclose,
94         .d_read =       ptcread,
95         .d_write =      ptcwrite,
96         .d_ioctl =      ptcioctl,
97         .d_poll =       ptcpoll,
98         .d_name =       "ptc",
99         .d_flags =      D_TTY | D_NEEDGIANT,
100 };
101
102 #define BUFSIZ 100              /* Chunk size iomoved to/from user */
103
104 struct  ptsc {
105         int     pt_flags;
106         struct  selinfo pt_selr, pt_selw;
107         u_char  pt_send;
108         u_char  pt_ucntl;
109         struct tty *pt_tty;
110         struct cdev *devs, *devc;
111         int     pt_devs_open, pt_devc_open;
112         struct  prison *pt_prison;
113 };
114
115 #define PF_PKT          0x08            /* packet mode */
116 #define PF_STOPPED      0x10            /* user told stopped */
117 #define PF_NOSTOP       0x40
118 #define PF_UCNTL        0x80            /* user control mode */
119
120 #define TSA_PTC_READ(tp)        ((void *)&(tp)->t_outq.c_cf)
121 #define TSA_PTC_WRITE(tp)       ((void *)&(tp)->t_rawq.c_cl)
122 #define TSA_PTS_READ(tp)        ((void *)&(tp)->t_canq)
123
124 static char *names = "pqrsPQRS";
125 /*
126  * This function creates and initializes a pts/ptc pair
127  *
128  * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
129  * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
130  *
131  * XXX: define and add mapping of upper minor bits to allow more
132  *      than 256 ptys.
133  */
134 static struct cdev *
135 ptyinit(struct cdev *devc, struct thread *td)
136 {
137         struct ptsc *pt;
138         int n;
139
140         n = minor(devc);
141         /* For now we only map the lower 8 bits of the minor */
142         if (n & ~0xff)
143                 return (NULL);
144
145         devc->si_flags &= ~SI_CHEAPCLONE;
146
147         /*
148          * Initially do not create a slave endpoint.
149          */
150         pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
151         pt->devc = devc;
152
153         pt->pt_tty = ttyalloc();
154         pt->pt_tty->t_sc = pt;
155         devc->si_drv1 = pt;
156         devc->si_tty = pt->pt_tty;
157         return (devc);
158 }
159
160 static void
161 pty_create_slave(struct ucred *cred, struct ptsc *pt, int n)
162 {
163
164         pt->devs = make_dev_cred(&pts_cdevsw, n, cred, UID_ROOT, GID_WHEEL,
165             0666, "tty%c%r", names[n / 32], n % 32);
166         pt->devs->si_drv1 = pt;
167         pt->devs->si_tty = pt->pt_tty;
168         pt->pt_tty->t_dev = pt->devs;
169 }
170
171 static void
172 pty_destroy_slave(struct ptsc *pt)
173 {
174
175         if (pt->pt_tty->t_refcnt > 1)
176                 return;
177         pt->pt_tty->t_dev = NULL;
178         ttyrel(pt->pt_tty);
179         pt->pt_tty = NULL;
180         destroy_dev(pt->devs);
181         pt->devs = NULL;
182 }
183
184 static void
185 pty_maybe_destroy_slave(struct ptsc *pt)
186 {
187
188         /*
189          * vfs bugs and complications near revoke() make
190          * it currently impossible to destroy struct cdev
191          */
192         if (0 && pt->pt_devc_open == 0 && pt->pt_devs_open == 0)
193                 pty_destroy_slave(pt);
194 }
195
196 /*ARGSUSED*/
197 static  int
198 ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
199 {
200         struct tty *tp;
201         int error;
202         struct ptsc *pt;
203
204         if (!dev->si_drv1)
205                 return(ENXIO);
206         pt = dev->si_drv1;
207         tp = dev->si_tty;
208
209         if ((tp->t_state & TS_ISOPEN) == 0) {
210                 ttyinitmode(tp, 1, 0);
211         } else if (tp->t_state & TS_XCLUDE && priv_check(td,
212             PRIV_TTY_EXCLUSIVE))
213                 return (EBUSY);
214         else if (pt->pt_prison != td->td_ucred->cr_prison &&
215             priv_check(td, PRIV_TTY_PRISON))
216                 return (EBUSY);
217         if (tp->t_oproc)                        /* Ctrlr still around. */
218                 (void)ttyld_modem(tp, 1);
219         while ((tp->t_state & TS_CARR_ON) == 0) {
220                 if (flag&FNONBLOCK)
221                         break;
222                 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
223                                  "ptsopn", 0);
224                 if (error)
225                         return (error);
226         }
227         error = ttyld_open(tp, dev);
228         if (error == 0) {
229                 ptcwakeup(tp, FREAD|FWRITE);
230                 pt->pt_devs_open = 1;
231         } else
232                 pty_maybe_destroy_slave(pt);
233         return (error);
234 }
235
236 static  int
237 ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
238 {
239         struct ptsc *pti;
240         struct tty *tp;
241         int err;
242
243         tp = dev->si_tty;
244         pti = dev->si_drv1;
245
246         KASSERT(dev == pti->devs, ("ptsclose: dev != pti->devs"));
247
248         err = ttyld_close(tp, flag);
249         (void) tty_close(tp);
250
251         pti->pt_devs_open = 0;
252         pty_maybe_destroy_slave(pti);
253
254         return (err);
255 }
256
257 static  int
258 ptsread(struct cdev *dev, struct uio *uio, int flag)
259 {
260         struct tty *tp = dev->si_tty;
261         int error = 0;
262
263         if (tp->t_oproc)
264                 error = ttyld_read(tp, uio, flag);
265         ptcwakeup(tp, FWRITE);
266         return (error);
267 }
268
269 /*
270  * Write to pseudo-tty.
271  * Wakeups of controlling tty will happen
272  * indirectly, when tty driver calls ptsstart.
273  */
274 static  int
275 ptswrite(struct cdev *dev, struct uio *uio, int flag)
276 {
277         struct tty *tp;
278
279         tp = dev->si_tty;
280         if (tp->t_oproc == 0)
281                 return (EIO);
282         return (ttyld_write(tp, uio, flag));
283 }
284
285 /*
286  * Start output on pseudo-tty.
287  * Wake up process selecting or sleeping for input from controlling tty.
288  */
289 static void
290 ptsstart(struct tty *tp)
291 {
292         struct ptsc *pt = tp->t_sc;
293
294         if (tp->t_state & TS_TTSTOP)
295                 return;
296         if (pt->pt_flags & PF_STOPPED) {
297                 pt->pt_flags &= ~PF_STOPPED;
298                 pt->pt_send = TIOCPKT_START;
299         }
300         ptcwakeup(tp, FREAD);
301 }
302
303 static void
304 ptcwakeup(struct tty *tp, int flag)
305 {
306         struct ptsc *pt = tp->t_sc;
307
308         if (flag & FREAD) {
309                 selwakeuppri(&pt->pt_selr, TTIPRI);
310                 wakeup(TSA_PTC_READ(tp));
311         }
312         if (flag & FWRITE) {
313                 selwakeuppri(&pt->pt_selw, TTOPRI);
314                 wakeup(TSA_PTC_WRITE(tp));
315         }
316 }
317
318 static  int
319 ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
320 {
321         struct tty *tp;
322         struct ptsc *pt;
323
324         if (!dev->si_drv1)
325                 ptyinit(dev, td);
326         if (!dev->si_drv1)
327                 return(ENXIO);
328
329         pt = dev->si_drv1;
330         /*
331          * In case we have destroyed the struct tty at the last connect time,
332          * we need to recreate it.
333          */
334         if (pt->pt_tty == NULL) {
335                 pt->pt_tty = ttyalloc();
336                 pt->pt_tty->t_sc = pt;
337                 dev->si_tty = pt->pt_tty;
338         }
339         tp = dev->si_tty;
340
341         if (tp->t_oproc)
342                 return (EIO);
343         tp->t_timeout = -1;
344         tp->t_oproc = ptsstart;
345         tp->t_stop = ptsstop;
346         (void)ttyld_modem(tp, 1);
347         tp->t_lflag &= ~EXTPROC;
348         pt->pt_prison = td->td_ucred->cr_prison;
349         pt->pt_flags = 0;
350         pt->pt_send = 0;
351         pt->pt_ucntl = 0;
352
353         if (!pt->devs)
354                 pty_create_slave(td->td_ucred, pt, minor(dev));
355         pt->pt_devc_open = 1;
356
357         return (0);
358 }
359
360 static  int
361 ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
362 {
363         struct ptsc *pti = dev->si_drv1;
364         struct tty *tp;
365
366         tp = dev->si_tty;
367         (void)ttyld_modem(tp, 0);
368
369         /*
370          * XXX MDMBUF makes no sense for ptys but would inhibit the above
371          * l_modem().  CLOCAL makes sense but isn't supported.   Special
372          * l_modem()s that ignore carrier drop make no sense for ptys but
373          * may be in use because other parts of the line discipline make
374          * sense for ptys.  Recover by doing everything that a normal
375          * ttymodem() would have done except for sending a SIGHUP.
376          */
377         if (tp->t_state & TS_ISOPEN) {
378                 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
379                 tp->t_state |= TS_ZOMBIE;
380                 ttyflush(tp, FREAD | FWRITE);
381         }
382
383         tp->t_oproc = 0;                /* mark closed */
384         pti->pt_devc_open = 0;
385         pty_maybe_destroy_slave(pti);
386         return (0);
387 }
388
389 static  int
390 ptcread(struct cdev *dev, struct uio *uio, int flag)
391 {
392         struct tty *tp = dev->si_tty;
393         struct ptsc *pt = dev->si_drv1;
394         char buf[BUFSIZ];
395         int error = 0, cc;
396
397         /*
398          * We want to block until the slave
399          * is open, and there's something to read;
400          * but if we lost the slave or we're NBIO,
401          * then return the appropriate error instead.
402          */
403         for (;;) {
404                 if (tp->t_state&TS_ISOPEN) {
405                         if (pt->pt_flags&PF_PKT && pt->pt_send) {
406                                 error = ureadc((int)pt->pt_send, uio);
407                                 if (error)
408                                         return (error);
409                                 if (pt->pt_send & TIOCPKT_IOCTL) {
410                                         cc = min(uio->uio_resid,
411                                                 sizeof(tp->t_termios));
412                                         uiomove(&tp->t_termios, cc, uio);
413                                 }
414                                 pt->pt_send = 0;
415                                 return (0);
416                         }
417                         if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
418                                 error = ureadc((int)pt->pt_ucntl, uio);
419                                 if (error)
420                                         return (error);
421                                 pt->pt_ucntl = 0;
422                                 return (0);
423                         }
424                         if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
425                                 break;
426                 }
427                 if ((tp->t_state & TS_CONNECTED) == 0)
428                         return (0);     /* EOF */
429                 if (flag & O_NONBLOCK)
430                         return (EWOULDBLOCK);
431                 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
432                 if (error)
433                         return (error);
434         }
435         if (pt->pt_flags & (PF_PKT|PF_UCNTL))
436                 error = ureadc(0, uio);
437         while (uio->uio_resid > 0 && error == 0) {
438                 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
439                 if (cc <= 0)
440                         break;
441                 error = uiomove(buf, cc, uio);
442         }
443         ttwwakeup(tp);
444         return (error);
445 }
446
447 static  void
448 ptsstop(struct tty *tp, int flush)
449 {
450         struct ptsc *pt = tp->t_sc;
451         int flag;
452
453         /* note: FLUSHREAD and FLUSHWRITE already ok */
454         if (flush == 0) {
455                 flush = TIOCPKT_STOP;
456                 pt->pt_flags |= PF_STOPPED;
457         } else
458                 pt->pt_flags &= ~PF_STOPPED;
459         pt->pt_send |= flush;
460         /* change of perspective */
461         flag = 0;
462         if (flush & FREAD)
463                 flag |= FWRITE;
464         if (flush & FWRITE)
465                 flag |= FREAD;
466         ptcwakeup(tp, flag);
467 }
468
469 static  int
470 ptcpoll(struct cdev *dev, int events, struct thread *td)
471 {
472         struct tty *tp = dev->si_tty;
473         struct ptsc *pt = dev->si_drv1;
474         int revents = 0;
475         int s;
476
477         if ((tp->t_state & TS_CONNECTED) == 0)
478                 return (events & 
479                    (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
480
481         /*
482          * Need to block timeouts (ttrstart).
483          */
484         s = spltty();
485
486         if (events & (POLLIN | POLLRDNORM))
487                 if ((tp->t_state & TS_ISOPEN) &&
488                     ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
489                      ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
490                      ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
491                         revents |= events & (POLLIN | POLLRDNORM);
492
493         if (events & (POLLOUT | POLLWRNORM))
494                 if (tp->t_state & TS_ISOPEN &&
495                     (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
496                       (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
497                         revents |= events & (POLLOUT | POLLWRNORM);
498
499         if (events & POLLHUP)
500                 if ((tp->t_state & TS_CARR_ON) == 0)
501                         revents |= POLLHUP;
502
503         if (revents == 0) {
504                 if (events & (POLLIN | POLLRDNORM))
505                         selrecord(td, &pt->pt_selr);
506
507                 if (events & (POLLOUT | POLLWRNORM))
508                         selrecord(td, &pt->pt_selw);
509         }
510         splx(s);
511
512         return (revents);
513 }
514
515 static  int
516 ptcwrite(struct cdev *dev, struct uio *uio, int flag)
517 {
518         struct tty *tp = dev->si_tty;
519         u_char *cp = 0;
520         int cc = 0;
521         u_char locbuf[BUFSIZ];
522         int cnt = 0;
523         int error = 0;
524
525 again:
526         if ((tp->t_state&TS_ISOPEN) == 0)
527                 goto block;
528         while (uio->uio_resid > 0 || cc > 0) {
529                 if (cc == 0) {
530                         cc = min(uio->uio_resid, BUFSIZ);
531                         cp = locbuf;
532                         error = uiomove(cp, cc, uio);
533                         if (error)
534                                 return (error);
535                         /* check again for safety */
536                         if ((tp->t_state & TS_ISOPEN) == 0) {
537                                 /* adjust for data copied in but not written */
538                                 uio->uio_resid += cc;
539                                 return (EIO);
540                         }
541                 }
542                 while (cc > 0) {
543                         if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
544                            (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
545                                 wakeup(TSA_HUP_OR_INPUT(tp));
546                                 goto block;
547                         }
548                         ttyld_rint(tp, *cp++);
549                         cnt++;
550                         cc--;
551                 }
552                 cc = 0;
553         }
554         return (0);
555 block:
556         /*
557          * Come here to wait for slave to open, for space
558          * in outq, or space in rawq, or an empty canq.
559          */
560         if ((tp->t_state & TS_CONNECTED) == 0) {
561                 /* adjust for data copied in but not written */
562                 uio->uio_resid += cc;
563                 return (EIO);
564         }
565         if (flag & O_NONBLOCK) {
566                 /* adjust for data copied in but not written */
567                 uio->uio_resid += cc;
568                 if (cnt == 0)
569                         return (EWOULDBLOCK);
570                 return (0);
571         }
572         error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
573         if (error) {
574                 /* adjust for data copied in but not written */
575                 uio->uio_resid += cc;
576                 return (error);
577         }
578         goto again;
579 }
580
581 /*ARGSUSED*/
582 static  int
583 ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
584 {
585         struct tty *tp = dev->si_tty;
586         struct ptsc *pt = dev->si_drv1;
587 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
588     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
589         int ival;
590 #endif
591
592         switch (cmd) {
593
594         case TIOCGPGRP:
595                 /*
596                  * We avoid calling ttioctl on the controller since,
597                  * in that case, tp must be the controlling terminal.
598                  */
599                 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
600                 return (0);
601
602         case TIOCPKT:
603                 if (*(int *)data) {
604                         if (pt->pt_flags & PF_UCNTL)
605                                 return (EINVAL);
606                         pt->pt_flags |= PF_PKT;
607                 } else
608                         pt->pt_flags &= ~PF_PKT;
609                 return (0);
610
611         case TIOCUCNTL:
612                 if (*(int *)data) {
613                         if (pt->pt_flags & PF_PKT)
614                                 return (EINVAL);
615                         pt->pt_flags |= PF_UCNTL;
616                 } else
617                         pt->pt_flags &= ~PF_UCNTL;
618                 return (0);
619         }
620
621         /*
622          * The rest of the ioctls shouldn't be called until
623          * the slave is open.
624          */
625         if ((tp->t_state & TS_ISOPEN) == 0)
626                 return (EAGAIN);
627
628         switch (cmd) {
629 #ifdef COMPAT_43TTY
630         case TIOCSETP:
631         case TIOCSETN:
632 #endif
633         case TIOCSETD:
634         case TIOCSETA:
635         case TIOCSETAW:
636         case TIOCSETAF:
637                 /*
638                  * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
639                  * ttywflush(tp) will hang if there are characters in
640                  * the outq.
641                  */
642                 ndflush(&tp->t_outq, tp->t_outq.c_cc);
643                 break;
644
645 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
646     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
647         case _IO('t', 95):
648                 ival = IOCPARM_IVAL(data);
649                 data = (caddr_t)&ival;
650                 /* FALLTHROUGH */
651 #endif
652         case TIOCSIG:
653                 if (*(unsigned int *)data >= NSIG ||
654                     *(unsigned int *)data == 0)
655                         return(EINVAL);
656                 if ((tp->t_lflag&NOFLSH) == 0)
657                         ttyflush(tp, FREAD|FWRITE);
658                 if (tp->t_pgrp != NULL) {
659                         PGRP_LOCK(tp->t_pgrp);
660                         pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
661                         PGRP_UNLOCK(tp->t_pgrp);
662                 }
663                 if ((*(unsigned int *)data == SIGINFO) &&
664                     ((tp->t_lflag&NOKERNINFO) == 0))
665                         ttyinfo(tp);
666                 return(0);
667         }
668
669         return (ptsioctl(dev, cmd, data, flag, td));
670 }
671
672 /*ARGSUSED*/
673 static  int
674 ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
675 {
676         struct tty *tp = dev->si_tty;
677         struct ptsc *pt = dev->si_drv1;
678         u_char *cc = tp->t_cc;
679         int stop, error;
680
681         if (cmd == TIOCEXT) {
682                 /*
683                  * When the EXTPROC bit is being toggled, we need
684                  * to send an TIOCPKT_IOCTL if the packet driver
685                  * is turned on.
686                  */
687                 if (*(int *)data) {
688                         if (pt->pt_flags & PF_PKT) {
689                                 pt->pt_send |= TIOCPKT_IOCTL;
690                                 ptcwakeup(tp, FREAD);
691                         }
692                         tp->t_lflag |= EXTPROC;
693                 } else {
694                         if ((tp->t_lflag & EXTPROC) &&
695                             (pt->pt_flags & PF_PKT)) {
696                                 pt->pt_send |= TIOCPKT_IOCTL;
697                                 ptcwakeup(tp, FREAD);
698                         }
699                         tp->t_lflag &= ~EXTPROC;
700                 }
701                 return(0);
702         }
703         error = ttyioctl(dev, cmd, data, flag, td);
704         if (error == ENOTTY) {
705                 if (pt->pt_flags & PF_UCNTL &&
706                     (cmd & ~0xff) == UIOCCMD(0)) {
707                         if (cmd & 0xff) {
708                                 pt->pt_ucntl = (u_char)cmd;
709                                 ptcwakeup(tp, FREAD);
710                         }
711                         return (0);
712                 }
713                 error = ENOTTY;
714         }
715         /*
716          * If external processing and packet mode send ioctl packet.
717          */
718         if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
719                 switch(cmd) {
720                 case TIOCSETA:
721                 case TIOCSETAW:
722                 case TIOCSETAF:
723 #ifdef COMPAT_43TTY
724                 case TIOCSETP:
725                 case TIOCSETN:
726                 case TIOCSETC:
727                 case TIOCSLTC:
728                 case TIOCLBIS:
729                 case TIOCLBIC:
730                 case TIOCLSET:
731 #endif
732                         pt->pt_send |= TIOCPKT_IOCTL;
733                         ptcwakeup(tp, FREAD);
734                         break;
735                 default:
736                         break;
737                 }
738         }
739         stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
740                 && CCEQ(cc[VSTART], CTRL('q'));
741         if (pt->pt_flags & PF_NOSTOP) {
742                 if (stop) {
743                         pt->pt_send &= ~TIOCPKT_NOSTOP;
744                         pt->pt_send |= TIOCPKT_DOSTOP;
745                         pt->pt_flags &= ~PF_NOSTOP;
746                         ptcwakeup(tp, FREAD);
747                 }
748         } else {
749                 if (!stop) {
750                         pt->pt_send &= ~TIOCPKT_DOSTOP;
751                         pt->pt_send |= TIOCPKT_NOSTOP;
752                         pt->pt_flags |= PF_NOSTOP;
753                         ptcwakeup(tp, FREAD);
754                 }
755         }
756         return (error);
757 }
758
759 static void
760 pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
761     struct cdev **dev)
762 {
763         int u;
764
765         if (*dev != NULL)
766                 return;
767         if (bcmp(name, "pty", 3) != 0)
768                 return;
769         if (name[5] != '\0')
770                 return;
771         switch (name[3]) {
772         case 'p': u =   0; break;
773         case 'q': u =  32; break;
774         case 'r': u =  64; break;
775         case 's': u =  96; break;
776         case 'P': u = 128; break;
777         case 'Q': u = 160; break;
778         case 'R': u = 192; break;
779         case 'S': u = 224; break;
780         default: return;
781         }
782         if (name[4] >= '0' && name[4] <= '9')
783                 u += name[4] - '0';
784         else if (name[4] >= 'a' && name[4] <= 'v')
785                 u += name[4] - 'a' + 10;
786         else
787                 return;
788         *dev = make_dev_cred(&ptc_cdevsw, u, cr,
789             UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
790         dev_ref(*dev);
791         (*dev)->si_flags |= SI_CHEAPCLONE;
792         return;
793 }
794
795 static void
796 ptc_drvinit(void *unused)
797 {
798
799         EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
800 }
801
802 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ptc_drvinit,NULL)