tty_pty.c revision 1.47 1 1.47 jdolecek /* $NetBSD: tty_pty.c,v 1.47 2000/09/09 16:42:04 jdolecek 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.39 fvdl * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
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.41 thorpej
43 1.41 thorpej #include "opt_compat_sunos.h"
44 1.41 thorpej
45 1.16 mycroft #include <sys/param.h>
46 1.16 mycroft #include <sys/systm.h>
47 1.16 mycroft #include <sys/ioctl.h>
48 1.22 cgd #include <sys/proc.h>
49 1.16 mycroft #include <sys/tty.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.31 christos #include <sys/signalvar.h>
55 1.31 christos #include <sys/uio.h>
56 1.33 christos #include <sys/conf.h>
57 1.38 mycroft #include <sys/poll.h>
58 1.47 jdolecek #include <sys/malloc.h>
59 1.31 christos
60 1.47 jdolecek #define DEFAULT_NPTYS 16 /* default number of initial ptys */
61 1.47 jdolecek #define DEFAULT_MAXPTYS 512 /* default maximum number of ptys */
62 1.1 cgd
63 1.37 mycroft /* Macros to clear/set/test flags. */
64 1.37 mycroft #define SET(t, f) (t) |= (f)
65 1.37 mycroft #define CLR(t, f) (t) &= ~((unsigned)(f))
66 1.37 mycroft #define ISSET(t, f) ((t) & (f))
67 1.37 mycroft
68 1.1 cgd #define BUFSIZ 100 /* Chunk size iomoved to/from user */
69 1.1 cgd
70 1.1 cgd /*
71 1.1 cgd * pts == /dev/tty[pqrs]?
72 1.1 cgd * ptc == /dev/pty[pqrs]?
73 1.1 cgd */
74 1.27 mycroft struct pt_softc {
75 1.27 mycroft struct tty *pt_tty;
76 1.1 cgd int pt_flags;
77 1.22 cgd struct selinfo pt_selr, pt_selw;
78 1.1 cgd u_char pt_send;
79 1.1 cgd u_char pt_ucntl;
80 1.47 jdolecek };
81 1.47 jdolecek
82 1.47 jdolecek struct pt_softc **pt_softc = NULL; /* pty array */
83 1.47 jdolecek int npty = 0; /* for pstat -t */
84 1.47 jdolecek int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */
85 1.47 jdolecek struct lock pt_softc_lock;
86 1.1 cgd
87 1.1 cgd #define PF_PKT 0x08 /* packet mode */
88 1.1 cgd #define PF_STOPPED 0x10 /* user told stopped */
89 1.1 cgd #define PF_REMOTE 0x20 /* remote and flow controlled input */
90 1.1 cgd #define PF_NOSTOP 0x40
91 1.1 cgd #define PF_UCNTL 0x80 /* user control mode */
92 1.1 cgd
93 1.31 christos void ptyattach __P((int));
94 1.31 christos void ptcwakeup __P((struct tty *, int));
95 1.47 jdolecek int ptcopen __P((dev_t, int, int, struct proc *));
96 1.31 christos struct tty *ptytty __P((dev_t));
97 1.31 christos void ptsstart __P((struct tty *));
98 1.15 deraadt
99 1.47 jdolecek static struct pt_softc **ptyarralloc __P((int));
100 1.47 jdolecek static int check_pty __P((dev_t));
101 1.47 jdolecek
102 1.47 jdolecek /*
103 1.47 jdolecek * Allocate array of nelem elements.
104 1.47 jdolecek */
105 1.47 jdolecek static struct pt_softc **
106 1.47 jdolecek ptyarralloc(nelem)
107 1.47 jdolecek int nelem;
108 1.47 jdolecek {
109 1.47 jdolecek struct pt_softc **pt;
110 1.47 jdolecek nelem += 10;
111 1.47 jdolecek pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
112 1.47 jdolecek memset(pt, '\0', nelem * sizeof(struct pt_softc *));
113 1.47 jdolecek return pt;
114 1.47 jdolecek }
115 1.47 jdolecek
116 1.47 jdolecek /*
117 1.47 jdolecek * Check if the minor is correct and ensure necessary structures
118 1.47 jdolecek * are properly allocated.
119 1.47 jdolecek */
120 1.47 jdolecek static int
121 1.47 jdolecek check_pty(dev)
122 1.47 jdolecek dev_t dev;
123 1.47 jdolecek {
124 1.47 jdolecek struct pt_softc *pti;
125 1.47 jdolecek int locked = 0;
126 1.47 jdolecek
127 1.47 jdolecek if (minor(dev) >= maxptys) {
128 1.47 jdolecek tablefull("pty", "increase kern.maxptys");
129 1.47 jdolecek return (ENXIO);
130 1.47 jdolecek }
131 1.47 jdolecek
132 1.47 jdolecek if (minor(dev) >= npty) {
133 1.47 jdolecek struct pt_softc **newpt;
134 1.47 jdolecek int newnpty = npty;
135 1.47 jdolecek
136 1.47 jdolecek while(newnpty <= minor(dev))
137 1.47 jdolecek newnpty *= 2;
138 1.47 jdolecek if (newnpty > maxptys)
139 1.47 jdolecek newnpty = maxptys;
140 1.47 jdolecek
141 1.47 jdolecek newpt = ptyarralloc(newnpty);
142 1.47 jdolecek
143 1.47 jdolecek /*
144 1.47 jdolecek * Now grab the pty array lock. We need to ensure
145 1.47 jdolecek * that the pty array is consistent while copying it's
146 1.47 jdolecek * content to newly allocated, larger space.
147 1.47 jdolecek */
148 1.47 jdolecek lockmgr(&pt_softc_lock, LK_EXCLUSIVE, NULL);
149 1.47 jdolecek locked = 1;
150 1.47 jdolecek
151 1.47 jdolecek /*
152 1.47 jdolecek * If the number of ptys is still not enough, replace
153 1.47 jdolecek * the old array with the newly allocated one.
154 1.47 jdolecek */
155 1.47 jdolecek if (minor(dev) >= npty) {
156 1.47 jdolecek /*
157 1.47 jdolecek * Copy old pt_softc[] contents over, free old
158 1.47 jdolecek * array and start using new array.
159 1.47 jdolecek */
160 1.47 jdolecek memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
161 1.47 jdolecek free(pt_softc, M_DEVBUF);
162 1.47 jdolecek
163 1.47 jdolecek pt_softc = newpt;
164 1.47 jdolecek npty = newnpty;
165 1.47 jdolecek } else {
166 1.47 jdolecek /* has been enlarged while we slept, free space */
167 1.47 jdolecek free(newpt, M_DEVBUF);
168 1.47 jdolecek }
169 1.47 jdolecek }
170 1.47 jdolecek
171 1.47 jdolecek /*
172 1.47 jdolecek * If the entry is not yet allocated, allocate one. The lock is
173 1.47 jdolecek * needed so that the state of pt_softc[] arrat is consistant
174 1.47 jdolecek * should it be longened above. Though we might use shared lock
175 1.47 jdolecek * here and use
176 1.47 jdolecek *
177 1.47 jdolecek */
178 1.47 jdolecek if (!pt_softc[minor(dev)]) {
179 1.47 jdolecek if (!locked) {
180 1.47 jdolecek lockmgr(&pt_softc_lock, LK_EXCLUSIVE, NULL);
181 1.47 jdolecek locked = 1;
182 1.47 jdolecek }
183 1.47 jdolecek
184 1.47 jdolecek /*
185 1.47 jdolecek * Check the entry again - the entry might have been
186 1.47 jdolecek * added while we were waiting for lock.
187 1.47 jdolecek */
188 1.47 jdolecek if (!pt_softc[minor(dev)]) {
189 1.47 jdolecek MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
190 1.47 jdolecek M_DEVBUF, M_WAITOK);
191 1.47 jdolecek
192 1.47 jdolecek pti->pt_tty = ttymalloc();
193 1.47 jdolecek tty_attach(pti->pt_tty);
194 1.47 jdolecek
195 1.47 jdolecek pt_softc[minor(dev)] = pti;
196 1.47 jdolecek }
197 1.47 jdolecek }
198 1.47 jdolecek
199 1.47 jdolecek if (locked)
200 1.47 jdolecek lockmgr(&pt_softc_lock, LK_RELEASE, NULL);
201 1.47 jdolecek
202 1.47 jdolecek return (0);
203 1.47 jdolecek }
204 1.47 jdolecek
205 1.22 cgd /*
206 1.22 cgd * Establish n (or default if n is 1) ptys in the system.
207 1.22 cgd */
208 1.15 deraadt void
209 1.15 deraadt ptyattach(n)
210 1.15 deraadt int n;
211 1.15 deraadt {
212 1.22 cgd /* maybe should allow 0 => none? */
213 1.22 cgd if (n <= 1)
214 1.47 jdolecek n = DEFAULT_NPTYS;
215 1.47 jdolecek pt_softc = ptyarralloc(n);
216 1.22 cgd npty = n;
217 1.47 jdolecek
218 1.47 jdolecek lockinit(&pt_softc_lock, PWAIT, "ptyarrlk", 0, 0);
219 1.15 deraadt }
220 1.7 andrew
221 1.1 cgd /*ARGSUSED*/
222 1.31 christos int
223 1.1 cgd ptsopen(dev, flag, devtype, p)
224 1.1 cgd dev_t dev;
225 1.7 andrew int flag, devtype;
226 1.1 cgd struct proc *p;
227 1.1 cgd {
228 1.27 mycroft struct pt_softc *pti;
229 1.43 augustss struct tty *tp;
230 1.1 cgd int error;
231 1.1 cgd
232 1.47 jdolecek if ((error = check_pty(dev)))
233 1.47 jdolecek return (error);
234 1.47 jdolecek
235 1.47 jdolecek pti = pt_softc[minor(dev)];
236 1.47 jdolecek tp = pti->pt_tty;
237 1.47 jdolecek
238 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN)) {
239 1.1 cgd ttychars(tp); /* Set up default chars */
240 1.1 cgd tp->t_iflag = TTYDEF_IFLAG;
241 1.1 cgd tp->t_oflag = TTYDEF_OFLAG;
242 1.1 cgd tp->t_lflag = TTYDEF_LFLAG;
243 1.1 cgd tp->t_cflag = TTYDEF_CFLAG;
244 1.1 cgd tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
245 1.1 cgd ttsetwater(tp); /* would be done in xxparam() */
246 1.37 mycroft } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
247 1.1 cgd return (EBUSY);
248 1.1 cgd if (tp->t_oproc) /* Ctrlr still around. */
249 1.37 mycroft SET(tp->t_state, TS_CARR_ON);
250 1.40 mycroft if (!ISSET(flag, O_NONBLOCK))
251 1.40 mycroft while (!ISSET(tp->t_state, TS_CARR_ON)) {
252 1.40 mycroft tp->t_wopen++;
253 1.40 mycroft error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
254 1.40 mycroft ttopen, 0);
255 1.40 mycroft tp->t_wopen--;
256 1.40 mycroft if (error)
257 1.40 mycroft return (error);
258 1.40 mycroft }
259 1.22 cgd error = (*linesw[tp->t_line].l_open)(dev, tp);
260 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
261 1.22 cgd return (error);
262 1.1 cgd }
263 1.1 cgd
264 1.31 christos int
265 1.1 cgd ptsclose(dev, flag, mode, p)
266 1.1 cgd dev_t dev;
267 1.1 cgd int flag, mode;
268 1.1 cgd struct proc *p;
269 1.1 cgd {
270 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
271 1.43 augustss struct tty *tp = pti->pt_tty;
272 1.31 christos int error;
273 1.1 cgd
274 1.31 christos error = (*linesw[tp->t_line].l_close)(tp, flag);
275 1.31 christos error |= ttyclose(tp);
276 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
277 1.31 christos return (error);
278 1.1 cgd }
279 1.1 cgd
280 1.31 christos int
281 1.1 cgd ptsread(dev, uio, flag)
282 1.1 cgd dev_t dev;
283 1.1 cgd struct uio *uio;
284 1.7 andrew int flag;
285 1.1 cgd {
286 1.1 cgd struct proc *p = curproc;
287 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
288 1.43 augustss struct tty *tp = pti->pt_tty;
289 1.1 cgd int error = 0;
290 1.1 cgd
291 1.1 cgd again:
292 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
293 1.1 cgd while (isbackground(p, tp)) {
294 1.42 mycroft if (sigismember(&p->p_sigignore, SIGTTIN) ||
295 1.42 mycroft sigismember(&p->p_sigmask, SIGTTIN) ||
296 1.1 cgd p->p_pgrp->pg_jobc == 0 ||
297 1.22 cgd p->p_flag & P_PPWAIT)
298 1.1 cgd return (EIO);
299 1.1 cgd pgsignal(p->p_pgrp, SIGTTIN, 1);
300 1.35 pk error = ttysleep(tp, (caddr_t)&lbolt,
301 1.31 christos TTIPRI | PCATCH, ttybg, 0);
302 1.31 christos if (error)
303 1.1 cgd return (error);
304 1.1 cgd }
305 1.8 mycroft if (tp->t_canq.c_cc == 0) {
306 1.1 cgd if (flag & IO_NDELAY)
307 1.1 cgd return (EWOULDBLOCK);
308 1.31 christos error = ttysleep(tp, (caddr_t)&tp->t_canq,
309 1.31 christos TTIPRI | PCATCH, ttyin, 0);
310 1.31 christos if (error)
311 1.1 cgd return (error);
312 1.1 cgd goto again;
313 1.1 cgd }
314 1.8 mycroft while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
315 1.8 mycroft if (ureadc(getc(&tp->t_canq), uio) < 0) {
316 1.1 cgd error = EFAULT;
317 1.1 cgd break;
318 1.1 cgd }
319 1.8 mycroft if (tp->t_canq.c_cc == 1)
320 1.8 mycroft (void) getc(&tp->t_canq);
321 1.8 mycroft if (tp->t_canq.c_cc)
322 1.1 cgd return (error);
323 1.1 cgd } else
324 1.1 cgd if (tp->t_oproc)
325 1.1 cgd error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
326 1.1 cgd ptcwakeup(tp, FWRITE);
327 1.1 cgd return (error);
328 1.1 cgd }
329 1.1 cgd
330 1.1 cgd /*
331 1.1 cgd * Write to pseudo-tty.
332 1.1 cgd * Wakeups of controlling tty will happen
333 1.1 cgd * indirectly, when tty driver calls ptsstart.
334 1.1 cgd */
335 1.31 christos int
336 1.1 cgd ptswrite(dev, uio, flag)
337 1.1 cgd dev_t dev;
338 1.1 cgd struct uio *uio;
339 1.7 andrew int flag;
340 1.1 cgd {
341 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
342 1.43 augustss struct tty *tp = pti->pt_tty;
343 1.1 cgd
344 1.1 cgd if (tp->t_oproc == 0)
345 1.1 cgd return (EIO);
346 1.1 cgd return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
347 1.1 cgd }
348 1.1 cgd
349 1.1 cgd /*
350 1.1 cgd * Start output on pseudo-tty.
351 1.38 mycroft * Wake up process polling or sleeping for input from controlling tty.
352 1.1 cgd */
353 1.12 deraadt void
354 1.1 cgd ptsstart(tp)
355 1.1 cgd struct tty *tp;
356 1.1 cgd {
357 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
358 1.1 cgd
359 1.37 mycroft if (ISSET(tp->t_state, TS_TTSTOP))
360 1.12 deraadt return;
361 1.1 cgd if (pti->pt_flags & PF_STOPPED) {
362 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
363 1.1 cgd pti->pt_send = TIOCPKT_START;
364 1.1 cgd }
365 1.1 cgd ptcwakeup(tp, FREAD);
366 1.1 cgd }
367 1.1 cgd
368 1.36 mycroft void
369 1.31 christos ptsstop(tp, flush)
370 1.43 augustss struct tty *tp;
371 1.31 christos int flush;
372 1.31 christos {
373 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
374 1.31 christos int flag;
375 1.31 christos
376 1.31 christos /* note: FLUSHREAD and FLUSHWRITE already ok */
377 1.31 christos if (flush == 0) {
378 1.31 christos flush = TIOCPKT_STOP;
379 1.31 christos pti->pt_flags |= PF_STOPPED;
380 1.31 christos } else
381 1.31 christos pti->pt_flags &= ~PF_STOPPED;
382 1.31 christos pti->pt_send |= flush;
383 1.31 christos /* change of perspective */
384 1.31 christos flag = 0;
385 1.31 christos if (flush & FREAD)
386 1.31 christos flag |= FWRITE;
387 1.31 christos if (flush & FWRITE)
388 1.31 christos flag |= FREAD;
389 1.31 christos ptcwakeup(tp, flag);
390 1.31 christos }
391 1.31 christos
392 1.31 christos void
393 1.1 cgd ptcwakeup(tp, flag)
394 1.1 cgd struct tty *tp;
395 1.7 andrew int flag;
396 1.1 cgd {
397 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
398 1.1 cgd
399 1.1 cgd if (flag & FREAD) {
400 1.4 cgd selwakeup(&pti->pt_selr);
401 1.22 cgd wakeup((caddr_t)&tp->t_outq.c_cf);
402 1.1 cgd }
403 1.1 cgd if (flag & FWRITE) {
404 1.4 cgd selwakeup(&pti->pt_selw);
405 1.8 mycroft wakeup((caddr_t)&tp->t_rawq.c_cf);
406 1.1 cgd }
407 1.1 cgd }
408 1.1 cgd
409 1.1 cgd /*ARGSUSED*/
410 1.25 mycroft int
411 1.26 mycroft ptcopen(dev, flag, devtype, p)
412 1.1 cgd dev_t dev;
413 1.1 cgd int flag, devtype;
414 1.1 cgd struct proc *p;
415 1.1 cgd {
416 1.27 mycroft struct pt_softc *pti;
417 1.43 augustss struct tty *tp;
418 1.47 jdolecek int error;
419 1.47 jdolecek
420 1.47 jdolecek if ((error = check_pty(dev)))
421 1.47 jdolecek return (error);
422 1.47 jdolecek
423 1.47 jdolecek pti = pt_softc[minor(dev)];
424 1.47 jdolecek tp = pti->pt_tty;
425 1.1 cgd
426 1.1 cgd if (tp->t_oproc)
427 1.1 cgd return (EIO);
428 1.1 cgd tp->t_oproc = ptsstart;
429 1.1 cgd (void)(*linesw[tp->t_line].l_modem)(tp, 1);
430 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
431 1.22 cgd pti->pt_flags = 0;
432 1.1 cgd pti->pt_send = 0;
433 1.1 cgd pti->pt_ucntl = 0;
434 1.1 cgd return (0);
435 1.1 cgd }
436 1.1 cgd
437 1.31 christos /*ARGSUSED*/
438 1.25 mycroft int
439 1.31 christos ptcclose(dev, flag, devtype, p)
440 1.1 cgd dev_t dev;
441 1.31 christos int flag, devtype;
442 1.31 christos struct proc *p;
443 1.1 cgd {
444 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
445 1.43 augustss struct tty *tp = pti->pt_tty;
446 1.1 cgd
447 1.1 cgd (void)(*linesw[tp->t_line].l_modem)(tp, 0);
448 1.37 mycroft CLR(tp->t_state, TS_CARR_ON);
449 1.1 cgd tp->t_oproc = 0; /* mark closed */
450 1.2 cgd return (0);
451 1.1 cgd }
452 1.1 cgd
453 1.31 christos int
454 1.1 cgd ptcread(dev, uio, flag)
455 1.1 cgd dev_t dev;
456 1.1 cgd struct uio *uio;
457 1.7 andrew int flag;
458 1.1 cgd {
459 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
460 1.43 augustss struct tty *tp = pti->pt_tty;
461 1.22 cgd char buf[BUFSIZ];
462 1.1 cgd int error = 0, cc;
463 1.1 cgd
464 1.1 cgd /*
465 1.1 cgd * We want to block until the slave
466 1.1 cgd * is open, and there's something to read;
467 1.1 cgd * but if we lost the slave or we're NBIO,
468 1.1 cgd * then return the appropriate error instead.
469 1.1 cgd */
470 1.1 cgd for (;;) {
471 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN)) {
472 1.1 cgd if (pti->pt_flags&PF_PKT && pti->pt_send) {
473 1.1 cgd error = ureadc((int)pti->pt_send, uio);
474 1.1 cgd if (error)
475 1.1 cgd return (error);
476 1.1 cgd if (pti->pt_send & TIOCPKT_IOCTL) {
477 1.22 cgd cc = min(uio->uio_resid,
478 1.1 cgd sizeof(tp->t_termios));
479 1.31 christos uiomove((caddr_t) &tp->t_termios,
480 1.31 christos cc, uio);
481 1.1 cgd }
482 1.1 cgd pti->pt_send = 0;
483 1.1 cgd return (0);
484 1.1 cgd }
485 1.1 cgd if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
486 1.1 cgd error = ureadc((int)pti->pt_ucntl, uio);
487 1.1 cgd if (error)
488 1.1 cgd return (error);
489 1.1 cgd pti->pt_ucntl = 0;
490 1.1 cgd return (0);
491 1.1 cgd }
492 1.37 mycroft if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
493 1.1 cgd break;
494 1.1 cgd }
495 1.37 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
496 1.1 cgd return (0); /* EOF */
497 1.1 cgd if (flag & IO_NDELAY)
498 1.1 cgd return (EWOULDBLOCK);
499 1.31 christos error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
500 1.31 christos ttyin, 0);
501 1.31 christos if (error)
502 1.1 cgd return (error);
503 1.1 cgd }
504 1.1 cgd if (pti->pt_flags & (PF_PKT|PF_UCNTL))
505 1.1 cgd error = ureadc(0, uio);
506 1.1 cgd while (uio->uio_resid > 0 && error == 0) {
507 1.22 cgd cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
508 1.1 cgd if (cc <= 0)
509 1.1 cgd break;
510 1.1 cgd error = uiomove(buf, cc, uio);
511 1.1 cgd }
512 1.8 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
513 1.37 mycroft if (ISSET(tp->t_state, TS_ASLEEP)) {
514 1.37 mycroft CLR(tp->t_state, TS_ASLEEP);
515 1.8 mycroft wakeup((caddr_t)&tp->t_outq);
516 1.1 cgd }
517 1.4 cgd selwakeup(&tp->t_wsel);
518 1.1 cgd }
519 1.1 cgd return (error);
520 1.1 cgd }
521 1.1 cgd
522 1.1 cgd
523 1.31 christos int
524 1.1 cgd ptcwrite(dev, uio, flag)
525 1.1 cgd dev_t dev;
526 1.43 augustss struct uio *uio;
527 1.7 andrew int flag;
528 1.1 cgd {
529 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
530 1.43 augustss struct tty *tp = pti->pt_tty;
531 1.43 augustss u_char *cp = NULL;
532 1.43 augustss int cc = 0;
533 1.1 cgd u_char locbuf[BUFSIZ];
534 1.1 cgd int cnt = 0;
535 1.1 cgd int error = 0;
536 1.1 cgd
537 1.1 cgd again:
538 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
539 1.1 cgd goto block;
540 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
541 1.8 mycroft if (tp->t_canq.c_cc)
542 1.1 cgd goto block;
543 1.8 mycroft while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
544 1.1 cgd if (cc == 0) {
545 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
546 1.8 mycroft cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
547 1.1 cgd cp = locbuf;
548 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
549 1.1 cgd if (error)
550 1.1 cgd return (error);
551 1.1 cgd /* check again for safety */
552 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
553 1.1 cgd return (EIO);
554 1.1 cgd }
555 1.1 cgd if (cc)
556 1.22 cgd (void) b_to_q((char *)cp, cc, &tp->t_canq);
557 1.1 cgd cc = 0;
558 1.1 cgd }
559 1.8 mycroft (void) putc(0, &tp->t_canq);
560 1.1 cgd ttwakeup(tp);
561 1.8 mycroft wakeup((caddr_t)&tp->t_canq);
562 1.1 cgd return (0);
563 1.1 cgd }
564 1.1 cgd while (uio->uio_resid > 0) {
565 1.1 cgd if (cc == 0) {
566 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
567 1.1 cgd cp = locbuf;
568 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
569 1.1 cgd if (error)
570 1.1 cgd return (error);
571 1.1 cgd /* check again for safety */
572 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
573 1.1 cgd return (EIO);
574 1.1 cgd }
575 1.1 cgd while (cc > 0) {
576 1.8 mycroft if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
577 1.37 mycroft (tp->t_canq.c_cc > 0 || !ISSET(tp->t_iflag, ICANON))) {
578 1.8 mycroft wakeup((caddr_t)&tp->t_rawq);
579 1.1 cgd goto block;
580 1.1 cgd }
581 1.1 cgd (*linesw[tp->t_line].l_rint)(*cp++, tp);
582 1.1 cgd cnt++;
583 1.1 cgd cc--;
584 1.1 cgd }
585 1.1 cgd cc = 0;
586 1.1 cgd }
587 1.1 cgd return (0);
588 1.1 cgd block:
589 1.1 cgd /*
590 1.1 cgd * Come here to wait for slave to open, for space
591 1.1 cgd * in outq, or space in rawq.
592 1.1 cgd */
593 1.37 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
594 1.1 cgd return (EIO);
595 1.1 cgd if (flag & IO_NDELAY) {
596 1.1 cgd /* adjust for data copied in but not written */
597 1.1 cgd uio->uio_resid += cc;
598 1.1 cgd if (cnt == 0)
599 1.1 cgd return (EWOULDBLOCK);
600 1.1 cgd return (0);
601 1.1 cgd }
602 1.31 christos error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
603 1.31 christos ttyout, 0);
604 1.31 christos if (error) {
605 1.1 cgd /* adjust for data copied in but not written */
606 1.1 cgd uio->uio_resid += cc;
607 1.1 cgd return (error);
608 1.1 cgd }
609 1.1 cgd goto again;
610 1.29 mycroft }
611 1.29 mycroft
612 1.31 christos int
613 1.38 mycroft ptcpoll(dev, events, p)
614 1.31 christos dev_t dev;
615 1.38 mycroft int events;
616 1.31 christos struct proc *p;
617 1.31 christos {
618 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
619 1.43 augustss struct tty *tp = pti->pt_tty;
620 1.38 mycroft int revents = 0;
621 1.38 mycroft int s = splsoftclock();
622 1.31 christos
623 1.38 mycroft if (events & (POLLIN | POLLRDNORM))
624 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
625 1.38 mycroft ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
626 1.38 mycroft ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
627 1.38 mycroft ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
628 1.38 mycroft revents |= events & (POLLIN | POLLRDNORM);
629 1.31 christos
630 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
631 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
632 1.38 mycroft ((pti->pt_flags & PF_REMOTE) ?
633 1.38 mycroft (tp->t_canq.c_cc == 0) :
634 1.38 mycroft ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
635 1.38 mycroft (tp->t_canq.c_cc == 0 && ISSET(tp->t_iflag, ICANON)))))
636 1.38 mycroft revents |= events & (POLLOUT | POLLWRNORM);
637 1.31 christos
638 1.38 mycroft if (events & POLLHUP)
639 1.38 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
640 1.38 mycroft revents |= POLLHUP;
641 1.31 christos
642 1.38 mycroft if (revents == 0) {
643 1.38 mycroft if (events & (POLLIN | POLLHUP | POLLRDNORM))
644 1.38 mycroft selrecord(p, &pti->pt_selr);
645 1.31 christos
646 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
647 1.38 mycroft selrecord(p, &pti->pt_selw);
648 1.31 christos }
649 1.38 mycroft
650 1.38 mycroft splx(s);
651 1.38 mycroft return (revents);
652 1.31 christos }
653 1.31 christos
654 1.31 christos
655 1.29 mycroft struct tty *
656 1.29 mycroft ptytty(dev)
657 1.29 mycroft dev_t dev;
658 1.29 mycroft {
659 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
660 1.43 augustss struct tty *tp = pti->pt_tty;
661 1.29 mycroft
662 1.29 mycroft return (tp);
663 1.1 cgd }
664 1.1 cgd
665 1.1 cgd /*ARGSUSED*/
666 1.31 christos int
667 1.18 mycroft ptyioctl(dev, cmd, data, flag, p)
668 1.18 mycroft dev_t dev;
669 1.24 cgd u_long cmd;
670 1.1 cgd caddr_t data;
671 1.18 mycroft int flag;
672 1.18 mycroft struct proc *p;
673 1.1 cgd {
674 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
675 1.43 augustss struct tty *tp = pti->pt_tty;
676 1.43 augustss u_char *cc = tp->t_cc;
677 1.44 fvdl int stop, error, sig;
678 1.1 cgd
679 1.1 cgd /*
680 1.1 cgd * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
681 1.1 cgd * ttywflush(tp) will hang if there are characters in the outq.
682 1.1 cgd */
683 1.1 cgd if (cmd == TIOCEXT) {
684 1.1 cgd /*
685 1.1 cgd * When the EXTPROC bit is being toggled, we need
686 1.1 cgd * to send an TIOCPKT_IOCTL if the packet driver
687 1.1 cgd * is turned on.
688 1.1 cgd */
689 1.1 cgd if (*(int *)data) {
690 1.1 cgd if (pti->pt_flags & PF_PKT) {
691 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
692 1.1 cgd ptcwakeup(tp, FREAD);
693 1.1 cgd }
694 1.37 mycroft SET(tp->t_lflag, EXTPROC);
695 1.1 cgd } else {
696 1.39 fvdl if (ISSET(tp->t_lflag, EXTPROC) &&
697 1.1 cgd (pti->pt_flags & PF_PKT)) {
698 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
699 1.1 cgd ptcwakeup(tp, FREAD);
700 1.1 cgd }
701 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
702 1.1 cgd }
703 1.1 cgd return(0);
704 1.1 cgd } else
705 1.1 cgd if (cdevsw[major(dev)].d_open == ptcopen)
706 1.1 cgd switch (cmd) {
707 1.1 cgd
708 1.1 cgd case TIOCGPGRP:
709 1.35 pk #ifdef COMPAT_SUNOS
710 1.35 pk {
711 1.1 cgd /*
712 1.35 pk * I'm not sure about SunOS TIOCGPGRP semantics
713 1.35 pk * on PTYs, but it's something like this:
714 1.35 pk */
715 1.35 pk extern struct emul emul_sunos;
716 1.35 pk if (p->p_emul == &emul_sunos && tp->t_pgrp == 0)
717 1.35 pk return (EIO);
718 1.35 pk *(int *)data = tp->t_pgrp->pg_id;
719 1.35 pk return (0);
720 1.35 pk }
721 1.35 pk #endif
722 1.35 pk /*
723 1.35 pk * We avoid calling ttioctl on the controller since,
724 1.1 cgd * in that case, tp must be the controlling terminal.
725 1.1 cgd */
726 1.1 cgd *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
727 1.1 cgd return (0);
728 1.1 cgd
729 1.1 cgd case TIOCPKT:
730 1.1 cgd if (*(int *)data) {
731 1.1 cgd if (pti->pt_flags & PF_UCNTL)
732 1.1 cgd return (EINVAL);
733 1.1 cgd pti->pt_flags |= PF_PKT;
734 1.1 cgd } else
735 1.1 cgd pti->pt_flags &= ~PF_PKT;
736 1.1 cgd return (0);
737 1.1 cgd
738 1.1 cgd case TIOCUCNTL:
739 1.1 cgd if (*(int *)data) {
740 1.1 cgd if (pti->pt_flags & PF_PKT)
741 1.1 cgd return (EINVAL);
742 1.1 cgd pti->pt_flags |= PF_UCNTL;
743 1.1 cgd } else
744 1.1 cgd pti->pt_flags &= ~PF_UCNTL;
745 1.1 cgd return (0);
746 1.1 cgd
747 1.1 cgd case TIOCREMOTE:
748 1.1 cgd if (*(int *)data)
749 1.1 cgd pti->pt_flags |= PF_REMOTE;
750 1.1 cgd else
751 1.1 cgd pti->pt_flags &= ~PF_REMOTE;
752 1.1 cgd ttyflush(tp, FREAD|FWRITE);
753 1.1 cgd return (0);
754 1.1 cgd
755 1.32 christos #ifdef COMPAT_OLDTTY
756 1.35 pk case TIOCSETP:
757 1.1 cgd case TIOCSETN:
758 1.2 cgd #endif
759 1.1 cgd case TIOCSETD:
760 1.1 cgd case TIOCSETA:
761 1.1 cgd case TIOCSETAW:
762 1.1 cgd case TIOCSETAF:
763 1.21 cgd ndflush(&tp->t_outq, tp->t_outq.c_cc);
764 1.1 cgd break;
765 1.1 cgd
766 1.1 cgd case TIOCSIG:
767 1.46 mrg sig = (int)(long)*(caddr_t *)data;
768 1.44 fvdl if (sig <= 0 || sig >= NSIG)
769 1.44 fvdl return (EINVAL);
770 1.37 mycroft if (!ISSET(tp->t_lflag, NOFLSH))
771 1.1 cgd ttyflush(tp, FREAD|FWRITE);
772 1.44 fvdl pgsignal(tp->t_pgrp, sig, 1);
773 1.44 fvdl if ((sig == SIGINFO) &&
774 1.37 mycroft (!ISSET(tp->t_lflag, NOKERNINFO)))
775 1.1 cgd ttyinfo(tp);
776 1.1 cgd return(0);
777 1.1 cgd }
778 1.18 mycroft error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
779 1.1 cgd if (error < 0)
780 1.18 mycroft error = ttioctl(tp, cmd, data, flag, p);
781 1.1 cgd if (error < 0) {
782 1.1 cgd if (pti->pt_flags & PF_UCNTL &&
783 1.1 cgd (cmd & ~0xff) == UIOCCMD(0)) {
784 1.1 cgd if (cmd & 0xff) {
785 1.1 cgd pti->pt_ucntl = (u_char)cmd;
786 1.1 cgd ptcwakeup(tp, FREAD);
787 1.1 cgd }
788 1.1 cgd return (0);
789 1.1 cgd }
790 1.1 cgd error = ENOTTY;
791 1.1 cgd }
792 1.1 cgd /*
793 1.1 cgd * If external processing and packet mode send ioctl packet.
794 1.1 cgd */
795 1.37 mycroft if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
796 1.1 cgd switch(cmd) {
797 1.1 cgd case TIOCSETA:
798 1.1 cgd case TIOCSETAW:
799 1.1 cgd case TIOCSETAF:
800 1.32 christos #ifdef COMPAT_OLDTTY
801 1.1 cgd case TIOCSETP:
802 1.1 cgd case TIOCSETN:
803 1.1 cgd case TIOCSETC:
804 1.1 cgd case TIOCSLTC:
805 1.1 cgd case TIOCLBIS:
806 1.1 cgd case TIOCLBIC:
807 1.1 cgd case TIOCLSET:
808 1.1 cgd #endif
809 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
810 1.22 cgd ptcwakeup(tp, FREAD);
811 1.1 cgd default:
812 1.1 cgd break;
813 1.1 cgd }
814 1.1 cgd }
815 1.37 mycroft stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
816 1.1 cgd && CCEQ(cc[VSTART], CTRL('q'));
817 1.1 cgd if (pti->pt_flags & PF_NOSTOP) {
818 1.1 cgd if (stop) {
819 1.1 cgd pti->pt_send &= ~TIOCPKT_NOSTOP;
820 1.1 cgd pti->pt_send |= TIOCPKT_DOSTOP;
821 1.1 cgd pti->pt_flags &= ~PF_NOSTOP;
822 1.1 cgd ptcwakeup(tp, FREAD);
823 1.1 cgd }
824 1.1 cgd } else {
825 1.1 cgd if (!stop) {
826 1.1 cgd pti->pt_send &= ~TIOCPKT_DOSTOP;
827 1.1 cgd pti->pt_send |= TIOCPKT_NOSTOP;
828 1.1 cgd pti->pt_flags |= PF_NOSTOP;
829 1.1 cgd ptcwakeup(tp, FREAD);
830 1.1 cgd }
831 1.1 cgd }
832 1.1 cgd return (error);
833 1.1 cgd }
834