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