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