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