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