kernel - Fix token mismatch in tty code.
[dragonfly.git] / sys / kern / tty_pty.c
CommitLineData
984263bc 1/*
22ff886e
AH
2 * (MPSAFE)
3 *
984263bc
MD
4 * Copyright (c) 1982, 1986, 1989, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 *
35 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
36 * $FreeBSD: src/sys/kern/tty_pty.c,v 1.74.2.4 2002/02/20 19:58:13 dillon Exp $
8683f632 37 * $DragonFly: src/sys/kern/tty_pty.c,v 1.21 2008/08/13 10:29:38 swildner Exp $
984263bc
MD
38 */
39
22ff886e
AH
40/*
41 * MPSAFE NOTE:
42 * Most functions here could use a separate lock to deal with concurrent
43 * access to the 'pt's.
44 *
45 * Right now the tty_token must be held for all this.
46 */
47
984263bc
MD
48/*
49 * Pseudo-teletype Driver
fef8985e 50 * (Actually two drivers, requiring two dev_ops structures)
984263bc 51 */
1f2de5d4 52#include "use_pty.h" /* XXX */
984263bc 53#include "opt_compat.h"
1f2de5d4 54
984263bc
MD
55#include <sys/param.h>
56#include <sys/systm.h>
57#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
58#include <sys/ioctl_compat.h>
59#endif
60#include <sys/proc.h>
895c1f85 61#include <sys/priv.h>
984263bc
MD
62#include <sys/tty.h>
63#include <sys/conf.h>
64#include <sys/fcntl.h>
984263bc
MD
65#include <sys/kernel.h>
66#include <sys/vnode.h>
67#include <sys/signalvar.h>
68#include <sys/malloc.h>
335dda38 69#include <sys/device.h>
e43a034f 70#include <sys/thread2.h>
2c1e28dd 71#include <sys/devfs.h>
c578a286
AH
72#include <sys/stat.h>
73#include <sys/sysctl.h>
cd29885a 74
c578a286 75#define UNIX98_PTYS 1
984263bc
MD
76
77MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
78
402ed7e1
RG
79static void ptsstart (struct tty *tp);
80static void ptsstop (struct tty *tp, int rw);
81static void ptcwakeup (struct tty *tp, int flag);
82static void ptyinit (int n);
8acdf1cf
MD
83static int filt_ptcread (struct knote *kn, long hint);
84static void filt_ptcrdetach (struct knote *kn);
85static int filt_ptcwrite (struct knote *kn, long hint);
86static void filt_ptcwdetach (struct knote *kn);
984263bc
MD
87
88static d_open_t ptsopen;
89static d_close_t ptsclose;
90static d_read_t ptsread;
91static d_write_t ptswrite;
92static d_ioctl_t ptyioctl;
93static d_open_t ptcopen;
94static d_close_t ptcclose;
95static d_read_t ptcread;
96static d_write_t ptcwrite;
8acdf1cf 97static d_kqfilter_t ptckqfilter;
c578a286
AH
98
99#ifdef UNIX98_PTYS
100DEVFS_DECLARE_CLONE_BITMAP(pty);
101
cd29885a 102static d_clone_t ptyclone;
c578a286
AH
103
104static int pty_debug_level = 0;
530c8749
AH
105
106static struct dev_ops pts98_ops = {
d4b8aec4 107 { "pts98", 0, D_TTY },
530c8749
AH
108 .d_open = ptsopen,
109 .d_close = ptsclose,
110 .d_read = ptsread,
111 .d_write = ptswrite,
112 .d_ioctl = ptyioctl,
530c8749
AH
113 .d_kqfilter = ttykqfilter,
114 .d_revoke = ttyrevoke
115};
116
117static struct dev_ops ptc98_ops = {
d4b8aec4 118 { "ptc98", 0, D_TTY | D_MASTER },
530c8749
AH
119 .d_open = ptcopen,
120 .d_close = ptcclose,
121 .d_read = ptcread,
122 .d_write = ptcwrite,
123 .d_ioctl = ptyioctl,
8acdf1cf 124 .d_kqfilter = ptckqfilter,
530c8749
AH
125 .d_revoke = ttyrevoke
126};
5d40c1cd 127#endif
984263bc
MD
128
129#define CDEV_MAJOR_S 5
fef8985e 130static struct dev_ops pts_ops = {
d4b8aec4 131 { "pts", CDEV_MAJOR_S, D_TTY },
fef8985e
MD
132 .d_open = ptsopen,
133 .d_close = ptsclose,
134 .d_read = ptsread,
135 .d_write = ptswrite,
136 .d_ioctl = ptyioctl,
a32446b7
MD
137 .d_kqfilter = ttykqfilter,
138 .d_revoke = ttyrevoke
984263bc
MD
139};
140
141#define CDEV_MAJOR_C 6
fef8985e 142static struct dev_ops ptc_ops = {
d4b8aec4 143 { "ptc", CDEV_MAJOR_C, D_TTY | D_MASTER },
fef8985e
MD
144 .d_open = ptcopen,
145 .d_close = ptcclose,
146 .d_read = ptcread,
147 .d_write = ptcwrite,
148 .d_ioctl = ptyioctl,
8acdf1cf 149 .d_kqfilter = ptckqfilter,
a32446b7 150 .d_revoke = ttyrevoke
984263bc
MD
151};
152
153#define BUFSIZ 100 /* Chunk size iomoved to/from user */
154
155struct pt_ioctl {
156 int pt_flags;
c578a286 157 int pt_flags2;
2281ff2e
MD
158 int pt_refs; /* Structural references interlock S/MOPEN */
159 int pt_uminor;
5b22f1a7 160 struct kqinfo pt_kqr, pt_kqw;
984263bc
MD
161 u_char pt_send;
162 u_char pt_ucntl;
163 struct tty pt_tty;
b13267a5 164 cdev_t devs, devc;
984263bc
MD
165 struct prison *pt_prison;
166};
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
c578a286
AH
174#define PF_UNIX98 0x01
175#define PF_SOPEN 0x02
176#define PF_MOPEN 0x04
2281ff2e 177#define PF_TERMINATED 0x08
c578a286
AH
178
179static int
180ptydebug(int level, char *fmt, ...)
181{
182 __va_list ap;
183
184 __va_start(ap, fmt);
185 if (level <= pty_debug_level)
186 kvprintf(fmt, ap);
187 __va_end(ap);
188
189 return 0;
190}
191
984263bc
MD
192/*
193 * This function creates and initializes a pts/ptc pair
194 *
195 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
196 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
197 *
198 * XXX: define and add mapping of upper minor bits to allow more
199 * than 256 ptys.
200 */
201static void
c972a82f 202ptyinit(int n)
984263bc 203{
b13267a5 204 cdev_t devs, devc;
984263bc
MD
205 char *names = "pqrsPQRS";
206 struct pt_ioctl *pt;
207
208 /* For now we only map the lower 8 bits of the minor */
209 if (n & ~0xff)
210 return;
211
e7b4468c 212 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
fef8985e 213 pt->devs = devs = make_dev(&pts_ops, n,
984263bc 214 0, 0, 0666, "tty%c%r", names[n / 32], n % 32);
fef8985e 215 pt->devc = devc = make_dev(&ptc_ops, n,
984263bc
MD
216 0, 0, 0666, "pty%c%r", names[n / 32], n % 32);
217
22ff886e
AH
218 pt->pt_tty.t_dev = devs;
219 pt->pt_uminor = n;
984263bc
MD
220 devs->si_drv1 = devc->si_drv1 = pt;
221 devs->si_tty = devc->si_tty = &pt->pt_tty;
4062d050
MD
222 devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
223 devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
984263bc
MD
224 ttyregister(&pt->pt_tty);
225}
226
c578a286 227#ifdef UNIX98_PTYS
cd29885a
MD
228static int
229ptyclone(struct dev_clone_args *ap)
230{
231 int unit;
232 struct pt_ioctl *pt;
233
c578a286
AH
234 /*
235 * Limit the number of unix98 pty (slave) devices to 1000, as
236 * the utmp(5) format only allows for 8 bytes for the tty,
237 * "pts/XXX".
238 * If this limit is reached, we don't clone and return error
239 * to devfs.
240 */
241 unit = devfs_clone_bitmap_get(&DEVFS_CLONE_BITMAP(pty), 1000);
cd29885a 242
c578a286
AH
243 if (unit < 0) {
244 ap->a_dev = NULL;
cd29885a 245 return 1;
c578a286 246 }
cd29885a
MD
247
248 pt = kmalloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
cd29885a 249
2281ff2e
MD
250 pt->devc = make_only_dev(&ptc98_ops, unit,
251 ap->a_cred->cr_ruid,
252 0, 0600, "ptm/%d", unit);
253 pt->devs = make_dev(&pts98_ops, unit,
254 ap->a_cred->cr_ruid,
255 GID_TTY, 0620, "pts/%d", unit);
256 ap->a_dev = pt->devc;
c578a286
AH
257
258 pt->devs->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
259 pt->devc->si_flags |= SI_OVERRIDE; /* uid, gid, perms from dev */
cd29885a 260
cd29885a 261 pt->pt_tty.t_dev = pt->devs;
c578a286 262 pt->pt_flags2 |= PF_UNIX98;
2281ff2e 263 pt->pt_uminor = unit;
22ff886e
AH
264 pt->devs->si_drv1 = pt->devc->si_drv1 = pt;
265 pt->devs->si_tty = pt->devc->si_tty = &pt->pt_tty;
c578a286 266
cd29885a
MD
267 ttyregister(&pt->pt_tty);
268
269 return 0;
270}
5d40c1cd 271#endif
cd29885a 272
2281ff2e
MD
273/*
274 * pti_hold() prevents the pti from being destroyed due to a termination
275 * while a pt*open() is blocked.
276 *
277 * This function returns non-zero if we cannot hold due to a termination
278 * interlock.
22ff886e
AH
279 *
280 * NOTE: Must be called with tty_token held
2281ff2e
MD
281 */
282static int
283pti_hold(struct pt_ioctl *pti)
284{
285 if (pti->pt_flags2 & PF_TERMINATED)
286 return(ENXIO);
287 ++pti->pt_refs;
288 return(0);
289}
290
291/*
292 * pti_done() releases the reference and checks to see if both sides have
293 * been closed on a unix98 pty, allowing us to destroy the device and
294 * release resources.
295 *
296 * We do not release resources on non-unix98 ptys. Those are left
297 * statically allocated.
298 */
299static void
300pti_done(struct pt_ioctl *pti)
301{
22ff886e 302 lwkt_gettoken(&tty_token);
2281ff2e
MD
303 if (--pti->pt_refs == 0) {
304#ifdef UNIX98_PTYS
305 cdev_t dev;
306 int uminor_no;
307
308 /*
309 * Only unix09 ptys are freed up
310 */
22ff886e
AH
311 if ((pti->pt_flags2 & PF_UNIX98) == 0) {
312 lwkt_reltoken(&tty_token);
2281ff2e 313 return;
22ff886e 314 }
2281ff2e
MD
315
316 /*
317 * Interlock open attempts against termination by setting
318 * PF_TERMINATED. This allows us to block while cleaning
319 * out the device infrastructure.
449728b3
MD
320 *
321 * Do not terminate the tty if it still has a session
94a6eea8 322 * association (t_refs).
2281ff2e 323 */
449728b3 324 if ((pti->pt_flags2 & (PF_SOPEN|PF_MOPEN)) == 0 &&
94a6eea8 325 pti->pt_tty.t_refs == 0) {
2281ff2e
MD
326 pti->pt_flags2 |= PF_TERMINATED;
327 uminor_no = pti->pt_uminor;
328
329 if ((dev = pti->devs) != NULL) {
330 dev->si_drv1 = NULL;
331 pti->devs = NULL;
332 destroy_dev(dev);
333 }
334 if ((dev = pti->devc) != NULL) {
335 dev->si_drv1 = NULL;
336 pti->devc = NULL;
337 destroy_dev(dev);
338 }
339 ttyunregister(&pti->pt_tty);
340 devfs_clone_bitmap_put(&DEVFS_CLONE_BITMAP(pty),
341 uminor_no);
342 kfree(pti, M_PTY);
343 }
344#endif
345 }
22ff886e 346 lwkt_reltoken(&tty_token);
2281ff2e
MD
347}
348
984263bc
MD
349/*ARGSUSED*/
350static int
fef8985e 351ptsopen(struct dev_open_args *ap)
984263bc 352{
b13267a5 353 cdev_t dev = ap->a_head.a_dev;
1fd87d54 354 struct tty *tp;
984263bc 355 int error;
984263bc
MD
356 struct pt_ioctl *pti;
357
2281ff2e
MD
358 /*
359 * The pti will already be assigned by the clone code or
360 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
361 * we are somehow racing a unix98 termination.
362 */
363 if (dev->si_drv1 == NULL)
cd29885a 364 return(ENXIO);
984263bc 365 pti = dev->si_drv1;
22ff886e
AH
366
367 lwkt_gettoken(&tty_token);
368 if (pti_hold(pti)) {
369 lwkt_reltoken(&tty_token);
2281ff2e 370 return(ENXIO);
22ff886e 371 }
984263bc
MD
372 tp = dev->si_tty;
373 if ((tp->t_state & TS_ISOPEN) == 0) {
374 ttychars(tp); /* Set up default chars */
375 tp->t_iflag = TTYDEF_IFLAG;
376 tp->t_oflag = TTYDEF_OFLAG;
377 tp->t_lflag = TTYDEF_LFLAG;
378 tp->t_cflag = TTYDEF_CFLAG;
379 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
895c1f85 380 } else if ((tp->t_state & TS_XCLUDE) && priv_check_cred(ap->a_cred, PRIV_ROOT, 0)) {
2281ff2e 381 pti_done(pti);
22ff886e 382 lwkt_reltoken(&tty_token);
984263bc 383 return (EBUSY);
fef8985e 384 } else if (pti->pt_prison != ap->a_cred->cr_prison) {
2281ff2e 385 pti_done(pti);
22ff886e 386 lwkt_reltoken(&tty_token);
984263bc
MD
387 return (EBUSY);
388 }
389 if (tp->t_oproc) /* Ctrlr still around. */
390 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
391 while ((tp->t_state & TS_CARR_ON) == 0) {
fef8985e 392 if (ap->a_oflags & FNONBLOCK)
984263bc 393 break;
377d4740 394 error = ttysleep(tp, TSA_CARR_ON(tp), PCATCH, "ptsopn", 0);
2281ff2e
MD
395 if (error) {
396 pti_done(pti);
22ff886e 397 lwkt_reltoken(&tty_token);
984263bc 398 return (error);
2281ff2e 399 }
984263bc
MD
400 }
401 error = (*linesw[tp->t_line].l_open)(dev, tp);
402 if (error == 0)
403 ptcwakeup(tp, FREAD|FWRITE);
cd29885a 404
c578a286
AH
405#ifdef UNIX98_PTYS
406 /*
407 * Unix98 pty stuff.
408 * On open of the slave, we set the corresponding flag in the common
409 * struct.
410 */
411 ptydebug(1, "ptsopen=%s | unix98? %s\n", dev->si_name,
412 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
413
2281ff2e 414 if (error == 0 && (pti->pt_flags2 & PF_UNIX98)) {
c578a286 415 pti->pt_flags2 |= PF_SOPEN;
cd29885a
MD
416 }
417#endif
2281ff2e 418 pti_done(pti);
cd29885a 419
22ff886e 420 lwkt_reltoken(&tty_token);
984263bc
MD
421 return (error);
422}
423
424static int
fef8985e 425ptsclose(struct dev_close_args *ap)
984263bc 426{
b13267a5 427 cdev_t dev = ap->a_head.a_dev;
1fd87d54 428 struct tty *tp;
c578a286 429 struct pt_ioctl *pti = dev->si_drv1;
984263bc 430 int err;
c578a286 431
22ff886e 432 lwkt_gettoken(&tty_token);
2281ff2e
MD
433 if (pti_hold(pti))
434 panic("ptsclose on terminated pti");
c578a286
AH
435 tp = dev->si_tty;
436 err = (*linesw[tp->t_line].l_close)(tp, ap->a_fflag);
437 ptsstop(tp, FREAD|FWRITE);
847ff8ce 438 (void) ttyclose(tp); /* clears t_state */
c578a286
AH
439
440#ifdef UNIX98_PTYS
441 /*
442 * Unix98 pty stuff.
443 * On close of the slave, we unset the corresponding flag, and if the master
444 * isn't open anymore, we destroy the slave and unset the unit.
445 */
446 ptydebug(1, "ptsclose=%s | unix98? %s\n", dev->si_name,
447 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
448
449 if (pti->pt_flags2 & PF_UNIX98) {
450 pti->pt_flags2 &= ~PF_SOPEN;
451 KKASSERT((pti->pt_flags2 & PF_SOPEN) == 0);
452 ptydebug(1, "master open? %s\n",
453 (pti->pt_flags2 & PF_MOPEN)?"yes":"no");
cd29885a
MD
454 }
455#endif
2281ff2e 456 pti_done(pti);
22ff886e 457 lwkt_reltoken(&tty_token);
984263bc
MD
458 return (err);
459}
460
461static int
fef8985e 462ptsread(struct dev_read_args *ap)
984263bc 463{
b13267a5 464 cdev_t dev = ap->a_head.a_dev;
984263bc 465 struct proc *p = curproc;
1fd87d54
RG
466 struct tty *tp = dev->si_tty;
467 struct pt_ioctl *pti = dev->si_drv1;
08f2f1bb
SS
468 struct lwp *lp;
469
984263bc
MD
470 int error = 0;
471
08f2f1bb
SS
472 lp = curthread->td_lwp;
473
22ff886e 474 lwkt_gettoken(&tty_token);
984263bc
MD
475again:
476 if (pti->pt_flags & PF_REMOTE) {
477 while (isbackground(p, tp)) {
478 if (SIGISMEMBER(p->p_sigignore, SIGTTIN) ||
08f2f1bb 479 SIGISMEMBER(lp->lwp_sigmask, SIGTTIN) ||
22ff886e
AH
480 p->p_pgrp->pg_jobc == 0 || p->p_flag & P_PPWAIT) {
481 lwkt_reltoken(&tty_token);
984263bc 482 return (EIO);
22ff886e 483 }
984263bc 484 pgsignal(p->p_pgrp, SIGTTIN, 1);
377d4740 485 error = ttysleep(tp, &lbolt, PCATCH, "ptsbg", 0);
22ff886e
AH
486 if (error) {
487 lwkt_reltoken(&tty_token);
984263bc 488 return (error);
22ff886e 489 }
984263bc
MD
490 }
491 if (tp->t_canq.c_cc == 0) {
22ff886e
AH
492 if (ap->a_ioflag & IO_NDELAY) {
493 lwkt_reltoken(&tty_token);
984263bc 494 return (EWOULDBLOCK);
22ff886e 495 }
377d4740 496 error = ttysleep(tp, TSA_PTS_READ(tp), PCATCH,
984263bc 497 "ptsin", 0);
22ff886e
AH
498 if (error) {
499 lwkt_reltoken(&tty_token);
984263bc 500 return (error);
22ff886e 501 }
984263bc
MD
502 goto again;
503 }
fef8985e 504 while (tp->t_canq.c_cc > 1 && ap->a_uio->uio_resid > 0)
0ced1954 505 if (ureadc(clist_getc(&tp->t_canq), ap->a_uio) < 0) {
984263bc
MD
506 error = EFAULT;
507 break;
508 }
509 if (tp->t_canq.c_cc == 1)
0ced1954 510 clist_getc(&tp->t_canq);
22ff886e
AH
511 if (tp->t_canq.c_cc) {
512 lwkt_reltoken(&tty_token);
984263bc 513 return (error);
22ff886e 514 }
984263bc
MD
515 } else
516 if (tp->t_oproc)
fef8985e 517 error = (*linesw[tp->t_line].l_read)(tp, ap->a_uio, ap->a_ioflag);
984263bc 518 ptcwakeup(tp, FWRITE);
22ff886e 519 lwkt_reltoken(&tty_token);
984263bc
MD
520 return (error);
521}
522
523/*
524 * Write to pseudo-tty.
525 * Wakeups of controlling tty will happen
526 * indirectly, when tty driver calls ptsstart.
527 */
528static int
fef8985e 529ptswrite(struct dev_write_args *ap)
984263bc 530{
b13267a5 531 cdev_t dev = ap->a_head.a_dev;
1fd87d54 532 struct tty *tp;
22ff886e 533 int ret;
984263bc 534
22ff886e 535 lwkt_gettoken(&tty_token);
984263bc 536 tp = dev->si_tty;
22ff886e
AH
537 if (tp->t_oproc == 0) {
538 lwkt_reltoken(&tty_token);
984263bc 539 return (EIO);
22ff886e
AH
540 }
541 ret = ((*linesw[tp->t_line].l_write)(tp, ap->a_uio, ap->a_ioflag));
542 lwkt_reltoken(&tty_token);
543 return ret;
984263bc
MD
544}
545
546/*
547 * Start output on pseudo-tty.
548 * Wake up process selecting or sleeping for input from controlling tty.
549 */
550static void
c972a82f 551ptsstart(struct tty *tp)
984263bc 552{
22ff886e 553 lwkt_gettoken(&tty_token);
1fd87d54 554 struct pt_ioctl *pti = tp->t_dev->si_drv1;
984263bc 555
adb56101
MD
556 if (tp->t_state & TS_TTSTOP) {
557 lwkt_reltoken(&tty_token);
984263bc 558 return;
adb56101 559 }
1c375cd1
MD
560 if (pti) {
561 if (pti->pt_flags & PF_STOPPED) {
562 pti->pt_flags &= ~PF_STOPPED;
563 pti->pt_send = TIOCPKT_START;
564 }
984263bc
MD
565 }
566 ptcwakeup(tp, FREAD);
22ff886e 567 lwkt_reltoken(&tty_token);
984263bc
MD
568}
569
22ff886e
AH
570/*
571 * NOTE: Must be called with tty_token held
572 */
984263bc 573static void
c972a82f 574ptcwakeup(struct tty *tp, int flag)
984263bc 575{
22ff886e
AH
576 ASSERT_LWKT_TOKEN_HELD(&tty_token);
577
984263bc 578 if (flag & FREAD) {
984263bc 579 wakeup(TSA_PTC_READ(tp));
5b22f1a7 580 KNOTE(&tp->t_rkq.ki_note, 0);
984263bc
MD
581 }
582 if (flag & FWRITE) {
984263bc 583 wakeup(TSA_PTC_WRITE(tp));
5b22f1a7 584 KNOTE(&tp->t_wkq.ki_note, 0);
984263bc
MD
585 }
586}
587
588static int
fef8985e 589ptcopen(struct dev_open_args *ap)
984263bc 590{
b13267a5 591 cdev_t dev = ap->a_head.a_dev;
1fd87d54 592 struct tty *tp;
984263bc
MD
593 struct pt_ioctl *pti;
594
2281ff2e
MD
595 /*
596 * The pti will already be assigned by the clone code or
597 * pre-created if a non-unix 98 pty. If si_drv1 is NULL
598 * we are somehow racing a unix98 termination.
599 */
600 if (dev->si_drv1 == NULL)
22ff886e
AH
601 return(ENXIO);
602
603 lwkt_gettoken(&tty_token);
4062d050 604 pti = dev->si_drv1;
22ff886e
AH
605 if (pti_hold(pti)) {
606 lwkt_reltoken(&tty_token);
2281ff2e 607 return(ENXIO);
22ff886e 608 }
2281ff2e
MD
609 if (pti->pt_prison && pti->pt_prison != ap->a_cred->cr_prison) {
610 pti_done(pti);
22ff886e 611 lwkt_reltoken(&tty_token);
4062d050 612 return(EBUSY);
2281ff2e 613 }
984263bc 614 tp = dev->si_tty;
2281ff2e
MD
615 if (tp->t_oproc) {
616 pti_done(pti);
22ff886e 617 lwkt_reltoken(&tty_token);
984263bc 618 return (EIO);
2281ff2e 619 }
984263bc
MD
620 tp->t_oproc = ptsstart;
621 tp->t_stop = ptsstop;
622 (void)(*linesw[tp->t_line].l_modem)(tp, 1);
623 tp->t_lflag &= ~EXTPROC;
fef8985e 624 pti->pt_prison = ap->a_cred->cr_prison;
984263bc
MD
625 pti->pt_flags = 0;
626 pti->pt_send = 0;
627 pti->pt_ucntl = 0;
4062d050 628
cd65363e 629 pti->devs->si_uid = ap->a_cred->cr_uid;
4062d050
MD
630 pti->devs->si_gid = 0;
631 pti->devs->si_perms = 0600;
632 pti->devc->si_uid = ap->a_cred->cr_uid;
633 pti->devc->si_gid = 0;
634 pti->devc->si_perms = 0600;
635
c578a286
AH
636#ifdef UNIX98_PTYS
637 /*
638 * Unix98 pty stuff.
639 * On open of the master, we set the corresponding flag in the common
640 * struct.
641 */
642 ptydebug(1, "ptcopen=%s (master) | unix98? %s\n", dev->si_name,
643 (pti->pt_flags2 & PF_UNIX98)?"yes":"no");
644
645 if (pti->pt_flags2 & PF_UNIX98) {
646 pti->pt_flags2 |= PF_MOPEN;
647 }
648#endif
2281ff2e 649 pti_done(pti);
c578a286 650
22ff886e 651 lwkt_reltoken(&tty_token);
984263bc
MD
652 return (0);
653}
654
655static int
fef8985e 656ptcclose(struct dev_close_args *ap)
984263bc 657{
b13267a5 658 cdev_t dev = ap->a_head.a_dev;
1fd87d54 659 struct tty *tp;
c578a286 660 struct pt_ioctl *pti = dev->si_drv1;
2281ff2e 661
22ff886e 662 lwkt_gettoken(&tty_token);
2281ff2e
MD
663 if (pti_hold(pti))
664 panic("ptcclose on terminated pti");
984263bc
MD
665
666 tp = dev->si_tty;
667 (void)(*linesw[tp->t_line].l_modem)(tp, 0);
668
669 /*
670 * XXX MDMBUF makes no sense for ptys but would inhibit the above
671 * l_modem(). CLOCAL makes sense but isn't supported. Special
672 * l_modem()s that ignore carrier drop make no sense for ptys but
673 * may be in use because other parts of the line discipline make
674 * sense for ptys. Recover by doing everything that a normal
675 * ttymodem() would have done except for sending a SIGHUP.
676 */
677 if (tp->t_state & TS_ISOPEN) {
678 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
679 tp->t_state |= TS_ZOMBIE;
680 ttyflush(tp, FREAD | FWRITE);
681 }
984263bc 682 tp->t_oproc = 0; /* mark closed */
4062d050 683
2281ff2e
MD
684#ifdef UNIX98_PTYS
685 /*
686 * Unix98 pty stuff.
687 * On close of the master, we unset the corresponding flag in the common
688 * struct asap.
689 */
690 pti->pt_flags2 &= ~PF_MOPEN;
691#endif
692
4062d050
MD
693 pti->pt_prison = NULL;
694 pti->devs->si_uid = 0;
695 pti->devs->si_gid = 0;
696 pti->devs->si_perms = 0666;
697 pti->devc->si_uid = 0;
698 pti->devc->si_gid = 0;
699 pti->devc->si_perms = 0666;
700
2281ff2e 701 pti_done(pti);
22ff886e
AH
702
703 lwkt_reltoken(&tty_token);
984263bc
MD
704 return (0);
705}
706
707static int
fef8985e 708ptcread(struct dev_read_args *ap)
984263bc 709{
b13267a5 710 cdev_t dev = ap->a_head.a_dev;
1fd87d54 711 struct tty *tp = dev->si_tty;
984263bc
MD
712 struct pt_ioctl *pti = dev->si_drv1;
713 char buf[BUFSIZ];
714 int error = 0, cc;
715
22ff886e 716 lwkt_gettoken(&tty_token);
984263bc
MD
717 /*
718 * We want to block until the slave
719 * is open, and there's something to read;
720 * but if we lost the slave or we're NBIO,
721 * then return the appropriate error instead.
722 */
723 for (;;) {
724 if (tp->t_state&TS_ISOPEN) {
725 if (pti->pt_flags&PF_PKT && pti->pt_send) {
fef8985e 726 error = ureadc((int)pti->pt_send, ap->a_uio);
22ff886e
AH
727 if (error) {
728 lwkt_reltoken(&tty_token);
984263bc 729 return (error);
22ff886e 730 }
984263bc 731 if (pti->pt_send & TIOCPKT_IOCTL) {
e54488bb
MD
732 cc = (int)szmin(ap->a_uio->uio_resid,
733 sizeof(tp->t_termios));
984263bc 734 uiomove((caddr_t)&tp->t_termios, cc,
fef8985e 735 ap->a_uio);
984263bc
MD
736 }
737 pti->pt_send = 0;
22ff886e 738 lwkt_reltoken(&tty_token);
984263bc
MD
739 return (0);
740 }
741 if (pti->pt_flags&PF_UCNTL && pti->pt_ucntl) {
fef8985e 742 error = ureadc((int)pti->pt_ucntl, ap->a_uio);
22ff886e
AH
743 if (error) {
744 lwkt_reltoken(&tty_token);
984263bc 745 return (error);
22ff886e 746 }
984263bc 747 pti->pt_ucntl = 0;
22ff886e 748 lwkt_reltoken(&tty_token);
984263bc
MD
749 return (0);
750 }
751 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
752 break;
753 }
22ff886e
AH
754 if ((tp->t_state & TS_CONNECTED) == 0) {
755 lwkt_reltoken(&tty_token);
984263bc 756 return (0); /* EOF */
22ff886e
AH
757 }
758 if (ap->a_ioflag & IO_NDELAY) {
759 lwkt_reltoken(&tty_token);
984263bc 760 return (EWOULDBLOCK);
22ff886e 761 }
377d4740 762 error = tsleep(TSA_PTC_READ(tp), PCATCH, "ptcin", 0);
22ff886e
AH
763 if (error) {
764 lwkt_reltoken(&tty_token);
984263bc 765 return (error);
22ff886e 766 }
984263bc
MD
767 }
768 if (pti->pt_flags & (PF_PKT|PF_UCNTL))
fef8985e
MD
769 error = ureadc(0, ap->a_uio);
770 while (ap->a_uio->uio_resid > 0 && error == 0) {
e54488bb
MD
771 cc = q_to_b(&tp->t_outq, buf,
772 (int)szmin(ap->a_uio->uio_resid, BUFSIZ));
984263bc
MD
773 if (cc <= 0)
774 break;
e54488bb 775 error = uiomove(buf, (size_t)cc, ap->a_uio);
984263bc
MD
776 }
777 ttwwakeup(tp);
22ff886e 778 lwkt_reltoken(&tty_token);
984263bc
MD
779 return (error);
780}
781
782static void
c972a82f 783ptsstop(struct tty *tp, int flush)
984263bc
MD
784{
785 struct pt_ioctl *pti = tp->t_dev->si_drv1;
786 int flag;
787
22ff886e 788 lwkt_gettoken(&tty_token);
984263bc 789 /* note: FLUSHREAD and FLUSHWRITE already ok */
1c375cd1
MD
790 if (pti) {
791 if (flush == 0) {
792 flush = TIOCPKT_STOP;
793 pti->pt_flags |= PF_STOPPED;
794 } else {
795 pti->pt_flags &= ~PF_STOPPED;
796 }
797 pti->pt_send |= flush;
798 /* change of perspective */
799 }
984263bc
MD
800 flag = 0;
801 if (flush & FREAD)
802 flag |= FWRITE;
803 if (flush & FWRITE)
804 flag |= FREAD;
805 ptcwakeup(tp, flag);
22ff886e
AH
806
807 lwkt_reltoken(&tty_token);
984263bc
MD
808}
809
8acdf1cf
MD
810/*
811 * kqueue ops for pseudo-terminals.
812 */
813static struct filterops ptcread_filtops =
4c91dbc9 814 { FILTEROP_ISFD, NULL, filt_ptcrdetach, filt_ptcread };
8acdf1cf 815static struct filterops ptcwrite_filtops =
4c91dbc9 816 { FILTEROP_ISFD, NULL, filt_ptcwdetach, filt_ptcwrite };
8acdf1cf
MD
817
818static int
819ptckqfilter(struct dev_kqfilter_args *ap)
820{
821 cdev_t dev = ap->a_head.a_dev;
822 struct knote *kn = ap->a_kn;
823 struct tty *tp = dev->si_tty;
824 struct klist *klist;
825
22ff886e 826 lwkt_gettoken(&tty_token);
8acdf1cf
MD
827 ap->a_result = 0;
828 switch (kn->kn_filter) {
829 case EVFILT_READ:
5b22f1a7 830 klist = &tp->t_rkq.ki_note;
8acdf1cf
MD
831 kn->kn_fop = &ptcread_filtops;
832 break;
833 case EVFILT_WRITE:
5b22f1a7 834 klist = &tp->t_wkq.ki_note;
8acdf1cf
MD
835 kn->kn_fop = &ptcwrite_filtops;
836 break;
837 default:
b287d649 838 ap->a_result = EOPNOTSUPP;
22ff886e 839 lwkt_reltoken(&tty_token);
8acdf1cf
MD
840 return (0);
841 }
842
843 kn->kn_hook = (caddr_t)dev;
5b22f1a7 844 knote_insert(klist, kn);
22ff886e 845 lwkt_reltoken(&tty_token);
8acdf1cf
MD
846 return (0);
847}
848
849static int
850filt_ptcread (struct knote *kn, long hint)
851{
852 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
853 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
854
22ff886e 855 lwkt_gettoken(&tty_token);
847ff8ce
SG
856 if (tp->t_state & TS_ZOMBIE) {
857 kn->kn_flags |= EV_EOF;
22ff886e 858 lwkt_reltoken(&tty_token);
847ff8ce
SG
859 return (1);
860 }
861
8acdf1cf
MD
862 if ((tp->t_state & TS_ISOPEN) &&
863 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
864 ((pti->pt_flags & PF_PKT) && pti->pt_send) ||
865 ((pti->pt_flags & PF_UCNTL) && pti->pt_ucntl))) {
866 kn->kn_data = tp->t_outq.c_cc;
22ff886e 867 lwkt_reltoken(&tty_token);
8acdf1cf
MD
868 return(1);
869 } else {
22ff886e 870 lwkt_reltoken(&tty_token);
8acdf1cf
MD
871 return(0);
872 }
873}
874
875static int
876filt_ptcwrite (struct knote *kn, long hint)
877{
878 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
879 struct pt_ioctl *pti = ((cdev_t)kn->kn_hook)->si_drv1;
880
22ff886e 881 lwkt_gettoken(&tty_token);
847ff8ce
SG
882 if (tp->t_state & TS_ZOMBIE) {
883 kn->kn_flags |= EV_EOF;
22ff886e 884 lwkt_reltoken(&tty_token);
847ff8ce
SG
885 return (1);
886 }
887
8acdf1cf
MD
888 if (tp->t_state & TS_ISOPEN &&
889 ((pti->pt_flags & PF_REMOTE) ?
890 (tp->t_canq.c_cc == 0) :
891 ((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
892 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON))))) {
893 kn->kn_data = tp->t_canq.c_cc + tp->t_rawq.c_cc;
22ff886e 894 lwkt_reltoken(&tty_token);
8acdf1cf
MD
895 return(1);
896 } else {
22ff886e 897 lwkt_reltoken(&tty_token);
8acdf1cf
MD
898 return(0);
899 }
22ff886e 900 /* NOTREACHED */
8acdf1cf
MD
901}
902
903static void
904filt_ptcrdetach (struct knote *kn)
905{
906 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
907
5b22f1a7 908 knote_remove(&tp->t_rkq.ki_note, kn);
8acdf1cf
MD
909}
910
911static void
912filt_ptcwdetach (struct knote *kn)
913{
914 struct tty *tp = ((cdev_t)kn->kn_hook)->si_tty;
915
5b22f1a7 916 knote_remove(&tp->t_wkq.ki_note, kn);
8acdf1cf
MD
917}
918
919/*
920 * I/O ops
921 */
984263bc 922static int
fef8985e 923ptcwrite(struct dev_write_args *ap)
984263bc 924{
b13267a5 925 cdev_t dev = ap->a_head.a_dev;
1fd87d54
RG
926 struct tty *tp = dev->si_tty;
927 u_char *cp = 0;
928 int cc = 0;
984263bc
MD
929 u_char locbuf[BUFSIZ];
930 int cnt = 0;
931 struct pt_ioctl *pti = dev->si_drv1;
932 int error = 0;
933
22ff886e 934 lwkt_gettoken(&tty_token);
984263bc
MD
935again:
936 if ((tp->t_state&TS_ISOPEN) == 0)
937 goto block;
938 if (pti->pt_flags & PF_REMOTE) {
939 if (tp->t_canq.c_cc)
940 goto block;
fef8985e 941 while ((ap->a_uio->uio_resid > 0 || cc > 0) &&
984263bc
MD
942 tp->t_canq.c_cc < TTYHOG - 1) {
943 if (cc == 0) {
e54488bb
MD
944 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
945 cc = imin(cc, TTYHOG - 1 - tp->t_canq.c_cc);
984263bc 946 cp = locbuf;
e54488bb 947 error = uiomove(cp, (size_t)cc, ap->a_uio);
22ff886e
AH
948 if (error) {
949 lwkt_reltoken(&tty_token);
984263bc 950 return (error);
22ff886e 951 }
984263bc
MD
952 /* check again for safety */
953 if ((tp->t_state & TS_ISOPEN) == 0) {
954 /* adjust as usual */
fef8985e 955 ap->a_uio->uio_resid += cc;
22ff886e 956 lwkt_reltoken(&tty_token);
984263bc
MD
957 return (EIO);
958 }
959 }
960 if (cc > 0) {
961 cc = b_to_q((char *)cp, cc, &tp->t_canq);
962 /*
963 * XXX we don't guarantee that the canq size
964 * is >= TTYHOG, so the above b_to_q() may
965 * leave some bytes uncopied. However, space
966 * is guaranteed for the null terminator if
967 * we don't fail here since (TTYHOG - 1) is
968 * not a multiple of CBSIZE.
969 */
970 if (cc > 0)
971 break;
972 }
973 }
974 /* adjust for data copied in but not written */
fef8985e 975 ap->a_uio->uio_resid += cc;
0ced1954 976 clist_putc(0, &tp->t_canq);
984263bc
MD
977 ttwakeup(tp);
978 wakeup(TSA_PTS_READ(tp));
22ff886e 979 lwkt_reltoken(&tty_token);
984263bc
MD
980 return (0);
981 }
fef8985e 982 while (ap->a_uio->uio_resid > 0 || cc > 0) {
984263bc 983 if (cc == 0) {
e54488bb 984 cc = (int)szmin(ap->a_uio->uio_resid, BUFSIZ);
984263bc 985 cp = locbuf;
e54488bb 986 error = uiomove(cp, (size_t)cc, ap->a_uio);
22ff886e
AH
987 if (error) {
988 lwkt_reltoken(&tty_token);
984263bc 989 return (error);
22ff886e 990 }
984263bc
MD
991 /* check again for safety */
992 if ((tp->t_state & TS_ISOPEN) == 0) {
993 /* adjust for data copied in but not written */
fef8985e 994 ap->a_uio->uio_resid += cc;
22ff886e 995 lwkt_reltoken(&tty_token);
984263bc
MD
996 return (EIO);
997 }
998 }
999 while (cc > 0) {
1000 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
1001 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
1002 wakeup(TSA_HUP_OR_INPUT(tp));
1003 goto block;
1004 }
1005 (*linesw[tp->t_line].l_rint)(*cp++, tp);
1006 cnt++;
1007 cc--;
1008 }
1009 cc = 0;
1010 }
22ff886e 1011 lwkt_reltoken(&tty_token);
984263bc
MD
1012 return (0);
1013block:
1014 /*
1015 * Come here to wait for slave to open, for space
1016 * in outq, or space in rawq, or an empty canq.
1017 */
1018 if ((tp->t_state & TS_CONNECTED) == 0) {
1019 /* adjust for data copied in but not written */
fef8985e 1020 ap->a_uio->uio_resid += cc;
22ff886e 1021 lwkt_reltoken(&tty_token);
984263bc
MD
1022 return (EIO);
1023 }
fef8985e 1024 if (ap->a_ioflag & IO_NDELAY) {
984263bc 1025 /* adjust for data copied in but not written */
fef8985e 1026 ap->a_uio->uio_resid += cc;
22ff886e
AH
1027 if (cnt == 0) {
1028 lwkt_reltoken(&tty_token);
984263bc 1029 return (EWOULDBLOCK);
22ff886e
AH
1030 }
1031 lwkt_reltoken(&tty_token);
984263bc
MD
1032 return (0);
1033 }
377d4740 1034 error = tsleep(TSA_PTC_WRITE(tp), PCATCH, "ptcout", 0);
984263bc
MD
1035 if (error) {
1036 /* adjust for data copied in but not written */
fef8985e 1037 ap->a_uio->uio_resid += cc;
22ff886e 1038 lwkt_reltoken(&tty_token);
984263bc
MD
1039 return (error);
1040 }
1041 goto again;
1042}
1043
1044/*ARGSUSED*/
1045static int
fef8985e 1046ptyioctl(struct dev_ioctl_args *ap)
984263bc 1047{
b13267a5 1048 cdev_t dev = ap->a_head.a_dev;
1fd87d54
RG
1049 struct tty *tp = dev->si_tty;
1050 struct pt_ioctl *pti = dev->si_drv1;
1051 u_char *cc = tp->t_cc;
984263bc
MD
1052 int stop, error;
1053
22ff886e 1054 lwkt_gettoken(&tty_token);
335dda38 1055 if (dev_dflags(dev) & D_MASTER) {
fef8985e 1056 switch (ap->a_cmd) {
984263bc
MD
1057
1058 case TIOCGPGRP:
1059 /*
1060 * We avoid calling ttioctl on the controller since,
1061 * in that case, tp must be the controlling terminal.
1062 */
fef8985e 1063 *(int *)ap->a_data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
22ff886e 1064 lwkt_reltoken(&tty_token);
984263bc
MD
1065 return (0);
1066
1067 case TIOCPKT:
fef8985e 1068 if (*(int *)ap->a_data) {
22ff886e
AH
1069 if (pti->pt_flags & PF_UCNTL) {
1070 lwkt_reltoken(&tty_token);
984263bc 1071 return (EINVAL);
22ff886e 1072 }
984263bc
MD
1073 pti->pt_flags |= PF_PKT;
1074 } else
1075 pti->pt_flags &= ~PF_PKT;
22ff886e 1076 lwkt_reltoken(&tty_token);
984263bc
MD
1077 return (0);
1078
1079 case TIOCUCNTL:
fef8985e 1080 if (*(int *)ap->a_data) {
22ff886e
AH
1081 if (pti->pt_flags & PF_PKT) {
1082 lwkt_reltoken(&tty_token);
984263bc 1083 return (EINVAL);
22ff886e 1084 }
984263bc
MD
1085 pti->pt_flags |= PF_UCNTL;
1086 } else
1087 pti->pt_flags &= ~PF_UCNTL;
22ff886e 1088 lwkt_reltoken(&tty_token);
984263bc
MD
1089 return (0);
1090
1091 case TIOCREMOTE:
fef8985e 1092 if (*(int *)ap->a_data)
984263bc
MD
1093 pti->pt_flags |= PF_REMOTE;
1094 else
1095 pti->pt_flags &= ~PF_REMOTE;
1096 ttyflush(tp, FREAD|FWRITE);
22ff886e 1097 lwkt_reltoken(&tty_token);
984263bc 1098 return (0);
c578a286 1099
22ff886e 1100#ifdef UNIX98_PTYS
c578a286 1101 case TIOCISPTMASTER:
22ff886e
AH
1102 if ((pti->pt_flags2 & PF_UNIX98) && (pti->devc == dev)) {
1103 lwkt_reltoken(&tty_token);
c578a286 1104 return (0);
22ff886e
AH
1105 } else {
1106 lwkt_reltoken(&tty_token);
c578a286 1107 return (EINVAL);
22ff886e 1108 }
984263bc 1109 }
22ff886e 1110#endif
984263bc
MD
1111
1112 /*
1113 * The rest of the ioctls shouldn't be called until
1114 * the slave is open.
1115 */
22ff886e
AH
1116 if ((tp->t_state & TS_ISOPEN) == 0) {
1117 lwkt_reltoken(&tty_token);
984263bc 1118 return (EAGAIN);
22ff886e 1119 }
984263bc 1120
fef8985e 1121 switch (ap->a_cmd) {
984263bc
MD
1122#ifdef COMPAT_43
1123 case TIOCSETP:
1124 case TIOCSETN:
1125#endif
1126 case TIOCSETD:
1127 case TIOCSETA:
1128 case TIOCSETAW:
1129 case TIOCSETAF:
1130 /*
1131 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
1132 * ttywflush(tp) will hang if there are characters in
1133 * the outq.
1134 */
1135 ndflush(&tp->t_outq, tp->t_outq.c_cc);
1136 break;
1137
1138 case TIOCSIG:
fef8985e 1139 if (*(unsigned int *)ap->a_data >= NSIG ||
22ff886e
AH
1140 *(unsigned int *)ap->a_data == 0) {
1141 lwkt_reltoken(&tty_token);
984263bc 1142 return(EINVAL);
22ff886e 1143 }
984263bc
MD
1144 if ((tp->t_lflag&NOFLSH) == 0)
1145 ttyflush(tp, FREAD|FWRITE);
fef8985e
MD
1146 pgsignal(tp->t_pgrp, *(unsigned int *)ap->a_data, 1);
1147 if ((*(unsigned int *)ap->a_data == SIGINFO) &&
984263bc
MD
1148 ((tp->t_lflag&NOKERNINFO) == 0))
1149 ttyinfo(tp);
22ff886e 1150 lwkt_reltoken(&tty_token);
984263bc
MD
1151 return(0);
1152 }
1153 }
fef8985e 1154 if (ap->a_cmd == TIOCEXT) {
984263bc
MD
1155 /*
1156 * When the EXTPROC bit is being toggled, we need
1157 * to send an TIOCPKT_IOCTL if the packet driver
1158 * is turned on.
1159 */
fef8985e 1160 if (*(int *)ap->a_data) {
984263bc
MD
1161 if (pti->pt_flags & PF_PKT) {
1162 pti->pt_send |= TIOCPKT_IOCTL;
1163 ptcwakeup(tp, FREAD);
1164 }
1165 tp->t_lflag |= EXTPROC;
1166 } else {
1167 if ((tp->t_lflag & EXTPROC) &&
1168 (pti->pt_flags & PF_PKT)) {
1169 pti->pt_send |= TIOCPKT_IOCTL;
1170 ptcwakeup(tp, FREAD);
1171 }
1172 tp->t_lflag &= ~EXTPROC;
1173 }
22ff886e 1174 lwkt_reltoken(&tty_token);
984263bc
MD
1175 return(0);
1176 }
fef8985e
MD
1177 error = (*linesw[tp->t_line].l_ioctl)(tp, ap->a_cmd, ap->a_data,
1178 ap->a_fflag, ap->a_cred);
984263bc 1179 if (error == ENOIOCTL)
fef8985e 1180 error = ttioctl(tp, ap->a_cmd, ap->a_data, ap->a_fflag);
984263bc
MD
1181 if (error == ENOIOCTL) {
1182 if (pti->pt_flags & PF_UCNTL &&
fef8985e
MD
1183 (ap->a_cmd & ~0xff) == UIOCCMD(0)) {
1184 if (ap->a_cmd & 0xff) {
1185 pti->pt_ucntl = (u_char)ap->a_cmd;
984263bc
MD
1186 ptcwakeup(tp, FREAD);
1187 }
22ff886e 1188 lwkt_reltoken(&tty_token);
984263bc
MD
1189 return (0);
1190 }
1191 error = ENOTTY;
1192 }
1193 /*
1194 * If external processing and packet mode send ioctl packet.
1195 */
1196 if ((tp->t_lflag&EXTPROC) && (pti->pt_flags & PF_PKT)) {
fef8985e 1197 switch(ap->a_cmd) {
984263bc
MD
1198 case TIOCSETA:
1199 case TIOCSETAW:
1200 case TIOCSETAF:
1201#ifdef COMPAT_43
1202 case TIOCSETP:
1203 case TIOCSETN:
1204#endif
1205#if defined(COMPAT_43) || defined(COMPAT_SUNOS)
1206 case TIOCSETC:
1207 case TIOCSLTC:
1208 case TIOCLBIS:
1209 case TIOCLBIC:
1210 case TIOCLSET:
1211#endif
1212 pti->pt_send |= TIOCPKT_IOCTL;
1213 ptcwakeup(tp, FREAD);
1214 default:
1215 break;
1216 }
1217 }
1218 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
1219 && CCEQ(cc[VSTART], CTRL('q'));
1220 if (pti->pt_flags & PF_NOSTOP) {
1221 if (stop) {
1222 pti->pt_send &= ~TIOCPKT_NOSTOP;
1223 pti->pt_send |= TIOCPKT_DOSTOP;
1224 pti->pt_flags &= ~PF_NOSTOP;
1225 ptcwakeup(tp, FREAD);
1226 }
1227 } else {
1228 if (!stop) {
1229 pti->pt_send &= ~TIOCPKT_DOSTOP;
1230 pti->pt_send |= TIOCPKT_NOSTOP;
1231 pti->pt_flags |= PF_NOSTOP;
1232 ptcwakeup(tp, FREAD);
1233 }
1234 }
22ff886e 1235 lwkt_reltoken(&tty_token);
984263bc
MD
1236 return (error);
1237}
1238
1239
402ed7e1 1240static void ptc_drvinit (void *unused);
984263bc 1241
c578a286
AH
1242#ifdef UNIX98_PTYS
1243SYSCTL_INT(_kern, OID_AUTO, pty_debug, CTLFLAG_RW, &pty_debug_level,
1244 0, "Change pty debug level");
1245#endif
1246
984263bc 1247static void
c972a82f 1248ptc_drvinit(void *unused)
984263bc 1249{
cd29885a 1250 int i;
cd29885a 1251
c578a286
AH
1252#ifdef UNIX98_PTYS
1253 /*
1254 * Unix98 pty stuff.
1255 * Create the clonable base device.
1256 */
1257 make_autoclone_dev(&ptc_ops, &DEVFS_CLONE_BITMAP(pty), ptyclone,
1258 0, 0, 0666, "ptmx");
cd29885a 1259#endif
c578a286 1260
cd29885a
MD
1261 for (i = 0; i < 256; i++) {
1262 ptyinit(i);
1263 }
984263bc
MD
1264}
1265
1266SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)