]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/kern/tty_pty.c
Free tty struct after last close. This should fix the pty-leak by numbers.
[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         if (pt->pt_tty->t_refcnt > 1)
175                 return;
176         pt->pt_tty->t_dev = NULL;
177         ttyrel(pt->pt_tty);
178         pt->pt_tty = NULL;
179         destroy_dev(pt->devs);
180         pt->devs = NULL;
181 }
182
183 static void
184 pty_maybe_destroy_slave(struct ptsc *pt)
185 {
186
187         if (pt->pt_devc_open == 0 && pt->pt_devs_open == 0)
188                 pty_destroy_slave(pt);
189 }
190
191 /*ARGSUSED*/
192 static  int
193 ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
194 {
195         struct tty *tp;
196         int error;
197         struct ptsc *pt;
198
199         if (!dev->si_drv1)
200                 return(ENXIO);
201         pt = dev->si_drv1;
202         tp = dev->si_tty;
203
204         if ((tp->t_state & TS_ISOPEN) == 0) {
205                 ttyinitmode(tp, 1, 0);
206         } else if (tp->t_state & TS_XCLUDE && suser(td))
207                 return (EBUSY);
208         else if (pt->pt_prison != td->td_ucred->cr_prison && suser(td))
209                 return (EBUSY);
210         if (tp->t_oproc)                        /* Ctrlr still around. */
211                 (void)ttyld_modem(tp, 1);
212         while ((tp->t_state & TS_CARR_ON) == 0) {
213                 if (flag&FNONBLOCK)
214                         break;
215                 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
216                                  "ptsopn", 0);
217                 if (error)
218                         return (error);
219         }
220         error = ttyld_open(tp, dev);
221         if (error == 0) {
222                 ptcwakeup(tp, FREAD|FWRITE);
223                 pt->pt_devs_open = 1;
224         } else
225                 pty_maybe_destroy_slave(pt);
226         return (error);
227 }
228
229 static  int
230 ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
231 {
232         struct ptsc *pti;
233         struct tty *tp;
234         int err;
235
236         tp = dev->si_tty;
237         pti = dev->si_drv1;
238
239         KASSERT(dev == pti->devs, ("ptsclose: dev != pti->devs"));
240
241         err = ttyld_close(tp, flag);
242         (void) tty_close(tp);
243
244         pti->pt_devs_open = 0;
245         pty_maybe_destroy_slave(pti);
246
247         return (err);
248 }
249
250 static  int
251 ptsread(struct cdev *dev, struct uio *uio, int flag)
252 {
253         struct tty *tp = dev->si_tty;
254         int error = 0;
255
256         if (tp->t_oproc)
257                 error = ttyld_read(tp, uio, flag);
258         ptcwakeup(tp, FWRITE);
259         return (error);
260 }
261
262 /*
263  * Write to pseudo-tty.
264  * Wakeups of controlling tty will happen
265  * indirectly, when tty driver calls ptsstart.
266  */
267 static  int
268 ptswrite(struct cdev *dev, struct uio *uio, int flag)
269 {
270         struct tty *tp;
271
272         tp = dev->si_tty;
273         if (tp->t_oproc == 0)
274                 return (EIO);
275         return (ttyld_write(tp, uio, flag));
276 }
277
278 /*
279  * Start output on pseudo-tty.
280  * Wake up process selecting or sleeping for input from controlling tty.
281  */
282 static void
283 ptsstart(struct tty *tp)
284 {
285         struct ptsc *pt = tp->t_sc;
286
287         if (tp->t_state & TS_TTSTOP)
288                 return;
289         if (pt->pt_flags & PF_STOPPED) {
290                 pt->pt_flags &= ~PF_STOPPED;
291                 pt->pt_send = TIOCPKT_START;
292         }
293         ptcwakeup(tp, FREAD);
294 }
295
296 static void
297 ptcwakeup(struct tty *tp, int flag)
298 {
299         struct ptsc *pt = tp->t_sc;
300
301         if (flag & FREAD) {
302                 selwakeuppri(&pt->pt_selr, TTIPRI);
303                 wakeup(TSA_PTC_READ(tp));
304         }
305         if (flag & FWRITE) {
306                 selwakeuppri(&pt->pt_selw, TTOPRI);
307                 wakeup(TSA_PTC_WRITE(tp));
308         }
309 }
310
311 static  int
312 ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
313 {
314         struct tty *tp;
315         struct ptsc *pt;
316
317         if (!dev->si_drv1)
318                 ptyinit(dev, td);
319         if (!dev->si_drv1)
320                 return(ENXIO);
321
322         pt = dev->si_drv1;
323         /*
324          * In case we have destroyed the struct tty at the last connect time,
325          * we need to recreate it.
326          */
327         if (pt->pt_tty == NULL) {
328                 pt->pt_tty = ttyalloc();
329                 pt->pt_tty->t_sc = pt;
330                 dev->si_tty = pt->pt_tty;
331                 pty_create_slave(td->td_ucred, pt, minor(dev));
332         }
333         tp = dev->si_tty;
334
335         if (tp->t_oproc)
336                 return (EIO);
337         tp->t_timeout = -1;
338         tp->t_oproc = ptsstart;
339         tp->t_stop = ptsstop;
340         (void)ttyld_modem(tp, 1);
341         tp->t_lflag &= ~EXTPROC;
342         pt->pt_prison = td->td_ucred->cr_prison;
343         pt->pt_flags = 0;
344         pt->pt_send = 0;
345         pt->pt_ucntl = 0;
346
347         if (!pt->devs)
348                 pty_create_slave(td->td_ucred, pt, minor(dev));
349         pt->pt_devc_open = 1;
350
351         return (0);
352 }
353
354 static  int
355 ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
356 {
357         struct ptsc *pti = dev->si_drv1;
358         struct tty *tp;
359
360         tp = dev->si_tty;
361         (void)ttyld_modem(tp, 0);
362
363         /*
364          * XXX MDMBUF makes no sense for ptys but would inhibit the above
365          * l_modem().  CLOCAL makes sense but isn't supported.   Special
366          * l_modem()s that ignore carrier drop make no sense for ptys but
367          * may be in use because other parts of the line discipline make
368          * sense for ptys.  Recover by doing everything that a normal
369          * ttymodem() would have done except for sending a SIGHUP.
370          */
371         if (tp->t_state & TS_ISOPEN) {
372                 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
373                 tp->t_state |= TS_ZOMBIE;
374                 ttyflush(tp, FREAD | FWRITE);
375         }
376
377         tp->t_oproc = 0;                /* mark closed */
378         pti->pt_devc_open = 0;
379         pty_maybe_destroy_slave(pti);
380         return (0);
381 }
382
383 static  int
384 ptcread(struct cdev *dev, struct uio *uio, int flag)
385 {
386         struct tty *tp = dev->si_tty;
387         struct ptsc *pt = dev->si_drv1;
388         char buf[BUFSIZ];
389         int error = 0, cc;
390
391         /*
392          * We want to block until the slave
393          * is open, and there's something to read;
394          * but if we lost the slave or we're NBIO,
395          * then return the appropriate error instead.
396          */
397         for (;;) {
398                 if (tp->t_state&TS_ISOPEN) {
399                         if (pt->pt_flags&PF_PKT && pt->pt_send) {
400                                 error = ureadc((int)pt->pt_send, uio);
401                                 if (error)
402                                         return (error);
403                                 if (pt->pt_send & TIOCPKT_IOCTL) {
404                                         cc = min(uio->uio_resid,
405                                                 sizeof(tp->t_termios));
406                                         uiomove(&tp->t_termios, cc, uio);
407                                 }
408                                 pt->pt_send = 0;
409                                 return (0);
410                         }
411                         if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
412                                 error = ureadc((int)pt->pt_ucntl, uio);
413                                 if (error)
414                                         return (error);
415                                 pt->pt_ucntl = 0;
416                                 return (0);
417                         }
418                         if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
419                                 break;
420                 }
421                 if ((tp->t_state & TS_CONNECTED) == 0)
422                         return (0);     /* EOF */
423                 if (flag & O_NONBLOCK)
424                         return (EWOULDBLOCK);
425                 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
426                 if (error)
427                         return (error);
428         }
429         if (pt->pt_flags & (PF_PKT|PF_UCNTL))
430                 error = ureadc(0, uio);
431         while (uio->uio_resid > 0 && error == 0) {
432                 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
433                 if (cc <= 0)
434                         break;
435                 error = uiomove(buf, cc, uio);
436         }
437         ttwwakeup(tp);
438         return (error);
439 }
440
441 static  void
442 ptsstop(struct tty *tp, int flush)
443 {
444         struct ptsc *pt = tp->t_sc;
445         int flag;
446
447         /* note: FLUSHREAD and FLUSHWRITE already ok */
448         if (flush == 0) {
449                 flush = TIOCPKT_STOP;
450                 pt->pt_flags |= PF_STOPPED;
451         } else
452                 pt->pt_flags &= ~PF_STOPPED;
453         pt->pt_send |= flush;
454         /* change of perspective */
455         flag = 0;
456         if (flush & FREAD)
457                 flag |= FWRITE;
458         if (flush & FWRITE)
459                 flag |= FREAD;
460         ptcwakeup(tp, flag);
461 }
462
463 static  int
464 ptcpoll(struct cdev *dev, int events, struct thread *td)
465 {
466         struct tty *tp = dev->si_tty;
467         struct ptsc *pt = dev->si_drv1;
468         int revents = 0;
469         int s;
470
471         if ((tp->t_state & TS_CONNECTED) == 0)
472                 return (events & 
473                    (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
474
475         /*
476          * Need to block timeouts (ttrstart).
477          */
478         s = spltty();
479
480         if (events & (POLLIN | POLLRDNORM))
481                 if ((tp->t_state & TS_ISOPEN) &&
482                     ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
483                      ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
484                      ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
485                         revents |= events & (POLLIN | POLLRDNORM);
486
487         if (events & (POLLOUT | POLLWRNORM))
488                 if (tp->t_state & TS_ISOPEN &&
489                     (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
490                       (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
491                         revents |= events & (POLLOUT | POLLWRNORM);
492
493         if (events & POLLHUP)
494                 if ((tp->t_state & TS_CARR_ON) == 0)
495                         revents |= POLLHUP;
496
497         if (revents == 0) {
498                 if (events & (POLLIN | POLLRDNORM))
499                         selrecord(td, &pt->pt_selr);
500
501                 if (events & (POLLOUT | POLLWRNORM))
502                         selrecord(td, &pt->pt_selw);
503         }
504         splx(s);
505
506         return (revents);
507 }
508
509 static  int
510 ptcwrite(struct cdev *dev, struct uio *uio, int flag)
511 {
512         struct tty *tp = dev->si_tty;
513         u_char *cp = 0;
514         int cc = 0;
515         u_char locbuf[BUFSIZ];
516         int cnt = 0;
517         int error = 0;
518
519 again:
520         if ((tp->t_state&TS_ISOPEN) == 0)
521                 goto block;
522         while (uio->uio_resid > 0 || cc > 0) {
523                 if (cc == 0) {
524                         cc = min(uio->uio_resid, BUFSIZ);
525                         cp = locbuf;
526                         error = uiomove(cp, cc, uio);
527                         if (error)
528                                 return (error);
529                         /* check again for safety */
530                         if ((tp->t_state & TS_ISOPEN) == 0) {
531                                 /* adjust for data copied in but not written */
532                                 uio->uio_resid += cc;
533                                 return (EIO);
534                         }
535                 }
536                 while (cc > 0) {
537                         if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
538                            (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
539                                 wakeup(TSA_HUP_OR_INPUT(tp));
540                                 goto block;
541                         }
542                         ttyld_rint(tp, *cp++);
543                         cnt++;
544                         cc--;
545                 }
546                 cc = 0;
547         }
548         return (0);
549 block:
550         /*
551          * Come here to wait for slave to open, for space
552          * in outq, or space in rawq, or an empty canq.
553          */
554         if ((tp->t_state & TS_CONNECTED) == 0) {
555                 /* adjust for data copied in but not written */
556                 uio->uio_resid += cc;
557                 return (EIO);
558         }
559         if (flag & O_NONBLOCK) {
560                 /* adjust for data copied in but not written */
561                 uio->uio_resid += cc;
562                 if (cnt == 0)
563                         return (EWOULDBLOCK);
564                 return (0);
565         }
566         error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
567         if (error) {
568                 /* adjust for data copied in but not written */
569                 uio->uio_resid += cc;
570                 return (error);
571         }
572         goto again;
573 }
574
575 /*ARGSUSED*/
576 static  int
577 ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
578 {
579         struct tty *tp = dev->si_tty;
580         struct ptsc *pt = dev->si_drv1;
581 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
582     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
583         int ival;
584 #endif
585
586         switch (cmd) {
587
588         case TIOCGPGRP:
589                 /*
590                  * We avoid calling ttioctl on the controller since,
591                  * in that case, tp must be the controlling terminal.
592                  */
593                 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
594                 return (0);
595
596         case TIOCPKT:
597                 if (*(int *)data) {
598                         if (pt->pt_flags & PF_UCNTL)
599                                 return (EINVAL);
600                         pt->pt_flags |= PF_PKT;
601                 } else
602                         pt->pt_flags &= ~PF_PKT;
603                 return (0);
604
605         case TIOCUCNTL:
606                 if (*(int *)data) {
607                         if (pt->pt_flags & PF_PKT)
608                                 return (EINVAL);
609                         pt->pt_flags |= PF_UCNTL;
610                 } else
611                         pt->pt_flags &= ~PF_UCNTL;
612                 return (0);
613         }
614
615         /*
616          * The rest of the ioctls shouldn't be called until
617          * the slave is open.
618          */
619         if ((tp->t_state & TS_ISOPEN) == 0)
620                 return (EAGAIN);
621
622         switch (cmd) {
623 #ifdef COMPAT_43TTY
624         case TIOCSETP:
625         case TIOCSETN:
626 #endif
627         case TIOCSETD:
628         case TIOCSETA:
629         case TIOCSETAW:
630         case TIOCSETAF:
631                 /*
632                  * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
633                  * ttywflush(tp) will hang if there are characters in
634                  * the outq.
635                  */
636                 ndflush(&tp->t_outq, tp->t_outq.c_cc);
637                 break;
638
639 #if defined(COMPAT_FREEBSD6) || defined(COMPAT_FREEBSD5) || \
640     defined(COMPAT_FREEBSD4) || defined(COMPAT_43)
641         case _IO('t', 95):
642                 ival = IOCPARM_IVAL(data);
643                 data = (caddr_t)&ival;
644                 /* FALLTHROUGH */
645 #endif
646         case TIOCSIG:
647                 if (*(unsigned int *)data >= NSIG ||
648                     *(unsigned int *)data == 0)
649                         return(EINVAL);
650                 if ((tp->t_lflag&NOFLSH) == 0)
651                         ttyflush(tp, FREAD|FWRITE);
652                 if (tp->t_pgrp != NULL) {
653                         PGRP_LOCK(tp->t_pgrp);
654                         pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
655                         PGRP_UNLOCK(tp->t_pgrp);
656                 }
657                 if ((*(unsigned int *)data == SIGINFO) &&
658                     ((tp->t_lflag&NOKERNINFO) == 0))
659                         ttyinfo(tp);
660                 return(0);
661         }
662
663         return (ptsioctl(dev, cmd, data, flag, td));
664 }
665
666 /*ARGSUSED*/
667 static  int
668 ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
669 {
670         struct tty *tp = dev->si_tty;
671         struct ptsc *pt = dev->si_drv1;
672         u_char *cc = tp->t_cc;
673         int stop, error;
674
675         if (cmd == TIOCEXT) {
676                 /*
677                  * When the EXTPROC bit is being toggled, we need
678                  * to send an TIOCPKT_IOCTL if the packet driver
679                  * is turned on.
680                  */
681                 if (*(int *)data) {
682                         if (pt->pt_flags & PF_PKT) {
683                                 pt->pt_send |= TIOCPKT_IOCTL;
684                                 ptcwakeup(tp, FREAD);
685                         }
686                         tp->t_lflag |= EXTPROC;
687                 } else {
688                         if ((tp->t_lflag & EXTPROC) &&
689                             (pt->pt_flags & PF_PKT)) {
690                                 pt->pt_send |= TIOCPKT_IOCTL;
691                                 ptcwakeup(tp, FREAD);
692                         }
693                         tp->t_lflag &= ~EXTPROC;
694                 }
695                 return(0);
696         }
697         error = ttyioctl(dev, cmd, data, flag, td);
698         if (error == ENOTTY) {
699                 if (pt->pt_flags & PF_UCNTL &&
700                     (cmd & ~0xff) == UIOCCMD(0)) {
701                         if (cmd & 0xff) {
702                                 pt->pt_ucntl = (u_char)cmd;
703                                 ptcwakeup(tp, FREAD);
704                         }
705                         return (0);
706                 }
707                 error = ENOTTY;
708         }
709         /*
710          * If external processing and packet mode send ioctl packet.
711          */
712         if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
713                 switch(cmd) {
714                 case TIOCSETA:
715                 case TIOCSETAW:
716                 case TIOCSETAF:
717 #ifdef COMPAT_43TTY
718                 case TIOCSETP:
719                 case TIOCSETN:
720                 case TIOCSETC:
721                 case TIOCSLTC:
722                 case TIOCLBIS:
723                 case TIOCLBIC:
724                 case TIOCLSET:
725 #endif
726                         pt->pt_send |= TIOCPKT_IOCTL;
727                         ptcwakeup(tp, FREAD);
728                         break;
729                 default:
730                         break;
731                 }
732         }
733         stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
734                 && CCEQ(cc[VSTART], CTRL('q'));
735         if (pt->pt_flags & PF_NOSTOP) {
736                 if (stop) {
737                         pt->pt_send &= ~TIOCPKT_NOSTOP;
738                         pt->pt_send |= TIOCPKT_DOSTOP;
739                         pt->pt_flags &= ~PF_NOSTOP;
740                         ptcwakeup(tp, FREAD);
741                 }
742         } else {
743                 if (!stop) {
744                         pt->pt_send &= ~TIOCPKT_DOSTOP;
745                         pt->pt_send |= TIOCPKT_NOSTOP;
746                         pt->pt_flags |= PF_NOSTOP;
747                         ptcwakeup(tp, FREAD);
748                 }
749         }
750         return (error);
751 }
752
753 static void
754 pty_clone(void *arg, struct ucred *cr, char *name, int namelen,
755     struct cdev **dev)
756 {
757         int u;
758
759         if (*dev != NULL)
760                 return;
761         if (bcmp(name, "pty", 3) != 0)
762                 return;
763         if (name[5] != '\0')
764                 return;
765         switch (name[3]) {
766         case 'p': u =   0; break;
767         case 'q': u =  32; break;
768         case 'r': u =  64; break;
769         case 's': u =  96; break;
770         case 'P': u = 128; break;
771         case 'Q': u = 160; break;
772         case 'R': u = 192; break;
773         case 'S': u = 224; break;
774         default: return;
775         }
776         if (name[4] >= '0' && name[4] <= '9')
777                 u += name[4] - '0';
778         else if (name[4] >= 'a' && name[4] <= 'v')
779                 u += name[4] - 'a' + 10;
780         else
781                 return;
782         *dev = make_dev_cred(&ptc_cdevsw, u, cr,
783             UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
784         dev_ref(*dev);
785         (*dev)->si_flags |= SI_CHEAPCLONE;
786         return;
787 }
788
789 static void
790 ptc_drvinit(void *unused)
791 {
792
793         EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
794 }
795
796 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE,ptc_drvinit,NULL)