summaryrefslogtreecommitdiff
path: root/minix/tests/test88.c
blob: 865682769569c2517ecbbbc024702538ec9502ec (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
/* Tests for System V IPC semaphores - by D.C. van Moolenbroek */
/* This test must be run as root, as it includes permission checking tests. */
#include <stdlib.h>
#include <limits.h>
#include <pwd.h>
#include <grp.h>
#include <sys/ipc.h>
#include <sys/sem.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <sys/sysctl.h>
#include <signal.h>

#include "common.h"

#define ITERATIONS	3

#define WAIT_USECS	100000		/* time for processes to get ready */

#define KEY_A		0x73570001
#define KEY_B		(KEY_A + 1)
#define KEY_C		(KEY_A + 2)

#define ROOT_USER	"root"		/* name of root */
#define ROOT_GROUP	"wheel"		/* name of root's group */
#define NONROOT_USER	"bin"		/* name of any unprivileged user */
#define NONROOT_GROUP	"bin"		/* name of any unprivileged group */

enum {
	DROP_NONE,
	DROP_USER,
	DROP_ALL,
};

enum {
	SUGID_NONE,
	SUGID_ROOT_USER,
	SUGID_NONROOT_USER,
	SUGID_ROOT_GROUP,
	SUGID_NONROOT_GROUP,
};

struct link {
	pid_t pid;
	int sndfd;
	int rcvfd;
};

/*
 * Test semaphore properties.  This is a macro, so that it prints useful line
 * information if an error occurs.
 */
#define TEST_SEM(id, num, val, pid, ncnt, zcnt) do { \
	if (semctl(id, num, GETVAL) != val) e(0); \
	if (pid != -1 && semctl(id, num, GETPID) != pid) e(1); \
	if (ncnt != -1 && semctl(id, num, GETNCNT) != ncnt) e(2); \
	if (zcnt != -1 && semctl(id, num, GETZCNT) != zcnt) e(3); \
} while (0);

static int nr_signals = 0;

static size_t page_size;
static char *page_ptr;
static void *bad_ptr;

/*
 * Spawn a child process, with a pair of pipes to talk to it bidirectionally.
 * Drop user and group privileges in the child process if requested.
 */
static void
spawn(struct link * link, void (* proc)(struct link *), int drop)
{
	struct passwd *pw;
	struct group *gr;
	int up[2], dn[2];

	fflush(stdout);
	fflush(stderr);

	if (pipe(up) != 0) e(0);
	if (pipe(dn) != 0) e(0);

	link->pid = fork();

	switch (link->pid) {
	case 0:
		close(up[1]);
		close(dn[0]);

		link->pid = getppid();
		link->rcvfd = up[0];
		link->sndfd = dn[1];

		errct = 0;

		switch (drop) {
		case DROP_ALL:
			if (setgroups(0, NULL) != 0) e(0);

			if ((gr = getgrnam(NONROOT_GROUP)) == NULL) e(0);

			if (setgid(gr->gr_gid) != 0) e(0);
			if (setegid(gr->gr_gid) != 0) e(0);

			/* FALLTHROUGH */
		case DROP_USER:
			if ((pw = getpwnam(NONROOT_USER)) == NULL) e(0);

			if (setuid(pw->pw_uid) != 0) e(0);
		}

		proc(link);

		/* Close our pipe FDs on exit, so that we can make zombies. */
		exit(errct);
	case -1:
		e(0);
		break;
	}

	close(up[0]);
	close(dn[1]);

	link->sndfd = up[1];
	link->rcvfd = dn[0];
}

/*
 * Wait for a child process to terminate, and clean up.
 */
static void
collect(struct link * link)
{
	int status;

	close(link->sndfd);
	close(link->rcvfd);

	if (waitpid(link->pid, &status, 0) != link->pid) e(0);

	if (!WIFEXITED(status)) e(0);
	else errct += WEXITSTATUS(status);
}

/*
 * Forcibly terminate a child process, and clean up.
 */
static void
terminate(struct link * link)
{
	int status;

	if (kill(link->pid, SIGKILL) != 0) e(0);

	close(link->sndfd);
	close(link->rcvfd);

	if (waitpid(link->pid, &status, 0) <= 0) e(0);

	if (WIFSIGNALED(status)) {
		if (WTERMSIG(status) != SIGKILL) e(0);
	} else {
		if (!WIFEXITED(status)) e(0);
		else errct += WEXITSTATUS(status);
	}
}

/*
 * Send an integer value to the child or parent.
 */
static void
snd(struct link * link, int val)
{

	if (write(link->sndfd, (void *)&val, sizeof(val)) != sizeof(val)) e(0);
}

/*
 * Receive an integer value from the child or parent, or -1 on EOF.
 */
static int
rcv(struct link * link)
{
	int r, val;

	if ((r = read(link->rcvfd, (void *)&val, sizeof(val))) == 0)
		return -1;

	if (r != sizeof(val)) e(0);

	return val;
}

/*
 * Child procedure that creates semaphore sets.
 */
static void
test_perm_child(struct link * parent)
{
	struct passwd *pw;
	struct group *gr;
	struct semid_ds semds;
	uid_t uid;
	gid_t gid;
	int mask, rmask, sugid, id[3];

	/*
	 * Repeatedly create a number of semaphores with the masks provided by
	 * the parent process.
	 */
	while ((mask = rcv(parent)) != -1) {
		rmask = rcv(parent);
		sugid = rcv(parent);

		/*
		 * Create the semaphores.  For KEY_A, if we are going to set
		 * the mode through IPC_SET anyway, start with a zero mask to
		 * check that the replaced mode is used (thus testing IPC_SET).
		 */
		if ((id[0] = semget(KEY_A, 3,
		    IPC_CREAT | IPC_EXCL |
		    ((sugid == SUGID_NONE) ? mask : 0))) == -1) e(0);
		if ((id[1] = semget(KEY_B, 3,
		    IPC_CREAT | IPC_EXCL | mask | rmask)) == -1) e(0);
		if ((id[2] = semget(KEY_C, 3,
		    IPC_CREAT | IPC_EXCL | rmask)) == -1) e(0);

		uid = geteuid();
		gid = getegid();
		if (sugid != SUGID_NONE) {
			switch (sugid) {
			case SUGID_ROOT_USER:
				if ((pw = getpwnam(ROOT_USER)) == NULL) e(0);
				uid = pw->pw_uid;
				break;
			case SUGID_NONROOT_USER:
				if ((pw = getpwnam(NONROOT_USER)) == NULL)
					e(0);
				uid = pw->pw_uid;
				break;
			case SUGID_ROOT_GROUP:
				if ((gr = getgrnam(ROOT_GROUP)) == NULL) e(0);
				gid = gr->gr_gid;
				break;
			case SUGID_NONROOT_GROUP:
				if ((gr = getgrnam(NONROOT_GROUP)) == NULL)
					e(0);
				gid = gr->gr_gid;
				break;
			}

			semds.sem_perm.uid = uid;
			semds.sem_perm.gid = gid;
			semds.sem_perm.mode = mask;
			if (semctl(id[0], 0, IPC_SET, &semds) != 0) e(0);
			semds.sem_perm.mode = mask | rmask;
			if (semctl(id[1], 0, IPC_SET, &semds) != 0) e(0);
			semds.sem_perm.mode = rmask;
			if (semctl(id[2], 0, IPC_SET, &semds) != 0) e(0);
		}

		/* Do a quick test to confirm the right privileges. */
		if (mask & IPC_R) {
			if (semctl(id[0], 0, IPC_STAT, &semds) != 0) e(0);
			if (semds.sem_perm.mode != (SEM_ALLOC | mask)) e(0);
			if (semds.sem_perm.uid != uid) e(0);
			if (semds.sem_perm.gid != gid) e(0);
			if (semds.sem_perm.cuid != geteuid()) e(0);
			if (semds.sem_perm.cgid != getegid()) e(0);
		}

		snd(parent, id[0]);
		snd(parent, id[1]);
		snd(parent, id[2]);

		/* The other child process runs here. */

		if (rcv(parent) != 0) e(0);

		/*
		 * For owner tests, the other child may already have removed
		 * the semaphore sets, so ignore return values here.
		 */
		(void)semctl(id[0], 0, IPC_RMID);
		(void)semctl(id[1], 0, IPC_RMID);
		(void)semctl(id[2], 0, IPC_RMID);
	}
}

/*
 * Perform a permission test.  The given procedure will be called for various
 * access masks, which it can use to determine whether operations on three
 * created semaphore sets should succeed or fail.  The first two semaphore sets
 * are created with appropriate privileges, the third one is not.  If the
 * 'owner_test' variable is set, the test will change slightly so as to allow
 * testing of operations that require a matching uid/cuid.
 */
static void
test_perm(void (* proc)(struct link *), int owner_test)
{
	struct link child1, child2;
	int n, shift, bit, mask, rmask, drop1, drop2, sugid, id[3];

	for (n = 0; n < 7; n++) {
		/*
		 * Child 1 creates the semaphores, and child 2 opens them.
		 * For shift 6 (0700), child 1 drops its privileges to match
		 * child 2's (n=0).  For shift 3 (0070), child 2 drops its user
		 * privileges (n=3).  For shift 0 (0007), child 2 drops its
		 * group in addition to its user privileges (n=6).  Also try
		 * with differing uid/cuid (n=1,2) and gid/cgid (n=4,5), where
		 * the current ownership (n=1,4) or the creator's ownership
		 * (n=2,5) is tested.
		 */
		switch (n) {
		case 0:
			shift = 6;
			drop1 = DROP_ALL;
			drop2 = DROP_ALL;
			sugid = SUGID_NONE;
			break;
		case 1:
			shift = 6;
			drop1 = DROP_NONE;
			drop2 = DROP_ALL;
			sugid = SUGID_NONROOT_USER;
			break;
		case 2:
			shift = 6;
			drop1 = DROP_USER;
			drop2 = DROP_ALL;
			sugid = SUGID_ROOT_USER;
			break;
		case 3:
			shift = 3;
			drop1 = DROP_NONE;
			drop2 = DROP_USER;
			sugid = SUGID_NONE;
			break;
		case 4:
			shift = 3;
			drop1 = DROP_NONE;
			drop2 = DROP_ALL;
			sugid = SUGID_NONROOT_GROUP;
			break;
		case 5:
			/* The root group has no special privileges. */
			shift = 3;
			drop1 = DROP_NONE;
			drop2 = DROP_USER;
			sugid = SUGID_NONROOT_GROUP;
			break;
		case 6:
			shift = 0;
			drop1 = DROP_NONE;
			drop2 = DROP_ALL;
			sugid = SUGID_NONE;
			break;
		}

		spawn(&child1, test_perm_child, drop1);
		spawn(&child2, proc, drop2);

		for (bit = 0; bit <= 7; bit++) {
			mask = bit << shift;
			rmask = 0777 & ~(7 << shift);

			snd(&child1, mask);
			snd(&child1, rmask);
			snd(&child1, sugid);
			id[0] = rcv(&child1);
			id[1] = rcv(&child1);
			id[2] = rcv(&child1);

			snd(&child2, (owner_test) ? shift : bit);
			snd(&child2, id[0]);
			snd(&child2, id[1]);
			snd(&child2, id[2]);
			if (rcv(&child2) != 0) e(0);

			snd(&child1, 0);
		}

		/* We use a bitmask of -1 to terminate the children. */
		snd(&child1, -1);
		snd(&child2, -1);

		collect(&child1);
		collect(&child2);
	}
}

/*
 * Test semget(2) permission checks.  Please note that the checks are advisory:
 * nothing keeps a process from opening a semaphore set with fewer privileges
 * than required by the operations the process subsequently issues on the set.
 */
static void
test88a_perm(struct link * parent)
{
	int r, tbit, bit, mask, id[3];

	while ((tbit = rcv(parent)) != -1) {
		id[0] = rcv(parent);
		id[1] = rcv(parent);
		id[2] = rcv(parent);

		/*
		 * We skip setting lower bits, as it is not clear what effect
		 * that should have.  We assume that zero bits should result in
		 * failure.
		 */
		for (bit = 0; bit <= 7; bit++) {
			mask = bit << 6;

			/*
			 * Opening semaphore set A must succeed iff the given
			 * bits are all set in the relevant three-bit section
			 * of the creation mask.
			 */
			r = semget(KEY_A, 0, mask);
			if (r < 0 && (r != -1 || errno != EACCES)) e(0);
			if ((bit != 0 && (bit & tbit) == bit) != (r != -1))
				e(0);
			if (r != -1 && r != id[0]) e(0);

			/*
			 * Same for semaphore set B, which was created with all
			 * irrelevant mode bits inverted.
			 */
			r = semget(KEY_B, 0, mask);
			if (r < 0 && (r != -1 || errno != EACCES)) e(0);
			if ((bit != 0 && (bit & tbit) == bit) != (r != -1))
				e(0);
			if (r != -1 && r != id[1]) e(0);

			/*
			 * Semaphore set C was created with only irrelevant
			 * mode bits set, so opening it must always fail.
			 */
			if (semget(KEY_C, 0, mask) != -1) e(0);
			if (errno != EACCES) e(0);
		}

		snd(parent, 0);
	}
}

/*
 * Test the basic semget(2) functionality.
 */
static void
test88a(void)
{
	struct seminfo seminfo;
	struct semid_ds semds;
	time_t now;
	unsigned int i, j;
	int id[3], *idp;

	subtest = 0;

	/*
	 * The key IPC_PRIVATE must always yield a new semaphore set identifier
	 * regardless of whether IPC_CREAT and IPC_EXCL are supplied.
	 */
	if ((id[0] = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600)) < 0) e(0);

	if ((id[1] = semget(IPC_PRIVATE, 1, IPC_CREAT | IPC_EXCL | 0600)) < 0)
		e(0);

	if ((id[2] = semget(IPC_PRIVATE, 1, 0600)) < 0) e(0);

	if (id[0] == id[1]) e(0);
	if (id[1] == id[2]) e(0);
	if (id[0] == id[2]) e(0);

	if (semctl(id[0], 0, IPC_RMID) != 0) e(0);
	if (semctl(id[1], 0, IPC_RMID) != 0) e(0);
	if (semctl(id[2], 0, IPC_RMID) != 0) e(0);

	/* Remove any leftovers from previous test runs. */
	if ((id[0] = semget(KEY_A, 0, 0600)) >= 0 &&
	    semctl(id[0], 0, IPC_RMID) == -1) e(0);
	if ((id[0] = semget(KEY_B, 0, 0600)) >= 0 &&
	    semctl(id[0], 0, IPC_RMID) == -1) e(0);

	/*
	 * For non-IPC_PRIVATE keys, open(2)-like semantics apply with respect
	 * to IPC_CREAT and IPC_EXCL flags.  The behavior of supplying IPC_EXCL
	 * without IPC_CREAT is undefined, so we do not test for that here.
	 */
	if (semget(KEY_A, 1, 0600) != -1) e(0);
	if (errno != ENOENT);

	if ((id[0] = semget(KEY_A, 1, IPC_CREAT | IPC_EXCL | 0600)) < 0) e(0);

	if (semget(KEY_B, 1, 0600) != -1) e(0);
	if (errno != ENOENT);

	if ((id[1] = semget(KEY_B, 1, IPC_CREAT | 0600)) < 0) e(0);

	if (id[0] == id[1]) e(0);

	if ((id[2] = semget(KEY_A, 1, 0600)) < 0) e(0);
	if (id[2] != id[0]) e(0);

	if ((id[2] = semget(KEY_B, 1, IPC_CREAT | 0600)) < 0) e(0);
	if (id[2] != id[2]) e(0);

	if (semget(KEY_A, 1, IPC_CREAT | IPC_EXCL | 0600) != -1) e(0);
	if (errno != EEXIST) e(0);

	if (semctl(id[0], 0, IPC_RMID) != 0) e(0);
	if (semctl(id[1], 0, IPC_RMID) != 0) e(0);

	/*
	 * Check that we get the right error when we run out of semaphore sets.
	 * It is possible that other processes in the system are using sets
	 * right now, so see if we can anywhere from three (the number we had
	 * already) to SEMMNI semaphore sets, and check for ENOSPC after that.
	 */
	if (semctl(0, 0, IPC_INFO, &seminfo) == -1) e(0);
	if (seminfo.semmni < 3 || seminfo.semmni > USHRT_MAX) e(0);

	if ((idp = malloc(sizeof(int) * (seminfo.semmni + 1))) == NULL) e(0);

	for (i = 0; i < seminfo.semmni + 1; i++) {
		if ((idp[i] = semget(KEY_A + i, 1, IPC_CREAT | 0600)) < 0)
			break;

		/* Ensure that there are no ID collisions.  O(n**2). */
		for (j = 0; j < i; j++)
			if (idp[i] == idp[j]) e(0);
	}

	if (errno != ENOSPC) e(0);
	if (i < 3) e(0);
	if (i == seminfo.semmni + 1) e(0);

	while (i-- > 0)
		if (semctl(idp[i], 0, IPC_RMID) != 0) e(0);

	free(idp);

	/*
	 * The given number of semaphores must be within bounds.
	 */
	if (semget(KEY_A, -1, IPC_CREAT | 0600) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semget(KEY_A, 0, IPC_CREAT | 0600) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (seminfo.semmsl < 3 || seminfo.semmsl > USHRT_MAX) e(0);
	if (semget(KEY_A, seminfo.semmsl + 1, IPC_CREAT | 0600) != -1) e(0);
	if (errno != EINVAL) e(0);

	if ((id[0] = semget(KEY_A, seminfo.semmsl, IPC_CREAT | 0600)) < 0)
		e(0);
	if (semctl(id[0], 0, IPC_RMID) != 0) e(0);

	if ((id[0] = semget(KEY_A, 2, IPC_CREAT | 0600)) < 0) e(0);

	if ((id[1] = semget(KEY_A, 0, 0600)) < 0) e(0);
	if (id[0] != id[1]) e(0);

	if ((id[1] = semget(KEY_A, 1, 0600)) < 0) e(0);
	if (id[0] != id[1]) e(0);

	if ((id[1] = semget(KEY_A, 2, 0600)) < 0) e(0);
	if (id[0] != id[1]) e(0);

	if ((id[1] = semget(KEY_A, 3, 0600)) != -1) e(0);
	if (errno != EINVAL) e(0);

	if ((id[1] = semget(KEY_A, seminfo.semmsl + 1, 0600)) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id[0], 0, IPC_RMID) != 0) e(0);

	/*
	 * Verify that the initial values for the semaphore set are as
	 * expected.
	 */
	time(&now);
	if (seminfo.semmns < 3 + seminfo.semmsl) e(0);
	if ((id[0] = semget(IPC_PRIVATE, 3, IPC_CREAT | IPC_EXCL | 0642)) < 0)
		e(0);
	if ((id[1] = semget(KEY_A, seminfo.semmsl, IPC_CREAT | 0613)) < 0)
		e(0);

	if (semctl(id[0], 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_perm.uid != geteuid()) e(0);
	if (semds.sem_perm.gid != getegid()) e(0);
	if (semds.sem_perm.cuid != geteuid()) e(0);
	if (semds.sem_perm.cgid != getegid()) e(0);
	if (semds.sem_perm.mode != (SEM_ALLOC | 0642)) e(0);
	if (semds.sem_perm._key != IPC_PRIVATE) e(0);
	if (semds.sem_nsems != 3) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime < now || semds.sem_ctime >= now + 10) e(0);

	for (i = 0; i < semds.sem_nsems; i++)
		TEST_SEM(id[0], i, 0, 0, 0, 0);

	if (semctl(id[1], 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_perm.uid != geteuid()) e(0);
	if (semds.sem_perm.gid != getegid()) e(0);
	if (semds.sem_perm.cuid != geteuid()) e(0);
	if (semds.sem_perm.cgid != getegid()) e(0);
	if (semds.sem_perm.mode != (SEM_ALLOC | 0613)) e(0);
	if (semds.sem_perm._key != KEY_A) e(0);
	if (semds.sem_nsems != seminfo.semmsl) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime < now || semds.sem_ctime >= now + 10) e(0);

	for (i = 0; i < semds.sem_nsems; i++)
		TEST_SEM(id[1], i, 0, 0, 0, 0);

	if (semctl(id[1], 0, IPC_RMID) != 0) e(0);
	if (semctl(id[0], 0, IPC_RMID) != 0) e(0);

	/*
	 * Finally, perform a number of permission-related checks.  Since the
	 * main test program is running with superuser privileges, most of the
	 * permission tests use an unprivileged child process.
	 */
	/* The superuser can always open and destroy a semaphore set. */
	if ((id[0] = semget(KEY_A, 1, IPC_CREAT | IPC_EXCL | 0000)) < 0) e(0);

	if ((id[1] = semget(KEY_A, 0, 0600)) < 0) e(0);
	if (id[0] != id[1]) e(0);

	if ((id[1] = semget(KEY_A, 0, 0000)) < 0) e(0);
	if (id[0] != id[1]) e(0);

	if (semctl(id[0], 0, IPC_RMID) != 0) e(0);

	/*
	 * When an unprivileged process tries to open a semaphore set, the
	 * given upper three permission bits from the mode (0700) are tested
	 * against the appropriate permission bits from the semaphore set.
	 */
	test_perm(test88a_perm, 0 /*owner_test*/);
}

/*
 * Test semop(2) permission checks.
 */
static void
test88b_perm(struct link * parent)
{
	struct sembuf sops[2];
	size_t nsops;
	int i, r, tbit, bit, id[3];

	while ((tbit = rcv(parent)) != -1) {
		id[0] = rcv(parent);
		id[1] = rcv(parent);
		id[2] = rcv(parent);

		/*
		 * This loop is designed such that failure of any bit-based
		 * subset will not result in subsequent operations blocking.
		 */
		for (i = 0; i < 8; i++) {
			memset(sops, 0, sizeof(sops));

			switch (i) {
			case 0:
				nsops = 1;
				bit = 4;
				break;
			case 1:
				sops[0].sem_op = 1;
				nsops = 1;
				bit = 2;
				break;
			case 2:
				sops[0].sem_op = -1;
				nsops = 1;
				bit = 2;
				break;
			case 3:
				sops[1].sem_op = 1;
				nsops = 2;
				bit = 6;
				break;
			case 4:
				sops[0].sem_num = 1;
				sops[1].sem_op = -1;
				nsops = 2;
				bit = 6;
				break;
			case 5:
				sops[1].sem_num = 1;
				nsops = 2;
				bit = 4;
				break;
			case 6:
				/*
				 * Two operations on the same semaphore.  As
				 * such, this verifies that operations are
				 * processed in array order.
				 */
				sops[0].sem_op = 1;
				sops[1].sem_op = -1;
				nsops = 2;
				bit = 2;
				break;
			case 7:
				/*
				 * Test the order of checks.  Since IPC_STAT
				 * requires read permission, it is reasonable
				 * that the check against sem_nsems be done
				 * only after the permission check as well.
				 * For this test we rewrite EFBIG to OK below.
				 */
				sops[0].sem_num = USHRT_MAX;
				nsops = 2;
				bit = 4;
				break;
			}

			r = semop(id[0], sops, nsops);
			if (i == 7 && r == -1 && errno == EFBIG) r = 0;
			if (r < 0 && (r != -1 || errno != EACCES)) e(0);
			if (((bit & tbit) == bit) != (r != -1)) e(0);

			r = semop(id[1], sops, nsops);
			if (i == 7 && r == -1 && errno == EFBIG) r = 0;
			if (r < 0 && (r != -1 || errno != EACCES)) e(0);
			if (((bit & tbit) == bit) != (r != -1)) e(0);

			if (semop(id[2], sops, nsops) != -1) e(0);
			if (errno != EACCES) e(0);
		}

		snd(parent, 0);
	}
}

/*
 * Signal handler.
 */
static void
got_signal(int sig)
{

	if (sig != SIGHUP) e(0);
	if (nr_signals != 0) e(0);
	nr_signals++;
}

/*
 * Child process for semop(2) tests, mainly testing blocking operations.
 */
static void
test88b_child(struct link * parent)
{
	struct sembuf sops[5];
	struct sigaction act;
	int id;

	id = rcv(parent);

	memset(sops, 0, sizeof(sops));
	if (semop(id, sops, 1) != 0) e(0);

	if (rcv(parent) != 1) e(0);

	sops[0].sem_op = -3;
	if (semop(id, sops, 1) != 0) e(0);

	if (rcv(parent) != 2) e(0);

	sops[0].sem_num = 2;
	sops[0].sem_op = 2;
	sops[1].sem_num = 1;
	sops[1].sem_op = -1;
	sops[2].sem_num = 0;
	sops[2].sem_op = 1;
	if (semop(id, sops, 3) != 0) e(0);

	if (rcv(parent) != 3) e(0);

	sops[0].sem_num = 1;
	sops[0].sem_op = 0;
	sops[1].sem_num = 1;
	sops[1].sem_op = 1;
	sops[2].sem_num = 0;
	sops[2].sem_op = 0;
	sops[3].sem_num = 2;
	sops[3].sem_op = 0;
	sops[4].sem_num = 2;
	sops[4].sem_op = 1;
	if (semop(id, sops, 5) != 0) e(0);

	if (rcv(parent) != 4) e(0);

	sops[0].sem_num = 1;
	sops[0].sem_op = -2;
	sops[1].sem_num = 2;
	sops[1].sem_op = 0;
	if (semop(id, sops, 2) != 0) e(0);

	if (rcv(parent) != 5) e(0);

	sops[0].sem_num = 0;
	sops[0].sem_op = -1;
	sops[1].sem_num = 1;
	sops[1].sem_op = -1;
	sops[1].sem_flg = IPC_NOWAIT;
	if (semop(id, sops, 2) != 0) e(0);

	if (rcv(parent) != 6) e(0);

	sops[0].sem_num = 1;
	sops[0].sem_op = 0;
	sops[1].sem_num = 0;
	sops[1].sem_op = 0;
	sops[1].sem_flg = IPC_NOWAIT;
	if (semop(id, sops, 2) != -1) e(0);
	if (errno != EAGAIN) e(0);

	if (rcv(parent) != 7) e(0);

	sops[0].sem_num = 0;
	sops[0].sem_op = 0;
	sops[1].sem_num = 1;
	sops[1].sem_op = 1;
	sops[1].sem_flg = 0;
	if (semop(id, sops, 2) != 0) e(0);

	if (rcv(parent) != 8) e(0);

	sops[0].sem_num = 0;
	sops[0].sem_op = -1;
	sops[1].sem_num = 1;
	sops[1].sem_op = 2;
	if (semop(id, sops, 2) != -1) e(0);
	if (errno != ERANGE) e(0);

	memset(&act, 0, sizeof(act));
	act.sa_handler = got_signal;
	sigfillset(&act.sa_mask);
	if (sigaction(SIGHUP, &act, NULL) != 0) e(0);

	if (rcv(parent) != 9) e(0);

	memset(sops, 0, sizeof(sops));
	sops[0].sem_num = 0;
	sops[0].sem_op = 0;
	sops[1].sem_num = 0;
	sops[1].sem_op = 1;
	sops[2].sem_num = 1;
	sops[2].sem_op = 0;
	if (semop(id, sops, 3) != -1)
	if (errno != EINTR) e(0);
	if (nr_signals != 1) e(0);

	TEST_SEM(id, 0, 0, parent->pid, 0, 0);
	TEST_SEM(id, 1, 1, parent->pid, 0, 0);

	if (rcv(parent) != 10) e(0);

	memset(sops, 0, sizeof(sops));
	sops[0].sem_op = -3;
	if (semop(id, sops, 1) != -1) e(0);
	if (errno != EIDRM) e(0);

	id = rcv(parent);

	sops[0].sem_num = 0;
	sops[0].sem_op = -1;
	sops[1].sem_num = 1;
	sops[1].sem_op = 1;
	if (semop(id, sops, 2) != -1) e(0);
	if (errno != ERANGE) e(0);

	if (rcv(parent) != 11) e(0);

	sops[0].sem_num = 1;
	sops[0].sem_op = 0;
	sops[1].sem_num = 0;
	sops[1].sem_op = -1;
	if (semop(id, sops, 2) != 0) e(0);

	id = rcv(parent);

	sops[0].sem_num = 0;
	sops[0].sem_op = -1;
	sops[1].sem_num = 1;
	sops[1].sem_op = 0;
	if (semop(id, sops, 2) != 0) e(0);

	snd(parent, errct);
	if (rcv(parent) != 12) e(0);

	/* The child will be killed during this call.  It should not return. */
	sops[0].sem_num = 1;
	sops[0].sem_op = -1;
	sops[1].sem_num = 0;
	sops[1].sem_op = 3;
	(void)semop(id, sops, 2);

	e(0);
}

/*
 * Test the basic semop(2) functionality.
 */
static void
test88b(void)
{
	struct seminfo seminfo;
	struct semid_ds semds;
	struct sembuf *sops, *sops2;
	size_t size;
	struct link child;
	time_t now;
	unsigned short val[2];
	int id;

	subtest = 1;

	/* Allocate a buffer for operations. */
	if (semctl(0, 0, IPC_INFO, &seminfo) == -1) e(0);

	if (seminfo.semopm < 3 || seminfo.semopm > USHRT_MAX) e(0);

	size = sizeof(sops[0]) * (seminfo.semopm + 1);
	if ((sops = malloc(size)) == NULL) e(0);
	memset(sops, 0, size);

	/* Do a few first tests with a set containing one semaphore. */
	if ((id = semget(IPC_PRIVATE, 1, IPC_CREAT | 0600)) == -1) e(0);

	/* If no operations are given, the call should succeed. */
	if (semop(id, NULL, 0) != 0) e(0);

	/*
	 * If any operations are given, the pointer must be valid.  Moreover,
	 * partially valid buffers must never be processed partially.
	 */
	if (semop(id, NULL, 1) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semop(id, bad_ptr, 1) != -1) e(0);
	if (errno != EFAULT) e(0);

	memset(page_ptr, 0, page_size);
	sops2 = ((struct sembuf *)bad_ptr) - 1;
	sops2->sem_op = 1;
	if (semop(id, sops2, 2) != -1) e(0);
	if (errno != EFAULT) e(0);

	TEST_SEM(id, 0, 0, 0, 0, 0);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);

	/*
	 * A new semaphore set is initialized to an all-zeroes state, and a
	 * zeroed operation tests for a zeroed semaphore.  This should pass.
	 */
	time(&now);
	if (semop(id, sops, 1) != 0) e(0);

	TEST_SEM(id, 0, 0, getpid(), 0, 0);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime < now || semds.sem_otime >= now + 10) e(0);

	/* Test the limit on the number of operations. */
	if (semop(id, sops, seminfo.semopm) != 0) e(0);

	if (semop(id, sops, seminfo.semopm + 1) != -1) e(0);
	if (errno != E2BIG) e(0);

	if (semop(id, sops, SIZE_MAX) != -1) e(0);
	if (errno != E2BIG) e(0);

	/* Test the range check on the semaphore numbers. */
	sops[1].sem_num = 1;
	if (semop(id, sops, 2) != -1) e(0);
	if (errno != EFBIG) e(0);

	sops[1].sem_num = USHRT_MAX;
	if (semop(id, sops, 2) != -1) e(0);
	if (errno != EFBIG) e(0);

	/*
	 * Test nonblocking operations on a single semaphore, starting with
	 * value limit and overflow cases.
	 */
	if (seminfo.semvmx < 3 || seminfo.semvmx > SHRT_MAX) e(0);

	sops[0].sem_flg = IPC_NOWAIT;

	/* This block does not trigger on MINIX3. */
	if (seminfo.semvmx < SHRT_MAX) {
		sops[0].sem_op = seminfo.semvmx + 1;
		if (semop(id, sops, 1) != -1) e(0);
		if (errno != ERANGE) e(0);
		if (semctl(id, 0, GETVAL) != 0) e(0);
	}

	sops[0].sem_op = seminfo.semvmx;
	if (semop(id, sops, 1) != 0) e(0);
	if (semctl(id, 0, GETVAL) != seminfo.semvmx) e(0);

	/* As of writing, the proper checks for this is missing on NetBSD. */
	sops[0].sem_op = 1;
	if (semop(id, sops, 1) != -1) e(0);
	if (errno != ERANGE) e(0);
	if (semctl(id, 0, GETVAL) != seminfo.semvmx) e(0);

	sops[0].sem_op = seminfo.semvmx;
	if (semop(id, sops, 1) != -1) e(0);
	if (errno != ERANGE) e(0);
	if (semctl(id, 0, GETVAL) != seminfo.semvmx) e(0);

	sops[0].sem_op = SHRT_MAX;
	if (semop(id, sops, 1) != -1) e(0);
	if (errno != ERANGE) e(0);
	if (semctl(id, 0, GETVAL) != seminfo.semvmx) e(0);

	/* This block does trigger on MINIX3. */
	if (seminfo.semvmx < -(int)SHRT_MIN) {
		sops[0].sem_op = -seminfo.semvmx - 1;
		if (semop(id, sops, 1) != -1) e(0);
		if (errno != EAGAIN) e(0);
		if (semctl(id, 0, GETVAL) != seminfo.semvmx) e(0);
	}

	sops[0].sem_op = -seminfo.semvmx;
	if (semop(id, sops, 1) != 0) e(0);
	if (semctl(id, 0, GETVAL) != 0) e(0);

	/*
	 * Test basic nonblocking operations on a single semaphore.
	 */
	sops[0].sem_op = 0;
	if (semop(id, sops, 1) != 0) e(0);

	sops[0].sem_op = 2;
	if (semop(id, sops, 1) != 0) e(0);
	if (semctl(id, 0, GETVAL) != 2) e(0);

	sops[0].sem_op = 0;
	if (semop(id, sops, 1) != -1) e(0);
	if (errno != EAGAIN) e(0);

	sops[0].sem_op = -3;
	if (semop(id, sops, 1) != -1) e(0);
	if (errno != EAGAIN) e(0);

	sops[0].sem_op = 1;
	if (semop(id, sops, 1) != 0) e(0);
	if (semctl(id, 0, GETVAL) != 3) e(0);

	sops[0].sem_op = -1;
	if (semop(id, sops, 1) != 0) e(0);
	if (semctl(id, 0, GETVAL) != 2) e(0);

	sops[0].sem_op = 0;
	if (semop(id, sops, 1) != -1) e(0);
	if (errno != EAGAIN) e(0);

	sops[0].sem_op = -2;
	if (semop(id, sops, 1) != 0) e(0);
	if (semctl(id, 0, GETVAL) != 0) e(0);

	sops[0].sem_op = 0;
	if (semop(id, sops, 1) != 0) e(0);

	/* Make sure that not too much data is being read in. */
	sops2->sem_op = 0;
	sops2--;
	if (semop(id, sops2, 2) != 0) e(0);

	/* Even if no operations are given, the identifier must be valid. */
	if (semctl(id, 0, IPC_RMID) != 0) e(0);

	if (semop(id, NULL, 0) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semop(-1, NULL, 0) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semop(INT_MIN, NULL, 0) != -1) e(0);
	if (errno != EINVAL) e(0);

	memset(&semds, 0, sizeof(semds));
	id = IXSEQ_TO_IPCID(seminfo.semmni, semds.sem_perm);
	if (semop(id, NULL, 0) != -1) e(0);
	if (errno != EINVAL) e(0);

	/*
	 * Test permission checks.  As part of this, test basic nonblocking
	 * multi-operation calls, including operation processing in array order
	 * and the order of (permission vs other) checks.
	 */
	test_perm(test88b_perm, 0 /*owner_test*/);

	/*
	 * Test blocking operations, starting with a single blocking operation.
	 */
	if ((id = semget(IPC_PRIVATE, 3, 0600)) == -1) e(0);

	memset(sops, 0, sizeof(sops[0]));
	sops[0].sem_op = 1;
	if (semop(id, sops, 1) != 0) e(0);

	TEST_SEM(id, 0, 1, getpid(), 0, 0);

	spawn(&child, test88b_child, DROP_NONE);

	snd(&child, id);

	/*
	 * In various places, we have to sleep in order to allow the child to
	 * get itself blocked in a semop(2) call.
	 */
	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 1, getpid(), 0, 1);

	sops[0].sem_op = -1;
	if (semop(id, sops, 1) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);

	sops[0].sem_op = 1;
	if (semop(id, sops, 1) != 0) e(0);

	TEST_SEM(id, 0, 1, getpid(), 0, 0);

	snd(&child, 1);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 1, getpid(), 1, 0);

	/* This should cause a (fruitless) retry of the blocking operation. */
	sops[0].sem_op = 1;
	if (semop(id, sops, 1) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 2, getpid(), 1, 0);

	sops[0].sem_op = 1;
	if (semop(id, sops, 1) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);

	/*
	 * Test blocking operations, verifying the correct operation of
	 * multiple (partially) blocking operations and atomicity.
	 */
	memset(sops, 0, sizeof(sops[0]) * 2);
	if (semop(id, sops, 1) != 0) e(0);

	/* One blocking operation. */
	snd(&child, 2);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, getpid(), 0, 0);
	TEST_SEM(id, 1, 0, 0, 1, 0);
	TEST_SEM(id, 2, 0, 0, 0, 0);

	sops[0].sem_num = 1;
	sops[0].sem_op = 1;
	if (semop(id, sops, 1) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 1, child.pid, 0, 0);
	TEST_SEM(id, 1, 0, child.pid, 0, 0);
	TEST_SEM(id, 2, 2, child.pid, 0, 0);

	/* Two blocking operations in one call, resolved at once. */
	snd(&child, 3);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 1, child.pid, 0, 1);
	TEST_SEM(id, 1, 0, child.pid, 0, 0);
	TEST_SEM(id, 2, 2, child.pid, 0, 0);

	sops[0].sem_num = 0;
	sops[0].sem_op = -1;
	sops[1].sem_num = 2;
	sops[1].sem_op = -2;
	if (semop(id, sops, 2) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);
	TEST_SEM(id, 1, 1, child.pid, 0, 0);
	TEST_SEM(id, 2, 1, child.pid, 0, 0);

	/* Two blocking operations in one call, resolved one by one. */
	snd(&child, 4);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);
	TEST_SEM(id, 1, 1, child.pid, 1, 0);
	TEST_SEM(id, 2, 1, child.pid, 0, 0);

	sops[0].sem_num = 1;
	sops[0].sem_op = 1;
	if (semop(id, sops, 1) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);
	TEST_SEM(id, 1, 2, getpid(), 0, 0);
	TEST_SEM(id, 2, 1, child.pid, 0, 1);

	sops[0].sem_num = 2;
	sops[0].sem_op = -1;
	if (semop(id, sops, 1) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);
	TEST_SEM(id, 1, 0, child.pid, 0, 0);
	TEST_SEM(id, 2, 0, child.pid, 0, 0);

	/* One blocking op followed by a nonblocking one, cleared at once. */
	sops[0].sem_num = 0;
	sops[0].sem_op = 0;
	sops[1].sem_num = 1;
	sops[1].sem_op = 0;
	if (semop(id, sops, 2) != 0) e(0);

	snd(&child, 5);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, getpid(), 1, 0);
	TEST_SEM(id, 1, 0, getpid(), 0, 0);

	sops[0].sem_num = 0;
	sops[0].sem_op = 1;
	sops[1].sem_num = 1;
	sops[1].sem_op = 1;
	if (semop(id, sops, 2) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);
	TEST_SEM(id, 1, 0, child.pid, 0, 0);

	/* One blocking op followed by a nonblocking one, only one cleared. */
	sops[0].sem_num = 0;
	sops[0].sem_op = 1;
	sops[1].sem_num = 1;
	sops[1].sem_op = 1;
	if (semop(id, sops, 2) != 0) e(0);

	snd(&child, 6);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 1, getpid(), 0, 0);
	TEST_SEM(id, 1, 1, getpid(), 0, 1);

	sops[0].sem_num = 1;
	sops[0].sem_op = -1;
	if (semop(id, sops, 1) != 0) e(0);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 1, getpid(), 0, 0);
	TEST_SEM(id, 1, 0, getpid(), 0, 0);

	/*
	 * Ensure that all semaphore numbers are checked immediately, which
	 * given the earlier test results also implies that permissions are
	 * checked immediately (so we don't have to recheck that too).  We do
	 * not check whether permissions are rechecked after a blocking
	 * operation, because the specification does not describe the intended
	 * behavior on this point.
	 */
	sops[0].sem_num = 0;
	sops[0].sem_op = 0;
	sops[1].sem_num = 4;
	sops[1].sem_op = 0;
	if (semop(id, sops, 2) != -1) e(0);
	if (errno != EFBIG) e(0);

	/*
	 * Ensure that semaphore value overflow is detected properly, at the
	 * moment that the operation is actually processed.
	 */
	sops[0].sem_num = 1;
	sops[0].sem_op = seminfo.semvmx;
	if (semop(id, sops, 1) != 0) e(0);

	snd(&child, 7);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 1, getpid(), 0, 1);
	TEST_SEM(id, 1, seminfo.semvmx, getpid(), 0, 0);

	sops[0].sem_num = 0;
	sops[0].sem_op = -1;
	sops[1].sem_num = 1;
	sops[1].sem_op = -1;
	if (semop(id, sops, 2) != 0) e(0);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);
	TEST_SEM(id, 1, seminfo.semvmx, child.pid, 0, 0);

	sops[0].sem_num = 1;
	sops[0].sem_op = -2;
	if (semop(id, sops, 1) != 0) e(0);

	snd(&child, 8);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, child.pid, 1, 0);
	TEST_SEM(id, 1, seminfo.semvmx - 2, getpid(), 0, 0);

	sops[0].sem_num = 0;
	sops[0].sem_op = 1;
	sops[1].sem_num = 1;
	sops[1].sem_op = 1;
	if (semop(id, sops, 2) != 0) e(0);

	TEST_SEM(id, 0, 1, getpid(), 0, 0);
	TEST_SEM(id, 1, seminfo.semvmx - 1, getpid(), 0, 0);

	sops[0].sem_num = 0;
	sops[0].sem_op = seminfo.semvmx - 1;
	sops[1].sem_num = 0;
	sops[1].sem_op = seminfo.semvmx - 1;
	sops[2].sem_num = 0;
	sops[2].sem_op = 2;
	/*
	 * With the current SEMVMX, the sum of the values is now USHRT_MAX-1,
	 * which if processed could result in a zero semaphore value.  That
	 * should not happen.  Looking at you, NetBSD.
	 */
	if (semop(id, sops, 3) != -1) e(0);
	if (errno != ERANGE) e(0);

	TEST_SEM(id, 0, 1, getpid(), 0, 0);

	/*
	 * Check that a blocking semop(2) call fails with EINTR if a signal is
	 * caught by the process after the call has blocked.
	 */
	if (semctl(id, 1, SETVAL, 0) != 0) e(0);
	sops[0].sem_num = 0;
	sops[0].sem_op = -1;
	sops[1].sem_num = 1;
	sops[1].sem_op = 1;
	if (semop(id, sops, 2) != 0) e(0);

	TEST_SEM(id, 0, 0, getpid(), 0, 0);
	TEST_SEM(id, 1, 1, getpid(), 0, 0);

	snd(&child, 9);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, getpid(), 0, 0);
	TEST_SEM(id, 1, 1, getpid(), 0, 1);

	kill(child.pid, SIGHUP);
	/*
	 * Kills are not guaranteed to be delivered immediately to processes
	 * other than the caller of kill(2), so let the child perform checks.
	 */

	/*
	 * Check that a blocking semop(2) call fails with EIDRM if the
	 * semaphore set is removed after the call has blocked.
	 */
	snd(&child, 10);

	usleep(WAIT_USECS);

	if (semctl(id, 0, IPC_RMID) != 0) e(0);

	/*
	 * Check if sem_otime is updated correctly.  Instead of sleeping for
	 * whole seconds so as to be able to detect differences, use SETVAL,
	 * which does not update sem_otime at all.  This doubles as a first
	 * test to see if SETVAL correctly wakes up a blocked semop(2) call.
	 */
	if ((id = semget(IPC_PRIVATE, 2, 0600)) == -1) e(0);

	snd(&child, id);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, 0, 1, 0);
	TEST_SEM(id, 1, 0, 0, 0, 0);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);

	if (semctl(id, 1, SETVAL, seminfo.semvmx) != 0) e(0);

	TEST_SEM(id, 0, 0, 0, 1, 0);
	TEST_SEM(id, 1, seminfo.semvmx, 0, 0, 0);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);

	if (semctl(id, 0, SETVAL, 1) != 0) e(0);
	TEST_SEM(id, 0, 1, 0, 0, 0);
	TEST_SEM(id, 1, seminfo.semvmx, 0, 0, 0);

	if (semctl(id, 0, SETVAL, 0) != 0) e(0);

	snd(&child, 11);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, 0, 0, 0);
	TEST_SEM(id, 1, seminfo.semvmx, 0, 0, 1);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);

	if (semctl(id, 1, SETVAL, 0) != 0) e(0);

	TEST_SEM(id, 0, 0, 0, 1, 0);
	TEST_SEM(id, 1, 0, 0, 0, 0);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);

	time(&now);
	if (semctl(id, 0, SETVAL, 2) != 0) e(0);

	TEST_SEM(id, 0, 1, child.pid, 0, 0);
	TEST_SEM(id, 1, 0, child.pid, 0, 0);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime < now || semds.sem_otime >= now + 10) e(0);

	if (semctl(id, 0, IPC_RMID) != 0) e(0);

	/*
	 * Perform a similar test for SETALL, ensuring that it causes an
	 * ongoing semop(2) to behave correctly.
	 */
	if ((id = semget(IPC_PRIVATE, 2, 0600)) == -1) e(0);

	snd(&child, id);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, 0, 1, 0);
	TEST_SEM(id, 1, 0, 0, 0, 0);

	val[0] = 1;
	val[1] = 1;
	if (semctl(id, 0, SETALL, val) != 0) e(0);

	TEST_SEM(id, 0, 1, 0, 0, 0);
	TEST_SEM(id, 1, 1, 0, 0, 1);

	val[0] = 0;
	val[1] = 1;
	if (semctl(id, 0, SETALL, val) != 0) e(0);

	TEST_SEM(id, 0, 0, 0, 1, 0);
	TEST_SEM(id, 1, 1, 0, 0, 0);

	val[0] = 1;
	val[1] = 1;
	if (semctl(id, 0, SETALL, val) != 0) e(0);

	TEST_SEM(id, 0, 1, 0, 0, 0);
	TEST_SEM(id, 1, 1, 0, 0, 1);

	val[0] = 0;
	val[1] = 0;
	if (semctl(id, 0, SETALL, val) != 0) e(0);

	TEST_SEM(id, 0, 0, 0, 1, 0);
	TEST_SEM(id, 1, 0, 0, 0, 0);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);

	time(&now);
	val[0] = 1;
	val[1] = 0;
	if (semctl(id, 0, SETALL, val) != 0) e(0);

	TEST_SEM(id, 0, 0, child.pid, 0, 0);
	TEST_SEM(id, 1, 0, child.pid, 0, 0);
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime < now || semds.sem_otime >= now + 10) e(0);

	/*
	 * Finally, ensure that if the child is killed, its blocked semop(2)
	 * call is properly cancelled.
	 */
	sops[0].sem_num = 0;
	sops[0].sem_op = 0;
	sops[1].sem_num = 1;
	sops[1].sem_op = 0;
	if (semop(id, sops, 2) != 0) e(0);

	TEST_SEM(id, 0, 0, getpid(), 0, 0);
	TEST_SEM(id, 1, 0, getpid(), 0, 0);

	/* We'll be terminating the child, so let it report its errors now. */
	if (rcv(&child) != 0) e(0);

	snd(&child, 12);

	usleep(WAIT_USECS);

	TEST_SEM(id, 0, 0, getpid(), 0, 0);
	TEST_SEM(id, 1, 0, getpid(), 1, 0);

	terminate(&child);

	TEST_SEM(id, 0, 0, getpid(), 0, 0);
	TEST_SEM(id, 1, 0, getpid(), 0, 0);

	if (semctl(id, 0, IPC_RMID) != 0) e(0);

	free(sops);
}

/*
 * Test semctl(2) permission checks, part 1: regular commands.
 */
static void
test88c_perm1(struct link * parent)
{
	static const int cmds[] = { GETVAL, GETPID, GETNCNT, GETZCNT };
	struct semid_ds semds;
	struct seminfo seminfo;
	unsigned short val[3];
	int i, r, tbit, bit, id[3], cmd;
	void *ptr;

	while ((tbit = rcv(parent)) != -1) {
		id[0] = rcv(parent);
		id[1] = rcv(parent);
		id[2] = rcv(parent);

		/* First the read-only, no-argument cases. */
		bit = 4;
		for (i = 0; i < __arraycount(cmds); i++) {
			r = semctl(id[0], 0, cmds[i]);
			if (r < 0 && (r != -1 || errno != EACCES)) e(0);
			if (((bit & tbit) == bit) != (r != -1)) e(0);

			r = semctl(id[1], 0, cmds[i]);
			if (r < 0 && (r != -1 || errno != EACCES)) e(0);
			if (((bit & tbit) == bit) != (r != -1)) e(0);

			if (semctl(id[2], 0, cmds[i]) != -1) e(0);
			if (errno != EACCES) e(0);
		}

		/*
		 * Then SETVAL, which requires write permission and is the only
		 * one that takes an integer argument.
		 */
		bit = 2;
		r = semctl(id[0], 0, SETVAL, 0);
		if (r < 0 && (r != -1 || errno != EACCES)) e(0);
		if (((bit & tbit) == bit) != (r != -1)) e(0);

		r = semctl(id[1], 0, SETVAL, 0);
		if (r < 0 && (r != -1 || errno != EACCES)) e(0);
		if (((bit & tbit) == bit) != (r != -1)) e(0);

		if (semctl(id[2], 0, SETVAL, 0) != -1) e(0);
		if (errno != EACCES) e(0);

		/*
		 * Finally the commands that require read or write permission
		 * and take a pointer as argument.
		 */
		memset(val, 0, sizeof(val));

		for (i = 0; i < 3; i++) {
			switch (i) {
			case 0:
				cmd = GETALL;
				ptr = val;
				bit = 4;
				break;
			case 1:
				cmd = SETALL;
				ptr = val;
				bit = 2;
				break;
			case 2:
				cmd = IPC_STAT;
				ptr = &semds;
				bit = 4;
				break;
			default:
				abort();
			}

			r = semctl(id[0], 0, cmd, ptr);
			if (r < 0 && (r != -1 || errno != EACCES)) e(0);
			if (((bit & tbit) == bit) != (r != -1)) e(0);

			r = semctl(id[1], 0, cmd, ptr);
			if (r < 0 && (r != -1 || errno != EACCES)) e(0);
			if (((bit & tbit) == bit) != (r != -1)) e(0);

			if (semctl(id[2], 0, cmd, ptr) != -1) e(0);
			if (errno != EACCES) e(0);
		}

		/*
		 * I was hoping to avoid this, but otherwise we have to make
		 * the other child iterate through all semaphore sets to find
		 * the right index for each of the identifiers.  As noted in
		 * the IPC server itself as well, duplicating these macros is
		 * not a big deal since the split is firmly hardcoded through
		 * the exposure of IXSEQ_TO_IPCID to userland.
		 */
#ifndef IPCID_TO_IX
#define IPCID_TO_IX(id)		((id) & 0xffff)
#endif

		bit = 4;

		r = semctl(IPCID_TO_IX(id[0]), 0, SEM_STAT, &semds);
		if (r < 0 && (r != -1 || errno != EACCES)) e(0);
		if (((bit & tbit) == bit) != (r != -1)) e(0);

		r = semctl(IPCID_TO_IX(id[1]), 0, SEM_STAT, &semds);
		if (r < 0 && (r != -1 || errno != EACCES)) e(0);
		if (((bit & tbit) == bit) != (r != -1)) e(0);

		if (semctl(IPCID_TO_IX(id[2]), 0, SEM_STAT, &semds) != -1)
			e(0);
		if (errno != EACCES) e(0);

		/*
		 * IPC_INFO and SEM_INFO should always succeed.  They do not
		 * even take a semaphore set identifier.
		 */
		if (semctl(0, 0, IPC_INFO, &seminfo) == -1) e(0);
		if (semctl(0, 0, SEM_INFO, &seminfo) == -1) e(0);

		snd(parent, 0);
	}
}

/*
 * Test semctl(2) permission checks, part 2: the IPC_SET command.
 */
static void
test88c_perm2(struct link * parent)
{
	struct semid_ds semds;
	int r, shift, id[3];

	while ((shift = rcv(parent)) != -1) {
		id[0] = rcv(parent);
		id[1] = rcv(parent);
		id[2] = rcv(parent);

		/*
		 * Test IPC_SET.  Ideally, we would set the permissions to what
		 * they currently are, but we do not actually know what they
		 * are, and IPC_STAT requires read permission which we may not
		 * have!  However, no matter what we do, we cannot prevent the
		 * other child from being able to remove the semaphore sets
		 * afterwards.  So, we just set the permissions to all-zeroes;
		 * even though those values are meaningful (mode 0000, uid 0,
		 * gid 0) they could be anything: the API will accept anything.
		 * This does mean we need to test IPC_RMID permissions from
		 * another procedure, because we may now be locking ourselves
		 * out.  The System V IPC interface is pretty strange that way.
		 */
		memset(&semds, 0, sizeof(semds));

		r = semctl(id[0], 0, IPC_SET, &semds);
		if (r < 0 && (r != -1 || errno != EPERM)) e(0);
		if ((shift == 6) != (r != -1)) e(0);

		r = semctl(id[1], 0, IPC_SET, &semds);
		if (r < 0 && (r != -1 || errno != EPERM)) e(0);
		if ((shift == 6) != (r != -1)) e(0);

		/* For once, this too should succeed. */
		r = semctl(id[2], 0, IPC_SET, &semds);
		if (r < 0 && (r != -1 || errno != EPERM)) e(0);
		if ((shift == 6) != (r != -1)) e(0);

		snd(parent, 0);
	}
}

/*
 * Test semctl(2) permission checks, part 3: the IPC_RMID command.
 */
static void
test88c_perm3(struct link * parent)
{
	int r, shift, id[3];

	while ((shift = rcv(parent)) != -1) {
		id[0] = rcv(parent);
		id[1] = rcv(parent);
		id[2] = rcv(parent);

		r = semctl(id[0], 0, IPC_RMID);
		if (r < 0 && (r != -1 || errno != EPERM)) e(0);
		if ((shift == 6) != (r != -1)) e(0);

		r = semctl(id[1], 0, IPC_RMID);
		if (r < 0 && (r != -1 || errno != EPERM)) e(0);
		if ((shift == 6) != (r != -1)) e(0);

		/* Okay, twice then. */
		r = semctl(id[2], 0, IPC_RMID);
		if (r < 0 && (r != -1 || errno != EPERM)) e(0);
		if ((shift == 6) != (r != -1)) e(0);

		snd(parent, 0);
	}
}

/*
 * Test the basic semctl(2) functionality.
 */
static void
test88c(void)
{
	static const int cmds[] = { GETVAL, GETPID, GETNCNT, GETZCNT };
	struct seminfo seminfo;
	struct semid_ds semds, osemds;
	unsigned short val[4], seen[2];
	char statbuf[sizeof(struct semid_ds) + 1];
	unsigned int i, j;
	time_t now;
	int r, id, id2, badid1, badid2, cmd;

	subtest = 2;

	if (semctl(0, 0, IPC_INFO, &seminfo) == -1) e(0);

	/*
	 * Start with permission checks on the commands.  IPC_SET and IPC_RMID
	 * are special: they check for ownership (uid/cuid) and return EPERM
	 * rather than EACCES on permission failure.
	 */
	test_perm(test88c_perm1, 0 /*owner_test*/);
	test_perm(test88c_perm2, 1 /*owner_test*/);
	test_perm(test88c_perm3, 1 /*owner_test*/);

	/* Create identifiers known to be invalid. */
	if ((badid1 = semget(IPC_PRIVATE, 1, 0600)) < 0) e(0);

	if (semctl(badid1, 0, IPC_RMID) != 0) e(0);

	memset(&semds, 0, sizeof(semds));
	badid2 = IXSEQ_TO_IPCID(seminfo.semmni, semds.sem_perm);

	if ((id = semget(IPC_PRIVATE, 3, IPC_CREAT | 0600)) < 0) e(0);

	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime == 0) e(0);

	/* In this case we can't avoid sleeping for longer periods.. */
	while (time(&now) == semds.sem_ctime)
		usleep(250000);

	/*
	 * Test the simple GET commands.  The actual functionality of these
	 * commands have already been tested thoroughly as part of the
	 * semop(2) part of the test set, so we do not repeat that here.
	 */
	for (i = 0; i < __arraycount(cmds); i++) {
		for (j = 0; j < 3; j++)
			if (semctl(id, j, cmds[i]) != 0) e(0);

		if (semctl(badid1, 0, cmds[i]) != -1) e(0);
		if (errno != EINVAL) e(0);

		if (semctl(badid2, 0, cmds[i]) != -1) e(0);
		if (errno != EINVAL) e(0);

		if (semctl(-1, 0, cmds[i]) != -1) e(0);
		if (errno != EINVAL) e(0);

		if (semctl(INT_MIN, 0, cmds[i]) != -1) e(0);
		if (errno != EINVAL) e(0);

		if (semctl(id, -1, cmds[i]) != -1) e(0);
		if (errno != EINVAL) e(0);

		if (semctl(id, 3, cmds[i]) != -1) e(0);
		if (errno != EINVAL) e(0);

		/* These commands should not update ctime or otime. */
		if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
		if (semds.sem_otime != 0) e(0);
		if (semds.sem_ctime >= now) e(0);
	}

	/*
	 * Test the GETALL command.
	 */
	/*
	 * Contrary to what the Open Group specification suggests, actual
	 * implementations agree that the semnum parameter is to be ignored for
	 * calls not involving a specific semaphore in the set.
	 */
	for (j = 0; j < 5; j++) {
		for (i = 0; i < __arraycount(val); i++)
			val[i] = USHRT_MAX;

		if (semctl(id, (int)j - 1, GETALL, val) != 0) e(0);
		for (i = 0; i < 3; i++)
			if (val[i] != 0) e(0);
		if (val[i] != USHRT_MAX) e(0);
	}

	for (i = 0; i < __arraycount(val); i++)
		val[i] = USHRT_MAX;

	if (semctl(badid1, 0, GETALL, val) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(badid2, 0, GETALL, val) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(-1, 0, GETALL, val) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(INT_MIN, 0, GETALL, val) != -1) e(0);
	if (errno != EINVAL) e(0);

	for (i = 0; i < __arraycount(val); i++)
		if (val[i] != USHRT_MAX) e(0);

	if (semctl(id, 0, GETALL, NULL) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, GETALL, bad_ptr) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, GETALL, ((unsigned short *)bad_ptr) - 2) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, GETALL, ((unsigned short *)bad_ptr) - 3) != 0) e(0);

	/* Still no change in either otime or ctime. */
	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime >= now) e(0);

	/*
	 * Test the IPC_STAT command.  This is the last command we are testing
	 * here that does not affect sem_ctime, so in order to avoid extra
	 * sleep times, we test this command first now.
	 */
	/*
	 * The basic IPC_STAT functionality has already been tested heavily as
	 * part of the semget(2) and permission tests, so we do not repeat that
	 * here.
	 */
	memset(statbuf, 0x5a, sizeof(statbuf));

	if (semctl(badid1, 0, IPC_STAT, statbuf) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(badid2, 0, IPC_STAT, statbuf) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(-1, 0, IPC_STAT, statbuf) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(INT_MIN, 0, IPC_STAT, statbuf) != -1) e(0);
	if (errno != EINVAL) e(0);

	for (i = 0; i < sizeof(statbuf); i++)
		if (statbuf[i] != 0x5a) e(0);

	if (semctl(id, 0, IPC_STAT, statbuf) != 0) e(0);

	if (statbuf[sizeof(statbuf) - 1] != 0x5a) e(0);

	if (semctl(id, 0, IPC_STAT, NULL) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, IPC_STAT, bad_ptr) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, IPC_STAT, ((struct semid_ds *)bad_ptr) - 1) != 0)
		e(0);

	if (semctl(id, -1, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime >= now) e(0);

	/*
	 * Test SEM_STAT.
	 */
	if ((id2 = semget(KEY_A, seminfo.semmsl, IPC_CREAT | 0642)) < 0) e(0);

	memset(statbuf, 0x5a, sizeof(statbuf));

	if (semctl(-1, 0, SEM_STAT, statbuf) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(seminfo.semmni, 0, SEM_STAT, statbuf) != -1) e(0);
	if (errno != EINVAL) e(0);

	for (i = 0; i < sizeof(statbuf); i++)
		if (statbuf[i] != 0x5a) e(0);

	memset(seen, 0, sizeof(seen));

	for (i = 0; i < seminfo.semmni; i++) {
		errno = 0;
		if ((r = semctl(i, i / 2 - 1, SEM_STAT, statbuf)) == -1) {
			if (errno != EINVAL) e(0);
			continue;
		}
		if (r < 0) e(0);
		memcpy(&semds, statbuf, sizeof(semds));
		if (!(semds.sem_perm.mode & SEM_ALLOC)) e(0);
		if (semds.sem_ctime == 0) e(0);
		if (IXSEQ_TO_IPCID(i, semds.sem_perm) != r) e(0);
		if (r == id) {
			seen[0]++;
			if (semds.sem_perm.mode != (SEM_ALLOC | 0600)) e(0);
			if (semds.sem_perm.uid != geteuid()) e(0);
			if (semds.sem_perm.gid != getegid()) e(0);
			if (semds.sem_perm.cuid != semds.sem_perm.uid) e(0);
			if (semds.sem_perm.cgid != semds.sem_perm.gid) e(0);
			if (semds.sem_perm._key != IPC_PRIVATE) e(0);
			if (semds.sem_nsems != 3) e(0);
			if (semds.sem_otime != 0) e(0);

			/* This is here because we need a valid index. */
			if (semctl(i, 0, SEM_STAT, NULL) != -1) e(0);
			if (errno != EFAULT) e(0);

			if (semctl(i, 0, SEM_STAT, bad_ptr) != -1) e(0);
			if (errno != EFAULT) e(0);
		} else if (r == id2) {
			seen[1]++;
			if (semds.sem_perm.mode != (SEM_ALLOC | 0642)) e(0);
			if (semds.sem_perm.uid != geteuid()) e(0);
			if (semds.sem_perm.gid != getegid()) e(0);
			if (semds.sem_perm.cuid != semds.sem_perm.uid) e(0);
			if (semds.sem_perm.cgid != semds.sem_perm.gid) e(0);
			if (semds.sem_perm._key != KEY_A) e(0);
			if (semds.sem_nsems != seminfo.semmsl) e(0);
		}
	}

	if (seen[0] != 1) e(0);
	if (seen[1] != 1) e(0);

	if (statbuf[sizeof(statbuf) - 1] != 0x5a) e(0);

	if (semctl(id, 5, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime >= now) e(0);

	/*
	 * Test SETVAL.  We start with all the failure cases, so as to be able
	 * to check that sem_ctime is not changed in those cases.
	 */
	if (semctl(badid1, 0, SETVAL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(badid2, 0, SETVAL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(-1, 0, SETVAL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(INT_MIN, 0, SETVAL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, -1, SETVAL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, 3, SETVAL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, 0, SETVAL, -1) != -1) e(0);
	if (errno != ERANGE) e(0);

	if (semctl(id, 0, SETVAL, seminfo.semvmx + 1) != -1) e(0);
	if (errno != ERANGE) e(0);

	TEST_SEM(id, 0, 0, 0, 0, 0);

	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime >= now) e(0);

	/* Alright, there we go.. */
	if (semctl(id, 1, SETVAL, 0) != 0) e(0);

	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime < now || semds.sem_ctime >= now + 10) e(0);

	TEST_SEM(id, 1, 0, 0, 0, 0);

	if (semctl(id, 2, SETVAL, seminfo.semvmx) != 0) e(0);

	TEST_SEM(id, 2, seminfo.semvmx, 0, 0, 0);

	if (semctl(id, 0, SETVAL, 1) != 0) e(0);

	TEST_SEM(id, 0, 1, 0, 0, 0);
	TEST_SEM(id, 1, 0, 0, 0, 0);
	TEST_SEM(id, 2, seminfo.semvmx, 0, 0, 0);

	if (semctl(id, 0, GETALL, val) != 0) e(0);
	if (val[0] != 1) e(0);
	if (val[1] != 0) e(0);
	if (val[2] != seminfo.semvmx) e(0);

	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);

	while (time(&now) == semds.sem_ctime)
		usleep(250000);

	/*
	 * Test SETALL.  Same idea: failure cases first.
	 */
	if (semctl(badid1, 0, SETALL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(badid2, 0, SETALL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(-1, 0, SETALL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(INT_MIN, 0, SETALL, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	val[0] = seminfo.semvmx + 1;
	val[1] = 0;
	val[2] = 0;
	if (semctl(id, 0, SETALL, val) != -1) e(0);
	if (errno != ERANGE) e(0);

	val[0] = 0;
	val[1] = 1;
	val[2] = seminfo.semvmx + 1;
	if (semctl(id, 0, SETALL, val) != -1) e(0);
	if (errno != ERANGE) e(0);

	if (semctl(id, 0, SETALL, NULL) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, SETALL, bad_ptr) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, SETALL, ((unsigned short *)bad_ptr) - 2) != -1) e(0);
	if (errno != EFAULT) e(0);

	TEST_SEM(id, 0, 1, 0, 0, 0);
	TEST_SEM(id, 1, 0, 0, 0, 0);
	TEST_SEM(id, 2, seminfo.semvmx, 0, 0, 0);

	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime >= now) e(0);

	val[0] = seminfo.semvmx;
	val[1] = 0;
	val[2] = 0;
	if (semctl(id, 0, SETALL, val) != 0) e(0);

	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_otime != 0) e(0);
	if (semds.sem_ctime < now || semds.sem_ctime >= now + 10) e(0);

	TEST_SEM(id, 0, seminfo.semvmx, 0, 0, 0);
	TEST_SEM(id, 1, 0, 0, 0, 0);
	TEST_SEM(id, 2, 0, 0, 0, 0);

	val[0] = 0;
	val[1] = 1;
	val[2] = seminfo.semvmx;
	if (semctl(id, INT_MAX, SETALL, val) != 0) e(0);

	TEST_SEM(id, 0, 0, 0, 0, 0);
	TEST_SEM(id, 1, 1, 0, 0, 0);
	TEST_SEM(id, 2, seminfo.semvmx, 0, 0, 0);

	memset(page_ptr, 0, page_size);
	if (semctl(id, 0, SETALL, ((unsigned short *)bad_ptr) - 3) != 0) e(0);

	TEST_SEM(id, 0, 0, 0, 0, 0);
	TEST_SEM(id, 1, 0, 0, 0, 0);
	TEST_SEM(id, 2, 0, 0, 0, 0);

	while (time(&now) == semds.sem_ctime)
		usleep(250000);

	/*
	 * Test IPC_SET.  Its core functionality has already been tested
	 * thoroughly as part of the permission tests.
	 */
	if (semctl(badid1, 0, IPC_SET, &semds) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(badid2, 0, IPC_SET, &semds) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(-1, 0, IPC_SET, &semds) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(INT_MIN, 0, IPC_SET, &semds) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, 0, IPC_SET, NULL) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, IPC_SET, bad_ptr) != -1) e(0);
	if (errno != EFAULT) e(0);

	if (semctl(id, 0, IPC_STAT, &osemds) != 0) e(0);
	if (osemds.sem_otime != 0) e(0);
	if (osemds.sem_ctime >= now) e(0);

	/*
	 * Only mode, uid, gid may be set.  While the given mode is sanitized
	 * in our implementation (see below; the open group specification
	 * leaves this undefined), the uid and gid are not (we do not test this
	 * exhaustively).  The other given fields must be ignored.  The ctime
	 * field will be updated.
	 */
	memset(&semds, 0x5b, sizeof(semds));
	semds.sem_perm.mode = 0712;
	semds.sem_perm.uid = UID_MAX;
	semds.sem_perm.gid = GID_MAX - 1;
	if (semctl(id, 0, IPC_SET, &semds) != 0) e(0);

	if (semctl(id, 0, IPC_STAT, &semds) != 0) e(0);
	if (semds.sem_perm.mode != (SEM_ALLOC | 0712)) e(0);
	if (semds.sem_perm.uid != UID_MAX) e(0);
	if (semds.sem_perm.gid != GID_MAX - 1) e(0);
	if (semds.sem_perm.cuid != osemds.sem_perm.cuid) e(0);
	if (semds.sem_perm.cgid != osemds.sem_perm.cgid) e(0);
	if (semds.sem_perm._seq != osemds.sem_perm._seq) e(0);
	if (semds.sem_perm._key != osemds.sem_perm._key) e(0);
	if (semds.sem_nsems != osemds.sem_nsems) e(0);
	if (semds.sem_otime != osemds.sem_otime) e(0);
	if (semds.sem_ctime < now || semds.sem_ctime >= now + 10) e(0);

	/* It should be possible to set any mode, but mask 0777 is applied. */
	semds.sem_perm.uid = osemds.sem_perm.uid;
	semds.sem_perm.gid = osemds.sem_perm.gid;
	for (i = 0; i < 0777; i++) {
		semds.sem_perm.mode = i;
		if (semctl(id, i / 2 - 1, IPC_SET, &semds) != 0) e(0);

		if (semctl(id, i / 2 - 2, IPC_STAT, &semds) != 0) e(0);
		if (semds.sem_perm.mode != (SEM_ALLOC | i)) e(0);

		semds.sem_perm.mode = ~0777 | i;
		if (semctl(id, i / 2 - 3, IPC_SET, &semds) != 0) e(0);

		if (semctl(id, i / 2 - 4, IPC_STAT, &semds) != 0) e(0);
		if (semds.sem_perm.mode != (SEM_ALLOC | i)) e(0);
	}
	if (semds.sem_perm.uid != osemds.sem_perm.uid) e(0);
	if (semds.sem_perm.gid != osemds.sem_perm.gid) e(0);

	if (semctl(id, 0, IPC_SET, ((struct semid_ds *)bad_ptr) - 1) != 0)
		e(0);

	/*
	 * Test IPC_RMID.  Its basic functionality has already been tested
	 * multiple times over, so there is not much left to do here.
	 */
	if (semctl(badid1, 0, IPC_RMID) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(badid2, 0, IPC_RMID) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(-1, 0, IPC_RMID) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(INT_MIN, 0, IPC_RMID) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, 0, IPC_RMID) != 0) e(0);

	if (semctl(id, 0, IPC_RMID) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, 0, IPC_STAT, &semds) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id2, 1, IPC_RMID) != 0) e(0);

	if (semctl(id2, 1, IPC_RMID) != -1) e(0);
	if (errno != EINVAL) e(0);

	/*
	 * Test IPC_INFO and SEM_INFO.  Right now, for all practical purposes,
	 * these identifiers behave pretty much the same.
	 */
	if ((id = semget(IPC_PRIVATE, 3, 0600)) == -1) e(0);
	if ((id2 = semget(IPC_PRIVATE, 1, 0600)) == -1) e(0);

	for (i = 0; i <= 1; i++) {
		cmd = (i == 0) ? IPC_INFO : SEM_INFO;

		memset(&seminfo, 0xff, sizeof(seminfo));

		if ((r = semctl(0, 0, cmd, &seminfo)) == -1) e(0);

		/*
		 * These commands return the index of the highest in-use slot
		 * in the semaphore set table.  Bad idea of course, because
		 * that means the value 0 has two potential meanings.  Since we
		 * cannot guarantee that no other running application is using
		 * semaphores, we settle for "at least" tests based on the two
		 * semaphore sets we just created.
		 */
		if (r < 1 || r >= seminfo.semmni) e(0);

		/*
		 * Many of these checks are rather basic because of missing
		 * SEM_UNDO support.  The only difference between IPC_INFO and
		 * SEM_INFO is the meaning of the semusz and semaem fields.
		 */
		if (seminfo.semmap < 0) e(0);
		if (seminfo.semmni < 3 || seminfo.semmni > USHRT_MAX) e(0);
		if (seminfo.semmns < 3 || seminfo.semmns > USHRT_MAX) e(0);
		if (seminfo.semmnu < 0) e(0);
		if (seminfo.semmsl < 3 || seminfo.semmsl > USHRT_MAX) e(0);
		if (seminfo.semopm < 3 || seminfo.semopm > USHRT_MAX) e(0);
		if (seminfo.semume < 0) e(0);
		if (cmd == SEM_INFO) {
			if (seminfo.semusz < 2) e(0);
		} else
			if (seminfo.semusz < 0) e(0);
		if (seminfo.semvmx < 3 || seminfo.semvmx > SHRT_MAX) e(0);
		if (cmd == SEM_INFO) {
			if (seminfo.semaem < 4) e(0);
		} else
			if (seminfo.semaem < 0) e(0);

		if (semctl(INT_MAX, -1, cmd, &seminfo) == -1) e(0);
		if (semctl(-1, INT_MAX, cmd, &seminfo) == -1) e(0);

		if (semctl(0, 0, cmd, NULL) != -1) e(0);
		if (errno != EFAULT) e(0);

		if (semctl(0, 0, cmd, bad_ptr) != -1) e(0);
		if (errno != EFAULT) e(0);

		if (semctl(0, 0, cmd, ((struct seminfo *)bad_ptr) - 1) == -1)
			e(0);
	}

	if (semctl(id2, 0, IPC_RMID) != 0) e(0);

	/*
	 * Finally, test invalid commands.  Well, hopefully invalid commands,
	 * anyway.
	 */
	if (semctl(id, 0, INT_MIN) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, 0, INT_MAX) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, 0, IPC_RMID) != 0) e(0);
}

/*
 * Test SEM_UNDO support.  Right now this functionality is missing altogether.
 * For now, we test that any attempt to use SEM_UNDO fails.
 */
static void
test88d(void)
{
	struct sembuf sop;
	int id;

	subtest = 3;

	if ((id = semget(IPC_PRIVATE, 1, 0600)) == -1) e(0);

	/*
	 * Use an all-ones (but positive) flag field.  This will include
	 * SEM_UNDO, but also tell the IPC server to report no warning.
	 */
	if (!(SHRT_MAX & SEM_UNDO)) e(0);
	sop.sem_num = 0;
	sop.sem_op = 1;
	sop.sem_flg = SHRT_MAX;
	if (semop(id, &sop, 1) != -1) e(0);
	if (errno != EINVAL) e(0);

	if (semctl(id, 0, IPC_RMID) != 0) e(0);
}

enum {
	RESUME_SEMOP,	/* use semop() to resume blocked parties */
	RESUME_SETVAL,	/* use semctl(SETVAL) to resume blocked parties */
	RESUME_SETALL,	/* use semctl(SETALL) to resume blocked parties */
	NR_RESUMES
};

enum {
	MATCH_FIRST,	/* first match completes, blocks second match */
	MATCH_SECOND,	/* first match does not complete, second match does */
	MATCH_KILL,	/* second match completes after first is aborted */
	MATCH_BOTH,	/* first and second match both complete */
	MATCH_CASCADE,	/* completed match in turn causes another match */
	MATCH_ALL,	/* a combination of the last two */
	NR_MATCHES
};

/*
 * Auxiliary child procedure.  The auxiliary children will deadlock until the
 * semaphore set is removed.
 */
static void
test88e_childaux(struct link * parent)
{
	struct sembuf sops[3];
	struct seminfo seminfo;
	int child, id, num;

	child = rcv(parent);
	id = rcv(parent);
	num = rcv(parent);

	memset(sops, 0, sizeof(sops));

	/* These operations are guaranteed to never return successfully. */
	switch (child) {
	case 1:
		sops[0].sem_num = num;
		sops[0].sem_op = 1;
		sops[1].sem_num = num;
		sops[1].sem_op = 0;
		sops[2].sem_num = 0;
		sops[2].sem_op = 1;
		break;
	case 2:
		if (semctl(0, 0, IPC_INFO, &seminfo) == -1) e(0);
		sops[0].sem_num = num;
		sops[0].sem_op = -seminfo.semvmx;
		sops[1].sem_num = num;
		sops[1].sem_op = -seminfo.semvmx;
		sops[2].sem_num = 0;
		sops[2].sem_op = 1;
		break;
	default:
		e(0);
	}

	snd(parent, 0);

	if (semop(id, sops, 3) != -1) e(0);
	if (errno != EIDRM) e(0);
}

/*
 * First child procedure.
 */
static void
test88e_child1(struct link * parent)
{
	struct sembuf sops[3];
	size_t nsops;
	int match, id, expect;

	match = rcv(parent);
	id = rcv(parent);

	/* Start off with some defaults, then refine by match type. */
	memset(sops, 0, sizeof(sops));
	sops[0].sem_num = 2;
	sops[0].sem_op = -1;
	nsops = 2;
	expect = 0;
	switch (match) {
	case MATCH_FIRST:
		sops[1].sem_num = 3;
		sops[1].sem_op = 1;
		break;
	case MATCH_SECOND:
		sops[1].sem_num = 3;
		sops[1].sem_op = -1;
		sops[2].sem_num = 0;
		sops[2].sem_op = 1;
		nsops = 3;
		expect = -1;
		break;
	case MATCH_KILL:
		sops[1].sem_num = 0;
		sops[1].sem_op = 1;
		expect = INT_MIN;
		break;
	case MATCH_BOTH:
	case MATCH_CASCADE:
	case MATCH_ALL:
		sops[1].sem_num = 3;
		sops[1].sem_op = 1;
		break;
	default:
		e(0);
	}

	snd(parent, 0);

	if (semop(id, sops, nsops) != expect) e(0);
	if (expect == -1 && errno != EIDRM) e(0);
}

/*
 * Second child procedure.
 */
static void
test88e_child2(struct link * parent)
{
	struct sembuf sops[2];
	size_t nsops;
	int match, id, expect;

	match = rcv(parent);
	id = rcv(parent);

	/* Start off with some defaults, then refine by match type. */
	memset(sops, 0, sizeof(sops));
	sops[0].sem_num = 2;
	sops[0].sem_op = -1;
	nsops = 2;
	expect = 0;
	switch (match) {
	case MATCH_FIRST:
		sops[1].sem_num = 0;
		sops[1].sem_op = 1;
		expect = -1;
		break;
	case MATCH_SECOND:
	case MATCH_KILL:
		nsops = 1;
		break;
	case MATCH_BOTH:
	case MATCH_ALL:
		sops[1].sem_num = 3;
		sops[1].sem_op = 1;
		break;
	case MATCH_CASCADE:
		sops[0].sem_num = 3;
		nsops = 1;
		break;
	default:
		e(0);
	}

	snd(parent, 0);

	if (semop(id, sops, nsops) != expect) e(0);
	if (expect == -1 && errno != EIDRM) e(0);
}

/*
 * Third child procedure.
 */
static void
test88e_child3(struct link * parent)
{
	struct sembuf sops[1];
	size_t nsops;
	int match, id;

	match = rcv(parent);
	id = rcv(parent);

	/* Things are a bit simpler here. */
	memset(sops, 0, sizeof(sops));
	nsops = 1;
	switch (match) {
	case MATCH_ALL:
		sops[0].sem_num = 3;
		sops[0].sem_op = -2;
		break;
	default:
		e(0);
	}

	snd(parent, 0);

	if (semop(id, sops, nsops) != 0) e(0);
}

/*
 * Perform one test for operations affecting multiple processes.
 */
static void
sub88e(unsigned int match, unsigned int resume, unsigned int aux)
{
	struct link aux1, aux2, child1, child2, child3;
	struct sembuf sop;
	unsigned short val[4];
	int id, inc, aux_zcnt, aux_ncnt;

	/*
	 * For this test we use one single semaphore set, with four semaphores.
	 * The first semaphore is increased in the case that an operation that
	 * should never complete does complete, and thus should stay zero.
	 * Depending on 'aux', the second or third semaphore is used by the
	 * auxiliary children (if any, also depending on 'aux') to deadlock on.
	 * The third and higher semaphores are used in the main operations.
	 */
	if ((id = semget(IPC_PRIVATE, __arraycount(val), 0666)) == -1) e(0);

	aux_zcnt = aux_ncnt = 0;

	/* Start the first auxiliary child if desired, before all others. */
	if (aux & 1) {
		spawn(&aux1, test88e_childaux, DROP_ALL);

		snd(&aux1, 1);
		snd(&aux1, id);
		snd(&aux1, (aux & 4) ? 2 : 1);

		if (rcv(&aux1) != 0) e(0);

		if (aux & 4)
			aux_zcnt++;
	}

	/* Start and configure all children for this specific match test. */
	spawn(&child1, test88e_child1, DROP_ALL);

	snd(&child1, match);
	snd(&child1, id);

	if (rcv(&child1) != 0) e(0);

	/*
	 * For fairness tests, we must ensure that the first child blocks on
	 * the semaphore before the second child does.
	 */
	switch (match) {
	case MATCH_FIRST:
	case MATCH_SECOND:
	case MATCH_KILL:
		usleep(WAIT_USECS);
		break;
	}

	spawn(&child2, test88e_child2, DROP_NONE);

	snd(&child2, match);
	snd(&child2, id);

	if (rcv(&child2) != 0) e(0);

	if (match == MATCH_ALL) {
		spawn(&child3, test88e_child3, DROP_USER);

		snd(&child3, match);
		snd(&child3, id);

		if (rcv(&child3) != 0) e(0);
	}

	/* Start the second auxiliary child if desired, after all others. */
	if (aux & 2) {
		spawn(&aux2, test88e_childaux, DROP_NONE);

		snd(&aux2, 2);
		snd(&aux2, id);
		snd(&aux2, (aux & 4) ? 2 : 1);

		if (rcv(&aux2) != 0) e(0);

		if (aux & 4)
			aux_ncnt++;
	}

	usleep(WAIT_USECS);

	/*
	 * Test semaphore values and determine the value with which to increase
	 * the third semaphore.  For MATCH_KILL, also kill the first child.
	 */
	inc = 1;
	switch (match) {
	case MATCH_FIRST:
	case MATCH_SECOND:
		TEST_SEM(id, 2, 0, 0, 2 + aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, 0, 0, 0);
		break;
	case MATCH_KILL:
		TEST_SEM(id, 2, 0, 0, 2 + aux_ncnt, aux_zcnt);

		terminate(&child1);

		/* As stated before, non-self kills need not be instant. */
		usleep(WAIT_USECS);

		TEST_SEM(id, 2, 0, 0, 1 + aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, 0, 0, 0);
		break;
	case MATCH_BOTH:
		TEST_SEM(id, 2, 0, 0, 2 + aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, 0, 0, 0);
		inc = 2;
		break;
	case MATCH_CASCADE:
		TEST_SEM(id, 2, 0, 0, 1 + aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, 0, 1, 0);
		break;
	case MATCH_ALL:
		TEST_SEM(id, 2, 0, 0, 2 + aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, 0, 1, 0);
		inc = 2;
		break;
	default:
		e(0);
	}

	TEST_SEM(id, 0, 0, 0, 0, 0);
	TEST_SEM(id, 1, 0, 0, -1, -1);

	/* Resume the appropriate set of children. */
	switch (resume) {
	case RESUME_SEMOP:
		memset(&sop, 0, sizeof(sop));
		sop.sem_num = 2;
		sop.sem_op = inc;
		if (semop(id, &sop, 1) != 0) e(0);
		break;
	case RESUME_SETVAL:
		if (semctl(id, 2, SETVAL, inc) != 0) e(0);
		break;
	case RESUME_SETALL:
		memset(val, 0, sizeof(val));
		val[2] = inc;
		if (semctl(id, 0, SETALL, val) != 0) e(0);
		break;
	default:
		e(0);
	}

	/*
	 * See if the right children were indeed resumed, and retest the
	 * semaphore values.
	 */
	switch (match) {
	case MATCH_FIRST:
		TEST_SEM(id, 2, 0, child1.pid, 1 + aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 1, child1.pid, 0, 0);
		collect(&child1);
		break;
	case MATCH_SECOND:
		TEST_SEM(id, 2, 0, child2.pid, 1 + aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, 0, 0, 0);
		collect(&child2);
		break;
	case MATCH_KILL:
		TEST_SEM(id, 2, 0, child2.pid, aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, 0, 0, 0);
		collect(&child2);
		break;
	case MATCH_BOTH:
		/*
		 * The children are not ordered in this case, so we do not know
		 * which one gets access to the semaphores last.
		 */
		TEST_SEM(id, 2, 0, -1, aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 2, -1, 0, 0);
		collect(&child1);
		collect(&child2);
		break;
	case MATCH_CASCADE:
		TEST_SEM(id, 2, 0, child1.pid, aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, child2.pid, 0, 0);
		collect(&child1);
		collect(&child2);
		break;
	case MATCH_ALL:
		TEST_SEM(id, 2, 0, -1, aux_ncnt, aux_zcnt);
		TEST_SEM(id, 3, 0, child3.pid, 0, 0);
		collect(&child1);
		collect(&child2);
		collect(&child3);
		break;
	default:
		e(0);
	}

	TEST_SEM(id, 0, 0, 0, 0, 0);
	TEST_SEM(id, 1, 0, 0, -1, -1);

	/* Remove the semaphore set.  This should unblock remaining callers. */
	if (semctl(id, 0, IPC_RMID) != 0) e(0);

	/* Wait for the children that were not resumed, but should be now. */
	switch (match) {
	case MATCH_FIRST:
		collect(&child2);
		break;
	case MATCH_SECOND:
		collect(&child1);
		break;
	case MATCH_KILL:
	case MATCH_BOTH:
	case MATCH_CASCADE:
	case MATCH_ALL:
		break;
	default:
		e(0);
	}

	/* Wait for the auxiliary children as well. */
	if (aux & 1)
		collect(&aux1);
	if (aux & 2)
		collect(&aux2);
}

/*
 * Test operations affecting multiple processes, ensuring the following points:
 * 1) an operation resumes all possible waiters; 2) a resumed operation in turn
 * correctly resumes other now-unblocked operations; 3) a basic level of FIFO
 * fairness is provided between blocked parties; 4) all the previous points are
 * unaffected by additional waiters that are not being resumed; 5) identifier
 * removal properly resumes all affected waiters.
 */
static void
test88e(void)
{
	unsigned int resume, match, aux;

	subtest = 4;

	for (match = 0; match < NR_MATCHES; match++)
		for (resume = 0; resume < NR_RESUMES; resume++)
			for (aux = 1; aux <= 8; aux++) /* 0 and 4 are equal */
				sub88e(match, resume, aux);
}

/*
 * Verify that non-root processes can use sysctl(2) to see semaphore sets
 * created by root.
 */
static void
test88f_child(struct link * parent)
{
	static const int mib[] = { CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO,
	    KERN_SYSVIPC_SEM_INFO };
	struct sem_sysctl_info *semsi;
	size_t len;
	int id[2], id2, seen[2];
	int32_t i;

	id[0] = rcv(parent);
	id[1] = rcv(parent);

	if (sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0) != 0) e(0);

	if ((semsi = malloc(len)) == NULL) e(0);

	if (sysctl(mib, __arraycount(mib), semsi, &len, NULL, 0) != 0) e(0);

	seen[0] = seen[1] = 0;
	for (i = 0; i < semsi->seminfo.semmni; i++) {
		if (!(semsi->semids[i].sem_perm.mode & SEM_ALLOC))
			continue;

		id2 = IXSEQ_TO_IPCID(i, semsi->semids[i].sem_perm);
		if (id2 == id[0])
			seen[0]++;
		else if (id2 == id[1])
			seen[1]++;
	}

	free(semsi);

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

/*
 * Test sysctl(2) based information retrieval.  This test aims to ensure that
 * in particular ipcs(1) and ipcrm(1) will be able to do their jobs.
 */
static void
test88f(void)
{
	static const int mib[] = { CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_INFO,
	    KERN_SYSVIPC_SEM_INFO };
	struct seminfo seminfo, seminfo2;
	struct sem_sysctl_info *semsi;
	struct semid_ds_sysctl *semds;
	struct link child;
	size_t len, size;
	int id[2], id2;
	int32_t i, slot[2];

	/*
	 * Verify that we can retrieve only the general semaphore information,
	 * without any actual semaphore set entries.  This is actually a dirty
	 * sysctl-level hack, as sysctl requests should not behave differently
	 * based on the requested length.  However, ipcs(1) relies on this.
	 */
	len = sizeof(seminfo);
	if (sysctl(mib, __arraycount(mib), &seminfo, &len, NULL, 0) != 0) e(0);
	if (len != sizeof(seminfo)) e(0);

	if (semctl(0, 0, IPC_INFO, &seminfo2) == -1) e(0);

	if (memcmp(&seminfo, &seminfo2, sizeof(seminfo)) != 0) e(0);

	/* Verify that the correct size estimation is returned. */
	if (seminfo.semmni <= 0) e(0);
	if (seminfo.semmni > SHRT_MAX) e(0);

	size = sizeof(*semsi) +
	    sizeof(semsi->semids[0]) * (seminfo.semmni - 1);

	len = 0;
	if (sysctl(mib, __arraycount(mib), NULL, &len, NULL, 0) != 0) e(0);
	if (len != size) e(0);

	/* Create two semaphore sets that should show up in the listing. */
	if ((id[0] = semget(KEY_A, 5, IPC_CREAT | 0612)) < 0) e(0);

	if ((id[1] = semget(IPC_PRIVATE, 3, 0650)) < 0) e(0);

	/*
	 * Retrieve the entire semaphore array, and verify that the general
	 * semaphore information is still correct.
	 */
	if ((semsi = malloc(size)) == NULL) e(0);

	len = size;
	if (sysctl(mib, __arraycount(mib), semsi, &len, NULL, 0) != 0) e(0);
	if (len != size) e(0);

	if (sizeof(semsi->seminfo) != sizeof(seminfo)) e(0);
	if (memcmp(&semsi->seminfo, &seminfo, sizeof(semsi->seminfo)) != 0)
	    e(0);

	/* Verify that our semaphore sets are each in the array once. */
	slot[0] = slot[1] = -1;
	for (i = 0; i < seminfo.semmni; i++) {
		if (!(semsi->semids[i].sem_perm.mode & SEM_ALLOC))
			continue;

		id2 = IXSEQ_TO_IPCID(i, semsi->semids[i].sem_perm);
		if (id2 == id[0]) {
			if (slot[0] != -1) e(0);
			slot[0] = i;
		} else if (id2 == id[1]) {
			if (slot[1] != -1) e(0);
			slot[1] = i;
		}
	}

	if (slot[0] < 0) e(0);
	if (slot[1] < 0) e(0);

	/* Check that the semaphore sets have the expected properties. */
	semds = &semsi->semids[slot[0]];
	if (semds->sem_perm.uid != geteuid()) e(0);
	if (semds->sem_perm.gid != getegid()) e(0);
	if (semds->sem_perm.cuid != geteuid()) e(0);
	if (semds->sem_perm.cgid != getegid()) e(0);
	if (semds->sem_perm.mode != (SEM_ALLOC | 0612)) e(0);
	if (semds->sem_perm._key != KEY_A) e(0);
	if (semds->sem_nsems != 5) e(0);
	if (semds->sem_otime != 0) e(0);
	if (semds->sem_ctime == 0) e(0);

	semds = &semsi->semids[slot[1]];
	if (semds->sem_perm.uid != geteuid()) e(0);
	if (semds->sem_perm.gid != getegid()) e(0);
	if (semds->sem_perm.cuid != geteuid()) e(0);
	if (semds->sem_perm.cgid != getegid()) e(0);
	if (semds->sem_perm.mode != (SEM_ALLOC | 0650)) e(0);
	if (semds->sem_perm._key != IPC_PRIVATE) e(0);
	if (semds->sem_nsems != 3) e(0);
	if (semds->sem_otime != 0) e(0);
	if (semds->sem_ctime == 0) e(0);

	/* Make sure that non-root users can see them as well. */
	spawn(&child, test88f_child, DROP_ALL);

	snd(&child, id[0]);
	snd(&child, id[1]);

	collect(&child);

	/* Clean up, and verify that the sets are no longer in the listing. */
	if (semctl(id[0], 0, IPC_RMID) != 0) e(0);
	if (semctl(id[1], 0, IPC_RMID) != 0) e(0);

	len = size;
	if (sysctl(mib, __arraycount(mib), semsi, &len, NULL, 0) != 0) e(0);
	if (len != size) e(0);

	for (i = 0; i < seminfo.semmni; i++) {
		if (!(semsi->semids[i].sem_perm.mode & SEM_ALLOC))
			continue;

		id2 = IXSEQ_TO_IPCID(i, semsi->semids[i].sem_perm);
		if (id2 == id[0]) e(0);
		if (id2 == id[1]) e(0);
	}

	free(semsi);
}

/*
 * Initialize the test.
 */
static void
test88_init(void)
{
	static const int mib[] = { CTL_KERN, KERN_SYSVIPC, KERN_SYSVIPC_SEM };
	struct group *gr;
	size_t len;
	int i;

	/* Start with full root privileges. */
	setuid(geteuid());

	if ((gr = getgrnam(ROOT_GROUP)) == NULL) e(0);

	setgid(gr->gr_gid);
	setegid(gr->gr_gid);

	/*
	 * Verify that the IPC service is running at all.  If not, there is
	 * obviously no point in running this test.
	 */
	len = sizeof(i);
	if (sysctl(mib, __arraycount(mib), &i, &len, NULL, 0) != 0) e(0);
	if (len != sizeof(i)) e(0);

	if (i == 0) {
		printf("skipped\n");
		cleanup();
		exit(0);
	}

	/* Allocate a memory page followed by an unmapped page. */
	page_size = getpagesize();
	page_ptr = mmap(NULL, page_size * 2, PROT_READ | PROT_WRITE,
	    MAP_ANON | MAP_PRIVATE, -1, 0);
	if (page_ptr == MAP_FAILED) e(0);
	bad_ptr = page_ptr + page_size;
	if (munmap(bad_ptr, page_size) != 0) e(0);
}

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

	start(88);

	test88_init();

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

	for (i = 0; i < ITERATIONS; i++) {
		if (m & 0x01) test88a();
		if (m & 0x02) test88b();
		if (m & 0x04) test88c();
		if (m & 0x08) test88d();
		if (m & 0x10) test88e();
		if (m & 0x20) test88f();
	}

	quit();
}