summaryrefslogtreecommitdiff
path: root/minix/tests/test90.c
blob: 8fc25fdb7c250eec0652635d6fb33b3ef386c9f9 (plain)
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
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
/* Advanced tests for UNIX Domain Sockets - by D.C. van Moolenbroek */
/*
 * This is a somewhat random collection of in-depth tests, complementing the
 * more general functionality tests in test56.  The overall test set is still
 * by no means expected to be "complete."  The subtests are in random order.
 */
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
#include <stddef.h>
#include <assert.h>

#include "common.h"
#include "socklib.h"

#define ITERATIONS	1

#define SOCK_PATH_A	"sock_a"
#define SOCK_PATH_B	"sock_b"
#define SOCK_PATH_C	"sock_c"
#define SOCK_PATH_D	"sock_d"

#define SOCK_PATH_A_X	".//sock_a"
#define SOCK_PATH_A_Y	"./././sock_a"

#define PRINT_STATS	0

/*
 * Check that the given returned socket address matches the given path.  A NULL
 * path may be passed in to indicate the result should be for an unbound
 * socket.
 */
static void
check_addr(struct sockaddr_un * sun, socklen_t len, const char * path)
{

	if (len < offsetof(struct sockaddr_un, sun_path)) e(0);

	if (sun->sun_family != AF_UNIX) e(0);
	if (sun->sun_len != len - ((path != NULL) ? 1 : 0)) e(0);

	len -= offsetof(struct sockaddr_un, sun_path);

	if (path != NULL) {
		if (len != strlen(path) + 1) e(0);
		if (sun->sun_path[len - 1] != '\0') e(0);
		if (strcmp(sun->sun_path, path)) e(0);
	} else
		if (len != 0) e(0);
}

/*
 * Get a socket of the given type, bound to the given path.  Return the file
 * descriptor, as well as the bound addres in 'sun'.
 */
static int
get_bound_socket(int type, const char * path, struct sockaddr_un * sun)
{
	int fd;

	if ((fd = socket(AF_UNIX, type, 0)) < 0) e(0);

	(void)unlink(path);

	memset(sun, 0, sizeof(*sun));
	sun->sun_family = AF_UNIX;
	strlcpy(sun->sun_path, path, sizeof(sun->sun_path));

	if (bind(fd, (struct sockaddr *)sun, sizeof(*sun)) != 0) e(0);

	return fd;
}

/*
 * Get a pair of connected sockets.
 */
static void
get_socket_pair(int type, int fd[2])
{
	struct sockaddr_un sunA, sunB;

	if ((type & ~SOCK_FLAGS_MASK) == SOCK_DGRAM) {
		fd[0] = get_bound_socket(type, SOCK_PATH_A, &sunA);
		fd[1] = get_bound_socket(type, SOCK_PATH_B, &sunB);

		if (connect(fd[0], (struct sockaddr *)&sunB,
		    sizeof(sunB)) != 0) e(0);
		if (connect(fd[1], (struct sockaddr *)&sunA,
		    sizeof(sunA)) != 0) e(0);

		if (unlink(SOCK_PATH_A) != 0) e(0);
		if (unlink(SOCK_PATH_B) != 0) e(0);
	} else
		if (socketpair(AF_UNIX, type, 0, fd) != 0) e(0);
}

/*
 * Return the receive buffer size of the given socket.
 */
static int
get_rcvbuf_len(int fd)
{
	socklen_t len;
	int val;

	len = sizeof(val);
	if (getsockopt(fd, SOL_SOCKET, SO_RCVBUF, &val, &len) != 0) e(0);

	if (len != sizeof(val)) e(0);
	if (val <= 0) e(0);

	return val;
}

static const enum state unix_connect_states[] = {
		S_NEW,		S_N_SHUT_R,	S_N_SHUT_W,	S_N_SHUT_RW,
		S_BOUND,	S_LISTENING,	S_L_SHUT_R,	S_L_SHUT_W,
		S_L_SHUT_RW,	S_CONNECTING,	S_CONNECTED,	S_ACCEPTED,
		S_SHUT_R,	S_SHUT_W,	S_SHUT_RW,	S_RSHUT_R,
		S_RSHUT_W,	S_RSHUT_RW,	S_SHUT2_R,	S_SHUT2_W,
		S_SHUT2_RW,	S_PRE_EOF,	S_AT_EOF,	S_POST_EOF,
		S_PRE_SHUT_R,	S_EOF_SHUT_R,	S_POST_SHUT_R,	S_PRE_SHUT_W,
		S_EOF_SHUT_W,	S_POST_SHUT_W,	S_PRE_SHUT_RW,	S_EOF_SHUT_RW,
		S_POST_SHUT_RW,	S_AT_RESET,	S_POST_RESET,	S_POST_FAILED
		/*
		 * It is impossible to generate the S_PRE_RESET state: we can
		 * only generate a reset on a connected socket for which the
		 * other end is pending acceptance, by closing the listening
		 * socket.  That means we cannot send data to the connected end
		 * (from the listening socket) before triggering the reset.
		 *
		 * It is impossible to generate the S_FAILED state: even a non-
		 * blocking connect will always fail immediately when it cannot
		 * connect to the target.
		 */
};

static const int unix_connect_results[][__arraycount(unix_connect_states)] = {
	[C_ACCEPT]		= {
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EAGAIN,	-ECONNABORTED,	-ECONNABORTED,
		-ECONNABORTED,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
	},
	[C_BIND]		= {
		0,		0,		0,		0,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	0,		0,		-EINVAL,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_CONNECT]		= {
		0,		0,		0,		0,
		0,		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EALREADY,	-EISCONN,	-EISCONN,
		-EISCONN,	-EISCONN,	-EISCONN,	-EISCONN,
		-EISCONN,	-EISCONN,	-EISCONN,	-EISCONN,
		-EISCONN,	0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_GETPEERNAME]		= {
		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,	-ENOTCONN,
		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,	-ENOTCONN,
		-ENOTCONN,	-ENOTCONN,	0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,
		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,	-ENOTCONN,
		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,	-ENOTCONN,
		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,	-ENOTCONN,
	},
	[C_GETSOCKNAME]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_GETSOCKOPT_ERR]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		-ECONNRESET,	0,		0,
	},
	[C_GETSOCKOPT_KA]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_GETSOCKOPT_RB]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_IOCTL_NREAD]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		1,		0,		0,
		0,		0,		0,		1,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_LISTEN]		= {
		-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,
		0,		0,		0,		0,
		0,		-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,
		-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,
		-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,
		-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,	-EDESTADDRREQ,
	},
	[C_RECV]		= {
		-ENOTCONN,	0,		-ENOTCONN,	0,
		-ENOTCONN,	-ENOTCONN,	0,		-ENOTCONN,
		0,		-EAGAIN,	-EAGAIN,	-EAGAIN,
		0,		-EAGAIN,	0,		-EAGAIN,
		0,		0,		0,		0,
		0,		1,		0,		0,
		0,		0,		0,		1,
		0,		0,		0,		0,
		0,		-ECONNRESET,	0,		-ENOTCONN,
	},
	[C_RECVFROM]		= {
		-ENOTCONN,	0,		-ENOTCONN,	0,
		-ENOTCONN,	-ENOTCONN,	0,		-ENOTCONN,
		0,		-EAGAIN,	-EAGAIN,	-EAGAIN,
		0,		-EAGAIN,	0,		-EAGAIN,
		0,		0,		0,		0,
		0,		1,		0,		0,
		0,		0,		0,		1,
		0,		0,		0,		0,
		0,		-ECONNRESET,	0,		-ENOTCONN,
	},
	[C_SEND]		= {
		-ENOTCONN,	-ENOTCONN,	-EPIPE,		-EPIPE,
		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,	-EPIPE,
		-EPIPE,		-EAGAIN,	1,		1,
		1,		-EPIPE,		-EPIPE,		-EPIPE,
		1,		-EPIPE,		-EPIPE,		-EPIPE,
		-EPIPE,		-EPIPE,		-EPIPE,		-EPIPE,
		-EPIPE,		-EPIPE,		-EPIPE,		-EPIPE,
		-EPIPE,		-EPIPE,		-EPIPE,		-EPIPE,
		-EPIPE,		-ECONNRESET,	-EPIPE,		-ENOTCONN,
	},
	[C_SENDTO]		= {
		-ENOTCONN,	-ENOTCONN,	-EPIPE,		-EPIPE,
		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,	-EPIPE,
		-EPIPE,		-EAGAIN,	1,		1,
		1,		-EPIPE,		-EPIPE,		-EPIPE,
		1,		-EPIPE,		-EPIPE,		-EPIPE,
		-EPIPE,		-EPIPE,		-EPIPE,		-EPIPE,
		-EPIPE,		-EPIPE,		-EPIPE,		-EPIPE,
		-EPIPE,		-EPIPE,		-EPIPE,		-EPIPE,
		-EPIPE,		-ECONNRESET,	-EPIPE,		-ENOTCONN,
	},
	[C_SELECT_R]		= {
		1,		1,		1,		1,
		1,		0,		1,		1,
		1,		0,		0,		0,
		1,		0,		1,		0,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
	},
	[C_SELECT_W]		= {
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		0,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
	},
	[C_SELECT_X]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_SETSOCKOPT_BC]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_SETSOCKOPT_KA]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_SETSOCKOPT_L]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_SETSOCKOPT_RA]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_SHUTDOWN_R]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_SHUTDOWN_RW]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
	[C_SHUTDOWN_W]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
	},
};

/*
 * Set up a connection-oriented socket file descriptor in the requested state
 * and pass it to socklib_sweep_call() along with local and remote addresses
 * and their length.
 */
static int
unix_connect_sweep(int domain, int type, int protocol __unused,
	enum state state, enum call call)
{
	struct sockaddr_un sunA, sunB, sunC;
	char buf[1];
	socklen_t len;
	fd_set fds;
	int r, fd, fd2, fd3, tmpfd, val, fl;

	(void)unlink(SOCK_PATH_A);
	(void)unlink(SOCK_PATH_B);
	(void)unlink(SOCK_PATH_C);

	memset(&sunA, 0, sizeof(sunA));
	sunA.sun_family = AF_UNIX;
	strlcpy(sunA.sun_path, SOCK_PATH_A, sizeof(sunA.sun_path));

	fd = fd3 = -1;

	fd2 = get_bound_socket(type | SOCK_NONBLOCK, SOCK_PATH_B, &sunB);

	if (listen(fd2, 1) == -1) e(0);

	switch (state) {
	case S_NEW:
	case S_N_SHUT_R:
	case S_N_SHUT_W:
	case S_N_SHUT_RW:
		if ((fd = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

		switch (state) {
		case S_N_SHUT_R: if (shutdown(fd, SHUT_RD)) e(0); break;
		case S_N_SHUT_W: if (shutdown(fd, SHUT_WR)) e(0); break;
		case S_N_SHUT_RW: if (shutdown(fd, SHUT_RDWR)) e(0); break;
		default: break;
		}

		break;

	case S_BOUND:
	case S_LISTENING:
	case S_L_SHUT_R:
	case S_L_SHUT_W:
	case S_L_SHUT_RW:
		fd = get_bound_socket(type | SOCK_NONBLOCK, SOCK_PATH_A,
		    &sunA);

		if (state == S_BOUND)
			break;

		if (listen(fd, 1) == -1) e(0);

		switch (state) {
		case S_L_SHUT_R: if (shutdown(fd, SHUT_RD)) e(0); break;
		case S_L_SHUT_W: if (shutdown(fd, SHUT_WR)) e(0); break;
		case S_L_SHUT_RW: if (shutdown(fd, SHUT_RDWR)) e(0); break;
		default: break;
		}

		break;

	case S_CONNECTING:
		/*
		 * The following block is nonportable.  On NetBSD, the
		 * LOCAL_CONNWAIT socket option is present but seems somewhat..
		 * under-tested.  On Linux, it is not possible to put a UNIX
		 * domain socket in a connecting state.
		 */
		if ((fd = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

		val = 1;
		if (setsockopt(fd, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0)
			e(0);

		if (connect(fd, (struct sockaddr *)&sunB, sizeof(sunB)) != -1)
			e(0);
		if (errno != EINPROGRESS) e(0);

		break;

	case S_CONNECTED:
	case S_ACCEPTED:
	case S_SHUT_R:
	case S_SHUT_W:
	case S_SHUT_RW:
	case S_RSHUT_R:
	case S_RSHUT_W:
	case S_RSHUT_RW:
	case S_SHUT2_R:
	case S_SHUT2_W:
	case S_SHUT2_RW:
		if ((fd = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

		if (connect(fd, (struct sockaddr *)&sunB, sizeof(sunB)) != 0)
			e(0);

		len = sizeof(sunC);
		if ((fd3 = accept(fd2, (struct sockaddr *)&sunC, &len)) < 0)
			e(0);

		if ((fl = fcntl(fd3, F_GETFL)) == -1) e(0);
		if (fcntl(fd3, F_SETFL, fl | O_NONBLOCK) != 0) e(0);

		/* Just to make sure, wait for the socket to be connected. */
		FD_ZERO(&fds);
		FD_SET(fd, &fds);
		if (select(fd + 1, NULL, &fds, NULL, NULL) != 1) e(0);

		switch (state) {
		case S_SHUT_R:
		case S_SHUT2_R: if (shutdown(fd, SHUT_RD)) e(0); break;
		case S_SHUT_W:
		case S_SHUT2_W: if (shutdown(fd, SHUT_WR)) e(0); break;
		case S_SHUT_RW:
		case S_SHUT2_RW: if (shutdown(fd, SHUT_RDWR)) e(0); break;
		default: break;
		}

		switch (state) {
		case S_RSHUT_R:
		case S_SHUT2_R: if (shutdown(fd3, SHUT_RD)) e(0); break;
		case S_RSHUT_W:
		case S_SHUT2_W: if (shutdown(fd3, SHUT_WR)) e(0); break;
		case S_RSHUT_RW:
		case S_SHUT2_RW: if (shutdown(fd3, SHUT_RDWR)) e(0); break;
		default: break;
		}

		if (state == S_ACCEPTED) {
			tmpfd = fd;
			fd = fd3;
			fd3 = tmpfd;
		}

		break;

	case S_PRE_EOF:
	case S_AT_EOF:
	case S_POST_EOF:
	case S_PRE_SHUT_R:
	case S_EOF_SHUT_R:
	case S_POST_SHUT_R:
	case S_PRE_SHUT_W:
	case S_EOF_SHUT_W:
	case S_POST_SHUT_W:
	case S_PRE_SHUT_RW:
	case S_EOF_SHUT_RW:
	case S_POST_SHUT_RW:
		if ((fd = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

		if (connect(fd, (struct sockaddr *)&sunB, sizeof(sunB)) != 0)
			e(0);

		len = sizeof(sunC);
		if ((fd3 = accept(fd2, (struct sockaddr *)&sunC, &len)) < 0)
			e(0);

		if ((fl = fcntl(fd3, F_GETFL)) == -1) e(0);
		if (fcntl(fd3, F_SETFL, fl | O_NONBLOCK) != 0) e(0);

		if (send(fd3, "", 1, 0) != 1) e(0);

		if (close(fd3) != 0) e(0);
		fd3 = -1;

		FD_ZERO(&fds);
		FD_SET(fd, &fds);
		if (select(fd + 1, &fds, NULL, NULL, NULL) != 1) e(0);

		switch (state) {
		case S_AT_EOF:
		case S_EOF_SHUT_R:
		case S_EOF_SHUT_W:
		case S_EOF_SHUT_RW:
			if (recv(fd, buf, sizeof(buf), 0) != 1) e(0);
			break;
		case S_POST_EOF:
		case S_POST_SHUT_R:
		case S_POST_SHUT_W:
		case S_POST_SHUT_RW:
			if (recv(fd, buf, sizeof(buf), 0) != 1) e(0);
			if (recv(fd, buf, sizeof(buf), 0) != 0) e(0);
			break;
		default:
			break;
		}

		switch (state) {
		case S_PRE_SHUT_R:
		case S_EOF_SHUT_R:
		case S_POST_SHUT_R: if (shutdown(fd, SHUT_RD)) e(0); break;
		case S_PRE_SHUT_W:
		case S_EOF_SHUT_W:
		case S_POST_SHUT_W: if (shutdown(fd, SHUT_WR)) e(0); break;
		case S_PRE_SHUT_RW:
		case S_EOF_SHUT_RW:
		case S_POST_SHUT_RW: if (shutdown(fd, SHUT_RDWR)) e(0); break;
		default: break;
		}

		break;

	case S_AT_RESET:
	case S_POST_RESET:
		if ((fd = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

		if (connect(fd, (struct sockaddr *)&sunB, sizeof(sunB)) != 0)
			e(0);

		/*
		 * Closing the listening socket before the connection has been
		 * accepted should generate ECONNRESET on the connected socket.
		 * Well, should.. we choose to do that.  So does Linux.  NetBSD
		 * just returns EOF for that case.  There are really no strong
		 * arguments for either behavior.
		 */
		if (close(fd2) != 0) e(0);

		if (state == S_POST_RESET)
			(void)recv(fd, buf, sizeof(buf), 0);

		/* Recreate the listening socket just for consistency. */
		fd2 = get_bound_socket(type | SOCK_NONBLOCK, SOCK_PATH_B,
		    &sunB);

		if (listen(fd2, 1) == -1) e(0);

		break;

	case S_POST_FAILED:
		if ((fd = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

		memset(&sunC, 0, sizeof(sunC));
		sunC.sun_family = AF_UNIX;
		strlcpy(sunC.sun_path, SOCK_PATH_C, sizeof(sunC.sun_path));

		r = connect(fd, (struct sockaddr *)&sunC, sizeof(sunC));
		if (r != -1 || errno != ENOENT)
			e(0);

		break;

	default:
		e(0);
	}

	r = socklib_sweep_call(call, fd, (struct sockaddr *)&sunA,
	    (struct sockaddr *)&sunB, sizeof(struct sockaddr_un));

	if (fd >= 0 && close(fd) != 0) e(0);
	if (fd2 >= 0 && close(fd2) != 0) e(0);
	if (fd3 >= 0 && close(fd3) != 0) e(0);

	(void)unlink(SOCK_PATH_A);
	(void)unlink(SOCK_PATH_B);

	return r;
}

static const enum state unix_dgram_states[] = {
		S_NEW,		S_N_SHUT_R,	S_N_SHUT_W,	S_N_SHUT_RW,
		S_BOUND,	S_CONNECTED,	S_SHUT_R,	S_SHUT_W,
		S_SHUT_RW,	S_RSHUT_R,	S_RSHUT_W,	S_RSHUT_RW,
		S_SHUT2_R,	S_SHUT2_W,	S_SHUT2_RW,	S_PRE_RESET,
		S_AT_RESET,	S_POST_RESET
};

static const int unix_dgram_results[][__arraycount(unix_dgram_states)] = {
	[C_ACCEPT]		= {
		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EOPNOTSUPP,
	},
	[C_BIND]		= {
		0,		0,		0,		0,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,	-EINVAL,	-EINVAL,
		-EINVAL,	-EINVAL,
	},
	[C_CONNECT]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		-ECONNREFUSED,
		-ECONNREFUSED,	-ECONNREFUSED,
	},
	[C_GETPEERNAME]		= {
		-ENOTCONN,	-ENOTCONN,	-ENOTCONN,	-ENOTCONN,
		-ENOTCONN,	0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		-ENOTCONN,
		-ENOTCONN,	-ENOTCONN,
	},
	[C_GETSOCKNAME]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_GETSOCKOPT_ERR]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		-ECONNRESET,
		-ECONNRESET,	0,
	},
	[C_GETSOCKOPT_KA]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_GETSOCKOPT_RB]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_IOCTL_NREAD]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		1,
		0,		0,
	},
	[C_LISTEN]		= {
		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,	-EOPNOTSUPP,
		-EOPNOTSUPP,	-EOPNOTSUPP,
	},
	[C_RECV]		= {
		-EAGAIN,	0,		-EAGAIN,	0,
		-EAGAIN,	-EAGAIN,	0,		-EAGAIN,
		0,		-EAGAIN,	-EAGAIN,	-EAGAIN,
		0,		-EAGAIN,	0,		1,
		-ECONNRESET,	-EAGAIN,
	},
	[C_RECVFROM]		= {
		-EAGAIN,	0,		-EAGAIN,	0,
		-EAGAIN,	-EAGAIN,	0,		-EAGAIN,
		0,		-EAGAIN,	-EAGAIN,	-EAGAIN,
		0,		-EAGAIN,	0,		1,
		-ECONNRESET,	-EAGAIN,
	},
	[C_SEND]		= {
		-EDESTADDRREQ,	-EDESTADDRREQ,	-EPIPE,		-EPIPE,
		-EDESTADDRREQ,	1,		1,		-EPIPE,
		-EPIPE,		-ENOBUFS,	1,		-ENOBUFS,
		-ENOBUFS,	-EPIPE,		-EPIPE,		-ECONNRESET,
		-ECONNRESET,	-EDESTADDRREQ,
	},
	[C_SENDTO]		= {
		1,		1,		-EPIPE,		-EPIPE,
		1,		1,		1,		-EPIPE,
		-EPIPE,		-ENOBUFS,	1,		-ENOBUFS,
		-ENOBUFS,	-EPIPE,		-EPIPE,		-ECONNRESET,
		-ECONNRESET,	-ECONNREFUSED,
	},
	[C_SELECT_R]		= {
		0,		1,		0,		1,
		0,		0,		1,		0,
		1,		0,		0,		0,
		1,		0,		1,		1,
		1,		0,
	},
	[C_SELECT_W]		= {
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,		1,		1,
		1,		1,
	},
	[C_SELECT_X]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_SETSOCKOPT_BC]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_SETSOCKOPT_KA]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_SETSOCKOPT_L]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_SETSOCKOPT_RA]	= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_SHUTDOWN_R]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_SHUTDOWN_RW]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
	[C_SHUTDOWN_W]		= {
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,		0,		0,
		0,		0,
	},
};

/*
 * Set up a datagram socket file descriptor in the requested state and pass it
 * to socklib_sweep_call() along with local and remote addresses and their
 * length.
 */
static int
unix_dgram_sweep(int domain __unused, int type, int protocol __unused,
	enum state state, enum call call)
{
	struct sockaddr_un sunA, sunB;
	char buf[1];
	int r, fd, fd2;

	(void)unlink(SOCK_PATH_A);
	(void)unlink(SOCK_PATH_B);

	/* Create a bound remote socket. */
	fd2 = get_bound_socket(type | SOCK_NONBLOCK, SOCK_PATH_B, &sunB);

	switch (state) {
	case S_NEW:
	case S_N_SHUT_R:
	case S_N_SHUT_W:
	case S_N_SHUT_RW:
		memset(&sunA, 0, sizeof(sunA));
		sunA.sun_family = AF_UNIX;
		strlcpy(sunA.sun_path, SOCK_PATH_A, sizeof(sunA.sun_path));

		if ((fd = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0)
			e(0);

		switch (state) {
		case S_N_SHUT_R: if (shutdown(fd, SHUT_RD)) e(0); break;
		case S_N_SHUT_W: if (shutdown(fd, SHUT_WR)) e(0); break;
		case S_N_SHUT_RW: if (shutdown(fd, SHUT_RDWR)) e(0); break;
		default: break;
		}

		break;

	case S_BOUND:
	case S_CONNECTED:
	case S_SHUT_R:
	case S_SHUT_W:
	case S_SHUT_RW:
	case S_RSHUT_R:
	case S_RSHUT_W:
	case S_RSHUT_RW:
	case S_SHUT2_R:
	case S_SHUT2_W:
	case S_SHUT2_RW:
	case S_PRE_RESET:
	case S_AT_RESET:
	case S_POST_RESET:
		fd = get_bound_socket(type | SOCK_NONBLOCK, SOCK_PATH_A,
		    &sunA);

		if (state == S_BOUND)
			break;

		if (connect(fd, (struct sockaddr *)&sunB, sizeof(sunB)) != 0)
			e(0);

		switch (state) {
		case S_SHUT_R:
		case S_SHUT2_R: if (shutdown(fd, SHUT_RD)) e(0); break;
		case S_SHUT_W:
		case S_SHUT2_W: if (shutdown(fd, SHUT_WR)) e(0); break;
		case S_SHUT_RW:
		case S_SHUT2_RW: if (shutdown(fd, SHUT_RDWR)) e(0); break;
		default: break;
		}

		switch (state) {
		case S_RSHUT_R:
		case S_SHUT2_R: if (shutdown(fd2, SHUT_RD)) e(0); break;
		case S_RSHUT_W:
		case S_SHUT2_W: if (shutdown(fd2, SHUT_WR)) e(0); break;
		case S_RSHUT_RW:
		case S_SHUT2_RW: if (shutdown(fd2, SHUT_RDWR)) e(0); break;
		case S_PRE_RESET:
		case S_AT_RESET:
		case S_POST_RESET:
			if (sendto(fd2, "", 1, 0, (struct sockaddr *)&sunA,
			    sizeof(sunA)) != 1) e(0);

			if (close(fd2) != 0) e(0);
			fd2 = -1;

			if (state != S_PRE_RESET) {
				if (recv(fd, buf, sizeof(buf), 0) != 1) e(0);
			}
			if (state == S_POST_RESET) {
				(void)recv(fd, buf, sizeof(buf), 0);
			}
		default:
			break;
		}

		break;

	default:
		fd = -1;
		e(0);
	}

	r = socklib_sweep_call(call, fd, (struct sockaddr *)&sunA,
	    (struct sockaddr *)&sunB, sizeof(struct sockaddr_un));

	if (close(fd) != 0) e(0);
	if (fd2 != -1 && close(fd2) != 0) e(0);

	(void)unlink(SOCK_PATH_A);
	(void)unlink(SOCK_PATH_B);

	return r;
}

/*
 * Sweep test for socket calls versus socket states of all socket types.
 */
static void
test90a(void)
{

	subtest = 1;

	socklib_sweep(AF_UNIX, SOCK_STREAM, 0, unix_connect_states,
	    __arraycount(unix_connect_states),
	    (const int *)unix_connect_results, unix_connect_sweep);

	socklib_sweep(AF_UNIX, SOCK_SEQPACKET, 0, unix_connect_states,
	    __arraycount(unix_connect_states),
	    (const int *)unix_connect_results, unix_connect_sweep);

	socklib_sweep(AF_UNIX, SOCK_DGRAM, 0, unix_dgram_states,
	    __arraycount(unix_dgram_states), (const int *)unix_dgram_results,
	    unix_dgram_sweep);

}

/*
 * Test for large sends and receives with MSG_WAITALL.
 */
static void
test90b(void)
{
	int fd[2];

	subtest = 2;

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0) e(0);

	socklib_large_transfers(fd);
}

/*
 * A randomized producer-consumer test for datagram sockets.
 */
static void
sub90c(int type)
{
	char *buf;
	time_t t;
	socklen_t len, size;
	ssize_t r;
	pid_t pid;
	unsigned int count;
	int i, fd[2], rcvlen, status, exp, flags, num, stat[2] = { 0, 0 };

	get_socket_pair(type, fd);

	size = rcvlen = get_rcvbuf_len(fd[0]);

	if ((buf = malloc(size)) == NULL) e(0);

	t = time(NULL);

	/*
	 * We vary small versus large (random) send and receive sizes,
	 * splitting the entire transfer in four phases along those lines.
	 *
	 * In theory, the use of an extra system call and the use of MSG_PEEK
	 * both contribute to the expectation that the consumer side will fall
	 * behind the producer.  In this case, we cannot vary receive sizes to
	 * compensate.  This not appear to be a major problem here, though.
	 */
#define NR_PACKETS	(256 * 1024)

	pid = fork();
	switch (pid) {
	case 0:
		errct = 0;

		if (close(fd[0]) != 0) e(0);

		srand48(t + 1);

		for (count = 0; count < NR_PACKETS; ) {
			if (count < NR_PACKETS / 2)
				len = lrand48() % 64;
			else
				len = lrand48() % size;

			num = lrand48() % 16;
			flags = 0;
			if (num & 1) flags |= MSG_PEEK;
			if (num & 2) flags |= MSG_WAITALL;
			if (num & 4) flags |= MSG_DONTWAIT;
			if (num & 8) {
				/*
				 * Obviously there are race conditions here but
				 * the returned number should be accurate if
				 * not zero.  Hopefully it's not always zero.
				 */
				if (ioctl(fd[1], FIONREAD, &exp) != 0) e(0);
				if (exp < 0 || exp > rcvlen) e(0);
			} else
				exp = 0;

			stat[0]++;

			/*
			 * A lame approach to preventing unbounded spinning on
			 * ENOBUFS on the producer side.
			 */
			if (type == SOCK_DGRAM)
				(void)send(fd[1], "", 1, MSG_DONTWAIT);

			if ((r = recv(fd[1], buf, len, flags)) == -1) {
				if (errno != EWOULDBLOCK) e(0);
				if (exp > 0) e(0);

				stat[1]++;

				continue;
			}

			if (exp != 0) {
				if (r == len && exp < r) e(0);
				else if (r < len && exp != r) e(0);
			}

			if (r >= 2 &&
			    r > ((size_t)(unsigned char)buf[0] << 8) +
			    (size_t)(unsigned char)buf[1]) e(0);

			for (i = 2; i < r; i++)
				if (buf[i] != (char)i) e(0);

			if (!(flags & MSG_PEEK))
				count++;
		}

#if PRINT_STATS
		/*
		 * The second and third numbers should ideally be a large but
		 * non-dominating fraction of the first one.
		 */
		printf("RECV: total %d again %d\n", stat[0], stat[1]);
#endif

		if (close(fd[1]) != 0) e(0);
		exit(errct);
	case -1:
		e(0);
	}

	if (close(fd[1]) != 0) e(0);

	srand48(t);

	for (count = 0; count < NR_PACKETS; ) {
		if (count < NR_PACKETS / 4 ||
		    (count >= NR_PACKETS / 2 && count < NR_PACKETS * 3 / 4))
			len = lrand48() % 64;
		else
			len = lrand48() % size;

		buf[0] = (len >> 8) & 0xff;
		buf[1] = len & 0xff;
		for (i = 2; i < len; i++)
			buf[i] = i;

		flags = (lrand48() % 2) ? MSG_DONTWAIT : 0;

		r = send(fd[0], buf, len, flags);

		if (r != len) {
			if (r != -1) e(0);

			if (errno != EMSGSIZE && errno != EWOULDBLOCK &&
			    errno != ENOBUFS) e(0);

			if (errno == ENOBUFS || errno == EWOULDBLOCK) {
				/*
				 * As stated above: lame.  Ideally we would
				 * continue only when the receiver side drains
				 * the queue, but it may block once it has done
				 * so.  Instead, by going through consumer
				 * "tokens" we will ultimately block here and
				 * let the receiver catch up.
				 */
				if (type == SOCK_DGRAM && errno == ENOBUFS)
					(void)recv(fd[0], buf, 1, 0);

				stat[0]++;
				stat[1]++;
			}
			continue;
		} else
			stat[0]++;

		if (count % (NR_PACKETS / 4) == 0)
			sleep(1);

		count++;
	}

#if PRINT_STATS
	/*
	 * The second number should ideally be a large but non-dominating
	 * fraction of the first one.
	 */
	printf("SEND: total %d again %d\n", stat[0], stat[1]);
#endif

	free(buf);

	if (close(fd[0]) != 0) e(0);

	if (waitpid(pid, &status, 0) != pid) e(0);
	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);
}

/*
 * A randomized producer-consumer test.  As part of this, we also perform very
 * basic bulk functionality tests of FIONREAD, MSG_PEEK, MSG_DONTWAIT, and
 * MSG_WAITALL.
 */
static void
test90c(void)
{
	int fd[2];

	subtest = 4;

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0) e(0);

	socklib_producer_consumer(fd);

	sub90c(SOCK_SEQPACKET);

	sub90c(SOCK_DGRAM);
}

/*
 * Test that immediately accepted non-blocking connect requests to a listening
 * socket with LOCAL_CONNWAIT turned on, return OK rather than EINPROGRESS.
 * This requires a hack in libsockevent.
 */
static void
test90d(void)
{
	struct sockaddr_un sunA, sunB;
	socklen_t len;
	pid_t pid;
	int fd, fd2, fd3, val, status;

	subtest = 4;

	/*
	 * First ensure that a non-blocking connect to a listening socket that
	 * does not have a accept call blocked on it, fails with EINPROGRESS.
	 */
	fd = get_bound_socket(SOCK_STREAM, SOCK_PATH_A, &sunA);

	val = 1;
	if (setsockopt(fd, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0) e(0);

	if (listen(fd, 1) != 0) e(0);

	if ((fd2 = socket(AF_UNIX, SOCK_STREAM | SOCK_NONBLOCK, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != -1) e(0);
	if (errno != EINPROGRESS) e(0);

	len = sizeof(sunB);
	if ((fd3 = accept(fd, (struct sockaddr *)&sunB, &len)) < 0) e(0);
	check_addr(&sunB, len, NULL);

	if (close(fd) != 0) e(0);
	if (close(fd2) != 0) e(0);
	if (close(fd3) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);

	/*
	 * Second, ensure that a blocking connect eventually does return
	 * success if an accept call is made later on.
	 */
	fd = get_bound_socket(SOCK_STREAM, SOCK_PATH_A, &sunA);

	val = 1;
	if (setsockopt(fd, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0) e(0);

	if (listen(fd, 1) != 0) e(0);

	pid = fork();
	switch (pid) {
	case 0:
		errct = 0;

		sleep(1);

		len = sizeof(sunB);
		if ((fd2 = accept(fd, (struct sockaddr *)&sunB, &len)) < 0)
			e(0);
		check_addr(&sunB, len, NULL);

		exit(errct);
	case -1:
		e(0);
	}

	if (close(fd) != 0) e(0);

	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) e(0);

	if (connect(fd, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (close(fd) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);

	if (waitpid(pid, &status, 0) != pid) e(0);
	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);

	/*
	 * Finally, test the most implementation-complex case: a non-blocking
	 * connect should succeed (i.e., yield return code 0) immediately if
	 * there is a accept call blocked on it.
	 */
	fd = get_bound_socket(SOCK_STREAM, SOCK_PATH_A, &sunA);

	val = 1;
	if (setsockopt(fd, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0) e(0);

	if (listen(fd, 1) != 0) e(0);

	pid = fork();
	switch (pid) {
	case 0:
		errct = 0;

		len = sizeof(sunB);
		if ((fd2 = accept(fd, (struct sockaddr *)&sunB, &len)) < 0)
			e(0);
		check_addr(&sunB, len, SOCK_PATH_B);

		exit(errct);
	case -1:
		e(0);
	}

	if (close(fd) != 0) e(0);

	sleep(1);

	fd = get_bound_socket(SOCK_STREAM | SOCK_NONBLOCK, SOCK_PATH_B, &sunB);

	if (connect(fd, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (close(fd) != 0) e(0);

	if (waitpid(pid, &status, 0) != pid) e(0);
	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
	if (unlink(SOCK_PATH_B) != 0) e(0);
}

/*
 * Test self-connecting datagram sockets.
 */
static void
test90e(void)
{
	struct sockaddr_un sunA, sunB, sunC;
	socklen_t len;
	char buf[3];
	pid_t pid;
	int fdA, fdB, val, status;

	subtest = 5;

	fdA = get_bound_socket(SOCK_DGRAM, SOCK_PATH_A, &sunA);
	fdB = get_bound_socket(SOCK_DGRAM, SOCK_PATH_B, &sunB);

	/* Connect the socket to itself, and attempt to communicate. */
	if (connect(fdA, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (send(fdA, "abc", 3, 0) != 3) e(0);

	if (recv(fdA, buf, sizeof(buf), 0) != 3) e(0);
	if (strncmp(buf, "abc", 3)) e(0);

	/* Reconnect the socket to another target. */
	if (connect(fdA, (struct sockaddr *)&sunB, sizeof(sunB)) != 0) e(0);

	len = sizeof(val);
	if (getsockopt(fdA, SOL_SOCKET, SO_ERROR, &val, &len) != 0) e(0);
	if (val != 0) e(0);

	if (send(fdA, "def", 3, 0) != 3) e(0);

	memset(&sunC, 0, sizeof(sunC));
	len = sizeof(sunC);
	if (recvfrom(fdB, buf, sizeof(buf), 0, (struct sockaddr *)&sunC,
	    &len) != 3) e(0);
	check_addr(&sunC, len, SOCK_PATH_A);
	if (strncmp(buf, "def", 3)) e(0);

	/* Reconnect the socket to itself again. */
	if (connect(fdA, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	len = sizeof(val);
	if (getsockopt(fdA, SOL_SOCKET, SO_ERROR, &val, &len) != 0) e(0);
	if (val != 0) e(0);

	pid = fork();
	switch (pid) {
	case 0:
		errct = 0;

		if (recv(fdA, buf, sizeof(buf), 0) != 3) e(0);
		if (strncmp(buf, "ghi", 3)) e(0);

		exit(errct);
	case -1:
		e(0);
	}

	sleep(1);

	if (send(fdA, "ghi", 3, 0) != 3) e(0);

	if (waitpid(pid, &status, 0) != pid) e(0);
	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);

	if (close(fdA) != 0) e(0);
	if (close(fdB) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
	if (unlink(SOCK_PATH_B) != 0) e(0);
}

/*
 * Test multiple blocked calls getting resumed (or not) upon connect(2) success
 * or failure.  This test uses LOCAL_CONNWAIT.  TODO: rewrite this to use
 * interprocess communication rather than the current carefully arranged and
 * rather brittle timing approach.
 */
static void
sub90f(unsigned int test)
{
	struct sockaddr_un sun;
	pid_t pid[4], apid;
	char buf[1];
	unsigned int i;
	socklen_t len;
	int r, fd, fd2, fl, val, status;

	fd = get_bound_socket(SOCK_STREAM, SOCK_PATH_A, &sun);

	val = 1;
	if (setsockopt(fd, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0) e(0);

	if (listen(fd, 1) != 0) e(0);

	apid = fork();
	switch (apid) {
	case 0:
		errct = 0;

		sleep(3);

		if (test < 2) {
			len = sizeof(sun);
			if ((fd2 = accept(fd, (struct sockaddr *)&sun,
			    &len)) < 0) e(0);

			sleep(2);

			if (close(fd2) != 0) e(0);
		}

		if (close(fd) != 0) e(0);

		exit(errct);
	case -1:
		e(0);
	}

	if (close(fd) != 0) e(0);

	if ((fd = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) e(0);

	for (i = 0; i < __arraycount(pid); i++) {
		pid[i] = fork();
		switch (pid[i]) {
		case 0:
			errct = 0;

			sleep((i == 0) ? 1 : 2);

			if ((i & 1) == 0) {
				r = send(fd, "", 1, 0);

				switch (test) {
				case 0:
				case 1:
					if (r != 1) e(0);
					break;
				case 3:
					if (i == 0) {
						if (r != -1) e(0);
						if (errno != ECONNRESET) e(0);
						break;
					}
					/* FALLTHROUGH */
				case 2:
					if (r != -1) e(0);
					if (errno != ENOTCONN) e(0);
				}
			} else {
				r = recv(fd, buf, sizeof(buf), 0);

				if (test >= 2) {
					if (r != -1) e(0);
					if (errno != ENOTCONN) e(0);
				} else
					if (r != 0) e(0);
			}

			exit(errct);
		case -1:
			e(0);
		}
	}

	if (test & 1) {
		if ((fl = fcntl(fd, F_GETFL)) == -1) e(0);
		if (fcntl(fd, F_SETFL, fl | O_NONBLOCK) != 0) e(0);
	}

	r = connect(fd, (struct sockaddr *)&sun, sizeof(sun));

	if (test & 1) {
		if (r != -1) e(0);
		if (errno != EINPROGRESS) e(0);

		if (fcntl(fd, F_SETFL, fl) != 0) e(0);

		sleep(4);
	} else {
		if (test >= 2) {
			if (r != -1) e(0);
			if (errno != ECONNRESET) e(0);
		} else
			if (r != 0) e(0);

		sleep(1);
	}

	/*
	 * If the connect failed, collect the senders and receivers.
	 * Otherwise, collect just the senders.
	 */
	for (i = 0; i < __arraycount(pid); i++) {
		r = waitpid(pid[i], &status, WNOHANG);
		if (r == pid[i]) {
			if (test < 2 && (i & 1)) e(0);
			if (!WIFEXITED(status)) e(0);
			if (WEXITSTATUS(status) != 0) e(0);
		} else if (r == 0) {
			if (test >= 2 || !(i & 1)) e(0);
		} else
			e(0);
	}

	if (close(fd) != 0) e(0);

	/* Wait for, and collect the accepting child. */
	if (waitpid(apid, &status, 0) != apid) e(0);
	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);

	/*
	 * If the connect succeeded, collect the receivers, which will
	 * terminate once the accepting child closes the accepted socket.
	 */
	if (test < 2) {
		if (waitpid(pid[1], &status, 0) != pid[1]) e(0);
		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);
		if (waitpid(pid[3], &status, 0) != pid[3]) e(0);
		if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);
	}

	if (unlink(SOCK_PATH_A) != 0) e(0);
}

/*
 * Test multiple blocked calls getting resumed (or not) upon connect(2) success
 * or failure.  In particular, ensure that the error code ends up with the
 * right call.
 */
static void
test90f(void)
{

	subtest = 6;

	/* If a connect succeeds, sends continue but reads block until EOF. */
	sub90f(0);	/* blocking connect */
	sub90f(1);	/* non-blocking connect */

	/* If a blocking connect fails, the connect call gets the error. */
	sub90f(2);

	/* If a non-blocking connect fails, the first blocked call gets it. */
	sub90f(3);
}

/*
 * Test whether various calls all return the same expected error code.
 */
static void
sub90g(struct sockaddr_un * sun, int err)
{
	int fd;

	if ((fd = socket(AF_UNIX, SOCK_SEQPACKET, 0)) < 0) e(0);

	if (connect(fd, (struct sockaddr *)sun, sizeof(*sun)) != -1) e(0);
	if (errno != err) e(0);

	if (close(fd) != 0) e(0);

	if ((fd = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) e(0);

	if (sendto(fd, "", 1, 0, (struct sockaddr *)sun, sizeof(*sun)) != -1)
		e(0);
	if (errno != err) e(0);

	if (connect(fd, (struct sockaddr *)sun, sizeof(*sun)) != -1) e(0);
	if (errno != err) e(0);

	if (close(fd) != 0) e(0);
}

/*
 * Test for error codes thrown by connect(2) and sendto(2) with problematic
 * destinations.  In particular, we verify that the errors for sendto(2) are
 * the same as for connect(2), just like on NetBSD and Linux, even though
 * POSIX does not document all of these under sendto(2).
 */
static void
test90g(void)
{
	struct sockaddr_un sun;
	int fd;

	subtest = 7;

	fd = get_bound_socket(SOCK_STREAM, SOCK_PATH_A, &sun);

	sub90g(&sun, EPROTOTYPE);

	if (listen(fd, 1) != 0) e(0);

	sub90g(&sun, EPROTOTYPE);

	if (close(fd) != 0) e(0);

	sub90g(&sun, ECONNREFUSED);

	if (unlink(SOCK_PATH_A) != 0) e(0);

	sub90g(&sun, ENOENT);
}

/*
 * Test addresses returned for unbound connection-type sockets by various
 * calls.
 */
static void
sub90h(int type)
{
	struct sockaddr_un sun;
	socklen_t len;
	char buf[1];
	int fd, fd2, fd3;

	fd = get_bound_socket(type, SOCK_PATH_A, &sun);

	if (listen(fd, 5) != 0) e(0);

	if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sun, sizeof(sun)) != 0) e(0);

	/* Test for accept(2), which returns an empty address. */
	memset(&sun, 0, sizeof(sun));
	len = sizeof(sun);
	if ((fd3 = accept(fd, (struct sockaddr *)&sun, &len)) < 0) e(0);
	check_addr(&sun, len, NULL);

	/* Test for recvfrom(2), which ignores the address pointer. */
	if (send(fd2, "", 1, 0) != 1) e(0);

	memset(&sun, 0, sizeof(sun));
	len = sizeof(sun);
	if (recvfrom(fd3, buf, sizeof(buf), 0, (struct sockaddr *)&sun,
	    &len) != 1) e(0);
	if (len != 0) e(0);
	if (sun.sun_family != 0) e(0);
	if (sun.sun_len != 0) e(0);

	/* Test for getsockname(2), which returns an empty address. */
	memset(&sun, 0, sizeof(sun));
	len = sizeof(sun);
	if (getsockname(fd2, (struct sockaddr *)&sun, &len) != 0) e(0);
	check_addr(&sun, len, NULL);

	/* Test for getpeername(2), which returns an empty address. */
	memset(&sun, 0, sizeof(sun));
	len = sizeof(sun);
	if (getpeername(fd3, (struct sockaddr *)&sun, &len) != 0) e(0);
	check_addr(&sun, len, NULL);

	if (close(fd) != 0) e(0);
	if (close(fd2) != 0) e(0);
	if (close(fd3) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
}

/*
 * Test addresses returned for unbound sockets by various calls.
 */
static void
test90h(void)
{
	struct sockaddr_un sun;
	socklen_t len;
	char buf[1];
	int fd, fd2;

	subtest = 8;

	/* Connection-type socket tests. */
	sub90h(SOCK_STREAM);

	sub90h(SOCK_SEQPACKET);

	/* Datagram socket tests. */
	fd = get_bound_socket(SOCK_DGRAM, SOCK_PATH_A, &sun);

	if ((fd2 = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) e(0);

	if (sendto(fd2, "", 1, 0, (struct sockaddr *)&sun, sizeof(sun)) != 1)
		e(0);

	/*
	 * Datagram test for recvfrom(2), which returns no address.  This is
	 * the one result in this subtest that is not specified by POSIX and
	 * (not so coincidentally) is different between NetBSD and Linux.
	 * MINIX3 happens to follow Linux behavior for now, but this may be
	 * changed in the future.
	 */
	memset(&sun, 0, sizeof(sun));
	len = sizeof(sun);
	if (recvfrom(fd, buf, sizeof(buf), 0, (struct sockaddr *)&sun,
	    &len) != 1) e(0);
	if (len != 0) e(0);
	if (sun.sun_family != 0) e(0);
	if (sun.sun_len != 0) e(0);

	/* Datagram test for getsockname(2), which returns an empty address. */
	memset(&sun, 0, sizeof(sun));
	len = sizeof(sun);
	if (getsockname(fd2, (struct sockaddr *)&sun, &len) != 0) e(0);
	check_addr(&sun, len, NULL);

	if (close(fd) != 0) e(0);
	if (close(fd2) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
}

#define MAX_FDS 7

/*
 * Send anywhere from zero to MAX_FDS file descriptors onto a socket, possibly
 * along with regular data.  Return the result of the sendmsg(2) call, with
 * errno preserved.  Written to be reusable outside this test set.
 */
static int
send_fds(int fd, const char * data, size_t len, int flags,
	struct sockaddr * addr, socklen_t addr_len, int * fds, int nfds)
{
	union {
		char buf[CMSG_SPACE(MAX_FDS * sizeof(int))];
		struct cmsghdr cmsg;
	} control;
	struct msghdr msg;
	struct iovec iov;

	assert(nfds >= 0 && nfds <= MAX_FDS);

	iov.iov_base = __UNCONST(data);
	iov.iov_len = len;

	memset(&control.cmsg, 0, sizeof(control.cmsg));
	control.cmsg.cmsg_len = CMSG_LEN(nfds * sizeof(int));
	control.cmsg.cmsg_level = SOL_SOCKET;
	control.cmsg.cmsg_type = SCM_RIGHTS;
	memcpy(CMSG_DATA(&control.cmsg), fds, nfds * sizeof(int));

	memset(&msg, 0, sizeof(msg));
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = &control;
	msg.msg_controllen = control.cmsg.cmsg_len;
	msg.msg_name = addr;
	msg.msg_namelen = addr_len;

	return sendmsg(fd, &msg, flags);
}

/*
 * Receive anywhere from zero to up to MAX_FDS file descriptors from a socket,
 * possibly along with regular data.  The 'nfds' parameter must point to the
 * maximum number of file descriptors to be received.  Return the result of the
 * recvmsg(2) call, with errno preserved.  On success, return the received
 * flags in 'rflags', the received file descriptors stored in 'fds' and their
 * number stored in 'nfds'.  Written to be (somewhat) reusable.
 */
static int
recv_fds(int fd, char * buf, size_t size, int flags, int * rflags, int * fds,
	int * nfds)
{
	union {
		char buf[CMSG_SPACE(MAX_FDS * sizeof(int))];
		struct cmsghdr cmsg;
	} control;
	struct msghdr msg;
	struct iovec iov;
	size_t len;
	int r, rnfds;

	assert(*nfds >= 0 && *nfds <= MAX_FDS);

	iov.iov_base = buf;
	iov.iov_len = size;

	memset(&control.cmsg, 0, sizeof(control.cmsg));
	control.cmsg.cmsg_len = CMSG_LEN(*nfds * sizeof(int));
	control.cmsg.cmsg_level = SOL_SOCKET;
	control.cmsg.cmsg_type = SCM_RIGHTS;

	memset(&msg, 0, sizeof(msg));
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = &control;
	msg.msg_controllen = control.cmsg.cmsg_len;

	if ((r = recvmsg(fd, &msg, flags)) < 0)
		return r;

	if (msg.msg_controllen > 0) {
		assert(msg.msg_controllen <= sizeof(control));
		assert(msg.msg_controllen >= sizeof(control.cmsg));
		len = control.cmsg.cmsg_len - CMSG_LEN(0);
		assert(len % sizeof(int) == 0);
		rnfds = len / sizeof(int);
		assert(rnfds <= *nfds);

		memcpy(fds, CMSG_DATA(&control.cmsg), rnfds * sizeof(int));
	} else
		rnfds = 0;

	*rflags = msg.msg_flags;
	*nfds = rnfds;
	return r;
}

/*
 * Generate and send zero or more file descriptors onto a socket, possibly
 * along with regular data.  Return the result of the sendmsg(2) call, with
 * errno preserved.  Also return a set of peer FDs for each of the sent file
 * descriptors, which should later be used in a call to close_test_fds().
 */
static int
send_test_fds(int fd, const char * data, size_t len, int flags, int * peers,
	int nfds)
{
	int i, r, saved_errno, fds[MAX_FDS], pfd[2];

	if (nfds > MAX_FDS) e(0);

	for (i = 0; i < nfds; i++) {
		if (pipe2(pfd, O_NONBLOCK) != 0) e(0);

		peers[i] = pfd[0];
		fds[i] = pfd[1];
	}

	r = send_fds(fd, data, len, flags, NULL, 0, fds, nfds);
	saved_errno = errno;

	for (i = 0; i < nfds; i++)
		if (close(fds[i]) != 0) e(0);

	errno = saved_errno;
	return r;
}

/*
 * Given an array of peer file descriptors as returned from a call to
 * send_test_fds(), test if the original file descriptors have correctly been
 * closed, and close all peer file descriptors.  The ultimate goal here is to
 * detect any possible file descriptor leaks in the UDS service.
 */
static void
close_test_fds(int * peers, int nfds)
{
	char buf[1];
	unsigned int i;
	int fd;

	for (i = 0; i < nfds; i++) {
		fd = peers[i];

		/* If the other side is still open, we would get EAGAIN. */
		if (read(fd, buf, sizeof(buf)) != 0) e(0);

		if (close(peers[i]) != 0) e(0);
	}
}

/*
 * Receive and close zero or more file descriptors from a socket, possibly
 * along with regular data.  Return the result of the recvmsg(2) call, with
 * errno preserved.
 */
static int
recv_test_fds(int fd, char * buf, size_t size, int flags, int * rflags,
	int * nfds)
{
	int i, r, saved_errno, fds[MAX_FDS];

	if (*nfds > MAX_FDS) e(0);

	if ((r = recv_fds(fd, buf, size, flags, rflags, fds, nfds)) < 0)
		return r;
	saved_errno = errno;

	for (i = 0; i < *nfds; i++)
		if (close(fds[i]) != 0) e(0);

	errno = saved_errno;
	return r;
}

/*
 * Test receive requests on various socket states and in various forms.
 * Following this function requires a very close look at what is in the
 * receive queue versus what is being received.
 */
static void
sub90i_recv(int fd, int type, int state, int test, int sub, int sentfds)
{
	struct msghdr msg;
	struct iovec iov;
	char data[4];
	unsigned int i;
	int res, err, nfds, rflags;

	memset(data, 0, sizeof(data));

	if (sub & 2) {
		rflags = 0;
		nfds = sentfds;
		res = recv_test_fds(fd, data, (sub & 1) ? 0 : sizeof(data), 0,
		    &rflags, &nfds);
		if (rflags & MSG_CTRUNC) e(0);
		if (nfds != 0 && nfds != sentfds) e(0);
		if ((type == SOCK_STREAM) && (rflags & MSG_TRUNC)) e(0);
	} else {
		iov.iov_base = data;
		iov.iov_len = (sub & 1) ? 0 : sizeof(data);
		memset(&msg, 0, sizeof(msg));
		msg.msg_iov = &iov;
		msg.msg_iovlen = 1;
		res = recvmsg(fd, &msg, 0);
		if (res >= 0)
			rflags = msg.msg_flags;
		else
			rflags = 0;
		nfds = 0;
	}
	err = errno;

	if (res < -1 || res > (int)sizeof(data)) e(0);

	if (type == SOCK_STREAM) {
		if (sub & 1) {
			/*
			 * Zero-size requests should receive no regular data
			 * and no control data, even if the tail segment is
			 * zero-sized and terminated.  This policy is in place
			 * for simplicity reasons.
			 */
			if (res != 0) e(0);
			if (nfds != 0) e(0);
			if (rflags & MSG_CTRUNC) e(0);

			/*
			 * Since nothing happened yet, do another, now non-
			 * zero receive call immediately, and continue as if
			 * that was the first call.
			 */
			sub = (sub & ~1) | 2;
			nfds = sentfds;
			rflags = 0;
			res = recv_test_fds(fd, data, sizeof(data), 0, &rflags,
			    &nfds);
			if (rflags & (MSG_TRUNC | MSG_CTRUNC)) e(0);
			if (nfds != 0 && nfds != sentfds) e(0);
			err = errno;
			if (res < -1 || res > (int)sizeof(data)) e(0);
		}

		if (state == 0 && !(test & 1) && !(sub & 13)) {
			/*
			 * There are no regular data bytes to be received, and
			 * the current segment may still be extended (i.e.,
			 * there is no EOF condition), and we are trying to
			 * receive at least one data byte.  This is the
			 * SO_RCVLOWAT test.
			 */
			if (res != -1) e(0);
			if (err != EWOULDBLOCK) e(0);
			if (test == 4) {
				/*
				 * There are still pending file descriptors but
				 * we cannot get them, due to the SO_RCVLOWAT
				 * test.  This is proper behavior but somewhat
				 * annoying, because we want to see if UDS
				 * forgot to close any file descriptors.  So,
				 * we let it force-close them here.
				 */
				if (shutdown(fd, SHUT_RD) != 0) e(0);
				sub |= 8;
			}
		} else {
			i = 0;
			if (state == 1) {
				if (res < 1) e(0);
				if (data[i] != 'A') e(0);
				i++;
			}
			if ((state == 0 && (test & 1)) ||
			    (state == 1 && (test == 1 || test == 3))) {
				if (res < i + 1) e(0);
				if (data[i] != 'B') e(0);
				i++;
			}
			if ((sub & 4) && (state != 1 || test < 4)) {
				if (res < i + 1) e(0);
				if (data[i] != 'C') e(0);
				i++;
			}
			if (i != res) e(0);
			if (state == 0 && test >= 4) {
				if (sub & 2) {
					if (nfds != sentfds) e(0);
				} else
					if (!(rflags & MSG_CTRUNC)) e(0);
			}
		}

		if (state == 1 && test >= 4) {
			/*
			 * We just read the first segment, but there is a
			 * second segment with ancillary data.  Read it too.
			 */
			nfds = sentfds;
			rflags = 0;
			res = recv_test_fds(fd, data, sizeof(data), 0, &rflags,
			    &nfds);
			if (rflags & (MSG_TRUNC | MSG_CTRUNC)) e(0);
			if (nfds != sentfds) e(0); /* untouched on failure */
			if (res < -1 || res > (int)sizeof(data)) e(0);
			if (test != 5 && !(sub & 12)) {
				if (res != -1) e(0);
				if (errno != EWOULDBLOCK) e(0);
				/* As above. */
				if (shutdown(fd, SHUT_RD) != 0) e(0);
				sub |= 8;
			} else {
				if (res != (test == 5) + !!(sub & 4)) e(0);
				if (test == 5 && data[0] != 'B') e(0);
				if ((sub & 4) && data[res - 1] != 'C') e(0);
			}
		}
	} else {
		if (res != ((state == 1 || (test & 1)) && !(sub & 1))) e(0);
		if (state == 0 && test >= 4) {
			if (sub & 2) {
				if (nfds != sentfds) e(0);
			} else
				if (!(rflags & MSG_CTRUNC)) e(0);
		}
		if (res > 0 && data[0] != ((state == 1) ? 'A' : 'B')) e(0);

		if (state == 1) {
			nfds = sentfds;
			rflags = 0;
			res = recv_test_fds(fd, data, sizeof(data), 0, &rflags,
			    &nfds);
			if (res != (test & 1)) e(0);
			if (res > 0 && data[0] != 'B') e(0);
			if (nfds != ((test >= 4) ? sentfds : 0)) e(0);
		}

		if (sub & 4) {
			nfds = sentfds;
			rflags = 0;
			res = recv_test_fds(fd, data, sizeof(data), 0, &rflags,
			    &nfds);
			if (res != 1) e(0);
			if (data[0] != 'C') e(0);
			if (nfds != 0) e(0);
		}
	}

	/*
	 * At this point, there is nothing to receive.  Depending on
	 * whether we closed the socket, we expect EOF or EWOULDBLOCK.
	 */
	res = recv(fd, data, sizeof(data), 0);
	if (type == SOCK_DGRAM || !(sub & 8)) {
		if (res != -1) e(0);
		if (errno != EWOULDBLOCK) e(0);
	} else
		if (res != 0) e(0);
}

/*
 * Test send requests on various socket states and in various forms.
 */
static void
sub90i_send(int type, int state, int test, int sub)
{
	char *buf;
	int r, res, err, fd[2], peers[2], rcvlen;

	get_socket_pair(type | SOCK_NONBLOCK, fd);

	/*
	 * State 0: an empty buffer.
	 * State 1: a non-empty, non-full buffer.
	 * State 2: a full buffer.
	 */
	if (state == 2) {
		if (type == SOCK_STREAM) {
			rcvlen = get_rcvbuf_len(fd[0]);

			if ((buf = malloc(rcvlen)) == NULL) e(0);

			memset(buf, 'A', rcvlen);

			if (send(fd[0], buf, rcvlen, 0) != rcvlen) e(0);

			free(buf);
		} else {
			while ((r = send(fd[0], "A", 1, 0)) == 1);
			if (r != -1) e(0);
			if (errno !=
			    ((type == SOCK_SEQPACKET) ? EAGAIN : ENOBUFS))
				e(0);
		}
	} else if (state == 1)
		if (send(fd[0], "A", 1, 0) != 1) e(0);

	/*
	 * Test 0: no data, no control data.
	 * Test 1: data, no control data.
	 * Test 2: no data, empty control data.
	 * Test 3: data, empty control data.
	 * Test 4: no data, control data with a file descriptor.
	 * Test 5: data, control data with a file descriptor.
	 */
	switch (test) {
	case 0:
	case 1:
		res = send(fd[0], "B", test % 2, 0);
		err = errno;
		break;
	case 2:
	case 3:
		res = send_test_fds(fd[0], "B", test % 2, 0, NULL, 0);
		err = errno;
		break;
	case 4:
	case 5:
		res = send_test_fds(fd[0], "B", test % 2, 0, peers,
		    __arraycount(peers));
		err = errno;
		break;
	default:
		res = -1;
		err = EINVAL;
		e(0);
	}

	if (res < -1 || res > 1) e(0);

	switch (state) {
	case 0:
	case 1:
		if (res != (test % 2)) e(0);

		/*
		 * Subtest bit 0x1: try a zero-size receive first.
		 * Subtest bit 0x2: try receiving control data.
		 * Subtest bit 0x4: send an extra segment with no control data.
		 * Subtest bit 0x8: after completing receives, expect EOF.
		 */
		if (sub & 4)
			if (send(fd[0], "C", 1, 0) != 1) e(0);
		if (sub & 8)
			if (shutdown(fd[0], SHUT_WR) != 0) e(0);

		/*
		 * Assuming (sub&4), which means there is an extra "C"..
		 *
		 * For stream sockets, we should now receive:
		 * - state 0:
		 *   - test 0, 2: "C"
		 *   - test 1, 3: "BC"
		 *   - test 4: "C" (w/fds)
		 *   - test 5: "BC" (w/fds)
		 * - state 1:
		 *   - test 0, 2: "AC"
		 *   - test 1, 3: "ABC"
		 *   - test 4: "A", "C" (w/fds)
		 *   - test 5: "A", "BC" (w/fds)
		 *
		 * For packet sockets, we should now receive:
		 * - state 1:
		 *   - all tests: "A", followed by..
		 * - state 0, 1:
		 *   - test 0, 2: "" (no fds), "C"
		 *   - test 1, 3: "B" (no fds), "C"
		 *   - test 4: "" (w/fds), "C"
		 *   - test 5: "B" (w/fds), "C"
		 */
		sub90i_recv(fd[1], type, state, test, sub,
		    __arraycount(peers));

		break;
	case 2:
		/*
		 * Alright, the results are a bit tricky to interpret here,
		 * because UDS's current strict send admission control prevents
		 * the receive buffer from being fully utilized.  We therefore
		 * only test the following aspects:
		 *
		 * - if we sent no regular or control data to a stream socket,
		 *   the call should have succeeded (note that the presence of
		 *   empty control data may cause the call to fail);
		 * - if we sent either regular or control data to a stream
		 *   socket, the call should have failed with EWOULDBLOCK;
		 * - if the call failed, the error should have been EWOULDBLOCK
		 *   for connection-type sockets and ENOBUFS for connectionless
		 *   sockets.
		 *
		 * Everything else gets a pass; we can't even be sure that for
		 * packet-oriented sockets we completely filled up the buffer.
		 */
		if (res == -1) {
			if (type == SOCK_STREAM && test == 0) e(0);

			if (type != SOCK_DGRAM && err != EWOULDBLOCK) e(0);
			if (type == SOCK_DGRAM && err != ENOBUFS) e(0);
		} else
			if (type == SOCK_STREAM && test != 0) e(0);
		break;
	}

	/*
	 * Make sure there are no more in-flight file descriptors now, even
	 * before closing the socket.
	 */
	if (res >= 0 && test >= 4)
		close_test_fds(peers, __arraycount(peers));

	close(fd[0]);
	close(fd[1]);
}

/*
 * Test send and receive requests with regular data, control data, both, or
 * neither, and test segment boundaries.
 */
static void
test90i(void)
{
	int state, test, sub;

	subtest = 9;

	for (state = 0; state < 3; state++) {
		for (test = 0; test < 6; test++) {
			for (sub = 0; sub < ((state < 2) ? 16 : 1); sub++) {
				sub90i_send(SOCK_STREAM, state, test, sub);

				sub90i_send(SOCK_SEQPACKET, state, test, sub);

				sub90i_send(SOCK_DGRAM, state, test, sub);
			}
		}
	}
}

/*
 * Test segmentation of file descriptor transfer on a particular socket type.
 */
static void
sub90j(int type)
{
	char path[PATH_MAX], buf[2];
	int i, fd[2], out[7], in[7], rflags, nfds;
	ssize_t len;

	get_socket_pair(type, fd);

	for (i = 0; i < __arraycount(out); i++) {
		snprintf(path, sizeof(path), "file%d", i);
		out[i] = open(path, O_RDWR | O_CREAT | O_EXCL | O_TRUNC, 0644);
		if (out[i] < 0) e(0);
		if (write(out[i], path, strlen(path)) != strlen(path)) e(0);
		if (lseek(out[i], 0, SEEK_SET) != 0) e(0);
	}

	if (send_fds(fd[1], "A", 1, 0, NULL, 0, &out[0], 1) != 1) e(0);
	if (send_fds(fd[1], "B", 1, 0, NULL, 0, &out[1], 3) != 1) e(0);
	if (send_fds(fd[1], "C", 1, 0, NULL, 0, &out[4], 2) != 1) e(0);

	nfds = 2;
	if (recv_fds(fd[0], buf, sizeof(buf), 0, &rflags, &in[0], &nfds) != 1)
		e(0);
	if (buf[0] != 'A') e(0);
	if (rflags != 0) e(0);
	if (nfds != 1) e(0);

	nfds = 5;
	if (recv_fds(fd[0], buf, sizeof(buf), 0, &rflags, &in[1], &nfds) != 1)
		e(0);
	if (buf[0] != 'B') e(0);
	if (rflags != 0) e(0);
	if (nfds != 3) e(0);

	if (send_fds(fd[1], "D", 1, 0, NULL, 0, &out[6], 1) != 1) e(0);

	nfds = 2;
	if (recv_fds(fd[0], buf, sizeof(buf), 0, &rflags, &in[4], &nfds) != 1)
		e(0);
	if (buf[0] != 'C') e(0);
	if (rflags != 0) e(0);
	if (nfds != 2) e(0);

	nfds = 2;
	if (recv_fds(fd[0], buf, sizeof(buf), 0, &rflags, &in[6], &nfds) != 1)
		e(0);
	if (buf[0] != 'D') e(0);
	if (rflags != 0) e(0);
	if (nfds != 1) e(0);

	for (i = 0; i < __arraycount(in); i++) {
		len = read(in[i], path, sizeof(path));
		if (len < 5 || len > 7) e(0);
		path[len] = '\0';
		if (strncmp(path, "file", 4) != 0) e(0);
		if (atoi(&path[4]) != i) e(0);
		if (unlink(path) != 0) e(0);
		if (close(in[i]) != 0) e(0);
	}

	for (i = 0; i < __arraycount(out); i++)
		if (close(out[i]) != 0) e(0);

	/*
	 * While we're here, see if UDS properly closes any remaining in-flight
	 * file descriptors when the socket is closed.
	 */
	if (send_test_fds(fd[1], "E", 1, 0, out, 7) != 1) e(0);

	close(fd[0]);
	close(fd[1]);

	close_test_fds(out, 7);
}

/*
 * Test segmentation of file descriptor transfer.  That is, there are multiple
 * in-flight file descriptors, they must each be associated with their
 * respective segments.
 */
static void
test90j(void)
{

	subtest = 10;

	sub90j(SOCK_STREAM);

	sub90j(SOCK_SEQPACKET);

	sub90j(SOCK_DGRAM);
}

/*
 * Test whether we can deadlock UDS by making it close the last reference to
 * an in-flight file descriptor for a UDS socket.  Currently we allow VFS/UDS
 * to get away with throwing EDEADLK as a sledgehammer approach to preventing
 * problems with in-flight UDS sockets.
 */
static void
test90k(void)
{
	int r, fd[2], fd2;

	subtest = 11;

	get_socket_pair(SOCK_STREAM, fd);

	if ((fd2 = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) e(0);

	if ((r = send_fds(fd[0], "X", 1, 0, NULL, 0, &fd2, 1)) != 1) {
		if (r != -1) e(0);
		if (errno != EDEADLK) e(0); /* whew */
	}

	if (close(fd2) != 0) e(0);
	if (close(fd[0]) != 0) e(0);
	if (close(fd[1]) != 0) e(0); /* boom */
}

/*
 * Test whether we can make UDS run out of file descriptors by transferring a
 * UDS socket over itself and then closing all other references while it is
 * in-flight.  Currently we allow VFS/UDS to get away with throwing EDEADLK as
 * a sledgehammer approach to preventing problems with in-flight UDS sockets.
 */
static void
test90l(void)
{
	struct sockaddr_un sun;
	int i, r, fd, fd2;

	subtest = 12;

	fd = get_bound_socket(SOCK_DGRAM, SOCK_PATH_A, &sun);

	for (i = 0; i < OPEN_MAX + 1; i++) {
		if ((fd2 = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) e(0);

		if ((r = send_fds(fd2, "X", 1, 0, (struct sockaddr *)&sun,
		    sizeof(sun), &fd2, 1)) != 1) {
			if (r != -1) e(0);
			if (errno != EDEADLK) e(0); /* whew */
		}

		if (close(fd2) != 0) e(0); /* have fun in limbo.. */
	}

	if (close(fd) != 0) e(0);
	if (unlink(SOCK_PATH_A) != 0) e(0);
}

/*
 * Receive with credentials.
 */
static int
recv_creds(int fd, char * buf, size_t size, int flags, int * rflags,
	struct sockcred * sc, socklen_t * sc_len)
{
	union {
		char buf[CMSG_SPACE(SOCKCREDSIZE(NGROUPS_MAX))];
		struct cmsghdr cmsg;
	} control;
	struct msghdr msg;
	struct iovec iov;
	size_t len;
	int r;

	iov.iov_base = buf;
	iov.iov_len = size;

	memset(&control.cmsg, 0, sizeof(control.cmsg));
	control.cmsg.cmsg_len = CMSG_LEN(SOCKCREDSIZE(NGROUPS_MAX));
	control.cmsg.cmsg_level = SOL_SOCKET;
	control.cmsg.cmsg_type = SCM_RIGHTS;

	memset(&msg, 0, sizeof(msg));
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = &control;
	msg.msg_controllen = control.cmsg.cmsg_len;

	if ((r = recvmsg(fd, &msg, flags)) < 0)
		return r;

	if (msg.msg_controllen > 0) {
		assert(msg.msg_controllen <= sizeof(control));
		assert(msg.msg_controllen >= sizeof(control.cmsg));
		assert(control.cmsg.cmsg_len <= msg.msg_controllen);
		len = control.cmsg.cmsg_len - CMSG_LEN(0);
		assert(len >= sizeof(struct sockcred));
		assert(len <= SOCKCREDSIZE(NGROUPS_MAX));
		if (*sc_len > len)
			*sc_len = len;
		memcpy(sc, CMSG_DATA(&control.cmsg), *sc_len);
	} else
		*sc_len = 0;

	*rflags = msg.msg_flags;
	return r;
}

/*
 * Test basic credentials passing on connection-oriented sockets.
 */
static void
sub90m(int type)
{
	struct sockaddr_un sun;
	struct sockcred sc;
	struct msghdr msg;
	struct iovec iov;
	socklen_t len;
	char buf[1];
	int fd, fd2, fd3, val, rflags;

	fd = get_bound_socket(type, SOCK_PATH_A, &sun);

	val = 1;
	if (setsockopt(fd, 0, LOCAL_CREDS, &val, sizeof(val)) != 0) e(0);

	if (listen(fd, 1) != 0) e(0);

	if ((fd2 = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sun, sizeof(sun)) != 0) e(0);

	len = sizeof(sun);
	if ((fd3 = accept(fd, (struct sockaddr *)&sun, &len)) < 0) e(0);

	if (send(fd2, "A", 1, 0) != 1) e(0);
	if (send(fd2, "B", 1, 0) != 1) e(0);

	len = sizeof(sc);
	if (recv_creds(fd3, buf, sizeof(buf), 0, &rflags, &sc, &len) != 1)
		e(0);
	if (buf[0] != 'A') e(0);
	if (rflags != 0) e(0);
	if (len != sizeof(sc)) e(0);
	if (sc.sc_uid != getuid()) e(0);
	if (sc.sc_euid != geteuid()) e(0);

	len = sizeof(sc);
	if (recv_creds(fd3, buf, sizeof(buf), 0, &rflags, &sc, &len) != 1)
		e(0);
	if (buf[0] != 'B') e(0);
	if (rflags != 0) e(0);
	if (len != 0) e(0);

	if (send(fd3, "C", 1, 0) != 1) e(0);

	val = 1;
	if (setsockopt(fd2, 0, LOCAL_CREDS, &val, sizeof(val)) != 0) e(0);

	if (send(fd3, "D", 1, 0) != 1) e(0);

	val = 1;
	if (setsockopt(fd2, 0, LOCAL_CREDS, &val, sizeof(val)) != 0) e(0);

	if (send(fd3, "E", 1, 0) != 1) e(0);

	len = sizeof(sc);
	if (recv_creds(fd2, buf, sizeof(buf), 0, &rflags, &sc, &len) != 1)
		e(0);
	if (buf[0] != 'C') e(0);
	if (rflags != 0) e(0);
	if (len != 0) e(0);

	len = sizeof(sc);
	if (recv_creds(fd2, buf, sizeof(buf), 0, &rflags, &sc, &len) != 1)
		e(0);
	if (buf[0] != 'D') e(0);
	if (rflags != 0) e(0);
	if (len != sizeof(sc)) e(0);
	if (sc.sc_uid != getuid()) e(0);
	if (sc.sc_euid != geteuid()) e(0);

	memset(&msg, 0, sizeof(msg));
	iov.iov_base = buf;
	iov.iov_len = sizeof(buf);
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	if (recvmsg(fd2, &msg, 0) != 1) e(0);
	if (buf[0] != 'E') e(0);
	if (msg.msg_flags != MSG_CTRUNC) e(0);

	if (close(fd2) != 0) e(0);
	if (close(fd3) != 0) e(0);

	val = 0;
	if (setsockopt(fd, 0, LOCAL_CREDS, &val, sizeof(val)) != 0) e(0);

	if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sun, sizeof(sun)) != 0) e(0);

	len = sizeof(sun);
	if ((fd3 = accept(fd, (struct sockaddr *)&sun, &len)) < 0) e(0);

	if (send(fd2, "F", 1, 0) != 1) e(0);

	len = sizeof(sc);
	if (recv_creds(fd3, buf, sizeof(buf), 0, &rflags, &sc, &len) != 1)
		e(0);
	if (buf[0] != 'F') e(0);
	if (rflags != 0) e(0);
	if (len != 0) e(0);

	if (close(fd) != 0) e(0);
	if (close(fd2) != 0) e(0);
	if (close(fd3) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
}

/*
 * A few tests for credentials passing that matter to some applications:
 * the credentials passing setting is inherited by accepted connections from
 * their listening socket, and, credentials are passed only once on a
 * connection-oriented socket.
 */
static void
test90m(void)
{
	struct sockcred sc;
	socklen_t len;
	char buf[1];
	int fd[2], val, rflags;

	subtest = 13;

	sub90m(SOCK_STREAM);

	sub90m(SOCK_SEQPACKET);

	get_socket_pair(SOCK_DGRAM, fd);

	val = 1;
	if (setsockopt(fd[0], 0, LOCAL_CREDS, &val, sizeof(val)) != 0) e(0);

	if (send(fd[1], "A", 1, 0) != 1) e(0);
	if (send(fd[0], "B", 1, 0) != 1) e(0);
	if (send(fd[1], "C", 1, 0) != 1) e(0);

	len = sizeof(sc);
	if (recv_creds(fd[0], buf, sizeof(buf), 0, &rflags, &sc, &len) != 1)
		e(0);
	if (buf[0] != 'A') e(0);
	if (rflags != 0) e(0);
	if (len != sizeof(sc)) e(0);
	if (sc.sc_uid != getuid()) e(0);
	if (sc.sc_euid != geteuid()) e(0);

	len = sizeof(sc);
	if (recv_creds(fd[1], buf, sizeof(buf), 0, &rflags, &sc, &len) != 1)
		e(0);
	if (buf[0] != 'B') e(0);
	if (rflags != 0) e(0);
	if (len != 0) e(0);

	len = sizeof(sc);
	if (recv_creds(fd[0], buf, sizeof(buf), 0, &rflags, &sc, &len) != 1)
		e(0);
	if (buf[0] != 'C') e(0);
	if (rflags != 0) e(0);
	if (len != sizeof(sc)) e(0);
	if (sc.sc_uid != getuid()) e(0);
	if (sc.sc_euid != geteuid()) e(0);

	if (close(fd[0]) != 0) e(0);
	if (close(fd[1]) != 0) e(0);
}

/*
 * Test whether MSG_CMSG_CLOEXEC is honored when copying in file descriptors.
 * We do not bother to test with execve(2w); obtaining the FD flags suffices.
 */
static void
test90n(void)
{
	char buf[1];
	int i, fd[2], sfd, rfd, fl, rflags, nfds;

	subtest = 14;

	get_socket_pair(SOCK_STREAM, fd);

	if ((sfd = open("/dev/null", O_RDONLY)) < 0) e(0);

	if (send_fds(fd[0], "A", 1, 0, NULL, 0, &sfd, 1) != 1) e(0);
	if (send_fds(fd[0], "B", 1, 0, NULL, 0, &sfd, 1) != 1) e(0);

	if ((fl = fcntl(sfd, F_GETFD, 0)) < 0) e(0);
	if (fcntl(sfd, F_SETFD, fl | FD_CLOEXEC) != 0) e(0);

	if (send_fds(fd[0], "C", 1, 0, NULL, 0, &sfd, 1) != 1) e(0);
	if (send_fds(fd[0], "D", 1, 0, NULL, 0, &sfd, 1) != 1) e(0);

	for (i = 0; i < 4; i++) {
		fl = (i & 1) ? MSG_CMSG_CLOEXEC : 0;
		nfds = 1;
		if (recv_fds(fd[1], buf, sizeof(buf), fl, &rflags, &rfd,
		    &nfds) != 1) e(0);
		if (buf[0] != 'A' + i) e(0);
		if (rflags != 0) e(0);
		if (nfds != 1) e(0);

		if ((fl = fcntl(rfd, F_GETFD, 0)) < 0) e(0);
		if (!!(fl & FD_CLOEXEC) != (i & 1)) e(0);

		if (close(rfd) != 0) e(0);
	}

	if (close(sfd) != 0) e(0);
	if (close(fd[0]) != 0) e(0);
	if (close(fd[1]) != 0) e(0);
}

/*
 * Test failures sending and receiving sets of file descriptors.
 */
static void
sub90o(int type)
{
	static int ofd[OPEN_MAX];
	char buf[1];
	int i, fd[2], sfd[2], rfd[2], rflags, nfds;

	get_socket_pair(type, fd);

	if ((sfd[0] = open("/dev/null", O_RDONLY)) < 0) e(0);
	sfd[1] = -1;

	if (send_fds(fd[0], "A", 1, 0, NULL, 0, &sfd[1], 1) != -1) e(0);
	if (errno != EBADF) e(0);
	if (send_fds(fd[0], "B", 1, 0, NULL, 0, &sfd[0], 2) != -1) e(0);
	if (errno != EBADF) e(0);
	if ((sfd[1] = dup(sfd[0])) < 0) e(0);
	if (send_fds(fd[0], "C", 1, 0, NULL, 0, &sfd[0], 2) != 1) e(0);

	for (i = 0; i < __arraycount(ofd); i++) {
		if ((ofd[i] = dup(sfd[0])) < 0) {
			/* Either will do. */
			if (errno != EMFILE && errno != ENFILE) e(0);
			break;
		}
	}

	nfds = 2;
	if (recv_fds(fd[1], buf, sizeof(buf), 0, &rflags, rfd, &nfds) != -1)
		e(0);
	if (errno != ENFILE && errno != EMFILE) e(0);

	if (close(sfd[1]) != 0) e(0);

	nfds = 2;
	if (recv_fds(fd[1], buf, sizeof(buf), 0, &rflags, rfd, &nfds) != -1)
		e(0);
	if (errno != ENFILE && errno != EMFILE) e(0);

	if (close(sfd[0]) != 0) e(0);

	nfds = 2;
	if (recv_fds(fd[1], buf, sizeof(buf), 0, &rflags, rfd, &nfds) != 1)
		e(0);
	if (buf[0] != 'C') e(0);
	if (rflags != 0) e(0);
	if (nfds != 2) e(0);

	if (close(rfd[1]) != 0) e(0);
	if (close(rfd[0]) != 0) e(0);
	while (i-- > 0)
		if (close(ofd[i]) != 0) e(0);
	if (close(fd[1]) != 0) e(0);
	if (close(fd[0]) != 0) e(0);
}

/*
 * Test failures sending and receiving sets of file descriptors.
 */
static void
test90o(void)
{
	const int types[] = { SOCK_STREAM, SOCK_SEQPACKET, SOCK_DGRAM };
	int i;

	subtest = 15;

	for (i = 0; i < OPEN_MAX + 1; i++)
		sub90o(types[i % __arraycount(types)]);
}

/*
 * Test socket reuse for a particular socket type.
 */
static void
sub90p(int type)
{
	struct sockaddr_un sunA, sunB, sunC;
	socklen_t len;
	char buf[1];
	uid_t euid;
	gid_t egid;
	int fd, fd2, fd3, val;

	if ((fd = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);
	/* Unconnected. */

	if (getpeereid(fd, &euid, &egid) != -1) e(0);
	if (errno != ENOTCONN) e(0);

	fd2 = get_bound_socket(type, SOCK_PATH_A, &sunA);

	val = 1;
	if (setsockopt(fd2, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0) e(0);

	if (listen(fd2, 5) != 0) e(0);

	if (connect(fd, (struct sockaddr *)&sunA, sizeof(sunA)) != -1) e(0);
	if (errno != EINPROGRESS) e(0);
	/* Connecting. */

	if (getpeereid(fd, &euid, &egid) != -1) e(0);
	if (errno != ENOTCONN) e(0);

	len = sizeof(sunB);
	if ((fd3 = accept(fd2, (struct sockaddr *)&sunB, &len)) < 0) e(0);
	/* Connected. */

	len = sizeof(sunC);
	if (getpeername(fd, (struct sockaddr *)&sunC, &len) != 0) e(0);
	check_addr(&sunC, len, SOCK_PATH_A);

	if (getpeereid(fd, &euid, &egid) != 0) e(0);
	if (euid == -1 || egid == -1) e(0);

	if (getpeereid(fd3, &euid, &egid) != 0) e(0);
	if (euid == -1 || egid == -1) e(0);

	if (send(fd3, "A", 1, 0) != 1) e(0);
	if (send(fd3, "B", 1, 0) != 1) e(0);
	if (send(fd3, "C", 1, 0) != 1) e(0);

	if (close(fd3) != 0) e(0);
	/* Disconnected. */

	if (getpeereid(fd, &euid, &egid) != -1) e(0);
	if (errno != ENOTCONN) e(0);

	if (close(fd2) != 0) e(0);
	fd2 = get_bound_socket(type, SOCK_PATH_B, &sunA);

	if (listen(fd2, 5) != 0) e(0);

	val = 1;
	if (setsockopt(fd, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0) e(0);

	if (connect(fd, (struct sockaddr *)&sunA, sizeof(sunA)) != -1) e(0);
	if (errno != EINPROGRESS) e(0);
	/* Connecting. */

	if (getpeereid(fd, &euid, &egid) != -1) e(0);
	if (errno != ENOTCONN) e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'A') e(0);

	len = sizeof(sunB);
	if ((fd3 = accept(fd2, (struct sockaddr *)&sunB, &len)) < 0) e(0);
	/* Connected. */

	if (send(fd3, "D", 1, 0) != 1) e(0);
	if (send(fd3, "E", 1, 0) != 1) e(0);

	len = sizeof(sunC);
	if (getpeername(fd, (struct sockaddr *)&sunC, &len) != 0) e(0);
	check_addr(&sunC, len, SOCK_PATH_B);

	if (getpeereid(fd, &euid, &egid) != 0) e(0);
	if (euid == -1 || egid == -1) e(0);

	if (close(fd2) != 0) e(0);
	if (unlink(SOCK_PATH_B) != 0) e(0);

	if (close(fd3) != 0) e(0);
	/* Disconnected. */

	if (connect(fd, (struct sockaddr *)&sunA, sizeof(sunA)) != -1) e(0);
	if (errno != ENOENT) e(0);
	/* Unconnected. */

	if (getpeereid(fd, &euid, &egid) != -1) e(0);
	if (errno != ENOTCONN) e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'B') e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);

	if (bind(fd, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'C') e(0);

	val = 0;
	if (setsockopt(fd, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0) e(0);

	if (listen(fd, 1) != 0) e(0);
	/* Listening. */

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'D') e(0);

	if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	len = sizeof(sunC);
	if ((fd3 = accept(fd, (struct sockaddr *)&sunC, &len)) < 0) e(0);

	if (send(fd2, "F", 1, 0) != 1) e(0);

	if (recv(fd3, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'F') e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'E') e(0);

	if (recv(fd, buf, 1, 0) != -1) e(0);
	if (errno != ENOTCONN) e(0);

	/* It should be possible to obtain peer credentials now. */
	if (getpeereid(fd2, &euid, &egid) != 0) e(0);
	if (euid == -1 || egid == -1) e(0);

	if (getpeereid(fd3, &euid, &egid) != 0) e(0);
	if (euid == -1 || egid == -1) e(0);

	if (close(fd3) != 0) e(0);
	if (close(fd2) != 0) e(0);

	if (close(fd) != 0) e(0);
	/* Closed. */

	if (unlink(SOCK_PATH_B) != 0) e(0);

	if ((fd = socket(AF_UNIX, type, 0)) < 0) e(0);
	/* Unconnected. */

	fd2 = get_bound_socket(type, SOCK_PATH_A, &sunA);

	if (listen(fd2, 5) != 0) e(0);

	if (connect(fd, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);
	/* Connected. */

	len = sizeof(sunB);
	if ((fd3 = accept(fd2, (struct sockaddr *)&sunB, &len)) < 0) e(0);

	if (close(fd2) != 0) e(0);

	memset(&sunB, 0, sizeof(sunB));
	sunB.sun_family = AF_UNIX;
	strlcpy(sunB.sun_path, SOCK_PATH_B, sizeof(sunB.sun_path));

	if (bind(fd, (struct sockaddr *)&sunB, sizeof(sunB)) != 0) e(0);

	if (close(fd3) != 0) e(0);
	/* Disconnected. */

	if (listen(fd, 1) != 0) e(0);
	/* Listening. */

	if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sunB, sizeof(sunB)) != 0) e(0);

	len = sizeof(sunC);
	if ((fd3 = accept(fd, (struct sockaddr *)&sunC, &len)) < 0) e(0);

	/* It should NOT be possible to obtain peer credentials now. */
	if (getpeereid(fd2, &euid, &egid) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (getpeereid(fd3, &euid, &egid) != 0) e(0);
	if (euid == -1 || egid == -1) e(0);

	if (close(fd3) != 0) e(0);
	if (close(fd2) != 0) e(0);

	if (close(fd) != 0) e(0);
	/* Closed. */

	if (unlink(SOCK_PATH_A) != 0) e(0);
	if (unlink(SOCK_PATH_B) != 0) e(0);
}

/*
 * Test socket reuse, receiving left-overs in the receive buffer, and the
 * (in)ability to obtain peer credentials.
 */
static void
test90p(void)
{

	subtest = 16;

	sub90p(SOCK_STREAM);

	sub90p(SOCK_SEQPACKET);
}

/*
 * Test state changes and errors related to connected datagram sockets.
 */
static void
test90q(void)
{
	struct sockaddr_un sunA, sunB, sunC, sunD;
	socklen_t len;
	char buf[1];
	int fd, fd2, fd3, val;

	subtest = 17;

	/*
	 * Sending a datagram to a datagram socket connected elsewhere should
	 * fail explicitly (specifically, EPERM).
	 */
	fd = get_bound_socket(SOCK_DGRAM, SOCK_PATH_A, &sunA);
	fd2 = get_bound_socket(SOCK_DGRAM, SOCK_PATH_B, &sunB);
	fd3 = get_bound_socket(SOCK_DGRAM, SOCK_PATH_C, &sunC);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (sendto(fd3, "A", 1, 0, (struct sockaddr *)&sunB,
	    sizeof(sunB)) != -1) e(0);
	if (errno != EPERM) e(0);

	/* Similarly, connecting to such a socket should fail. */
	if (connect(fd3, (struct sockaddr *)&sunB, sizeof(sunB)) != -1) e(0);
	if (errno != EPERM) e(0);

	if (send(fd2, "B", 1, 0) != 1) e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'B') e(0);

	/* Reconnection of a socket's target should result in ECONNRESET. */
	if (connect(fd, (struct sockaddr *)&sunC, sizeof(sunC)) != 0) e(0);

	len = sizeof(val);
	if (getsockopt(fd2, SOL_SOCKET, SO_ERROR, &val, &len) != 0) e(0);
	if (val != ECONNRESET) e(0);

	if (send(fd2, "C", 1, 0) != -1) e(0);
	if (errno != EDESTADDRREQ) e(0);

	if (send(fd, "D", 1, 0) != 1) e(0);

	len = sizeof(sunD);
	if (recvfrom(fd3, buf, 1, 0, (struct sockaddr *)&sunD, &len) != 1)
		e(0);
	if (buf[0] != 'D') e(0);
	check_addr(&sunD, len, SOCK_PATH_A);

	if (connect(fd2, (struct sockaddr *)&sunC, sizeof(sunC)) != 0) e(0);

	if (connect(fd3, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	memset(&sunD, 0, sizeof(sunD));
	sunD.sun_family = AF_UNIX;
	strlcpy(sunD.sun_path, SOCK_PATH_D, sizeof(sunD.sun_path));

	/* A failed reconnection attempt should not break the previous one. */
	if (connect(fd3, (struct sockaddr *)&sunD, sizeof(sunD)) != -1) e(0);
	if (errno != ENOENT) e(0);

	/* The destination address should be ignored here. */
	if (sendto(fd3, "E", 1, 0, (struct sockaddr *)&sunB,
	    sizeof(sunB)) != 1) e(0);

	if (recv(fd2, buf, 1, 0) != -1) e(0);
	if (errno != ECONNRESET) e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'E') e(0);

	if (close(fd3) != 0) e(0);

	len = sizeof(val);
	if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) != 0) e(0);
	if (val != ECONNRESET) e(0);

	if (close(fd2) != 0) e(0);

	len = sizeof(val);
	if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) != 0) e(0);
	if (val != 0) e(0);

	if (close(fd) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
	if (unlink(SOCK_PATH_B) != 0) e(0);
	if (unlink(SOCK_PATH_C) != 0) e(0);

	/*
	 * Finally, test unconnecting sockets.
	 */
	fd = get_bound_socket(SOCK_DGRAM, SOCK_PATH_A, &sunA);
	fd2 = get_bound_socket(SOCK_DGRAM, SOCK_PATH_B, &sunB);

	if (connect(fd, (struct sockaddr *)&sunB, sizeof(sunB)) != 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (sendto(fd2, "F", 1, 0, NULL, 0) != 1) e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'F') e(0);

	memset(&sunC, 0, sizeof(sunC));
	sunC.sun_family = AF_UNSPEC;
	if (connect(fd2, (struct sockaddr *)&sunC, sizeof(sunC)) != 0) e(0);

	len = sizeof(val);
	if (getsockopt(fd, SOL_SOCKET, SO_ERROR, &val, &len) != 0) e(0);
	if (val != 0) e(0);

	if (send(fd, "G", 1, 0) != 1) e(0);

	if (sendto(fd2, "H", 1, 0, NULL, 0) != -1) e(0);
	if (errno != EDESTADDRREQ) e(0);

	if (sendto(fd2, "I", 1, 0, (struct sockaddr *)&sunA, sizeof(sunA)) != 1)
		e(0);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (sendto(fd2, "J", 1, 0, NULL, 0) != 1) e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'I') e(0);

	if (recv(fd, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'J') e(0);

	if (recv(fd2, buf, 1, 0) != 1) e(0);
	if (buf[0] != 'G') e(0);

	if (close(fd) != 0) e(0);
	if (close(fd2) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
	if (unlink(SOCK_PATH_B) != 0) e(0);
}

/*
 * Test socket file name reuse.
 */
static void
test90r(void)
{
	struct sockaddr_un sun;
	socklen_t len;
	int fd, fd2, fd3, fd4;

	subtest = 18;

	fd = get_bound_socket(SOCK_STREAM | SOCK_NONBLOCK, SOCK_PATH_A, &sun);

	if (rename(SOCK_PATH_A, SOCK_PATH_B) != 0) e(0);

	if ((fd3 = socket(AF_UNIX, SOCK_STREAM, 0)) < 0) e(0);

	if (connect(fd3, (struct sockaddr *)&sun, sizeof(sun)) != -1) e(0);
	if (errno != ENOENT) e(0);

	if (listen(fd, 1) != 0) e(0);

	len = sizeof(sun);
	if (getsockname(fd, (struct sockaddr *)&sun, &len) != 0) e(0);
	check_addr(&sun, len, SOCK_PATH_A);

	fd2 = get_bound_socket(SOCK_STREAM | SOCK_NONBLOCK, SOCK_PATH_A, &sun);

	if (listen(fd2, 1) != 0) e(0);

	len = sizeof(sun);
	if (getsockname(fd2, (struct sockaddr *)&sun, &len) != 0) e(0);
	check_addr(&sun, len, SOCK_PATH_A);

	len = sizeof(sun);
	if (getsockname(fd, (struct sockaddr *)&sun, &len) != 0) e(0);
	check_addr(&sun, len, SOCK_PATH_A);

	memset(&sun, 0, sizeof(sun));
	sun.sun_family = AF_UNIX;
	strlcpy(sun.sun_path, SOCK_PATH_B, sizeof(sun.sun_path));
	if (connect(fd3, (struct sockaddr *)&sun, sizeof(sun)) != 0) e(0);

	len = sizeof(sun);
	if ((fd4 = accept(fd2, (struct sockaddr *)&sun, &len)) >= 0) e(0);
	if (errno != EWOULDBLOCK) e(0);
	if ((fd4 = accept(fd, (struct sockaddr *)&sun, &len)) < 0) e(0);

	len = sizeof(sun);
	if (getpeername(fd3, (struct sockaddr *)&sun, &len) != 0) e(0);
	check_addr(&sun, len, SOCK_PATH_A);

	if (close(fd) != 0) e(0);
	if (close(fd2) != 0) e(0);
	if (close(fd3) != 0) e(0);
	if (close(fd4) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
	if (unlink(SOCK_PATH_B) != 0) e(0);
}

/*
 * Test that non-canonized path names are accepted and returned.
 * Also test datagram send errors on disconnect.
 */
static void
test90s(void)
{
	struct sockaddr_un sun;
	socklen_t len;
	int fd, fd2;

	subtest = 19;

	fd = get_bound_socket(SOCK_DGRAM, SOCK_PATH_A_X, &sun);

	if ((fd2 = socket(AF_UNIX, SOCK_DGRAM, 0)) < 0) e(0);

	memset(&sun, 0, sizeof(sun));
	sun.sun_family = AF_UNIX;
	strlcpy(sun.sun_path, SOCK_PATH_A_Y, sizeof(sun.sun_path));

	if (connect(fd2, (struct sockaddr *)&sun, sizeof(sun)) != 0) e(0);

	len = sizeof(sun);
	if (getpeername(fd2, (struct sockaddr *)&sun, &len) != 0) e(0);
	check_addr(&sun, len, SOCK_PATH_A_X);

	if (send(fd2, "A", 1, 0) != 1) e(0);

	if (close(fd) != 0) e(0);

	if (send(fd2, "B", 1, 0) != -1) e(0);
	if (errno != ECONNRESET) e(0);

	if (send(fd2, "B", 1, 0) != -1) e(0);
	if (errno != EDESTADDRREQ) e(0);

	if (close(fd2) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
}

/*
 * Test basic sysctl(2) socket enumeration for a specific socket type.
 */
static void
sub90t(int type, const char * path)
{
	struct kinfo_pcb *ki;
	size_t i, len, oldlen;
	int fd, mib[8];

	if ((fd = socket(AF_UNIX, type, 0)) < 0) e(0);

	memset(mib, 0, sizeof(mib));

	len = __arraycount(mib);
	if (sysctlnametomib(path, mib, &len) != 0) e(0);
	if (len != 4) e(0);

	if (sysctl(mib, __arraycount(mib), NULL, &oldlen, NULL, 0) != 0) e(0);
	if (oldlen == 0) e(0);
	if (oldlen % sizeof(*ki)) e(0);

	if ((ki = (struct kinfo_pcb *)malloc(oldlen)) == NULL) e(0);

	if (sysctl(mib, __arraycount(mib), ki, &oldlen, NULL, 0) != 0) e(0);
	if (oldlen == 0) e(0);
	if (oldlen % sizeof(*ki)) e(0);

	/*
	 * We cannot check a whole lot of things, because we have no way of
	 * knowing which is the socket we created.  Check some basics and leave
	 * it at that.  This subtest is mostly trying to guarantee that
	 * netstat(1) will not show nothing, anyway.
	 */
	for (i = 0; i < oldlen / sizeof(*ki); i++) {
		if (ki[i].ki_pcbaddr == 0) e(0);
		if (ki[i].ki_sockaddr == 0) e(0);
		if (ki[i].ki_family != AF_UNIX) e(0);
		if (ki[i].ki_type != type) e(0);
		if (ki[i].ki_protocol != 0) e(0);
	}

	free(ki);

	if (close(fd) != 0) e(0);
}

/*
 * Test basic sysctl(2) socket enumeration support.
 */
static void
test90t(void)
{

	subtest = 20;

	/*
	 * We test that for each of the socket types, when we create a socket,
	 * we can find at least one socket of that type in the respective
	 * sysctl(2) out.
	 */
	sub90t(SOCK_STREAM, "net.local.stream.pcblist");

	sub90t(SOCK_SEQPACKET, "net.local.seqpacket.pcblist");

	sub90t(SOCK_DGRAM, "net.local.dgram.pcblist");
}

/*
 * Cause a pending recv() call to return.  Here 'fd' is the file descriptor
 * identifying the other end of the socket pair.  If breaking the recv()
 * requires sending data, 'data' and 'len' identify the data that should be
 * sent.  Return 'fd' if it is still open, or -1 if it is closed.
 */
static int
break_uds_recv(int fd, const char * data, size_t len)
{
	int fd2;

	/*
	 * This UDS-specific routine makes the recv() in one of two ways
	 * depending on whether the recv() call already made partial progress:
	 * if it did, this send call creates a segment boundary which should
	 * cut short the current receive call.  If it did not, the send call
	 * will simply satisfy the receive call with regular data.
	 */
	if ((fd2 = open("/dev/null", O_RDONLY)) < 0) e(0);

	if (send_fds(fd, data, len, 0, NULL, 0, &fd2, 1) != len) e(0);

	if (close(fd2) != 0) e(0);

	return fd;
}

/*
 * Test for receiving on stream sockets.  In particular, test SO_RCVLOWAT,
 * MSG_PEEK, MSG_DONTWAIT, and MSG_WAITALL.
 */
static void
test90u(void)
{

	subtest = 21;

	socklib_stream_recv(socketpair, AF_UNIX, SOCK_STREAM, break_uds_recv);
}

#define MAX_BYTES	2	/* set to 3 for slightly better(?) testing */
#define USLEEP_TIME	250000	/* increase on wimpy platforms if needed */

/*
 * Signal handler which just needs to exist, so that invoking it will interrupt
 * an ongoing system call.
 */
static void
test90_got_signal(int sig __unused)
{

	/* Nothing. */
}

/*
 * Test for sending on stream sockets.  The quick summary here is that send()
 * should basically act as the mirror of recv(MSG_WAITALL), i.e., it should
 * keep suspending until all data is sent (or the call is interrupted or no
 * more can possibly be sent), and, SO_SNDLOWAT, mirroring SO_RCVLOWAT, acts as
 * an admission test for the send: nothing is sent until there is room in the
 * send buffer (i.e., the peer's receive buffer) for at least the low send
 * watermark, or the whole send request length, whichever is smaller.  In
 * addition, select(2) should use the same threshold.
 */
static void
sub90v(int iroom, int istate, int slowat, int len, int bits, int act)
{
	const char *data = "ABC";	/* this limits MAX_BYTES to 3 */
	struct sigaction sa;
	struct timeval tv;
	fd_set fds;
	char buf[2], *sndbuf;
	pid_t pid;
	int fd[2], rcvlen, min, flags, res, err;
	int pfd[2], eroom, tstate, fl, status;

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0) e(0);

	/*
	 * Set up the initial condition on the sockets.
	 */
	rcvlen = get_rcvbuf_len(fd[1]);
	if (rcvlen <= iroom) e(0);
	rcvlen -= iroom;

	if ((sndbuf = malloc(rcvlen)) == NULL) e(0);

	memset(sndbuf, 'X', rcvlen);
	if (send(fd[0], sndbuf, rcvlen, 0) != rcvlen) e(0);

	free(sndbuf);

	switch (istate) {
	case 0: break;
	case 1: if (shutdown(fd[0], SHUT_WR) != 0) e(0); break;
	case 2: if (shutdown(fd[1], SHUT_RD) != 0) e(0); break;
	case 3: if (close(fd[1]) != 0) e(0); break;
	}

	if (setsockopt(fd[0], SOL_SOCKET, SO_SNDLOWAT, &slowat,
	    sizeof(slowat)) != 0) e(0);

	/* SO_SNDLOWAT is always bounded by the actual send length. */
	min = MIN(len, slowat);

	flags = MSG_NOSIGNAL;
	if (bits & 1) flags |= MSG_DONTWAIT;

	/*
	 * Do a quick select test to see if its result indeed matches whether
	 * the available space in the "send" buffer meets the threshold.
	 */
	FD_ZERO(&fds);
	FD_SET(fd[0], &fds);
	tv.tv_sec = 0;
	tv.tv_usec = 0;
	res = select(fd[0] + 1, NULL, &fds, NULL, &tv);
	if (res < 0 || res > 1) e(0);
	if (res != (iroom >= slowat || istate > 0)) e(0);
	if (res == 1 && !FD_ISSET(fd[0], &fds)) e(0);

	/*
	 * Cut short a whole lot of cases, to avoid the overhead of forking,
	 * namely when we know the call should return immediately.  This is the
	 * case when the socket state disallows further sending, or when all
	 * data could be sent, or when the call was non-blocking.  The low
	 * send watermark only helps determine whether anything was sent here.
	 */
	if (istate > 0 || iroom >= len || (flags & MSG_DONTWAIT)) {
		res = send(fd[0], data, len, flags);

		if (istate > 0) {
			if (res != -1) e(0);
			if (errno != EPIPE) e(0);
		} else if (iroom >= len) {
			if (res != len) e(0);
		} else if (iroom >= min) {
			if (res != iroom) e(0);
		} else {
			if (res != -1) e(0);
			if (errno != EWOULDBLOCK) e(0);
		}

		/* Early cleanup and return to avoid even more code clutter. */
		if (istate != 3 && close(fd[1]) != 0) e(0);
		if (close(fd[0]) != 0) e(0);

		return;
	}

	/*
	 * Now starts the interesting stuff: the send call should now block,
	 * even though if we add MSG_DONTWAIT it may not return EWOULDBLOCK,
	 * because MSG_DONTWAIT prevents the send from blocking after partial
	 * completion.  As such, we can only test our expectations by letting
	 * the call block, in a child process, and waiting.  We do test as much
	 * of the above assumption as we can for safety right here, but this is
	 * not a substitute for actually blocking even in these cases!
	 */
	if (iroom < min) {
		if (send(fd[0], data, len, flags | MSG_DONTWAIT) != -1) e(0);
		if (errno != EWOULDBLOCK) e(0);
	}

	/*
	 * If (act < 9), we receive 0, 1, or 2 bytes from the receive queue
	 * before forcing the send call to terminate in one of three ways.
	 *
	 * If (act == 9), we use a signal to interrupt the send call.
	 */
	if (act < 9) {
		eroom = act % 3;
		tstate = act / 3;
	} else
		eroom = tstate = 0;

	if (pipe2(pfd, O_NONBLOCK) != 0) e(0);

	pid = fork();
	switch (pid) {
	case 0:
		errct = 0;

		if (close(fd[1]) != 0) e(0);
		if (close(pfd[0]) != 0) e(0);

		if (act == 9) {
			memset(&sa, 0, sizeof(sa));
			sa.sa_handler = test90_got_signal;
			if (sigaction(SIGUSR1, &sa, NULL) != 0) e(0);
		}

		res = send(fd[0], data, len, flags);
		err = errno;

		if (write(pfd[1], &res, sizeof(res)) != sizeof(res)) e(0);
		if (write(pfd[1], &err, sizeof(err)) != sizeof(err)) e(0);

		exit(errct);
	case -1:
		e(0);
	}

	if (close(pfd[1]) != 0) e(0);

	/*
	 * Allow the child to enter the blocking send(2), and check the pipe
	 * to see if it is really blocked.
	 */
	if (usleep(USLEEP_TIME) != 0) e(0);

	if (read(pfd[0], &res, sizeof(res)) != -1) e(0);
	if (errno != EAGAIN) e(0);

	if (eroom > 0) {
		if (recv(fd[1], buf, eroom, 0) != eroom) e(0);

		/*
		 * The threshold for the send is now met if the entire request
		 * has been satisfied.
		 */
		if (iroom + eroom >= len) {
			if ((fl = fcntl(pfd[0], F_GETFL)) == -1) e(0);
			if (fcntl(pfd[0], F_SETFL, fl & ~O_NONBLOCK) != 0)
				e(0);

			if (read(pfd[0], &res, sizeof(res)) != sizeof(res))
				e(0);
			if (read(pfd[0], &err, sizeof(err)) != sizeof(err))
				e(0);

			if (res != len) e(0);

			/* Bail out. */
			goto cleanup;
		}
	}

	if (act < 9) {
		/*
		 * Now test various ways to terminate the send call.  Ideally
		 * we would also like to have a case that raises a socket error
		 * here, but with UDS there is currently no way to do that.
		 */
		switch (tstate) {
		case 0: if (shutdown(fd[0], SHUT_WR) != 0) e(0); break;
		case 1: if (shutdown(fd[1], SHUT_RD) != 0) e(0); break;
		case 2: if (close(fd[1]) != 0) e(0); fd[1] = -1; break;
		}
	} else
		if (kill(pid, SIGUSR1) != 0) e(0);

	if ((fl = fcntl(pfd[0], F_GETFL)) == -1) e(0);
	if (fcntl(pfd[0], F_SETFL, fl & ~O_NONBLOCK) != 0) e(0);

	if (read(pfd[0], &res, sizeof(res)) != sizeof(res)) e(0);
	if (read(pfd[0], &err, sizeof(err)) != sizeof(err)) e(0);

	/*
	 * If the send met the threshold before being terminate or interrupted,
	 * we should at least have sent something.  Otherwise, the send was
	 * never admitted and should return EPIPE (if the send was terminated)
	 * or EINTR (if the child was killed).
	 */
	if (iroom + eroom >= min) {
		if (res != MIN(iroom + eroom, len)) e(0);
	} else {
		if (res != -1) e(0);
		if (act < 9) {
			if (err != EPIPE) e(0);
		} else
			if (err != EINTR) e(0);
	}

cleanup:
	if (close(pfd[0]) != 0) e(0);

	if (wait(&status) != pid) e(0);
	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) e(0);

	if (fd[1] != -1 && close(fd[1]) != 0) e(0);
	if (close(fd[0]) != 0) e(0);
}

/*
 * Test for sending on stream sockets.  In particular, test SO_SNDLOWAT and
 * MSG_DONTWAIT.
 */
static void
test90v(void)
{
	int iroom, istate, slowat, len, bits, act;

	subtest = 22;

	/* Insanity. */
	for (iroom = 0; iroom <= MAX_BYTES; iroom++)
		for (istate = 0; istate <= 3; istate++)
			for (slowat = 1; slowat <= MAX_BYTES; slowat++)
				for (len = 1; len <= MAX_BYTES; len++)
					for (bits = 0; bits < 2; bits++)
						for (act = 0; act <= 9; act++)
							sub90v(iroom, istate,
							    slowat, len, bits,
							    act);
}

/*
 * Test that SO_RCVLOWAT is limited to the size of the receive buffer.
 */
static void
sub90w_recv(int fill_delta, int rlowat_delta, int exp_delta)
{
	char *buf;
	int fd[2], rcvlen, fill, rlowat, res;

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0) e(0);

	rcvlen = get_rcvbuf_len(fd[0]);

	if ((buf = malloc(rcvlen + 1)) == NULL) e(0);

	fill = rcvlen + fill_delta;
	rlowat = rcvlen + rlowat_delta;

	memset(buf, 0, fill);

	if (send(fd[1], buf, fill, 0) != fill) e(0);

	if (setsockopt(fd[0], SOL_SOCKET, SO_RCVLOWAT, &rlowat,
	    sizeof(rlowat)) != 0) e(0);

	res = recv(fd[0], buf, rcvlen + 1, MSG_DONTWAIT);
	if (exp_delta < 0) {
		if (res != -1) e(0);
		if (errno != EWOULDBLOCK) e(0);
	} else
		if (res != rcvlen - exp_delta) e(0);

	free(buf);

	if (close(fd[0]) != 0) e(0);
	if (close(fd[1]) != 0) e(0);
}

/*
 * Test that SO_SNDLOWAT is limited to the size of the "send" buffer.
 */
static void
sub90w_send(int fill, int slowat_delta, int exp_delta)
{
	char *buf;
	socklen_t len;
	int fd[2], sndlen, slowat, res;

	if (socketpair(AF_UNIX, SOCK_STREAM, 0, fd) != 0) e(0);

	len = sizeof(sndlen);
	if (getsockopt(fd[0], SOL_SOCKET, SO_SNDBUF, &sndlen, &len) != 0) e(0);
	if (len != sizeof(sndlen)) e(0);

	if ((buf = malloc(sndlen + 1)) == NULL) e(0);

	slowat = sndlen + slowat_delta;

	if (fill > 0) {
		memset(buf, 0, fill);

		if (send(fd[0], buf, fill, 0) != fill) e(0);
	}

	if (setsockopt(fd[0], SOL_SOCKET, SO_SNDLOWAT, &slowat,
	    sizeof(slowat)) != 0) e(0);

	res = send(fd[0], buf, sndlen + 1, MSG_DONTWAIT);
	if (exp_delta < 0) {
		if (res != -1) e(0);
		if (errno != EWOULDBLOCK) e(0);
	} else
		if (res != sndlen - exp_delta) e(0);

	free(buf);

	if (close(fd[0]) != 0) e(0);
	if (close(fd[1]) != 0) e(0);
}

/*
 * Test that on stream sockets, SO_RCVLOWAT and SO_SNDLOWAT are limited to
 * their respective buffer sizes.
 */
static void
test90w(void)
{

	subtest = 23;

	/*
	 * With the receive buffer filled except for one byte, all data should
	 * be retrieved unless the threshold is not met.
	 */
	sub90w_recv(-1, -1, 1);
	sub90w_recv(-1, 0, -1);
	sub90w_recv(-1, 1, -1);

	/*
	 * With the receive buffer filled completely, all data should be
	 * retrieved in all cases.
	 */
	sub90w_recv(0, -1, 0);
	sub90w_recv(0, 0, 0);
	sub90w_recv(0, 1, 0);

	/*
	 * With a "send" buffer that contains one byte, all data should be sent
	 * unless the threshold is not met.
	 */
	sub90w_send(1, -1, 1);
	sub90w_send(1, 0, -1);
	sub90w_send(1, 1, -1);

	/*
	 * With the "send" buffer filled completely, all data should be sent
	 * in all cases.
	 */
	sub90w_send(0, -1, 0);
	sub90w_send(0, 0, 0);
	sub90w_send(0, 1, 0);
}

/*
 * Test shutdown on listening sockets.
 */
static void
sub90x(int type, int how, int connwait)
{
	struct sockaddr_un sun;
	socklen_t len;
	char buf[1];
	int fd, fd2, fd3, val, fl;

	subtest = 24;

	fd = get_bound_socket(type, SOCK_PATH_A, &sun);

	if (listen(fd, 5) != 0) e(0);

	if (!connwait) {
		if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

		if (connect(fd2, (struct sockaddr *)&sun, sizeof(sun)) != 0)
			e(0);
	} else {
		val = 1;
		if (setsockopt(fd, 0, LOCAL_CONNWAIT, &val, sizeof(val)) != 0)
			e(0);

		if ((fd2 = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

		if (connect(fd2, (struct sockaddr *)&sun, sizeof(sun)) != -1)
			e(0);
		if (errno != EINPROGRESS) e(0);
	}

	if (shutdown(fd, how) != 0) e(0);

	len = sizeof(sun);
	if ((fd3 = accept(fd, (struct sockaddr *)&sun, &len)) < 0) e(0);

	if (write(fd2, "A", 1) != 1) e(0);
	if (read(fd3, buf, 1) != 1) e(0);
	if (buf[0] != 'A') e(0);

	if (write(fd3, "B", 1) != 1) e(0);
	if (read(fd2, buf, 1) != 1) e(0);
	if (buf[0] != 'B') e(0);

	len = sizeof(sun);
	if (accept(fd, (struct sockaddr *)&sun, &len) != -1) e(0);
	if (errno != ECONNABORTED) e(0);

	/*
	 * Strangely, both NetBSD and Linux (yes, my two reference platforms)
	 * return EWOULDBLOCK from non-blocking accept(2) calls even though
	 * they always return ECONNABORTED when blocking.  For consistency and
	 * select(2), we always return ECONNABORTED.
	 */
	if ((fl = fcntl(fd, F_GETFL)) == -1) e(0);
	if (fcntl(fd, F_SETFL, fl | O_NONBLOCK) != 0) e(0);

	len = sizeof(sun);
	if (accept(fd, (struct sockaddr *)&sun, &len) != -1) e(0);
	if (errno != ECONNABORTED) e(0);

	if (fcntl(fd, F_SETFL, fl) != 0) e(0);

	if (close(fd3) != 0) e(0);
	if (close(fd2) != 0) e(0);

	if ((fd2 = socket(AF_UNIX, type | SOCK_NONBLOCK, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sun, sizeof(sun)) != -1) e(0);
	if (errno != ECONNREFUSED) e(0);

	len = sizeof(sun);
	if (accept(fd, (struct sockaddr *)&sun, &len) != -1) e(0);
	if (errno != ECONNABORTED) e(0);

	if (close(fd2) != 0) e(0);
	if (close(fd) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
}

/*
 * Test shutdown on listening sockets.  Pending connections should still be
 * acceptable (and not inherit the shutdown flags), but new connections must be
 * refused, and the accept call must no longer ever block.
 */
static void
test90x(void)
{
	const int types[] = { SOCK_STREAM, SOCK_SEQPACKET };
	const int hows[] = { SHUT_RD, SHUT_WR, SHUT_RDWR };
	unsigned int i, j, k;

	for (i = 0; i < __arraycount(types); i++)
		for (j = 0; j < __arraycount(hows); j++)
			for (k = 0; k <= 1; k++)
				sub90x(types[i], hows[j], k);
}

/*
 * Test accepting connections without LOCAL_CONNWAIT for the given socket type.
 */
static void
sub90y(int type)
{
	struct sockaddr_un sunA, sunB, sunC;
	socklen_t len;
	struct timeval tv;
	fd_set fds;
	char buf[7];
	uid_t uid;
	gid_t gid;
	int fd, fd2, fd3, fd4, val;

	fd = get_bound_socket(type | SOCK_NONBLOCK, SOCK_PATH_A, &sunA);

	len = sizeof(val);
	if (getsockopt(fd, 0, LOCAL_CONNWAIT, &val, &len) != 0) e(0);
	if (len != sizeof(val)) e(0);
	if (val != 0) e(0);

	if (listen(fd, 5) != 0) e(0);

	/*
	 * Any socket options should be inherited from the listening socket at
	 * connect time, and not be re-inherited at accept time.  It does not
	 * really matter what socket option we set here, as long as it is
	 * supposed to be inherited.
	 */
	val = 123;
	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &val, sizeof(val)) != 0)
		e(0);

	fd2 = get_bound_socket(type, SOCK_PATH_B, &sunB);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	val = 456;
	if (setsockopt(fd, SOL_SOCKET, SO_SNDLOWAT, &val, sizeof(val)) != 0)
		e(0);

	/*
	 * Obtaining the peer name should work.  As always, the name should be
	 * inherited from the listening socket.
	 */
	len = sizeof(sunC);
	if (getpeername(fd2, (struct sockaddr *)&sunC, &len) != 0) e(0);
	check_addr(&sunC, len, SOCK_PATH_A);

	/*
	 * Obtaining peer credentials should work.  This is why NetBSD obtains
	 * the peer credentials at bind time, not at accept time.
	 */
	if (getpeereid(fd2, &uid, &gid) != 0) e(0);
	if (uid != geteuid()) e(0);
	if (gid != getegid()) e(0);

	/*
	 * Sending to the socket should work, and it should be possible to
	 * receive the data from the other side once accepted.
	 */
	if (send(fd2, "Hello, ", 7, 0) != 7) e(0);
	if (send(fd2, "world!", 6, 0) != 6) e(0);

	/* Shutdown settings should be visible after accepting, too. */
	if (shutdown(fd2, SHUT_RDWR) != 0) e(0);

	len = sizeof(sunB);
	if ((fd3 = accept(fd, (struct sockaddr *)&sunB, &len)) < 0) e(0);
	check_addr(&sunB, len, SOCK_PATH_B);

	len = sizeof(val);
	if (getsockopt(fd3, SOL_SOCKET, SO_SNDLOWAT, &val, &len) != 0) e(0);
	if (len != sizeof(val)) e(0);
	if (val != 123) e(0);

	if (recv(fd3, buf, 7, 0) != 7) e(0);
	if (memcmp(buf, "Hello, ", 7) != 0) e(0);
	if (recv(fd3, buf, 7, 0) != 6) e(0);
	if (memcmp(buf, "world!", 6) != 0) e(0);

	if (recv(fd3, buf, sizeof(buf), 0) != 0) e(0);

	if (send(fd3, "X", 1, MSG_NOSIGNAL) != -1) e(0);
	if (errno != EPIPE) e(0);

	if (close(fd2) != 0) e(0);
	if (close(fd3) != 0) e(0);

	if (unlink(SOCK_PATH_B) != 0) e(0);

	/*
	 * If the socket pending acceptance is closed, the listening socket
	 * should pretend as though the connection was never there.
	 */
	if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	FD_ZERO(&fds);
	FD_SET(fd, &fds);
	tv.tv_sec = 0;
	tv.tv_usec = 0;
	if (select(fd + 1, &fds, NULL, NULL, &tv) != 1) e(0);
	if (!FD_ISSET(fd, &fds)) e(0);

	if (close(fd2) != 0) e(0);

	if (select(fd + 1, &fds, NULL, NULL, &tv) != 0) e(0);
	if (FD_ISSET(fd, &fds)) e(0);

	len = sizeof(sunB);
	if (accept(fd, (struct sockaddr *)&sunB, &len) != -1) e(0);
	if (errno != EWOULDBLOCK) e(0);

	/*
	 * Try the same thing, but now with the connection sandwiched between
	 * two different pending connections, which should be left intact.
	 */
	if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (send(fd2, "A", 1, 0) != 1) e(0);

	if ((fd3 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd3, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (send(fd3, "B", 1, 0) != 1) e(0);

	if ((fd4 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd4, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (send(fd4, "C", 1, 0) != 1) e(0);

	if (close(fd3) != 0) e(0);

	len = sizeof(sunB);
	if ((fd3 = accept(fd, (struct sockaddr *)&sunB, &len)) < 0) e(0);

	if (recv(fd3, buf, sizeof(buf), 0) != 1) e(0);
	if (buf[0] != 'A') e(0);

	if (close(fd3) != 0) e(0);
	if (close(fd2) != 0) e(0);

	FD_ZERO(&fds);
	FD_SET(fd, &fds);
	tv.tv_sec = 0;
	tv.tv_usec = 0;
	if (select(fd + 1, &fds, NULL, NULL, &tv) != 1) e(0);
	if (!FD_ISSET(fd, &fds)) e(0);

	len = sizeof(sunB);
	if ((fd3 = accept(fd, (struct sockaddr *)&sunB, &len)) < 0) e(0);

	if (recv(fd3, buf, sizeof(buf), 0) != 1) e(0);
	if (buf[0] != 'C') e(0);

	if (close(fd3) != 0) e(0);
	if (close(fd4) != 0) e(0);

	if (select(fd + 1, &fds, NULL, NULL, &tv) != 0) e(0);
	if (FD_ISSET(fd, &fds)) e(0);

	len = sizeof(sunB);
	if (accept(fd, (struct sockaddr *)&sunB, &len) != -1) e(0);
	if (errno != EWOULDBLOCK) e(0);

	/*
	 * If the listening socket is closed, the socket pending acceptance
	 * should be reset.  We actually rely on this behavior in the sweep
	 * test, but we test this with more than one socket this time.
	 */
	if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if ((fd3 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd3, (struct sockaddr *)&sunA, sizeof(sunA)) != 0) e(0);

	if (close(fd) != 0) e(0);

	if (recv(fd2, buf, sizeof(buf), 0) != -1) e(0);
	if (errno != ECONNRESET) e(0);

	if (recv(fd2, buf, sizeof(buf), 0) != 0) e(0);

	if (recv(fd3, buf, sizeof(buf), 0) != -1) e(0);
	if (errno != ECONNRESET) e(0);

	if (recv(fd3, buf, sizeof(buf), 0) != 0) e(0);

	if (close(fd3) != 0) e(0);

	if (close(fd2) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
}

/*
 * Test accepting connections without LOCAL_CONNWAIT.  Since both the old UDS
 * service and the initial version of the new UDS service supported only the
 * LOCAL_CONNWAIT behavior, the alternative (which is now the default, as it is
 * on other platforms) has been a bit under-tested so far.
 */
static void
test90y(void)
{

	subtest = 25;

	sub90y(SOCK_STREAM);

	sub90y(SOCK_SEQPACKET);
}

/*
 * Test that SO_LINGER has no effect on sockets of the given type.
 */
static void
sub90z(int type)
{
	struct sockaddr_un sun;
	socklen_t len;
	struct linger l;
	char buf[1];
	int fd, fd2, fd3;

	fd = get_bound_socket(type, SOCK_PATH_A, &sun);

	if (listen(fd, 1) != 0) e(0);

	if ((fd2 = socket(AF_UNIX, type, 0)) < 0) e(0);

	if (connect(fd2, (struct sockaddr *)&sun, sizeof(sun)) != 0) e(0);

	len = sizeof(sun);
	if ((fd3 = accept(fd, (struct sockaddr *)&sun, &len)) < 0) e(0);

	if (close(fd) != 0) e(0);

	if (send(fd2, "A", 1, 0) != 1) e(0);

	l.l_onoff = 1;
	l.l_linger = 0;
	if (setsockopt(fd2, SOL_SOCKET, SO_LINGER, &l, sizeof(l)) != 0) e(0);

	if (close(fd2) != 0) e(0);

	if (recv(fd3, buf, sizeof(buf), 0) != 1) e(0);
	if (buf[0] != 'A') e(0);

	/* We should not get ECONNRESET now. */
	if (recv(fd3, buf, sizeof(buf), 0) != 0) e(0);

	if (close(fd3) != 0) e(0);

	if (unlink(SOCK_PATH_A) != 0) e(0);
}

/*
 * Test that SO_LINGER has no effect on UNIX domain sockets.  In particular, a
 * timeout of zero does not cause the connection to be reset forcefully.
 */
static void
test90z(void)
{

	subtest = 26;

	sub90z(SOCK_STREAM);

	sub90z(SOCK_SEQPACKET);
}

/*
 * Test program for UDS.
 */
int
main(int argc, char ** argv)
{
	int i, m;

	start(90);

	if (argc == 2)
		m = atoi(argv[1]);
	else
		m = 0xFFFFFFF;

	for (i = 0; i < ITERATIONS; i++) {
		if (m & 0x0000001) test90a();
		if (m & 0x0000002) test90b();
		if (m & 0x0000004) test90c();
		if (m & 0x0000008) test90d();
		if (m & 0x0000010) test90e();
		if (m & 0x0000020) test90f();
		if (m & 0x0000040) test90g();
		if (m & 0x0000080) test90h();
		if (m & 0x0000100) test90i();
		if (m & 0x0000200) test90j();
		if (m & 0x0000400) test90k();
		if (m & 0x0000800) test90l();
		if (m & 0x0001000) test90m();
		if (m & 0x0002000) test90n();
		if (m & 0x0004000) test90o();
		if (m & 0x0008000) test90p();
		if (m & 0x0010000) test90q();
		if (m & 0x0020000) test90r();
		if (m & 0x0040000) test90s();
		if (m & 0x0080000) test90t();
		if (m & 0x0100000) test90u();
		if (m & 0x0200000) test90v();
		if (m & 0x0400000) test90w();
		if (m & 0x0800000) test90x();
		if (m & 0x1000000) test90y();
		if (m & 0x2000000) test90z();
	}

	quit();
	/* NOTREACHED */
}