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