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