Commit | Line | Data |
---|---|---|
da7c5cc6 | 1 | /* |
fccd027d KB |
2 | * Copyright (c) 1982, 1986, 1989 The Regents of the University of California. |
3 | * All rights reserved. | |
da7c5cc6 | 4 | * |
fccd027d KB |
5 | * Redistribution and use in source and binary forms are permitted |
6 | * provided that the above copyright notice and this paragraph are | |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
16 | * | |
17 | * @(#)tty_pty.c 7.7 (Berkeley) %G% | |
da7c5cc6 | 18 | */ |
be4367b3 | 19 | |
45372428 MT |
20 | /* |
21 | * Pseudo-teletype Driver | |
22 | * (Actually two drivers, requiring two entries in 'cdevsw') | |
45372428 | 23 | */ |
647d645f MT |
24 | #include "pty.h" |
25 | ||
f12183e4 | 26 | #if NPTY > 0 |
94368568 JB |
27 | #include "param.h" |
28 | #include "systm.h" | |
29 | #include "ioctl.h" | |
30 | #include "tty.h" | |
94368568 JB |
31 | #include "user.h" |
32 | #include "conf.h" | |
33 | #include "file.h" | |
34 | #include "proc.h" | |
35 | #include "uio.h" | |
36 | #include "kernel.h" | |
c4ec2128 | 37 | #include "vnode.h" |
6a5ca8a0 | 38 | #include "tsleep.h" |
941944c9 | 39 | |
b3c8737d | 40 | #if NPTY == 1 |
5bb90914 | 41 | #undef NPTY |
c91c01fe | 42 | #define NPTY 32 /* crude XXX */ |
b3c8737d | 43 | #endif |
45372428 | 44 | |
2bb0bef9 | 45 | #define BUFSIZ 100 /* Chunk size iomoved to/from user */ |
e1d74936 | 46 | |
45372428 | 47 | /* |
5bb90914 JB |
48 | * pts == /dev/tty[pqrs]? |
49 | * ptc == /dev/pty[pqrs]? | |
45372428 | 50 | */ |
e1d74936 BJ |
51 | struct tty pt_tty[NPTY]; |
52 | struct pt_ioctl { | |
1a954a11 | 53 | int pt_flags; |
1a954a11 | 54 | struct proc *pt_selr, *pt_selw; |
5bb90914 JB |
55 | u_char pt_send; |
56 | u_char pt_ucntl; | |
6a5ca8a0 | 57 | struct clist pt_ioc; |
e1d74936 | 58 | } pt_ioctl[NPTY]; |
5bb90914 | 59 | int npty = NPTY; /* for pstat -t */ |
45372428 | 60 | |
e9fe78f8 MT |
61 | int ptydebug = 0; |
62 | ||
6a5ca8a0 KM |
63 | #define PF_RCOLL 0x0001 |
64 | #define PF_WCOLL 0x0002 | |
65 | #define PF_NBIO 0x0004 | |
66 | #define PF_PKT 0x0008 /* packet mode */ | |
67 | #define PF_STOPPED 0x0010 /* user told stopped */ | |
68 | #define PF_REMOTE 0x0020 /* remote and flow controlled input */ | |
69 | #define PF_NOSTOP 0x0040 | |
70 | #define PF_UCNTL 0x0080 /* user control mode */ | |
71 | #define PF_TIOC 0x0100 /* transparent control mode */ | |
72 | #define PF_LIOC 0x0200 /* transparent control locked */ | |
73 | #define PF_WIOC 0x0400 /* waiting for PF_LIOC to clear */ | |
74 | #define PF_BLOCK 0x0800 /* block writes to slave */ | |
75 | #define PF_OWAIT 0x1000 /* waiting for PF_BLOCK to clear */ | |
45372428 MT |
76 | |
77 | /*ARGSUSED*/ | |
78 | ptsopen(dev, flag) | |
73c77d38 | 79 | dev_t dev; |
e1d74936 | 80 | { |
45372428 | 81 | register struct tty *tp; |
5bb90914 | 82 | int error; |
45372428 | 83 | |
5bb90914 JB |
84 | #ifdef lint |
85 | npty = npty; | |
86 | #endif | |
f21c8fb8 BJ |
87 | if (minor(dev) >= NPTY) |
88 | return (ENXIO); | |
45372428 | 89 | tp = &pt_tty[minor(dev)]; |
941944c9 | 90 | if ((tp->t_state & TS_ISOPEN) == 0) { |
bdda6b91 | 91 | ttychars(tp); /* Set up default chars */ |
e39f9ea6 MT |
92 | tp->t_iflag = TTYDEF_IFLAG; |
93 | tp->t_oflag = TTYDEF_OFLAG; | |
94 | tp->t_lflag = TTYDEF_LFLAG; | |
95 | tp->t_cflag = TTYDEF_CFLAG; | |
96 | tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; | |
97 | ttsetwater(tp); /* would be done in xxparam() */ | |
f21c8fb8 BJ |
98 | } else if (tp->t_state&TS_XCLUDE && u.u_uid != 0) |
99 | return (EBUSY); | |
e1d74936 | 100 | if (tp->t_oproc) /* Ctrlr still around. */ |
941944c9 BJ |
101 | tp->t_state |= TS_CARR_ON; |
102 | while ((tp->t_state & TS_CARR_ON) == 0) { | |
103 | tp->t_state |= TS_WOPEN; | |
e9fe78f8 MT |
104 | if (flag&FNDELAY) |
105 | break; | |
45372428 MT |
106 | sleep((caddr_t)&tp->t_rawq, TTIPRI); |
107 | } | |
e9fe78f8 | 108 | error = (*linesw[tp->t_line].l_open)(dev, tp, flag); |
5bb90914 JB |
109 | ptcwakeup(tp, FREAD|FWRITE); |
110 | return (error); | |
45372428 MT |
111 | } |
112 | ||
113 | ptsclose(dev) | |
73c77d38 | 114 | dev_t dev; |
941944c9 | 115 | { |
45372428 MT |
116 | register struct tty *tp; |
117 | ||
118 | tp = &pt_tty[minor(dev)]; | |
119 | (*linesw[tp->t_line].l_close)(tp); | |
4b39cb77 | 120 | ttyclose(tp); |
5bb90914 | 121 | ptcwakeup(tp, FREAD|FWRITE); |
6a5ca8a0 | 122 | return (0); |
45372428 MT |
123 | } |
124 | ||
e9fe78f8 | 125 | ptsread(dev, uio, flag) |
73c77d38 | 126 | dev_t dev; |
ae9a0a69 | 127 | struct uio *uio; |
e1d74936 | 128 | { |
defdbcd1 BJ |
129 | register struct tty *tp = &pt_tty[minor(dev)]; |
130 | register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; | |
840510a3 | 131 | int error = 0; |
defdbcd1 BJ |
132 | |
133 | again: | |
134 | if (pti->pt_flags & PF_REMOTE) { | |
e39f9ea6 MT |
135 | while (tp == u.u_ttyp && |
136 | u.u_procp->p_pgrp->pg_id != tp->t_pgid){ | |
5bb90914 JB |
137 | if ((u.u_procp->p_sigignore & sigmask(SIGTTIN)) || |
138 | (u.u_procp->p_sigmask & sigmask(SIGTTIN)) || | |
e39f9ea6 | 139 | !u.u_procp->p_pgrp->pg_jobc || |
defdbcd1 | 140 | u.u_procp->p_flag&SVFORK) |
840510a3 | 141 | return (EIO); |
e39f9ea6 | 142 | pgsignal(u.u_procp->p_pgrp, SIGTTIN); |
defdbcd1 | 143 | sleep((caddr_t)&lbolt, TTIPRI); |
941944c9 | 144 | } |
5bb90914 | 145 | if (tp->t_canq.c_cc == 0) { |
c4ec2128 | 146 | if (flag & IO_NDELAY) |
840510a3 | 147 | return (EWOULDBLOCK); |
5bb90914 | 148 | sleep((caddr_t)&tp->t_canq, TTIPRI); |
defdbcd1 BJ |
149 | goto again; |
150 | } | |
5bb90914 JB |
151 | while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0) |
152 | if (ureadc(getc(&tp->t_canq), uio) < 0) { | |
840510a3 | 153 | error = EFAULT; |
ae9a0a69 BJ |
154 | break; |
155 | } | |
5bb90914 JB |
156 | if (tp->t_canq.c_cc == 1) |
157 | (void) getc(&tp->t_canq); | |
158 | if (tp->t_canq.c_cc) | |
840510a3 | 159 | return (error); |
defdbcd1 BJ |
160 | } else |
161 | if (tp->t_oproc) | |
e9fe78f8 | 162 | error = (*linesw[tp->t_line].l_read)(tp, uio, flag); |
5bb90914 | 163 | ptcwakeup(tp, FWRITE); |
840510a3 | 164 | return (error); |
45372428 MT |
165 | } |
166 | ||
941944c9 BJ |
167 | /* |
168 | * Write to pseudo-tty. | |
169 | * Wakeups of controlling tty will happen | |
170 | * indirectly, when tty driver calls ptsstart. | |
171 | */ | |
e9fe78f8 | 172 | ptswrite(dev, uio, flag) |
73c77d38 | 173 | dev_t dev; |
ae9a0a69 | 174 | struct uio *uio; |
e1d74936 | 175 | { |
6a5ca8a0 KM |
176 | register struct tty *tp = &pt_tty[minor(dev)]; |
177 | register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; | |
45372428 | 178 | |
840510a3 BJ |
179 | if (tp->t_oproc == 0) |
180 | return (EIO); | |
6a5ca8a0 KM |
181 | |
182 | while (pti->pt_flags & PF_BLOCK) { | |
183 | pti->pt_flags |= PF_OWAIT; | |
184 | sleep((caddr_t)pti + 1, TTOPRI); | |
185 | } | |
186 | ||
e9fe78f8 | 187 | return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); |
45372428 MT |
188 | } |
189 | ||
941944c9 BJ |
190 | /* |
191 | * Start output on pseudo-tty. | |
192 | * Wake up process selecting or sleeping for input from controlling tty. | |
193 | */ | |
45372428 | 194 | ptsstart(tp) |
e1d74936 BJ |
195 | struct tty *tp; |
196 | { | |
1e6d24b1 | 197 | register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; |
e1d74936 | 198 | |
941944c9 | 199 | if (tp->t_state & TS_TTSTOP) |
45372428 | 200 | return; |
1e6d24b1 BJ |
201 | if (pti->pt_flags & PF_STOPPED) { |
202 | pti->pt_flags &= ~PF_STOPPED; | |
203 | pti->pt_send = TIOCPKT_START; | |
204 | } | |
5bb90914 | 205 | ptcwakeup(tp, FREAD); |
d427b3b2 BJ |
206 | } |
207 | ||
5bb90914 | 208 | ptcwakeup(tp, flag) |
d427b3b2 BJ |
209 | struct tty *tp; |
210 | { | |
211 | struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; | |
212 | ||
5bb90914 JB |
213 | if (flag & FREAD) { |
214 | if (pti->pt_selr) { | |
215 | selwakeup(pti->pt_selr, pti->pt_flags & PF_RCOLL); | |
216 | pti->pt_selr = 0; | |
217 | pti->pt_flags &= ~PF_RCOLL; | |
218 | } | |
219 | wakeup((caddr_t)&tp->t_outq.c_cf); | |
220 | } | |
221 | if (flag & FWRITE) { | |
222 | if (pti->pt_selw) { | |
223 | selwakeup(pti->pt_selw, pti->pt_flags & PF_WCOLL); | |
224 | pti->pt_selw = 0; | |
225 | pti->pt_flags &= ~PF_WCOLL; | |
226 | } | |
e9fe78f8 | 227 | if (ptydebug) printf("WAKEUP c_cf %d\n", u.u_procp->p_pid); |
5bb90914 | 228 | wakeup((caddr_t)&tp->t_rawq.c_cf); |
e1d74936 | 229 | } |
45372428 MT |
230 | } |
231 | ||
232 | /*ARGSUSED*/ | |
233 | ptcopen(dev, flag) | |
e1d74936 BJ |
234 | dev_t dev; |
235 | int flag; | |
236 | { | |
45372428 | 237 | register struct tty *tp; |
1a954a11 | 238 | struct pt_ioctl *pti; |
45372428 | 239 | |
f21c8fb8 BJ |
240 | if (minor(dev) >= NPTY) |
241 | return (ENXIO); | |
45372428 | 242 | tp = &pt_tty[minor(dev)]; |
f21c8fb8 BJ |
243 | if (tp->t_oproc) |
244 | return (EIO); | |
e1d74936 | 245 | tp->t_oproc = ptsstart; |
f2f3b49b | 246 | (void)(*linesw[tp->t_line].l_modem)(tp, 1); |
1a954a11 BJ |
247 | pti = &pt_ioctl[minor(dev)]; |
248 | pti->pt_flags = 0; | |
249 | pti->pt_send = 0; | |
5bb90914 | 250 | pti->pt_ucntl = 0; |
f21c8fb8 | 251 | return (0); |
45372428 MT |
252 | } |
253 | ||
254 | ptcclose(dev) | |
e1d74936 BJ |
255 | dev_t dev; |
256 | { | |
45372428 MT |
257 | register struct tty *tp; |
258 | ||
259 | tp = &pt_tty[minor(dev)]; | |
f2f3b49b | 260 | (void)(*linesw[tp->t_line].l_modem)(tp, 0); |
d3be7d55 | 261 | tp->t_state &= ~TS_CARR_ON; |
e1d74936 | 262 | tp->t_oproc = 0; /* mark closed */ |
6a5ca8a0 | 263 | return (0); |
45372428 MT |
264 | } |
265 | ||
e9fe78f8 | 266 | ptcread(dev, uio, flag) |
1a954a11 | 267 | dev_t dev; |
ae9a0a69 | 268 | struct uio *uio; |
e1d74936 | 269 | { |
840510a3 | 270 | register struct tty *tp = &pt_tty[minor(dev)]; |
5bb90914 | 271 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
2bb0bef9 SL |
272 | char buf[BUFSIZ]; |
273 | int error = 0, cc; | |
45372428 | 274 | |
5bb90914 JB |
275 | /* |
276 | * We want to block until the slave | |
277 | * is open, and there's something to read; | |
278 | * but if we lost the slave or we're NBIO, | |
279 | * then return the appropriate error instead. | |
280 | */ | |
281 | for (;;) { | |
282 | if (tp->t_state&TS_ISOPEN) { | |
283 | if (pti->pt_flags&PF_PKT && pti->pt_send) { | |
8011f5df | 284 | error = ureadc((int)pti->pt_send, uio); |
5bb90914 JB |
285 | if (error) |
286 | return (error); | |
287 | pti->pt_send = 0; | |
288 | return (0); | |
289 | } | |
290 | if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { | |
8011f5df | 291 | error = ureadc((int)pti->pt_ucntl, uio); |
5bb90914 JB |
292 | if (error) |
293 | return (error); | |
294 | pti->pt_ucntl = 0; | |
295 | return (0); | |
296 | } | |
6a5ca8a0 KM |
297 | if (pti->pt_flags&PF_TIOC && pti->pt_ioc.c_cc) { |
298 | if (uio->uio_resid < pti->pt_ioc.c_cc + 1) | |
299 | return (E2BIG); | |
300 | error = ureadc(TIOCPKT_TIOC, uio); | |
301 | while (error == 0 && pti->pt_ioc.c_cc > 0) { | |
302 | cc = q_to_b(&pti->pt_ioc, buf, | |
303 | MIN(pti->pt_ioc.c_cc, BUFSIZ)); | |
304 | if (cc <= 0) /* impossible? */ | |
305 | break; | |
306 | error = uiomove(buf, cc, UIO_READ, uio); | |
307 | } | |
308 | return (error); | |
309 | } | |
5bb90914 JB |
310 | if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) |
311 | break; | |
1a954a11 | 312 | } |
2bb0bef9 | 313 | if ((tp->t_state&TS_CARR_ON) == 0) |
e9fe78f8 | 314 | return (0); /* EOF */ |
c4ec2128 | 315 | if (flag & IO_NDELAY) |
840510a3 | 316 | return (EWOULDBLOCK); |
e9fe78f8 | 317 | if (ptydebug) printf("SLEEP(1) c_cf %d\n", u.u_procp->p_pid); |
45372428 | 318 | sleep((caddr_t)&tp->t_outq.c_cf, TTIPRI); |
8b8271ac | 319 | } |
6a5ca8a0 | 320 | if (pti->pt_flags & (PF_PKT|PF_UCNTL|PF_TIOC)) |
5bb90914 | 321 | error = ureadc(0, uio); |
2bb0bef9 SL |
322 | while (uio->uio_resid > 0 && error == 0) { |
323 | cc = q_to_b(&tp->t_outq, buf, MIN(uio->uio_resid, BUFSIZ)); | |
324 | if (cc <= 0) | |
ae9a0a69 | 325 | break; |
c4ec2128 | 326 | error = uiomove(buf, cc, uio); |
2bb0bef9 | 327 | } |
6a5ca8a0 KM |
328 | if (tp->t_outq.c_cc <= TTLOWAT(tp) && !(pti->pt_flags & PF_BLOCK)) |
329 | ptswake(tp); | |
840510a3 | 330 | return (error); |
45372428 MT |
331 | } |
332 | ||
6a5ca8a0 KM |
333 | ptswake(tp) |
334 | register struct tty *tp; | |
335 | { | |
336 | if (tp->t_state&TS_ASLEEP) { | |
337 | tp->t_state &= ~TS_ASLEEP; | |
338 | wakeup((caddr_t)&tp->t_outq); | |
339 | } | |
340 | if (tp->t_wsel) { | |
341 | selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL); | |
342 | tp->t_wsel = 0; | |
343 | tp->t_state &= ~TS_WCOLL; | |
344 | } | |
345 | } | |
346 | ||
1a954a11 BJ |
347 | ptsstop(tp, flush) |
348 | register struct tty *tp; | |
349 | int flush; | |
350 | { | |
351 | struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; | |
5bb90914 | 352 | int flag; |
1a954a11 | 353 | |
1e6d24b1 BJ |
354 | /* note: FLUSHREAD and FLUSHWRITE already ok */ |
355 | if (flush == 0) { | |
356 | flush = TIOCPKT_STOP; | |
357 | pti->pt_flags |= PF_STOPPED; | |
5bb90914 | 358 | } else |
1e6d24b1 | 359 | pti->pt_flags &= ~PF_STOPPED; |
0a2bd708 | 360 | pti->pt_send |= flush; |
5bb90914 JB |
361 | /* change of perspective */ |
362 | flag = 0; | |
363 | if (flush & FREAD) | |
364 | flag |= FWRITE; | |
365 | if (flush & FWRITE) | |
366 | flag |= FREAD; | |
9d2a90b1 | 367 | ptcwakeup(tp, flag); |
1a954a11 BJ |
368 | } |
369 | ||
941944c9 | 370 | ptcselect(dev, rw) |
e1d74936 | 371 | dev_t dev; |
941944c9 | 372 | int rw; |
e1d74936 BJ |
373 | { |
374 | register struct tty *tp = &pt_tty[minor(dev)]; | |
defdbcd1 | 375 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
e1d74936 | 376 | struct proc *p; |
d427b3b2 | 377 | int s; |
e1d74936 | 378 | |
5bb90914 | 379 | if ((tp->t_state&TS_CARR_ON) == 0) |
e1d74936 | 380 | return (1); |
941944c9 BJ |
381 | switch (rw) { |
382 | ||
383 | case FREAD: | |
892f2f35 MK |
384 | /* |
385 | * Need to block timeouts (ttrstart). | |
386 | */ | |
387 | s = spltty(); | |
7cbd29e4 MK |
388 | if ((tp->t_state&TS_ISOPEN) && |
389 | tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) { | |
390 | splx(s); | |
391 | return (1); | |
392 | } | |
892f2f35 | 393 | splx(s); |
7cbd29e4 MK |
394 | /* FALLTHROUGH */ |
395 | ||
396 | case 0: /* exceptional */ | |
5bb90914 JB |
397 | if ((tp->t_state&TS_ISOPEN) && |
398 | (pti->pt_flags&PF_PKT && pti->pt_send || | |
6a5ca8a0 | 399 | pti->pt_flags&PF_TIOC && pti->pt_ioc.c_cc || |
892f2f35 | 400 | pti->pt_flags&PF_UCNTL && pti->pt_ucntl)) |
941944c9 | 401 | return (1); |
1a954a11 BJ |
402 | if ((p = pti->pt_selr) && p->p_wchan == (caddr_t)&selwait) |
403 | pti->pt_flags |= PF_RCOLL; | |
941944c9 | 404 | else |
1a954a11 | 405 | pti->pt_selr = u.u_procp; |
d427b3b2 | 406 | break; |
941944c9 | 407 | |
7cbd29e4 | 408 | |
941944c9 | 409 | case FWRITE: |
892f2f35 MK |
410 | if (tp->t_state&TS_ISOPEN) { |
411 | if (pti->pt_flags & PF_REMOTE) { | |
412 | if (tp->t_canq.c_cc == 0) | |
413 | return (1); | |
414 | } else { | |
415 | if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) | |
416 | return (1); | |
e39f9ea6 | 417 | if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON)) |
892f2f35 MK |
418 | return (1); |
419 | } | |
d427b3b2 | 420 | } |
1a954a11 BJ |
421 | if ((p = pti->pt_selw) && p->p_wchan == (caddr_t)&selwait) |
422 | pti->pt_flags |= PF_WCOLL; | |
941944c9 | 423 | else |
1a954a11 | 424 | pti->pt_selw = u.u_procp; |
d427b3b2 | 425 | break; |
7cbd29e4 | 426 | |
941944c9 | 427 | } |
d427b3b2 | 428 | return (0); |
e1d74936 BJ |
429 | } |
430 | ||
e9fe78f8 | 431 | ptcwrite(dev, uio, flag) |
941944c9 | 432 | dev_t dev; |
5bb90914 | 433 | register struct uio *uio; |
e1d74936 | 434 | { |
840510a3 | 435 | register struct tty *tp = &pt_tty[minor(dev)]; |
5bb90914 JB |
436 | register struct iovec *iov; |
437 | register char *cp; | |
438 | register int cc = 0; | |
45372428 | 439 | char locbuf[BUFSIZ]; |
941944c9 | 440 | int cnt = 0; |
defdbcd1 | 441 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
840510a3 | 442 | int error = 0; |
45372428 | 443 | |
defdbcd1 | 444 | again: |
5bb90914 JB |
445 | if ((tp->t_state&TS_ISOPEN) == 0) |
446 | goto block; | |
9d2a90b1 | 447 | if (pti->pt_flags & PF_REMOTE) { |
5bb90914 JB |
448 | if (tp->t_canq.c_cc) |
449 | goto block; | |
9d2a90b1 JB |
450 | while (uio->uio_iovcnt > 0 && tp->t_canq.c_cc < TTYHOG - 1) { |
451 | iov = uio->uio_iov; | |
452 | if (iov->iov_len == 0) { | |
453 | uio->uio_iovcnt--; | |
454 | uio->uio_iov++; | |
455 | continue; | |
456 | } | |
457 | if (cc == 0) { | |
458 | cc = MIN(iov->iov_len, BUFSIZ); | |
459 | cc = MIN(cc, TTYHOG - 1 - tp->t_canq.c_cc); | |
460 | cp = locbuf; | |
c4ec2128 | 461 | error = uiomove(cp, cc, uio); |
9d2a90b1 JB |
462 | if (error) |
463 | return (error); | |
464 | /* check again for safety */ | |
465 | if ((tp->t_state&TS_ISOPEN) == 0) | |
466 | return (EIO); | |
467 | } | |
468 | if (cc) | |
469 | (void) b_to_q(cp, cc, &tp->t_canq); | |
470 | cc = 0; | |
defdbcd1 | 471 | } |
5bb90914 JB |
472 | (void) putc(0, &tp->t_canq); |
473 | ttwakeup(tp); | |
474 | wakeup((caddr_t)&tp->t_canq); | |
475 | return (0); | |
476 | } | |
477 | while (uio->uio_iovcnt > 0) { | |
478 | iov = uio->uio_iov; | |
479 | if (cc == 0) { | |
480 | if (iov->iov_len == 0) { | |
481 | uio->uio_iovcnt--; | |
482 | uio->uio_iov++; | |
483 | continue; | |
45372428 | 484 | } |
5bb90914 JB |
485 | cc = MIN(iov->iov_len, BUFSIZ); |
486 | cp = locbuf; | |
c4ec2128 | 487 | error = uiomove(cp, cc, uio); |
5bb90914 JB |
488 | if (error) |
489 | return (error); | |
490 | /* check again for safety */ | |
491 | if ((tp->t_state&TS_ISOPEN) == 0) | |
492 | return (EIO); | |
493 | } | |
9d2a90b1 JB |
494 | while (cc > 0) { |
495 | if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 && | |
e39f9ea6 | 496 | (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) { |
9d2a90b1 JB |
497 | wakeup((caddr_t)&tp->t_rawq); |
498 | goto block; | |
499 | } | |
e9fe78f8 | 500 | (*linesw[tp->t_line].l_rint)(*cp++&0377, tp); |
941944c9 | 501 | cnt++; |
9d2a90b1 | 502 | cc--; |
45372428 | 503 | } |
5bb90914 JB |
504 | cc = 0; |
505 | } | |
506 | return (0); | |
507 | block: | |
508 | /* | |
9d2a90b1 JB |
509 | * Come here to wait for slave to open, for space |
510 | * in outq, or space in rawq. | |
5bb90914 JB |
511 | */ |
512 | if ((tp->t_state&TS_CARR_ON) == 0) | |
513 | return (EIO); | |
c4ec2128 | 514 | if ((pti->pt_flags & PF_NBIO) || (flag & IO_NDELAY)) { |
5bb90914 JB |
515 | iov->iov_base -= cc; |
516 | iov->iov_len += cc; | |
517 | uio->uio_resid += cc; | |
518 | uio->uio_offset -= cc; | |
9d2a90b1 JB |
519 | if (cnt == 0) |
520 | return (EWOULDBLOCK); | |
5bb90914 JB |
521 | return (0); |
522 | } | |
e9fe78f8 | 523 | if (ptydebug) printf("SLEEP(2) c_cf %d\n", u.u_procp->p_pid); |
5bb90914 JB |
524 | sleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI); |
525 | goto again; | |
45372428 MT |
526 | } |
527 | ||
45372428 | 528 | /*ARGSUSED*/ |
4b72e2f9 SL |
529 | ptyioctl(dev, cmd, data, flag) |
530 | caddr_t data; | |
e1d74936 BJ |
531 | dev_t dev; |
532 | { | |
0a2bd708 BJ |
533 | register struct tty *tp = &pt_tty[minor(dev)]; |
534 | register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; | |
e39f9ea6 | 535 | register u_char *cc = tp->t_cc; |
5bb90914 | 536 | int stop, error; |
f2f3b49b | 537 | extern ttyinput(); |
45372428 | 538 | |
f2f3b49b MK |
539 | /* |
540 | * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. | |
541 | * ttywflush(tp) will hang if there are characters in the outq. | |
542 | */ | |
6a5ca8a0 KM |
543 | if (cdevsw[major(dev)].d_open == ptcopen) { |
544 | if ((cmd & 0xffff) == (TIOCIOANS(0) & 0xffff)) { | |
545 | if (!(pti->pt_flags & PF_LIOC) || pti->pt_ioc.c_cc) | |
546 | return (EINVAL); | |
547 | (void) b_to_q(data, IOCPARM_LEN(cmd), &pti->pt_ioc); | |
548 | wakeup((caddr_t)&pti->pt_ioc); | |
549 | return (0); | |
550 | } | |
4b72e2f9 SL |
551 | switch (cmd) { |
552 | ||
553 | case TIOCPKT: | |
5bb90914 JB |
554 | if (*(int *)data) { |
555 | if (pti->pt_flags & PF_UCNTL) | |
556 | return (EINVAL); | |
1a954a11 | 557 | pti->pt_flags |= PF_PKT; |
5bb90914 | 558 | } else |
1a954a11 | 559 | pti->pt_flags &= ~PF_PKT; |
f21c8fb8 | 560 | return (0); |
4b72e2f9 | 561 | |
5bb90914 JB |
562 | case TIOCUCNTL: |
563 | if (*(int *)data) { | |
564 | if (pti->pt_flags & PF_PKT) | |
565 | return (EINVAL); | |
566 | pti->pt_flags |= PF_UCNTL; | |
567 | } else | |
568 | pti->pt_flags &= ~PF_UCNTL; | |
569 | return (0); | |
570 | ||
6a5ca8a0 KM |
571 | case TIOCTIOC: |
572 | if (*(int *)data) { | |
573 | if (pti->pt_flags & PF_UCNTL) | |
574 | return (EINVAL); | |
575 | pti->pt_flags |= PF_TIOC; | |
576 | } else { | |
577 | pti->pt_flags &= ~(PF_TIOC|PF_LIOC|PF_WIOC); | |
578 | while (pti->pt_ioc.c_cc) | |
579 | (void) getc(&pti->pt_ioc); | |
580 | wakeup((caddr_t)&pti->pt_ioc); | |
581 | } | |
582 | return (0); | |
583 | ||
584 | case TIOCBLK: | |
585 | if (*(int *)data) | |
586 | pti->pt_flags |= PF_BLOCK; | |
587 | else { | |
588 | if (pti->pt_flags & PF_OWAIT) | |
589 | wakeup((caddr_t)pti + 1); | |
590 | pti->pt_flags &= ~(PF_BLOCK|PF_OWAIT); | |
591 | ptswake(tp); | |
592 | } | |
593 | return (0); | |
594 | ||
4b72e2f9 SL |
595 | case TIOCREMOTE: |
596 | if (*(int *)data) | |
defdbcd1 BJ |
597 | pti->pt_flags |= PF_REMOTE; |
598 | else | |
599 | pti->pt_flags &= ~PF_REMOTE; | |
88a7a62a | 600 | ttyflush(tp, FREAD|FWRITE); |
f21c8fb8 | 601 | return (0); |
4b72e2f9 SL |
602 | |
603 | case FIONBIO: | |
604 | if (*(int *)data) | |
1a954a11 | 605 | pti->pt_flags |= PF_NBIO; |
8b8271ac | 606 | else |
1a954a11 | 607 | pti->pt_flags &= ~PF_NBIO; |
f21c8fb8 | 608 | return (0); |
4b72e2f9 | 609 | |
6a5ca8a0 KM |
610 | case FIONREAD: |
611 | *(int *)data = tp->t_outq.c_cc; | |
612 | return (0); | |
613 | ||
e39f9ea6 | 614 | case TIOCSETP: |
f2f3b49b MK |
615 | case TIOCSETN: |
616 | case TIOCSETD: | |
e39f9ea6 MT |
617 | case TIOCSETA: |
618 | case TIOCSETAW: | |
619 | case TIOCSETAF: | |
620 | case TIOCSETAS: | |
621 | case TIOCSETAWS: | |
622 | case TIOCSETAFS: | |
4b72e2f9 SL |
623 | while (getc(&tp->t_outq) >= 0) |
624 | ; | |
625 | break; | |
8b8271ac | 626 | } |
6a5ca8a0 KM |
627 | } else if (pti->pt_flags & PF_TIOC) { |
628 | while (pti->pt_flags & PF_LIOC) { | |
629 | pti->pt_flags |= PF_WIOC; | |
630 | switch (tsleep((caddr_t)&pti->pt_flags,TTIPRI-1,5*hz)) { | |
631 | case TS_OK: | |
632 | continue; | |
633 | case TS_SIG: | |
634 | case TS_TIME: | |
635 | return (EBUSY); | |
636 | } | |
637 | } | |
638 | pti->pt_flags |= PF_LIOC | PF_BLOCK; | |
639 | while (pti->pt_ioc.c_cc) | |
640 | (void) getc(&pti->pt_ioc); | |
641 | (void) b_to_q(&cmd, sizeof cmd, &pti->pt_ioc); | |
642 | if (cmd & IOC_IN) | |
643 | (void) b_to_q(data, IOCPARM_LEN(cmd), &pti->pt_ioc); | |
644 | ptcwakeup(tp, FREAD); | |
645 | switch (tsleep((caddr_t)&pti->pt_ioc, TTIPRI-1, 5*hz)) { | |
646 | case TS_SIG: | |
647 | case TS_TIME: | |
648 | while (pti->pt_ioc.c_cc) | |
649 | (void) getc(&pti->pt_ioc); | |
650 | if (pti->pt_flags & PF_WIOC) | |
651 | wakeup((caddr_t)&pti->pt_flags); | |
652 | if (pti->pt_flags & PF_OWAIT) | |
653 | wakeup((caddr_t)pti + 1); | |
654 | pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT); | |
655 | ptswake(tp); | |
656 | return (EBUSY); | |
657 | case TS_OK: | |
658 | break; | |
659 | } | |
660 | if (pti->pt_ioc.c_cc == 0) { | |
661 | if (pti->pt_flags & PF_WIOC) | |
662 | wakeup((caddr_t)&pti->pt_flags); | |
663 | if (pti->pt_flags & PF_OWAIT) | |
664 | wakeup((caddr_t)pti + 1); | |
665 | pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT); | |
666 | ptswake(tp); | |
667 | goto doioctl; | |
668 | } | |
669 | if (q_to_b(&pti->pt_ioc, &error, sizeof error) != sizeof error) | |
670 | error = EINVAL; | |
671 | if (error == 0 && cmd & IOC_OUT) { | |
672 | if (IOCPARM_LEN(cmd) != pti->pt_ioc.c_cc) | |
673 | error = EINVAL; | |
674 | else | |
675 | (void) q_to_b(&pti->pt_ioc, data, | |
676 | pti->pt_ioc.c_cc); | |
677 | } | |
678 | while (pti->pt_ioc.c_cc) | |
679 | (void) getc(&pti->pt_ioc); | |
680 | if (pti->pt_flags & PF_WIOC) | |
681 | wakeup((caddr_t)&pti->pt_flags); | |
682 | if (pti->pt_flags & PF_OWAIT) | |
683 | wakeup((caddr_t)pti + 1); | |
684 | pti->pt_flags &= ~(PF_LIOC|PF_WIOC|PF_BLOCK|PF_OWAIT); | |
685 | ptswake(tp); | |
686 | return (error); | |
687 | } | |
688 | ||
689 | doioctl: | |
e39f9ea6 MT |
690 | error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag); |
691 | if (error < 0) | |
692 | error = ttioctl(tp, cmd, data, flag); | |
f2f3b49b MK |
693 | /* |
694 | * Since we use the tty queues internally, | |
695 | * pty's can't be switched to disciplines which overwrite | |
696 | * the queues. We can't tell anything about the discipline | |
697 | * from here... | |
6a5ca8a0 KM |
698 | * |
699 | * Nb: this is not really good enough, the line disc open routine | |
700 | * may have done anything at all, no guarantees that close | |
701 | * will fix it. This also has the effect of losing the | |
702 | * previous discipline, which an error on a TIOCSETD shouldn't | |
703 | * do... Sometime it should be done via an explicit check | |
704 | * for TIOCSETD, then check to see what linesw[new_number].l_rint | |
705 | * really is. | |
f2f3b49b MK |
706 | */ |
707 | if (linesw[tp->t_line].l_rint != ttyinput) { | |
708 | (*linesw[tp->t_line].l_close)(tp); | |
709 | tp->t_line = 0; | |
e9fe78f8 | 710 | (void)(*linesw[tp->t_line].l_open)(dev, tp, flag); |
f2f3b49b MK |
711 | error = ENOTTY; |
712 | } | |
6a5ca8a0 | 713 | |
5bb90914 JB |
714 | if (error < 0) { |
715 | if (pti->pt_flags & PF_UCNTL && | |
97d7430f | 716 | (cmd & ~0xff) == UIOCCMD(0)) { |
5bb90914 JB |
717 | if (cmd & 0xff) { |
718 | pti->pt_ucntl = (u_char)cmd; | |
719 | ptcwakeup(tp, FREAD); | |
720 | } | |
721 | return (0); | |
722 | } | |
f21c8fb8 | 723 | error = ENOTTY; |
5bb90914 | 724 | } |
e39f9ea6 MT |
725 | stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) |
726 | && CCEQ(cc[VSTART], CTRL('q')); | |
0a2bd708 BJ |
727 | if (pti->pt_flags & PF_NOSTOP) { |
728 | if (stop) { | |
b31eb1ff | 729 | pti->pt_send &= ~TIOCPKT_NOSTOP; |
0a2bd708 BJ |
730 | pti->pt_send |= TIOCPKT_DOSTOP; |
731 | pti->pt_flags &= ~PF_NOSTOP; | |
5bb90914 | 732 | ptcwakeup(tp, FREAD); |
0a2bd708 BJ |
733 | } |
734 | } else { | |
5bb90914 | 735 | if (!stop) { |
0a2bd708 BJ |
736 | pti->pt_send &= ~TIOCPKT_DOSTOP; |
737 | pti->pt_send |= TIOCPKT_NOSTOP; | |
738 | pti->pt_flags |= PF_NOSTOP; | |
5bb90914 | 739 | ptcwakeup(tp, FREAD); |
0a2bd708 BJ |
740 | } |
741 | } | |
f21c8fb8 | 742 | return (error); |
45372428 | 743 | } |
4f07e6e5 | 744 | #endif |