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