]> git.saurik.com Git - apple/xnu.git/blob - bsd/kern/tty_pty.c
xnu-517.11.1.tar.gz
[apple/xnu.git] / bsd / kern / tty_pty.c
1 /*
2 * Copyright (c) 2000-2002 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/user.h>
75 #include <sys/signalvar.h>
76
77 #ifndef NeXT
78
79 #define FREE_BSDSTATIC static
80 #else
81 #include <machine/spl.h>
82
83 #define FREE_BSDSTATIC __private_extern__
84 #define d_devtotty_t struct tty **
85
86 #ifdef d_stop_t
87 #undef d_stop_t
88 #endif
89 typedef void d_stop_t __P((struct tty *tp, int rw));
90
91 #endif /* NeXT */
92
93 #ifdef notyet
94 static void ptyattach __P((int n));
95 #endif
96 static void ptsstart __P((struct tty *tp));
97 static void ptcwakeup __P((struct tty *tp, int flag));
98
99 FREE_BSDSTATIC d_open_t ptsopen;
100 FREE_BSDSTATIC d_close_t ptsclose;
101 FREE_BSDSTATIC d_read_t ptsread;
102 FREE_BSDSTATIC d_write_t ptswrite;
103 FREE_BSDSTATIC d_ioctl_t ptyioctl;
104 FREE_BSDSTATIC d_stop_t ptsstop;
105 FREE_BSDSTATIC d_devtotty_t ptydevtotty;
106 FREE_BSDSTATIC d_open_t ptcopen;
107 FREE_BSDSTATIC d_close_t ptcclose;
108 FREE_BSDSTATIC d_read_t ptcread;
109 FREE_BSDSTATIC d_write_t ptcwrite;
110 FREE_BSDSTATIC d_select_t ptcselect;
111
112 #ifndef NeXT
113 #define CDEV_MAJOR_S 5
114 #define CDEV_MAJOR_C 6
115 static struct cdevsw pts_cdevsw =
116 { ptsopen, ptsclose, ptsread, ptswrite, /*5*/
117 ptyioctl, ptsstop, nullreset, ptydevtotty,/* ttyp */
118 ttselect, nommap, NULL, "pts", NULL, -1 };
119
120 static 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)
136 static void *devfs_token_pts[MAXUNITS];
137 static void *devfs_token_ptc[MAXUNITS];
138 static 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
154 FREE_BSDSTATIC struct tty pt_tty[NPTY]; /* XXX */
155 #else /* NeXT */
156 /* NeXT All references to have been changed to indirections in the file */
157 FREE_BSDSTATIC struct tty *pt_tty[NPTY] = { NULL };
158 #endif /* ! NeXT */
159
160 static 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 */
166 static 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 */
180 FREEBSD_STATIC void
181 ptyattach(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
207 int 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
215 int 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*/
240 FREE_BSDSTATIC int
241 ptsopen(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
289 FREE_BSDSTATIC int
290 ptsclose(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
305 FREE_BSDSTATIC int
306 ptsread(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 struct uthread *ut;
320
321 ut = (struct uthread *)get_bsdthread_info(current_act());
322 again:
323 if (pti->pt_flags & PF_REMOTE) {
324 while (isbackground(p, tp)) {
325 if ((p->p_sigignore & sigmask(SIGTTIN)) ||
326 (ut->uu_sigmask & sigmask(SIGTTIN)) ||
327 p->p_pgrp->pg_jobc == 0 ||
328 p->p_flag & P_PPWAIT)
329 return (EIO);
330 pgsignal(p->p_pgrp, SIGTTIN, 1);
331 error = ttysleep(tp, &lbolt, TTIPRI | PCATCH | PTTYBLOCK, "ptsbg",
332 0);
333 if (error)
334 return (error);
335 }
336 if (tp->t_canq.c_cc == 0) {
337 if (flag & IO_NDELAY)
338 return (EWOULDBLOCK);
339 error = ttysleep(tp, TSA_PTS_READ(tp), TTIPRI | PCATCH,
340 "ptsin", 0);
341 if (error)
342 return (error);
343 goto again;
344 }
345 while (tp->t_canq.c_cc > 1 && uio->uio_resid > 0)
346 if (ureadc(getc(&tp->t_canq), uio) < 0) {
347 error = EFAULT;
348 break;
349 }
350 if (tp->t_canq.c_cc == 1)
351 (void) getc(&tp->t_canq);
352 if (tp->t_canq.c_cc)
353 return (error);
354 } else
355 if (tp->t_oproc)
356 error = (*linesw[tp->t_line].l_read)(tp, uio, flag);
357 ptcwakeup(tp, FWRITE);
358 return (error);
359 }
360
361 /*
362 * Write to pseudo-tty.
363 * Wakeups of controlling tty will happen
364 * indirectly, when tty driver calls ptsstart.
365 */
366 FREE_BSDSTATIC int
367 ptswrite(dev, uio, flag)
368 dev_t dev;
369 struct uio *uio;
370 int flag;
371 {
372 register struct tty *tp;
373
374 tp = pt_tty[minor(dev)];
375 if (tp->t_oproc == 0)
376 return (EIO);
377 return ((*linesw[tp->t_line].l_write)(tp, uio, flag));
378 }
379
380 /*
381 * Start output on pseudo-tty.
382 * Wake up process selecting or sleeping for input from controlling tty.
383 */
384 static void
385 ptsstart(tp)
386 struct tty *tp;
387 {
388 register struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
389
390 if (tp->t_state & TS_TTSTOP)
391 return;
392 if (pti->pt_flags & PF_STOPPED) {
393 pti->pt_flags &= ~PF_STOPPED;
394 pti->pt_send = TIOCPKT_START;
395 }
396 ptcwakeup(tp, FREAD);
397 }
398
399 static void
400 ptcwakeup(tp, flag)
401 struct tty *tp;
402 int flag;
403 {
404 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
405
406 if (flag & FREAD) {
407 selwakeup(&pti->pt_selr);
408 wakeup(TSA_PTC_READ(tp));
409 }
410 if (flag & FWRITE) {
411 selwakeup(&pti->pt_selw);
412 wakeup(TSA_PTC_WRITE(tp));
413 }
414 }
415
416 FREE_BSDSTATIC int
417 ptcopen(dev, flag, devtype, p)
418 dev_t dev;
419 int flag, devtype;
420 struct proc *p;
421 {
422 register struct tty *tp;
423 struct pt_ioctl *pti;
424
425 if (minor(dev) >= npty)
426 return (ENXIO);
427 if(!pt_tty[minor(dev)]) {
428 tp = pt_tty[minor(dev)] = ttymalloc();
429 } else
430 tp = pt_tty[minor(dev)];
431 if (tp->t_oproc)
432 return (EIO);
433 tp->t_oproc = ptsstart;
434 #ifdef sun4c
435 tp->t_stop = ptsstop;
436 #endif
437 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
438 tp->t_lflag &= ~EXTPROC;
439 pti = &pt_ioctl[minor(dev)];
440 pti->pt_flags = 0;
441 pti->pt_send = 0;
442 pti->pt_ucntl = 0;
443 return (0);
444 }
445
446 FREE_BSDSTATIC int
447 ptcclose(dev, flags, fmt, p)
448 dev_t dev;
449 int flags;
450 int fmt;
451 struct proc *p;
452 {
453 register struct tty *tp;
454
455 tp = pt_tty[minor(dev)];
456 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
457
458 /*
459 * XXX MDMBUF makes no sense for ptys but would inhibit the above
460 * l_modem(). CLOCAL makes sense but isn't supported. Special
461 * l_modem()s that ignore carrier drop make no sense for ptys but
462 * may be in use because other parts of the line discipline make
463 * sense for ptys. Recover by doing everything that a normal
464 * ttymodem() would have done except for sending a SIGHUP.
465 */
466 if (tp->t_state & TS_ISOPEN) {
467 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
468 tp->t_state |= TS_ZOMBIE;
469 ttyflush(tp, FREAD | FWRITE);
470 }
471
472 tp->t_oproc = 0; /* mark closed */
473 return (0);
474 }
475
476 FREE_BSDSTATIC int
477 ptcread(dev, uio, flag)
478 dev_t dev;
479 struct uio *uio;
480 int flag;
481 {
482 register struct tty *tp = pt_tty[minor(dev)];
483 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
484 char buf[BUFSIZ];
485 int error = 0, cc;
486
487 /*
488 * We want to block until the slave
489 * is open, and there's something to read;
490 * but if we lost the slave or we're NBIO,
491 * then return the appropriate error instead.
492 */
493 for (;;) {
494 if (tp->t_state&TS_ISOPEN) {
495 if (pti->pt_flags&PF_PKT && pti->pt_send) {
496 error = ureadc((int)pti->pt_send, uio);
497 if (error)
498 return (error);
499 if (pti->pt_send & TIOCPKT_IOCTL) {
500 cc = min(uio->uio_resid,
501 sizeof(tp->t_termios));
502 uiomove((caddr_t)&tp->t_termios, cc,
503 uio);
504 }
505 pti->pt_send = 0;
506 return (0);
507 }
508 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
509 error = ureadc((int)pti->pt_ucntl, uio);
510 if (error)
511 return (error);
512 pti->pt_ucntl = 0;
513 return (0);
514 }
515 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
516 break;
517 }
518 if ((tp->t_state & TS_CONNECTED) == 0)
519 return (0); /* EOF */
520 if (flag & IO_NDELAY)
521 return (EWOULDBLOCK);
522 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
523 if (error)
524 return (error);
525 }
526 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
527 error = ureadc(0, uio);
528 while (uio->uio_resid > 0 && error == 0) {
529 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
530 if (cc <= 0)
531 break;
532 error = uiomove(buf, cc, uio);
533 }
534 ttwwakeup(tp);
535 return (error);
536 }
537
538 FREE_BSDSTATIC void
539 ptsstop(tp, flush)
540 register struct tty *tp;
541 int flush;
542 {
543 struct pt_ioctl *pti = &pt_ioctl[minor(tp->t_dev)];
544 int flag;
545
546 /* note: FLUSHREAD and FLUSHWRITE already ok */
547 if (flush == 0) {
548 flush = TIOCPKT_STOP;
549 pti->pt_flags |= PF_STOPPED;
550 } else
551 pti->pt_flags &= ~PF_STOPPED;
552 pti->pt_send |= flush;
553 /* change of perspective */
554 flag = 0;
555 if (flush & FREAD)
556 flag |= FWRITE;
557 if (flush & FWRITE)
558 flag |= FREAD;
559 ptcwakeup(tp, flag);
560 }
561
562 FREE_BSDSTATIC int
563 ptcselect(dev, rw, wql, p)
564 dev_t dev;
565 int rw;
566 void * wql;
567 struct proc *p;
568 {
569 register struct tty *tp = pt_tty[minor(dev)];
570 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
571 int s;
572
573 if ((tp->t_state & TS_CONNECTED) == 0)
574 return (1);
575 switch (rw) {
576
577 case FREAD:
578 /*
579 * Need to block timeouts (ttrstart).
580 */
581 s = spltty();
582 if ((tp->t_state&TS_ISOPEN) &&
583 tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0) {
584 splx(s);
585 return (1);
586 }
587 splx(s);
588 /* FALLTHROUGH */
589
590 case 0: /* exceptional */
591 if ((tp->t_state&TS_ISOPEN) &&
592 ((pti->pt_flags&PF_PKT && pti->pt_send) ||
593 (pti->pt_flags&PF_UCNTL && pti->pt_ucntl)))
594 return (1);
595 selrecord(p, &pti->pt_selr, wql);
596 break;
597
598
599 case FWRITE:
600 if (tp->t_state&TS_ISOPEN) {
601 if (pti->pt_flags & PF_REMOTE) {
602 if (tp->t_canq.c_cc == 0)
603 return (1);
604 } else {
605 if (tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG-2)
606 return (1);
607 if (tp->t_canq.c_cc == 0 && (tp->t_iflag&ICANON))
608 return (1);
609 }
610 }
611 selrecord(p, &pti->pt_selw, wql);
612 break;
613
614 }
615 return (0);
616 }
617
618 FREE_BSDSTATIC int
619 ptcwrite(dev, uio, flag)
620 dev_t dev;
621 register struct uio *uio;
622 int flag;
623 {
624 register struct tty *tp = pt_tty[minor(dev)];
625 register u_char *cp = NULL;
626 register int cc = 0;
627 u_char locbuf[BUFSIZ];
628 int cnt = 0;
629 struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
630 int error = 0;
631
632 again:
633 if ((tp->t_state&TS_ISOPEN) == 0)
634 goto block;
635 if (pti->pt_flags & PF_REMOTE) {
636 if (tp->t_canq.c_cc)
637 goto block;
638 while ((uio->uio_resid > 0 || cc > 0) &&
639 tp->t_canq.c_cc < TTYHOG - 1) {
640 if (cc == 0) {
641 cc = min(uio->uio_resid, BUFSIZ);
642 cc = min(cc, TTYHOG - 1 - tp->t_canq.c_cc);
643 cp = locbuf;
644 error = uiomove((caddr_t)cp, cc, uio);
645 if (error)
646 return (error);
647 /* check again for safety */
648 if ((tp->t_state & TS_ISOPEN) == 0) {
649 /* adjust as usual */
650 uio->uio_resid += cc;
651 return (EIO);
652 }
653 }
654 if (cc > 0) {
655 cc = b_to_q((char *)cp, cc, &tp->t_canq);
656 /*
657 * XXX we don't guarantee that the canq size
658 * is >= TTYHOG, so the above b_to_q() may
659 * leave some bytes uncopied. However, space
660 * is guaranteed for the null terminator if
661 * we don't fail here since (TTYHOG - 1) is
662 * not a multiple of CBSIZE.
663 */
664 if (cc > 0)
665 break;
666 }
667 }
668 /* adjust for data copied in but not written */
669 uio->uio_resid += cc;
670 (void) putc(0, &tp->t_canq);
671 ttwakeup(tp);
672 wakeup(TSA_PTS_READ(tp));
673 return (0);
674 }
675 while (uio->uio_resid > 0 || cc > 0) {
676 if (cc == 0) {
677 cc = min(uio->uio_resid, BUFSIZ);
678 cp = locbuf;
679 error = uiomove((caddr_t)cp, cc, uio);
680 if (error)
681 return (error);
682 /* check again for safety */
683 if ((tp->t_state & TS_ISOPEN) == 0) {
684 /* adjust for data copied in but not written */
685 uio->uio_resid += cc;
686 return (EIO);
687 }
688 }
689 while (cc > 0) {
690 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
691 (tp->t_canq.c_cc > 0 || !(tp->t_iflag&ICANON))) {
692 wakeup(TSA_HUP_OR_INPUT(tp));
693 goto block;
694 }
695 (*linesw[tp->t_line].l_rint)(*cp++, tp);
696 cnt++;
697 cc--;
698 }
699 cc = 0;
700 }
701 return (0);
702 block:
703 /*
704 * Come here to wait for slave to open, for space
705 * in outq, or space in rawq, or an empty canq.
706 */
707 if ((tp->t_state & TS_CONNECTED) == 0) {
708 /* adjust for data copied in but not written */
709 uio->uio_resid += cc;
710 return (EIO);
711 }
712 if (flag & IO_NDELAY) {
713 /* adjust for data copied in but not written */
714 uio->uio_resid += cc;
715 if (cnt == 0)
716 return (EWOULDBLOCK);
717 return (0);
718 }
719 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
720 if (error) {
721 /* adjust for data copied in but not written */
722 uio->uio_resid += cc;
723 return (error);
724 }
725 goto again;
726 }
727
728 #ifndef NeXT
729 /* XXX we eventually want to go to this model,
730 * but premier can't change the cdevsw */
731 static struct tty *
732 ptydevtotty(dev)
733 dev_t dev;
734 {
735 if (minor(dev) >= npty)
736 return (NULL);
737
738 return &pt_tty[minor(dev)];
739 }
740 #endif /* !NeXT */
741
742 /*ARGSUSED*/
743 FREE_BSDSTATIC int
744 #ifndef NeXT
745 ptyioctl(dev, cmd, data, flag)
746 dev_t dev;
747 int cmd;
748 caddr_t data;
749 int flag;
750 #else
751 ptyioctl(dev, cmd, data, flag, p)
752 dev_t dev;
753 u_long cmd;
754 caddr_t data;
755 int flag;
756 struct proc *p;
757 #endif
758 {
759 register struct tty *tp = pt_tty[minor(dev)];
760 register struct pt_ioctl *pti = &pt_ioctl[minor(dev)];
761 register u_char *cc = tp->t_cc;
762 int stop, error;
763
764 /*
765 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
766 * ttywflush(tp) will hang if there are characters in the outq.
767 */
768 if (cmd == TIOCEXT) {
769 /*
770 * When the EXTPROC bit is being toggled, we need
771 * to send an TIOCPKT_IOCTL if the packet driver
772 * is turned on.
773 */
774 if (*(int *)data) {
775 if (pti->pt_flags & PF_PKT) {
776 pti->pt_send |= TIOCPKT_IOCTL;
777 ptcwakeup(tp, FREAD);
778 }
779 tp->t_lflag |= EXTPROC;
780 } else {
781 if ((tp->t_lflag & EXTPROC) &&
782 (pti->pt_flags & PF_PKT)) {
783 pti->pt_send |= TIOCPKT_IOCTL;
784 ptcwakeup(tp, FREAD);
785 }
786 tp->t_lflag &= ~EXTPROC;
787 }
788 return(0);
789 } else
790 #ifndef NeXT
791 if (cdevsw[major(dev)]->d_open == ptcopen)
792 #else
793 if (cdevsw[major(dev)].d_open == ptcopen)
794 #endif
795 switch (cmd) {
796
797 case TIOCGPGRP:
798 /*
799 * We aviod calling ttioctl on the controller since,
800 * in that case, tp must be the controlling terminal.
801 */
802 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
803 return (0);
804
805 case TIOCPKT:
806 if (*(int *)data) {
807 if (pti->pt_flags & PF_UCNTL)
808 return (EINVAL);
809 pti->pt_flags |= PF_PKT;
810 } else
811 pti->pt_flags &= ~PF_PKT;
812 return (0);
813
814 case TIOCUCNTL:
815 if (*(int *)data) {
816 if (pti->pt_flags & PF_PKT)
817 return (EINVAL);
818 pti->pt_flags |= PF_UCNTL;
819 } else
820 pti->pt_flags &= ~PF_UCNTL;
821 return (0);
822
823 case TIOCREMOTE:
824 if (*(int *)data)
825 pti->pt_flags |= PF_REMOTE;
826 else
827 pti->pt_flags &= ~PF_REMOTE;
828 ttyflush(tp, FREAD|FWRITE);
829 return (0);
830
831 #ifdef COMPAT_43
832 case TIOCSETP:
833 case TIOCSETN:
834 #endif
835 case TIOCSETD:
836 case TIOCSETA:
837 case TIOCSETAW:
838 case TIOCSETAF:
839 ndflush(&tp->t_outq, tp->t_outq.c_cc);
840 break;
841
842 case TIOCSIG:
843 if (*(unsigned int *)data >= NSIG ||
844 *(unsigned int *)data == 0)
845 return(EINVAL);
846 if ((tp->t_lflag&NOFLSH) == 0)
847 ttyflush(tp, FREAD|FWRITE);
848 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
849 if ((*(unsigned int *)data == SIGINFO) &&
850 ((tp->t_lflag&NOKERNINFO) == 0))
851 ttyinfo(tp);
852 return(0);
853 }
854 error = (*linesw[tp->t_line].l_ioctl)(tp, cmd, data, flag, p);
855 if (error < 0)
856 error = ttioctl(tp, cmd, data, flag, p);
857 if (error < 0) {
858 if (pti->pt_flags & PF_UCNTL &&
859 (cmd & ~0xff) == UIOCCMD(0)) {
860 if (cmd & 0xff) {
861 pti->pt_ucntl = (u_char)cmd;
862 ptcwakeup(tp, FREAD);
863 }
864 return (0);
865 }
866 error = ENOTTY;
867 }
868 /*
869 * If external processing and packet mode send ioctl packet.
870 */
871 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
872 switch(cmd) {
873 case TIOCSETA:
874 case TIOCSETAW:
875 case TIOCSETAF:
876 #ifdef COMPAT_43
877 case TIOCSETP:
878 case TIOCSETN:
879 #endif
880 #if defined(COMPAT_43) || defined(COMPAT_SUNOS)
881 case TIOCSETC:
882 case TIOCSLTC:
883 case TIOCLBIS:
884 case TIOCLBIC:
885 case TIOCLSET:
886 #endif
887 pti->pt_send |= TIOCPKT_IOCTL;
888 ptcwakeup(tp, FREAD);
889 default:
890 break;
891 }
892 }
893 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
894 && CCEQ(cc[VSTART], CTRL('q'));
895 if (pti->pt_flags & PF_NOSTOP) {
896 if (stop) {
897 pti->pt_send &= ~TIOCPKT_NOSTOP;
898 pti->pt_send |= TIOCPKT_DOSTOP;
899 pti->pt_flags &= ~PF_NOSTOP;
900 ptcwakeup(tp, FREAD);
901 }
902 } else {
903 if (!stop) {
904 pti->pt_send &= ~TIOCPKT_DOSTOP;
905 pti->pt_send |= TIOCPKT_NOSTOP;
906 pti->pt_flags |= PF_NOSTOP;
907 ptcwakeup(tp, FREAD);
908 }
909 }
910 return (error);
911 }
912
913 #ifndef NeXT
914 static ptc_devsw_installed = 0;
915
916 static void
917 ptc_drvinit(void *unused)
918 {
919 #ifdef DEVFS
920 int i,j,k;
921 #endif
922 dev_t dev;
923
924 if( ! ptc_devsw_installed ) {
925 dev = makedev(CDEV_MAJOR_S, 0);
926 cdevsw_add(&dev, &pts_cdevsw, NULL);
927 dev = makedev(CDEV_MAJOR_C, 0);
928 cdevsw_add(&dev, &ptc_cdevsw, NULL);
929 ptc_devsw_installed = 1;
930 #ifdef DEVFS
931 for ( i = 0 ; i<NPTY ; i++ ) {
932 j = i / 32;
933 k = i % 32;
934 devfs_token_pts[i] =
935 devfs_add_devswf(&pts_cdevsw,i,
936 DV_CHR,0,0,0666,
937 "tty%c%n",jnames[j],k);
938 devfs_token_ptc[i] =
939 devfs_add_devswf(&ptc_cdevsw,i,
940 DV_CHR,0,0,0666,
941 "pty%c%n",jnames[j],k);
942 }
943 #endif
944 }
945 }
946
947 SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
948 #endif /* !NeXT */