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
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
|
/*
Copyright (C) 1995, 96, 97, 98, 1999 Free Software Foundation, Inc.
Written by Michael I. Bushnell, p/BSG.
This file is part of the GNU Hurd.
The GNU Hurd is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License as
published by the Free Software Foundation; either version 2, or (at
your option) any later version.
The GNU Hurd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA. */
#include "term.h"
#include <sys/stat.h>
#include <unistd.h>
#include <sys/types.h>
#include <string.h>
#include <fcntl.h>
#include <hurd/trivfs.h>
#include <cthreads.h>
#include <hurd.h>
#include <stdio.h>
#include <hurd/iohelp.h>
#include <hurd/fshelp.h>
#include "ourmsg_U.h"
#undef ECHO
#undef MDMBUF
#undef TOSTOP
#undef FLUSHO
#undef PENDIN
#undef NOFLSH
#include "term_S.h"
#include "tioctl_S.h"
#include <sys/ioctl.h>
#define TTYDEFCHARS
#include <sys/ttydefaults.h>
/* Count of active opens */
int nperopens;
/* io_async requests */
struct async_req
{
mach_port_t notify;
struct async_req *next;
};
struct async_req *async_requests;
/* Number of peropens that have set the ICKY_ASYNC flags. */
static int num_icky_async_peropens = 0;
mach_port_t async_icky_id;
mach_port_t async_id;
struct port_info *cttyid;
int foreground_id;
struct winsize window_size;
static int sigs_in_progress;
static struct condition input_sig_wait = CONDITION_INITIALIZER;
static int input_sig_wakeup;
/* Attach this on the hook of any protid that is a ctty. */
struct protid_hook
{
int refcnt;
pid_t pid, pgrp;
};
void
init_users ()
{
errno = ports_create_port (cttyid_class, term_bucket,
sizeof (struct port_info), &cttyid);
if (errno)
{
perror ("Allocating cttyid");
exit (1);
}
mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE,
&async_icky_id);
/* Add a send right, since hurd_sig_post needs one. */
mach_port_insert_right (mach_task_self (),
async_icky_id, async_icky_id,
MACH_MSG_TYPE_MAKE_SEND);
mach_port_allocate (mach_task_self (), MACH_PORT_RIGHT_RECEIVE, &async_id);
/* Add a send right, since hurd_sig_post needs one. */
mach_port_insert_right (mach_task_self (),
async_id, async_id, MACH_MSG_TYPE_MAKE_SEND);
}
static error_t
check_access_hook (struct trivfs_control *cntl,
struct iouser *user,
mach_port_t realnode,
int *allowed)
{
struct stat st;
mutex_lock (&global_lock);
st.st_uid = term_owner;
st.st_gid = term_group;
st.st_mode = term_mode;
*allowed = 0;
if (fshelp_access (&st, S_IREAD, user) == 0)
*allowed |= O_READ;
if (fshelp_access (&st, S_IWRITE, user) == 0)
*allowed |= O_WRITE;
mutex_unlock (&global_lock);
return 0;
}
error_t (*trivfs_check_access_hook) (struct trivfs_control *, struct iouser *,
mach_port_t, int *)
= check_access_hook;
static error_t
open_hook (struct trivfs_control *cntl,
struct iouser *user,
int flags)
{
static int open_count = 0; /* XXX debugging */
int cancel = 0;
error_t err;
if (cntl == ptyctl)
return pty_open_hook (cntl, user, flags);
if ((flags & (O_READ|O_WRITE)) == 0)
return 0;
mutex_lock (&global_lock);
if (!(termflags & TTY_OPEN))
{
bzero (&termstate, sizeof termstate);
/* This is different from BSD: we don't turn on ISTRIP,
and we use CS8 rather than CS7|PARENB. */
termstate.c_iflag |= BRKINT | ICRNL | IMAXBEL | IXON | IXANY;
termstate.c_oflag |= OPOST | ONLCR | OXTABS;
termstate.c_lflag |= (ECHO | ICANON | ISIG | IEXTEN
| ECHOE|ECHOKE|ECHOCTL);
termstate.c_cflag |= CREAD | CS8 | HUPCL;
bcopy (ttydefchars, termstate.c_cc, NCCS);
bzero (&window_size, sizeof window_size);
termflags |= NO_OWNER;
}
else
{
assert (open_count > 0); /* XXX debugging */
if (termflags & EXCL_USE)
{
mutex_unlock (&global_lock);
return EBUSY;
}
}
open_count++; /* XXX debugging */
/* XXX debugging */
assert (! (termstate.c_oflag & OTILDE));
/* Assert DTR if necessary. */
if (termflags & NO_CARRIER)
{
err = (*bottom->assert_dtr) ();
if (err)
{
mutex_unlock (&global_lock);
return err;
}
}
/* Wait for carrier to turn on. */
while (((termflags & NO_CARRIER) && !(termstate.c_cflag & CLOCAL))
&& !(flags & O_NONBLOCK)
&& !cancel)
cancel = hurd_condition_wait (&carrier_alert, &global_lock);
if (cancel)
{
mutex_unlock (&global_lock);
return EINTR;
}
termflags |= TTY_OPEN;
(*bottom->set_bits) ();
mutex_unlock (&global_lock);
return 0;
}
error_t (*trivfs_check_open_hook) (struct trivfs_control *,
struct iouser *, int)
= open_hook;
static error_t
pi_create_hook (struct trivfs_protid *cred)
{
if (cred->pi.class == pty_class)
return 0;
mutex_lock (&global_lock);
if (cred->hook)
((struct protid_hook *)cred->hook)->refcnt++;
mutex_unlock (&global_lock);
return 0;
}
error_t (*trivfs_protid_create_hook) (struct trivfs_protid *) = pi_create_hook;
static void
pi_destroy_hook (struct trivfs_protid *cred)
{
if (cred->pi.class == pty_class)
return;
mutex_lock (&global_lock);
if (cred->hook)
{
assert (((struct protid_hook *)cred->hook)->refcnt > 0);
if (--((struct protid_hook *)cred->hook)->refcnt == 0)
/* XXX don't free for now, so we can try and catch a multiple-freeing
bug. */
/* free (cred->hook) */;
}
mutex_unlock (&global_lock);
}
void (*trivfs_protid_destroy_hook) (struct trivfs_protid *) = pi_destroy_hook;
static error_t
po_create_hook (struct trivfs_peropen *po)
{
if (po->cntl == ptyctl)
return pty_po_create_hook (po);
mutex_lock (&global_lock);
nperopens++;
if (po->openmodes & O_ASYNC)
{
termflags |= ICKY_ASYNC;
num_icky_async_peropens++;
call_asyncs (O_READ | O_WRITE);
}
mutex_unlock (&global_lock);
return 0;
}
error_t (*trivfs_peropen_create_hook) (struct trivfs_peropen *) =
po_create_hook;
static void
po_destroy_hook (struct trivfs_peropen *po)
{
if (po->cntl == ptyctl)
{
pty_po_destroy_hook (po);
return;
}
mutex_lock (&global_lock);
if ((po->openmodes & O_ASYNC) && --num_icky_async_peropens == 0)
termflags &= ~ICKY_ASYNC;
nperopens--;
if (!nperopens && (termflags & TTY_OPEN))
{
/* Empty queues */
clear_queue (inputq);
clear_queue (rawq);
(*bottom->notice_input_flushed) ();
drain_output ();
/* Possibly drop carrier */
if ((termstate.c_cflag & HUPCL) || (termflags & NO_CARRIER))
(*bottom->desert_dtr) ();
termflags &= ~TTY_OPEN;
}
mutex_unlock (&global_lock);
}
void (*trivfs_peropen_destroy_hook) (struct trivfs_peropen *)
= po_destroy_hook;
/* Tell if CRED can do foreground terminal operations */
static inline int
fg_p (struct trivfs_protid *cred)
{
struct protid_hook *hook = cred->hook;
if (!hook || (termflags & NO_OWNER))
return 1;
if (hook->pid == foreground_id
|| hook->pgrp == -foreground_id)
return 1;
return 0;
}
void
trivfs_modify_stat (struct trivfs_protid *cred, struct stat *st)
{
st->st_blksize = 512;
st->st_fstype = FSTYPE_TERM;
st->st_fsid = getpid ();
st->st_ino = 0;
st->st_mode = term_mode;
st->st_uid = term_owner;
st->st_gid = term_group;
}
/* Implement term_getctty as described in <hurd/term.defs>. */
kern_return_t
S_term_getctty (mach_port_t arg,
mach_port_t *id,
mach_msg_type_name_t *idtype)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket,
arg, tty_class);
error_t err;
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
*id = ports_get_right (cttyid);
*idtype = MACH_MSG_TYPE_MAKE_SEND;
err = 0;
}
ports_port_deref (cred);
mutex_unlock (&global_lock);
return err;
}
/* Implement termctty_open_terminal as described in <hurd/term.defs>. */
kern_return_t
S_termctty_open_terminal (mach_port_t arg,
int flags,
mach_port_t *result,
mach_msg_type_name_t *resulttype)
{
error_t err;
mach_port_t new_realnode;
struct trivfs_protid *newcred;
struct port_info *pi = ports_lookup_port (term_bucket, arg, cttyid_class);
if (!pi)
return EOPNOTSUPP;
assert (pi == cttyid);
err = io_restrict_auth (termctl->underlying, &new_realnode, 0, 0, 0, 0);
if (!err)
{
err = trivfs_open (termctl,
iohelp_create_iouser (make_idvec (), make_idvec ()),
flags, new_realnode, &newcred);
if (!err)
{
*result = ports_get_right (newcred);
*resulttype = MACH_MSG_TYPE_MAKE_SEND;
ports_port_deref (newcred);
}
}
ports_port_deref (pi);
return err;
}
/* Implement term_become_ctty as described in <hurd/term.defs>. */
kern_return_t
S_term_open_ctty (mach_port_t arg,
pid_t pid,
pid_t pgrp,
mach_port_t *newpt,
mach_msg_type_name_t *newpttype)
{
error_t err;
struct trivfs_protid *newcred;
struct trivfs_protid *cred = ports_lookup_port (term_bucket, arg, tty_class);
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if (!cred->po->openmodes & (O_READ|O_WRITE))
{
mutex_unlock (&global_lock);
err = EBADF;
}
else
{
mutex_unlock (&global_lock);
err = trivfs_protid_dup (cred, &newcred);
if (!err)
{
struct protid_hook *hook = malloc (sizeof (struct protid_hook));
hook->pid = pid;
hook->pgrp = pgrp;
hook->refcnt = 1;
if (newcred->hook)
/* We inherited CRED's hook, get rid of our ref to it. */
pi_destroy_hook (newcred);
newcred->hook = hook;
*newpt = ports_get_right (newcred);
*newpttype = MACH_MSG_TYPE_MAKE_SEND;
ports_port_deref (newcred);
}
}
ports_port_deref (cred);
return err;
}
/* Implement chown locally; don't pass the value down to the
underlying node. */
error_t
trivfs_S_file_chown (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t reply_type,
uid_t uid,
gid_t gid)
{
struct stat st;
error_t err;
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
/* XXX */
st.st_uid = term_owner;
st.st_gid = term_group;
if (!cred->isroot)
{
err = fshelp_isowner (&st, cred->user);
if (err)
goto out;
if ((uid != (uid_t) -1 && !idvec_contains (cred->user->uids, uid))
|| (gid != (gid_t) -1 && !idvec_contains (cred->user->gids, gid)))
{
err = EPERM;
goto out;
}
}
/* Make the change */
if (uid != (uid_t) -1)
term_owner = uid;
if (gid != (gid_t) -1)
term_group = gid;
err = 0;
out:
mutex_unlock (&global_lock);
return err;
}
/* Implement chmod locally */
error_t
trivfs_S_file_chmod (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t reply_type,
mode_t mode)
{
error_t err;
struct stat st;
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if (!cred->isroot)
{
/* XXX */
st.st_uid = term_owner;
st.st_gid = term_group;
err = fshelp_isowner (&st, cred->user);
if (err)
goto out;
mode &= ~S_ISVTX;
if (!idvec_contains (cred->user->uids, term_owner))
mode &= ~S_ISUID;
if (!idvec_contains (cred->user->gids, term_group))
mode &= ~S_ISUID;
}
term_mode = ((mode & ~S_IFMT & ~S_ITRANS & ~S_ISPARE) | S_IFCHR | S_IROOT);
err = 0;
out:
mutex_unlock (&global_lock);
return err;
}
/* Called for user writes to the terminal as described
in <hurd/io.defs>. */
error_t
trivfs_S_io_write (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t replytype,
char *data,
u_int datalen,
off_t offset,
int *amt)
{
int i;
int cancel;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class == pty_class)
return pty_io_write (cred, data, datalen, amt);
mutex_lock (&global_lock);
/* Check for errors first. */
if ((cred->po->openmodes & O_WRITE) == 0)
{
mutex_unlock (&global_lock);
return EBADF;
}
if ((termstate.c_lflag & TOSTOP) && !fg_p (cred))
{
mutex_unlock (&global_lock);
return EBACKGROUND;
}
if ((termflags & NO_CARRIER) && !(termstate.c_cflag & CLOCAL))
{
mutex_unlock (&global_lock);
return EIO;
}
cancel = 0;
for (i = 0; i < datalen; i++)
{
while (!qavail (outputq) && !cancel)
{
(*bottom->start_output) ();
if (!qavail (outputq))
cancel = hurd_condition_wait (outputq->wait, &global_lock);
}
if (cancel)
break;
write_character (data[i]);
}
*amt = i;
(*bottom->start_output) ();
trivfs_set_mtime (termctl);
call_asyncs (O_WRITE);
mutex_unlock (&global_lock);
return ((cancel && datalen && !*amt) ? EINTR : 0);
}
/* Called for user reads from the terminal. */
error_t
trivfs_S_io_read (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t replytype,
char **data,
u_int *datalen,
off_t offset,
int amount)
{
int cancel;
int i, max;
char *cp;
int avail;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class == pty_class)
return pty_io_read (cred, data, datalen, amount);
mutex_lock (&global_lock);
if ((cred->po->openmodes & O_READ) == 0)
{
mutex_unlock (&global_lock);
return EBADF;
}
if (!fg_p (cred))
{
mutex_unlock (&global_lock);
return EBACKGROUND;
}
while (!qsize (inputq))
{
if ((termflags & NO_CARRIER) && !(termstate.c_cflag & CLOCAL))
{
/* Return EOF, Posix.1 7.1.1.10. */
mutex_unlock (&global_lock);
*datalen = 0;
return 0;
}
if (cred->po->openmodes & O_NONBLOCK)
{
mutex_unlock (&global_lock);
return EWOULDBLOCK;
}
if (hurd_condition_wait (inputq->wait, &global_lock))
{
mutex_unlock (&global_lock);
return EINTR;
}
/* If a signal is being delivered, and we got woken up by
arriving input, then there's a possible race; we have to not
read from the queue as long as the signal is in progress.
Now, you might think that we should not read from the queue
when a signal is in progress even if we didn't block, but
that's not so. It's specifically that we have to get
*interrupted* by signals in progress (when the signallee is
this thread and wants to interrupt is) that is the race we
are avoiding. A reader who gets in after the signal begins,
while the signal is in progress, is harmless, because this
case is indiscernable from one where the reader gets in after
the signal has completed. */
if (sigs_in_progress)
{
input_sig_wakeup++;
if (hurd_condition_wait (&input_sig_wait, &global_lock))
{
mutex_unlock (&global_lock);
return EINTR;
}
}
}
avail = qsize (inputq);
if (remote_input_mode)
avail--;
max = (amount < avail) ? amount : avail;
if (max > *datalen)
*data = mmap (0, max, PROT_READ|PROT_WRITE, MAP_ANON, 0, 0);
cancel = 0;
cp = *data;
for (i = 0; i < max; i++)
{
char c = dequeue (inputq);
if (remote_input_mode)
*cp++ = c;
else
{
/* Unless this is EOF, add it to the response. */
if (!(termstate.c_lflag & ICANON)
|| !CCEQ (termstate.c_cc[VEOF], c))
*cp++ = c;
/* If this is a break character, then finish now. */
if ((termstate.c_lflag & ICANON)
&& (c == '\n'
|| CCEQ (termstate.c_cc[VEOF], c)
|| CCEQ (termstate.c_cc[VEOL], c)
|| CCEQ (termstate.c_cc[VEOL2], c)))
break;
/* If this is the delayed suspend character, then signal now. */
if ((termstate.c_lflag & ISIG)
&& CCEQ (termstate.c_cc[VDSUSP], c))
{
/* The CANCEL flag is being used here to tell the return
below to make sure we don't signal EOF on a VDUSP that
happens at the front of a line. */
send_signal (SIGTSTP);
cancel = 1;
break;
}
}
}
if (remote_input_mode && qsize (inputq) == 1)
dequeue (inputq);
*datalen = cp - *data;
/* If we really read something, set atime */
if (*datalen || !cancel)
trivfs_set_atime (termctl);
call_asyncs (O_READ);
mutex_unlock (&global_lock);
return !*datalen && cancel ? EINTR : 0;
}
error_t
trivfs_S_io_pathconf (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t reply_type,
int name,
int *val)
{
if (!cred)
return EOPNOTSUPP;
switch (name)
{
case _PC_LINK_MAX:
case _PC_NAME_MAX:
case _PC_PATH_MAX:
case _PC_PIPE_BUF:
case _PC_NO_TRUNC:
default:
return io_pathconf (cred->realnode, name, val);
case _PC_MAX_CANON:
*val = rawq->hiwat;
return 0;
case _PC_MAX_INPUT:
*val = inputq->hiwat;
return 0;
case _PC_CHOWN_RESTRICTED:
/* We implement this locally, remember... */
*val = 1;
return 0;
case _PC_VDISABLE:
*val = _POSIX_VDISABLE;
return 0;
}
}
error_t
trivfs_S_io_readable (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t replytype,
int *amt)
{
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class == pty_class)
return pty_io_readable (amt);
mutex_lock (&global_lock);
if ((cred->po->openmodes & O_READ) == 0)
{
mutex_unlock (&global_lock);
return EBADF;
}
*amt = qsize (inputq);
if (remote_input_mode && *amt)
--*amt;
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_revoke (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t replytype)
{
struct stat st;
error_t iterator_function (void *port)
{
struct trivfs_protid *user = port;
if (user != cred)
ports_destroy_right (user);
return 0;
}
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if (!cred->isroot)
{
error_t err;
/* XXX */
st.st_uid = term_owner;
st.st_gid = term_group;
err = fshelp_isowner (&st, cred->user);
if (err)
{
mutex_unlock (&global_lock);
return err;
}
}
mutex_unlock (&global_lock);
ports_inhibit_bucket_rpcs (term_bucket);
ports_class_iterate (cred->pi.class, iterator_function);
ports_resume_bucket_rpcs (term_bucket);
return 0;
}
/* TIOCMODG ioctl -- Get modem state */
kern_return_t
S_tioctl_tiocmodg (io_t port,
int *state)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
*state = (*bottom->mdmstate) ();
mutex_unlock (&global_lock);
ports_port_deref (cred);
return 0;
}
/* TIOCMODS ioctl -- Set modem state */
kern_return_t
S_tioctl_tiocmods (io_t port,
int state)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
(*bottom->mdmctl) (MDMCTL_SET, state);
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCEXCL ioctl -- Set exclusive use */
kern_return_t
S_tioctl_tiocexcl (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
termflags |= EXCL_USE;
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCNXCL ioctl -- Clear exclusive use */
kern_return_t
S_tioctl_tiocnxcl (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
termflags &= ~EXCL_USE;
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCFLUSH ioctl -- Flush input, output, or both */
kern_return_t
S_tioctl_tiocflush (io_t port,
int flags)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
if (flags == 0)
flags = O_READ|O_WRITE;
if (flags & O_READ)
{
clear_queue (inputq);
(*bottom->notice_input_flushed) ();
}
if (flags & O_WRITE)
drop_output ();
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCGETA ioctl -- Get termios state */
kern_return_t
S_tioctl_tiocgeta (io_t port,
tcflag_t *modes,
cc_t *ccs,
speed_t *speeds)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
modes[0] = termstate.c_iflag;
modes[1] = termstate.c_oflag;
modes[2] = termstate.c_cflag;
modes[3] = termstate.c_lflag;
bcopy (termstate.c_cc, ccs, NCCS);
speeds[0] = termstate.__ispeed;
speeds[1] = termstate.__ospeed;
mutex_unlock (&global_lock);
ports_port_deref (cred);
return 0;
}
/* Common code for the varios TIOCSET* commands. */
static error_t
set_state (io_t port,
tcflag_t *modes,
cc_t *ccs,
speed_t *speeds,
int draino,
int flushi)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
int oldlflag;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else if (!fg_p (cred))
err = EBACKGROUND;
else
{
if (cred->pi.class == pty_class)
{
clear_queue (outputq);
(*bottom->abandon_physical_output) ();
}
if (draino)
{
err = drain_output ();
if (err)
{
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
}
if (flushi)
{
clear_queue (inputq);
(*bottom->notice_input_flushed) ();
}
oldlflag = termstate.c_lflag;
termstate.c_iflag = modes[0];
termstate.c_oflag = modes[1];
termstate.c_cflag = modes[2];
termstate.c_lflag = modes[3];
bcopy (ccs, termstate.c_cc, NCCS);
termstate.__ispeed = speeds[0];
termstate.__ospeed = speeds[1];
if (external_processing)
termstate.c_lflag |= EXTPROC;
else
termstate.c_lflag &= ~EXTPROC;
(*bottom->set_bits) ();
if (oldlflag & ICANON)
{
if (!(termstate.c_lflag & ICANON))
copy_rawq ();
}
else
{
if (termstate.c_lflag & ICANON)
rescan_inputq ();
}
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCSETA -- Set termios state */
kern_return_t
S_tioctl_tiocseta (io_t port,
tcflag_t *modes,
cc_t *ccs,
speed_t *speeds)
{
return set_state (port, modes, ccs, speeds, 0, 0);
}
/* Drain output, then set term state. */
kern_return_t
S_tioctl_tiocsetaw (io_t port,
tcflag_t *modes,
cc_t *ccs,
speed_t *speeds)
{
return set_state (port, modes, ccs, speeds, 1, 0);
}
/* Flush input, drain output, then set term state. */
kern_return_t
S_tioctl_tiocsetaf (io_t port,
tcflag_t *modes,
cc_t *ccs,
speed_t *speeds)
{
return set_state (port, modes, ccs, speeds, 1, 1);
}
/* TIOCGETD -- Return line discipline */
kern_return_t
S_tioctl_tiocgetd (io_t port,
int *disc)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
*disc = 0;
ports_port_deref (cred);
return 0;
}
/* TIOCSETD -- Set line discipline */
kern_return_t
S_tioctl_tiocsetd (io_t port,
int disc)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
mutex_unlock (&global_lock);
if (disc != 0)
err = ENXIO;
else
err = 0;
ports_port_deref (cred);
return 0;
}
/* TIOCDRAIN -- Wait for output to drain */
kern_return_t
S_tioctl_tiocdrain (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & O_WRITE))
{
mutex_unlock (&global_lock);
ports_port_deref (cred);
return EBADF;
}
err = drain_output ();
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCSWINSZ -- Set window size */
kern_return_t
S_tioctl_tiocswinsz (io_t port,
struct winsize size)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
err = 0;
ports_port_deref (cred);
if (! err
&& (size.ws_row != window_size.ws_row
|| size.ws_col != window_size.ws_col
|| size.ws_xpixel != window_size.ws_xpixel
|| size.ws_ypixel != window_size.ws_ypixel))
{
/* The size is actually changing. Record the new size and notify the
process group. */
window_size = size;
send_signal (SIGWINCH);
}
mutex_unlock (&global_lock);
return err;
}
/* TIOCGWINSZ -- Fetch window size */
kern_return_t
S_tioctl_tiocgwinsz (io_t port,
struct winsize *size)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
*size = window_size;
mutex_unlock (&global_lock);
ports_port_deref (cred);
return 0;
}
/* TIOCMGET -- Fetch all modem bits */
kern_return_t
S_tioctl_tiocmget (io_t port,
int *bits)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
*bits = (*bottom->mdmstate) ();
mutex_unlock (&global_lock);
ports_port_deref (cred);
return 0;
}
/* TIOCMSET -- Set all modem bits */
kern_return_t
S_tioctl_tiocmset (io_t port,
int bits)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
(*bottom->mdmctl) (MDMCTL_SET, bits);
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCMBIC -- Clear some modem bits */
kern_return_t
S_tioctl_tiocmbic (io_t port,
int bits)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
(*bottom->mdmctl) (MDMCTL_BIC, bits);
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCMBIS -- Set some modem bits */
kern_return_t
S_tioctl_tiocmbis (io_t port,
int bits)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
(*bottom->mdmctl) (MDMCTL_BIS, bits);
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCSTART -- start output as if VSTART were typed */
kern_return_t
S_tioctl_tiocstart (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
termflags &= ~USER_OUTPUT_SUSP;
(*bottom->start_output) ();
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCSTOP -- stop output as if VSTOP were typed */
kern_return_t
S_tioctl_tiocstop (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
termflags |= USER_OUTPUT_SUSP;
(*bottom->suspend_physical_output) ();
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCSTI -- Simulate terminal input */
kern_return_t
S_tioctl_tiocsti (io_t port,
char c)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
/* BSD returns EACCES if this is not our controlling terminal,
but we have no way to do that. (And I don't think it actually
provides any security there, either.) */
if (!(cred->po->openmodes & O_READ))
err = EPERM;
else
{
input_character (c);
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCOUTQ -- return output queue size */
kern_return_t
S_tioctl_tiocoutq (io_t port,
int *queue_size)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
*queue_size = qsize (outputq) + (*bottom->pending_output_size) ();
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCSPGRP -- set pgrp of terminal */
kern_return_t
S_tioctl_tiocspgrp (io_t port,
int pgrp)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
termflags &= ~NO_OWNER;
foreground_id = -pgrp;
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCGPGRP --- fetch pgrp of terminal */
kern_return_t
S_tioctl_tiocgpgrp (io_t port,
int *pgrp)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t ret;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (termflags & NO_OWNER)
ret = ENOTTY; /* that's what BSD says... */
else
{
*pgrp = - foreground_id;
ret = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return ret;
}
/* TIOCCDTR -- clear DTR */
kern_return_t
S_tioctl_tioccdtr (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
(*bottom->mdmctl) (MDMCTL_BIC, TIOCM_DTR);
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCSDTR -- set DTR */
kern_return_t
S_tioctl_tiocsdtr (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
(*bottom->mdmctl) (MDMCTL_BIS, TIOCM_DTR);
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCCBRK -- Clear break condition */
kern_return_t
S_tioctl_tioccbrk (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
(*bottom->clear_break) ();
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
/* TIOCSBRK -- Set break condition */
kern_return_t
S_tioctl_tiocsbrk (io_t port)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, port, 0);
error_t err;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class != pty_class
&& cred->pi.class != tty_class)
{
ports_port_deref (cred);
return EOPNOTSUPP;
}
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
err = EBADF;
else
{
(*bottom->set_break) ();
err = 0;
}
mutex_unlock (&global_lock);
ports_port_deref (cred);
return err;
}
error_t
trivfs_S_file_set_size (struct trivfs_protid *cred,
mach_port_t reply, mach_msg_type_name_t reply_type,
off_t size)
{
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if ((cred->po->openmodes & O_WRITE) == 0)
{
mutex_unlock (&global_lock);
return EBADF;
}
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_seek (struct trivfs_protid *cred,
mach_port_t reply, mach_msg_type_name_t reply_type,
off_t off,
int whence,
off_t *newp)
{
return ESPIPE;
}
error_t
trivfs_S_io_get_openmodes (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t replytype,
int *bits)
{
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
*bits = cred->po->openmodes;
mutex_unlock (&global_lock);
return 0;
}
#define HONORED_STATE_MODES (O_APPEND|O_ASYNC|O_FSYNC|O_NONBLOCK|O_NOATIME)
error_t
trivfs_S_io_set_all_openmodes (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t replytype,
int bits)
{
int obits;
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
obits = cred->po->openmodes;
if ((obits & O_ASYNC) && --num_icky_async_peropens == 0)
termflags &= ~ICKY_ASYNC;
cred->po->openmodes &= ~HONORED_STATE_MODES;
cred->po->openmodes |= (bits & HONORED_STATE_MODES);
if ((bits & O_ASYNC) && !(obits & O_ASYNC))
{
termflags |= ICKY_ASYNC;
num_icky_async_peropens++;
call_asyncs (O_READ | O_WRITE);
}
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_set_some_openmodes (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t reply_type,
int bits)
{
int obits;
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
obits = cred->po->openmodes;
cred->po->openmodes |= (bits & HONORED_STATE_MODES);
if ((bits & O_ASYNC) && !(obits & O_ASYNC))
{
termflags |= ICKY_ASYNC;
num_icky_async_peropens++;
call_asyncs (O_READ | O_WRITE);
}
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_clear_some_openmodes (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t reply_type,
int bits)
{
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if ((cred->po->openmodes & O_ASYNC) && --num_icky_async_peropens == 0)
termflags &= ~ICKY_ASYNC;
cred->po->openmodes &= ~(bits & HONORED_STATE_MODES);
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_mod_owner (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t reply_type,
pid_t owner)
{
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
termflags &= ~NO_OWNER;
foreground_id = owner;
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_get_owner (struct trivfs_protid *cred,
mach_port_t erply,
mach_msg_type_name_t reply_type,
pid_t *owner)
{
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if (termflags & NO_OWNER)
{
mutex_unlock (&global_lock);
return ENOTTY;
}
*owner = foreground_id;
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_get_icky_async_id (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t reply_type,
mach_port_t *id, mach_msg_type_name_t *idtype)
{
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
{
mutex_unlock (&global_lock);
return EBADF;
}
*id = async_icky_id;
*idtype = MACH_MSG_TYPE_MAKE_SEND;
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_async (struct trivfs_protid *cred,
mach_port_t reply, mach_msg_type_name_t reply_type,
mach_port_t notify,
mach_port_t *id, mach_msg_type_name_t *idtype)
{
struct async_req *ar;
if (!cred)
return EOPNOTSUPP;
mutex_lock (&global_lock);
if (!(cred->po->openmodes & (O_READ|O_WRITE)))
{
mutex_unlock (&global_lock);
return EBADF;
}
ar = malloc (sizeof (struct async_req));
ar->notify = notify;
ar->next = async_requests;
async_requests = ar;
*id = async_id;
*idtype = MACH_MSG_TYPE_MAKE_SEND;
mutex_unlock (&global_lock);
return 0;
}
error_t
trivfs_S_io_select (struct trivfs_protid *cred,
mach_port_t reply,
mach_msg_type_name_t reply_type,
int *type,
int *idtag)
{
int available;
if (!cred)
return EOPNOTSUPP;
if (cred->pi.class == pty_class)
return pty_io_select (cred, reply, type, idtag);
/* We don't deal with SELECT_URG here. */
if (*type & ~(SELECT_READ | SELECT_WRITE))
return EINVAL;
available = 0;
if (*type == 0)
return 0;
mutex_lock (&global_lock);
while (1)
{
if ((*type & SELECT_READ) && qsize (inputq))
available |= SELECT_READ;
if ((*type & SELECT_WRITE) && qavail (outputq))
available |= SELECT_WRITE;
if (available)
{
*type = available;
mutex_unlock (&global_lock);
return 0;
}
ports_interrupt_self_on_port_death (cred, reply);
if (hurd_condition_wait (&select_alert, &global_lock))
{
*type = 0;
mutex_unlock (&global_lock);
return EINTR;
}
}
}
kern_return_t
trivfs_S_io_map (struct trivfs_protid *cred,
mach_port_t *rdobj,
mach_msg_type_name_t *rdtype,
mach_port_t *wrobj,
mach_msg_type_name_t *wrtype)
{
return EOPNOTSUPP;
}
static void
report_sig_start ()
{
sigs_in_progress++;
}
static void
report_sig_end ()
{
sigs_in_progress--;
if ((sigs_in_progress == 0) && input_sig_wakeup)
{
input_sig_wakeup = 0;
condition_broadcast (&input_sig_wait);
}
}
/* Call all the scheduled async I/O handlers. DIR is a mask of O_READ &
O_WRITE; the asyncs will only be called if output is possible in one of
the directions given in DIR. */
void
call_asyncs (int dir)
{
struct async_req *ar, *nxt, **prevp;
mach_port_t err;
/* If nobody wants async messages, don't bother further. */
if (!(termflags & ICKY_ASYNC) && !async_requests)
return;
if ((!(dir & O_READ) || qsize (inputq) == 0)
&& (!(dir & O_WRITE) && qavail (outputq) == 0))
/* Output isn't possible in the desired directions. */
return;
if ((termflags & ICKY_ASYNC) && !(termflags & NO_OWNER))
{
report_sig_start ();
mutex_unlock (&global_lock);
hurd_sig_post (foreground_id, SIGIO, async_icky_id);
mutex_lock (&global_lock);
report_sig_end ();
}
for (ar = async_requests, prevp = &async_requests;
ar;
ar = nxt)
{
nxt = ar->next;
err = nowait_msg_sig_post (ar->notify, SIGIO, 0, async_id);
if (err == MACH_SEND_INVALID_DEST)
{
/* Receiver died; remove the notification request. */
*prevp = ar->next;
mach_port_deallocate (mach_task_self (), ar->notify);
free (ar);
}
else
prevp = &ar->next;
}
}
/* Send a signal to the current process (group) of the terminal. */
void
send_signal (int signo)
{
mach_port_t right;
if (!(termflags & NO_OWNER))
{
right = ports_get_right (cttyid);
mach_port_insert_right (mach_task_self (), right, right,
MACH_MSG_TYPE_MAKE_SEND);
report_sig_start ();
mutex_unlock (&global_lock);
hurd_sig_post (foreground_id, signo, right);
mutex_lock (&global_lock);
report_sig_end ();
mach_port_deallocate (mach_task_self (), right);
}
}
void
report_carrier_off ()
{
clear_queue (inputq);
(*bottom->notice_input_flushed) ();
drop_output ();
termflags |= NO_CARRIER;
if (!(termstate.c_cflag & CLOCAL))
send_signal (SIGHUP);
}
void
report_carrier_on ()
{
termflags &= ~NO_CARRIER;
condition_broadcast (&carrier_alert);
}
kern_return_t
S_term_get_nodename (io_t arg,
char *name)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, arg,
tty_class);
if (!cred)
return EOPNOTSUPP;
if (!cred->po->cntl->hook)
{
ports_port_deref (cred);
return ENOENT;
}
strcpy (name, (char *)cred->po->cntl->hook);
ports_port_deref (cred);
return 0;
}
kern_return_t
S_term_set_nodename (io_t arg,
char *name)
{
error_t err = 0;
struct trivfs_protid *cred = ports_lookup_port (term_bucket, arg, tty_class);
if (!cred)
return EOPNOTSUPP;
if (strcmp (name, (char *)cred->po->cntl->hook) != 0)
err = EINVAL;
ports_port_deref (cred);
return err;
}
kern_return_t
S_term_set_filenode (io_t arg,
file_t filenode)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, arg,
tty_class);
if (!cred)
return EOPNOTSUPP;
ports_port_deref (cred);
return EINVAL;
}
kern_return_t
S_term_get_peername (io_t arg,
char *name)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, arg, 0);
struct trivfs_control *peer;
if (!cred || (cred->pi.class != tty_class && cred->pi.class != pty_class))
{
if (cred)
ports_port_deref (cred);
return EOPNOTSUPP;
}
peer = (cred->pi.class == tty_class) ? ptyctl : termctl;
if (bottom != &ptyio_bottom || !peer->hook)
{
ports_port_deref (cred);
return ENOENT;
}
strcpy (name, (char *) peer->hook);
ports_port_deref (cred);
return 0;
}
kern_return_t
S_term_get_bottom_type (io_t arg,
int *ttype)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket,
arg, tty_class);
if (!cred)
return EOPNOTSUPP;
ports_port_deref (cred);
if (bottom == &devio_bottom)
*ttype = TERM_ON_MACHDEV;
else
*ttype = TERM_ON_MASTERPTY;
return 0;
}
kern_return_t
S_term_on_machdev (io_t arg,
device_t machdev)
{
struct trivfs_protid *cred = ports_lookup_port (term_bucket, arg,
tty_class);
if (!cred)
return EOPNOTSUPP;
ports_port_deref (cred);
return EINVAL;
}
kern_return_t
S_term_on_hurddev (io_t arg,
io_t hurddev)
{
return EOPNOTSUPP;
}
kern_return_t
S_term_on_pty (io_t arg,
mach_port_t *master)
{
return EOPNOTSUPP;
}
error_t
trivfs_goaway (struct trivfs_control *cntl, int flags)
{
return EBUSY;
}
|