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