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
|
/*-
* Copyright (c) 2005 David Xu <davidxu@freebsd.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*/
/*
* POSIX message queue implementation.
*
* 1) A mqueue filesystem can be mounted, each message queue appears
* in mounted directory, user can change queue's permission and
* ownership, or remove a queue. Manually creating a file in the
* directory causes a message queue to be created in the kernel with
* default message queue attributes applied and same name used, this
* method is not advocated since mq_open syscall allows user to specify
* different attributes. Also the file system can be mounted multiple
* times at different mount points but shows same contents.
*
* 2) Standard POSIX message queue API. The syscalls do not use vfs layer,
* but directly operate on internal data structure, this allows user to
* use the IPC facility without having to mount mqueue file system.
*/
#include <sys/cdefs.h>
__FBSDID("$FreeBSD$");
#include "opt_compat.h"
#include <sys/param.h>
#include <sys/kernel.h>
#include <sys/systm.h>
#include <sys/limits.h>
#include <sys/buf.h>
#include <sys/capability.h>
#include <sys/dirent.h>
#include <sys/event.h>
#include <sys/eventhandler.h>
#include <sys/fcntl.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/lock.h>
#include <sys/malloc.h>
#include <sys/module.h>
#include <sys/mount.h>
#include <sys/mqueue.h>
#include <sys/mutex.h>
#include <sys/namei.h>
#include <sys/posix4.h>
#include <sys/poll.h>
#include <sys/priv.h>
#include <sys/proc.h>
#include <sys/queue.h>
#include <sys/sysproto.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/syscallsubr.h>
#include <sys/sysent.h>
#include <sys/sx.h>
#include <sys/sysctl.h>
#include <sys/taskqueue.h>
#include <sys/unistd.h>
#include <sys/vnode.h>
#include <machine/atomic.h>
FEATURE(p1003_1b_mqueue, "POSIX P1003.1B message queues support");
/*
* Limits and constants
*/
#define MQFS_NAMELEN NAME_MAX
#define MQFS_DELEN (8 + MQFS_NAMELEN)
/* node types */
typedef enum {
mqfstype_none = 0,
mqfstype_root,
mqfstype_dir,
mqfstype_this,
mqfstype_parent,
mqfstype_file,
mqfstype_symlink,
} mqfs_type_t;
struct mqfs_node;
/*
* mqfs_info: describes a mqfs instance
*/
struct mqfs_info {
struct sx mi_lock;
struct mqfs_node *mi_root;
struct unrhdr *mi_unrhdr;
};
struct mqfs_vdata {
LIST_ENTRY(mqfs_vdata) mv_link;
struct mqfs_node *mv_node;
struct vnode *mv_vnode;
struct task mv_task;
};
/*
* mqfs_node: describes a node (file or directory) within a mqfs
*/
struct mqfs_node {
char mn_name[MQFS_NAMELEN+1];
struct mqfs_info *mn_info;
struct mqfs_node *mn_parent;
LIST_HEAD(,mqfs_node) mn_children;
LIST_ENTRY(mqfs_node) mn_sibling;
LIST_HEAD(,mqfs_vdata) mn_vnodes;
int mn_refcount;
mqfs_type_t mn_type;
int mn_deleted;
uint32_t mn_fileno;
void *mn_data;
struct timespec mn_birth;
struct timespec mn_ctime;
struct timespec mn_atime;
struct timespec mn_mtime;
uid_t mn_uid;
gid_t mn_gid;
int mn_mode;
};
#define VTON(vp) (((struct mqfs_vdata *)((vp)->v_data))->mv_node)
#define VTOMQ(vp) ((struct mqueue *)(VTON(vp)->mn_data))
#define VFSTOMQFS(m) ((struct mqfs_info *)((m)->mnt_data))
#define FPTOMQ(fp) ((struct mqueue *)(((struct mqfs_node *) \
(fp)->f_data)->mn_data))
TAILQ_HEAD(msgq, mqueue_msg);
struct mqueue;
struct mqueue_notifier {
LIST_ENTRY(mqueue_notifier) nt_link;
struct sigevent nt_sigev;
ksiginfo_t nt_ksi;
struct proc *nt_proc;
};
struct mqueue {
struct mtx mq_mutex;
int mq_flags;
long mq_maxmsg;
long mq_msgsize;
long mq_curmsgs;
long mq_totalbytes;
struct msgq mq_msgq;
int mq_receivers;
int mq_senders;
struct selinfo mq_rsel;
struct selinfo mq_wsel;
struct mqueue_notifier *mq_notifier;
};
#define MQ_RSEL 0x01
#define MQ_WSEL 0x02
struct mqueue_msg {
TAILQ_ENTRY(mqueue_msg) msg_link;
unsigned int msg_prio;
unsigned int msg_size;
/* following real data... */
};
static SYSCTL_NODE(_kern, OID_AUTO, mqueue, CTLFLAG_RW, 0,
"POSIX real time message queue");
static int default_maxmsg = 10;
static int default_msgsize = 1024;
static int maxmsg = 100;
SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsg, CTLFLAG_RW,
&maxmsg, 0, "Default maximum messages in queue");
static int maxmsgsize = 16384;
SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmsgsize, CTLFLAG_RW,
&maxmsgsize, 0, "Default maximum message size");
static int maxmq = 100;
SYSCTL_INT(_kern_mqueue, OID_AUTO, maxmq, CTLFLAG_RW,
&maxmq, 0, "maximum message queues");
static int curmq = 0;
SYSCTL_INT(_kern_mqueue, OID_AUTO, curmq, CTLFLAG_RW,
&curmq, 0, "current message queue number");
static int unloadable = 0;
static MALLOC_DEFINE(M_MQUEUEDATA, "mqdata", "mqueue data");
static eventhandler_tag exit_tag;
/* Only one instance per-system */
static struct mqfs_info mqfs_data;
static uma_zone_t mqnode_zone;
static uma_zone_t mqueue_zone;
static uma_zone_t mvdata_zone;
static uma_zone_t mqnoti_zone;
static struct vop_vector mqfs_vnodeops;
static struct fileops mqueueops;
/*
* Directory structure construction and manipulation
*/
#ifdef notyet
static struct mqfs_node *mqfs_create_dir(struct mqfs_node *parent,
const char *name, int namelen, struct ucred *cred, int mode);
static struct mqfs_node *mqfs_create_link(struct mqfs_node *parent,
const char *name, int namelen, struct ucred *cred, int mode);
#endif
static struct mqfs_node *mqfs_create_file(struct mqfs_node *parent,
const char *name, int namelen, struct ucred *cred, int mode);
static int mqfs_destroy(struct mqfs_node *mn);
static void mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn);
static void mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn);
static int mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn);
/*
* Message queue construction and maniplation
*/
static struct mqueue *mqueue_alloc(const struct mq_attr *attr);
static void mqueue_free(struct mqueue *mq);
static int mqueue_send(struct mqueue *mq, const char *msg_ptr,
size_t msg_len, unsigned msg_prio, int waitok,
const struct timespec *abs_timeout);
static int mqueue_receive(struct mqueue *mq, char *msg_ptr,
size_t msg_len, unsigned *msg_prio, int waitok,
const struct timespec *abs_timeout);
static int _mqueue_send(struct mqueue *mq, struct mqueue_msg *msg,
int timo);
static int _mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg,
int timo);
static void mqueue_send_notification(struct mqueue *mq);
static void mqueue_fdclose(struct thread *td, int fd, struct file *fp);
static void mq_proc_exit(void *arg, struct proc *p);
/*
* kqueue filters
*/
static void filt_mqdetach(struct knote *kn);
static int filt_mqread(struct knote *kn, long hint);
static int filt_mqwrite(struct knote *kn, long hint);
struct filterops mq_rfiltops = {
.f_isfd = 1,
.f_detach = filt_mqdetach,
.f_event = filt_mqread,
};
struct filterops mq_wfiltops = {
.f_isfd = 1,
.f_detach = filt_mqdetach,
.f_event = filt_mqwrite,
};
/*
* Initialize fileno bitmap
*/
static void
mqfs_fileno_init(struct mqfs_info *mi)
{
struct unrhdr *up;
up = new_unrhdr(1, INT_MAX, NULL);
mi->mi_unrhdr = up;
}
/*
* Tear down fileno bitmap
*/
static void
mqfs_fileno_uninit(struct mqfs_info *mi)
{
struct unrhdr *up;
up = mi->mi_unrhdr;
mi->mi_unrhdr = NULL;
delete_unrhdr(up);
}
/*
* Allocate a file number
*/
static void
mqfs_fileno_alloc(struct mqfs_info *mi, struct mqfs_node *mn)
{
/* make sure our parent has a file number */
if (mn->mn_parent && !mn->mn_parent->mn_fileno)
mqfs_fileno_alloc(mi, mn->mn_parent);
switch (mn->mn_type) {
case mqfstype_root:
case mqfstype_dir:
case mqfstype_file:
case mqfstype_symlink:
mn->mn_fileno = alloc_unr(mi->mi_unrhdr);
break;
case mqfstype_this:
KASSERT(mn->mn_parent != NULL,
("mqfstype_this node has no parent"));
mn->mn_fileno = mn->mn_parent->mn_fileno;
break;
case mqfstype_parent:
KASSERT(mn->mn_parent != NULL,
("mqfstype_parent node has no parent"));
if (mn->mn_parent == mi->mi_root) {
mn->mn_fileno = mn->mn_parent->mn_fileno;
break;
}
KASSERT(mn->mn_parent->mn_parent != NULL,
("mqfstype_parent node has no grandparent"));
mn->mn_fileno = mn->mn_parent->mn_parent->mn_fileno;
break;
default:
KASSERT(0,
("mqfs_fileno_alloc() called for unknown type node: %d",
mn->mn_type));
break;
}
}
/*
* Release a file number
*/
static void
mqfs_fileno_free(struct mqfs_info *mi, struct mqfs_node *mn)
{
switch (mn->mn_type) {
case mqfstype_root:
case mqfstype_dir:
case mqfstype_file:
case mqfstype_symlink:
free_unr(mi->mi_unrhdr, mn->mn_fileno);
break;
case mqfstype_this:
case mqfstype_parent:
/* ignore these, as they don't "own" their file number */
break;
default:
KASSERT(0,
("mqfs_fileno_free() called for unknown type node: %d",
mn->mn_type));
break;
}
}
static __inline struct mqfs_node *
mqnode_alloc(void)
{
return uma_zalloc(mqnode_zone, M_WAITOK | M_ZERO);
}
static __inline void
mqnode_free(struct mqfs_node *node)
{
uma_zfree(mqnode_zone, node);
}
static __inline void
mqnode_addref(struct mqfs_node *node)
{
atomic_fetchadd_int(&node->mn_refcount, 1);
}
static __inline void
mqnode_release(struct mqfs_node *node)
{
struct mqfs_info *mqfs;
int old, exp;
mqfs = node->mn_info;
old = atomic_fetchadd_int(&node->mn_refcount, -1);
if (node->mn_type == mqfstype_dir ||
node->mn_type == mqfstype_root)
exp = 3; /* include . and .. */
else
exp = 1;
if (old == exp) {
int locked = sx_xlocked(&mqfs->mi_lock);
if (!locked)
sx_xlock(&mqfs->mi_lock);
mqfs_destroy(node);
if (!locked)
sx_xunlock(&mqfs->mi_lock);
}
}
/*
* Add a node to a directory
*/
static int
mqfs_add_node(struct mqfs_node *parent, struct mqfs_node *node)
{
KASSERT(parent != NULL, ("%s(): parent is NULL", __func__));
KASSERT(parent->mn_info != NULL,
("%s(): parent has no mn_info", __func__));
KASSERT(parent->mn_type == mqfstype_dir ||
parent->mn_type == mqfstype_root,
("%s(): parent is not a directory", __func__));
node->mn_info = parent->mn_info;
node->mn_parent = parent;
LIST_INIT(&node->mn_children);
LIST_INIT(&node->mn_vnodes);
LIST_INSERT_HEAD(&parent->mn_children, node, mn_sibling);
mqnode_addref(parent);
return (0);
}
static struct mqfs_node *
mqfs_create_node(const char *name, int namelen, struct ucred *cred, int mode,
int nodetype)
{
struct mqfs_node *node;
node = mqnode_alloc();
strncpy(node->mn_name, name, namelen);
node->mn_type = nodetype;
node->mn_refcount = 1;
vfs_timestamp(&node->mn_birth);
node->mn_ctime = node->mn_atime = node->mn_mtime
= node->mn_birth;
node->mn_uid = cred->cr_uid;
node->mn_gid = cred->cr_gid;
node->mn_mode = mode;
return (node);
}
/*
* Create a file
*/
static struct mqfs_node *
mqfs_create_file(struct mqfs_node *parent, const char *name, int namelen,
struct ucred *cred, int mode)
{
struct mqfs_node *node;
node = mqfs_create_node(name, namelen, cred, mode, mqfstype_file);
if (mqfs_add_node(parent, node) != 0) {
mqnode_free(node);
return (NULL);
}
return (node);
}
/*
* Add . and .. to a directory
*/
static int
mqfs_fixup_dir(struct mqfs_node *parent)
{
struct mqfs_node *dir;
dir = mqnode_alloc();
dir->mn_name[0] = '.';
dir->mn_type = mqfstype_this;
dir->mn_refcount = 1;
if (mqfs_add_node(parent, dir) != 0) {
mqnode_free(dir);
return (-1);
}
dir = mqnode_alloc();
dir->mn_name[0] = dir->mn_name[1] = '.';
dir->mn_type = mqfstype_parent;
dir->mn_refcount = 1;
if (mqfs_add_node(parent, dir) != 0) {
mqnode_free(dir);
return (-1);
}
return (0);
}
#ifdef notyet
/*
* Create a directory
*/
static struct mqfs_node *
mqfs_create_dir(struct mqfs_node *parent, const char *name, int namelen,
struct ucred *cred, int mode)
{
struct mqfs_node *node;
node = mqfs_create_node(name, namelen, cred, mode, mqfstype_dir);
if (mqfs_add_node(parent, node) != 0) {
mqnode_free(node);
return (NULL);
}
if (mqfs_fixup_dir(node) != 0) {
mqfs_destroy(node);
return (NULL);
}
return (node);
}
/*
* Create a symlink
*/
static struct mqfs_node *
mqfs_create_link(struct mqfs_node *parent, const char *name, int namelen,
struct ucred *cred, int mode)
{
struct mqfs_node *node;
node = mqfs_create_node(name, namelen, cred, mode, mqfstype_symlink);
if (mqfs_add_node(parent, node) != 0) {
mqnode_free(node);
return (NULL);
}
return (node);
}
#endif
/*
* Destroy a node or a tree of nodes
*/
static int
mqfs_destroy(struct mqfs_node *node)
{
struct mqfs_node *parent;
KASSERT(node != NULL,
("%s(): node is NULL", __func__));
KASSERT(node->mn_info != NULL,
("%s(): node has no mn_info", __func__));
/* destroy children */
if (node->mn_type == mqfstype_dir || node->mn_type == mqfstype_root)
while (! LIST_EMPTY(&node->mn_children))
mqfs_destroy(LIST_FIRST(&node->mn_children));
/* unlink from parent */
if ((parent = node->mn_parent) != NULL) {
KASSERT(parent->mn_info == node->mn_info,
("%s(): parent has different mn_info", __func__));
LIST_REMOVE(node, mn_sibling);
}
if (node->mn_fileno != 0)
mqfs_fileno_free(node->mn_info, node);
if (node->mn_data != NULL)
mqueue_free(node->mn_data);
mqnode_free(node);
return (0);
}
/*
* Mount a mqfs instance
*/
static int
mqfs_mount(struct mount *mp)
{
struct statfs *sbp;
if (mp->mnt_flag & MNT_UPDATE)
return (EOPNOTSUPP);
mp->mnt_data = &mqfs_data;
MNT_ILOCK(mp);
mp->mnt_flag |= MNT_LOCAL;
mp->mnt_kern_flag |= MNTK_MPSAFE;
MNT_IUNLOCK(mp);
vfs_getnewfsid(mp);
sbp = &mp->mnt_stat;
vfs_mountedfrom(mp, "mqueue");
sbp->f_bsize = PAGE_SIZE;
sbp->f_iosize = PAGE_SIZE;
sbp->f_blocks = 1;
sbp->f_bfree = 0;
sbp->f_bavail = 0;
sbp->f_files = 1;
sbp->f_ffree = 0;
return (0);
}
/*
* Unmount a mqfs instance
*/
static int
mqfs_unmount(struct mount *mp, int mntflags)
{
int error;
error = vflush(mp, 0, (mntflags & MNT_FORCE) ? FORCECLOSE : 0,
curthread);
return (error);
}
/*
* Return a root vnode
*/
static int
mqfs_root(struct mount *mp, int flags, struct vnode **vpp)
{
struct mqfs_info *mqfs;
int ret;
mqfs = VFSTOMQFS(mp);
ret = mqfs_allocv(mp, vpp, mqfs->mi_root);
return (ret);
}
/*
* Return filesystem stats
*/
static int
mqfs_statfs(struct mount *mp, struct statfs *sbp)
{
/* XXX update statistics */
return (0);
}
/*
* Initialize a mqfs instance
*/
static int
mqfs_init(struct vfsconf *vfc)
{
struct mqfs_node *root;
struct mqfs_info *mi;
mqnode_zone = uma_zcreate("mqnode", sizeof(struct mqfs_node),
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
mqueue_zone = uma_zcreate("mqueue", sizeof(struct mqueue),
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
mvdata_zone = uma_zcreate("mvdata",
sizeof(struct mqfs_vdata), NULL, NULL, NULL,
NULL, UMA_ALIGN_PTR, 0);
mqnoti_zone = uma_zcreate("mqnotifier", sizeof(struct mqueue_notifier),
NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
mi = &mqfs_data;
sx_init(&mi->mi_lock, "mqfs lock");
/* set up the root diretory */
root = mqfs_create_node("/", 1, curthread->td_ucred, 01777,
mqfstype_root);
root->mn_info = mi;
LIST_INIT(&root->mn_children);
LIST_INIT(&root->mn_vnodes);
mi->mi_root = root;
mqfs_fileno_init(mi);
mqfs_fileno_alloc(mi, root);
mqfs_fixup_dir(root);
exit_tag = EVENTHANDLER_REGISTER(process_exit, mq_proc_exit, NULL,
EVENTHANDLER_PRI_ANY);
mq_fdclose = mqueue_fdclose;
p31b_setcfg(CTL_P1003_1B_MESSAGE_PASSING, _POSIX_MESSAGE_PASSING);
return (0);
}
/*
* Destroy a mqfs instance
*/
static int
mqfs_uninit(struct vfsconf *vfc)
{
struct mqfs_info *mi;
if (!unloadable)
return (EOPNOTSUPP);
EVENTHANDLER_DEREGISTER(process_exit, exit_tag);
mi = &mqfs_data;
mqfs_destroy(mi->mi_root);
mi->mi_root = NULL;
mqfs_fileno_uninit(mi);
sx_destroy(&mi->mi_lock);
uma_zdestroy(mqnode_zone);
uma_zdestroy(mqueue_zone);
uma_zdestroy(mvdata_zone);
uma_zdestroy(mqnoti_zone);
return (0);
}
/*
* task routine
*/
static void
do_recycle(void *context, int pending __unused)
{
struct vnode *vp = (struct vnode *)context;
vrecycle(vp, curthread);
vdrop(vp);
}
/*
* Allocate a vnode
*/
static int
mqfs_allocv(struct mount *mp, struct vnode **vpp, struct mqfs_node *pn)
{
struct mqfs_vdata *vd;
struct mqfs_info *mqfs;
struct vnode *newvpp;
int error;
mqfs = pn->mn_info;
*vpp = NULL;
sx_xlock(&mqfs->mi_lock);
LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
if (vd->mv_vnode->v_mount == mp) {
vhold(vd->mv_vnode);
break;
}
}
if (vd != NULL) {
found:
*vpp = vd->mv_vnode;
sx_xunlock(&mqfs->mi_lock);
error = vget(*vpp, LK_RETRY | LK_EXCLUSIVE, curthread);
vdrop(*vpp);
return (error);
}
sx_xunlock(&mqfs->mi_lock);
error = getnewvnode("mqueue", mp, &mqfs_vnodeops, &newvpp);
if (error)
return (error);
vn_lock(newvpp, LK_EXCLUSIVE | LK_RETRY);
error = insmntque(newvpp, mp);
if (error != 0)
return (error);
sx_xlock(&mqfs->mi_lock);
/*
* Check if it has already been allocated
* while we were blocked.
*/
LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
if (vd->mv_vnode->v_mount == mp) {
vhold(vd->mv_vnode);
sx_xunlock(&mqfs->mi_lock);
vgone(newvpp);
vput(newvpp);
goto found;
}
}
*vpp = newvpp;
vd = uma_zalloc(mvdata_zone, M_WAITOK);
(*vpp)->v_data = vd;
vd->mv_vnode = *vpp;
vd->mv_node = pn;
TASK_INIT(&vd->mv_task, 0, do_recycle, *vpp);
LIST_INSERT_HEAD(&pn->mn_vnodes, vd, mv_link);
mqnode_addref(pn);
switch (pn->mn_type) {
case mqfstype_root:
(*vpp)->v_vflag = VV_ROOT;
/* fall through */
case mqfstype_dir:
case mqfstype_this:
case mqfstype_parent:
(*vpp)->v_type = VDIR;
break;
case mqfstype_file:
(*vpp)->v_type = VREG;
break;
case mqfstype_symlink:
(*vpp)->v_type = VLNK;
break;
case mqfstype_none:
KASSERT(0, ("mqfs_allocf called for null node\n"));
default:
panic("%s has unexpected type: %d", pn->mn_name, pn->mn_type);
}
sx_xunlock(&mqfs->mi_lock);
return (0);
}
/*
* Search a directory entry
*/
static struct mqfs_node *
mqfs_search(struct mqfs_node *pd, const char *name, int len)
{
struct mqfs_node *pn;
sx_assert(&pd->mn_info->mi_lock, SX_LOCKED);
LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
if (strncmp(pn->mn_name, name, len) == 0 &&
pn->mn_name[len] == '\0')
return (pn);
}
return (NULL);
}
/*
* Look up a file or directory.
*/
static int
mqfs_lookupx(struct vop_cachedlookup_args *ap)
{
struct componentname *cnp;
struct vnode *dvp, **vpp;
struct mqfs_node *pd;
struct mqfs_node *pn;
struct mqfs_info *mqfs;
int nameiop, flags, error, namelen;
char *pname;
struct thread *td;
cnp = ap->a_cnp;
vpp = ap->a_vpp;
dvp = ap->a_dvp;
pname = cnp->cn_nameptr;
namelen = cnp->cn_namelen;
td = cnp->cn_thread;
flags = cnp->cn_flags;
nameiop = cnp->cn_nameiop;
pd = VTON(dvp);
pn = NULL;
mqfs = pd->mn_info;
*vpp = NULLVP;
if (dvp->v_type != VDIR)
return (ENOTDIR);
error = VOP_ACCESS(dvp, VEXEC, cnp->cn_cred, cnp->cn_thread);
if (error)
return (error);
/* shortcut: check if the name is too long */
if (cnp->cn_namelen >= MQFS_NAMELEN)
return (ENOENT);
/* self */
if (namelen == 1 && pname[0] == '.') {
if ((flags & ISLASTCN) && nameiop != LOOKUP)
return (EINVAL);
pn = pd;
*vpp = dvp;
VREF(dvp);
return (0);
}
/* parent */
if (cnp->cn_flags & ISDOTDOT) {
if (dvp->v_vflag & VV_ROOT)
return (EIO);
if ((flags & ISLASTCN) && nameiop != LOOKUP)
return (EINVAL);
VOP_UNLOCK(dvp, 0);
KASSERT(pd->mn_parent, ("non-root directory has no parent"));
pn = pd->mn_parent;
error = mqfs_allocv(dvp->v_mount, vpp, pn);
vn_lock(dvp, LK_EXCLUSIVE | LK_RETRY);
return (error);
}
/* named node */
sx_xlock(&mqfs->mi_lock);
pn = mqfs_search(pd, pname, namelen);
if (pn != NULL)
mqnode_addref(pn);
sx_xunlock(&mqfs->mi_lock);
/* found */
if (pn != NULL) {
/* DELETE */
if (nameiop == DELETE && (flags & ISLASTCN)) {
error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
if (error) {
mqnode_release(pn);
return (error);
}
if (*vpp == dvp) {
VREF(dvp);
*vpp = dvp;
mqnode_release(pn);
return (0);
}
}
/* allocate vnode */
error = mqfs_allocv(dvp->v_mount, vpp, pn);
mqnode_release(pn);
if (error == 0 && cnp->cn_flags & MAKEENTRY)
cache_enter(dvp, *vpp, cnp);
return (error);
}
/* not found */
/* will create a new entry in the directory ? */
if ((nameiop == CREATE || nameiop == RENAME) && (flags & LOCKPARENT)
&& (flags & ISLASTCN)) {
error = VOP_ACCESS(dvp, VWRITE, cnp->cn_cred, td);
if (error)
return (error);
cnp->cn_flags |= SAVENAME;
return (EJUSTRETURN);
}
return (ENOENT);
}
#if 0
struct vop_lookup_args {
struct vop_generic_args a_gen;
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
};
#endif
/*
* vnode lookup operation
*/
static int
mqfs_lookup(struct vop_cachedlookup_args *ap)
{
int rc;
rc = mqfs_lookupx(ap);
return (rc);
}
#if 0
struct vop_create_args {
struct vnode *a_dvp;
struct vnode **a_vpp;
struct componentname *a_cnp;
struct vattr *a_vap;
};
#endif
/*
* vnode creation operation
*/
static int
mqfs_create(struct vop_create_args *ap)
{
struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
struct componentname *cnp = ap->a_cnp;
struct mqfs_node *pd;
struct mqfs_node *pn;
struct mqueue *mq;
int error;
pd = VTON(ap->a_dvp);
if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
return (ENOTDIR);
mq = mqueue_alloc(NULL);
if (mq == NULL)
return (EAGAIN);
sx_xlock(&mqfs->mi_lock);
if ((cnp->cn_flags & HASBUF) == 0)
panic("%s: no name", __func__);
pn = mqfs_create_file(pd, cnp->cn_nameptr, cnp->cn_namelen,
cnp->cn_cred, ap->a_vap->va_mode);
if (pn == NULL) {
sx_xunlock(&mqfs->mi_lock);
error = ENOSPC;
} else {
mqnode_addref(pn);
sx_xunlock(&mqfs->mi_lock);
error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
mqnode_release(pn);
if (error)
mqfs_destroy(pn);
else
pn->mn_data = mq;
}
if (error)
mqueue_free(mq);
return (error);
}
/*
* Remove an entry
*/
static
int do_unlink(struct mqfs_node *pn, struct ucred *ucred)
{
struct mqfs_node *parent;
struct mqfs_vdata *vd;
int error = 0;
sx_assert(&pn->mn_info->mi_lock, SX_LOCKED);
if (ucred->cr_uid != pn->mn_uid &&
(error = priv_check_cred(ucred, PRIV_MQ_ADMIN, 0)) != 0)
error = EACCES;
else if (!pn->mn_deleted) {
parent = pn->mn_parent;
pn->mn_parent = NULL;
pn->mn_deleted = 1;
LIST_REMOVE(pn, mn_sibling);
LIST_FOREACH(vd, &pn->mn_vnodes, mv_link) {
cache_purge(vd->mv_vnode);
vhold(vd->mv_vnode);
taskqueue_enqueue(taskqueue_thread, &vd->mv_task);
}
mqnode_release(pn);
mqnode_release(parent);
} else
error = ENOENT;
return (error);
}
#if 0
struct vop_remove_args {
struct vnode *a_dvp;
struct vnode *a_vp;
struct componentname *a_cnp;
};
#endif
/*
* vnode removal operation
*/
static int
mqfs_remove(struct vop_remove_args *ap)
{
struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
struct mqfs_node *pn;
int error;
if (ap->a_vp->v_type == VDIR)
return (EPERM);
pn = VTON(ap->a_vp);
sx_xlock(&mqfs->mi_lock);
error = do_unlink(pn, ap->a_cnp->cn_cred);
sx_xunlock(&mqfs->mi_lock);
return (error);
}
#if 0
struct vop_inactive_args {
struct vnode *a_vp;
struct thread *a_td;
};
#endif
static int
mqfs_inactive(struct vop_inactive_args *ap)
{
struct mqfs_node *pn = VTON(ap->a_vp);
if (pn->mn_deleted)
vrecycle(ap->a_vp, ap->a_td);
return (0);
}
#if 0
struct vop_reclaim_args {
struct vop_generic_args a_gen;
struct vnode *a_vp;
struct thread *a_td;
};
#endif
static int
mqfs_reclaim(struct vop_reclaim_args *ap)
{
struct mqfs_info *mqfs = VFSTOMQFS(ap->a_vp->v_mount);
struct vnode *vp = ap->a_vp;
struct mqfs_node *pn;
struct mqfs_vdata *vd;
vd = vp->v_data;
pn = vd->mv_node;
sx_xlock(&mqfs->mi_lock);
vp->v_data = NULL;
LIST_REMOVE(vd, mv_link);
uma_zfree(mvdata_zone, vd);
mqnode_release(pn);
sx_xunlock(&mqfs->mi_lock);
return (0);
}
#if 0
struct vop_open_args {
struct vop_generic_args a_gen;
struct vnode *a_vp;
int a_mode;
struct ucred *a_cred;
struct thread *a_td;
struct file *a_fp;
};
#endif
static int
mqfs_open(struct vop_open_args *ap)
{
return (0);
}
#if 0
struct vop_close_args {
struct vop_generic_args a_gen;
struct vnode *a_vp;
int a_fflag;
struct ucred *a_cred;
struct thread *a_td;
};
#endif
static int
mqfs_close(struct vop_close_args *ap)
{
return (0);
}
#if 0
struct vop_access_args {
struct vop_generic_args a_gen;
struct vnode *a_vp;
accmode_t a_accmode;
struct ucred *a_cred;
struct thread *a_td;
};
#endif
/*
* Verify permissions
*/
static int
mqfs_access(struct vop_access_args *ap)
{
struct vnode *vp = ap->a_vp;
struct vattr vattr;
int error;
error = VOP_GETATTR(vp, &vattr, ap->a_cred);
if (error)
return (error);
error = vaccess(vp->v_type, vattr.va_mode, vattr.va_uid,
vattr.va_gid, ap->a_accmode, ap->a_cred, NULL);
return (error);
}
#if 0
struct vop_getattr_args {
struct vop_generic_args a_gen;
struct vnode *a_vp;
struct vattr *a_vap;
struct ucred *a_cred;
};
#endif
/*
* Get file attributes
*/
static int
mqfs_getattr(struct vop_getattr_args *ap)
{
struct vnode *vp = ap->a_vp;
struct mqfs_node *pn = VTON(vp);
struct vattr *vap = ap->a_vap;
int error = 0;
vap->va_type = vp->v_type;
vap->va_mode = pn->mn_mode;
vap->va_nlink = 1;
vap->va_uid = pn->mn_uid;
vap->va_gid = pn->mn_gid;
vap->va_fsid = vp->v_mount->mnt_stat.f_fsid.val[0];
vap->va_fileid = pn->mn_fileno;
vap->va_size = 0;
vap->va_blocksize = PAGE_SIZE;
vap->va_bytes = vap->va_size = 0;
vap->va_atime = pn->mn_atime;
vap->va_mtime = pn->mn_mtime;
vap->va_ctime = pn->mn_ctime;
vap->va_birthtime = pn->mn_birth;
vap->va_gen = 0;
vap->va_flags = 0;
vap->va_rdev = NODEV;
vap->va_bytes = 0;
vap->va_filerev = 0;
return (error);
}
#if 0
struct vop_setattr_args {
struct vop_generic_args a_gen;
struct vnode *a_vp;
struct vattr *a_vap;
struct ucred *a_cred;
};
#endif
/*
* Set attributes
*/
static int
mqfs_setattr(struct vop_setattr_args *ap)
{
struct mqfs_node *pn;
struct vattr *vap;
struct vnode *vp;
struct thread *td;
int c, error;
uid_t uid;
gid_t gid;
td = curthread;
vap = ap->a_vap;
vp = ap->a_vp;
if ((vap->va_type != VNON) ||
(vap->va_nlink != VNOVAL) ||
(vap->va_fsid != VNOVAL) ||
(vap->va_fileid != VNOVAL) ||
(vap->va_blocksize != VNOVAL) ||
(vap->va_flags != VNOVAL && vap->va_flags != 0) ||
(vap->va_rdev != VNOVAL) ||
((int)vap->va_bytes != VNOVAL) ||
(vap->va_gen != VNOVAL)) {
return (EINVAL);
}
pn = VTON(vp);
error = c = 0;
if (vap->va_uid == (uid_t)VNOVAL)
uid = pn->mn_uid;
else
uid = vap->va_uid;
if (vap->va_gid == (gid_t)VNOVAL)
gid = pn->mn_gid;
else
gid = vap->va_gid;
if (uid != pn->mn_uid || gid != pn->mn_gid) {
/*
* To modify the ownership of a file, must possess VADMIN
* for that file.
*/
if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)))
return (error);
/*
* XXXRW: Why is there a privilege check here: shouldn't the
* check in VOP_ACCESS() be enough? Also, are the group bits
* below definitely right?
*/
if (((ap->a_cred->cr_uid != pn->mn_uid) || uid != pn->mn_uid ||
(gid != pn->mn_gid && !groupmember(gid, ap->a_cred))) &&
(error = priv_check(td, PRIV_MQ_ADMIN)) != 0)
return (error);
pn->mn_uid = uid;
pn->mn_gid = gid;
c = 1;
}
if (vap->va_mode != (mode_t)VNOVAL) {
if ((ap->a_cred->cr_uid != pn->mn_uid) &&
(error = priv_check(td, PRIV_MQ_ADMIN)))
return (error);
pn->mn_mode = vap->va_mode;
c = 1;
}
if (vap->va_atime.tv_sec != VNOVAL || vap->va_mtime.tv_sec != VNOVAL) {
/* See the comment in ufs_vnops::ufs_setattr(). */
if ((error = VOP_ACCESS(vp, VADMIN, ap->a_cred, td)) &&
((vap->va_vaflags & VA_UTIMES_NULL) == 0 ||
(error = VOP_ACCESS(vp, VWRITE, ap->a_cred, td))))
return (error);
if (vap->va_atime.tv_sec != VNOVAL) {
pn->mn_atime = vap->va_atime;
}
if (vap->va_mtime.tv_sec != VNOVAL) {
pn->mn_mtime = vap->va_mtime;
}
c = 1;
}
if (c) {
vfs_timestamp(&pn->mn_ctime);
}
return (0);
}
#if 0
struct vop_read_args {
struct vop_generic_args a_gen;
struct vnode *a_vp;
struct uio *a_uio;
int a_ioflag;
struct ucred *a_cred;
};
#endif
/*
* Read from a file
*/
static int
mqfs_read(struct vop_read_args *ap)
{
char buf[80];
struct vnode *vp = ap->a_vp;
struct uio *uio = ap->a_uio;
struct mqfs_node *pn;
struct mqueue *mq;
int len, error;
if (vp->v_type != VREG)
return (EINVAL);
pn = VTON(vp);
mq = VTOMQ(vp);
snprintf(buf, sizeof(buf),
"QSIZE:%-10ld MAXMSG:%-10ld CURMSG:%-10ld MSGSIZE:%-10ld\n",
mq->mq_totalbytes,
mq->mq_maxmsg,
mq->mq_curmsgs,
mq->mq_msgsize);
buf[sizeof(buf)-1] = '\0';
len = strlen(buf);
error = uiomove_frombuf(buf, len, uio);
return (error);
}
#if 0
struct vop_readdir_args {
struct vop_generic_args a_gen;
struct vnode *a_vp;
struct uio *a_uio;
struct ucred *a_cred;
int *a_eofflag;
int *a_ncookies;
u_long **a_cookies;
};
#endif
/*
* Return directory entries.
*/
static int
mqfs_readdir(struct vop_readdir_args *ap)
{
struct vnode *vp;
struct mqfs_info *mi;
struct mqfs_node *pd;
struct mqfs_node *pn;
struct dirent entry;
struct uio *uio;
int *tmp_ncookies = NULL;
off_t offset;
int error, i;
vp = ap->a_vp;
mi = VFSTOMQFS(vp->v_mount);
pd = VTON(vp);
uio = ap->a_uio;
if (vp->v_type != VDIR)
return (ENOTDIR);
if (uio->uio_offset < 0)
return (EINVAL);
if (ap->a_ncookies != NULL) {
tmp_ncookies = ap->a_ncookies;
*ap->a_ncookies = 0;
ap->a_ncookies = NULL;
}
error = 0;
offset = 0;
sx_xlock(&mi->mi_lock);
LIST_FOREACH(pn, &pd->mn_children, mn_sibling) {
entry.d_reclen = sizeof(entry);
if (!pn->mn_fileno)
mqfs_fileno_alloc(mi, pn);
entry.d_fileno = pn->mn_fileno;
for (i = 0; i < MQFS_NAMELEN - 1 && pn->mn_name[i] != '\0'; ++i)
entry.d_name[i] = pn->mn_name[i];
entry.d_name[i] = 0;
entry.d_namlen = i;
switch (pn->mn_type) {
case mqfstype_root:
case mqfstype_dir:
case mqfstype_this:
case mqfstype_parent:
entry.d_type = DT_DIR;
break;
case mqfstype_file:
entry.d_type = DT_REG;
break;
case mqfstype_symlink:
entry.d_type = DT_LNK;
break;
default:
panic("%s has unexpected node type: %d", pn->mn_name,
pn->mn_type);
}
if (entry.d_reclen > uio->uio_resid)
break;
if (offset >= uio->uio_offset) {
error = vfs_read_dirent(ap, &entry, offset);
if (error)
break;
}
offset += entry.d_reclen;
}
sx_xunlock(&mi->mi_lock);
uio->uio_offset = offset;
if (tmp_ncookies != NULL)
ap->a_ncookies = tmp_ncookies;
return (error);
}
#ifdef notyet
#if 0
struct vop_mkdir_args {
struct vnode *a_dvp;
struvt vnode **a_vpp;
struvt componentname *a_cnp;
struct vattr *a_vap;
};
#endif
/*
* Create a directory.
*/
static int
mqfs_mkdir(struct vop_mkdir_args *ap)
{
struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
struct componentname *cnp = ap->a_cnp;
struct mqfs_node *pd = VTON(ap->a_dvp);
struct mqfs_node *pn;
int error;
if (pd->mn_type != mqfstype_root && pd->mn_type != mqfstype_dir)
return (ENOTDIR);
sx_xlock(&mqfs->mi_lock);
if ((cnp->cn_flags & HASBUF) == 0)
panic("%s: no name", __func__);
pn = mqfs_create_dir(pd, cnp->cn_nameptr, cnp->cn_namelen,
ap->a_vap->cn_cred, ap->a_vap->va_mode);
if (pn != NULL)
mqnode_addref(pn);
sx_xunlock(&mqfs->mi_lock);
if (pn == NULL) {
error = ENOSPC;
} else {
error = mqfs_allocv(ap->a_dvp->v_mount, ap->a_vpp, pn);
mqnode_release(pn);
}
return (error);
}
#if 0
struct vop_rmdir_args {
struct vnode *a_dvp;
struct vnode *a_vp;
struct componentname *a_cnp;
};
#endif
/*
* Remove a directory.
*/
static int
mqfs_rmdir(struct vop_rmdir_args *ap)
{
struct mqfs_info *mqfs = VFSTOMQFS(ap->a_dvp->v_mount);
struct mqfs_node *pn = VTON(ap->a_vp);
struct mqfs_node *pt;
if (pn->mn_type != mqfstype_dir)
return (ENOTDIR);
sx_xlock(&mqfs->mi_lock);
if (pn->mn_deleted) {
sx_xunlock(&mqfs->mi_lock);
return (ENOENT);
}
pt = LIST_FIRST(&pn->mn_children);
pt = LIST_NEXT(pt, mn_sibling);
pt = LIST_NEXT(pt, mn_sibling);
if (pt != NULL) {
sx_xunlock(&mqfs->mi_lock);
return (ENOTEMPTY);
}
pt = pn->mn_parent;
pn->mn_parent = NULL;
pn->mn_deleted = 1;
LIST_REMOVE(pn, mn_sibling);
mqnode_release(pn);
mqnode_release(pt);
sx_xunlock(&mqfs->mi_lock);
cache_purge(ap->a_vp);
return (0);
}
#endif /* notyet */
/*
* Allocate a message queue
*/
static struct mqueue *
mqueue_alloc(const struct mq_attr *attr)
{
struct mqueue *mq;
if (curmq >= maxmq)
return (NULL);
mq = uma_zalloc(mqueue_zone, M_WAITOK | M_ZERO);
TAILQ_INIT(&mq->mq_msgq);
if (attr != NULL) {
mq->mq_maxmsg = attr->mq_maxmsg;
mq->mq_msgsize = attr->mq_msgsize;
} else {
mq->mq_maxmsg = default_maxmsg;
mq->mq_msgsize = default_msgsize;
}
mtx_init(&mq->mq_mutex, "mqueue lock", NULL, MTX_DEF);
knlist_init_mtx(&mq->mq_rsel.si_note, &mq->mq_mutex);
knlist_init_mtx(&mq->mq_wsel.si_note, &mq->mq_mutex);
atomic_add_int(&curmq, 1);
return (mq);
}
/*
* Destroy a message queue
*/
static void
mqueue_free(struct mqueue *mq)
{
struct mqueue_msg *msg;
while ((msg = TAILQ_FIRST(&mq->mq_msgq)) != NULL) {
TAILQ_REMOVE(&mq->mq_msgq, msg, msg_link);
free(msg, M_MQUEUEDATA);
}
mtx_destroy(&mq->mq_mutex);
seldrain(&mq->mq_rsel);
seldrain(&mq->mq_wsel);
knlist_destroy(&mq->mq_rsel.si_note);
knlist_destroy(&mq->mq_wsel.si_note);
uma_zfree(mqueue_zone, mq);
atomic_add_int(&curmq, -1);
}
/*
* Load a message from user space
*/
static struct mqueue_msg *
mqueue_loadmsg(const char *msg_ptr, size_t msg_size, int msg_prio)
{
struct mqueue_msg *msg;
size_t len;
int error;
len = sizeof(struct mqueue_msg) + msg_size;
msg = malloc(len, M_MQUEUEDATA, M_WAITOK);
error = copyin(msg_ptr, ((char *)msg) + sizeof(struct mqueue_msg),
msg_size);
if (error) {
free(msg, M_MQUEUEDATA);
msg = NULL;
} else {
msg->msg_size = msg_size;
msg->msg_prio = msg_prio;
}
return (msg);
}
/*
* Save a message to user space
*/
static int
mqueue_savemsg(struct mqueue_msg *msg, char *msg_ptr, int *msg_prio)
{
int error;
error = copyout(((char *)msg) + sizeof(*msg), msg_ptr,
msg->msg_size);
if (error == 0 && msg_prio != NULL)
error = copyout(&msg->msg_prio, msg_prio, sizeof(int));
return (error);
}
/*
* Free a message's memory
*/
static __inline void
mqueue_freemsg(struct mqueue_msg *msg)
{
free(msg, M_MQUEUEDATA);
}
/*
* Send a message. if waitok is false, thread will not be
* blocked if there is no data in queue, otherwise, absolute
* time will be checked.
*/
int
mqueue_send(struct mqueue *mq, const char *msg_ptr,
size_t msg_len, unsigned msg_prio, int waitok,
const struct timespec *abs_timeout)
{
struct mqueue_msg *msg;
struct timespec ts, ts2;
struct timeval tv;
int error;
if (msg_prio >= MQ_PRIO_MAX)
return (EINVAL);
if (msg_len > mq->mq_msgsize)
return (EMSGSIZE);
msg = mqueue_loadmsg(msg_ptr, msg_len, msg_prio);
if (msg == NULL)
return (EFAULT);
/* O_NONBLOCK case */
if (!waitok) {
error = _mqueue_send(mq, msg, -1);
if (error)
goto bad;
return (0);
}
/* we allow a null timeout (wait forever) */
if (abs_timeout == NULL) {
error = _mqueue_send(mq, msg, 0);
if (error)
goto bad;
return (0);
}
/* send it before checking time */
error = _mqueue_send(mq, msg, -1);
if (error == 0)
return (0);
if (error != EAGAIN)
goto bad;
if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
error = EINVAL;
goto bad;
}
for (;;) {
ts2 = *abs_timeout;
getnanotime(&ts);
timespecsub(&ts2, &ts);
if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
error = ETIMEDOUT;
break;
}
TIMESPEC_TO_TIMEVAL(&tv, &ts2);
error = _mqueue_send(mq, msg, tvtohz(&tv));
if (error != ETIMEDOUT)
break;
}
if (error == 0)
return (0);
bad:
mqueue_freemsg(msg);
return (error);
}
/*
* Common routine to send a message
*/
static int
_mqueue_send(struct mqueue *mq, struct mqueue_msg *msg, int timo)
{
struct mqueue_msg *msg2;
int error = 0;
mtx_lock(&mq->mq_mutex);
while (mq->mq_curmsgs >= mq->mq_maxmsg && error == 0) {
if (timo < 0) {
mtx_unlock(&mq->mq_mutex);
return (EAGAIN);
}
mq->mq_senders++;
error = msleep(&mq->mq_senders, &mq->mq_mutex,
PCATCH, "mqsend", timo);
mq->mq_senders--;
if (error == EAGAIN)
error = ETIMEDOUT;
}
if (mq->mq_curmsgs >= mq->mq_maxmsg) {
mtx_unlock(&mq->mq_mutex);
return (error);
}
error = 0;
if (TAILQ_EMPTY(&mq->mq_msgq)) {
TAILQ_INSERT_HEAD(&mq->mq_msgq, msg, msg_link);
} else {
if (msg->msg_prio <= TAILQ_LAST(&mq->mq_msgq, msgq)->msg_prio) {
TAILQ_INSERT_TAIL(&mq->mq_msgq, msg, msg_link);
} else {
TAILQ_FOREACH(msg2, &mq->mq_msgq, msg_link) {
if (msg2->msg_prio < msg->msg_prio)
break;
}
TAILQ_INSERT_BEFORE(msg2, msg, msg_link);
}
}
mq->mq_curmsgs++;
mq->mq_totalbytes += msg->msg_size;
if (mq->mq_receivers)
wakeup_one(&mq->mq_receivers);
else if (mq->mq_notifier != NULL)
mqueue_send_notification(mq);
if (mq->mq_flags & MQ_RSEL) {
mq->mq_flags &= ~MQ_RSEL;
selwakeup(&mq->mq_rsel);
}
KNOTE_LOCKED(&mq->mq_rsel.si_note, 0);
mtx_unlock(&mq->mq_mutex);
return (0);
}
/*
* Send realtime a signal to process which registered itself
* successfully by mq_notify.
*/
static void
mqueue_send_notification(struct mqueue *mq)
{
struct mqueue_notifier *nt;
struct thread *td;
struct proc *p;
int error;
mtx_assert(&mq->mq_mutex, MA_OWNED);
nt = mq->mq_notifier;
if (nt->nt_sigev.sigev_notify != SIGEV_NONE) {
p = nt->nt_proc;
error = sigev_findtd(p, &nt->nt_sigev, &td);
if (error) {
mq->mq_notifier = NULL;
return;
}
if (!KSI_ONQ(&nt->nt_ksi)) {
ksiginfo_set_sigev(&nt->nt_ksi, &nt->nt_sigev);
tdsendsignal(p, td, nt->nt_ksi.ksi_signo, &nt->nt_ksi);
}
PROC_UNLOCK(p);
}
mq->mq_notifier = NULL;
}
/*
* Get a message. if waitok is false, thread will not be
* blocked if there is no data in queue, otherwise, absolute
* time will be checked.
*/
int
mqueue_receive(struct mqueue *mq, char *msg_ptr,
size_t msg_len, unsigned *msg_prio, int waitok,
const struct timespec *abs_timeout)
{
struct mqueue_msg *msg;
struct timespec ts, ts2;
struct timeval tv;
int error;
if (msg_len < mq->mq_msgsize)
return (EMSGSIZE);
/* O_NONBLOCK case */
if (!waitok) {
error = _mqueue_recv(mq, &msg, -1);
if (error)
return (error);
goto received;
}
/* we allow a null timeout (wait forever). */
if (abs_timeout == NULL) {
error = _mqueue_recv(mq, &msg, 0);
if (error)
return (error);
goto received;
}
/* try to get a message before checking time */
error = _mqueue_recv(mq, &msg, -1);
if (error == 0)
goto received;
if (error != EAGAIN)
return (error);
if (abs_timeout->tv_nsec >= 1000000000 || abs_timeout->tv_nsec < 0) {
error = EINVAL;
return (error);
}
for (;;) {
ts2 = *abs_timeout;
getnanotime(&ts);
timespecsub(&ts2, &ts);
if (ts2.tv_sec < 0 || (ts2.tv_sec == 0 && ts2.tv_nsec <= 0)) {
error = ETIMEDOUT;
return (error);
}
TIMESPEC_TO_TIMEVAL(&tv, &ts2);
error = _mqueue_recv(mq, &msg, tvtohz(&tv));
if (error == 0)
break;
if (error != ETIMEDOUT)
return (error);
}
received:
error = mqueue_savemsg(msg, msg_ptr, msg_prio);
if (error == 0) {
curthread->td_retval[0] = msg->msg_size;
curthread->td_retval[1] = 0;
}
mqueue_freemsg(msg);
return (error);
}
/*
* Common routine to receive a message
*/
static int
_mqueue_recv(struct mqueue *mq, struct mqueue_msg **msg, int timo)
{
int error = 0;
mtx_lock(&mq->mq_mutex);
while ((*msg = TAILQ_FIRST(&mq->mq_msgq)) == NULL && error == 0) {
if (timo < 0) {
mtx_unlock(&mq->mq_mutex);
return (EAGAIN);
}
mq->mq_receivers++;
error = msleep(&mq->mq_receivers, &mq->mq_mutex,
PCATCH, "mqrecv", timo);
mq->mq_receivers--;
if (error == EAGAIN)
error = ETIMEDOUT;
}
if (*msg != NULL) {
error = 0;
TAILQ_REMOVE(&mq->mq_msgq, *msg, msg_link);
mq->mq_curmsgs--;
mq->mq_totalbytes -= (*msg)->msg_size;
if (mq->mq_senders)
wakeup_one(&mq->mq_senders);
if (mq->mq_flags & MQ_WSEL) {
mq->mq_flags &= ~MQ_WSEL;
selwakeup(&mq->mq_wsel);
}
KNOTE_LOCKED(&mq->mq_wsel.si_note, 0);
}
if (mq->mq_notifier != NULL && mq->mq_receivers == 0 &&
!TAILQ_EMPTY(&mq->mq_msgq)) {
mqueue_send_notification(mq);
}
mtx_unlock(&mq->mq_mutex);
return (error);
}
static __inline struct mqueue_notifier *
notifier_alloc(void)
{
return (uma_zalloc(mqnoti_zone, M_WAITOK | M_ZERO));
}
static __inline void
notifier_free(struct mqueue_notifier *p)
{
uma_zfree(mqnoti_zone, p);
}
static struct mqueue_notifier *
notifier_search(struct proc *p, int fd)
{
struct mqueue_notifier *nt;
LIST_FOREACH(nt, &p->p_mqnotifier, nt_link) {
if (nt->nt_ksi.ksi_mqd == fd)
break;
}
return (nt);
}
static __inline void
notifier_insert(struct proc *p, struct mqueue_notifier *nt)
{
LIST_INSERT_HEAD(&p->p_mqnotifier, nt, nt_link);
}
static __inline void
notifier_delete(struct proc *p, struct mqueue_notifier *nt)
{
LIST_REMOVE(nt, nt_link);
notifier_free(nt);
}
static void
notifier_remove(struct proc *p, struct mqueue *mq, int fd)
{
struct mqueue_notifier *nt;
mtx_assert(&mq->mq_mutex, MA_OWNED);
PROC_LOCK(p);
nt = notifier_search(p, fd);
if (nt != NULL) {
if (mq->mq_notifier == nt)
mq->mq_notifier = NULL;
sigqueue_take(&nt->nt_ksi);
notifier_delete(p, nt);
}
PROC_UNLOCK(p);
}
static int
kern_kmq_open(struct thread *td, const char *upath, int flags, mode_t mode,
const struct mq_attr *attr)
{
char path[MQFS_NAMELEN + 1];
struct mqfs_node *pn;
struct filedesc *fdp;
struct file *fp;
struct mqueue *mq;
int fd, error, len, cmode;
fdp = td->td_proc->p_fd;
cmode = (((mode & ~fdp->fd_cmask) & ALLPERMS) & ~S_ISTXT);
mq = NULL;
if ((flags & O_CREAT) != 0 && attr != NULL) {
if (attr->mq_maxmsg <= 0 || attr->mq_maxmsg > maxmsg)
return (EINVAL);
if (attr->mq_msgsize <= 0 || attr->mq_msgsize > maxmsgsize)
return (EINVAL);
}
error = copyinstr(upath, path, MQFS_NAMELEN + 1, NULL);
if (error)
return (error);
/*
* The first character of name must be a slash (/) character
* and the remaining characters of name cannot include any slash
* characters.
*/
len = strlen(path);
if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
return (EINVAL);
error = falloc(td, &fp, &fd, 0);
if (error)
return (error);
sx_xlock(&mqfs_data.mi_lock);
pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
if (pn == NULL) {
if (!(flags & O_CREAT)) {
error = ENOENT;
} else {
mq = mqueue_alloc(attr);
if (mq == NULL) {
error = ENFILE;
} else {
pn = mqfs_create_file(mqfs_data.mi_root,
path + 1, len - 1, td->td_ucred,
cmode);
if (pn == NULL) {
error = ENOSPC;
mqueue_free(mq);
}
}
}
if (error == 0) {
pn->mn_data = mq;
}
} else {
if ((flags & (O_CREAT | O_EXCL)) == (O_CREAT | O_EXCL)) {
error = EEXIST;
} else {
accmode_t accmode = 0;
if (flags & FREAD)
accmode |= VREAD;
if (flags & FWRITE)
accmode |= VWRITE;
error = vaccess(VREG, pn->mn_mode, pn->mn_uid,
pn->mn_gid, accmode, td->td_ucred, NULL);
}
}
if (error) {
sx_xunlock(&mqfs_data.mi_lock);
fdclose(fdp, fp, fd, td);
fdrop(fp, td);
return (error);
}
mqnode_addref(pn);
sx_xunlock(&mqfs_data.mi_lock);
finit(fp, flags & (FREAD | FWRITE | O_NONBLOCK), DTYPE_MQUEUE, pn,
&mqueueops);
FILEDESC_XLOCK(fdp);
if (fdp->fd_ofiles[fd] == fp)
fdp->fd_ofileflags[fd] |= UF_EXCLOSE;
FILEDESC_XUNLOCK(fdp);
td->td_retval[0] = fd;
fdrop(fp, td);
return (0);
}
/*
* Syscall to open a message queue.
*/
int
sys_kmq_open(struct thread *td, struct kmq_open_args *uap)
{
struct mq_attr attr;
int flags, error;
if ((uap->flags & O_ACCMODE) == O_ACCMODE)
return (EINVAL);
flags = FFLAGS(uap->flags);
if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
error = copyin(uap->attr, &attr, sizeof(attr));
if (error)
return (error);
}
return (kern_kmq_open(td, uap->path, flags, uap->mode,
uap->attr != NULL ? &attr : NULL));
}
/*
* Syscall to unlink a message queue.
*/
int
sys_kmq_unlink(struct thread *td, struct kmq_unlink_args *uap)
{
char path[MQFS_NAMELEN+1];
struct mqfs_node *pn;
int error, len;
error = copyinstr(uap->path, path, MQFS_NAMELEN + 1, NULL);
if (error)
return (error);
len = strlen(path);
if (len < 2 || path[0] != '/' || strchr(path + 1, '/') != NULL)
return (EINVAL);
sx_xlock(&mqfs_data.mi_lock);
pn = mqfs_search(mqfs_data.mi_root, path + 1, len - 1);
if (pn != NULL)
error = do_unlink(pn, td->td_ucred);
else
error = ENOENT;
sx_xunlock(&mqfs_data.mi_lock);
return (error);
}
typedef int (*_fgetf)(struct thread *, int, cap_rights_t, struct file **);
/*
* Get message queue by giving file slot
*/
static int
_getmq(struct thread *td, int fd, cap_rights_t rights, _fgetf func,
struct file **fpp, struct mqfs_node **ppn, struct mqueue **pmq)
{
struct mqfs_node *pn;
int error;
error = func(td, fd, rights, fpp);
if (error)
return (error);
if (&mqueueops != (*fpp)->f_ops) {
fdrop(*fpp, td);
return (EBADF);
}
pn = (*fpp)->f_data;
if (ppn)
*ppn = pn;
if (pmq)
*pmq = pn->mn_data;
return (0);
}
static __inline int
getmq(struct thread *td, int fd, struct file **fpp, struct mqfs_node **ppn,
struct mqueue **pmq)
{
return _getmq(td, fd, CAP_POLL_EVENT, fget, fpp, ppn, pmq);
}
static __inline int
getmq_read(struct thread *td, int fd, struct file **fpp,
struct mqfs_node **ppn, struct mqueue **pmq)
{
return _getmq(td, fd, CAP_READ, fget_read, fpp, ppn, pmq);
}
static __inline int
getmq_write(struct thread *td, int fd, struct file **fpp,
struct mqfs_node **ppn, struct mqueue **pmq)
{
return _getmq(td, fd, CAP_WRITE, fget_write, fpp, ppn, pmq);
}
static int
kern_kmq_setattr(struct thread *td, int mqd, const struct mq_attr *attr,
struct mq_attr *oattr)
{
struct mqueue *mq;
struct file *fp;
u_int oflag, flag;
int error;
if (attr != NULL && (attr->mq_flags & ~O_NONBLOCK) != 0)
return (EINVAL);
error = getmq(td, mqd, &fp, NULL, &mq);
if (error)
return (error);
oattr->mq_maxmsg = mq->mq_maxmsg;
oattr->mq_msgsize = mq->mq_msgsize;
oattr->mq_curmsgs = mq->mq_curmsgs;
if (attr != NULL) {
do {
oflag = flag = fp->f_flag;
flag &= ~O_NONBLOCK;
flag |= (attr->mq_flags & O_NONBLOCK);
} while (atomic_cmpset_int(&fp->f_flag, oflag, flag) == 0);
} else
oflag = fp->f_flag;
oattr->mq_flags = (O_NONBLOCK & oflag);
fdrop(fp, td);
return (error);
}
int
sys_kmq_setattr(struct thread *td, struct kmq_setattr_args *uap)
{
struct mq_attr attr, oattr;
int error;
if (uap->attr != NULL) {
error = copyin(uap->attr, &attr, sizeof(attr));
if (error != 0)
return (error);
}
error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
&oattr);
if (error != 0)
return (error);
if (uap->oattr != NULL)
error = copyout(&oattr, uap->oattr, sizeof(oattr));
return (error);
}
int
sys_kmq_timedreceive(struct thread *td, struct kmq_timedreceive_args *uap)
{
struct mqueue *mq;
struct file *fp;
struct timespec *abs_timeout, ets;
int error;
int waitok;
error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
if (error)
return (error);
if (uap->abs_timeout != NULL) {
error = copyin(uap->abs_timeout, &ets, sizeof(ets));
if (error != 0)
return (error);
abs_timeout = &ets;
} else
abs_timeout = NULL;
waitok = !(fp->f_flag & O_NONBLOCK);
error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
uap->msg_prio, waitok, abs_timeout);
fdrop(fp, td);
return (error);
}
int
sys_kmq_timedsend(struct thread *td, struct kmq_timedsend_args *uap)
{
struct mqueue *mq;
struct file *fp;
struct timespec *abs_timeout, ets;
int error, waitok;
error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
if (error)
return (error);
if (uap->abs_timeout != NULL) {
error = copyin(uap->abs_timeout, &ets, sizeof(ets));
if (error != 0)
return (error);
abs_timeout = &ets;
} else
abs_timeout = NULL;
waitok = !(fp->f_flag & O_NONBLOCK);
error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
uap->msg_prio, waitok, abs_timeout);
fdrop(fp, td);
return (error);
}
int
sys_kmq_notify(struct thread *td, struct kmq_notify_args *uap)
{
struct sigevent ev;
struct filedesc *fdp;
struct proc *p;
struct mqueue *mq;
struct file *fp, *fp2;
struct mqueue_notifier *nt, *newnt = NULL;
int error;
p = td->td_proc;
fdp = td->td_proc->p_fd;
if (uap->sigev) {
error = copyin(uap->sigev, &ev, sizeof(ev));
if (error)
return (error);
if (ev.sigev_notify != SIGEV_SIGNAL &&
ev.sigev_notify != SIGEV_THREAD_ID &&
ev.sigev_notify != SIGEV_NONE)
return (EINVAL);
if ((ev.sigev_notify == SIGEV_SIGNAL ||
ev.sigev_notify == SIGEV_THREAD_ID) &&
!_SIG_VALID(ev.sigev_signo))
return (EINVAL);
}
error = getmq(td, uap->mqd, &fp, NULL, &mq);
if (error)
return (error);
again:
FILEDESC_SLOCK(fdp);
fp2 = fget_locked(fdp, uap->mqd);
if (fp2 == NULL) {
FILEDESC_SUNLOCK(fdp);
error = EBADF;
goto out;
}
error = cap_funwrap(fp2, CAP_POLL_EVENT, &fp2);
if (error) {
FILEDESC_SUNLOCK(fdp);
goto out;
}
if (fp2 != fp) {
FILEDESC_SUNLOCK(fdp);
error = EBADF;
goto out;
}
mtx_lock(&mq->mq_mutex);
FILEDESC_SUNLOCK(fdp);
if (uap->sigev != NULL) {
if (mq->mq_notifier != NULL) {
error = EBUSY;
} else {
PROC_LOCK(p);
nt = notifier_search(p, uap->mqd);
if (nt == NULL) {
if (newnt == NULL) {
PROC_UNLOCK(p);
mtx_unlock(&mq->mq_mutex);
newnt = notifier_alloc();
goto again;
}
}
if (nt != NULL) {
sigqueue_take(&nt->nt_ksi);
if (newnt != NULL) {
notifier_free(newnt);
newnt = NULL;
}
} else {
nt = newnt;
newnt = NULL;
ksiginfo_init(&nt->nt_ksi);
nt->nt_ksi.ksi_flags |= KSI_INS | KSI_EXT;
nt->nt_ksi.ksi_code = SI_MESGQ;
nt->nt_proc = p;
nt->nt_ksi.ksi_mqd = uap->mqd;
notifier_insert(p, nt);
}
nt->nt_sigev = ev;
mq->mq_notifier = nt;
PROC_UNLOCK(p);
/*
* if there is no receivers and message queue
* is not empty, we should send notification
* as soon as possible.
*/
if (mq->mq_receivers == 0 &&
!TAILQ_EMPTY(&mq->mq_msgq))
mqueue_send_notification(mq);
}
} else {
notifier_remove(p, mq, uap->mqd);
}
mtx_unlock(&mq->mq_mutex);
out:
fdrop(fp, td);
if (newnt != NULL)
notifier_free(newnt);
return (error);
}
static void
mqueue_fdclose(struct thread *td, int fd, struct file *fp)
{
struct filedesc *fdp;
struct mqueue *mq;
fdp = td->td_proc->p_fd;
FILEDESC_LOCK_ASSERT(fdp);
if (fp->f_ops == &mqueueops) {
mq = FPTOMQ(fp);
mtx_lock(&mq->mq_mutex);
notifier_remove(td->td_proc, mq, fd);
/* have to wakeup thread in same process */
if (mq->mq_flags & MQ_RSEL) {
mq->mq_flags &= ~MQ_RSEL;
selwakeup(&mq->mq_rsel);
}
if (mq->mq_flags & MQ_WSEL) {
mq->mq_flags &= ~MQ_WSEL;
selwakeup(&mq->mq_wsel);
}
mtx_unlock(&mq->mq_mutex);
}
}
static void
mq_proc_exit(void *arg __unused, struct proc *p)
{
struct filedesc *fdp;
struct file *fp;
struct mqueue *mq;
int i;
fdp = p->p_fd;
FILEDESC_SLOCK(fdp);
for (i = 0; i < fdp->fd_nfiles; ++i) {
fp = fget_locked(fdp, i);
if (fp != NULL && fp->f_ops == &mqueueops) {
mq = FPTOMQ(fp);
mtx_lock(&mq->mq_mutex);
notifier_remove(p, FPTOMQ(fp), i);
mtx_unlock(&mq->mq_mutex);
}
}
FILEDESC_SUNLOCK(fdp);
KASSERT(LIST_EMPTY(&p->p_mqnotifier), ("mq notifiers left"));
}
static int
mqf_read(struct file *fp, struct uio *uio, struct ucred *active_cred,
int flags, struct thread *td)
{
return (EOPNOTSUPP);
}
static int
mqf_write(struct file *fp, struct uio *uio, struct ucred *active_cred,
int flags, struct thread *td)
{
return (EOPNOTSUPP);
}
static int
mqf_truncate(struct file *fp, off_t length, struct ucred *active_cred,
struct thread *td)
{
return (EINVAL);
}
static int
mqf_ioctl(struct file *fp, u_long cmd, void *data,
struct ucred *active_cred, struct thread *td)
{
return (ENOTTY);
}
static int
mqf_poll(struct file *fp, int events, struct ucred *active_cred,
struct thread *td)
{
struct mqueue *mq = FPTOMQ(fp);
int revents = 0;
mtx_lock(&mq->mq_mutex);
if (events & (POLLIN | POLLRDNORM)) {
if (mq->mq_curmsgs) {
revents |= events & (POLLIN | POLLRDNORM);
} else {
mq->mq_flags |= MQ_RSEL;
selrecord(td, &mq->mq_rsel);
}
}
if (events & POLLOUT) {
if (mq->mq_curmsgs < mq->mq_maxmsg)
revents |= POLLOUT;
else {
mq->mq_flags |= MQ_WSEL;
selrecord(td, &mq->mq_wsel);
}
}
mtx_unlock(&mq->mq_mutex);
return (revents);
}
static int
mqf_close(struct file *fp, struct thread *td)
{
struct mqfs_node *pn;
fp->f_ops = &badfileops;
pn = fp->f_data;
fp->f_data = NULL;
sx_xlock(&mqfs_data.mi_lock);
mqnode_release(pn);
sx_xunlock(&mqfs_data.mi_lock);
return (0);
}
static int
mqf_stat(struct file *fp, struct stat *st, struct ucred *active_cred,
struct thread *td)
{
struct mqfs_node *pn = fp->f_data;
bzero(st, sizeof *st);
sx_xlock(&mqfs_data.mi_lock);
st->st_atim = pn->mn_atime;
st->st_mtim = pn->mn_mtime;
st->st_ctim = pn->mn_ctime;
st->st_birthtim = pn->mn_birth;
st->st_uid = pn->mn_uid;
st->st_gid = pn->mn_gid;
st->st_mode = S_IFIFO | pn->mn_mode;
sx_xunlock(&mqfs_data.mi_lock);
return (0);
}
static int
mqf_chmod(struct file *fp, mode_t mode, struct ucred *active_cred,
struct thread *td)
{
struct mqfs_node *pn;
int error;
error = 0;
pn = fp->f_data;
sx_xlock(&mqfs_data.mi_lock);
error = vaccess(VREG, pn->mn_mode, pn->mn_uid, pn->mn_gid, VADMIN,
active_cred, NULL);
if (error != 0)
goto out;
pn->mn_mode = mode & ACCESSPERMS;
out:
sx_xunlock(&mqfs_data.mi_lock);
return (error);
}
static int
mqf_chown(struct file *fp, uid_t uid, gid_t gid, struct ucred *active_cred,
struct thread *td)
{
struct mqfs_node *pn;
int error;
error = 0;
pn = fp->f_data;
sx_xlock(&mqfs_data.mi_lock);
if (uid == (uid_t)-1)
uid = pn->mn_uid;
if (gid == (gid_t)-1)
gid = pn->mn_gid;
if (((uid != pn->mn_uid && uid != active_cred->cr_uid) ||
(gid != pn->mn_gid && !groupmember(gid, active_cred))) &&
(error = priv_check_cred(active_cred, PRIV_VFS_CHOWN, 0)))
goto out;
pn->mn_uid = uid;
pn->mn_gid = gid;
out:
sx_xunlock(&mqfs_data.mi_lock);
return (error);
}
static int
mqf_kqfilter(struct file *fp, struct knote *kn)
{
struct mqueue *mq = FPTOMQ(fp);
int error = 0;
if (kn->kn_filter == EVFILT_READ) {
kn->kn_fop = &mq_rfiltops;
knlist_add(&mq->mq_rsel.si_note, kn, 0);
} else if (kn->kn_filter == EVFILT_WRITE) {
kn->kn_fop = &mq_wfiltops;
knlist_add(&mq->mq_wsel.si_note, kn, 0);
} else
error = EINVAL;
return (error);
}
static void
filt_mqdetach(struct knote *kn)
{
struct mqueue *mq = FPTOMQ(kn->kn_fp);
if (kn->kn_filter == EVFILT_READ)
knlist_remove(&mq->mq_rsel.si_note, kn, 0);
else if (kn->kn_filter == EVFILT_WRITE)
knlist_remove(&mq->mq_wsel.si_note, kn, 0);
else
panic("filt_mqdetach");
}
static int
filt_mqread(struct knote *kn, long hint)
{
struct mqueue *mq = FPTOMQ(kn->kn_fp);
mtx_assert(&mq->mq_mutex, MA_OWNED);
return (mq->mq_curmsgs != 0);
}
static int
filt_mqwrite(struct knote *kn, long hint)
{
struct mqueue *mq = FPTOMQ(kn->kn_fp);
mtx_assert(&mq->mq_mutex, MA_OWNED);
return (mq->mq_curmsgs < mq->mq_maxmsg);
}
static struct fileops mqueueops = {
.fo_read = mqf_read,
.fo_write = mqf_write,
.fo_truncate = mqf_truncate,
.fo_ioctl = mqf_ioctl,
.fo_poll = mqf_poll,
.fo_kqfilter = mqf_kqfilter,
.fo_stat = mqf_stat,
.fo_chmod = mqf_chmod,
.fo_chown = mqf_chown,
.fo_close = mqf_close
};
static struct vop_vector mqfs_vnodeops = {
.vop_default = &default_vnodeops,
.vop_access = mqfs_access,
.vop_cachedlookup = mqfs_lookup,
.vop_lookup = vfs_cache_lookup,
.vop_reclaim = mqfs_reclaim,
.vop_create = mqfs_create,
.vop_remove = mqfs_remove,
.vop_inactive = mqfs_inactive,
.vop_open = mqfs_open,
.vop_close = mqfs_close,
.vop_getattr = mqfs_getattr,
.vop_setattr = mqfs_setattr,
.vop_read = mqfs_read,
.vop_write = VOP_EOPNOTSUPP,
.vop_readdir = mqfs_readdir,
.vop_mkdir = VOP_EOPNOTSUPP,
.vop_rmdir = VOP_EOPNOTSUPP
};
static struct vfsops mqfs_vfsops = {
.vfs_init = mqfs_init,
.vfs_uninit = mqfs_uninit,
.vfs_mount = mqfs_mount,
.vfs_unmount = mqfs_unmount,
.vfs_root = mqfs_root,
.vfs_statfs = mqfs_statfs,
};
static struct vfsconf mqueuefs_vfsconf = {
.vfc_version = VFS_VERSION,
.vfc_name = "mqueuefs",
.vfc_vfsops = &mqfs_vfsops,
.vfc_typenum = -1,
.vfc_flags = VFCF_SYNTHETIC
};
static struct syscall_helper_data mq_syscalls[] = {
SYSCALL_INIT_HELPER(kmq_open),
SYSCALL_INIT_HELPER(kmq_setattr),
SYSCALL_INIT_HELPER(kmq_timedsend),
SYSCALL_INIT_HELPER(kmq_timedreceive),
SYSCALL_INIT_HELPER(kmq_notify),
SYSCALL_INIT_HELPER(kmq_unlink),
SYSCALL_INIT_LAST
};
#ifdef COMPAT_FREEBSD32
#include <compat/freebsd32/freebsd32.h>
#include <compat/freebsd32/freebsd32_proto.h>
#include <compat/freebsd32/freebsd32_syscall.h>
#include <compat/freebsd32/freebsd32_util.h>
static void
mq_attr_from32(const struct mq_attr32 *from, struct mq_attr *to)
{
to->mq_flags = from->mq_flags;
to->mq_maxmsg = from->mq_maxmsg;
to->mq_msgsize = from->mq_msgsize;
to->mq_curmsgs = from->mq_curmsgs;
}
static void
mq_attr_to32(const struct mq_attr *from, struct mq_attr32 *to)
{
to->mq_flags = from->mq_flags;
to->mq_maxmsg = from->mq_maxmsg;
to->mq_msgsize = from->mq_msgsize;
to->mq_curmsgs = from->mq_curmsgs;
}
int
freebsd32_kmq_open(struct thread *td, struct freebsd32_kmq_open_args *uap)
{
struct mq_attr attr;
struct mq_attr32 attr32;
int flags, error;
if ((uap->flags & O_ACCMODE) == O_ACCMODE)
return (EINVAL);
flags = FFLAGS(uap->flags);
if ((flags & O_CREAT) != 0 && uap->attr != NULL) {
error = copyin(uap->attr, &attr32, sizeof(attr32));
if (error)
return (error);
mq_attr_from32(&attr32, &attr);
}
return (kern_kmq_open(td, uap->path, flags, uap->mode,
uap->attr != NULL ? &attr : NULL));
}
int
freebsd32_kmq_setattr(struct thread *td, struct freebsd32_kmq_setattr_args *uap)
{
struct mq_attr attr, oattr;
struct mq_attr32 attr32, oattr32;
int error;
if (uap->attr != NULL) {
error = copyin(uap->attr, &attr32, sizeof(attr32));
if (error != 0)
return (error);
mq_attr_from32(&attr32, &attr);
}
error = kern_kmq_setattr(td, uap->mqd, uap->attr != NULL ? &attr : NULL,
&oattr);
if (error != 0)
return (error);
if (uap->oattr != NULL) {
mq_attr_to32(&oattr, &oattr32);
error = copyout(&oattr32, uap->oattr, sizeof(oattr32));
}
return (error);
}
int
freebsd32_kmq_timedsend(struct thread *td,
struct freebsd32_kmq_timedsend_args *uap)
{
struct mqueue *mq;
struct file *fp;
struct timespec32 ets32;
struct timespec *abs_timeout, ets;
int error;
int waitok;
error = getmq_read(td, uap->mqd, &fp, NULL, &mq);
if (error)
return (error);
if (uap->abs_timeout != NULL) {
error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
if (error != 0)
return (error);
CP(ets32, ets, tv_sec);
CP(ets32, ets, tv_nsec);
abs_timeout = &ets;
} else
abs_timeout = NULL;
waitok = !(fp->f_flag & O_NONBLOCK);
error = mqueue_send(mq, uap->msg_ptr, uap->msg_len,
uap->msg_prio, waitok, abs_timeout);
fdrop(fp, td);
return (error);
}
int
freebsd32_kmq_timedreceive(struct thread *td,
struct freebsd32_kmq_timedreceive_args *uap)
{
struct mqueue *mq;
struct file *fp;
struct timespec32 ets32;
struct timespec *abs_timeout, ets;
int error, waitok;
error = getmq_write(td, uap->mqd, &fp, NULL, &mq);
if (error)
return (error);
if (uap->abs_timeout != NULL) {
error = copyin(uap->abs_timeout, &ets32, sizeof(ets32));
if (error != 0)
return (error);
CP(ets32, ets, tv_sec);
CP(ets32, ets, tv_nsec);
abs_timeout = &ets;
} else
abs_timeout = NULL;
waitok = !(fp->f_flag & O_NONBLOCK);
error = mqueue_receive(mq, uap->msg_ptr, uap->msg_len,
uap->msg_prio, waitok, abs_timeout);
fdrop(fp, td);
return (error);
}
static struct syscall_helper_data mq32_syscalls[] = {
SYSCALL32_INIT_HELPER(freebsd32_kmq_open),
SYSCALL32_INIT_HELPER(freebsd32_kmq_setattr),
SYSCALL32_INIT_HELPER(freebsd32_kmq_timedsend),
SYSCALL32_INIT_HELPER(freebsd32_kmq_timedreceive),
SYSCALL32_INIT_HELPER_COMPAT(kmq_notify),
SYSCALL32_INIT_HELPER_COMPAT(kmq_unlink),
SYSCALL_INIT_LAST
};
#endif
static int
mqinit(void)
{
int error;
error = syscall_helper_register(mq_syscalls);
if (error != 0)
return (error);
#ifdef COMPAT_FREEBSD32
error = syscall32_helper_register(mq32_syscalls);
if (error != 0)
return (error);
#endif
return (0);
}
static int
mqunload(void)
{
#ifdef COMPAT_FREEBSD32
syscall32_helper_unregister(mq32_syscalls);
#endif
syscall_helper_unregister(mq_syscalls);
return (0);
}
static int
mq_modload(struct module *module, int cmd, void *arg)
{
int error = 0;
error = vfs_modevent(module, cmd, arg);
if (error != 0)
return (error);
switch (cmd) {
case MOD_LOAD:
error = mqinit();
if (error != 0)
mqunload();
break;
case MOD_UNLOAD:
error = mqunload();
break;
default:
break;
}
return (error);
}
static moduledata_t mqueuefs_mod = {
"mqueuefs",
mq_modload,
&mqueuefs_vfsconf
};
DECLARE_MODULE(mqueuefs, mqueuefs_mod, SI_SUB_VFS, SI_ORDER_MIDDLE);
MODULE_VERSION(mqueuefs, 1);
|