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