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