Print this page
14249 pseudo-terminal nomenclature should reflect POSIX
Change-Id: Ib4a3cef899ff4c71b09cb0dc6878863c5e8357bc
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/tty_pty.c
+++ new/usr/src/uts/common/io/tty_pty.c
1 1 /*
2 2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
3 3 * Use is subject to license terms.
↓ open down ↓ |
3 lines elided |
↑ open up ↑ |
4 4 * Copyright 2015, Joyent, Inc.
5 5 */
6 6
7 7 /*
8 8 * Copyright (c) 1983 Regents of the University of California.
9 9 * All rights reserved. The Berkeley software License Agreement
10 10 * specifies the terms and conditions for redistribution.
11 11 */
12 12
13 13 /*
14 - * PTY - Stream "pseudo-tty" device. For each "controller" side
15 - * it connects to a "slave" side.
14 + * PTY - Stream "pseudo-terminal" device. For each "manager" side it connects
15 + * to a "subsidiary" side.
16 16 */
17 17
18 18
19 19 #include <sys/param.h>
20 20 #include <sys/systm.h>
21 21 #include <sys/filio.h>
22 22 #include <sys/ioccom.h>
23 23 #include <sys/termios.h>
24 24 #include <sys/termio.h>
25 25 #include <sys/ttold.h>
26 26 #include <sys/stropts.h>
27 27 #include <sys/stream.h>
28 28 #include <sys/tty.h>
29 29 #include <sys/user.h>
30 30 #include <sys/conf.h>
31 31 #include <sys/file.h>
32 -#include <sys/vnode.h> /* 1/0 on the vomit meter */
32 +#include <sys/vnode.h>
33 33 #include <sys/proc.h>
34 34 #include <sys/uio.h>
35 35 #include <sys/errno.h>
36 36 #include <sys/strsubr.h>
37 37 #include <sys/poll.h>
38 38 #include <sys/sysmacros.h>
39 39 #include <sys/debug.h>
40 40 #include <sys/procset.h>
41 41 #include <sys/cred.h>
42 42 #include <sys/ptyvar.h>
43 43 #include <sys/suntty.h>
44 44 #include <sys/stat.h>
45 45
46 46 #include <sys/conf.h>
47 47 #include <sys/ddi.h>
48 48 #include <sys/sunddi.h>
49 49
50 50 extern int npty; /* number of pseudo-ttys configured in */
51 51 extern struct pty *pty_softc;
52 52 extern struct pollhead ptcph; /* poll head for ptcpoll() use */
53 53
54 54 int ptcopen(dev_t *, int, int, struct cred *);
55 55 int ptcclose(dev_t, int, int, struct cred *);
56 56 int ptcwrite(dev_t, struct uio *, struct cred *);
57 57 int ptcread(dev_t, struct uio *, struct cred *);
58 58 int ptcioctl(dev_t, int, intptr_t, int, struct cred *, int *);
59 59 int ptcpoll(dev_t, short, int, short *, struct pollhead **);
60 60
61 61 static int ptc_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
62 62 static int ptc_attach(dev_info_t *, ddi_attach_cmd_t);
63 63 static dev_info_t *ptc_dip; /* for dev-to-dip conversions */
64 64
65 65 static void ptc_init(void), ptc_uninit(void);
66 66
67 67 static int makemsg(ssize_t count, struct uio *uiop,
68 68 struct pty *pty, mblk_t **mpp);
69 69
70 70 struct cb_ops ptc_cb_ops = {
71 71 ptcopen, /* open */
72 72 ptcclose, /* close */
73 73 nodev, /* strategy */
74 74 nodev, /* print */
75 75 nodev, /* dump */
76 76 ptcread, /* read */
77 77 ptcwrite, /* write */
78 78 ptcioctl, /* ioctl */
79 79 nodev, /* devmap */
80 80 nodev, /* mmap */
81 81 nodev, /* segmap */
82 82 ptcpoll, /* poll */
83 83 ddi_prop_op, /* prop_op */
84 84 0, /* streamtab */
85 85 D_NEW | D_MP /* Driver compatibility flag */
86 86 };
87 87
88 88 struct dev_ops ptc_ops = {
89 89 DEVO_REV, /* devo_rev */
90 90 0, /* refcnt */
91 91 ptc_info, /* info */
92 92 nulldev, /* identify */
93 93 nulldev, /* probe */
94 94 ptc_attach, /* attach */
95 95 nodev, /* detach */
96 96 nodev, /* reset */
97 97 &ptc_cb_ops, /* driver operations */
98 98 (struct bus_ops *)0, /* bus operations */
99 99 NULL, /* power */
100 100 ddi_quiesce_not_supported, /* devo_quiesce */
101 101 };
102 102
103 103 #include <sys/types.h>
104 104 #include <sys/conf.h>
105 105 #include <sys/param.h>
106 106 #include <sys/systm.h>
107 107 #include <sys/errno.h>
108 108 #include <sys/modctl.h>
↓ open down ↓ |
66 lines elided |
↑ open up ↑ |
109 109
110 110 extern int dseekneg_flag;
111 111 extern struct mod_ops mod_driverops;
112 112 extern struct dev_ops ptc_ops;
113 113
114 114 /*
115 115 * Module linkage information for the kernel.
116 116 */
117 117
118 118 static struct modldrv modldrv = {
119 - &mod_driverops, /* Type of module. This one is a pseudo driver */
119 + &mod_driverops,
120 120 "tty pseudo driver control 'ptc'",
121 - &ptc_ops, /* driver ops */
121 + &ptc_ops,
122 122 };
123 123
124 124 static struct modlinkage modlinkage = {
125 125 MODREV_1,
126 126 &modldrv,
127 127 NULL
128 128 };
129 129
130 130 int
131 131 _init()
132 132 {
133 133 int rc;
134 134
135 135 if ((rc = mod_install(&modlinkage)) == 0)
136 136 ptc_init();
137 137 return (rc);
138 138 }
139 139
140 140
141 141 int
142 142 _fini()
143 143 {
144 144 int rc;
145 145
146 146 if ((rc = mod_remove(&modlinkage)) == 0)
147 147 ptc_uninit();
148 148 return (rc);
149 149 }
150 150
151 151 int
152 152 _info(struct modinfo *modinfop)
153 153 {
154 154 return (mod_info(&modlinkage, modinfop));
155 155 }
156 156
157 157 static char *pty_banks = PTY_BANKS;
158 158 static char *pty_digits = PTY_DIGITS;
159 159
160 160 /* ARGSUSED */
161 161 static int
162 162 ptc_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
163 163 {
164 164 char name[8];
165 165 int pty_num;
166 166 char *pty_digit = pty_digits;
167 167 char *pty_bank = pty_banks;
168 168
169 169 for (pty_num = 0; pty_num < npty; pty_num++) {
170 170 (void) sprintf(name, "pty%c%c", *pty_bank, *pty_digit);
171 171 if (ddi_create_minor_node(devi, name, S_IFCHR,
172 172 pty_num, DDI_PSEUDO, 0) == DDI_FAILURE) {
173 173 ddi_remove_minor_node(devi, NULL);
174 174 return (-1);
175 175 }
176 176 if (*(++pty_digit) == '\0') {
177 177 pty_digit = pty_digits;
178 178 if (*(++pty_bank) == '\0')
179 179 break;
180 180 }
181 181 }
182 182 ptc_dip = devi;
183 183 return (DDI_SUCCESS);
184 184 }
185 185
186 186 /* ARGSUSED */
187 187 static int
188 188 ptc_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg, void **result)
189 189 {
190 190 int error;
191 191
192 192 switch (infocmd) {
193 193 case DDI_INFO_DEVT2DEVINFO:
194 194 if (ptc_dip == NULL) {
195 195 *result = (void *)NULL;
196 196 error = DDI_FAILURE;
197 197 } else {
198 198 *result = (void *) ptc_dip;
199 199 error = DDI_SUCCESS;
200 200 }
201 201 break;
202 202 case DDI_INFO_DEVT2INSTANCE:
203 203 *result = (void *)0;
204 204 error = DDI_SUCCESS;
205 205 break;
206 206 default:
207 207 error = DDI_FAILURE;
208 208 }
209 209 return (error);
210 210 }
211 211
212 212 static void
213 213 ptc_init(void)
214 214 {
215 215 minor_t dev;
216 216
217 217 for (dev = 0; dev < npty; dev++) {
218 218 cv_init(&pty_softc[dev].pt_cv_flags, NULL, CV_DEFAULT, NULL);
219 219 cv_init(&pty_softc[dev].pt_cv_readq, NULL, CV_DEFAULT, NULL);
220 220 cv_init(&pty_softc[dev].pt_cv_writeq, NULL, CV_DEFAULT, NULL);
221 221 mutex_init(&pty_softc[dev].ptc_lock, NULL, MUTEX_DEFAULT, NULL);
222 222 }
223 223 }
224 224
225 225 static void
226 226 ptc_uninit(void)
227 227 {
228 228 minor_t dev;
↓ open down ↓ |
97 lines elided |
↑ open up ↑ |
229 229
230 230 for (dev = 0; dev < npty; dev++) {
231 231 cv_destroy(&pty_softc[dev].pt_cv_flags);
232 232 cv_destroy(&pty_softc[dev].pt_cv_readq);
233 233 cv_destroy(&pty_softc[dev].pt_cv_writeq);
234 234 mutex_destroy(&pty_softc[dev].ptc_lock);
235 235 }
236 236 }
237 237
238 238 /*
239 - * Controller side. This is not, alas, a streams device; there are too
239 + * Manager side. This is not, alas, a streams device; there are too
240 240 * many old features that we must support and that don't work well
241 241 * with streams.
242 242 */
243 243
244 -/*ARGSUSED*/
245 244 int
246 245 ptcopen(dev_t *devp, int flag, int otyp, struct cred *cred)
247 246 {
248 247 dev_t dev = *devp;
249 248 struct pty *pty;
250 249 queue_t *q;
251 250
252 251 if (getminor(dev) >= npty) {
253 252 return (ENXIO);
254 253 }
255 254 pty = &pty_softc[getminor(dev)];
256 255 mutex_enter(&pty->ptc_lock);
257 256 if (pty->pt_flags & PF_CARR_ON) {
258 257 mutex_exit(&pty->ptc_lock);
259 - return (EIO); /* controller is exclusive use */
258 + return (EIO); /* manager is exclusive use */
260 259 /* XXX - should be EBUSY! */
261 260 }
262 261 if (pty->pt_flags & PF_WOPEN) {
263 262 pty->pt_flags &= ~PF_WOPEN;
264 263 cv_broadcast(&pty->pt_cv_flags);
265 264 }
266 265
267 266 if ((q = pty->pt_ttycommon.t_readq) != NULL) {
268 267 /*
269 - * Send an un-hangup to the slave, since "carrier" is
268 + * Send an un-hangup to the subsidiary, since "carrier" is
270 269 * coming back up. Make sure we're doing canonicalization.
271 270 */
272 271 (void) putctl(q, M_UNHANGUP);
273 272 (void) putctl1(q, M_CTL, MC_DOCANON);
274 273 }
275 274 pty->pt_flags |= PF_CARR_ON;
276 275 pty->pt_send = 0;
277 276 pty->pt_ucntl = 0;
278 277
279 278 mutex_exit(&pty->ptc_lock);
280 279 return (0);
281 280 }
282 281
283 -/*ARGSUSED1*/
284 282 int
285 283 ptcclose(dev_t dev, int flag, int otyp, struct cred *cred)
286 284 {
287 285 struct pty *pty;
288 286 mblk_t *bp;
289 287 queue_t *q;
290 288
291 289 pty = &pty_softc[getminor(dev)];
292 290
293 291 mutex_enter(&pty->ptc_lock);
294 292 if ((q = pty->pt_ttycommon.t_readq) != NULL) {
295 293 /*
296 - * Send a hangup to the slave, since "carrier" is dropping.
294 + * Send a hangup to the subsidiary, since "carrier" is dropping.
297 295 */
298 296 (void) putctl(q, M_HANGUP);
299 297 }
300 298
301 299 /*
302 - * Clear out all the controller-side state. This also
300 + * Clear out all the manager-side state. This also
303 301 * clears PF_CARR_ON, which is correct because the
304 - * "carrier" is dropping since the controller process
302 + * "carrier" is dropping since the manager process
305 303 * is going away.
306 304 */
307 305 pty->pt_flags &= (PF_WOPEN|PF_STOPPED|PF_NOSTOP);
308 306 while ((bp = pty->pt_stuffqfirst) != NULL) {
309 307 if ((pty->pt_stuffqfirst = bp->b_next) == NULL)
310 308 pty->pt_stuffqlast = NULL;
311 309 else
312 310 pty->pt_stuffqfirst->b_prev = NULL;
313 311 pty->pt_stuffqlen--;
314 312 bp->b_next = bp->b_prev = NULL;
315 313 freemsg(bp);
316 314 }
317 315 mutex_exit(&pty->ptc_lock);
318 316 return (0);
319 317 }
320 318
321 319 int
↓ open down ↓ |
7 lines elided |
↑ open up ↑ |
322 320 ptcread(dev_t dev, struct uio *uio, struct cred *cred)
323 321 {
324 322 struct pty *pty = &pty_softc[getminor(dev)];
325 323 mblk_t *bp, *nbp;
326 324 queue_t *q;
327 325 unsigned char tmp;
328 326 ssize_t cc;
329 327 int error;
330 328 off_t off;
331 329
332 -#ifdef lint
333 - cred = cred;
334 -#endif
335 -
336 330 off = uio->uio_offset;
337 331
338 332 mutex_enter(&pty->ptc_lock);
339 333
340 334 for (;;) {
341 335 while (pty->pt_flags & PF_READ) {
342 336 pty->pt_flags |= PF_WREAD;
343 337 cv_wait(&pty->pt_cv_flags, &pty->ptc_lock);
344 338 }
345 339 pty->pt_flags |= PF_READ;
346 340
347 341 /*
348 342 * If there's a TIOCPKT packet waiting, pass it back.
349 343 */
350 344 while (pty->pt_flags&(PF_PKT|PF_UCNTL) && pty->pt_send) {
351 345 tmp = pty->pt_send;
352 346 pty->pt_send = 0;
353 347 mutex_exit(&pty->ptc_lock);
354 348 error = ureadc((int)tmp, uio);
355 349 uio->uio_offset = off;
356 350 mutex_enter(&pty->ptc_lock);
357 351 if (error) {
358 352 pty->pt_send |= tmp;
359 353 goto out;
360 354 }
361 355 if (pty->pt_send == 0)
362 356 goto out;
363 357 }
364 358
365 359 /*
366 360 * If there's a user-control packet waiting, pass the
367 361 * "ioctl" code back.
368 362 */
369 363 while ((pty->pt_flags & (PF_UCNTL|PF_43UCNTL)) &&
370 364 pty->pt_ucntl) {
371 365 tmp = pty->pt_ucntl;
372 366 pty->pt_ucntl = 0;
373 367 mutex_exit(&pty->ptc_lock);
374 368 error = ureadc((int)tmp, uio);
375 369 uio->uio_offset = off;
376 370 mutex_enter(&pty->ptc_lock);
377 371 if (error) {
378 372 if (pty->pt_ucntl == 0)
379 373 pty->pt_ucntl = tmp;
380 374 goto out;
381 375 }
382 376 if (pty->pt_ucntl == 0)
383 377 goto out;
384 378 }
385 379
386 380 /*
387 381 * If there's any data waiting, pass it back.
388 382 */
389 383 if ((q = pty->pt_ttycommon.t_writeq) != NULL &&
390 384 q->q_first != NULL &&
391 385 !(pty->pt_flags & PF_STOPPED)) {
392 386 if (pty->pt_flags & (PF_PKT|PF_UCNTL|PF_43UCNTL)) {
393 387 /*
394 388 * We're about to begin a move in packet or
395 389 * user-control mode; precede the data with a
396 390 * data header.
397 391 */
398 392 mutex_exit(&pty->ptc_lock);
399 393 error = ureadc(TIOCPKT_DATA, uio);
400 394 uio->uio_offset = off;
401 395 mutex_enter(&pty->ptc_lock);
402 396 if (error != 0)
403 397 goto out;
404 398 if ((q = pty->pt_ttycommon.t_writeq) == NULL)
405 399 goto out;
406 400 }
407 401 if ((bp = getq(q)) == NULL)
408 402 goto out;
409 403 while (uio->uio_resid > 0) {
410 404 while ((cc = bp->b_wptr - bp->b_rptr) == 0) {
411 405 nbp = bp->b_cont;
412 406 freeb(bp);
413 407 if ((bp = nbp) == NULL) {
414 408 if ((q == NULL) ||
415 409 (bp = getq(q)) == NULL)
416 410 goto out;
417 411 }
418 412 }
419 413 cc = MIN(cc, uio->uio_resid);
420 414 mutex_exit(&pty->ptc_lock);
421 415 error = uiomove((caddr_t)bp->b_rptr,
422 416 cc, UIO_READ, uio);
423 417 uio->uio_offset = off;
424 418 mutex_enter(&pty->ptc_lock);
425 419 if (error != 0) {
426 420 freemsg(bp);
427 421 goto out;
428 422 }
429 423 q = pty->pt_ttycommon.t_writeq;
430 424 bp->b_rptr += cc;
431 425 }
432 426 /*
433 427 * Strip off zero-length blocks from the front of
434 428 * what we're putting back on the queue.
435 429 */
436 430 while ((bp->b_wptr - bp->b_rptr) == 0) {
437 431 nbp = bp->b_cont;
438 432 freeb(bp);
439 433 if ((bp = nbp) == NULL)
440 434 goto out; /* nothing left */
441 435 }
442 436 if (q != NULL)
443 437 (void) putbq(q, bp);
444 438 else
445 439 freemsg(bp);
446 440 goto out;
447 441 }
448 442
449 443 /*
450 444 * If there's any TIOCSTI-stuffed characters, pass
451 445 * them back. (They currently arrive after all output;
452 446 * is this correct?)
453 447 */
454 448 if (pty->pt_flags&PF_UCNTL && pty->pt_stuffqfirst != NULL) {
455 449 mutex_exit(&pty->ptc_lock);
456 450 error = ureadc(TIOCSTI&0xff, uio);
457 451 mutex_enter(&pty->ptc_lock);
458 452 while (error == 0 &&
459 453 (bp = pty->pt_stuffqfirst) != NULL &&
460 454 uio->uio_resid > 0) {
461 455 pty->pt_stuffqlen--;
462 456 if ((pty->pt_stuffqfirst = bp->b_next) == NULL)
463 457 pty->pt_stuffqlast = NULL;
464 458 else
465 459 pty->pt_stuffqfirst->b_prev = NULL;
466 460 mutex_exit(&pty->ptc_lock);
467 461 error = ureadc((int)*bp->b_rptr, uio);
↓ open down ↓ |
122 lines elided |
↑ open up ↑ |
468 462 bp->b_next = bp->b_prev = NULL;
469 463 freemsg(bp);
470 464 mutex_enter(&pty->ptc_lock);
471 465 }
472 466 uio->uio_offset = off;
473 467 goto out;
474 468 }
475 469
476 470 /*
477 471 * There's no data available.
478 - * We want to block until the slave is open, and there's
479 - * something to read; but if we lost the slave or we're NBIO,
480 - * then return the appropriate error instead. POSIX-style
481 - * non-block has top billing and gives -1 with errno = EAGAIN,
482 - * BSD-style comes next and gives -1 with errno = EWOULDBLOCK,
483 - * SVID-style comes last and gives 0.
472 + * We want to block until the subsidiary is open, and there's
473 + * something to read; but if we lost the subsidiary or we're
474 + * NBIO, then return the appropriate error instead.
475 + * POSIX-style non-block has top billing and gives -1 with
476 + * errno = EAGAIN, BSD-style comes next and gives -1 with
477 + * errno = EWOULDBLOCK, SVID-style comes last and gives 0.
484 478 */
485 - if (pty->pt_flags & PF_SLAVEGONE) {
479 + if (pty->pt_flags & PF_SUBSIDGONE) {
486 480 error = EIO;
487 481 goto out;
488 482 }
489 483 if (uio->uio_fmode & FNONBLOCK) {
490 484 error = EAGAIN;
491 485 goto out;
492 486 }
493 487 if (pty->pt_flags & PF_NBIO) {
494 488 error = EWOULDBLOCK;
495 489 goto out;
496 490 }
497 491 if (uio->uio_fmode & FNDELAY)
498 492 goto out;
499 493
500 494 if (pty->pt_flags & PF_WREAD)
501 495 cv_broadcast(&pty->pt_cv_flags);
502 496
503 497 pty->pt_flags &= ~(PF_READ | PF_WREAD);
504 498
505 499
506 500 if (!cv_wait_sig(&pty->pt_cv_writeq, &pty->ptc_lock)) {
507 501 mutex_exit(&pty->ptc_lock);
508 502 return (EINTR);
509 503 }
510 504 }
511 505
512 506 out:
513 507 if (pty->pt_flags & PF_WREAD)
514 508 cv_broadcast(&pty->pt_cv_flags);
515 509
516 510 pty->pt_flags &= ~(PF_READ | PF_WREAD);
517 511
518 512 mutex_exit(&pty->ptc_lock);
519 513 return (error);
520 514 }
521 515
522 516 int
523 517 ptcwrite(dev_t dev, struct uio *uio, struct cred *cred)
524 518 {
↓ open down ↓ |
29 lines elided |
↑ open up ↑ |
525 519 struct pty *pty = &pty_softc[getminor(dev)];
526 520 queue_t *q;
527 521 int written;
528 522 mblk_t *mp;
529 523 int fmode = 0;
530 524 int error = 0;
531 525
532 526 off_t off;
533 527 off = uio->uio_offset;
534 528
535 -#ifdef lint
536 - cred = cred;
537 -#endif
538 -
539 -
540 529 mutex_enter(&pty->ptc_lock);
541 530
542 531 again:
543 532 while (pty->pt_flags & PF_WRITE) {
544 533 pty->pt_flags |= PF_WWRITE;
545 534 cv_wait(&pty->pt_cv_flags, &pty->ptc_lock);
546 535 }
547 536
548 537 pty->pt_flags |= PF_WRITE;
549 538
550 539 if ((q = pty->pt_ttycommon.t_readq) == NULL) {
551 540
552 541 /*
553 - * Wait for slave to open.
542 + * Wait for subsidiary to open.
554 543 */
555 - if (pty->pt_flags & PF_SLAVEGONE) {
544 + if (pty->pt_flags & PF_SUBSIDGONE) {
556 545 error = EIO;
557 546 goto out;
558 547 }
559 548 if (uio->uio_fmode & FNONBLOCK) {
560 549 error = EAGAIN;
561 550 goto out;
562 551 }
563 552 if (pty->pt_flags & PF_NBIO) {
564 553 error = EWOULDBLOCK;
565 554 goto out;
566 555 }
567 556 if (uio->uio_fmode & FNDELAY)
568 557 goto out;
569 558
570 559 if (pty->pt_flags & PF_WWRITE)
571 560 cv_broadcast(&pty->pt_cv_flags);
572 561
573 562 pty->pt_flags &= ~(PF_WRITE | PF_WWRITE);
574 563
575 564 if (!cv_wait_sig(&pty->pt_cv_readq, &pty->ptc_lock)) {
576 565 mutex_exit(&pty->ptc_lock);
577 566 return (EINTR);
578 567 }
579 568
580 569 goto again;
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
581 570 }
582 571
583 572 /*
584 573 * If in remote mode, even zero-length writes generate messages.
585 574 */
586 575 written = 0;
587 576 if ((pty->pt_flags & PF_REMOTE) || uio->uio_resid > 0) {
588 577 do {
589 578 while (!canput(q)) {
590 579 /*
591 - * Wait for slave's read queue to unclog.
580 + * Wait for subsidiary's read queue to unclog.
592 581 */
593 - if (pty->pt_flags & PF_SLAVEGONE) {
582 + if (pty->pt_flags & PF_SUBSIDGONE) {
594 583 error = EIO;
595 584 goto out;
596 585 }
597 586 if (uio->uio_fmode & FNONBLOCK) {
598 587 if (!written)
599 588 error = EAGAIN;
600 589 goto out;
601 590 }
602 591 if (pty->pt_flags & PF_NBIO) {
603 592 if (!written)
604 593 error = EWOULDBLOCK;
605 594 goto out;
606 595 }
607 596 if (uio->uio_fmode & FNDELAY)
608 597 goto out;
609 598
610 599 if (pty->pt_flags & PF_WWRITE)
611 600 cv_broadcast(&pty->pt_cv_flags);
612 601
613 602 pty->pt_flags &= ~(PF_WRITE | PF_WWRITE);
614 603
615 604 if (!cv_wait_sig(&pty->pt_cv_readq,
616 605 &pty->ptc_lock)) {
617 606 mutex_exit(&pty->ptc_lock);
618 607 return (EINTR);
619 608 }
620 609
621 610 while (pty->pt_flags & PF_WRITE) {
622 611 pty->pt_flags |= PF_WWRITE;
623 612 cv_wait(&pty->pt_cv_flags,
624 613 &pty->ptc_lock);
625 614 }
626 615
627 616 pty->pt_flags |= PF_WRITE;
628 617 }
629 618
630 619 if ((pty->pt_flags & PF_NBIO) &&
631 620 !(uio->uio_fmode & FNONBLOCK)) {
632 621 fmode = uio->uio_fmode;
633 622 uio->uio_fmode |= FNONBLOCK;
634 623 }
635 624
636 625 error = makemsg(uio->uio_resid, uio, pty, &mp);
637 626 uio->uio_offset = off;
638 627 if (fmode)
639 628 uio->uio_fmode = fmode;
640 629 if (error != 0) {
641 630 if (error != EAGAIN && error != EWOULDBLOCK)
642 631 goto out;
643 632 if (uio->uio_fmode & FNONBLOCK) {
644 633 if (!written)
645 634 error = EAGAIN;
646 635 goto out;
647 636 }
648 637 if (pty->pt_flags & PF_NBIO) {
649 638 if (!written)
650 639 error = EWOULDBLOCK;
651 640 goto out;
652 641 }
653 642 if (uio->uio_fmode & FNDELAY)
654 643 goto out;
655 644 cmn_err(CE_PANIC,
656 645 "ptcwrite: non null return from"
657 646 " makemsg");
658 647 }
659 648
660 649 /*
661 650 * Check again for safety; since "uiomove" can take a
662 651 * page fault, there's no guarantee that "pt_flags"
663 652 * didn't change while it was happening.
664 653 */
665 654 if ((q = pty->pt_ttycommon.t_readq) == NULL) {
666 655 if (mp)
667 656 freemsg(mp);
668 657 error = EIO;
669 658 goto out;
670 659 }
671 660 if (mp)
672 661 (void) putq(q, mp);
673 662 written = 1;
674 663 } while (uio->uio_resid > 0);
675 664 }
676 665 out:
677 666 if (pty->pt_flags & PF_WWRITE)
678 667 cv_broadcast(&pty->pt_cv_flags);
679 668
680 669 pty->pt_flags &= ~(PF_WRITE | PF_WWRITE);
681 670
682 671 mutex_exit(&pty->ptc_lock);
683 672 return (error);
684 673 }
685 674
686 675 #define copy_in(data, d_arg) \
687 676 if (copyin((caddr_t)data, &d_arg, sizeof (int)) != 0) \
688 677 return (EFAULT)
689 678
690 679 #define copy_out(d_arg, data) \
691 680 if (copyout(&d_arg, (caddr_t)data, sizeof (int)) != 0) \
692 681 return (EFAULT)
693 682
694 683 int
695 684 ptcioctl(dev_t dev, int cmd, intptr_t data, int flag, struct cred *cred,
696 685 int *rvalp)
697 686 {
698 687 struct pty *pty = &pty_softc[getminor(dev)];
699 688 queue_t *q;
700 689 struct ttysize tty_arg;
701 690 struct winsize win_arg;
702 691 int d_arg;
703 692 int err;
704 693
705 694 switch (cmd) {
706 695
707 696 case TIOCPKT:
708 697 copy_in(data, d_arg);
709 698 mutex_enter(&pty->ptc_lock);
710 699 if (d_arg) {
711 700 if (pty->pt_flags & (PF_UCNTL|PF_43UCNTL)) {
712 701 mutex_exit(&pty->ptc_lock);
713 702 return (EINVAL);
714 703 }
715 704 pty->pt_flags |= PF_PKT;
716 705 } else
717 706 pty->pt_flags &= ~PF_PKT;
718 707 mutex_exit(&pty->ptc_lock);
719 708 break;
720 709
721 710 case TIOCUCNTL:
722 711 copy_in(data, d_arg);
723 712 mutex_enter(&pty->ptc_lock);
724 713 if (d_arg) {
725 714 if (pty->pt_flags & (PF_PKT|PF_UCNTL)) {
726 715 mutex_exit(&pty->ptc_lock);
727 716 return (EINVAL);
728 717 }
729 718 pty->pt_flags |= PF_43UCNTL;
730 719 } else
731 720 pty->pt_flags &= ~PF_43UCNTL;
732 721 mutex_exit(&pty->ptc_lock);
733 722 break;
734 723
735 724 case TIOCTCNTL:
736 725 copy_in(data, d_arg);
737 726 mutex_enter(&pty->ptc_lock);
738 727 if (d_arg) {
739 728 if (pty->pt_flags & PF_PKT) {
740 729 mutex_exit(&pty->ptc_lock);
741 730 return (EINVAL);
742 731 }
743 732 pty->pt_flags |= PF_UCNTL;
744 733 } else
745 734 pty->pt_flags &= ~PF_UCNTL;
746 735 mutex_exit(&pty->ptc_lock);
747 736 break;
748 737
749 738 case TIOCREMOTE:
750 739 copy_in(data, d_arg);
751 740 mutex_enter(&pty->ptc_lock);
752 741 if (d_arg) {
753 742 if ((q = pty->pt_ttycommon.t_readq) != NULL)
754 743 (void) putctl1(q, M_CTL, MC_NOCANON);
755 744 pty->pt_flags |= PF_REMOTE;
↓ open down ↓ |
152 lines elided |
↑ open up ↑ |
756 745 } else {
757 746 if ((q = pty->pt_ttycommon.t_readq) != NULL)
758 747 (void) putctl1(q, M_CTL, MC_DOCANON);
759 748 pty->pt_flags &= ~PF_REMOTE;
760 749 }
761 750 mutex_exit(&pty->ptc_lock);
762 751 break;
763 752
764 753 case TIOCSIGNAL:
765 754 /*
766 - * Blast a M_PCSIG message up the slave stream; the
755 + * Blast a M_PCSIG message up the subsidiary stream; the
767 756 * signal number is the argument to the "ioctl".
768 757 */
769 758 copy_in(data, d_arg);
770 759 mutex_enter(&pty->ptc_lock);
771 760 if ((q = pty->pt_ttycommon.t_readq) != NULL)
772 761 (void) putctl1(q, M_PCSIG, (int)d_arg);
773 762 mutex_exit(&pty->ptc_lock);
774 763 break;
775 764
776 765 case FIONBIO:
777 766 copy_in(data, d_arg);
778 767 mutex_enter(&pty->ptc_lock);
779 768 if (d_arg)
780 769 pty->pt_flags |= PF_NBIO;
781 770 else
782 771 pty->pt_flags &= ~PF_NBIO;
783 772 mutex_exit(&pty->ptc_lock);
784 773 break;
785 774
786 775 case FIOASYNC:
↓ open down ↓ |
10 lines elided |
↑ open up ↑ |
787 776 copy_in(data, d_arg);
788 777 mutex_enter(&pty->ptc_lock);
789 778 if (d_arg)
790 779 pty->pt_flags |= PF_ASYNC;
791 780 else
792 781 pty->pt_flags &= ~PF_ASYNC;
793 782 mutex_exit(&pty->ptc_lock);
794 783 break;
795 784
796 785 /*
797 - * These, at least, can work on the controller-side process
786 + * These, at least, can work on the manager-side process
798 787 * group.
799 788 */
800 789 case FIOGETOWN:
801 790 mutex_enter(&pty->ptc_lock);
802 791 d_arg = -pty->pt_pgrp;
803 792 mutex_exit(&pty->ptc_lock);
804 793 copy_out(d_arg, data);
805 794 break;
806 795
807 796 case FIOSETOWN:
808 797 copy_in(data, d_arg);
809 798 mutex_enter(&pty->ptc_lock);
810 799 pty->pt_pgrp = (short)(-d_arg);
811 800 mutex_exit(&pty->ptc_lock);
812 801 break;
813 802
814 803 case FIONREAD: {
815 804 /*
816 - * Return the total number of bytes of data in all messages
817 - * in slave write queue, which is master read queue, unless a
818 - * special message would be read.
805 + * Return the total number of bytes of data in all messages in
806 + * subsidiary write queue, which is manager read queue, unless
807 + * a special message would be read.
819 808 */
820 809 mblk_t *mp;
821 810 size_t count = 0;
822 811
823 812 mutex_enter(&pty->ptc_lock);
824 813 if (pty->pt_flags&(PF_PKT|PF_UCNTL) && pty->pt_send)
825 814 count = 1; /* will return 1 byte */
826 815 else if ((pty->pt_flags & (PF_UCNTL|PF_43UCNTL)) &&
827 816 pty->pt_ucntl)
828 817 count = 1; /* will return 1 byte */
829 818 else if ((q = pty->pt_ttycommon.t_writeq) != NULL &&
830 819 q->q_first != NULL && !(pty->pt_flags & PF_STOPPED)) {
831 820 /*
832 821 * Will return whatever data is queued up.
833 822 */
834 823 for (mp = q->q_first; mp != NULL; mp = mp->b_next)
835 824 count += msgdsize(mp);
836 825 } else if ((pty->pt_flags & PF_UCNTL) &&
837 826 pty->pt_stuffqfirst != NULL) {
838 827 /*
839 828 * Will return STI'ed data.
840 829 */
841 830 count = pty->pt_stuffqlen + 1;
842 831 }
843 832
844 833 /*
845 834 * Under LP64 we could have more than INT_MAX bytes to report,
846 835 * but the interface is defined in terms of int, so we cap it.
847 836 */
848 837 d_arg = MIN(count, INT_MAX);
849 838 mutex_exit(&pty->ptc_lock);
850 839 copy_out(d_arg, data);
851 840 break;
852 841 }
853 842
854 843 case TIOCSWINSZ:
855 844 /*
856 845 * Unfortunately, TIOCSWINSZ and the old TIOCSSIZE "ioctl"s
857 846 * share the same code. If the upper 16 bits of the number
858 847 * of lines is non-zero, it was probably a TIOCSWINSZ,
859 848 * with both "ws_row" and "ws_col" non-zero.
860 849 */
861 850 if (copyin((caddr_t)data,
862 851 &tty_arg, sizeof (struct ttysize)) != 0)
863 852 return (EFAULT);
864 853
865 854 if ((tty_arg.ts_lines & 0xffff0000) != 0) {
866 855 /*
867 856 * It's a TIOCSWINSZ.
868 857 */
869 858 win_arg = *(struct winsize *)&tty_arg;
870 859
871 860 mutex_enter(&pty->ptc_lock);
872 861 /*
873 862 * If the window size changed, send a SIGWINCH.
874 863 */
875 864 if (bcmp(&pty->pt_ttycommon.t_size,
876 865 &win_arg, sizeof (struct winsize))) {
877 866 pty->pt_ttycommon.t_size = win_arg;
878 867 if ((q = pty->pt_ttycommon.t_readq) != NULL)
879 868 (void) putctl1(q, M_PCSIG, SIGWINCH);
880 869 }
881 870 mutex_exit(&pty->ptc_lock);
882 871 break;
883 872 }
884 873 /* FALLTHROUGH */
885 874
886 875 case TIOCSSIZE:
887 876 if (copyin((caddr_t)data,
888 877 &tty_arg, sizeof (struct ttysize)) != 0)
889 878 return (EFAULT);
890 879 mutex_enter(&pty->ptc_lock);
891 880 pty->pt_ttycommon.t_size.ws_row = (ushort_t)tty_arg.ts_lines;
892 881 pty->pt_ttycommon.t_size.ws_col = (ushort_t)tty_arg.ts_cols;
893 882 pty->pt_ttycommon.t_size.ws_xpixel = 0;
894 883 pty->pt_ttycommon.t_size.ws_ypixel = 0;
895 884 mutex_exit(&pty->ptc_lock);
896 885 break;
897 886
898 887 case TIOCGWINSZ:
899 888 mutex_enter(&pty->ptc_lock);
900 889 win_arg = pty->pt_ttycommon.t_size;
901 890 mutex_exit(&pty->ptc_lock);
902 891 if (copyout(&win_arg, (caddr_t)data,
903 892 sizeof (struct winsize)) != 0)
904 893 return (EFAULT);
905 894 break;
906 895
907 896 case TIOCGSIZE:
908 897 mutex_enter(&pty->ptc_lock);
↓ open down ↓ |
80 lines elided |
↑ open up ↑ |
909 898 tty_arg.ts_lines = pty->pt_ttycommon.t_size.ws_row;
910 899 tty_arg.ts_cols = pty->pt_ttycommon.t_size.ws_col;
911 900 mutex_exit(&pty->ptc_lock);
912 901 if (copyout(&tty_arg, (caddr_t)data,
913 902 sizeof (struct ttysize)) != 0)
914 903 return (EFAULT);
915 904 break;
916 905
917 906 /*
918 907 * XXX These should not be here. The only reason why an
919 - * "ioctl" on the controller side should get the
920 - * slave side's process group is so that the process on
921 - * the controller side can send a signal to the slave
908 + * "ioctl" on the manager side should get the
909 + * subsidiary side's process group is so that the process on
910 + * the manager side can send a signal to the subsidiary
922 911 * side's process group; however, this is better done
923 912 * with TIOCSIGNAL, both because it doesn't require us
924 - * to know about the slave side's process group and because
925 - * the controller side process may not have permission to
913 + * to know about the subsidiary side's process group and because
914 + * the manager side process may not have permission to
926 915 * send that signal to the entire process group.
927 916 *
928 917 * However, since vanilla 4BSD doesn't provide TIOCSIGNAL,
929 918 * we can't just get rid of them.
930 919 */
931 920 case TIOCGPGRP:
932 921 case TIOCSPGRP:
933 922 /*
934 923 * This is amazingly disgusting, but the stupid semantics of
935 924 * 4BSD pseudo-ttys makes us do it. If we do one of these guys
936 - * on the controller side, it really applies to the slave-side
925 + * on the manager side, it really applies to the subsidiary-side
937 926 * stream. It should NEVER have been possible to do ANY sort
938 - * of tty operations on the controller side, but it's too late
927 + * of tty operations on the manager side, but it's too late
939 928 * to fix that now. However, we won't waste our time implementing
940 929 * anything that the original pseudo-tty driver didn't handle.
941 930 */
942 931 case TIOCGETP:
943 932 case TIOCSETP:
944 933 case TIOCSETN:
945 934 case TIOCGETC:
946 935 case TIOCSETC:
947 936 case TIOCGLTC:
948 937 case TIOCSLTC:
949 938 case TIOCLGET:
950 939 case TIOCLSET:
951 940 case TIOCLBIS:
952 941 case TIOCLBIC:
953 942 mutex_enter(&pty->ptc_lock);
954 943 if (pty->pt_vnode == NULL) {
955 944 mutex_exit(&pty->ptc_lock);
956 945 return (EIO);
957 946 }
958 947 pty->pt_flags |= PF_IOCTL;
959 948 mutex_exit(&pty->ptc_lock);
960 949 err = strioctl(pty->pt_vnode, cmd, data, flag,
961 950 U_TO_K, cred, rvalp);
962 951 mutex_enter(&pty->ptc_lock);
963 952 if (pty->pt_flags & PF_WAIT)
964 953 cv_signal(&pty->pt_cv_flags);
965 954 pty->pt_flags &= ~(PF_IOCTL|PF_WAIT);
966 955 mutex_exit(&pty->ptc_lock);
967 956 return (err);
968 957
969 958 default:
970 959 return (ENOTTY);
971 960 }
972 961
973 962 return (0);
974 963 }
975 964
↓ open down ↓ |
27 lines elided |
↑ open up ↑ |
976 965
977 966 int
978 967 ptcpoll(dev_t dev, short events, int anyyet, short *reventsp,
979 968 struct pollhead **phpp)
980 969 {
981 970 struct pty *pty = &pty_softc[getminor(dev)];
982 971 pollhead_t *php = &ptcph;
983 972 queue_t *q;
984 973 int pos = 0;
985 974
986 -#ifdef lint
987 - anyyet = anyyet;
988 -#endif
989 975 if (polllock(php, &pty->ptc_lock) != 0) {
990 976 *reventsp = POLLNVAL;
991 977 return (0);
992 978 }
993 979
994 980 ASSERT(MUTEX_HELD(&pty->ptc_lock));
995 981
996 982 *reventsp = 0;
997 - if (pty->pt_flags & PF_SLAVEGONE) {
983 + if (pty->pt_flags & PF_SUBSIDGONE) {
998 984 if (events & (POLLIN|POLLRDNORM))
999 985 *reventsp |= (events & (POLLIN|POLLRDNORM));
1000 986 if (events & (POLLOUT|POLLWRNORM))
1001 987 *reventsp |= (events & (POLLOUT|POLLWRNORM));
1002 988 mutex_exit(&pty->ptc_lock);
1003 989 /*
1004 990 * A non NULL pollhead pointer should be returned in case
1005 991 * user polls for 0 events.
1006 992 */
1007 993 *phpp = !anyyet && !*reventsp ? php : (struct pollhead *)NULL;
1008 994 return (0);
1009 995 }
1010 996 if (events & (POLLIN|POLLRDNORM)) {
1011 997 if ((q = pty->pt_ttycommon.t_writeq) != NULL &&
1012 998 q->q_first != NULL && !(pty->pt_flags & PF_STOPPED)) {
1013 999 /*
1014 1000 * Regular data is available.
1015 1001 */
1016 1002 *reventsp |= (events & (POLLIN|POLLRDNORM));
1017 1003 pos++;
1018 1004 }
1019 1005 if (pty->pt_flags & (PF_PKT|PF_UCNTL) && pty->pt_send) {
1020 1006 /*
1021 1007 * A control packet is available.
1022 1008 */
1023 1009 *reventsp |= (events & (POLLIN|POLLRDNORM));
1024 1010 pos++;
1025 1011 }
1026 1012 if ((pty->pt_flags & PF_UCNTL) &&
1027 1013 (pty->pt_ucntl || pty->pt_stuffqfirst != NULL)) {
1028 1014 /*
1029 1015 * "ioctl" or TIOCSTI data is available.
1030 1016 */
1031 1017 *reventsp |= (events & (POLLIN|POLLRDNORM));
1032 1018 pos++;
1033 1019 }
1034 1020 if ((pty->pt_flags & PF_43UCNTL) && pty->pt_ucntl) {
1035 1021 *reventsp |= (events & (POLLIN|POLLRDNORM));
1036 1022 pos++;
1037 1023 }
1038 1024 }
1039 1025 if (events & (POLLOUT|POLLWRNORM)) {
1040 1026 if ((q = pty->pt_ttycommon.t_readq) != NULL &&
1041 1027 canput(q)) {
1042 1028 *reventsp |= (events & (POLLOUT|POLLWRNORM));
1043 1029 pos++;
1044 1030 }
1045 1031 }
1046 1032 if (events & POLLERR) {
1047 1033 *reventsp |= POLLERR;
1048 1034 pos++;
1049 1035 }
1050 1036 if (events == 0) { /* "exceptional conditions" */
1051 1037 if (((pty->pt_flags & (PF_PKT|PF_UCNTL)) && pty->pt_send) ||
1052 1038 ((pty->pt_flags & PF_UCNTL) &&
1053 1039 (pty->pt_ucntl || pty->pt_stuffqfirst != NULL))) {
1054 1040 pos++;
1055 1041 }
1056 1042 if ((pty->pt_flags & PF_43UCNTL) && pty->pt_ucntl) {
1057 1043 pos++;
1058 1044 }
1059 1045 }
1060 1046
1061 1047 /*
1062 1048 * Arrange to have poll waken up when event occurs.
1063 1049 * if (!anyyet)
1064 1050 */
1065 1051 if (!pos) {
1066 1052 *phpp = php;
1067 1053 *reventsp = 0;
1068 1054 }
1069 1055
1070 1056 mutex_exit(&pty->ptc_lock);
1071 1057 return (0);
1072 1058 }
1073 1059
1074 1060 void
1075 1061 gsignal(int pid, int sig)
1076 1062 {
1077 1063 procset_t set;
1078 1064 sigsend_t v;
1079 1065
1080 1066 bzero(&v, sizeof (v));
1081 1067 v.sig = sig;
1082 1068 v.perm = 0;
1083 1069 v.checkperm = 1;
1084 1070 v.value.sival_ptr = NULL;
1085 1071
1086 1072 setprocset(&set, POP_AND, P_PGID, -pid, P_ALL, P_MYID);
1087 1073 (void) sigsendset(&set, &v);
1088 1074 }
1089 1075
1090 1076 static int
1091 1077 makemsg(ssize_t count, struct uio *uiop, struct pty *pty, mblk_t **mpp)
1092 1078 {
1093 1079 int pri = BPRI_LO;
1094 1080 int error;
1095 1081 mblk_t *bp = NULL;
1096 1082
1097 1083 ASSERT(MUTEX_HELD(&pty->ptc_lock));
1098 1084
1099 1085 *mpp = NULL;
1100 1086
1101 1087 /*
1102 1088 * Create data part of message, if any.
1103 1089 */
1104 1090 if (count >= 0) {
1105 1091 if ((bp = allocb(count, pri)) == NULL)
1106 1092 return (ENOSR);
1107 1093
1108 1094 mutex_exit(&pty->ptc_lock);
1109 1095 error = uiomove((caddr_t)bp->b_wptr, count, UIO_WRITE, uiop);
1110 1096 mutex_enter(&pty->ptc_lock);
1111 1097 if (error) {
1112 1098 freeb(bp);
1113 1099 return (error);
1114 1100 }
1115 1101
1116 1102 bp->b_wptr += count;
1117 1103 }
1118 1104
1119 1105 *mpp = bp;
1120 1106 return (0);
1121 1107 }
↓ open down ↓ |
114 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX