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