Repository navigation
Expand file tree
/
Copy pathtty.c
More file actions
1979 lines (1747 loc) · 59.4 KB
/
Copy pathtty.c
File metadata and controls
1979 lines (1747 loc) · 59.4 KB
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
/* This file contains the terminal driver, both for the IBM console and regular
* ASCII terminals. It handles only the device-independent part of a TTY, the
* device dependent parts are in console.c, rs232.c, etc. This file contains
* two main entry points, tty_task() and tty_wakeup(), and several minor entry
* points for use by the device-dependent code.
*
* The device-independent part accepts "keyboard" input from the device-
* dependent part, performs input processing (special key interpretation),
* and sends the input to a process reading from the TTY. Output to a TTY
* is sent to the device-dependent code for output processing and "screen"
* display. Input processing is done by the device by calling 'in_process'
* on the input characters, output processing may be done by the device itself
* or by calling 'out_process'. The TTY takes care of input queuing, the
* device does the output queuing. If a device receives an external signal,
* like an interrupt, then it causes tty_wakeup() to be run by the CLOCK task
* to, you guessed it, wake up the TTY to check if input or output can
* continue.
*
* The valid messages and their parameters are:
*
* HARD_INT: output has been completed or input has arrived
* SYS_SIG: e.g., MINIX wants to shutdown; run code to cleanly stop
* DEV_READ: a process wants to read from a terminal
* DEV_WRITE: a process wants to write on a terminal
* DEV_IOCTL: a process wants to change a terminal's parameters
* DEV_OPEN: a tty line has been opened
* DEV_CLOSE: a tty line has been closed
* DEV_SELECT: start select notification request
* DEV_STATUS: FS wants to know status for SELECT or REVIVE
* CANCEL: terminate a previous incomplete system call immediately
*
* m_type TTY_LINE PROC_NR COUNT TTY_SPEK TTY_FLAGS ADDRESS
* ---------------------------------------------------------------------------
* | HARD_INT | | | | | | |
* |-------------+---------+---------+---------+---------+---------+---------|
* | SYS_SIG | sig set | | | | | |
* |-------------+---------+---------+---------+---------+---------+---------|
* | DEV_READ |minor dev| proc nr | count | O_NONBLOCK| buf ptr |
* |-------------+---------+---------+---------+---------+---------+---------|
* | DEV_WRITE |minor dev| proc nr | count | | | buf ptr |
* |-------------+---------+---------+---------+---------+---------+---------|
* | DEV_IOCTL |minor dev| proc nr |func code|erase etc| flags | |
* |-------------+---------+---------+---------+---------+---------+---------|
* | DEV_OPEN |minor dev| proc nr | O_NOCTTY| | | |
* |-------------+---------+---------+---------+---------+---------+---------|
* | DEV_CLOSE |minor dev| proc nr | | | | |
* |-------------+---------+---------+---------+---------+---------+---------|
* | DEV_STATUS | | | | | | |
* |-------------+---------+---------+---------+---------+---------+---------|
* | CANCEL |minor dev| proc nr | | | | |
* ---------------------------------------------------------------------------
*
* Changes:
* Jan 20, 2004 moved TTY driver to user-space (Jorrit N. Herder)
* Sep 20, 2004 local timer management/ sync alarms (Jorrit N. Herder)
* Jul 13, 2004 support for function key observers (Jorrit N. Herder)
*/
#include "../drivers.h"
#include "../drivers.h"
#include <termios.h>
#if ENABLE_SRCCOMPAT || ENABLE_BINCOMPAT
#include <sgtty.h>
#endif
#include <sys/ioc_tty.h>
#include <signal.h>
#include <minix/callnr.h>
#if (CHIP == INTEL)
#include <minix/keymap.h>
#endif
#include "tty.h"
#include <sys/time.h>
#include <sys/select.h>
extern int irq_hook_id;
unsigned long kbd_irq_set = 0;
unsigned long rs_irq_set = 0;
/* Address of a tty structure. */
#define tty_addr(line) (&tty_table[line])
/* Macros for magic tty types. */
#define isconsole(tp) ((tp) < tty_addr(NR_CONS))
#define ispty(tp) ((tp) >= tty_addr(NR_CONS+NR_RS_LINES))
/* Macros for magic tty structure pointers. */
#define FIRST_TTY tty_addr(0)
#define END_TTY tty_addr(sizeof(tty_table) / sizeof(tty_table[0]))
/* A device exists if at least its 'devread' function is defined. */
#define tty_active(tp) ((tp)->tty_devread != NULL)
/* RS232 lines or pseudo terminals can be completely configured out. */
#if NR_RS_LINES == 0
#define rs_init(tp) ((void) 0)
#endif
#if NR_PTYS == 0
#define pty_init(tp) ((void) 0)
#define do_pty(tp, mp) ((void) 0)
#endif
FORWARD _PROTOTYPE( void tty_timed_out, (timer_t *tp) );
FORWARD _PROTOTYPE( void expire_timers, (void) );
FORWARD _PROTOTYPE( void settimer, (tty_t *tty_ptr, int enable) );
FORWARD _PROTOTYPE( void do_cancel, (tty_t *tp, message *m_ptr) );
FORWARD _PROTOTYPE( void do_ioctl, (tty_t *tp, message *m_ptr) );
FORWARD _PROTOTYPE( void do_open, (tty_t *tp, message *m_ptr) );
FORWARD _PROTOTYPE( void do_close, (tty_t *tp, message *m_ptr) );
FORWARD _PROTOTYPE( void do_read, (tty_t *tp, message *m_ptr) );
FORWARD _PROTOTYPE( void do_write, (tty_t *tp, message *m_ptr) );
FORWARD _PROTOTYPE( void do_select, (tty_t *tp, message *m_ptr) );
FORWARD _PROTOTYPE( void do_status, (message *m_ptr) );
FORWARD _PROTOTYPE( void in_transfer, (tty_t *tp) );
FORWARD _PROTOTYPE( int tty_echo, (tty_t *tp, int ch) );
FORWARD _PROTOTYPE( void rawecho, (tty_t *tp, int ch) );
FORWARD _PROTOTYPE( int back_over, (tty_t *tp) );
FORWARD _PROTOTYPE( void reprint, (tty_t *tp) );
FORWARD _PROTOTYPE( void dev_ioctl, (tty_t *tp) );
FORWARD _PROTOTYPE( void setattr, (tty_t *tp) );
FORWARD _PROTOTYPE( void tty_icancel, (tty_t *tp) );
FORWARD _PROTOTYPE( void tty_init, (void) );
#if ENABLE_SRCCOMPAT || ENABLE_BINCOMPAT
FORWARD _PROTOTYPE( int compat_getp, (tty_t *tp, struct sgttyb *sg) );
FORWARD _PROTOTYPE( int compat_getc, (tty_t *tp, struct tchars *sg) );
FORWARD _PROTOTYPE( int compat_setp, (tty_t *tp, struct sgttyb *sg) );
FORWARD _PROTOTYPE( int compat_setc, (tty_t *tp, struct tchars *sg) );
FORWARD _PROTOTYPE( int tspd2sgspd, (speed_t tspd) );
FORWARD _PROTOTYPE( speed_t sgspd2tspd, (int sgspd) );
#if ENABLE_BINCOMPAT
FORWARD _PROTOTYPE( void do_ioctl_compat, (tty_t *tp, message *m_ptr) );
#endif
#endif
/* Default attributes. */
PRIVATE struct termios termios_defaults = {
TINPUT_DEF, TOUTPUT_DEF, TCTRL_DEF, TLOCAL_DEF, TSPEED_DEF, TSPEED_DEF,
{
TEOF_DEF, TEOL_DEF, TERASE_DEF, TINTR_DEF, TKILL_DEF, TMIN_DEF,
TQUIT_DEF, TTIME_DEF, TSUSP_DEF, TSTART_DEF, TSTOP_DEF,
TREPRINT_DEF, TLNEXT_DEF, TDISCARD_DEF,
},
};
PRIVATE struct winsize winsize_defaults; /* = all zeroes */
/* Global variables for the TTY task (declared extern in tty.h). */
PUBLIC tty_t tty_table[NR_CONS+NR_RS_LINES+NR_PTYS];
PUBLIC int ccurrent; /* currently active console */
PUBLIC timer_t *tty_timers; /* queue of TTY timers */
PUBLIC clock_t tty_next_timeout; /* time that the next alarm is due */
PUBLIC struct machine machine; /* kernel environment variables */
/*===========================================================================*
* tty_task *
*===========================================================================*/
PUBLIC void main(void)
{
/* Main routine of the terminal task. */
message tty_mess; /* buffer for all incoming messages */
unsigned line;
int s;
char *types[] = {"task","driver","server", "user"};
register struct proc *rp;
register tty_t *tp;
/* Initialize the TTY driver. */
tty_init();
/* Get kernel environment (protected_mode, pc_at and ega are needed). */
if (OK != (s=sys_getmachine(&machine))) {
panic("TTY","Couldn't obtain kernel environment.", s);
}
/* Final one-time keyboard initialization. */
kb_init_once();
printf("\n");
while (TRUE) {
/* Check for and handle any events on any of the ttys. */
for (tp = FIRST_TTY; tp < END_TTY; tp++) {
if (tp->tty_events) handle_events(tp);
}
/* Get a request message. */
receive(ANY, &tty_mess);
/* First handle all kernel notification types that the TTY supports.
* - An alarm went off, expire all timers and handle the events.
* - A hardware interrupt also is an invitation to check for events.
* - A new kernel message is available for printing.
* - Reset the console on system shutdown.
* Then see if this message is different from a normal device driver
* request and should be handled separately. These extra functions
* do not operate on a device, in constrast to the driver requests.
*/
switch (tty_mess.m_type) {
case SYN_ALARM: /* fall through */
expire_timers(); /* run watchdogs of expired timers */
continue; /* contine to check for events */
case HARD_INT: { /* hardware interrupt notification */
if (tty_mess.NOTIFY_ARG & kbd_irq_set)
kbd_interrupt(&tty_mess);/* fetch chars from keyboard */
#if NR_RS_LINES > 0
if (tty_mess.NOTIFY_ARG & rs_irq_set)
rs_interrupt(&tty_mess);/* serial I/O */
#endif
expire_timers(); /* run watchdogs of expired timers */
continue; /* contine to check for events */
}
case SYS_SIG: { /* system signal */
sigset_t sigset = (sigset_t) tty_mess.NOTIFY_ARG;
if (sigismember(&sigset, SIGKSTOP)) {
cons_stop(); /* switch to primary console */
if (irq_hook_id != -1) {
sys_irqdisable(&irq_hook_id);
sys_irqrmpolicy(KEYBOARD_IRQ, &irq_hook_id);
}
}
if (sigismember(&sigset, SIGTERM)) cons_stop();
if (sigismember(&sigset, SIGKMESS)) do_new_kmess(&tty_mess);
continue;
}
case PANIC_DUMPS: /* allow panic dumps */
cons_stop(); /* switch to primary console */
do_panic_dumps(&tty_mess);
continue;
case DIAGNOSTICS: /* a server wants to print some */
do_diagnostics(&tty_mess);
continue;
case FKEY_CONTROL: /* (un)register a fkey observer */
do_fkey_ctl(&tty_mess);
continue;
default: /* should be a driver request */
; /* do nothing; end switch */
}
/* Only device requests should get to this point. All requests,
* except DEV_STATUS, have a minor device number. Check this
* exception and get the minor device number otherwise.
*/
if (tty_mess.m_type == DEV_STATUS) {
do_status(&tty_mess);
continue;
}
line = tty_mess.TTY_LINE;
if ((line - CONS_MINOR) < NR_CONS) {
tp = tty_addr(line - CONS_MINOR);
} else if (line == LOG_MINOR) {
tp = tty_addr(0);
} else if ((line - RS232_MINOR) < NR_RS_LINES) {
tp = tty_addr(line - RS232_MINOR + NR_CONS);
} else if ((line - TTYPX_MINOR) < NR_PTYS) {
tp = tty_addr(line - TTYPX_MINOR + NR_CONS + NR_RS_LINES);
} else if ((line - PTYPX_MINOR) < NR_PTYS) {
tp = tty_addr(line - PTYPX_MINOR + NR_CONS + NR_RS_LINES);
if (tty_mess.m_type != DEV_IOCTL) {
do_pty(tp, &tty_mess);
continue;
}
} else {
tp = NULL;
}
/* If the device doesn't exist or is not configured return ENXIO. */
if (tp == NULL || ! tty_active(tp)) {
printf("Warning, TTY got illegal request %d from %d\n",
tty_mess.m_type, tty_mess.m_source);
tty_reply(TASK_REPLY, tty_mess.m_source,
tty_mess.PROC_NR, ENXIO);
continue;
}
/* Execute the requested device driver function. */
switch (tty_mess.m_type) {
case DEV_READ: do_read(tp, &tty_mess); break;
case DEV_WRITE: do_write(tp, &tty_mess); break;
case DEV_IOCTL: do_ioctl(tp, &tty_mess); break;
case DEV_OPEN: do_open(tp, &tty_mess); break;
case DEV_CLOSE: do_close(tp, &tty_mess); break;
case DEV_SELECT: do_select(tp, &tty_mess); break;
case CANCEL: do_cancel(tp, &tty_mess); break;
default:
printf("Warning, TTY got unexpected request %d from %d\n",
tty_mess.m_type, tty_mess.m_source);
tty_reply(TASK_REPLY, tty_mess.m_source,
tty_mess.PROC_NR, EINVAL);
}
}
}
/*===========================================================================*
* do_status *
*===========================================================================*/
PRIVATE void do_status(m_ptr)
message *m_ptr;
{
register struct tty *tp;
int event_found;
int status;
int ops;
/* Check for select or revive events on any of the ttys. If we found an,
* event return a single status message for it. The FS will make another
* call to see if there is more.
*/
event_found = 0;
for (tp = FIRST_TTY; tp < END_TTY; tp++) {
if ((ops = select_try(tp, tp->tty_select_ops)) &&
tp->tty_select_proc == m_ptr->m_source) {
/* I/O for a selected minor device is ready. */
m_ptr->m_type = DEV_IO_READY;
m_ptr->DEV_MINOR = tp->tty_index;
m_ptr->DEV_SEL_OPS = ops;
tp->tty_select_ops &= ~ops; /* unmark select event */
event_found = 1;
break;
}
else if (tp->tty_inrevived && tp->tty_incaller == m_ptr->m_source) {
/* Suspended request finished. Send a REVIVE. */
m_ptr->m_type = DEV_REVIVE;
m_ptr->REP_PROC_NR = tp->tty_inproc;
m_ptr->REP_STATUS = tp->tty_incum;
tp->tty_inleft = tp->tty_incum = 0;
tp->tty_inrevived = 0; /* unmark revive event */
event_found = 1;
break;
}
else if (tp->tty_outrevived && tp->tty_outcaller == m_ptr->m_source) {
/* Suspended request finished. Send a REVIVE. */
m_ptr->m_type = DEV_REVIVE;
m_ptr->REP_PROC_NR = tp->tty_outproc;
m_ptr->REP_STATUS = tp->tty_outcum;
tp->tty_outcum = 0;
tp->tty_outrevived = 0; /* unmark revive event */
event_found = 1;
break;
}
}
#if NR_PTYS > 0
if (!event_found)
event_found = pty_status(m_ptr);
#endif
if (! event_found) {
/* No events of interest were found. Return an empty message. */
m_ptr->m_type = DEV_NO_STATUS;
}
/* Almost done. Send back the reply message to the caller. */
if ((status = send(m_ptr->m_source, m_ptr)) != OK) {
panic("TTY","send in do_status failed, status\n", status);
}
}
/*===========================================================================*
* do_read *
*===========================================================================*/
PRIVATE void do_read(tp, m_ptr)
register tty_t *tp; /* pointer to tty struct */
register message *m_ptr; /* pointer to message sent to the task */
{
/* A process wants to read from a terminal. */
int r, status;
phys_bytes phys_addr;
/* Check if there is already a process hanging in a read, check if the
* parameters are correct, do I/O.
*/
if (tp->tty_inleft > 0) {
r = EIO;
} else
if (m_ptr->COUNT <= 0) {
r = EINVAL;
} else
if (sys_umap(m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS, m_ptr->COUNT,
&phys_addr) != OK) {
r = EFAULT;
} else {
/* Copy information from the message to the tty struct. */
tp->tty_inrepcode = TASK_REPLY;
tp->tty_incaller = m_ptr->m_source;
tp->tty_inproc = m_ptr->PROC_NR;
tp->tty_in_vir = (vir_bytes) m_ptr->ADDRESS;
tp->tty_inleft = m_ptr->COUNT;
if (!(tp->tty_termios.c_lflag & ICANON)
&& tp->tty_termios.c_cc[VTIME] > 0) {
if (tp->tty_termios.c_cc[VMIN] == 0) {
/* MIN & TIME specify a read timer that finishes the
* read in TIME/10 seconds if no bytes are available.
*/
settimer(tp, TRUE);
tp->tty_min = 1;
} else {
/* MIN & TIME specify an inter-byte timer that may
* have to be cancelled if there are no bytes yet.
*/
if (tp->tty_eotct == 0) {
settimer(tp, FALSE);
tp->tty_min = tp->tty_termios.c_cc[VMIN];
}
}
}
/* Anything waiting in the input buffer? Clear it out... */
in_transfer(tp);
/* ...then go back for more. */
handle_events(tp);
if (tp->tty_inleft == 0) {
if (tp->tty_select_ops)
select_retry(tp);
return; /* already done */
}
/* There were no bytes in the input queue available, so either suspend
* the caller or break off the read if nonblocking.
*/
if (m_ptr->TTY_FLAGS & O_NONBLOCK) {
r = EAGAIN; /* cancel the read */
tp->tty_inleft = tp->tty_incum = 0;
} else {
r = SUSPEND; /* suspend the caller */
tp->tty_inrepcode = REVIVE;
}
}
tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, r);
if (tp->tty_select_ops)
select_retry(tp);
}
/*===========================================================================*
* do_write *
*===========================================================================*/
PRIVATE void do_write(tp, m_ptr)
register tty_t *tp;
register message *m_ptr; /* pointer to message sent to the task */
{
/* A process wants to write on a terminal. */
int r;
phys_bytes phys_addr;
/* Check if there is already a process hanging in a write, check if the
* parameters are correct, do I/O.
*/
if (tp->tty_outleft > 0) {
r = EIO;
} else
if (m_ptr->COUNT <= 0) {
r = EINVAL;
} else
if (sys_umap(m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS, m_ptr->COUNT,
&phys_addr) != OK) {
r = EFAULT;
} else {
/* Copy message parameters to the tty structure. */
tp->tty_outrepcode = TASK_REPLY;
tp->tty_outcaller = m_ptr->m_source;
tp->tty_outproc = m_ptr->PROC_NR;
tp->tty_out_vir = (vir_bytes) m_ptr->ADDRESS;
tp->tty_outleft = m_ptr->COUNT;
/* Try to write. */
handle_events(tp);
if (tp->tty_outleft == 0)
return; /* already done */
/* None or not all the bytes could be written, so either suspend the
* caller or break off the write if nonblocking.
*/
if (m_ptr->TTY_FLAGS & O_NONBLOCK) { /* cancel the write */
r = tp->tty_outcum > 0 ? tp->tty_outcum : EAGAIN;
tp->tty_outleft = tp->tty_outcum = 0;
} else {
r = SUSPEND; /* suspend the caller */
tp->tty_outrepcode = REVIVE;
}
}
tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, r);
}
/*===========================================================================*
* do_ioctl *
*===========================================================================*/
PRIVATE void do_ioctl(tp, m_ptr)
register tty_t *tp;
message *m_ptr; /* pointer to message sent to task */
{
/* Perform an IOCTL on this terminal. Posix termios calls are handled
* by the IOCTL system call
*/
int r;
union {
int i;
#if ENABLE_SRCCOMPAT
struct sgttyb sg;
struct tchars tc;
#endif
} param;
size_t size;
/* Size of the ioctl parameter. */
switch (m_ptr->TTY_REQUEST) {
case TCGETS: /* Posix tcgetattr function */
case TCSETS: /* Posix tcsetattr function, TCSANOW option */
case TCSETSW: /* Posix tcsetattr function, TCSADRAIN option */
case TCSETSF: /* Posix tcsetattr function, TCSAFLUSH option */
size = sizeof(struct termios);
break;
case TCSBRK: /* Posix tcsendbreak function */
case TCFLOW: /* Posix tcflow function */
case TCFLSH: /* Posix tcflush function */
case TIOCGPGRP: /* Posix tcgetpgrp function */
case TIOCSPGRP: /* Posix tcsetpgrp function */
size = sizeof(int);
break;
case TIOCGWINSZ: /* get window size (not Posix) */
case TIOCSWINSZ: /* set window size (not Posix) */
size = sizeof(struct winsize);
break;
#if ENABLE_SRCCOMPAT
case TIOCGETP: /* BSD-style get terminal properties */
case TIOCSETP: /* BSD-style set terminal properties */
size = sizeof(struct sgttyb);
break;
case TIOCGETC: /* BSD-style get terminal special characters */
case TIOCSETC: /* BSD-style get terminal special characters */
size = sizeof(struct tchars);
break;
#endif
#if (MACHINE == IBM_PC)
case KIOCSMAP: /* load keymap (Minix extension) */
size = sizeof(keymap_t);
break;
case TIOCSFON: /* load font (Minix extension) */
size = sizeof(u8_t [8192]);
break;
#endif
case TCDRAIN: /* Posix tcdrain function -- no parameter */
default: size = 0;
}
r = OK;
switch (m_ptr->TTY_REQUEST) {
case TCGETS:
/* Get the termios attributes. */
r = sys_vircopy(SELF, D, (vir_bytes) &tp->tty_termios,
m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
(vir_bytes) size);
break;
case TCSETSW:
case TCSETSF:
case TCDRAIN:
if (tp->tty_outleft > 0) {
/* Wait for all ongoing output processing to finish. */
tp->tty_iocaller = m_ptr->m_source;
tp->tty_ioproc = m_ptr->PROC_NR;
tp->tty_ioreq = m_ptr->REQUEST;
tp->tty_iovir = (vir_bytes) m_ptr->ADDRESS;
r = SUSPEND;
break;
}
if (m_ptr->TTY_REQUEST == TCDRAIN) break;
if (m_ptr->TTY_REQUEST == TCSETSF) tty_icancel(tp);
/*FALL THROUGH*/
case TCSETS:
/* Set the termios attributes. */
r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
SELF, D, (vir_bytes) &tp->tty_termios, (vir_bytes) size);
if (r != OK) break;
setattr(tp);
break;
case TCFLSH:
r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
SELF, D, (vir_bytes) ¶m.i, (vir_bytes) size);
if (r != OK) break;
switch (param.i) {
case TCIFLUSH: tty_icancel(tp); break;
case TCOFLUSH: (*tp->tty_ocancel)(tp, 0); break;
case TCIOFLUSH: tty_icancel(tp); (*tp->tty_ocancel)(tp, 0); break;
default: r = EINVAL;
}
break;
case TCFLOW:
r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
SELF, D, (vir_bytes) ¶m.i, (vir_bytes) size);
if (r != OK) break;
switch (param.i) {
case TCOOFF:
case TCOON:
tp->tty_inhibited = (param.i == TCOOFF);
tp->tty_events = 1;
break;
case TCIOFF:
(*tp->tty_echo)(tp, tp->tty_termios.c_cc[VSTOP]);
break;
case TCION:
(*tp->tty_echo)(tp, tp->tty_termios.c_cc[VSTART]);
break;
default:
r = EINVAL;
}
break;
case TCSBRK:
if (tp->tty_break != NULL) (*tp->tty_break)(tp,0);
break;
case TIOCGWINSZ:
r = sys_vircopy(SELF, D, (vir_bytes) &tp->tty_winsize,
m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
(vir_bytes) size);
break;
case TIOCSWINSZ:
r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
SELF, D, (vir_bytes) &tp->tty_winsize, (vir_bytes) size);
/* SIGWINCH... */
break;
#if ENABLE_SRCCOMPAT
case TIOCGETP:
compat_getp(tp, ¶m.sg);
r = sys_vircopy(SELF, D, (vir_bytes) ¶m.sg,
m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
(vir_bytes) size);
break;
case TIOCSETP:
r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
SELF, D, (vir_bytes) ¶m.sg, (vir_bytes) size);
if (r != OK) break;
compat_setp(tp, ¶m.sg);
break;
case TIOCGETC:
compat_getc(tp, ¶m.tc);
r = sys_vircopy(SELF, D, (vir_bytes) ¶m.tc,
m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
(vir_bytes) size);
break;
case TIOCSETC:
r = sys_vircopy( m_ptr->PROC_NR, D, (vir_bytes) m_ptr->ADDRESS,
SELF, D, (vir_bytes) ¶m.tc, (vir_bytes) size);
if (r != OK) break;
compat_setc(tp, ¶m.tc);
break;
#endif
#if (MACHINE == IBM_PC)
case KIOCSMAP:
/* Load a new keymap (only /dev/console). */
if (isconsole(tp)) r = kbd_loadmap(m_ptr);
break;
case TIOCSFON:
/* Load a font into an EGA or VGA card (hs@hck.hr) */
if (isconsole(tp)) r = con_loadfont(m_ptr);
break;
#endif
#if (MACHINE == ATARI)
case VDU_LOADFONT:
r = vdu_loadfont(m_ptr);
break;
#endif
/* These Posix functions are allowed to fail if _POSIX_JOB_CONTROL is
* not defined.
*/
case TIOCGPGRP:
case TIOCSPGRP:
default:
#if ENABLE_BINCOMPAT
do_ioctl_compat(tp, m_ptr);
return;
#else
r = ENOTTY;
#endif
}
/* Send the reply. */
tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, r);
}
/*===========================================================================*
* do_open *
*===========================================================================*/
PRIVATE void do_open(tp, m_ptr)
register tty_t *tp;
message *m_ptr; /* pointer to message sent to task */
{
/* A tty line has been opened. Make it the callers controlling tty if
* O_NOCTTY is *not* set and it is not the log device. 1 is returned if
* the tty is made the controlling tty, otherwise OK or an error code.
*/
int r = OK;
if (m_ptr->TTY_LINE == LOG_MINOR) {
/* The log device is a write-only diagnostics device. */
if (m_ptr->COUNT & R_BIT) r = EACCES;
} else {
if (!(m_ptr->COUNT & O_NOCTTY)) {
tp->tty_pgrp = m_ptr->PROC_NR;
r = 1;
}
tp->tty_openct++;
}
tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, r);
}
/*===========================================================================*
* do_close *
*===========================================================================*/
PRIVATE void do_close(tp, m_ptr)
register tty_t *tp;
message *m_ptr; /* pointer to message sent to task */
{
/* A tty line has been closed. Clean up the line if it is the last close. */
if (m_ptr->TTY_LINE != LOG_MINOR && --tp->tty_openct == 0) {
tp->tty_pgrp = 0;
tty_icancel(tp);
(*tp->tty_ocancel)(tp, 0);
(*tp->tty_close)(tp, 0);
tp->tty_termios = termios_defaults;
tp->tty_winsize = winsize_defaults;
setattr(tp);
}
tty_reply(TASK_REPLY, m_ptr->m_source, m_ptr->PROC_NR, OK);
}
/*===========================================================================*
* do_cancel *
*===========================================================================*/
PRIVATE void do_cancel(tp, m_ptr)
register tty_t *tp;
message *m_ptr; /* pointer to message sent to task */
{
/* A signal has been sent to a process that is hanging trying to read or write.
* The pending read or write must be finished off immediately.
*/
int proc_nr;
int mode;
/* Check the parameters carefully, to avoid cancelling twice. */
proc_nr = m_ptr->PROC_NR;
mode = m_ptr->COUNT;
if ((mode & R_BIT) && tp->tty_inleft != 0 && proc_nr == tp->tty_inproc) {
/* Process was reading when killed. Clean up input. */
tty_icancel(tp);
tp->tty_inleft = tp->tty_incum = 0;
}
if ((mode & W_BIT) && tp->tty_outleft != 0 && proc_nr == tp->tty_outproc) {
/* Process was writing when killed. Clean up output. */
(*tp->tty_ocancel)(tp, 0);
tp->tty_outleft = tp->tty_outcum = 0;
}
if (tp->tty_ioreq != 0 && proc_nr == tp->tty_ioproc) {
/* Process was waiting for output to drain. */
tp->tty_ioreq = 0;
}
tp->tty_events = 1;
tty_reply(TASK_REPLY, m_ptr->m_source, proc_nr, EINTR);
}
PUBLIC int select_try(struct tty *tp, int ops)
{
int ready_ops = 0;
/* Special case. If line is hung up, no operations will block.
* (and it can be seen as an exceptional condition.)
*/
if (tp->tty_termios.c_ospeed == B0) {
ready_ops |= ops;
}
if (ops & SEL_RD) {
/* will i/o not block on read? */
if (tp->tty_inleft > 0) {
ready_ops |= SEL_RD; /* EIO - no blocking */
} else if (tp->tty_incount > 0) {
/* Is a regular read possible? tty_incount
* says there is data. But a read will only succeed
* in canonical mode if a newline has been seen.
*/
if (!(tp->tty_termios.c_lflag & ICANON) ||
tp->tty_eotct > 0) {
ready_ops |= SEL_RD;
}
}
}
if (ops & SEL_WR) {
if (tp->tty_outleft > 0) ready_ops |= SEL_WR;
else if ((*tp->tty_devwrite)(tp, 1)) ready_ops |= SEL_WR;
}
return ready_ops;
}
PUBLIC int select_retry(struct tty *tp)
{
if (select_try(tp, tp->tty_select_ops))
notify(tp->tty_select_proc);
return OK;
}
/*===========================================================================*
* handle_events *
*===========================================================================*/
PUBLIC void handle_events(tp)
tty_t *tp; /* TTY to check for events. */
{
/* Handle any events pending on a TTY. These events are usually device
* interrupts.
*
* Two kinds of events are prominent:
* - a character has been received from the console or an RS232 line.
* - an RS232 line has completed a write request (on behalf of a user).
* The interrupt handler may delay the interrupt message at its discretion
* to avoid swamping the TTY task. Messages may be overwritten when the
* lines are fast or when there are races between different lines, input
* and output, because MINIX only provides single buffering for interrupt
* messages (in proc.c). This is handled by explicitly checking each line
* for fresh input and completed output on each interrupt.
*/
char *buf;
unsigned count;
int status;
do {
tp->tty_events = 0;
/* Read input and perform input processing. */
(*tp->tty_devread)(tp, 0);
/* Perform output processing and write output. */
(*tp->tty_devwrite)(tp, 0);
/* Ioctl waiting for some event? */
if (tp->tty_ioreq != 0) dev_ioctl(tp);
} while (tp->tty_events);
/* Transfer characters from the input queue to a waiting process. */
in_transfer(tp);
/* Reply if enough bytes are available. */
if (tp->tty_incum >= tp->tty_min && tp->tty_inleft > 0) {
if (tp->tty_inrepcode == REVIVE) {
notify(tp->tty_incaller);
tp->tty_inrevived = 1;
} else {
tty_reply(tp->tty_inrepcode, tp->tty_incaller,
tp->tty_inproc, tp->tty_incum);
tp->tty_inleft = tp->tty_incum = 0;
}
}
if (tp->tty_select_ops)
select_retry(tp);
#if NR_PTYS > 0
if (ispty(tp))
select_retry_pty(tp);
#endif
}
/*===========================================================================*
* in_transfer *
*===========================================================================*/
PRIVATE void in_transfer(tp)
register tty_t *tp; /* pointer to terminal to read from */
{
/* Transfer bytes from the input queue to a process reading from a terminal. */
int ch;
int count;
char buf[64], *bp;
/* Force read to succeed if the line is hung up, looks like EOF to reader. */
if (tp->tty_termios.c_ospeed == B0) tp->tty_min = 0;
/* Anything to do? */
if (tp->tty_inleft == 0 || tp->tty_eotct < tp->tty_min) return;
bp = buf;
while (tp->tty_inleft > 0 && tp->tty_eotct > 0) {
ch = *tp->tty_intail;
if (!(ch & IN_EOF)) {
/* One character to be delivered to the user. */
*bp = ch & IN_CHAR;
tp->tty_inleft--;
if (++bp == bufend(buf)) {
/* Temp buffer full, copy to user space. */
sys_vircopy(SELF, D, (vir_bytes) buf,
tp->tty_inproc, D, tp->tty_in_vir,
(vir_bytes) buflen(buf));
tp->tty_in_vir += buflen(buf);
tp->tty_incum += buflen(buf);
bp = buf;
}
}
/* Remove the character from the input queue. */
if (++tp->tty_intail == bufend(tp->tty_inbuf))
tp->tty_intail = tp->tty_inbuf;
tp->tty_incount--;
if (ch & IN_EOT) {
tp->tty_eotct--;
/* Don't read past a line break in canonical mode. */
if (tp->tty_termios.c_lflag & ICANON) tp->tty_inleft = 0;
}
}
if (bp > buf) {
/* Leftover characters in the buffer. */
count = bp - buf;
sys_vircopy(SELF, D, (vir_bytes) buf,
tp->tty_inproc, D, tp->tty_in_vir, (vir_bytes) count);
tp->tty_in_vir += count;
tp->tty_incum += count;
}
/* Usually reply to the reader, possibly even if incum == 0 (EOF). */
if (tp->tty_inleft == 0) {
if (tp->tty_inrepcode == REVIVE) {
notify(tp->tty_incaller);
tp->tty_inrevived = 1;
} else {
tty_reply(tp->tty_inrepcode, tp->tty_incaller,
tp->tty_inproc, tp->tty_incum);
tp->tty_inleft = tp->tty_incum = 0;
}
}
}
/*===========================================================================*
* in_process *
*===========================================================================*/
PUBLIC int in_process(tp, buf, count)
register tty_t *tp; /* terminal on which character has arrived */
char *buf; /* buffer with input characters */
int count; /* number of input characters */
{
/* Characters have just been typed in. Process, save, and echo them. Return
* the number of characters processed.
*/
int ch, sig, ct;
int timeset = FALSE;
static unsigned char csize_mask[] = { 0x1F, 0x3F, 0x7F, 0xFF };
for (ct = 0; ct < count; ct++) {
/* Take one character. */
ch = *buf++ & BYTE;
/* Strip to seven bits? */
if (tp->tty_termios.c_iflag & ISTRIP) ch &= 0x7F;
/* Input extensions? */
if (tp->tty_termios.c_lflag & IEXTEN) {
/* Previous character was a character escape? */
if (tp->tty_escaped) {
tp->tty_escaped = NOT_ESCAPED;
ch |= IN_ESC; /* protect character */
}
/* LNEXT (^V) to escape the next character? */
if (ch == tp->tty_termios.c_cc[VLNEXT]) {
tp->tty_escaped = ESCAPED;
rawecho(tp, '^');
rawecho(tp, '\b');
continue; /* do not store the escape */
}
/* REPRINT (^R) to reprint echoed characters? */