tty_pty.c revision 1.63 1 1.63 gehenna /* $NetBSD: tty_pty.c,v 1.63 2002/09/06 13:18:43 gehenna 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.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.39 fvdl * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
36 1.1 cgd */
37 1.1 cgd
38 1.1 cgd /*
39 1.1 cgd * Pseudo-teletype Driver
40 1.1 cgd * (Actually two drivers, requiring two entries in 'cdevsw')
41 1.1 cgd */
42 1.57 lukem
43 1.57 lukem #include <sys/cdefs.h>
44 1.63 gehenna __KERNEL_RCSID(0, "$NetBSD: tty_pty.c,v 1.63 2002/09/06 13:18:43 gehenna Exp $");
45 1.41 thorpej
46 1.41 thorpej #include "opt_compat_sunos.h"
47 1.41 thorpej
48 1.16 mycroft #include <sys/param.h>
49 1.16 mycroft #include <sys/systm.h>
50 1.16 mycroft #include <sys/ioctl.h>
51 1.22 cgd #include <sys/proc.h>
52 1.16 mycroft #include <sys/tty.h>
53 1.16 mycroft #include <sys/file.h>
54 1.16 mycroft #include <sys/uio.h>
55 1.16 mycroft #include <sys/kernel.h>
56 1.16 mycroft #include <sys/vnode.h>
57 1.31 christos #include <sys/signalvar.h>
58 1.31 christos #include <sys/uio.h>
59 1.33 christos #include <sys/conf.h>
60 1.38 mycroft #include <sys/poll.h>
61 1.47 jdolecek #include <sys/malloc.h>
62 1.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.37 mycroft /* Macros to clear/set/test flags. */
67 1.37 mycroft #define SET(t, f) (t) |= (f)
68 1.37 mycroft #define CLR(t, f) (t) &= ~((unsigned)(f))
69 1.37 mycroft #define ISSET(t, f) ((t) & (f))
70 1.37 mycroft
71 1.1 cgd #define BUFSIZ 100 /* Chunk size iomoved to/from user */
72 1.1 cgd
73 1.1 cgd /*
74 1.1 cgd * pts == /dev/tty[pqrs]?
75 1.1 cgd * ptc == /dev/pty[pqrs]?
76 1.1 cgd */
77 1.27 mycroft struct pt_softc {
78 1.27 mycroft struct tty *pt_tty;
79 1.1 cgd int pt_flags;
80 1.22 cgd struct selinfo pt_selr, pt_selw;
81 1.1 cgd u_char pt_send;
82 1.1 cgd u_char pt_ucntl;
83 1.47 jdolecek };
84 1.47 jdolecek
85 1.48 jdolecek static struct pt_softc **pt_softc = NULL; /* pty array */
86 1.48 jdolecek static int npty = 0; /* for pstat -t */
87 1.48 jdolecek static int maxptys = DEFAULT_MAXPTYS; /* maximum number of ptys (sysctable) */
88 1.48 jdolecek static struct simplelock pt_softc_mutex = SIMPLELOCK_INITIALIZER;
89 1.1 cgd
90 1.1 cgd #define PF_PKT 0x08 /* packet mode */
91 1.1 cgd #define PF_STOPPED 0x10 /* user told stopped */
92 1.1 cgd #define PF_REMOTE 0x20 /* remote and flow controlled input */
93 1.1 cgd #define PF_NOSTOP 0x40
94 1.1 cgd #define PF_UCNTL 0x80 /* user control mode */
95 1.1 cgd
96 1.31 christos void ptyattach __P((int));
97 1.31 christos void ptcwakeup __P((struct tty *, int));
98 1.31 christos void ptsstart __P((struct tty *));
99 1.48 jdolecek int pty_maxptys __P((int, int));
100 1.15 deraadt
101 1.47 jdolecek static struct pt_softc **ptyarralloc __P((int));
102 1.47 jdolecek static int check_pty __P((dev_t));
103 1.47 jdolecek
104 1.63 gehenna dev_type_open(ptcopen);
105 1.63 gehenna dev_type_close(ptcclose);
106 1.63 gehenna dev_type_read(ptcread);
107 1.63 gehenna dev_type_write(ptcwrite);
108 1.63 gehenna dev_type_poll(ptcpoll);
109 1.63 gehenna
110 1.63 gehenna dev_type_open(ptsopen);
111 1.63 gehenna dev_type_close(ptsclose);
112 1.63 gehenna dev_type_read(ptsread);
113 1.63 gehenna dev_type_write(ptswrite);
114 1.63 gehenna dev_type_stop(ptsstop);
115 1.63 gehenna dev_type_poll(ptspoll);
116 1.63 gehenna
117 1.63 gehenna dev_type_ioctl(ptyioctl);
118 1.63 gehenna dev_type_tty(ptytty);
119 1.63 gehenna
120 1.63 gehenna const struct cdevsw ptc_cdevsw = {
121 1.63 gehenna ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
122 1.63 gehenna nullstop, ptytty, ptcpoll, nommap, D_TTY
123 1.63 gehenna };
124 1.63 gehenna
125 1.63 gehenna const struct cdevsw pts_cdevsw = {
126 1.63 gehenna ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
127 1.63 gehenna ptsstop, ptytty, ptspoll, nommap, D_TTY
128 1.63 gehenna };
129 1.63 gehenna
130 1.63 gehenna #if defined(pmax)
131 1.63 gehenna const struct cdevsw ptc_ultrix_cdevsw = {
132 1.63 gehenna ptcopen, ptcclose, ptcread, ptcwrite, ptyioctl,
133 1.63 gehenna nullstop, ptytty, ptcpoll, nommap, D_TTY
134 1.63 gehenna };
135 1.63 gehenna
136 1.63 gehenna const struct cdevsw pts_ultrix_cdevsw = {
137 1.63 gehenna ptsopen, ptsclose, ptsread, ptswrite, ptyioctl,
138 1.63 gehenna ptsstop, ptytty, ptspoll, nommap, D_TTY
139 1.63 gehenna };
140 1.63 gehenna #endif /* defined(pmax) */
141 1.63 gehenna
142 1.47 jdolecek /*
143 1.48 jdolecek * Allocate and zero array of nelem elements.
144 1.47 jdolecek */
145 1.47 jdolecek static struct pt_softc **
146 1.47 jdolecek ptyarralloc(nelem)
147 1.47 jdolecek int nelem;
148 1.47 jdolecek {
149 1.47 jdolecek struct pt_softc **pt;
150 1.47 jdolecek nelem += 10;
151 1.47 jdolecek pt = malloc(nelem * sizeof(struct pt_softc *), M_DEVBUF, M_WAITOK);
152 1.47 jdolecek memset(pt, '\0', nelem * sizeof(struct pt_softc *));
153 1.47 jdolecek return pt;
154 1.47 jdolecek }
155 1.47 jdolecek
156 1.47 jdolecek /*
157 1.47 jdolecek * Check if the minor is correct and ensure necessary structures
158 1.47 jdolecek * are properly allocated.
159 1.47 jdolecek */
160 1.47 jdolecek static int
161 1.47 jdolecek check_pty(dev)
162 1.47 jdolecek dev_t dev;
163 1.47 jdolecek {
164 1.47 jdolecek struct pt_softc *pti;
165 1.47 jdolecek
166 1.47 jdolecek if (minor(dev) >= npty) {
167 1.47 jdolecek struct pt_softc **newpt;
168 1.48 jdolecek int newnpty;
169 1.47 jdolecek
170 1.48 jdolecek /* check if the requested pty can be granted */
171 1.48 jdolecek if (minor(dev) >= maxptys) {
172 1.48 jdolecek limit_reached:
173 1.48 jdolecek tablefull("pty", "increase kern.maxptys");
174 1.48 jdolecek return (ENXIO);
175 1.48 jdolecek }
176 1.47 jdolecek
177 1.47 jdolecek /*
178 1.48 jdolecek * Now grab the pty array mutex - we need to ensure
179 1.47 jdolecek * that the pty array is consistent while copying it's
180 1.48 jdolecek * content to newly allocated, larger space; we also
181 1.48 jdolecek * need to be safe against pty_maxptys().
182 1.47 jdolecek */
183 1.48 jdolecek simple_lock(&pt_softc_mutex);
184 1.48 jdolecek
185 1.48 jdolecek do {
186 1.48 jdolecek for(newnpty = npty; newnpty <= minor(dev);
187 1.48 jdolecek newnpty *= 2);
188 1.48 jdolecek
189 1.48 jdolecek if (newnpty > maxptys)
190 1.48 jdolecek newnpty = maxptys;
191 1.48 jdolecek
192 1.48 jdolecek simple_unlock(&pt_softc_mutex);
193 1.48 jdolecek newpt = ptyarralloc(newnpty);
194 1.48 jdolecek simple_lock(&pt_softc_mutex);
195 1.48 jdolecek
196 1.48 jdolecek if (maxptys == npty) {
197 1.54 enami simple_unlock(&pt_softc_mutex);
198 1.48 jdolecek goto limit_reached;
199 1.48 jdolecek }
200 1.48 jdolecek } while(newnpty > maxptys);
201 1.47 jdolecek
202 1.47 jdolecek /*
203 1.48 jdolecek * If the pty array was not enlarged while we were waiting
204 1.48 jdolecek * for mutex, copy current contents of pt_softc[] to newly
205 1.48 jdolecek * allocated array and start using the new bigger array.
206 1.47 jdolecek */
207 1.47 jdolecek if (minor(dev) >= npty) {
208 1.47 jdolecek memcpy(newpt, pt_softc, npty*sizeof(struct pt_softc *));
209 1.47 jdolecek free(pt_softc, M_DEVBUF);
210 1.47 jdolecek
211 1.47 jdolecek pt_softc = newpt;
212 1.47 jdolecek npty = newnpty;
213 1.47 jdolecek } else {
214 1.48 jdolecek /* was enlarged when waited fot lock, free new space */
215 1.47 jdolecek free(newpt, M_DEVBUF);
216 1.47 jdolecek }
217 1.48 jdolecek
218 1.48 jdolecek simple_unlock(&pt_softc_mutex);
219 1.47 jdolecek }
220 1.47 jdolecek
221 1.47 jdolecek /*
222 1.48 jdolecek * If the entry is not yet allocated, allocate one. The mutex is
223 1.48 jdolecek * needed so that the state of pt_softc[] array is consistant
224 1.48 jdolecek * in case it has been longened above.
225 1.47 jdolecek */
226 1.47 jdolecek if (!pt_softc[minor(dev)]) {
227 1.48 jdolecek MALLOC(pti, struct pt_softc *, sizeof(struct pt_softc),
228 1.48 jdolecek M_DEVBUF, M_WAITOK);
229 1.48 jdolecek
230 1.48 jdolecek pti->pt_tty = ttymalloc();
231 1.48 jdolecek
232 1.48 jdolecek simple_lock(&pt_softc_mutex);
233 1.47 jdolecek
234 1.47 jdolecek /*
235 1.48 jdolecek * Check the entry again - it might have been
236 1.48 jdolecek * added while we were waiting for mutex.
237 1.47 jdolecek */
238 1.47 jdolecek if (!pt_softc[minor(dev)]) {
239 1.47 jdolecek tty_attach(pti->pt_tty);
240 1.47 jdolecek pt_softc[minor(dev)] = pti;
241 1.48 jdolecek } else {
242 1.48 jdolecek ttyfree(pti->pt_tty);
243 1.48 jdolecek FREE(pti, M_DEVBUF);
244 1.47 jdolecek }
245 1.48 jdolecek
246 1.48 jdolecek simple_unlock(&pt_softc_mutex);
247 1.48 jdolecek }
248 1.48 jdolecek
249 1.48 jdolecek return (0);
250 1.48 jdolecek }
251 1.48 jdolecek
252 1.48 jdolecek /*
253 1.48 jdolecek * Set maxpty in thread-safe way. Returns 0 in case of error, otherwise
254 1.48 jdolecek * new value of maxptys.
255 1.48 jdolecek */
256 1.48 jdolecek int
257 1.48 jdolecek pty_maxptys(newmax, set)
258 1.48 jdolecek int newmax, set;
259 1.48 jdolecek {
260 1.48 jdolecek if (!set)
261 1.48 jdolecek return (maxptys);
262 1.48 jdolecek
263 1.48 jdolecek /* the value cannot be set to value lower than current number of ptys */
264 1.48 jdolecek if (newmax < npty)
265 1.48 jdolecek return (0);
266 1.48 jdolecek
267 1.48 jdolecek /* can proceed immediatelly if bigger than current maximum */
268 1.48 jdolecek if (newmax > maxptys) {
269 1.48 jdolecek maxptys = newmax;
270 1.48 jdolecek return (maxptys);
271 1.47 jdolecek }
272 1.47 jdolecek
273 1.48 jdolecek /*
274 1.48 jdolecek * We have to grab the pt_softc lock, so that we would pick correct
275 1.48 jdolecek * value of npty (might be modified in check_pty()).
276 1.48 jdolecek */
277 1.48 jdolecek simple_lock(&pt_softc_mutex);
278 1.48 jdolecek
279 1.48 jdolecek if (newmax > npty)
280 1.48 jdolecek maxptys = newmax;
281 1.47 jdolecek
282 1.48 jdolecek simple_unlock(&pt_softc_mutex);
283 1.48 jdolecek
284 1.48 jdolecek return (maxptys);
285 1.47 jdolecek }
286 1.47 jdolecek
287 1.22 cgd /*
288 1.22 cgd * Establish n (or default if n is 1) ptys in the system.
289 1.22 cgd */
290 1.15 deraadt void
291 1.15 deraadt ptyattach(n)
292 1.15 deraadt int n;
293 1.15 deraadt {
294 1.22 cgd /* maybe should allow 0 => none? */
295 1.22 cgd if (n <= 1)
296 1.47 jdolecek n = DEFAULT_NPTYS;
297 1.47 jdolecek pt_softc = ptyarralloc(n);
298 1.22 cgd npty = n;
299 1.15 deraadt }
300 1.7 andrew
301 1.1 cgd /*ARGSUSED*/
302 1.31 christos int
303 1.1 cgd ptsopen(dev, flag, devtype, p)
304 1.1 cgd dev_t dev;
305 1.7 andrew int flag, devtype;
306 1.1 cgd struct proc *p;
307 1.1 cgd {
308 1.27 mycroft struct pt_softc *pti;
309 1.43 augustss struct tty *tp;
310 1.1 cgd int error;
311 1.1 cgd
312 1.47 jdolecek if ((error = check_pty(dev)))
313 1.47 jdolecek return (error);
314 1.47 jdolecek
315 1.47 jdolecek pti = pt_softc[minor(dev)];
316 1.47 jdolecek tp = pti->pt_tty;
317 1.47 jdolecek
318 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN)) {
319 1.1 cgd ttychars(tp); /* Set up default chars */
320 1.1 cgd tp->t_iflag = TTYDEF_IFLAG;
321 1.1 cgd tp->t_oflag = TTYDEF_OFLAG;
322 1.1 cgd tp->t_lflag = TTYDEF_LFLAG;
323 1.1 cgd tp->t_cflag = TTYDEF_CFLAG;
324 1.1 cgd tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
325 1.1 cgd ttsetwater(tp); /* would be done in xxparam() */
326 1.37 mycroft } else if (ISSET(tp->t_state, TS_XCLUDE) && p->p_ucred->cr_uid != 0)
327 1.1 cgd return (EBUSY);
328 1.1 cgd if (tp->t_oproc) /* Ctrlr still around. */
329 1.37 mycroft SET(tp->t_state, TS_CARR_ON);
330 1.40 mycroft if (!ISSET(flag, O_NONBLOCK))
331 1.40 mycroft while (!ISSET(tp->t_state, TS_CARR_ON)) {
332 1.40 mycroft tp->t_wopen++;
333 1.40 mycroft error = ttysleep(tp, &tp->t_rawq, TTIPRI | PCATCH,
334 1.40 mycroft ttopen, 0);
335 1.40 mycroft tp->t_wopen--;
336 1.40 mycroft if (error)
337 1.40 mycroft return (error);
338 1.40 mycroft }
339 1.50 eeh error = (*tp->t_linesw->l_open)(dev, tp);
340 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
341 1.22 cgd return (error);
342 1.1 cgd }
343 1.1 cgd
344 1.31 christos int
345 1.1 cgd ptsclose(dev, flag, mode, p)
346 1.1 cgd dev_t dev;
347 1.1 cgd int flag, mode;
348 1.1 cgd struct proc *p;
349 1.1 cgd {
350 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
351 1.43 augustss struct tty *tp = pti->pt_tty;
352 1.31 christos int error;
353 1.1 cgd
354 1.50 eeh error = (*tp->t_linesw->l_close)(tp, flag);
355 1.31 christos error |= ttyclose(tp);
356 1.1 cgd ptcwakeup(tp, FREAD|FWRITE);
357 1.31 christos return (error);
358 1.1 cgd }
359 1.1 cgd
360 1.31 christos int
361 1.1 cgd ptsread(dev, uio, flag)
362 1.1 cgd dev_t dev;
363 1.1 cgd struct uio *uio;
364 1.7 andrew int flag;
365 1.1 cgd {
366 1.1 cgd struct proc *p = curproc;
367 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
368 1.43 augustss struct tty *tp = pti->pt_tty;
369 1.1 cgd int error = 0;
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.35 pk error = ttysleep(tp, (caddr_t)&lbolt,
380 1.31 christos TTIPRI | PCATCH, ttybg, 0);
381 1.31 christos if (error)
382 1.1 cgd return (error);
383 1.1 cgd }
384 1.8 mycroft if (tp->t_canq.c_cc == 0) {
385 1.1 cgd if (flag & IO_NDELAY)
386 1.1 cgd return (EWOULDBLOCK);
387 1.31 christos error = ttysleep(tp, (caddr_t)&tp->t_canq,
388 1.31 christos TTIPRI | PCATCH, ttyin, 0);
389 1.31 christos if (error)
390 1.1 cgd return (error);
391 1.1 cgd goto again;
392 1.1 cgd }
393 1.8 mycroft while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
394 1.8 mycroft if (ureadc(getc(&tp->t_canq), uio) < 0) {
395 1.1 cgd error = EFAULT;
396 1.1 cgd break;
397 1.1 cgd }
398 1.8 mycroft if (tp->t_canq.c_cc == 1)
399 1.8 mycroft (void) getc(&tp->t_canq);
400 1.8 mycroft if (tp->t_canq.c_cc)
401 1.1 cgd return (error);
402 1.1 cgd } else
403 1.1 cgd if (tp->t_oproc)
404 1.50 eeh error = (*tp->t_linesw->l_read)(tp, uio, flag);
405 1.1 cgd ptcwakeup(tp, FWRITE);
406 1.1 cgd return (error);
407 1.1 cgd }
408 1.1 cgd
409 1.1 cgd /*
410 1.1 cgd * Write to pseudo-tty.
411 1.1 cgd * Wakeups of controlling tty will happen
412 1.1 cgd * indirectly, when tty driver calls ptsstart.
413 1.1 cgd */
414 1.31 christos int
415 1.1 cgd ptswrite(dev, uio, flag)
416 1.1 cgd dev_t dev;
417 1.1 cgd struct uio *uio;
418 1.7 andrew int flag;
419 1.1 cgd {
420 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
421 1.43 augustss struct tty *tp = pti->pt_tty;
422 1.1 cgd
423 1.1 cgd if (tp->t_oproc == 0)
424 1.1 cgd return (EIO);
425 1.50 eeh return ((*tp->t_linesw->l_write)(tp, uio, flag));
426 1.56 scw }
427 1.56 scw
428 1.56 scw /*
429 1.56 scw * Poll pseudo-tty.
430 1.56 scw */
431 1.56 scw int
432 1.56 scw ptspoll(dev, events, p)
433 1.56 scw dev_t dev;
434 1.56 scw int events;
435 1.56 scw struct proc *p;
436 1.56 scw {
437 1.56 scw struct pt_softc *pti = pt_softc[minor(dev)];
438 1.56 scw struct tty *tp = pti->pt_tty;
439 1.56 scw
440 1.56 scw if (tp->t_oproc == 0)
441 1.56 scw return (EIO);
442 1.56 scw
443 1.56 scw return ((*tp->t_linesw->l_poll)(tp, events, p));
444 1.1 cgd }
445 1.1 cgd
446 1.1 cgd /*
447 1.1 cgd * Start output on pseudo-tty.
448 1.38 mycroft * Wake up process polling or sleeping for input from controlling tty.
449 1.1 cgd */
450 1.12 deraadt void
451 1.1 cgd ptsstart(tp)
452 1.1 cgd struct tty *tp;
453 1.1 cgd {
454 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
455 1.1 cgd
456 1.37 mycroft if (ISSET(tp->t_state, TS_TTSTOP))
457 1.12 deraadt return;
458 1.1 cgd if (pti->pt_flags & PF_STOPPED) {
459 1.1 cgd pti->pt_flags &= ~PF_STOPPED;
460 1.1 cgd pti->pt_send = TIOCPKT_START;
461 1.1 cgd }
462 1.1 cgd ptcwakeup(tp, FREAD);
463 1.1 cgd }
464 1.1 cgd
465 1.36 mycroft void
466 1.31 christos ptsstop(tp, flush)
467 1.43 augustss struct tty *tp;
468 1.31 christos int flush;
469 1.31 christos {
470 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
471 1.31 christos int flag;
472 1.31 christos
473 1.31 christos /* note: FLUSHREAD and FLUSHWRITE already ok */
474 1.31 christos if (flush == 0) {
475 1.31 christos flush = TIOCPKT_STOP;
476 1.31 christos pti->pt_flags |= PF_STOPPED;
477 1.31 christos } else
478 1.31 christos pti->pt_flags &= ~PF_STOPPED;
479 1.31 christos pti->pt_send |= flush;
480 1.31 christos /* change of perspective */
481 1.31 christos flag = 0;
482 1.31 christos if (flush & FREAD)
483 1.31 christos flag |= FWRITE;
484 1.31 christos if (flush & FWRITE)
485 1.31 christos flag |= FREAD;
486 1.31 christos ptcwakeup(tp, flag);
487 1.31 christos }
488 1.31 christos
489 1.31 christos void
490 1.1 cgd ptcwakeup(tp, flag)
491 1.1 cgd struct tty *tp;
492 1.7 andrew int flag;
493 1.1 cgd {
494 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(tp->t_dev)];
495 1.1 cgd
496 1.1 cgd if (flag & FREAD) {
497 1.4 cgd selwakeup(&pti->pt_selr);
498 1.22 cgd wakeup((caddr_t)&tp->t_outq.c_cf);
499 1.1 cgd }
500 1.1 cgd if (flag & FWRITE) {
501 1.4 cgd selwakeup(&pti->pt_selw);
502 1.8 mycroft wakeup((caddr_t)&tp->t_rawq.c_cf);
503 1.1 cgd }
504 1.1 cgd }
505 1.1 cgd
506 1.1 cgd /*ARGSUSED*/
507 1.25 mycroft int
508 1.26 mycroft ptcopen(dev, flag, devtype, p)
509 1.1 cgd dev_t dev;
510 1.1 cgd int flag, devtype;
511 1.1 cgd struct proc *p;
512 1.1 cgd {
513 1.27 mycroft struct pt_softc *pti;
514 1.43 augustss struct tty *tp;
515 1.47 jdolecek int error;
516 1.47 jdolecek
517 1.47 jdolecek if ((error = check_pty(dev)))
518 1.47 jdolecek return (error);
519 1.47 jdolecek
520 1.47 jdolecek pti = pt_softc[minor(dev)];
521 1.47 jdolecek tp = pti->pt_tty;
522 1.1 cgd
523 1.1 cgd if (tp->t_oproc)
524 1.1 cgd return (EIO);
525 1.1 cgd tp->t_oproc = ptsstart;
526 1.50 eeh (void)(*tp->t_linesw->l_modem)(tp, 1);
527 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
528 1.22 cgd pti->pt_flags = 0;
529 1.1 cgd pti->pt_send = 0;
530 1.1 cgd pti->pt_ucntl = 0;
531 1.1 cgd return (0);
532 1.1 cgd }
533 1.1 cgd
534 1.31 christos /*ARGSUSED*/
535 1.25 mycroft int
536 1.31 christos ptcclose(dev, flag, devtype, p)
537 1.1 cgd dev_t dev;
538 1.31 christos int flag, devtype;
539 1.31 christos struct proc *p;
540 1.1 cgd {
541 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
542 1.43 augustss struct tty *tp = pti->pt_tty;
543 1.1 cgd
544 1.50 eeh (void)(*tp->t_linesw->l_modem)(tp, 0);
545 1.37 mycroft CLR(tp->t_state, TS_CARR_ON);
546 1.1 cgd tp->t_oproc = 0; /* mark closed */
547 1.2 cgd return (0);
548 1.1 cgd }
549 1.1 cgd
550 1.31 christos int
551 1.1 cgd ptcread(dev, uio, flag)
552 1.1 cgd dev_t dev;
553 1.1 cgd struct uio *uio;
554 1.7 andrew int flag;
555 1.1 cgd {
556 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
557 1.43 augustss struct tty *tp = pti->pt_tty;
558 1.22 cgd char buf[BUFSIZ];
559 1.1 cgd int error = 0, cc;
560 1.1 cgd
561 1.1 cgd /*
562 1.1 cgd * We want to block until the slave
563 1.1 cgd * is open, and there's something to read;
564 1.1 cgd * but if we lost the slave or we're NBIO,
565 1.1 cgd * then return the appropriate error instead.
566 1.1 cgd */
567 1.1 cgd for (;;) {
568 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN)) {
569 1.1 cgd if (pti->pt_flags&PF_PKT && pti->pt_send) {
570 1.1 cgd error = ureadc((int)pti->pt_send, uio);
571 1.1 cgd if (error)
572 1.1 cgd return (error);
573 1.1 cgd if (pti->pt_send & TIOCPKT_IOCTL) {
574 1.22 cgd cc = min(uio->uio_resid,
575 1.1 cgd sizeof(tp->t_termios));
576 1.31 christos uiomove((caddr_t) &tp->t_termios,
577 1.31 christos cc, uio);
578 1.1 cgd }
579 1.1 cgd pti->pt_send = 0;
580 1.1 cgd return (0);
581 1.1 cgd }
582 1.1 cgd if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
583 1.1 cgd error = ureadc((int)pti->pt_ucntl, uio);
584 1.1 cgd if (error)
585 1.1 cgd return (error);
586 1.1 cgd pti->pt_ucntl = 0;
587 1.1 cgd return (0);
588 1.1 cgd }
589 1.37 mycroft if (tp->t_outq.c_cc && !ISSET(tp->t_state, TS_TTSTOP))
590 1.1 cgd break;
591 1.1 cgd }
592 1.37 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
593 1.1 cgd return (0); /* EOF */
594 1.1 cgd if (flag & IO_NDELAY)
595 1.1 cgd return (EWOULDBLOCK);
596 1.31 christos error = tsleep((caddr_t)&tp->t_outq.c_cf, TTIPRI | PCATCH,
597 1.31 christos ttyin, 0);
598 1.31 christos if (error)
599 1.1 cgd return (error);
600 1.1 cgd }
601 1.1 cgd if (pti->pt_flags & (PF_PKT|PF_UCNTL))
602 1.1 cgd error = ureadc(0, uio);
603 1.1 cgd while (uio->uio_resid > 0 && error == 0) {
604 1.22 cgd cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
605 1.1 cgd if (cc <= 0)
606 1.1 cgd break;
607 1.1 cgd error = uiomove(buf, cc, uio);
608 1.1 cgd }
609 1.8 mycroft if (tp->t_outq.c_cc <= tp->t_lowat) {
610 1.37 mycroft if (ISSET(tp->t_state, TS_ASLEEP)) {
611 1.37 mycroft CLR(tp->t_state, TS_ASLEEP);
612 1.8 mycroft wakeup((caddr_t)&tp->t_outq);
613 1.1 cgd }
614 1.4 cgd selwakeup(&tp->t_wsel);
615 1.1 cgd }
616 1.1 cgd return (error);
617 1.1 cgd }
618 1.1 cgd
619 1.1 cgd
620 1.31 christos int
621 1.1 cgd ptcwrite(dev, uio, flag)
622 1.1 cgd dev_t dev;
623 1.43 augustss struct uio *uio;
624 1.7 andrew int flag;
625 1.1 cgd {
626 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
627 1.43 augustss struct tty *tp = pti->pt_tty;
628 1.43 augustss u_char *cp = NULL;
629 1.43 augustss int cc = 0;
630 1.1 cgd u_char locbuf[BUFSIZ];
631 1.1 cgd int cnt = 0;
632 1.1 cgd int error = 0;
633 1.1 cgd
634 1.1 cgd again:
635 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
636 1.1 cgd goto block;
637 1.1 cgd if (pti->pt_flags & PF_REMOTE) {
638 1.8 mycroft if (tp->t_canq.c_cc)
639 1.1 cgd goto block;
640 1.8 mycroft while (uio->uio_resid > 0 && tp->t_canq.c_cc < TTYHOG - 1) {
641 1.1 cgd if (cc == 0) {
642 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
643 1.8 mycroft cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
644 1.1 cgd cp = locbuf;
645 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
646 1.1 cgd if (error)
647 1.1 cgd return (error);
648 1.1 cgd /* check again for safety */
649 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
650 1.1 cgd return (EIO);
651 1.1 cgd }
652 1.1 cgd if (cc)
653 1.22 cgd (void) b_to_q((char *)cp, cc, &tp->t_canq);
654 1.1 cgd cc = 0;
655 1.1 cgd }
656 1.8 mycroft (void) putc(0, &tp->t_canq);
657 1.1 cgd ttwakeup(tp);
658 1.8 mycroft wakeup((caddr_t)&tp->t_canq);
659 1.1 cgd return (0);
660 1.1 cgd }
661 1.1 cgd while (uio->uio_resid > 0) {
662 1.1 cgd if (cc == 0) {
663 1.1 cgd cc = min(uio->uio_resid, BUFSIZ);
664 1.1 cgd cp = locbuf;
665 1.1 cgd error = uiomove((caddr_t)cp, cc, uio);
666 1.1 cgd if (error)
667 1.1 cgd return (error);
668 1.1 cgd /* check again for safety */
669 1.37 mycroft if (!ISSET(tp->t_state, TS_ISOPEN))
670 1.1 cgd return (EIO);
671 1.1 cgd }
672 1.1 cgd while (cc > 0) {
673 1.8 mycroft if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
674 1.59 christos (tp->t_canq.c_cc > 0 || !ISSET(tp->t_lflag, ICANON))) {
675 1.8 mycroft wakeup((caddr_t)&tp->t_rawq);
676 1.1 cgd goto block;
677 1.1 cgd }
678 1.50 eeh (*tp->t_linesw->l_rint)(*cp++, tp);
679 1.1 cgd cnt++;
680 1.1 cgd cc--;
681 1.1 cgd }
682 1.1 cgd cc = 0;
683 1.1 cgd }
684 1.1 cgd return (0);
685 1.1 cgd block:
686 1.1 cgd /*
687 1.1 cgd * Come here to wait for slave to open, for space
688 1.1 cgd * in outq, or space in rawq.
689 1.1 cgd */
690 1.37 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
691 1.1 cgd return (EIO);
692 1.1 cgd if (flag & IO_NDELAY) {
693 1.1 cgd /* adjust for data copied in but not written */
694 1.1 cgd uio->uio_resid += cc;
695 1.1 cgd if (cnt == 0)
696 1.1 cgd return (EWOULDBLOCK);
697 1.1 cgd return (0);
698 1.1 cgd }
699 1.31 christos error = tsleep((caddr_t)&tp->t_rawq.c_cf, TTOPRI | PCATCH,
700 1.31 christos ttyout, 0);
701 1.31 christos if (error) {
702 1.1 cgd /* adjust for data copied in but not written */
703 1.1 cgd uio->uio_resid += cc;
704 1.1 cgd return (error);
705 1.1 cgd }
706 1.1 cgd goto again;
707 1.29 mycroft }
708 1.29 mycroft
709 1.31 christos int
710 1.38 mycroft ptcpoll(dev, events, p)
711 1.31 christos dev_t dev;
712 1.38 mycroft int events;
713 1.31 christos struct proc *p;
714 1.31 christos {
715 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
716 1.43 augustss struct tty *tp = pti->pt_tty;
717 1.38 mycroft int revents = 0;
718 1.38 mycroft int s = splsoftclock();
719 1.31 christos
720 1.38 mycroft if (events & (POLLIN | POLLRDNORM))
721 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
722 1.38 mycroft ((tp->t_outq.c_cc > 0 && !ISSET(tp->t_state, TS_TTSTOP)) ||
723 1.38 mycroft ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
724 1.38 mycroft ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
725 1.38 mycroft revents |= events & (POLLIN | POLLRDNORM);
726 1.31 christos
727 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
728 1.37 mycroft if (ISSET(tp->t_state, TS_ISOPEN) &&
729 1.38 mycroft ((pti->pt_flags & PF_REMOTE) ?
730 1.38 mycroft (tp->t_canq.c_cc == 0) :
731 1.38 mycroft ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2) ||
732 1.59 christos (tp->t_canq.c_cc == 0 && ISSET(tp->t_lflag, ICANON)))))
733 1.38 mycroft revents |= events & (POLLOUT | POLLWRNORM);
734 1.31 christos
735 1.38 mycroft if (events & POLLHUP)
736 1.38 mycroft if (!ISSET(tp->t_state, TS_CARR_ON))
737 1.38 mycroft revents |= POLLHUP;
738 1.31 christos
739 1.38 mycroft if (revents == 0) {
740 1.38 mycroft if (events & (POLLIN | POLLHUP | POLLRDNORM))
741 1.38 mycroft selrecord(p, &pti->pt_selr);
742 1.31 christos
743 1.38 mycroft if (events & (POLLOUT | POLLWRNORM))
744 1.38 mycroft selrecord(p, &pti->pt_selw);
745 1.31 christos }
746 1.38 mycroft
747 1.38 mycroft splx(s);
748 1.38 mycroft return (revents);
749 1.31 christos }
750 1.31 christos
751 1.31 christos
752 1.29 mycroft struct tty *
753 1.29 mycroft ptytty(dev)
754 1.29 mycroft dev_t dev;
755 1.29 mycroft {
756 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
757 1.43 augustss struct tty *tp = pti->pt_tty;
758 1.29 mycroft
759 1.29 mycroft return (tp);
760 1.1 cgd }
761 1.1 cgd
762 1.1 cgd /*ARGSUSED*/
763 1.31 christos int
764 1.18 mycroft ptyioctl(dev, cmd, data, flag, p)
765 1.18 mycroft dev_t dev;
766 1.24 cgd u_long cmd;
767 1.1 cgd caddr_t data;
768 1.18 mycroft int flag;
769 1.18 mycroft struct proc *p;
770 1.1 cgd {
771 1.47 jdolecek struct pt_softc *pti = pt_softc[minor(dev)];
772 1.43 augustss struct tty *tp = pti->pt_tty;
773 1.63 gehenna const struct cdevsw *cdev;
774 1.43 augustss u_char *cc = tp->t_cc;
775 1.44 fvdl int stop, error, sig;
776 1.1 cgd
777 1.63 gehenna cdev = cdevsw_lookup(dev);
778 1.1 cgd /*
779 1.1 cgd * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
780 1.1 cgd * ttywflush(tp) will hang if there are characters in the outq.
781 1.1 cgd */
782 1.1 cgd if (cmd == TIOCEXT) {
783 1.1 cgd /*
784 1.1 cgd * When the EXTPROC bit is being toggled, we need
785 1.1 cgd * to send an TIOCPKT_IOCTL if the packet driver
786 1.1 cgd * is turned on.
787 1.1 cgd */
788 1.1 cgd if (*(int *)data) {
789 1.1 cgd if (pti->pt_flags & PF_PKT) {
790 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
791 1.1 cgd ptcwakeup(tp, FREAD);
792 1.1 cgd }
793 1.37 mycroft SET(tp->t_lflag, EXTPROC);
794 1.1 cgd } else {
795 1.39 fvdl if (ISSET(tp->t_lflag, EXTPROC) &&
796 1.1 cgd (pti->pt_flags & PF_PKT)) {
797 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
798 1.1 cgd ptcwakeup(tp, FREAD);
799 1.1 cgd }
800 1.37 mycroft CLR(tp->t_lflag, EXTPROC);
801 1.1 cgd }
802 1.1 cgd return(0);
803 1.1 cgd } else
804 1.63 gehenna if (cdev != NULL && cdev->d_open == ptcopen)
805 1.1 cgd switch (cmd) {
806 1.1 cgd
807 1.1 cgd case TIOCGPGRP:
808 1.35 pk /*
809 1.35 pk * We avoid calling ttioctl on the controller since,
810 1.1 cgd * in that case, tp must be the controlling terminal.
811 1.1 cgd */
812 1.1 cgd *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
813 1.1 cgd return (0);
814 1.1 cgd
815 1.1 cgd case TIOCPKT:
816 1.1 cgd if (*(int *)data) {
817 1.1 cgd if (pti->pt_flags & PF_UCNTL)
818 1.1 cgd return (EINVAL);
819 1.1 cgd pti->pt_flags |= PF_PKT;
820 1.1 cgd } else
821 1.1 cgd pti->pt_flags &= ~PF_PKT;
822 1.1 cgd return (0);
823 1.1 cgd
824 1.1 cgd case TIOCUCNTL:
825 1.1 cgd if (*(int *)data) {
826 1.1 cgd if (pti->pt_flags & PF_PKT)
827 1.1 cgd return (EINVAL);
828 1.1 cgd pti->pt_flags |= PF_UCNTL;
829 1.1 cgd } else
830 1.1 cgd pti->pt_flags &= ~PF_UCNTL;
831 1.1 cgd return (0);
832 1.1 cgd
833 1.1 cgd case TIOCREMOTE:
834 1.1 cgd if (*(int *)data)
835 1.1 cgd pti->pt_flags |= PF_REMOTE;
836 1.1 cgd else
837 1.1 cgd pti->pt_flags &= ~PF_REMOTE;
838 1.1 cgd ttyflush(tp, FREAD|FWRITE);
839 1.1 cgd return (0);
840 1.1 cgd
841 1.32 christos #ifdef COMPAT_OLDTTY
842 1.35 pk case TIOCSETP:
843 1.1 cgd case TIOCSETN:
844 1.2 cgd #endif
845 1.1 cgd case TIOCSETD:
846 1.1 cgd case TIOCSETA:
847 1.1 cgd case TIOCSETAW:
848 1.1 cgd case TIOCSETAF:
849 1.21 cgd ndflush(&tp->t_outq, tp->t_outq.c_cc);
850 1.1 cgd break;
851 1.1 cgd
852 1.1 cgd case TIOCSIG:
853 1.46 mrg sig = (int)(long)*(caddr_t *)data;
854 1.44 fvdl if (sig <= 0 || sig >= NSIG)
855 1.44 fvdl return (EINVAL);
856 1.37 mycroft if (!ISSET(tp->t_lflag, NOFLSH))
857 1.1 cgd ttyflush(tp, FREAD|FWRITE);
858 1.44 fvdl if ((sig == SIGINFO) &&
859 1.37 mycroft (!ISSET(tp->t_lflag, NOKERNINFO)))
860 1.1 cgd ttyinfo(tp);
861 1.62 itohy pgsignal(tp->t_pgrp, sig, 1);
862 1.1 cgd return(0);
863 1.1 cgd }
864 1.50 eeh error = (*tp->t_linesw->l_ioctl)(tp, cmd, data, flag, p);
865 1.61 atatat if (error == EPASSTHROUGH)
866 1.18 mycroft error = ttioctl(tp, cmd, data, flag, p);
867 1.61 atatat if (error == EPASSTHROUGH) {
868 1.1 cgd if (pti->pt_flags & PF_UCNTL &&
869 1.1 cgd (cmd & ~0xff) == UIOCCMD(0)) {
870 1.1 cgd if (cmd & 0xff) {
871 1.1 cgd pti->pt_ucntl = (u_char)cmd;
872 1.1 cgd ptcwakeup(tp, FREAD);
873 1.1 cgd }
874 1.1 cgd return (0);
875 1.1 cgd }
876 1.1 cgd }
877 1.1 cgd /*
878 1.1 cgd * If external processing and packet mode send ioctl packet.
879 1.1 cgd */
880 1.37 mycroft if (ISSET(tp->t_lflag, EXTPROC) && (pti->pt_flags & PF_PKT)) {
881 1.1 cgd switch(cmd) {
882 1.1 cgd case TIOCSETA:
883 1.1 cgd case TIOCSETAW:
884 1.1 cgd case TIOCSETAF:
885 1.32 christos #ifdef COMPAT_OLDTTY
886 1.1 cgd case TIOCSETP:
887 1.1 cgd case TIOCSETN:
888 1.1 cgd case TIOCSETC:
889 1.1 cgd case TIOCSLTC:
890 1.1 cgd case TIOCLBIS:
891 1.1 cgd case TIOCLBIC:
892 1.1 cgd case TIOCLSET:
893 1.1 cgd #endif
894 1.1 cgd pti->pt_send |= TIOCPKT_IOCTL;
895 1.22 cgd ptcwakeup(tp, FREAD);
896 1.1 cgd default:
897 1.1 cgd break;
898 1.1 cgd }
899 1.1 cgd }
900 1.37 mycroft stop = ISSET(tp->t_iflag, IXON) && CCEQ(cc[VSTOP], CTRL('s'))
901 1.1 cgd && CCEQ(cc[VSTART], CTRL('q'));
902 1.1 cgd if (pti->pt_flags & PF_NOSTOP) {
903 1.1 cgd if (stop) {
904 1.1 cgd pti->pt_send &= ~TIOCPKT_NOSTOP;
905 1.1 cgd pti->pt_send |= TIOCPKT_DOSTOP;
906 1.1 cgd pti->pt_flags &= ~PF_NOSTOP;
907 1.1 cgd ptcwakeup(tp, FREAD);
908 1.1 cgd }
909 1.1 cgd } else {
910 1.1 cgd if (!stop) {
911 1.1 cgd pti->pt_send &= ~TIOCPKT_DOSTOP;
912 1.1 cgd pti->pt_send |= TIOCPKT_NOSTOP;
913 1.1 cgd pti->pt_flags |= PF_NOSTOP;
914 1.1 cgd ptcwakeup(tp, FREAD);
915 1.1 cgd }
916 1.1 cgd }
917 1.1 cgd return (error);
918 1.1 cgd }
919