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