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