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