]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/tty_pty.c
xnu-201.19.tar.gz
[apple/xnu.git] / bsd / kern / tty_pty.c
CommitLineData
1c79356b
A
1/*
2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
11 *
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
18 * under the License.
19 *
20 * @APPLE_LICENSE_HEADER_END@
21 */
22/* Copyright (c) 1997 Apple Computer, Inc. All Rights Reserved */
23/*
24 * Copyright (c) 1982, 1986, 1989, 1993
25 * The Regents of the University of California. All rights reserved.
26 *
27 * Redistribution and use in source and binary forms, with or without
28 * modification, are permitted provided that the following conditions
29 * are met:
30 * 1. Redistributions of source code must retain the above copyright
31 * notice, this list of conditions and the following disclaimer.
32 * 2. Redistributions in binary form must reproduce the above copyright
33 * notice, this list of conditions and the following disclaimer in the
34 * documentation and/or other materials provided with the distribution.
35 * 3. All advertising materials mentioning features or use of this software
36 * must display the following acknowledgement:
37 * This product includes software developed by the University of
38 * California, Berkeley and its contributors.
39 * 4. Neither the name of the University nor the names of its contributors
40 * may be used to endorse or promote products derived from this software
41 * without specific prior written permission.
42 *
43 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
44 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
45 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
46 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
47 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
48 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
49 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
50 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
51 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
52 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * SUCH DAMAGE.
54 *
55 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
56 */
57
58/*
59 * Pseudo-teletype Driver
60 * (Actually two drivers, requiring two entries in 'cdevsw')
61 */
62#include "pty.h" /* XXX */
63
64#include <sys/param.h>
65#include <sys/systm.h>
66#include <sys/ioctl.h>
67#include <sys/proc.h>
68#include <sys/tty.h>
69#include <sys/conf.h>
70#include <sys/file.h>
71#include <sys/uio.h>
72#include <sys/kernel.h>
73#include <sys/vnode.h>
74#include <sys/signalvar.h>
75
76#ifndef NeXT
77
78#define FREE_BSDSTATIC static
79#else
80#include <machine/spl.h>
81
82#define FREE_BSDSTATIC __private_extern__
83#define d_open_t open_close_fcn_t
84#define d_close_t open_close_fcn_t
85#define d_devtotty_t struct tty **
86#define d_ioctl_t ioctl_fcn_t
87#define d_read_t read_write_fcn_t
88#define d_write_t read_write_fcn_t
89#define d_select_t select_fcn_t
90typedef void d_stop_t __P((struct tty *tp, int rw));
91#endif /* NeXT */
92
93#ifdef notyet
94static void ptyattach __P((int n));
95#endif
96static void ptsstart __P((struct tty *tp));
97static void ptcwakeup __P((struct tty *tp, int flag));
98
99FREE_BSDSTATIC d_open_t ptsopen;
100FREE_BSDSTATIC d_close_t ptsclose;
101FREE_BSDSTATIC d_read_t ptsread;
102FREE_BSDSTATIC d_write_t ptswrite;
103FREE_BSDSTATIC d_ioctl_t ptyioctl;
104FREE_BSDSTATIC d_stop_t ptsstop;
105FREE_BSDSTATIC d_devtotty_t ptydevtotty;
106FREE_BSDSTATIC d_open_t ptcopen;
107FREE_BSDSTATIC d_close_t ptcclose;
108FREE_BSDSTATIC d_read_t ptcread;
109FREE_BSDSTATIC d_write_t ptcwrite;
110FREE_BSDSTATIC d_select_t ptcselect;
111
112#ifndef NeXT
113#define CDEV_MAJOR_S 5
114#define CDEV_MAJOR_C 6
115static struct cdevsw pts_cdevsw =
116 { ptsopen, ptsclose, ptsread, ptswrite, /*5*/
117 ptyioctl, ptsstop, nullreset, ptydevtotty,/* ttyp */
118 ttselect, nommap, NULL, "pts", NULL, -1 };
119
120static struct cdevsw ptc_cdevsw =
121 { ptcopen, ptcclose, ptcread, ptcwrite, /*6*/
122 ptyioctl, nullstop, nullreset, ptydevtotty,/* ptyp */
123 ptcselect, nommap, NULL, "ptc", NULL, -1 };
124#endif /* !NeXT */
125
126
127#if NPTY == 1
128#undef NPTY
129#define NPTY 32 /* crude XXX */
130#warning You have only one pty defined, redefining to 32.
131#endif
132
133#ifndef NeXT
134#ifdef DEVFS
135#define MAXUNITS (8 * 32)
136static void *devfs_token_pts[MAXUNITS];
137static void *devfs_token_ptc[MAXUNITS];
138static const char jnames[] = "pqrsPQRS";
139#if NPTY > MAXUNITS
140#undef NPTY
141#define NPTY MAXUNITS
142#warning Can't have more than 256 pty's with DEVFS defined.
143#endif /* NPTY > MAXUNITS */
144#endif /* DEVFS */
145#endif /* !NeXT */
146
147#define BUFSIZ 100 /* Chunk size iomoved to/from user */
148
149/*
150 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
151 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
152 */
153#ifndef NeXT
154FREE_BSDSTATIC struct tty pt_tty[NPTY]; /* XXX */
155#else /* NeXT */
156/* NeXT All references to have been changed to indirections in the file */
157FREE_BSDSTATIC struct tty *pt_tty[NPTY] = { NULL };
158#endif /* ! NeXT */
159
160static struct pt_ioctl {
161 int pt_flags;
162 struct selinfo pt_selr, pt_selw;
163 u_char pt_send;
164 u_char pt_ucntl;
165} pt_ioctl[NPTY]; /* XXX */
166static int npty = NPTY; /* for pstat -t */
167
168#define PF_PKT 0x08 /* packet mode */
169#define PF_STOPPED 0x10 /* user told stopped */
170#define PF_REMOTE 0x20 /* remote and flow controlled input */
171#define PF_NOSTOP 0x40
172#define PF_UCNTL 0x80 /* user control mode */
173
174#ifdef notyet
175/*
176 * Establish n (or default if n is 1) ptys in the system.
177 *
178 * XXX cdevsw & pstat require the array `pty[]' to be an array
179 */
180FREEBSD_STATIC void
181ptyattach(n)
182 int n;
183{
184 char *mem;
185 register u_long ntb;
186#define DEFAULT_NPTY 32
187
188 /* maybe should allow 0 => none? */
189 if (n <= 1)
190 n = DEFAULT_NPTY;
191 ntb = n * sizeof(struct tty);
192#ifndef NeXT
193 mem = malloc(ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl),
194 M_DEVBUF, M_WAITOK);
195#else
196 MALLOC(mem, char *, ntb + ALIGNBYTES + n * sizeof(struct pt_ioctl),
197 M_DEVBUF, M_WAITOK);
198#endif /* !NeXT */
199 pt_tty = (struct tty *)mem;
200 mem = (char *)ALIGN(mem + ntb);
201 pt_ioctl = (struct pt_ioctl *)mem;
202 npty = n;
203}
204#endif
205
206#ifndef DEVFS
207int pty_init()
208{
209 return 0;
210}
211#else
212#include <miscfs/devfs/devfs.h>
213#define START_CHAR 'p'
214#define HEX_BASE 16
215int pty_init(int n_ptys)
216{
217 int i;
218 int j;
219
220 /* create the pseudo tty device nodes */
221 for (j = 0; j < 10; j++) {
222 for (i = 0; i < HEX_BASE; i++) {
223 int m = j * HEX_BASE + i;
224 if (m == n_ptys)
225 goto done;
226 (void)devfs_make_node(makedev(4, m),
227 DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
228 "tty%c%x", j + START_CHAR, i);
229 (void)devfs_make_node(makedev(5, m),
230 DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
231 "pty%c%x", j + START_CHAR, i);
232 }
233 }
234 done:
235 return (0);
236}
237#endif DEVFS
238
239/*ARGSUSED*/
240FREE_BSDSTATIC int
241ptsopen(dev, flag, devtype, p)
242 dev_t dev;
243 int flag, devtype;
244 struct proc *p;
245{
246 register struct tty *tp;
247 int error;
248
249#ifndef NeXT
250 tp = &pt_tty[minor(dev)];
251#else
252 /*
253 * You will see this sourt of code coming up in diffs later both
254 * the ttymalloc and the tp indirection.
255 */
256 if (minor(dev) >= npty)
257 return (ENXIO);
258 if (!pt_tty[minor(dev)]) {
259 tp = pt_tty[minor(dev)] = ttymalloc();
260 } else
261 tp = pt_tty[minor(dev)];
262#endif
263 if ((tp->t_state & TS_ISOPEN) == 0) {
264 ttychars(tp); /* Set up default chars */
265 tp->t_iflag = TTYDEF_IFLAG;
266 tp->t_oflag = TTYDEF_OFLAG;
267 tp->t_lflag = TTYDEF_LFLAG;
268 tp->t_cflag = TTYDEF_CFLAG;
269 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
270 ttsetwater(tp); /* would be done in xxparam() */
271 } else if (tp->t_state&TS_XCLUDE && p->p_ucred->cr_uid != 0)
272 return (EBUSY);
273 if (tp->t_oproc) /* Ctrlr still around. */
274 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
275 while ((tp->t_state & TS_CARR_ON) == 0) {
276 if (flag&FNONBLOCK)
277 break;
278 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
279 "ptsopn", 0);
280 if (error)
281 return (error);
282 }
283 error = (*linesw[tp->t_line].l_open)(dev, tp);
284 if (error == 0)
285 ptcwakeup(tp, FREAD|FWRITE);
286 return (error);
287}
288
289FREE_BSDSTATIC int
290ptsclose(dev, flag, mode, p)
291 dev_t dev;
292 int flag, mode;
293 struct proc *p;
294{
295 register struct tty *tp;
296 int err;
297
298 tp = pt_tty[minor(dev)];
299 err = (*linesw[tp->t_line].l_close)(tp, flag);
300 ptsstop(tp, FREAD|FWRITE);
301 (void) ttyclose(tp);
302 return (err);
303}
304
305FREE_BSDSTATIC int
306ptsread(dev, uio, flag)
307 dev_t dev;
308 struct uio *uio;
309 int flag;
310{
311#ifndef NeXT
312 struct proc *p = curproc;
313#else
314 struct proc *p = current_proc();
315#endif /* NeXT */
316 register struct tty *tp = pt_tty[minor(dev)];
317 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
318 int error = 0;
319
320again:
321 if (pti->pt_flags & PF_REMOTE) {
322 while (isbackground(p, tp)) {
323 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
324 (p->p_sigmask & sigmask(SIGTTIN)) ||
325 p->p_pgrp->pg_jobc == 0 ||
326 p->p_flag & P_PPWAIT)
327 return (EIO);
328 pgsignal(p->p_pgrp, SIGTTIN, 1);
329 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH | PTTYBLOCK, "ptsbg",
330 0);
331 if (error)
332 return (error);
333 }
334 if (tp->t_canq.c_cc == 0) {
335 if (flag & IO_NDELAY)
336 return (EWOULDBLOCK);
337 error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
338 "ptsin", 0);
339 if (error)
340 return (error);
341 goto again;
342 }
343 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
344 if (ureadc(getc(&tp->t_canq), uio) < 0) {
345 error = EFAULT;
346 break;
347 }
348 if (tp->t_canq.c_cc == 1)
349 (void) getc(&tp->t_canq);
350 if (tp->t_canq.c_cc)
351 return (error);
352 } else
353 if (tp->t_oproc)
354 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
355 ptcwakeup(tp, FWRITE);
356 return (error);
357}
358
359/*
360 * Write to pseudo-tty.
361 * Wakeups of controlling tty will happen
362 * indirectly, when tty driver calls ptsstart.
363 */
364FREE_BSDSTATIC int
365ptswrite(dev, uio, flag)
366 dev_t dev;
367 struct uio *uio;
368 int flag;
369{
370 register struct tty *tp;
371
372 tp = pt_tty[minor(dev)];
373 if (tp->t_oproc == 0)
374 return (EIO);
375 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
376}
377
378/*
379 * Start output on pseudo-tty.
380 * Wake up process selecting or sleeping for input from controlling tty.
381 */
382static void
383ptsstart(tp)
384 struct tty *tp;
385{
386 register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
387
388 if (tp->t_state & TS_TTSTOP)
389 return;
390 if (pti->pt_flags & PF_STOPPED) {
391 pti->pt_flags &= ~PF_STOPPED;
392 pti->pt_send = TIOCPKT_START;
393 }
394 ptcwakeup(tp, FREAD);
395}
396
397static void
398ptcwakeup(tp, flag)
399 struct tty *tp;
400 int flag;
401{
402 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
403
404 if (flag & FREAD) {
405 selwakeup(&pti->pt_selr);
406 wakeup(TSA_PTC_READ(tp));
407 }
408 if (flag & FWRITE) {
409 selwakeup(&pti->pt_selw);
410 wakeup(TSA_PTC_WRITE(tp));
411 }
412}
413
414FREE_BSDSTATIC int
415ptcopen(dev, flag, devtype, p)
416 dev_t dev;
417 int flag, devtype;
418 struct proc *p;
419{
420 register struct tty *tp;
421 struct pt_ioctl *pti;
422
423 if (minor(dev) >= npty)
424 return (ENXIO);
425 if(!pt_tty[minor(dev)]) {
426 tp = pt_tty[minor(dev)] = ttymalloc();
427 } else
428 tp = pt_tty[minor(dev)];
429 if (tp->t_oproc)
430 return (EIO);
431 tp->t_oproc = ptsstart;
432#ifdef sun4c
433 tp->t_stop = ptsstop;
434#endif
435 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
436 tp->t_lflag &= ~EXTPROC;
437 pti = &pt_ioctl[minor(dev)];
438 pti->pt_flags = 0;
439 pti->pt_send = 0;
440 pti->pt_ucntl = 0;
441 return (0);
442}
443
444FREE_BSDSTATIC int
445ptcclose(dev, flags, fmt, p)
446 dev_t dev;
447 int flags;
448 int fmt;
449 struct proc *p;
450{
451 register struct tty *tp;
452
453 tp = pt_tty[minor(dev)];
454 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
455
456 /*
457 * XXX MDMBUF makes no sense for ptys but would inhibit the above
458 * l_modem(). CLOCAL makes sense but isn't supported. Special
459 * l_modem()s that ignore carrier drop make no sense for ptys but
460 * may be in use because other parts of the line discipline make
461 * sense for ptys. Recover by doing everything that a normal
462 * ttymodem() would have done except for sending a SIGHUP.
463 */
464 if (tp->t_state & TS_ISOPEN) {
465 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
466 tp->t_state |= TS_ZOMBIE;
467 ttyflush(tp, FREAD | FWRITE);
468 }
469
470 tp->t_oproc = 0; /* mark closed */
471 return (0);
472}
473
474FREE_BSDSTATIC int
475ptcread(dev, uio, flag)
476 dev_t dev;
477 struct uio *uio;
478 int flag;
479{
480 register struct tty *tp = pt_tty[minor(dev)];
481 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
482 char buf[BUFSIZ];
483 int error = 0, cc;
484
485 /*
486 * We want to block until the slave
487 * is open, and there's something to read;
488 * but if we lost the slave or we're NBIO,
489 * then return the appropriate error instead.
490 */
491 for (;;) {
492 if (tp->t_state&TS_ISOPEN) {
493 if (pti->pt_flags&PF_PKT && pti->pt_send) {
494 error = ureadc((int)pti->pt_send, uio);
495 if (error)
496 return (error);
497 if (pti->pt_send & TIOCPKT_IOCTL) {
498 cc = min(uio->uio_resid,
499 sizeof(tp->t_termios));
500 uiomove((caddr_t)&tp->t_termios, cc,
501 uio);
502 }
503 pti->pt_send = 0;
504 return (0);
505 }
506 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
507 error = ureadc((int)pti->pt_ucntl, uio);
508 if (error)
509 return (error);
510 pti->pt_ucntl = 0;
511 return (0);
512 }
513 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
514 break;
515 }
516 if ((tp->t_state & TS_CONNECTED) == 0)
517 return (0); /* EOF */
518 if (flag & IO_NDELAY)
519 return (EWOULDBLOCK);
520 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
521 if (error)
522 return (error);
523 }
524 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
525 error = ureadc(0, uio);
526 while (uio->uio_resid > 0 && error == 0) {
527 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
528 if (cc <= 0)
529 break;
530 error = uiomove(buf, cc, uio);
531 }
532 ttwwakeup(tp);
533 return (error);
534}
535
536FREE_BSDSTATIC void
537ptsstop(tp, flush)
538 register struct tty *tp;
539 int flush;
540{
541 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
542 int flag;
543
544 /* note: FLUSHREAD and FLUSHWRITE already ok */
545 if (flush == 0) {
546 flush = TIOCPKT_STOP;
547 pti->pt_flags |= PF_STOPPED;
548 } else
549 pti->pt_flags &= ~PF_STOPPED;
550 pti->pt_send |= flush;
551 /* change of perspective */
552 flag = 0;
553 if (flush & FREAD)
554 flag |= FWRITE;
555 if (flush & FWRITE)
556 flag |= FREAD;
557 ptcwakeup(tp, flag);
558}
559
560FREE_BSDSTATIC int
0b4e3aa0 561ptcselect(dev, rw, wql, p)
1c79356b
A
562 dev_t dev;
563 int rw;
0b4e3aa0 564 void * wql;
1c79356b
A
565 struct proc *p;
566{
567 register struct tty *tp = pt_tty[minor(dev)];
568 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
569 int s;
570
571 if ((tp->t_state & TS_CONNECTED) == 0)
572 return (1);
573 switch (rw) {
574
575 case FREAD:
576 /*
577 * Need to block timeouts (ttrstart).
578 */
579 s = spltty();
580 if ((tp->t_state&TS_ISOPEN) &&
581 tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
582 splx(s);
583 return (1);
584 }
585 splx(s);
586 /* FALLTHROUGH */
587
588 case 0: /* exceptional */
589 if ((tp->t_state&TS_ISOPEN) &&
590 ((pti->pt_flags&PF_PKT && pti->pt_send) ||
591 (pti->pt_flags&PF_UCNTL && pti->pt_ucntl)))
592 return (1);
0b4e3aa0 593 selrecord(p, &pti->pt_selr, wql);
1c79356b
A
594 break;
595
596
597 case FWRITE:
598 if (tp->t_state&TS_ISOPEN) {
599 if (pti->pt_flags & PF_REMOTE) {
600 if (tp->t_canq.c_cc == 0)
601 return (1);
602 } else {
603 if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
604 return (1);
605 if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
606 return (1);
607 }
608 }
0b4e3aa0 609 selrecord(p, &pti->pt_selw, wql);
1c79356b
A
610 break;
611
612 }
613 return (0);
614}
615
616FREE_BSDSTATIC int
617ptcwrite(dev, uio, flag)
618 dev_t dev;
619 register struct uio *uio;
620 int flag;
621{
622 register struct tty *tp = pt_tty[minor(dev)];
623 register u_char *cp = NULL;
624 register int cc = 0;
625 u_char locbuf[BUFSIZ];
626 int cnt = 0;
627 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
628 int error = 0;
629
630again:
631 if ((tp->t_state&TS_ISOPEN) == 0)
632 goto block;
633 if (pti->pt_flags & PF_REMOTE) {
634 if (tp->t_canq.c_cc)
635 goto block;
636 while ((uio->uio_resid > 0 || cc > 0) &&
637 tp->t_canq.c_cc < TTYHOG - 1) {
638 if (cc == 0) {
639 cc = min(uio->uio_resid, BUFSIZ);
640 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
641 cp = locbuf;
642 error = uiomove((caddr_t)cp, cc, uio);
643 if (error)
644 return (error);
645 /* check again for safety */
646 if ((tp->t_state & TS_ISOPEN) == 0) {
647 /* adjust as usual */
648 uio->uio_resid += cc;
649 return (EIO);
650 }
651 }
652 if (cc > 0) {
653 cc = b_to_q((char *)cp, cc, &tp->t_canq);
654 /*
655 * XXX we don't guarantee that the canq size
656 * is >= TTYHOG, so the above b_to_q() may
657 * leave some bytes uncopied. However, space
658 * is guaranteed for the null terminator if
659 * we don't fail here since (TTYHOG - 1) is
660 * not a multiple of CBSIZE.
661 */
662 if (cc > 0)
663 break;
664 }
665 }
666 /* adjust for data copied in but not written */
667 uio->uio_resid += cc;
668 (void) putc(0, &tp->t_canq);
669 ttwakeup(tp);
670 wakeup(TSA_PTS_READ(tp));
671 return (0);
672 }
673 while (uio->uio_resid > 0 || cc > 0) {
674 if (cc == 0) {
675 cc = min(uio->uio_resid, BUFSIZ);
676 cp = locbuf;
677 error = uiomove((caddr_t)cp, cc, uio);
678 if (error)
679 return (error);
680 /* check again for safety */
681 if ((tp->t_state & TS_ISOPEN) == 0) {
682 /* adjust for data copied in but not written */
683 uio->uio_resid += cc;
684 return (EIO);
685 }
686 }
687 while (cc > 0) {
688 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
689 (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
690 wakeup(TSA_HUP_OR_INPUT(tp));
691 goto block;
692 }
693 (*linesw[tp->t_line].l_rint)(*cp++, tp);
694 cnt++;
695 cc--;
696 }
697 cc = 0;
698 }
699 return (0);
700block:
701 /*
702 * Come here to wait for slave to open, for space
703 * in outq, or space in rawq, or an empty canq.
704 */
705 if ((tp->t_state & TS_CONNECTED) == 0) {
706 /* adjust for data copied in but not written */
707 uio->uio_resid += cc;
708 return (EIO);
709 }
710 if (flag & IO_NDELAY) {
711 /* adjust for data copied in but not written */
712 uio->uio_resid += cc;
713 if (cnt == 0)
714 return (EWOULDBLOCK);
715 return (0);
716 }
717 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
718 if (error) {
719 /* adjust for data copied in but not written */
720 uio->uio_resid += cc;
721 return (error);
722 }
723 goto again;
724}
725
726#ifndef NeXT
727/* XXX we eventually want to go to this model,
728 * but premier can't change the cdevsw */
729static struct tty *
730ptydevtotty(dev)
731 dev_t dev;
732{
733 if (minor(dev) >= npty)
734 return (NULL);
735
736 return &pt_tty[minor(dev)];
737}
738#endif /* !NeXT */
739
740/*ARGSUSED*/
741FREE_BSDSTATIC int
742#ifndef NeXT
743ptyioctl(dev, cmd, data, flag)
744 dev_t dev;
745 int cmd;
746 caddr_t data;
747 int flag;
748#else
749ptyioctl(dev, cmd, data, flag, p)
750 dev_t dev;
751 u_long cmd;
752 caddr_t data;
753 int flag;
754 struct proc *p;
755#endif
756{
757 register struct tty *tp = pt_tty[minor(dev)];
758 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
759 register u_char *cc = tp->t_cc;
760 int stop, error;
761
762 /*
763 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
764 * ttywflush(tp) will hang if there are characters in the outq.
765 */
766 if (cmd == TIOCEXT) {
767 /*
768 * When the EXTPROC bit is being toggled, we need
769 * to send an TIOCPKT_IOCTL if the packet driver
770 * is turned on.
771 */
772 if (*(int *)data) {
773 if (pti->pt_flags & PF_PKT) {
774 pti->pt_send |= TIOCPKT_IOCTL;
775 ptcwakeup(tp, FREAD);
776 }
777 tp->t_lflag |= EXTPROC;
778 } else {
779 if ((tp->t_lflag & EXTPROC) &&
780 (pti->pt_flags & PF_PKT)) {
781 pti->pt_send |= TIOCPKT_IOCTL;
782 ptcwakeup(tp, FREAD);
783 }
784 tp->t_lflag &= ~EXTPROC;
785 }
786 return(0);
787 } else
788#ifndef NeXT
789 if (cdevsw[major(dev)]->d_open == ptcopen)
790#else
791 if (cdevsw[major(dev)].d_open == ptcopen)
792#endif
793 switch (cmd) {
794
795 case TIOCGPGRP:
796 /*
797 * We aviod calling ttioctl on the controller since,
798 * in that case, tp must be the controlling terminal.
799 */
800 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
801 return (0);
802
803 case TIOCPKT:
804 if (*(int *)data) {
805 if (pti->pt_flags & PF_UCNTL)
806 return (EINVAL);
807 pti->pt_flags |= PF_PKT;
808 } else
809 pti->pt_flags &= ~PF_PKT;
810 return (0);
811
812 case TIOCUCNTL:
813 if (*(int *)data) {
814 if (pti->pt_flags & PF_PKT)
815 return (EINVAL);
816 pti->pt_flags |= PF_UCNTL;
817 } else
818 pti->pt_flags &= ~PF_UCNTL;
819 return (0);
820
821 case TIOCREMOTE:
822 if (*(int *)data)
823 pti->pt_flags |= PF_REMOTE;
824 else
825 pti->pt_flags &= ~PF_REMOTE;
826 ttyflush(tp, FREAD|FWRITE);
827 return (0);
828
829#ifdef COMPAT_43
830 case TIOCSETP:
831 case TIOCSETN:
832#endif
833 case TIOCSETD:
834 case TIOCSETA:
835 case TIOCSETAW:
836 case TIOCSETAF:
837 ndflush(&tp->t_outq, tp->t_outq.c_cc);
838 break;
839
840 case TIOCSIG:
841 if (*(unsigned int *)data >= NSIG ||
842 *(unsigned int *)data == 0)
843 return(EINVAL);
844 if ((tp->t_lflag&NOFLSH) == 0)
845 ttyflush(tp, FREAD|FWRITE);
846 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
847 if ((*(unsigned int *)data == SIGINFO) &&
848 ((tp->t_lflag&NOKERNINFO) == 0))
849 ttyinfo(tp);
850 return(0);
851 }
852 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
853 if (error < 0)
854 error = ttioctl(tp, cmd, data, flag, p);
855 if (error < 0) {
856 if (pti->pt_flags & PF_UCNTL &&
857 (cmd & ~0xff) == UIOCCMD(0)) {
858 if (cmd & 0xff) {
859 pti->pt_ucntl = (u_char)cmd;
860 ptcwakeup(tp, FREAD);
861 }
862 return (0);
863 }
864 error = ENOTTY;
865 }
866 /*
867 * If external processing and packet mode send ioctl packet.
868 */
869 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
870 switch(cmd) {
871 case TIOCSETA:
872 case TIOCSETAW:
873 case TIOCSETAF:
874#ifdef COMPAT_43
875 case TIOCSETP:
876 case TIOCSETN:
877#endif
878#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
879 case TIOCSETC:
880 case TIOCSLTC:
881 case TIOCLBIS:
882 case TIOCLBIC:
883 case TIOCLSET:
884#endif
885 pti->pt_send |= TIOCPKT_IOCTL;
886 ptcwakeup(tp, FREAD);
887 default:
888 break;
889 }
890 }
891 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
892 && CCEQ(cc[VSTART], CTRL('q'));
893 if (pti->pt_flags & PF_NOSTOP) {
894 if (stop) {
895 pti->pt_send &= ~TIOCPKT_NOSTOP;
896 pti->pt_send |= TIOCPKT_DOSTOP;
897 pti->pt_flags &= ~PF_NOSTOP;
898 ptcwakeup(tp, FREAD);
899 }
900 } else {
901 if (!stop) {
902 pti->pt_send &= ~TIOCPKT_DOSTOP;
903 pti->pt_send |= TIOCPKT_NOSTOP;
904 pti->pt_flags |= PF_NOSTOP;
905 ptcwakeup(tp, FREAD);
906 }
907 }
908 return (error);
909}
910
911#ifndef NeXT
912static ptc_devsw_installed = 0;
913
914static void
915ptc_drvinit(void *unused)
916{
917#ifdef DEVFS
918 int i,j,k;
919#endif
920 dev_t dev;
921
922 if( ! ptc_devsw_installed ) {
923 dev = makedev(CDEV_MAJOR_S, 0);
924 cdevsw_add(&dev, &pts_cdevsw, NULL);
925 dev = makedev(CDEV_MAJOR_C, 0);
926 cdevsw_add(&dev, &ptc_cdevsw, NULL);
927 ptc_devsw_installed = 1;
928#ifdef DEVFS
929 for ( i = 0 ; i<NPTY ; i++ ) {
930 j = i / 32;
931 k = i % 32;
932 devfs_token_pts[i] =
933 devfs_add_devswf(&pts_cdevsw,i,
934 DV_CHR,0,0,0666,
935 "tty%c%n",jnames[j],k);
936 devfs_token_ptc[i] =
937 devfs_add_devswf(&ptc_cdevsw,i,
938 DV_CHR,0,0,0666,
939 "pty%c%n",jnames[j],k);
940 }
941#endif
942 }
943}
944
945SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
946#endif /* !NeXT */