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