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