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