Print this page
7127 remove -Wno-missing-braces from Makefile.uts
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/uts/common/io/tty_pts.c
+++ new/usr/src/uts/common/io/tty_pts.c
1 1 /*
2 2 * Copyright (c) 2011 Bayard G. Bell. All rights reserved.
3 3 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
4 4 * Use is subject to license terms.
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.
15 15 * This is the "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/strsun.h>
29 29 #include <sys/tty.h>
30 30 #include <sys/user.h>
31 31 #include <sys/conf.h>
32 32 #include <sys/file.h>
33 33 #include <sys/vnode.h> /* 1/0 on the vomit meter */
34 34 #include <sys/proc.h>
35 35 #include <sys/uio.h>
36 36 #include <sys/errno.h>
37 37 #include <sys/strsubr.h>
38 38 #include <sys/poll.h>
39 39 #include <sys/sysmacros.h>
40 40 #include <sys/debug.h>
41 41 #include <sys/procset.h>
42 42 #include <sys/cred.h>
43 43 #include <sys/ptyvar.h>
44 44 #include <sys/suntty.h>
45 45 #include <sys/stat.h>
46 46 #include <sys/policy.h>
47 47
48 48 #include <sys/conf.h>
49 49 #include <sys/ddi.h>
50 50 #include <sys/sunddi.h>
51 51
52 52 extern void gsignal(int pid, int sig);
53 53
54 54 extern int npty; /* number of pseudo-ttys configured in */
55 55 extern struct pty *pty_softc;
56 56
57 57 extern struct pollhead ptcph; /* poll head for ptcpoll() use */
58 58
59 59 #define IFLAGS (CS7|CREAD|PARENB)
60 60
61 61
62 62 /*
63 63 * Most of these should be "void", but the people who defined the "streams"
64 64 * data structure for S5 didn't understand data types.
65 65 */
66 66
67 67 /*
68 68 * Slave side. This is a streams device.
69 69 */
70 70 static int ptslopen(queue_t *, dev_t *, int flag, int, cred_t *);
71 71 static int ptslclose(queue_t *, int, cred_t *);
72 72 static int ptslrserv(queue_t *);
73 73
74 74 /*
75 75 * To save instructions, since STREAMS ignores the return value
76 76 * from this function, it is defined as void here. Kind of icky, but...
77 77 */
78 78
79 79 static void ptslwput(queue_t *q, mblk_t *mp);
80 80
81 81 static struct module_info ptslm_info = {
82 82 0,
83 83 "ptys",
84 84 0,
85 85 INFPSZ,
86 86 2048,
87 87 200
88 88 };
89 89
90 90 static struct qinit ptslrinit = {
91 91 putq,
92 92 ptslrserv,
93 93 ptslopen,
94 94 ptslclose,
95 95 NULL,
96 96 &ptslm_info,
97 97 NULL
98 98 };
99 99
100 100 static struct qinit ptslwinit = {
101 101 (int (*)())ptslwput,
102 102 NULL,
103 103 NULL,
104 104 NULL,
105 105 NULL,
106 106 &ptslm_info,
107 107 NULL
108 108 };
109 109
110 110 struct streamtab ptysinfo = {
111 111 &ptslrinit,
112 112 &ptslwinit,
113 113 NULL,
114 114 NULL
115 115 };
116 116
117 117 static void ptslreioctl(void *);
118 118 static void ptslioctl(struct pty *, queue_t *, mblk_t *);
119 119 static void pt_sendstop(struct pty *);
120 120 static void ptcpollwakeup(struct pty *, int);
121 121
122 122
123 123 static int ptsl_info(dev_info_t *, ddi_info_cmd_t, void *, void **);
124 124 static int ptsl_attach(dev_info_t *, ddi_attach_cmd_t);
125 125 static dev_info_t *ptsl_dip; /* for dev-to-dip conversions */
126 126
127 127 DDI_DEFINE_STREAM_OPS(ptsl_ops, nulldev, nulldev,
128 128 ptsl_attach, nodev, nodev, ptsl_info, D_MP, &ptysinfo,
129 129 ddi_quiesce_not_supported);
130 130
131 131 #include <sys/types.h>
132 132 #include <sys/conf.h>
133 133 #include <sys/param.h>
134 134 #include <sys/systm.h>
135 135 #include <sys/errno.h>
136 136 #include <sys/modctl.h>
137 137
138 138 /*
139 139 * Module linkage information for the kernel.
↓ open down ↓ |
139 lines elided |
↑ open up ↑ |
140 140 */
141 141
142 142 static struct modldrv modldrv = {
143 143 &mod_driverops, /* Type of module. This one is a pseudo driver */
144 144 "tty pseudo driver slave 'ptsl'",
145 145 &ptsl_ops, /* driver ops */
146 146 };
147 147
148 148 static struct modlinkage modlinkage = {
149 149 MODREV_1,
150 - &modldrv,
151 - NULL
150 + { &modldrv, NULL }
152 151 };
153 152
154 153 int
155 154 _init(void)
156 155 {
157 156 return (mod_install(&modlinkage));
158 157 }
159 158
160 159 int
161 160 _fini(void)
162 161 {
163 162 return (mod_remove(&modlinkage));
164 163 }
165 164
166 165 int
167 166 _info(struct modinfo *modinfop)
168 167 {
169 168 return (mod_info(&modlinkage, modinfop));
170 169 }
171 170
172 171 static char *tty_banks = PTY_BANKS;
173 172 static char *tty_digits = PTY_DIGITS;
174 173
175 174 /* ARGSUSED */
176 175 static int
177 176 ptsl_attach(dev_info_t *devi, ddi_attach_cmd_t cmd)
178 177 {
179 178 char name[8];
180 179 int tty_num;
181 180 char *tty_digit = tty_digits;
182 181 char *tty_bank = tty_banks;
183 182
184 183 for (tty_num = 0; tty_num < npty; tty_num++) {
185 184 (void) sprintf(name, "tty%c%c", *tty_bank, *tty_digit);
186 185 if (ddi_create_minor_node(devi, name, S_IFCHR,
187 186 tty_num, DDI_PSEUDO, NULL) == DDI_FAILURE) {
188 187 ddi_remove_minor_node(devi, NULL);
189 188 return (-1);
190 189 }
191 190 if (*(++tty_digit) == '\0') {
192 191 tty_digit = tty_digits;
193 192 if (*(++tty_bank) == '\0')
194 193 break;
195 194 }
196 195 }
197 196 ptsl_dip = devi;
198 197 return (DDI_SUCCESS);
199 198 }
200 199
201 200 /* ARGSUSED */
202 201 static int
203 202 ptsl_info(dev_info_t *dip, ddi_info_cmd_t infocmd, void *arg,
204 203 void **result)
205 204 {
206 205 int error;
207 206
208 207 switch (infocmd) {
209 208 case DDI_INFO_DEVT2DEVINFO:
210 209 if (ptsl_dip == NULL) {
211 210 error = DDI_FAILURE;
212 211 } else {
213 212 *result = (void *)ptsl_dip;
214 213 error = DDI_SUCCESS;
215 214 }
216 215 break;
217 216 case DDI_INFO_DEVT2INSTANCE:
218 217 *result = (void *)0;
219 218 error = DDI_SUCCESS;
220 219 break;
221 220 default:
222 221 error = DDI_FAILURE;
223 222 }
224 223 return (error);
225 224 }
226 225
227 226
228 227 /*
229 228 * Open the slave side of a pty.
230 229 */
231 230 /*ARGSUSED*/
232 231 static int
233 232 ptslopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *cred)
234 233 {
235 234 minor_t unit;
236 235 dev_t dev = *devp;
237 236 struct pty *pty;
238 237
239 238 unit = getminor(dev);
240 239 if (unit >= npty)
241 240 return (ENXIO);
242 241
243 242 pty = &pty_softc[unit];
244 243
245 244 mutex_enter(&pty->ptc_lock);
246 245 /*
247 246 * Block waiting for controller to open, unless this is a no-delay
248 247 * open.
249 248 */
250 249 again:
251 250 if (pty->pt_ttycommon.t_writeq == NULL) {
252 251 pty->pt_ttycommon.t_iflag = 0;
253 252 pty->pt_ttycommon.t_cflag = (B38400 << IBSHIFT)|B38400|IFLAGS;
254 253 pty->pt_ttycommon.t_iocpending = NULL;
255 254 pty->pt_wbufcid = 0;
256 255 pty->pt_ttycommon.t_size.ws_row = 0;
257 256 pty->pt_ttycommon.t_size.ws_col = 0;
258 257 pty->pt_ttycommon.t_size.ws_xpixel = 0;
259 258 pty->pt_ttycommon.t_size.ws_ypixel = 0;
260 259 } else if ((pty->pt_ttycommon.t_flags & TS_XCLUDE) &&
261 260 secpolicy_excl_open(cred) != 0) {
262 261 mutex_exit(&pty->ptc_lock);
263 262 return (EBUSY);
264 263 }
265 264 if (!(flag & (FNONBLOCK|FNDELAY)) &&
266 265 !(pty->pt_ttycommon.t_cflag & CLOCAL)) {
267 266 if (!(pty->pt_flags & PF_CARR_ON)) {
268 267 pty->pt_flags |= PF_WOPEN;
269 268 if (!cv_wait_sig(&pty->pt_cv_flags, &pty->ptc_lock)) {
270 269 pty->pt_flags &= ~PF_WOPEN;
271 270 mutex_exit(&pty->ptc_lock);
272 271 return (EINTR);
273 272 }
274 273 goto again;
275 274 }
276 275 }
277 276
278 277 pty->pt_sdev = dev;
279 278 q->q_ptr = WR(q)->q_ptr = pty;
280 279 pty->pt_flags &= ~PF_SLAVEGONE;
281 280 pty->pt_ttycommon.t_readq = pty->pt_ttycommon.t_writeq = NULL;
282 281
283 282 /*
284 283 * Slave is ready to accept messages but master still can't send
285 284 * messages to the slave queue since it is not plumbed
286 285 * yet. So do qprocson() and finish slave initialization.
287 286 */
288 287
289 288 mutex_exit(&pty->ptc_lock);
290 289
291 290 qprocson(q);
292 291
293 292 /*
294 293 * Now it is safe to send messages to q, so wakeup master possibly
295 294 * waiting for slave queue to finish open.
296 295 */
297 296 mutex_enter(&pty->ptc_lock);
298 297 /*
299 298 * queue has already been setup with a pointer to
300 299 * the stream head that is being referenced
301 300 */
302 301 pty->pt_vnode = strq2vp(q);
303 302 VN_RELE(pty->pt_vnode);
304 303 pty->pt_ttycommon.t_readq = q;
305 304 pty->pt_ttycommon.t_writeq = WR(q);
306 305 /* tell master device that slave is ready for writing */
307 306 if (pty->pt_flags & PF_CARR_ON)
308 307 cv_broadcast(&pty->pt_cv_readq);
309 308 mutex_exit(&pty->ptc_lock);
310 309
311 310 return (0);
312 311 }
313 312
314 313 static int
315 314 ptslclose(queue_t *q, int flag, cred_t *cred)
316 315 {
317 316 struct pty *pty;
318 317 bufcall_id_t pt_wbufcid = 0;
319 318
320 319 #ifdef lint
321 320 flag = flag;
322 321 cred = cred;
323 322 #endif
324 323
325 324 if ((pty = (struct pty *)q->q_ptr) == NULL)
326 325 return (ENODEV); /* already been closed once */
327 326
328 327 /*
329 328 * Prevent the queues from being uses by master device.
330 329 * This should be done before qprocsoff or writer may attempt
331 330 * to use the slave queue after qprocsoff removed it from the stream and
332 331 * before entering mutex_enter().
333 332 */
334 333 mutex_enter(&pty->ptc_lock);
335 334 pty->pt_ttycommon.t_readq = NULL;
336 335 pty->pt_ttycommon.t_writeq = NULL;
337 336 while (pty->pt_flags & PF_IOCTL) {
338 337 pty->pt_flags |= PF_WAIT;
339 338 cv_wait(&pty->pt_cv_flags, &pty->ptc_lock);
340 339 }
341 340 pty->pt_vnode = NULL;
342 341 mutex_exit(&pty->ptc_lock);
343 342
344 343 qprocsoff(q);
345 344
346 345 mutex_enter(&pty->ptc_lock);
347 346 /*
348 347 * ptc_lock mutex is not dropped across
349 348 * the call to the routine ttycommon_close
350 349 */
351 350 ttycommon_close(&pty->pt_ttycommon);
352 351
353 352 /*
354 353 * Cancel outstanding "bufcall" request.
355 354 */
356 355 if (pty->pt_wbufcid) {
357 356 pt_wbufcid = pty->pt_wbufcid;
358 357 pty->pt_wbufcid = 0;
359 358 }
360 359
361 360 /*
362 361 * Clear out all the slave-side state.
363 362 */
364 363 pty->pt_flags &= ~(PF_WOPEN|PF_STOPPED|PF_NOSTOP);
365 364 if (pty->pt_flags & PF_CARR_ON) {
366 365 pty->pt_flags |= PF_SLAVEGONE; /* let the controller know */
367 366 ptcpollwakeup(pty, 0); /* wake up readers/selectors */
368 367 ptcpollwakeup(pty, FWRITE); /* wake up writers/selectors */
369 368 cv_broadcast(&pty->pt_cv_flags);
370 369 }
371 370 pty->pt_sdev = 0;
372 371 q->q_ptr = WR(q)->q_ptr = NULL;
373 372 mutex_exit(&pty->ptc_lock);
374 373
375 374 if (pt_wbufcid)
376 375 unbufcall(pt_wbufcid);
377 376
378 377 return (0);
379 378 }
380 379
381 380 /*
382 381 * Put procedure for write queue.
383 382 * Respond to M_STOP, M_START, M_IOCTL, and M_FLUSH messages here;
384 383 * queue up M_DATA messages for processing by the controller "read"
385 384 * routine; discard everything else.
386 385 */
387 386 static void
388 387 ptslwput(queue_t *q, mblk_t *mp)
389 388 {
390 389 struct pty *pty;
391 390 mblk_t *bp;
392 391
393 392 pty = (struct pty *)q->q_ptr;
394 393
395 394 mutex_enter(&pty->ptc_lock);
396 395
397 396 switch (mp->b_datap->db_type) {
398 397
399 398 case M_STOP:
400 399 if (!(pty->pt_flags & PF_STOPPED)) {
401 400 pty->pt_flags |= PF_STOPPED;
402 401 pty->pt_send |= TIOCPKT_STOP;
403 402 ptcpollwakeup(pty, 0);
404 403 }
405 404 freemsg(mp);
406 405 break;
407 406
408 407 case M_START:
409 408 if (pty->pt_flags & PF_STOPPED) {
410 409 pty->pt_flags &= ~PF_STOPPED;
411 410 pty->pt_send = TIOCPKT_START;
412 411 ptcpollwakeup(pty, 0);
413 412 }
414 413 ptcpollwakeup(pty, FREAD); /* permit controller to read */
415 414 freemsg(mp);
416 415 break;
417 416
418 417 case M_IOCTL:
419 418 ptslioctl(pty, q, mp);
420 419 break;
421 420
422 421 case M_FLUSH:
423 422 if (*mp->b_rptr & FLUSHW) {
424 423 /*
425 424 * Set the "flush write" flag, so that we
426 425 * notify the controller if they're in packet
427 426 * or user control mode.
428 427 */
429 428 if (!(pty->pt_send & TIOCPKT_FLUSHWRITE)) {
430 429 pty->pt_send |= TIOCPKT_FLUSHWRITE;
431 430 ptcpollwakeup(pty, 0);
432 431 }
433 432 /*
434 433 * Flush our write queue.
435 434 */
436 435 flushq(q, FLUSHDATA); /* XXX doesn't flush M_DELAY */
437 436 *mp->b_rptr &= ~FLUSHW; /* it has been flushed */
438 437 }
439 438 if (*mp->b_rptr & FLUSHR) {
440 439 /*
441 440 * Set the "flush read" flag, so that we
442 441 * notify the controller if they're in packet
443 442 * mode.
444 443 */
445 444 if (!(pty->pt_send & TIOCPKT_FLUSHREAD)) {
446 445 pty->pt_send |= TIOCPKT_FLUSHREAD;
447 446 ptcpollwakeup(pty, 0);
448 447 }
449 448 flushq(RD(q), FLUSHDATA);
450 449 mutex_exit(&pty->ptc_lock);
451 450 qreply(q, mp); /* give the read queues a crack at it */
452 451 return;
453 452 } else
454 453 freemsg(mp);
455 454 break;
456 455
457 456 case M_DATA:
458 457 /*
459 458 * Throw away any leading zero-length blocks, and queue it up
460 459 * for the controller to read.
461 460 */
462 461 if (pty->pt_flags & PF_CARR_ON) {
463 462 bp = mp;
464 463 while ((bp->b_wptr - bp->b_rptr) == 0) {
465 464 mp = bp->b_cont;
466 465 freeb(bp);
467 466 if (mp == NULL) {
468 467 mutex_exit(&pty->ptc_lock);
469 468 return; /* damp squib of a message */
470 469 }
471 470 bp = mp;
472 471 }
473 472 (void) putq(q, mp);
474 473 ptcpollwakeup(pty, FREAD); /* soup's on! */
475 474 } else
476 475 freemsg(mp); /* nobody listening */
477 476 break;
478 477
479 478 case M_CTL:
480 479 if ((*(int *)mp->b_rptr) == MC_CANONQUERY) {
481 480 /*
482 481 * We're being asked whether we do canonicalization
483 482 * or not. Send a reply back up indicating whether
484 483 * we do or not.
485 484 */
486 485 (void) putctl1(RD(q), M_CTL,
487 486 (pty->pt_flags & PF_REMOTE) ?
488 487 MC_NOCANON : MC_DOCANON);
489 488 }
490 489 freemsg(mp);
491 490 break;
492 491
493 492 default:
494 493 /*
495 494 * "No, I don't want a subscription to Chain Store Age,
496 495 * thank you anyway."
497 496 */
498 497 freemsg(mp);
499 498 break;
500 499 }
501 500 mutex_exit(&pty->ptc_lock);
502 501 }
503 502
504 503 /*
505 504 * Retry an "ioctl", now that "bufcall" claims we may be able to allocate
506 505 * the buffer we need.
507 506 */
508 507 static void
509 508 ptslreioctl(void *arg)
510 509 {
511 510 struct pty *pty = arg;
512 511 queue_t *q;
513 512 mblk_t *mp;
514 513
515 514 mutex_enter(&pty->ptc_lock);
516 515 /*
517 516 * The bufcall is no longer pending.
518 517 */
519 518 if (pty->pt_wbufcid == 0) {
520 519 mutex_exit(&pty->ptc_lock);
521 520 return;
522 521 }
523 522
524 523 pty->pt_wbufcid = 0;
525 524 if ((q = pty->pt_ttycommon.t_writeq) == NULL) {
526 525 mutex_exit(&pty->ptc_lock);
527 526 return;
528 527 }
529 528 if ((mp = pty->pt_ttycommon.t_iocpending) != NULL) {
530 529 /* It's not pending any more. */
531 530 pty->pt_ttycommon.t_iocpending = NULL;
532 531 ptslioctl(pty, q, mp);
533 532 }
534 533 mutex_exit(&pty->ptc_lock);
535 534 }
536 535
537 536 /*
538 537 * Process an "ioctl" message sent down to us.
539 538 * Drops pty's ptc_lock mutex and then reacquire
540 539 */
541 540 static void
542 541 ptslioctl(struct pty *pty, queue_t *q, mblk_t *mp)
543 542 {
544 543 struct iocblk *iocp;
545 544 int cmd;
546 545 size_t datasize;
547 546 int error = 0;
548 547
549 548 ASSERT(MUTEX_HELD(&pty->ptc_lock));
550 549
551 550 iocp = (struct iocblk *)mp->b_rptr;
552 551 cmd = iocp->ioc_cmd;
553 552
554 553 switch (cmd) {
555 554
556 555 case TIOCSTI: {
557 556 /*
558 557 * The permission checking has already been done at the stream
559 558 * head, since it has to be done in the context of the process
560 559 * doing the call.
561 560 */
562 561 mblk_t *bp;
563 562
564 563 error = miocpullup(mp, sizeof (char));
565 564 if (error != 0)
566 565 goto out;
567 566
568 567 /*
569 568 * Simulate typing of a character at the terminal.
570 569 */
571 570 if ((bp = allocb(1, BPRI_MED)) != NULL) {
572 571 *bp->b_wptr++ = *mp->b_cont->b_rptr;
573 572 if (!(pty->pt_flags & PF_REMOTE)) {
574 573 if (!canput(pty->pt_ttycommon.t_readq)) {
575 574 mutex_exit(&pty->ptc_lock);
576 575 ttycommon_qfull(&pty->pt_ttycommon, q);
577 576 mutex_enter(&pty->ptc_lock);
578 577 freemsg(bp);
579 578 error = EAGAIN;
580 579 goto out;
581 580 } else
582 581 (void) putq(
583 582 pty->pt_ttycommon.t_readq, bp);
584 583 } else {
585 584 if (pty->pt_flags & PF_UCNTL) {
586 585 /*
587 586 * XXX - flow control; don't overflow
588 587 * this "queue".
589 588 */
590 589 if (pty->pt_stuffqfirst != NULL) {
591 590 pty->pt_stuffqlast->b_next = bp;
592 591 bp->b_prev = pty->pt_stuffqlast;
593 592 } else {
594 593 pty->pt_stuffqfirst = bp;
595 594 bp->b_prev = NULL;
596 595 }
597 596 bp->b_next = NULL;
598 597 pty->pt_stuffqlast = bp;
599 598 pty->pt_stuffqlen++;
600 599 ptcpollwakeup(pty, 0);
601 600 }
602 601 }
603 602 } else {
604 603 error = EAGAIN;
605 604 goto out;
606 605 }
607 606
608 607 /*
609 608 * Turn the ioctl message into an ioctl ACK message.
610 609 */
611 610 iocp->ioc_count = 0; /* no data returned */
612 611 mp->b_datap->db_type = M_IOCACK;
613 612 goto out;
614 613 }
615 614
616 615 case TIOCSSIZE: {
617 616 tty_common_t *tc = &pty->pt_ttycommon;
618 617 struct ttysize *tp;
619 618
620 619 error = miocpullup(mp, sizeof (struct ttysize));
621 620 if (error != 0)
622 621 goto out;
623 622
624 623 /*
625 624 * Set the window size, but don't send a SIGWINCH.
626 625 */
627 626 tp = (struct ttysize *)mp->b_cont->b_rptr;
628 627 tc->t_size.ws_row = tp->ts_lines;
629 628 tc->t_size.ws_col = tp->ts_cols;
630 629 tc->t_size.ws_xpixel = 0;
631 630 tc->t_size.ws_ypixel = 0;
632 631
633 632 /*
634 633 * Send an ACK back.
635 634 */
636 635 iocp->ioc_count = 0; /* no data returned */
637 636 mp->b_datap->db_type = M_IOCACK;
638 637 goto out;
639 638 }
640 639
641 640 case TIOCGSIZE: {
642 641 tty_common_t *tc = &pty->pt_ttycommon;
643 642 mblk_t *datap;
644 643 struct ttysize *tp;
645 644
646 645 if ((datap = allocb(sizeof (struct ttysize),
647 646 BPRI_HI)) == NULL) {
648 647 if (pty->pt_wbufcid) {
649 648 if (pty->pt_ttycommon.t_iocpending)
650 649 freemsg(pty->pt_ttycommon.t_iocpending);
651 650 pty->pt_ttycommon.t_iocpending = mp;
652 651 return;
653 652 }
654 653 pty->pt_wbufcid = bufcall(sizeof (struct ttysize),
655 654 BPRI_HI, ptslreioctl, pty);
656 655 if (pty->pt_wbufcid == 0) {
657 656 error = ENOMEM;
658 657 goto out;
659 658 }
660 659 pty->pt_ttycommon.t_iocpending = mp;
661 660 return;
662 661 }
663 662 /*
664 663 * Return the current size.
665 664 */
666 665 tp = (struct ttysize *)datap->b_wptr;
667 666 tp->ts_lines = tc->t_size.ws_row;
668 667 tp->ts_cols = tc->t_size.ws_col;
669 668 datap->b_wptr += sizeof (struct ttysize);
670 669 iocp->ioc_count = sizeof (struct ttysize);
671 670
672 671 if (mp->b_cont != NULL)
673 672 freemsg(mp->b_cont);
674 673 mp->b_cont = datap;
675 674 mp->b_datap->db_type = M_IOCACK;
676 675 goto out;
677 676 }
678 677
679 678 /*
680 679 * Imported from ttycommon_ioctl routine
681 680 */
682 681
683 682 case TCSETSF: {
684 683 tty_common_t *tc = &pty->pt_ttycommon;
685 684 struct termios *cb;
686 685
687 686 error = miocpullup(mp, sizeof (struct termios));
688 687 if (error != 0)
689 688 goto out;
690 689
691 690 cb = (struct termios *)mp->b_cont->b_rptr;
692 691
693 692 flushq(RD(q), FLUSHDATA);
694 693 mutex_exit(&pty->ptc_lock);
695 694 (void) putnextctl1(RD(q), M_FLUSH, FLUSHR);
696 695 mutex_enter(&pty->ptc_lock);
697 696 mutex_enter(&tc->t_excl);
698 697 tc->t_iflag = cb->c_iflag;
699 698 tc->t_cflag = cb->c_cflag;
700 699 tc->t_stopc = cb->c_cc[VSTOP];
701 700 tc->t_startc = cb->c_cc[VSTART];
702 701 mutex_exit(&tc->t_excl);
703 702
704 703 /*
705 704 * Turn the ioctl message into an ioctl ACK message.
706 705 */
707 706 iocp->ioc_count = 0; /* no data returned */
708 707 mp->b_datap->db_type = M_IOCACK;
709 708 goto ioctldone;
710 709 }
711 710
712 711 case TCSETAF: {
713 712 tty_common_t *tc = &pty->pt_ttycommon;
714 713 struct termios *cb;
715 714
716 715 error = miocpullup(mp, sizeof (struct termios));
717 716 if (error != 0)
718 717 goto out;
719 718
720 719 cb = (struct termios *)mp->b_cont->b_rptr;
721 720
722 721 flushq(RD(q), FLUSHDATA);
723 722 mutex_exit(&pty->ptc_lock);
724 723 (void) putnextctl1(RD(q), M_FLUSH, FLUSHR);
725 724 mutex_enter(&pty->ptc_lock);
726 725 mutex_enter(&tc->t_excl);
727 726 tc->t_iflag = (tc->t_iflag & 0xffff0000 | cb->c_iflag);
728 727 tc->t_cflag = (tc->t_cflag & 0xffff0000 | cb->c_cflag);
729 728 mutex_exit(&tc->t_excl);
730 729
731 730 /*
732 731 * Turn the ioctl message into an ioctl ACK message.
733 732 */
734 733 iocp->ioc_count = 0; /* no data returned */
735 734 mp->b_datap->db_type = M_IOCACK;
736 735 goto ioctldone;
737 736 }
738 737
739 738 case TIOCSWINSZ: {
740 739 tty_common_t *tc = &pty->pt_ttycommon;
741 740 struct winsize *ws;
742 741
743 742 error = miocpullup(mp, sizeof (struct winsize));
744 743 if (error != 0)
745 744 goto out;
746 745
747 746 ws = (struct winsize *)mp->b_cont->b_rptr;
748 747 /*
749 748 * If the window size changed, send a SIGWINCH.
750 749 */
751 750 mutex_enter(&tc->t_excl);
752 751 if (bcmp(&tc->t_size, ws, sizeof (struct winsize))) {
753 752 tc->t_size = *ws;
754 753 mutex_exit(&tc->t_excl);
755 754 mutex_exit(&pty->ptc_lock);
756 755 (void) putnextctl1(RD(q), M_PCSIG, SIGWINCH);
757 756 mutex_enter(&pty->ptc_lock);
758 757 } else
759 758 mutex_exit(&tc->t_excl);
760 759
761 760 /*
762 761 * Turn the ioctl message into an ioctl ACK message.
763 762 */
764 763 iocp->ioc_count = 0; /* no data returned */
765 764 mp->b_datap->db_type = M_IOCACK;
766 765 goto ioctldone;
767 766 }
768 767
769 768 /*
770 769 * If they were just trying to drain output, that's OK.
771 770 * If they are actually trying to send a break it's an error.
772 771 */
773 772 case TCSBRK:
774 773 error = miocpullup(mp, sizeof (int));
775 774 if (error != 0)
776 775 goto out;
777 776
778 777 if (*(int *)mp->b_cont->b_rptr != 0) {
779 778 /*
780 779 * Turn the ioctl message into an ioctl ACK message.
781 780 */
782 781 iocp->ioc_count = 0; /* no data returned */
783 782 mp->b_datap->db_type = M_IOCACK;
784 783 } else {
785 784 error = ENOTTY;
786 785 }
787 786 goto out;
788 787 }
789 788
790 789 /*
791 790 * The only way in which "ttycommon_ioctl" can fail is if the "ioctl"
792 791 * requires a response containing data to be returned to the user,
793 792 * and no mblk could be allocated for the data.
794 793 * No such "ioctl" alters our state. Thus, we always go ahead and
795 794 * do any state-changes the "ioctl" calls for. If we couldn't allocate
796 795 * the data, "ttycommon_ioctl" has stashed the "ioctl" away safely, so
797 796 * we just call "bufcall" to request that we be called back when we
798 797 * stand a better chance of allocating the data.
799 798 */
800 799 if ((datasize =
801 800 ttycommon_ioctl(&pty->pt_ttycommon, q, mp, &error)) != 0) {
802 801 if (pty->pt_wbufcid) {
803 802 if (pty->pt_ttycommon.t_iocpending)
804 803 freemsg(pty->pt_ttycommon.t_iocpending);
805 804 pty->pt_ttycommon.t_iocpending = mp;
806 805 return;
807 806 }
808 807 pty->pt_wbufcid = bufcall(datasize, BPRI_HI, ptslreioctl, pty);
809 808 if (pty->pt_wbufcid == 0) {
810 809 error = ENOMEM;
811 810 goto out;
812 811 }
813 812 pty->pt_ttycommon.t_iocpending = mp;
814 813 return;
815 814 }
816 815
817 816 ioctldone:
818 817 if (error == 0) {
819 818 /*
820 819 * "ttycommon_ioctl" did most of the work; we just use the
821 820 * data it set up.
822 821 */
823 822 switch (cmd) {
824 823
825 824 case TCSETSF:
826 825 case TCSETAF:
827 826 /*
828 827 * Set the "flush read" flag, so that we
829 828 * notify the controller if they're in packet
830 829 * mode.
831 830 */
832 831 if (!(pty->pt_send & TIOCPKT_FLUSHREAD)) {
833 832 pty->pt_send |= TIOCPKT_FLUSHREAD;
834 833 ptcpollwakeup(pty, 0);
835 834 }
836 835 /*FALLTHROUGH*/
837 836
838 837 case TCSETSW:
839 838 case TCSETAW:
840 839 cmd = TIOCSETP; /* map backwards to old codes */
841 840 pt_sendstop(pty);
842 841 break;
843 842
844 843 case TCSETS:
845 844 case TCSETA:
846 845 cmd = TIOCSETN; /* map backwards to old codes */
847 846 pt_sendstop(pty);
848 847 break;
849 848 }
850 849 }
851 850
852 851 if (pty->pt_flags & PF_43UCNTL) {
853 852 if (error < 0) {
854 853 if ((cmd & ~0xff) == _IO('u', 0)) {
855 854 if (cmd & 0xff) {
856 855 pty->pt_ucntl = (uchar_t)cmd & 0xff;
857 856 ptcpollwakeup(pty, FREAD);
858 857 }
859 858 error = 0; /* XXX */
860 859 goto out;
861 860 }
862 861 error = ENOTTY;
863 862 }
864 863 } else {
865 864 if ((pty->pt_flags & PF_UCNTL) &&
866 865 (cmd & (IOC_INOUT | 0xff00)) == (IOC_IN|('t'<<8)) &&
867 866 (cmd & 0xff)) {
868 867 pty->pt_ucntl = (uchar_t)cmd & 0xff;
869 868 ptcpollwakeup(pty, FREAD);
870 869 goto out;
871 870 }
872 871 if (error < 0)
873 872 error = ENOTTY;
874 873 }
875 874
876 875 out:
877 876 if (error != 0) {
878 877 ((struct iocblk *)mp->b_rptr)->ioc_error = error;
879 878 mp->b_datap->db_type = M_IOCNAK;
880 879 }
881 880
882 881 mutex_exit(&pty->ptc_lock);
883 882 qreply(q, mp);
884 883 mutex_enter(&pty->ptc_lock);
885 884 }
886 885
887 886 /*
888 887 * Service routine for read queue.
889 888 * Just wakes the controller side up so it can write some more data
890 889 * to that queue.
891 890 */
892 891 static int
893 892 ptslrserv(queue_t *q)
894 893 {
895 894 struct pty *pty = (struct pty *)q->q_ptr;
896 895 mblk_t *mp;
897 896 mblk_t *head = NULL, *tail = NULL;
898 897 /*
899 898 * Build up the link list of messages, then drop
900 899 * drop the lock and do putnext()
901 900 */
902 901 mutex_enter(&pty->ptc_lock);
903 902
904 903 while ((mp = getq(q)) != NULL) {
905 904 if ((mp->b_datap->db_type < QPCTL) && !canputnext(q)) {
906 905 (void) putbq(q, mp);
907 906 break;
908 907 }
909 908 if (!head) {
910 909 head = mp;
911 910 tail = mp;
912 911 } else {
913 912 tail->b_next = mp;
914 913 tail = mp;
915 914 }
916 915 }
917 916
918 917 if (q->q_count <= q->q_lowat)
919 918 ptcpollwakeup((struct pty *)q->q_ptr, FWRITE);
920 919
921 920 mutex_exit(&pty->ptc_lock);
922 921
923 922 while (head) {
924 923 mp = head;
925 924 head = mp->b_next;
926 925 mp->b_next = NULL;
927 926 putnext(q, mp);
928 927 }
929 928
930 929 return (0);
931 930 }
932 931
933 932 static void
934 933 pt_sendstop(struct pty *pty)
935 934 {
936 935 int stop;
937 936
938 937 ASSERT(MUTEX_HELD(&pty->ptc_lock));
939 938
940 939 if ((pty->pt_ttycommon.t_cflag&CBAUD) == 0) {
941 940 if (pty->pt_flags & PF_CARR_ON) {
942 941 /*
943 942 * Let the controller know, then wake up
944 943 * readers/selectors and writers/selectors.
945 944 */
946 945 pty->pt_flags |= PF_SLAVEGONE;
947 946 ptcpollwakeup(pty, 0);
948 947 ptcpollwakeup(pty, FWRITE);
949 948 }
950 949 }
951 950
952 951 stop = (pty->pt_ttycommon.t_iflag & IXON) &&
953 952 pty->pt_ttycommon.t_stopc == CTRL('s') &&
954 953 pty->pt_ttycommon.t_startc == CTRL('q');
955 954
956 955 if (pty->pt_flags & PF_NOSTOP) {
957 956 if (stop) {
958 957 pty->pt_send &= ~TIOCPKT_NOSTOP;
959 958 pty->pt_send |= TIOCPKT_DOSTOP;
960 959 pty->pt_flags &= ~PF_NOSTOP;
961 960 ptcpollwakeup(pty, 0);
962 961 }
963 962 } else {
964 963 if (!stop) {
965 964 pty->pt_send &= ~TIOCPKT_DOSTOP;
966 965 pty->pt_send |= TIOCPKT_NOSTOP;
967 966 pty->pt_flags |= PF_NOSTOP;
968 967 ptcpollwakeup(pty, 0);
969 968 }
970 969 }
971 970 }
972 971
973 972 /*
974 973 * Wake up controller side. "flag" is 0 if a special packet or
975 974 * user control mode message has been queued up (this data is readable,
976 975 * so we also treat it as a regular data event; should we send SIGIO,
977 976 * though?), FREAD if regular data has been queued up, or FWRITE if
978 977 * the slave's read queue has drained sufficiently to allow writing.
979 978 */
980 979 static void
981 980 ptcpollwakeup(struct pty *pty, int flag)
982 981 {
983 982 ASSERT(MUTEX_HELD(&pty->ptc_lock));
984 983
985 984 if (flag == 0) {
986 985 /*
987 986 * "Exceptional condition" occurred. This means that
988 987 * a "read" is now possible, so do a "read" wakeup.
989 988 */
990 989 flag = FREAD;
991 990 pollwakeup(&ptcph, POLLIN | POLLRDBAND);
992 991 if (pty->pt_flags & PF_ASYNC)
993 992 gsignal(pty->pt_pgrp, SIGURG);
994 993 }
995 994 if (flag & FREAD) {
996 995 /*
997 996 * Wake up the parent process as there is regular
998 997 * data to read from slave's write queue
999 998 */
1000 999 pollwakeup(&ptcph, POLLIN | POLLRDNORM);
1001 1000 cv_broadcast(&pty->pt_cv_writeq);
1002 1001 if (pty->pt_flags & PF_ASYNC)
1003 1002 gsignal(pty->pt_pgrp, SIGIO);
1004 1003 }
1005 1004 if (flag & FWRITE) {
1006 1005 /*
1007 1006 * Wake up the parent process to write
1008 1007 * data into slave's read queue as the
1009 1008 * read queue has drained enough
1010 1009 */
1011 1010 pollwakeup(&ptcph, POLLOUT | POLLWRNORM);
1012 1011 cv_broadcast(&pty->pt_cv_readq);
1013 1012 if (pty->pt_flags & PF_ASYNC)
1014 1013 gsignal(pty->pt_pgrp, SIGIO);
1015 1014 }
1016 1015 }
↓ open down ↓ |
855 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX