tty_pty.c revision 1.68 1 1.68 pk /* $NetBSD: tty_pty.c,v 1.68 2003/02/05 15:49:03 pk 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.57 lukem
43 1.57 lukem #include <sys/cdefs.h>
44 1.68 pk __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.68 2003/02/05 15:49:03 pk Exp $");
45 1.41 thorpej
46 1.41 thorpej #include "opt_compat_sunos.h"
47 1.41 thorpej
48 1.16 mycroft #include <sys/param.h>
49 1.16 mycroft #include <sys/systm.h>
50 1.16 mycroft #include <sys/ioctl.h>
51 1.22 cgd #include <sys/proc.h>
52 1.16 mycroft #include <sys/tty.h>
53 1.16 mycroft #include <sys/file.h>
54 1.16 mycroft #include <sys/uio.h>
55 1.16 mycroft #include <sys/kernel.h>
56 1.16 mycroft #include <sys/vnode.h>
57 1.31 christos #include <sys/signalvar.h>
58 1.31 christos #include <sys/uio.h>
59 1.33 christos #include <sys/conf.h>
60 1.38 mycroft #include <sys/poll.h>
61 1.47 jdolecek #include <sys/malloc.h>
62 1.31 christos
63 1.47 jdolecek #define DEFAULT_NPTYS 16 /* default number of initial ptys */
64 1.58 tls #define DEFAULT_MAXPTYS 992 /* default maximum number of ptys */
65 1.1 cgd
66 1.37 mycroft /* Macros to clear/set/test flags. */
67 1.37 mycroft #define SET(t, f) (t) |= (f)
68 1.37 mycroft #define CLR(t, f) (t) &= ~((unsigned)(f))
69 1.37 mycroft #define ISSET(t, f) ((t) & (f))
70 1.37 mycroft
71 1.1 cgd #define BUFSIZ 100 /* Chunk size iomoved to/from user */
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * pts == /dev/tty[pqrs]?
75 1.1 cgd * ptc == /dev/pty[pqrs]?
76 1.1 cgd */
77 1.27 mycroft struct pt_softc {
78 1.27 mycroft struct tty *pt_tty;
79 1.1 cgd int pt_flags;
80 1.22 cgd struct selinfo pt_selr, pt_selw;
81 1.1 cgd u_char pt_send;
82 1.1 cgd u_char pt_ucntl;
83 1.47 jdolecek };
84 1.47 jdolecek
85 1.48 jdolecek static struct pt_softc **pt_softc = NULL; /* pty array */
86 1.48 jdolecek static int npty = 0; /* for pstat -t */
87 1.48 jdolecek static int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */
88 1.48 jdolecek static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
89 1.1 cgd
90 1.1 cgd #define PF_PKT 0x08 /* packet mode */
91 1.1 cgd #define PF_STOPPED 0x10 /* user told stopped */
92 1.1 cgd #define PF_REMOTE 0x20 /* remote and flow controlled input */
93 1.1 cgd #define PF_NOSTOP 0x40
94 1.1 cgd #define PF_UCNTL 0x80 /* user control mode */
95 1.1 cgd
96 1.31 christos void ptyattach __P((int));
97 1.31 christos void ptcwakeup __P((struct tty *, int));
98 1.31 christos void ptsstart __P((struct tty *));
99 1.48 jdolecek int pty_maxptys __P((int, int));
100 1.15 deraadt
101 1.47 jdolecek static struct pt_softc **ptyarralloc __P((int));
102 1.47 jdolecek static int check_pty __P((dev_t));
103 1.47 jdolecek
104 1.63 gehenna dev_type_open(ptcopen);
105 1.63 gehenna dev_type_close(ptcclose);
106 1.63 gehenna dev_type_read(ptcread);
107 1.63 gehenna dev_type_write(ptcwrite);
108 1.63 gehenna dev_type_poll(ptcpoll);
109 1.65 jdolecek dev_type_kqfilter(ptckqfilter);
110 1.63 gehenna
111 1.63 gehenna dev_type_open(ptsopen);
112 1.63 gehenna dev_type_close(ptsclose);
113 1.63 gehenna dev_type_read(ptsread);
114 1.63 gehenna dev_type_write(ptswrite);
115 1.63 gehenna dev_type_stop(ptsstop);
116 1.63 gehenna dev_type_poll(ptspoll);
117 1.63 gehenna
118 1.63 gehenna dev_type_ioctl(ptyioctl);
119 1.63 gehenna dev_type_tty(ptytty);
120 1.63 gehenna
121 1.63 gehenna const struct cdevsw ptc_cdevsw = {
122 1.63 gehenna ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
123 1.65 jdolecek nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
124 1.63 gehenna };
125 1.63 gehenna
126 1.63 gehenna const struct cdevsw pts_cdevsw = {
127 1.63 gehenna ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
128 1.65 jdolecek ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
129 1.63 gehenna };
130 1.63 gehenna
131 1.63 gehenna #if defined(pmax)
132 1.63 gehenna const struct cdevsw ptc_ultrix_cdevsw = {
133 1.63 gehenna ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
134 1.65 jdolecek nullstop, ptytty, ptcpoll, nommap, ptckqfilter, D_TTY
135 1.63 gehenna };
136 1.63 gehenna
137 1.63 gehenna const struct cdevsw pts_ultrix_cdevsw = {
138 1.63 gehenna ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
139 1.65 jdolecek ptsstop, ptytty, ptspoll, nommap, ttykqfilter, D_TTY
140 1.63 gehenna };
141 1.63 gehenna #endif /* defined(pmax) */
142 1.63 gehenna
143 1.47 jdolecek /*
144 1.48 jdolecek * Allocate and zero array of nelem elements.
145 1.47 jdolecek */
146 1.47 jdolecek static struct pt_softc **
147 1.47 jdolecek ptyarralloc(nelem)
148 1.47 jdolecek int nelem;
149 1.47 jdolecek {
150 1.47 jdolecek struct pt_softc **pt;
151 1.47 jdolecek nelem += 10;
152 1.47 jdolecek pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
153 1.47 jdolecek memset(pt, '\0', nelem * sizeof(struct pt_softc *));
154 1.47 jdolecek return pt;
155 1.47 jdolecek }
156 1.47 jdolecek
157 1.47 jdolecek /*
158 1.47 jdolecek * Check if the minor is correct and ensure necessary structures
159 1.47 jdolecek * are properly allocated.
160 1.47 jdolecek */
161 1.47 jdolecek static int
162 1.47 jdolecek check_pty(dev)
163 1.47 jdolecek dev_t dev;
164 1.47 jdolecek {
165 1.47 jdolecek struct pt_softc *pti;
166 1.47 jdolecek
167 1.47 jdolecek if (minor(dev) >= npty) {
168 1.47 jdolecek struct pt_softc **newpt;
169 1.48 jdolecek int newnpty;
170 1.47 jdolecek
171 1.48 jdolecek /* check if the requested pty can be granted */
172 1.48 jdolecek if (minor(dev) >= maxptys) {
173 1.48 jdolecek limit_reached:
174 1.48 jdolecek tablefull("pty", "increase kern.maxptys");
175 1.48 jdolecek return (ENXIO);
176 1.48 jdolecek }
177 1.47 jdolecek
178 1.47 jdolecek /*
179 1.48 jdolecek * Now grab the pty array mutex - we need to ensure
180 1.47 jdolecek * that the pty array is consistent while copying it's
181 1.48 jdolecek * content to newly allocated, larger space; we also
182 1.48 jdolecek * need to be safe against pty_maxptys().
183 1.47 jdolecek */
184 1.48 jdolecek simple_lock(&pt_softc_mutex);
185 1.48 jdolecek
186 1.48 jdolecek do {
187 1.48 jdolecek for(newnpty = npty; newnpty <= minor(dev);
188 1.48 jdolecek newnpty *= 2);
189 1.48 jdolecek
190 1.48 jdolecek if (newnpty > maxptys)
191 1.48 jdolecek newnpty = maxptys;
192 1.48 jdolecek
193 1.48 jdolecek simple_unlock(&pt_softc_mutex);
194 1.48 jdolecek newpt = ptyarralloc(newnpty);
195 1.48 jdolecek simple_lock(&pt_softc_mutex);
196 1.48 jdolecek
197 1.48 jdolecek if (maxptys == npty) {
198 1.54 enami simple_unlock(&pt_softc_mutex);
199 1.48 jdolecek goto limit_reached;
200 1.48 jdolecek }
201 1.48 jdolecek } while(newnpty > maxptys);
202 1.47 jdolecek
203 1.47 jdolecek /*
204 1.48 jdolecek * If the pty array was not enlarged while we were waiting
205 1.48 jdolecek * for mutex, copy current contents of pt_softc[] to newly
206 1.48 jdolecek * allocated array and start using the new bigger array.
207 1.47 jdolecek */
208 1.47 jdolecek if (minor(dev) >= npty) {
209 1.47 jdolecek memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
210 1.47 jdolecek free(pt_softc, M_DEVBUF);
211 1.47 jdolecek
212 1.47 jdolecek pt_softc = newpt;
213 1.47 jdolecek npty = newnpty;
214 1.47 jdolecek } else {
215 1.48 jdolecek /* was enlarged when waited fot lock, free new space */
216 1.47 jdolecek free(newpt, M_DEVBUF);
217 1.47 jdolecek }
218 1.48 jdolecek
219 1.48 jdolecek simple_unlock(&pt_softc_mutex);
220 1.47 jdolecek }
221 1.47 jdolecek
222 1.47 jdolecek /*
223 1.48 jdolecek * If the entry is not yet allocated, allocate one. The mutex is
224 1.48 jdolecek * needed so that the state of pt_softc[] array is consistant
225 1.48 jdolecek * in case it has been longened above.
226 1.47 jdolecek */
227 1.47 jdolecek if (!pt_softc[minor(dev)]) {
228 1.48 jdolecek MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
229 1.48 jdolecek M_DEVBUF, M_WAITOK);
230 1.64 jdolecek memset(pti, 0, sizeof(struct pt_softc));
231 1.48 jdolecek
232 1.48 jdolecek pti->pt_tty = ttymalloc();
233 1.48 jdolecek
234 1.48 jdolecek simple_lock(&pt_softc_mutex);
235 1.47 jdolecek
236 1.47 jdolecek /*
237 1.48 jdolecek * Check the entry again - it might have been
238 1.48 jdolecek * added while we were waiting for mutex.
239 1.47 jdolecek */
240 1.47 jdolecek if (!pt_softc[minor(dev)]) {
241 1.47 jdolecek tty_attach(pti->pt_tty);
242 1.47 jdolecek pt_softc[minor(dev)] = pti;
243 1.48 jdolecek } else {
244 1.48 jdolecek ttyfree(pti->pt_tty);
245 1.48 jdolecek FREE(pti, M_DEVBUF);
246 1.47 jdolecek }
247 1.48 jdolecek
248 1.48 jdolecek simple_unlock(&pt_softc_mutex);
249 1.48 jdolecek }
250 1.48 jdolecek
251 1.48 jdolecek return (0);
252 1.48 jdolecek }
253 1.48 jdolecek
254 1.48 jdolecek /*
255 1.48 jdolecek * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
256 1.48 jdolecek * new value of maxptys.
257 1.48 jdolecek */
258 1.48 jdolecek int
259 1.48 jdolecek pty_maxptys(newmax, set)
260 1.48 jdolecek int newmax, set;
261 1.48 jdolecek {
262 1.48 jdolecek if (!set)
263 1.48 jdolecek return (maxptys);
264 1.48 jdolecek
265 1.48 jdolecek /* the value cannot be set to value lower than current number of ptys */
266 1.48 jdolecek if (newmax < npty)
267 1.48 jdolecek return (0);
268 1.48 jdolecek
269 1.48 jdolecek /* can proceed immediatelly if bigger than current maximum */
270 1.48 jdolecek if (newmax > maxptys) {
271 1.48 jdolecek maxptys = newmax;
272 1.48 jdolecek return (maxptys);
273 1.47 jdolecek }
274 1.47 jdolecek
275 1.48 jdolecek /*
276 1.48 jdolecek * We have to grab the pt_softc lock, so that we would pick correct
277 1.48 jdolecek * value of npty (might be modified in check_pty()).
278 1.48 jdolecek */
279 1.48 jdolecek simple_lock(&pt_softc_mutex);
280 1.48 jdolecek
281 1.48 jdolecek if (newmax > npty)
282 1.48 jdolecek maxptys = newmax;
283 1.47 jdolecek
284 1.48 jdolecek simple_unlock(&pt_softc_mutex);
285 1.48 jdolecek
286 1.48 jdolecek return (maxptys);
287 1.47 jdolecek }
288 1.47 jdolecek
289 1.22 cgd /*
290 1.22 cgd * Establish n (or default if n is 1) ptys in the system.
291 1.22 cgd */
292 1.15 deraadt void
293 1.15 deraadt ptyattach(n)
294 1.15 deraadt int n;
295 1.15 deraadt {
296 1.22 cgd /* maybe should allow 0 => none? */
297 1.22 cgd if (n <= 1)
298 1.47 jdolecek n = DEFAULT_NPTYS;
299 1.47 jdolecek pt_softc = ptyarralloc(n);
300 1.22 cgd npty = n;
301 1.15 deraadt }
302 1.7 andrew
303 1.1 cgd /*ARGSUSED*/
304 1.31 christos int
305 1.1 cgd ptsopen(dev, flag, devtype, p)
306 1.1 cgd dev_t dev;
307 1.7 andrew int flag, devtype;
308 1.1 cgd struct proc *p;
309 1.1 cgd {
310 1.27 mycroft struct pt_softc *pti;
311 1.43 augustss struct tty *tp;
312 1.1 cgd int error;
313 1.1 cgd
314 1.47 jdolecek if ((error = check_pty(dev)))
315 1.47 jdolecek return (error);
316 1.47 jdolecek
317 1.47 jdolecek pti = pt_softc[minor(dev)];
318 1.47 jdolecek tp = pti->pt_tty;
319 1.47 jdolecek
320 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN)) {
321 1.1 cgd ttychars(tp); /* Set up default chars */
322 1.1 cgd tp->t_iflag = TTYDEF_IFLAG;
323 1.1 cgd tp->t_oflag = TTYDEF_OFLAG;
324 1.1 cgd tp->t_lflag = TTYDEF_LFLAG;
325 1.1 cgd tp->t_cflag = TTYDEF_CFLAG;
326 1.1 cgd tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
327 1.1 cgd ttsetwater(tp); /* would be done in xxparam() */
328 1.37 mycroft } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
329 1.1 cgd return (EBUSY);
330 1.1 cgd if (tp->t_oproc) /* Ctrlr still around. */
331 1.37 mycroft SET(tp->t_state, TS_CARR_ON);
332 1.68 pk if (!ISSET(flag, O_NONBLOCK)) {
333 1.68 pk TTY_LOCK(tp);
334 1.40 mycroft while (!ISSET(tp->t_state, TS_CARR_ON)) {
335 1.40 mycroft tp->t_wopen++;
336 1.40 mycroft error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
337 1.40 mycroft ttopen, 0);
338 1.40 mycroft tp->t_wopen--;
339 1.68 pk if (error) {
340 1.68 pk TTY_UNLOCK(tp);
341 1.40 mycroft return (error);
342 1.68 pk }
343 1.40 mycroft }
344 1.68 pk TTY_UNLOCK(tp);
345 1.68 pk }
346 1.50 eeh error = (*tp->t_linesw->l_open)(dev, tp);
347 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
348 1.22 cgd return (error);
349 1.1 cgd }
350 1.1 cgd
351 1.31 christos int
352 1.1 cgd ptsclose(dev, flag, mode, p)
353 1.1 cgd dev_t dev;
354 1.1 cgd int flag, mode;
355 1.1 cgd struct proc *p;
356 1.1 cgd {
357 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
358 1.43 augustss struct tty *tp = pti->pt_tty;
359 1.31 christos int error;
360 1.1 cgd
361 1.50 eeh error = (*tp->t_linesw->l_close)(tp, flag);
362 1.31 christos error |= ttyclose(tp);
363 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
364 1.31 christos return (error);
365 1.1 cgd }
366 1.1 cgd
367 1.31 christos int
368 1.1 cgd ptsread(dev, uio, flag)
369 1.1 cgd dev_t dev;
370 1.1 cgd struct uio *uio;
371 1.7 andrew int flag;
372 1.1 cgd {
373 1.1 cgd struct proc *p = curproc;
374 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
375 1.43 augustss struct tty *tp = pti->pt_tty;
376 1.1 cgd int error = 0;
377 1.68 pk int cc;
378 1.1 cgd
379 1.1 cgd again:
380 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
381 1.1 cgd while (isbackground(p, tp)) {
382 1.51 jdolecek if (sigismasked(p, SIGTTIN) ||
383 1.1 cgd p->p_pgrp->pg_jobc == 0 ||
384 1.22 cgd p->p_flag & P_PPWAIT)
385 1.1 cgd return (EIO);
386 1.1 cgd pgsignal(p->p_pgrp, SIGTTIN, 1);
387 1.68 pk TTY_LOCK(tp);
388 1.35 pk error = ttysleep(tp, (caddr_t)&lbolt,
389 1.68 pk TTIPRI | PCATCH | PNORELOCK, ttybg, 0);
390 1.31 christos if (error)
391 1.1 cgd return (error);
392 1.1 cgd }
393 1.68 pk TTY_LOCK(tp);
394 1.8 mycroft if (tp->t_canq.c_cc == 0) {
395 1.68 pk if (flag & IO_NDELAY) {
396 1.68 pk TTY_UNLOCK(tp);
397 1.1 cgd return (EWOULDBLOCK);
398 1.68 pk }
399 1.31 christos error = ttysleep(tp, (caddr_t)&tp->t_canq,
400 1.68 pk TTIPRI | PCATCH | PNORELOCK, ttyin, 0);
401 1.31 christos if (error)
402 1.1 cgd return (error);
403 1.1 cgd goto again;
404 1.1 cgd }
405 1.68 pk while(error == 0 && tp->t_canq.c_cc > 1 && uio->uio_resid > 0) {
406 1.68 pk TTY_UNLOCK(tp);
407 1.68 pk error = ureadc(getc(&tp->t_canq), uio);
408 1.68 pk TTY_LOCK(tp);
409 1.68 pk /* Re-check terminal state here? */
410 1.68 pk }
411 1.8 mycroft if (tp->t_canq.c_cc == 1)
412 1.8 mycroft (void) getc(&tp->t_canq);
413 1.68 pk cc = tp->t_canq.c_cc;
414 1.68 pk TTY_UNLOCK(tp);
415 1.68 pk if (cc)
416 1.1 cgd return (error);
417 1.1 cgd } else
418 1.1 cgd if (tp->t_oproc)
419 1.50 eeh error = (*tp->t_linesw->l_read)(tp, uio, flag);
420 1.1 cgd ptcwakeup(tp, FWRITE);
421 1.1 cgd return (error);
422 1.1 cgd }
423 1.1 cgd
424 1.1 cgd /*
425 1.1 cgd * Write to pseudo-tty.
426 1.1 cgd * Wakeups of controlling tty will happen
427 1.1 cgd * indirectly, when tty driver calls ptsstart.
428 1.1 cgd */
429 1.31 christos int
430 1.1 cgd ptswrite(dev, uio, flag)
431 1.1 cgd dev_t dev;
432 1.1 cgd struct uio *uio;
433 1.7 andrew int flag;
434 1.1 cgd {
435 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
436 1.43 augustss struct tty *tp = pti->pt_tty;
437 1.1 cgd
438 1.1 cgd if (tp->t_oproc == 0)
439 1.1 cgd return (EIO);
440 1.50 eeh return ((*tp->t_linesw->l_write)(tp, uio, flag));
441 1.56 scw }
442 1.56 scw
443 1.56 scw /*
444 1.56 scw * Poll pseudo-tty.
445 1.56 scw */
446 1.56 scw int
447 1.56 scw ptspoll(dev, events, p)
448 1.56 scw dev_t dev;
449 1.56 scw int events;
450 1.56 scw struct proc *p;
451 1.56 scw {
452 1.56 scw struct pt_softc *pti = pt_softc[minor(dev)];
453 1.56 scw struct tty *tp = pti->pt_tty;
454 1.56 scw
455 1.56 scw if (tp->t_oproc == 0)
456 1.56 scw return (EIO);
457 1.56 scw
458 1.56 scw return ((*tp->t_linesw->l_poll)(tp, events, p));
459 1.1 cgd }
460 1.1 cgd
461 1.1 cgd /*
462 1.1 cgd * Start output on pseudo-tty.
463 1.38 mycroft * Wake up process polling or sleeping for input from controlling tty.
464 1.68 pk * Called with tty slock held.
465 1.1 cgd */
466 1.12 deraadt void
467 1.1 cgd ptsstart(tp)
468 1.1 cgd struct tty *tp;
469 1.1 cgd {
470 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
471 1.1 cgd
472 1.37 mycroft if (ISSET(tp->t_state, TS_TTSTOP))
473 1.12 deraadt return;
474 1.1 cgd if (pti->pt_flags & PF_STOPPED) {
475 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
476 1.1 cgd pti->pt_send = TIOCPKT_START;
477 1.1 cgd }
478 1.68 pk
479 1.68 pk selnotify(&pti->pt_selr, 0);
480 1.68 pk wakeup((caddr_t)&tp->t_outq.c_cf);
481 1.1 cgd }
482 1.1 cgd
483 1.68 pk /*
484 1.68 pk * Stop output.
485 1.68 pk * Called with tty slock held.
486 1.68 pk */
487 1.36 mycroft void
488 1.31 christos ptsstop(tp, flush)
489 1.43 augustss struct tty *tp;
490 1.31 christos int flush;
491 1.31 christos {
492 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
493 1.31 christos
494 1.31 christos /* note: FLUSHREAD and FLUSHWRITE already ok */
495 1.31 christos if (flush == 0) {
496 1.31 christos flush = TIOCPKT_STOP;
497 1.31 christos pti->pt_flags |= PF_STOPPED;
498 1.31 christos } else
499 1.31 christos pti->pt_flags &= ~PF_STOPPED;
500 1.31 christos pti->pt_send |= flush;
501 1.68 pk
502 1.31 christos /* change of perspective */
503 1.68 pk if (flush & FREAD) {
504 1.68 pk selnotify(&pti->pt_selw, 0);
505 1.68 pk wakeup((caddr_t)&tp->t_rawq.c_cf);
506 1.68 pk }
507 1.68 pk if (flush & FWRITE) {
508 1.68 pk selnotify(&pti->pt_selr, 0);
509 1.68 pk wakeup((caddr_t)&tp->t_outq.c_cf);
510 1.68 pk }
511 1.31 christos }
512 1.31 christos
513 1.31 christos void
514 1.1 cgd ptcwakeup(tp, flag)
515 1.1 cgd struct tty *tp;
516 1.7 andrew int flag;
517 1.1 cgd {
518 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
519 1.1 cgd
520 1.68 pk TTY_LOCK(tp);
521 1.1 cgd if (flag & FREAD) {
522 1.65 jdolecek selnotify(&pti->pt_selr, 0);
523 1.22 cgd wakeup((caddr_t)&tp->t_outq.c_cf);
524 1.1 cgd }
525 1.1 cgd if (flag & FWRITE) {
526 1.65 jdolecek selnotify(&pti->pt_selw, 0);
527 1.8 mycroft wakeup((caddr_t)&tp->t_rawq.c_cf);
528 1.1 cgd }
529 1.68 pk TTY_UNLOCK(tp);
530 1.1 cgd }
531 1.1 cgd
532 1.1 cgd /*ARGSUSED*/
533 1.25 mycroft int
534 1.26 mycroft ptcopen(dev, flag, devtype, p)
535 1.1 cgd dev_t dev;
536 1.1 cgd int flag, devtype;
537 1.1 cgd struct proc *p;
538 1.1 cgd {
539 1.27 mycroft struct pt_softc *pti;
540 1.43 augustss struct tty *tp;
541 1.47 jdolecek int error;
542 1.47 jdolecek
543 1.47 jdolecek if ((error = check_pty(dev)))
544 1.47 jdolecek return (error);
545 1.47 jdolecek
546 1.47 jdolecek pti = pt_softc[minor(dev)];
547 1.47 jdolecek tp = pti->pt_tty;
548 1.1 cgd
549 1.1 cgd if (tp->t_oproc)
550 1.1 cgd return (EIO);
551 1.1 cgd tp->t_oproc = ptsstart;
552 1.50 eeh (void)(*tp->t_linesw->l_modem)(tp, 1);
553 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
554 1.22 cgd pti->pt_flags = 0;
555 1.1 cgd pti->pt_send = 0;
556 1.1 cgd pti->pt_ucntl = 0;
557 1.1 cgd return (0);
558 1.1 cgd }
559 1.1 cgd
560 1.31 christos /*ARGSUSED*/
561 1.25 mycroft int
562 1.31 christos ptcclose(dev, flag, devtype, p)
563 1.1 cgd dev_t dev;
564 1.31 christos int flag, devtype;
565 1.31 christos struct proc *p;
566 1.1 cgd {
567 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
568 1.43 augustss struct tty *tp = pti->pt_tty;
569 1.1 cgd
570 1.50 eeh (void)(*tp->t_linesw->l_modem)(tp, 0);
571 1.37 mycroft CLR(tp->t_state, TS_CARR_ON);
572 1.1 cgd tp->t_oproc = 0; /* mark closed */
573 1.2 cgd return (0);
574 1.1 cgd }
575 1.1 cgd
576 1.31 christos int
577 1.1 cgd ptcread(dev, uio, flag)
578 1.1 cgd dev_t dev;
579 1.1 cgd struct uio *uio;
580 1.7 andrew int flag;
581 1.1 cgd {
582 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
583 1.43 augustss struct tty *tp = pti->pt_tty;
584 1.67 simonb u_char buf[BUFSIZ];
585 1.1 cgd int error = 0, cc;
586 1.1 cgd
587 1.1 cgd /*
588 1.1 cgd * We want to block until the slave
589 1.1 cgd * is open, and there's something to read;
590 1.1 cgd * but if we lost the slave or we're NBIO,
591 1.1 cgd * then return the appropriate error instead.
592 1.1 cgd */
593 1.68 pk TTY_LOCK(tp);
594 1.1 cgd for (;;) {
595 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN)) {
596 1.1 cgd if (pti->pt_flags&PF_PKT && pti->pt_send) {
597 1.68 pk TTY_UNLOCK(tp);
598 1.1 cgd error = ureadc((int)pti->pt_send, uio);
599 1.1 cgd if (error)
600 1.1 cgd return (error);
601 1.68 pk /*
602 1.68 pk * Since we don't have the tty locked, there's
603 1.68 pk * a risk of messing up `t_termios'. This is
604 1.68 pk * relevant only if the tty got closed and then
605 1.68 pk * opened again while we were out uiomoving.
606 1.68 pk */
607 1.1 cgd if (pti->pt_send & TIOCPKT_IOCTL) {
608 1.22 cgd cc = min(uio->uio_resid,
609 1.1 cgd sizeof(tp->t_termios));
610 1.31 christos uiomove((caddr_t) &tp->t_termios,
611 1.31 christos cc, uio);
612 1.1 cgd }
613 1.1 cgd pti->pt_send = 0;
614 1.1 cgd return (0);
615 1.1 cgd }
616 1.1 cgd if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
617 1.68 pk TTY_UNLOCK(tp);
618 1.1 cgd error = ureadc((int)pti->pt_ucntl, uio);
619 1.1 cgd if (error)
620 1.1 cgd return (error);
621 1.1 cgd pti->pt_ucntl = 0;
622 1.1 cgd return (0);
623 1.1 cgd }
624 1.37 mycroft if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
625 1.1 cgd break;
626 1.1 cgd }
627 1.68 pk if (!ISSET(tp->t_state, TS_CARR_ON)) {
628 1.68 pk error = 0; /* EOF */
629 1.68 pk goto out;
630 1.68 pk }
631 1.68 pk if (flag & IO_NDELAY) {
632 1.68 pk error = EWOULDBLOCK;
633 1.68 pk goto out;
634 1.68 pk }
635 1.68 pk error = ltsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
636 1.68 pk ttyin, 0, &tp->t_slock);
637 1.31 christos if (error)
638 1.68 pk goto out;
639 1.1 cgd }
640 1.68 pk
641 1.68 pk if (pti->pt_flags & (PF_PKT|PF_UCNTL)) {
642 1.68 pk TTY_UNLOCK(tp);
643 1.1 cgd error = ureadc(0, uio);
644 1.68 pk TTY_LOCK(tp);
645 1.68 pk if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
646 1.68 pk error = EIO;
647 1.68 pk }
648 1.1 cgd while (uio->uio_resid > 0 && error == 0) {
649 1.22 cgd cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
650 1.1 cgd if (cc <= 0)
651 1.1 cgd break;
652 1.68 pk TTY_UNLOCK(tp);
653 1.1 cgd error = uiomove(buf, cc, uio);
654 1.68 pk TTY_LOCK(tp);
655 1.68 pk if (error == 0 && !ISSET(tp->t_state, TS_ISOPEN))
656 1.68 pk error = EIO;
657 1.1 cgd }
658 1.68 pk
659 1.8 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
660 1.37 mycroft if (ISSET(tp->t_state, TS_ASLEEP)) {
661 1.37 mycroft CLR(tp->t_state, TS_ASLEEP);
662 1.8 mycroft wakeup((caddr_t)&tp->t_outq);
663 1.1 cgd }
664 1.65 jdolecek selnotify(&tp->t_wsel, 0);
665 1.1 cgd }
666 1.68 pk out:
667 1.68 pk TTY_UNLOCK(tp);
668 1.1 cgd return (error);
669 1.1 cgd }
670 1.1 cgd
671 1.1 cgd
672 1.31 christos int
673 1.1 cgd ptcwrite(dev, uio, flag)
674 1.1 cgd dev_t dev;
675 1.43 augustss struct uio *uio;
676 1.7 andrew int flag;
677 1.1 cgd {
678 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
679 1.43 augustss struct tty *tp = pti->pt_tty;
680 1.43 augustss u_char *cp = NULL;
681 1.43 augustss int cc = 0;
682 1.1 cgd u_char locbuf[BUFSIZ];
683 1.1 cgd int cnt = 0;
684 1.1 cgd int error = 0;
685 1.1 cgd
686 1.1 cgd again:
687 1.68 pk TTY_LOCK(tp);
688 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
689 1.1 cgd goto block;
690 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
691 1.8 mycroft if (tp->t_canq.c_cc)
692 1.1 cgd goto block;
693 1.8 mycroft while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
694 1.1 cgd if (cc == 0) {
695 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
696 1.8 mycroft cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
697 1.1 cgd cp = locbuf;
698 1.68 pk TTY_UNLOCK(tp);
699 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
700 1.1 cgd if (error)
701 1.1 cgd return (error);
702 1.68 pk TTY_LOCK(tp);
703 1.1 cgd /* check again for safety */
704 1.68 pk if (!ISSET(tp->t_state, TS_ISOPEN)) {
705 1.68 pk error = EIO;
706 1.68 pk goto out;
707 1.68 pk }
708 1.1 cgd }
709 1.1 cgd if (cc)
710 1.67 simonb (void) b_to_q(cp, cc, &tp->t_canq);
711 1.1 cgd cc = 0;
712 1.1 cgd }
713 1.8 mycroft (void) putc(0, &tp->t_canq);
714 1.1 cgd ttwakeup(tp);
715 1.8 mycroft wakeup((caddr_t)&tp->t_canq);
716 1.68 pk error = 0;
717 1.68 pk goto out;
718 1.1 cgd }
719 1.1 cgd while (uio->uio_resid > 0) {
720 1.1 cgd if (cc == 0) {
721 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
722 1.1 cgd cp = locbuf;
723 1.68 pk TTY_UNLOCK(tp);
724 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
725 1.1 cgd if (error)
726 1.1 cgd return (error);
727 1.68 pk TTY_LOCK(tp);
728 1.1 cgd /* check again for safety */
729 1.68 pk if (!ISSET(tp->t_state, TS_ISOPEN)) {
730 1.68 pk error = EIO;
731 1.68 pk goto out;
732 1.68 pk }
733 1.1 cgd }
734 1.1 cgd while (cc > 0) {
735 1.8 mycroft if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
736 1.59 christos (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
737 1.8 mycroft wakeup((caddr_t)&tp->t_rawq);
738 1.1 cgd goto block;
739 1.1 cgd }
740 1.68 pk /* XXX - should change l_rint to be called with lock
741 1.68 pk * see also tty.c:ttyinput_wlock()
742 1.68 pk */
743 1.68 pk TTY_UNLOCK(tp);
744 1.50 eeh (*tp->t_linesw->l_rint)(*cp++, tp);
745 1.68 pk TTY_LOCK(tp);
746 1.1 cgd cnt++;
747 1.1 cgd cc--;
748 1.1 cgd }
749 1.1 cgd cc = 0;
750 1.1 cgd }
751 1.68 pk error = 0;
752 1.68 pk goto out;
753 1.68 pk
754 1.1 cgd block:
755 1.1 cgd /*
756 1.1 cgd * Come here to wait for slave to open, for space
757 1.1 cgd * in outq, or space in rawq.
758 1.1 cgd */
759 1.68 pk if (!ISSET(tp->t_state, TS_CARR_ON)) {
760 1.68 pk error = EIO;
761 1.68 pk goto out;
762 1.68 pk }
763 1.1 cgd if (flag & IO_NDELAY) {
764 1.1 cgd /* adjust for data copied in but not written */
765 1.1 cgd uio->uio_resid += cc;
766 1.68 pk error = cnt == 0 ? EWOULDBLOCK : 0;
767 1.68 pk goto out;
768 1.1 cgd }
769 1.68 pk error = ltsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH | PNORELOCK,
770 1.68 pk ttyout, 0, &tp->t_slock);
771 1.31 christos if (error) {
772 1.1 cgd /* adjust for data copied in but not written */
773 1.1 cgd uio->uio_resid += cc;
774 1.1 cgd return (error);
775 1.1 cgd }
776 1.1 cgd goto again;
777 1.68 pk
778 1.68 pk out:
779 1.68 pk TTY_UNLOCK(tp);
780 1.68 pk return (error);
781 1.29 mycroft }
782 1.29 mycroft
783 1.31 christos int
784 1.38 mycroft ptcpoll(dev, events, p)
785 1.31 christos dev_t dev;
786 1.38 mycroft int events;
787 1.31 christos struct proc *p;
788 1.31 christos {
789 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
790 1.43 augustss struct tty *tp = pti->pt_tty;
791 1.38 mycroft int revents = 0;
792 1.38 mycroft int s = splsoftclock();
793 1.31 christos
794 1.38 mycroft if (events & (POLLIN | POLLRDNORM))
795 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
796 1.38 mycroft ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
797 1.38 mycroft ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
798 1.38 mycroft ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
799 1.38 mycroft revents |= events & (POLLIN | POLLRDNORM);
800 1.31 christos
801 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
802 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
803 1.38 mycroft ((pti->pt_flags & PF_REMOTE) ?
804 1.38 mycroft (tp->t_canq.c_cc == 0) :
805 1.38 mycroft ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
806 1.59 christos (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
807 1.38 mycroft revents |= events & (POLLOUT | POLLWRNORM);
808 1.31 christos
809 1.38 mycroft if (events & POLLHUP)
810 1.38 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
811 1.38 mycroft revents |= POLLHUP;
812 1.31 christos
813 1.38 mycroft if (revents == 0) {
814 1.38 mycroft if (events & (POLLIN | POLLHUP | POLLRDNORM))
815 1.38 mycroft selrecord(p, &pti->pt_selr);
816 1.31 christos
817 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
818 1.38 mycroft selrecord(p, &pti->pt_selw);
819 1.31 christos }
820 1.38 mycroft
821 1.38 mycroft splx(s);
822 1.38 mycroft return (revents);
823 1.31 christos }
824 1.31 christos
825 1.65 jdolecek static void
826 1.65 jdolecek filt_ptcrdetach(struct knote *kn)
827 1.65 jdolecek {
828 1.65 jdolecek struct pt_softc *pti;
829 1.65 jdolecek int s;
830 1.65 jdolecek
831 1.65 jdolecek pti = kn->kn_hook;
832 1.65 jdolecek s = spltty();
833 1.66 christos SLIST_REMOVE(&pti->pt_selr.sel_klist, kn, knote, kn_selnext);
834 1.65 jdolecek splx(s);
835 1.65 jdolecek }
836 1.65 jdolecek
837 1.65 jdolecek static int
838 1.65 jdolecek filt_ptcread(struct knote *kn, long hint)
839 1.65 jdolecek {
840 1.65 jdolecek struct pt_softc *pti;
841 1.65 jdolecek struct tty *tp;
842 1.65 jdolecek int canread;
843 1.65 jdolecek
844 1.65 jdolecek pti = kn->kn_hook;
845 1.65 jdolecek tp = pti->pt_tty;
846 1.65 jdolecek
847 1.65 jdolecek canread = (ISSET(tp->t_state, TS_ISOPEN) &&
848 1.65 jdolecek ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
849 1.65 jdolecek ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
850 1.65 jdolecek ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)));
851 1.65 jdolecek
852 1.65 jdolecek if (canread) {
853 1.65 jdolecek /*
854 1.65 jdolecek * c_cc is number of characters after output post-processing;
855 1.65 jdolecek * the amount of data actually read(2) depends on
856 1.65 jdolecek * setting of input flags for the terminal.
857 1.65 jdolecek */
858 1.65 jdolecek kn->kn_data = tp->t_outq.c_cc;
859 1.65 jdolecek if (((pti->pt_flags & PF_PKT) && pti->pt_send) ||
860 1.65 jdolecek ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))
861 1.65 jdolecek kn->kn_data++;
862 1.65 jdolecek }
863 1.65 jdolecek
864 1.65 jdolecek return (canread);
865 1.65 jdolecek }
866 1.65 jdolecek
867 1.65 jdolecek static void
868 1.65 jdolecek filt_ptcwdetach(struct knote *kn)
869 1.65 jdolecek {
870 1.65 jdolecek struct pt_softc *pti;
871 1.65 jdolecek int s;
872 1.65 jdolecek
873 1.65 jdolecek pti = kn->kn_hook;
874 1.65 jdolecek s = spltty();
875 1.66 christos SLIST_REMOVE(&pti->pt_selw.sel_klist, kn, knote, kn_selnext);
876 1.65 jdolecek splx(s);
877 1.65 jdolecek }
878 1.65 jdolecek
879 1.65 jdolecek static int
880 1.65 jdolecek filt_ptcwrite(struct knote *kn, long hint)
881 1.65 jdolecek {
882 1.65 jdolecek struct pt_softc *pti;
883 1.65 jdolecek struct tty *tp;
884 1.65 jdolecek int canwrite;
885 1.65 jdolecek int nwrite;
886 1.65 jdolecek
887 1.65 jdolecek pti = kn->kn_hook;
888 1.65 jdolecek tp = pti->pt_tty;
889 1.65 jdolecek
890 1.65 jdolecek canwrite = (ISSET(tp->t_state, TS_ISOPEN) &&
891 1.65 jdolecek ((pti->pt_flags & PF_REMOTE) ?
892 1.65 jdolecek (tp->t_canq.c_cc == 0) :
893 1.65 jdolecek ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
894 1.65 jdolecek (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))));
895 1.65 jdolecek
896 1.65 jdolecek if (canwrite) {
897 1.65 jdolecek if (pti->pt_flags & PF_REMOTE)
898 1.65 jdolecek nwrite = tp->t_canq.c_cn;
899 1.65 jdolecek else {
900 1.65 jdolecek /* this is guaranteed to be > 0 due to above check */
901 1.65 jdolecek nwrite = tp->t_canq.c_cn
902 1.65 jdolecek - (tp->t_rawq.c_cc + tp->t_canq.c_cc);
903 1.65 jdolecek }
904 1.65 jdolecek kn->kn_data = nwrite;
905 1.65 jdolecek }
906 1.65 jdolecek
907 1.65 jdolecek return (canwrite);
908 1.65 jdolecek }
909 1.65 jdolecek
910 1.65 jdolecek static const struct filterops ptcread_filtops =
911 1.65 jdolecek { 1, NULL, filt_ptcrdetach, filt_ptcread };
912 1.65 jdolecek static const struct filterops ptcwrite_filtops =
913 1.65 jdolecek { 1, NULL, filt_ptcwdetach, filt_ptcwrite };
914 1.65 jdolecek
915 1.65 jdolecek int
916 1.65 jdolecek ptckqfilter(dev_t dev, struct knote *kn)
917 1.65 jdolecek {
918 1.65 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
919 1.65 jdolecek struct klist *klist;
920 1.65 jdolecek int s;
921 1.65 jdolecek
922 1.65 jdolecek switch (kn->kn_filter) {
923 1.65 jdolecek case EVFILT_READ:
924 1.66 christos klist = &pti->pt_selr.sel_klist;
925 1.65 jdolecek kn->kn_fop = &ptcread_filtops;
926 1.65 jdolecek break;
927 1.65 jdolecek case EVFILT_WRITE:
928 1.66 christos klist = &pti->pt_selw.sel_klist;
929 1.65 jdolecek kn->kn_fop = &ptcwrite_filtops;
930 1.65 jdolecek break;
931 1.65 jdolecek default:
932 1.65 jdolecek return (1);
933 1.65 jdolecek }
934 1.65 jdolecek
935 1.65 jdolecek kn->kn_hook = pti;
936 1.65 jdolecek
937 1.65 jdolecek s = spltty();
938 1.65 jdolecek SLIST_INSERT_HEAD(klist, kn, kn_selnext);
939 1.65 jdolecek splx(s);
940 1.65 jdolecek
941 1.65 jdolecek return (0);
942 1.65 jdolecek }
943 1.31 christos
944 1.29 mycroft struct tty *
945 1.29 mycroft ptytty(dev)
946 1.29 mycroft dev_t dev;
947 1.29 mycroft {
948 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
949 1.43 augustss struct tty *tp = pti->pt_tty;
950 1.29 mycroft
951 1.29 mycroft return (tp);
952 1.1 cgd }
953 1.1 cgd
954 1.1 cgd /*ARGSUSED*/
955 1.31 christos int
956 1.18 mycroft ptyioctl(dev, cmd, data, flag, p)
957 1.18 mycroft dev_t dev;
958 1.24 cgd u_long cmd;
959 1.1 cgd caddr_t data;
960 1.18 mycroft int flag;
961 1.18 mycroft struct proc *p;
962 1.1 cgd {
963 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
964 1.43 augustss struct tty *tp = pti->pt_tty;
965 1.63 gehenna const struct cdevsw *cdev;
966 1.43 augustss u_char *cc = tp->t_cc;
967 1.44 fvdl int stop, error, sig;
968 1.1 cgd
969 1.63 gehenna cdev = cdevsw_lookup(dev);
970 1.1 cgd /*
971 1.1 cgd * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
972 1.1 cgd * ttywflush(tp) will hang if there are characters in the outq.
973 1.1 cgd */
974 1.1 cgd if (cmd == TIOCEXT) {
975 1.1 cgd /*
976 1.1 cgd * When the EXTPROC bit is being toggled, we need
977 1.1 cgd * to send an TIOCPKT_IOCTL if the packet driver
978 1.1 cgd * is turned on.
979 1.1 cgd */
980 1.1 cgd if (*(int *)data) {
981 1.1 cgd if (pti->pt_flags & PF_PKT) {
982 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
983 1.1 cgd ptcwakeup(tp, FREAD);
984 1.1 cgd }
985 1.37 mycroft SET(tp->t_lflag, EXTPROC);
986 1.1 cgd } else {
987 1.39 fvdl if (ISSET(tp->t_lflag, EXTPROC) &&
988 1.1 cgd (pti->pt_flags & PF_PKT)) {
989 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
990 1.1 cgd ptcwakeup(tp, FREAD);
991 1.1 cgd }
992 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
993 1.1 cgd }
994 1.1 cgd return(0);
995 1.1 cgd } else
996 1.63 gehenna if (cdev != NULL && cdev->d_open == ptcopen)
997 1.1 cgd switch (cmd) {
998 1.1 cgd
999 1.1 cgd case TIOCGPGRP:
1000 1.35 pk /*
1001 1.35 pk * We avoid calling ttioctl on the controller since,
1002 1.1 cgd * in that case, tp must be the controlling terminal.
1003 1.1 cgd */
1004 1.1 cgd *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
1005 1.1 cgd return (0);
1006 1.1 cgd
1007 1.1 cgd case TIOCPKT:
1008 1.1 cgd if (*(int *)data) {
1009 1.1 cgd if (pti->pt_flags & PF_UCNTL)
1010 1.1 cgd return (EINVAL);
1011 1.1 cgd pti->pt_flags |= PF_PKT;
1012 1.1 cgd } else
1013 1.1 cgd pti->pt_flags &= ~PF_PKT;
1014 1.1 cgd return (0);
1015 1.1 cgd
1016 1.1 cgd case TIOCUCNTL:
1017 1.1 cgd if (*(int *)data) {
1018 1.1 cgd if (pti->pt_flags & PF_PKT)
1019 1.1 cgd return (EINVAL);
1020 1.1 cgd pti->pt_flags |= PF_UCNTL;
1021 1.1 cgd } else
1022 1.1 cgd pti->pt_flags &= ~PF_UCNTL;
1023 1.1 cgd return (0);
1024 1.1 cgd
1025 1.1 cgd case TIOCREMOTE:
1026 1.1 cgd if (*(int *)data)
1027 1.1 cgd pti->pt_flags |= PF_REMOTE;
1028 1.1 cgd else
1029 1.1 cgd pti->pt_flags &= ~PF_REMOTE;
1030 1.68 pk TTY_LOCK(tp);
1031 1.1 cgd ttyflush(tp, FREAD|FWRITE);
1032 1.68 pk TTY_UNLOCK(tp);
1033 1.1 cgd return (0);
1034 1.1 cgd
1035 1.32 christos #ifdef COMPAT_OLDTTY
1036 1.35 pk case TIOCSETP:
1037 1.1 cgd case TIOCSETN:
1038 1.2 cgd #endif
1039 1.1 cgd case TIOCSETD:
1040 1.1 cgd case TIOCSETA:
1041 1.1 cgd case TIOCSETAW:
1042 1.1 cgd case TIOCSETAF:
1043 1.68 pk TTY_LOCK(tp);
1044 1.21 cgd ndflush(&tp->t_outq, tp->t_outq.c_cc);
1045 1.68 pk TTY_UNLOCK(tp);
1046 1.1 cgd break;
1047 1.1 cgd
1048 1.1 cgd case TIOCSIG:
1049 1.46 mrg sig = (int)(long)*(caddr_t *)data;
1050 1.44 fvdl if (sig <= 0 || sig >= NSIG)
1051 1.44 fvdl return (EINVAL);
1052 1.68 pk TTY_LOCK(tp);
1053 1.37 mycroft if (!ISSET(tp->t_lflag, NOFLSH))
1054 1.1 cgd ttyflush(tp, FREAD|FWRITE);
1055 1.44 fvdl if ((sig == SIGINFO) &&
1056 1.37 mycroft (!ISSET(tp->t_lflag, NOKERNINFO)))
1057 1.1 cgd ttyinfo(tp);
1058 1.68 pk TTY_UNLOCK(tp);
1059 1.62 itohy pgsignal(tp->t_pgrp, sig, 1);
1060 1.1 cgd return(0);
1061 1.1 cgd }
1062 1.50 eeh error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
1063 1.61 atatat if (error == EPASSTHROUGH)
1064 1.18 mycroft error = ttioctl(tp, cmd, data, flag, p);
1065 1.61 atatat if (error == EPASSTHROUGH) {
1066 1.1 cgd if (pti->pt_flags & PF_UCNTL &&
1067 1.1 cgd (cmd & ~0xff) == UIOCCMD(0)) {
1068 1.1 cgd if (cmd & 0xff) {
1069 1.1 cgd pti->pt_ucntl = (u_char)cmd;
1070 1.1 cgd ptcwakeup(tp, FREAD);
1071 1.1 cgd }
1072 1.1 cgd return (0);
1073 1.1 cgd }
1074 1.1 cgd }
1075 1.1 cgd /*
1076 1.1 cgd * If external processing and packet mode send ioctl packet.
1077 1.1 cgd */
1078 1.37 mycroft if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
1079 1.1 cgd switch(cmd) {
1080 1.1 cgd case TIOCSETA:
1081 1.1 cgd case TIOCSETAW:
1082 1.1 cgd case TIOCSETAF:
1083 1.32 christos #ifdef COMPAT_OLDTTY
1084 1.1 cgd case TIOCSETP:
1085 1.1 cgd case TIOCSETN:
1086 1.1 cgd case TIOCSETC:
1087 1.1 cgd case TIOCSLTC:
1088 1.1 cgd case TIOCLBIS:
1089 1.1 cgd case TIOCLBIC:
1090 1.1 cgd case TIOCLSET:
1091 1.1 cgd #endif
1092 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
1093 1.22 cgd ptcwakeup(tp, FREAD);
1094 1.1 cgd default:
1095 1.1 cgd break;
1096 1.1 cgd }
1097 1.1 cgd }
1098 1.37 mycroft stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1099 1.1 cgd && CCEQ(cc[VSTART], CTRL('q'));
1100 1.1 cgd if (pti->pt_flags & PF_NOSTOP) {
1101 1.1 cgd if (stop) {
1102 1.1 cgd pti->pt_send &= ~TIOCPKT_NOSTOP;
1103 1.1 cgd pti->pt_send |= TIOCPKT_DOSTOP;
1104 1.1 cgd pti->pt_flags &= ~PF_NOSTOP;
1105 1.1 cgd ptcwakeup(tp, FREAD);
1106 1.1 cgd }
1107 1.1 cgd } else {
1108 1.1 cgd if (!stop) {
1109 1.1 cgd pti->pt_send &= ~TIOCPKT_DOSTOP;
1110 1.1 cgd pti->pt_send |= TIOCPKT_NOSTOP;
1111 1.1 cgd pti->pt_flags |= PF_NOSTOP;
1112 1.1 cgd ptcwakeup(tp, FREAD);
1113 1.1 cgd }
1114 1.1 cgd }
1115 1.1 cgd return (error);
1116 1.1 cgd }
1117