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