tty_pty.c revision 1.5 1 1.1 cgd /*
2 1.1 cgd * Copyright (c) 1982, 1986, 1989 The Regents of the University of California.
3 1.1 cgd * All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd *
33 1.4 cgd * from: @(#)tty_pty.c 7.21 (Berkeley) 5/30/91
34 1.5 deraadt * $Id: tty_pty.c,v 1.5 1993/05/26 10:06:47 deraadt Exp $
35 1.1 cgd */
36 1.1 cgd
37 1.1 cgd /*
38 1.1 cgd * Pseudo-teletype Driver
39 1.1 cgd * (Actually two drivers, requiring two entries in 'cdevsw')
40 1.1 cgd */
41 1.1 cgd #include "pty.h"
42 1.1 cgd
43 1.1 cgd #if NPTY > 0
44 1.1 cgd #include "param.h"
45 1.1 cgd #include "systm.h"
46 1.1 cgd #include "ioctl.h"
47 1.4 cgd #include "select.h"
48 1.1 cgd #include "tty.h"
49 1.1 cgd #include "conf.h"
50 1.1 cgd #include "file.h"
51 1.5 deraadt #include "malloc.h"
52 1.1 cgd #include "proc.h"
53 1.1 cgd #include "uio.h"
54 1.1 cgd #include "kernel.h"
55 1.1 cgd #include "vnode.h"
56 1.1 cgd
57 1.1 cgd #if NPTY == 1
58 1.1 cgd #undef NPTY
59 1.1 cgd #define NPTY 32 /* crude XXX */
60 1.1 cgd #endif
61 1.1 cgd
62 1.1 cgd #define BUFSIZ 100 /* Chunk size iomoved to/from user */
63 1.1 cgd
64 1.1 cgd /*
65 1.1 cgd * pts == /dev/tty[pqrs]?
66 1.1 cgd * ptc == /dev/pty[pqrs]?
67 1.1 cgd */
68 1.5 deraadt struct tty *pt_tty[NPTY];
69 1.1 cgd struct pt_ioctl {
70 1.1 cgd int pt_flags;
71 1.4 cgd struct selinfo pt_selr, pt_selw;
72 1.1 cgd u_char pt_send;
73 1.1 cgd u_char pt_ucntl;
74 1.1 cgd } pt_ioctl[NPTY];
75 1.1 cgd int npty = NPTY; /* for pstat -t */
76 1.1 cgd
77 1.1 cgd #define PF_RCOLL 0x01
78 1.1 cgd #define PF_WCOLL 0x02
79 1.1 cgd #define PF_PKT 0x08 /* packet mode */
80 1.1 cgd #define PF_STOPPED 0x10 /* user told stopped */
81 1.1 cgd #define PF_REMOTE 0x20 /* remote and flow controlled input */
82 1.1 cgd #define PF_NOSTOP 0x40
83 1.1 cgd #define PF_UCNTL 0x80 /* user control mode */
84 1.1 cgd
85 1.1 cgd /*ARGSUSED*/
86 1.1 cgd ptsopen(dev, flag, devtype, p)
87 1.1 cgd dev_t dev;
88 1.1 cgd struct proc *p;
89 1.1 cgd {
90 1.1 cgd register struct tty *tp;
91 1.1 cgd int error;
92 1.1 cgd
93 1.1 cgd #ifdef lint
94 1.1 cgd npty = npty;
95 1.1 cgd #endif
96 1.1 cgd if (minor(dev) >= NPTY)
97 1.1 cgd return (ENXIO);
98 1.5 deraadt if(!pt_tty[minor(dev)]) {
99 1.5 deraadt MALLOC(tp, struct tty *, sizeof(struct tty), M_TTYS, M_WAITOK);
100 1.5 deraadt bzero(tp, sizeof(struct tty));
101 1.5 deraadt pt_tty[minor(dev)] = tp;
102 1.5 deraadt } else
103 1.5 deraadt tp = pt_tty[minor(dev)];
104 1.1 cgd if ((tp->t_state & TS_ISOPEN) == 0) {
105 1.1 cgd tp->t_state |= TS_WOPEN;
106 1.1 cgd ttychars(tp); /* Set up default chars */
107 1.1 cgd tp->t_iflag = TTYDEF_IFLAG;
108 1.1 cgd tp->t_oflag = TTYDEF_OFLAG;
109 1.1 cgd tp->t_lflag = TTYDEF_LFLAG;
110 1.1 cgd tp->t_cflag = TTYDEF_CFLAG;
111 1.1 cgd tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
112 1.1 cgd ttsetwater(tp); /* would be done in xxparam() */
113 1.1 cgd } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
114 1.1 cgd return (EBUSY);
115 1.1 cgd if (tp->t_oproc) /* Ctrlr still around. */
116 1.1 cgd tp->t_state |= TS_CARR_ON;
117 1.1 cgd while ((tp->t_state & TS_CARR_ON) == 0) {
118 1.1 cgd tp->t_state |= TS_WOPEN;
119 1.1 cgd if (flag&FNONBLOCK)
120 1.1 cgd break;
121 1.1 cgd if (error = ttysleep(tp, (caddr_t)&tp->t_raw, TTIPRI | PCATCH,
122 1.1 cgd ttopen, 0))
123 1.1 cgd return (error);
124 1.1 cgd }
125 1.1 cgd error = (*linesw[tp->t_line].l_open)(dev, tp, flag);
126 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
127 1.1 cgd return (error);
128 1.1 cgd }
129 1.1 cgd
130 1.1 cgd ptsclose(dev, flag, mode, p)
131 1.1 cgd dev_t dev;
132 1.1 cgd int flag, mode;
133 1.1 cgd struct proc *p;
134 1.1 cgd {
135 1.1 cgd register struct tty *tp;
136 1.1 cgd
137 1.5 deraadt tp = pt_tty[minor(dev)];
138 1.1 cgd (*linesw[tp->t_line].l_close)(tp, flag);
139 1.1 cgd ttyclose(tp);
140 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
141 1.2 cgd return(0);
142 1.1 cgd }
143 1.1 cgd
144 1.1 cgd ptsread(dev, uio, flag)
145 1.1 cgd dev_t dev;
146 1.1 cgd struct uio *uio;
147 1.1 cgd {
148 1.1 cgd struct proc *p = curproc;
149 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
150 1.1 cgd register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
151 1.1 cgd int error = 0;
152 1.1 cgd
153 1.1 cgd again:
154 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
155 1.1 cgd while (isbackground(p, tp)) {
156 1.1 cgd if ((p->p_sigignore & sigmask(SIGTTIN)) ||
157 1.1 cgd (p->p_sigmask & sigmask(SIGTTIN)) ||
158 1.1 cgd p->p_pgrp->pg_jobc == 0 ||
159 1.1 cgd p->p_flag&SPPWAIT)
160 1.1 cgd return (EIO);
161 1.1 cgd pgsignal(p->p_pgrp, SIGTTIN, 1);
162 1.1 cgd if (error = ttysleep(tp, (caddr_t)&lbolt,
163 1.1 cgd TTIPRI | PCATCH, ttybg, 0))
164 1.1 cgd return (error);
165 1.1 cgd }
166 1.1 cgd if (RB_LEN(&tp->t_can) == 0) {
167 1.1 cgd if (flag & IO_NDELAY)
168 1.1 cgd return (EWOULDBLOCK);
169 1.1 cgd if (error = ttysleep(tp, (caddr_t)&tp->t_can,
170 1.1 cgd TTIPRI | PCATCH, ttyin, 0))
171 1.1 cgd return (error);
172 1.1 cgd goto again;
173 1.1 cgd }
174 1.1 cgd while (RB_LEN(&tp->t_can) > 1 && uio->uio_resid > 0)
175 1.1 cgd if (ureadc(getc(&tp->t_can), uio) < 0) {
176 1.1 cgd error = EFAULT;
177 1.1 cgd break;
178 1.1 cgd }
179 1.1 cgd if (RB_LEN(&tp->t_can) == 1)
180 1.1 cgd (void) getc(&tp->t_can);
181 1.1 cgd if (RB_LEN(&tp->t_can))
182 1.1 cgd return (error);
183 1.1 cgd } else
184 1.1 cgd if (tp->t_oproc)
185 1.1 cgd error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
186 1.1 cgd ptcwakeup(tp, FWRITE);
187 1.1 cgd return (error);
188 1.1 cgd }
189 1.1 cgd
190 1.1 cgd /*
191 1.1 cgd * Write to pseudo-tty.
192 1.1 cgd * Wakeups of controlling tty will happen
193 1.1 cgd * indirectly, when tty driver calls ptsstart.
194 1.1 cgd */
195 1.1 cgd ptswrite(dev, uio, flag)
196 1.1 cgd dev_t dev;
197 1.1 cgd struct uio *uio;
198 1.1 cgd {
199 1.1 cgd register struct tty *tp;
200 1.1 cgd
201 1.5 deraadt tp = pt_tty[minor(dev)];
202 1.1 cgd if (tp->t_oproc == 0)
203 1.1 cgd return (EIO);
204 1.1 cgd return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
205 1.1 cgd }
206 1.1 cgd
207 1.1 cgd /*
208 1.1 cgd * Start output on pseudo-tty.
209 1.1 cgd * Wake up process selecting or sleeping for input from controlling tty.
210 1.1 cgd */
211 1.1 cgd ptsstart(tp)
212 1.1 cgd struct tty *tp;
213 1.1 cgd {
214 1.1 cgd register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
215 1.1 cgd
216 1.1 cgd if (tp->t_state & TS_TTSTOP)
217 1.1 cgd return;
218 1.1 cgd if (pti->pt_flags & PF_STOPPED) {
219 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
220 1.1 cgd pti->pt_send = TIOCPKT_START;
221 1.1 cgd }
222 1.1 cgd ptcwakeup(tp, FREAD);
223 1.1 cgd }
224 1.1 cgd
225 1.1 cgd ptcwakeup(tp, flag)
226 1.1 cgd struct tty *tp;
227 1.1 cgd {
228 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
229 1.1 cgd
230 1.1 cgd if (flag & FREAD) {
231 1.4 cgd selwakeup(&pti->pt_selr);
232 1.2 cgd wakeup((caddr_t)&tp->t_out.rb_tl);
233 1.1 cgd }
234 1.1 cgd if (flag & FWRITE) {
235 1.4 cgd selwakeup(&pti->pt_selw);
236 1.1 cgd wakeup((caddr_t)&tp->t_raw.rb_hd);
237 1.1 cgd }
238 1.1 cgd }
239 1.1 cgd
240 1.1 cgd /*ARGSUSED*/
241 1.1 cgd #ifdef __STDC__
242 1.1 cgd ptcopen(dev_t dev, int flag, int devtype, struct proc *p)
243 1.1 cgd #else
244 1.1 cgd ptcopen(dev, flag, devtype, p)
245 1.1 cgd dev_t dev;
246 1.1 cgd int flag, devtype;
247 1.1 cgd struct proc *p;
248 1.1 cgd #endif
249 1.1 cgd {
250 1.1 cgd register struct tty *tp;
251 1.1 cgd struct pt_ioctl *pti;
252 1.1 cgd
253 1.1 cgd if (minor(dev) >= NPTY)
254 1.1 cgd return (ENXIO);
255 1.5 deraadt if(!pt_tty[minor(dev)]) {
256 1.5 deraadt MALLOC(tp, struct tty *, sizeof(struct tty), M_TTYS, M_WAITOK);
257 1.5 deraadt bzero(tp, sizeof(struct tty));
258 1.5 deraadt pt_tty[minor(dev)] = tp;
259 1.5 deraadt } else
260 1.5 deraadt tp = pt_tty[minor(dev)];
261 1.1 cgd if (tp->t_oproc)
262 1.1 cgd return (EIO);
263 1.1 cgd tp->t_oproc = ptsstart;
264 1.1 cgd (void)(*linesw[tp->t_line].l_modem)(tp, 1);
265 1.1 cgd tp->t_lflag &= ~EXTPROC;
266 1.1 cgd pti = &pt_ioctl[minor(dev)];
267 1.1 cgd pti->pt_flags = 0;
268 1.1 cgd pti->pt_send = 0;
269 1.1 cgd pti->pt_ucntl = 0;
270 1.1 cgd return (0);
271 1.1 cgd }
272 1.1 cgd
273 1.2 cgd extern struct tty *constty; /* -hv- 06.Oct.92*/
274 1.1 cgd ptcclose(dev)
275 1.1 cgd dev_t dev;
276 1.1 cgd {
277 1.1 cgd register struct tty *tp;
278 1.1 cgd
279 1.5 deraadt tp = pt_tty[minor(dev)];
280 1.1 cgd (void)(*linesw[tp->t_line].l_modem)(tp, 0);
281 1.1 cgd tp->t_state &= ~TS_CARR_ON;
282 1.1 cgd tp->t_oproc = 0; /* mark closed */
283 1.1 cgd tp->t_session = 0;
284 1.2 cgd
285 1.2 cgd /* XXX -hv- 6.Oct.92 this prevents the "hanging console bug" with X11 */
286 1.2 cgd if (constty==tp)
287 1.2 cgd constty = 0;
288 1.2 cgd
289 1.5 deraadt #if 0
290 1.5 deraadt /*
291 1.5 deraadt * currently, we do not free the pty structures once they have
292 1.5 deraadt * been allocated. Two reasons: cheap to keep them around, and
293 1.5 deraadt * I don't want to spend my time finding the exact variables that
294 1.5 deraadt * will tell me if the slave/master are closed and if I can free
295 1.5 deraadt * them. <deraadt (at) fsa.ca>
296 1.5 deraadt */
297 1.5 deraadt FREE(tp, M_TTYS);
298 1.5 deraadt pt_tty[minor(dev)] = (struct tty *)NULL;
299 1.5 deraadt #endif
300 1.2 cgd return (0);
301 1.1 cgd }
302 1.1 cgd
303 1.1 cgd ptcread(dev, uio, flag)
304 1.1 cgd dev_t dev;
305 1.1 cgd struct uio *uio;
306 1.1 cgd {
307 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
308 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
309 1.1 cgd char buf[BUFSIZ];
310 1.1 cgd int error = 0, cc;
311 1.1 cgd
312 1.1 cgd /*
313 1.1 cgd * We want to block until the slave
314 1.1 cgd * is open, and there's something to read;
315 1.1 cgd * but if we lost the slave or we're NBIO,
316 1.1 cgd * then return the appropriate error instead.
317 1.1 cgd */
318 1.1 cgd for (;;) {
319 1.1 cgd if (tp->t_state&TS_ISOPEN) {
320 1.1 cgd if (pti->pt_flags&PF_PKT && pti->pt_send) {
321 1.1 cgd error = ureadc((int)pti->pt_send, uio);
322 1.1 cgd if (error)
323 1.1 cgd return (error);
324 1.1 cgd if (pti->pt_send & TIOCPKT_IOCTL) {
325 1.1 cgd cc = MIN(uio->uio_resid,
326 1.1 cgd sizeof(tp->t_termios));
327 1.1 cgd uiomove(&tp->t_termios, cc, uio);
328 1.1 cgd }
329 1.1 cgd pti->pt_send = 0;
330 1.1 cgd return (0);
331 1.1 cgd }
332 1.1 cgd if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
333 1.1 cgd error = ureadc((int)pti->pt_ucntl, uio);
334 1.1 cgd if (error)
335 1.1 cgd return (error);
336 1.1 cgd pti->pt_ucntl = 0;
337 1.1 cgd return (0);
338 1.1 cgd }
339 1.1 cgd if (RB_LEN(&tp->t_out) && (tp->t_state&TS_TTSTOP) == 0)
340 1.1 cgd break;
341 1.1 cgd }
342 1.1 cgd if ((tp->t_state&TS_CARR_ON) == 0)
343 1.1 cgd return (0); /* EOF */
344 1.1 cgd if (flag & IO_NDELAY)
345 1.1 cgd return (EWOULDBLOCK);
346 1.2 cgd if (error = tsleep((caddr_t)&tp->t_out.rb_tl, TTIPRI | PCATCH,
347 1.1 cgd ttyin, 0))
348 1.1 cgd return (error);
349 1.1 cgd }
350 1.1 cgd if (pti->pt_flags & (PF_PKT|PF_UCNTL))
351 1.1 cgd error = ureadc(0, uio);
352 1.1 cgd while (uio->uio_resid > 0 && error == 0) {
353 1.1 cgd #ifdef was
354 1.1 cgd cc = q_to_b(&tp->t_outq, buf, MIN(uio->uio_resid, BUFSIZ));
355 1.1 cgd #else
356 1.1 cgd cc = min(MIN(uio->uio_resid, BUFSIZ), RB_CONTIGGET(&tp->t_out));
357 1.1 cgd if (cc) {
358 1.3 deraadt rbunpack(tp->t_out.rb_hd, buf, cc);
359 1.1 cgd tp->t_out.rb_hd =
360 1.1 cgd RB_ROLLOVER(&tp->t_out, tp->t_out.rb_hd+cc);
361 1.1 cgd }
362 1.1 cgd #endif
363 1.1 cgd if (cc <= 0)
364 1.1 cgd break;
365 1.1 cgd error = uiomove(buf, cc, uio);
366 1.1 cgd }
367 1.1 cgd if (RB_LEN(&tp->t_out) <= tp->t_lowat) {
368 1.1 cgd if (tp->t_state&TS_ASLEEP) {
369 1.1 cgd tp->t_state &= ~TS_ASLEEP;
370 1.1 cgd wakeup((caddr_t)&tp->t_out);
371 1.1 cgd }
372 1.4 cgd selwakeup(&tp->t_wsel);
373 1.1 cgd }
374 1.1 cgd return (error);
375 1.1 cgd }
376 1.1 cgd
377 1.1 cgd ptsstop(tp, flush)
378 1.1 cgd register struct tty *tp;
379 1.1 cgd int flush;
380 1.1 cgd {
381 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
382 1.1 cgd int flag;
383 1.1 cgd
384 1.1 cgd /* note: FLUSHREAD and FLUSHWRITE already ok */
385 1.1 cgd if (flush == 0) {
386 1.1 cgd flush = TIOCPKT_STOP;
387 1.1 cgd pti->pt_flags |= PF_STOPPED;
388 1.1 cgd } else
389 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
390 1.1 cgd pti->pt_send |= flush;
391 1.1 cgd /* change of perspective */
392 1.1 cgd flag = 0;
393 1.1 cgd if (flush & FREAD)
394 1.1 cgd flag |= FWRITE;
395 1.1 cgd if (flush & FWRITE)
396 1.1 cgd flag |= FREAD;
397 1.1 cgd ptcwakeup(tp, flag);
398 1.1 cgd }
399 1.1 cgd
400 1.1 cgd ptcselect(dev, rw, p)
401 1.1 cgd dev_t dev;
402 1.1 cgd int rw;
403 1.1 cgd struct proc *p;
404 1.1 cgd {
405 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
406 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
407 1.1 cgd struct proc *prev;
408 1.1 cgd int s;
409 1.1 cgd
410 1.1 cgd if ((tp->t_state&TS_CARR_ON) == 0)
411 1.1 cgd return (1);
412 1.1 cgd switch (rw) {
413 1.1 cgd
414 1.1 cgd case FREAD:
415 1.1 cgd /*
416 1.1 cgd * Need to block timeouts (ttrstart).
417 1.1 cgd */
418 1.1 cgd s = spltty();
419 1.1 cgd if ((tp->t_state&TS_ISOPEN) &&
420 1.1 cgd RB_LEN(&tp->t_out) && (tp->t_state&TS_TTSTOP) == 0) {
421 1.1 cgd splx(s);
422 1.1 cgd return (1);
423 1.1 cgd }
424 1.1 cgd splx(s);
425 1.1 cgd /* FALLTHROUGH */
426 1.1 cgd
427 1.1 cgd case 0: /* exceptional */
428 1.1 cgd if ((tp->t_state&TS_ISOPEN) &&
429 1.1 cgd (pti->pt_flags&PF_PKT && pti->pt_send ||
430 1.1 cgd pti->pt_flags&PF_UCNTL && pti->pt_ucntl))
431 1.1 cgd return (1);
432 1.4 cgd selrecord(p, &pti->pt_selr);
433 1.1 cgd break;
434 1.1 cgd
435 1.1 cgd
436 1.1 cgd case FWRITE:
437 1.1 cgd if (tp->t_state&TS_ISOPEN) {
438 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
439 1.1 cgd if (RB_LEN(&tp->t_can) == 0)
440 1.1 cgd return (1);
441 1.1 cgd } else {
442 1.1 cgd if (RB_LEN(&tp->t_raw) + RB_LEN(&tp->t_can) < TTYHOG-2)
443 1.1 cgd return (1);
444 1.1 cgd if (RB_LEN(&tp->t_can) == 0 && (tp->t_iflag&ICANON))
445 1.1 cgd return (1);
446 1.1 cgd }
447 1.1 cgd }
448 1.4 cgd selrecord(p, &pti->pt_selw);
449 1.1 cgd break;
450 1.1 cgd
451 1.1 cgd }
452 1.1 cgd return (0);
453 1.1 cgd }
454 1.1 cgd
455 1.1 cgd ptcwrite(dev, uio, flag)
456 1.1 cgd dev_t dev;
457 1.1 cgd register struct uio *uio;
458 1.1 cgd {
459 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
460 1.1 cgd register u_char *cp;
461 1.1 cgd register int cc = 0;
462 1.1 cgd u_char locbuf[BUFSIZ];
463 1.1 cgd int cnt = 0;
464 1.1 cgd struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
465 1.1 cgd int error = 0;
466 1.1 cgd
467 1.1 cgd again:
468 1.1 cgd if ((tp->t_state&TS_ISOPEN) == 0)
469 1.1 cgd goto block;
470 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
471 1.1 cgd if (RB_LEN(&tp->t_can))
472 1.1 cgd goto block;
473 1.1 cgd while (uio->uio_resid > 0 && RB_LEN(&tp->t_can) < TTYHOG - 1) {
474 1.1 cgd if (cc == 0) {
475 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
476 1.1 cgd cc = min(cc, TTYHOG - 1 - RB_CONTIGPUT(&tp->t_can));
477 1.1 cgd cp = locbuf;
478 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
479 1.1 cgd if (error)
480 1.1 cgd return (error);
481 1.1 cgd /* check again for safety */
482 1.1 cgd if ((tp->t_state&TS_ISOPEN) == 0)
483 1.1 cgd return (EIO);
484 1.1 cgd }
485 1.1 cgd #ifdef was
486 1.1 cgd if (cc)
487 1.1 cgd (void) b_to_q((char *)cp, cc, &tp->t_canq);
488 1.1 cgd #else
489 1.1 cgd if (cc) {
490 1.3 deraadt rbpack(cp, tp->t_can.rb_tl, cc);
491 1.1 cgd tp->t_can.rb_tl =
492 1.1 cgd RB_ROLLOVER(&tp->t_can, tp->t_can.rb_tl+cc);
493 1.1 cgd }
494 1.1 cgd #endif
495 1.1 cgd cc = 0;
496 1.1 cgd }
497 1.1 cgd (void) putc(0, &tp->t_can);
498 1.1 cgd ttwakeup(tp);
499 1.1 cgd wakeup((caddr_t)&tp->t_can);
500 1.1 cgd return (0);
501 1.1 cgd }
502 1.1 cgd while (uio->uio_resid > 0) {
503 1.1 cgd if (cc == 0) {
504 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
505 1.1 cgd cp = locbuf;
506 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
507 1.1 cgd if (error)
508 1.1 cgd return (error);
509 1.1 cgd /* check again for safety */
510 1.1 cgd if ((tp->t_state&TS_ISOPEN) == 0)
511 1.1 cgd return (EIO);
512 1.1 cgd }
513 1.1 cgd while (cc > 0) {
514 1.1 cgd if ((RB_LEN(&tp->t_raw) + RB_LEN(&tp->t_can)) >= TTYHOG - 2 &&
515 1.1 cgd (RB_LEN(&tp->t_can) > 0 || !(tp->t_iflag&ICANON))) {
516 1.1 cgd wakeup((caddr_t)&tp->t_raw);
517 1.1 cgd goto block;
518 1.1 cgd }
519 1.1 cgd (*linesw[tp->t_line].l_rint)(*cp++, tp);
520 1.1 cgd cnt++;
521 1.1 cgd cc--;
522 1.1 cgd }
523 1.1 cgd cc = 0;
524 1.1 cgd }
525 1.1 cgd return (0);
526 1.1 cgd block:
527 1.1 cgd /*
528 1.1 cgd * Come here to wait for slave to open, for space
529 1.1 cgd * in outq, or space in rawq.
530 1.1 cgd */
531 1.1 cgd if ((tp->t_state&TS_CARR_ON) == 0)
532 1.1 cgd return (EIO);
533 1.1 cgd if (flag & IO_NDELAY) {
534 1.1 cgd /* adjust for data copied in but not written */
535 1.1 cgd uio->uio_resid += cc;
536 1.1 cgd if (cnt == 0)
537 1.1 cgd return (EWOULDBLOCK);
538 1.1 cgd return (0);
539 1.1 cgd }
540 1.1 cgd if (error = tsleep((caddr_t)&tp->t_raw.rb_hd, TTOPRI | PCATCH,
541 1.1 cgd ttyout, 0)) {
542 1.1 cgd /* adjust for data copied in but not written */
543 1.1 cgd uio->uio_resid += cc;
544 1.1 cgd return (error);
545 1.1 cgd }
546 1.1 cgd goto again;
547 1.1 cgd }
548 1.1 cgd
549 1.1 cgd /*ARGSUSED*/
550 1.1 cgd ptyioctl(dev, cmd, data, flag)
551 1.1 cgd caddr_t data;
552 1.1 cgd dev_t dev;
553 1.1 cgd {
554 1.5 deraadt register struct tty *tp = pt_tty[minor(dev)];
555 1.1 cgd register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
556 1.1 cgd register u_char *cc = tp->t_cc;
557 1.1 cgd int stop, error;
558 1.1 cgd extern ttyinput();
559 1.1 cgd
560 1.1 cgd /*
561 1.1 cgd * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
562 1.1 cgd * ttywflush(tp) will hang if there are characters in the outq.
563 1.1 cgd */
564 1.1 cgd if (cmd == TIOCEXT) {
565 1.1 cgd /*
566 1.1 cgd * When the EXTPROC bit is being toggled, we need
567 1.1 cgd * to send an TIOCPKT_IOCTL if the packet driver
568 1.1 cgd * is turned on.
569 1.1 cgd */
570 1.1 cgd if (*(int *)data) {
571 1.1 cgd if (pti->pt_flags & PF_PKT) {
572 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
573 1.1 cgd ptcwakeup(tp, FREAD);
574 1.1 cgd }
575 1.1 cgd tp->t_lflag |= EXTPROC;
576 1.1 cgd } else {
577 1.1 cgd if ((tp->t_state & EXTPROC) &&
578 1.1 cgd (pti->pt_flags & PF_PKT)) {
579 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
580 1.1 cgd ptcwakeup(tp, FREAD);
581 1.1 cgd }
582 1.1 cgd tp->t_lflag &= ~EXTPROC;
583 1.1 cgd }
584 1.1 cgd return(0);
585 1.1 cgd } else
586 1.1 cgd if (cdevsw[major(dev)].d_open == ptcopen)
587 1.1 cgd switch (cmd) {
588 1.1 cgd
589 1.1 cgd case TIOCGPGRP:
590 1.1 cgd /*
591 1.1 cgd * We aviod calling ttioctl on the controller since,
592 1.1 cgd * in that case, tp must be the controlling terminal.
593 1.1 cgd */
594 1.1 cgd *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
595 1.1 cgd return (0);
596 1.1 cgd
597 1.1 cgd case TIOCPKT:
598 1.1 cgd if (*(int *)data) {
599 1.1 cgd if (pti->pt_flags & PF_UCNTL)
600 1.1 cgd return (EINVAL);
601 1.1 cgd pti->pt_flags |= PF_PKT;
602 1.1 cgd } else
603 1.1 cgd pti->pt_flags &= ~PF_PKT;
604 1.1 cgd return (0);
605 1.1 cgd
606 1.1 cgd case TIOCUCNTL:
607 1.1 cgd if (*(int *)data) {
608 1.1 cgd if (pti->pt_flags & PF_PKT)
609 1.1 cgd return (EINVAL);
610 1.1 cgd pti->pt_flags |= PF_UCNTL;
611 1.1 cgd } else
612 1.1 cgd pti->pt_flags &= ~PF_UCNTL;
613 1.1 cgd return (0);
614 1.1 cgd
615 1.1 cgd case TIOCREMOTE:
616 1.1 cgd if (*(int *)data)
617 1.1 cgd pti->pt_flags |= PF_REMOTE;
618 1.1 cgd else
619 1.1 cgd pti->pt_flags &= ~PF_REMOTE;
620 1.1 cgd ttyflush(tp, FREAD|FWRITE);
621 1.1 cgd return (0);
622 1.1 cgd
623 1.2 cgd #ifdef COMPAT_43
624 1.2 cgd /* wkt */
625 1.1 cgd case TIOCSETP:
626 1.1 cgd case TIOCSETN:
627 1.2 cgd #endif
628 1.1 cgd case TIOCSETD:
629 1.1 cgd case TIOCSETA:
630 1.1 cgd case TIOCSETAW:
631 1.1 cgd case TIOCSETAF:
632 1.1 cgd while (getc(&tp->t_out) >= 0)
633 1.1 cgd ;
634 1.1 cgd break;
635 1.1 cgd
636 1.1 cgd case TIOCSIG:
637 1.1 cgd if (*(unsigned int *)data >= NSIG)
638 1.1 cgd return(EINVAL);
639 1.1 cgd if ((tp->t_lflag&NOFLSH) == 0)
640 1.1 cgd ttyflush(tp, FREAD|FWRITE);
641 1.1 cgd pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
642 1.1 cgd if ((*(unsigned int *)data == SIGINFO) &&
643 1.1 cgd ((tp->t_lflag&NOKERNINFO) == 0))
644 1.1 cgd ttyinfo(tp);
645 1.1 cgd return(0);
646 1.1 cgd }
647 1.1 cgd error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag);
648 1.1 cgd if (error < 0)
649 1.1 cgd error = ttioctl(tp, cmd, data, flag);
650 1.1 cgd /*
651 1.1 cgd * Since we use the tty queues internally,
652 1.1 cgd * pty's can't be switched to disciplines which overwrite
653 1.1 cgd * the queues. We can't tell anything about the discipline
654 1.1 cgd * from here...
655 1.1 cgd */
656 1.1 cgd if (linesw[tp->t_line].l_rint != ttyinput) {
657 1.1 cgd (*linesw[tp->t_line].l_close)(tp, flag);
658 1.1 cgd tp->t_line = TTYDISC;
659 1.1 cgd (void)(*linesw[tp->t_line].l_open)(dev, tp, flag);
660 1.1 cgd error = ENOTTY;
661 1.1 cgd }
662 1.1 cgd if (error < 0) {
663 1.1 cgd if (pti->pt_flags & PF_UCNTL &&
664 1.1 cgd (cmd & ~0xff) == UIOCCMD(0)) {
665 1.1 cgd if (cmd & 0xff) {
666 1.1 cgd pti->pt_ucntl = (u_char)cmd;
667 1.1 cgd ptcwakeup(tp, FREAD);
668 1.1 cgd }
669 1.1 cgd return (0);
670 1.1 cgd }
671 1.1 cgd error = ENOTTY;
672 1.1 cgd }
673 1.1 cgd /*
674 1.1 cgd * If external processing and packet mode send ioctl packet.
675 1.1 cgd */
676 1.1 cgd if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
677 1.1 cgd switch(cmd) {
678 1.1 cgd case TIOCSETA:
679 1.1 cgd case TIOCSETAW:
680 1.1 cgd case TIOCSETAF:
681 1.2 cgd #ifdef COMPAT_43
682 1.2 cgd /* wkt */
683 1.1 cgd case TIOCSETP:
684 1.1 cgd case TIOCSETN:
685 1.1 cgd case TIOCSETC:
686 1.1 cgd case TIOCSLTC:
687 1.1 cgd case TIOCLBIS:
688 1.1 cgd case TIOCLBIC:
689 1.1 cgd case TIOCLSET:
690 1.1 cgd #endif
691 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
692 1.1 cgd default:
693 1.1 cgd break;
694 1.1 cgd }
695 1.1 cgd }
696 1.1 cgd stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
697 1.1 cgd && CCEQ(cc[VSTART], CTRL('q'));
698 1.1 cgd if (pti->pt_flags & PF_NOSTOP) {
699 1.1 cgd if (stop) {
700 1.1 cgd pti->pt_send &= ~TIOCPKT_NOSTOP;
701 1.1 cgd pti->pt_send |= TIOCPKT_DOSTOP;
702 1.1 cgd pti->pt_flags &= ~PF_NOSTOP;
703 1.1 cgd ptcwakeup(tp, FREAD);
704 1.1 cgd }
705 1.1 cgd } else {
706 1.1 cgd if (!stop) {
707 1.1 cgd pti->pt_send &= ~TIOCPKT_DOSTOP;
708 1.1 cgd pti->pt_send |= TIOCPKT_NOSTOP;
709 1.1 cgd pti->pt_flags |= PF_NOSTOP;
710 1.1 cgd ptcwakeup(tp, FREAD);
711 1.1 cgd }
712 1.1 cgd }
713 1.1 cgd return (error);
714 1.1 cgd }
715 1.1 cgd #endif
716