Deleted Added
full compact
pty.c (111742) pty.c (111815)
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 *
33 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
34 * $FreeBSD: head/sys/kern/tty_pty.c 111742 2003-03-02 15:56:49Z des $
34 * $FreeBSD: head/sys/kern/tty_pty.c 111815 2003-03-03 12:15:54Z phk $
35 */
36
37/*
38 * Pseudo-teletype Driver
39 * (Actually two drivers, requiring two entries in 'cdevsw')
40 */
41#include "opt_compat.h"
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/sx.h>
47#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48#include <sys/ioctl_compat.h>
49#endif
50#include <sys/proc.h>
51#include <sys/tty.h>
52#include <sys/conf.h>
53#include <sys/fcntl.h>
54#include <sys/poll.h>
55#include <sys/kernel.h>
56#include <sys/vnode.h>
57#include <sys/signalvar.h>
58#include <sys/malloc.h>
59
60static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
61
62static void ptsstart(struct tty *tp);
63static void ptsstop(struct tty *tp, int rw);
64static void ptcwakeup(struct tty *tp, int flag);
65static dev_t ptyinit(dev_t cdev);
66
67static d_open_t ptsopen;
68static d_close_t ptsclose;
69static d_read_t ptsread;
70static d_write_t ptswrite;
71static d_ioctl_t ptyioctl;
72static d_open_t ptcopen;
73static d_close_t ptcclose;
74static d_read_t ptcread;
75static d_write_t ptcwrite;
76static d_poll_t ptcpoll;
77
78#define CDEV_MAJOR_S 5
79static struct cdevsw pts_cdevsw = {
35 */
36
37/*
38 * Pseudo-teletype Driver
39 * (Actually two drivers, requiring two entries in 'cdevsw')
40 */
41#include "opt_compat.h"
42#include <sys/param.h>
43#include <sys/systm.h>
44#include <sys/lock.h>
45#include <sys/mutex.h>
46#include <sys/sx.h>
47#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
48#include <sys/ioctl_compat.h>
49#endif
50#include <sys/proc.h>
51#include <sys/tty.h>
52#include <sys/conf.h>
53#include <sys/fcntl.h>
54#include <sys/poll.h>
55#include <sys/kernel.h>
56#include <sys/vnode.h>
57#include <sys/signalvar.h>
58#include <sys/malloc.h>
59
60static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
61
62static void ptsstart(struct tty *tp);
63static void ptsstop(struct tty *tp, int rw);
64static void ptcwakeup(struct tty *tp, int flag);
65static dev_t ptyinit(dev_t cdev);
66
67static d_open_t ptsopen;
68static d_close_t ptsclose;
69static d_read_t ptsread;
70static d_write_t ptswrite;
71static d_ioctl_t ptyioctl;
72static d_open_t ptcopen;
73static d_close_t ptcclose;
74static d_read_t ptcread;
75static d_write_t ptcwrite;
76static d_poll_t ptcpoll;
77
78#define CDEV_MAJOR_S 5
79static struct cdevsw pts_cdevsw = {
80 /* open */ ptsopen,
81 /* close */ ptsclose,
82 /* read */ ptsread,
83 /* write */ ptswrite,
84 /* ioctl */ ptyioctl,
85 /* poll */ ttypoll,
86 /* mmap */ nommap,
87 /* strategy */ nostrategy,
88 /* name */ "pts",
89 /* maj */ CDEV_MAJOR_S,
90 /* dump */ nodump,
91 /* psize */ nopsize,
92 /* flags */ D_TTY | D_KQFILTER,
93 /* kqfilter */ ttykqfilter,
80 .d_open = ptsopen,
81 .d_close = ptsclose,
82 .d_read = ptsread,
83 .d_write = ptswrite,
84 .d_ioctl = ptyioctl,
85 .d_poll = ttypoll,
86 .d_name = "pts",
87 .d_maj = CDEV_MAJOR_S,
88 .d_flags = D_TTY | D_KQFILTER,
89 .d_kqfilter = ttykqfilter,
94};
95
96#define CDEV_MAJOR_C 6
97static struct cdevsw ptc_cdevsw = {
90};
91
92#define CDEV_MAJOR_C 6
93static struct cdevsw ptc_cdevsw = {
98 /* open */ ptcopen,
99 /* close */ ptcclose,
100 /* read */ ptcread,
101 /* write */ ptcwrite,
102 /* ioctl */ ptyioctl,
103 /* poll */ ptcpoll,
104 /* mmap */ nommap,
105 /* strategy */ nostrategy,
106 /* name */ "ptc",
107 /* maj */ CDEV_MAJOR_C,
108 /* dump */ nodump,
109 /* psize */ nopsize,
110 /* flags */ D_TTY | D_KQFILTER,
111 /* kqfilter */ ttykqfilter,
94 .d_open = ptcopen,
95 .d_close = ptcclose,
96 .d_read = ptcread,
97 .d_write = ptcwrite,
98 .d_ioctl = ptyioctl,
99 .d_poll = ptcpoll,
100 .d_name = "ptc",
101 .d_maj = CDEV_MAJOR_C,
102 .d_flags = D_TTY | D_KQFILTER,
103 .d_kqfilter = ttykqfilter,
112};
113
114#define BUFSIZ 100 /* Chunk size iomoved to/from user */
115
116struct pt_ioctl {
117 int pt_flags;
118 struct selinfo pt_selr, pt_selw;
119 u_char pt_send;
120 u_char pt_ucntl;
121 struct tty pt_tty;
122 dev_t devs, devc;
123 struct prison *pt_prison;
124};
125
126#define PF_PKT 0x08 /* packet mode */
127#define PF_STOPPED 0x10 /* user told stopped */
128#define PF_REMOTE 0x20 /* remote and flow controlled input */
129#define PF_NOSTOP 0x40
130#define PF_UCNTL 0x80 /* user control mode */
131
132static char *names = "pqrsPQRS";
133/*
134 * This function creates and initializes a pts/ptc pair
135 *
136 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
137 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
138 *
139 * XXX: define and add mapping of upper minor bits to allow more
140 * than 256 ptys.
141 */
142static dev_t
143ptyinit(dev_t devc)
144{
145 dev_t devs;
146 struct pt_ioctl *pt;
147 int n;
148
149 n = minor(devc);
150 /* For now we only map the lower 8 bits of the minor */
151 if (n & ~0xff)
152 return (NODEV);
153
154 devc->si_flags &= ~SI_CHEAPCLONE;
155
156 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
157 pt->devs = devs = make_dev(&pts_cdevsw, n,
158 UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
159 pt->devc = devc;
160
161 devs->si_drv1 = devc->si_drv1 = pt;
162 devs->si_tty = devc->si_tty = &pt->pt_tty;
163 pt->pt_tty.t_dev = devs;
164 ttyregister(&pt->pt_tty);
165 return (devc);
166}
167
168/*ARGSUSED*/
169static int
170ptsopen(dev, flag, devtype, td)
171 dev_t dev;
172 int flag, devtype;
173 struct thread *td;
174{
175 struct tty *tp;
176 int error;
177 struct pt_ioctl *pti;
178
179 if (!dev->si_drv1)
180 return(ENXIO);
181 pti = dev->si_drv1;
182 tp = dev->si_tty;
183 if ((tp->t_state & TS_ISOPEN) == 0) {
184 ttychars(tp); /* Set up default chars */
185 tp->t_iflag = TTYDEF_IFLAG;
186 tp->t_oflag = TTYDEF_OFLAG;
187 tp->t_lflag = TTYDEF_LFLAG;
188 tp->t_cflag = TTYDEF_CFLAG;
189 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
190 } else if (tp->t_state & TS_XCLUDE && suser(td)) {
191 return (EBUSY);
192 } else if (pti->pt_prison != td->td_ucred->cr_prison) {
193 return (EBUSY);
194 }
195 if (tp->t_oproc) /* Ctrlr still around. */
196 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
197 while ((tp->t_state & TS_CARR_ON) == 0) {
198 if (flag&FNONBLOCK)
199 break;
200 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
201 "ptsopn", 0);
202 if (error)
203 return (error);
204 }
205 error = (*linesw[tp->t_line].l_open)(dev, tp);
206 if (error == 0)
207 ptcwakeup(tp, FREAD|FWRITE);
208 return (error);
209}
210
211static int
212ptsclose(dev, flag, mode, td)
213 dev_t dev;
214 int flag, mode;
215 struct thread *td;
216{
217 struct tty *tp;
218 int err;
219
220 tp = dev->si_tty;
221 err = (*linesw[tp->t_line].l_close)(tp, flag);
222 ptsstop(tp, FREAD|FWRITE);
223 (void) ttyclose(tp);
224 return (err);
225}
226
227static int
228ptsread(dev, uio, flag)
229 dev_t dev;
230 struct uio *uio;
231 int flag;
232{
233 struct thread *td = curthread;
234 struct proc *p = td->td_proc;
235 struct tty *tp = dev->si_tty;
236 struct pt_ioctl *pti = dev->si_drv1;
237 struct pgrp *pg;
238 int error = 0;
239
240again:
241 if (pti->pt_flags & PF_REMOTE) {
242 while (isbackground(p, tp)) {
243 sx_slock(&proctree_lock);
244 PROC_LOCK(p);
245 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
246 SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
247 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) {
248 PROC_UNLOCK(p);
249 sx_sunlock(&proctree_lock);
250 return (EIO);
251 }
252 pg = p->p_pgrp;
253 PROC_UNLOCK(p);
254 PGRP_LOCK(pg);
255 sx_sunlock(&proctree_lock);
256 pgsignal(pg, SIGTTIN, 1);
257 PGRP_UNLOCK(pg);
258 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
259 0);
260 if (error)
261 return (error);
262 }
263 if (tp->t_canq.c_cc == 0) {
264 if (flag & IO_NDELAY)
265 return (EWOULDBLOCK);
266 error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
267 "ptsin", 0);
268 if (error)
269 return (error);
270 goto again;
271 }
272 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
273 if (ureadc(getc(&tp->t_canq), uio) < 0) {
274 error = EFAULT;
275 break;
276 }
277 if (tp->t_canq.c_cc == 1)
278 (void) getc(&tp->t_canq);
279 if (tp->t_canq.c_cc)
280 return (error);
281 } else
282 if (tp->t_oproc)
283 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
284 ptcwakeup(tp, FWRITE);
285 return (error);
286}
287
288/*
289 * Write to pseudo-tty.
290 * Wakeups of controlling tty will happen
291 * indirectly, when tty driver calls ptsstart.
292 */
293static int
294ptswrite(dev, uio, flag)
295 dev_t dev;
296 struct uio *uio;
297 int flag;
298{
299 struct tty *tp;
300
301 tp = dev->si_tty;
302 if (tp->t_oproc == 0)
303 return (EIO);
304 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
305}
306
307/*
308 * Start output on pseudo-tty.
309 * Wake up process selecting or sleeping for input from controlling tty.
310 */
311static void
312ptsstart(tp)
313 struct tty *tp;
314{
315 struct pt_ioctl *pti = tp->t_dev->si_drv1;
316
317 if (tp->t_state & TS_TTSTOP)
318 return;
319 if (pti->pt_flags & PF_STOPPED) {
320 pti->pt_flags &= ~PF_STOPPED;
321 pti->pt_send = TIOCPKT_START;
322 }
323 ptcwakeup(tp, FREAD);
324}
325
326static void
327ptcwakeup(tp, flag)
328 struct tty *tp;
329 int flag;
330{
331 struct pt_ioctl *pti = tp->t_dev->si_drv1;
332
333 if (flag & FREAD) {
334 selwakeup(&pti->pt_selr);
335 wakeup(TSA_PTC_READ(tp));
336 }
337 if (flag & FWRITE) {
338 selwakeup(&pti->pt_selw);
339 wakeup(TSA_PTC_WRITE(tp));
340 }
341}
342
343static int
344ptcopen(dev, flag, devtype, td)
345 dev_t dev;
346 int flag, devtype;
347 struct thread *td;
348{
349 struct tty *tp;
350 struct pt_ioctl *pti;
351
352 if (!dev->si_drv1)
353 ptyinit(dev);
354 if (!dev->si_drv1)
355 return(ENXIO);
356 tp = dev->si_tty;
357 if (tp->t_oproc)
358 return (EIO);
359 tp->t_timeout = -1;
360 tp->t_oproc = ptsstart;
361 tp->t_stop = ptsstop;
362 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
363 tp->t_lflag &= ~EXTPROC;
364 pti = dev->si_drv1;
365 pti->pt_prison = td->td_ucred->cr_prison;
366 pti->pt_flags = 0;
367 pti->pt_send = 0;
368 pti->pt_ucntl = 0;
369 return (0);
370}
371
372static int
373ptcclose(dev, flags, fmt, td)
374 dev_t dev;
375 int flags;
376 int fmt;
377 struct thread *td;
378{
379 struct tty *tp;
380
381 tp = dev->si_tty;
382 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
383
384 /*
385 * XXX MDMBUF makes no sense for ptys but would inhibit the above
386 * l_modem(). CLOCAL makes sense but isn't supported. Special
387 * l_modem()s that ignore carrier drop make no sense for ptys but
388 * may be in use because other parts of the line discipline make
389 * sense for ptys. Recover by doing everything that a normal
390 * ttymodem() would have done except for sending a SIGHUP.
391 */
392 if (tp->t_state & TS_ISOPEN) {
393 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
394 tp->t_state |= TS_ZOMBIE;
395 ttyflush(tp, FREAD | FWRITE);
396 }
397
398 tp->t_oproc = 0; /* mark closed */
399 return (0);
400}
401
402static int
403ptcread(dev, uio, flag)
404 dev_t dev;
405 struct uio *uio;
406 int flag;
407{
408 struct tty *tp = dev->si_tty;
409 struct pt_ioctl *pti = dev->si_drv1;
410 char buf[BUFSIZ];
411 int error = 0, cc;
412
413 /*
414 * We want to block until the slave
415 * is open, and there's something to read;
416 * but if we lost the slave or we're NBIO,
417 * then return the appropriate error instead.
418 */
419 for (;;) {
420 if (tp->t_state&TS_ISOPEN) {
421 if (pti->pt_flags&PF_PKT && pti->pt_send) {
422 error = ureadc((int)pti->pt_send, uio);
423 if (error)
424 return (error);
425 if (pti->pt_send & TIOCPKT_IOCTL) {
426 cc = min(uio->uio_resid,
427 sizeof(tp->t_termios));
428 uiomove(&tp->t_termios, cc, uio);
429 }
430 pti->pt_send = 0;
431 return (0);
432 }
433 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
434 error = ureadc((int)pti->pt_ucntl, uio);
435 if (error)
436 return (error);
437 pti->pt_ucntl = 0;
438 return (0);
439 }
440 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
441 break;
442 }
443 if ((tp->t_state & TS_CONNECTED) == 0)
444 return (0); /* EOF */
445 if (flag & IO_NDELAY)
446 return (EWOULDBLOCK);
447 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
448 if (error)
449 return (error);
450 }
451 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
452 error = ureadc(0, uio);
453 while (uio->uio_resid > 0 && error == 0) {
454 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
455 if (cc <= 0)
456 break;
457 error = uiomove(buf, cc, uio);
458 }
459 ttwwakeup(tp);
460 return (error);
461}
462
463static void
464ptsstop(tp, flush)
465 struct tty *tp;
466 int flush;
467{
468 struct pt_ioctl *pti = tp->t_dev->si_drv1;
469 int flag;
470
471 /* note: FLUSHREAD and FLUSHWRITE already ok */
472 if (flush == 0) {
473 flush = TIOCPKT_STOP;
474 pti->pt_flags |= PF_STOPPED;
475 } else
476 pti->pt_flags &= ~PF_STOPPED;
477 pti->pt_send |= flush;
478 /* change of perspective */
479 flag = 0;
480 if (flush & FREAD)
481 flag |= FWRITE;
482 if (flush & FWRITE)
483 flag |= FREAD;
484 ptcwakeup(tp, flag);
485}
486
487static int
488ptcpoll(dev, events, td)
489 dev_t dev;
490 int events;
491 struct thread *td;
492{
493 struct tty *tp = dev->si_tty;
494 struct pt_ioctl *pti = dev->si_drv1;
495 int revents = 0;
496 int s;
497
498 if ((tp->t_state & TS_CONNECTED) == 0)
499 return (seltrue(dev, events, td) | POLLHUP);
500
501 /*
502 * Need to block timeouts (ttrstart).
503 */
504 s = spltty();
505
506 if (events & (POLLIN | POLLRDNORM))
507 if ((tp->t_state & TS_ISOPEN) &&
508 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
509 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
510 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
511 revents |= events & (POLLIN | POLLRDNORM);
512
513 if (events & (POLLOUT | POLLWRNORM))
514 if (tp->t_state & TS_ISOPEN &&
515 ((pti->pt_flags & PF_REMOTE) ?
516 (tp->t_canq.c_cc == 0) :
517 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
518 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
519 revents |= events & (POLLOUT | POLLWRNORM);
520
521 if (events & POLLHUP)
522 if ((tp->t_state & TS_CARR_ON) == 0)
523 revents |= POLLHUP;
524
525 if (revents == 0) {
526 if (events & (POLLIN | POLLRDNORM))
527 selrecord(td, &pti->pt_selr);
528
529 if (events & (POLLOUT | POLLWRNORM))
530 selrecord(td, &pti->pt_selw);
531 }
532 splx(s);
533
534 return (revents);
535}
536
537static int
538ptcwrite(dev, uio, flag)
539 dev_t dev;
540 struct uio *uio;
541 int flag;
542{
543 struct tty *tp = dev->si_tty;
544 u_char *cp = 0;
545 int cc = 0;
546 u_char locbuf[BUFSIZ];
547 int cnt = 0;
548 struct pt_ioctl *pti = dev->si_drv1;
549 int error = 0;
550
551again:
552 if ((tp->t_state&TS_ISOPEN) == 0)
553 goto block;
554 if (pti->pt_flags & PF_REMOTE) {
555 if (tp->t_canq.c_cc)
556 goto block;
557 while ((uio->uio_resid > 0 || cc > 0) &&
558 tp->t_canq.c_cc < TTYHOG - 1) {
559 if (cc == 0) {
560 cc = min(uio->uio_resid, BUFSIZ);
561 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
562 cp = locbuf;
563 error = uiomove(cp, cc, uio);
564 if (error)
565 return (error);
566 /* check again for safety */
567 if ((tp->t_state & TS_ISOPEN) == 0) {
568 /* adjust as usual */
569 uio->uio_resid += cc;
570 return (EIO);
571 }
572 }
573 if (cc > 0) {
574 cc = b_to_q((char *)cp, cc, &tp->t_canq);
575 /*
576 * XXX we don't guarantee that the canq size
577 * is >= TTYHOG, so the above b_to_q() may
578 * leave some bytes uncopied. However, space
579 * is guaranteed for the null terminator if
580 * we don't fail here since (TTYHOG - 1) is
581 * not a multiple of CBSIZE.
582 */
583 if (cc > 0)
584 break;
585 }
586 }
587 /* adjust for data copied in but not written */
588 uio->uio_resid += cc;
589 (void) putc(0, &tp->t_canq);
590 ttwakeup(tp);
591 wakeup(TSA_PTS_READ(tp));
592 return (0);
593 }
594 while (uio->uio_resid > 0 || cc > 0) {
595 if (cc == 0) {
596 cc = min(uio->uio_resid, BUFSIZ);
597 cp = locbuf;
598 error = uiomove(cp, cc, uio);
599 if (error)
600 return (error);
601 /* check again for safety */
602 if ((tp->t_state & TS_ISOPEN) == 0) {
603 /* adjust for data copied in but not written */
604 uio->uio_resid += cc;
605 return (EIO);
606 }
607 }
608 while (cc > 0) {
609 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
610 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
611 wakeup(TSA_HUP_OR_INPUT(tp));
612 goto block;
613 }
614 (*linesw[tp->t_line].l_rint)(*cp++, tp);
615 cnt++;
616 cc--;
617 }
618 cc = 0;
619 }
620 return (0);
621block:
622 /*
623 * Come here to wait for slave to open, for space
624 * in outq, or space in rawq, or an empty canq.
625 */
626 if ((tp->t_state & TS_CONNECTED) == 0) {
627 /* adjust for data copied in but not written */
628 uio->uio_resid += cc;
629 return (EIO);
630 }
631 if (flag & IO_NDELAY) {
632 /* adjust for data copied in but not written */
633 uio->uio_resid += cc;
634 if (cnt == 0)
635 return (EWOULDBLOCK);
636 return (0);
637 }
638 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
639 if (error) {
640 /* adjust for data copied in but not written */
641 uio->uio_resid += cc;
642 return (error);
643 }
644 goto again;
645}
646
647/*ARGSUSED*/
648static int
649ptyioctl(dev, cmd, data, flag, td)
650 dev_t dev;
651 u_long cmd;
652 caddr_t data;
653 int flag;
654 struct thread *td;
655{
656 struct tty *tp = dev->si_tty;
657 struct pt_ioctl *pti = dev->si_drv1;
658 u_char *cc = tp->t_cc;
659 int stop, error;
660
661 if (devsw(dev)->d_open == ptcopen) {
662 switch (cmd) {
663
664 case TIOCGPGRP:
665 /*
666 * We avoid calling ttioctl on the controller since,
667 * in that case, tp must be the controlling terminal.
668 */
669 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
670 return (0);
671
672 case TIOCPKT:
673 if (*(int *)data) {
674 if (pti->pt_flags & PF_UCNTL)
675 return (EINVAL);
676 pti->pt_flags |= PF_PKT;
677 } else
678 pti->pt_flags &= ~PF_PKT;
679 return (0);
680
681 case TIOCUCNTL:
682 if (*(int *)data) {
683 if (pti->pt_flags & PF_PKT)
684 return (EINVAL);
685 pti->pt_flags |= PF_UCNTL;
686 } else
687 pti->pt_flags &= ~PF_UCNTL;
688 return (0);
689
690 case TIOCREMOTE:
691 if (*(int *)data)
692 pti->pt_flags |= PF_REMOTE;
693 else
694 pti->pt_flags &= ~PF_REMOTE;
695 ttyflush(tp, FREAD|FWRITE);
696 return (0);
697 }
698
699 /*
700 * The rest of the ioctls shouldn't be called until
701 * the slave is open.
702 */
703 if ((tp->t_state & TS_ISOPEN) == 0)
704 return (EAGAIN);
705
706 switch (cmd) {
707#ifdef COMPAT_43
708 case TIOCSETP:
709 case TIOCSETN:
710#endif
711 case TIOCSETD:
712 case TIOCSETA:
713 case TIOCSETAW:
714 case TIOCSETAF:
715 /*
716 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
717 * ttywflush(tp) will hang if there are characters in
718 * the outq.
719 */
720 ndflush(&tp->t_outq, tp->t_outq.c_cc);
721 break;
722
723 case TIOCSIG:
724 if (*(unsigned int *)data >= NSIG ||
725 *(unsigned int *)data == 0)
726 return(EINVAL);
727 if ((tp->t_lflag&NOFLSH) == 0)
728 ttyflush(tp, FREAD|FWRITE);
729 if (tp->t_pgrp != NULL) {
730 PGRP_LOCK(tp->t_pgrp);
731 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
732 PGRP_UNLOCK(tp->t_pgrp);
733 }
734 if ((*(unsigned int *)data == SIGINFO) &&
735 ((tp->t_lflag&NOKERNINFO) == 0))
736 ttyinfo(tp);
737 return(0);
738 }
739 }
740 if (cmd == TIOCEXT) {
741 /*
742 * When the EXTPROC bit is being toggled, we need
743 * to send an TIOCPKT_IOCTL if the packet driver
744 * is turned on.
745 */
746 if (*(int *)data) {
747 if (pti->pt_flags & PF_PKT) {
748 pti->pt_send |= TIOCPKT_IOCTL;
749 ptcwakeup(tp, FREAD);
750 }
751 tp->t_lflag |= EXTPROC;
752 } else {
753 if ((tp->t_lflag & EXTPROC) &&
754 (pti->pt_flags & PF_PKT)) {
755 pti->pt_send |= TIOCPKT_IOCTL;
756 ptcwakeup(tp, FREAD);
757 }
758 tp->t_lflag &= ~EXTPROC;
759 }
760 return(0);
761 }
762 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
763 if (error == ENOIOCTL)
764 error = ttioctl(tp, cmd, data, flag);
765 if (error == ENOIOCTL) {
766 if (pti->pt_flags & PF_UCNTL &&
767 (cmd & ~0xff) == UIOCCMD(0)) {
768 if (cmd & 0xff) {
769 pti->pt_ucntl = (u_char)cmd;
770 ptcwakeup(tp, FREAD);
771 }
772 return (0);
773 }
774 error = ENOTTY;
775 }
776 /*
777 * If external processing and packet mode send ioctl packet.
778 */
779 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
780 switch(cmd) {
781 case TIOCSETA:
782 case TIOCSETAW:
783 case TIOCSETAF:
784#ifdef COMPAT_43
785 case TIOCSETP:
786 case TIOCSETN:
787#endif
788#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
789 case TIOCSETC:
790 case TIOCSLTC:
791 case TIOCLBIS:
792 case TIOCLBIC:
793 case TIOCLSET:
794#endif
795 pti->pt_send |= TIOCPKT_IOCTL;
796 ptcwakeup(tp, FREAD);
797 default:
798 break;
799 }
800 }
801 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
802 && CCEQ(cc[VSTART], CTRL('q'));
803 if (pti->pt_flags & PF_NOSTOP) {
804 if (stop) {
805 pti->pt_send &= ~TIOCPKT_NOSTOP;
806 pti->pt_send |= TIOCPKT_DOSTOP;
807 pti->pt_flags &= ~PF_NOSTOP;
808 ptcwakeup(tp, FREAD);
809 }
810 } else {
811 if (!stop) {
812 pti->pt_send &= ~TIOCPKT_DOSTOP;
813 pti->pt_send |= TIOCPKT_NOSTOP;
814 pti->pt_flags |= PF_NOSTOP;
815 ptcwakeup(tp, FREAD);
816 }
817 }
818 return (error);
819}
820
821
822static void ptc_drvinit(void *unused);
823
824static void pty_clone(void *arg, char *name, int namelen, dev_t *dev);
825
826static void
827pty_clone(arg, name, namelen, dev)
828 void *arg;
829 char *name;
830 int namelen;
831 dev_t *dev;
832{
833 int u;
834
835 if (*dev != NODEV)
836 return;
837 if (bcmp(name, "pty", 3) != 0)
838 return;
839 if (name[5] != '\0')
840 return;
841 switch (name[3]) {
842 case 'p': u = 0; break;
843 case 'q': u = 32; break;
844 case 'r': u = 64; break;
845 case 's': u = 96; break;
846 case 'P': u = 128; break;
847 case 'Q': u = 160; break;
848 case 'R': u = 192; break;
849 case 'S': u = 224; break;
850 default: return;
851 }
852 if (name[4] >= '0' && name[4] <= '9')
853 u += name[4] - '0';
854 else if (name[4] >= 'a' && name[4] <= 'v')
855 u += name[4] - 'a' + 10;
856 else
857 return;
858 *dev = make_dev(&ptc_cdevsw, u,
859 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
860 (*dev)->si_flags |= SI_CHEAPCLONE;
861 return;
862}
863
864static void
865ptc_drvinit(unused)
866 void *unused;
867{
868
869 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
870}
871
872SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
104};
105
106#define BUFSIZ 100 /* Chunk size iomoved to/from user */
107
108struct pt_ioctl {
109 int pt_flags;
110 struct selinfo pt_selr, pt_selw;
111 u_char pt_send;
112 u_char pt_ucntl;
113 struct tty pt_tty;
114 dev_t devs, devc;
115 struct prison *pt_prison;
116};
117
118#define PF_PKT 0x08 /* packet mode */
119#define PF_STOPPED 0x10 /* user told stopped */
120#define PF_REMOTE 0x20 /* remote and flow controlled input */
121#define PF_NOSTOP 0x40
122#define PF_UCNTL 0x80 /* user control mode */
123
124static char *names = "pqrsPQRS";
125/*
126 * This function creates and initializes a pts/ptc pair
127 *
128 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
129 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
130 *
131 * XXX: define and add mapping of upper minor bits to allow more
132 * than 256 ptys.
133 */
134static dev_t
135ptyinit(dev_t devc)
136{
137 dev_t devs;
138 struct pt_ioctl *pt;
139 int n;
140
141 n = minor(devc);
142 /* For now we only map the lower 8 bits of the minor */
143 if (n & ~0xff)
144 return (NODEV);
145
146 devc->si_flags &= ~SI_CHEAPCLONE;
147
148 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
149 pt->devs = devs = make_dev(&pts_cdevsw, n,
150 UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
151 pt->devc = devc;
152
153 devs->si_drv1 = devc->si_drv1 = pt;
154 devs->si_tty = devc->si_tty = &pt->pt_tty;
155 pt->pt_tty.t_dev = devs;
156 ttyregister(&pt->pt_tty);
157 return (devc);
158}
159
160/*ARGSUSED*/
161static int
162ptsopen(dev, flag, devtype, td)
163 dev_t dev;
164 int flag, devtype;
165 struct thread *td;
166{
167 struct tty *tp;
168 int error;
169 struct pt_ioctl *pti;
170
171 if (!dev->si_drv1)
172 return(ENXIO);
173 pti = dev->si_drv1;
174 tp = dev->si_tty;
175 if ((tp->t_state & TS_ISOPEN) == 0) {
176 ttychars(tp); /* Set up default chars */
177 tp->t_iflag = TTYDEF_IFLAG;
178 tp->t_oflag = TTYDEF_OFLAG;
179 tp->t_lflag = TTYDEF_LFLAG;
180 tp->t_cflag = TTYDEF_CFLAG;
181 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
182 } else if (tp->t_state & TS_XCLUDE && suser(td)) {
183 return (EBUSY);
184 } else if (pti->pt_prison != td->td_ucred->cr_prison) {
185 return (EBUSY);
186 }
187 if (tp->t_oproc) /* Ctrlr still around. */
188 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
189 while ((tp->t_state & TS_CARR_ON) == 0) {
190 if (flag&FNONBLOCK)
191 break;
192 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
193 "ptsopn", 0);
194 if (error)
195 return (error);
196 }
197 error = (*linesw[tp->t_line].l_open)(dev, tp);
198 if (error == 0)
199 ptcwakeup(tp, FREAD|FWRITE);
200 return (error);
201}
202
203static int
204ptsclose(dev, flag, mode, td)
205 dev_t dev;
206 int flag, mode;
207 struct thread *td;
208{
209 struct tty *tp;
210 int err;
211
212 tp = dev->si_tty;
213 err = (*linesw[tp->t_line].l_close)(tp, flag);
214 ptsstop(tp, FREAD|FWRITE);
215 (void) ttyclose(tp);
216 return (err);
217}
218
219static int
220ptsread(dev, uio, flag)
221 dev_t dev;
222 struct uio *uio;
223 int flag;
224{
225 struct thread *td = curthread;
226 struct proc *p = td->td_proc;
227 struct tty *tp = dev->si_tty;
228 struct pt_ioctl *pti = dev->si_drv1;
229 struct pgrp *pg;
230 int error = 0;
231
232again:
233 if (pti->pt_flags & PF_REMOTE) {
234 while (isbackground(p, tp)) {
235 sx_slock(&proctree_lock);
236 PROC_LOCK(p);
237 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
238 SIGISMEMBER(p->p_sigmask, SIGTTIN) ||
239 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) {
240 PROC_UNLOCK(p);
241 sx_sunlock(&proctree_lock);
242 return (EIO);
243 }
244 pg = p->p_pgrp;
245 PROC_UNLOCK(p);
246 PGRP_LOCK(pg);
247 sx_sunlock(&proctree_lock);
248 pgsignal(pg, SIGTTIN, 1);
249 PGRP_UNLOCK(pg);
250 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH, "ptsbg",
251 0);
252 if (error)
253 return (error);
254 }
255 if (tp->t_canq.c_cc == 0) {
256 if (flag & IO_NDELAY)
257 return (EWOULDBLOCK);
258 error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
259 "ptsin", 0);
260 if (error)
261 return (error);
262 goto again;
263 }
264 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
265 if (ureadc(getc(&tp->t_canq), uio) < 0) {
266 error = EFAULT;
267 break;
268 }
269 if (tp->t_canq.c_cc == 1)
270 (void) getc(&tp->t_canq);
271 if (tp->t_canq.c_cc)
272 return (error);
273 } else
274 if (tp->t_oproc)
275 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
276 ptcwakeup(tp, FWRITE);
277 return (error);
278}
279
280/*
281 * Write to pseudo-tty.
282 * Wakeups of controlling tty will happen
283 * indirectly, when tty driver calls ptsstart.
284 */
285static int
286ptswrite(dev, uio, flag)
287 dev_t dev;
288 struct uio *uio;
289 int flag;
290{
291 struct tty *tp;
292
293 tp = dev->si_tty;
294 if (tp->t_oproc == 0)
295 return (EIO);
296 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
297}
298
299/*
300 * Start output on pseudo-tty.
301 * Wake up process selecting or sleeping for input from controlling tty.
302 */
303static void
304ptsstart(tp)
305 struct tty *tp;
306{
307 struct pt_ioctl *pti = tp->t_dev->si_drv1;
308
309 if (tp->t_state & TS_TTSTOP)
310 return;
311 if (pti->pt_flags & PF_STOPPED) {
312 pti->pt_flags &= ~PF_STOPPED;
313 pti->pt_send = TIOCPKT_START;
314 }
315 ptcwakeup(tp, FREAD);
316}
317
318static void
319ptcwakeup(tp, flag)
320 struct tty *tp;
321 int flag;
322{
323 struct pt_ioctl *pti = tp->t_dev->si_drv1;
324
325 if (flag & FREAD) {
326 selwakeup(&pti->pt_selr);
327 wakeup(TSA_PTC_READ(tp));
328 }
329 if (flag & FWRITE) {
330 selwakeup(&pti->pt_selw);
331 wakeup(TSA_PTC_WRITE(tp));
332 }
333}
334
335static int
336ptcopen(dev, flag, devtype, td)
337 dev_t dev;
338 int flag, devtype;
339 struct thread *td;
340{
341 struct tty *tp;
342 struct pt_ioctl *pti;
343
344 if (!dev->si_drv1)
345 ptyinit(dev);
346 if (!dev->si_drv1)
347 return(ENXIO);
348 tp = dev->si_tty;
349 if (tp->t_oproc)
350 return (EIO);
351 tp->t_timeout = -1;
352 tp->t_oproc = ptsstart;
353 tp->t_stop = ptsstop;
354 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
355 tp->t_lflag &= ~EXTPROC;
356 pti = dev->si_drv1;
357 pti->pt_prison = td->td_ucred->cr_prison;
358 pti->pt_flags = 0;
359 pti->pt_send = 0;
360 pti->pt_ucntl = 0;
361 return (0);
362}
363
364static int
365ptcclose(dev, flags, fmt, td)
366 dev_t dev;
367 int flags;
368 int fmt;
369 struct thread *td;
370{
371 struct tty *tp;
372
373 tp = dev->si_tty;
374 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
375
376 /*
377 * XXX MDMBUF makes no sense for ptys but would inhibit the above
378 * l_modem(). CLOCAL makes sense but isn't supported. Special
379 * l_modem()s that ignore carrier drop make no sense for ptys but
380 * may be in use because other parts of the line discipline make
381 * sense for ptys. Recover by doing everything that a normal
382 * ttymodem() would have done except for sending a SIGHUP.
383 */
384 if (tp->t_state & TS_ISOPEN) {
385 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
386 tp->t_state |= TS_ZOMBIE;
387 ttyflush(tp, FREAD | FWRITE);
388 }
389
390 tp->t_oproc = 0; /* mark closed */
391 return (0);
392}
393
394static int
395ptcread(dev, uio, flag)
396 dev_t dev;
397 struct uio *uio;
398 int flag;
399{
400 struct tty *tp = dev->si_tty;
401 struct pt_ioctl *pti = dev->si_drv1;
402 char buf[BUFSIZ];
403 int error = 0, cc;
404
405 /*
406 * We want to block until the slave
407 * is open, and there's something to read;
408 * but if we lost the slave or we're NBIO,
409 * then return the appropriate error instead.
410 */
411 for (;;) {
412 if (tp->t_state&TS_ISOPEN) {
413 if (pti->pt_flags&PF_PKT && pti->pt_send) {
414 error = ureadc((int)pti->pt_send, uio);
415 if (error)
416 return (error);
417 if (pti->pt_send & TIOCPKT_IOCTL) {
418 cc = min(uio->uio_resid,
419 sizeof(tp->t_termios));
420 uiomove(&tp->t_termios, cc, uio);
421 }
422 pti->pt_send = 0;
423 return (0);
424 }
425 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
426 error = ureadc((int)pti->pt_ucntl, uio);
427 if (error)
428 return (error);
429 pti->pt_ucntl = 0;
430 return (0);
431 }
432 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
433 break;
434 }
435 if ((tp->t_state & TS_CONNECTED) == 0)
436 return (0); /* EOF */
437 if (flag & IO_NDELAY)
438 return (EWOULDBLOCK);
439 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
440 if (error)
441 return (error);
442 }
443 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
444 error = ureadc(0, uio);
445 while (uio->uio_resid > 0 && error == 0) {
446 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
447 if (cc <= 0)
448 break;
449 error = uiomove(buf, cc, uio);
450 }
451 ttwwakeup(tp);
452 return (error);
453}
454
455static void
456ptsstop(tp, flush)
457 struct tty *tp;
458 int flush;
459{
460 struct pt_ioctl *pti = tp->t_dev->si_drv1;
461 int flag;
462
463 /* note: FLUSHREAD and FLUSHWRITE already ok */
464 if (flush == 0) {
465 flush = TIOCPKT_STOP;
466 pti->pt_flags |= PF_STOPPED;
467 } else
468 pti->pt_flags &= ~PF_STOPPED;
469 pti->pt_send |= flush;
470 /* change of perspective */
471 flag = 0;
472 if (flush & FREAD)
473 flag |= FWRITE;
474 if (flush & FWRITE)
475 flag |= FREAD;
476 ptcwakeup(tp, flag);
477}
478
479static int
480ptcpoll(dev, events, td)
481 dev_t dev;
482 int events;
483 struct thread *td;
484{
485 struct tty *tp = dev->si_tty;
486 struct pt_ioctl *pti = dev->si_drv1;
487 int revents = 0;
488 int s;
489
490 if ((tp->t_state & TS_CONNECTED) == 0)
491 return (seltrue(dev, events, td) | POLLHUP);
492
493 /*
494 * Need to block timeouts (ttrstart).
495 */
496 s = spltty();
497
498 if (events & (POLLIN | POLLRDNORM))
499 if ((tp->t_state & TS_ISOPEN) &&
500 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
501 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
502 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl)))
503 revents |= events & (POLLIN | POLLRDNORM);
504
505 if (events & (POLLOUT | POLLWRNORM))
506 if (tp->t_state & TS_ISOPEN &&
507 ((pti->pt_flags & PF_REMOTE) ?
508 (tp->t_canq.c_cc == 0) :
509 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
510 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
511 revents |= events & (POLLOUT | POLLWRNORM);
512
513 if (events & POLLHUP)
514 if ((tp->t_state & TS_CARR_ON) == 0)
515 revents |= POLLHUP;
516
517 if (revents == 0) {
518 if (events & (POLLIN | POLLRDNORM))
519 selrecord(td, &pti->pt_selr);
520
521 if (events & (POLLOUT | POLLWRNORM))
522 selrecord(td, &pti->pt_selw);
523 }
524 splx(s);
525
526 return (revents);
527}
528
529static int
530ptcwrite(dev, uio, flag)
531 dev_t dev;
532 struct uio *uio;
533 int flag;
534{
535 struct tty *tp = dev->si_tty;
536 u_char *cp = 0;
537 int cc = 0;
538 u_char locbuf[BUFSIZ];
539 int cnt = 0;
540 struct pt_ioctl *pti = dev->si_drv1;
541 int error = 0;
542
543again:
544 if ((tp->t_state&TS_ISOPEN) == 0)
545 goto block;
546 if (pti->pt_flags & PF_REMOTE) {
547 if (tp->t_canq.c_cc)
548 goto block;
549 while ((uio->uio_resid > 0 || cc > 0) &&
550 tp->t_canq.c_cc < TTYHOG - 1) {
551 if (cc == 0) {
552 cc = min(uio->uio_resid, BUFSIZ);
553 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
554 cp = locbuf;
555 error = uiomove(cp, cc, uio);
556 if (error)
557 return (error);
558 /* check again for safety */
559 if ((tp->t_state & TS_ISOPEN) == 0) {
560 /* adjust as usual */
561 uio->uio_resid += cc;
562 return (EIO);
563 }
564 }
565 if (cc > 0) {
566 cc = b_to_q((char *)cp, cc, &tp->t_canq);
567 /*
568 * XXX we don't guarantee that the canq size
569 * is >= TTYHOG, so the above b_to_q() may
570 * leave some bytes uncopied. However, space
571 * is guaranteed for the null terminator if
572 * we don't fail here since (TTYHOG - 1) is
573 * not a multiple of CBSIZE.
574 */
575 if (cc > 0)
576 break;
577 }
578 }
579 /* adjust for data copied in but not written */
580 uio->uio_resid += cc;
581 (void) putc(0, &tp->t_canq);
582 ttwakeup(tp);
583 wakeup(TSA_PTS_READ(tp));
584 return (0);
585 }
586 while (uio->uio_resid > 0 || cc > 0) {
587 if (cc == 0) {
588 cc = min(uio->uio_resid, BUFSIZ);
589 cp = locbuf;
590 error = uiomove(cp, cc, uio);
591 if (error)
592 return (error);
593 /* check again for safety */
594 if ((tp->t_state & TS_ISOPEN) == 0) {
595 /* adjust for data copied in but not written */
596 uio->uio_resid += cc;
597 return (EIO);
598 }
599 }
600 while (cc > 0) {
601 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
602 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
603 wakeup(TSA_HUP_OR_INPUT(tp));
604 goto block;
605 }
606 (*linesw[tp->t_line].l_rint)(*cp++, tp);
607 cnt++;
608 cc--;
609 }
610 cc = 0;
611 }
612 return (0);
613block:
614 /*
615 * Come here to wait for slave to open, for space
616 * in outq, or space in rawq, or an empty canq.
617 */
618 if ((tp->t_state & TS_CONNECTED) == 0) {
619 /* adjust for data copied in but not written */
620 uio->uio_resid += cc;
621 return (EIO);
622 }
623 if (flag & IO_NDELAY) {
624 /* adjust for data copied in but not written */
625 uio->uio_resid += cc;
626 if (cnt == 0)
627 return (EWOULDBLOCK);
628 return (0);
629 }
630 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
631 if (error) {
632 /* adjust for data copied in but not written */
633 uio->uio_resid += cc;
634 return (error);
635 }
636 goto again;
637}
638
639/*ARGSUSED*/
640static int
641ptyioctl(dev, cmd, data, flag, td)
642 dev_t dev;
643 u_long cmd;
644 caddr_t data;
645 int flag;
646 struct thread *td;
647{
648 struct tty *tp = dev->si_tty;
649 struct pt_ioctl *pti = dev->si_drv1;
650 u_char *cc = tp->t_cc;
651 int stop, error;
652
653 if (devsw(dev)->d_open == ptcopen) {
654 switch (cmd) {
655
656 case TIOCGPGRP:
657 /*
658 * We avoid calling ttioctl on the controller since,
659 * in that case, tp must be the controlling terminal.
660 */
661 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
662 return (0);
663
664 case TIOCPKT:
665 if (*(int *)data) {
666 if (pti->pt_flags & PF_UCNTL)
667 return (EINVAL);
668 pti->pt_flags |= PF_PKT;
669 } else
670 pti->pt_flags &= ~PF_PKT;
671 return (0);
672
673 case TIOCUCNTL:
674 if (*(int *)data) {
675 if (pti->pt_flags & PF_PKT)
676 return (EINVAL);
677 pti->pt_flags |= PF_UCNTL;
678 } else
679 pti->pt_flags &= ~PF_UCNTL;
680 return (0);
681
682 case TIOCREMOTE:
683 if (*(int *)data)
684 pti->pt_flags |= PF_REMOTE;
685 else
686 pti->pt_flags &= ~PF_REMOTE;
687 ttyflush(tp, FREAD|FWRITE);
688 return (0);
689 }
690
691 /*
692 * The rest of the ioctls shouldn't be called until
693 * the slave is open.
694 */
695 if ((tp->t_state & TS_ISOPEN) == 0)
696 return (EAGAIN);
697
698 switch (cmd) {
699#ifdef COMPAT_43
700 case TIOCSETP:
701 case TIOCSETN:
702#endif
703 case TIOCSETD:
704 case TIOCSETA:
705 case TIOCSETAW:
706 case TIOCSETAF:
707 /*
708 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
709 * ttywflush(tp) will hang if there are characters in
710 * the outq.
711 */
712 ndflush(&tp->t_outq, tp->t_outq.c_cc);
713 break;
714
715 case TIOCSIG:
716 if (*(unsigned int *)data >= NSIG ||
717 *(unsigned int *)data == 0)
718 return(EINVAL);
719 if ((tp->t_lflag&NOFLSH) == 0)
720 ttyflush(tp, FREAD|FWRITE);
721 if (tp->t_pgrp != NULL) {
722 PGRP_LOCK(tp->t_pgrp);
723 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
724 PGRP_UNLOCK(tp->t_pgrp);
725 }
726 if ((*(unsigned int *)data == SIGINFO) &&
727 ((tp->t_lflag&NOKERNINFO) == 0))
728 ttyinfo(tp);
729 return(0);
730 }
731 }
732 if (cmd == TIOCEXT) {
733 /*
734 * When the EXTPROC bit is being toggled, we need
735 * to send an TIOCPKT_IOCTL if the packet driver
736 * is turned on.
737 */
738 if (*(int *)data) {
739 if (pti->pt_flags & PF_PKT) {
740 pti->pt_send |= TIOCPKT_IOCTL;
741 ptcwakeup(tp, FREAD);
742 }
743 tp->t_lflag |= EXTPROC;
744 } else {
745 if ((tp->t_lflag & EXTPROC) &&
746 (pti->pt_flags & PF_PKT)) {
747 pti->pt_send |= TIOCPKT_IOCTL;
748 ptcwakeup(tp, FREAD);
749 }
750 tp->t_lflag &= ~EXTPROC;
751 }
752 return(0);
753 }
754 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, td);
755 if (error == ENOIOCTL)
756 error = ttioctl(tp, cmd, data, flag);
757 if (error == ENOIOCTL) {
758 if (pti->pt_flags & PF_UCNTL &&
759 (cmd & ~0xff) == UIOCCMD(0)) {
760 if (cmd & 0xff) {
761 pti->pt_ucntl = (u_char)cmd;
762 ptcwakeup(tp, FREAD);
763 }
764 return (0);
765 }
766 error = ENOTTY;
767 }
768 /*
769 * If external processing and packet mode send ioctl packet.
770 */
771 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
772 switch(cmd) {
773 case TIOCSETA:
774 case TIOCSETAW:
775 case TIOCSETAF:
776#ifdef COMPAT_43
777 case TIOCSETP:
778 case TIOCSETN:
779#endif
780#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
781 case TIOCSETC:
782 case TIOCSLTC:
783 case TIOCLBIS:
784 case TIOCLBIC:
785 case TIOCLSET:
786#endif
787 pti->pt_send |= TIOCPKT_IOCTL;
788 ptcwakeup(tp, FREAD);
789 default:
790 break;
791 }
792 }
793 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
794 && CCEQ(cc[VSTART], CTRL('q'));
795 if (pti->pt_flags & PF_NOSTOP) {
796 if (stop) {
797 pti->pt_send &= ~TIOCPKT_NOSTOP;
798 pti->pt_send |= TIOCPKT_DOSTOP;
799 pti->pt_flags &= ~PF_NOSTOP;
800 ptcwakeup(tp, FREAD);
801 }
802 } else {
803 if (!stop) {
804 pti->pt_send &= ~TIOCPKT_DOSTOP;
805 pti->pt_send |= TIOCPKT_NOSTOP;
806 pti->pt_flags |= PF_NOSTOP;
807 ptcwakeup(tp, FREAD);
808 }
809 }
810 return (error);
811}
812
813
814static void ptc_drvinit(void *unused);
815
816static void pty_clone(void *arg, char *name, int namelen, dev_t *dev);
817
818static void
819pty_clone(arg, name, namelen, dev)
820 void *arg;
821 char *name;
822 int namelen;
823 dev_t *dev;
824{
825 int u;
826
827 if (*dev != NODEV)
828 return;
829 if (bcmp(name, "pty", 3) != 0)
830 return;
831 if (name[5] != '\0')
832 return;
833 switch (name[3]) {
834 case 'p': u = 0; break;
835 case 'q': u = 32; break;
836 case 'r': u = 64; break;
837 case 's': u = 96; break;
838 case 'P': u = 128; break;
839 case 'Q': u = 160; break;
840 case 'R': u = 192; break;
841 case 'S': u = 224; break;
842 default: return;
843 }
844 if (name[4] >= '0' && name[4] <= '9')
845 u += name[4] - '0';
846 else if (name[4] >= 'a' && name[4] <= 'v')
847 u += name[4] - 'a' + 10;
848 else
849 return;
850 *dev = make_dev(&ptc_cdevsw, u,
851 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
852 (*dev)->si_flags |= SI_CHEAPCLONE;
853 return;
854}
855
856static void
857ptc_drvinit(unused)
858 void *unused;
859{
860
861 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
862}
863
864SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)