Commit | Line | Data |
---|---|---|
15637ed4 RG |
1 | /* |
2 | * Copyright (c) 1982, 1986, 1989 The Regents of the University of California. | |
3 | * 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 | * | |
600f7f07 | 33 | * from: @(#)tty_pty.c 7.21 (Berkeley) 5/30/91 |
9eba4092 | 34 | * $Id: tty_pty.c,v 1.11 1994/04/03 19:40:01 ache Exp $ |
15637ed4 | 35 | */ |
15637ed4 RG |
36 | |
37 | /* | |
38 | * Pseudo-teletype Driver | |
39 | * (Actually two drivers, requiring two entries in 'cdevsw') | |
40 | */ | |
41 | #include "pty.h" | |
42 | ||
43 | #if NPTY > 0 | |
44 | #include "param.h" | |
45 | #include "systm.h" | |
46 | #include "ioctl.h" | |
47 | #include "tty.h" | |
48 | #include "conf.h" | |
49 | #include "file.h" | |
50 | #include "proc.h" | |
51 | #include "uio.h" | |
52 | #include "kernel.h" | |
53 | #include "vnode.h" | |
fde1aeb2 | 54 | #include "signalvar.h" |
15637ed4 RG |
55 | |
56 | #if NPTY == 1 | |
57 | #undef NPTY | |
58 | #define NPTY 32 /* crude XXX */ | |
59 | #endif | |
60 | ||
61 | #define BUFSIZ 100 /* Chunk size iomoved to/from user */ | |
62 | ||
4c45483e GW |
63 | static void ptcwakeup(struct tty *, int); |
64 | ||
15637ed4 RG |
65 | /* |
66 | * pts == /dev/tty[pqrs]? | |
67 | * ptc == /dev/pty[pqrs]? | |
68 | */ | |
6cc15668 | 69 | struct tty *pt_tty[NPTY]; |
15637ed4 RG |
70 | struct pt_ioctl { |
71 | int pt_flags; | |
72 | pid_t pt_selr, pt_selw; | |
73 | u_char pt_send; | |
74 | u_char pt_ucntl; | |
75 | } pt_ioctl[NPTY]; | |
76 | int npty = NPTY; /* for pstat -t */ | |
77 | ||
6cc15668 GR |
78 | #define PF_RCOLL 0x0001 |
79 | #define PF_WCOLL 0x0002 | |
80 | #define PF_PKT 0x0008 /* packet mode */ | |
81 | #define PF_STOPPED 0x0010 /* user told stopped */ | |
82 | #define PF_REMOTE 0x0020 /* remote and flow controlled input */ | |
83 | #define PF_NOSTOP 0x0040 | |
84 | #define PF_UCNTL 0x0080 /* user control mode */ | |
85 | #define PF_COPEN 0x0100 /* master open */ | |
86 | #define PF_SOPEN 0x0200 /* slave open */ | |
15637ed4 RG |
87 | |
88 | /*ARGSUSED*/ | |
4c45483e | 89 | int |
15637ed4 RG |
90 | ptsopen(dev, flag, devtype, p) |
91 | dev_t dev; | |
4c45483e GW |
92 | int flag; |
93 | int devtype; | |
15637ed4 RG |
94 | struct proc *p; |
95 | { | |
96 | register struct tty *tp; | |
9eba4092 | 97 | int error = 0; |
15637ed4 RG |
98 | |
99 | #ifdef lint | |
100 | npty = npty; | |
101 | #endif | |
102 | if (minor(dev) >= NPTY) | |
103 | return (ENXIO); | |
6cc15668 | 104 | tp = pt_tty[minor(dev)] = ttymalloc(pt_tty[minor(dev)]); |
15637ed4 RG |
105 | if ((tp->t_state & TS_ISOPEN) == 0) { |
106 | tp->t_state |= TS_WOPEN; | |
107 | ttychars(tp); /* Set up default chars */ | |
108 | tp->t_iflag = TTYDEF_IFLAG; | |
109 | tp->t_oflag = TTYDEF_OFLAG; | |
110 | tp->t_lflag = TTYDEF_LFLAG; | |
111 | tp->t_cflag = TTYDEF_CFLAG; | |
112 | tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED; | |
113 | ttsetwater(tp); /* would be done in xxparam() */ | |
114 | } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0) | |
115 | return (EBUSY); | |
116 | if (tp->t_oproc) /* Ctrlr still around. */ | |
117 | tp->t_state |= TS_CARR_ON; | |
118 | while ((tp->t_state & TS_CARR_ON) == 0) { | |
119 | tp->t_state |= TS_WOPEN; | |
120 | if (flag&FNONBLOCK) | |
121 | break; | |
6cc15668 | 122 | if (error = ttysleep(tp, (caddr_t)tp->t_raw, TTIPRI | PCATCH, |
9eba4092 AC |
123 | "ptsopn", 0)) |
124 | break; | |
15637ed4 | 125 | } |
9eba4092 AC |
126 | if (error == 0) |
127 | error = (*linesw[tp->t_line].l_open)(dev, tp, flag); | |
128 | if (error == 0) { | |
129 | ptcwakeup(tp, FREAD|FWRITE); | |
130 | pt_ioctl[minor(dev)].pt_flags |= PF_SOPEN; | |
131 | } else | |
132 | tp->t_state &= ~TS_WOPEN; | |
15637ed4 RG |
133 | return (error); |
134 | } | |
135 | ||
4c45483e | 136 | int |
15637ed4 RG |
137 | ptsclose(dev, flag, mode, p) |
138 | dev_t dev; | |
139 | int flag, mode; | |
140 | struct proc *p; | |
141 | { | |
142 | register struct tty *tp; | |
143 | ||
6cc15668 | 144 | tp = pt_tty[minor(dev)]; |
9eba4092 | 145 | ptcwakeup(tp, FREAD|FWRITE); |
15637ed4 RG |
146 | (*linesw[tp->t_line].l_close)(tp, flag); |
147 | ttyclose(tp); | |
6cc15668 GR |
148 | pt_ioctl[minor(dev)].pt_flags &= ~PF_SOPEN; |
149 | if ((pt_ioctl[minor(dev)].pt_flags & PF_COPEN) == 0) { | |
150 | ttyfree(tp); | |
151 | #ifdef broken /* session holds a ref to the tty; can't deallocate */ | |
152 | pt_tty[minor(dev)] = (struct tty *)NULL; | |
153 | #endif | |
154 | } | |
15637ed4 RG |
155 | return(0); |
156 | } | |
157 | ||
4c45483e | 158 | int |
15637ed4 RG |
159 | ptsread(dev, uio, flag) |
160 | dev_t dev; | |
161 | struct uio *uio; | |
4c45483e | 162 | int flag; |
15637ed4 RG |
163 | { |
164 | struct proc *p = curproc; | |
6cc15668 | 165 | register struct tty *tp = pt_tty[minor(dev)]; |
15637ed4 RG |
166 | register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
167 | int error = 0; | |
168 | ||
169 | again: | |
170 | if (pti->pt_flags & PF_REMOTE) { | |
171 | while (isbackground(p, tp)) { | |
172 | if ((p->p_sigignore & sigmask(SIGTTIN)) || | |
173 | (p->p_sigmask & sigmask(SIGTTIN)) || | |
174 | p->p_pgrp->pg_jobc == 0 || | |
175 | p->p_flag&SPPWAIT) | |
176 | return (EIO); | |
177 | pgsignal(p->p_pgrp, SIGTTIN, 1); | |
178 | if (error = ttysleep(tp, (caddr_t)&lbolt, | |
9eba4092 | 179 | TTIPRI | PCATCH, "ptsbg", 0)) |
15637ed4 RG |
180 | return (error); |
181 | } | |
6cc15668 | 182 | if (RB_LEN(tp->t_can) == 0) { |
15637ed4 RG |
183 | if (flag & IO_NDELAY) |
184 | return (EWOULDBLOCK); | |
6cc15668 | 185 | if (error = ttysleep(tp, (caddr_t)tp->t_can, |
9eba4092 | 186 | TTIPRI | PCATCH, "ptsin", 0)) |
15637ed4 RG |
187 | return (error); |
188 | goto again; | |
189 | } | |
6cc15668 GR |
190 | while (RB_LEN(tp->t_can) > 1 && uio->uio_resid > 0) |
191 | if (ureadc(getc(tp->t_can), uio) < 0) { | |
15637ed4 RG |
192 | error = EFAULT; |
193 | break; | |
194 | } | |
6cc15668 GR |
195 | if (RB_LEN(tp->t_can) == 1) |
196 | (void) getc(tp->t_can); | |
197 | if (RB_LEN(tp->t_can)) | |
15637ed4 RG |
198 | return (error); |
199 | } else | |
200 | if (tp->t_oproc) | |
201 | error = (*linesw[tp->t_line].l_read)(tp, uio, flag); | |
202 | ptcwakeup(tp, FWRITE); | |
203 | return (error); | |
204 | } | |
205 | ||
206 | /* | |
207 | * Write to pseudo-tty. | |
208 | * Wakeups of controlling tty will happen | |
209 | * indirectly, when tty driver calls ptsstart. | |
210 | */ | |
4c45483e | 211 | int |
15637ed4 RG |
212 | ptswrite(dev, uio, flag) |
213 | dev_t dev; | |
214 | struct uio *uio; | |
4c45483e | 215 | int flag; |
15637ed4 RG |
216 | { |
217 | register struct tty *tp; | |
218 | ||
6cc15668 | 219 | tp = pt_tty[minor(dev)]; |
15637ed4 RG |
220 | if (tp->t_oproc == 0) |
221 | return (EIO); | |
222 | return ((*linesw[tp->t_line].l_write)(tp, uio, flag)); | |
223 | } | |
224 | ||
225 | /* | |
226 | * Start output on pseudo-tty. | |
227 | * Wake up process selecting or sleeping for input from controlling tty. | |
228 | */ | |
4c45483e | 229 | void |
15637ed4 RG |
230 | ptsstart(tp) |
231 | struct tty *tp; | |
232 | { | |
233 | register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; | |
234 | ||
235 | if (tp->t_state & TS_TTSTOP) | |
236 | return; | |
237 | if (pti->pt_flags & PF_STOPPED) { | |
238 | pti->pt_flags &= ~PF_STOPPED; | |
239 | pti->pt_send = TIOCPKT_START; | |
240 | } | |
241 | ptcwakeup(tp, FREAD); | |
242 | } | |
243 | ||
4c45483e | 244 | static void |
15637ed4 RG |
245 | ptcwakeup(tp, flag) |
246 | struct tty *tp; | |
4c45483e | 247 | int flag; |
15637ed4 RG |
248 | { |
249 | struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; | |
250 | ||
251 | if (flag & FREAD) { | |
252 | if (pti->pt_selr) { | |
253 | selwakeup(pti->pt_selr, pti->pt_flags & PF_RCOLL); | |
254 | pti->pt_selr = 0; | |
255 | pti->pt_flags &= ~PF_RCOLL; | |
256 | } | |
6cc15668 | 257 | wakeup((caddr_t)&tp->t_out->rb_tl); |
15637ed4 RG |
258 | } |
259 | if (flag & FWRITE) { | |
260 | if (pti->pt_selw) { | |
261 | selwakeup(pti->pt_selw, pti->pt_flags & PF_WCOLL); | |
262 | pti->pt_selw = 0; | |
263 | pti->pt_flags &= ~PF_WCOLL; | |
264 | } | |
6cc15668 | 265 | wakeup((caddr_t)&tp->t_raw->rb_hd); |
15637ed4 RG |
266 | } |
267 | } | |
268 | ||
269 | /*ARGSUSED*/ | |
4c45483e | 270 | int |
15637ed4 | 271 | #ifdef __STDC__ |
4c45483e | 272 | ptcopen(int /*dev_t*/ dev, int flag, int devtype, struct proc *p) |
15637ed4 RG |
273 | #else |
274 | ptcopen(dev, flag, devtype, p) | |
275 | dev_t dev; | |
276 | int flag, devtype; | |
277 | struct proc *p; | |
278 | #endif | |
279 | { | |
280 | register struct tty *tp; | |
281 | struct pt_ioctl *pti; | |
282 | ||
283 | if (minor(dev) >= NPTY) | |
284 | return (ENXIO); | |
6cc15668 | 285 | tp = pt_tty[minor(dev)] = ttymalloc(pt_tty[minor(dev)]); |
15637ed4 RG |
286 | if (tp->t_oproc) |
287 | return (EIO); | |
288 | tp->t_oproc = ptsstart; | |
289 | (void)(*linesw[tp->t_line].l_modem)(tp, 1); | |
290 | tp->t_lflag &= ~EXTPROC; | |
291 | pti = &pt_ioctl[minor(dev)]; | |
6cc15668 GR |
292 | pti->pt_flags &= PF_SOPEN; |
293 | pti->pt_flags |= PF_COPEN; | |
15637ed4 RG |
294 | pti->pt_send = 0; |
295 | pti->pt_ucntl = 0; | |
296 | return (0); | |
297 | } | |
298 | ||
299 | extern struct tty *constty; /* -hv- 06.Oct.92*/ | |
4c45483e GW |
300 | |
301 | int | |
15637ed4 RG |
302 | ptcclose(dev) |
303 | dev_t dev; | |
304 | { | |
305 | register struct tty *tp; | |
306 | ||
6cc15668 | 307 | tp = pt_tty[minor(dev)]; |
15637ed4 | 308 | (void)(*linesw[tp->t_line].l_modem)(tp, 0); |
1396a309 | 309 | tp->t_state &= ~TS_CARR_ON; |
15637ed4 RG |
310 | tp->t_oproc = 0; /* mark closed */ |
311 | tp->t_session = 0; | |
312 | ||
15637ed4 RG |
313 | if (constty==tp) |
314 | constty = 0; | |
315 | ||
6cc15668 GR |
316 | pt_ioctl[minor(dev)].pt_flags &= ~PF_COPEN; |
317 | if ((pt_ioctl[minor(dev)].pt_flags & PF_SOPEN) == 0) { | |
318 | ttyfree(tp); | |
319 | #ifdef broken /* session holds a ref to the tty; can't deallocate */ | |
320 | pt_tty[minor(dev)] = (struct tty *)NULL; | |
321 | #endif | |
322 | } | |
15637ed4 RG |
323 | return (0); |
324 | } | |
325 | ||
4c45483e | 326 | int |
15637ed4 RG |
327 | ptcread(dev, uio, flag) |
328 | dev_t dev; | |
329 | struct uio *uio; | |
4c45483e | 330 | int flag; |
15637ed4 | 331 | { |
6cc15668 | 332 | register struct tty *tp = pt_tty[minor(dev)]; |
15637ed4 RG |
333 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
334 | char buf[BUFSIZ]; | |
335 | int error = 0, cc; | |
336 | ||
337 | /* | |
338 | * We want to block until the slave | |
339 | * is open, and there's something to read; | |
340 | * but if we lost the slave or we're NBIO, | |
341 | * then return the appropriate error instead. | |
342 | */ | |
343 | for (;;) { | |
344 | if (tp->t_state&TS_ISOPEN) { | |
345 | if (pti->pt_flags&PF_PKT && pti->pt_send) { | |
346 | error = ureadc((int)pti->pt_send, uio); | |
347 | if (error) | |
348 | return (error); | |
349 | if (pti->pt_send & TIOCPKT_IOCTL) { | |
350 | cc = MIN(uio->uio_resid, | |
351 | sizeof(tp->t_termios)); | |
352 | uiomove(&tp->t_termios, cc, uio); | |
353 | } | |
354 | pti->pt_send = 0; | |
355 | return (0); | |
356 | } | |
357 | if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) { | |
358 | error = ureadc((int)pti->pt_ucntl, uio); | |
359 | if (error) | |
360 | return (error); | |
361 | pti->pt_ucntl = 0; | |
362 | return (0); | |
363 | } | |
6cc15668 | 364 | if (RB_LEN(tp->t_out) && (tp->t_state&TS_TTSTOP) == 0) |
15637ed4 RG |
365 | break; |
366 | } | |
367 | if ((tp->t_state&TS_CARR_ON) == 0) | |
368 | return (0); /* EOF */ | |
369 | if (flag & IO_NDELAY) | |
370 | return (EWOULDBLOCK); | |
6cc15668 | 371 | if (error = tsleep((caddr_t)&tp->t_out->rb_tl, TTIPRI | PCATCH, |
15637ed4 RG |
372 | ttyin, 0)) |
373 | return (error); | |
374 | } | |
375 | if (pti->pt_flags & (PF_PKT|PF_UCNTL)) | |
376 | error = ureadc(0, uio); | |
377 | while (uio->uio_resid > 0 && error == 0) { | |
378 | #ifdef was | |
379 | cc = q_to_b(&tp->t_outq, buf, MIN(uio->uio_resid, BUFSIZ)); | |
380 | #else | |
6cc15668 | 381 | cc = min(MIN(uio->uio_resid, BUFSIZ), RB_CONTIGGET(tp->t_out)); |
15637ed4 | 382 | if (cc) { |
6cc15668 GR |
383 | bcopy(tp->t_out->rb_hd, buf, cc); |
384 | tp->t_out->rb_hd = | |
385 | RB_ROLLOVER(tp->t_out, tp->t_out->rb_hd+cc); | |
15637ed4 RG |
386 | } |
387 | #endif | |
388 | if (cc <= 0) | |
389 | break; | |
390 | error = uiomove(buf, cc, uio); | |
391 | } | |
6cc15668 | 392 | if (RB_LEN(tp->t_out) <= tp->t_lowat) { |
15637ed4 RG |
393 | if (tp->t_state&TS_ASLEEP) { |
394 | tp->t_state &= ~TS_ASLEEP; | |
6cc15668 | 395 | wakeup((caddr_t)tp->t_out); |
15637ed4 RG |
396 | } |
397 | if (tp->t_wsel) { | |
398 | selwakeup(tp->t_wsel, tp->t_state & TS_WCOLL); | |
399 | tp->t_wsel = 0; | |
400 | tp->t_state &= ~TS_WCOLL; | |
401 | } | |
402 | } | |
403 | return (error); | |
404 | } | |
405 | ||
4c45483e | 406 | void |
15637ed4 RG |
407 | ptsstop(tp, flush) |
408 | register struct tty *tp; | |
409 | int flush; | |
410 | { | |
411 | struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)]; | |
412 | int flag; | |
413 | ||
414 | /* note: FLUSHREAD and FLUSHWRITE already ok */ | |
415 | if (flush == 0) { | |
416 | flush = TIOCPKT_STOP; | |
417 | pti->pt_flags |= PF_STOPPED; | |
418 | } else | |
419 | pti->pt_flags &= ~PF_STOPPED; | |
420 | pti->pt_send |= flush; | |
421 | /* change of perspective */ | |
422 | flag = 0; | |
423 | if (flush & FREAD) | |
424 | flag |= FWRITE; | |
425 | if (flush & FWRITE) | |
426 | flag |= FREAD; | |
427 | ptcwakeup(tp, flag); | |
428 | } | |
429 | ||
4c45483e | 430 | int |
15637ed4 RG |
431 | ptcselect(dev, rw, p) |
432 | dev_t dev; | |
433 | int rw; | |
434 | struct proc *p; | |
435 | { | |
6cc15668 | 436 | register struct tty *tp = pt_tty[minor(dev)]; |
15637ed4 RG |
437 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
438 | struct proc *prev; | |
439 | int s; | |
440 | ||
441 | if ((tp->t_state&TS_CARR_ON) == 0) | |
442 | return (1); | |
443 | switch (rw) { | |
444 | ||
445 | case FREAD: | |
446 | /* | |
447 | * Need to block timeouts (ttrstart). | |
448 | */ | |
449 | s = spltty(); | |
450 | if ((tp->t_state&TS_ISOPEN) && | |
6cc15668 | 451 | RB_LEN(tp->t_out) && (tp->t_state&TS_TTSTOP) == 0) { |
15637ed4 RG |
452 | splx(s); |
453 | return (1); | |
454 | } | |
455 | splx(s); | |
456 | /* FALLTHROUGH */ | |
457 | ||
458 | case 0: /* exceptional */ | |
459 | if ((tp->t_state&TS_ISOPEN) && | |
460 | (pti->pt_flags&PF_PKT && pti->pt_send || | |
461 | pti->pt_flags&PF_UCNTL && pti->pt_ucntl)) | |
462 | return (1); | |
463 | if (pti->pt_selr && (prev = pfind(pti->pt_selr)) && prev->p_wchan == (caddr_t)&selwait) | |
464 | pti->pt_flags |= PF_RCOLL; | |
465 | else | |
466 | pti->pt_selr = p->p_pid; | |
467 | break; | |
468 | ||
469 | ||
470 | case FWRITE: | |
471 | if (tp->t_state&TS_ISOPEN) { | |
472 | if (pti->pt_flags & PF_REMOTE) { | |
6cc15668 | 473 | if (RB_LEN(tp->t_can) == 0) |
15637ed4 RG |
474 | return (1); |
475 | } else { | |
6cc15668 | 476 | if (RB_LEN(tp->t_raw) + RB_LEN(tp->t_can) < TTYHOG-2) |
15637ed4 | 477 | return (1); |
6cc15668 | 478 | if (RB_LEN(tp->t_can) == 0 && (tp->t_iflag&ICANON)) |
15637ed4 RG |
479 | return (1); |
480 | } | |
481 | } | |
482 | if (pti->pt_selw && (prev = pfind(pti->pt_selw)) && prev->p_wchan == (caddr_t)&selwait) | |
483 | pti->pt_flags |= PF_WCOLL; | |
484 | else | |
485 | pti->pt_selw = p->p_pid; | |
486 | break; | |
487 | ||
488 | } | |
489 | return (0); | |
490 | } | |
491 | ||
4c45483e | 492 | int |
15637ed4 RG |
493 | ptcwrite(dev, uio, flag) |
494 | dev_t dev; | |
495 | register struct uio *uio; | |
4c45483e | 496 | int flag; |
15637ed4 | 497 | { |
6cc15668 | 498 | register struct tty *tp = pt_tty[minor(dev)]; |
4c45483e | 499 | register u_char *cp = 0; |
15637ed4 RG |
500 | register int cc = 0; |
501 | u_char locbuf[BUFSIZ]; | |
502 | int cnt = 0; | |
503 | struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; | |
504 | int error = 0; | |
505 | ||
506 | again: | |
507 | if ((tp->t_state&TS_ISOPEN) == 0) | |
508 | goto block; | |
509 | if (pti->pt_flags & PF_REMOTE) { | |
6cc15668 | 510 | if (RB_LEN(tp->t_can)) |
15637ed4 | 511 | goto block; |
6cc15668 | 512 | while (uio->uio_resid > 0 && RB_LEN(tp->t_can) < TTYHOG - 1) { |
15637ed4 RG |
513 | if (cc == 0) { |
514 | cc = min(uio->uio_resid, BUFSIZ); | |
6cc15668 | 515 | cc = min(cc, RB_CONTIGPUT(tp->t_can)); |
15637ed4 RG |
516 | cp = locbuf; |
517 | error = uiomove((caddr_t)cp, cc, uio); | |
518 | if (error) | |
519 | return (error); | |
520 | /* check again for safety */ | |
521 | if ((tp->t_state&TS_ISOPEN) == 0) | |
522 | return (EIO); | |
523 | } | |
524 | #ifdef was | |
525 | if (cc) | |
526 | (void) b_to_q((char *)cp, cc, &tp->t_canq); | |
527 | #else | |
528 | if (cc) { | |
6cc15668 GR |
529 | bcopy(cp, tp->t_can->rb_tl, cc); |
530 | tp->t_can->rb_tl = | |
531 | RB_ROLLOVER(tp->t_can, tp->t_can->rb_tl+cc); | |
15637ed4 RG |
532 | } |
533 | #endif | |
534 | cc = 0; | |
535 | } | |
6cc15668 | 536 | (void) putc(0, tp->t_can); |
15637ed4 | 537 | ttwakeup(tp); |
6cc15668 | 538 | wakeup((caddr_t)tp->t_can); |
15637ed4 RG |
539 | return (0); |
540 | } | |
541 | while (uio->uio_resid > 0) { | |
542 | if (cc == 0) { | |
543 | cc = min(uio->uio_resid, BUFSIZ); | |
544 | cp = locbuf; | |
545 | error = uiomove((caddr_t)cp, cc, uio); | |
546 | if (error) | |
547 | return (error); | |
548 | /* check again for safety */ | |
549 | if ((tp->t_state&TS_ISOPEN) == 0) | |
550 | return (EIO); | |
551 | } | |
552 | while (cc > 0) { | |
6cc15668 GR |
553 | if ((RB_LEN(tp->t_raw) + RB_LEN(tp->t_can)) >= TTYHOG - 2 && |
554 | (RB_LEN(tp->t_can) > 0 || !(tp->t_iflag&ICANON))) { | |
555 | wakeup((caddr_t)tp->t_raw); | |
15637ed4 RG |
556 | goto block; |
557 | } | |
558 | (*linesw[tp->t_line].l_rint)(*cp++, tp); | |
559 | cnt++; | |
560 | cc--; | |
561 | } | |
562 | cc = 0; | |
563 | } | |
564 | return (0); | |
565 | block: | |
566 | /* | |
567 | * Come here to wait for slave to open, for space | |
568 | * in outq, or space in rawq. | |
569 | */ | |
570 | if ((tp->t_state&TS_CARR_ON) == 0) | |
571 | return (EIO); | |
572 | if (flag & IO_NDELAY) { | |
573 | /* adjust for data copied in but not written */ | |
574 | uio->uio_resid += cc; | |
575 | if (cnt == 0) | |
576 | return (EWOULDBLOCK); | |
577 | return (0); | |
578 | } | |
6cc15668 | 579 | if (error = tsleep((caddr_t)&tp->t_raw->rb_hd, TTOPRI | PCATCH, |
15637ed4 RG |
580 | ttyout, 0)) { |
581 | /* adjust for data copied in but not written */ | |
582 | uio->uio_resid += cc; | |
583 | return (error); | |
584 | } | |
585 | goto again; | |
586 | } | |
587 | ||
588 | /*ARGSUSED*/ | |
4c45483e | 589 | int |
15637ed4 RG |
590 | ptyioctl(dev, cmd, data, flag) |
591 | caddr_t data; | |
4c45483e | 592 | int cmd; |
15637ed4 | 593 | dev_t dev; |
4c45483e | 594 | int flag; |
15637ed4 | 595 | { |
6cc15668 | 596 | register struct tty *tp = pt_tty[minor(dev)]; |
15637ed4 RG |
597 | register struct pt_ioctl *pti = &pt_ioctl[minor(dev)]; |
598 | register u_char *cc = tp->t_cc; | |
599 | int stop, error; | |
15637ed4 RG |
600 | |
601 | /* | |
602 | * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG. | |
603 | * ttywflush(tp) will hang if there are characters in the outq. | |
604 | */ | |
605 | if (cmd == TIOCEXT) { | |
606 | /* | |
607 | * When the EXTPROC bit is being toggled, we need | |
608 | * to send an TIOCPKT_IOCTL if the packet driver | |
609 | * is turned on. | |
610 | */ | |
611 | if (*(int *)data) { | |
612 | if (pti->pt_flags & PF_PKT) { | |
613 | pti->pt_send |= TIOCPKT_IOCTL; | |
614 | ptcwakeup(tp, FREAD); | |
615 | } | |
616 | tp->t_lflag |= EXTPROC; | |
617 | } else { | |
618 | if ((tp->t_state & EXTPROC) && | |
619 | (pti->pt_flags & PF_PKT)) { | |
620 | pti->pt_send |= TIOCPKT_IOCTL; | |
621 | ptcwakeup(tp, FREAD); | |
622 | } | |
623 | tp->t_lflag &= ~EXTPROC; | |
624 | } | |
625 | return(0); | |
626 | } else | |
627 | if (cdevsw[major(dev)].d_open == ptcopen) | |
628 | switch (cmd) { | |
629 | ||
630 | case TIOCGPGRP: | |
631 | /* | |
632 | * We aviod calling ttioctl on the controller since, | |
633 | * in that case, tp must be the controlling terminal. | |
634 | */ | |
635 | *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0; | |
636 | return (0); | |
637 | ||
638 | case TIOCPKT: | |
639 | if (*(int *)data) { | |
640 | if (pti->pt_flags & PF_UCNTL) | |
641 | return (EINVAL); | |
642 | pti->pt_flags |= PF_PKT; | |
643 | } else | |
644 | pti->pt_flags &= ~PF_PKT; | |
645 | return (0); | |
646 | ||
647 | case TIOCUCNTL: | |
648 | if (*(int *)data) { | |
649 | if (pti->pt_flags & PF_PKT) | |
650 | return (EINVAL); | |
651 | pti->pt_flags |= PF_UCNTL; | |
652 | } else | |
653 | pti->pt_flags &= ~PF_UCNTL; | |
654 | return (0); | |
655 | ||
656 | case TIOCREMOTE: | |
657 | if (*(int *)data) | |
658 | pti->pt_flags |= PF_REMOTE; | |
659 | else | |
660 | pti->pt_flags &= ~PF_REMOTE; | |
661 | ttyflush(tp, FREAD|FWRITE); | |
662 | return (0); | |
663 | ||
664 | #ifdef COMPAT_43 | |
665 | /* wkt */ | |
666 | case TIOCSETP: | |
667 | case TIOCSETN: | |
668 | #endif | |
669 | case TIOCSETD: | |
670 | case TIOCSETA: | |
671 | case TIOCSETAW: | |
672 | case TIOCSETAF: | |
6cc15668 | 673 | while (getc(tp->t_out) >= 0) |
15637ed4 RG |
674 | ; |
675 | break; | |
676 | ||
677 | case TIOCSIG: | |
678 | if (*(unsigned int *)data >= NSIG) | |
679 | return(EINVAL); | |
680 | if ((tp->t_lflag&NOFLSH) == 0) | |
681 | ttyflush(tp, FREAD|FWRITE); | |
682 | pgsignal(tp->t_pgrp, *(unsigned int *)data, 1); | |
683 | if ((*(unsigned int *)data == SIGINFO) && | |
684 | ((tp->t_lflag&NOKERNINFO) == 0)) | |
685 | ttyinfo(tp); | |
686 | return(0); | |
687 | } | |
688 | error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag); | |
8b3438a6 RG |
689 | if (error >= 0) |
690 | return (error); | |
691 | error = ttioctl(tp, cmd, data, flag); | |
15637ed4 RG |
692 | /* |
693 | * Since we use the tty queues internally, | |
694 | * pty's can't be switched to disciplines which overwrite | |
695 | * the queues. We can't tell anything about the discipline | |
696 | * from here... | |
697 | */ | |
698 | if (linesw[tp->t_line].l_rint != ttyinput) { | |
699 | (*linesw[tp->t_line].l_close)(tp, flag); | |
700 | tp->t_line = TTYDISC; | |
701 | (void)(*linesw[tp->t_line].l_open)(dev, tp, flag); | |
702 | error = ENOTTY; | |
703 | } | |
704 | if (error < 0) { | |
705 | if (pti->pt_flags & PF_UCNTL && | |
706 | (cmd & ~0xff) == UIOCCMD(0)) { | |
707 | if (cmd & 0xff) { | |
708 | pti->pt_ucntl = (u_char)cmd; | |
709 | ptcwakeup(tp, FREAD); | |
710 | } | |
711 | return (0); | |
712 | } | |
713 | error = ENOTTY; | |
714 | } | |
715 | /* | |
716 | * If external processing and packet mode send ioctl packet. | |
717 | */ | |
718 | if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) { | |
719 | switch(cmd) { | |
720 | case TIOCSETA: | |
721 | case TIOCSETAW: | |
722 | case TIOCSETAF: | |
723 | #ifdef COMPAT_43 | |
724 | /* wkt */ | |
725 | case TIOCSETP: | |
726 | case TIOCSETN: | |
727 | case TIOCSETC: | |
728 | case TIOCSLTC: | |
729 | case TIOCLBIS: | |
730 | case TIOCLBIC: | |
731 | case TIOCLSET: | |
732 | #endif | |
733 | pti->pt_send |= TIOCPKT_IOCTL; | |
734 | default: | |
735 | break; | |
736 | } | |
737 | } | |
738 | stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s')) | |
739 | && CCEQ(cc[VSTART], CTRL('q')); | |
740 | if (pti->pt_flags & PF_NOSTOP) { | |
741 | if (stop) { | |
742 | pti->pt_send &= ~TIOCPKT_NOSTOP; | |
743 | pti->pt_send |= TIOCPKT_DOSTOP; | |
744 | pti->pt_flags &= ~PF_NOSTOP; | |
745 | ptcwakeup(tp, FREAD); | |
746 | } | |
747 | } else { | |
748 | if (!stop) { | |
749 | pti->pt_send &= ~TIOCPKT_DOSTOP; | |
750 | pti->pt_send |= TIOCPKT_NOSTOP; | |
751 | pti->pt_flags |= PF_NOSTOP; | |
752 | ptcwakeup(tp, FREAD); | |
753 | } | |
754 | } | |
755 | return (error); | |
756 | } | |
757 | #endif |