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