]> git.saurik.com Git - apple/xnu.git/blame - bsd/kern/tty_pty.c
xnu-792.24.17.tar.gz
[apple/xnu.git] / bsd / kern / tty_pty.c
CommitLineData
1c79356b 1/*
5d5c5d0d
A
2 * Copyright (c) 2000-2004 Apple Computer, Inc. All rights reserved.
3 *
6601e61a 4 * @APPLE_LICENSE_HEADER_START@
1c79356b 5 *
6601e61a
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.
8f6c56a5 11 *
6601e61a
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
8f6c56a5
A
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
6601e61a
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.
8f6c56a5 19 *
6601e61a 20 * @APPLE_LICENSE_HEADER_END@
1c79356b
A
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 482 tp->t_oproc = ptsstart;
743b1565 483 CLR(tp->t_state, TS_ZOMBIE);
1c79356b
A
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 */