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