1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
|
#!/bin/sh
#
# Copyright (c) 2010-2013 Baptiste Daroussin <bapt@FreeBSD.org>
# Copyright (c) 2010-2011 Julien Laffaye <jlaffaye@FreeBSD.org>
# Copyright (c) 2012-2017 Bryan Drewery <bdrewery@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.
BSDPLATFORM=`uname -s | tr '[:upper:]' '[:lower:]'`
. ${SCRIPTPREFIX}/include/common.sh.${BSDPLATFORM}
BLACKLIST=""
EX_SOFTWARE=70
# Return true if ran from bulk/testport, ie not daemon/status/jail
was_a_bulk_run() {
[ "${SCRIPTPATH##*/}" = "bulk.sh" -o "${SCRIPTPATH##*/}" \
= "testport.sh" ]
}
# Return true if in a bulk or other jail run that needs to shutdown the jail
was_a_jail_run() {
was_a_bulk_run || [ "${SCRIPTPATH##*/}" = "pkgclean.sh" ]
}
# Return true if output via msg() should show elapsed time
should_show_elapsed() {
[ -z "${TIME_START}" ] && return 1
[ "${NO_ELAPSED_IN_MSG:-0}" -eq 1 ] && return 1
case "${SCRIPTPATH##*/}" in
daemon.sh) ;;
help.sh) ;;
queue.sh) ;;
status.sh) ;;
version.sh) ;;
*) return 0 ;;
esac
return 1
}
not_for_os() {
local os=$1
shift
[ "${os}" = "${BSDPLATFORM}" ] && err 1 "This is not supported on ${BSDPLATFORM}: $@"
}
err() {
trap '' SIGINFO
export CRASHED=1
if [ $# -ne 2 ]; then
err 1 "err expects 2 arguments: exit_number \"message\""
fi
# Try to set status so other processes know this crashed
# Don't set it from children failures though, only master
if [ -z "${PARALLEL_CHILD}" ] && was_a_bulk_run; then
if [ -n "${MY_JOBID}" ]; then
bset ${MY_JOBID} status "${EXIT_STATUS:-crashed:}" \
2>/dev/null || :
else
bset status "${EXIT_STATUS:-crashed:}" 2>/dev/null || :
fi
fi
if [ ${1} -eq 0 ]; then
msg "$2" || :
else
msg_error "$2" || :
fi
# Avoid recursive err()->exit_handler()->err()... Just let
# exit_handler() cleanup.
if [ ${ERRORS_ARE_FATAL:-1} -eq 1 ]; then
exit $1
else
return 0
fi
}
# Message functions that depend on VERBOSE are stubbed out in post_getopts.
msg_n() {
local -; set +x
local now elapsed
elapsed=
if should_show_elapsed; then
now=$(clock -monotonic)
calculate_duration elapsed "$((${now} - ${TIME_START:-0}))"
elapsed="[${elapsed}] "
fi
if [ -n "${COLOR_ARROW}" ] || [ -z "${1##*\033[*}" ]; then
printf "${elapsed}${DRY_MODE}${COLOR_ARROW}====>>${COLOR_RESET} ${1}${COLOR_RESET_REAL}"
else
printf "${elapsed}${DRY_MODE}====>> ${1}"
fi
}
msg() {
msg_n "$@""\n"
}
msg_verbose() {
msg_n "$@""\n"
}
msg_error() {
local -; set +x
if [ -n "${MY_JOBID}" ]; then
# Send colored msg to bulk log...
COLOR_ARROW="${COLOR_ERROR}" job_msg "${COLOR_ERROR}Error: $1"
# And non-colored to buld log
msg "Error: $1" >&2
elif [ ${OUTPUT_REDIRECTED:-0} -eq 1 ]; then
# Send to true stderr
COLOR_ARROW="${COLOR_ERROR}" msg "${COLOR_ERROR}Error: $1" >&4
else
COLOR_ARROW="${COLOR_ERROR}" msg "${COLOR_ERROR}Error: $1" >&2
fi
return 0
}
msg_dev() {
COLOR_ARROW="${COLOR_DEV}" \
msg_n "${COLOR_DEV}Dev: $@""\n" >&2
}
msg_debug() {
COLOR_ARROW="${COLOR_DEBUG}" \
msg_n "${COLOR_DEBUG}Debug: $@""\n" >&2
}
msg_warn() {
COLOR_ARROW="${COLOR_WARN}" \
msg_n "${COLOR_WARN}Warning: $@""\n" >&2
}
job_msg() {
local -; set +x
local now elapsed NO_ELAPSED_IN_MSG output
if [ -n "${MY_JOBID}" ]; then
NO_ELAPSED_IN_MSG=0
now=$(clock -monotonic)
calculate_duration elapsed "$((${now} - ${TIME_START_JOB:-${TIME_START:-0}}))"
output="[${COLOR_JOBID}${MY_JOBID}${COLOR_RESET}][${elapsed}] $1"
else
output="$@"
fi
if [ ${OUTPUT_REDIRECTED:-0} -eq 1 ]; then
# Send to true stdout (not any build log)
msg_n "${output}\n" >&3
else
msg_n "${output}\n"
fi
}
# Stubbed until post_getopts
job_msg_verbose() {
local -; set +x
job_msg "$@"
}
job_msg_warn() {
COLOR_ARROW="${COLOR_WARN}" \
job_msg "${COLOR_WARN}Warning: $@"
}
prompt() {
[ $# -eq 1 ] || eargs prompt message
local message="$1"
local answer
msg_n "${message} [y/N] "
read answer
case "${answer}" in
[Yy][Ee][Ss]|[Yy][Ee]|[Yy])
return 0
;;
esac
return 1
}
confirm_if_tty() {
[ $# -eq 1 ] || eargs confirm_if_tty message
local message="${1}"
[ -t 0 ] || return 0
prompt "${message}"
}
# Handle needs after processing arguments.
post_getopts() {
# Short-circuit verbose functions to save CPU
if ! [ ${VERBOSE} -gt 2 ]; then
msg_dev() { }
fi
if ! [ ${VERBOSE} -gt 1 ]; then
msg_debug() { }
fi
if ! [ ${VERBOSE} -gt 0 ]; then
msg_verbose() { }
job_msg_verbose() { }
fi
}
_mastermnt() {
local hashed_name mnt mnttest mnamelen
mnamelen=$(grep "#define[[:space:]]MNAMELEN" \
/usr/include/sys/mount.h 2>/dev/null | awk '{print $3}')
mnt="${POUDRIERE_DATA}/.m/${MASTERNAME}/ref"
mnttest="${mnt}/compat/linux/proc"
if [ -n "${mnamelen}" ] && \
[ ${#mnttest} -ge $((${mnamelen} - 1)) ]; then
hashed_name=$(sha256 -qs "${MASTERNAME}" | \
awk '{print substr($0, 0, 6)}')
mnt="${POUDRIERE_DATA}/.m/${hashed_name}/ref"
mnttest="${mnt}/var/db/ports"
[ ${#mnttest} -ge $((${mnamelen} - 1)) ] && \
err 1 "Mountpath '${mnt}' exceeds system MNAMELEN limit of ${mnamelen}. Unable to mount. Try shortening BASEFS."
msg_warn "MASTERNAME '${MASTERNAME}' too long for mounting, using hashed version of '${hashed_name}'"
fi
setvar "$1" "${mnt}"
# MASTERMNTROOT
setvar "${1}ROOT" "${mnt%/ref}"
}
_my_path() {
if [ -z "${MY_JOBID}" ]; then
setvar "$1" "${MASTERMNT}"
elif [ -n "${MASTERMNTROOT}" ]; then
setvar "$1" "${MASTERMNTROOT}/${MY_JOBID}"
else
setvar "$1" "${MASTERMNT}/../${MY_JOBID}"
fi
}
_my_name() {
setvar "$1" "${MASTERNAME}${MY_JOBID+-job-${MY_JOBID}}"
}
_log_path_top() {
setvar "$1" "${POUDRIERE_DATA}/logs/${POUDRIERE_BUILD_TYPE}"
}
_log_path_jail() {
local log_path_top
_log_path_top log_path_top
setvar "$1" "${log_path_top}/${MASTERNAME}"
}
_log_path() {
local log_path_jail
_log_path_jail log_path_jail
setvar "$1" "${log_path_jail}/${BUILDNAME}"
}
# Call function with vars set:
# log MASTERNAME BUILDNAME jailname ptname setname
for_each_build() {
[ -n "${BUILDNAME_GLOB}" ] || \
err 1 "for_each_build requires BUILDNAME_GLOB"
[ -n "${SHOW_FINISHED}" ] || \
err 1 "for_each_build requires SHOW_FINISHED"
[ $# -eq 1 ] || eargs for_each_build action
local action="$1"
local MASTERNAME BUILDNAME buildname jailname ptname setname
local log_top ret
POUDRIERE_BUILD_TYPE="bulk" _log_path_top log_top
[ -d "${log_top}" ] || err 1 "Log path ${log_top} does not exist."
cd ${log_top}
found_jobs=0
ret=0
for mastername in *; do
# Check empty dir
case "${mastername}" in
"*") break ;;
esac
[ -L "${mastername}/latest" ] || continue
MASTERNAME=${mastername}
[ "${MASTERNAME}" = "latest-per-pkg" ] && continue
[ ${SHOW_FINISHED} -eq 0 ] && ! jail_runs ${MASTERNAME} && \
continue
# Look for all wanted buildnames (will be 1 or Many(-a)))
for buildname in ${mastername}/${BUILDNAME_GLOB}; do
# Check for no match. If not using a glob ensure the
# file exists otherwise check for the glob coming back
if [ "${BUILDNAME_GLOB%\**}" != \
"${BUILDNAME_GLOB}" ]; then
case "${buildname}" in
# Check no results
"${mastername}/${BUILDNAME_GLOB}")
break
;;
# Skip latest if from a glob, let it be
# found normally.
"${mastername}/latest")
continue
;;
# Don't want latest-per-pkg
"${mastername}/latest-per-pkg")
continue
;;
esac
else
# No match
[ -e "${buildname}" ] || break
fi
buildname="${buildname#${mastername}/}"
BUILDNAME="${buildname}"
# Unset so later they can be checked for NULL (don't
# want to lookup again if value looked up is empty
unset jailname ptname setname
# Try matching on any given JAILNAME/PTNAME/SETNAME,
# and if any don't match skip this MASTERNAME entirely.
# If the file is missing it's a legacy build, skip it
# but not the entire mastername if it has a match.
if [ -n "${JAILNAME}" ]; then
if _bget jailname jailname 2>/dev/null; then
[ "${jailname}" = "${JAILNAME}" ] || \
continue 2
else
case "${MASTERNAME}" in
${JAILNAME}-*) ;;
*) continue 2 ;;
esac
continue
fi
fi
if [ -n "${PTNAME}" ]; then
if _bget ptname ptname 2>/dev/null; then
[ "${ptname}" = "${PTNAME}" ] || \
continue 2
else
case "${MASTERNAME}" in
*-${PTNAME}) ;;
*) continue 2 ;;
esac
continue
fi
fi
if [ -n "${SETNAME}" ]; then
if _bget setname setname 2>/dev/null; then
[ "${setname}" = "${SETNAME%0}" ] || \
continue 2
else
case "${MASTERNAME}" in
*-${SETNAME%0}) ;;
*) continue 2 ;;
esac
continue
fi
fi
# Dereference latest into actual buildname
[ "${buildname}" = "latest" ] && \
_bget BUILDNAME buildname 2>/dev/null
# May be blank if build is still starting up
[ -z "${BUILDNAME}" ] && continue 2
found_jobs=$((${found_jobs} + 1))
# Lookup jailname/setname/ptname if needed. Delayed
# from earlier for performance for -a
[ -z "${jailname+null}" ] && \
_bget jailname jailname 2>/dev/null || :
[ -z "${setname+null}" ] && \
_bget setname setname 2>/dev/null || :
[ -z "${ptname+null}" ] && \
_bget ptname ptname 2>/dev/null || :
log=${mastername}/${BUILDNAME}
${action} || ret=$?
# Skip the rest of this build if return = 100
[ ${ret} -eq 100 ] && continue 2
# Halt if the function requests it
[ ${ret} -eq 101 ] && break 2
done
done
cd ${OLDPWD}
return ${ret}
}
stat_humanize() {
xargs -0 stat -f '%i %b' | \
sort -u | \
awk -vbsize=512 '{total += $2} END {print total*bsize}' | \
awk -f ${AWKPREFIX}/humanize.awk
}
do_confirm_delete() {
[ $# -eq 4 ] || eargs do_confirm_delete badfiles_list \
reason_plural_object answer DRY_RUN
local filelist="$1"
local reason="$2"
local answer="$3"
local DRY_RUN="$4"
local file_cnt hsize ret
file_cnt=$(wc -l ${filelist} | awk '{print $1}')
if [ ${file_cnt} -eq 0 ]; then
msg "No ${reason} to cleanup"
return 2
fi
msg_n "Calculating size for found files..."
hsize=$(cat ${filelist} | \
tr '\n' '\000' | \
xargs -0 -J % find % -print0 | \
stat_humanize)
echo " done"
msg "These ${reason} will be deleted:"
cat ${filelist}
msg "Removing these ${reason} will free: ${hsize}"
if [ ${DRY_RUN} -eq 1 ]; then
msg "Dry run: not cleaning anything."
return 2
fi
if [ -z "${answer}" ]; then
prompt "Proceed?" && answer="yes"
fi
ret=0
if [ "${answer}" = "yes" ]; then
msg_n "Removing files..."
cat ${filelist} | tr '\n' '\000' | \
xargs -0 -J % \
find % -mindepth 0 -maxdepth 0 -exec rm -rf {} +
echo " done"
ret=1
fi
return ${ret}
}
# It may be defined as a NOP for tests
if ! type injail >/dev/null 2>&1; then
injail() {
if [ "${USE_JEXECD}" = "no" ]; then
injail_tty "$@"
else
local name
_my_name name
[ -n "${name}" ] || err 1 "No jail setup"
rexec -s ${MASTERMNT}/../${name}${JNETNAME:+-${JNETNAME}}.sock \
-u ${JUSER:-root} "$@"
fi
}
fi
injail_tty() {
local name
_my_name name
[ -n "${name}" ] || err 1 "No jail setup"
if [ ${JEXEC_LIMITS:-0} -eq 1 ]; then
jexec -U ${JUSER:-root} ${name}${JNETNAME:+-${JNETNAME}} \
${JEXEC_LIMITS+/usr/bin/limits} \
${MAX_MEMORY_BYTES:+-v ${MAX_MEMORY_BYTES}} \
${MAX_FILES:+-n ${MAX_FILES}} \
"$@"
else
jexec -U ${JUSER:-root} ${name}${JNETNAME:+-${JNETNAME}} \
"$@"
fi
}
jstart() {
local name network
network="${localipargs}"
[ "${RESTRICT_NETWORKING}" = "yes" ] || network="${ipargs}"
_my_name name
jail -c persist name=${name} \
path=${MASTERMNT}${MY_JOBID+/../${MY_JOBID}} \
host.hostname=${BUILDER_HOSTNAME-${name}} \
${network} ${JAIL_PARAMS} \
allow.socket_af allow.raw_sockets allow.chflags allow.sysvipc
[ "${USE_JEXECD}" = "yes" ] && \
jexecd -j ${name} -d ${MASTERMNT}/../ \
${MAX_MEMORY_BYTES+-m ${MAX_MEMORY_BYTES}} \
${MAX_FILES+-n ${MAX_FILES}}
jail -c persist name=${name}-n \
path=${MASTERMNT}${MY_JOBID+/../${MY_JOBID}} \
host.hostname=${BUILDER_HOSTNAME-${name}} \
${ipargs} ${JAIL_PARAMS} \
allow.socket_af allow.raw_sockets allow.chflags allow.sysvipc
[ "${USE_JEXECD}" = "yes" ] && \
jexecd -j ${name}-n -d ${MASTERMNT}/../ \
${MAX_MEMORY_BYTES+-m ${MAX_MEMORY_BYTES}} \
${MAX_FILES+-n ${MAX_FILES}}
return 0
}
jail_has_processes() {
local pscnt
# 2 = HEADER+ps itself
pscnt=2
[ "${USE_JEXECD}" = "yes" ] && pscnt=4
# Cannot use ps -J here as not all versions support it.
if [ $(injail ps aux | wc -l) -ne ${pscnt} ]; then
return 0
fi
return 1
}
jkill_wait() {
injail kill -9 -1 2>/dev/null || :
while jail_has_processes; do
sleep 1
injail kill -9 -1 2>/dev/null || :
done
}
# Kill everything in the jail and ensure it is free of any processes
# before returning.
jkill() {
jkill_wait
JNETNAME="n" jkill_wait
}
jstop() {
local name
_my_name name
jail -r ${name} 2>/dev/null || :
jail -r ${name}-n 2>/dev/null || :
}
eargs() {
local fname="$1"
shift
case $# in
0) err ${EX_SOFTWARE} "${fname}: No arguments expected" ;;
1) err ${EX_SOFTWARE} "${fname}: 1 argument expected: $1" ;;
*) err ${EX_SOFTWARE} "${fname}: $# arguments expected: $*" ;;
esac
}
run_hook() {
local hookfile="${HOOKDIR}/${1}.sh"
local build_url log_url
shift
build_url build_url || :
log_url log_url || :
if [ -f "${hookfile}" ]; then
(
cd /
BUILD_URL="${build_url}" \
LOG_URL="${log_url}" \
POUDRIERE_BUILD_TYPE=${POUDRIERE_BUILD_TYPE} \
POUDRIERED="${POUDRIERED}" \
POUDRIERE_DATA="${POUDRIERE_DATA}" \
MASTERNAME="${MASTERNAME}" \
MASTERMNT="${MASTERMNT}" \
MY_JOBID="${MY_JOBID}" \
BUILDNAME="${BUILDNAME}" \
JAILNAME="${JAILNAME}" \
PTNAME="${PTNAME}" \
SETNAME="${SETNAME}" \
PACKAGES="${PACKAGES}" \
PACKAGES_ROOT="${PACKAGES_ROOT}" \
/bin/sh "${hookfile}" "$@"
)
fi
return 0
}
log_start() {
[ $# -eq 1 ] || eargs log_start need_tee
local need_tee="$1"
local log log_top
local latest_log
_log_path log
_log_path_top log_top
logfile="${log}/logs/${PKGNAME}.log"
latest_log=${log_top}/latest-per-pkg/${PKGNAME%-*}/${PKGNAME##*-}
# Make sure directory exists
mkdir -p ${log}/logs ${latest_log}
:> ${logfile}
# Link to BUILD_TYPE/latest-per-pkg/PORTNAME/PKGVERSION/MASTERNAME.log
ln -f ${logfile} ${latest_log}/${MASTERNAME}.log
# Link to JAIL/latest-per-pkg/PKGNAME.log
ln -f ${logfile} ${log}/../latest-per-pkg/${PKGNAME}.log
# Save stdout/stderr for restoration later for bulk/testport -i
exec 3>&1 4>&2
OUTPUT_REDIRECTED=1
# Pipe output to tee(1) or timestamp if needed.
if [ ${need_tee} -eq 1 ] || [ "${TIMESTAMP_LOGS}" = "yes" ]; then
[ ! -e ${logfile}.pipe ] && mkfifo ${logfile}.pipe
if [ ${need_tee} -eq 1 ]; then
if [ "${TIMESTAMP_LOGS}" = "yes" ]; then
timestamp < ${logfile}.pipe | tee ${logfile} &
else
tee ${logfile} < ${logfile}.pipe &
fi
elif [ "${TIMESTAMP_LOGS}" = "yes" ]; then
timestamp > ${logfile} < ${logfile}.pipe &
fi
tpid=$!
exec > ${logfile}.pipe 2>&1
# Remove fifo pipe file right away to avoid orphaning it.
# The pipe will continue to work as long as we keep
# the FD open to it.
rm -f ${logfile}.pipe
else
# Send output directly to file.
tpid=
exec > ${logfile} 2>&1
fi
}
buildlog_start() {
local portdir=$1
local mnt
local var
_my_path mnt
echo "build started at $(date)"
echo "port directory: ${portdir}"
echo "building for: $(injail uname -a)"
echo "maintained by: $(injail /usr/bin/make -C ${portdir} maintainer)"
echo "Makefile ident: $(ident -q ${mnt}/${portdir}/Makefile|sed -n '2,2p')"
echo "Poudriere version: ${POUDRIERE_VERSION}"
echo "Host OSVERSION: ${HOST_OSVERSION}"
echo "Jail OSVERSION: ${JAIL_OSVERSION}"
echo "Job Id: ${MY_JOBID}"
echo
if [ ${JAIL_OSVERSION} -gt ${HOST_OSVERSION} ]; then
echo
echo
echo
echo "!!! Jail is newer than host. (Jail: ${JAIL_OSVERSION}, Host: ${HOST_OSVERSION}) !!!"
echo "!!! This is not supported. !!!"
echo "!!! Host kernel must be same or newer than jail. !!!"
echo "!!! Expect build failures. !!!"
echo
echo
echo
fi
echo "---Begin Environment---"
injail /usr/bin/env
echo "---End Environment---"
echo ""
echo "---Begin Poudriere Port Flags/Env---"
echo "PORT_FLAGS=${PORT_FLAGS}"
echo "PKGENV=${PKGENV}"
echo "---End Poudriere Port Flags/Env---"
echo ""
echo "---Begin OPTIONS List---"
injail /usr/bin/make -C ${portdir} showconfig || :
echo "---End OPTIONS List---"
echo ""
for var in CONFIGURE_ARGS CONFIGURE_ENV MAKE_ENV; do
echo "--${var}--"
echo "$(injail /usr/bin/env ${PORT_FLAGS} /usr/bin/make -C ${portdir} -V ${var})"
echo "--End ${var}--"
echo ""
done
echo "--PLIST_SUB--"
echo "$(injail /usr/bin/env ${PORT_FLAGS} /usr/bin/make -C ${portdir} -V PLIST_SUB | tr ' ' '\n' | grep -v '^$')"
echo "--End PLIST_SUB--"
echo ""
echo "--SUB_LIST--"
echo "$(injail /usr/bin/env ${PORT_FLAGS} /usr/bin/make -C ${portdir} -V SUB_LIST | tr ' ' '\n' | grep -v '^$')"
echo "--End SUB_LIST--"
echo ""
echo "---Begin make.conf---"
cat ${mnt}/etc/make.conf
echo "---End make.conf---"
if [ -f "${mnt}/etc/make.nxb.conf" ]; then
echo "---Begin make.nxb.conf---"
cat ${mnt}/etc/make.nxb.conf
echo "---End make.nxb.conf---"
fi
echo "--Resource limits--"
injail /bin/sh -c "ulimit -a" || :
echo "--End resource limits--"
}
buildlog_stop() {
[ $# -eq 3 ] || eargs buildlog_stop pkgname origin build_failed
local pkgname="$1"
local origin=$2
local build_failed="$3"
local log
local buildtime
_log_path log
buildtime=$( \
stat -f '%N %B' ${log}/logs/${pkgname}.log | awk -v now=$(clock -epoch) \
-f ${AWKPREFIX}/siginfo_buildtime.awk |
awk -F'!' '{print $2}' \
)
echo "build of ${origin} ended at $(date)"
echo "build time: ${buildtime}"
[ ${build_failed} -gt 0 ] && echo "!!! build failure encountered !!!"
return 0
}
log_stop() {
if [ ${OUTPUT_REDIRECTED:-0} -eq 1 ]; then
exec 1>&3 3>&- 2>&4 4>&-
OUTPUT_REDIRECTED=0
fi
if [ -n "${tpid}" ]; then
# Give tee a moment to flush buffers
timed_wait_and_kill 5 $tpid || :
unset tpid
fi
}
read_file() {
[ $# -eq 2 ] || eargs read_file var_return file
local var_return="$1"
local file="$2"
local _data line
local ret -
# var_return may be empty if only $_read_file_lines_read is being
# used.
set +e
_data=
_read_file_lines_read=0
if [ ${READ_FILE_USE_CAT:-0} -eq 1 ]; then
if [ -f "${file}" ]; then
if [ -n "${var_return}" ]; then
_data="$(cat "${file}")"
fi
_read_file_lines_read=$(wc -l < "${file}")
_read_file_lines_read=${_read_file_lines_read##* }
ret=0
else
ret=1
fi
else
while :; do
read -r line
ret=$?
case ${ret} in
# Success, process data and keep reading.
0) ;;
# EOF
1)
ret=0
break
;;
# Some error or interruption/signal. Reread.
*) continue ;;
esac
if [ -n "${var_return}" ]; then
# Add extra newline
[ ${_read_file_lines_read} -gt 0 ] && \
_data="${_data}
"
_data="${_data}${line}"
fi
_read_file_lines_read=$((${_read_file_lines_read} + 1))
done < "${file}" || ret=$?
fi
if [ -n "${var_return}" ]; then
setvar "${var_return}" "${_data}"
fi
return ${ret}
}
attr_set() {
local type=$1
local name=$2
local property=$3
shift 3
mkdir -p ${POUDRIERED}/${type}/${name}
echo "$@" > ${POUDRIERED}/${type}/${name}/${property} || :
}
jset() { attr_set jails "$@" ; }
pset() { attr_set ports "$@" ; }
_attr_get() {
[ $# -eq 4 ] || eargs _attr_get var_return type name property
local var_return="$1"
local type="$2"
local name="$3"
local property="$4"
read_file "${var_return}" \
"${POUDRIERED}/${type}/${name}/${property}" && return 0
setvar "${var_return}" ""
return 1
}
attr_get() {
local attr_get_data
if _attr_get attr_get_data "$@"; then
[ -n "${attr_get_data}" ] && echo "${attr_get_data}"
return 0
fi
return 1
}
jget() { attr_get jails "$@" ; }
_jget() {
[ $# -eq 3 ] || eargs _jget var_return ptname property
local var_return="$1"
shift
_attr_get "${var_return}" jails "$@"
}
pget() { attr_get ports "$@" ; }
_pget() {
[ $# -eq 3 ] || eargs _pget var_return ptname property
local var_return="$1"
shift
_attr_get "${var_return}" ports "$@"
}
#build getter/setter
_bget() {
local var_return id property mnt log file READ_FILE_USE_CAT
var_return="$1"
_log_path log
shift
if [ $# -eq 2 ]; then
id="$1"
shift
fi
file=".poudriere.${1}${id:+.${id}}"
# Use cat(1) to read long list files.
[ -z "${1##ports.*}" ] && READ_FILE_USE_CAT=1
read_file "${var_return}" "${log}/${file}" && return 0
# It may be empty if only a count was being looked up
# via $_read_file_lines_read hack.
if [ -n "${var_return}" ]; then
setvar "${var_return}" ""
fi
return 1
}
bget() {
local bget_data
if _bget bget_data "$@"; then
[ -n "${bget_data}" ] && echo "${bget_data}"
return 0
fi
return 1
}
bset() {
was_a_bulk_run || return 0
local id property mnt log
_log_path log
if [ $# -eq 3 ]; then
id=$1
shift
fi
property="$1"
file=.poudriere.${property}${id:+.${id}}
shift
[ "${property}" = "status" ] && \
echo "$@" >> ${log}/${file}.journal% || :
echo "$@" > "${log}/${file}"
}
bset_job_status() {
[ $# -eq 2 ] || eargs bset_job_status status origin
local status="$1"
local origin="$2"
bset ${MY_JOBID} status "${status}:${origin}:${PKGNAME}:${TIME_START_JOB:-${TIME_START}}:$(clock -monotonic)"
}
badd() {
local id property mnt log
_log_path log
if [ $# -eq 3 ]; then
id=$1
shift
fi
file=.poudriere.${1}${id:+.${id}}
shift
echo "$@" >> ${log}/${file} || :
}
update_stats() {
local type unused
local -
if [ -n "${update_stats_done}" ]; then
return 0
fi
set +e
lock_acquire update_stats || return 1
for type in built failed ignored; do
_bget '' "ports.${type}"
bset "stats_${type}" ${_read_file_lines_read}
done
# Skipped may have duplicates in it
bset stats_skipped $(bget ports.skipped | awk '{print $1}' | \
sort -u | wc -l)
lock_release update_stats
}
sigpipe_handler() {
EXIT_STATUS="sigpipe:"
SIGNAL="SIGPIPE"
sig_handler
}
sigint_handler() {
EXIT_STATUS="sigint:"
SIGNAL="SIGINT"
sig_handler
}
sigterm_handler() {
EXIT_STATUS="sigterm:"
SIGNAL="SIGTERM"
sig_handler
}
sig_handler() {
# Reset SIGTERM handler, just exit if another is received.
trap - SIGTERM
# Ignore SIGPIPE for messages
trap '' SIGPIPE
# Ignore SIGINT while cleaning up
trap '' SIGINT
trap '' SIGINFO
err 1 "Signal ${SIGNAL} caught, cleaning up and exiting"
}
exit_handler() {
# Ignore errors while cleaning up
set +e
ERRORS_ARE_FATAL=0
trap '' SIGINFO
# Avoid recursively cleaning up here
trap - EXIT SIGTERM
# Ignore SIGPIPE for messages
trap '' SIGPIPE
# Ignore SIGINT while cleaning up
trap '' SIGINT
if was_a_bulk_run; then
log_stop
# build_queue may have done cd MASTERMNT/.p/pool,
# but some of the cleanup here assumes we are
# PWD=MASTERMNT/.p. Switch back if possible.
# It will be changed to / in jail_cleanup
if [ -d "${MASTERMNT}/.p" ]; then
cd "${MASTERMNT}/.p"
fi
fi
if was_a_jail_run; then
# Don't use jail for any caching in cleanup
SHASH_VAR_PATH="${SHASH_VAR_PATH_DEFAULT}"
fi
parallel_shutdown
if was_a_bulk_run; then
# build_queue socket
exec 6<&- 6>&- || :
coprocess_stop pkg_cacher
fi
[ ${STATUS} -eq 1 ] && jail_cleanup
if was_a_bulk_run; then
coprocess_stop html_json
if [ ${CREATED_JLOCK:-0} -eq 1 ]; then
update_stats >/dev/null 2>&1 || :
fi
fi
[ -n ${CLEANUP_HOOK} ] && ${CLEANUP_HOOK}
if [ ${CREATED_JLOCK:-0} -eq 1 ]; then
_jlock jlock
rm -rf "${jlock}" 2>/dev/null || :
fi
rm -rf "${POUDRIERE_TMPDIR}" >/dev/null 2>&1 || :
}
build_url() {
if [ -z "${URL_BASE}" ]; then
setvar "$1" ""
return 1
fi
setvar "$1" "${URL_BASE}/build.html?mastername=${MASTERNAME}&build=${BUILDNAME}"
}
log_url() {
if [ -z "${URL_BASE}" ]; then
setvar "$1" ""
return 1
fi
setvar "$1" "${URL_BASE}/data/${MASTERNAME}/${BUILDNAME}/logs"
}
show_log_info() {
local log build_url
_log_path log
msg "Logs: ${log}"
build_url build_url && \
msg "WWW: ${build_url}"
return 0
}
show_build_summary() {
local status nbb nbf nbs nbi nbq ndone nbtobuild buildname
local log now elapsed buildtime queue_width
update_stats 2>/dev/null || return 0
_bget nbq stats_queued 2>/dev/null || nbq=0
_bget status status 2>/dev/null || status=unknown
_bget nbf stats_failed 2>/dev/null || nbf=0
_bget nbi stats_ignored 2>/dev/null || nbi=0
_bget nbs stats_skipped 2>/dev/null || nbs=0
_bget nbb stats_built 2>/dev/null || nbb=0
ndone=$((nbb + nbf + nbi + nbs))
nbtobuild=$((nbq - ndone))
if [ ${nbq} -gt 9999 ]; then
queue_width=5
elif [ ${nbq} -gt 999 ]; then
queue_width=4
elif [ ${nbq} -gt 99 ]; then
queue_width=3
else
queue_width=2
fi
_log_path log
_bget buildname buildname 2>/dev/null || :
now=$(clock -epoch)
calculate_elapsed_from_log ${now} ${log} || return 1
elapsed=${_elapsed_time}
calculate_duration buildtime ${elapsed}
printf "[${MASTERNAME}] [${buildname}] [${status}] \
Queued: %-${queue_width}d ${COLOR_SUCCESS}Built: %-${queue_width}d \
${COLOR_FAIL}Failed: %-${queue_width}d ${COLOR_SKIP}Skipped: \
%-${queue_width}d ${COLOR_IGNORE}Ignored: %-${queue_width}d${COLOR_RESET} \
Tobuild: %-${queue_width}d Time: %s\n" \
${nbq} ${nbb} ${nbf} ${nbs} ${nbi} ${nbtobuild} "${buildtime}"
}
siginfo_handler() {
trappedinfo=1
in_siginfo_handler=1
[ "${POUDRIERE_BUILD_TYPE}" != "bulk" ] && return 0
local status
local now
local j elapsed job_id_color
local pkgname origin phase buildtime started
local format_origin_phase format_phase
local -
set +e
trap '' SIGINFO
_bget status status 2>/dev/null || status=unknown
if [ "${status}" = "index:" -o "${status#stopped:}" = "crashed:" ]; then
enable_siginfo_handler
return 0
fi
_bget nbq stats_queued 2>/dev/null || nbq=0
if [ -z "${nbq}" ]; then
enable_siginfo_handler
return 0
fi
show_build_summary
now=$(clock -monotonic)
# Skip if stopping or starting jobs or stopped.
if [ -n "${JOBS}" -a "${status#starting_jobs:}" = "${status}" \
-a "${status}" != "stopping_jobs:" -a -n "${MASTERMNT}" ] && \
! status_is_stopped "${status}"; then
for j in ${JOBS}; do
# Ignore error here as the zfs dataset may not be cloned yet.
_bget status ${j} status 2>/dev/null || :
# Skip builders not started yet
[ -z "${status}" ] && continue
# Hide idle workers
[ "${status}" = "idle:" ] && continue
phase="${status%%:*}"
status="${status#*:}"
origin="${status%%:*}"
status="${status#*:}"
pkgname="${status%%:*}"
status="${status#*:}"
started="${status%%:*}"
colorize_job_id job_id_color "${j}"
# Must put colors in format
format_origin_phase="\t[${job_id_color}%s${COLOR_RESET}]: ${COLOR_PORT}%-32s ${COLOR_PHASE}%-15s${COLOR_RESET} (%s)\n"
format_phase="\t[${job_id_color}%s${COLOR_RESET}]: ${COLOR_PHASE}%15s${COLOR_RESET}\n"
if [ -n "${pkgname}" ]; then
elapsed=$((${now} - ${started}))
calculate_duration buildtime "${elapsed}"
printf "${format_origin_phase}" "${j}" \
"${origin}" "${phase}" ${buildtime}
else
printf "${format_phase}" "${j}" "${phase}"
fi
done
fi
show_log_info
enable_siginfo_handler
}
jail_exists() {
[ $# -ne 1 ] && eargs jail_exists jailname
local jname=$1
[ -d ${POUDRIERED}/jails/${jname} ] && return 0
return 1
}
jail_runs() {
[ $# -ne 1 ] && eargs jail_runs jname
local jname=$1
jls -j $jname >/dev/null 2>&1 && return 0
return 1
}
porttree_list() {
local name method mntpoint
[ -d ${POUDRIERED}/ports ] || return 0
for p in $(find ${POUDRIERED}/ports -type d -maxdepth 1 -mindepth 1 -print); do
name=${p##*/}
_pget mnt ${name} mnt 2>/dev/null || :
_pget method ${name} method 2>/dev/null || :
echo "${name} ${method:--} ${mnt}"
done
}
porttree_exists() {
[ $# -ne 1 ] && eargs porttree_exists portstree_name
porttree_list |
awk -v portstree_name=$1 '
BEGIN { ret = 1 }
$1 == portstree_name {ret = 0; }
END { exit ret }
' && return 0
return 1
}
get_data_dir() {
local data
if [ -n "${POUDRIERE_DATA}" ]; then
echo ${POUDRIERE_DATA}
return
fi
if [ -z "${NO_ZFS}" ]; then
data=$(zfs list -rt filesystem -H -o ${NS}:type,mountpoint ${ZPOOL}${ZROOTFS} 2>/dev/null |
awk '$1 == "data" { print $2; exit; }')
if [ -n "${data}" ]; then
echo "${data}"
return
fi
# Manually created dataset may be missing type, set it and
# don't add more child datasets.
if zfs get mountpoint ${ZPOOL}${ZROOTFS}/data >/dev/null \
2>&1; then
zfs set ${NS}:type=data ${ZPOOL}${ZROOTFS}/data
zfs get -H -o value mountpoint ${ZPOOL}${ZROOTFS}/data
return
fi
zfs create -p -o ${NS}:type=data \
-o atime=off \
-o mountpoint=${BASEFS}/data \
${ZPOOL}${ZROOTFS}/data
zfs create ${ZPOOL}${ZROOTFS}/data/.m
zfs create -o compression=off ${ZPOOL}${ZROOTFS}/data/cache
zfs create -o compression=lz4 ${ZPOOL}${ZROOTFS}/data/logs
zfs create -o compression=off ${ZPOOL}${ZROOTFS}/data/packages
zfs create -o compression=off ${ZPOOL}${ZROOTFS}/data/wrkdirs
else
mkdir -p "${BASEFS}/data"
fi
echo "${BASEFS}/data"
}
fetch_file() {
[ $# -ne 2 ] && eargs fetch_file destination source
fetch -p -o $1 $2 || fetch -p -o $1 $2 || err 1 "Failed to fetch from $2"
}
# Export handling is different in builtin vs external
if [ "$(type mktemp)" = "mktemp is a shell builtin" ]; then
MKTEMP_BUILTIN=1
fi
# Wrap mktemp to put most tmpfiles in mnt/.p/tmp rather than system /tmp.
mktemp() {
local ret
if [ -z "${TMPDIR}" ]; then
if [ -n "${MASTERMNT}" -a ${STATUS} -eq 1 ]; then
local mnt
_my_path mnt
TMPDIR="${mnt}/.p/tmp"
[ -d "${TMPDIR}" ] || unset TMPDIR
else
TMPDIR="${POUDRIERE_TMPDIR}"
fi
fi
if [ -n "${MKTEMP_BUILTIN}" ]; then
# No export needed here since TMPDIR is set above in scope.
builtin mktemp "$@"
else
[ -n "${TMPDIR}" ] && export TMPDIR
command mktemp "$@"
fi
}
common_mtree() {
[ $# -eq 1 ] || eargs common_mtree mtreefile
local mtreefile="$1"
local exclude
cat > "${mtreefile}" <<EOF
./.npkg
./.p
./.poudriere-snap-*
.${HOME}/.ccache
./compat/linux/proc
./dev
./distfiles
./packages
./portdistfiles
./proc
.${PORTSDIR}
./usr/src
./var/db/freebsd-update
./var/db/ports
./wrkdirs
EOF
for exclude in ${LOCAL_MTREE_EXCLUDES}; do
echo ".${exclude#.}" >> "${mtreefile}"
done
}
markfs() {
[ $# -lt 2 ] && eargs markfs name mnt path
local name=$1
local mnt="${2}"
local path="$3"
local fs="$(zfs_getfs ${mnt})"
local dozfs=0
local domtree=0
local mtreefile
local snapfile
msg_n "Recording filesystem state for ${name}..."
case "${name}" in
clean) [ -n "${fs}" ] && dozfs=1 ;;
prepkg)
[ -n "${fs}" ] && dozfs=1
# Only create prepkg mtree in ref
# Everything else may need to snapshot
if [ "${mnt##*/}" = "ref" ]; then
domtree=1
else
domtree=0
fi
;;
prebuild|prestage) domtree=1 ;;
preinst) domtree=1 ;;
esac
if [ $dozfs -eq 1 ]; then
# remove old snapshot if exists
zfs destroy -r ${fs}@${name} 2>/dev/null || :
rollback_file "${mnt}" "${name}" snapfile
rm -f "${snapfile}" >/dev/null 2>&1 || :
#create new snapshot
zfs snapshot ${fs}@${name}
# Mark that we are in this snapshot, which rollbackfs
# will check for not existing when rolling back later.
: > "${snapfile}"
fi
if [ $domtree -eq 0 ]; then
echo " done"
return 0
fi
mtreefile=${mnt}/.p/mtree.${name}exclude
common_mtree ${mtreefile}
case "${name}" in
prebuild|prestage)
cat >> ${mtreefile} <<-EOF
./tmp
./var/tmp
EOF
;;
preinst)
cat >> ${mnt}/.p/mtree.${name}exclude << EOF
./etc/group
./etc/make.conf
./etc/make.conf.bak
./etc/master.passwd
./etc/passwd
./etc/pwd.db
./etc/shells
./etc/spwd.db
./tmp
./var/db/pkg
./var/log
./var/mail
./var/run
./var/tmp
EOF
;;
esac
( cd "${mnt}${path}" && mtree -X ${mnt}/.p/mtree.${name}exclude \
-cn -k uid,gid,mode,size \
-p . ) > ${mnt}/.p/mtree.${name}
echo " done"
}
rm() {
local arg
for arg in "$@"; do
[ "${arg}" = "/" ] && err 1 "Tried to rm /"
[ "${arg%/}" = "/bin" ] && err 1 "Tried to rm /*"
done
command rm "$@"
}
# Handle relative path change needs
cd() {
local ret
ret=0
command cd "$@" || ret=$?
# Handle fixing relative paths
if [ "${OLDPWD}" != "${PWD}" ]; then
# Only change if it is relative
if [ -n "${SHASH_VAR_PATH##/*}" ]; then
_relpath "${OLDPWD}/${SHASH_VAR_PATH}" "${PWD}"
SHASH_VAR_PATH="${_relpath}"
fi
fi
return ${ret}
}
do_jail_mounts() {
[ $# -ne 4 ] && eargs do_jail_mounts from mnt arch name
local from="$1"
local mnt="$2"
local arch="$3"
local name="$4"
local devfspath="null zero random urandom stdin stdout stderr fd fd/* bpf* pts pts/*"
local srcpath nullpaths nullpath
# from==mnt is via jail -u
# clone will inherit from the ref jail
if [ ${mnt##*/} = "ref" ]; then
mkdir -p ${mnt}/proc \
${mnt}/dev \
${mnt}/compat/linux/proc \
${mnt}/usr/src
fi
# Mount some paths read-only from the ref-jail if possible.
nullpaths="/rescue"
if [ "${MUTABLE_BASE}" = "no" ]; then
# Need to keep /usr/src and /usr/ports on their own.
nullpaths="${nullpaths} /usr/bin /usr/include /usr/lib \
/usr/lib32 /usr/libdata /usr/libexec /usr/obj \
/usr/sbin /usr/share /usr/tests /boot /bin /sbin /lib \
/libexec"
# Do a real copy for the ref jail since we need to modify
# or create directories in them.
if [ "${mnt##*/}" != "ref" ]; then
nullpaths="${nullpaths} /etc"
fi
fi
echo ${nullpaths} | tr ' ' '\n' | sed -e "s,^/,${mnt}/," | \
xargs mkdir -p
for nullpath in ${nullpaths}; do
[ -d "${from}${nullpath}" -a "${from}" != "${mnt}" ] && \
${NULLMOUNT} -o ro "${from}${nullpath}" "${mnt}${nullpath}"
done
# Mount /usr/src into target if it exists and not overridden
_jget srcpath ${name} srcpath 2>/dev/null || srcpath="${from}/usr/src"
[ -d "${srcpath}" -a "${from}" != "${mnt}" ] && \
${NULLMOUNT} -o ro ${srcpath} ${mnt}/usr/src
mount -t devfs devfs ${mnt}/dev
if [ ${JAILED} -eq 0 ]; then
devfs -m ${mnt}/dev rule apply hide
for p in ${devfspath} ; do
devfs -m ${mnt}/dev/ rule apply path "${p}" unhide
done
fi
[ "${USE_FDESCFS}" = "yes" ] && \
[ ${JAILED} -eq 0 -o "${PATCHED_FS_KERNEL}" = "yes" ] && \
mount -t fdescfs fdesc "${mnt}/dev/fd"
[ "${USE_PROCFS}" = "yes" ] && \
mount -t procfs proc "${mnt}/proc"
[ -z "${NOLINUX}" ] && \
[ "${arch}" = "i386" -o "${arch}" = "amd64" ] && \
[ -d "${mnt}/compat" ] && \
mount -t linprocfs linprocfs "${mnt}/compat/linux/proc"
run_hook jail mount ${mnt}
return 0
}
# Interactive test mode
enter_interactive() {
local stopmsg
if [ ${ALL} -ne 0 ]; then
msg "(-a) Not entering interactive mode."
return 0
fi
print_phase_header "Interactive"
bset status "interactive:"
msg "Installing packages"
echo "PACKAGES=/packages" >> ${MASTERMNT}/etc/make.conf
echo "127.0.0.1 ${MASTERNAME}" >> ${MASTERMNT}/etc/hosts
# Skip for testport as it has already installed pkg in the ref jail.
if [ "${SCRIPTPATH##*/}" != "testport.sh" ]; then
# Install pkg-static so full pkg package can install
ensure_pkg_installed force_extract || \
err 1 "Unable to extract pkg."
# Install the selected pkg package
injail env USE_PACKAGE_DEPENDS_ONLY=1 \
/usr/bin/make -C \
${PORTSDIR}/$(injail /usr/bin/make \
-f ${PORTSDIR}/Mk/bsd.port.mk -V PKGNG_ORIGIN) \
PKG_BIN="${PKG_BIN}" install-package
fi
# Enable all selected ports and their run-depends
for port in $(listed_ports); do
# Install run-depends since this is an interactive test
msg "Installing run-depends for ${COLOR_PORT}${port}"
injail env USE_PACKAGE_DEPENDS_ONLY=1 \
/usr/bin/make -C ${PORTSDIR}/${port} run-depends ||
msg_warn "Failed to install ${COLOR_PORT}${port} run-depends"
msg "Installing ${COLOR_PORT}${port}"
# Only use PKGENV during install as testport will store
# the package in a different place than dependencies
injail env USE_PACKAGE_DEPENDS_ONLY=1 ${PKGENV} \
/usr/bin/make -C ${PORTSDIR}/${port} install-package ||
msg_warn "Failed to install ${COLOR_PORT}${port}"
done
# Create a pkg repo configuration, and disable FreeBSD
msg "Installing local Pkg repository to ${LOCALBASE}/etc/pkg/repos"
mkdir -p ${MASTERMNT}${LOCALBASE}/etc/pkg/repos
cat > ${MASTERMNT}${LOCALBASE}/etc/pkg/repos/local.conf << EOF
FreeBSD: {
enabled: no
}
local: {
url: "file:///packages",
enabled: yes
}
EOF
if [ ${INTERACTIVE_MODE} -eq 1 ]; then
msg "Entering interactive test mode. Type 'exit' when done."
JNETNAME="n" injail_tty env -i TERM=${SAVED_TERM} \
/usr/bin/login -fp root || :
elif [ ${INTERACTIVE_MODE} -eq 2 ]; then
# XXX: Not tested/supported with bulk yet.
msg "Leaving jail ${MASTERNAME}-n running, mounted at ${MASTERMNT} for interactive run testing"
msg "To enter jail: jexec ${MASTERNAME}-n env -i TERM=\$TERM /usr/bin/login -fp root"
stopmsg="-j ${JAILNAME}"
[ -n "${SETNAME}" ] && stopmsg="${stopmsg} -z ${SETNAME}"
[ -n "${PTNAME#default}" ] && stopmsg="${stopmsg} -p ${PTNAME}"
msg "To stop jail: poudriere jail -k ${stopmsg}"
CLEANED_UP=1
return 0
fi
print_phase_footer
}
use_options() {
[ $# -ne 2 ] && eargs use_options mnt optionsdir
local mnt=$1
local optionsdir=$2
if [ "${optionsdir}" = "-" ]; then
optionsdir="${POUDRIERED}/options"
else
optionsdir="${POUDRIERED}/${optionsdir}-options"
fi
[ -d "${optionsdir}" ] || return 1
optionsdir=$(realpath ${optionsdir} 2>/dev/null)
[ "${mnt##*/}" = "ref" ] && \
msg "Copying /var/db/ports from: ${optionsdir}"
do_clone "${optionsdir}" "${mnt}/var/db/ports" || \
err 1 "Failed to copy OPTIONS directory"
return 0
}
mount_packages() {
local mnt
_my_path mnt
${NULLMOUNT} "$@" ${PACKAGES} \
${mnt}/packages ||
err 1 "Failed to mount the packages directory "
}
do_portbuild_mounts() {
[ $# -lt 3 ] && eargs do_portbuild_mounts mnt jname ptname setname
local mnt=$1
local jname=$2
local ptname=$3
local setname=$4
local portsdir
local optionsdir
# clone will inherit from the ref jail
if [ ${mnt##*/} = "ref" ]; then
mkdir -p "${mnt}${PORTSDIR}" \
"${mnt}/wrkdirs" \
"${mnt}/${LOCALBASE:-/usr/local}" \
"${mnt}/distfiles" \
"${mnt}/packages" \
"${mnt}/.npkg" \
"${mnt}/var/db/ports" \
"${mnt}${HOME}/.ccache"
fi
[ ${TMPFS_DATA} -eq 1 -o ${TMPFS_ALL} -eq 1 ] &&
mnt_tmpfs data "${mnt}/.p"
mkdir -p "${mnt}/.p/tmp"
[ -d "${CCACHE_DIR:-/nonexistent}" ] &&
${NULLMOUNT} ${CCACHE_DIR} ${mnt}${HOME}/.ccache
[ -n "${MFSSIZE}" ] && mdmfs -t -S -o async -s ${MFSSIZE} md ${mnt}/wrkdirs
[ ${TMPFS_WRKDIR} -eq 1 ] && mnt_tmpfs wrkdir ${mnt}/wrkdirs
# Only show mounting messages once, not for every builder
if [ ${mnt##*/} = "ref" ]; then
[ -d "${CCACHE_DIR}" ] &&
msg "Mounting ccache from: ${CCACHE_DIR}"
msg "Mounting packages from: ${PACKAGES_ROOT}"
fi
_pget portsdir ${ptname} mnt
[ -d ${portsdir}/ports ] && portsdir=${portsdir}/ports
${NULLMOUNT} -o ro ${portsdir} ${mnt}${PORTSDIR} ||
err 1 "Failed to mount the ports directory "
mount_packages -o ro
${NULLMOUNT} ${DISTFILES_CACHE} ${mnt}/distfiles ||
err 1 "Failed to mount the distfiles cache directory"
# Copy in the options for the ref jail, but just ro nullmount it
# in builders.
if [ "${mnt##*/}" = "ref" ]; then
[ ${TMPFS_DATA} -eq 1 -o ${TMPFS_ALL} -eq 1 ] && \
mnt_tmpfs config "${mnt}/var/db/ports"
optionsdir="${MASTERNAME}"
[ -n "${setname}" ] && optionsdir="${optionsdir} ${jname}-${setname}"
optionsdir="${optionsdir} ${jname}-${ptname} ${setname} ${ptname} ${jname} -"
for opt in ${optionsdir}; do
use_options ${mnt} ${opt} && break || continue
done
else
${NULLMOUNT} -o ro ${MASTERMNT}/var/db/ports \
${mnt}/var/db/ports || \
err 1 "Failed to mount the options directory"
fi
return 0
}
# Convert the repository to the new format of links
# so that an atomic update can be done at the end
# of the build.
# This is done at the package repo level instead of the parent
# dir in DATA/packages because someone may have created a separate
# ZFS dataset / NFS mount for each dataset. Avoid cross-device linking.
convert_repository() {
local pkgdir
msg "Converting package repository to new format"
pkgdir=.real_$(clock -epoch)
mkdir ${PACKAGES}/${pkgdir}
# Move all top-level dirs into .real
find ${PACKAGES}/ -mindepth 1 -maxdepth 1 -type d ! -name ${pkgdir} |
xargs -J % mv % ${PACKAGES}/${pkgdir}
# Symlink them over through .latest
find ${PACKAGES}/${pkgdir} -mindepth 1 -maxdepth 1 -type d \
! -name ${pkgdir} | while read directory; do
dirname=${directory##*/}
ln -s .latest/${dirname} ${PACKAGES}/${dirname}
done
# Now move+symlink any files in the top-level
find ${PACKAGES}/ -mindepth 1 -maxdepth 1 -type f |
xargs -J % mv % ${PACKAGES}/${pkgdir}
find ${PACKAGES}/${pkgdir} -mindepth 1 -maxdepth 1 -type f |
while read file; do
fname=${file##*/}
ln -s .latest/${fname} ${PACKAGES}/${fname}
done
# Setup current symlink which is how the build will atomically finish
ln -s ${pkgdir} ${PACKAGES}/.latest
}
stash_packages() {
PACKAGES_ROOT=${PACKAGES}
[ "${ATOMIC_PACKAGE_REPOSITORY}" = "yes" ] || return 0
[ -L ${PACKAGES}/.latest ] || convert_repository
if [ -d ${PACKAGES}/.building ]; then
# If the .building directory is still around, use it. The
# previous build may have failed, but all of the successful
# packages are still worth keeping for this build.
msg "Using packages from previously failed build"
else
msg "Stashing existing package repository"
# Use a linked shadow directory in the package root, not
# in the parent directory as the user may have created
# a separate ZFS dataset or NFS mount for each package
# set; Must stay on the same device for linking.
mkdir -p ${PACKAGES}/.building
# hardlink copy all top-level directories
find ${PACKAGES}/.latest/ -mindepth 1 -maxdepth 1 -type d \
! -name .building | xargs -J % cp -al % ${PACKAGES}/.building
# Copy all top-level files to avoid appending
# to real copy in pkg-repo, etc.
find ${PACKAGES}/.latest/ -mindepth 1 -maxdepth 1 -type f |
xargs -J % cp -a % ${PACKAGES}/.building
fi
# From this point forward, only work in the shadow
# package dir
PACKAGES=${PACKAGES}/.building
}
commit_packages() {
local pkgdir_old pkgdir_new stats_failed
[ "${ATOMIC_PACKAGE_REPOSITORY}" = "yes" ] || return 0
if [ "${COMMIT_PACKAGES_ON_FAILURE}" = "no" ] &&
_bget stats_failed stats_failed && [ ${stats_failed} -gt 0 ]; then
msg_warn "Not committing packages to repository as failures were encountered"
return 0
fi
msg "Committing packages to repository"
bset status "committing:"
# Find any new top-level files not symlinked yet. This is
# mostly incase pkg adds a new top-level repo or the ports framework
# starts creating a new directory
find ${PACKAGES}/ -mindepth 1 -maxdepth 1 \
\( ! -name '.*' -o -name '.jailversion' \) |
while read path; do
name=${path##*/}
[ ! -L "${PACKAGES_ROOT}/${name}" ] || continue
if [ -e "${PACKAGES_ROOT}/${name}" ]; then
case "${name}" in
.jailversion|meta.txz|digests.txz|packagesite.txz|All|Latest)
# Auto fix pkg-owned files
rm -f "${PACKAGES_ROOT}/${name}"
;;
*)
msg_error "${PACKAGES_ROOT}/${name}
shadows repository file in .latest/${name}. Remove the top-level one and
symlink to .latest/${name}"
continue
;;
esac
fi
ln -s .latest/${name} ${PACKAGES_ROOT}/${name}
done
pkgdir_old=$(realpath ${PACKAGES_ROOT}/.latest 2>/dev/null || :)
# Rename shadow dir to a production name
pkgdir_new=.real_$(clock -epoch)
mv ${PACKAGES_ROOT}/.building ${PACKAGES_ROOT}/${pkgdir_new}
# XXX: Copy in packages that failed to build
# Switch latest symlink to new build
PACKAGES=${PACKAGES_ROOT}/.latest
ln -s ${pkgdir_new} ${PACKAGES_ROOT}/.latest_new
rename ${PACKAGES_ROOT}/.latest_new ${PACKAGES}
# Look for broken top-level links and remove them, if they reference
# the old directory
find -L ${PACKAGES_ROOT}/ -mindepth 1 -maxdepth 1 \
\( ! -name '.*' -o -name '.jailversion' \) \
-type l |
while read path; do
link=$(readlink ${path})
# Skip if link does not reference inside latest
[ "${link##.latest}" != "${link}" ] || continue
rm -f ${path}
done
msg "Removing old packages"
if [ "${KEEP_OLD_PACKAGES}" = "yes" ]; then
keep_cnt=$((${KEEP_OLD_PACKAGES_COUNT} + 1))
find ${PACKAGES_ROOT}/ -type d -mindepth 1 -maxdepth 1 \
-name '.real_*' | sort -dr |
sed -n "${keep_cnt},\$p" |
xargs rm -rf 2>/dev/null || :
else
# Remove old and shadow dir
[ -n "${pkgdir_old}" ] && rm -rf ${pkgdir_old} 2>/dev/null || :
fi
}
show_build_results() {
local failed built ignored skipped nbbuilt nbfailed nbignored nbskipped
failed=$(bget ports.failed | awk '{print $1 ":" $3 }' | xargs echo)
failed=$(bget ports.failed | \
awk -v color_phase="${COLOR_PHASE}" \
-v color_port="${COLOR_PORT}" \
'{print $1 ":" color_phase $3 color_port }' | xargs echo)
built=$(bget ports.built | awk '{print $1}' | xargs echo)
ignored=$(bget ports.ignored | awk '{print $1}' | xargs echo)
skipped=$(bget ports.skipped | awk '{print $1}' | sort -u | xargs echo)
_bget nbbuilt stats_built
_bget nbfailed stats_failed
_bget nbignored stats_ignored
_bget nbskipped stats_skipped
[ $nbbuilt -gt 0 ] && COLOR_ARROW="${COLOR_SUCCESS}" \
msg "${COLOR_SUCCESS}Built ports: ${COLOR_PORT}${built}"
[ $nbfailed -gt 0 ] && COLOR_ARROW="${COLOR_FAIL}" \
msg "${COLOR_FAIL}Failed ports: ${COLOR_PORT}${failed}"
[ $nbskipped -gt 0 ] && COLOR_ARROW="${COLOR_SKIP}" \
msg "${COLOR_SKIP}Skipped ports: ${COLOR_PORT}${skipped}"
[ $nbignored -gt 0 ] && COLOR_ARROW="${COLOR_IGNORE}" \
msg "${COLOR_IGNORE}Ignored ports: ${COLOR_PORT}${ignored}"
show_build_summary
show_log_info
return 0
}
write_usock() {
[ $# -gt 1 ] || eargs write_usock socket msg
local socket="$1"
shift
nc -U "${socket}" <<- EOF
$@
EOF
}
# If running as non-root, redirect this command to queue and exit
maybe_run_queued() {
[ $(/usr/bin/id -u) -eq 0 ] && return 0
local this_command
# If poudriered not running then the command cannot be
# satisfied.
/usr/sbin/service poudriered onestatus >/dev/null 2>&1 || \
err 1 "This command requires root or poudriered running"
this_command="${SCRIPTPATH##*/}"
this_command="${this_command%.sh}"
write_usock ${QUEUE_SOCKET} command: "${this_command}", arguments: "$@"
exit
}
get_host_arch() {
[ $# -eq 1 ] || eargs get_host_arch var_return
local var_return="$1"
local _arch
_arch="$(uname -m).$(uname -p)"
# If TARGET=TARGET_ARCH trim it away and just use TARGET_ARCH
[ "${_arch%.*}" = "${_arch#*.}" ] && _arch="${_arch#*.}"
setvar "${var_return}" "${_arch}"
}
check_emulation() {
[ $# -eq 2 ] || eargs check_emulation real_arch wanted_arch
local real_arch="${1}"
local wanted_arch="${2}"
if need_emulation "${wanted_arch}"; then
msg "Cross-building ports for ${wanted_arch} on ${real_arch} requires QEMU"
[ -x "${BINMISC}" ] || \
err 1 "Cannot find ${BINMISC}. Install ${BINMISC} and restart"
EMULATOR=$(${BINMISC} lookup ${wanted_arch#*.} 2>/dev/null | \
awk '/interpreter:/ {print $2}')
[ -x "${EMULATOR}" ] || \
err 1 "You need to setup an emulator with binmiscctl(8) for ${wanted_arch#*.}"
export QEMU_EMULATING=1
fi
}
need_emulation() {
[ $# -eq 1 ] || eargs need_emulation wanted_arch
local wanted_arch="$1"
local target_arch
# kern.supported_archs is a list of TARGET_ARCHs.
target_arch="${wanted_arch#*.}"
# Check the list of supported archs from the kernel.
# DragonFly does not have kern.supported_archs, fallback to
# uname -m (advised by dillon)
if { sysctl -n kern.supported_archs 2>/dev/null || uname -m; } | \
grep -qw "${target_arch}"; then
return 1
else
# Returning 1 means no emulation required.
return 0
fi
}
need_cross_build() {
[ $# -eq 2 ] || eargs need_cross_build real_arch wanted_arch
local real_arch="$1"
local wanted_arch="$2"
# Check TARGET=i386 not TARGET_ARCH due to pc98/i386
[ "${wanted_arch%.*}" = "i386" -a "${real_arch}" = "amd64" ] || \
[ "${wanted_arch#*.}" = "powerpc" -a \
"${real_arch#*.}" = "powerpc64" ] || \
need_emulation "${wanted_arch}"
}
_jlock() {
setvar "$1" "/var/run/poudriere/poudriere.${MASTERNAME}.lock"
}
lock_jail() {
local jlock jlockf jlockpid
_jlock jlock
jlockf="${jlock}/pid"
mkdir -p /var/run/poudriere >/dev/null 2>&1 || :
# Ensure no other processes are trying to start this jail
if ! mkdir "${jlock}" 2>/dev/null; then
if [ -d "${jlock}" ]; then
jlockpid=
if [ -f "${jlockf}" ]; then
if locked_mkdir 5 "${jlock}.pid"; then
read jlockpid < "${jlockf}" || :
rmdir "${jlock}.pid"
else
# Something went wrong, just try again
lock_jail
return
fi
fi
if [ -n "${jlockpid}" ]; then
if ! kill -0 ${jlockpid} >/dev/null 2>&1; then
# The process is dead;
# the lock is stale
rm -rf "${jlock}"
# Try to get the lock again
lock_jail
return
else
# The lock is currently held
err 1 "jail currently starting: ${MASTERNAME}"
fi
else
# This shouldn't happen due to the
# use of locking on the file, just
# blow it away and try again.
rm -rf "${jlock}"
lock_jail
return
fi
else
err 1 "Unable to create jail lock ${jlock}"
fi
else
# We're safe to start the jail and to later remove the lock.
if locked_mkdir 5 "${jlock}.pid"; then
CREATED_JLOCK=1
echo "$$" > "${jlock}/pid"
rmdir "${jlock}.pid"
return 0
else
# Something went wrong, just try again
lock_jail
return
fi
fi
}
setup_ccache() {
[ $# -eq 1 ] || eargs setup_ccache tomnt
local tomnt="$1"
local ccacheprefix
if [ -d "${CCACHE_DIR:-/nonexistent}" ]; then
cat >> "${tomnt}/etc/make.conf" <<-EOF
WITH_CCACHE_BUILD=yes
CCACHE_DIR=${HOME}/.ccache
EOF
fi
# A static host version may have been requested.
if [ -n "${CCACHE_STATIC_PREFIX}" ] && \
[ -x "${CCACHE_STATIC_PREFIX}/bin/ccache" ]; then
file "${CCACHE_STATIC_PREFIX}/bin/ccache" | \
grep -q "statically linked" || \
err 1 "CCACHE_STATIC_PREFIX used but ${CCACHE_STATIC_PREFIX}/bin/ccache is not static."
ccacheprefix=/ccache
mkdir -p "${tomnt}${ccacheprefix}/libexec/ccache/world" \
"${tomnt}${ccacheprefix}/bin"
msg "Copying host static ccache from ${CCACHE_STATIC_PREFIX}/bin/ccache"
cp -f "${CCACHE_STATIC_PREFIX}/bin/ccache" \
"${CCACHE_STATIC_PREFIX}/bin/ccache-update-links" \
"${tomnt}${ccacheprefix}/bin/"
cp -f "${CCACHE_STATIC_PREFIX}/libexec/ccache/world/ccache" \
"${tomnt}${ccacheprefix}/libexec/ccache/world/ccache"
# Tell the ports framework that we don't need it to add
# a BUILD_DEPENDS on everything for ccache.
# Also set it up to look in our ccacheprefix location for the
# wrappers.
cat >> "${tomnt}/etc/make.conf" <<-EOF
NO_CCACHE_DEPEND=1
CCACHE_WRAPPER_PATH= ${ccacheprefix}/libexec/ccache
EOF
# Link the wrapper update script to /sbin so that
# any package trying to update the links will find it
# rather than an actual ccache package in the jail.
ln -fs "../${ccacheprefix}/bin/ccache-update-links" \
"${tomnt}/sbin/ccache-update-links"
# Fix the wrapper update script to always make the links
# in the new prefix.
sed -i '' -e "s,^\(PREFIX\)=.*,\1=\"${ccacheprefix}\"," \
"${tomnt}${ccacheprefix}/bin/ccache-update-links"
# Create base compiler links
injail "${ccacheprefix}/bin/ccache-update-links"
fi
}
jail_start() {
[ $# -lt 2 ] && eargs jail_start name ptname setname
local name=$1
local ptname=$2
local setname=$3
local portsdir
local arch host_arch
local mnt
local needfs="${NULLFSREF}"
local needkld kldpair kld kldmodname
local tomnt
local portbuild_uid
lock_jail
if [ -n "${MASTERMNT}" ]; then
tomnt="${MASTERMNT}"
else
_mastermnt tomnt
fi
_pget portsdir ${ptname} mnt
_jget arch ${name} arch
get_host_arch host_arch
_jget mnt ${name} mnt
# Protect ourselves from OOM
madvise_protect $$ || :
PORTSDIR="/usr/ports"
JAIL_OSVERSION=$(awk '/\#define __FreeBSD_version/ { print $3 }' "${mnt}/usr/include/sys/param.h")
[ ${JAIL_OSVERSION} -lt 900000 ] && needkld="${needkld} sem"
if [ "${DISTFILES_CACHE}" != "no" -a ! -d "${DISTFILES_CACHE}" ]; then
err 1 "DISTFILES_CACHE directory does not exist. (c.f. poudriere.conf)"
fi
[ ${TMPFS_ALL} -ne 1 ] && [ $(sysctl -n kern.securelevel) -ge 1 ] && \
err 1 "kern.securelevel >= 1. Poudriere requires no securelevel to be able to handle schg flags. USE_TMPFS=all can override this."
[ "${name#*.*}" = "${name}" ] ||
err 1 "The jail name cannot contain a period (.). See jail(8)"
[ "${ptname#*.*}" = "${ptname}" ] ||
err 1 "The ports name cannot contain a period (.). See jail(8)"
[ "${setname#*.*}" = "${setname}" ] ||
err 1 "The set name cannot contain a period (.). See jail(8)"
if [ -z "${NOLINUX}" ]; then
if [ "${arch}" = "i386" -o "${arch}" = "amd64" ]; then
needfs="${needfs} linprocfs"
needkld="${needkld} linuxelf:linux"
if [ "${arch}" = "amd64" ] && \
[ ${HOST_OSVERSION} -ge 1002507 ]; then
needkld="${needkld} linux64elf:linux64"
fi
fi
fi
[ "${USE_TMPFS}" != "no" ] && needfs="${needfs} tmpfs"
[ "${USE_PROCFS}" = "yes" ] && needfs="${needfs} procfs"
[ "${USE_FDESCFS}" = "yes" ] && \
[ ${JAILED} -eq 0 -o "${PATCHED_FS_KERNEL}" = "yes" ] && \
needfs="${needfs} fdescfs"
for fs in ${needfs}; do
if ! lsvfs $fs >/dev/null 2>&1; then
if [ $JAILED -eq 0 ]; then
kldload $fs || err 1 "Required kernel module '${fs}' not found"
else
err 1 "please load the $fs module on host using \"kldload $fs\""
fi
fi
done
for kldpair in ${needkld}; do
kldmodname="${kldpair%:*}"
kld="${kldpair#*:}"
if ! kldstat -q -m "${kldmodname}" ; then
if [ $JAILED -eq 0 ]; then
kldload "${kld}" || \
err 1 "Required kernel module '${kld}' not found"
else
err 1 "Please load the ${kld} module on the host using \"kldload ${kld}\""
fi
fi
done
jail_exists ${name} || err 1 "No such jail: ${name}"
jail_runs ${MASTERNAME} && err 1 "jail already running: ${MASTERNAME}"
check_emulation "${host_arch}" "${arch}"
# Block the build dir from being traversed by non-root to avoid
# system blowup due to all of the extra mounts
mkdir -p ${MASTERMNT%/ref}
chmod 0755 ${POUDRIERE_DATA}/.m
chmod 0711 ${MASTERMNT%/ref}
export HOME=/root
export USER=root
# Only set STATUS=1 if not turned off
# jail -s should not do this or jail will stop on EXIT
[ ${SET_STATUS_ON_START-1} -eq 1 ] && export STATUS=1
msg_n "Creating the reference jail..."
if [ ${USE_CACHED} = "yes" ]; then
export CACHESOCK=${MASTERMNT%/ref}/cache.sock
export CACHEPID=${MASTERMNT%/ref}/cache.pid
cached -s /${MASTERNAME} -p ${CACHEPID} -n ${MASTERNAME}
fi
clonefs ${mnt} ${tomnt} clean
echo " done"
if [ ${JAIL_OSVERSION} -gt ${HOST_OSVERSION} ]; then
msg_warn "!!! Jail is newer than host. (Jail: ${JAIL_OSVERSION}, Host: ${HOST_OSVERSION}) !!!"
msg_warn "This is not supported."
msg_warn "Host kernel must be same or newer than jail."
msg_warn "Expect build failures."
sleep 1
fi
msg "Mounting system devices for ${MASTERNAME}"
do_jail_mounts "${mnt}" "${tomnt}" ${arch} ${name}
PACKAGES=${POUDRIERE_DATA}/packages/${MASTERNAME}
[ -d "${portsdir}/ports" ] && portsdir="${portsdir}/ports"
msg "Mounting ports/packages/distfiles"
mkdir -p ${PACKAGES}/
was_a_bulk_run && stash_packages
do_portbuild_mounts ${tomnt} ${name} ${ptname} ${setname}
# Handle special QEMU needs.
if [ ${QEMU_EMULATING} -eq 1 ]; then
# QEMU is really slow. Extend the time significantly.
msg "Raising MAX_EXECUTION_TIME and NOHANG_TIME for QEMU"
MAX_EXECUTION_TIME=864000
NOHANG_TIME=72000
# Setup native-xtools overrides.
cat >> "${tomnt}/etc/make.conf" <<-EOF
.sinclude "/etc/make.nxb.conf"
EOF
# Copy in the latest version of the emulator.
msg "Copying latest version of the emulator from: ${EMULATOR}"
mkdir -p "${tomnt}${EMULATOR%/*}"
cp -f "${EMULATOR}" "${tomnt}${EMULATOR}"
fi
# Handle special ARM64 needs
if [ "${arch#*.}" = "aarch64" ] && ! [ -f "${tomnt}/usr/bin/ld" ]; then
if [ -f /usr/local/aarch64-freebsd/bin/ld ]; then
msg "Copying aarch64-binutils ld from /usr/local/aarch64-freebsd/bin/ld"
cp -f /usr/local/aarch64-freebsd/bin/ld \
"${tomnt}/usr/bin/ld"
if [ -d "${tomnt}/nxb-bin/usr/bin" ]; then
# Create a symlink to satisfy the LD in
# make.nxb.conf and because running
# /nxb-bin/usr/bin/cc defaults to looking for
# /nxb-bin/usr/bin/ld.
ln -f "${tomnt}/usr/bin/ld" \
"${tomnt}/nxb-bin/usr/bin/ld"
fi
else
err 1 "Arm64 requires aarch64-binutils to be installed."
fi
fi
cat >> "${tomnt}/etc/make.conf" <<-EOF
USE_PACKAGE_DEPENDS=yes
BATCH=yes
WRKDIRPREFIX=/wrkdirs
PORTSDIR=${PORTSDIR}
PACKAGES=/packages
DISTDIR=/distfiles
EOF
[ -z "${NO_FORCE_PACKAGE}" ] && \
echo "FORCE_PACKAGE=yes" >> "${tomnt}/etc/make.conf"
[ -z "${NO_PACKAGE_BUILDING}" ] && \
echo "PACKAGE_BUILDING=yes" >> "${tomnt}/etc/make.conf"
setup_makeconf ${tomnt}/etc/make.conf ${name} ${ptname} ${setname}
load_blacklist ${name} ${ptname} ${setname}
[ -n "${RESOLV_CONF}" ] && cp -v "${RESOLV_CONF}" "${tomnt}/etc/"
msg "Starting jail ${MASTERNAME}"
jstart
if [ ${CREATED_JLOCK:-0} -eq 1 ]; then
_jlock jlock
rm -rf "${jlock}" 2>/dev/null || :
fi
injail id >/dev/null 2>&1 || \
err 1 "Unable to execute id(1) in jail. Emulation or ABI wrong."
portbuild_uid=$(injail id -u ${PORTBUILD_USER} 2>/dev/null || :)
if [ -z "${portbuild_uid}" ]; then
msg_n "Creating user/group ${PORTBUILD_USER}"
injail pw groupadd ${PORTBUILD_USER} -g ${PORTBUILD_UID} || \
err 1 "Unable to create group ${PORTBUILD_USER}"
injail pw useradd ${PORTBUILD_USER} -u ${PORTBUILD_UID} -d /nonexistent -c "Package builder" || \
err 1 "Unable to create user ${PORTBUILD_USER}"
echo " done"
else
PORTBUILD_UID=${portbuild_uid}
PORTBUILD_GID=$(injail id -g ${PORTBUILD_USER})
fi
injail service ldconfig start >/dev/null || \
err 1 "Failed to set ldconfig paths."
setup_ccache "${tomnt}"
# We want this hook to run before any make -V executions in case
# a hook modifies ports or the jail somehow relevant.
run_hook jail start
# Suck in ports environment to avoid redundant fork/exec for each
# child.
if [ -f "${tomnt}${PORTSDIR}/Mk/Scripts/ports_env.sh" ]; then
local make
if [ -x "${tomnt}/usr/bin/bmake" ]; then
make=/usr/bin/bmake
else
make=/usr/bin/make
fi
{
echo "#### /usr/ports/Mk/Scripts/ports_env.sh ####"
injail env \
SCRIPTSDIR=${PORTSDIR}/Mk/Scripts \
PORTSDIR=${PORTSDIR} \
MAKE=${make} \
/bin/sh ${PORTSDIR}/Mk/Scripts/ports_env.sh | \
grep '^export [^;&]*' | \
sed -e 's,^export ,,' -e 's,=",=,' -e 's,"$,,'
echo "#### Misc Poudriere ####"
# This is not set by ports_env as older Poudriere
# would not handle it right.
echo "GID=0"
} >> ${tomnt}/etc/make.conf
fi
# Determine if the ports tree supports SELECTED_OPTIONS from
# r403743
if [ -f "${tomnt}${PORTSDIR}/Mk/bsd.options.mk" ] && \
grep -m1 -q SELECTED_OPTIONS \
"${tomnt}${PORTSDIR}/Mk/bsd.options.mk"; then
PORTS_HAS_SELECTED_OPTIONS=1
else
# Fallback on pretty-print-config.
PORTS_HAS_SELECTED_OPTIONS=0
# XXX: If we know we can use bmake then this would work
# make _SELECTED_OPTIONS='${ALL_OPTIONS:@opt@${PORT_OPTIONS:M${opt}}@} ${MULTI GROUP SINGLE RADIO:L:@otype@${OPTIONS_${otype}:@m@${OPTIONS_${otype}_${m}:@opt@${PORT_OPTIONS:M${opt}}@}@}@}' -V _SELECTED_OPTIONS:O
fi
PKG_EXT="txz"
PKG_BIN="/.p/pkg-static"
PKG_ADD="${PKG_BIN} add"
PKG_DELETE="${PKG_BIN} delete -y -f"
PKG_VERSION="${PKG_BIN} version"
[ -n "${PKG_REPO_SIGNING_KEY}" ] &&
! [ -f "${PKG_REPO_SIGNING_KEY}" ] &&
err 1 "PKG_REPO_SIGNING_KEY defined but the file is missing."
return 0
}
load_blacklist() {
[ $# -lt 2 ] && eargs load_blacklist name ptname setname
local name=$1
local ptname=$2
local setname=$3
local bl b bfile
bl="- ${setname} ${ptname} ${name} ${name}-${ptname}"
[ -n "${setname}" ] && bl="${bl} ${name}-${setname} \
${name}-${ptname}-${setname}"
# If emulating always load a qemu-blacklist as it has special needs.
[ ${QEMU_EMULATING} -eq 1 ] && bl="${bl} qemu"
for b in ${bl} ; do
if [ "${b}" = "-" ]; then
unset b
fi
bfile=${b:+${b}-}blacklist
[ -f ${POUDRIERED}/${bfile} ] || continue
for port in $(grep -h -v -E '(^[[:space:]]*#|^[[:space:]]*$)' \
${POUDRIERED}/${bfile} | sed -e 's|[[:space:]]*#.*||'); do
case " ${BLACKLIST} " in
*\ ${port}\ *) continue;;
esac
msg_warn "Blacklisting (from ${POUDRIERED}/${bfile}): ${COLOR_PORT}${port}"
BLACKLIST="${BLACKLIST} ${port}"
done
done
}
setup_makeconf() {
[ $# -lt 3 ] && eargs setup_makeconf dst_makeconf name ptname setname
local dst_makeconf=$1
local name=$2
local ptname=$3
local setname=$4
local makeconf opt
local arch host_arch
# The jail may be empty for poudriere-options.
if [ -n "${name}" ]; then
_jget arch "${name}" arch
get_host_arch host_arch
if need_cross_build "${host_arch}" "${arch}"; then
cat >> "${dst_makeconf}" <<-EOF
MACHINE=${arch%.*}
MACHINE_ARCH=${arch#*.}
ARCH=\${MACHINE_ARCH}
EOF
fi
fi
makeconf="- ${setname} ${ptname} ${name} ${name}-${ptname}"
[ -n "${setname}" ] && makeconf="${makeconf} ${name}-${setname} \
${name}-${ptname}-${setname}"
for opt in ${makeconf}; do
append_make ${opt} ${dst_makeconf}
done
# We will handle DEVELOPER for testing when appropriate
if grep -q '^DEVELOPER=' ${dst_makeconf}; then
msg_warn "DEVELOPER=yes ignored from make.conf. Use 'bulk -t' or 'testport' for testing instead."
sed -i '' '/^DEVELOPER=/d' ${dst_makeconf}
fi
}
include_poudriere_confs() {
local files file flag args_hack
# Spy on cmdline arguments so this function is not needed in
# every new sub-command file, which could lead to missing it.
args_hack=$(echo " $@"|grep -Eo -- ' -[^ ]*([jpz]) ?([^ ]*)'|tr '\n' ' '|sed -Ee 's, -[^ ]*([jpz]) ?([^ ]*),-\1 \2,g')
set -- ${args_hack}
while getopts "j:p:z:" flag; do
case ${flag} in
j) jail="${OPTARG}" ;;
p) ptname="${OPTARG}" ;;
z) setname="${OPTARG}" ;;
*) ;;
esac
done
files="${setname} ${ptname} ${jail}"
[ -n "${jail}" -a -n "${ptname}" ] && \
files="${files} ${jail}-${ptname}"
[ -n "${jail}" -a -n "${setname}" ] && \
files="${files} ${jail}-${setname}"
[ -n "${jail}" -a -n "${setname}" -a -n "${ptname}" ] && \
files="${files} ${jail}-${ptname}-${setname}"
for file in ${files}; do
file="${POUDRIERED}/${file}-poudriere.conf"
[ -r "${file}" ] && . "${file}"
done
return 0
}
jail_stop() {
[ $# -ne 0 ] && eargs jail_stop
local last_status
# Make sure CWD is not inside the jail or MASTERMNT/.p, which may
# cause EBUSY from umount.
cd /
stop_builders >/dev/null || :
if [ ${USE_CACHED} = "yes" ]; then
pkill -15 -F ${CACHEPID} >/dev/null 2>&1 || :
fi
run_hook jail stop
jstop || :
msg "Unmounting file systems"
destroyfs ${MASTERMNT} jail || :
rm -rfx ${MASTERMNT}/../
export STATUS=0
# Don't override if there is a failure to grab the last status.
_bget last_status status 2>/dev/null || :
[ -n "${last_status}" ] && bset status "stopped:${last_status}" \
2>/dev/null || :
}
jail_cleanup() {
local wait_pids
[ -n "${CLEANED_UP}" ] && return 0
msg "Cleaning up"
# Only bother with this if using jails as this may be being ran
# from queue.sh or daemon.sh, etc.
if [ -n "${MASTERMNT}" -a -n "${MASTERNAME}" ] && was_a_jail_run; then
# If this is a builder, don't cleanup, the master will handle that.
if [ -n "${MY_JOBID}" ]; then
if [ -n "${PKGNAME}" ]; then
clean_pool "${PKGNAME}" "" "failed" || :
fi
return 0
fi
if [ -d ${MASTERMNT}/.p/var/run ]; then
for pid in ${MASTERMNT}/.p/var/run/*.pid; do
# Ensure there is a pidfile to read or break
[ "${pid}" = "${MASTERMNT}/.p/var/run/*.pid" ] && break
pkill -15 -F ${pid} >/dev/null 2>&1 || :
wait_pids="${wait_pids} ${pid}"
done
_wait ${wait_pids} || :
fi
jail_stop
rm -rf \
${PACKAGES}/.npkg \
${POUDRIERE_DATA}/packages/${MASTERNAME}/.latest/.npkg \
2>/dev/null || :
fi
export CLEANED_UP=1
}
# return 0 if the package dir exists and has packages, 0 otherwise
package_dir_exists_and_has_packages() {
[ ! -d ${PACKAGES}/All ] && return 1
dirempty ${PACKAGES}/All && return 1
# Check for non-empty directory with no packages in it
for pkg in ${PACKAGES}/All/*.${PKG_EXT}; do
[ "${pkg}" = \
"${PACKAGES}/All/*.${PKG_EXT}" ] \
&& return 1
# Stop on first match
break
done
return 0
}
sanity_check_pkg() {
[ $# -eq 1 ] || eargs sanity_check_pkg pkg
local pkg="$1"
local depfile origin pkgname
pkg_get_origin origin "${pkg}"
pkgname="${pkg##*/}"
pkgname="${pkgname%.*}"
pkg_is_needed "${pkgname}" || return 0
deps_file depfile "${pkg}"
while read dep; do
if [ ! -e "${PACKAGES}/All/${dep}.${PKG_EXT}" ]; then
msg_debug "${pkg} needs missing ${PACKAGES}/All/${dep}.${PKG_EXT}"
msg "Deleting ${pkg##*/}: missing dependency: ${dep}"
delete_pkg "${pkg}"
return 65 # Package deleted, need another pass
fi
done < "${depfile}"
return 0
}
sanity_check_pkgs() {
local ret=0
package_dir_exists_and_has_packages || return 0
parallel_start
for pkg in ${PACKAGES}/All/*.${PKG_EXT}; do
parallel_run sanity_check_pkg "${pkg}" || ret=$?
done
parallel_stop || ret=$?
[ ${ret} -eq 0 ] && return 0 # Nothing deleted
[ ${ret} -eq 65 ] && return 1 # Packages deleted
err 1 "Failure during sanity check"
}
check_leftovers() {
[ $# -eq 1 ] || eargs check_leftovers mnt
local mnt="${1}"
( cd "${mnt}" && \
mtree -X ${mnt}/.p/mtree.preinstexclude -f ${mnt}/.p/mtree.preinst \
-p . ) | while read l; do
local changed read_again
changed=
while :; do
read_again=0
# Handle leftover read from changed paths
case ${l} in
*extra|*missing|extra:*|*changed|*:*)
if [ -n "${changed}" ]; then
echo "${changed}"
changed=
fi
;;
esac
case ${l} in
*extra)
if [ -d ${mnt}/${l% *} ]; then
find ${mnt}/${l% *} -exec echo "+ {}" \;
else
echo "+ ${mnt}/${l% *}"
fi
;;
*missing)
l=${l#./}
echo "- ${mnt}/${l% *}"
;;
*changed)
changed="M ${mnt}/${l% *}"
read_again=1
;;
extra:*)
if [ -d ${mnt}/${l#* } ]; then
find ${mnt}/${l#* } -exec echo "+ {}" \;
else
echo "+ ${mnt}/${l#* }"
fi
;;
*:*)
changed="M ${mnt}/${l%:*} ${l#*:}"
read_again=1
;;
*)
changed="${changed} ${l}"
read_again=1
;;
esac
# Need to read again to find all changes
[ ${read_again} -eq 1 ] && read l && continue
[ -n "${changed}" ] && echo "${changed}"
break
done
done
}
check_fs_violation() {
[ $# -eq 6 ] || eargs check_fs_violation mnt mtree_target port \
status_msg err_msg status_value
local mnt="$1"
local mtree_target="$2"
local port="$3"
local status_msg="$4"
local err_msg="$5"
local status_value="$6"
local tmpfile=$(mktemp -t check_fs_violation)
local ret=0
msg_n "${status_msg}..."
( cd "${mnt}" && mtree -X ${mnt}/.p/mtree.${mtree_target}exclude \
-f ${mnt}/.p/mtree.${mtree_target} \
-p . ) >> ${tmpfile}
echo " done"
if [ -s ${tmpfile} ]; then
msg "Error: ${err_msg}"
cat ${tmpfile}
bset_job_status "${status_value}" "${port}"
job_msg_verbose "Status ${COLOR_PORT}${port}${COLOR_RESET}: ${status_value}"
ret=1
fi
rm -f ${tmpfile}
return $ret
}
gather_distfiles() {
[ $# -eq 3 ] || eargs gather_distfiles origin from to
local origin="$1"
local from=$(realpath $2)
local to=$(realpath $3)
local sub dists d tosubd specials special
port_var_fetch "${origin}" \
DIST_SUBDIR sub \
ALLFILES dists \
_DEPEND_SPECIALS specials || \
err 1 "Failed to lookup distfiles for ${origin}"
job_msg_verbose "Status ${COLOR_PORT}${origin}${COLOR_RESET}: distfiles ${from} -> ${to}"
for d in ${dists}; do
[ -f ${from}/${sub}/${d} ] || continue
tosubd=${to}/${sub}/${d}
mkdir -p ${tosubd%/*} || return 1
do_clone "${from}/${sub}/${d}" "${to}/${sub}/${d}" || return 1
done
for special in ${specials}; do
case "${special}" in
${PORTSDIR}/*) special=${special#${PORTSDIR}/} ;;
esac
gather_distfiles ${special} ${from} ${to}
done
return 0
}
# Build+test port and return 1 on first failure
# Return 2 on test failure if PORTTESTING_FATAL=no
_real_build_port() {
[ $# -ne 1 ] && eargs _real_build_port portdir
local portdir=$1
local port=${portdir##${PORTSDIR}/}
local mnt
local log
local network
local hangstatus
local pkgenv phaseenv jpkg
local targets install_order
local jailuser
local testfailure=0
local max_execution_time allownetworking
_my_path mnt
_log_path log
# Use bootstrap PKG when not building pkg itself.
if false && [ ${QEMU_EMULATING} -eq 1 ]; then
case "${port}" in
ports-mgmt/pkg|ports-mgmt/pkg-devel) ;;
*)
if ensure_pkg_installed; then
export PKG_BIN="/.p/pkg-static"
fi
;;
esac
fi
for jpkg in ${ALLOW_MAKE_JOBS_PACKAGES}; do
case "${PKGNAME%-*}" in
${jpkg})
job_msg_verbose "Allowing MAKE_JOBS for ${COLOR_PORT}${port}${COLOR_RESET}"
sed -i '' '/DISABLE_MAKE_JOBS=poudriere/d' \
${mnt}/etc/make.conf
break
;;
esac
done
allownetworking=0
for jpkg in ${ALLOW_NETWORKING_PACKAGES}; do
case "${PKGNAME%-*}" in
${jpkg})
job_msg_warn "ALLOW_NETWORKING_PACKAGES: Allowing full network access for ${COLOR_PORT}${port}${COLOR_RESET}"
msg_warn "ALLOW_NETWORKING_PACKAGES: Allowing full network access for ${COLOR_PORT}${port}${COLOR_RESET}"
allownetworking=1
JNETNAME="n"
break
;;
esac
done
# Must install run-depends as 'actual-package-depends' and autodeps
# only consider installed packages as dependencies
jailuser=root
if [ "${BUILD_AS_NON_ROOT}" = "yes" ] &&
[ -z "$(injail /usr/bin/make -C ${portdir} -VNEED_ROOT)" ]; then
jailuser=${PORTBUILD_USER}
fi
# XXX: run-depends can come out of here with some bsd.port.mk
# changes. Easier once pkg_install is EOL.
install_order="run-depends stage package"
# Don't need to install if only making packages and not
# testing.
[ -n "${PORTTESTING}" ] && \
install_order="${install_order} install"
targets="check-sanity pkg-depends fetch-depends fetch checksum \
extract-depends extract patch-depends patch build-depends \
lib-depends configure build ${install_order} \
${PORTTESTING:+deinstall}"
# If not testing, then avoid rechecking deps in build/install;
# When testing, check depends twice to ensure they depend on
# proper files, otherwise they'll hit 'package already installed'
# errors.
if [ -z "${PORTTESTING}" ]; then
PORT_FLAGS="${PORT_FLAGS} NO_DEPENDS=yes"
else
PORT_FLAGS="${PORT_FLAGS} STRICT_DEPENDS=yes"
fi
for phase in ${targets}; do
max_execution_time=${MAX_EXECUTION_TIME}
phaseenv=
JUSER=${jailuser}
bset_job_status "${phase}" "${port}"
job_msg_verbose "Status ${COLOR_PORT}${port}${COLOR_RESET}: ${COLOR_PHASE}${phase}"
[ -n "${PORTTESTING}" ] && \
phaseenv="${phaseenv} DEVELOPER_MODE=yes"
case ${phase} in
check-sanity)
[ -n "${PORTTESTING}" ] && \
phaseenv="${phaseenv} DEVELOPER=1"
;;
fetch)
mkdir -p ${mnt}/portdistfiles
if [ "${DISTFILES_CACHE}" != "no" ]; then
echo "DISTDIR=/portdistfiles" >> ${mnt}/etc/make.conf
gather_distfiles "${port}" ${DISTFILES_CACHE} ${mnt}/portdistfiles || return 1
fi
JNETNAME="n"
JUSER=root
;;
extract)
max_execution_time=3600
if [ "${JUSER}" != "root" ]; then
chown -R ${JUSER} ${mnt}/wrkdirs
fi
;;
configure) [ -n "${PORTTESTING}" ] && markfs prebuild ${mnt} ;;
run-depends)
JUSER=root
if [ -n "${PORTTESTING}" ]; then
check_fs_violation ${mnt} prebuild "${port}" \
"Checking for filesystem violations" \
"Filesystem touched during build:" \
"build_fs_violation" ||
if [ "${PORTTESTING_FATAL}" != "no" ]; then
return 1
else
testfailure=2
fi
fi
;;
checksum|*-depends) JUSER=root ;;
stage) [ -n "${PORTTESTING}" ] && markfs prestage ${mnt} ;;
install)
max_execution_time=3600
JUSER=root
[ -n "${PORTTESTING}" ] && markfs preinst ${mnt}
;;
package)
max_execution_time=7200
if [ -n "${PORTTESTING}" ]; then
check_fs_violation ${mnt} prestage "${port}" \
"Checking for staging violations" \
"Filesystem touched during stage (files must install to \${STAGEDIR}):" \
"stage_fs_violation" || if [ "${PORTTESTING_FATAL}" != "no" ]; then
return 1
else
testfailure=2
fi
fi
;;
deinstall)
max_execution_time=3600
JUSER=root
# Skip for all linux ports, they are not safe
if [ "${PKGNAME%%*linux*}" != "" ]; then
msg "Checking shared library dependencies"
# Not using PKG_BIN to avoid bootstrap issues.
injail "${LOCALBASE}/sbin/pkg" query '%Fp' "${PKGNAME}" | \
injail xargs readelf -d 2>/dev/null | \
grep NEEDED | sort -u
fi
;;
esac
print_phase_header ${phase}
if [ "${phase}" = "package" ]; then
echo "PACKAGES=/.npkg" >> ${mnt}/etc/make.conf
# Create sandboxed staging dir for new package for this build
rm -rf "${PACKAGES}/.npkg/${PKGNAME}"
mkdir -p "${PACKAGES}/.npkg/${PKGNAME}"
${NULLMOUNT} \
"${PACKAGES}/.npkg/${PKGNAME}" \
${mnt}/.npkg
chown -R ${JUSER} ${mnt}/.npkg
:> "${mnt}/.npkg_mounted"
fi
if [ "${JUSER}" = "root" ]; then
export UID=0
export GID=0
else
export UID=${PORTBUILD_UID}
export GID=${PORTBUILD_UID}
fi
if [ "${phase#*-}" = "depends" ]; then
# No need for nohang or PORT_FLAGS for *-depends
injail /usr/bin/env USE_PACKAGE_DEPENDS_ONLY=1 ${phaseenv} \
/usr/bin/make -C ${portdir} ${phase} || return 1
else
# Only set PKGENV during 'package' to prevent
# testport-built packages from going into the main repo
# Also enable during stage/install since it now
# uses a pkg for pkg_tools
if [ "${phase}" = "package" ]; then
pkgenv="${PKGENV}"
else
pkgenv=
fi
nohang ${max_execution_time} ${NOHANG_TIME} \
${log}/logs/${PKGNAME}.log \
${MASTERMNT}/.p/var/run/${MY_JOBID:-00}_nohang.pid \
injail /usr/bin/env ${pkgenv} ${phaseenv} ${PORT_FLAGS} \
/usr/bin/make -C ${portdir} ${phase}
hangstatus=$? # This is done as it may return 1 or 2 or 3
if [ $hangstatus -ne 0 ]; then
# 1 = cmd failed, not a timeout
# 2 = log timed out
# 3 = cmd timeout
if [ $hangstatus -eq 2 ]; then
msg "Killing runaway build after ${NOHANG_TIME} seconds with no output"
bset_job_status "${phase}/runaway" "${port}"
job_msg_verbose "Status ${COLOR_PORT}${port}${COLOR_RESET}: ${COLOR_PHASE}runaway"
elif [ $hangstatus -eq 3 ]; then
msg "Killing timed out build after ${max_execution_time} seconds"
bset_job_status "${phase}/timeout" "${port}"
job_msg_verbose "Status ${COLOR_PORT}${port}${COLOR_RESET}: ${COLOR_PHASE}timeout"
fi
return 1
fi
fi
if [ "${phase}" = "checksum" ] && \
[ ${allownetworking} -eq 0 ]; then
JNETNAME=""
fi
print_phase_footer
if [ "${phase}" = "checksum" -a "${DISTFILES_CACHE}" != "no" ]; then
gather_distfiles "${port}" ${mnt}/portdistfiles ${DISTFILES_CACHE} || return 1
fi
if [ "${phase}" = "stage" -a -n "${PORTTESTING}" ]; then
local die=0
bset_job_status "stage-qa" "${port}"
if ! injail /usr/bin/env DEVELOPER=1 ${PORT_FLAGS} \
/usr/bin/make -C ${portdir} stage-qa; then
msg "Error: stage-qa failures detected"
[ "${PORTTESTING_FATAL}" != "no" ] &&
return 1
die=1
fi
bset_job_status "check-plist" "${port}"
if ! injail /usr/bin/env DEVELOPER=1 ${PORT_FLAGS} \
/usr/bin/make -C ${portdir} check-plist; then
msg "Error: check-plist failures detected"
[ "${PORTTESTING_FATAL}" != "no" ] &&
return 1
die=1
fi
if [ ${die} -eq 1 ]; then
testfailure=2
die=0
fi
fi
if [ "${phase}" = "deinstall" ]; then
local add=$(mktemp -t lo.add)
local add1=$(mktemp -t lo.add1)
local del=$(mktemp -t lo.del)
local del1=$(mktemp -t lo.del1)
local mod=$(mktemp -t lo.mod)
local mod1=$(mktemp -t lo.mod1)
local die=0
PREFIX=$(injail /usr/bin/env ${PORT_FLAGS} /usr/bin/make -C ${portdir} -VPREFIX)
msg "Checking for extra files and directories"
bset_job_status "leftovers" "${port}"
if [ -f "${mnt}${PORTSDIR}/Mk/Scripts/check_leftovers.sh" ]; then
check_leftovers ${mnt} | sed -e "s|${mnt}||" |
injail /usr/bin/env PORTSDIR=${PORTSDIR} \
${PORT_FLAGS} /bin/sh \
${PORTSDIR}/Mk/Scripts/check_leftovers.sh \
${port} | while read modtype data; do
case "${modtype}" in
+) echo "${data}" >> ${add} ;;
-) echo "${data}" >> ${del} ;;
M) echo "${data}" >> ${mod} ;;
esac
done
else
# LEGACY - Support for older ports tree.
local users user homedirs plistsub_sed
plistsub_sed=$(injail /usr/bin/env ${PORT_FLAGS} /usr/bin/make -C ${portdir} -V'PLIST_SUB:C/"//g:NLIB32*:NPERL_*:NPREFIX*:N*="":N*="@comment*:C/(.*)=(.*)/-es!\2!%%\1%%!g/')
users=$(injail /usr/bin/make -C ${portdir} -VUSERS)
homedirs=""
for user in ${users}; do
user=$(grep ^${user}: ${mnt}${PORTSDIR}/UIDs | cut -f 9 -d : | sed -e "s|/usr/local|${PREFIX}| ; s|^|${mnt}|")
homedirs="${homedirs} ${user}"
done
check_leftovers ${mnt} | \
while read modtype path extra; do
local ppath ignore_path=0
# If this is a directory, use @dirrm in output
if [ -d "${path}" ]; then
ppath="@dirrm "`echo $path | sed \
-e "s,^${mnt},," \
-e "s,^${PREFIX}/,," \
${plistsub_sed} \
`
else
ppath=`echo "$path" | sed \
-e "s,^${mnt},," \
-e "s,^${PREFIX}/,," \
${plistsub_sed} \
`
fi
case $modtype in
+)
if [ -d "${path}" ]; then
# home directory of users created
case " ${homedirs} " in
*\ ${path}\ *) continue;;
*\ ${path}/*\ *) continue;;
esac
fi
case "${ppath}" in
# gconftool-2 --makefile-uninstall-rule is unpredictable
etc/gconf/gconf.xml.defaults/%gconf-tree*.xml) ;;
# fc-cache - skip for now
/var/db/fontconfig/*) ;;
*) echo "${ppath}" >> ${add} ;;
esac
;;
-)
# Skip if it is PREFIX and non-LOCALBASE. See misc/kdehier4
# or mail/qmail for examples
[ "${path#${mnt}}" = "${PREFIX}" -a \
"${LOCALBASE}" != "${PREFIX}" ] && ignore_path=1
# fc-cache - skip for now
case "${ppath}" in
/var/db/fontconfig/*) ignore_path=1 ;;
esac
if [ $ignore_path -eq 0 ]; then
echo "${ppath}" >> ${del}
fi
;;
M)
case "${ppath}" in
# gconftool-2 --makefile-uninstall-rule is unpredictable
etc/gconf/gconf.xml.defaults/%gconf-tree*.xml) ;;
# This is a cache file for gio modules could be modified for any gio modules
lib/gio/modules/giomodule.cache) ;;
# removal of info files leaves entry uneasy to cleanup in info/dir
# accept a modification of this file
info/dir) ;;
*/info/dir) ;;
# The is pear database cache
%%PEARDIR%%/.depdb|%%PEARDIR%%/.filemap) ;;
#ls-R files from texmf are often regenerated
*/ls-R);;
# Octave packages database, blank lines can be inserted between pre-install and post-deinstall
share/octave/octave_packages) ;;
# xmlcatmgr is constantly updating catalog.ports ignore modification to that file
share/xml/catalog.ports);;
# fc-cache - skip for now
/var/db/fontconfig/*) ;;
*) echo "${ppath#@dirrm } ${extra}" >> ${mod} ;;
esac
;;
esac
done
fi
sort ${add} > ${add1}
sort ${del} > ${del1}
sort ${mod} > ${mod1}
comm -12 ${add1} ${del1} >> ${mod1}
comm -23 ${add1} ${del1} > ${add}
comm -13 ${add1} ${del1} > ${del}
if [ -s "${add}" ]; then
msg "Error: Files or directories left over:"
die=1
grep -v "^@dirrm" ${add}
grep "^@dirrm" ${add} | sort -r
fi
if [ -s "${del}" ]; then
msg "Error: Files or directories removed:"
die=1
cat ${del}
fi
if [ -s "${mod}" ]; then
msg "Error: Files or directories modified:"
die=1
cat ${mod1}
fi
[ ${die} -eq 1 -a "${SCRIPTPATH##*/}" = "testport.sh" \
-a "${PREFIX}" != "${LOCALBASE}" ] && msg \
"This test was done with PREFIX!=LOCALBASE which \
may show failures if the port does not respect PREFIX. \
Try testport with -n to use PREFIX=LOCALBASE"
rm -f ${add} ${add1} ${del} ${del1} ${mod} ${mod1}
[ $die -eq 0 ] || if [ "${PORTTESTING_FATAL}" != "no" ]; then
return 1
else
testfailure=2
fi
fi
done
if [ -d "${PACKAGES}/.npkg/${PKGNAME}" ]; then
# everything was fine we can copy the package to the package
# directory
find ${PACKAGES}/.npkg/${PKGNAME} \
-mindepth 1 \( -type f -or -type l \) | while read pkg_path; do
pkg_file=${pkg_path#${PACKAGES}/.npkg/${PKGNAME}}
pkg_base=${pkg_file%/*}
mkdir -p ${PACKAGES}/${pkg_base}
mv ${pkg_path} ${PACKAGES}/${pkg_base}
done
fi
bset_job_status "build_port_done" "${port}"
return ${testfailure}
}
# Wrapper to ensure JUSER is reset and any other cleanup needed
build_port() {
local ret
_real_build_port "$@" || ret=$?
JUSER=root
return ${ret}
}
# Save wrkdir and return path to file
save_wrkdir() {
[ $# -ne 4 ] && eargs save_wrkdir mnt port portdir phase
local mnt=$1
local port="$2"
local portdir="$3"
local phase="$4"
local tardir=${POUDRIERE_DATA}/wrkdirs/${MASTERNAME}/${PTNAME}
local tarname=${tardir}/${PKGNAME}.${WRKDIR_ARCHIVE_FORMAT}
local mnted_portdir=${mnt}/wrkdirs/${portdir}
[ "${SAVE_WRKDIR}" != "no" ] || return 0
# Only save if not in fetch/checksum phase
[ "${failed_phase}" != "fetch" -a "${failed_phase}" != "checksum" -a \
"${failed_phase}" != "extract" ] || return 0
mkdir -p ${tardir}
# Tar up the WRKDIR, and ignore errors
case ${WRKDIR_ARCHIVE_FORMAT} in
tar) COMPRESSKEY="" ;;
tgz) COMPRESSKEY="z" ;;
tbz) COMPRESSKEY="j" ;;
txz) COMPRESSKEY="J" ;;
esac
rm -f ${tarname}
tar -s ",${mnted_portdir},," -c${COMPRESSKEY}f ${tarname} ${mnted_portdir}/work > /dev/null 2>&1
job_msg "Saved ${COLOR_PORT}${port}${COLOR_RESET} wrkdir to: ${tarname}"
}
start_builder() {
local id=$1
local arch=$2
local mnt MY_JOBID
MY_JOBID=${id}
_my_path mnt
# Jail might be lingering from previous build. Already recursively
# destroyed all the builder datasets, so just try stopping the jail
# and ignore any errors
stop_builder "${id}"
mkdir -p "${mnt}"
clonefs ${MASTERMNT} ${mnt} prepkg
markfs prepkg ${mnt} >/dev/null
do_jail_mounts "${MASTERMNT}" ${mnt} ${arch} ${jname}
do_portbuild_mounts ${mnt} ${jname} ${ptname} ${setname}
jstart
bset ${id} status "idle:"
run_hook builder start "${id}" "${mnt}"
}
start_builders() {
local arch=$(injail uname -p)
bset builders "${JOBS}"
bset status "starting_builders:"
parallel_start
for j in ${JOBS}; do
parallel_run start_builder ${j} ${arch}
done
parallel_stop
}
stop_builder() {
[ $# -eq 1 ] || eargs stop_builder jobid
local jobid="$1"
local mnt MY_JOBID
MY_JOBID="${jobid}"
_my_path mnt
run_hook builder stop "${jobid}" "${mnt}"
jstop
destroyfs "${mnt}" jail
}
stop_builders() {
local PARALLEL_JOBS real_parallel_jobs
# wait for the last running processes
cat ${MASTERMNT}/.p/var/run/*.pid 2>/dev/null | xargs pwait 2>/dev/null
if [ ${PARALLEL_JOBS} -ne 0 ]; then
msg "Stopping ${PARALLEL_JOBS} builders"
real_parallel_jobs=${PARALLEL_JOBS}
if [ ${UMOUNT_BATCHING} -eq 0 ]; then
# Limit builders
PARALLEL_JOBS=2
fi
parallel_start
for j in ${JOBS-$(jot -w %02d ${real_parallel_jobs})}; do
parallel_run stop_builder "${j}"
done
parallel_stop
fi
# No builders running, unset JOBS
JOBS=""
}
sanity_check_queue() {
local always_fail=${1:-1}
local crashed_packages dependency_cycles deps pkgname origin
local failed_phase pwd
pwd="${PWD}"
cd "${MASTERMNT}/.p"
# If there are still packages marked as "building" they have crashed
# and it's likely some poudriere or system bug
crashed_packages=$( \
find building -type d -mindepth 1 -maxdepth 1 | \
sed -e "s,^building/,," | tr '\n' ' ' \
)
[ -z "${crashed_packages}" ] || \
err 1 "Crashed package builds detected: ${crashed_packages}"
# Check if there's a cycle in the need-to-build queue
dependency_cycles=$(\
find deps -mindepth 2 | \
sed -e "s,^deps/,," -e 's:/: :' | \
# Only cycle errors are wanted
tsort 2>&1 >/dev/null | \
sed -e 's/tsort: //' | \
awk -f ${AWKPREFIX}/dependency_loop.awk \
)
if [ -n "${dependency_cycles}" ]; then
err 1 "Dependency loop detected:
${dependency_cycles}"
fi
if [ ${always_fail} -eq 0 ]; then
cd "${pwd}"
return 0
fi
dead_packages=
highest_dep=
while read deps pkgname; do
[ -z "${highest_dep}" ] && highest_dep=${deps}
[ ${deps} -ne ${highest_dep} ] && break
dead_packages="${dead_packages} ${pkgname}"
done <<-EOF
$(find deps -mindepth 2 | \
sed -e "s,^deps/,," -e 's:/: :' | \
tsort -D 2>/dev/null | sort -nr)
EOF
if [ -n "${dead_packages}" ]; then
failed_phase="stuck_in_queue"
for pkgname in ${dead_packages}; do
crashed_build "${pkgname}" "${failed_phase}"
done
cd "${pwd}"
return 0
fi
# No cycle, there's some unknown poudriere bug
err 1 "Unknown stuck queue bug detected. Please submit the entire build output to poudriere developers.
$(find ${MASTERMNT}/.p/building ${MASTERMNT}/.p/pool ${MASTERMNT}/.p/deps ${MASTERMNT}/.p/cleaning)"
}
queue_empty() {
[ "${PWD}" = "${MASTERMNT}/.p/pool" ] || \
err 1 "queue_empty requires PWD=${MASTERMNT}/.p/pool"
local pool_dir dirs
local n
# CWD is MASTERMNT/.p/pool
dirs="../deps ${POOL_BUCKET_DIRS}"
n=0
# Check twice that the queue is empty. This avoids racing with
# clean.sh and balance_pool() moving files between the dirs.
while [ ${n} -lt 2 ]; do
for pool_dir in ${dirs}; do
if ! dirempty ${pool_dir}; then
return 1
fi
done
n=$((n + 1))
done
# Queue is empty
return 0
}
job_done() {
[ "${PWD}" = "${MASTERMNT}/.p/pool" ] || \
err 1 "job_done requires PWD=${MASTERMNT}/.p/pool"
[ $# -eq 1 ] || eargs job_done j
local j="$1"
local pkgname status
# CWD is MASTERMNT/.p/pool
# Failure to find this indicates the job is already done.
hash_get builder_pkgnames "${j}" pkgname || return 1
hash_unset builder_pids "${j}"
hash_unset builder_pkgnames "${j}"
rm -f "../var/run/${j}.pid"
_bget status ${j} status
rmdir "../building/${pkgname}"
if [ "${status%%:*}" = "done" ]; then
bset ${j} status "idle:"
else
# Try to cleanup and mark build crashed
MY_JOBID="${j}" crashed_build "${pkgname}" "${status%%:*}"
bset ${j} status "crashed:"
fi
}
build_queue() {
[ "${PWD}" = "${MASTERMNT}/.p/pool" ] || \
err 1 "build_queue requires PWD=${MASTERMNT}/.p/pool"
local j jobid pid pkgname builders_active queue_empty
local builders_idle idle_only timeout log
_log_path log
mkfifo ${MASTERMNT}/.p/builders.pipe
exec 6<> ${MASTERMNT}/.p/builders.pipe
rm -f ${MASTERMNT}/.p/builders.pipe
queue_empty=0
msg "Hit CTRL+t at any time to see build progress and stats"
idle_only=0
while :; do
builders_active=0
builders_idle=0
timeout=30
for j in ${JOBS}; do
# Check if pid is alive. A job will have no PID if it
# is idle. idle_only=1 is a quick check for giving
# new work only to idle workers.
if hash_get builder_pids "${j}" pid; then
if [ ${idle_only} -eq 1 ] ||
kill -0 ${pid} 2>/dev/null; then
# Job still active or skipping busy.
builders_active=1
continue
fi
job_done "${j}"
# Set a 0 timeout to quickly rescan for idle
# builders to toss a job at.
[ ${queue_empty} -eq 0 -a \
${builders_idle} -eq 1 ] && timeout=0
fi
# This builder is idle and needs work.
[ ${queue_empty} -eq 0 ] || continue
next_in_queue pkgname || \
err 1 "Failed to find a package from the queue."
if [ -z "${pkgname}" ]; then
# Check if the ready-to-build pool and need-to-build pools
# are empty
queue_empty && queue_empty=1
# Pool is waiting on dep, wait until a build
# is done before checking the queue again
builders_idle=1
else
MY_JOBID="${j}" \
PORTTESTING=$(get_porttesting "${pkgname}") \
spawn_protected build_pkg "${pkgname}"
pid=$!
echo "${pid}" > "../var/run/${j}.pid"
hash_set builder_pids "${j}" "${pid}"
hash_set builder_pkgnames "${j}" "${pkgname}"
# A new job is spawned, try to read the queue
# just to keep things moving
builders_active=1
fi
done
if [ ${queue_empty} -eq 1 ]; then
if [ ${builders_active} -eq 1 ]; then
# The queue is empty, but builds are still
# going. Wait on them below.
# FALLTHROUGH
else
# All work is done
sanity_check_queue 0
break
fi
fi
# If builders are idle then there is a problem.
[ ${builders_active} -eq 1 ] || sanity_check_queue
if [ "${HTML_TRACK_REMAINING}" = "yes" ]; then
{
# Find items in pool ready-to-build
find . -type d -depth 2
# Find items in queue not ready-to-build.
find ../deps -type d -depth 1
} | sed -e 's,.*/,,' > \
"${log}/.poudriere.ports.remaining.tmp"
mv -f "${log}/.poudriere.ports.remaining.tmp" \
"${log}/.poudriere.ports.remaining"
fi
# Wait for an event from a child. All builders are busy.
unset jobid; until trappedinfo=; read -t ${timeout} jobid <&6 ||
[ -z "$trappedinfo" ]; do :; done
if [ -n "${jobid}" ]; then
# A job just finished.
if job_done "${jobid}"; then
# Do a quick scan to try dispatching
# ready-to-build to idle builders.
idle_only=1
else
# The job is already done. It was found to be
# done by a kill -0 check in a scan.
fi
else
# No event found. The next scan will check for
# crashed builders and deadlocks by validating
# every builder is really non-idle.
idle_only=0
fi
done
exec 6<&- 6>&-
}
calculate_tobuild() {
local nbq nbb nbf nbi nbsndone nremaining
_bget nbq stats_queued 2>/dev/null || nbq=0
_bget nbb stats_built 2>/dev/null || nbb=0
_bget nbf stats_failed 2>/dev/null || nbf=0
_bget nbi stats_ignored 2>/dev/null || nbi=0
_bget nbs stats_skipped 2>/dev/null || nbs=0
ndone=$((nbb + nbf + nbi + nbs))
nremaining=$((nbq - ndone))
echo ${nremaining}
}
status_is_stopped() {
[ $# -eq 1 ] || eargs status_is_stopped status
local status="$1"
case "${status}" in
sigterm:|sigint:|crashed:|stop:|stopped:*) return 0 ;;
esac
return 1
}
calculate_elapsed_from_log() {
[ $# -eq 2 ] || eargs calculate_elapsed_from_log now log
local now="$1"
local log="$2"
[ -f "${log}/.poudriere.status" ] || return 1
start_end_time=$(stat -f '%B %m' ${log}/.poudriere.status.journal% 2>/dev/null || stat -f '%B %m' ${log}/.poudriere.status)
start_time=${start_end_time% *}
if status_is_stopped "${status}"; then
end_time=${start_end_time#* }
else
end_time=${now}
fi
_start_time=${start_time}
_end_time=${end_time}
_elapsed_time=$((${end_time} - ${start_time}))
return 0
}
calculate_duration() {
[ $# -eq 2 ] || eargs calculate_duration var_return elapsed
local var_return="$1"
local _elapsed="$2"
local seconds minutes hours _duration
seconds=$((${_elapsed} % 60))
minutes=$(((${_elapsed} / 60) % 60))
hours=$((${_elapsed} / 3600))
_duration=$(printf "%02d:%02d:%02d" ${hours} ${minutes} ${seconds})
setvar "${var_return}" "${_duration}"
}
# Build ports in parallel
# Returns when all are built.
parallel_build() {
local jname=$1
local ptname=$2
local setname=$3
local real_parallel_jobs=${PARALLEL_JOBS}
local nremaining=$(calculate_tobuild)
# Subtract the 1 for the main port to test
[ "${SCRIPTPATH##*/}" = "testport.sh" ] && \
nremaining=$((${nremaining} - 1))
# If pool is empty, just return
[ ${nremaining} -eq 0 ] && return 0
# Minimize PARALLEL_JOBS to queue size
[ ${PARALLEL_JOBS} -gt ${nremaining} ] && PARALLEL_JOBS=${nremaining##* }
msg "Building ${nremaining} packages using ${PARALLEL_JOBS} builders"
JOBS="$(jot -w %02d ${PARALLEL_JOBS})"
bset status "starting_jobs:"
msg "Starting/Cloning builders"
start_builders
coprocess_start pkg_cacher
bset status "parallel_build:"
[ ! -d "${MASTERMNT}/.p/pool" ] && err 1 "Build pool is missing"
cd "${MASTERMNT}/.p/pool"
build_queue
cd ..
bset status "stopping_jobs:"
stop_builders
bset status "updating_stats:"
update_stats || msg_warn "Error updating build stats"
update_stats_done=1
bset status "idle:"
# Restore PARALLEL_JOBS
PARALLEL_JOBS=${real_parallel_jobs}
return 0
}
crashed_build() {
[ $# -eq 2 ] || eargs crashed_build pkgname failed_phase
local pkgname="$1"
local failed_phase="$2"
local origin log
_log_path log
cache_get_origin origin "${pkgname}"
echo "Build crashed: ${failed_phase}" >> "${log}/logs/${pkgname}.log"
# If the file already exists then all of this handling was done in
# build_pkg() already; The port failed already. What crashed
# came after.
if ! [ -e "${log}/logs/errors/${pkgname}.log" ]; then
# Symlink the buildlog into errors/
ln -s "../${pkgname}.log" "${log}/logs/errors/${pkgname}.log"
badd ports.failed \
"${origin} ${pkgname} ${failed_phase} ${failed_phase}"
COLOR_ARROW="${COLOR_FAIL}" msg \
"${COLOR_FAIL}Finished ${COLOR_PORT}${origin}${COLOR_FAIL}: Failed: ${COLOR_PHASE}${failed_phase}"
run_hook pkgbuild failed "${origin}" "${pkgname}" \
"${failed_phase}" \
"${log}/logs/errors/${pkgname}.log"
fi
clean_pool "${pkgname}" "${origin}" "${failed_phase}"
stop_build "${pkgname}" "${origin}" 1 >> "${log}/logs/${pkgname}.log"
}
clean_pool() {
[ $# -ne 3 ] && eargs clean_pool pkgname origin clean_rdepends
local pkgname=$1
local port=$2
local clean_rdepends="$3"
local skipped_origin
[ -n "${MY_JOBID}" ] && bset ${MY_JOBID} status "clean_pool:"
[ -z "${port}" -a -n "${clean_rdepends}" ] && \
cache_get_origin port "${pkgname}"
# Cleaning queue (pool is cleaned here)
sh ${SCRIPTPREFIX}/clean.sh "${MASTERMNT}" "${pkgname}" "${clean_rdepends}" | sort -u | while read skipped_pkgname; do
cache_get_origin skipped_origin "${skipped_pkgname}"
badd ports.skipped "${skipped_origin} ${skipped_pkgname} ${pkgname}"
COLOR_ARROW="${COLOR_SKIP}" \
job_msg "${COLOR_SKIP}Skipping ${COLOR_PORT}${skipped_origin}${COLOR_SKIP}: Dependent port ${COLOR_PORT}${port}${COLOR_SKIP} ${clean_rdepends}"
run_hook pkgbuild skipped "${skipped_origin}" "${skipped_pkgname}" "${port}"
done
(
cd "${MASTERMNT}/.p"
balance_pool || :
)
}
print_phase_header() {
printf "=======================<phase: %-15s>============================\n" "$1"
}
print_phase_footer() {
echo "==========================================================================="
}
build_pkg() {
# If this first check fails, the pool will not be cleaned up,
# since PKGNAME is not yet set.
[ $# -ne 1 ] && eargs build_pkg pkgname
local pkgname="$1"
local port portdir
local build_failed=0
local name
local mnt
local failed_status failed_phase cnt
local clean_rdepends
local log
local ignore
local errortype
local ret=0
local elapsed now
_my_path mnt
_my_name name
_log_path log
clean_rdepends=
trap '' SIGTSTP
PKGNAME="${pkgname}" # set ASAP so jail_cleanup() can use it
cache_get_origin port "${pkgname}"
portdir="${PORTSDIR}/${port}"
if [ -n "${MAX_MEMORY_BYTES}" -o -n "${MAX_FILES}" ]; then
JEXEC_LIMITS=1
fi
setproctitle "build_pkg (${pkgname})" || :
TIME_START_JOB=$(clock -monotonic)
# Don't show timestamps in msg() which goes to logs, only job_msg()
# which goes to master
NO_ELAPSED_IN_MSG=1
colorize_job_id COLOR_JOBID "${MY_JOBID}"
job_msg "Building ${COLOR_PORT}${port}${COLOR_RESET}"
bset_job_status "starting" "${port}"
if [ "${USE_JEXECD}" = "no" ]; then
# Kill everything in jail first
jkill
fi
if [ ${TMPFS_LOCALBASE} -eq 1 -o ${TMPFS_ALL} -eq 1 ]; then
if [ -f "${mnt}/${LOCALBASE:-/usr/local}/.mounted" ]; then
umount ${UMOUNT_NONBUSY} ${mnt}/${LOCALBASE:-/usr/local}
fi
mnt_tmpfs localbase ${mnt}/${LOCALBASE:-/usr/local}
do_clone -r "${MASTERMNT}/${LOCALBASE:-/usr/local}" \
"${mnt}/${LOCALBASE:-/usr/local}"
:> "${mnt}/${LOCALBASE:-/usr/local}/.mounted"
fi
[ -f ${mnt}/.need_rollback ] && rollbackfs prepkg ${mnt}
[ -f ${mnt}/.need_rollback ] && \
err 1 "Failed to rollback ${mnt} to prepkg"
:> ${mnt}/.need_rollback
case " ${BLACKLIST} " in
*\ ${port}\ *) ignore="Blacklisted" ;;
esac
# If this port is IGNORED, skip it
# This is checked here instead of when building the queue
# as the list may start big but become very small, so here
# is a less-common check
: ${ignore:=$(injail /usr/bin/make -C ${portdir} -VIGNORE)}
rm -rf ${mnt}/wrkdirs/* || :
log_start 0
msg "Building ${port}"
buildlog_start ${portdir}
# Ensure /dev/null exists (kern/139014)
[ ${JAILED} -eq 0 ] && ! [ -c "${mnt}/dev/null" ] && \
devfs -m ${mnt}/dev rule apply path null unhide
if [ -n "${ignore}" ]; then
msg "Ignoring ${port}: ${ignore}"
badd ports.ignored "${port} ${PKGNAME} ${ignore}"
COLOR_ARROW="${COLOR_IGNORE}" job_msg "${COLOR_IGNORE}Finished ${COLOR_PORT}${port}${COLOR_IGNORE}: Ignored: ${ignore}"
clean_rdepends="ignored"
run_hook pkgbuild ignored "${port}" "${PKGNAME}" "${ignore}"
else
build_port ${portdir} || ret=$?
if [ ${ret} -ne 0 ]; then
build_failed=1
# ret=2 is a test failure
if [ ${ret} -eq 2 ]; then
failed_phase=$(awk -f ${AWKPREFIX}/processonelog2.awk \
${log}/logs/${PKGNAME}.log \
2> /dev/null)
else
_bget failed_status ${MY_JOBID} status
failed_phase=${failed_status%%:*}
fi
save_wrkdir ${mnt} "${port}" "${portdir}" "${failed_phase}" || :
elif [ -f ${mnt}/${portdir}/.keep ]; then
save_wrkdir ${mnt} "${port}" "${portdir}" "noneed" ||:
fi
now=$(clock -monotonic)
elapsed=$((${now} - ${TIME_START_JOB}))
if [ ${build_failed} -eq 0 ]; then
badd ports.built "${port} ${PKGNAME} ${elapsed}"
COLOR_ARROW="${COLOR_SUCCESS}" job_msg "${COLOR_SUCCESS}Finished ${COLOR_PORT}${port}${COLOR_SUCCESS}: Success"
run_hook pkgbuild success "${port}" "${PKGNAME}"
# Cache information for next run
pkg_cacher_queue "${port}" "${pkgname}" || :
else
# Symlink the buildlog into errors/
ln -s ../${PKGNAME}.log ${log}/logs/errors/${PKGNAME}.log
errortype=$(/bin/sh ${SCRIPTPREFIX}/processonelog.sh \
${log}/logs/errors/${PKGNAME}.log \
2> /dev/null)
badd ports.failed "${port} ${PKGNAME} ${failed_phase} ${errortype} ${elapsed}"
COLOR_ARROW="${COLOR_FAIL}" job_msg "${COLOR_FAIL}Finished ${COLOR_PORT}${port}${COLOR_FAIL}: Failed: ${COLOR_PHASE}${failed_phase}"
run_hook pkgbuild failed "${port}" "${PKGNAME}" "${failed_phase}" \
"${log}/logs/errors/${PKGNAME}.log"
# ret=2 is a test failure
if [ ${ret} -eq 2 ]; then
clean_rdepends=
else
clean_rdepends="failed"
fi
fi
msg "Cleaning up wrkdir"
injail /usr/bin/make -C "${portdir}" -DNOCLEANDEPENDS clean || :
rm -rf ${mnt}/wrkdirs/* || :
fi
clean_pool ${PKGNAME} ${port} "${clean_rdepends}"
stop_build "${PKGNAME}" ${port} ${build_failed}
log_stop
bset ${MY_JOBID} status "done:"
echo ${MY_JOBID} >&6
}
stop_build() {
[ $# -eq 3 ] || eargs stop_build pkgname origin build_failed
local pkgname="$1"
local origin="$2"
local build_failed="$3"
local mnt
if [ -n "${MY_JOBID}" ]; then
_my_path mnt
if [ -f "${mnt}/.npkg_mounted" ]; then
umount ${UMOUNT_NONBUSY} "${mnt}/.npkg"
rm -f "${mnt}/.npkg_mounted"
fi
rm -rf "${PACKAGES}/.npkg/${PKGNAME}"
if jail_has_processes; then
msg_warn "Leftover processes:"
injail ps auxwwd | egrep -v '(ps auxwwd|jexecd)'
fi
if JNETNAME="n" jail_has_processes; then
msg_warn "Leftover processes (network jail):"
JNETNAME="n" injail ps auxwwd | egrep -v '(ps auxwwd|jexecd)'
fi
if [ "${USE_JEXECD}" = "no" ]; then
# Always kill to avoid missing anything
jkill
fi
fi
buildlog_stop "${pkgname}" ${origin} ${build_failed}
}
prefix_stderr_quick() {
local -; set +x
local extra="$1"
shift 1
{
{ "$@"; } 2>&1 1>&3 | {
setproctitle "${PROC_TITLE} (prefix_stderr_quick)"
while read -r line; do
msg_warn "${extra}: ${line}"
done
}
} 3>&1
}
prefix_stderr() {
local extra="$1"
shift 1
local prefixpipe prefixpid ret
prefixpipe=$(mktemp -ut prefix_stderr.pipe)
mkfifo "${prefixpipe}"
(
set +x
setproctitle "${PROC_TITLE} (prefix_stderr)"
while read -r line; do
msg_warn "${extra}: ${line}"
done
) < ${prefixpipe} &
prefixpid=$!
exec 4>&2
exec 2> "${prefixpipe}"
rm -f "${prefixpipe}"
ret=0
"$@" || ret=$?
exec 2>&4 4>&-
wait ${prefixpid}
return ${ret}
}
prefix_stdout() {
local extra="$1"
shift 1
local prefixpipe prefixpid ret
prefixpipe=$(mktemp -ut prefix_stdout.pipe)
mkfifo "${prefixpipe}"
(
set +x
setproctitle "${PROC_TITLE} (prefix_stdout)"
while read -r line; do
msg "${extra}: ${line}"
done
) < ${prefixpipe} &
prefixpid=$!
exec 3>&1
exec > "${prefixpipe}"
rm -f "${prefixpipe}"
ret=0
"$@" || ret=$?
exec 1>&3 3>&-
wait ${prefixpid}
return ${ret}
}
prefix_output() {
local extra="$1"
shift 1
prefix_stderr "${extra}" prefix_stdout "${extra}" "$@"
}
deps_fetch_vars() {
[ $# -ne 3 ] && eargs deps_fetch_vars origin deps_var pkgname_var
local origin="$1"
local deps_var="$2"
local pkgname_var="$3"
local _pkgname _pkg_deps _lib_depends= _run_depends= _selected_options=
local _changed_options= _changed_deps=
local _existing_pkgname _existing_origin
if [ "${CHECK_CHANGED_OPTIONS}" != "no" ]; then
_changed_options="SELECTED_OPTIONS:O _selected_options"
fi
if [ "${CHECK_CHANGED_DEPS}" != "no" ]; then
_changed_deps="LIB_DEPENDS _lib_depends RUN_DEPENDS _run_depends"
fi
if ! port_var_fetch "${origin}" \
PKGNAME _pkgname \
${_changed_deps} \
${_changed_options} \
_PDEPS='${PKG_DEPENDS} ${EXTRACT_DEPENDS} ${PATCH_DEPENDS} ${FETCH_DEPENDS} ${BUILD_DEPENDS} ${LIB_DEPENDS} ${RUN_DEPENDS}' \
'${_PDEPS:C,([^:]*):([^:]*):?.*,\2,:C,^${PORTSDIR}/,,:O:u}' \
_pkg_deps; then
msg_error "Error fetching dependencies for ${COLOR_PORT}${origin}${COLOR_RESET}"
return 1
fi
[ -n "${_pkgname}" ] || \
err 1 "deps_fetch_vars: failed to get PKGNAME for ${origin}"
setvar "${deps_var}" "${_pkg_deps}"
setvar "${pkgname_var}" "${_pkgname}"
if ! shash_get origin-pkgname "${origin}" _existing_pkgname; then
# Make sure this origin did not already exist
cache_get_origin _existing_origin "${_pkgname}" 2>/dev/null || :
# It may already exist due to race conditions, it is not
# harmful. Just ignore.
if [ "${_existing_origin}" != "${origin}" ]; then
[ -n "${_existing_origin}" ] && \
err 1 "Duplicated origin for ${_pkgname}: ${COLOR_PORT}${origin}${COLOR_RESET} AND ${COLOR_PORT}${_existing_origin}${COLOR_RESET}. Rerun with -v to see which ports are depending on these."
shash_set origin-pkgname "${origin}" "${_pkgname}"
shash_set pkgname-origin "${_pkgname}" "${origin}"
fi
else
# compute_deps raced and managed to process the same port
# before creating its pool dir. This is wasted time
# but is harmless. We could add some kind of locking
# based on the origin name, but it's probably not
# worth it. The same race exists in the above case.
return 0
fi
shash_set pkgname-deps "${_pkgname}" "${_pkg_deps}"
# Store for delete_old_pkg
if [ -n "${_lib_depends}" ]; then
shash_set pkgname-lib_deps "${_pkgname}" "${_lib_depends}"
fi
if [ -n "${_run_depends}" ]; then
shash_set pkgname-run_deps "${_pkgname}" "${_run_depends}"
fi
if [ -n "${_selected_options}" ]; then
shash_set pkgname-options "${_pkgname}" "${_selected_options}"
fi
}
deps_file() {
[ $# -ne 2 ] && eargs deps_file var_return pkg
local var_return="$1"
local pkg="$2"
local pkg_cache_dir
local _depfile
get_pkg_cache_dir pkg_cache_dir "${pkg}"
_depfile="${pkg_cache_dir}/deps"
if [ ! -f "${_depfile}" ]; then
if [ "${PKG_EXT}" = "tbz" ]; then
injail tar -qxf "/packages/All/${pkg##*/}" -O +CONTENTS | awk '$1 == "@pkgdep" { print $2 }' > "${_depfile}"
else
injail ${PKG_BIN} info -qdF "/packages/All/${pkg##*/}" > "${_depfile}"
fi
fi
setvar "${var_return}" "${_depfile}"
}
pkg_get_origin() {
[ $# -lt 2 ] && eargs pkg_get_origin var_return pkg [origin]
local var_return="$1"
local pkg="$2"
local _origin=$3
local pkg_cache_dir
local originfile
local new_origin
get_pkg_cache_dir pkg_cache_dir "${pkg}"
originfile="${pkg_cache_dir}/origin"
if [ ! -f "${originfile}" ]; then
if [ -z "${_origin}" ]; then
if [ "${PKG_EXT}" = "tbz" ]; then
_origin=$(injail tar -qxf "/packages/All/${pkg##*/}" -O +CONTENTS | \
awk -F: '$1 == "@comment ORIGIN" { print $2 }')
else
_origin=$(injail ${PKG_BIN} query -F \
"/packages/All/${pkg##*/}" "%o")
fi
fi
echo ${_origin} > "${originfile}"
else
read_line _origin "${originfile}"
fi
check_moved new_origin "${_origin}" && _origin=${new_origin}
setvar "${var_return}" "${_origin}"
[ -n "${_origin}" ]
}
pkg_get_dep_origin() {
[ $# -ne 2 ] && eargs pkg_get_dep_origin var_return pkg
local var_return="$1"
local pkg="$2"
local dep_origin_file
local pkg_cache_dir
local compiled_dep_origins
local origin new_origin _old_dep_origins
get_pkg_cache_dir pkg_cache_dir "${pkg}"
dep_origin_file="${pkg_cache_dir}/dep_origin"
if [ ! -f "${dep_origin_file}" ]; then
if [ "${PKG_EXT}" = "tbz" ]; then
compiled_dep_origins=$(injail tar -qxf "/packages/All/${pkg##*/}" -O +CONTENTS | \
awk -F: '$1 == "@comment DEPORIGIN" {print $2}' | tr '\n' ' ')
else
compiled_dep_origins=$(injail ${PKG_BIN} query -F \
"/packages/All/${pkg##*/}" '%do' | tr '\n' ' ')
fi
echo "${compiled_dep_origins}" > "${dep_origin_file}"
else
while read line; do
compiled_dep_origins="${compiled_dep_origins} ${line}"
done < "${dep_origin_file}"
fi
# Check MOVED
_old_dep_origins="${compiled_dep_origins}"
compiled_dep_origins=
for origin in ${_old_dep_origins}; do
if check_moved new_origin "${origin}"; then
compiled_dep_origins="${compiled_dep_origins} ${new_origin}"
else
compiled_dep_origins="${compiled_dep_origins} ${origin}"
fi
done
setvar "${var_return}" "${compiled_dep_origins}"
}
pkg_get_options() {
[ $# -ne 2 ] && eargs pkg_get_options var_return pkg
local var_return="$1"
local pkg="$2"
local optionsfile
local pkg_cache_dir
local _compiled_options
get_pkg_cache_dir pkg_cache_dir "${pkg}"
optionsfile="${pkg_cache_dir}/options"
if [ ! -f "${optionsfile}" ]; then
if [ "${PKG_EXT}" = "tbz" ]; then
_compiled_options=$(injail tar -qxf "/packages/All/${pkg##*/}" -O +CONTENTS | \
awk -F: '$1 == "@comment OPTIONS" {print $2}' | tr ' ' '\n' | \
sed -n 's/^\+\(.*\)/\1/p' | sort | tr '\n' ' ')
else
_compiled_options=
while read key value; do
case "${value}" in
off|false) continue ;;
esac
_compiled_options="${_compiled_options}${_compiled_options:+ }${key}"
done <<-EOF
$(injail ${PKG_BIN} query -F "/packages/All/${pkg##*/}" '%Ok %Ov' | sort)
EOF
# Compat with pretty-print-config
if [ -n "${_compiled_options}" ]; then
_compiled_options="${_compiled_options} "
fi
fi
echo "${_compiled_options}" > "${optionsfile}"
setvar "${var_return}" "${_compiled_options}"
return 0
fi
# Special care here to match whitespace of 'pretty-print-config'
while read line; do
_compiled_options="${_compiled_options}${_compiled_options:+ }${line}"
done < "${optionsfile}"
# Space on end to match 'pretty-print-config' in delete_old_pkg
[ -n "${_compiled_options}" ] &&
_compiled_options="${_compiled_options} "
setvar "${var_return}" "${_compiled_options}"
}
ensure_pkg_installed() {
local force="$1"
local mnt
_my_path mnt
[ -z "${force}" ] && [ -x "${mnt}${PKG_BIN}" ] && return 0
# Hack, speed up QEMU usage on pkg-repo.
if [ ${QEMU_EMULATING} -eq 1 ] && \
[ -f /usr/local/sbin/pkg-static ]; then
cp -f /usr/local/sbin/pkg-static "${mnt}/.p/pkg-static"
return 0
fi
[ -e ${MASTERMNT}/packages/Latest/pkg.txz ] || return 1 #pkg missing
injail tar xf /packages/Latest/pkg.txz -C / \
-s ",/.*/,.p/,g" "*/pkg-static"
return 0
}
pkg_cache_data() {
[ $# -ne 2 ] && eargs pkg_cache_data pkg origin
local pkg="$1"
local origin="$2"
ensure_pkg_installed || return 1
pkg_get_options _ignored "${pkg}" > /dev/null
pkg_get_origin _ignored "${pkg}" "${origin}" > /dev/null
pkg_get_dep_origin _ignored "${pkg}" > /dev/null
deps_file _ignored "${pkg}" > /dev/null
}
pkg_cacher_queue() {
[ $# -eq 2 ] || eargs pkg_cacher_queue origin pkgname
local origin="$1"
local pkgname="$2"
echo "${origin} ${pkgname}" > ${MASTERMNT}/.p/pkg_cacher.pipe
}
pkg_cacher_main() {
local pkg pkgname origin work
mkfifo ${MASTERMNT}/.p/pkg_cacher.pipe
exec 6<> ${MASTERMNT}/.p/pkg_cacher.pipe
trap exit TERM
trap pkg_cacher_cleanup EXIT
# Wait for packages to process.
while :; do
read -r work <&6
set -- ${work}
origin="$1"
pkgname="$2"
pkg="${PACKAGES}/All/${pkgname}.${PKG_EXT}"
if [ -f "${pkg}" ]; then
pkg_cache_data "${pkg}" "${origin}"
fi
done
}
pkg_cacher_cleanup() {
rm -f ${MASTERMNT}/.p/pkg_cacher.pipe
}
get_cache_dir() {
local var_return="$1"
setvar "${var_return}" ${POUDRIERE_DATA}/cache/${MASTERNAME}
}
# Return the cache dir for the given pkg
# @param var_return The variable to set the result in
# @param string pkg $PKGDIR/All/PKGNAME.PKG_EXT
get_pkg_cache_dir() {
[ $# -lt 2 ] && eargs get_pkg_cache_dir var_return pkg
local var_return="$1"
local pkg="$2"
local use_mtime="${3:-1}"
local pkg_file="${pkg##*/}"
local pkg_dir
local cache_dir
local pkg_mtime=
get_cache_dir cache_dir
[ ${use_mtime} -eq 1 ] && pkg_mtime=$(stat -f %m "${pkg}")
pkg_dir="${cache_dir}/${pkg_file}/${pkg_mtime}"
if [ ${use_mtime} -eq 1 ]; then
[ -d "${pkg_dir}" ] || mkdir -p "${pkg_dir}"
fi
setvar "${var_return}" "${pkg_dir}"
}
clear_pkg_cache() {
[ $# -ne 1 ] && eargs clear_pkg_cache pkg
local pkg="$1"
local pkg_cache_dir
get_pkg_cache_dir pkg_cache_dir "${pkg}" 0
rm -fr "${pkg_cache_dir}"
}
delete_pkg() {
[ $# -ne 1 ] && eargs delete_pkg pkg
local pkg="$1"
# Delete the package and the depsfile since this package is being deleted,
# which will force it to be recreated
rm -f "${pkg}"
clear_pkg_cache "${pkg}"
}
# Deleted cached information for stale packages (manually removed)
delete_stale_pkg_cache() {
local pkgname
local cache_dir
get_cache_dir cache_dir
msg_verbose "Checking for stale cache files"
[ ! -d ${cache_dir} ] && return 0
dirempty ${cache_dir} && return 0
for pkg in ${cache_dir}/*.${PKG_EXT}; do
pkg_file="${pkg##*/}"
# If this package no longer exists in the PKGDIR, delete the cache.
[ ! -e "${PACKAGES}/All/${pkg_file}" ] &&
clear_pkg_cache "${pkg}"
done
return 0
}
delete_old_pkg() {
[ $# -eq 1 ] || eargs delete_old_pkg pkgname
local pkg="$1"
local mnt pkgname new_pkgname
local origin v v2 compiled_options current_options current_deps compiled_deps
pkgname="${pkg##*/}"
pkgname="${pkgname%.*}"
pkg_is_needed "${pkgname}" || return 0
pkg_get_origin origin "${pkg}"
_my_path mnt
if [ ! -d "${mnt}${PORTSDIR}/${origin}" ]; then
msg "${origin} does not exist anymore. Deleting stale ${pkg##*/}"
delete_pkg "${pkg}"
return 0
fi
v="${pkgname##*-}"
if ! shash_get origin-pkgname "${origin}" new_pkgname; then
# This origin was not looked up in gather_port_vars. It is
# a stale package with the same PKGBASE as one we want, but
# with a different origin. Such as lang/perl5.20 vs
# lang/perl5.22 both with 'perl5' as PKGBASE. A pkgclean
# would handle removing this.
msg "Deleting ${pkg##*/}: stale package: unwanted origin ${origin}"
delete_pkg "${pkg}"
return 0
fi
v2=${new_pkgname##*-}
if [ "$v" != "$v2" ]; then
msg "Deleting ${pkg##*/}: new version: ${v2}"
delete_pkg "${pkg}"
return 0
fi
# Detect ports that have new dependencies that the existing packages
# do not have and delete them.
if [ "${CHECK_CHANGED_DEPS}" != "no" ]; then
current_deps=""
liblist=""
# FIXME: Move into Infrastructure/scripts and
# 'make actual-run-depends-list' after enough testing,
# which will avoida all of the injail hacks
for td in lib run; do
shash_get pkgname-${td}_deps "${new_pkgname}" raw_deps || raw_deps=
for d in ${raw_deps}; do
key=${d%:*}
dpath=${d#*:}
case "${dpath}" in
${PORTSDIR}/*) dpath=${dpath#${PORTSDIR}/} ;;
esac
case ${td} in
lib)
[ -n "${liblist}" ] || liblist=$(injail ldconfig -r | awk '$1 ~ /:-l/ { gsub(/.*-l/, "", $1); printf("%s ",$1) } END { printf("\n") }')
case ${key} in
lib*)
unset found
for dir in /lib /usr/lib ; do
if injail test -f "${dir}/${key}"; then
found=yes
break;
fi
done
[ -n "${found}" ] || current_deps="${current_deps} ${dpath}"
;;
*.*)
case " ${liblist} " in
*\ ${key}\ *) ;;
*) current_deps="${current_deps} ${dpath}" ;;
esac
;;
*)
unset found
for dir in /lib /usr/lib ; do
if injail test -f "${dir}/lib${key}.so"; then
found=yes
break;
fi
done
[ -n "${found}" ] || current_deps="${current_deps} ${dpath}"
;;
esac
;;
run)
case $key in
/*) [ -e ${mnt}/${key} ] || current_deps="${current_deps} ${dpath}" ;;
*) [ -n "$(injail which ${key})" ] || current_deps="${current_deps} ${dpath}" ;;
esac
;;
esac
done
done
pkg_get_dep_origin compiled_deps "${pkg}"
for d in ${current_deps}; do
case " $compiled_deps " in
*\ $d\ *) ;;
*)
msg "Deleting ${pkg##*/}: new dependency: ${d}"
delete_pkg "${pkg}"
return 0
;;
esac
done
fi
# Check if the compiled options match the current options from make.conf and /var/db/ports
if [ "${CHECK_CHANGED_OPTIONS}" != "no" ]; then
if [ ${PORTS_HAS_SELECTED_OPTIONS} -eq 1 ]; then
shash_get pkgname-options "${new_pkgname}" \
current_options || current_options=
# pretty-print-config has a trailing space, so
# pkg_get_options does as well. Add in for compat.
if [ -n "${current_options}" ]; then
current_options="${current_options} "
fi
else
# Backwards-compat
current_options=$(injail /usr/bin/make -C \
${PORTSDIR}/${origin} \
pretty-print-config | tr ' ' '\n' | \
sed -n 's/^\+\(.*\)/\1/p' | sort -u | tr '\n' ' ')
fi
pkg_get_options compiled_options "${pkg}"
if [ "${compiled_options}" != "${current_options}" ]; then
msg "Deleting ${pkg##*/}: changed options"
if [ "${CHECK_CHANGED_OPTIONS}" = "verbose" ]; then
msg "Pkg: ${compiled_options}"
msg "New: ${current_options}"
fi
delete_pkg "${pkg}"
return 0
fi
fi
# XXX: Check if the pkgname has changed and rename in the repo
if [ "${pkgname%-*}" != "${new_pkgname%-*}" ]; then
msg "Deleting ${pkg##*/}: package name changed to '${new_pkgname%-*}'"
delete_pkg "${pkg}"
return 0
fi
}
delete_old_pkgs() {
msg "Checking packages for incremental rebuild needed"
package_dir_exists_and_has_packages || return 0
parallel_start
for pkg in ${PACKAGES}/All/*.${PKG_EXT}; do
parallel_run delete_old_pkg "${pkg}"
done
parallel_stop
}
## Pick the next package from the "ready to build" queue in pool/
## Then move the package to the "building" dir in building/
## This is only ran from 1 process
next_in_queue() {
[ "${PWD}" = "${MASTERMNT}/.p/pool" ] || \
err 1 "next_in_queue requires PWD=${MASTERMNT}/.p/pool"
local var_return="$1"
local p _pkgname ret
# CWD is MASTERMNT/.p/pool
p=$(find ${POOL_BUCKET_DIRS} -type d -depth 1 -empty -print -quit || :)
if [ -n "$p" ]; then
_pkgname=${p##*/}
if ! rename "${p}" "../building/${_pkgname}" \
2>/dev/null; then
# Was the failure from /unbalanced?
if [ -z "${p%%*unbalanced/*}" ]; then
# We lost the race with a child running
# balance_queue(). The file is already
# gone and moved to a bucket. Try again.
ret=0
next_in_queue "${var_return}" || ret=$?
return ${ret}
else
# Failure to move a balanced item??
err 1 "next_in_queue: Failed to mv ${p} to ${MASTERMNT}/.p/building/${_pkgname}"
fi
fi
# Update timestamp for buildtime accounting
touch "../building/${_pkgname}"
fi
setvar "${var_return}" "${_pkgname}"
}
lock_acquire() {
[ $# -ge 1 ] || eargs lock_acquire lockname [waittime]
local lockname="$1"
local waittime="${2:-30}"
# Don't take locks inside siginfo_handler
[ ${in_siginfo_handler} -eq 1 ] && lock_have "${lockname}" && \
return 1
if ! locked_mkdir "${waittime}" \
"${POUDRIERE_TMPDIR}/lock-${MASTERNAME}-${lockname}"; then
msg_warn "Failed to acquire ${lockname} lock"
return 1
fi
hash_set have_lock "${lockname}" 1
# Delay TERM/INT while holding the lock
critical_start
}
lock_release() {
[ $# -ne 1 ] && eargs lock_release lockname
local lockname="$1"
hash_unset have_lock "${lockname}" || \
err 1 "Releasing unheld lock ${lockname}"
rmdir "${POUDRIERE_TMPDIR}/lock-${MASTERNAME}-${lockname}" 2>/dev/null
# Restore and deliver INT/TERM signals
critical_end
}
lock_have() {
[ $# -ne 1 ] && eargs lock_have lockname
local lockname="$1"
local _ignored
hash_get have_lock "${lockname}" _ignored
}
# Fetch vars from the Makefile and set them locally.
# port_var_fetch ports-mgmt/pkg PKGNAME pkgname PKGBASE pkgbase ...
# Assignments are supported as well, without a subsequent variable for storage.
port_var_fetch() {
local -; set +x
[ $# -ge 3 ] || eargs port_var_fetch origin PORTVAR var_set ...
local origin="$1"
local _makeflags _vars
local _portvar _var _line _errexit shiftcnt varcnt
# Use a tab rather than space to allow FOO='BLAH BLAH' assignments
# and lookups like -V'${PKG_DEPENDS} ${BUILD_DEPENDS}'
local IFS sep=$'\t'
# Use invalid shell var character '!' to ensure we
# don't setvar it later.
local assign_var="!"
shift
while [ $# -gt 0 ]; do
_portvar="$1"
_var="$2"
if [ -z "${_portvar%%*=*}" ]; then
# This is an assignment, no associated variable
# for storage.
_makeflags="${_makeflags}${_makeflags:+${sep}}${_portvar}"
_vars="${_vars}${_vars:+ }${assign_var}"
shift 1
else
[ $# -eq 1 ] && break
_makeflags="${_makeflags}${_makeflags:+${sep}}-V${_portvar}"
_vars="${_vars}${_vars:+ }${_var}"
shift 2
fi
done
[ $# -eq 0 ] || eargs port_var_fetch origin PORTVAR var_set ...
_errexit="!errexit!"
ret=0
set -- ${_vars}
varcnt=$#
shiftcnt=0
while read -r _line; do
if [ "${_line% *}" = "${_errexit}" ]; then
ret=${_line#* }
# Encountered an error, abort parsing anything further.
# Cleanup already-set vars of 'make: stopped in'
# stuff in case the caller is ignoring our non-0
# return status. The shiftcnt handler can deal with
# this all itself.
shiftcnt=0
break
fi
# This var was just an assignment, no actual value to read from
# stdout. Shift until we find an actual -V var.
while [ "${1}" = "${assign_var}" ]; do
shift
shiftcnt=$((shiftcnt + 1))
done
# We may have more lines than expected on an error, but our
# errexit output is last, so keep reading until then.
if [ $# -gt 0 ]; then
setvar "$1" "${_line}" || return $?
shift
shiftcnt=$((shiftcnt + 1))
fi
done <<-EOF
$(IFS="${sep}"; injail /usr/bin/make -C "${PORTSDIR}/${origin}" ${_makeflags} || echo "${_errexit} $?")
EOF
# If the entire output was blank, then $() ate all of the excess
# newlines, which resulted in some vars not getting setvar'd.
# This could also be cleaning up after the errexit case.
if [ ${shiftcnt} -ne ${varcnt} ]; then
set -- ${_vars}
# Be sure to start at the last setvar'd value.
if [ ${shiftcnt} -gt 0 ]; then
shift ${shiftcnt}
fi
while [ $# -gt 0 ]; do
# Skip assignment vars
while [ "${1}" = "${assign_var}" ]; do
shift
done
if [ $# -gt 0 ]; then
setvar "$1" "" || return $?
shift
fi
done
fi
return ${ret}
}
cache_get_origin() {
[ $# -ne 2 ] && eargs cache_get_origin var_return pkgname
local var_return="$1"
local pkgname="$2"
local _origin
shash_get pkgname-origin "${pkgname}" _origin
setvar "${var_return}" "${_origin}"
}
set_dep_fatal_error() {
[ -n "${DEP_FATAL_ERROR}" ] && return 0
DEP_FATAL_ERROR=1
# Mark the fatal error flag. Must do it like this as this may be
# running in a sub-shell.
: > dep_fatal_error
}
clear_dep_fatal_error() {
unset DEP_FATAL_ERROR
rm -f dep_fatal_error 2>/dev/null || :
}
check_dep_fatal_error() {
[ -n "${DEP_FATAL_ERROR}" ] || [ -f dep_fatal_error ]
}
gather_port_vars() {
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "gather_port_vars requires PWD=${MASTERMNT}/.p"
local origin qorigin log rdep
# A. Lookup all port vars/deps from the given list of ports.
# B. For every dependency found (depqueue):
# 1. Add it into the depqueue, which will then process
# each dependency into the gatherqueue if it was not
# already gathered by the previous iteration.
# C. Lookup all port vars/deps from the gatherqueue
# D. If the depqueue is empty, done, otherwise go to B.
#
# This 2-queue solution is to avoid excessive races that cause
# make -V to be ran multiple times per port. We only want to
# process each port once without explicit locking.
# For the -a case the depqueue is not needed since all ports will be
# visited once in the first pass and make it into the gatherqueue.
msg "Gathering ports metadata"
bset status "gatheringportvars:"
_log_path log
:> "all_pkgs"
[ ${ALL} -eq 0 ] && :> "all_pkgbases"
rm -rf gqueue dqueue 2>/dev/null || :
mkdir gqueue dqueue
clear_dep_fatal_error
parallel_start
for origin in $(listed_ports show_moved); do
if [ -d "../${PORTSDIR}/${origin}" ]; then
echo "${origin} listed" >> \
"${log}/.poudriere.ports.queued"
parallel_run \
prefix_stderr_quick \
"(${COLOR_PORT}${origin}${COLOR_RESET})${COLOR_WARN}" \
gather_port_vars_port "${origin}" || \
set_dep_fatal_error
else
if [ ${ALL} -eq 1 ]; then
msg_warn "Nonexistent origin listed in category Makefiles: ${COLOR_PORT}${origin}"
else
msg_error "Nonexistent origin listed for build: ${COLOR_PORT}${origin}"
set_dep_fatal_error
fi
fi
done
if ! parallel_stop || check_dep_fatal_error; then
err 1 "Fatal errors encountered gathering initial ports metadata"
fi
until dirempty dqueue && dirempty gqueue; do
# Process all newly found deps into the gatherqueue
clear_dep_fatal_error
msg_debug "Processing depqueue"
parallel_start
for qorigin in dqueue/*; do
case "${qorigin}" in
"dqueue/*") break ;;
esac
parallel_run \
gather_port_vars_process_depqueue "${qorigin}" || \
set_dep_fatal_error
done
if ! parallel_stop || check_dep_fatal_error; then
err 1 "Fatal errors encountered processing gathered ports metadata"
fi
# Now process the gatherqueue
# Now rerun until the work queue is empty
# XXX: If the initial run were to use an efficient work queue then
# this could be avoided.
clear_dep_fatal_error
parallel_start
msg_debug "Processing gatherqueue"
for qorigin in gqueue/*; do
case "${qorigin}" in
"gqueue/*") break ;;
esac
origin="${qorigin#*/}"
origin="${origin%!*}/${origin#*!}"
read_line rdep "${qorigin}/rdep"
echo "${origin} ${rdep}" >> \
"${log}/.poudriere.ports.queued"
parallel_run \
prefix_stderr_quick \
"(${COLOR_PORT}${origin}${COLOR_RESET})${COLOR_WARN}" \
gather_port_vars_port \
"${origin}" inqueue || set_dep_fatal_error
done
if ! parallel_stop || check_dep_fatal_error; then
err 1 "Fatal errors encountered gathering ports metadata"
fi
done
if ! rmdir gqueue || ! rmdir dqueue; then
ls gqueue dqueue 2>/dev/null || :
err 1 "Gather port queues not empty"
fi
}
gather_port_vars_port() {
[ "${SHASH_VAR_PATH}" = "var/cache" ] || \
err 1 "gather_port_vars_port requires SHASH_VAR_PATH=var/cache"
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "gather_port_vars_port requires PWD=${MASTERMNT}/.p"
[ $# -lt 1 ] && eargs gather_port_vars_port origin [inqueue]
[ $# -gt 2 ] && eargs gather_port_vars_port origin [inqueue]
local origin="$1"
local inqueue="$2"
local dep_origin deps pkgname
msg_debug "gather_port_vars_port (${origin}): LOOKUP"
# Remove queue entry
[ -n "${inqueue}" ] && rm -rf "gqueue/${origin%/*}!${origin#*/}"
shash_get origin-pkgname "${origin}" pkgname && \
err 1 "gather_port_vars_port: Already had ${origin}"
if ! deps_fetch_vars "${origin}" deps pkgname; then
# An error is printed from deps_fetch_vars
set_dep_fatal_error
return 1
fi
echo "${pkgname}" >> "all_pkgs"
[ ${ALL} -eq 0 ] && echo "${pkgname%-*}" >> "all_pkgbases"
# If there are no deps for this port then there's nothing left to do.
[ -z "${deps}" ] && return 0
# Assert some policy before proceeding to process these deps
# further.
for dep_origin in ${deps}; do
msg_verbose "${COLOR_PORT}${origin}${COLOR_DEBUG} depends on ${COLOR_PORT}${dep_origin}"
if [ "${origin}" = "${dep_origin}" ]; then
msg_error "${COLOR_PORT}${origin}${COLOR_RESET} incorrectly depends on itself. Please contact maintainer of the port to fix this."
set_dep_fatal_error
return 1
fi
# Detect bad cat/origin/ dependency which pkg will not register properly
if ! [ "${dep_origin}" = "${dep_origin%/}" ]; then
msg_error "${COLOR_PORT}${origin}${COLOR_RESET} depends on bad origin '${COLOR_PORT}${dep_origin}${COLOR_RESET}'; Please contact maintainer of the port to fix this."
set_dep_fatal_error
return 1
fi
if ! [ -d "../${PORTSDIR}/${dep_origin}" ]; then
msg_error "${COLOR_PORT}${origin}${COLOR_RESET} depends on nonexistent origin '${COLOR_PORT}${dep_origin}${COLOR_RESET}'; Please contact maintainer of the port to fix this."
set_dep_fatal_error
return 1
fi
done
# In the -a case, there's no need to gather the vars for these deps
# since we are going to visit all ports from the category Makefiles
# anyway.
if [ ${ALL} -eq 0 ]; then
msg_debug "gather_port_vars_port (${origin}): Adding to depqueue"
mkdir "dqueue/${origin%/*}!${origin#*/}" || \
err 1 "gather_port_vars_port: Failed to add ${origin} to depqueue"
fi
}
gather_port_vars_process_depqueue() {
[ "${SHASH_VAR_PATH}" = "var/cache" ] || \
err 1 "gather_port_vars_process_depqueue requires SHASH_VAR_PATH=var/cache"
[ $# -ne 1 ] && eargs gather_port_vars_process_depqueue qorigin
local qorigin="$1"
local origin pkgname deps dep_origin dep_pkgname
origin="${qorigin#*/}"
origin="${origin%!*}/${origin#*!}"
msg_debug "gather_port_vars_process_depqueue (${origin})"
# Remove queue entry
rmdir "${qorigin}"
# Add all of this origin's deps into the gatherqueue to reprocess
shash_get origin-pkgname "${origin}" pkgname || \
err 1 "gather_port_vars_process_depqueue failed to find pkgname for origin ${origin}"
shash_get pkgname-deps "${pkgname}" deps || \
err 1 "gather_port_vars_process_depqueue failed to find deps for pkg ${pkgname}"
for dep_origin in ${deps}; do
# Add this origin into the gatherqueue if not already done.
if ! shash_get origin-pkgname "${dep_origin}" dep_pkgname; then
msg_debug "gather_port_vars_process_depqueue (${origin}): Adding ${dep_origin} into the gatherqueue"
# Another worker may have created it
if mkdir "gqueue/${dep_origin%/*}!${dep_origin#*/}" \
2>/dev/null; then
echo "${origin}" > \
"gqueue/${dep_origin%/*}!${dep_origin#*/}/rdep"
fi
fi
done
}
compute_deps() {
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "compute_deps requires PWD=${MASTERMNT}/.p"
local pkgname dep_pkgname
msg "Calculating ports order and dependencies"
bset status "computingdeps:"
:> "pkg_deps.unsorted"
clear_dep_fatal_error
parallel_start
while read pkgname; do
parallel_run compute_deps_pkg "${pkgname}" || \
set_dep_fatal_error
done < "all_pkgs"
if ! parallel_stop || check_dep_fatal_error; then
err 1 "Fatal errors encountered calculating dependencies"
fi
sort -u "pkg_deps.unsorted" > "pkg_deps"
bset status "computingrdeps:"
# cd into rdeps to allow xargs mkdir to have more args.
(
cd "rdeps"
awk '{print $2}' "../pkg_deps" | sort -u | xargs mkdir
awk '{print $2 "/" $1}' "../pkg_deps" | xargs touch
)
rm -f "pkg_deps.unsorted"
return 0
}
compute_deps_pkg() {
[ "${SHASH_VAR_PATH}" = "var/cache" ] || \
err 1 "compute_deps_pkg requires SHASH_VAR_PATH=var/cache"
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "compute_deps_pkgname requires PWD=${MASTERMNT}/.p"
[ $# -lt 1 ] && eargs compute_deps_pkg pkgname
local pkgname="$1"
local pkg_pooldir deps dep_origin dep_pkgname
shash_get pkgname-deps "${pkgname}" deps || \
err 1 "compute_deps_pkg failed to find deps for ${pkgname}"
pkg_pooldir="deps/${pkgname}"
mkdir "${pkg_pooldir}" || \
err 1 "compute_deps_pkg: Error creating pool dir for ${pkgname}: There may be a duplicate origin in a category Makefile"
for dep_origin in ${deps}; do
shash_get origin-pkgname "${dep_origin}" dep_pkgname || \
err 1 "compute_deps_pkg failed to lookup pkgname for ${dep_origin} processing package ${pkgname}"
:> "${pkg_pooldir}/${dep_pkgname}"
echo "${pkgname} ${dep_pkgname}" >> "pkg_deps.unsorted"
done
return 0
}
listed_ports() {
local tell_moved="${1}"
local portsdir origin file
if [ ${ALL} -eq 1 ]; then
_pget portsdir ${PTNAME} mnt
[ -d "${portsdir}/ports" ] && portsdir="${portsdir}/ports"
for cat in $(awk -F= '$1 ~ /^[[:space:]]*SUBDIR[[:space:]]*\+/ {gsub(/[[:space:]]/, "", $2); print $2}' ${portsdir}/Makefile); do
awk -F= -v cat=${cat} '$1 ~ /^[[:space:]]*SUBDIR[[:space:]]*\+/ {gsub(/[[:space:]]/, "", $2); print cat"/"$2}' ${portsdir}/${cat}/Makefile
done
return 0
fi
{
# -f specified
if [ -z "${LISTPORTS}" ]; then
for file in ${LISTPKGS}; do
while read origin; do
# Skip blank lines and comments
[ -z "${origin%%#*}" ] && continue
# Remove trailing slash for historical reasons.
echo "${origin%/}"
done < "${file}"
done
else
# Ports specified on cmdline
for origin in ${LISTPORTS}; do
# Remove trailing slash for historical reasons.
echo "${origin%/}"
done
fi
} | sort -u | while read origin; do
if check_moved new_origin ${origin}; then
[ -n "${tell_moved}" ] && msg \
"MOVED: ${COLOR_PORT}${origin}${COLOR_RESET} renamed to ${COLOR_PORT}${new_origin}${COLOR_RESET}" >&2
origin="${new_origin}"
fi
echo "${origin}"
done
}
# Port was requested to be built
port_is_listed() {
[ $# -eq 1 ] || eargs port_is_listed origin
local origin="$1"
if [ ${ALL} -eq 1 -o ${PORTTESTING_RECURSIVE} -eq 1 ]; then
return 0
fi
listed_ports | grep -q "^${origin}\$" && return 0
return 1
}
# Port was requested to be built, or is needed by a port requested to be built
pkg_is_needed() {
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "pkg_is_needed requires PWD=${MASTERMNT}/.p"
[ $# -eq 1 ] || eargs pkg_is_needed pkgname
local pkgname="$1"
local pkgbase
[ ${ALL} -eq 1 ] && return 0
# We check on PKGBASE rather than PKGNAME from pkg_deps
# since the caller may be passing in a different version
# compared to what is in the queue to build for.
pkgbase="${pkgname%-*}"
awk -vpkgbase="${pkgbase}" '
$1 == pkgbase {
found=1
exit 0
}
END {
if (found != 1)
exit 1
}' "all_pkgbases"
}
get_porttesting() {
[ $# -eq 1 ] || eargs get_porttesting pkgname
local pkgname="$1"
local porttesting
local origin
if [ -n "${PORTTESTING}" ]; then
cache_get_origin origin "${pkgname}"
if port_is_listed "${origin}"; then
porttesting=1
fi
fi
echo $porttesting
}
find_all_deps() {
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "find_all_deps requires PWD=${MASTERMNT}/.p"
[ $# -ne 1 ] && eargs find_all_deps pkgname
local pkgname="$1"
local dep_pkgname
FIND_ALL_DEPS="${FIND_ALL_DEPS} ${pkgname}"
#msg_debug "find_all_deps ${pkgname}"
# Show deps/*/${pkgname}
for pn in deps/${pkgname}/*; do
dep_pkgname=${pn##*/}
case " ${FIND_ALL_DEPS} " in
*\ ${dep_pkgname}\ *) continue ;;
esac
case "${pn}" in
"deps/${pkgname}/*") break ;;
esac
echo "deps/${dep_pkgname}"
find_all_deps "${dep_pkgname}"
done
echo "deps/${pkgname}"
}
find_all_pool_references() {
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "find_all_pool_references requires PWD=${MASTERMNT}/.p"
[ $# -ne 1 ] && eargs find_all_pool_references pkgname
local pkgname="$1"
local rpn dep_pkgname
# Cleanup rdeps/*/${pkgname}
for rpn in deps/${pkgname}/*; do
case "${rpn}" in
"deps/${pkgname}/*")
break ;;
esac
dep_pkgname=${rpn##*/}
echo "rdeps/${dep_pkgname}/${pkgname}"
done
echo "deps/${pkgname}"
# Cleanup deps/*/${pkgname}
for rpn in rdeps/${pkgname}/*; do
case "${rpn}" in
"rdeps/${pkgname}/*")
break ;;
esac
dep_pkgname=${rpn##*/}
echo "deps/${dep_pkgname}/${pkgname}"
done
echo "rdeps/${pkgname}"
}
delete_stale_symlinks_and_empty_dirs() {
msg_n "Deleting stale symlinks..."
find -L ${PACKAGES} -type l \
-exec rm -f {} +
echo " done"
msg_n "Deleting empty directories..."
find ${PACKAGES} -type d -mindepth 1 \
-empty -delete
echo " done"
}
load_moved() {
[ "${SHASH_VAR_PATH}" = "var/cache" ] || \
err 1 "load_moved requires SHASH_VAR_PATH=var/cache"
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "load_moved requires PWD=${MASTERMNT}/.p"
[ -f ${MASTERMNT}${PORTSDIR}/MOVED ] || return 0
msg "Loading MOVED"
bset status "loading_moved:"
grep -v '^#' ${MASTERMNT}${PORTSDIR}/MOVED | awk \
-F\| '
$2 != "" {
print $1,$2;
}' | while read old_origin new_origin; do
shash_set origin-moved "${old_origin}" "${new_origin}"
done
}
check_moved() {
[ $# -lt 2 ] && eargs check_moved var_return origin
local var_return="$1"
local origin="$2"
shash_get origin-moved "${origin}" "${var_return}"
}
clean_build_queue() {
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "clean_build_queue requires PWD=${MASTERMNT}/.p"
local tmp pn port
bset status "cleaning:"
msg "Cleaning the build queue"
# Delete from the queue all that already have a current package.
for pn in $(ls deps/); do
[ -f "../packages/All/${pn}.${PKG_EXT}" ] && \
find_all_pool_references "${pn}"
done | xargs rm -rf
# Delete from the queue orphaned build deps. This can happen if
# the specified-to-build ports have all their deps satisifed
# but one of their run deps has missing build deps packages which
# causes the build deps to be in the queue at this point.
if [ ${TRIM_ORPHANED_BUILD_DEPS} = "yes" -a ${ALL} -eq 0 ]; then
tmp=$(mktemp -t queue)
{
listed_ports | while read port; do
shash_get origin-pkgname "${port}" pkgname || \
err 1 "Failed to lookup PKGNAME for ${port}"
echo "${pkgname}"
done
# Pkg is a special case. It may not have been requested,
# but it should always be rebuilt if missing. The
# origin-pkgname lookup may fail if it wasn't
# in the build queue.
for port in ports-mgmt/pkg ports-mgmt/pkg-devel; do
shash_get origin-pkgname "${port}" pkgname && \
echo "${pkgname}"
done
} | {
FIND_ALL_DEPS=
while read pkgname; do
find_all_deps "${pkgname}"
done | sort -u > ${tmp}
}
find deps -type d -mindepth 1 -maxdepth 1 | \
sort > ${tmp}.actual
comm -13 ${tmp} ${tmp}.actual | while read pd; do
find_all_pool_references "${pd##*/}"
done | xargs rm -rf
rm -f ${tmp} ${tmp}.actual
fi
}
# PWD will be MASTERMNT/.p after this
prepare_ports() {
local pkg
local log log_top
local n pn nbq resuming_build
local cache_dir sflag
_log_path log
mkdir -p "${MASTERMNT}/.p/building" \
"${MASTERMNT}/.p/pool" \
"${MASTERMNT}/.p/pool/unbalanced" \
"${MASTERMNT}/.p/deps" \
"${MASTERMNT}/.p/rdeps" \
"${MASTERMNT}/.p/cleaning/deps" \
"${MASTERMNT}/.p/cleaning/rdeps" \
"${MASTERMNT}/.p/var/run" \
"${MASTERMNT}/.p/var/cache"
cd "${MASTERMNT}/.p"
SHASH_VAR_PATH="var/cache"
# No prefix needed since we're unique in MASTERMNT.
SHASH_VAR_PREFIX=
# Allow caching values now
USE_CACHE_CALL=1
if [ -e "${log}/.poudriere.ports.built" ]; then
resuming_build=1
else
resuming_build=0
fi
if was_a_bulk_run; then
_log_path_top log_top
get_cache_dir cache_dir
if [ ${resuming_build} -eq 0 ] || ! [ -d "${log}" ]; then
# Sync in HTML files through a base dir
install_html_files "${HTMLPREFIX}" "${log_top}/.html" \
"${log}"
# Create log dirs
mkdir -p ${log}/../../latest-per-pkg \
${log}/../latest-per-pkg \
${log}/logs \
${log}/logs/errors \
${cache_dir}
# Link this build as the /latest
ln -sfh ${BUILDNAME} ${log%/*}/latest
# Record the SVN URL@REV in the build
[ -d ${MASTERMNT}${PORTSDIR}/.svn ] && bset svn_url $(
${SVN_CMD} info ${MASTERMNT}${PORTSDIR} | awk '
/^URL: / {URL=substr($0, 6)}
/Revision: / {REVISION=substr($0, 11)}
END { print URL "@" REVISION }
')
bset mastername "${MASTERNAME}"
bset jailname "${JAILNAME}"
bset setname "${SETNAME}"
bset ptname "${PTNAME}"
bset buildname "${BUILDNAME}"
bset started "${EPOCH_START}"
fi
show_log_info
# Must acquire "update_stats" on shutdown to ensure
# the process is not killed while holding it.
if [ ${HTML_JSON_UPDATE_INTERVAL} -ne 0 ]; then
coprocess_start html_json
else
msg "HTML UI updates are disabled by HTML_JSON_UPDATE_INTERVAL being 0"
fi
fi
load_moved
gather_port_vars
compute_deps
bset status "sanity:"
if [ -f ${PACKAGES}/.jailversion ]; then
if [ "$(cat ${PACKAGES}/.jailversion)" != \
"$(jget ${JAILNAME} version)" ]; then
JAIL_NEEDS_CLEAN=1
fi
fi
if was_a_bulk_run; then
# Stash dependency graph
cp -f "${MASTERMNT}/.p/pkg_deps" "${log}/.poudriere.deps%"
if [ ${JAIL_NEEDS_CLEAN} -eq 1 ]; then
msg_n "Cleaning all packages due to newer version of the jail..."
elif [ ${CLEAN} -eq 1 ]; then
msg_n "(-c) Cleaning all packages..."
fi
if [ ${JAIL_NEEDS_CLEAN} -eq 1 ] || [ ${CLEAN} -eq 1 ]; then
rm -rf ${PACKAGES}/* ${cache_dir}
echo " done"
fi
if [ ${CLEAN_LISTED} -eq 1 ]; then
msg "(-C) Cleaning specified ports to build"
listed_ports | while read port; do
shash_get origin-pkgname "${port}" pkgname || \
err 1 "Failed to lookup PKGNAME for ${port}"
pkg="${PACKAGES}/All/${pkgname}.${PKG_EXT}"
if [ -f "${pkg}" ]; then
msg "(-C) Deleting existing package: ${pkg##*/}"
delete_pkg "${pkg}"
fi
done
fi
# If the build is being resumed then packages already
# built/failed/skipped/ignored should not be rebuilt.
if [ ${resuming_build} -eq 1 ]; then
awk '{print $2}' \
${log}/.poudriere.ports.built \
${log}/.poudriere.ports.failed \
${log}/.poudriere.ports.ignored \
${log}/.poudriere.ports.skipped | \
while read pn; do
find_all_pool_references "${pn}"
done | xargs rm -rf
else
# New build
bset stats_queued 0
bset stats_built 0
bset stats_failed 0
bset stats_ignored 0
bset stats_skipped 0
:> ${log}/.data.json
:> ${log}/.data.mini.json
:> ${log}/.poudriere.ports.built
:> ${log}/.poudriere.ports.failed
:> ${log}/.poudriere.ports.ignored
:> ${log}/.poudriere.ports.skipped
fi
fi
if ! ensure_pkg_installed && [ ${SKIPSANITY} -eq 0 ]; then
msg "pkg package missing, skipping sanity"
SKIPSANITY=2
fi
if [ $SKIPSANITY -eq 0 ]; then
msg "Sanity checking the repository"
for n in repo.txz digests.txz packagesite.txz; do
pkg="${PACKAGES}/All/${n}"
if [ -f "${pkg}" ]; then
msg "Removing invalid pkg repo file: ${pkg}"
rm -f "${pkg}"
fi
done
delete_stale_pkg_cache
# Skip incremental build for pkgclean
if was_a_bulk_run; then
delete_old_pkgs
if [ ${SKIP_RECURSIVE_REBUILD} -eq 0 ]; then
msg_verbose "Checking packages for missing dependencies"
while :; do
sanity_check_pkgs && break
done
else
msg "(-S) Skipping recursive rebuild"
fi
delete_stale_symlinks_and_empty_dirs
fi
else
[ ${SKIPSANITY} -eq 1 ] && sflag="(-s) "
msg "${sflag}Skipping incremental rebuild and repository sanity checks"
fi
export LOCALBASE=${LOCALBASE:-/usr/local}
clean_build_queue
# Call the deadlock code as non-fatal which will check for cycles
sanity_check_queue 0
if was_a_bulk_run && [ $resuming_build -eq 0 ]; then
nbq=0
nbq=$(find deps -type d -depth 1 | wc -l)
# Add 1 for the main port to test
[ "${SCRIPTPATH##*/}" = "testport.sh" ] && nbq=$((${nbq} + 1))
bset stats_queued ${nbq##* }
fi
if was_a_bulk_run; then
# Create a pool of ready-to-build from the deps pool
find deps -type d -empty -depth 1 | \
xargs -J % mv % pool/unbalanced
load_priorities
balance_pool
[ -n "${ALLOW_MAKE_JOBS}" ] || \
echo "DISABLE_MAKE_JOBS=poudriere" \
>> ${MASTERMNT}/etc/make.conf
# Don't leak ports-env UID as it conflicts with BUILD_AS_NON_ROOT
if [ "${BUILD_AS_NON_ROOT}" = "yes" ]; then
sed -i '' '/^UID=0$/d' "${MASTERMNT}/etc/make.conf"
sed -i '' '/^GID=0$/d' "${MASTERMNT}/etc/make.conf"
# Will handle manually for now on until build_port.
export UID=0
export GID=0
fi
jget ${JAILNAME} version > ${PACKAGES}/.jailversion
fi
return 0
}
load_priorities_tsortD() {
local priority pkgname pkg_boost boosted origin
local - # Keep set -f local
tsort -D "pkg_deps" > "pkg_deps.depth"
# Create buckets to satisfy the dependency chains, in reverse
# order. Not counting here as there may be boosted priorities
# at 99 or other high values.
POOL_BUCKET_DIRS=$(awk '{print $1}' "pkg_deps.depth"|sort -run)
set -f # for PRIORITY_BOOST
boosted=0
while read priority pkgname; do
# Does this pkg have an override?
for pkg_boost in ${PRIORITY_BOOST}; do
case ${pkgname%-*} in
${pkg_boost})
[ -d "deps/${pkgname}" ] \
|| continue
cache_get_origin origin "${pkgname}"
msg "Boosting priority: ${COLOR_PORT}${origin}"
priority=${PRIORITY_BOOST_VALUE}
boosted=1
break
;;
esac
done
hash_set "priority" "${pkgname}" ${priority}
done < "pkg_deps.depth"
# Add ${PRIORITY_BOOST_VALUE} into the pool if needed.
[ ${boosted} -eq 1 ] && POOL_BUCKET_DIRS="${PRIORITY_BOOST_VALUE} ${POOL_BUCKET_DIRS}"
return 0
}
load_priorities_ptsort() {
local priority pkgname pkg_boost origin
local - # Keep set -f local
set -f # for PRIORITY_BOOST
awk '{print $2 " " $1}' "pkg_deps" > "pkg_deps.ptsort"
# Add in boosts before running ptsort
while read pkgname; do
# Does this pkg have an override?
for pkg_boost in ${PRIORITY_BOOST}; do
case ${pkgname%-*} in
${pkg_boost})
[ -d "deps/${pkgname}" ] \
|| continue
cache_get_origin origin "${pkgname}"
msg "Boosting priority: ${COLOR_PORT}${origin}"
echo "${pkgname} ${PRIORITY_BOOST_VALUE}" >> \
"pkg_deps.ptsort"
break
;;
esac
done
done < "all_pkgs"
ptsort -p "pkg_deps.ptsort" > \
"pkg_deps.priority"
# Create buckets to satisfy the dependency chain priorities.
POOL_BUCKET_DIRS=$(awk '{print $1}' \
"pkg_deps.priority"|sort -run)
# Read all priorities into the "priority" hash
while read priority pkgname; do
hash_set "priority" "${pkgname}" ${priority}
done < "pkg_deps.priority"
return 0
}
load_priorities() {
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "load_priorities requires PWD=${MASTERMNT}/.p"
POOL_BUCKET_DIRS=""
if [ ${POOL_BUCKETS} -gt 0 ]; then
if [ "${USE_PTSORT}" = "yes" ]; then
load_priorities_ptsort
else
load_priorities_tsortD
fi
fi
# If there are no buckets then everything to build will fall
# into 0 as they depend on nothing and nothing depends on them.
# I.e., pkg-devel in -ac or testport on something with no deps
# needed.
[ -z "${POOL_BUCKET_DIRS}" ] && POOL_BUCKET_DIRS="0"
# Create buckets after loading priorities in case of boosts.
( cd pool && mkdir ${POOL_BUCKET_DIRS} )
# unbalanced is where everything starts at. Items are moved in
# balance_pool based on their priority in the "priority" hash.
POOL_BUCKET_DIRS="${POOL_BUCKET_DIRS} unbalanced"
return 0
}
balance_pool() {
# Don't bother if disabled
[ ${POOL_BUCKETS} -gt 0 ] || return 0
[ "${PWD}" = "${MASTERMNT}/.p" ] || \
err 1 "balance_pool requires PWD=${MASTERMNT}/.p"
local pkgname pkg_dir dep_count lock
# Avoid running this in parallel, no need. Note that this lock is
# not on the unbalanced/ dir, but only this function. clean.sh writes
# to unbalanced/, queue_empty() reads from it, and next_in_queue()
# moves from it.
lock=.lock-balance_pool
mkdir ${lock} 2>/dev/null || return 0
if dirempty pool/unbalanced; then
rmdir ${lock}
return 0
fi
if [ -n "${MY_JOBID}" ]; then
bset ${MY_JOBID} status "balancing_pool:"
else
bset status "balancing_pool:"
fi
# For everything ready-to-build...
for pkg_dir in pool/unbalanced/*; do
# May be empty due to racing with next_in_queue()
case "${pkg_dir}" in
"pool/unbalanced/*") break ;;
esac
pkgname=${pkg_dir##*/}
hash_get "priority" "${pkgname}" dep_count || dep_count=0
# This races with next_in_queue(), just ignore failure
# to move it.
rename "${pkg_dir}" \
"pool/${dep_count}/${pkgname}" \
2>/dev/null || :
done
# New files may have been added in unbalanced/ via clean.sh due to not
# being locked. These will be picked up in the next run.
rmdir ${lock}
}
append_make() {
[ $# -ne 2 ] && eargs append_make src_makeconf dst_makeconf
local src_makeconf=$1
local dst_makeconf=$2
if [ "${src_makeconf}" = "-" ]; then
src_makeconf="${POUDRIERED}/make.conf"
else
src_makeconf="${POUDRIERED}/${src_makeconf}-make.conf"
fi
[ -f "${src_makeconf}" ] || return 0
src_makeconf="$(realpath ${src_makeconf} 2>/dev/null)"
# Only append if not already done (-z -p or -j match)
grep -q "# ${src_makeconf} #" ${dst_makeconf} && return 0
msg "Appending to make.conf: ${src_makeconf}"
echo "#### ${src_makeconf} ####" >> ${dst_makeconf}
cat "${src_makeconf}" >> ${dst_makeconf}
}
read_packages_from_params()
{
if [ $# -eq 0 -o -z "$1" ]; then
[ -n "${LISTPKGS}" -o ${ALL} -eq 1 ] ||
err 1 "No packages specified"
if [ ${ALL} -eq 0 ]; then
for listpkg_name in ${LISTPKGS}; do
[ -f "${listpkg_name}" ] ||
err 1 "No such list of packages: ${listpkg_name}"
done
fi
else
[ ${ALL} -eq 0 ] ||
err 1 "command line arguments and -a cannot be used at the same time"
[ -z "${LISTPKGS}" ] ||
err 1 "command line arguments and list of ports cannot be used at the same time"
LISTPORTS="$@"
fi
}
clean_restricted() {
msg "Cleaning restricted packages"
bset status "clean_restricted:"
# Remount rw
# mount_nullfs does not support mount -u
umount ${UMOUNT_NONBUSY} ${MASTERMNT}/packages
mount_packages
injail /usr/bin/make -s -C ${PORTSDIR} -j ${PARALLEL_JOBS} \
RM="/bin/rm -fv" ECHO_MSG="true" clean-restricted
# Remount ro
umount ${UMOUNT_NONBUSY} ${MASTERMNT}/packages
mount_packages -o ro
}
sign_pkg() {
[ $# -eq 2 ] || eargs sign_pkg sigtype pkgfile
local sigtype="$1"
local pkgfile="$2"
if [ "${sigtype}" = "fingerprint" ]; then
rm -f "${pkgfile}.sig"
sha256 -q "${pkgfile}" | ${SIGNING_COMMAND} > "${pkgfile}.sig"
elif [ "${sigtype}" = "pubkey" ]; then
rm -f "${pkgfile}.pubkeysig"
echo -n $(sha256 -q "${pkgfile}") | \
openssl dgst -sha256 -sign "${PKG_REPO_SIGNING_KEY}" \
-binary -out "${pkgfile}.pubkeysig"
fi
}
build_repo() {
local origin
msg "Creating pkg repository"
bset status "pkgrepo:"
ensure_pkg_installed force_extract || \
err 1 "Unable to extract pkg."
if [ -r "${PKG_REPO_META_FILE:-/nonexistent}" ]; then
PKG_META="-m /tmp/pkgmeta"
PKG_META_MASTERMNT="-m ${MASTERMNT}/tmp/pkgmeta"
install -m 0400 "${PKG_REPO_META_FILE}" \
${MASTERMNT}/tmp/pkgmeta
fi
mkdir -p ${MASTERMNT}/tmp/packages
if [ -n "${PKG_REPO_SIGNING_KEY}" ]; then
install -m 0400 ${PKG_REPO_SIGNING_KEY} \
${MASTERMNT}/tmp/repo.key
injail ${PKG_BIN} repo -o /tmp/packages \
${PKG_META} \
/packages /tmp/repo.key
rm -f ${MASTERMNT}/tmp/repo.key
elif [ "${PKG_REPO_FROM_HOST:-no}" = "yes" ]; then
# Sometimes building repo from host is needed if
# using SSH with DNSSEC as older hosts don't support
# it.
${MASTERMNT}${PKG_BIN} repo \
-o ${MASTERMNT}/tmp/packages ${PKG_META_MASTERMNT} \
${MASTERMNT}/packages \
${SIGNING_COMMAND:+signing_command: ${SIGNING_COMMAND}}
else
JNETNAME="n" injail ${PKG_BIN} repo \
-o /tmp/packages ${PKG_META} /packages \
${SIGNING_COMMAND:+signing_command: ${SIGNING_COMMAND}}
fi
cp ${MASTERMNT}/tmp/packages/* ${PACKAGES}/
# Sign the ports-mgmt/pkg package for bootstrap
if [ -e "${PACKAGES}/Latest/pkg.txz" ]; then
if [ -n "${SIGNING_COMMAND}" ]; then
sign_pkg fingerprint "${PACKAGES}/Latest/pkg.txz"
elif [ -n "${PKG_REPO_SIGNING_KEY}" ]; then
sign_pkg pubkey "${PACKAGES}/Latest/pkg.txz"
fi
fi
}
# Builtin-only functions
_BUILTIN_ONLY=""
for _var in ${_BUILTIN_ONLY}; do
if ! [ "$(type ${_var} 2>/dev/null)" = \
"${_var} is a shell builtin" ]; then
eval "${_var}() { return 0; }"
fi
done
if [ "$(type setproctitle 2>/dev/null)" = "setproctitle is a shell builtin" ]; then
setproctitle() {
PROC_TITLE="$@"
command setproctitle "poudriere${MASTERNAME:+[${MASTERNAME}]}${MY_JOBID:+[${MY_JOBID}]}: $@"
}
else
setproctitle() { }
fi
RESOLV_CONF=""
STATUS=0 # out of jail #
# cd into / to avoid foot-shooting if running from deleted dirs or
# NFS dir which root has no access to.
SAVED_PWD="${PWD}"
cd /
. ${SCRIPTPREFIX}/include/colors.pre.sh
[ -z "${POUDRIERE_ETC}" ] &&
POUDRIERE_ETC=$(realpath ${SCRIPTPREFIX}/../../etc)
# If this is a relative path, add in ${PWD} as a cd / is done.
[ "${POUDRIERE_ETC#/}" = "${POUDRIERE_ETC}" ] && \
POUDRIERE_ETC="${SAVED_PWD}/${POUDRIERE_ETC}"
POUDRIERED=${POUDRIERE_ETC}/poudriere.d
if [ -r "${POUDRIERE_ETC}/poudriere.conf" ]; then
. "${POUDRIERE_ETC}/poudriere.conf"
elif [ -r "${POUDRIERED}/poudriere.conf" ]; then
. "${POUDRIERED}/poudriere.conf"
else
err 1 "Unable to find a readable poudriere.conf in ${POUDRIERE_ETC} or ${POUDRIERED}"
fi
include_poudriere_confs "$@"
AWKPREFIX=${SCRIPTPREFIX}/awk
HTMLPREFIX=${SCRIPTPREFIX}/html
HOOKDIR=${POUDRIERED}/hooks
# If the zfs module is not loaded it means we can't have zfs
[ -z "${NO_ZFS}" ] && lsvfs zfs >/dev/null 2>&1 || NO_ZFS=yes
# Short circuit to prevent running zpool(1) and loading zfs.ko
[ -z "${NO_ZFS}" ] && [ -z "$(zpool list -H -o name 2>/dev/null)" ] && NO_ZFS=yes
[ -z "${NO_ZFS}" -a -z ${ZPOOL} ] && err 1 "ZPOOL variable is not set"
[ -z ${BASEFS} ] && err 1 "Please provide a BASEFS variable in your poudriere.conf"
trap sigpipe_handler SIGPIPE
trap sigint_handler SIGINT
trap sigterm_handler SIGTERM
trap exit_handler EXIT
enable_siginfo_handler() {
was_a_bulk_run && trap siginfo_handler SIGINFO
in_siginfo_handler=0
return 0
}
enable_siginfo_handler
# Test if zpool exists
if [ -z "${NO_ZFS}" ]; then
zpool list ${ZPOOL} >/dev/null 2>&1 || err 1 "No such zpool: ${ZPOOL}"
fi
: ${SVN_HOST="svn.freebsd.org"}
: ${GIT_BASEURL="github.com/freebsd/freebsd.git"}
: ${GIT_PORTSURL="github.com/freebsd/freebsd-ports.git"}
: ${FREEBSD_HOST="https://download.FreeBSD.org"}
if [ -z "${NO_ZFS}" ]; then
: ${ZROOTFS="/poudriere"}
case ${ZROOTFS} in
[!/]*) err 1 "ZROOTFS shoud start with a /" ;;
esac
fi
HOST_OSVERSION="$(sysctl -n kern.osreldate)"
if [ -z "${NO_ZFS}" -a -z "${ZFS_DEADLOCK_IGNORED}" ]; then
[ ${HOST_OSVERSION} -gt 900000 -a \
${HOST_OSVERSION} -le 901502 ] && err 1 \
"FreeBSD 9.1 ZFS is not safe. It is known to deadlock and cause system hang. Either upgrade the host or set ZFS_DEADLOCK_IGNORED=yes in poudriere.conf"
fi
: ${USE_TMPFS:=no}
[ -n "${MFSSIZE}" -a "${USE_TMPFS}" != "no" ] && err 1 "You can't use both tmpfs and mdmfs"
for val in ${USE_TMPFS}; do
case ${val} in
wrkdir) TMPFS_WRKDIR=1 ;;
data) TMPFS_DATA=1 ;;
all) TMPFS_ALL=1 ;;
localbase) TMPFS_LOCALBASE=1 ;;
yes)
TMPFS_WRKDIR=1
TMPFS_DATA=1
;;
no) ;;
*) err 1 "Unknown value for USE_TMPFS can be a combination of wrkdir,data,all,yes,no,localbase" ;;
esac
done
case ${TMPFS_WRKDIR}${TMPFS_DATA}${TMPFS_LOCALBASE}${TMPFS_ALL} in
1**1|*1*1|**11)
TMPFS_WRKDIR=0
TMPFS_DATA=0
TMPFS_LOCALBASE=0
;;
esac
POUDRIERE_DATA=`get_data_dir`
: ${WRKDIR_ARCHIVE_FORMAT="tbz"}
case "${WRKDIR_ARCHIVE_FORMAT}" in
tar|tgz|tbz|txz);;
*) err 1 "invalid format for WRKDIR_ARCHIVE_FORMAT: ${WRKDIR_ARCHIVE_FORMAT}" ;;
esac
#Converting portstree if any
if [ ! -d ${POUDRIERED}/ports ]; then
mkdir -p ${POUDRIERED}/ports
[ -z "${NO_ZFS}" ] && zfs list -t filesystem -H \
-o ${NS}:type,${NS}:name,${NS}:method,mountpoint,name | \
grep "^ports" | \
while read t name method mnt fs; do
msg "Converting the ${name} ports tree"
pset ${name} method ${method}
pset ${name} mnt ${mnt}
pset ${name} fs ${fs}
# Delete the old properties
zfs inherit -r ${NS}:type ${fs}
zfs inherit -r ${NS}:name ${fs}
zfs inherit -r ${NS}:method ${fs}
done
if [ -f ${POUDRIERED}/portstrees ]; then
while read name method mnt; do
[ -z "${name###*}" ] && continue # Skip comments
msg "Converting the ${name} ports tree"
mkdir ${POUDRIERED}/ports/${name}
echo ${method} > ${POUDRIERED}/ports/${name}/method
echo ${mnt} > ${POUDRIERED}/ports/${name}/mnt
done < ${POUDRIERED}/portstrees
rm -f ${POUDRIERED}/portstrees
fi
fi
#Converting jails if any
if [ ! -d ${POUDRIERED}/jails ]; then
mkdir -p ${POUDRIERED}/jails
[ -z "${NO_ZFS}" ] && zfs list -t filesystem -H \
-o ${NS}:type,${NS}:name,${NS}:version,${NS}:arch,${NS}:method,mountpoint,name | \
grep "^rootfs" | \
while read t name version arch method mnt fs; do
msg "Converting the ${name} jail"
jset ${name} version ${version}
jset ${name} arch ${arch}
jset ${name} method ${method}
jset ${name} mnt ${mnt}
jset ${name} fs ${fs}
# Delete the old properties
zfs inherit -r ${NS}:type ${fs}
zfs inherit -r ${NS}:name ${fs}
zfs inherit -r ${NS}:method ${fs}
zfs inherit -r ${NS}:version ${fs}
zfs inherit -r ${NS}:arch ${fs}
zfs inherit -r ${NS}:stats_built ${fs}
zfs inherit -r ${NS}:stats_failed ${fs}
zfs inherit -r ${NS}:stats_skipped ${fs}
zfs inherit -r ${NS}:stats_ignored ${fs}
zfs inherit -r ${NS}:stats_queued ${fs}
zfs inherit -r ${NS}:status ${fs}
done
fi
: ${LOIP6:=::1}
: ${LOIP4:=127.0.0.1}
case $IPS in
01)
localipargs="ip6.addr=${LOIP6}"
ipargs="ip6=inherit"
;;
10)
localipargs="ip4.addr=${LOIP4}"
ipargs="ip4=inherit"
;;
11)
localipargs="ip4.addr=${LOIP4} ip6.addr=${LOIP6}"
ipargs="ip4=inherit ip6=inherit"
;;
esac
NCPU=$(sysctl -n hw.ncpu)
# Check if parallel umount will contend on the vnode free list lock
if sysctl -n vfs.mnt_free_list_batch >/dev/null 2>&1; then
# Nah, parallel umount should be fine.
UMOUNT_BATCHING=1
else
UMOUNT_BATCHING=0
fi
# Determine if umount -n can be used.
if grep -q "#define[[:space:]]MNT_NONBUSY" /usr/include/sys/mount.h \
2>/dev/null; then
UMOUNT_NONBUSY="-n"
fi
case ${PARALLEL_JOBS} in
''|*[!0-9]*)
PARALLEL_JOBS=${NCPU}
;;
esac
case ${POOL_BUCKETS} in
''|*[!0-9]*)
# 1 will auto determine proper size, 0 disables.
POOL_BUCKETS=1
;;
esac
if [ "${PRESERVE_TIMESTAMP:-no}" = "yes" ]; then
SVN_PRESERVE_TIMESTAMP="--config-option config:miscellany:use-commit-times=yes"
fi
: ${WATCHDIR:=${POUDRIERE_DATA}/queue}
: ${PIDFILE:=${POUDRIERE_DATA}/daemon.pid}
: ${QUEUE_SOCKET:=/var/run/poudriered.sock}
: ${PORTBUILD_UID:=65532}
: ${PORTBUILD_GID:=${PORTBUILD_UID}}
: ${PORTBUILD_USER:=nobody}
: ${CCACHE_DIR_NON_ROOT_SAFE:=no}
if [ -n "${CCACHE_DIR}" ] && [ "${CCACHE_DIR_NON_ROOT_SAFE}" = "no" ]; then
if [ "${BUILD_AS_NON_ROOT}" = "yes" ]; then
msg_warn "BUILD_AS_NON_ROOT and CCACHE_DIR are potentially incompatible. Disabling BUILD_AS_NON_ROOT"
msg_warn "Either disable one or set CCACHE_DIR_NON_ROOT_SAFE=yes and chown -R CCACHE_DIR to the user ${PORTBUILD_USER} (uid: ${PORTBUILD_UID})"
fi
# Default off with CCACHE_DIR.
: ${BUILD_AS_NON_ROOT:=no}
fi
# Default on otherwise.
: ${BUILD_AS_NON_ROOT:=yes}
: ${DISTFILES_CACHE:=/nonexistent}
: ${SVN_CMD:=$(which svn 2>/dev/null || which svnlite 2>/dev/null)}
: ${BINMISC:=/usr/sbin/binmiscctl}
# 24 hours for 1 command
: ${MAX_EXECUTION_TIME:=86400}
# 120 minutes with no log update
: ${NOHANG_TIME:=7200}
: ${PATCHED_FS_KERNEL:=no}
: ${ALL:=0}
: ${CLEAN:=0}
: ${CLEAN_LISTED:=0}
: ${JAIL_NEEDS_CLEAN:=0}
: ${VERBOSE:=0}
: ${QEMU_EMULATING:=0}
: ${PORTTESTING_FATAL:=yes}
: ${PORTTESTING_RECURSIVE:=0}
: ${PRIORITY_BOOST_VALUE:=99}
: ${RESTRICT_NETWORKING:=yes}
: ${TRIM_ORPHANED_BUILD_DEPS:=yes}
: ${USE_JEXECD:=no}
: ${USE_PROCFS:=yes}
: ${USE_FDESCFS:=yes}
: ${USE_PTSORT:=yes}
: ${MUTABLE_BASE:=yes}
: ${HTML_JSON_UPDATE_INTERVAL:=2}
: ${HTML_TRACK_REMAINING:=no}
# Be sure to update poudriere.conf to document the default when changing these
: ${MAX_EXECUTION_TIME:=86400} # 24 hours for 1 command
: ${NOHANG_TIME:=7200} # 120 minutes with no log update
: ${TIMESTAMP_LOGS:=no}
: ${ATOMIC_PACKAGE_REPOSITORY:=yes}
: ${KEEP_OLD_PACKAGES:=no}
: ${KEEP_OLD_PACKAGES_COUNT:=5}
: ${COMMIT_PACKAGES_ON_FAILURE:=yes}
: ${SAVE_WRKDIR:=no}
: ${CHECK_CHANGED_DEPS:=yes}
: ${CHECK_CHANGED_OPTIONS:=verbose}
: ${NO_RESTRICTED:=no}
: ${USE_COLORS:=yes}
: ${ALLOW_MAKE_JOBS_PACKAGES=pkg ccache}
: ${POUDRIERE_TMPDIR:=$(command mktemp -dt poudriere)}
: ${SHASH_VAR_PATH_DEFAULT:=${POUDRIERE_TMPDIR}}
: ${SHASH_VAR_PATH:=${SHASH_VAR_PATH_DEFAULT}}
: ${SHASH_VAR_PREFIX:=sh-}
: ${USE_CACHED:=no}
: ${BUILDNAME_FORMAT:="%Y-%m-%d_%Hh%Mm%Ss"}
: ${BUILDNAME:=$(date +${BUILDNAME_FORMAT})}
: ${HTML_TYPE:=inline}
if [ -n "${MAX_MEMORY}" ]; then
MAX_MEMORY_BYTES="$((${MAX_MEMORY} * 1024 * 1024 * 1024))"
fi
: ${MAX_FILES:=1024}
TIME_START=$(clock -monotonic)
EPOCH_START=$(clock -epoch)
[ -d ${WATCHDIR} ] || mkdir -p ${WATCHDIR}
. ${SCRIPTPREFIX}/include/util.sh
. ${SCRIPTPREFIX}/include/colors.sh
. ${SCRIPTPREFIX}/include/display.sh
. ${SCRIPTPREFIX}/include/html.sh
. ${SCRIPTPREFIX}/include/parallel.sh
. ${SCRIPTPREFIX}/include/hash.sh
. ${SCRIPTPREFIX}/include/shared_hash.sh
. ${SCRIPTPREFIX}/include/cache.sh
. ${SCRIPTPREFIX}/include/fs.sh
if [ -e /nonexistent ]; then
err 1 "You may not have a /nonexistent. Please remove it."
fi
|