add support for kernel profiling; add sysctl_struct; eliminate trailing blanks
[unix-history] / usr / src / sys / kern / tty_pty.c
CommitLineData
da7c5cc6 1/*
fccd027d
KB
2 * Copyright (c) 1982, 1986, 1989 The Regents of the University of California.
3 * All rights reserved.
da7c5cc6 4 *
dbf0c423 5 * %sccs.include.redist.c%
fccd027d 6 *
d98fceb7 7 * @(#)tty_pty.c 7.29 (Berkeley) %G%
da7c5cc6 8 */
be4367b3 9
45372428
MT
10/*
11 * Pseudo-teletype Driver
12 * (Actually two drivers, requiring two entries in 'cdevsw')
45372428 13 */
d98fceb7 14#include "pty.h" /* XXX */
647d645f 15
38a01dbe
KB
16#include <sys/param.h>
17#include <sys/systm.h>
18#include <sys/ioctl.h>
19#include <sys/proc.h>
20#include <sys/tty.h>
21#include <sys/conf.h>
22#include <sys/file.h>
23#include <sys/uio.h>
24#include <sys/kernel.h>
25#include <sys/vnode.h>
6a5ca8a0 26#include "tsleep.h"
941944c9 27
b3c8737d 28#if NPTY == 1
5bb90914 29#undef NPTY
c91c01fe 30#define NPTY 32 /* crude XXX */
b3c8737d 31#endif
45372428 32
2bb0bef9 33#define BUFSIZ 100 /* Chunk size iomoved to/from user */
e1d74936 34
45372428 35/*
5bb90914
JB
36 * pts == /dev/tty[pqrs]?
37 * ptc == /dev/pty[pqrs]?
45372428 38 */
d98fceb7 39struct tty pt_tty[NPTY]; /* XXX */
e1d74936 40struct pt_ioctl {
1a954a11 41 int pt_flags;
77f9c500 42 struct selinfo pt_selr, pt_selw;
5bb90914
JB
43 u_char pt_send;
44 u_char pt_ucntl;
6a5ca8a0 45 struct clist pt_ioc;
d98fceb7 46} pt_ioctl[NPTY]; /* XXX */
5bb90914 47int npty = NPTY; /* for pstat -t */
45372428 48
6a5ca8a0
KM
49#define PF_RCOLL 0x0001
50#define PF_WCOLL 0x0002
51#define PF_NBIO 0x0004
52#define PF_PKT 0x0008 /* packet mode */
53#define PF_STOPPED 0x0010 /* user told stopped */
54#define PF_REMOTE 0x0020 /* remote and flow controlled input */
55#define PF_NOSTOP 0x0040
56#define PF_UCNTL 0x0080 /* user control mode */
57#define PF_TIOC 0x0100 /* transparent control mode */
58#define PF_LIOC 0x0200 /* transparent control locked */
59#define PF_WIOC 0x0400 /* waiting for PF_LIOC to clear */
60#define PF_BLOCK 0x0800 /* block writes to slave */
61#define PF_OWAIT 0x1000 /* waiting for PF_BLOCK to clear */
45372428 62
c82bf64c
CT
63void ptsstop __P((struct tty *, int));
64
d98fceb7
CT
65/*
66 * Establish n (or default if n is 1) ptys in the system.
67 *
68 * XXX cdevsw & pstat require the array `pty[]' to be an array
69 */
70void
71ptyattach(n)
72 int n;
73{
74#ifdef notyet
75 char *mem;
76 register u_long ntb;
77#define DEFAULT_NPTY 32
78
79 /* maybe should allow 0 => none? */
80 if (n <= 1)
81 n = DEFAULT_NPTY;
82 ntb = n * sizeof(struct tty);
83 mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl),
84 M_DEVBUF, M_WAITOK);
85 pt_tty = (struct tty *)mem;
86 mem = (char *)ALIGN(mem + ntb);
87 pt_ioctl = (struct pt_ioctl *)mem;
88 npty = n;
89#endif
90}
91
45372428 92/*ARGSUSED*/
f50ba5a1 93ptsopen(dev, flag, devtype, p)
73c77d38 94 dev_t dev;
c82bf64c 95 int flag, devtype;
f50ba5a1 96 struct proc *p;
e1d74936 97{
45372428 98 register struct tty *tp;
5bb90914 99 int error;
45372428 100
d98fceb7 101 if (minor(dev) >= npty)
f21c8fb8 102 return (ENXIO);
45372428 103 tp = &pt_tty[minor(dev)];
941944c9 104 if ((tp->t_state & TS_ISOPEN) == 0) {
7595f992 105 tp->t_state |= TS_WOPEN;
bdda6b91 106 ttychars(tp); /* Set up default chars */
e39f9ea6
MT
107 tp->t_iflag = TTYDEF_IFLAG;
108 tp->t_oflag = TTYDEF_OFLAG;
109 tp->t_lflag = TTYDEF_LFLAG;
110 tp->t_cflag = TTYDEF_CFLAG;
111 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
112 ttsetwater(tp); /* would be done in xxparam() */
b98b2de8 113 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
f21c8fb8 114 return (EBUSY);
e1d74936 115 if (tp->t_oproc) /* Ctrlr still around. */
941944c9
BJ
116 tp->t_state |= TS_CARR_ON;
117 while ((tp->t_state & TS_CARR_ON) == 0) {
118 tp->t_state |= TS_WOPEN;
93080c46 119 if (flag&FNONBLOCK)
e9fe78f8 120 break;
7595f992 121 if (error = ttysleep(tp, (caddr_t)&tp->t_rawq, TTIPRI | PCATCH,
9a418eca
MK
122 ttopen, 0))
123 return (error);
45372428 124 }
c82bf64c 125 error = (*linesw[tp->t_line].l_open)(dev, tp);
5bb90914
JB
126 ptcwakeup(tp, FREAD|FWRITE);
127 return (error);
45372428
MT
128}
129
2b38506d 130ptsclose(dev, flag, mode, p)
73c77d38 131 dev_t dev;
2b38506d
MT
132 int flag, mode;
133 struct proc *p;
941944c9 134{
45372428 135 register struct tty *tp;
d33b3008 136 int err;
45372428
MT
137
138 tp = &pt_tty[minor(dev)];
d33b3008
MT
139 err = (*linesw[tp->t_line].l_close)(tp, flag);
140 err |= ttyclose(tp);
5bb90914 141 ptcwakeup(tp, FREAD|FWRITE);
d33b3008 142 return (err);
6a5ca8a0 143 return (0);
45372428
MT
144}
145
e9fe78f8 146ptsread(dev, uio, flag)
73c77d38 147 dev_t dev;
ae9a0a69 148 struct uio *uio;
c82bf64c 149 int flag;
e1d74936 150{
b98b2de8 151 struct proc *p = curproc;
defdbcd1
BJ
152 register struct tty *tp = &pt_tty[minor(dev)];
153 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
840510a3 154 int error = 0;
defdbcd1
BJ
155
156again:
157 if (pti->pt_flags & PF_REMOTE) {
b98b2de8
MK
158 while (isbackground(p, tp)) {
159 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
160 (p->p_sigmask & sigmask(SIGTTIN)) ||
161 p->p_pgrp->pg_jobc == 0 ||
162 p->p_flag&SPPWAIT)
840510a3 163 return (EIO);
b98b2de8 164 pgsignal(p->p_pgrp, SIGTTIN, 1);
7595f992
MT
165 if (error = ttysleep(tp, (caddr_t)&lbolt,
166 TTIPRI | PCATCH, ttybg, 0))
9a418eca 167 return (error);
941944c9 168 }
5bb90914 169 if (tp->t_canq.c_cc == 0) {
c4ec2128 170 if (flag & IO_NDELAY)
840510a3 171 return (EWOULDBLOCK);
7595f992 172 if (error = ttysleep(tp, (caddr_t)&tp->t_canq,
9a418eca
MK
173 TTIPRI | PCATCH, ttyin, 0))
174 return (error);
defdbcd1
BJ
175 goto again;
176 }
5bb90914
JB
177 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
178 if (ureadc(getc(&tp->t_canq), uio) < 0) {
840510a3 179 error = EFAULT;
ae9a0a69
BJ
180 break;
181 }
5bb90914
JB
182 if (tp->t_canq.c_cc == 1)
183 (void) getc(&tp->t_canq);
184 if (tp->t_canq.c_cc)
840510a3 185 return (error);
defdbcd1
BJ
186 } else
187 if (tp->t_oproc)
e9fe78f8 188 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
5bb90914 189 ptcwakeup(tp, FWRITE);
840510a3 190 return (error);
45372428
MT
191}
192
941944c9
BJ
193/*
194 * Write to pseudo-tty.
195 * Wakeups of controlling tty will happen
196 * indirectly, when tty driver calls ptsstart.
197 */
e9fe78f8 198ptswrite(dev, uio, flag)
73c77d38 199 dev_t dev;
ae9a0a69 200 struct uio *uio;
73699774 201 int flag;
e1d74936 202{
6a5ca8a0
KM
203 register struct tty *tp = &pt_tty[minor(dev)];
204 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
45372428 205
840510a3
BJ
206 if (tp->t_oproc == 0)
207 return (EIO);
6a5ca8a0
KM
208
209 while (pti->pt_flags & PF_BLOCK) {
210 pti->pt_flags |= PF_OWAIT;
211 sleep((caddr_t)pti + 1, TTOPRI);
212 }
213
e9fe78f8 214 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
45372428
MT
215}
216
941944c9
BJ
217/*
218 * Start output on pseudo-tty.
219 * Wake up process selecting or sleeping for input from controlling tty.
220 */
c82bf64c 221void
45372428 222ptsstart(tp)
e1d74936
BJ
223 struct tty *tp;
224{
1e6d24b1 225 register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
e1d74936 226
941944c9 227 if (tp->t_state & TS_TTSTOP)
45372428 228 return;
1e6d24b1
BJ
229 if (pti->pt_flags & PF_STOPPED) {
230 pti->pt_flags &= ~PF_STOPPED;
231 pti->pt_send = TIOCPKT_START;
232 }
5bb90914 233 ptcwakeup(tp, FREAD);
d427b3b2
BJ
234}
235
5bb90914 236ptcwakeup(tp, flag)
d427b3b2 237 struct tty *tp;
c82bf64c 238 int flag;
d427b3b2
BJ
239{
240 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
241
5bb90914 242 if (flag & FREAD) {
77f9c500 243 selwakeup(&pti->pt_selr);
5bb90914
JB
244 wakeup((caddr_t)&tp->t_outq.c_cf);
245 }
246 if (flag & FWRITE) {
77f9c500 247 selwakeup(&pti->pt_selw);
5bb90914 248 wakeup((caddr_t)&tp->t_rawq.c_cf);
e1d74936 249 }
45372428
MT
250}
251
252/*ARGSUSED*/
f50ba5a1
MK
253#ifdef __STDC__
254ptcopen(dev_t dev, int flag, int devtype, struct proc *p)
255#else
256ptcopen(dev, flag, devtype, p)
e1d74936 257 dev_t dev;
f50ba5a1
MK
258 int flag, devtype;
259 struct proc *p;
260#endif
e1d74936 261{
45372428 262 register struct tty *tp;
1a954a11 263 struct pt_ioctl *pti;
45372428 264
d98fceb7 265 if (minor(dev) >= npty)
f21c8fb8 266 return (ENXIO);
45372428 267 tp = &pt_tty[minor(dev)];
f21c8fb8
BJ
268 if (tp->t_oproc)
269 return (EIO);
e1d74936 270 tp->t_oproc = ptsstart;
c82bf64c
CT
271#ifdef sun4c
272 tp->t_stop = ptsstop;
273#endif
f2f3b49b 274 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
44ce8f01 275 tp->t_lflag &= ~EXTPROC;
1a954a11
BJ
276 pti = &pt_ioctl[minor(dev)];
277 pti->pt_flags = 0;
278 pti->pt_send = 0;
5bb90914 279 pti->pt_ucntl = 0;
f21c8fb8 280 return (0);
45372428
MT
281}
282
283ptcclose(dev)
e1d74936
BJ
284 dev_t dev;
285{
45372428
MT
286 register struct tty *tp;
287
288 tp = &pt_tty[minor(dev)];
f2f3b49b 289 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
d3be7d55 290 tp->t_state &= ~TS_CARR_ON;
e1d74936 291 tp->t_oproc = 0; /* mark closed */
bc8d7925 292 tp->t_session = 0;
6a5ca8a0 293 return (0);
d33b3008 294 return (0);
45372428
MT
295}
296
e9fe78f8 297ptcread(dev, uio, flag)
1a954a11 298 dev_t dev;
ae9a0a69 299 struct uio *uio;
c82bf64c 300 int flag;
e1d74936 301{
840510a3 302 register struct tty *tp = &pt_tty[minor(dev)];
5bb90914 303 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
2bb0bef9
SL
304 char buf[BUFSIZ];
305 int error = 0, cc;
45372428 306
5bb90914
JB
307 /*
308 * We want to block until the slave
309 * is open, and there's something to read;
310 * but if we lost the slave or we're NBIO,
311 * then return the appropriate error instead.
312 */
313 for (;;) {
314 if (tp->t_state&TS_ISOPEN) {
315 if (pti->pt_flags&PF_PKT && pti->pt_send) {
8011f5df 316 error = ureadc((int)pti->pt_send, uio);
5bb90914
JB
317 if (error)
318 return (error);
44ce8f01 319 if (pti->pt_send & TIOCPKT_IOCTL) {
21973ccb 320 cc = min(uio->uio_resid,
44ce8f01
MT
321 sizeof(tp->t_termios));
322 uiomove(&tp->t_termios, cc, uio);
323 }
5bb90914
JB
324 pti->pt_send = 0;
325 return (0);
326 }
327 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
8011f5df 328 error = ureadc((int)pti->pt_ucntl, uio);
5bb90914
JB
329 if (error)
330 return (error);
331 pti->pt_ucntl = 0;
332 return (0);
333 }
6a5ca8a0
KM
334 if (pti->pt_flags&PF_TIOC && pti->pt_ioc.c_cc) {
335 if (uio->uio_resid < pti->pt_ioc.c_cc + 1)
336 return (E2BIG);
337 error = ureadc(TIOCPKT_TIOC, uio);
338 while (error == 0 && pti->pt_ioc.c_cc > 0) {
339 cc = q_to_b(&pti->pt_ioc, buf,
340 MIN(pti->pt_ioc.c_cc, BUFSIZ));
341 if (cc <= 0) /* impossible? */
342 break;
343 error = uiomove(buf, cc, UIO_READ, uio);
344 }
345 return (error);
346 }
5bb90914
JB
347 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
348 break;
1a954a11 349 }
2bb0bef9 350 if ((tp->t_state&TS_CARR_ON) == 0)
e9fe78f8 351 return (0); /* EOF */
c4ec2128 352 if (flag & IO_NDELAY)
840510a3 353 return (EWOULDBLOCK);
9a418eca
MK
354 if (error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
355 ttyin, 0))
356 return (error);
8b8271ac 357 }
6a5ca8a0 358 if (pti->pt_flags & (PF_PKT|PF_UCNTL|PF_TIOC))
5bb90914 359 error = ureadc(0, uio);
2bb0bef9 360 while (uio->uio_resid > 0 && error == 0) {
21973ccb 361 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
2bb0bef9 362 if (cc <= 0)
ae9a0a69 363 break;
c4ec2128 364 error = uiomove(buf, cc, uio);
2bb0bef9 365 }
6a5ca8a0
KM
366 if (tp->t_outq.c_cc <= TTLOWAT(tp) && !(pti->pt_flags & PF_BLOCK))
367 ptswake(tp);
840510a3 368 return (error);
45372428
MT
369}
370
c82bf64c 371void
6a5ca8a0
KM
372ptswake(tp)
373 register struct tty *tp;
374{
375 if (tp->t_state&TS_ASLEEP) {
376 tp->t_state &= ~TS_ASLEEP;
377 wakeup((caddr_t)&tp->t_outq);
378 }
379 if (tp->t_wsel) {
380 selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL);
381 tp->t_wsel = 0;
382 tp->t_state &= ~TS_WCOLL;
383 }
384}
385
1a954a11
BJ
386ptsstop(tp, flush)
387 register struct tty *tp;
388 int flush;
389{
390 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
5bb90914 391 int flag;
1a954a11 392
1e6d24b1
BJ
393 /* note: FLUSHREAD and FLUSHWRITE already ok */
394 if (flush == 0) {
395 flush = TIOCPKT_STOP;
396 pti->pt_flags |= PF_STOPPED;
5bb90914 397 } else
1e6d24b1 398 pti->pt_flags &= ~PF_STOPPED;
0a2bd708 399 pti->pt_send |= flush;
5bb90914
JB
400 /* change of perspective */
401 flag = 0;
402 if (flush & FREAD)
403 flag |= FWRITE;
404 if (flush & FWRITE)
405 flag |= FREAD;
9d2a90b1 406 ptcwakeup(tp, flag);
1a954a11
BJ
407}
408
f50ba5a1 409ptcselect(dev, rw, p)
e1d74936 410 dev_t dev;
941944c9 411 int rw;
f50ba5a1 412 struct proc *p;
e1d74936
BJ
413{
414 register struct tty *tp = &pt_tty[minor(dev)];
defdbcd1 415 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
d427b3b2 416 int s;
e1d74936 417
5bb90914 418 if ((tp->t_state&TS_CARR_ON) == 0)
e1d74936 419 return (1);
941944c9
BJ
420 switch (rw) {
421
422 case FREAD:
892f2f35
MK
423 /*
424 * Need to block timeouts (ttrstart).
425 */
426 s = spltty();
7cbd29e4
MK
427 if ((tp->t_state&TS_ISOPEN) &&
428 tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
429 splx(s);
430 return (1);
431 }
892f2f35 432 splx(s);
7cbd29e4
MK
433 /* FALLTHROUGH */
434
435 case 0: /* exceptional */
5bb90914
JB
436 if ((tp->t_state&TS_ISOPEN) &&
437 (pti->pt_flags&PF_PKT && pti->pt_send ||
6a5ca8a0 438 pti->pt_flags&PF_TIOC && pti->pt_ioc.c_cc ||
892f2f35 439 pti->pt_flags&PF_UCNTL && pti->pt_ucntl))
941944c9 440 return (1);
77f9c500 441 selrecord(p, &pti->pt_selr);
d427b3b2 442 break;
941944c9 443
7cbd29e4 444
941944c9 445 case FWRITE:
892f2f35
MK
446 if (tp->t_state&TS_ISOPEN) {
447 if (pti->pt_flags & PF_REMOTE) {
448 if (tp->t_canq.c_cc == 0)
449 return (1);
450 } else {
451 if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
452 return (1);
e39f9ea6 453 if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
892f2f35
MK
454 return (1);
455 }
d427b3b2 456 }
77f9c500 457 selrecord(p, &pti->pt_selw);
d427b3b2 458 break;
7cbd29e4 459
941944c9 460 }
d427b3b2 461 return (0);
e1d74936
BJ
462}
463
e9fe78f8 464ptcwrite(dev, uio, flag)
941944c9 465 dev_t dev;
5bb90914 466 register struct uio *uio;
73699774 467 int flag;
e1d74936 468{
840510a3 469 register struct tty *tp = &pt_tty[minor(dev)];
88b2ff48 470 register u_char *cp;
5bb90914 471 register int cc = 0;
88b2ff48 472 u_char locbuf[BUFSIZ];
941944c9 473 int cnt = 0;
defdbcd1 474 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
840510a3 475 int error = 0;
45372428 476
defdbcd1 477again:
5bb90914
JB
478 if ((tp->t_state&TS_ISOPEN) == 0)
479 goto block;
9d2a90b1 480 if (pti->pt_flags & PF_REMOTE) {
5bb90914
JB
481 if (tp->t_canq.c_cc)
482 goto block;
88b2ff48 483 while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
9d2a90b1 484 if (cc == 0) {
88b2ff48
MK
485 cc = min(uio->uio_resid, BUFSIZ);
486 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
9d2a90b1 487 cp = locbuf;
88b2ff48 488 error = uiomove((caddr_t)cp, cc, uio);
9d2a90b1
JB
489 if (error)
490 return (error);
491 /* check again for safety */
492 if ((tp->t_state&TS_ISOPEN) == 0)
493 return (EIO);
494 }
495 if (cc)
88b2ff48 496 (void) b_to_q((char *)cp, cc, &tp->t_canq);
9d2a90b1 497 cc = 0;
defdbcd1 498 }
5bb90914
JB
499 (void) putc(0, &tp->t_canq);
500 ttwakeup(tp);
501 wakeup((caddr_t)&tp->t_canq);
502 return (0);
503 }
88b2ff48 504 while (uio->uio_resid > 0) {
5bb90914 505 if (cc == 0) {
88b2ff48 506 cc = min(uio->uio_resid, BUFSIZ);
5bb90914 507 cp = locbuf;
88b2ff48 508 error = uiomove((caddr_t)cp, cc, uio);
5bb90914
JB
509 if (error)
510 return (error);
511 /* check again for safety */
512 if ((tp->t_state&TS_ISOPEN) == 0)
513 return (EIO);
514 }
9d2a90b1
JB
515 while (cc > 0) {
516 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
e39f9ea6 517 (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
9d2a90b1
JB
518 wakeup((caddr_t)&tp->t_rawq);
519 goto block;
520 }
88b2ff48 521 (*linesw[tp->t_line].l_rint)(*cp++, tp);
941944c9 522 cnt++;
9d2a90b1 523 cc--;
45372428 524 }
5bb90914
JB
525 cc = 0;
526 }
527 return (0);
528block:
529 /*
9d2a90b1
JB
530 * Come here to wait for slave to open, for space
531 * in outq, or space in rawq.
5bb90914
JB
532 */
533 if ((tp->t_state&TS_CARR_ON) == 0)
534 return (EIO);
88b2ff48
MK
535 if (flag & IO_NDELAY) {
536 /* adjust for data copied in but not written */
5bb90914 537 uio->uio_resid += cc;
9d2a90b1
JB
538 if (cnt == 0)
539 return (EWOULDBLOCK);
5bb90914
JB
540 return (0);
541 }
9a418eca 542 if (error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
88b2ff48
MK
543 ttyout, 0)) {
544 /* adjust for data copied in but not written */
545 uio->uio_resid += cc;
9a418eca 546 return (error);
88b2ff48 547 }
5bb90914 548 goto again;
45372428
MT
549}
550
45372428 551/*ARGSUSED*/
c82bf64c 552ptyioctl(dev, cmd, data, flag, p)
e1d74936 553 dev_t dev;
c82bf64c
CT
554 int cmd;
555 caddr_t data;
556 int flag;
557 struct proc *p;
e1d74936 558{
0a2bd708
BJ
559 register struct tty *tp = &pt_tty[minor(dev)];
560 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
e39f9ea6 561 register u_char *cc = tp->t_cc;
5bb90914 562 int stop, error;
45372428 563
f2f3b49b
MK
564 /*
565 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
566 * ttywflush(tp) will hang if there are characters in the outq.
567 */
44ce8f01
MT
568 if (cmd == TIOCEXT) {
569 /*
570 * When the EXTPROC bit is being toggled, we need
571 * to send an TIOCPKT_IOCTL if the packet driver
572 * is turned on.
573 */
574 if (*(int *)data) {
575 if (pti->pt_flags & PF_PKT) {
576 pti->pt_send |= TIOCPKT_IOCTL;
577 ptcwakeup(tp);
578 }
579 tp->t_lflag |= EXTPROC;
580 } else {
581 if ((tp->t_state & EXTPROC) &&
582 (pti->pt_flags & PF_PKT)) {
583 pti->pt_send |= TIOCPKT_IOCTL;
584 ptcwakeup(tp);
585 }
586 tp->t_lflag &= ~EXTPROC;
587 }
588 return(0);
589 } else
6a5ca8a0
KM
590 if (cdevsw[major(dev)].d_open == ptcopen) {
591 if ((cmd & 0xffff) == (TIOCIOANS(0) & 0xffff)) {
592 if (!(pti->pt_flags & PF_LIOC) || pti->pt_ioc.c_cc)
593 return (EINVAL);
594 (void) b_to_q(data, IOCPARM_LEN(cmd), &pti->pt_ioc);
595 wakeup((caddr_t)&pti->pt_ioc);
596 return (0);
597 }
4b72e2f9
SL
598 switch (cmd) {
599
1b2a00b5
MT
600 case TIOCGPGRP:
601 /*
602 * We aviod calling ttioctl on the controller since,
603 * in that case, tp must be the controlling terminal.
604 */
605 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
606 return (0);
607
4b72e2f9 608 case TIOCPKT:
5bb90914
JB
609 if (*(int *)data) {
610 if (pti->pt_flags & PF_UCNTL)
611 return (EINVAL);
1a954a11 612 pti->pt_flags |= PF_PKT;
5bb90914 613 } else
1a954a11 614 pti->pt_flags &= ~PF_PKT;
f21c8fb8 615 return (0);
4b72e2f9 616
5bb90914
JB
617 case TIOCUCNTL:
618 if (*(int *)data) {
619 if (pti->pt_flags & PF_PKT)
620 return (EINVAL);
621 pti->pt_flags |= PF_UCNTL;
622 } else
623 pti->pt_flags &= ~PF_UCNTL;
624 return (0);
625
6a5ca8a0
KM
626 case TIOCTIOC:
627 if (*(int *)data) {
628 if (pti->pt_flags & PF_UCNTL)
629 return (EINVAL);
630 pti->pt_flags |= PF_TIOC;
631 } else {
632 pti->pt_flags &= ~(PF_TIOC|PF_LIOC|PF_WIOC);
633 while (pti->pt_ioc.c_cc)
634 (void) getc(&pti->pt_ioc);
635 wakeup((caddr_t)&pti->pt_ioc);
636 }
637 return (0);
638
639 case TIOCBLK:
640 if (*(int *)data)
641 pti->pt_flags |= PF_BLOCK;
642 else {
643 if (pti->pt_flags & PF_OWAIT)
644 wakeup((caddr_t)pti + 1);
645 pti->pt_flags &= ~(PF_BLOCK|PF_OWAIT);
646 ptswake(tp);
647 }
648 return (0);
649
4b72e2f9
SL
650 case TIOCREMOTE:
651 if (*(int *)data)
defdbcd1
BJ
652 pti->pt_flags |= PF_REMOTE;
653 else
654 pti->pt_flags &= ~PF_REMOTE;
88a7a62a 655 ttyflush(tp, FREAD|FWRITE);
f21c8fb8 656 return (0);
4b72e2f9 657
c82bf64c 658#ifdef COMPAT_43
6a5ca8a0
KM
659 case FIONREAD:
660 *(int *)data = tp->t_outq.c_cc;
661 return (0);
662
e39f9ea6 663 case TIOCSETP:
f2f3b49b 664 case TIOCSETN:
c82bf64c 665#endif
f2f3b49b 666 case TIOCSETD:
e39f9ea6
MT
667 case TIOCSETA:
668 case TIOCSETAW:
669 case TIOCSETAF:
c82bf64c 670 ndflush(&tp->t_outq, tp->t_outq.c_cc);
4b72e2f9 671 break;
44ce8f01
MT
672
673 case TIOCSIG:
674 if (*(unsigned int *)data >= NSIG)
675 return(EINVAL);
676 if ((tp->t_lflag&NOFLSH) == 0)
677 ttyflush(tp, FREAD|FWRITE);
3ae82c34
PB
678 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
679 if ((*(unsigned int *)data == SIGINFO) &&
680 ((tp->t_lflag&NOKERNINFO) == 0))
681 ttyinfo(tp);
44ce8f01 682 return(0);
8b8271ac 683 }
6a5ca8a0
KM
684 } else if (pti->pt_flags & PF_TIOC) {
685 while (pti->pt_flags & PF_LIOC) {
686 pti->pt_flags |= PF_WIOC;
687 switch (tsleep((caddr_t)&pti->pt_flags,TTIPRI-1,5*hz)) {
688 case TS_OK:
689 continue;
690 case TS_SIG:
691 case TS_TIME:
692 return (EBUSY);
693 }
694 }
695 pti->pt_flags |= PF_LIOC | PF_BLOCK;
696 while (pti->pt_ioc.c_cc)
697 (void) getc(&pti->pt_ioc);
698 (void) b_to_q(&cmd, sizeof cmd, &pti->pt_ioc);
699 if (cmd & IOC_IN)
700 (void) b_to_q(data, IOCPARM_LEN(cmd), &pti->pt_ioc);
701 ptcwakeup(tp, FREAD);
702 switch (tsleep((caddr_t)&pti->pt_ioc, TTIPRI-1, 5*hz)) {
703 case TS_SIG:
704 case TS_TIME:
705 while (pti->pt_ioc.c_cc)
706 (void) getc(&pti->pt_ioc);
707 if (pti->pt_flags & PF_WIOC)
708 wakeup((caddr_t)&pti->pt_flags);
709 if (pti->pt_flags & PF_OWAIT)
710 wakeup((caddr_t)pti + 1);
711 pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT);
712 ptswake(tp);
713 return (EBUSY);
714 case TS_OK:
715 break;
716 }
717 if (pti->pt_ioc.c_cc == 0) {
718 if (pti->pt_flags & PF_WIOC)
719 wakeup((caddr_t)&pti->pt_flags);
720 if (pti->pt_flags & PF_OWAIT)
721 wakeup((caddr_t)pti + 1);
722 pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT);
723 ptswake(tp);
724 goto doioctl;
725 }
726 if (q_to_b(&pti->pt_ioc, &error, sizeof error) != sizeof error)
727 error = EINVAL;
728 if (error == 0 && cmd & IOC_OUT) {
729 if (IOCPARM_LEN(cmd) != pti->pt_ioc.c_cc)
730 error = EINVAL;
731 else
732 (void) q_to_b(&pti->pt_ioc, data,
733 pti->pt_ioc.c_cc);
734 }
735 while (pti->pt_ioc.c_cc)
736 (void) getc(&pti->pt_ioc);
737 if (pti->pt_flags & PF_WIOC)
738 wakeup((caddr_t)&pti->pt_flags);
739 if (pti->pt_flags & PF_OWAIT)
740 wakeup((caddr_t)pti + 1);
741 pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT);
742 ptswake(tp);
743 return (error);
744 }
745
746 doioctl:
c82bf64c 747 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
e39f9ea6
MT
748 if (error < 0)
749 error = ttioctl(tp, cmd, data, flag);
6a5ca8a0 750
5bb90914
JB
751 if (error < 0) {
752 if (pti->pt_flags & PF_UCNTL &&
97d7430f 753 (cmd & ~0xff) == UIOCCMD(0)) {
5bb90914
JB
754 if (cmd & 0xff) {
755 pti->pt_ucntl = (u_char)cmd;
756 ptcwakeup(tp, FREAD);
757 }
758 return (0);
759 }
f21c8fb8 760 error = ENOTTY;
5bb90914 761 }
44ce8f01
MT
762 /*
763 * If external processing and packet mode send ioctl packet.
764 */
765 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
766 switch(cmd) {
767 case TIOCSETA:
768 case TIOCSETAW:
769 case TIOCSETAF:
c82bf64c 770#ifdef COMPAT_43
44ce8f01
MT
771 case TIOCSETP:
772 case TIOCSETN:
c82bf64c
CT
773#endif
774#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
44ce8f01
MT
775 case TIOCSETC:
776 case TIOCSLTC:
777 case TIOCLBIS:
778 case TIOCLBIC:
779 case TIOCLSET:
780#endif
781 pti->pt_send |= TIOCPKT_IOCTL;
782 default:
783 break;
784 }
785 }
e39f9ea6
MT
786 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
787 && CCEQ(cc[VSTART], CTRL('q'));
0a2bd708
BJ
788 if (pti->pt_flags & PF_NOSTOP) {
789 if (stop) {
b31eb1ff 790 pti->pt_send &= ~TIOCPKT_NOSTOP;
0a2bd708
BJ
791 pti->pt_send |= TIOCPKT_DOSTOP;
792 pti->pt_flags &= ~PF_NOSTOP;
5bb90914 793 ptcwakeup(tp, FREAD);
0a2bd708
BJ
794 }
795 } else {
5bb90914 796 if (!stop) {
0a2bd708
BJ
797 pti->pt_send &= ~TIOCPKT_DOSTOP;
798 pti->pt_send |= TIOCPKT_NOSTOP;
799 pti->pt_flags |= PF_NOSTOP;
5bb90914 800 ptcwakeup(tp, FREAD);
0a2bd708
BJ
801 }
802 }
f21c8fb8 803 return (error);
45372428 804}