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