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