Deleted Added
full compact
pty.c (135298) pty.c (135622)
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
30 */
31
32#include <sys/cdefs.h>
1/*
2 * Copyright (c) 1982, 1986, 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 4. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
28 *
29 * @(#)tty_pty.c 8.4 (Berkeley) 2/20/95
30 */
31
32#include <sys/cdefs.h>
33__FBSDID("$FreeBSD: head/sys/kern/tty_pty.c 135298 2004-09-16 12:07:25Z phk $");
33__FBSDID("$FreeBSD: head/sys/kern/tty_pty.c 135622 2004-09-23 16:13:46Z phk $");
34
35/*
36 * Pseudo-teletype Driver
37 * (Actually two drivers, requiring two entries in 'cdevsw')
38 */
39#include "opt_compat.h"
40#include "opt_tty.h"
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/lock.h>
44#include <sys/mutex.h>
45#include <sys/sx.h>
46#ifndef BURN_BRIDGES
47#if defined(COMPAT_43)
48#include <sys/ioctl_compat.h>
49#endif
50#endif
51#include <sys/proc.h>
52#include <sys/tty.h>
53#include <sys/conf.h>
54#include <sys/fcntl.h>
55#include <sys/poll.h>
56#include <sys/kernel.h>
57#include <sys/vnode.h>
58#include <sys/signalvar.h>
59#include <sys/malloc.h>
60
61static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
62
63static void ptsstart(struct tty *tp);
64static void ptsstop(struct tty *tp, int rw);
65static void ptcwakeup(struct tty *tp, int flag);
66static struct cdev *ptyinit(struct cdev *cdev);
67
68static d_open_t ptsopen;
69static d_close_t ptsclose;
70static d_read_t ptsread;
71static d_write_t ptswrite;
34
35/*
36 * Pseudo-teletype Driver
37 * (Actually two drivers, requiring two entries in 'cdevsw')
38 */
39#include "opt_compat.h"
40#include "opt_tty.h"
41#include <sys/param.h>
42#include <sys/systm.h>
43#include <sys/lock.h>
44#include <sys/mutex.h>
45#include <sys/sx.h>
46#ifndef BURN_BRIDGES
47#if defined(COMPAT_43)
48#include <sys/ioctl_compat.h>
49#endif
50#endif
51#include <sys/proc.h>
52#include <sys/tty.h>
53#include <sys/conf.h>
54#include <sys/fcntl.h>
55#include <sys/poll.h>
56#include <sys/kernel.h>
57#include <sys/vnode.h>
58#include <sys/signalvar.h>
59#include <sys/malloc.h>
60
61static MALLOC_DEFINE(M_PTY, "ptys", "pty data structures");
62
63static void ptsstart(struct tty *tp);
64static void ptsstop(struct tty *tp, int rw);
65static void ptcwakeup(struct tty *tp, int flag);
66static struct cdev *ptyinit(struct cdev *cdev);
67
68static d_open_t ptsopen;
69static d_close_t ptsclose;
70static d_read_t ptsread;
71static d_write_t ptswrite;
72static d_ioctl_t ptyioctl;
72static d_ioctl_t ptsioctl;
73static d_open_t ptcopen;
74static d_close_t ptcclose;
75static d_read_t ptcread;
73static d_open_t ptcopen;
74static d_close_t ptcclose;
75static d_read_t ptcread;
76static d_ioctl_t ptcioctl;
76static d_write_t ptcwrite;
77static d_poll_t ptcpoll;
78
79#define CDEV_MAJOR_S 5
80static struct cdevsw pts_cdevsw = {
81 .d_version = D_VERSION,
82 .d_open = ptsopen,
83 .d_close = ptsclose,
84 .d_read = ptsread,
85 .d_write = ptswrite,
77static d_write_t ptcwrite;
78static d_poll_t ptcpoll;
79
80#define CDEV_MAJOR_S 5
81static struct cdevsw pts_cdevsw = {
82 .d_version = D_VERSION,
83 .d_open = ptsopen,
84 .d_close = ptsclose,
85 .d_read = ptsread,
86 .d_write = ptswrite,
86 .d_ioctl = ptyioctl,
87 .d_ioctl = ptsioctl,
87 .d_name = "pts",
88 .d_maj = CDEV_MAJOR_S,
89 .d_flags = D_TTY | D_NEEDGIANT,
90};
91
92#define CDEV_MAJOR_C 6
93static struct cdevsw ptc_cdevsw = {
94 .d_version = D_VERSION,
95 .d_open = ptcopen,
96 .d_close = ptcclose,
97 .d_read = ptcread,
98 .d_write = ptcwrite,
88 .d_name = "pts",
89 .d_maj = CDEV_MAJOR_S,
90 .d_flags = D_TTY | D_NEEDGIANT,
91};
92
93#define CDEV_MAJOR_C 6
94static struct cdevsw ptc_cdevsw = {
95 .d_version = D_VERSION,
96 .d_open = ptcopen,
97 .d_close = ptcclose,
98 .d_read = ptcread,
99 .d_write = ptcwrite,
99 .d_ioctl = ptyioctl,
100 .d_ioctl = ptcioctl,
100 .d_poll = ptcpoll,
101 .d_name = "ptc",
102 .d_maj = CDEV_MAJOR_C,
103 .d_flags = D_TTY | D_NEEDGIANT,
104};
105
106#define BUFSIZ 100 /* Chunk size iomoved to/from user */
107
108struct ptsc {
109 int pt_flags;
110 struct selinfo pt_selr, pt_selw;
111 u_char pt_send;
112 u_char pt_ucntl;
113 struct tty *pt_tty;
114 struct cdev *devs, *devc;
115 struct prison *pt_prison;
116};
117
118#define PF_PKT 0x08 /* packet mode */
119#define PF_STOPPED 0x10 /* user told stopped */
120#define PF_NOSTOP 0x40
121#define PF_UCNTL 0x80 /* user control mode */
122
123#define TSA_PTC_READ(tp) ((void *)&(tp)->t_outq.c_cf)
124#define TSA_PTC_WRITE(tp) ((void *)&(tp)->t_rawq.c_cl)
125#define TSA_PTS_READ(tp) ((void *)&(tp)->t_canq)
126
127static char *names = "pqrsPQRS";
128/*
129 * This function creates and initializes a pts/ptc pair
130 *
131 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
132 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
133 *
134 * XXX: define and add mapping of upper minor bits to allow more
135 * than 256 ptys.
136 */
137static struct cdev *
138ptyinit(struct cdev *devc)
139{
140 struct cdev *devs;
141 struct ptsc *pt;
142 int n;
143
144 n = minor(devc);
145 /* For now we only map the lower 8 bits of the minor */
146 if (n & ~0xff)
147 return (NULL);
148
149 devc->si_flags &= ~SI_CHEAPCLONE;
150
151 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
152 pt->devs = devs = make_dev(&pts_cdevsw, n,
153 UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
154 pt->devc = devc;
155
156 pt->pt_tty = ttymalloc(pt->pt_tty);
157 pt->pt_tty->t_sc = pt;
158 devs->si_drv1 = devc->si_drv1 = pt;
159 devs->si_tty = devc->si_tty = pt->pt_tty;
160 pt->pt_tty->t_dev = devs;
161 return (devc);
162}
163
164/*ARGSUSED*/
165static int
166ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
167{
168 struct tty *tp;
169 int error;
170 struct ptsc *pt;
171
172 if (!dev->si_drv1)
173 return(ENXIO);
174 pt = dev->si_drv1;
175 tp = dev->si_tty;
176 if ((tp->t_state & TS_ISOPEN) == 0) {
177 ttychars(tp); /* Set up default chars */
178 tp->t_iflag = TTYDEF_IFLAG;
179 tp->t_oflag = TTYDEF_OFLAG;
180 tp->t_lflag = TTYDEF_LFLAG;
181 tp->t_cflag = TTYDEF_CFLAG;
182 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
183 } else if (tp->t_state & TS_XCLUDE && suser(td))
184 return (EBUSY);
185 else if (pt->pt_prison != td->td_ucred->cr_prison)
186 return (EBUSY);
187 if (tp->t_oproc) /* Ctrlr still around. */
188 (void)ttyld_modem(tp, 1);
189 while ((tp->t_state & TS_CARR_ON) == 0) {
190 if (flag&FNONBLOCK)
191 break;
192 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
193 "ptsopn", 0);
194 if (error)
195 return (error);
196 }
197 error = ttyld_open(tp, dev);
198 if (error == 0)
199 ptcwakeup(tp, FREAD|FWRITE);
200 return (error);
201}
202
203static int
204ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
205{
206 struct tty *tp;
207 int err;
208
209 tp = dev->si_tty;
210 err = ttyld_close(tp, flag);
211 (void) tty_close(tp);
212 return (err);
213}
214
215static int
216ptsread(struct cdev *dev, struct uio *uio, int flag)
217{
218 struct tty *tp = dev->si_tty;
219 int error = 0;
220
221 if (tp->t_oproc)
222 error = ttyld_read(tp, uio, flag);
223 ptcwakeup(tp, FWRITE);
224 return (error);
225}
226
227/*
228 * Write to pseudo-tty.
229 * Wakeups of controlling tty will happen
230 * indirectly, when tty driver calls ptsstart.
231 */
232static int
233ptswrite(struct cdev *dev, struct uio *uio, int flag)
234{
235 struct tty *tp;
236
237 tp = dev->si_tty;
238 if (tp->t_oproc == 0)
239 return (EIO);
240 return (ttyld_write(tp, uio, flag));
241}
242
243/*
244 * Start output on pseudo-tty.
245 * Wake up process selecting or sleeping for input from controlling tty.
246 */
247static void
248ptsstart(struct tty *tp)
249{
250 struct ptsc *pt = tp->t_sc;
251
252 if (tp->t_state & TS_TTSTOP)
253 return;
254 if (pt->pt_flags & PF_STOPPED) {
255 pt->pt_flags &= ~PF_STOPPED;
256 pt->pt_send = TIOCPKT_START;
257 }
258 ptcwakeup(tp, FREAD);
259}
260
261static void
262ptcwakeup(struct tty *tp, int flag)
263{
264 struct ptsc *pt = tp->t_sc;
265
266 if (flag & FREAD) {
267 selwakeuppri(&pt->pt_selr, TTIPRI);
268 wakeup(TSA_PTC_READ(tp));
269 }
270 if (flag & FWRITE) {
271 selwakeuppri(&pt->pt_selw, TTOPRI);
272 wakeup(TSA_PTC_WRITE(tp));
273 }
274}
275
276static int
277ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
278{
279 struct tty *tp;
280 struct ptsc *pt;
281
282 if (!dev->si_drv1)
283 ptyinit(dev);
284 if (!dev->si_drv1)
285 return(ENXIO);
286 tp = dev->si_tty;
287 if (tp->t_oproc)
288 return (EIO);
289 tp->t_timeout = -1;
290 tp->t_oproc = ptsstart;
291 tp->t_stop = ptsstop;
292 (void)ttyld_modem(tp, 1);
293 tp->t_lflag &= ~EXTPROC;
294 pt = dev->si_drv1;
295 pt->pt_prison = td->td_ucred->cr_prison;
296 pt->pt_flags = 0;
297 pt->pt_send = 0;
298 pt->pt_ucntl = 0;
299 return (0);
300}
301
302static int
303ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
304{
305 struct tty *tp;
306
307 tp = dev->si_tty;
308 (void)ttyld_modem(tp, 0);
309
310 /*
311 * XXX MDMBUF makes no sense for ptys but would inhibit the above
312 * l_modem(). CLOCAL makes sense but isn't supported. Special
313 * l_modem()s that ignore carrier drop make no sense for ptys but
314 * may be in use because other parts of the line discipline make
315 * sense for ptys. Recover by doing everything that a normal
316 * ttymodem() would have done except for sending a SIGHUP.
317 */
318 if (tp->t_state & TS_ISOPEN) {
319 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
320 tp->t_state |= TS_ZOMBIE;
321 ttyflush(tp, FREAD | FWRITE);
322 }
323
324 tp->t_oproc = 0; /* mark closed */
325 return (0);
326}
327
328static int
329ptcread(struct cdev *dev, struct uio *uio, int flag)
330{
331 struct tty *tp = dev->si_tty;
332 struct ptsc *pt = dev->si_drv1;
333 char buf[BUFSIZ];
334 int error = 0, cc;
335
336 /*
337 * We want to block until the slave
338 * is open, and there's something to read;
339 * but if we lost the slave or we're NBIO,
340 * then return the appropriate error instead.
341 */
342 for (;;) {
343 if (tp->t_state&TS_ISOPEN) {
344 if (pt->pt_flags&PF_PKT && pt->pt_send) {
345 error = ureadc((int)pt->pt_send, uio);
346 if (error)
347 return (error);
348 if (pt->pt_send & TIOCPKT_IOCTL) {
349 cc = min(uio->uio_resid,
350 sizeof(tp->t_termios));
351 uiomove(&tp->t_termios, cc, uio);
352 }
353 pt->pt_send = 0;
354 return (0);
355 }
356 if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
357 error = ureadc((int)pt->pt_ucntl, uio);
358 if (error)
359 return (error);
360 pt->pt_ucntl = 0;
361 return (0);
362 }
363 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
364 break;
365 }
366 if ((tp->t_state & TS_CONNECTED) == 0)
367 return (0); /* EOF */
368 if (flag & IO_NDELAY)
369 return (EWOULDBLOCK);
370 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
371 if (error)
372 return (error);
373 }
374 if (pt->pt_flags & (PF_PKT|PF_UCNTL))
375 error = ureadc(0, uio);
376 while (uio->uio_resid > 0 && error == 0) {
377 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
378 if (cc <= 0)
379 break;
380 error = uiomove(buf, cc, uio);
381 }
382 ttwwakeup(tp);
383 return (error);
384}
385
386static void
387ptsstop(struct tty *tp, int flush)
388{
389 struct ptsc *pt = tp->t_sc;
390 int flag;
391
392 /* note: FLUSHREAD and FLUSHWRITE already ok */
393 if (flush == 0) {
394 flush = TIOCPKT_STOP;
395 pt->pt_flags |= PF_STOPPED;
396 } else
397 pt->pt_flags &= ~PF_STOPPED;
398 pt->pt_send |= flush;
399 /* change of perspective */
400 flag = 0;
401 if (flush & FREAD)
402 flag |= FWRITE;
403 if (flush & FWRITE)
404 flag |= FREAD;
405 ptcwakeup(tp, flag);
406}
407
408static int
409ptcpoll(struct cdev *dev, int events, struct thread *td)
410{
411 struct tty *tp = dev->si_tty;
412 struct ptsc *pt = dev->si_drv1;
413 int revents = 0;
414 int s;
415
416 if ((tp->t_state & TS_CONNECTED) == 0)
417 return (events &
418 (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
419
420 /*
421 * Need to block timeouts (ttrstart).
422 */
423 s = spltty();
424
425 if (events & (POLLIN | POLLRDNORM))
426 if ((tp->t_state & TS_ISOPEN) &&
427 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
428 ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
429 ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
430 revents |= events & (POLLIN | POLLRDNORM);
431
432 if (events & (POLLOUT | POLLWRNORM))
433 if (tp->t_state & TS_ISOPEN &&
434 (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
435 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
436 revents |= events & (POLLOUT | POLLWRNORM);
437
438 if (events & POLLHUP)
439 if ((tp->t_state & TS_CARR_ON) == 0)
440 revents |= POLLHUP;
441
442 if (revents == 0) {
443 if (events & (POLLIN | POLLRDNORM))
444 selrecord(td, &pt->pt_selr);
445
446 if (events & (POLLOUT | POLLWRNORM))
447 selrecord(td, &pt->pt_selw);
448 }
449 splx(s);
450
451 return (revents);
452}
453
454static int
455ptcwrite(struct cdev *dev, struct uio *uio, int flag)
456{
457 struct tty *tp = dev->si_tty;
458 u_char *cp = 0;
459 int cc = 0;
460 u_char locbuf[BUFSIZ];
461 int cnt = 0;
462 int error = 0;
463
464again:
465 if ((tp->t_state&TS_ISOPEN) == 0)
466 goto block;
467 while (uio->uio_resid > 0 || cc > 0) {
468 if (cc == 0) {
469 cc = min(uio->uio_resid, BUFSIZ);
470 cp = locbuf;
471 error = uiomove(cp, cc, uio);
472 if (error)
473 return (error);
474 /* check again for safety */
475 if ((tp->t_state & TS_ISOPEN) == 0) {
476 /* adjust for data copied in but not written */
477 uio->uio_resid += cc;
478 return (EIO);
479 }
480 }
481 while (cc > 0) {
482 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
483 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
484 wakeup(TSA_HUP_OR_INPUT(tp));
485 goto block;
486 }
487 ttyld_rint(tp, *cp++);
488 cnt++;
489 cc--;
490 }
491 cc = 0;
492 }
493 return (0);
494block:
495 /*
496 * Come here to wait for slave to open, for space
497 * in outq, or space in rawq, or an empty canq.
498 */
499 if ((tp->t_state & TS_CONNECTED) == 0) {
500 /* adjust for data copied in but not written */
501 uio->uio_resid += cc;
502 return (EIO);
503 }
504 if (flag & IO_NDELAY) {
505 /* adjust for data copied in but not written */
506 uio->uio_resid += cc;
507 if (cnt == 0)
508 return (EWOULDBLOCK);
509 return (0);
510 }
511 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
512 if (error) {
513 /* adjust for data copied in but not written */
514 uio->uio_resid += cc;
515 return (error);
516 }
517 goto again;
518}
519
520/*ARGSUSED*/
521static int
101 .d_poll = ptcpoll,
102 .d_name = "ptc",
103 .d_maj = CDEV_MAJOR_C,
104 .d_flags = D_TTY | D_NEEDGIANT,
105};
106
107#define BUFSIZ 100 /* Chunk size iomoved to/from user */
108
109struct ptsc {
110 int pt_flags;
111 struct selinfo pt_selr, pt_selw;
112 u_char pt_send;
113 u_char pt_ucntl;
114 struct tty *pt_tty;
115 struct cdev *devs, *devc;
116 struct prison *pt_prison;
117};
118
119#define PF_PKT 0x08 /* packet mode */
120#define PF_STOPPED 0x10 /* user told stopped */
121#define PF_NOSTOP 0x40
122#define PF_UCNTL 0x80 /* user control mode */
123
124#define TSA_PTC_READ(tp) ((void *)&(tp)->t_outq.c_cf)
125#define TSA_PTC_WRITE(tp) ((void *)&(tp)->t_rawq.c_cl)
126#define TSA_PTS_READ(tp) ((void *)&(tp)->t_canq)
127
128static char *names = "pqrsPQRS";
129/*
130 * This function creates and initializes a pts/ptc pair
131 *
132 * pts == /dev/tty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
133 * ptc == /dev/pty[pqrsPQRS][0123456789abcdefghijklmnopqrstuv]
134 *
135 * XXX: define and add mapping of upper minor bits to allow more
136 * than 256 ptys.
137 */
138static struct cdev *
139ptyinit(struct cdev *devc)
140{
141 struct cdev *devs;
142 struct ptsc *pt;
143 int n;
144
145 n = minor(devc);
146 /* For now we only map the lower 8 bits of the minor */
147 if (n & ~0xff)
148 return (NULL);
149
150 devc->si_flags &= ~SI_CHEAPCLONE;
151
152 pt = malloc(sizeof(*pt), M_PTY, M_WAITOK | M_ZERO);
153 pt->devs = devs = make_dev(&pts_cdevsw, n,
154 UID_ROOT, GID_WHEEL, 0666, "tty%c%r", names[n / 32], n % 32);
155 pt->devc = devc;
156
157 pt->pt_tty = ttymalloc(pt->pt_tty);
158 pt->pt_tty->t_sc = pt;
159 devs->si_drv1 = devc->si_drv1 = pt;
160 devs->si_tty = devc->si_tty = pt->pt_tty;
161 pt->pt_tty->t_dev = devs;
162 return (devc);
163}
164
165/*ARGSUSED*/
166static int
167ptsopen(struct cdev *dev, int flag, int devtype, struct thread *td)
168{
169 struct tty *tp;
170 int error;
171 struct ptsc *pt;
172
173 if (!dev->si_drv1)
174 return(ENXIO);
175 pt = dev->si_drv1;
176 tp = dev->si_tty;
177 if ((tp->t_state & TS_ISOPEN) == 0) {
178 ttychars(tp); /* Set up default chars */
179 tp->t_iflag = TTYDEF_IFLAG;
180 tp->t_oflag = TTYDEF_OFLAG;
181 tp->t_lflag = TTYDEF_LFLAG;
182 tp->t_cflag = TTYDEF_CFLAG;
183 tp->t_ispeed = tp->t_ospeed = TTYDEF_SPEED;
184 } else if (tp->t_state & TS_XCLUDE && suser(td))
185 return (EBUSY);
186 else if (pt->pt_prison != td->td_ucred->cr_prison)
187 return (EBUSY);
188 if (tp->t_oproc) /* Ctrlr still around. */
189 (void)ttyld_modem(tp, 1);
190 while ((tp->t_state & TS_CARR_ON) == 0) {
191 if (flag&FNONBLOCK)
192 break;
193 error = ttysleep(tp, TSA_CARR_ON(tp), TTIPRI | PCATCH,
194 "ptsopn", 0);
195 if (error)
196 return (error);
197 }
198 error = ttyld_open(tp, dev);
199 if (error == 0)
200 ptcwakeup(tp, FREAD|FWRITE);
201 return (error);
202}
203
204static int
205ptsclose(struct cdev *dev, int flag, int mode, struct thread *td)
206{
207 struct tty *tp;
208 int err;
209
210 tp = dev->si_tty;
211 err = ttyld_close(tp, flag);
212 (void) tty_close(tp);
213 return (err);
214}
215
216static int
217ptsread(struct cdev *dev, struct uio *uio, int flag)
218{
219 struct tty *tp = dev->si_tty;
220 int error = 0;
221
222 if (tp->t_oproc)
223 error = ttyld_read(tp, uio, flag);
224 ptcwakeup(tp, FWRITE);
225 return (error);
226}
227
228/*
229 * Write to pseudo-tty.
230 * Wakeups of controlling tty will happen
231 * indirectly, when tty driver calls ptsstart.
232 */
233static int
234ptswrite(struct cdev *dev, struct uio *uio, int flag)
235{
236 struct tty *tp;
237
238 tp = dev->si_tty;
239 if (tp->t_oproc == 0)
240 return (EIO);
241 return (ttyld_write(tp, uio, flag));
242}
243
244/*
245 * Start output on pseudo-tty.
246 * Wake up process selecting or sleeping for input from controlling tty.
247 */
248static void
249ptsstart(struct tty *tp)
250{
251 struct ptsc *pt = tp->t_sc;
252
253 if (tp->t_state & TS_TTSTOP)
254 return;
255 if (pt->pt_flags & PF_STOPPED) {
256 pt->pt_flags &= ~PF_STOPPED;
257 pt->pt_send = TIOCPKT_START;
258 }
259 ptcwakeup(tp, FREAD);
260}
261
262static void
263ptcwakeup(struct tty *tp, int flag)
264{
265 struct ptsc *pt = tp->t_sc;
266
267 if (flag & FREAD) {
268 selwakeuppri(&pt->pt_selr, TTIPRI);
269 wakeup(TSA_PTC_READ(tp));
270 }
271 if (flag & FWRITE) {
272 selwakeuppri(&pt->pt_selw, TTOPRI);
273 wakeup(TSA_PTC_WRITE(tp));
274 }
275}
276
277static int
278ptcopen(struct cdev *dev, int flag, int devtype, struct thread *td)
279{
280 struct tty *tp;
281 struct ptsc *pt;
282
283 if (!dev->si_drv1)
284 ptyinit(dev);
285 if (!dev->si_drv1)
286 return(ENXIO);
287 tp = dev->si_tty;
288 if (tp->t_oproc)
289 return (EIO);
290 tp->t_timeout = -1;
291 tp->t_oproc = ptsstart;
292 tp->t_stop = ptsstop;
293 (void)ttyld_modem(tp, 1);
294 tp->t_lflag &= ~EXTPROC;
295 pt = dev->si_drv1;
296 pt->pt_prison = td->td_ucred->cr_prison;
297 pt->pt_flags = 0;
298 pt->pt_send = 0;
299 pt->pt_ucntl = 0;
300 return (0);
301}
302
303static int
304ptcclose(struct cdev *dev, int flags, int fmt, struct thread *td)
305{
306 struct tty *tp;
307
308 tp = dev->si_tty;
309 (void)ttyld_modem(tp, 0);
310
311 /*
312 * XXX MDMBUF makes no sense for ptys but would inhibit the above
313 * l_modem(). CLOCAL makes sense but isn't supported. Special
314 * l_modem()s that ignore carrier drop make no sense for ptys but
315 * may be in use because other parts of the line discipline make
316 * sense for ptys. Recover by doing everything that a normal
317 * ttymodem() would have done except for sending a SIGHUP.
318 */
319 if (tp->t_state & TS_ISOPEN) {
320 tp->t_state &= ~(TS_CARR_ON | TS_CONNECTED);
321 tp->t_state |= TS_ZOMBIE;
322 ttyflush(tp, FREAD | FWRITE);
323 }
324
325 tp->t_oproc = 0; /* mark closed */
326 return (0);
327}
328
329static int
330ptcread(struct cdev *dev, struct uio *uio, int flag)
331{
332 struct tty *tp = dev->si_tty;
333 struct ptsc *pt = dev->si_drv1;
334 char buf[BUFSIZ];
335 int error = 0, cc;
336
337 /*
338 * We want to block until the slave
339 * is open, and there's something to read;
340 * but if we lost the slave or we're NBIO,
341 * then return the appropriate error instead.
342 */
343 for (;;) {
344 if (tp->t_state&TS_ISOPEN) {
345 if (pt->pt_flags&PF_PKT && pt->pt_send) {
346 error = ureadc((int)pt->pt_send, uio);
347 if (error)
348 return (error);
349 if (pt->pt_send & TIOCPKT_IOCTL) {
350 cc = min(uio->uio_resid,
351 sizeof(tp->t_termios));
352 uiomove(&tp->t_termios, cc, uio);
353 }
354 pt->pt_send = 0;
355 return (0);
356 }
357 if (pt->pt_flags&PF_UCNTL && pt->pt_ucntl) {
358 error = ureadc((int)pt->pt_ucntl, uio);
359 if (error)
360 return (error);
361 pt->pt_ucntl = 0;
362 return (0);
363 }
364 if (tp->t_outq.c_cc && (tp->t_state&TS_TTSTOP) == 0)
365 break;
366 }
367 if ((tp->t_state & TS_CONNECTED) == 0)
368 return (0); /* EOF */
369 if (flag & IO_NDELAY)
370 return (EWOULDBLOCK);
371 error = tsleep(TSA_PTC_READ(tp), TTIPRI | PCATCH, "ptcin", 0);
372 if (error)
373 return (error);
374 }
375 if (pt->pt_flags & (PF_PKT|PF_UCNTL))
376 error = ureadc(0, uio);
377 while (uio->uio_resid > 0 && error == 0) {
378 cc = q_to_b(&tp->t_outq, buf, min(uio->uio_resid, BUFSIZ));
379 if (cc <= 0)
380 break;
381 error = uiomove(buf, cc, uio);
382 }
383 ttwwakeup(tp);
384 return (error);
385}
386
387static void
388ptsstop(struct tty *tp, int flush)
389{
390 struct ptsc *pt = tp->t_sc;
391 int flag;
392
393 /* note: FLUSHREAD and FLUSHWRITE already ok */
394 if (flush == 0) {
395 flush = TIOCPKT_STOP;
396 pt->pt_flags |= PF_STOPPED;
397 } else
398 pt->pt_flags &= ~PF_STOPPED;
399 pt->pt_send |= flush;
400 /* change of perspective */
401 flag = 0;
402 if (flush & FREAD)
403 flag |= FWRITE;
404 if (flush & FWRITE)
405 flag |= FREAD;
406 ptcwakeup(tp, flag);
407}
408
409static int
410ptcpoll(struct cdev *dev, int events, struct thread *td)
411{
412 struct tty *tp = dev->si_tty;
413 struct ptsc *pt = dev->si_drv1;
414 int revents = 0;
415 int s;
416
417 if ((tp->t_state & TS_CONNECTED) == 0)
418 return (events &
419 (POLLHUP | POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM));
420
421 /*
422 * Need to block timeouts (ttrstart).
423 */
424 s = spltty();
425
426 if (events & (POLLIN | POLLRDNORM))
427 if ((tp->t_state & TS_ISOPEN) &&
428 ((tp->t_outq.c_cc && (tp->t_state & TS_TTSTOP) == 0) ||
429 ((pt->pt_flags & PF_PKT) && pt->pt_send) ||
430 ((pt->pt_flags & PF_UCNTL) && pt->pt_ucntl)))
431 revents |= events & (POLLIN | POLLRDNORM);
432
433 if (events & (POLLOUT | POLLWRNORM))
434 if (tp->t_state & TS_ISOPEN &&
435 (((tp->t_rawq.c_cc + tp->t_canq.c_cc < TTYHOG - 2) ||
436 (tp->t_canq.c_cc == 0 && (tp->t_lflag & ICANON)))))
437 revents |= events & (POLLOUT | POLLWRNORM);
438
439 if (events & POLLHUP)
440 if ((tp->t_state & TS_CARR_ON) == 0)
441 revents |= POLLHUP;
442
443 if (revents == 0) {
444 if (events & (POLLIN | POLLRDNORM))
445 selrecord(td, &pt->pt_selr);
446
447 if (events & (POLLOUT | POLLWRNORM))
448 selrecord(td, &pt->pt_selw);
449 }
450 splx(s);
451
452 return (revents);
453}
454
455static int
456ptcwrite(struct cdev *dev, struct uio *uio, int flag)
457{
458 struct tty *tp = dev->si_tty;
459 u_char *cp = 0;
460 int cc = 0;
461 u_char locbuf[BUFSIZ];
462 int cnt = 0;
463 int error = 0;
464
465again:
466 if ((tp->t_state&TS_ISOPEN) == 0)
467 goto block;
468 while (uio->uio_resid > 0 || cc > 0) {
469 if (cc == 0) {
470 cc = min(uio->uio_resid, BUFSIZ);
471 cp = locbuf;
472 error = uiomove(cp, cc, uio);
473 if (error)
474 return (error);
475 /* check again for safety */
476 if ((tp->t_state & TS_ISOPEN) == 0) {
477 /* adjust for data copied in but not written */
478 uio->uio_resid += cc;
479 return (EIO);
480 }
481 }
482 while (cc > 0) {
483 if ((tp->t_rawq.c_cc + tp->t_canq.c_cc) >= TTYHOG - 2 &&
484 (tp->t_canq.c_cc > 0 || !(tp->t_lflag&ICANON))) {
485 wakeup(TSA_HUP_OR_INPUT(tp));
486 goto block;
487 }
488 ttyld_rint(tp, *cp++);
489 cnt++;
490 cc--;
491 }
492 cc = 0;
493 }
494 return (0);
495block:
496 /*
497 * Come here to wait for slave to open, for space
498 * in outq, or space in rawq, or an empty canq.
499 */
500 if ((tp->t_state & TS_CONNECTED) == 0) {
501 /* adjust for data copied in but not written */
502 uio->uio_resid += cc;
503 return (EIO);
504 }
505 if (flag & IO_NDELAY) {
506 /* adjust for data copied in but not written */
507 uio->uio_resid += cc;
508 if (cnt == 0)
509 return (EWOULDBLOCK);
510 return (0);
511 }
512 error = tsleep(TSA_PTC_WRITE(tp), TTOPRI | PCATCH, "ptcout", 0);
513 if (error) {
514 /* adjust for data copied in but not written */
515 uio->uio_resid += cc;
516 return (error);
517 }
518 goto again;
519}
520
521/*ARGSUSED*/
522static int
522ptyioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
523ptcioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
523{
524 struct tty *tp = dev->si_tty;
525 struct ptsc *pt = dev->si_drv1;
524{
525 struct tty *tp = dev->si_tty;
526 struct ptsc *pt = dev->si_drv1;
526 u_char *cc = tp->t_cc;
527 int stop, error;
528
527
529 if (devsw(dev)->d_open == ptcopen) {
530 switch (cmd) {
528 switch (cmd) {
531
529
532 case TIOCGPGRP:
533 /*
534 * We avoid calling ttioctl on the controller since,
535 * in that case, tp must be the controlling terminal.
536 */
537 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
538 return (0);
530 case TIOCGPGRP:
531 /*
532 * We avoid calling ttioctl on the controller since,
533 * in that case, tp must be the controlling terminal.
534 */
535 *(int *)data = tp->t_pgrp ? tp->t_pgrp->pg_id : 0;
536 return (0);
539
537
540 case TIOCPKT:
541 if (*(int *)data) {
542 if (pt->pt_flags & PF_UCNTL)
543 return (EINVAL);
544 pt->pt_flags |= PF_PKT;
545 } else
546 pt->pt_flags &= ~PF_PKT;
547 return (0);
538 case TIOCPKT:
539 if (*(int *)data) {
540 if (pt->pt_flags & PF_UCNTL)
541 return (EINVAL);
542 pt->pt_flags |= PF_PKT;
543 } else
544 pt->pt_flags &= ~PF_PKT;
545 return (0);
548
546
549 case TIOCUCNTL:
550 if (*(int *)data) {
551 if (pt->pt_flags & PF_PKT)
552 return (EINVAL);
553 pt->pt_flags |= PF_UCNTL;
554 } else
555 pt->pt_flags &= ~PF_UCNTL;
556 return (0);
557 }
547 case TIOCUCNTL:
548 if (*(int *)data) {
549 if (pt->pt_flags & PF_PKT)
550 return (EINVAL);
551 pt->pt_flags |= PF_UCNTL;
552 } else
553 pt->pt_flags &= ~PF_UCNTL;
554 return (0);
555 }
558
556
559 /*
560 * The rest of the ioctls shouldn't be called until
561 * the slave is open.
562 */
563 if ((tp->t_state & TS_ISOPEN) == 0)
564 return (EAGAIN);
557 /*
558 * The rest of the ioctls shouldn't be called until
559 * the slave is open.
560 */
561 if ((tp->t_state & TS_ISOPEN) == 0)
562 return (EAGAIN);
565
563
566 switch (cmd) {
564 switch (cmd) {
567#ifndef BURN_BRIDGES
568#ifdef COMPAT_43
565#ifndef BURN_BRIDGES
566#ifdef COMPAT_43
569 case TIOCSETP:
570 case TIOCSETN:
567 case TIOCSETP:
568 case TIOCSETN:
571#endif
572#endif
569#endif
570#endif
573 case TIOCSETD:
574 case TIOCSETA:
575 case TIOCSETAW:
576 case TIOCSETAF:
577 /*
578 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
579 * ttywflush(tp) will hang if there are characters in
580 * the outq.
581 */
582 ndflush(&tp->t_outq, tp->t_outq.c_cc);
583 break;
571 case TIOCSETD:
572 case TIOCSETA:
573 case TIOCSETAW:
574 case TIOCSETAF:
575 /*
576 * IF CONTROLLER STTY THEN MUST FLUSH TO PREVENT A HANG.
577 * ttywflush(tp) will hang if there are characters in
578 * the outq.
579 */
580 ndflush(&tp->t_outq, tp->t_outq.c_cc);
581 break;
584
582
585 case TIOCSIG:
586 if (*(unsigned int *)data >= NSIG ||
587 *(unsigned int *)data == 0)
588 return(EINVAL);
589 if ((tp->t_lflag&NOFLSH) == 0)
590 ttyflush(tp, FREAD|FWRITE);
591 if (tp->t_pgrp != NULL) {
592 PGRP_LOCK(tp->t_pgrp);
593 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
594 PGRP_UNLOCK(tp->t_pgrp);
595 }
596 if ((*(unsigned int *)data == SIGINFO) &&
597 ((tp->t_lflag&NOKERNINFO) == 0))
598 ttyinfo(tp);
599 return(0);
583 case TIOCSIG:
584 if (*(unsigned int *)data >= NSIG ||
585 *(unsigned int *)data == 0)
586 return(EINVAL);
587 if ((tp->t_lflag&NOFLSH) == 0)
588 ttyflush(tp, FREAD|FWRITE);
589 if (tp->t_pgrp != NULL) {
590 PGRP_LOCK(tp->t_pgrp);
591 pgsignal(tp->t_pgrp, *(unsigned int *)data, 1);
592 PGRP_UNLOCK(tp->t_pgrp);
600 }
593 }
594 if ((*(unsigned int *)data == SIGINFO) &&
595 ((tp->t_lflag&NOKERNINFO) == 0))
596 ttyinfo(tp);
597 return(0);
601 }
598 }
599
600 return (ptsioctl(dev, cmd, data, flag, td));
601}
602
603/*ARGSUSED*/
604static int
605ptsioctl(struct cdev *dev, u_long cmd, caddr_t data, int flag, struct thread *td)
606{
607 struct tty *tp = dev->si_tty;
608 struct ptsc *pt = dev->si_drv1;
609 u_char *cc = tp->t_cc;
610 int stop, error;
611
602 if (cmd == TIOCEXT) {
603 /*
604 * When the EXTPROC bit is being toggled, we need
605 * to send an TIOCPKT_IOCTL if the packet driver
606 * is turned on.
607 */
608 if (*(int *)data) {
609 if (pt->pt_flags & PF_PKT) {
610 pt->pt_send |= TIOCPKT_IOCTL;
611 ptcwakeup(tp, FREAD);
612 }
613 tp->t_lflag |= EXTPROC;
614 } else {
615 if ((tp->t_lflag & EXTPROC) &&
616 (pt->pt_flags & PF_PKT)) {
617 pt->pt_send |= TIOCPKT_IOCTL;
618 ptcwakeup(tp, FREAD);
619 }
620 tp->t_lflag &= ~EXTPROC;
621 }
622 return(0);
623 }
624 error = ttyioctl(dev, cmd, data, flag, td);
625 if (error == ENOTTY) {
626 if (pt->pt_flags & PF_UCNTL &&
627 (cmd & ~0xff) == UIOCCMD(0)) {
628 if (cmd & 0xff) {
629 pt->pt_ucntl = (u_char)cmd;
630 ptcwakeup(tp, FREAD);
631 }
632 return (0);
633 }
634 error = ENOTTY;
635 }
636 /*
637 * If external processing and packet mode send ioctl packet.
638 */
639 if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
640 switch(cmd) {
641 case TIOCSETA:
642 case TIOCSETAW:
643 case TIOCSETAF:
644#ifndef BURN_BRIDGES
645#ifdef COMPAT_43
646 case TIOCSETP:
647 case TIOCSETN:
648 case TIOCSETC:
649 case TIOCSLTC:
650 case TIOCLBIS:
651 case TIOCLBIC:
652 case TIOCLSET:
653#endif
654#endif
655 pt->pt_send |= TIOCPKT_IOCTL;
656 ptcwakeup(tp, FREAD);
657 break;
658 default:
659 break;
660 }
661 }
662 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
663 && CCEQ(cc[VSTART], CTRL('q'));
664 if (pt->pt_flags & PF_NOSTOP) {
665 if (stop) {
666 pt->pt_send &= ~TIOCPKT_NOSTOP;
667 pt->pt_send |= TIOCPKT_DOSTOP;
668 pt->pt_flags &= ~PF_NOSTOP;
669 ptcwakeup(tp, FREAD);
670 }
671 } else {
672 if (!stop) {
673 pt->pt_send &= ~TIOCPKT_DOSTOP;
674 pt->pt_send |= TIOCPKT_NOSTOP;
675 pt->pt_flags |= PF_NOSTOP;
676 ptcwakeup(tp, FREAD);
677 }
678 }
679 return (error);
680}
681
682static void
683pty_clone(void *arg, char *name, int namelen, struct cdev **dev)
684{
685 int u;
686
687 if (*dev != NULL)
688 return;
689 if (bcmp(name, "pty", 3) != 0)
690 return;
691 if (name[5] != '\0')
692 return;
693 switch (name[3]) {
694 case 'p': u = 0; break;
695 case 'q': u = 32; break;
696 case 'r': u = 64; break;
697 case 's': u = 96; break;
698 case 'P': u = 128; break;
699 case 'Q': u = 160; break;
700 case 'R': u = 192; break;
701 case 'S': u = 224; break;
702 default: return;
703 }
704 if (name[4] >= '0' && name[4] <= '9')
705 u += name[4] - '0';
706 else if (name[4] >= 'a' && name[4] <= 'v')
707 u += name[4] - 'a' + 10;
708 else
709 return;
710 *dev = make_dev(&ptc_cdevsw, u,
711 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
712 (*dev)->si_flags |= SI_CHEAPCLONE;
713 return;
714}
715
716static void
717ptc_drvinit(void *unused)
718{
719
720 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
721}
722
723SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)
612 if (cmd == TIOCEXT) {
613 /*
614 * When the EXTPROC bit is being toggled, we need
615 * to send an TIOCPKT_IOCTL if the packet driver
616 * is turned on.
617 */
618 if (*(int *)data) {
619 if (pt->pt_flags & PF_PKT) {
620 pt->pt_send |= TIOCPKT_IOCTL;
621 ptcwakeup(tp, FREAD);
622 }
623 tp->t_lflag |= EXTPROC;
624 } else {
625 if ((tp->t_lflag & EXTPROC) &&
626 (pt->pt_flags & PF_PKT)) {
627 pt->pt_send |= TIOCPKT_IOCTL;
628 ptcwakeup(tp, FREAD);
629 }
630 tp->t_lflag &= ~EXTPROC;
631 }
632 return(0);
633 }
634 error = ttyioctl(dev, cmd, data, flag, td);
635 if (error == ENOTTY) {
636 if (pt->pt_flags & PF_UCNTL &&
637 (cmd & ~0xff) == UIOCCMD(0)) {
638 if (cmd & 0xff) {
639 pt->pt_ucntl = (u_char)cmd;
640 ptcwakeup(tp, FREAD);
641 }
642 return (0);
643 }
644 error = ENOTTY;
645 }
646 /*
647 * If external processing and packet mode send ioctl packet.
648 */
649 if ((tp->t_lflag&EXTPROC) && (pt->pt_flags & PF_PKT)) {
650 switch(cmd) {
651 case TIOCSETA:
652 case TIOCSETAW:
653 case TIOCSETAF:
654#ifndef BURN_BRIDGES
655#ifdef COMPAT_43
656 case TIOCSETP:
657 case TIOCSETN:
658 case TIOCSETC:
659 case TIOCSLTC:
660 case TIOCLBIS:
661 case TIOCLBIC:
662 case TIOCLSET:
663#endif
664#endif
665 pt->pt_send |= TIOCPKT_IOCTL;
666 ptcwakeup(tp, FREAD);
667 break;
668 default:
669 break;
670 }
671 }
672 stop = (tp->t_iflag & IXON) && CCEQ(cc[VSTOP], CTRL('s'))
673 && CCEQ(cc[VSTART], CTRL('q'));
674 if (pt->pt_flags & PF_NOSTOP) {
675 if (stop) {
676 pt->pt_send &= ~TIOCPKT_NOSTOP;
677 pt->pt_send |= TIOCPKT_DOSTOP;
678 pt->pt_flags &= ~PF_NOSTOP;
679 ptcwakeup(tp, FREAD);
680 }
681 } else {
682 if (!stop) {
683 pt->pt_send &= ~TIOCPKT_DOSTOP;
684 pt->pt_send |= TIOCPKT_NOSTOP;
685 pt->pt_flags |= PF_NOSTOP;
686 ptcwakeup(tp, FREAD);
687 }
688 }
689 return (error);
690}
691
692static void
693pty_clone(void *arg, char *name, int namelen, struct cdev **dev)
694{
695 int u;
696
697 if (*dev != NULL)
698 return;
699 if (bcmp(name, "pty", 3) != 0)
700 return;
701 if (name[5] != '\0')
702 return;
703 switch (name[3]) {
704 case 'p': u = 0; break;
705 case 'q': u = 32; break;
706 case 'r': u = 64; break;
707 case 's': u = 96; break;
708 case 'P': u = 128; break;
709 case 'Q': u = 160; break;
710 case 'R': u = 192; break;
711 case 'S': u = 224; break;
712 default: return;
713 }
714 if (name[4] >= '0' && name[4] <= '9')
715 u += name[4] - '0';
716 else if (name[4] >= 'a' && name[4] <= 'v')
717 u += name[4] - 'a' + 10;
718 else
719 return;
720 *dev = make_dev(&ptc_cdevsw, u,
721 UID_ROOT, GID_WHEEL, 0666, "pty%c%r", names[u / 32], u % 32);
722 (*dev)->si_flags |= SI_CHEAPCLONE;
723 return;
724}
725
726static void
727ptc_drvinit(void *unused)
728{
729
730 EVENTHANDLER_REGISTER(dev_clone, pty_clone, 0, 1000);
731}
732
733SYSINIT(ptcdev,SI_SUB_DRIVERS,SI_ORDER_MIDDLE+CDEV_MAJOR_C,ptc_drvinit,NULL)